PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Neo-Library-Mathmatics.ads

https://github.com/AdaDoom3/AdaDoom3
Ada | 204 lines | 167 code | 4 blank | 33 comment | 1 complexity | d0763d7d802309b43f0a72b524d9ffb7 MD5 | raw file
Possible License(s): GPL-3.0
  1. --
  2. --
  3. --
  4. --
  5. --
  6. --
  7. --
  8. --
  9. --
  10. --
  11. --
  12. --
  13. --
  14. --
  15. --
  16. --
  17. package Neo.Library.Mathmatics
  18. is
  19. ---------------
  20. -- Constants --
  21. ---------------
  22. DO_USE_Q_RSQRT : constant Boolean := False; -- http://eggroll.unbsj.ca/rsqrt/rsqrt.pdf
  23. LOOKUP_BITS : constant := 8,
  24. EXP_POS : constant := 23,
  25. EXP_BIAS : constant := 127,
  26. LOOKUP_POS : constant := (EXP_POS-LOOKUP_BITS),
  27. SEED_POS : constant := (EXP_POS-8),
  28. SQRT_TABLE_SIZE : constant := (2<<LOOKUP_BITS),
  29. LOOKUP_MASK : constant := (SQRT_TABLE_SIZE-1)
  30. MULTIPLIER_FOR_DEG2RAD: constant :; -- degrees to radians multiplier
  31. MULTIPLIER_FOR_RAD2DEG: constant :; -- radians to degrees multiplier
  32. MULTIPLIER_FOR_SEC2MS: constant :; -- seconds to milliseconds multiplier
  33. MULTIPLIER_FOR_MS2SEC: constant :; -- milliseconds to seconds multiplier
  34. INFINITY; -- huge number which should be larger than any valid number used
  35. FLT_EPSILON; -- smallest positive number such that 1.0+FLT_EPSILON != 1.0
  36. FLT_SMALLEST_NON_DENORMAL; -- smallest non-denormal 32-bit floating point value
  37. static const __m128 SIMD_SP_zero;
  38. static const __m128 SIMD_SP_255;
  39. static const __m128 SIMD_SP_min_char;
  40. static const __m128 SIMD_SP_max_char;
  41. static const __m128 SIMD_SP_min_short;
  42. static const __m128 SIMD_SP_max_short;
  43. static const __m128 SIMD_SP_smallestNonDenorm;
  44. static const __m128 SIMD_SP_tiny;
  45. static const __m128 SIMD_SP_rsqrt_c0;
  46. static const __m128 SIMD_SP_rsqrt_c1;
  47. -----------------
  48. -- Subprograms --
  49. -----------------
  50. procedure Initialize; -- Calculate tables
  51. function Inverse_Square_Root(
  52. Number : in Float_4_Real;
  53. Do_Use_16_Bit_Precision : in Boolean := False)
  54. return Float_4_Real;
  55. function Square_Root(
  56. Number : in Float_4_Real;
  57. Do_Use_16_Bit_Precision : in Boolean := False)
  58. return Float_4_Real;
  59. function Sine(
  60. Angle : in Float_4_Real;
  61. Do_Use_16_Bit_Precision : in Boolean := False)
  62. return Float_4_Real;
  63. function Cosine(
  64. Angle : in Float_4_Real;
  65. Do_Use_16_Bit_Precision : in Boolean := False)
  66. return Float_4_Real;
  67. procedure Sine_And_Cosine(
  68. Angle : in Float_4_Real;
  69. Sine_Result : in out Float_4_Real;
  70. Cosine_Result : in out Float_4_Real;
  71. Do_Use_16_Bit_Precision : in Boolean := False);
  72. function Tangent(
  73. Angle : in Float_4_Real;
  74. Do_Use_16_Bit_Precision : in Boolean := False)
  75. return Float_4_Real;
  76. function Arcsine(
  77. Angle : in Float_4_Real;
  78. Do_Use_16_Bit_Precision : in Boolean := False)
  79. return Float_4_Real;
  80. function Arccosine(
  81. Angle : in Float_4_Real;
  82. Do_Use_16_Bit_Precision : in Boolean := False)
  83. return Float_4_Real;
  84. function Arctangent(
  85. Angle : in Float_4_Real;
  86. Do_Use_16_Bit_Precision : in Boolean := False)
  87. return Float_4_Real;
  88. function Raise_To_Power(
  89. Number : in Float_4_Real;
  90. Power : in Float_4_Real;
  91. Do_Use_16_Bit_Precision : in Boolean := False)
  92. return Float_4_Real;
  93. function Natural_Base_To_Power(
  94. Power : in Float_4_Real;
  95. Do_Use_16_Bit_Precision : in Boolean := False)
  96. return Float_4_Real;
  97. function Natural_Logarithm(
  98. Number : in Float_4_Real;
  99. Do_Use_16_Bit_Precision : in Boolean := False)
  100. return Float_4_Real;
  101. function Integral_To_Power(
  102. Integral : in Integer_4_Signed;
  103. Power : in Integer_4_Signed)
  104. return Integer_4_Signed;
  105. function Integral_Base_Two_Logarithm(
  106. Number : in Float_4_Real)
  107. return Integer_4_Signed;
  108. function Integral_Base_Two_Logarithm(
  109. Number : in Integer_4_Signed)
  110. return Integer_4_Signed;
  111. function Get_Minimum_Number_Of_Bits_For_Ceiling(
  112. Number : in Float_4_Real)
  113. return Integer_4_Signed;
  114. function Get_Minimum_Number_Of_Bits(
  115. Number : in Integer_4_Signed)
  116. return Integer_4_Signed;
  117. static int MaskForFloatSign( float f );-- returns 0x00000000 if x >= 0.0f and returns 0xFFFFFFFF if x <= -0.0f
  118. static int MaskForIntegerSign( int i );-- returns 0x00000000 if x >= 0 and returns 0xFFFFFFFF if x < 0
  119. function Is_Power_Of_Two(
  120. Number : in Integer_4_Signed)
  121. return Boolean;
  122. function Get_Number_Of_Bits(
  123. Number : in Integer_4_Signed;
  124. Value : in Boolean := True)
  125. return Integer_4_Signed;
  126. function Reverse_Bits(
  127. Number : in Integer_4_Signed)
  128. return Integer_4_Signed;
  129. function Absolute_Value(
  130. Number : in Integer_4_Signed)
  131. return Integer_4_Natural;
  132. function Absolute_Value(
  133. Number : in Float_4_Real)
  134. return Float_4_Natural;
  135. function Round_To_Floor(
  136. Number : in Float_4_Real)
  137. return Float_4_Real;
  138. function Round_To_Ceiling(
  139. Number : in Float_4_Real)
  140. return Float_4_Real;
  141. static int FloorPowerOfTwo( int x ); -- round x down to the nearest power of 2
  142. static int CeilPowerOfTwo( int x ); -- round x up to the nearest power of 2
  143. static int BitCount( int x ); -- returns the number of 1 bits in x
  144. static int BitReverse( int x ); -- returns the bit reverse of x
  145. static int Abs( int x ); -- returns the absolute value of the integer value (for reference only)
  146. static float Fabs( float f ); -- returns the absolute value of the floating point value
  147. static float Floor( float f ); -- returns the largest integer that is less than or equal to the given value
  148. static float Ceil( float f ); -- returns the smallest integer that is greater than or equal to the given value
  149. static float Rint( float f ); -- returns the nearest integer
  150. static float Frac( float f ); -- f - Floor( f )
  151. static int Ftoi( float f ); -- float to int conversion
  152. static char Ftoi8( float f ); -- float to char conversion
  153. static short Ftoi16( float f ); -- float to short conversion
  154. static unsigned short Ftoui16( float f ); -- float to unsigned short conversion
  155. static byte Ftob( float f ); -- float to byte conversion, the result is clamped to the range [0-255]
  156. static signed char ClampChar( int i );
  157. static signed short ClampShort( int i );
  158. static int ClampInt( int min, int max, int value );
  159. static float ClampFloat( float min, float max, float value );
  160. static float AngleNormalize360( float angle );
  161. static float AngleNormalize180( float angle );
  162. static float AngleDelta( float angle1, float angle2 );
  163. static int FloatToBits( float f, int exponentBits, int mantissaBits );
  164. static float BitsToFloat( int i, int exponentBits, int mantissaBits );
  165. static int FloatHash( const float *array, const int numFloats );
  166. static float LerpToWithScale( const float cur, const float dest, const float scale );
  167. -------
  168. private
  169. -------
  170. ------------------
  171. -- Enumerations --
  172. ------------------
  173. type Enumerated_Float_Size_In_Bytes
  174. is(
  175. Four_Bytes,
  176. Eight_Bytes,
  177. Twelve_Bytes,
  178. Sixty_Four_Bytes,
  179. One_Hundred_Twenty_Eight);
  180. -------------
  181. -- Records --
  182. -------------
  183. type Record_(
  184. Do_Initialize_To_Float_Default_Over_Integer_Default : Boolean := True)
  185. is record
  186. case Do_Initialize_To_Float_Default_Over_Integer_Default is
  187. when True =>
  188. Value : Float_4_Real := 0.0;
  189. when False =>
  190. Data : Integer_4_Unsigned := 0;
  191. end case;
  192. end record;
  193. pragma Unchecked_Union(Record_);
  194. for Record_'Size
  195. use 4*8;
  196. ---------------
  197. -- Variables --
  198. ---------------
  199. static dword iSqrt[SQRT_TABLE_SIZE];
  200. end Neo.Library.Mathmatics;