You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
4.6 KiB

4 years ago
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2005-12-24
  5. // Updated : 2011-10-13
  6. // Licence : This source is under MIT License
  7. // File : glm/gtx/integer.inl
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. namespace glm
  10. {
  11. // pow
  12. GLM_FUNC_QUALIFIER int pow(int x, int y)
  13. {
  14. if(y == 0)
  15. return 1;
  16. int result = x;
  17. for(int i = 1; i < y; ++i)
  18. result *= x;
  19. return result;
  20. }
  21. // sqrt: From Christopher J. Musial, An integer square root, Graphics Gems, 1990, page 387
  22. GLM_FUNC_QUALIFIER int sqrt(int x)
  23. {
  24. if(x <= 1) return x;
  25. int NextTrial = x >> 1;
  26. int CurrentAnswer;
  27. do
  28. {
  29. CurrentAnswer = NextTrial;
  30. NextTrial = (NextTrial + x / NextTrial) >> 1;
  31. } while(NextTrial < CurrentAnswer);
  32. return CurrentAnswer;
  33. }
  34. // Henry Gordon Dietz: http://aggregate.org/MAGIC/
  35. namespace _detail
  36. {
  37. GLM_FUNC_QUALIFIER unsigned int ones32(unsigned int x)
  38. {
  39. /* 32-bit recursive reduction using SWAR...
  40. but first step is mapping 2-bit values
  41. into sum of 2 1-bit values in sneaky way
  42. */
  43. x -= ((x >> 1) & 0x55555555);
  44. x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
  45. x = (((x >> 4) + x) & 0x0f0f0f0f);
  46. x += (x >> 8);
  47. x += (x >> 16);
  48. return(x & 0x0000003f);
  49. }
  50. template <>
  51. struct _compute_log2<detail::float_or_int_value::GLM_INT>
  52. {
  53. template <typename T>
  54. GLM_FUNC_QUALIFIER T operator() (T const & Value) const
  55. {
  56. #if(GLM_COMPILER & (GLM_COMPILER_VC | GLM_COMPILER_GCC))
  57. return Value <= T(1) ? T(0) : T(32) - nlz(Value - T(1));
  58. #else
  59. return T(32) - nlz(Value - T(1));
  60. #endif
  61. }
  62. };
  63. }//namespace _detail
  64. // Henry Gordon Dietz: http://aggregate.org/MAGIC/
  65. /*
  66. GLM_FUNC_QUALIFIER unsigned int floor_log2(unsigned int x)
  67. {
  68. x |= (x >> 1);
  69. x |= (x >> 2);
  70. x |= (x >> 4);
  71. x |= (x >> 8);
  72. x |= (x >> 16);
  73. return _detail::ones32(x) >> 1;
  74. }
  75. */
  76. // mod
  77. GLM_FUNC_QUALIFIER int mod(int x, int y)
  78. {
  79. return x - y * (x / y);
  80. }
  81. // factorial (!12 max, integer only)
  82. template <typename genType>
  83. GLM_FUNC_QUALIFIER genType factorial(genType const & x)
  84. {
  85. genType Temp = x;
  86. genType Result;
  87. for(Result = 1; Temp > 1; --Temp)
  88. Result *= Temp;
  89. return Result;
  90. }
  91. template <typename valType>
  92. GLM_FUNC_QUALIFIER detail::tvec2<valType> factorial(
  93. detail::tvec2<valType> const & x)
  94. {
  95. return detail::tvec2<valType>(
  96. factorial(x.x),
  97. factorial(x.y));
  98. }
  99. template <typename valType>
  100. GLM_FUNC_QUALIFIER detail::tvec3<valType> factorial(
  101. detail::tvec3<valType> const & x)
  102. {
  103. return detail::tvec3<valType>(
  104. factorial(x.x),
  105. factorial(x.y),
  106. factorial(x.z));
  107. }
  108. template <typename valType>
  109. GLM_FUNC_QUALIFIER detail::tvec4<valType> factorial(
  110. detail::tvec4<valType> const & x)
  111. {
  112. return detail::tvec4<valType>(
  113. factorial(x.x),
  114. factorial(x.y),
  115. factorial(x.z),
  116. factorial(x.w));
  117. }
  118. GLM_FUNC_QUALIFIER uint pow(uint x, uint y)
  119. {
  120. uint result = x;
  121. for(uint i = 1; i < y; ++i)
  122. result *= x;
  123. return result;
  124. }
  125. GLM_FUNC_QUALIFIER uint sqrt(uint x)
  126. {
  127. if(x <= 1) return x;
  128. uint NextTrial = x >> 1;
  129. uint CurrentAnswer;
  130. do
  131. {
  132. CurrentAnswer = NextTrial;
  133. NextTrial = (NextTrial + x / NextTrial) >> 1;
  134. } while(NextTrial < CurrentAnswer);
  135. return CurrentAnswer;
  136. }
  137. GLM_FUNC_QUALIFIER uint mod(uint x, uint y)
  138. {
  139. return x - y * (x / y);
  140. }
  141. #if(GLM_COMPILER & (GLM_COMPILER_VC | GLM_COMPILER_GCC))
  142. GLM_FUNC_QUALIFIER unsigned int nlz(unsigned int x)
  143. {
  144. return 31u - findMSB(x);
  145. }
  146. #else
  147. // Hackers Delight: http://www.hackersdelight.org/HDcode/nlz.c.txt
  148. GLM_FUNC_QUALIFIER unsigned int nlz(unsigned int x)
  149. {
  150. int y, m, n;
  151. y = -int(x >> 16); // If left half of x is 0,
  152. m = (y >> 16) & 16; // set n = 16. If left half
  153. n = 16 - m; // is nonzero, set n = 0 and
  154. x = x >> m; // shift x right 16.
  155. // Now x is of the form 0000xxxx.
  156. y = x - 0x100; // If positions 8-15 are 0,
  157. m = (y >> 16) & 8; // add 8 to n and shift x left 8.
  158. n = n + m;
  159. x = x << m;
  160. y = x - 0x1000; // If positions 12-15 are 0,
  161. m = (y >> 16) & 4; // add 4 to n and shift x left 4.
  162. n = n + m;
  163. x = x << m;
  164. y = x - 0x4000; // If positions 14-15 are 0,
  165. m = (y >> 16) & 2; // add 2 to n and shift x left 2.
  166. n = n + m;
  167. x = x << m;
  168. y = x >> 14; // Set y = 0, 1, 2, or 3.
  169. m = y & ~(y >> 1); // Set m = 0, 1, 2, or 2 resp.
  170. return unsigned(n + 2 - m);
  171. }
  172. #endif//(GLM_COMPILER)
  173. }//namespace glm