fp.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* libs/opengles/fp.h
  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. #ifndef ANDROID_OPENGLES_FP_H
  18. #define ANDROID_OPENGLES_FP_H
  19. #include <stdint.h>
  20. #include <stddef.h>
  21. #include <sys/types.h>
  22. #include <math.h>
  23. #include <private/pixelflinger/ggl_context.h>
  24. #include <GLES/gl.h>
  25. #define DEBUG_USE_FLOATS 0
  26. // ----------------------------------------------------------------------------
  27. extern "C" GLfixed gglFloatToFixed(float f) __attribute__((const));
  28. // ----------------------------------------------------------------------------
  29. namespace android {
  30. namespace gl {
  31. GLfloat fixedToFloat(GLfixed) CONST;
  32. void sincosf(GLfloat angle, GLfloat* s, GLfloat* c);
  33. float sinef(GLfloat x) CONST;
  34. float cosinef(GLfloat x) CONST;
  35. inline bool cmpf(GLfloat a, GLfloat b) CONST;
  36. inline bool isZerof(GLfloat) CONST;
  37. inline bool isOnef(GLfloat) CONST;
  38. inline int isZeroOrNegativef(GLfloat) CONST;
  39. inline int exponent(GLfloat) CONST;
  40. inline int32_t mantissa(GLfloat) CONST;
  41. inline GLfloat clampToZerof(GLfloat) CONST;
  42. inline GLfloat reciprocalf(GLfloat) CONST;
  43. inline GLfloat rsqrtf(GLfloat) CONST;
  44. inline GLfloat sqrf(GLfloat) CONST;
  45. inline GLfloat addExpf(GLfloat v, int e) CONST;
  46. inline GLfloat mul2f(GLfloat v) CONST;
  47. inline GLfloat div2f(GLfloat v) CONST;
  48. inline GLfloat absf(GLfloat v) CONST;
  49. /*
  50. * float fastexpf(float) : a fast approximation of expf(x)
  51. * give somewhat accurate results for -88 <= x <= 88
  52. *
  53. * exp(x) = 2^(x/ln(2))
  54. * we use the properties of float encoding
  55. * to get a fast 2^ and linear interpolation
  56. *
  57. */
  58. inline float fastexpf(float y) __attribute__((const));
  59. inline float fastexpf(float y)
  60. {
  61. union {
  62. float r;
  63. int32_t i;
  64. } u;
  65. // 127*ln(2) = 88
  66. if (y < -88.0f) {
  67. u.r = 0.0f;
  68. } else if (y > 88.0f) {
  69. u.r = INFINITY;
  70. } else {
  71. const float kOneOverLogTwo = (1L<<23) / M_LN2;
  72. const int32_t kExponentBias = 127L<<23;
  73. const int32_t e = int32_t(y*kOneOverLogTwo);
  74. u.i = e + kExponentBias;
  75. }
  76. return u.r;
  77. }
  78. bool cmpf(GLfloat a, GLfloat b) {
  79. #if DEBUG_USE_FLOATS
  80. return a == b;
  81. #else
  82. union {
  83. float f;
  84. uint32_t i;
  85. } ua, ub;
  86. ua.f = a;
  87. ub.f = b;
  88. return ua.i == ub.i;
  89. #endif
  90. }
  91. bool isZerof(GLfloat v) {
  92. #if DEBUG_USE_FLOATS
  93. return v == 0;
  94. #else
  95. union {
  96. float f;
  97. int32_t i;
  98. };
  99. f = v;
  100. return (i<<1) == 0;
  101. #endif
  102. }
  103. bool isOnef(GLfloat v) {
  104. return cmpf(v, 1.0f);
  105. }
  106. int isZeroOrNegativef(GLfloat v) {
  107. #if DEBUG_USE_FLOATS
  108. return v <= 0;
  109. #else
  110. union {
  111. float f;
  112. int32_t i;
  113. };
  114. f = v;
  115. return isZerof(v) | (i>>31);
  116. #endif
  117. }
  118. int exponent(GLfloat v) {
  119. union {
  120. float f;
  121. uint32_t i;
  122. };
  123. f = v;
  124. return ((i << 1) >> 24) - 127;
  125. }
  126. int32_t mantissa(GLfloat v) {
  127. union {
  128. float f;
  129. uint32_t i;
  130. };
  131. f = v;
  132. if (!(i&0x7F800000)) return 0;
  133. const int s = i >> 31;
  134. i |= (1L<<23);
  135. i &= ~0xFF000000;
  136. return s ? -i : i;
  137. }
  138. GLfloat clampToZerof(GLfloat v) {
  139. #if DEBUG_USE_FLOATS
  140. return v<0 ? 0 : (v>1 ? 1 : v);
  141. #else
  142. union {
  143. float f;
  144. int32_t i;
  145. };
  146. f = v;
  147. i &= ~(i>>31);
  148. return f;
  149. #endif
  150. }
  151. GLfloat reciprocalf(GLfloat v) {
  152. // XXX: do better
  153. return 1.0f / v;
  154. }
  155. GLfloat rsqrtf(GLfloat v) {
  156. // XXX: do better
  157. return 1.0f / sqrtf(v);
  158. }
  159. GLfloat sqrf(GLfloat v) {
  160. // XXX: do better
  161. return v*v;
  162. }
  163. GLfloat addExpf(GLfloat v, int e) {
  164. union {
  165. float f;
  166. int32_t i;
  167. };
  168. f = v;
  169. if (i<<1) { // XXX: deal with over/underflow
  170. i += int32_t(e)<<23;
  171. }
  172. return f;
  173. }
  174. GLfloat mul2f(GLfloat v) {
  175. #if DEBUG_USE_FLOATS
  176. return v*2;
  177. #else
  178. return addExpf(v, 1);
  179. #endif
  180. }
  181. GLfloat div2f(GLfloat v) {
  182. #if DEBUG_USE_FLOATS
  183. return v*0.5f;
  184. #else
  185. return addExpf(v, -1);
  186. #endif
  187. }
  188. GLfloat absf(GLfloat v) {
  189. #if DEBUG_USE_FLOATS
  190. return v<0 ? -v : v;
  191. #else
  192. union {
  193. float f;
  194. int32_t i;
  195. };
  196. f = v;
  197. i &= ~0x80000000;
  198. return f;
  199. #endif
  200. }
  201. }; // namespace gl
  202. // ----------------------------------------------------------------------------
  203. }; // namespace android
  204. #endif // ANDROID_OPENGLES_FP_H