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
508 if (inputBitCount > 23)
509 {
510 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
511 constexpr double inverseMax = 1.0 / ((1 << inputBitCount) - 1);
512 return static_cast<float>(input * inverseMax);
513 }
514 else
515 {
516 constexpr float inverseMax = 1.0f / ((1 << inputBitCount) - 1);
517 return input * inverseMax;
518 }
519 }
520
521 template <typename T>
floatToNormalized(float input)522 inline T floatToNormalized(float input)
523 {
524 if (sizeof(T) > 2)
525 {
526 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
527 return static_cast<T>(std::numeric_limits<T>::max() * static_cast<double>(input) + 0.5);
528 }
529 else
530 {
531 return static_cast<T>(std::numeric_limits<T>::max() * input + 0.5f);
532 }
533 }
534
535 template <unsigned int outputBitCount, typename T>
floatToNormalized(float input)536 inline T floatToNormalized(float input)
537 {
538 static_assert(outputBitCount < (sizeof(T) * 8), "T must have more bits than outputBitCount.");
539
540 if (outputBitCount > 23)
541 {
542 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
543 return static_cast<T>(((1 << outputBitCount) - 1) * static_cast<double>(input) + 0.5);
544 }
545 else
546 {
547 return static_cast<T>(((1 << outputBitCount) - 1) * input + 0.5f);
548 }
549 }
550
551 template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
getShiftedData(T input)552 inline T getShiftedData(T input)
553 {
554 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
555 "T must have at least as many bits as inputBitCount + inputBitStart.");
556 const T mask = (1 << inputBitCount) - 1;
557 return (input >> inputBitStart) & mask;
558 }
559
560 template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
shiftData(T input)561 inline T shiftData(T input)
562 {
563 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
564 "T must have at least as many bits as inputBitCount + inputBitStart.");
565 const T mask = (1 << inputBitCount) - 1;
566 return (input & mask) << inputBitStart;
567 }
568
CountLeadingZeros(uint32_t x)569 inline unsigned int CountLeadingZeros(uint32_t x)
570 {
571 // Use binary search to find the amount of leading zeros.
572 unsigned int zeros = 32u;
573 uint32_t y;
574
575 y = x >> 16u;
576 if (y != 0)
577 {
578 zeros = zeros - 16u;
579 x = y;
580 }
581 y = x >> 8u;
582 if (y != 0)
583 {
584 zeros = zeros - 8u;
585 x = y;
586 }
587 y = x >> 4u;
588 if (y != 0)
589 {
590 zeros = zeros - 4u;
591 x = y;
592 }
593 y = x >> 2u;
594 if (y != 0)
595 {
596 zeros = zeros - 2u;
597 x = y;
598 }
599 y = x >> 1u;
600 if (y != 0)
601 {
602 return zeros - 2u;
603 }
604 return zeros - x;
605 }
606
average(unsigned char a,unsigned char b)607 inline unsigned char average(unsigned char a, unsigned char b)
608 {
609 return ((a ^ b) >> 1) + (a & b);
610 }
611
average(signed char a,signed char b)612 inline signed char average(signed char a, signed char b)
613 {
614 return ((short)a + (short)b) / 2;
615 }
616
average(unsigned short a,unsigned short b)617 inline unsigned short average(unsigned short a, unsigned short b)
618 {
619 return ((a ^ b) >> 1) + (a & b);
620 }
621
average(signed short a,signed short b)622 inline signed short average(signed short a, signed short b)
623 {
624 return ((int)a + (int)b) / 2;
625 }
626
average(unsigned int a,unsigned int b)627 inline unsigned int average(unsigned int a, unsigned int b)
628 {
629 return ((a ^ b) >> 1) + (a & b);
630 }
631
average(int a,int b)632 inline int average(int a, int b)
633 {
634 long long average = (static_cast<long long>(a) + static_cast<long long>(b)) / 2ll;
635 return static_cast<int>(average);
636 }
637
average(float a,float b)638 inline float average(float a, float b)
639 {
640 return (a + b) * 0.5f;
641 }
642
averageHalfFloat(unsigned short a,unsigned short b)643 inline unsigned short averageHalfFloat(unsigned short a, unsigned short b)
644 {
645 return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f);
646 }
647
averageFloat11(unsigned int a,unsigned int b)648 inline unsigned int averageFloat11(unsigned int a, unsigned int b)
649 {
650 return float32ToFloat11((float11ToFloat32(static_cast<unsigned short>(a)) +
651 float11ToFloat32(static_cast<unsigned short>(b))) *
652 0.5f);
653 }
654
averageFloat10(unsigned int a,unsigned int b)655 inline unsigned int averageFloat10(unsigned int a, unsigned int b)
656 {
657 return float32ToFloat10((float10ToFloat32(static_cast<unsigned short>(a)) +
658 float10ToFloat32(static_cast<unsigned short>(b))) *
659 0.5f);
660 }
661
662 template <typename T>
663 class Range
664 {
665 public:
Range()666 Range() {}
Range(T lo,T hi)667 Range(T lo, T hi) : mLow(lo), mHigh(hi) {}
668
length()669 T length() const { return (empty() ? 0 : (mHigh - mLow)); }
670
intersects(Range<T> other)671 bool intersects(Range<T> other)
672 {
673 if (mLow <= other.mLow)
674 {
675 return other.mLow < mHigh;
676 }
677 else
678 {
679 return mLow < other.mHigh;
680 }
681 }
682
683 // Assumes that end is non-inclusive.. for example, extending to 5 will make "end" 6.
extend(T value)684 void extend(T value)
685 {
686 mLow = value < mLow ? value : mLow;
687 mHigh = value >= mHigh ? (value + 1) : mHigh;
688 }
689
empty()690 bool empty() const { return mHigh <= mLow; }
691
contains(T value)692 bool contains(T value) const { return value >= mLow && value < mHigh; }
693
694 class Iterator final
695 {
696 public:
Iterator(T value)697 Iterator(T value) : mCurrent(value) {}
698
699 Iterator &operator++()
700 {
701 mCurrent++;
702 return *this;
703 }
704 bool operator==(const Iterator &other) const { return mCurrent == other.mCurrent; }
705 bool operator!=(const Iterator &other) const { return mCurrent != other.mCurrent; }
706 T operator*() const { return mCurrent; }
707
708 private:
709 T mCurrent;
710 };
711
begin()712 Iterator begin() const { return Iterator(mLow); }
713
end()714 Iterator end() const { return Iterator(mHigh); }
715
low()716 T low() const { return mLow; }
high()717 T high() const { return mHigh; }
718
invalidate()719 void invalidate()
720 {
721 mLow = std::numeric_limits<T>::max();
722 mHigh = std::numeric_limits<T>::min();
723 }
724
725 private:
726 T mLow;
727 T mHigh;
728 };
729
730 typedef Range<int> RangeI;
731 typedef Range<unsigned int> RangeUI;
732
733 struct IndexRange
734 {
735 struct Undefined
736 {};
IndexRangeIndexRange737 IndexRange(Undefined) {}
IndexRangeIndexRange738 IndexRange() : IndexRange(0, 0, 0) {}
IndexRangeIndexRange739 IndexRange(size_t start_, size_t end_, size_t vertexIndexCount_)
740 : start(start_), end(end_), vertexIndexCount(vertexIndexCount_)
741 {
742 ASSERT(start <= end);
743 }
744
745 // Number of vertices in the range.
vertexCountIndexRange746 size_t vertexCount() const { return (end - start) + 1; }
747
748 // Inclusive range of indices that are not primitive restart
749 size_t start;
750 size_t end;
751
752 // Number of non-primitive restart indices
753 size_t vertexIndexCount;
754 };
755
756 // Combine a floating-point value representing a mantissa (x) and an integer exponent (exp) into a
757 // floating-point value. As in GLSL ldexp() built-in.
Ldexp(float x,int exp)758 inline float Ldexp(float x, int exp)
759 {
760 if (exp > 128)
761 {
762 return std::numeric_limits<float>::infinity();
763 }
764 if (exp < -126)
765 {
766 return 0.0f;
767 }
768 double result = static_cast<double>(x) * std::pow(2.0, static_cast<double>(exp));
769 return static_cast<float>(result);
770 }
771
772 // First, both normalized floating-point values are converted into 16-bit integer values.
773 // Then, the results are packed into the returned 32-bit unsigned integer.
774 // The first float value will be written to the least significant bits of the output;
775 // the last float value will be written to the most significant bits.
776 // The conversion of each value to fixed point is done as follows :
777 // packSnorm2x16 : round(clamp(c, -1, +1) * 32767.0)
packSnorm2x16(float f1,float f2)778 inline uint32_t packSnorm2x16(float f1, float f2)
779 {
780 int16_t leastSignificantBits = static_cast<int16_t>(roundf(clamp(f1, -1.0f, 1.0f) * 32767.0f));
781 int16_t mostSignificantBits = static_cast<int16_t>(roundf(clamp(f2, -1.0f, 1.0f) * 32767.0f));
782 return static_cast<uint32_t>(mostSignificantBits) << 16 |
783 (static_cast<uint32_t>(leastSignificantBits) & 0xFFFF);
784 }
785
786 // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then,
787 // each component is converted to a normalized floating-point value to generate the returned two
788 // float values. The first float value will be extracted from the least significant bits of the
789 // input; the last float value will be extracted from the most-significant bits. The conversion for
790 // unpacked fixed-point value to floating point is done as follows: unpackSnorm2x16 : clamp(f /
791 // 32767.0, -1, +1)
unpackSnorm2x16(uint32_t u,float * f1,float * f2)792 inline void unpackSnorm2x16(uint32_t u, float *f1, float *f2)
793 {
794 int16_t leastSignificantBits = static_cast<int16_t>(u & 0xFFFF);
795 int16_t mostSignificantBits = static_cast<int16_t>(u >> 16);
796 *f1 = clamp(static_cast<float>(leastSignificantBits) / 32767.0f, -1.0f, 1.0f);
797 *f2 = clamp(static_cast<float>(mostSignificantBits) / 32767.0f, -1.0f, 1.0f);
798 }
799
800 // First, both normalized floating-point values are converted into 16-bit integer values.
801 // Then, the results are packed into the returned 32-bit unsigned integer.
802 // The first float value will be written to the least significant bits of the output;
803 // the last float value will be written to the most significant bits.
804 // The conversion of each value to fixed point is done as follows:
805 // packUnorm2x16 : round(clamp(c, 0, +1) * 65535.0)
packUnorm2x16(float f1,float f2)806 inline uint32_t packUnorm2x16(float f1, float f2)
807 {
808 uint16_t leastSignificantBits = static_cast<uint16_t>(roundf(clamp(f1, 0.0f, 1.0f) * 65535.0f));
809 uint16_t mostSignificantBits = static_cast<uint16_t>(roundf(clamp(f2, 0.0f, 1.0f) * 65535.0f));
810 return static_cast<uint32_t>(mostSignificantBits) << 16 |
811 static_cast<uint32_t>(leastSignificantBits);
812 }
813
814 // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then,
815 // each component is converted to a normalized floating-point value to generate the returned two
816 // float values. The first float value will be extracted from the least significant bits of the
817 // input; the last float value will be extracted from the most-significant bits. The conversion for
818 // unpacked fixed-point value to floating point is done as follows: unpackUnorm2x16 : f / 65535.0
unpackUnorm2x16(uint32_t u,float * f1,float * f2)819 inline void unpackUnorm2x16(uint32_t u, float *f1, float *f2)
820 {
821 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
822 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
823 *f1 = static_cast<float>(leastSignificantBits) / 65535.0f;
824 *f2 = static_cast<float>(mostSignificantBits) / 65535.0f;
825 }
826
827 // Helper functions intended to be used only here.
828 namespace priv
829 {
830
ToPackedUnorm8(float f)831 inline uint8_t ToPackedUnorm8(float f)
832 {
833 return static_cast<uint8_t>(roundf(clamp(f, 0.0f, 1.0f) * 255.0f));
834 }
835
ToPackedSnorm8(float f)836 inline int8_t ToPackedSnorm8(float f)
837 {
838 return static_cast<int8_t>(roundf(clamp(f, -1.0f, 1.0f) * 127.0f));
839 }
840
841 } // namespace priv
842
843 // Packs 4 normalized unsigned floating-point values to a single 32-bit unsigned integer. Works
844 // similarly to packUnorm2x16. The floats are clamped to the range 0.0 to 1.0, and written to the
845 // unsigned integer starting from the least significant bits.
PackUnorm4x8(float f1,float f2,float f3,float f4)846 inline uint32_t PackUnorm4x8(float f1, float f2, float f3, float f4)
847 {
848 uint8_t bits[4];
849 bits[0] = priv::ToPackedUnorm8(f1);
850 bits[1] = priv::ToPackedUnorm8(f2);
851 bits[2] = priv::ToPackedUnorm8(f3);
852 bits[3] = priv::ToPackedUnorm8(f4);
853 uint32_t result = 0u;
854 for (int i = 0; i < 4; ++i)
855 {
856 int shift = i * 8;
857 result |= (static_cast<uint32_t>(bits[i]) << shift);
858 }
859 return result;
860 }
861
862 // Unpacks 4 normalized unsigned floating-point values from a single 32-bit unsigned integer into f.
863 // Works similarly to unpackUnorm2x16. The floats are unpacked starting from the least significant
864 // bits.
UnpackUnorm4x8(uint32_t u,float * f)865 inline void UnpackUnorm4x8(uint32_t u, float *f)
866 {
867 for (int i = 0; i < 4; ++i)
868 {
869 int shift = i * 8;
870 uint8_t bits = static_cast<uint8_t>((u >> shift) & 0xFF);
871 f[i] = static_cast<float>(bits) / 255.0f;
872 }
873 }
874
875 // Packs 4 normalized signed floating-point values to a single 32-bit unsigned integer. The floats
876 // are clamped to the range -1.0 to 1.0, and written to the unsigned integer starting from the least
877 // significant bits.
PackSnorm4x8(float f1,float f2,float f3,float f4)878 inline uint32_t PackSnorm4x8(float f1, float f2, float f3, float f4)
879 {
880 int8_t bits[4];
881 bits[0] = priv::ToPackedSnorm8(f1);
882 bits[1] = priv::ToPackedSnorm8(f2);
883 bits[2] = priv::ToPackedSnorm8(f3);
884 bits[3] = priv::ToPackedSnorm8(f4);
885 uint32_t result = 0u;
886 for (int i = 0; i < 4; ++i)
887 {
888 int shift = i * 8;
889 result |= ((static_cast<uint32_t>(bits[i]) & 0xFF) << shift);
890 }
891 return result;
892 }
893
894 // Unpacks 4 normalized signed floating-point values from a single 32-bit unsigned integer into f.
895 // Works similarly to unpackSnorm2x16. The floats are unpacked starting from the least significant
896 // bits, and clamped to the range -1.0 to 1.0.
UnpackSnorm4x8(uint32_t u,float * f)897 inline void UnpackSnorm4x8(uint32_t u, float *f)
898 {
899 for (int i = 0; i < 4; ++i)
900 {
901 int shift = i * 8;
902 int8_t bits = static_cast<int8_t>((u >> shift) & 0xFF);
903 f[i] = clamp(static_cast<float>(bits) / 127.0f, -1.0f, 1.0f);
904 }
905 }
906
907 // Returns an unsigned integer obtained by converting the two floating-point values to the 16-bit
908 // floating-point representation found in the OpenGL ES Specification, and then packing these
909 // two 16-bit integers into a 32-bit unsigned integer.
910 // f1: The 16 least-significant bits of the result;
911 // f2: The 16 most-significant bits.
packHalf2x16(float f1,float f2)912 inline uint32_t packHalf2x16(float f1, float f2)
913 {
914 uint16_t leastSignificantBits = static_cast<uint16_t>(float32ToFloat16(f1));
915 uint16_t mostSignificantBits = static_cast<uint16_t>(float32ToFloat16(f2));
916 return static_cast<uint32_t>(mostSignificantBits) << 16 |
917 static_cast<uint32_t>(leastSignificantBits);
918 }
919
920 // Returns two floating-point values obtained by unpacking a 32-bit unsigned integer into a pair of
921 // 16-bit values, interpreting those values as 16-bit floating-point numbers according to the OpenGL
922 // ES Specification, and converting them to 32-bit floating-point values. The first float value is
923 // obtained from the 16 least-significant bits of u; the second component is obtained from the 16
924 // most-significant bits of u.
unpackHalf2x16(uint32_t u,float * f1,float * f2)925 inline void unpackHalf2x16(uint32_t u, float *f1, float *f2)
926 {
927 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
928 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
929
930 *f1 = float16ToFloat32(leastSignificantBits);
931 *f2 = float16ToFloat32(mostSignificantBits);
932 }
933
sRGBToLinear(uint8_t srgbValue)934 inline uint8_t sRGBToLinear(uint8_t srgbValue)
935 {
936 float value = srgbValue / 255.0f;
937 if (value <= 0.04045f)
938 {
939 value = value / 12.92f;
940 }
941 else
942 {
943 value = std::pow((value + 0.055f) / 1.055f, 2.4f);
944 }
945 return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f));
946 }
947
linearToSRGB(uint8_t linearValue)948 inline uint8_t linearToSRGB(uint8_t linearValue)
949 {
950 float value = linearValue / 255.0f;
951 if (value <= 0.0f)
952 {
953 value = 0.0f;
954 }
955 else if (value < 0.0031308f)
956 {
957 value = value * 12.92f;
958 }
959 else if (value < 1.0f)
960 {
961 value = std::pow(value, 0.41666f) * 1.055f - 0.055f;
962 }
963 else
964 {
965 value = 1.0f;
966 }
967 return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f));
968 }
969
970 // Reverse the order of the bits.
BitfieldReverse(uint32_t value)971 inline uint32_t BitfieldReverse(uint32_t value)
972 {
973 // TODO(oetuaho@nvidia.com): Optimize this if needed. There don't seem to be compiler intrinsics
974 // for this, and right now it's not used in performance-critical paths.
975 uint32_t result = 0u;
976 for (size_t j = 0u; j < 32u; ++j)
977 {
978 result |= (((value >> j) & 1u) << (31u - j));
979 }
980 return result;
981 }
982
983 // Count the 1 bits.
984 #if defined(_MSC_VER) && !defined(__clang__)
985 # if defined(_M_IX86) || defined(_M_X64)
986 namespace priv
987 {
988 // Check POPCNT instruction support and cache the result.
989 // https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64#remarks
990 static const bool kHasPopcnt = [] {
991 int info[4];
992 __cpuid(&info[0], 1);
993 return static_cast<bool>(info[2] & 0x800000);
994 }();
995 } // namespace priv
996
997 // Polyfills for x86/x64 CPUs without POPCNT.
998 // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
BitCountPolyfill(uint32_t bits)999 inline int BitCountPolyfill(uint32_t bits)
1000 {
1001 bits = bits - ((bits >> 1) & 0x55555555);
1002 bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
1003 bits = ((bits + (bits >> 4) & 0x0F0F0F0F) * 0x01010101) >> 24;
1004 return static_cast<int>(bits);
1005 }
1006
BitCountPolyfill(uint64_t bits)1007 inline int BitCountPolyfill(uint64_t bits)
1008 {
1009 bits = bits - ((bits >> 1) & 0x5555555555555555ull);
1010 bits = (bits & 0x3333333333333333ull) + ((bits >> 2) & 0x3333333333333333ull);
1011 bits = ((bits + (bits >> 4) & 0x0F0F0F0F0F0F0F0Full) * 0x0101010101010101ull) >> 56;
1012 return static_cast<int>(bits);
1013 }
1014
BitCount(uint32_t bits)1015 inline int BitCount(uint32_t bits)
1016 {
1017 if (priv::kHasPopcnt)
1018 {
1019 return static_cast<int>(__popcnt(bits));
1020 }
1021 return BitCountPolyfill(bits);
1022 }
1023
BitCount(uint64_t bits)1024 inline int BitCount(uint64_t bits)
1025 {
1026 if (priv::kHasPopcnt)
1027 {
1028 # if defined(_M_X64)
1029 return static_cast<int>(__popcnt64(bits));
1030 # else // x86
1031 return static_cast<int>(__popcnt(static_cast<uint32_t>(bits >> 32)) +
1032 __popcnt(static_cast<uint32_t>(bits)));
1033 # endif // defined(_M_X64)
1034 }
1035 return BitCountPolyfill(bits);
1036 }
1037
1038 # elif defined(_M_ARM) || defined(_M_ARM64)
1039
1040 // MSVC's _CountOneBits* intrinsics are not defined for ARM64, moreover they do not use dedicated
1041 // NEON instructions.
1042
BitCount(uint32_t bits)1043 inline int BitCount(uint32_t bits)
1044 {
1045 // cast bits to 8x8 datatype and use VCNT on it
1046 const uint8x8_t vsum = vcnt_u8(vcreate_u8(static_cast<uint64_t>(bits)));
1047
1048 // pairwise sums: 8x8 -> 16x4 -> 32x2
1049 return static_cast<int>(vget_lane_u32(vpaddl_u16(vpaddl_u8(vsum)), 0));
1050 }
1051
BitCount(uint64_t bits)1052 inline int BitCount(uint64_t bits)
1053 {
1054 // cast bits to 8x8 datatype and use VCNT on it
1055 const uint8x8_t vsum = vcnt_u8(vcreate_u8(bits));
1056
1057 // pairwise sums: 8x8 -> 16x4 -> 32x2 -> 64x1
1058 return static_cast<int>(vget_lane_u64(vpaddl_u32(vpaddl_u16(vpaddl_u8(vsum))), 0));
1059 }
1060 # endif // defined(_M_IX86) || defined(_M_X64)
1061 #endif // defined(_MSC_VER) && !defined(__clang__)
1062
1063 #if defined(ANGLE_PLATFORM_POSIX) || defined(__clang__)
BitCount(uint32_t bits)1064 inline int BitCount(uint32_t bits)
1065 {
1066 return __builtin_popcount(bits);
1067 }
1068
BitCount(uint64_t bits)1069 inline int BitCount(uint64_t bits)
1070 {
1071 return __builtin_popcountll(bits);
1072 }
1073 #endif // defined(ANGLE_PLATFORM_POSIX) || defined(__clang__)
1074
BitCount(uint8_t bits)1075 inline int BitCount(uint8_t bits)
1076 {
1077 return BitCount(static_cast<uint32_t>(bits));
1078 }
1079
BitCount(uint16_t bits)1080 inline int BitCount(uint16_t bits)
1081 {
1082 return BitCount(static_cast<uint32_t>(bits));
1083 }
1084
1085 #if defined(ANGLE_PLATFORM_WINDOWS)
1086 // Return the index of the least significant bit set. Indexing is such that bit 0 is the least
1087 // significant bit. Implemented for different bit widths on different platforms.
ScanForward(uint32_t bits)1088 inline unsigned long ScanForward(uint32_t bits)
1089 {
1090 ASSERT(bits != 0u);
1091 unsigned long firstBitIndex = 0ul;
1092 unsigned char ret = _BitScanForward(&firstBitIndex, bits);
1093 ASSERT(ret != 0u);
1094 return firstBitIndex;
1095 }
1096
ScanForward(uint64_t bits)1097 inline unsigned long ScanForward(uint64_t bits)
1098 {
1099 ASSERT(bits != 0u);
1100 unsigned long firstBitIndex = 0ul;
1101 # if defined(ANGLE_IS_64_BIT_CPU)
1102 unsigned char ret = _BitScanForward64(&firstBitIndex, bits);
1103 # else
1104 unsigned char ret;
1105 if (static_cast<uint32_t>(bits) == 0)
1106 {
1107 ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits >> 32));
1108 firstBitIndex += 32ul;
1109 }
1110 else
1111 {
1112 ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits));
1113 }
1114 # endif // defined(ANGLE_IS_64_BIT_CPU)
1115 ASSERT(ret != 0u);
1116 return firstBitIndex;
1117 }
1118 #endif // defined(ANGLE_PLATFORM_WINDOWS)
1119
1120 #if defined(ANGLE_PLATFORM_POSIX)
ScanForward(uint32_t bits)1121 inline unsigned long ScanForward(uint32_t bits)
1122 {
1123 ASSERT(bits != 0u);
1124 return static_cast<unsigned long>(__builtin_ctz(bits));
1125 }
1126
ScanForward(uint64_t bits)1127 inline unsigned long ScanForward(uint64_t bits)
1128 {
1129 ASSERT(bits != 0u);
1130 # if defined(ANGLE_IS_64_BIT_CPU)
1131 return static_cast<unsigned long>(__builtin_ctzll(bits));
1132 # else
1133 return static_cast<unsigned long>(static_cast<uint32_t>(bits) == 0
1134 ? __builtin_ctz(static_cast<uint32_t>(bits >> 32)) + 32
1135 : __builtin_ctz(static_cast<uint32_t>(bits)));
1136 # endif // defined(ANGLE_IS_64_BIT_CPU)
1137 }
1138 #endif // defined(ANGLE_PLATFORM_POSIX)
1139
ScanForward(uint8_t bits)1140 inline unsigned long ScanForward(uint8_t bits)
1141 {
1142 return ScanForward(static_cast<uint32_t>(bits));
1143 }
1144
ScanForward(uint16_t bits)1145 inline unsigned long ScanForward(uint16_t bits)
1146 {
1147 return ScanForward(static_cast<uint32_t>(bits));
1148 }
1149
1150 // Return the index of the most significant bit set. Indexing is such that bit 0 is the least
1151 // significant bit.
ScanReverse(unsigned long bits)1152 inline unsigned long ScanReverse(unsigned long bits)
1153 {
1154 ASSERT(bits != 0u);
1155 #if defined(ANGLE_PLATFORM_WINDOWS)
1156 unsigned long lastBitIndex = 0ul;
1157 unsigned char ret = _BitScanReverse(&lastBitIndex, bits);
1158 ASSERT(ret != 0u);
1159 return lastBitIndex;
1160 #elif defined(ANGLE_PLATFORM_POSIX)
1161 return static_cast<unsigned long>(sizeof(unsigned long) * CHAR_BIT - 1 - __builtin_clzl(bits));
1162 #else
1163 # error Please implement bit-scan-reverse for your platform!
1164 #endif
1165 }
1166
1167 // Returns -1 on 0, otherwise the index of the least significant 1 bit as in GLSL.
1168 template <typename T>
FindLSB(T bits)1169 int FindLSB(T bits)
1170 {
1171 static_assert(std::is_integral<T>::value, "must be integral type.");
1172 if (bits == 0u)
1173 {
1174 return -1;
1175 }
1176 else
1177 {
1178 return static_cast<int>(ScanForward(bits));
1179 }
1180 }
1181
1182 // Returns -1 on 0, otherwise the index of the most significant 1 bit as in GLSL.
1183 template <typename T>
FindMSB(T bits)1184 int FindMSB(T bits)
1185 {
1186 static_assert(std::is_integral<T>::value, "must be integral type.");
1187 if (bits == 0u)
1188 {
1189 return -1;
1190 }
1191 else
1192 {
1193 return static_cast<int>(ScanReverse(bits));
1194 }
1195 }
1196
1197 // Returns whether the argument is Not a Number.
1198 // IEEE 754 single precision NaN representation: Exponent(8 bits) - 255, Mantissa(23 bits) -
1199 // non-zero.
isNaN(float f)1200 inline bool isNaN(float f)
1201 {
1202 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
1203 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
1204 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) &&
1205 (bitCast<uint32_t>(f) & 0x7fffffu);
1206 }
1207
1208 // Returns whether the argument is infinity.
1209 // IEEE 754 single precision infinity representation: Exponent(8 bits) - 255, Mantissa(23 bits) -
1210 // zero.
isInf(float f)1211 inline bool isInf(float f)
1212 {
1213 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
1214 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
1215 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) &&
1216 !(bitCast<uint32_t>(f) & 0x7fffffu);
1217 }
1218
1219 namespace priv
1220 {
1221 template <unsigned int N, unsigned int R>
1222 struct iSquareRoot
1223 {
solveiSquareRoot1224 static constexpr unsigned int solve()
1225 {
1226 return (R * R > N)
1227 ? 0
1228 : ((R * R == N) ? R : static_cast<unsigned int>(iSquareRoot<N, R + 1>::value));
1229 }
1230 enum Result
1231 {
1232 value = iSquareRoot::solve()
1233 };
1234 };
1235
1236 template <unsigned int N>
1237 struct iSquareRoot<N, N>
1238 {
1239 enum result
1240 {
1241 value = N
1242 };
1243 };
1244
1245 } // namespace priv
1246
1247 template <unsigned int N>
1248 constexpr unsigned int iSquareRoot()
1249 {
1250 return priv::iSquareRoot<N, 1>::value;
1251 }
1252
1253 // Sum, difference and multiplication operations for signed ints that wrap on 32-bit overflow.
1254 //
1255 // Unsigned types are defined to do arithmetic modulo 2^n in C++. For signed types, overflow
1256 // behavior is undefined.
1257
1258 template <typename T>
1259 inline T WrappingSum(T lhs, T rhs)
1260 {
1261 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
1262 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
1263 return static_cast<T>(lhsUnsigned + rhsUnsigned);
1264 }
1265
1266 template <typename T>
1267 inline T WrappingDiff(T lhs, T rhs)
1268 {
1269 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
1270 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
1271 return static_cast<T>(lhsUnsigned - rhsUnsigned);
1272 }
1273
1274 inline int32_t WrappingMul(int32_t lhs, int32_t rhs)
1275 {
1276 int64_t lhsWide = static_cast<int64_t>(lhs);
1277 int64_t rhsWide = static_cast<int64_t>(rhs);
1278 // The multiplication is guaranteed not to overflow.
1279 int64_t resultWide = lhsWide * rhsWide;
1280 // Implement the desired wrapping behavior by masking out the high-order 32 bits.
1281 resultWide = resultWide & 0xffffffffll;
1282 // Casting to a narrower signed type is fine since the casted value is representable in the
1283 // narrower type.
1284 return static_cast<int32_t>(resultWide);
1285 }
1286
1287 inline float scaleScreenDimensionToNdc(float dimensionScreen, float viewportDimension)
1288 {
1289 return 2.0f * dimensionScreen / viewportDimension;
1290 }
1291
1292 inline float scaleScreenCoordinateToNdc(float coordinateScreen, float viewportDimension)
1293 {
1294 float halfShifted = coordinateScreen / viewportDimension;
1295 return 2.0f * (halfShifted - 0.5f);
1296 }
1297
1298 } // namespace gl
1299
1300 namespace rx
1301 {
1302
1303 template <typename T>
1304 T roundUp(const T value, const T alignment)
1305 {
1306 auto temp = value + alignment - static_cast<T>(1);
1307 return temp - temp % alignment;
1308 }
1309
1310 template <typename T>
1311 constexpr T roundUpPow2(const T value, const T alignment)
1312 {
1313 ASSERT(gl::isPow2(alignment));
1314 return (value + alignment - 1) & ~(alignment - 1);
1315 }
1316
1317 template <typename T>
1318 angle::CheckedNumeric<T> CheckedRoundUp(const T value, const T alignment)
1319 {
1320 angle::CheckedNumeric<T> checkedValue(value);
1321 angle::CheckedNumeric<T> checkedAlignment(alignment);
1322 return roundUp(checkedValue, checkedAlignment);
1323 }
1324
1325 inline constexpr unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor)
1326 {
1327 unsigned int divided = value / divisor;
1328 return (divided + ((value % divisor == 0) ? 0 : 1));
1329 }
1330
1331 #if defined(__has_builtin)
1332 # define ANGLE_HAS_BUILTIN(x) __has_builtin(x)
1333 #else
1334 # define ANGLE_HAS_BUILTIN(x) 0
1335 #endif
1336
1337 #if defined(_MSC_VER)
1338
1339 # define ANGLE_ROTL(x, y) _rotl(x, y)
1340 # define ANGLE_ROTL64(x, y) _rotl64(x, y)
1341 # define ANGLE_ROTR16(x, y) _rotr16(x, y)
1342
1343 #elif defined(__clang__) && ANGLE_HAS_BUILTIN(__builtin_rotateleft32) && \
1344 ANGLE_HAS_BUILTIN(__builtin_rotateleft64) && ANGLE_HAS_BUILTIN(__builtin_rotateright16)
1345
1346 # define ANGLE_ROTL(x, y) __builtin_rotateleft32(x, y)
1347 # define ANGLE_ROTL64(x, y) __builtin_rotateleft64(x, y)
1348 # define ANGLE_ROTR16(x, y) __builtin_rotateright16(x, y)
1349
1350 #else
1351
1352 inline uint32_t RotL(uint32_t x, int8_t r)
1353 {
1354 return (x << r) | (x >> (32 - r));
1355 }
1356
1357 inline uint64_t RotL64(uint64_t x, int8_t r)
1358 {
1359 return (x << r) | (x >> (64 - r));
1360 }
1361
1362 inline uint16_t RotR16(uint16_t x, int8_t r)
1363 {
1364 return (x >> r) | (x << (16 - r));
1365 }
1366
1367 # define ANGLE_ROTL(x, y) ::rx::RotL(x, y)
1368 # define ANGLE_ROTL64(x, y) ::rx::RotL64(x, y)
1369 # define ANGLE_ROTR16(x, y) ::rx::RotR16(x, y)
1370
1371 #endif // namespace rx
1372
1373 constexpr unsigned int Log2(unsigned int bytes)
1374 {
1375 return bytes == 1 ? 0 : (1 + Log2(bytes / 2));
1376 }
1377 } // namespace rx
1378
1379 #endif // COMMON_MATHUTIL_H_
1380