1/// @ref gtx_integer 2/// @file glm/gtx/integer.inl 3 4namespace glm 5{ 6 // pow 7 GLM_FUNC_QUALIFIER int pow(int x, int y) 8 { 9 if(y == 0) 10 return 1; 11 int result = x; 12 for(int i = 1; i < y; ++i) 13 result *= x; 14 return result; 15 } 16 17 // sqrt: From Christopher J. Musial, An integer square root, Graphics Gems, 1990, page 387 18 GLM_FUNC_QUALIFIER int sqrt(int x) 19 { 20 if(x <= 1) return x; 21 22 int NextTrial = x >> 1; 23 int CurrentAnswer; 24 25 do 26 { 27 CurrentAnswer = NextTrial; 28 NextTrial = (NextTrial + x / NextTrial) >> 1; 29 } while(NextTrial < CurrentAnswer); 30 31 return CurrentAnswer; 32 } 33 34// Henry Gordon Dietz: http://aggregate.org/MAGIC/ 35namespace 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}//namespace detail 51 52 // Henry Gordon Dietz: http://aggregate.org/MAGIC/ 53/* 54 GLM_FUNC_QUALIFIER unsigned int floor_log2(unsigned int x) 55 { 56 x |= (x >> 1); 57 x |= (x >> 2); 58 x |= (x >> 4); 59 x |= (x >> 8); 60 x |= (x >> 16); 61 62 return _detail::ones32(x) >> 1; 63 } 64*/ 65 // mod 66 GLM_FUNC_QUALIFIER int mod(int x, int y) 67 { 68 return x - y * (x / y); 69 } 70 71 // factorial (!12 max, integer only) 72 template <typename genType> 73 GLM_FUNC_QUALIFIER genType factorial(genType const & x) 74 { 75 genType Temp = x; 76 genType Result; 77 for(Result = 1; Temp > 1; --Temp) 78 Result *= Temp; 79 return Result; 80 } 81 82 template <typename T, precision P> 83 GLM_FUNC_QUALIFIER tvec2<T, P> factorial( 84 tvec2<T, P> const & x) 85 { 86 return tvec2<T, P>( 87 factorial(x.x), 88 factorial(x.y)); 89 } 90 91 template <typename T, precision P> 92 GLM_FUNC_QUALIFIER tvec3<T, P> factorial( 93 tvec3<T, P> const & x) 94 { 95 return tvec3<T, P>( 96 factorial(x.x), 97 factorial(x.y), 98 factorial(x.z)); 99 } 100 101 template <typename T, precision P> 102 GLM_FUNC_QUALIFIER tvec4<T, P> factorial( 103 tvec4<T, P> const & x) 104 { 105 return tvec4<T, P>( 106 factorial(x.x), 107 factorial(x.y), 108 factorial(x.z), 109 factorial(x.w)); 110 } 111 112 GLM_FUNC_QUALIFIER uint pow(uint x, uint y) 113 { 114 uint result = x; 115 for(uint i = 1; i < y; ++i) 116 result *= x; 117 return result; 118 } 119 120 GLM_FUNC_QUALIFIER uint sqrt(uint x) 121 { 122 if(x <= 1) return x; 123 124 uint NextTrial = x >> 1; 125 uint CurrentAnswer; 126 127 do 128 { 129 CurrentAnswer = NextTrial; 130 NextTrial = (NextTrial + x / NextTrial) >> 1; 131 } while(NextTrial < CurrentAnswer); 132 133 return CurrentAnswer; 134 } 135 136 GLM_FUNC_QUALIFIER uint mod(uint x, uint y) 137 { 138 return x - y * (x / y); 139 } 140 141#if(GLM_COMPILER & (GLM_COMPILER_VC | GLM_COMPILER_GCC)) 142 143 GLM_FUNC_QUALIFIER unsigned int nlz(unsigned int x) 144 { 145 return 31u - findMSB(x); 146 } 147 148#else 149 150 // Hackers Delight: http://www.hackersdelight.org/HDcode/nlz.c.txt 151 GLM_FUNC_QUALIFIER unsigned int nlz(unsigned int x) 152 { 153 int y, m, n; 154 155 y = -int(x >> 16); // If left half of x is 0, 156 m = (y >> 16) & 16; // set n = 16. If left half 157 n = 16 - m; // is nonzero, set n = 0 and 158 x = x >> m; // shift x right 16. 159 // Now x is of the form 0000xxxx. 160 y = x - 0x100; // If positions 8-15 are 0, 161 m = (y >> 16) & 8; // add 8 to n and shift x left 8. 162 n = n + m; 163 x = x << m; 164 165 y = x - 0x1000; // If positions 12-15 are 0, 166 m = (y >> 16) & 4; // add 4 to n and shift x left 4. 167 n = n + m; 168 x = x << m; 169 170 y = x - 0x4000; // If positions 14-15 are 0, 171 m = (y >> 16) & 2; // add 2 to n and shift x left 2. 172 n = n + m; 173 x = x << m; 174 175 y = x >> 14; // Set y = 0, 1, 2, or 3. 176 m = y & ~(y >> 1); // Set m = 0, 1, 2, or 2 resp. 177 return unsigned(n + 2 - m); 178 } 179 180#endif//(GLM_COMPILER) 181 182}//namespace glm 183