Clutter Engine 0.0.1
Loading...
Searching...
No Matches
integer.inl
1
2
3namespace 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
11 int result = x;
12 for(uint 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) + y) % 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, qualifier Q>
83 GLM_FUNC_QUALIFIER vec<2, T, Q> factorial(
84 vec<2, T, Q> const& x)
85 {
86 return vec<2, T, Q>(
87 factorial(x.x),
88 factorial(x.y));
89 }
90
91 template<typename T, qualifier Q>
92 GLM_FUNC_QUALIFIER vec<3, T, Q> factorial(
93 vec<3, T, Q> const& x)
94 {
95 return vec<3, T, Q>(
96 factorial(x.x),
97 factorial(x.y),
98 factorial(x.z));
99 }
100
101 template<typename T, qualifier Q>
102 GLM_FUNC_QUALIFIER vec<4, T, Q> factorial(
103 vec<4, T, Q> const& x)
104 {
105 return vec<4, T, Q>(
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 if (y == 0)
115 return 1u;
116
117 uint result = x;
118 for(uint i = 1; i < y; ++i)
119 result *= x;
120 return result;
121 }
122
123 GLM_FUNC_QUALIFIER uint sqrt(uint x)
124 {
125 if(x <= 1) return x;
126
127 uint NextTrial = x >> 1;
128 uint CurrentAnswer;
129
130 do
131 {
132 CurrentAnswer = NextTrial;
133 NextTrial = (NextTrial + x / NextTrial) >> 1;
134 } while(NextTrial < CurrentAnswer);
135
136 return CurrentAnswer;
137 }
138
139 GLM_FUNC_QUALIFIER uint mod(uint x, uint y)
140 {
141 return x - y * (x / y);
142 }
143
144#if(GLM_COMPILER & (GLM_COMPILER_VC | GLM_COMPILER_GCC))
145
146 GLM_FUNC_QUALIFIER unsigned int nlz(unsigned int x)
147 {
148 return 31u - findMSB(x);
149 }
150
151#else
152
153 // Hackers Delight: http://www.hackersdelight.org/HDcode/nlz.c.txt
154 GLM_FUNC_QUALIFIER unsigned int nlz(unsigned int x)
155 {
156 int y, m, n;
157
158 y = -int(x >> 16); // If left half of x is 0,
159 m = (y >> 16) & 16; // set n = 16. If left half
160 n = 16 - m; // is nonzero, set n = 0 and
161 x = x >> m; // shift x right 16.
162 // Now x is of the form 0000xxxx.
163 y = x - 0x100; // If positions 8-15 are 0,
164 m = (y >> 16) & 8; // add 8 to n and shift x left 8.
165 n = n + m;
166 x = x << m;
167
168 y = x - 0x1000; // If positions 12-15 are 0,
169 m = (y >> 16) & 4; // add 4 to n and shift x left 4.
170 n = n + m;
171 x = x << m;
172
173 y = x - 0x4000; // If positions 14-15 are 0,
174 m = (y >> 16) & 2; // add 2 to n and shift x left 2.
175 n = n + m;
176 x = x << m;
177
178 y = x >> 14; // Set y = 0, 1, 2, or 3.
179 m = y & ~(y >> 1); // Set m = 0, 1, 2, or 2 resp.
180 return unsigned(n + 2 - m);
181 }
182
183#endif//(GLM_COMPILER)
184
185}//namespace glm
GLM_FUNC_QUALIFIER vec< L, T, Q > sqrt(vec< L, T, Q > const &x)
Definition func_exponential.inl:128
GLM_FUNC_QUALIFIER vec< L, T, Q > pow(vec< L, T, Q > const &base, vec< L, T, Q > const &exponent)
Definition func_exponential.inl:72
GLM_FUNC_QUALIFIER int findMSB(genIUType v)
Definition func_integer.inl:353
GLM_FUNC_DECL genType factorial(genType const &x)
Definition integer.inl:73
GLM_FUNC_DECL uint nlz(uint x)
Definition integer.inl:154
detail namespace with internal helper functions
Definition json.h:249
Core features
Definition common.hpp:21