Home | History | Annotate | Download | only in cpu_ref
      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 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.
     48 float SC_tgammaf(float x) {
     49     return tgamma(x);
     50 }
     51 
     52 uint32_t SC_abs_i32(int32_t v) {return abs(v);}
     53 
     54 static void SC_MatrixLoadRotate(Matrix4x4 *m, float rot, float x, float y, float z) {
     55     m->loadRotate(rot, x, y, z);
     56 }
     57 static void SC_MatrixLoadScale(Matrix4x4 *m, float x, float y, float z) {
     58     m->loadScale(x, y, z);
     59 }
     60 static void SC_MatrixLoadTranslate(Matrix4x4 *m, float x, float y, float z) {
     61     m->loadTranslate(x, y, z);
     62 }
     63 static void SC_MatrixRotate(Matrix4x4 *m, float rot, float x, float y, float z) {
     64     m->rotate(rot, x, y, z);
     65 }
     66 static void SC_MatrixScale(Matrix4x4 *m, float x, float y, float z) {
     67     m->scale(x, y, z);
     68 }
     69 static void SC_MatrixTranslate(Matrix4x4 *m, float x, float y, float z) {
     70     m->translate(x, y, z);
     71 }
     72 
     73 static void SC_MatrixLoadOrtho(Matrix4x4 *m, float l, float r, float b, float t, float n, float f) {
     74     m->loadOrtho(l, r, b, t, n, f);
     75 }
     76 static void SC_MatrixLoadFrustum(Matrix4x4 *m, float l, float r, float b, float t, float n, float f) {
     77     m->loadFrustum(l, r, b, t, n, f);
     78 }
     79 static void SC_MatrixLoadPerspective(Matrix4x4 *m, float fovy, float aspect, float near, float far) {
     80     m->loadPerspective(fovy, aspect, near, far);
     81 }
     82 
     83 static bool SC_MatrixInverse_4x4(Matrix4x4 *m) {
     84     return m->inverse();
     85 }
     86 static bool SC_MatrixInverseTranspose_4x4(Matrix4x4 *m) {
     87     return m->inverseTranspose();
     88 }
     89 static void SC_MatrixTranspose_4x4(Matrix4x4 *m) {
     90     m->transpose();
     91 }
     92 static void SC_MatrixTranspose_3x3(Matrix3x3 *m) {
     93     m->transpose();
     94 }
     95 static void SC_MatrixTranspose_2x2(Matrix2x2 *m) {
     96     m->transpose();
     97 }
     98 
     99 float SC_randf2(float min, float max) {
    100     float r = (float)rand();
    101     r /= RAND_MAX;
    102     r = r * (max - min) + min;
    103     return r;
    104 }
    105 
    106 static float SC_frac(float v) {
    107     int i = (int)floor(v);
    108     return fmin(v - i, 0x1.fffffep-1f);
    109 }
    110 
    111 #ifdef RS_COMPATIBILITY_LIB
    112 EXPORT_F32_FN_F32(acosf)
    113 EXPORT_F32_FN_F32(acoshf)
    114 EXPORT_F32_FN_F32(asinf)
    115 EXPORT_F32_FN_F32(asinhf)
    116 EXPORT_F32_FN_F32(atanf)
    117 EXPORT_F32_FN_F32_F32(atan2f)
    118 EXPORT_F32_FN_F32(atanhf)
    119 EXPORT_F32_FN_F32(cbrtf)
    120 EXPORT_F32_FN_F32(ceilf)
    121 EXPORT_F32_FN_F32_F32(copysignf)
    122 EXPORT_F32_FN_F32(cosf)
    123 EXPORT_F32_FN_F32(coshf)
    124 EXPORT_F32_FN_F32(erfcf)
    125 EXPORT_F32_FN_F32(erff)
    126 EXPORT_F32_FN_F32(expf)
    127 EXPORT_F32_FN_F32(exp2f)
    128 EXPORT_F32_FN_F32(expm1f)
    129 EXPORT_F32_FN_F32_F32(fdimf)
    130 EXPORT_F32_FN_F32(floorf)
    131 float SC_fmaf(float u, float t, float v) {return fmaf(u, t, v);}
    132 EXPORT_F32_FN_F32_F32(fmaxf)
    133 EXPORT_F32_FN_F32_F32(fminf)
    134 EXPORT_F32_FN_F32_F32(fmodf)
    135 float SC_frexpf(float v, int* ptr) {return frexpf(v, ptr);}
    136 EXPORT_F32_FN_F32_F32(hypotf)
    137 EXPORT_F32_FN_F32(ilogbf)
    138 float SC_ldexpf(float v, int i) {return ldexpf(v, i);}
    139 EXPORT_F32_FN_F32(lgammaf)
    140 float SC_lgammaf_r(float v, int* ptr) {return lgammaf_r(v, ptr);}
    141 EXPORT_F32_FN_F32(logf)
    142 EXPORT_F32_FN_F32(log10f)
    143 EXPORT_F32_FN_F32(log1pf)
    144 EXPORT_F32_FN_F32(logbf)
    145 float SC_modff(float v, float* ptr) {return modff(v, ptr);}
    146 EXPORT_F32_FN_F32_F32(nextafterf)
    147 EXPORT_F32_FN_F32_F32(powf)
    148 EXPORT_F32_FN_F32_F32(remainderf)
    149 float SC_remquof(float t, float v, int* ptr) {return remquof(t, v, ptr);}
    150 EXPORT_F32_FN_F32(rintf)
    151 EXPORT_F32_FN_F32(roundf)
    152 EXPORT_F32_FN_F32(sinf)
    153 EXPORT_F32_FN_F32(sinhf)
    154 EXPORT_F32_FN_F32(sqrtf)
    155 EXPORT_F32_FN_F32(tanf)
    156 EXPORT_F32_FN_F32(tanhf)
    157 EXPORT_F32_FN_F32(truncf)
    158 #endif
    159 
    160 //////////////////////////////////////////////////////////////////////////////
    161 // Class implementation
    162 //////////////////////////////////////////////////////////////////////////////
    163 
    164 // llvm name mangling ref
    165 //  <builtin-type> ::= v  # void
    166 //                 ::= b  # bool
    167 //                 ::= c  # char
    168 //                 ::= a  # signed char
    169 //                 ::= h  # unsigned char
    170 //                 ::= s  # short
    171 //                 ::= t  # unsigned short
    172 //                 ::= i  # int
    173 //                 ::= j  # unsigned int
    174 //                 ::= l  # long
    175 //                 ::= m  # unsigned long
    176 //                 ::= x  # long long, __int64
    177 //                 ::= y  # unsigned long long, __int64
    178 //                 ::= f  # float
    179 //                 ::= d  # double
    180 
    181 static RsdCpuReference::CpuSymbol gSyms[] = {
    182     { "_Z4acosf", (void *)&acosf, true },
    183     { "_Z5acoshf", (void *)&acoshf, true },
    184     { "_Z4asinf", (void *)&asinf, true },
    185     { "_Z5asinhf", (void *)&asinhf, true },
    186     { "_Z4atanf", (void *)&atanf, true },
    187     { "_Z5atan2ff", (void *)&atan2f, true },
    188     { "_Z5atanhf", (void *)&atanhf, true },
    189     { "_Z4cbrtf", (void *)&cbrtf, true },
    190     { "_Z4ceilf", (void *)&ceilf, true },
    191     { "_Z8copysignff", (void *)&copysignf, true },
    192     { "_Z3cosf", (void *)&cosf, true },
    193     { "_Z4coshf", (void *)&coshf, true },
    194     { "_Z4erfcf", (void *)&erfcf, true },
    195     { "_Z3erff", (void *)&erff, true },
    196     { "_Z3expf", (void *)&expf, true },
    197     { "_Z4exp2f", (void *)&exp2f, true },
    198     { "_Z5expm1f", (void *)&expm1f, true },
    199     { "_Z4fdimff", (void *)&fdimf, true },
    200     { "_Z5floorf", (void *)&floorf, true },
    201     { "_Z3fmafff", (void *)&fmaf, true },
    202     { "_Z4fmaxff", (void *)&fmaxf, true },
    203     { "_Z4fminff", (void *)&fminf, true },  // float fmin(float, float)
    204     { "_Z4fmodff", (void *)&fmodf, true },
    205     { "_Z5frexpfPi", (void *)&frexpf, true },
    206     { "_Z5hypotff", (void *)&hypotf, true },
    207     { "_Z5ilogbf", (void *)&ilogbf, true },
    208     { "_Z5ldexpfi", (void *)&ldexpf, true },
    209     { "_Z6lgammaf", (void *)&lgammaf, true },
    210     { "_Z6lgammafPi", (void *)&lgammaf_r, true },
    211     { "_Z3logf", (void *)&logf, true },
    212     { "_Z5log10f", (void *)&log10f, true },
    213     { "_Z5log1pf", (void *)&log1pf, true },
    214     { "_Z4logbf", (void *)&logbf, true },
    215     { "_Z4modffPf", (void *)&modff, true },
    216     //{ "_Z3nanj", (void *)&SC_nan, true },
    217     { "_Z9nextafterff", (void *)&nextafterf, true },
    218     { "_Z3powff", (void *)&powf, true },
    219     { "_Z9remainderff", (void *)&remainderf, true },
    220     { "_Z6remquoffPi", (void *)&remquof, true },
    221     { "_Z4rintf", (void *)&rintf, true },
    222     { "_Z5roundf", (void *)&roundf, true },
    223     { "_Z3sinf", (void *)&sinf, true },
    224     { "_Z4sinhf", (void *)&sinhf, true },
    225     { "_Z4sqrtf", (void *)&sqrtf, true },
    226     { "_Z3tanf", (void *)&tanf, true },
    227     { "_Z4tanhf", (void *)&tanhf, true },
    228     { "_Z6tgammaf", (void *)&tgammaf, true },
    229     { "_Z5truncf", (void *)&truncf, true },
    230 
    231     //{ "smoothstep", (void *)&, true },
    232 
    233     // matrix
    234     { "_Z18rsMatrixLoadRotateP12rs_matrix4x4ffff", (void *)&SC_MatrixLoadRotate, true },
    235     { "_Z17rsMatrixLoadScaleP12rs_matrix4x4fff", (void *)&SC_MatrixLoadScale, true },
    236     { "_Z21rsMatrixLoadTranslateP12rs_matrix4x4fff", (void *)&SC_MatrixLoadTranslate, true },
    237     { "_Z14rsMatrixRotateP12rs_matrix4x4ffff", (void *)&SC_MatrixRotate, true },
    238     { "_Z13rsMatrixScaleP12rs_matrix4x4fff", (void *)&SC_MatrixScale, true },
    239     { "_Z17rsMatrixTranslateP12rs_matrix4x4fff", (void *)&SC_MatrixTranslate, true },
    240 
    241     { "_Z17rsMatrixLoadOrthoP12rs_matrix4x4ffffff", (void *)&SC_MatrixLoadOrtho, true },
    242     { "_Z19rsMatrixLoadFrustumP12rs_matrix4x4ffffff", (void *)&SC_MatrixLoadFrustum, true },
    243     { "_Z23rsMatrixLoadPerspectiveP12rs_matrix4x4ffff", (void *)&SC_MatrixLoadPerspective, true },
    244 
    245     { "_Z15rsMatrixInverseP12rs_matrix4x4", (void *)&SC_MatrixInverse_4x4, true },
    246     { "_Z24rsMatrixInverseTransposeP12rs_matrix4x4", (void *)&SC_MatrixInverseTranspose_4x4, true },
    247     { "_Z17rsMatrixTransposeP12rs_matrix4x4", (void *)&SC_MatrixTranspose_4x4, true },
    248     { "_Z17rsMatrixTransposeP12rs_matrix3x3", (void *)&SC_MatrixTranspose_3x3, true },
    249     { "_Z17rsMatrixTransposeP12rs_matrix2x2", (void *)&SC_MatrixTranspose_2x2, true },
    250 
    251     // RS Math
    252     { "_Z6rsRandff", (void *)&SC_randf2, true },
    253     { "_Z6rsFracf", (void *)&SC_frac, true },
    254 
    255     { NULL, NULL, false }
    256 };
    257 
    258 const RsdCpuReference::CpuSymbol * RsdCpuScriptImpl::lookupSymbolMath(const char *sym) {
    259     const RsdCpuReference::CpuSymbol *syms = gSyms;
    260 
    261     while (syms->fnPtr) {
    262         if (!strcmp(syms->name, sym)) {
    263             return syms;
    264         }
    265         syms++;
    266     }
    267     return NULL;
    268 }
    269 
    270