Home | History | Annotate | Download | only in libagl
      1 /* libs/opengles/fp.cpp
      2 **
      3 ** Copyright 2006, The Android Open Source Project
      4 **
      5 ** Licensed under the Apache License, Version 2.0 (the "License");
      6 ** you may not use this file except in compliance with the License.
      7 ** You may obtain a copy of the License at
      8 **
      9 **     http://www.apache.org/licenses/LICENSE-2.0
     10 **
     11 ** Unless required by applicable law or agreed to in writing, software
     12 ** distributed under the License is distributed on an "AS IS" BASIS,
     13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 ** See the License for the specific language governing permissions and
     15 ** limitations under the License.
     16 */
     17 
     18 #include "fp.h"
     19 
     20 // ----------------------------------------------------------------------------
     21 
     22 #if !(defined(__arm__) || (defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6))
     23 GGLfixed gglFloatToFixed(float v) {
     24     return GGLfixed(floorf(v * 65536.0f + 0.5f));
     25 }
     26 #endif
     27 
     28 // ----------------------------------------------------------------------------
     29 
     30 namespace android {
     31 
     32 namespace gl {
     33 
     34 GLfloat fixedToFloat(GLfixed x)
     35 {
     36 #if DEBUG_USE_FLOATS
     37     return x / 65536.0f;
     38 #else
     39     if (!x) return 0;
     40     const uint32_t s = x & 0x80000000;
     41     union {
     42         uint32_t i;
     43         float f;
     44     };
     45     i = s ? -x : x;
     46     const int c = gglClz(i) - 8;
     47     i = (c>=0) ? (i<<c) : (i>>-c);
     48     const uint32_t e = 134 - c;
     49     i &= ~0x800000;
     50     i |= e<<23;
     51     i |= s;
     52     return f;
     53 #endif
     54 }
     55 
     56 float sinef(float x)
     57 {
     58     const float A =   1.0f / (2.0f*M_PI);
     59     const float B = -16.0f;
     60     const float C =   8.0f;
     61 
     62     // scale angle for easy argument reduction
     63     x *= A;
     64 
     65     if (fabsf(x) >= 0.5f) {
     66         // Argument reduction
     67         x = x - ceilf(x + 0.5f) + 1.0f;
     68     }
     69 
     70     const float y = B*x*fabsf(x) + C*x;
     71     return 0.2215f * (y*fabsf(y) - y) + y;
     72 }
     73 
     74 float cosinef(float x)
     75 {
     76     return sinef(x + float(M_PI/2));
     77 }
     78 
     79 void sincosf(GLfloat angle, GLfloat* s, GLfloat* c) {
     80     *s = sinef(angle);
     81     *c = cosinef(angle);
     82 }
     83 
     84 }; // namespace fp_utils
     85 
     86 // ----------------------------------------------------------------------------
     87 }; // namespace android
     88