fp.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #include "fp.h"
  18. // ----------------------------------------------------------------------------
  19. #if !(defined(__arm__) || (defined(__mips__) && !defined(__LP64__) && __mips_isa_rev < 6))
  20. GGLfixed gglFloatToFixed(float v) {
  21. return GGLfixed(floorf(v * 65536.0f + 0.5f));
  22. }
  23. #endif
  24. // ----------------------------------------------------------------------------
  25. namespace android {
  26. namespace gl {
  27. GLfloat fixedToFloat(GLfixed x)
  28. {
  29. #if DEBUG_USE_FLOATS
  30. return x / 65536.0f;
  31. #else
  32. if (!x) return 0;
  33. const uint32_t s = x & 0x80000000;
  34. union {
  35. uint32_t i;
  36. float f;
  37. };
  38. i = s ? -x : x;
  39. const int c = gglClz(i) - 8;
  40. i = (c>=0) ? (i<<c) : (i>>-c);
  41. const uint32_t e = 134 - c;
  42. i &= ~0x800000;
  43. i |= e<<23;
  44. i |= s;
  45. return f;
  46. #endif
  47. }
  48. float sinef(float x)
  49. {
  50. const float A = 1.0f / (2.0f*M_PI);
  51. const float B = -16.0f;
  52. const float C = 8.0f;
  53. // scale angle for easy argument reduction
  54. x *= A;
  55. if (fabsf(x) >= 0.5f) {
  56. // Argument reduction
  57. x = x - ceilf(x + 0.5f) + 1.0f;
  58. }
  59. const float y = B*x*fabsf(x) + C*x;
  60. return 0.2215f * (y*fabsf(y) - y) + y;
  61. }
  62. float cosinef(float x)
  63. {
  64. return sinef(x + float(M_PI/2));
  65. }
  66. void sincosf(GLfloat angle, GLfloat* s, GLfloat* c) {
  67. *s = sinef(angle);
  68. *c = cosinef(angle);
  69. }
  70. }; // namespace fp_utils
  71. // ----------------------------------------------------------------------------
  72. }; // namespace android