blob: 57855db2cf205afe5b7365f8768667a077b84a27 [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
Jason Samsaeb094b2010-05-18 13:35:45 -070027static float SC_exp10(float v) {
28 return pow(10.f, v);
Jason Samsaeb094b2010-05-18 13:35:45 -070029}
30
31static float SC_fract(float v, int *iptr) {
32 int i = (int)floor(v);
33 iptr[0] = i;
34 return fmin(v - i, 0x1.fffffep-1f);
35}
36
37static float SC_log2(float v) {
38 return log10(v) / log10(2.f);
39}
40
Stephen Hines3e6482f2011-01-19 12:51:33 -080041static float SC_mad(float v1, float v2, float v3) {
42 return v1 * v2 + v3;
43}
44
Jason Samsaeb094b2010-05-18 13:35:45 -070045static float SC_pown(float v, int p) {
46 return powf(v, (float)p);
47}
48
49static float SC_powr(float v, float p) {
50 return powf(v, p);
51}
52
53float SC_rootn(float v, int r) {
54 return pow(v, 1.f / r);
55}
56
57float SC_rsqrt(float v) {
58 return 1.f / sqrtf(v);
59}
60
61float SC_sincos(float v, float *cosptr) {
62 *cosptr = cosf(v);
63 return sinf(v);
64}
65
Jason Samsaeb094b2010-05-18 13:35:45 -070066//////////////////////////////////////////////////////////////////////////////
67// Integer
68//////////////////////////////////////////////////////////////////////////////
69
70
71static uint32_t SC_abs_i32(int32_t v) {return abs(v);}
72static uint16_t SC_abs_i16(int16_t v) {return (uint16_t)abs(v);}
73static uint8_t SC_abs_i8(int8_t v) {return (uint8_t)abs(v);}
74
75static uint32_t SC_clz_u32(uint32_t v) {return __builtin_clz(v);}
76static uint16_t SC_clz_u16(uint16_t v) {return (uint16_t)__builtin_clz(v);}
77static uint8_t SC_clz_u8(uint8_t v) {return (uint8_t)__builtin_clz(v);}
78static int32_t SC_clz_i32(int32_t v) {return (int32_t)__builtin_clz((uint32_t)v);}
79static int16_t SC_clz_i16(int16_t v) {return (int16_t)__builtin_clz(v);}
80static int8_t SC_clz_i8(int8_t v) {return (int8_t)__builtin_clz(v);}
81
82static uint32_t SC_max_u32(uint32_t v, uint32_t v2) {return rsMax(v, v2);}
83static uint16_t SC_max_u16(uint16_t v, uint16_t v2) {return rsMax(v, v2);}
84static uint8_t SC_max_u8(uint8_t v, uint8_t v2) {return rsMax(v, v2);}
85static int32_t SC_max_i32(int32_t v, int32_t v2) {return rsMax(v, v2);}
86static int16_t SC_max_i16(int16_t v, int16_t v2) {return rsMax(v, v2);}
87static int8_t SC_max_i8(int8_t v, int8_t v2) {return rsMax(v, v2);}
88
89static uint32_t SC_min_u32(uint32_t v, uint32_t v2) {return rsMin(v, v2);}
90static uint16_t SC_min_u16(uint16_t v, uint16_t v2) {return rsMin(v, v2);}
91static uint8_t SC_min_u8(uint8_t v, uint8_t v2) {return rsMin(v, v2);}
92static int32_t SC_min_i32(int32_t v, int32_t v2) {return rsMin(v, v2);}
93static int16_t SC_min_i16(int16_t v, int16_t v2) {return rsMin(v, v2);}
94static int8_t SC_min_i8(int8_t v, int8_t v2) {return rsMin(v, v2);}
95
96//////////////////////////////////////////////////////////////////////////////
97// Float util
98//////////////////////////////////////////////////////////////////////////////
99
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800100static float SC_clamp_f32(float amount, float low, float high) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700101 return amount < low ? low : (amount > high ? high : amount);
102}
103
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800104static float SC_degrees(float radians) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700105 return radians * (180.f / M_PI);
106}
107
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800108static float SC_max_f32(float v, float v2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700109 return rsMax(v, v2);
110}
111
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800112static float SC_min_f32(float v, float v2) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700113 return rsMin(v, v2);
114}
115
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800116static float SC_mix_f32(float start, float stop, float amount) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700117 //LOGE("lerpf %f %f %f", start, stop, amount);
118 return start + (stop - start) * amount;
119}
120
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800121static float SC_radians(float degrees) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700122 return degrees * (M_PI / 180.f);
123}
124
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800125static float SC_step_f32(float edge, float v) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700126 if (v < edge) return 0.f;
127 return 1.f;
128}
129
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800130static float SC_sign_f32(float value) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700131 if (value > 0) return 1.f;
132 if (value < 0) return -1.f;
133 return value;
134}
135
Jason Samsaeb094b2010-05-18 13:35:45 -0700136//////////////////////////////////////////////////////////////////////////////
137// Class implementation
138//////////////////////////////////////////////////////////////////////////////
139
140// llvm name mangling ref
141// <builtin-type> ::= v # void
142// ::= b # bool
143// ::= c # char
144// ::= a # signed char
145// ::= h # unsigned char
146// ::= s # short
147// ::= t # unsigned short
148// ::= i # int
149// ::= j # unsigned int
150// ::= l # long
151// ::= m # unsigned long
152// ::= x # long long, __int64
153// ::= y # unsigned long long, __int64
154// ::= f # float
155// ::= d # double
156
157static ScriptCState::SymbolTable_t gSyms[] = {
158 // OpenCL math
Jason Sams6bfc1b92010-11-01 14:26:30 -0700159 { "_Z4acosf", (void *)&acosf, true },
160 { "_Z5acoshf", (void *)&acoshf, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700161 { "_Z4asinf", (void *)&asinf, true },
162 { "_Z5asinhf", (void *)&asinhf, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700163 { "_Z4atanf", (void *)&atanf, true },
164 { "_Z5atan2ff", (void *)&atan2f, true },
Stephen Hines3e6482f2011-01-19 12:51:33 -0800165 { "_Z5atanhf", (void *)&atanhf, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700166 { "_Z4cbrtf", (void *)&cbrtf, true },
167 { "_Z4ceilf", (void *)&ceilf, true },
168 { "_Z8copysignff", (void *)&copysignf, true },
169 { "_Z3cosf", (void *)&cosf, true },
170 { "_Z4coshf", (void *)&coshf, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700171 { "_Z4erfcf", (void *)&erfcf, true },
172 { "_Z3erff", (void *)&erff, true },
173 { "_Z3expf", (void *)&expf, true },
174 { "_Z4exp2f", (void *)&exp2f, true },
175 { "_Z5exp10f", (void *)&SC_exp10, true },
176 { "_Z5expm1f", (void *)&expm1f, true },
177 { "_Z4fabsf", (void *)&fabsf, true },
178 { "_Z4fdimff", (void *)&fdimf, true },
179 { "_Z5floorf", (void *)&floorf, true },
180 { "_Z3fmafff", (void *)&fmaf, true },
181 { "_Z4fmaxff", (void *)&fmaxf, true },
182 { "_Z4fminff", (void *)&fminf, true }, // float fmin(float, float)
183 { "_Z4fmodff", (void *)&fmodf, true },
184 { "_Z5fractfPf", (void *)&SC_fract, true },
185 { "_Z5frexpfPi", (void *)&frexpf, true },
186 { "_Z5hypotff", (void *)&hypotf, true },
187 { "_Z5ilogbf", (void *)&ilogbf, true },
188 { "_Z5ldexpfi", (void *)&ldexpf, true },
189 { "_Z6lgammaf", (void *)&lgammaf, true },
Stephen Hines3e6482f2011-01-19 12:51:33 -0800190 { "_Z6lgammafPi", (void *)&lgammaf_r, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700191 { "_Z3logf", (void *)&logf, true },
192 { "_Z4log2f", (void *)&SC_log2, true },
193 { "_Z5log10f", (void *)&log10f, true },
194 { "_Z5log1pf", (void *)&log1pf, true },
Stephen Hines3e6482f2011-01-19 12:51:33 -0800195 { "_Z4logbf", (void *)&logbf, true },
196 { "_Z3madfff", (void *)&SC_mad, true },
Stephen Hines32086d82011-01-18 19:39:29 -0800197 { "_Z4modffPf", (void *)&modff, true },
Stephen Hines0469e322011-01-26 12:31:36 -0800198 //{ "_Z3nanj", (void *)&SC_nan, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700199 { "_Z9nextafterff", (void *)&nextafterf, true },
200 { "_Z3powff", (void *)&powf, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700201 { "_Z9remainderff", (void *)&remainderf, true },
Stephen Hines3e6482f2011-01-19 12:51:33 -0800202 { "_Z6remquoffPi", (void *)&remquof, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700203 { "_Z4rintf", (void *)&rintf, true },
204 { "_Z5rootnfi", (void *)&SC_rootn, true },
205 { "_Z5roundf", (void *)&roundf, true },
206 { "_Z5rsqrtf", (void *)&SC_rsqrt, true },
207 { "_Z3sinf", (void *)&sinf, true },
Stephen Hines3e6482f2011-01-19 12:51:33 -0800208 { "_Z6sincosfPf", (void *)&SC_sincos, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700209 { "_Z4sinhf", (void *)&sinhf, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700210 { "_Z4sqrtf", (void *)&sqrtf, true },
211 { "_Z3tanf", (void *)&tanf, true },
212 { "_Z4tanhf", (void *)&tanhf, true },
Stephen Hines0469e322011-01-26 12:31:36 -0800213 { "_Z6tgammaf", (void *)&tgammaf, true },
Jason Sams6bfc1b92010-11-01 14:26:30 -0700214 { "_Z5truncf", (void *)&truncf, true },
Jason Samsaeb094b2010-05-18 13:35:45 -0700215
216 // OpenCL Int
Jason Sams6bfc1b92010-11-01 14:26:30 -0700217 { "_Z3absi", (void *)&SC_abs_i32, true },
218 { "_Z3abss", (void *)&SC_abs_i16, true },
219 { "_Z3absc", (void *)&SC_abs_i8, true },
220 { "_Z3clzj", (void *)&SC_clz_u32, true },
221 { "_Z3clzt", (void *)&SC_clz_u16, true },
222 { "_Z3clzh", (void *)&SC_clz_u8, true },
223 { "_Z3clzi", (void *)&SC_clz_i32, true },
224 { "_Z3clzs", (void *)&SC_clz_i16, true },
225 { "_Z3clzc", (void *)&SC_clz_i8, true },
226 { "_Z3maxjj", (void *)&SC_max_u32, true },
227 { "_Z3maxtt", (void *)&SC_max_u16, true },
228 { "_Z3maxhh", (void *)&SC_max_u8, true },
229 { "_Z3maxii", (void *)&SC_max_i32, true },
230 { "_Z3maxss", (void *)&SC_max_i16, true },
231 { "_Z3maxcc", (void *)&SC_max_i8, true },
232 { "_Z3minjj", (void *)&SC_min_u32, true },
233 { "_Z3mintt", (void *)&SC_min_u16, true },
234 { "_Z3minhh", (void *)&SC_min_u8, true },
235 { "_Z3minii", (void *)&SC_min_i32, true },
236 { "_Z3minss", (void *)&SC_min_i16, true },
237 { "_Z3mincc", (void *)&SC_min_i8, true },
Jason Samsaeb094b2010-05-18 13:35:45 -0700238
239 // OpenCL 6.11.4
Jason Sams6bfc1b92010-11-01 14:26:30 -0700240 { "_Z5clampfff", (void *)&SC_clamp_f32, true },
241 { "_Z7degreesf", (void *)&SC_degrees, true },
242 { "_Z3maxff", (void *)&SC_max_f32, true },
243 { "_Z3minff", (void *)&SC_min_f32, true },
244 { "_Z3mixfff", (void *)&SC_mix_f32, true },
245 { "_Z7radiansf", (void *)&SC_radians, true },
246 { "_Z4stepff", (void *)&SC_step_f32, true },
247 //{ "smoothstep", (void *)&, true },
248 { "_Z4signf", (void *)&SC_sign_f32, true },
Jason Samsaeb094b2010-05-18 13:35:45 -0700249
Jason Sams6bfc1b92010-11-01 14:26:30 -0700250 { NULL, NULL, false }
Jason Samsaeb094b2010-05-18 13:35:45 -0700251};
252
Alex Sakhartchoukafb743a2010-11-09 17:00:54 -0800253const ScriptCState::SymbolTable_t * ScriptCState::lookupSymbolCL(const char *sym) {
Jason Samsaeb094b2010-05-18 13:35:45 -0700254 ScriptCState::SymbolTable_t *syms = gSyms;
255
256 while (syms->mPtr) {
257 if (!strcmp(syms->mName, sym)) {
258 return syms;
259 }
260 syms++;
261 }
262 return NULL;
263}
264