1 /*
2  * Copyright (C) 2011-2012 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 #if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
18 #include <cutils/compiler.h>
19 #endif
20 
21 #include "rsContext.h"
22 #include "rsScriptC.h"
23 #include "rsMatrix4x4.h"
24 #include "rsMatrix3x3.h"
25 #include "rsMatrix2x2.h"
26 
27 #include "rsCpuCore.h"
28 #include "rsCpuScript.h"
29 
30 using namespace android;
31 using namespace android::renderscript;
32 
33 #define EXPORT_F32_FN_F32(func)                                 \
34     float __attribute__((overloadable)) SC_##func(float v) {    \
35         return func(v);                                         \
36     }
37 
38 #define EXPORT_F32_FN_F32_F32(func)                                     \
39     float __attribute__((overloadable)) SC_##func(float t, float v) {   \
40         return func(t, v);                                              \
41     }
42 
43 //////////////////////////////////////////////////////////////////////////////
44 // Float util
45 //////////////////////////////////////////////////////////////////////////////
46 
47 // Handle missing Gingerbread functions like tgammaf.
SC_tgammaf(float x)48 float SC_tgammaf(float x) {
49 #ifdef RS_COMPATIBILITY_LIB
50     return tgamma(x);
51 #else
52     return tgammaf(x);
53 #endif
54 }
55 
SC_abs_i32(int32_t v)56 uint32_t SC_abs_i32(int32_t v) {return abs(v);}
57 
SC_MatrixLoadRotate(Matrix4x4 * m,float rot,float x,float y,float z)58 static void SC_MatrixLoadRotate(Matrix4x4 *m, float rot, float x, float y, float z) {
59     m->loadRotate(rot, x, y, z);
60 }
SC_MatrixLoadScale(Matrix4x4 * m,float x,float y,float z)61 static void SC_MatrixLoadScale(Matrix4x4 *m, float x, float y, float z) {
62     m->loadScale(x, y, z);
63 }
SC_MatrixLoadTranslate(Matrix4x4 * m,float x,float y,float z)64 static void SC_MatrixLoadTranslate(Matrix4x4 *m, float x, float y, float z) {
65     m->loadTranslate(x, y, z);
66 }
SC_MatrixRotate(Matrix4x4 * m,float rot,float x,float y,float z)67 static void SC_MatrixRotate(Matrix4x4 *m, float rot, float x, float y, float z) {
68     m->rotate(rot, x, y, z);
69 }
SC_MatrixScale(Matrix4x4 * m,float x,float y,float z)70 static void SC_MatrixScale(Matrix4x4 *m, float x, float y, float z) {
71     m->scale(x, y, z);
72 }
SC_MatrixTranslate(Matrix4x4 * m,float x,float y,float z)73 static void SC_MatrixTranslate(Matrix4x4 *m, float x, float y, float z) {
74     m->translate(x, y, z);
75 }
76 
SC_MatrixLoadOrtho(Matrix4x4 * m,float l,float r,float b,float t,float n,float f)77 static void SC_MatrixLoadOrtho(Matrix4x4 *m, float l, float r, float b, float t, float n, float f) {
78     m->loadOrtho(l, r, b, t, n, f);
79 }
SC_MatrixLoadFrustum(Matrix4x4 * m,float l,float r,float b,float t,float n,float f)80 static void SC_MatrixLoadFrustum(Matrix4x4 *m, float l, float r, float b, float t, float n, float f) {
81     m->loadFrustum(l, r, b, t, n, f);
82 }
SC_MatrixLoadPerspective(Matrix4x4 * m,float fovy,float aspect,float near,float far)83 static void SC_MatrixLoadPerspective(Matrix4x4 *m, float fovy, float aspect, float near, float far) {
84     m->loadPerspective(fovy, aspect, near, far);
85 }
86 
SC_MatrixInverse_4x4(Matrix4x4 * m)87 static bool SC_MatrixInverse_4x4(Matrix4x4 *m) {
88     return m->inverse();
89 }
SC_MatrixInverseTranspose_4x4(Matrix4x4 * m)90 static bool SC_MatrixInverseTranspose_4x4(Matrix4x4 *m) {
91     return m->inverseTranspose();
92 }
SC_MatrixTranspose_4x4(Matrix4x4 * m)93 static void SC_MatrixTranspose_4x4(Matrix4x4 *m) {
94     m->transpose();
95 }
SC_MatrixTranspose_3x3(Matrix3x3 * m)96 static void SC_MatrixTranspose_3x3(Matrix3x3 *m) {
97     m->transpose();
98 }
SC_MatrixTranspose_2x2(Matrix2x2 * m)99 static void SC_MatrixTranspose_2x2(Matrix2x2 *m) {
100     m->transpose();
101 }
102 
SC_randf2(float min,float max)103 float SC_randf2(float min, float max) {
104     float r = (float)rand();
105     r /= RAND_MAX;
106     r = r * (max - min) + min;
107     return r;
108 }
109 
110 EXPORT_F32_FN_F32(acosf)
EXPORT_F32_FN_F32(acoshf)111 EXPORT_F32_FN_F32(acoshf)
112 EXPORT_F32_FN_F32(asinf)
113 EXPORT_F32_FN_F32(asinhf)
114 EXPORT_F32_FN_F32(atanf)
115 EXPORT_F32_FN_F32_F32(atan2f)
116 EXPORT_F32_FN_F32(atanhf)
117 EXPORT_F32_FN_F32(cbrtf)
118 EXPORT_F32_FN_F32(ceilf)
119 EXPORT_F32_FN_F32_F32(copysignf)
120 EXPORT_F32_FN_F32(cosf)
121 EXPORT_F32_FN_F32(coshf)
122 EXPORT_F32_FN_F32(erfcf)
123 EXPORT_F32_FN_F32(erff)
124 EXPORT_F32_FN_F32(expf)
125 EXPORT_F32_FN_F32(exp2f)
126 EXPORT_F32_FN_F32(expm1f)
127 EXPORT_F32_FN_F32_F32(fdimf)
128 EXPORT_F32_FN_F32(floorf)
129 float SC_fmaf(float u, float t, float v) {return fmaf(u, t, v);}
130 EXPORT_F32_FN_F32_F32(fmaxf)
EXPORT_F32_FN_F32_F32(fminf)131 EXPORT_F32_FN_F32_F32(fminf)
132 EXPORT_F32_FN_F32_F32(fmodf)
133 float SC_frexpf(float v, int* ptr) {return frexpf(v, ptr);}
EXPORT_F32_FN_F32_F32(hypotf)134 EXPORT_F32_FN_F32_F32(hypotf)
135 int SC_ilogbf(float v) {return ilogbf(v); }
SC_ldexpf(float v,int i)136 float SC_ldexpf(float v, int i) {return ldexpf(v, i);}
EXPORT_F32_FN_F32(lgammaf)137 EXPORT_F32_FN_F32(lgammaf)
138 float SC_lgammaf_r(float v, int* ptr) {return lgammaf_r(v, ptr);}
139 EXPORT_F32_FN_F32(logf)
EXPORT_F32_FN_F32(log10f)140 EXPORT_F32_FN_F32(log10f)
141 EXPORT_F32_FN_F32(log1pf)
142 EXPORT_F32_FN_F32(logbf)
143 float SC_modff(float v, float* ptr) {return modff(v, ptr);}
144 EXPORT_F32_FN_F32_F32(nextafterf)
EXPORT_F32_FN_F32_F32(powf)145 EXPORT_F32_FN_F32_F32(powf)
146 EXPORT_F32_FN_F32_F32(remainderf)
147 float SC_remquof(float t, float v, int* ptr) {return remquof(t, v, ptr);}
148 EXPORT_F32_FN_F32(rintf)
EXPORT_F32_FN_F32(roundf)149 EXPORT_F32_FN_F32(roundf)
150 EXPORT_F32_FN_F32(sinf)
151 EXPORT_F32_FN_F32(sinhf)
152 EXPORT_F32_FN_F32(sqrtf)
153 EXPORT_F32_FN_F32(tanf)
154 EXPORT_F32_FN_F32(tanhf)
155 EXPORT_F32_FN_F32(truncf)
156 void __attribute__((overloadable)) rsMatrixLoadRotate(rs_matrix4x4 *m,
157         float rot, float x, float y, float z) {
158     SC_MatrixLoadRotate((Matrix4x4 *) m, rot, x, y, z);
159 }
rsMatrixLoadScale(rs_matrix4x4 * m,float x,float y,float z)160 void __attribute__((overloadable)) rsMatrixLoadScale(rs_matrix4x4 *m,
161         float x, float y, float z) {
162     SC_MatrixLoadScale((Matrix4x4 *) m, x, y, z);
163 }
rsMatrixLoadTranslate(rs_matrix4x4 * m,float x,float y,float z)164 void __attribute__((overloadable)) rsMatrixLoadTranslate(rs_matrix4x4 *m,
165         float x, float y, float z) {
166     SC_MatrixLoadTranslate((Matrix4x4 *) m, x, y, z);
167 }
rsMatrixRotate(rs_matrix4x4 * m,float rot,float x,float y,float z)168 void __attribute__((overloadable)) rsMatrixRotate(rs_matrix4x4 *m, float rot,
169         float x, float y, float z) {
170     SC_MatrixRotate((Matrix4x4 *) m, rot, x, y, z);
171 }
rsMatrixScale(rs_matrix4x4 * m,float x,float y,float z)172 void __attribute__((overloadable)) rsMatrixScale(rs_matrix4x4 *m, float x,
173         float y, float z) {
174     SC_MatrixScale((Matrix4x4 *) m, x, y, z);
175 }
rsMatrixTranslate(rs_matrix4x4 * m,float x,float y,float z)176 void __attribute__((overloadable)) rsMatrixTranslate(rs_matrix4x4 *m, float x,
177         float y, float z) {
178     SC_MatrixTranslate((Matrix4x4 *) m, x, y, z);
179 }
rsMatrixLoadOrtho(rs_matrix4x4 * m,float l,float r,float b,float t,float n,float f)180 void __attribute__((overloadable)) rsMatrixLoadOrtho(rs_matrix4x4 *m, float l,
181         float r, float b, float t, float n, float f) {
182     SC_MatrixLoadOrtho((Matrix4x4 *) m, l, r, b, t, n, f);
183 }
rsMatrixLoadFrustum(rs_matrix4x4 * m,float l,float r,float b,float t,float n,float f)184 void __attribute__((overloadable)) rsMatrixLoadFrustum(rs_matrix4x4 *m,
185         float l, float r, float b, float t, float n, float f) {
186     SC_MatrixLoadFrustum((Matrix4x4 *) m, l, r, b, t, n, f);
187 }
rsMatrixLoadPerspective(rs_matrix4x4 * m,float fovy,float aspect,float near,float far)188 void __attribute__((overloadable)) rsMatrixLoadPerspective(rs_matrix4x4 *m,
189         float fovy, float aspect, float near, float far) {
190     SC_MatrixLoadPerspective((Matrix4x4 *) m, fovy, aspect, near, far);
191 }
rsMatrixInverse(rs_matrix4x4 * m)192 bool __attribute__((overloadable)) rsMatrixInverse(rs_matrix4x4 *m) {
193     return SC_MatrixInverse_4x4((Matrix4x4 *) m);
194 }
rsMatrixInverseTranspose(rs_matrix4x4 * m)195 bool __attribute__((overloadable)) rsMatrixInverseTranspose(rs_matrix4x4 *m) {
196     return SC_MatrixInverseTranspose_4x4((Matrix4x4 *) m);
197 }
rsMatrixTranspose(rs_matrix4x4 * m)198 void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix4x4 *m) {
199     SC_MatrixTranspose_4x4((Matrix4x4 *) m);
200 }
rsMatrixTranspose(rs_matrix3x3 * m)201 void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix3x3 *m) {
202     SC_MatrixTranspose_3x3((Matrix3x3 *) m);
203 }
rsMatrixTranspose(rs_matrix2x2 * m)204 void __attribute__((overloadable)) rsMatrixTranspose(rs_matrix2x2 *m) {
205     SC_MatrixTranspose_2x2((Matrix2x2 *) m);
206 }
207 
208 
209