math.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. Copyright (C) 2004 Michael Liebscher
  3. Copyright (C) 1997-2001 Id Software, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. /*
  17. * math.c: Math routines.
  18. *
  19. * Author: Michael Liebscher <johnnycanuck@users.sourceforge.net>
  20. * Date: 2004
  21. *
  22. * Acknowledgement:
  23. * This code was derived from Quake II, and was originally
  24. * written by Id Software, Inc.
  25. *
  26. */
  27. #include "../wolfiphone.h"
  28. ////////////////////////////
  29. //
  30. // Square Root
  31. //
  32. ////////////////////////////
  33. /*
  34. -----------------------------------------------------------------------------
  35. Function: _sqrtf -Calculates the square root.
  36. Parameters: x -[in] Nonnegative floating-point value
  37. Returns: The square-root of x.
  38. Notes:
  39. -----------------------------------------------------------------------------
  40. */
  41. PUBLIC float _sqrtf( float x )
  42. {
  43. return (float)sqrt( x );
  44. }
  45. /*
  46. -----------------------------------------------------------------------------
  47. Function: _3DNow_Sqrt -Calculates the square root.
  48. Parameters: x -[in] Nonnegative floating-point value
  49. Returns: The square-root of x.
  50. Notes: 15-Bit Precision
  51. -----------------------------------------------------------------------------
  52. */
  53. #if __i386__
  54. PUBLIC float _3DNow_Sqrt( float x )
  55. {
  56. float root = 0.f;
  57. #if( _MSC_VER || __WATCOMC__ )
  58. __asm
  59. {
  60. femms
  61. movd mm0, x
  62. pfrsqrt mm1, mm0
  63. punpckldq mm0, mm0
  64. pfmul mm0, mm1
  65. movd root, mm0
  66. femms
  67. }
  68. #endif
  69. return root;
  70. }
  71. /*
  72. -----------------------------------------------------------------------------
  73. Function: _SSE_Sqrt -Calculates the square root.
  74. Parameters: x -[in] Nonnegative floating-point value
  75. Returns: The square-root of x.
  76. Notes:
  77. -----------------------------------------------------------------------------
  78. */
  79. float _SSE_Sqrt( float x )
  80. {
  81. float root = 0.f;
  82. #if( _MSC_VER || __WATCOMC__ )
  83. __asm
  84. {
  85. sqrtss xmm0, x
  86. movss root, xmm0
  87. }
  88. #endif
  89. return root;
  90. }
  91. #endif /* __i386__ */
  92. ////////////////////////////
  93. //
  94. // End Square Root
  95. //
  96. ////////////////////////////
  97. float (*pfSqrt)( float x ) = _sqrtf;
  98. /*
  99. -----------------------------------------------------------------------------
  100. Function:
  101. Parameters:
  102. Returns:
  103. Notes:
  104. -----------------------------------------------------------------------------
  105. */
  106. PUBLIC int my_log2( int val )
  107. {
  108. int answer = 0;
  109. while( ( val >>= 1 ) != 0 )
  110. {
  111. answer++;
  112. }
  113. return answer;
  114. }
  115. /*
  116. -----------------------------------------------------------------------------
  117. Function: CalcFov -Calculate the field of view.
  118. Parameters:fov_x -[in] Must be within 1 and 179 degrees.
  119. width -[in] Width of viewing area.
  120. height -[in] Height of viewing area.
  121. Returns: The field of view in degrees.
  122. Notes:
  123. -----------------------------------------------------------------------------
  124. */
  125. PUBLIC float CalcFov( float fov_x, float width, float height )
  126. {
  127. if( fov_x < 1 || fov_x > 179 )
  128. {
  129. Com_Error( ERR_DROP, "Bad fov: %f", fov_x );
  130. }
  131. return (float)RAD2DEG( atan( height / ( width / tan( fov_x / 360 * M_PI ) ) ) ) * 2;
  132. }
  133. /*
  134. -----------------------------------------------------------------------------
  135. Function: MathLib_Init -Initialize optimized math routines.
  136. Parameters: Nothing.
  137. Returns: Nothing.
  138. Notes:
  139. -----------------------------------------------------------------------------
  140. */
  141. PUBLIC void MathLib_Init( void )
  142. {
  143. Com_Printf( "Initializing Math Module\n" );
  144. #if 0//__i386__
  145. if( main_cpu_s.b3DNow )
  146. {
  147. // pfSqrt = _3DNow_Sqrt;
  148. Com_Printf( "...using 3DNow!\n" );
  149. }
  150. if( main_cpu_s.bSSE )
  151. {
  152. // pfSqrt = _SSE_Sqrt;
  153. Com_Printf( "...using SSE\n" );
  154. }
  155. #endif
  156. }