1 //
2 // Copyright 2002 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // mathutil.h: Math and bit manipulation functions.
8
9 #ifndef COMMON_MATHUTIL_H_
10 #define COMMON_MATHUTIL_H_
11
12 #include <math.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <algorithm>
17 #include <limits>
18
19 #include <anglebase/numerics/safe_math.h>
20
21 #include "common/debug.h"
22 #include "common/platform.h"
23
24 namespace angle
25 {
26 using base::CheckedNumeric;
27 using base::IsValueInRangeForNumericType;
28 } // namespace angle
29
30 namespace gl
31 {
32
33 const unsigned int Float32One = 0x3F800000;
34 const unsigned short Float16One = 0x3C00;
35
36 template <typename T>
isPow2(T x)37 inline constexpr bool isPow2(T x)
38 {
39 static_assert(std::is_integral<T>::value, "isPow2 must be called on an integer type.");
40 return (x & (x - 1)) == 0 && (x != 0);
41 }
42
43 template <typename T>
log2(T x)44 inline int log2(T x)
45 {
46 static_assert(std::is_integral<T>::value, "log2 must be called on an integer type.");
47 int r = 0;
48 while ((x >> r) > 1)
49 r++;
50 return r;
51 }
52
ceilPow2(unsigned int x)53 inline unsigned int ceilPow2(unsigned int x)
54 {
55 if (x != 0)
56 x--;
57 x |= x >> 1;
58 x |= x >> 2;
59 x |= x >> 4;
60 x |= x >> 8;
61 x |= x >> 16;
62 x++;
63
64 return x;
65 }
66
67 template <typename DestT, typename SrcT>
clampCast(SrcT value)68 inline DestT clampCast(SrcT value)
69 {
70 // For floating-point types with denormalization, min returns the minimum positive normalized
71 // value. To find the value that has no values less than it, use numeric_limits::lowest.
72 constexpr const long double destLo =
73 static_cast<long double>(std::numeric_limits<DestT>::lowest());
74 constexpr const long double destHi =
75 static_cast<long double>(std::numeric_limits<DestT>::max());
76 constexpr const long double srcLo =
77 static_cast<long double>(std::numeric_limits<SrcT>::lowest());
78 constexpr long double srcHi = static_cast<long double>(std::numeric_limits<SrcT>::max());
79
80 if (destHi < srcHi)
81 {
82 DestT destMax = std::numeric_limits<DestT>::max();
83 if (value >= static_cast<SrcT>(destMax))
84 {
85 return destMax;
86 }
87 }
88
89 if (destLo > srcLo)
90 {
91 DestT destLow = std::numeric_limits<DestT>::lowest();
92 if (value <= static_cast<SrcT>(destLow))
93 {
94 return destLow;
95 }
96 }
97
98 return static_cast<DestT>(value);
99 }
100
101 // Specialize clampCast for bool->int conversion to avoid MSVS 2015 performance warning when the max
102 // value is casted to the source type.
103 template <>
clampCast(bool value)104 inline unsigned int clampCast(bool value)
105 {
106 return static_cast<unsigned int>(value);
107 }
108
109 template <>
clampCast(bool value)110 inline int clampCast(bool value)
111 {
112 return static_cast<int>(value);
113 }
114
115 template <typename T, typename MIN, typename MAX>
clamp(T x,MIN min,MAX max)116 inline T clamp(T x, MIN min, MAX max)
117 {
118 // Since NaNs fail all comparison tests, a NaN value will default to min
119 return x > min ? (x > max ? max : x) : min;
120 }
121
clamp01(float x)122 inline float clamp01(float x)
123 {
124 return clamp(x, 0.0f, 1.0f);
125 }
126
127 template <const int n>
unorm(float x)128 inline unsigned int unorm(float x)
129 {
130 const unsigned int max = 0xFFFFFFFF >> (32 - n);
131
132 if (x > 1)
133 {
134 return max;
135 }
136 else if (x < 0)
137 {
138 return 0;
139 }
140 else
141 {
142 return (unsigned int)(max * x + 0.5f);
143 }
144 }
145
supportsSSE2()146 inline bool supportsSSE2()
147 {
148 #if defined(ANGLE_USE_SSE)
149 static bool checked = false;
150 static bool supports = false;
151
152 if (checked)
153 {
154 return supports;
155 }
156
157 # if defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) && !defined(_M_ARM64)
158 {
159 int info[4];
160 __cpuid(info, 0);
161
162 if (info[0] >= 1)
163 {
164 __cpuid(info, 1);
165
166 supports = (info[3] >> 26) & 1;
167 }
168 }
169 # endif // defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) && !defined(_M_ARM64)
170 checked = true;
171 return supports;
172 #else // defined(ANGLE_USE_SSE)
173 return false;
174 #endif
175 }
176
177 template <typename destType, typename sourceType>
bitCast(const sourceType & source)178 destType bitCast(const sourceType &source)
179 {
180 size_t copySize = std::min(sizeof(destType), sizeof(sourceType));
181 destType output;
182 memcpy(&output, &source, copySize);
183 return output;
184 }
185
186 // https://stackoverflow.com/a/37581284
187 template <typename T>
normalize(T value)188 static constexpr double normalize(T value)
189 {
190 return value < 0 ? -static_cast<double>(value) / std::numeric_limits<T>::min()
191 : static_cast<double>(value) / std::numeric_limits<T>::max();
192 }
193
float32ToFloat16(float fp32)194 inline unsigned short float32ToFloat16(float fp32)
195 {
196 unsigned int fp32i = bitCast<unsigned int>(fp32);
197 unsigned int sign = (fp32i & 0x80000000) >> 16;
198 unsigned int abs = fp32i & 0x7FFFFFFF;
199
200 if (abs > 0x7F800000)
201 { // NaN
202 return 0x7FFF;
203 }
204 else if (abs > 0x47FFEFFF)
205 { // Infinity
206 return static_cast<uint16_t>(sign | 0x7C00);
207 }
208 else if (abs < 0x38800000) // Denormal
209 {
210 unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
211 int e = 113 - (abs >> 23);
212
213 if (e < 24)
214 {
215 abs = mantissa >> e;
216 }
217 else
218 {
219 abs = 0;
220 }
221
222 return static_cast<unsigned short>(sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13);
223 }
224 else
225 {
226 return static_cast<unsigned short>(
227 sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13);
228 }
229 }
230
231 float float16ToFloat32(unsigned short h);
232
233 unsigned int convertRGBFloatsTo999E5(float red, float green, float blue);
234 void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue);
235
float32ToFloat11(float fp32)236 inline unsigned short float32ToFloat11(float fp32)
237 {
238 const unsigned int float32MantissaMask = 0x7FFFFF;
239 const unsigned int float32ExponentMask = 0x7F800000;
240 const unsigned int float32SignMask = 0x80000000;
241 const unsigned int float32ValueMask = ~float32SignMask;
242 const unsigned int float32ExponentFirstBit = 23;
243 const unsigned int float32ExponentBias = 127;
244
245 const unsigned short float11Max = 0x7BF;
246 const unsigned short float11MantissaMask = 0x3F;
247 const unsigned short float11ExponentMask = 0x7C0;
248 const unsigned short float11BitMask = 0x7FF;
249 const unsigned int float11ExponentBias = 14;
250
251 const unsigned int float32Maxfloat11 = 0x477E0000;
252 const unsigned int float32Minfloat11 = 0x38800000;
253
254 const unsigned int float32Bits = bitCast<unsigned int>(fp32);
255 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
256
257 unsigned int float32Val = float32Bits & float32ValueMask;
258
259 if ((float32Val & float32ExponentMask) == float32ExponentMask)
260 {
261 // INF or NAN
262 if ((float32Val & float32MantissaMask) != 0)
263 {
264 return float11ExponentMask |
265 (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) &
266 float11MantissaMask);
267 }
268 else if (float32Sign)
269 {
270 // -INF is clamped to 0 since float11 is positive only
271 return 0;
272 }
273 else
274 {
275 return float11ExponentMask;
276 }
277 }
278 else if (float32Sign)
279 {
280 // float11 is positive only, so clamp to zero
281 return 0;
282 }
283 else if (float32Val > float32Maxfloat11)
284 {
285 // The number is too large to be represented as a float11, set to max
286 return float11Max;
287 }
288 else
289 {
290 if (float32Val < float32Minfloat11)
291 {
292 // The number is too small to be represented as a normalized float11
293 // Convert it to a denormalized value.
294 const unsigned int shift = (float32ExponentBias - float11ExponentBias) -
295 (float32Val >> float32ExponentFirstBit);
296 float32Val =
297 ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
298 }
299 else
300 {
301 // Rebias the exponent to represent the value as a normalized float11
302 float32Val += 0xC8000000;
303 }
304
305 return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
306 }
307 }
308
float32ToFloat10(float fp32)309 inline unsigned short float32ToFloat10(float fp32)
310 {
311 const unsigned int float32MantissaMask = 0x7FFFFF;
312 const unsigned int float32ExponentMask = 0x7F800000;
313 const unsigned int float32SignMask = 0x80000000;
314 const unsigned int float32ValueMask = ~float32SignMask;
315 const unsigned int float32ExponentFirstBit = 23;
316 const unsigned int float32ExponentBias = 127;
317
318 const unsigned short float10Max = 0x3DF;
319 const unsigned short float10MantissaMask = 0x1F;
320 const unsigned short float10ExponentMask = 0x3E0;
321 const unsigned short float10BitMask = 0x3FF;
322 const unsigned int float10ExponentBias = 14;
323
324 const unsigned int float32Maxfloat10 = 0x477C0000;
325 const unsigned int float32Minfloat10 = 0x38800000;
326
327 const unsigned int float32Bits = bitCast<unsigned int>(fp32);
328 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
329
330 unsigned int float32Val = float32Bits & float32ValueMask;
331
332 if ((float32Val & float32ExponentMask) == float32ExponentMask)
333 {
334 // INF or NAN
335 if ((float32Val & float32MantissaMask) != 0)
336 {
337 return float10ExponentMask |
338 (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) &
339 float10MantissaMask);
340 }
341 else if (float32Sign)
342 {
343 // -INF is clamped to 0 since float11 is positive only
344 return 0;
345 }
346 else
347 {
348 return float10ExponentMask;
349 }
350 }
351 else if (float32Sign)
352 {
353 // float10 is positive only, so clamp to zero
354 return 0;
355 }
356 else if (float32Val > float32Maxfloat10)
357 {
358 // The number is too large to be represented as a float11, set to max
359 return float10Max;
360 }
361 else
362 {
363 if (float32Val < float32Minfloat10)
364 {
365 // The number is too small to be represented as a normalized float11
366 // Convert it to a denormalized value.
367 const unsigned int shift = (float32ExponentBias - float10ExponentBias) -
368 (float32Val >> float32ExponentFirstBit);
369 float32Val =
370 ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
371 }
372 else
373 {
374 // Rebias the exponent to represent the value as a normalized float11
375 float32Val += 0xC8000000;
376 }
377
378 return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
379 }
380 }
381
float11ToFloat32(unsigned short fp11)382 inline float float11ToFloat32(unsigned short fp11)
383 {
384 unsigned short exponent = (fp11 >> 6) & 0x1F;
385 unsigned short mantissa = fp11 & 0x3F;
386
387 if (exponent == 0x1F)
388 {
389 // INF or NAN
390 return bitCast<float>(0x7f800000 | (mantissa << 17));
391 }
392 else
393 {
394 if (exponent != 0)
395 {
396 // normalized
397 }
398 else if (mantissa != 0)
399 {
400 // The value is denormalized
401 exponent = 1;
402
403 do
404 {
405 exponent--;
406 mantissa <<= 1;
407 } while ((mantissa & 0x40) == 0);
408
409 mantissa = mantissa & 0x3F;
410 }
411 else // The value is zero
412 {
413 exponent = static_cast<unsigned short>(-112);
414 }
415
416 return bitCast<float>(((exponent + 112) << 23) | (mantissa << 17));
417 }
418 }
419
float10ToFloat32(unsigned short fp11)420 inline float float10ToFloat32(unsigned short fp11)
421 {
422 unsigned short exponent = (fp11 >> 5) & 0x1F;
423 unsigned short mantissa = fp11 & 0x1F;
424
425 if (exponent == 0x1F)
426 {
427 // INF or NAN
428 return bitCast<float>(0x7f800000 | (mantissa << 17));
429 }
430 else
431 {
432 if (exponent != 0)
433 {
434 // normalized
435 }
436 else if (mantissa != 0)
437 {
438 // The value is denormalized
439 exponent = 1;
440
441 do
442 {
443 exponent--;
444 mantissa <<= 1;
445 } while ((mantissa & 0x20) == 0);
446
447 mantissa = mantissa & 0x1F;
448 }
449 else // The value is zero
450 {
451 exponent = static_cast<unsigned short>(-112);
452 }
453
454 return bitCast<float>(((exponent + 112) << 23) | (mantissa << 18));
455 }
456 }
457
458 // Convers to and from float and 16.16 fixed point format.
459
ConvertFixedToFloat(uint32_t fixedInput)460 inline float ConvertFixedToFloat(uint32_t fixedInput)
461 {
462 return static_cast<float>(fixedInput) / 65536.0f;
463 }
464
ConvertFloatToFixed(float floatInput)465 inline uint32_t ConvertFloatToFixed(float floatInput)
466 {
467 static constexpr uint32_t kHighest = 32767 * 65536 + 65535;
468 static constexpr uint32_t kLowest = static_cast<uint32_t>(-32768 * 65536 + 65535);
469
470 if (floatInput > 32767.65535)
471 {
472 return kHighest;
473 }
474 else if (floatInput < -32768.65535)
475 {
476 return kLowest;
477 }
478 else
479 {
480 return static_cast<uint32_t>(floatInput * 65536);
481 }
482 }
483
484 template <typename T>
normalizedToFloat(T input)485 inline float normalizedToFloat(T input)
486 {
487 static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
488
489 if (sizeof(T) > 2)
490 {
491 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
492 constexpr double inverseMax = 1.0 / std::numeric_limits<T>::max();
493 return static_cast<float>(input * inverseMax);
494 }
495 else
496 {
497 constexpr float inverseMax = 1.0f / std::numeric_limits<T>::max();
498 return input * inverseMax;
499 }
500 }
501
502 template <unsigned int inputBitCount, typename T>
normalizedToFloat(T input)503 inline float normalizedToFloat(T input)
504 {
505 static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
506 static_assert(inputBitCount < (sizeof(T) * 8), "T must have more bits than inputBitCount.");
507 ASSERT((input & ~((1 << inputBitCount) - 1)) == 0);
508
509 if (inputBitCount > 23)
510 {
511 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
512 constexpr double inverseMax = 1.0 / ((1 << inputBitCount) - 1);
513 return static_cast<float>(input * inverseMax);
514 }
515 else
516 {
517 constexpr float inverseMax = 1.0f / ((1 << inputBitCount) - 1);
518 return input * inverseMax;
519 }
520 }
521
522 template <typename T>
floatToNormalized(float input)523 inline T floatToNormalized(float input)
524 {
525 if (sizeof(T) > 2)
526 {
527 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
528 return static_cast<T>(std::numeric_limits<T>::max() * static_cast<double>(input) + 0.5);
529 }
530 else
531 {
532 return static_cast<T>(std::numeric_limits<T>::max() * input + 0.5f);
533 }
534 }
535
536 template <unsigned int outputBitCount, typename T>
floatToNormalized(float input)537 inline T floatToNormalized(float input)
538 {
539 static_assert(outputBitCount < (sizeof(T) * 8), "T must have more bits than outputBitCount.");
540
541 if (outputBitCount > 23)
542 {
543 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
544 return static_cast<T>(((1 << outputBitCount) - 1) * static_cast<double>(input) + 0.5);
545 }
546 else
547 {
548 return static_cast<T>(((1 << outputBitCount) - 1) * input + 0.5f);
549 }
550 }
551
552 template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
getShiftedData(T input)553 inline T getShiftedData(T input)
554 {
555 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
556 "T must have at least as many bits as inputBitCount + inputBitStart.");
557 const T mask = (1 << inputBitCount) - 1;
558 return (input >> inputBitStart) & mask;
559 }
560
561 template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
shiftData(T input)562 inline T shiftData(T input)
563 {
564 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
565 "T must have at least as many bits as inputBitCount + inputBitStart.");
566 const T mask = (1 << inputBitCount) - 1;
567 return (input & mask) << inputBitStart;
568 }
569
CountLeadingZeros(uint32_t x)570 inline unsigned int CountLeadingZeros(uint32_t x)
571 {
572 // Use binary search to find the amount of leading zeros.
573 unsigned int zeros = 32u;
574 uint32_t y;
575
576 y = x >> 16u;
577 if (y != 0)
578 {
579 zeros = zeros - 16u;
580 x = y;
581 }
582 y = x >> 8u;
583 if (y != 0)
584 {
585 zeros = zeros - 8u;
586 x = y;
587 }
588 y = x >> 4u;
589 if (y != 0)
590 {
591 zeros = zeros - 4u;
592 x = y;
593 }
594 y = x >> 2u;
595 if (y != 0)
596 {
597 zeros = zeros - 2u;
598 x = y;
599 }
600 y = x >> 1u;
601 if (y != 0)
602 {
603 return zeros - 2u;
604 }
605 return zeros - x;
606 }
607
average(unsigned char a,unsigned char b)608 inline unsigned char average(unsigned char a, unsigned char b)
609 {
610 return ((a ^ b) >> 1) + (a & b);
611 }
612
average(signed char a,signed char b)613 inline signed char average(signed char a, signed char b)
614 {
615 return ((short)a + (short)b) / 2;
616 }
617
average(unsigned short a,unsigned short b)618 inline unsigned short average(unsigned short a, unsigned short b)
619 {
620 return ((a ^ b) >> 1) + (a & b);
621 }
622
average(signed short a,signed short b)623 inline signed short average(signed short a, signed short b)
624 {
625 return ((int)a + (int)b) / 2;
626 }
627
average(unsigned int a,unsigned int b)628 inline unsigned int average(unsigned int a, unsigned int b)
629 {
630 return ((a ^ b) >> 1) + (a & b);
631 }
632
average(int a,int b)633 inline int average(int a, int b)
634 {
635 long long average = (static_cast<long long>(a) + static_cast<long long>(b)) / 2ll;
636 return static_cast<int>(average);
637 }
638
average(float a,float b)639 inline float average(float a, float b)
640 {
641 return (a + b) * 0.5f;
642 }
643
averageHalfFloat(unsigned short a,unsigned short b)644 inline unsigned short averageHalfFloat(unsigned short a, unsigned short b)
645 {
646 return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f);
647 }
648
averageFloat11(unsigned int a,unsigned int b)649 inline unsigned int averageFloat11(unsigned int a, unsigned int b)
650 {
651 return float32ToFloat11((float11ToFloat32(static_cast<unsigned short>(a)) +
652 float11ToFloat32(static_cast<unsigned short>(b))) *
653 0.5f);
654 }
655
averageFloat10(unsigned int a,unsigned int b)656 inline unsigned int averageFloat10(unsigned int a, unsigned int b)
657 {
658 return float32ToFloat10((float10ToFloat32(static_cast<unsigned short>(a)) +
659 float10ToFloat32(static_cast<unsigned short>(b))) *
660 0.5f);
661 }
662
663 template <typename T>
664 class Range
665 {
666 public:
Range()667 Range() {}
Range(T lo,T hi)668 Range(T lo, T hi) : mLow(lo), mHigh(hi) {}
669
length()670 T length() const { return (empty() ? 0 : (mHigh - mLow)); }
671
intersects(Range<T> other)672 bool intersects(Range<T> other)
673 {
674 if (mLow <= other.mLow)
675 {
676 return other.mLow < mHigh;
677 }
678 else
679 {
680 return mLow < other.mHigh;
681 }
682 }
683
684 // Assumes that end is non-inclusive.. for example, extending to 5 will make "end" 6.
extend(T value)685 void extend(T value)
686 {
687 mLow = value < mLow ? value : mLow;
688 mHigh = value >= mHigh ? (value + 1) : mHigh;
689 }
690
empty()691 bool empty() const { return mHigh <= mLow; }
692
contains(T value)693 bool contains(T value) const { return value >= mLow && value < mHigh; }
694
695 class Iterator final
696 {
697 public:
Iterator(T value)698 Iterator(T value) : mCurrent(value) {}
699
700 Iterator &operator++()
701 {
702 mCurrent++;
703 return *this;
704 }
705 bool operator==(const Iterator &other) const { return mCurrent == other.mCurrent; }
706 bool operator!=(const Iterator &other) const { return mCurrent != other.mCurrent; }
707 T operator*() const { return mCurrent; }
708
709 private:
710 T mCurrent;
711 };
712
begin()713 Iterator begin() const { return Iterator(mLow); }
714
end()715 Iterator end() const { return Iterator(mHigh); }
716
low()717 T low() const { return mLow; }
high()718 T high() const { return mHigh; }
719
invalidate()720 void invalidate()
721 {
722 mLow = std::numeric_limits<T>::max();
723 mHigh = std::numeric_limits<T>::min();
724 }
725
726 private:
727 T mLow;
728 T mHigh;
729 };
730
731 typedef Range<int> RangeI;
732 typedef Range<unsigned int> RangeUI;
733
734 struct IndexRange
735 {
736 struct Undefined
737 {};
IndexRangeIndexRange738 IndexRange(Undefined) {}
IndexRangeIndexRange739 IndexRange() : IndexRange(0, 0, 0) {}
IndexRangeIndexRange740 IndexRange(size_t start_, size_t end_, size_t vertexIndexCount_)
741 : start(start_), end(end_), vertexIndexCount(vertexIndexCount_)
742 {
743 ASSERT(start <= end);
744 }
745
746 // Number of vertices in the range.
vertexCountIndexRange747 size_t vertexCount() const { return (end - start) + 1; }
748
749 // Inclusive range of indices that are not primitive restart
750 size_t start;
751 size_t end;
752
753 // Number of non-primitive restart indices
754 size_t vertexIndexCount;
755 };
756
757 // Combine a floating-point value representing a mantissa (x) and an integer exponent (exp) into a
758 // floating-point value. As in GLSL ldexp() built-in.
Ldexp(float x,int exp)759 inline float Ldexp(float x, int exp)
760 {
761 if (exp > 128)
762 {
763 return std::numeric_limits<float>::infinity();
764 }
765 if (exp < -126)
766 {
767 return 0.0f;
768 }
769 double result = static_cast<double>(x) * std::pow(2.0, static_cast<double>(exp));
770 return static_cast<float>(result);
771 }
772
773 // First, both normalized floating-point values are converted into 16-bit integer values.
774 // Then, the results are packed into the returned 32-bit unsigned integer.
775 // The first float value will be written to the least significant bits of the output;
776 // the last float value will be written to the most significant bits.
777 // The conversion of each value to fixed point is done as follows :
778 // packSnorm2x16 : round(clamp(c, -1, +1) * 32767.0)
packSnorm2x16(float f1,float f2)779 inline uint32_t packSnorm2x16(float f1, float f2)
780 {
781 int16_t leastSignificantBits = static_cast<int16_t>(roundf(clamp(f1, -1.0f, 1.0f) * 32767.0f));
782 int16_t mostSignificantBits = static_cast<int16_t>(roundf(clamp(f2, -1.0f, 1.0f) * 32767.0f));
783 return static_cast<uint32_t>(mostSignificantBits) << 16 |
784 (static_cast<uint32_t>(leastSignificantBits) & 0xFFFF);
785 }
786
787 // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then,
788 // each component is converted to a normalized floating-point value to generate the returned two
789 // float values. The first float value will be extracted from the least significant bits of the
790 // input; the last float value will be extracted from the most-significant bits. The conversion for
791 // unpacked fixed-point value to floating point is done as follows: unpackSnorm2x16 : clamp(f /
792 // 32767.0, -1, +1)
unpackSnorm2x16(uint32_t u,float * f1,float * f2)793 inline void unpackSnorm2x16(uint32_t u, float *f1, float *f2)
794 {
795 int16_t leastSignificantBits = static_cast<int16_t>(u & 0xFFFF);
796 int16_t mostSignificantBits = static_cast<int16_t>(u >> 16);
797 *f1 = clamp(static_cast<float>(leastSignificantBits) / 32767.0f, -1.0f, 1.0f);
798 *f2 = clamp(static_cast<float>(mostSignificantBits) / 32767.0f, -1.0f, 1.0f);
799 }
800
801 // First, both normalized floating-point values are converted into 16-bit integer values.
802 // Then, the results are packed into the returned 32-bit unsigned integer.
803 // The first float value will be written to the least significant bits of the output;
804 // the last float value will be written to the most significant bits.
805 // The conversion of each value to fixed point is done as follows:
806 // packUnorm2x16 : round(clamp(c, 0, +1) * 65535.0)
packUnorm2x16(float f1,float f2)807 inline uint32_t packUnorm2x16(float f1, float f2)
808 {
809 uint16_t leastSignificantBits = static_cast<uint16_t>(roundf(clamp(f1, 0.0f, 1.0f) * 65535.0f));
810 uint16_t mostSignificantBits = static_cast<uint16_t>(roundf(clamp(f2, 0.0f, 1.0f) * 65535.0f));
811 return static_cast<uint32_t>(mostSignificantBits) << 16 |
812 static_cast<uint32_t>(leastSignificantBits);
813 }
814
815 // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then,
816 // each component is converted to a normalized floating-point value to generate the returned two
817 // float values. The first float value will be extracted from the least significant bits of the
818 // input; the last float value will be extracted from the most-significant bits. The conversion for
819 // unpacked fixed-point value to floating point is done as follows: unpackUnorm2x16 : f / 65535.0
unpackUnorm2x16(uint32_t u,float * f1,float * f2)820 inline void unpackUnorm2x16(uint32_t u, float *f1, float *f2)
821 {
822 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
823 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
824 *f1 = static_cast<float>(leastSignificantBits) / 65535.0f;
825 *f2 = static_cast<float>(mostSignificantBits) / 65535.0f;
826 }
827
828 // Helper functions intended to be used only here.
829 namespace priv
830 {
831
ToPackedUnorm8(float f)832 inline uint8_t ToPackedUnorm8(float f)
833 {
834 return static_cast<uint8_t>(roundf(clamp(f, 0.0f, 1.0f) * 255.0f));
835 }
836
ToPackedSnorm8(float f)837 inline int8_t ToPackedSnorm8(float f)
838 {
839 return static_cast<int8_t>(roundf(clamp(f, -1.0f, 1.0f) * 127.0f));
840 }
841
842 } // namespace priv
843
844 // Packs 4 normalized unsigned floating-point values to a single 32-bit unsigned integer. Works
845 // similarly to packUnorm2x16. The floats are clamped to the range 0.0 to 1.0, and written to the
846 // unsigned integer starting from the least significant bits.
PackUnorm4x8(float f1,float f2,float f3,float f4)847 inline uint32_t PackUnorm4x8(float f1, float f2, float f3, float f4)
848 {
849 uint8_t bits[4];
850 bits[0] = priv::ToPackedUnorm8(f1);
851 bits[1] = priv::ToPackedUnorm8(f2);
852 bits[2] = priv::ToPackedUnorm8(f3);
853 bits[3] = priv::ToPackedUnorm8(f4);
854 uint32_t result = 0u;
855 for (int i = 0; i < 4; ++i)
856 {
857 int shift = i * 8;
858 result |= (static_cast<uint32_t>(bits[i]) << shift);
859 }
860 return result;
861 }
862
863 // Unpacks 4 normalized unsigned floating-point values from a single 32-bit unsigned integer into f.
864 // Works similarly to unpackUnorm2x16. The floats are unpacked starting from the least significant
865 // bits.
UnpackUnorm4x8(uint32_t u,float * f)866 inline void UnpackUnorm4x8(uint32_t u, float *f)
867 {
868 for (int i = 0; i < 4; ++i)
869 {
870 int shift = i * 8;
871 uint8_t bits = static_cast<uint8_t>((u >> shift) & 0xFF);
872 f[i] = static_cast<float>(bits) / 255.0f;
873 }
874 }
875
876 // Packs 4 normalized signed floating-point values to a single 32-bit unsigned integer. The floats
877 // are clamped to the range -1.0 to 1.0, and written to the unsigned integer starting from the least
878 // significant bits.
PackSnorm4x8(float f1,float f2,float f3,float f4)879 inline uint32_t PackSnorm4x8(float f1, float f2, float f3, float f4)
880 {
881 int8_t bits[4];
882 bits[0] = priv::ToPackedSnorm8(f1);
883 bits[1] = priv::ToPackedSnorm8(f2);
884 bits[2] = priv::ToPackedSnorm8(f3);
885 bits[3] = priv::ToPackedSnorm8(f4);
886 uint32_t result = 0u;
887 for (int i = 0; i < 4; ++i)
888 {
889 int shift = i * 8;
890 result |= ((static_cast<uint32_t>(bits[i]) & 0xFF) << shift);
891 }
892 return result;
893 }
894
895 // Unpacks 4 normalized signed floating-point values from a single 32-bit unsigned integer into f.
896 // Works similarly to unpackSnorm2x16. The floats are unpacked starting from the least significant
897 // bits, and clamped to the range -1.0 to 1.0.
UnpackSnorm4x8(uint32_t u,float * f)898 inline void UnpackSnorm4x8(uint32_t u, float *f)
899 {
900 for (int i = 0; i < 4; ++i)
901 {
902 int shift = i * 8;
903 int8_t bits = static_cast<int8_t>((u >> shift) & 0xFF);
904 f[i] = clamp(static_cast<float>(bits) / 127.0f, -1.0f, 1.0f);
905 }
906 }
907
908 // Returns an unsigned integer obtained by converting the two floating-point values to the 16-bit
909 // floating-point representation found in the OpenGL ES Specification, and then packing these
910 // two 16-bit integers into a 32-bit unsigned integer.
911 // f1: The 16 least-significant bits of the result;
912 // f2: The 16 most-significant bits.
packHalf2x16(float f1,float f2)913 inline uint32_t packHalf2x16(float f1, float f2)
914 {
915 uint16_t leastSignificantBits = static_cast<uint16_t>(float32ToFloat16(f1));
916 uint16_t mostSignificantBits = static_cast<uint16_t>(float32ToFloat16(f2));
917 return static_cast<uint32_t>(mostSignificantBits) << 16 |
918 static_cast<uint32_t>(leastSignificantBits);
919 }
920
921 // Returns two floating-point values obtained by unpacking a 32-bit unsigned integer into a pair of
922 // 16-bit values, interpreting those values as 16-bit floating-point numbers according to the OpenGL
923 // ES Specification, and converting them to 32-bit floating-point values. The first float value is
924 // obtained from the 16 least-significant bits of u; the second component is obtained from the 16
925 // most-significant bits of u.
unpackHalf2x16(uint32_t u,float * f1,float * f2)926 inline void unpackHalf2x16(uint32_t u, float *f1, float *f2)
927 {
928 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
929 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
930
931 *f1 = float16ToFloat32(leastSignificantBits);
932 *f2 = float16ToFloat32(mostSignificantBits);
933 }
934
sRGBToLinear(uint8_t srgbValue)935 inline uint8_t sRGBToLinear(uint8_t srgbValue)
936 {
937 float value = srgbValue / 255.0f;
938 if (value <= 0.04045f)
939 {
940 value = value / 12.92f;
941 }
942 else
943 {
944 value = std::pow((value + 0.055f) / 1.055f, 2.4f);
945 }
946 return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f));
947 }
948
linearToSRGB(uint8_t linearValue)949 inline uint8_t linearToSRGB(uint8_t linearValue)
950 {
951 float value = linearValue / 255.0f;
952 if (value <= 0.0f)
953 {
954 value = 0.0f;
955 }
956 else if (value < 0.0031308f)
957 {
958 value = value * 12.92f;
959 }
960 else if (value < 1.0f)
961 {
962 value = std::pow(value, 0.41666f) * 1.055f - 0.055f;
963 }
964 else
965 {
966 value = 1.0f;
967 }
968 return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f));
969 }
970
971 // Reverse the order of the bits.
BitfieldReverse(uint32_t value)972 inline uint32_t BitfieldReverse(uint32_t value)
973 {
974 // TODO(oetuaho@nvidia.com): Optimize this if needed. There don't seem to be compiler intrinsics
975 // for this, and right now it's not used in performance-critical paths.
976 uint32_t result = 0u;
977 for (size_t j = 0u; j < 32u; ++j)
978 {
979 result |= (((value >> j) & 1u) << (31u - j));
980 }
981 return result;
982 }
983
984 // Count the 1 bits.
985 #if defined(_MSC_VER) && !defined(__clang__)
986 # if defined(_M_IX86) || defined(_M_X64)
987 namespace priv
988 {
989 // Check POPCNT instruction support and cache the result.
990 // https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64#remarks
991 static const bool kHasPopcnt = [] {
992 int info[4];
993 __cpuid(&info[0], 1);
994 return static_cast<bool>(info[2] & 0x800000);
995 }();
996 } // namespace priv
997
998 // Polyfills for x86/x64 CPUs without POPCNT.
999 // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
BitCountPolyfill(uint32_t bits)1000 inline int BitCountPolyfill(uint32_t bits)
1001 {
1002 bits = bits - ((bits >> 1) & 0x55555555);
1003 bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
1004 bits = ((bits + (bits >> 4) & 0x0F0F0F0F) * 0x01010101) >> 24;
1005 return static_cast<int>(bits);
1006 }
1007
BitCountPolyfill(uint64_t bits)1008 inline int BitCountPolyfill(uint64_t bits)
1009 {
1010 bits = bits - ((bits >> 1) & 0x5555555555555555ull);
1011 bits = (bits & 0x3333333333333333ull) + ((bits >> 2) & 0x3333333333333333ull);
1012 bits = ((bits + (bits >> 4) & 0x0F0F0F0F0F0F0F0Full) * 0x0101010101010101ull) >> 56;
1013 return static_cast<int>(bits);
1014 }
1015
BitCount(uint32_t bits)1016 inline int BitCount(uint32_t bits)
1017 {
1018 if (priv::kHasPopcnt)
1019 {
1020 return static_cast<int>(__popcnt(bits));
1021 }
1022 return BitCountPolyfill(bits);
1023 }
1024
BitCount(uint64_t bits)1025 inline int BitCount(uint64_t bits)
1026 {
1027 if (priv::kHasPopcnt)
1028 {
1029 # if defined(_M_X64)
1030 return static_cast<int>(__popcnt64(bits));
1031 # else // x86
1032 return static_cast<int>(__popcnt(static_cast<uint32_t>(bits >> 32)) +
1033 __popcnt(static_cast<uint32_t>(bits)));
1034 # endif // defined(_M_X64)
1035 }
1036 return BitCountPolyfill(bits);
1037 }
1038
1039 # elif defined(_M_ARM) || defined(_M_ARM64)
1040
1041 // MSVC's _CountOneBits* intrinsics are not defined for ARM64, moreover they do not use dedicated
1042 // NEON instructions.
1043
BitCount(uint32_t bits)1044 inline int BitCount(uint32_t bits)
1045 {
1046 // cast bits to 8x8 datatype and use VCNT on it
1047 const uint8x8_t vsum = vcnt_u8(vcreate_u8(static_cast<uint64_t>(bits)));
1048
1049 // pairwise sums: 8x8 -> 16x4 -> 32x2
1050 return static_cast<int>(vget_lane_u32(vpaddl_u16(vpaddl_u8(vsum)), 0));
1051 }
1052
BitCount(uint64_t bits)1053 inline int BitCount(uint64_t bits)
1054 {
1055 // cast bits to 8x8 datatype and use VCNT on it
1056 const uint8x8_t vsum = vcnt_u8(vcreate_u8(bits));
1057
1058 // pairwise sums: 8x8 -> 16x4 -> 32x2 -> 64x1
1059 return static_cast<int>(vget_lane_u64(vpaddl_u32(vpaddl_u16(vpaddl_u8(vsum))), 0));
1060 }
1061 # endif // defined(_M_IX86) || defined(_M_X64)
1062 #endif // defined(_MSC_VER) && !defined(__clang__)
1063
1064 #if defined(ANGLE_PLATFORM_POSIX) || defined(__clang__)
BitCount(uint32_t bits)1065 inline int BitCount(uint32_t bits)
1066 {
1067 return __builtin_popcount(bits);
1068 }
1069
BitCount(uint64_t bits)1070 inline int BitCount(uint64_t bits)
1071 {
1072 return __builtin_popcountll(bits);
1073 }
1074 #endif // defined(ANGLE_PLATFORM_POSIX) || defined(__clang__)
1075
BitCount(uint8_t bits)1076 inline int BitCount(uint8_t bits)
1077 {
1078 return BitCount(static_cast<uint32_t>(bits));
1079 }
1080
BitCount(uint16_t bits)1081 inline int BitCount(uint16_t bits)
1082 {
1083 return BitCount(static_cast<uint32_t>(bits));
1084 }
1085
1086 #if defined(ANGLE_PLATFORM_WINDOWS)
1087 // Return the index of the least significant bit set. Indexing is such that bit 0 is the least
1088 // significant bit. Implemented for different bit widths on different platforms.
ScanForward(uint32_t bits)1089 inline unsigned long ScanForward(uint32_t bits)
1090 {
1091 ASSERT(bits != 0u);
1092 unsigned long firstBitIndex = 0ul;
1093 unsigned char ret = _BitScanForward(&firstBitIndex, bits);
1094 ASSERT(ret != 0u);
1095 return firstBitIndex;
1096 }
1097
ScanForward(uint64_t bits)1098 inline unsigned long ScanForward(uint64_t bits)
1099 {
1100 ASSERT(bits != 0u);
1101 unsigned long firstBitIndex = 0ul;
1102 # if defined(ANGLE_IS_64_BIT_CPU)
1103 unsigned char ret = _BitScanForward64(&firstBitIndex, bits);
1104 # else
1105 unsigned char ret;
1106 if (static_cast<uint32_t>(bits) == 0)
1107 {
1108 ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits >> 32));
1109 firstBitIndex += 32ul;
1110 }
1111 else
1112 {
1113 ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits));
1114 }
1115 # endif // defined(ANGLE_IS_64_BIT_CPU)
1116 ASSERT(ret != 0u);
1117 return firstBitIndex;
1118 }
1119 #endif // defined(ANGLE_PLATFORM_WINDOWS)
1120
1121 #if defined(ANGLE_PLATFORM_POSIX)
ScanForward(uint32_t bits)1122 inline unsigned long ScanForward(uint32_t bits)
1123 {
1124 ASSERT(bits != 0u);
1125 return static_cast<unsigned long>(__builtin_ctz(bits));
1126 }
1127
ScanForward(uint64_t bits)1128 inline unsigned long ScanForward(uint64_t bits)
1129 {
1130 ASSERT(bits != 0u);
1131 # if defined(ANGLE_IS_64_BIT_CPU)
1132 return static_cast<unsigned long>(__builtin_ctzll(bits));
1133 # else
1134 return static_cast<unsigned long>(static_cast<uint32_t>(bits) == 0
1135 ? __builtin_ctz(static_cast<uint32_t>(bits >> 32)) + 32
1136 : __builtin_ctz(static_cast<uint32_t>(bits)));
1137 # endif // defined(ANGLE_IS_64_BIT_CPU)
1138 }
1139 #endif // defined(ANGLE_PLATFORM_POSIX)
1140
ScanForward(uint8_t bits)1141 inline unsigned long ScanForward(uint8_t bits)
1142 {
1143 return ScanForward(static_cast<uint32_t>(bits));
1144 }
1145
ScanForward(uint16_t bits)1146 inline unsigned long ScanForward(uint16_t bits)
1147 {
1148 return ScanForward(static_cast<uint32_t>(bits));
1149 }
1150
1151 // Return the index of the most significant bit set. Indexing is such that bit 0 is the least
1152 // significant bit.
ScanReverse(unsigned long bits)1153 inline unsigned long ScanReverse(unsigned long bits)
1154 {
1155 ASSERT(bits != 0u);
1156 #if defined(ANGLE_PLATFORM_WINDOWS)
1157 unsigned long lastBitIndex = 0ul;
1158 unsigned char ret = _BitScanReverse(&lastBitIndex, bits);
1159 ASSERT(ret != 0u);
1160 return lastBitIndex;
1161 #elif defined(ANGLE_PLATFORM_POSIX)
1162 return static_cast<unsigned long>(sizeof(unsigned long) * CHAR_BIT - 1 - __builtin_clzl(bits));
1163 #else
1164 # error Please implement bit-scan-reverse for your platform!
1165 #endif
1166 }
1167
1168 // Returns -1 on 0, otherwise the index of the least significant 1 bit as in GLSL.
1169 template <typename T>
FindLSB(T bits)1170 int FindLSB(T bits)
1171 {
1172 static_assert(std::is_integral<T>::value, "must be integral type.");
1173 if (bits == 0u)
1174 {
1175 return -1;
1176 }
1177 else
1178 {
1179 return static_cast<int>(ScanForward(bits));
1180 }
1181 }
1182
1183 // Returns -1 on 0, otherwise the index of the most significant 1 bit as in GLSL.
1184 template <typename T>
FindMSB(T bits)1185 int FindMSB(T bits)
1186 {
1187 static_assert(std::is_integral<T>::value, "must be integral type.");
1188 if (bits == 0u)
1189 {
1190 return -1;
1191 }
1192 else
1193 {
1194 return static_cast<int>(ScanReverse(bits));
1195 }
1196 }
1197
1198 // Returns whether the argument is Not a Number.
1199 // IEEE 754 single precision NaN representation: Exponent(8 bits) - 255, Mantissa(23 bits) -
1200 // non-zero.
isNaN(float f)1201 inline bool isNaN(float f)
1202 {
1203 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
1204 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
1205 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) &&
1206 (bitCast<uint32_t>(f) & 0x7fffffu);
1207 }
1208
1209 // Returns whether the argument is infinity.
1210 // IEEE 754 single precision infinity representation: Exponent(8 bits) - 255, Mantissa(23 bits) -
1211 // zero.
isInf(float f)1212 inline bool isInf(float f)
1213 {
1214 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
1215 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
1216 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) &&
1217 !(bitCast<uint32_t>(f) & 0x7fffffu);
1218 }
1219
1220 namespace priv
1221 {
1222 template <unsigned int N, unsigned int R>
1223 struct iSquareRoot
1224 {
solveiSquareRoot1225 static constexpr unsigned int solve()
1226 {
1227 return (R * R > N)
1228 ? 0
1229 : ((R * R == N) ? R : static_cast<unsigned int>(iSquareRoot<N, R + 1>::value));
1230 }
1231 enum Result
1232 {
1233 value = iSquareRoot::solve()
1234 };
1235 };
1236
1237 template <unsigned int N>
1238 struct iSquareRoot<N, N>
1239 {
1240 enum result
1241 {
1242 value = N
1243 };
1244 };
1245
1246 } // namespace priv
1247
1248 template <unsigned int N>
1249 constexpr unsigned int iSquareRoot()
1250 {
1251 return priv::iSquareRoot<N, 1>::value;
1252 }
1253
1254 // Sum, difference and multiplication operations for signed ints that wrap on 32-bit overflow.
1255 //
1256 // Unsigned types are defined to do arithmetic modulo 2^n in C++. For signed types, overflow
1257 // behavior is undefined.
1258
1259 template <typename T>
1260 inline T WrappingSum(T lhs, T rhs)
1261 {
1262 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
1263 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
1264 return static_cast<T>(lhsUnsigned + rhsUnsigned);
1265 }
1266
1267 template <typename T>
1268 inline T WrappingDiff(T lhs, T rhs)
1269 {
1270 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
1271 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
1272 return static_cast<T>(lhsUnsigned - rhsUnsigned);
1273 }
1274
1275 inline int32_t WrappingMul(int32_t lhs, int32_t rhs)
1276 {
1277 int64_t lhsWide = static_cast<int64_t>(lhs);
1278 int64_t rhsWide = static_cast<int64_t>(rhs);
1279 // The multiplication is guaranteed not to overflow.
1280 int64_t resultWide = lhsWide * rhsWide;
1281 // Implement the desired wrapping behavior by masking out the high-order 32 bits.
1282 resultWide = resultWide & 0xffffffffll;
1283 // Casting to a narrower signed type is fine since the casted value is representable in the
1284 // narrower type.
1285 return static_cast<int32_t>(resultWide);
1286 }
1287
1288 inline float scaleScreenDimensionToNdc(float dimensionScreen, float viewportDimension)
1289 {
1290 return 2.0f * dimensionScreen / viewportDimension;
1291 }
1292
1293 inline float scaleScreenCoordinateToNdc(float coordinateScreen, float viewportDimension)
1294 {
1295 float halfShifted = coordinateScreen / viewportDimension;
1296 return 2.0f * (halfShifted - 0.5f);
1297 }
1298
1299 } // namespace gl
1300
1301 namespace rx
1302 {
1303
1304 template <typename T>
1305 T roundUp(const T value, const T alignment)
1306 {
1307 auto temp = value + alignment - static_cast<T>(1);
1308 return temp - temp % alignment;
1309 }
1310
1311 template <typename T>
1312 constexpr T roundUpPow2(const T value, const T alignment)
1313 {
1314 ASSERT(gl::isPow2(alignment));
1315 return (value + alignment - 1) & ~(alignment - 1);
1316 }
1317
1318 template <typename T>
1319 angle::CheckedNumeric<T> CheckedRoundUp(const T value, const T alignment)
1320 {
1321 angle::CheckedNumeric<T> checkedValue(value);
1322 angle::CheckedNumeric<T> checkedAlignment(alignment);
1323 return roundUp(checkedValue, checkedAlignment);
1324 }
1325
1326 inline constexpr unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor)
1327 {
1328 unsigned int divided = value / divisor;
1329 return (divided + ((value % divisor == 0) ? 0 : 1));
1330 }
1331
1332 #if defined(__has_builtin)
1333 # define ANGLE_HAS_BUILTIN(x) __has_builtin(x)
1334 #else
1335 # define ANGLE_HAS_BUILTIN(x) 0
1336 #endif
1337
1338 #if defined(_MSC_VER)
1339
1340 # define ANGLE_ROTL(x, y) _rotl(x, y)
1341 # define ANGLE_ROTL64(x, y) _rotl64(x, y)
1342 # define ANGLE_ROTR16(x, y) _rotr16(x, y)
1343
1344 #elif defined(__clang__) && ANGLE_HAS_BUILTIN(__builtin_rotateleft32) && \
1345 ANGLE_HAS_BUILTIN(__builtin_rotateleft64) && ANGLE_HAS_BUILTIN(__builtin_rotateright16)
1346
1347 # define ANGLE_ROTL(x, y) __builtin_rotateleft32(x, y)
1348 # define ANGLE_ROTL64(x, y) __builtin_rotateleft64(x, y)
1349 # define ANGLE_ROTR16(x, y) __builtin_rotateright16(x, y)
1350
1351 #else
1352
1353 inline uint32_t RotL(uint32_t x, int8_t r)
1354 {
1355 return (x << r) | (x >> (32 - r));
1356 }
1357
1358 inline uint64_t RotL64(uint64_t x, int8_t r)
1359 {
1360 return (x << r) | (x >> (64 - r));
1361 }
1362
1363 inline uint16_t RotR16(uint16_t x, int8_t r)
1364 {
1365 return (x >> r) | (x << (16 - r));
1366 }
1367
1368 # define ANGLE_ROTL(x, y) ::rx::RotL(x, y)
1369 # define ANGLE_ROTL64(x, y) ::rx::RotL64(x, y)
1370 # define ANGLE_ROTR16(x, y) ::rx::RotR16(x, y)
1371
1372 #endif // namespace rx
1373
1374 constexpr unsigned int Log2(unsigned int bytes)
1375 {
1376 return bytes == 1 ? 0 : (1 + Log2(bytes / 2));
1377 }
1378 } // namespace rx
1379
1380 #endif // COMMON_MATHUTIL_H_
1381