master
ブランチ master
ShaderPlayground / deps / glm / glm / detail / func_integer.inl
14.1 KB、362行 生 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
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>
29 struct compute_bitfieldReverseStep
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>
47 struct compute_bitfieldBitCountStep
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>
65 struct compute_findLSB
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>
103 struct compute_findMSB_step_vec
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>
121 struct compute_findMSB_vec
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 {
187 vec<L, detail::uint64, Q> Value64(vec<L, detail::uint64, Q>(x) + vec<L, detail::uint64, Q>(y));
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 {
223 vec<L, detail::uint64, Q> Value64(vec<L, detail::uint64, Q>(x) * vec<L, detail::uint64, Q>(y));
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 {
239 vec<L, detail::int64, Q> Value64(vec<L, detail::int64, Q>(x) * vec<L, detail::int64, Q>(y));
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 return bitfieldInsert(vec<1, genIUType>(Base), vec<1, genIUType>(Insert), Offset, Bits).x;
264 }
265
266 template<length_t L, typename T, qualifier Q>
267 GLM_FUNC_QUALIFIER vec<L, T, Q> bitfieldInsert(vec<L, T, Q> const& Base, vec<L, T, Q> const& Insert, int Offset, int Bits)
268 {
269 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'bitfieldInsert' only accept integer values");
270
271 T const Mask = static_cast<T>(detail::mask(Bits) << Offset);
272 return (Base & ~Mask) | (Insert & Mask);
273 }
274
275 // bitfieldReverse
276 template<typename genType>
277 GLM_FUNC_QUALIFIER genType bitfieldReverse(genType x)
278 {
279 return bitfieldReverse(glm::vec<1, genType, 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 vec<L, T, Q> x(v);
286 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));
287 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));
288 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));
289 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));
290 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));
291 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));
292 return x;
293 }
294
295 // bitCount
296 template<typename genType>
297 GLM_FUNC_QUALIFIER int bitCount(genType x)
298 {
299 return bitCount(glm::vec<1, genType, glm::defaultp>(x)).x;
300 }
301
302 template<length_t L, typename T, qualifier Q>
303 GLM_FUNC_QUALIFIER vec<L, int, Q> bitCount(vec<L, T, Q> const& v)
304 {
305# if GLM_COMPILER & GLM_COMPILER_VC
306# pragma warning(push)
307# pragma warning(disable : 4310) //cast truncates constant value
308# endif
309
310 vec<L, typename detail::make_unsigned<T>::type, Q> x(*reinterpret_cast<vec<L, typename detail::make_unsigned<T>::type, Q> const *>(&v));
311 x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 2>::call(x, typename detail::make_unsigned<T>::type(0x5555555555555555ull), typename detail::make_unsigned<T>::type( 1));
312 x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 4>::call(x, typename detail::make_unsigned<T>::type(0x3333333333333333ull), typename detail::make_unsigned<T>::type( 2));
313 x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 8>::call(x, typename detail::make_unsigned<T>::type(0x0F0F0F0F0F0F0F0Full), typename detail::make_unsigned<T>::type( 4));
314 x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 16>::call(x, typename detail::make_unsigned<T>::type(0x00FF00FF00FF00FFull), typename detail::make_unsigned<T>::type( 8));
315 x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 32>::call(x, typename detail::make_unsigned<T>::type(0x0000FFFF0000FFFFull), typename detail::make_unsigned<T>::type(16));
316 x = detail::compute_bitfieldBitCountStep<L, typename detail::make_unsigned<T>::type, Q, detail::is_aligned<Q>::value, sizeof(T) * 8>= 64>::call(x, typename detail::make_unsigned<T>::type(0x00000000FFFFFFFFull), typename detail::make_unsigned<T>::type(32));
317 return vec<L, int, Q>(x);
318
319# if GLM_COMPILER & GLM_COMPILER_VC
320# pragma warning(pop)
321# endif
322 }
323
324 // findLSB
325 template<typename genIUType>
326 GLM_FUNC_QUALIFIER int findLSB(genIUType Value)
327 {
328 GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findLSB' only accept integer values");
329
330 return detail::compute_findLSB<genIUType, sizeof(genIUType) * 8>::call(Value);
331 }
332
333 template<length_t L, typename T, qualifier Q>
334 GLM_FUNC_QUALIFIER vec<L, int, Q> findLSB(vec<L, T, Q> const& x)
335 {
336 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'findLSB' only accept integer values");
337
338 return detail::functor1<vec, L, int, T, Q>::call(findLSB, x);
339 }
340
341 // findMSB
342 template<typename genIUType>
343 GLM_FUNC_QUALIFIER int findMSB(genIUType v)
344 {
345 GLM_STATIC_ASSERT(std::numeric_limits<genIUType>::is_integer, "'findMSB' only accept integer values");
346
347 return findMSB(vec<1, genIUType>(v)).x;
348 }
349
350 template<length_t L, typename T, qualifier Q>
351 GLM_FUNC_QUALIFIER vec<L, int, Q> findMSB(vec<L, T, Q> const& v)
352 {
353 GLM_STATIC_ASSERT(std::numeric_limits<T>::is_integer, "'findMSB' only accept integer values");
354
355 return detail::compute_findMSB_vec<L, T, Q, sizeof(T) * 8>::call(v);
356 }
357}//namespace glm
358
359#if GLM_CONFIG_SIMD == GLM_ENABLE
360# include "func_integer_simd.inl"
361#endif
362