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