fptest.cpp revision 3cadc3a6df0b13deabb1b3423aafa5ff8bbfdf23
1/* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include <stdio.h> 18#include <stdlib.h> 19#include <string.h> 20#include <sys/time.h> 21#include <time.h> 22#include <unistd.h> 23#include <sched.h> 24#include <sys/resource.h> 25#include <sys/syscall.h> 26#include <sys/types.h> 27#include <sys/mman.h> 28 29typedef struct { 30 unsigned char x; 31 unsigned char y; 32 unsigned char z; 33 unsigned char w; 34} uchar4; 35 36typedef struct { 37 float x; 38 float y; 39 float z; 40 float w; 41} float4; 42 43 44#define MAX_RADIUS 25 45#define WIDTH 512 46#define HEIGHT 512 47 48uchar4 bufIn[WIDTH * HEIGHT]; 49uchar4 bufOut[WIDTH * HEIGHT]; 50uchar4 bufTmp[WIDTH * HEIGHT]; 51 52static float gaussian[MAX_RADIUS * 2 + 1]; 53static int height = 512; 54static int width = 512; 55static int radius = MAX_RADIUS; 56 57 58 59static void horiz(uchar4 *output, const uchar4 *inputBuf, uint32_t x, uint32_t y) { 60 const uchar4 *input = inputBuf + (y * width); 61 float4 blurredPixel = {0,0,0,0}; 62 float4 currentPixel; 63 64 for(int r = -radius; r <= radius; r ++) { 65 // Stepping left and right away from the pixel 66 int validW = x + r; 67 // Clamp to zero and width max() isn't exposed for ints yet 68 if(validW < 0) { 69 validW = 0; 70 } 71 if(validW > WIDTH - 1) { 72 validW = WIDTH - 1; 73 } 74 //int validW = rsClamp(w + r, 0, width - 1); 75 76 float weight = gaussian[r + radius]; 77 currentPixel.x = (float)(input[validW].x); 78 currentPixel.y = (float)(input[validW].y); 79 currentPixel.z = (float)(input[validW].z); 80 //currentPixel.w = (float)(input->a); 81 82 blurredPixel.x += currentPixel.x * weight; 83 blurredPixel.y += currentPixel.y * weight; 84 blurredPixel.z += currentPixel.z * weight; 85 } 86 87 output->x = (uint8_t)blurredPixel.x; 88 output->y = (uint8_t)blurredPixel.y; 89 output->z = (uint8_t)blurredPixel.z; 90} 91 92 93static void vert(uchar4 *output, const uchar4 *inputBuf, uint32_t x, uint32_t y) { 94 const uchar4 *input = inputBuf + x; 95 96 float4 blurredPixel = {0,0,0,0}; 97 98 float4 currentPixel; 99 for(int r = -radius; r <= radius; r ++) { 100 int validH = y + r; 101 // Clamp to zero and width 102 if(validH < 0) { 103 validH = 0; 104 } 105 if(validH > HEIGHT - 1) { 106 validH = HEIGHT - 1; 107 } 108 109 const uchar4 *i = input + validH * WIDTH; 110 111 float weight = gaussian[r + radius]; 112 113 currentPixel.x = (float)(i->x); 114 currentPixel.y = (float)(i->y); 115 currentPixel.z = (float)(i->z); 116 117 blurredPixel.x += currentPixel.x * weight; 118 blurredPixel.y += currentPixel.y * weight; 119 blurredPixel.z += currentPixel.z * weight; 120 } 121 122 //output->xyz = convert_uchar3(blurredPixel.xyz); 123 output->x = (uint8_t)blurredPixel.x; 124 output->y = (uint8_t)blurredPixel.y; 125 output->z = (uint8_t)blurredPixel.z; 126} 127 128 129typedef long long nsecs_t; 130 131static nsecs_t system_time() 132{ 133 struct timespec t; 134 t.tv_sec = t.tv_nsec = 0; 135 clock_gettime(CLOCK_MONOTONIC, &t); 136 return nsecs_t(t.tv_sec)*1000000000LL + t.tv_nsec; 137} 138 139int fp_test(int argc, char** argv) { 140 for (int ct=0; ct < (sizeof(gaussian)/4); ct++) { 141 gaussian[ct] = 1.f; 142 } 143 memset(bufIn, 0, sizeof(bufIn)); 144 145 nsecs_t t1 = system_time(); 146 147 for (int y = 0; y < HEIGHT; y++) { 148 for (int x = 0; x < WIDTH; x++) { 149 horiz(&bufTmp[x + y * WIDTH], bufIn, x, y); 150 } 151 } 152 153 for (int y = 0; y < HEIGHT; y++) { 154 for (int x = 0; x < WIDTH; x++) { 155 vert(&bufOut[x + y * WIDTH], bufTmp, x, y); 156 } 157 } 158 159 nsecs_t t2 = system_time(); 160 161 printf("FP Test time %i ms\n", (int)((t2 - t1) / 1000000) ); 162 163 return 0; 164} 165 166