Jason Sams | 536923d | 2010-05-18 13:35:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 "rsContext.h" |
| 18 | #include "rsScriptC.h" |
| 19 | |
| 20 | // Implements rs_cl.rsh |
| 21 | |
| 22 | |
| 23 | using namespace android; |
| 24 | using namespace android::renderscript; |
| 25 | |
| 26 | |
| 27 | static float SC_acospi(float v) { |
| 28 | return acosf(v)/ M_PI; |
| 29 | } |
| 30 | |
| 31 | static float SC_asinpi(float v) { |
| 32 | return asinf(v) / M_PI; |
| 33 | } |
| 34 | |
| 35 | static float SC_atanpi(float v) { |
| 36 | return atanf(v) / M_PI; |
| 37 | } |
| 38 | |
| 39 | static float SC_atan2pi(float y, float x) { |
| 40 | return atan2f(y, x) / M_PI; |
| 41 | } |
| 42 | |
| 43 | static float SC_cospi(float v) { |
| 44 | return cosf(v * M_PI); |
| 45 | } |
| 46 | |
| 47 | static float SC_exp10(float v) { |
| 48 | return pow(10.f, v); |
| 49 | |
| 50 | } |
| 51 | |
| 52 | static float SC_fract(float v, int *iptr) { |
| 53 | int i = (int)floor(v); |
| 54 | iptr[0] = i; |
| 55 | return fmin(v - i, 0x1.fffffep-1f); |
| 56 | } |
| 57 | |
| 58 | static float SC_log2(float v) { |
| 59 | return log10(v) / log10(2.f); |
| 60 | } |
| 61 | |
| 62 | static float SC_pown(float v, int p) { |
| 63 | return powf(v, (float)p); |
| 64 | } |
| 65 | |
| 66 | static float SC_powr(float v, float p) { |
| 67 | return powf(v, p); |
| 68 | } |
| 69 | |
| 70 | float SC_rootn(float v, int r) { |
| 71 | return pow(v, 1.f / r); |
| 72 | } |
| 73 | |
| 74 | float SC_rsqrt(float v) { |
| 75 | return 1.f / sqrtf(v); |
| 76 | } |
| 77 | |
| 78 | float SC_sincos(float v, float *cosptr) { |
| 79 | *cosptr = cosf(v); |
| 80 | return sinf(v); |
| 81 | } |
| 82 | |
| 83 | static float SC_sinpi(float v) { |
| 84 | return sinf(v * M_PI); |
| 85 | } |
| 86 | |
| 87 | static float SC_tanpi(float v) { |
| 88 | return tanf(v * M_PI); |
| 89 | } |
| 90 | |
| 91 | //{ "logb", (void *)& }, |
| 92 | //{ "mad", (void *)& }, |
| 93 | //{ "nan", (void *)& }, |
| 94 | //{ "tgamma", (void *)& }, |
| 95 | |
| 96 | ////////////////////////////////////////////////////////////////////////////// |
| 97 | // Integer |
| 98 | ////////////////////////////////////////////////////////////////////////////// |
| 99 | |
| 100 | |
| 101 | static uint32_t SC_abs_i32(int32_t v) {return abs(v);} |
| 102 | static uint16_t SC_abs_i16(int16_t v) {return (uint16_t)abs(v);} |
| 103 | static uint8_t SC_abs_i8(int8_t v) {return (uint8_t)abs(v);} |
| 104 | |
| 105 | static uint32_t SC_clz_u32(uint32_t v) {return __builtin_clz(v);} |
| 106 | static uint16_t SC_clz_u16(uint16_t v) {return (uint16_t)__builtin_clz(v);} |
| 107 | static uint8_t SC_clz_u8(uint8_t v) {return (uint8_t)__builtin_clz(v);} |
| 108 | static int32_t SC_clz_i32(int32_t v) {return (int32_t)__builtin_clz((uint32_t)v);} |
| 109 | static int16_t SC_clz_i16(int16_t v) {return (int16_t)__builtin_clz(v);} |
| 110 | static int8_t SC_clz_i8(int8_t v) {return (int8_t)__builtin_clz(v);} |
| 111 | |
| 112 | static uint32_t SC_max_u32(uint32_t v, uint32_t v2) {return rsMax(v, v2);} |
| 113 | static uint16_t SC_max_u16(uint16_t v, uint16_t v2) {return rsMax(v, v2);} |
| 114 | static uint8_t SC_max_u8(uint8_t v, uint8_t v2) {return rsMax(v, v2);} |
| 115 | static int32_t SC_max_i32(int32_t v, int32_t v2) {return rsMax(v, v2);} |
| 116 | static int16_t SC_max_i16(int16_t v, int16_t v2) {return rsMax(v, v2);} |
| 117 | static int8_t SC_max_i8(int8_t v, int8_t v2) {return rsMax(v, v2);} |
| 118 | |
| 119 | static uint32_t SC_min_u32(uint32_t v, uint32_t v2) {return rsMin(v, v2);} |
| 120 | static uint16_t SC_min_u16(uint16_t v, uint16_t v2) {return rsMin(v, v2);} |
| 121 | static uint8_t SC_min_u8(uint8_t v, uint8_t v2) {return rsMin(v, v2);} |
| 122 | static int32_t SC_min_i32(int32_t v, int32_t v2) {return rsMin(v, v2);} |
| 123 | static int16_t SC_min_i16(int16_t v, int16_t v2) {return rsMin(v, v2);} |
| 124 | static int8_t SC_min_i8(int8_t v, int8_t v2) {return rsMin(v, v2);} |
| 125 | |
| 126 | ////////////////////////////////////////////////////////////////////////////// |
| 127 | // Float util |
| 128 | ////////////////////////////////////////////////////////////////////////////// |
| 129 | |
| 130 | static float SC_clamp_f32(float amount, float low, float high) |
| 131 | { |
| 132 | return amount < low ? low : (amount > high ? high : amount); |
| 133 | } |
| 134 | |
| 135 | static float SC_degrees(float radians) |
| 136 | { |
| 137 | return radians * (180.f / M_PI); |
| 138 | } |
| 139 | |
| 140 | static float SC_max_f32(float v, float v2) |
| 141 | { |
| 142 | return rsMax(v, v2); |
| 143 | } |
| 144 | |
| 145 | static float SC_min_f32(float v, float v2) |
| 146 | { |
| 147 | return rsMin(v, v2); |
| 148 | } |
| 149 | |
| 150 | static float SC_mix_f32(float start, float stop, float amount) |
| 151 | { |
| 152 | //LOGE("lerpf %f %f %f", start, stop, amount); |
| 153 | return start + (stop - start) * amount; |
| 154 | } |
| 155 | |
| 156 | static float SC_radians(float degrees) |
| 157 | { |
| 158 | return degrees * (M_PI / 180.f); |
| 159 | } |
| 160 | |
| 161 | static float SC_step_f32(float edge, float v) |
| 162 | { |
| 163 | if (v < edge) return 0.f; |
| 164 | return 1.f; |
| 165 | } |
| 166 | |
| 167 | static float SC_sign_f32(float value) |
| 168 | { |
| 169 | if (value > 0) return 1.f; |
| 170 | if (value < 0) return -1.f; |
| 171 | return value; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | ////////////////////////////////////////////////////////////////////////////// |
| 179 | // Class implementation |
| 180 | ////////////////////////////////////////////////////////////////////////////// |
| 181 | |
| 182 | // llvm name mangling ref |
| 183 | // <builtin-type> ::= v # void |
| 184 | // ::= b # bool |
| 185 | // ::= c # char |
| 186 | // ::= a # signed char |
| 187 | // ::= h # unsigned char |
| 188 | // ::= s # short |
| 189 | // ::= t # unsigned short |
| 190 | // ::= i # int |
| 191 | // ::= j # unsigned int |
| 192 | // ::= l # long |
| 193 | // ::= m # unsigned long |
| 194 | // ::= x # long long, __int64 |
| 195 | // ::= y # unsigned long long, __int64 |
| 196 | // ::= f # float |
| 197 | // ::= d # double |
| 198 | |
| 199 | static ScriptCState::SymbolTable_t gSyms[] = { |
| 200 | // OpenCL math |
| 201 | { "_Z4acosf", (void *)&acosf }, |
| 202 | { "_Z5acoshf", (void *)&acoshf }, |
| 203 | { "_Z6acospif", (void *)&SC_acospi }, |
| 204 | { "_Z4asinf", (void *)&asinf }, |
| 205 | { "_Z5asinhf", (void *)&asinhf }, |
| 206 | { "_Z6asinpif", (void *)&SC_asinpi }, |
| 207 | { "_Z4atanf", (void *)&atanf }, |
| 208 | { "_Z5atan2f", (void *)&atan2f }, |
| 209 | { "_Z6atanpif", (void *)&SC_atanpi }, |
| 210 | { "_Z7atan2pif", (void *)&SC_atan2pi }, |
| 211 | { "_Z4cbrtf", (void *)&cbrtf }, |
| 212 | { "_Z4ceilf", (void *)&ceilf }, |
| 213 | { "_Z8copysignff", (void *)©signf }, |
| 214 | { "_Z3cosf", (void *)&cosf }, |
| 215 | { "_Z4coshf", (void *)&coshf }, |
| 216 | { "_Z5cospif", (void *)&SC_cospi }, |
| 217 | { "_Z4erfcf", (void *)&erfcf }, |
| 218 | { "_Z3erff", (void *)&erff }, |
| 219 | { "_Z3expf", (void *)&expf }, |
| 220 | { "_Z4exp2f", (void *)&exp2f }, |
| 221 | { "_Z5exp10f", (void *)&SC_exp10 }, |
| 222 | { "_Z5expm1f", (void *)&expm1f }, |
| 223 | { "_Z4fabsf", (void *)&fabsf }, |
| 224 | { "_Z4fdimff", (void *)&fdimf }, |
| 225 | { "_Z5floorf", (void *)&floorf }, |
| 226 | { "_Z3fmafff", (void *)&fmaf }, |
| 227 | { "_Z4fmaxff", (void *)&fmaxf }, |
| 228 | { "_Z4fminff", (void *)&fminf }, // float fmin(float, float) |
| 229 | { "_Z4fmodff", (void *)&fmodf }, |
| 230 | { "_Z5fractfPf", (void *)&SC_fract }, |
| 231 | { "_Z5frexpfPi", (void *)&frexpf }, |
| 232 | { "_Z5hypotff", (void *)&hypotf }, |
| 233 | { "_Z5ilogbf", (void *)&ilogbf }, |
| 234 | { "_Z5ldexpfi", (void *)&ldexpf }, |
| 235 | { "_Z6lgammaf", (void *)&lgammaf }, |
| 236 | { "_Z3logf", (void *)&logf }, |
| 237 | { "_Z4log2f", (void *)&SC_log2 }, |
| 238 | { "_Z5log10f", (void *)&log10f }, |
| 239 | { "_Z5log1pf", (void *)&log1pf }, |
| 240 | //{ "logb", (void *)& }, |
| 241 | //{ "mad", (void *)& }, |
| 242 | { "modf", (void *)&modff }, |
| 243 | //{ "nan", (void *)& }, |
| 244 | { "_Z9nextafterff", (void *)&nextafterf }, |
| 245 | { "_Z3powff", (void *)&powf }, |
| 246 | { "_Z4pownfi", (void *)&SC_pown }, |
| 247 | { "_Z4powrff", (void *)&SC_powr }, |
| 248 | { "_Z9remainderff", (void *)&remainderf }, |
| 249 | { "remquo", (void *)&remquof }, |
| 250 | { "_Z4rintf", (void *)&rintf }, |
| 251 | { "_Z5rootnfi", (void *)&SC_rootn }, |
| 252 | { "_Z5roundf", (void *)&roundf }, |
| 253 | { "_Z5rsqrtf", (void *)&SC_rsqrt }, |
| 254 | { "_Z3sinf", (void *)&sinf }, |
| 255 | { "sincos", (void *)&SC_sincos }, |
| 256 | { "_Z4sinhf", (void *)&sinhf }, |
| 257 | { "_Z5sinpif", (void *)&SC_sinpi }, |
| 258 | { "_Z4sqrtf", (void *)&sqrtf }, |
| 259 | { "_Z3tanf", (void *)&tanf }, |
| 260 | { "_Z4tanhf", (void *)&tanhf }, |
| 261 | { "_Z5tanpif", (void *)&SC_tanpi }, |
| 262 | //{ "tgamma", (void *)& }, |
| 263 | { "_Z5truncf", (void *)&truncf }, |
| 264 | |
| 265 | // OpenCL Int |
| 266 | { "_Z3absi", (void *)&SC_abs_i32 }, |
| 267 | { "_Z3abss", (void *)&SC_abs_i16 }, |
| 268 | { "_Z3absc", (void *)&SC_abs_i8 }, |
| 269 | { "_Z3clzj", (void *)&SC_clz_u32 }, |
| 270 | { "_Z3clzt", (void *)&SC_clz_u16 }, |
| 271 | { "_Z3clzh", (void *)&SC_clz_u8 }, |
| 272 | { "_Z3clzi", (void *)&SC_clz_i32 }, |
| 273 | { "_Z3clzs", (void *)&SC_clz_i16 }, |
| 274 | { "_Z3clzc", (void *)&SC_clz_i8 }, |
| 275 | { "_Z3maxjj", (void *)&SC_max_u32 }, |
| 276 | { "_Z3maxtt", (void *)&SC_max_u16 }, |
| 277 | { "_Z3maxhh", (void *)&SC_max_u8 }, |
| 278 | { "_Z3maxii", (void *)&SC_max_i32 }, |
| 279 | { "_Z3maxss", (void *)&SC_max_i16 }, |
| 280 | { "_Z3maxcc", (void *)&SC_max_i8 }, |
| 281 | { "_Z3minjj", (void *)&SC_min_u32 }, |
| 282 | { "_Z3mintt", (void *)&SC_min_u16 }, |
| 283 | { "_Z3minhh", (void *)&SC_min_u8 }, |
| 284 | { "_Z3minii", (void *)&SC_min_i32 }, |
| 285 | { "_Z3minss", (void *)&SC_min_i16 }, |
| 286 | { "_Z3mincc", (void *)&SC_min_i8 }, |
| 287 | |
| 288 | // OpenCL 6.11.4 |
| 289 | { "_Z5clampfff", (void *)&SC_clamp_f32 }, |
| 290 | { "_Z7degreesf", (void *)&SC_degrees }, |
| 291 | { "_Z3maxff", (void *)&SC_max_f32 }, |
| 292 | { "_Z3minff", (void *)&SC_min_f32 }, |
| 293 | { "_Z3mixfff", (void *)&SC_mix_f32 }, |
| 294 | { "_Z7radiansf", (void *)&SC_radians }, |
| 295 | { "_Z4stepff", (void *)&SC_step_f32 }, |
| 296 | //{ "smoothstep", (void *)& }, |
| 297 | { "_Z4signf", (void *)&SC_sign_f32 }, |
| 298 | |
| 299 | { NULL, NULL } |
| 300 | }; |
| 301 | |
| 302 | const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolCL(const char *sym) |
| 303 | { |
| 304 | ScriptCState::SymbolTable_t *syms = gSyms; |
| 305 | |
| 306 | while (syms->mPtr) { |
| 307 | if (!strcmp(syms->mName, sym)) { |
| 308 | return syms; |
| 309 | } |
| 310 | syms++; |
| 311 | } |
| 312 | return NULL; |
| 313 | } |
| 314 | |