master
ブランチ master
VulkanExample / dep / include / glm / detail / func_integer.inl
14.8 KB、372行 生 Blame 履歴
1/// @ref core
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
10namespace glm{
11namespace detail
12{
13 template<typename T>
14 GLM_FUNC_QUALIFIER T mask(T Bits)
15 {
16 return Bits >= static_cast<T>(sizeof(T) * 8) ? ~static_cast<T>(0) : (static_cast<T>(1) << Bits) - static_cast<T>(1);
17 }
18
19 template<length_t L, typename T, qualifier Q, bool Aligned, bool EXEC>
20 struct compute_bitfieldReverseStep
21 {
22 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T, T)
23 {
24 return v;
25 }
26 };
27
28 template<length_t L, typename T, qualifier Q, bool Aligned>
29 struct compute_bitfieldReverseStep<L, T, Q, Aligned, true>
30 {
31 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T Mask, T Shift)
32 {
33 return (v & Mask) << Shift | (v & (~Mask)) >> Shift;
34 }
35 };
36
37 template<length_t L, typename T, qualifier Q, bool Aligned, bool EXEC>
38 struct compute_bitfieldBitCountStep
39 {
40 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T, T)
41 {
42 return v;
43 }
44 };
45
46 template<length_t L, typename T, qualifier Q, bool Aligned>
47 struct compute_bitfieldBitCountStep<L, T, Q, Aligned, true>
48 {
49 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& v, T Mask, T Shift)
50 {
51 return (v & Mask) + ((v >> Shift) & Mask);
52 }
53 };
54
55 template<typename genIUType, size_t Bits>
56 struct compute_findLSB
57 {
58 GLM_FUNC_QUALIFIER static int call(genIUType Value)
59 {
60 if(Value == 0)
61 return -1;
62
63 return glm::bitCount(~Value & (Value - static_cast<genIUType>(1)));
64 }
65 };
66
67# if GLM_HAS_BITSCAN_WINDOWS
68 template<typename genIUType>
69 struct compute_findLSB<genIUType, 32>
70 {
71 GLM_FUNC_QUALIFIER static int call(genIUType Value)
72 {
73 unsigned long Result(0);
74 unsigned char IsNotNull = _BitScanForward(&Result, *reinterpret_cast<unsigned long*>(&Value));
75 return IsNotNull ? int(Result) : -1;
76 }
77 };
78
79# if !((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_MODEL == GLM_MODEL_32))
80 template<typename genIUType>
81 struct compute_findLSB<genIUType, 64>
82 {
83 GLM_FUNC_QUALIFIER static int call(genIUType Value)
84 {
85 unsigned long Result(0);
86 unsigned char IsNotNull = _BitScanForward64(&Result, *reinterpret_cast<unsigned __int64*>(&Value));
87 return IsNotNull ? int(Result) : -1;
88 }
89 };
90# endif
91# endif//GLM_HAS_BITSCAN_WINDOWS
92
93 template<length_t L, typename T, qualifier Q, bool EXEC = true>
94 struct compute_findMSB_step_vec
95 {
96 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, T Shift)
97 {
98 return x | (x >> Shift);
99 }
100 };
101
102 template<length_t L, typename T, qualifier Q>
103 struct compute_findMSB_step_vec<L, T, Q, false>
104 {
105 GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& x, T)
106 {
107 return x;
108 }
109 };
110
111 template<length_t L, typename T, qualifier Q, int>
112 struct compute_findMSB_vec
113 {
114 GLM_FUNC_QUALIFIER static vec<L, int, Q> call(vec<L, T, Q> const& v)
115 {
116 vec<L, T, Q> x(v);
117 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 8>::call(x, static_cast<T>( 1));
118 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 8>::call(x, static_cast<T>( 2));
119 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 8>::call(x, static_cast<T>( 4));
120 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 16>::call(x, static_cast<T>( 8));
121 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 32>::call(x, static_cast<T>(16));
122 x = compute_findMSB_step_vec<L, T, Q, sizeof(T) * 8 >= 64>::call(x, static_cast<T>(32));
123 return vec<L, int, Q>(sizeof(T) * 8 - 1) - glm::bitCount(~x);
124 }
125 };
126
127# if GLM_HAS_BITSCAN_WINDOWS
128 template<typename genIUType>
129 GLM_FUNC_QUALIFIER int compute_findMSB_32(genIUType Value)
130 {
131 unsigned long Result(0);
132 unsigned char IsNotNull = _BitScanReverse(&Result, *reinterpret_cast<unsigned long*>(&Value));
133 return IsNotNull ? int(Result) : -1;
134 }
135
136 template<length_t L, typename T, qualifier Q>
137 struct compute_findMSB_vec<L, T, Q, 32>
138 {
139 GLM_FUNC_QUALIFIER static vec<L, int, Q> call(vec<L, T, Q> const& x)
140 {
141 return detail::functor1<vec, L, int, T, Q>::call(compute_findMSB_32, x);
142 }
143 };
144
145# if !((GLM_COMPILER & GLM_COMPILER_VC) && (GLM_MODEL == GLM_MODEL_32))
146 template<typename genIUType>
147 GLM_FUNC_QUALIFIER int compute_findMSB_64(genIUType Value)
148 {
149 unsigned long Result(0);
150 unsigned char IsNotNull = _BitScanReverse64(&Result, *reinterpret_cast<unsigned __int64*>(&Value));
151 return IsNotNull ? int(Result) : -1;
152 }
153
154 template<length_t L, typename T, qualifier Q>
155 struct compute_findMSB_vec<L, T, Q, 64>
156 {
157 GLM_FUNC_QUALIFIER static vec<L, int, Q> call(vec<L, T, Q> const& x)
158 {
159 return detail::functor1<vec, L, int, T, Q>::call(compute_findMSB_64, x);
160 }
161 };
162# endif
163# endif//GLM_HAS_BITSCAN_WINDOWS
164}//namespace detail
165
166 // uaddCarry
167 GLM_FUNC_QUALIFIER uint uaddCarry(uint const& x, uint const& y, uint & Carry)
168 {
169 detail::uint64 const Value64(static_cast<detail::uint64>(x) + static_cast<detail::uint64>(y));
170 detail::uint64 const Max32((static_cast<detail::uint64>(1) << static_cast<detail::uint64>(32)) - static_cast<detail::uint64>(1));
171 Carry = Value64 > Max32 ? 1u : 0u;
172 return static_cast<uint>(Value64 % (Max32 + static_cast<detail::uint64>(1)));
173 }
174
175 template<length_t L, qualifier Q>
176 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)
177 {
178 vec<L, detail::uint64, Q> Value64(vec<L, detail::uint64, Q>(x) + vec<L, detail::uint64, Q>(y));
179 vec<L, detail::uint64, Q> Max32((static_cast<detail::uint64>(1) << static_cast<detail::uint64>(32)) - static_cast<detail::uint64>(1));
180 Carry = mix(vec<L, uint, Q>(0), vec<L, uint, Q>(1), greaterThan(Value64, Max32));
181 return vec<L, uint, Q>(Value64 % (Max32 + static_cast<detail::uint64>(1)));
182 }
183
184 // usubBorrow
185 GLM_FUNC_QUALIFIER uint usubBorrow(uint const& x, uint const& y, uint & Borrow)
186 {
187 Borrow = x >= y ? static_cast<uint>(0) : static_cast<uint>(1);
188 if(x >= y)
189 return x - y;
190 else
191 return static_cast<uint>((static_cast<detail::int64>(1) << static_cast<detail::int64>(32)) + (static_cast<detail::int64>(x) - static_cast<detail::int64>(y)));
192 }
193
194 template<length_t L, qualifier Q>
195 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)
196 {
197 Borrow = mix(vec<L, uint, Q>(1), vec<L, uint, Q>(0), greaterThanEqual(x, y));
198 vec<L, uint, Q> const XgeY(x - y);
199 vec<L, uint, Q> const YgX(vec<L, uint, Q>((static_cast<detail::int64>(1) << static_cast<detail::int64>(32)) + (vec<L, detail::int64, Q>(x) - vec<L, detail::int64, Q>(y))));
200 return mix(YgX, XgeY, greaterThanEqual(x, y));
201 }
202
203 // umulExtended
204 GLM_FUNC_QUALIFIER void umulExtended(uint const& x, uint const& y, uint & msb, uint & lsb)
205 {
206 detail::uint64 Value64 = static_cast<detail::uint64>(x) * static_cast<detail::uint64>(y);
207 msb = static_cast<uint>(Value64 >> static_cast<detail::uint64>(32));
208 lsb = static_cast<uint>(Value64);
209 }
210
211 template<length_t L, qualifier Q>
212 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)
213 {
214 vec<L, detail::uint64, Q> Value64(vec<L, detail::uint64, Q>(x) * vec<L, detail::uint64, Q>(y));
215 msb = vec<L, uint, Q>(Value64 >> static_cast<detail::uint64>(32));
216 lsb = vec<L, uint, Q>(Value64);
217 }
218
219 // imulExtended
220 GLM_FUNC_QUALIFIER void imulExtended(int x, int y, int& msb, int& lsb)
221 {
222 detail::int64 Value64 = static_cast<detail::int64>(x) * static_cast<detail::int64>(y);
223 msb = static_cast<int>(Value64 >> static_cast<detail::int64>(32));
224 lsb = static_cast<int>(Value64);
225 }
226
227 template<length_t L, qualifier Q>
228 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)
229 {
230 vec<L, detail::int64, Q> Value64(vec<L, detail::int64, Q>(x) * vec<L, detail::int64, Q>(y));
231 lsb = vec<L, int, Q>(Value64 & static_cast<detail::int64>(0xFFFFFFFF));
232 msb = vec<L, int, Q>((Value64 >> static_cast<detail::int64>(32)) & static_cast<detail::int64>(0xFFFFFFFF));
233 }
234
235 // bitfieldExtract
236 template<typename genIUType>
237 GLM_FUNC_QUALIFIER genIUType bitfieldExtract(genIUType Value, int Offset, int Bits)
238 {
239 return bitfieldExtract(vec<1, genIUType>(Value), Offset, Bits).x;
240 }
241
242 template<length_t L, typename T, qualifier Q>
243 GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldExtract(vec<L, T, Q> const& Value, int Offset, int Bits)
244 {
245 static_assert(std::numeric_limits<T>::is_integer, "'bitfieldExtract' only accept integer inputs");
246
247 return (Value >> static_cast<T>(Offset)) & static_cast<T>(detail::mask(Bits));
248 }
249
250 // bitfieldInsert
251 template<typename genIUType>
252 GLM_FUNC_QUALIFIER genIUType bitfieldInsert(genIUType const& Base, genIUType const& Insert, int Offset, int Bits)
253 {
254 static_assert(std::numeric_limits<genIUType>::is_integer, "'bitfieldInsert' only accept integer values");
255
256 return bitfieldInsert(vec<1, genIUType>(Base), vec<1, genIUType>(Insert), Offset, Bits).x;
257 }
258
259 template<length_t L, typename T, qualifier Q>
260 GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldInsert(vec<L, T, Q> const& Base, vec<L, T, Q> const& Insert, int Offset, int Bits)
261 {
262 static_assert(std::numeric_limits<T>::is_integer, "'bitfieldInsert' only accept integer values");
263
264 T const Mask = detail::mask(static_cast<T>(Bits)) << Offset;
265 return (Base & ~Mask) | ((Insert << static_cast<T>(Offset)) & Mask);
266 }
267
268#if GLM_COMPILER & GLM_COMPILER_VC
269# pragma warning(push)
270# pragma warning(disable : 4309)
271#endif
272
273 // bitfieldReverse
274 template<typename genIUType>
275 GLM_FUNC_QUALIFIER genIUType bitfieldReverse(genIUType x)
276 {
277 static_assert(std::numeric_limits<genIUType>::is_integer, "'bitfieldReverse' only accept integer values");
278
279 return bitfieldReverse(glm::vec<1, genIUType, glm::defaultp>(x)).x;
280 }
281
282 template<length_t L, typename T, qualifier Q>
283 GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldReverse(vec<L, T, Q> const& v)
284 {
285 static_assert(std::numeric_limits<T>::is_integer, "'bitfieldReverse' only accept integer values");
286
287 vec<L, T, Q> x(v);
288 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));
289 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));
290 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));
291 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));
292 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));
293 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));
294 return x;
295 }
296
297# if GLM_COMPILER & GLM_COMPILER_VC
298# pragma warning(pop)
299# endif
300
301 // bitCount
302 template<typename genIUType>
303 GLM_FUNC_QUALIFIER int bitCount(genIUType x)
304 {
305 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 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
320 vec<L, typename std::make_unsigned<T>::type, Q> x(v);
321 x = detail::compute_bitfieldBitCountStep<L, typename std::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 2>::call(x, typename std::make_unsigned<T>::type(0x5555555555555555ull), typename std::make_unsigned<T>::type( 1));
322 x = detail::compute_bitfieldBitCountStep<L, typename std::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 4>::call(x, typename std::make_unsigned<T>::type(0x3333333333333333ull), typename std::make_unsigned<T>::type( 2));
323 x = detail::compute_bitfieldBitCountStep<L, typename std::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 8>::call(x, typename std::make_unsigned<T>::type(0x0F0F0F0F0F0F0F0Full), typename std::make_unsigned<T>::type( 4));
324 x = detail::compute_bitfieldBitCountStep<L, typename std::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 16>::call(x, typename std::make_unsigned<T>::type(0x00FF00FF00FF00FFull), typename std::make_unsigned<T>::type( 8));
325 x = detail::compute_bitfieldBitCountStep<L, typename std::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 32>::call(x, typename std::make_unsigned<T>::type(0x0000FFFF0000FFFFull), typename std::make_unsigned<T>::type(16));
326 x = detail::compute_bitfieldBitCountStep<L, typename std::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 64>::call(x, typename std::make_unsigned<T>::type(0x00000000FFFFFFFFull), typename std::make_unsigned<T>::type(32));
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 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 static_assert(std::numeric_limits<T>::is_integer, "'findLSB' only accept integer values");
347
348 return detail::functor1<vec, L, int, T, Q>::call(findLSB, x);
349 }
350
351 // findMSB
352 template<typename genIUType>
353 GLM_FUNC_QUALIFIER int findMSB(genIUType v)
354 {
355 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 static_assert(std::numeric_limits<T>::is_integer, "'findMSB' only accept integer values");
364
365 return detail::compute_findMSB_vec<L, T, Q, static_cast<int>(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