123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- /* libs/opengles/fp.h
- **
- ** Copyright 2006, The Android Open Source Project
- **
- ** Licensed under the Apache License, Version 2.0 (the "License");
- ** you may not use this file except in compliance with the License.
- ** You may obtain a copy of the License at
- **
- ** http://www.apache.org/licenses/LICENSE-2.0
- **
- ** Unless required by applicable law or agreed to in writing, software
- ** distributed under the License is distributed on an "AS IS" BASIS,
- ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- ** See the License for the specific language governing permissions and
- ** limitations under the License.
- */
- #ifndef ANDROID_OPENGLES_FP_H
- #define ANDROID_OPENGLES_FP_H
- #include <stdint.h>
- #include <stddef.h>
- #include <sys/types.h>
- #include <math.h>
- #include <private/pixelflinger/ggl_context.h>
- #include <GLES/gl.h>
- #define DEBUG_USE_FLOATS 0
- // ----------------------------------------------------------------------------
- extern "C" GLfixed gglFloatToFixed(float f) __attribute__((const));
- // ----------------------------------------------------------------------------
- namespace android {
- namespace gl {
- GLfloat fixedToFloat(GLfixed) CONST;
- void sincosf(GLfloat angle, GLfloat* s, GLfloat* c);
- float sinef(GLfloat x) CONST;
- float cosinef(GLfloat x) CONST;
- inline bool cmpf(GLfloat a, GLfloat b) CONST;
- inline bool isZerof(GLfloat) CONST;
- inline bool isOnef(GLfloat) CONST;
- inline int isZeroOrNegativef(GLfloat) CONST;
- inline int exponent(GLfloat) CONST;
- inline int32_t mantissa(GLfloat) CONST;
- inline GLfloat clampToZerof(GLfloat) CONST;
- inline GLfloat reciprocalf(GLfloat) CONST;
- inline GLfloat rsqrtf(GLfloat) CONST;
- inline GLfloat sqrf(GLfloat) CONST;
- inline GLfloat addExpf(GLfloat v, int e) CONST;
- inline GLfloat mul2f(GLfloat v) CONST;
- inline GLfloat div2f(GLfloat v) CONST;
- inline GLfloat absf(GLfloat v) CONST;
- /*
- * float fastexpf(float) : a fast approximation of expf(x)
- * give somewhat accurate results for -88 <= x <= 88
- *
- * exp(x) = 2^(x/ln(2))
- * we use the properties of float encoding
- * to get a fast 2^ and linear interpolation
- *
- */
- inline float fastexpf(float y) __attribute__((const));
- inline float fastexpf(float y)
- {
- union {
- float r;
- int32_t i;
- } u;
- // 127*ln(2) = 88
- if (y < -88.0f) {
- u.r = 0.0f;
- } else if (y > 88.0f) {
- u.r = INFINITY;
- } else {
- const float kOneOverLogTwo = (1L<<23) / M_LN2;
- const int32_t kExponentBias = 127L<<23;
- const int32_t e = int32_t(y*kOneOverLogTwo);
- u.i = e + kExponentBias;
- }
-
- return u.r;
- }
- bool cmpf(GLfloat a, GLfloat b) {
- #if DEBUG_USE_FLOATS
- return a == b;
- #else
- union {
- float f;
- uint32_t i;
- } ua, ub;
- ua.f = a;
- ub.f = b;
- return ua.i == ub.i;
- #endif
- }
- bool isZerof(GLfloat v) {
- #if DEBUG_USE_FLOATS
- return v == 0;
- #else
- union {
- float f;
- int32_t i;
- };
- f = v;
- return (i<<1) == 0;
- #endif
- }
- bool isOnef(GLfloat v) {
- return cmpf(v, 1.0f);
- }
- int isZeroOrNegativef(GLfloat v) {
- #if DEBUG_USE_FLOATS
- return v <= 0;
- #else
- union {
- float f;
- int32_t i;
- };
- f = v;
- return isZerof(v) | (i>>31);
- #endif
- }
- int exponent(GLfloat v) {
- union {
- float f;
- uint32_t i;
- };
- f = v;
- return ((i << 1) >> 24) - 127;
- }
- int32_t mantissa(GLfloat v) {
- union {
- float f;
- uint32_t i;
- };
- f = v;
- if (!(i&0x7F800000)) return 0;
- const int s = i >> 31;
- i |= (1L<<23);
- i &= ~0xFF000000;
- return s ? -i : i;
- }
- GLfloat clampToZerof(GLfloat v) {
- #if DEBUG_USE_FLOATS
- return v<0 ? 0 : (v>1 ? 1 : v);
- #else
- union {
- float f;
- int32_t i;
- };
- f = v;
- i &= ~(i>>31);
- return f;
- #endif
- }
- GLfloat reciprocalf(GLfloat v) {
- // XXX: do better
- return 1.0f / v;
- }
- GLfloat rsqrtf(GLfloat v) {
- // XXX: do better
- return 1.0f / sqrtf(v);
- }
- GLfloat sqrf(GLfloat v) {
- // XXX: do better
- return v*v;
- }
- GLfloat addExpf(GLfloat v, int e) {
- union {
- float f;
- int32_t i;
- };
- f = v;
- if (i<<1) { // XXX: deal with over/underflow
- i += int32_t(e)<<23;
- }
- return f;
- }
- GLfloat mul2f(GLfloat v) {
- #if DEBUG_USE_FLOATS
- return v*2;
- #else
- return addExpf(v, 1);
- #endif
- }
- GLfloat div2f(GLfloat v) {
- #if DEBUG_USE_FLOATS
- return v*0.5f;
- #else
- return addExpf(v, -1);
- #endif
- }
- GLfloat absf(GLfloat v) {
- #if DEBUG_USE_FLOATS
- return v<0 ? -v : v;
- #else
- union {
- float f;
- int32_t i;
- };
- f = v;
- i &= ~0x80000000;
- return f;
- #endif
- }
- }; // namespace gl
- // ----------------------------------------------------------------------------
- }; // namespace android
- #endif // ANDROID_OPENGLES_FP_H
|