PageRenderTime 23ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/trickmath.c

https://github.com/AlbertVeli/Carnival
C | 138 lines | 61 code | 19 blank | 58 comment | 6 complexity | 728a3e71252e310f63057d644e2d51a5 MD5 | raw file
  1. /**
  2. * @file trickmath.c
  3. * @brief Tricky math routines.
  4. */
  5. /************************************************************************
  6. * ___ _ _
  7. * B / __\__ _ _ __ _ __ (_)_ ____ _| |
  8. * O / / / _` | '__| '_ \| \ \ / / _` | |
  9. * O / /__| (_| | | | | | | |\ V / (_| | |
  10. * M \____/\__,_|_| |_| |_|_| \_/ \__,_|_|
  11. *
  12. * $Id: $
  13. *
  14. * Authors
  15. * - Albert Veli
  16. *
  17. * Copyright (C) 2007 Albert Veli
  18. *
  19. * ------------------------------
  20. *
  21. * This file is part of Carnival
  22. *
  23. * Carnival is free software; you can redistribute it and/or modify
  24. * it under the terms of the GNU General Public License as published by
  25. * the Free Software Foundation; either version 3 of the License, or
  26. * (at your option) any later version.
  27. *
  28. * Carnival is distributed in the hope that it will be useful,
  29. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31. * GNU General Public License for more details.
  32. *
  33. * You should have received a copy of the GNU General Public License
  34. * along with this program; if not, write to the Free Software Foundation,
  35. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  36. *
  37. ************************************************************************/
  38. /* ----------------------------------------------
  39. * Local variables
  40. * ----------------------------------------------
  41. */
  42. /* Calculated with tools/sin.c */
  43. static float sintab[65] = {
  44. 0.000000, 0.024541, 0.049068, 0.073565,
  45. 0.098017, 0.122411, 0.146730, 0.170962,
  46. 0.195090, 0.219101, 0.242980, 0.266713,
  47. 0.290285, 0.313682, 0.336890, 0.359895,
  48. 0.382683, 0.405241, 0.427555, 0.449611,
  49. 0.471397, 0.492898, 0.514103, 0.534998,
  50. 0.555570, 0.575808, 0.595699, 0.615232,
  51. 0.634393, 0.653173, 0.671559, 0.689541,
  52. 0.707107, 0.724247, 0.740951, 0.757209,
  53. 0.773010, 0.788346, 0.803208, 0.817585,
  54. 0.831470, 0.844854, 0.857729, 0.870087,
  55. 0.881921, 0.893224, 0.903989, 0.914210,
  56. 0.923880, 0.932993, 0.941544, 0.949528,
  57. 0.956940, 0.963776, 0.970031, 0.975702,
  58. 0.980785, 0.985278, 0.989177, 0.992480,
  59. 0.995185, 0.997290, 0.998795, 0.999699,
  60. 1.000000
  61. };
  62. /* ----------------------------------------------
  63. * Exported functions
  64. * ----------------------------------------------
  65. */
  66. /* #if this out because it causes compiler warnings */
  67. #if 0
  68. /* Taken from Quake III source code */
  69. float Q_rsqrt(float number)
  70. {
  71. long i;
  72. float x2, y;
  73. const float threehalfs = 1.5F;
  74. x2 = number * 0.5F;
  75. y = number;
  76. i = *(long *)&y; // evil floating point bit level hacking
  77. i = 0x5f3759df - (i >> 1); // what the fuck?
  78. y = * (float *)&i;
  79. y = y * (threehalfs - (x2 * y * y)); // 1st iteration
  80. return y;
  81. }
  82. #endif
  83. /* Lookup in sintab (taken from Rockbox source code) */
  84. inline float u8sin(unsigned char v)
  85. {
  86. if (v < 65) {
  87. return sintab[v];
  88. } else if (v < 129) {
  89. return sintab[128 - v];
  90. } else if (v < 193) {
  91. return -sintab[v - 128];
  92. } else {
  93. return -sintab[256 - v];
  94. }
  95. }
  96. inline float u8cos(unsigned char v)
  97. {
  98. return u8sin(v - 64);
  99. }
  100. /* interpolate from sintab */
  101. inline float u8sinf(float v)
  102. {
  103. float s1 = u8sin((unsigned char)v);
  104. float s2 = u8sin((unsigned char)v + 1);
  105. float d = s2 - s1;
  106. return s1 + ((float)v - ((int)v)) * d;
  107. }
  108. inline float u8cosf(float v)
  109. {
  110. return u8sinf(v - 64);
  111. }
  112. /**
  113. * GNU Emacs settings: K&R with 3 spaces indent.
  114. * Local Variables:
  115. * c-file-style: "k&r"
  116. * c-basic-offset: 3
  117. * indent-tabs-mode: nil
  118. * End:
  119. */