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