Rev Author Line No. Line
23 ovan 1 ///////////////////////////////////////////////////////////////////////////////////
2 /// OpenGL Mathematics (glm.g-truc.net)
3 ///
4 /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
5 /// Permission is hereby granted, free of charge, to any person obtaining a copy
6 /// of this software and associated documentation files (the "Software"), to deal
7 /// in the Software without restriction, including without limitation the rights
8 /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 /// copies of the Software, and to permit persons to whom the Software is
10 /// furnished to do so, subject to the following conditions:
11 ///
12 /// The above copyright notice and this permission notice shall be included in
13 /// all copies or substantial portions of the Software.
14 ///
15 /// Restrictions:
16 /// By making use of the Software for military purposes, you choose to make
17 /// a Bunny unhappy.
18 ///
19 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 /// THE SOFTWARE.
26 ///
27 /// @ref gtx_integer
28 /// @file glm/gtx/integer.inl
29 /// @date 2005-12-24 / 2011-10-13
30 /// @author Christophe Riccio
31 ///////////////////////////////////////////////////////////////////////////////////////////////////
32  
33 namespace glm
34 {
35 // pow
36 GLM_FUNC_QUALIFIER int pow(int x, int y)
37 {
38 if(y == 0)
39 return 1;
40 int result = x;
41 for(int i = 1; i < y; ++i)
42 result *= x;
43 return result;
44 }
45  
46 // sqrt: From Christopher J. Musial, An integer square root, Graphics Gems, 1990, page 387
47 GLM_FUNC_QUALIFIER int sqrt(int x)
48 {
49 if(x <= 1) return x;
50  
51 int NextTrial = x >> 1;
52 int CurrentAnswer;
53  
54 do
55 {
56 CurrentAnswer = NextTrial;
57 NextTrial = (NextTrial + x / NextTrial) >> 1;
58 } while(NextTrial < CurrentAnswer);
59  
60 return CurrentAnswer;
61 }
62  
63 // Henry Gordon Dietz: http://aggregate.org/MAGIC/
64 namespace detail
65 {
66 GLM_FUNC_QUALIFIER unsigned int ones32(unsigned int x)
67 {
68 /* 32-bit recursive reduction using SWAR...
69 but first step is mapping 2-bit values
70 into sum of 2 1-bit values in sneaky way
71 */
72 x -= ((x >> 1) & 0x55555555);
73 x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
74 x = (((x >> 4) + x) & 0x0f0f0f0f);
75 x += (x >> 8);
76 x += (x >> 16);
77 return(x & 0x0000003f);
78 }
79 }//namespace detail
80  
81 // Henry Gordon Dietz: http://aggregate.org/MAGIC/
82 /*
83 GLM_FUNC_QUALIFIER unsigned int floor_log2(unsigned int x)
84 {
85 x |= (x >> 1);
86 x |= (x >> 2);
87 x |= (x >> 4);
88 x |= (x >> 8);
89 x |= (x >> 16);
90  
91 return _detail::ones32(x) >> 1;
92 }
93 */
94 // mod
95 GLM_FUNC_QUALIFIER int mod(int x, int y)
96 {
97 return x - y * (x / y);
98 }
99  
100 // factorial (!12 max, integer only)
101 template <typename genType>
102 GLM_FUNC_QUALIFIER genType factorial(genType const & x)
103 {
104 genType Temp = x;
105 genType Result;
106 for(Result = 1; Temp > 1; --Temp)
107 Result *= Temp;
108 return Result;
109 }
110  
111 template <typename T, precision P>
112 GLM_FUNC_QUALIFIER tvec2<T, P> factorial(
113 tvec2<T, P> const & x)
114 {
115 return tvec2<T, P>(
116 factorial(x.x),
117 factorial(x.y));
118 }
119  
120 template <typename T, precision P>
121 GLM_FUNC_QUALIFIER tvec3<T, P> factorial(
122 tvec3<T, P> const & x)
123 {
124 return tvec3<T, P>(
125 factorial(x.x),
126 factorial(x.y),
127 factorial(x.z));
128 }
129  
130 template <typename T, precision P>
131 GLM_FUNC_QUALIFIER tvec4<T, P> factorial(
132 tvec4<T, P> const & x)
133 {
134 return tvec4<T, P>(
135 factorial(x.x),
136 factorial(x.y),
137 factorial(x.z),
138 factorial(x.w));
139 }
140  
141 GLM_FUNC_QUALIFIER uint pow(uint x, uint y)
142 {
143 uint result = x;
144 for(uint i = 1; i < y; ++i)
145 result *= x;
146 return result;
147 }
148  
149 GLM_FUNC_QUALIFIER uint sqrt(uint x)
150 {
151 if(x <= 1) return x;
152  
153 uint NextTrial = x >> 1;
154 uint CurrentAnswer;
155  
156 do
157 {
158 CurrentAnswer = NextTrial;
159 NextTrial = (NextTrial + x / NextTrial) >> 1;
160 } while(NextTrial < CurrentAnswer);
161  
162 return CurrentAnswer;
163 }
164  
165 GLM_FUNC_QUALIFIER uint mod(uint x, uint y)
166 {
167 return x - y * (x / y);
168 }
169  
170 #if(GLM_COMPILER & (GLM_COMPILER_VC | GLM_COMPILER_GCC))
171  
172 GLM_FUNC_QUALIFIER unsigned int nlz(unsigned int x)
173 {
174 return 31u - findMSB(x);
175 }
176  
177 #else
178  
179 // Hackers Delight: http://www.hackersdelight.org/HDcode/nlz.c.txt
180 GLM_FUNC_QUALIFIER unsigned int nlz(unsigned int x)
181 {
182 int y, m, n;
183  
184 y = -int(x >> 16); // If left half of x is 0,
185 m = (y >> 16) & 16; // set n = 16. If left half
186 n = 16 - m; // is nonzero, set n = 0 and
187 x = x >> m; // shift x right 16.
188 // Now x is of the form 0000xxxx.
189 y = x - 0x100; // If positions 8-15 are 0,
190 m = (y >> 16) & 8; // add 8 to n and shift x left 8.
191 n = n + m;
192 x = x << m;
193  
194 y = x - 0x1000; // If positions 12-15 are 0,
195 m = (y >> 16) & 4; // add 4 to n and shift x left 4.
196 n = n + m;
197 x = x << m;
198  
199 y = x - 0x4000; // If positions 14-15 are 0,
200 m = (y >> 16) & 2; // add 2 to n and shift x left 2.
201 n = n + m;
202 x = x << m;
203  
204 y = x >> 14; // Set y = 0, 1, 2, or 3.
205 m = y & ~(y >> 1); // Set m = 0, 1, 2, or 2 resp.
206 return unsigned(n + 2 - m);
207 }
208  
209 #endif//(GLM_COMPILER)
210  
211 }//namespace glm