blob: acb990d8c78be7cd9a84b3c7fbab18e6da04c28f [file] [log] [blame]
Jason Samsfcf72312011-04-20 15:09:01 -07001/*
Alex Sakhartchouk4a36b452011-04-29 16:49:08 -07002 * Copyright (C) 2011 The Android Open Source Project
Jason Samsfcf72312011-04-20 15:09:01 -07003 *
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#include "rsMatrix4x4.h"
20#include "rsMatrix3x3.h"
21#include "rsMatrix2x2.h"
22
23#include "rsdCore.h"
24#include "rsdRuntime.h"
25
26
27using namespace android;
28using namespace android::renderscript;
29
30
31static float SC_exp10(float v) {
32 return pow(10.f, v);
33}
34
35static float SC_fract(float v, int *iptr) {
36 int i = (int)floor(v);
37 iptr[0] = i;
38 return fmin(v - i, 0x1.fffffep-1f);
39}
40
41static float SC_log2(float v) {
42 return log10(v) / log10(2.f);
43}
44
45static float SC_mad(float v1, float v2, float v3) {
46 return v1 * v2 + v3;
47}
48
49#if 0
50static float SC_pown(float v, int p) {
51 return powf(v, (float)p);
52}
53
54static float SC_powr(float v, float p) {
55 return powf(v, p);
56}
57#endif
58
59float SC_rootn(float v, int r) {
60 return pow(v, 1.f / r);
61}
62
63float SC_rsqrt(float v) {
64 return 1.f / sqrtf(v);
65}
66
67float SC_sincos(float v, float *cosptr) {
68 *cosptr = cosf(v);
69 return sinf(v);
70}
71
72//////////////////////////////////////////////////////////////////////////////
73// Integer
74//////////////////////////////////////////////////////////////////////////////
75
76
77static uint32_t SC_abs_i32(int32_t v) {return abs(v);}
78static uint16_t SC_abs_i16(int16_t v) {return (uint16_t)abs(v);}
79static uint8_t SC_abs_i8(int8_t v) {return (uint8_t)abs(v);}
80
81static uint32_t SC_clz_u32(uint32_t v) {return __builtin_clz(v);}
82static uint16_t SC_clz_u16(uint16_t v) {return (uint16_t)__builtin_clz(v);}
83static uint8_t SC_clz_u8(uint8_t v) {return (uint8_t)__builtin_clz(v);}
84static int32_t SC_clz_i32(int32_t v) {return (int32_t)__builtin_clz((uint32_t)v);}
85static int16_t SC_clz_i16(int16_t v) {return (int16_t)__builtin_clz(v);}
86static int8_t SC_clz_i8(int8_t v) {return (int8_t)__builtin_clz(v);}
87
88static uint32_t SC_max_u32(uint32_t v, uint32_t v2) {return rsMax(v, v2);}
89static uint16_t SC_max_u16(uint16_t v, uint16_t v2) {return rsMax(v, v2);}
90static uint8_t SC_max_u8(uint8_t v, uint8_t v2) {return rsMax(v, v2);}
91static int32_t SC_max_i32(int32_t v, int32_t v2) {return rsMax(v, v2);}
92static int16_t SC_max_i16(int16_t v, int16_t v2) {return rsMax(v, v2);}
93static int8_t SC_max_i8(int8_t v, int8_t v2) {return rsMax(v, v2);}
94
95static uint32_t SC_min_u32(uint32_t v, uint32_t v2) {return rsMin(v, v2);}
96static uint16_t SC_min_u16(uint16_t v, uint16_t v2) {return rsMin(v, v2);}
97static uint8_t SC_min_u8(uint8_t v, uint8_t v2) {return rsMin(v, v2);}
98static int32_t SC_min_i32(int32_t v, int32_t v2) {return rsMin(v, v2);}
99static int16_t SC_min_i16(int16_t v, int16_t v2) {return rsMin(v, v2);}
100static int8_t SC_min_i8(int8_t v, int8_t v2) {return rsMin(v, v2);}
101
102//////////////////////////////////////////////////////////////////////////////
103// Float util
104//////////////////////////////////////////////////////////////////////////////
105
106static float SC_clamp_f32(float amount, float low, float high) {
107 return amount < low ? low : (amount > high ? high : amount);
108}
109
110static float SC_degrees(float radians) {
111 return radians * (180.f / M_PI);
112}
113
114static float SC_max_f32(float v, float v2) {
115 return rsMax(v, v2);
116}
117
118static float SC_min_f32(float v, float v2) {
119 return rsMin(v, v2);
120}
121
122static float SC_mix_f32(float start, float stop, float amount) {
123 //LOGE("lerpf %f %f %f", start, stop, amount);
124 return start + (stop - start) * amount;
125}
126
127static float SC_radians(float degrees) {
128 return degrees * (M_PI / 180.f);
129}
130
131static float SC_step_f32(float edge, float v) {
132 if (v < edge) return 0.f;
133 return 1.f;
134}
135
136static float SC_sign_f32(float value) {
137 if (value > 0) return 1.f;
138 if (value < 0) return -1.f;
139 return value;
140}
141
142static void SC_MatrixLoadIdentity_4x4(Matrix4x4 *m) {
143 m->loadIdentity();
144}
145static void SC_MatrixLoadIdentity_3x3(Matrix3x3 *m) {
146 m->loadIdentity();
147}
148static void SC_MatrixLoadIdentity_2x2(Matrix2x2 *m) {
149 m->loadIdentity();
150}
151
152static void SC_MatrixLoad_4x4_f(Matrix4x4 *m, const float *f) {
153 m->load(f);
154}
155static void SC_MatrixLoad_3x3_f(Matrix3x3 *m, const float *f) {
156 m->load(f);
157}
158static void SC_MatrixLoad_2x2_f(Matrix2x2 *m, const float *f) {
159 m->load(f);
160}
161
162static void SC_MatrixLoad_4x4_4x4(Matrix4x4 *m, const Matrix4x4 *s) {
163 m->load(s);
164}
165static void SC_MatrixLoad_4x4_3x3(Matrix4x4 *m, const Matrix3x3 *s) {
166 m->load(s);
167}
168static void SC_MatrixLoad_4x4_2x2(Matrix4x4 *m, const Matrix2x2 *s) {
169 m->load(s);
170}
171static void SC_MatrixLoad_3x3_3x3(Matrix3x3 *m, const Matrix3x3 *s) {
172 m->load(s);
173}
174static void SC_MatrixLoad_2x2_2x2(Matrix2x2 *m, const Matrix2x2 *s) {
175 m->load(s);
176}
177
178static void SC_MatrixLoadRotate(Matrix4x4 *m, float rot, float x, float y, float z) {
179 m->loadRotate(rot, x, y, z);
180}
181static void SC_MatrixLoadScale(Matrix4x4 *m, float x, float y, float z) {
182 m->loadScale(x, y, z);
183}
184static void SC_MatrixLoadTranslate(Matrix4x4 *m, float x, float y, float z) {
185 m->loadTranslate(x, y, z);
186}
187static void SC_MatrixRotate(Matrix4x4 *m, float rot, float x, float y, float z) {
188 m->rotate(rot, x, y, z);
189}
190static void SC_MatrixScale(Matrix4x4 *m, float x, float y, float z) {
191 m->scale(x, y, z);
192}
193static void SC_MatrixTranslate(Matrix4x4 *m, float x, float y, float z) {
194 m->translate(x, y, z);
195}
196
197static void SC_MatrixLoadMultiply_4x4_4x4_4x4(Matrix4x4 *m, const Matrix4x4 *lhs, const Matrix4x4 *rhs) {
198 m->loadMultiply(lhs, rhs);
199}
200static void SC_MatrixLoadMultiply_3x3_3x3_3x3(Matrix3x3 *m, const Matrix3x3 *lhs, const Matrix3x3 *rhs) {
201 m->loadMultiply(lhs, rhs);
202}
203static void SC_MatrixLoadMultiply_2x2_2x2_2x2(Matrix2x2 *m, const Matrix2x2 *lhs, const Matrix2x2 *rhs) {
204 m->loadMultiply(lhs, rhs);
205}
206
207static void SC_MatrixMultiply_4x4_4x4(Matrix4x4 *m, const Matrix4x4 *rhs) {
208 m->multiply(rhs);
209}
210static void SC_MatrixMultiply_3x3_3x3(Matrix3x3 *m, const Matrix3x3 *rhs) {
211 m->multiply(rhs);
212}
213static void SC_MatrixMultiply_2x2_2x2(Matrix2x2 *m, const Matrix2x2 *rhs) {
214 m->multiply(rhs);
215}
216
217static void SC_MatrixLoadOrtho(Matrix4x4 *m, float l, float r, float b, float t, float n, float f) {
218 m->loadOrtho(l, r, b, t, n, f);
219}
220static void SC_MatrixLoadFrustum(Matrix4x4 *m, float l, float r, float b, float t, float n, float f) {
221 m->loadFrustum(l, r, b, t, n, f);
222}
223static void SC_MatrixLoadPerspective(Matrix4x4 *m, float fovy, float aspect, float near, float far) {
224 m->loadPerspective(fovy, aspect, near, far);
225}
226
227static bool SC_MatrixInverse_4x4(Matrix4x4 *m) {
228 return m->inverse();
229}
230static bool SC_MatrixInverseTranspose_4x4(Matrix4x4 *m) {
231 return m->inverseTranspose();
232}
233static void SC_MatrixTranspose_4x4(Matrix4x4 *m) {
234 m->transpose();
235}
236static void SC_MatrixTranspose_3x3(Matrix3x3 *m) {
237 m->transpose();
238}
239static void SC_MatrixTranspose_2x2(Matrix2x2 *m) {
240 m->transpose();
241}
242
243static float SC_randf(float max) {
244 float r = (float)rand();
245 r *= max;
Jason Samse2b33042011-04-22 14:19:42 -0700246 r /= RAND_MAX;
247 return r;
Jason Samsfcf72312011-04-20 15:09:01 -0700248}
249
250static float SC_randf2(float min, float max) {
251 float r = (float)rand();
Jason Samse2b33042011-04-22 14:19:42 -0700252 r /= RAND_MAX;
Jason Samsfcf72312011-04-20 15:09:01 -0700253 r = r * (max - min) + min;
Jason Samse2b33042011-04-22 14:19:42 -0700254 return r;
Jason Samsfcf72312011-04-20 15:09:01 -0700255}
256
257static int SC_randi(int max) {
258 return (int)SC_randf(max);
259}
260
261static int SC_randi2(int min, int max) {
262 return (int)SC_randf2(min, max);
263}
264
265static float SC_frac(float v) {
266 int i = (int)floor(v);
267 return fmin(v - i, 0x1.fffffep-1f);
268}
269
270
271//////////////////////////////////////////////////////////////////////////////
272// Class implementation
273//////////////////////////////////////////////////////////////////////////////
274
275// llvm name mangling ref
276// <builtin-type> ::= v # void
277// ::= b # bool
278// ::= c # char
279// ::= a # signed char
280// ::= h # unsigned char
281// ::= s # short
282// ::= t # unsigned short
283// ::= i # int
284// ::= j # unsigned int
285// ::= l # long
286// ::= m # unsigned long
287// ::= x # long long, __int64
288// ::= y # unsigned long long, __int64
289// ::= f # float
290// ::= d # double
291
292static RsdSymbolTable gSyms[] = {
293 { "_Z4acosf", (void *)&acosf, true },
294 { "_Z5acoshf", (void *)&acoshf, true },
295 { "_Z4asinf", (void *)&asinf, true },
296 { "_Z5asinhf", (void *)&asinhf, true },
297 { "_Z4atanf", (void *)&atanf, true },
298 { "_Z5atan2ff", (void *)&atan2f, true },
299 { "_Z5atanhf", (void *)&atanhf, true },
300 { "_Z4cbrtf", (void *)&cbrtf, true },
301 { "_Z4ceilf", (void *)&ceilf, true },
302 { "_Z8copysignff", (void *)&copysignf, true },
303 { "_Z3cosf", (void *)&cosf, true },
304 { "_Z4coshf", (void *)&coshf, true },
305 { "_Z4erfcf", (void *)&erfcf, true },
306 { "_Z3erff", (void *)&erff, true },
307 { "_Z3expf", (void *)&expf, true },
308 { "_Z4exp2f", (void *)&exp2f, true },
309 { "_Z5exp10f", (void *)&SC_exp10, true },
310 { "_Z5expm1f", (void *)&expm1f, true },
311 { "_Z4fabsf", (void *)&fabsf, true },
312 { "_Z4fdimff", (void *)&fdimf, true },
313 { "_Z5floorf", (void *)&floorf, true },
314 { "_Z3fmafff", (void *)&fmaf, true },
315 { "_Z4fmaxff", (void *)&fmaxf, true },
316 { "_Z4fminff", (void *)&fminf, true }, // float fmin(float, float)
317 { "_Z4fmodff", (void *)&fmodf, true },
318 { "_Z5fractfPf", (void *)&SC_fract, true },
319 { "_Z5frexpfPi", (void *)&frexpf, true },
320 { "_Z5hypotff", (void *)&hypotf, true },
321 { "_Z5ilogbf", (void *)&ilogbf, true },
322 { "_Z5ldexpfi", (void *)&ldexpf, true },
323 { "_Z6lgammaf", (void *)&lgammaf, true },
324 { "_Z6lgammafPi", (void *)&lgammaf_r, true },
325 { "_Z3logf", (void *)&logf, true },
326 { "_Z4log2f", (void *)&SC_log2, true },
327 { "_Z5log10f", (void *)&log10f, true },
328 { "_Z5log1pf", (void *)&log1pf, true },
329 { "_Z4logbf", (void *)&logbf, true },
330 { "_Z3madfff", (void *)&SC_mad, true },
331 { "_Z4modffPf", (void *)&modff, true },
332 //{ "_Z3nanj", (void *)&SC_nan, true },
333 { "_Z9nextafterff", (void *)&nextafterf, true },
334 { "_Z3powff", (void *)&powf, true },
335 { "_Z9remainderff", (void *)&remainderf, true },
336 { "_Z6remquoffPi", (void *)&remquof, true },
337 { "_Z4rintf", (void *)&rintf, true },
338 { "_Z5rootnfi", (void *)&SC_rootn, true },
339 { "_Z5roundf", (void *)&roundf, true },
340 { "_Z5rsqrtf", (void *)&SC_rsqrt, true },
341 { "_Z3sinf", (void *)&sinf, true },
342 { "_Z6sincosfPf", (void *)&SC_sincos, true },
343 { "_Z4sinhf", (void *)&sinhf, true },
344 { "_Z4sqrtf", (void *)&sqrtf, true },
345 { "_Z3tanf", (void *)&tanf, true },
346 { "_Z4tanhf", (void *)&tanhf, true },
347 { "_Z6tgammaf", (void *)&tgammaf, true },
348 { "_Z5truncf", (void *)&truncf, true },
349
350 { "_Z3absi", (void *)&SC_abs_i32, true },
351 { "_Z3abss", (void *)&SC_abs_i16, true },
352 { "_Z3absc", (void *)&SC_abs_i8, true },
353 { "_Z3clzj", (void *)&SC_clz_u32, true },
354 { "_Z3clzt", (void *)&SC_clz_u16, true },
355 { "_Z3clzh", (void *)&SC_clz_u8, true },
356 { "_Z3clzi", (void *)&SC_clz_i32, true },
357 { "_Z3clzs", (void *)&SC_clz_i16, true },
358 { "_Z3clzc", (void *)&SC_clz_i8, true },
359 { "_Z3maxjj", (void *)&SC_max_u32, true },
360 { "_Z3maxtt", (void *)&SC_max_u16, true },
361 { "_Z3maxhh", (void *)&SC_max_u8, true },
362 { "_Z3maxii", (void *)&SC_max_i32, true },
363 { "_Z3maxss", (void *)&SC_max_i16, true },
364 { "_Z3maxcc", (void *)&SC_max_i8, true },
365 { "_Z3minjj", (void *)&SC_min_u32, true },
366 { "_Z3mintt", (void *)&SC_min_u16, true },
367 { "_Z3minhh", (void *)&SC_min_u8, true },
368 { "_Z3minii", (void *)&SC_min_i32, true },
369 { "_Z3minss", (void *)&SC_min_i16, true },
370 { "_Z3mincc", (void *)&SC_min_i8, true },
371
372 { "_Z5clampfff", (void *)&SC_clamp_f32, true },
373 { "_Z7degreesf", (void *)&SC_degrees, true },
374 { "_Z3maxff", (void *)&SC_max_f32, true },
375 { "_Z3minff", (void *)&SC_min_f32, true },
376 { "_Z3mixfff", (void *)&SC_mix_f32, true },
377 { "_Z7radiansf", (void *)&SC_radians, true },
378 { "_Z4stepff", (void *)&SC_step_f32, true },
379 //{ "smoothstep", (void *)&, true },
380 { "_Z4signf", (void *)&SC_sign_f32, true },
381
382 // matrix
383 { "_Z20rsMatrixLoadIdentityP12rs_matrix4x4", (void *)&SC_MatrixLoadIdentity_4x4, true },
384 { "_Z20rsMatrixLoadIdentityP12rs_matrix3x3", (void *)&SC_MatrixLoadIdentity_3x3, true },
385 { "_Z20rsMatrixLoadIdentityP12rs_matrix2x2", (void *)&SC_MatrixLoadIdentity_2x2, true },
386
387 { "_Z12rsMatrixLoadP12rs_matrix4x4PKf", (void *)&SC_MatrixLoad_4x4_f, true },
388 { "_Z12rsMatrixLoadP12rs_matrix3x3PKf", (void *)&SC_MatrixLoad_3x3_f, true },
389 { "_Z12rsMatrixLoadP12rs_matrix2x2PKf", (void *)&SC_MatrixLoad_2x2_f, true },
390
391 { "_Z12rsMatrixLoadP12rs_matrix4x4PKS_", (void *)&SC_MatrixLoad_4x4_4x4, true },
392 { "_Z12rsMatrixLoadP12rs_matrix4x4PK12rs_matrix3x3", (void *)&SC_MatrixLoad_4x4_3x3, true },
393 { "_Z12rsMatrixLoadP12rs_matrix4x4PK12rs_matrix2x2", (void *)&SC_MatrixLoad_4x4_2x2, true },
394 { "_Z12rsMatrixLoadP12rs_matrix3x3PKS_", (void *)&SC_MatrixLoad_3x3_3x3, true },
395 { "_Z12rsMatrixLoadP12rs_matrix2x2PKS_", (void *)&SC_MatrixLoad_2x2_2x2, true },
396
397 { "_Z18rsMatrixLoadRotateP12rs_matrix4x4ffff", (void *)&SC_MatrixLoadRotate, true },
398 { "_Z17rsMatrixLoadScaleP12rs_matrix4x4fff", (void *)&SC_MatrixLoadScale, true },
399 { "_Z21rsMatrixLoadTranslateP12rs_matrix4x4fff", (void *)&SC_MatrixLoadTranslate, true },
400 { "_Z14rsMatrixRotateP12rs_matrix4x4ffff", (void *)&SC_MatrixRotate, true },
401 { "_Z13rsMatrixScaleP12rs_matrix4x4fff", (void *)&SC_MatrixScale, true },
402 { "_Z17rsMatrixTranslateP12rs_matrix4x4fff", (void *)&SC_MatrixTranslate, true },
403
404 { "_Z20rsMatrixLoadMultiplyP12rs_matrix4x4PKS_S2_", (void *)&SC_MatrixLoadMultiply_4x4_4x4_4x4, true },
405 { "_Z16rsMatrixMultiplyP12rs_matrix4x4PKS_", (void *)&SC_MatrixMultiply_4x4_4x4, true },
406 { "_Z20rsMatrixLoadMultiplyP12rs_matrix3x3PKS_S2_", (void *)&SC_MatrixLoadMultiply_3x3_3x3_3x3, true },
407 { "_Z16rsMatrixMultiplyP12rs_matrix3x3PKS_", (void *)&SC_MatrixMultiply_3x3_3x3, true },
408 { "_Z20rsMatrixLoadMultiplyP12rs_matrix2x2PKS_S2_", (void *)&SC_MatrixLoadMultiply_2x2_2x2_2x2, true },
409 { "_Z16rsMatrixMultiplyP12rs_matrix2x2PKS_", (void *)&SC_MatrixMultiply_2x2_2x2, true },
410
411 { "_Z17rsMatrixLoadOrthoP12rs_matrix4x4ffffff", (void *)&SC_MatrixLoadOrtho, true },
412 { "_Z19rsMatrixLoadFrustumP12rs_matrix4x4ffffff", (void *)&SC_MatrixLoadFrustum, true },
413 { "_Z23rsMatrixLoadPerspectiveP12rs_matrix4x4ffff", (void *)&SC_MatrixLoadPerspective, true },
414
415 { "_Z15rsMatrixInverseP12rs_matrix4x4", (void *)&SC_MatrixInverse_4x4, true },
416 { "_Z24rsMatrixInverseTransposeP12rs_matrix4x4", (void *)&SC_MatrixInverseTranspose_4x4, true },
417 { "_Z17rsMatrixTransposeP12rs_matrix4x4", (void *)&SC_MatrixTranspose_4x4, true },
418 { "_Z17rsMatrixTransposeP12rs_matrix4x4", (void *)&SC_MatrixTranspose_3x3, true },
419 { "_Z17rsMatrixTransposeP12rs_matrix4x4", (void *)&SC_MatrixTranspose_2x2, true },
420
421 // RS Math
422 { "_Z6rsRandi", (void *)&SC_randi, true },
423 { "_Z6rsRandii", (void *)&SC_randi2, true },
424 { "_Z6rsRandf", (void *)&SC_randf, true },
425 { "_Z6rsRandff", (void *)&SC_randf2, true },
426 { "_Z6rsFracf", (void *)&SC_frac, true },
427
428 { NULL, NULL, false }
429};
430
431const RsdSymbolTable * rsdLookupSymbolMath(const char *sym) {
432 const RsdSymbolTable *syms = gSyms;
433
434 while (syms->mPtr) {
435 if (!strcmp(syms->mName, sym)) {
436 return syms;
437 }
438 syms++;
439 }
440 return NULL;
441}
442