Clutter Engine 0.0.1
Loading...
Searching...
No Matches
func_integer.inl
1
2
3#include "_vectorize.hpp"
4#if(GLM_ARCH & GLM_ARCH_X86 && GLM_COMPILER & GLM_COMPILER_VC)
5# include <intrin.h>
6# pragma intrinsic(_BitScanReverse)
7#endif//(GLM_ARCH & GLM_ARCH_X86 && GLM_COMPILER & GLM_COMPILER_VC)
8#include <limits>
9
10#if !GLM_HAS_EXTENDED_INTEGER_TYPE
11# if GLM_COMPILER & GLM_COMPILER_GCC
12# pragma GCC diagnostic ignored "-Wlong-long"
13# endif
14# if (GLM_COMPILER & GLM_COMPILER_CLANG)
15# pragma clang diagnostic ignored "-Wc++11-long-long"
16# endif
17#endif
18
19namespace glm{
20namespace detail
21{
22 template<typename T>
23 GLM_FUNC_QUALIFIER T mask(T Bits)
24 {
25 return Bits >= static_cast<T>(sizeof(T) * 8) ? ~static_cast<T>(0) : (static_cast<T>(1) << Bits) - static_cast<T>(1);
26 }
27
28 template<length_t L, typename T, qualifier Q, bool Aligned, bool EXEC>
30 {
31 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T, T)
32 {
33 return v;
34 }
35 };
36
37 template<length_t L, typename T, qualifier Q, bool Aligned>
38 struct compute_bitfieldReverseStep<L, T, Q, Aligned, true>
39 {
40 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T Mask, T Shift)
41 {
42 return (v & Mask) << Shift | (v & (~Mask)) >> Shift;
43 }
44 };
45
46 template<length_t L, typename T, qualifier Q, bool Aligned, bool EXEC>
48 {
49 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T, T)
50 {
51 return v;
52 }
53 };
54
55 template<length_t L, typename T, qualifier Q, bool Aligned>
56 struct compute_bitfieldBitCountStep<L, T, Q, Aligned, true>
57 {
58 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T Mask, T Shift)
59 {
60 return (v & Mask) + ((v >> Shift) & Mask);
61 }
62 };
63
64 template<typename genIUType, size_t Bits>
66 {
67 GLM_FUNC_QUALIFIER static int call(genIUType Value)
68 {
69 if(Value == 0)
70 return -1;
71
72 return glm::bitCount(~Value & (Value - static_cast<genIUType>(1)));
73 }
74 };
75
76# if GLM_HAS_BITSCAN_WINDOWS
77 template<typename genIUType>
78 struct compute_findLSB<genIUType, 32>
79 {
80 GLM_FUNC_QUALIFIER static int call(genIUType Value)
81 {
82 unsigned long Result(0);
83 unsigned char IsNotNull = _BitScanForward(&Result, *reinterpret_cast<unsigned long*>(&Value));
84 return IsNotNull ? int(Result) : -1;
85 }
86 };
87
88# if !((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_MODEL == GLM_MODEL_32))
89 template<typename genIUType>
90 struct compute_findLSB<genIUType, 64>
91 {
92 GLM_FUNC_QUALIFIER static int call(genIUType Value)
93 {
94 unsigned long Result(0);
95 unsigned char IsNotNull = _BitScanForward64(&Result, *reinterpret_cast<unsigned __int64*>(&Value));
96 return IsNotNull ? int(Result) : -1;
97 }
98 };
99# endif
100# endif//GLM_HAS_BITSCAN_WINDOWS
101
102 template<length_t L, typename T, qualifier Q, bool EXEC = true>
104 {
105 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, T Shift)
106 {
107 return x | (x >> Shift);
108 }
109 };
110
111 template<length_t L, typename T, qualifier Q>
112 struct compute_findMSB_step_vec<L, T, Q, false>
113 {
114 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, T)
115 {
116 return x;
117 }
118 };
119
120 template<length_t L, typename T, qualifier Q, int>
122 {
123 GLM_FUNC_QUALIFIER static vec<L, int, Q> call(vec<L, T, Q> const& v)
124 {
125 vec<L, T, Q> x(v);
126 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 8>::call(x, static_cast<T>( 1));
127 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 8>::call(x, static_cast<T>( 2));
128 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 8>::call(x, static_cast<T>( 4));
129 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 16>::call(x, static_cast<T>( 8));
130 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 32>::call(x, static_cast<T>(16));
131 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 64>::call(x, static_cast<T>(32));
132 return vec<L, int, Q>(sizeof(T) * 8 - 1) - glm::bitCount(~x);
133 }
134 };
135
136# if GLM_HAS_BITSCAN_WINDOWS
137 template<typename genIUType>
138 GLM_FUNC_QUALIFIER int compute_findMSB_32(genIUType Value)
139 {
140 unsigned long Result(0);
141 unsigned char IsNotNull = _BitScanReverse(&Result, *reinterpret_cast<unsigned long*>(&Value));
142 return IsNotNull ? int(Result) : -1;
143 }
144
145 template<length_t L, typename T, qualifier Q>
146 struct compute_findMSB_vec<L, T, Q, 32>
147 {
148 GLM_FUNC_QUALIFIER static vec<L, int, Q> call(vec<L, T, Q> const& x)
149 {
150 return detail::functor1<vec, L, int, T, Q>::call(compute_findMSB_32, x);
151 }
152 };
153
154# if !((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_MODEL == GLM_MODEL_32))
155 template<typename genIUType>
156 GLM_FUNC_QUALIFIER int compute_findMSB_64(genIUType Value)
157 {
158 unsigned long Result(0);
159 unsigned char IsNotNull = _BitScanReverse64(&Result, *reinterpret_cast<unsigned __int64*>(&Value));
160 return IsNotNull ? int(Result) : -1;
161 }
162
163 template<length_t L, typename T, qualifier Q>
164 struct compute_findMSB_vec<L, T, Q, 64>
165 {
166 GLM_FUNC_QUALIFIER static vec<L, int, Q> call(vec<L, T, Q> const& x)
167 {
168 return detail::functor1<vec, L, int, T, Q>::call(compute_findMSB_64, x);
169 }
170 };
171# endif
172# endif//GLM_HAS_BITSCAN_WINDOWS
173}//namespace detail
174
175 // uaddCarry
176 GLM_FUNC_QUALIFIER uint uaddCarry(uint const& x, uint const& y, uint & Carry)
177 {
178 detail::uint64 const Value64(static_cast<detail::uint64>(x) + static_cast<detail::uint64>(y));
179 detail::uint64 const Max32((static_cast<detail::uint64>(1) << static_cast<detail::uint64>(32)) - static_cast<detail::uint64>(1));
180 Carry = Value64 > Max32 ? 1u : 0u;
181 return static_cast<uint>(Value64 % (Max32 + static_cast<detail::uint64>(1)));
182 }
183
184 template<length_t L, qualifier Q>
185 GLM_FUNC_QUALIFIER vec<L, uint, Q> uaddCarry(vec<L, uint, Q> const& x, vec<L, uint, Q> const& y, vec<L, uint, Q>& Carry)
186 {
188 vec<L, detail::uint64, Q> Max32((static_cast<detail::uint64>(1) << static_cast<detail::uint64>(32)) - static_cast<detail::uint64>(1));
189 Carry = mix(vec<L, uint, Q>(0), vec<L, uint, Q>(1), greaterThan(Value64, Max32));
190 return vec<L, uint, Q>(Value64 % (Max32 + static_cast<detail::uint64>(1)));
191 }
192
193 // usubBorrow
194 GLM_FUNC_QUALIFIER uint usubBorrow(uint const& x, uint const& y, uint & Borrow)
195 {
196 Borrow = x >= y ? static_cast<uint>(0) : static_cast<uint>(1);
197 if(y >= x)
198 return y - x;
199 else
200 return static_cast<uint>((static_cast<detail::int64>(1) << static_cast<detail::int64>(32)) + (static_cast<detail::int64>(y) - static_cast<detail::int64>(x)));
201 }
202
203 template<length_t L, qualifier Q>
204 GLM_FUNC_QUALIFIER vec<L, uint, Q> usubBorrow(vec<L, uint, Q> const& x, vec<L, uint, Q> const& y, vec<L, uint, Q>& Borrow)
205 {
206 Borrow = mix(vec<L, uint, Q>(1), vec<L, uint, Q>(0), greaterThanEqual(x, y));
207 vec<L, uint, Q> const YgeX(y - x);
208 vec<L, uint, Q> const XgeY(vec<L, uint, Q>((static_cast<detail::int64>(1) << static_cast<detail::int64>(32)) + (vec<L, detail::int64, Q>(y) - vec<L, detail::int64, Q>(x))));
209 return mix(XgeY, YgeX, greaterThanEqual(y, x));
210 }
211
212 // umulExtended
213 GLM_FUNC_QUALIFIER void umulExtended(uint const& x, uint const& y, uint & msb, uint & lsb)
214 {
215 detail::uint64 Value64 = static_cast<detail::uint64>(x) * static_cast<detail::uint64>(y);
216 msb = static_cast<uint>(Value64 >> static_cast<detail::uint64>(32));
217 lsb = static_cast<uint>(Value64);
218 }
219
220 template<length_t L, qualifier Q>
221 GLM_FUNC_QUALIFIER void umulExtended(vec<L, uint, Q> const& x, vec<L, uint, Q> const& y, vec<L, uint, Q>& msb, vec<L, uint, Q>& lsb)
222 {
224 msb = vec<L, uint, Q>(Value64 >> static_cast<detail::uint64>(32));
225 lsb = vec<L, uint, Q>(Value64);
226 }
227
228 // imulExtended
229 GLM_FUNC_QUALIFIER void imulExtended(int x, int y, int& msb, int& lsb)
230 {
231 detail::int64 Value64 = static_cast<detail::int64>(x) * static_cast<detail::int64>(y);
232 msb = static_cast<int>(Value64 >> static_cast<detail::int64>(32));
233 lsb = static_cast<int>(Value64);
234 }
235
236 template<length_t L, qualifier Q>
237 GLM_FUNC_QUALIFIER void imulExtended(vec<L, int, Q> const& x, vec<L, int, Q> const& y, vec<L, int, Q>& msb, vec<L, int, Q>& lsb)
238 {
240 lsb = vec<L, int, Q>(Value64 & static_cast<detail::int64>(0xFFFFFFFF));
241 msb = vec<L, int, Q>((Value64 >> static_cast<detail::int64>(32)) & static_cast<detail::int64>(0xFFFFFFFF));
242 }
243
244 // bitfieldExtract
245 template<typename genIUType>
246 GLM_FUNC_QUALIFIER genIUType bitfieldExtract(genIUType Value, int Offset, int Bits)
247 {
248 return bitfieldExtract(vec<1, genIUType>(Value), Offset, Bits).x;
249 }
250
251 template<length_t L, typename T, qualifier Q>
252 GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldExtract(vec<L, T, Q> const& Value, int Offset, int Bits)
253 {
254 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldExtract' only accept integer inputs");
255
256 return (Value >> static_cast<T>(Offset)) & static_cast<T>(detail::mask(Bits));
257 }
258
259 // bitfieldInsert
260 template<typename genIUType>
261 GLM_FUNC_QUALIFIER genIUType bitfieldInsert(genIUType const& Base, genIUType const& Insert, int Offset, int Bits)
262 {
263 GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'bitfieldInsert' only accept integer values");
264
265 return bitfieldInsert(vec<1, genIUType>(Base), vec<1, genIUType>(Insert), Offset, Bits).x;
266 }
267
268 template<length_t L, typename T, qualifier Q>
269 GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldInsert(vec<L, T, Q> const& Base, vec<L, T, Q> const& Insert, int Offset, int Bits)
270 {
271 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldInsert' only accept integer values");
272
273 T const Mask = static_cast<T>(detail::mask(Bits) << Offset);
274 return (Base & ~Mask) | ((Insert << static_cast<T>(Offset)) & Mask);
275 }
276
277 // bitfieldReverse
278 template<typename genIUType>
279 GLM_FUNC_QUALIFIER genIUType bitfieldReverse(genIUType x)
280 {
281 GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'bitfieldReverse' only accept integer values");
282
283 return bitfieldReverse(glm::vec<1, genIUType, glm::defaultp>(x)).x;
284 }
285
286 template<length_t L, typename T, qualifier Q>
287 GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldReverse(vec<L, T, Q> const& v)
288 {
289 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldReverse' only accept integer values");
290
291 vec<L, T, Q> x(v);
292 x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 2>::call(x, static_cast<T>(0x5555555555555555ull), static_cast<T>( 1));
293 x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 4>::call(x, static_cast<T>(0x3333333333333333ull), static_cast<T>( 2));
294 x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 8>::call(x, static_cast<T>(0x0F0F0F0F0F0F0F0Full), static_cast<T>( 4));
295 x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 16>::call(x, static_cast<T>(0x00FF00FF00FF00FFull), static_cast<T>( 8));
296 x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 32>::call(x, static_cast<T>(0x0000FFFF0000FFFFull), static_cast<T>(16));
297 x = detail::compute_bitfieldReverseStep<L, T, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 64>::call(x, static_cast<T>(0x00000000FFFFFFFFull), static_cast<T>(32));
298 return x;
299 }
300
301 // bitCount
302 template<typename genIUType>
303 GLM_FUNC_QUALIFIER int bitCount(genIUType x)
304 {
305 GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'bitCount' only accept integer values");
306
307 return bitCount(glm::vec<1, genIUType, glm::defaultp>(x)).x;
308 }
309
310 template<length_t L, typename T, qualifier Q>
311 GLM_FUNC_QUALIFIER vec<L, int, Q> bitCount(vec<L, T, Q> const& v)
312 {
313 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitCount' only accept integer values");
314
315# if GLM_COMPILER & GLM_COMPILER_VC
316# pragma warning(push)
317# pragma warning(disable : 4310) //cast truncates constant value
318# endif
319
327 return vec<L, int, Q>(x);
328
329# if GLM_COMPILER & GLM_COMPILER_VC
330# pragma warning(pop)
331# endif
332 }
333
334 // findLSB
335 template<typename genIUType>
336 GLM_FUNC_QUALIFIER int findLSB(genIUType Value)
337 {
338 GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findLSB' only accept integer values");
339
340 return detail::compute_findLSB<genIUType, sizeof(genIUType) * 8>::call(Value);
341 }
342
343 template<length_t L, typename T, qualifier Q>
344 GLM_FUNC_QUALIFIER vec<L, int, Q> findLSB(vec<L, T, Q> const& x)
345 {
346 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'findLSB' only accept integer values");
347
349 }
350
351 // findMSB
352 template<typename genIUType>
353 GLM_FUNC_QUALIFIER int findMSB(genIUType v)
354 {
355 GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
356
357 return findMSB(vec<1, genIUType>(v)).x;
358 }
359
360 template<length_t L, typename T, qualifier Q>
361 GLM_FUNC_QUALIFIER vec<L, int, Q> findMSB(vec<L, T, Q> const& v)
362 {
363 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'findMSB' only accept integer values");
364
365 return detail::compute_findMSB_vec<L, T, Q, sizeof(T) * 8>::call(v);
366 }
367}//namespace glm
368
369#if GLM_CONFIG_SIMD == GLM_ENABLE
370# include "func_integer_simd.inl"
371#endif
372
GLM_FUNC_DECL genTypeT mix(genTypeT x, genTypeT y, genTypeU a)
Definition func_common.inl:526
GLM_FUNC_QUALIFIER int findMSB(genIUType v)
Definition func_integer.inl:353
GLM_FUNC_QUALIFIER int findLSB(genIUType Value)
Definition func_integer.inl:336
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec< L, bool, Q > greaterThanEqual(vec< L, T, Q > const &x, vec< L, T, Q > const &y)
Definition func_vector_relational.inl:31
GLM_FUNC_QUALIFIER GLM_CONSTEXPR vec< L, bool, Q > greaterThan(vec< L, T, Q > const &x, vec< L, T, Q > const &y)
Definition func_vector_relational.inl:22
detail namespace with internal helper functions
Definition json.h:249
Core features
Definition common.hpp:21
Definition func_integer.inl:48
Definition func_integer.inl:30
Definition func_integer.inl:66
Definition func_integer.inl:104
Definition func_integer.inl:122
Definition _vectorize.hpp:7
Definition qualifier.hpp:60
Definition setup.hpp:706
Definition qualifier.hpp:35