Collection of DPF-based plugins for packaging
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.

187 lines
3.8KB

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