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