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