rsCpuRuntimeMath.cpp revision 0b575de8ed0b628d84d256f5846500b0385979bd
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#ifndef RS_SERVER
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
31using namespace android;
32using namespace android::renderscript;
33
34
35//////////////////////////////////////////////////////////////////////////////
36// Float util
37//////////////////////////////////////////////////////////////////////////////
38
39
40static void SC_MatrixLoadRotate(Matrix4x4 *m, float rot, float x, float y, float z) {
41    m->loadRotate(rot, x, y, z);
42}
43static void SC_MatrixLoadScale(Matrix4x4 *m, float x, float y, float z) {
44    m->loadScale(x, y, z);
45}
46static void SC_MatrixLoadTranslate(Matrix4x4 *m, float x, float y, float z) {
47    m->loadTranslate(x, y, z);
48}
49static void SC_MatrixRotate(Matrix4x4 *m, float rot, float x, float y, float z) {
50    m->rotate(rot, x, y, z);
51}
52static void SC_MatrixScale(Matrix4x4 *m, float x, float y, float z) {
53    m->scale(x, y, z);
54}
55static void SC_MatrixTranslate(Matrix4x4 *m, float x, float y, float z) {
56    m->translate(x, y, z);
57}
58
59static void SC_MatrixLoadOrtho(Matrix4x4 *m, float l, float r, float b, float t, float n, float f) {
60    m->loadOrtho(l, r, b, t, n, f);
61}
62static void SC_MatrixLoadFrustum(Matrix4x4 *m, float l, float r, float b, float t, float n, float f) {
63    m->loadFrustum(l, r, b, t, n, f);
64}
65static void SC_MatrixLoadPerspective(Matrix4x4 *m, float fovy, float aspect, float near, float far) {
66    m->loadPerspective(fovy, aspect, near, far);
67}
68
69static bool SC_MatrixInverse_4x4(Matrix4x4 *m) {
70    return m->inverse();
71}
72static bool SC_MatrixInverseTranspose_4x4(Matrix4x4 *m) {
73    return m->inverseTranspose();
74}
75static void SC_MatrixTranspose_4x4(Matrix4x4 *m) {
76    m->transpose();
77}
78static void SC_MatrixTranspose_3x3(Matrix3x3 *m) {
79    m->transpose();
80}
81static void SC_MatrixTranspose_2x2(Matrix2x2 *m) {
82    m->transpose();
83}
84
85static float SC_randf2(float min, float max) {
86    float r = (float)rand();
87    r /= RAND_MAX;
88    r = r * (max - min) + min;
89    return r;
90}
91
92static float SC_frac(float v) {
93    int i = (int)floor(v);
94    return fmin(v - i, 0x1.fffffep-1f);
95}
96
97
98//////////////////////////////////////////////////////////////////////////////
99// Class implementation
100//////////////////////////////////////////////////////////////////////////////
101
102// llvm name mangling ref
103//  <builtin-type> ::= v  # void
104//                 ::= b  # bool
105//                 ::= c  # char
106//                 ::= a  # signed char
107//                 ::= h  # unsigned char
108//                 ::= s  # short
109//                 ::= t  # unsigned short
110//                 ::= i  # int
111//                 ::= j  # unsigned int
112//                 ::= l  # long
113//                 ::= m  # unsigned long
114//                 ::= x  # long long, __int64
115//                 ::= y  # unsigned long long, __int64
116//                 ::= f  # float
117//                 ::= d  # double
118
119static RsdCpuReference::CpuSymbol gSyms[] = {
120    { "_Z4acosf", (void *)&acosf, true },
121    { "_Z5acoshf", (void *)&acoshf, true },
122    { "_Z4asinf", (void *)&asinf, true },
123    { "_Z5asinhf", (void *)&asinhf, true },
124    { "_Z4atanf", (void *)&atanf, true },
125    { "_Z5atan2ff", (void *)&atan2f, true },
126    { "_Z5atanhf", (void *)&atanhf, true },
127    { "_Z4cbrtf", (void *)&cbrtf, true },
128    { "_Z4ceilf", (void *)&ceilf, true },
129    { "_Z8copysignff", (void *)&copysignf, true },
130    { "_Z3cosf", (void *)&cosf, true },
131    { "_Z4coshf", (void *)&coshf, true },
132    { "_Z4erfcf", (void *)&erfcf, true },
133    { "_Z3erff", (void *)&erff, true },
134    { "_Z3expf", (void *)&expf, true },
135    { "_Z4exp2f", (void *)&exp2f, true },
136    { "_Z5expm1f", (void *)&expm1f, true },
137    { "_Z4fdimff", (void *)&fdimf, true },
138    { "_Z5floorf", (void *)&floorf, true },
139    { "_Z3fmafff", (void *)&fmaf, true },
140    { "_Z4fmaxff", (void *)&fmaxf, true },
141    { "_Z4fminff", (void *)&fminf, true },  // float fmin(float, float)
142    { "_Z4fmodff", (void *)&fmodf, true },
143    { "_Z5frexpfPi", (void *)&frexpf, true },
144    { "_Z5hypotff", (void *)&hypotf, true },
145    { "_Z5ilogbf", (void *)&ilogbf, true },
146    { "_Z5ldexpfi", (void *)&ldexpf, true },
147    { "_Z6lgammaf", (void *)&lgammaf, true },
148    { "_Z6lgammafPi", (void *)&lgammaf_r, true },
149    { "_Z3logf", (void *)&logf, true },
150    { "_Z5log10f", (void *)&log10f, true },
151    { "_Z5log1pf", (void *)&log1pf, true },
152    { "_Z4logbf", (void *)&logbf, true },
153    { "_Z4modffPf", (void *)&modff, true },
154    //{ "_Z3nanj", (void *)&SC_nan, true },
155    { "_Z9nextafterff", (void *)&nextafterf, true },
156    { "_Z3powff", (void *)&powf, true },
157    { "_Z9remainderff", (void *)&remainderf, true },
158    { "_Z6remquoffPi", (void *)&remquof, true },
159    { "_Z4rintf", (void *)&rintf, true },
160    { "_Z5roundf", (void *)&roundf, true },
161    { "_Z3sinf", (void *)&sinf, true },
162    { "_Z4sinhf", (void *)&sinhf, true },
163    { "_Z4sqrtf", (void *)&sqrtf, true },
164    { "_Z3tanf", (void *)&tanf, true },
165    { "_Z4tanhf", (void *)&tanhf, true },
166    { "_Z6tgammaf", (void *)&tgammaf, true },
167    { "_Z5truncf", (void *)&truncf, true },
168
169    //{ "smoothstep", (void *)&, true },
170
171    // matrix
172    { "_Z18rsMatrixLoadRotateP12rs_matrix4x4ffff", (void *)&SC_MatrixLoadRotate, true },
173    { "_Z17rsMatrixLoadScaleP12rs_matrix4x4fff", (void *)&SC_MatrixLoadScale, true },
174    { "_Z21rsMatrixLoadTranslateP12rs_matrix4x4fff", (void *)&SC_MatrixLoadTranslate, true },
175    { "_Z14rsMatrixRotateP12rs_matrix4x4ffff", (void *)&SC_MatrixRotate, true },
176    { "_Z13rsMatrixScaleP12rs_matrix4x4fff", (void *)&SC_MatrixScale, true },
177    { "_Z17rsMatrixTranslateP12rs_matrix4x4fff", (void *)&SC_MatrixTranslate, true },
178
179    { "_Z17rsMatrixLoadOrthoP12rs_matrix4x4ffffff", (void *)&SC_MatrixLoadOrtho, true },
180    { "_Z19rsMatrixLoadFrustumP12rs_matrix4x4ffffff", (void *)&SC_MatrixLoadFrustum, true },
181    { "_Z23rsMatrixLoadPerspectiveP12rs_matrix4x4ffff", (void *)&SC_MatrixLoadPerspective, true },
182
183    { "_Z15rsMatrixInverseP12rs_matrix4x4", (void *)&SC_MatrixInverse_4x4, true },
184    { "_Z24rsMatrixInverseTransposeP12rs_matrix4x4", (void *)&SC_MatrixInverseTranspose_4x4, true },
185    { "_Z17rsMatrixTransposeP12rs_matrix4x4", (void *)&SC_MatrixTranspose_4x4, true },
186    { "_Z17rsMatrixTransposeP12rs_matrix3x3", (void *)&SC_MatrixTranspose_3x3, true },
187    { "_Z17rsMatrixTransposeP12rs_matrix2x2", (void *)&SC_MatrixTranspose_2x2, true },
188
189    // RS Math
190    { "_Z6rsRandff", (void *)&SC_randf2, true },
191    { "_Z6rsFracf", (void *)&SC_frac, true },
192
193    { NULL, NULL, false }
194};
195
196const RsdCpuReference::CpuSymbol * RsdCpuScriptImpl::lookupSymbolMath(const char *sym) {
197    const RsdCpuReference::CpuSymbol *syms = gSyms;
198
199    while (syms->fnPtr) {
200        if (!strcmp(syms->name, sym)) {
201            return syms;
202        }
203        syms++;
204    }
205    return NULL;
206}
207
208