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 (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
791 struct IndexRange
792 {
793 struct Undefined
794 {};
IndexRangeIndexRange795 IndexRange(Undefined) {}
IndexRangeIndexRange796 IndexRange() : IndexRange(0, 0, 0) {}
IndexRangeIndexRange797 IndexRange(size_t start_, size_t end_, size_t vertexIndexCount_)
798 : start(start_), end(end_), vertexIndexCount(vertexIndexCount_)
799 {
800 ASSERT(start <= end);
801 }
802
803 // Number of vertices in the range.
vertexCountIndexRange804 size_t vertexCount() const { return (end - start) + 1; }
805
806 // Inclusive range of indices that are not primitive restart
807 size_t start;
808 size_t end;
809
810 // Number of non-primitive restart indices
811 size_t vertexIndexCount;
812 };
813
814 // Combine a floating-point value representing a mantissa (x) and an integer exponent (exp) into a
815 // floating-point value. As in GLSL ldexp() built-in.
Ldexp(float x,int exp)816 inline float Ldexp(float x, int exp)
817 {
818 if (exp > 128)
819 {
820 return std::numeric_limits<float>::infinity();
821 }
822 if (exp < -126)
823 {
824 return 0.0f;
825 }
826 double result = static_cast<double>(x) * std::pow(2.0, static_cast<double>(exp));
827 return static_cast<float>(result);
828 }
829
830 // First, both normalized floating-point values are converted into 16-bit integer values.
831 // Then, the results are packed into the returned 32-bit unsigned integer.
832 // The first float value will be written to the least significant bits of the output;
833 // the last float value will be written to the most significant bits.
834 // The conversion of each value to fixed point is done as follows :
835 // packSnorm2x16 : round(clamp(c, -1, +1) * 32767.0)
packSnorm2x16(float f1,float f2)836 inline uint32_t packSnorm2x16(float f1, float f2)
837 {
838 int16_t leastSignificantBits = static_cast<int16_t>(roundf(clamp(f1, -1.0f, 1.0f) * 32767.0f));
839 int16_t mostSignificantBits = static_cast<int16_t>(roundf(clamp(f2, -1.0f, 1.0f) * 32767.0f));
840 return static_cast<uint32_t>(mostSignificantBits) << 16 |
841 (static_cast<uint32_t>(leastSignificantBits) & 0xFFFF);
842 }
843
844 // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then,
845 // each component is converted to a normalized floating-point value to generate the returned two
846 // float values. The first float value will be extracted from the least significant bits of the
847 // input; the last float value will be extracted from the most-significant bits. The conversion for
848 // unpacked fixed-point value to floating point is done as follows: unpackSnorm2x16 : clamp(f /
849 // 32767.0, -1, +1)
unpackSnorm2x16(uint32_t u,float * f1,float * f2)850 inline void unpackSnorm2x16(uint32_t u, float *f1, float *f2)
851 {
852 int16_t leastSignificantBits = static_cast<int16_t>(u & 0xFFFF);
853 int16_t mostSignificantBits = static_cast<int16_t>(u >> 16);
854 *f1 = clamp(static_cast<float>(leastSignificantBits) / 32767.0f, -1.0f, 1.0f);
855 *f2 = clamp(static_cast<float>(mostSignificantBits) / 32767.0f, -1.0f, 1.0f);
856 }
857
858 // First, both normalized floating-point values are converted into 16-bit integer values.
859 // Then, the results are packed into the returned 32-bit unsigned integer.
860 // The first float value will be written to the least significant bits of the output;
861 // the last float value will be written to the most significant bits.
862 // The conversion of each value to fixed point is done as follows:
863 // packUnorm2x16 : round(clamp(c, 0, +1) * 65535.0)
packUnorm2x16(float f1,float f2)864 inline uint32_t packUnorm2x16(float f1, float f2)
865 {
866 uint16_t leastSignificantBits = static_cast<uint16_t>(roundf(clamp(f1, 0.0f, 1.0f) * 65535.0f));
867 uint16_t mostSignificantBits = static_cast<uint16_t>(roundf(clamp(f2, 0.0f, 1.0f) * 65535.0f));
868 return static_cast<uint32_t>(mostSignificantBits) << 16 |
869 static_cast<uint32_t>(leastSignificantBits);
870 }
871
872 // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then,
873 // each component is converted to a normalized floating-point value to generate the returned two
874 // float values. The first float value will be extracted from the least significant bits of the
875 // input; the last float value will be extracted from the most-significant bits. The conversion for
876 // unpacked fixed-point value to floating point is done as follows: unpackUnorm2x16 : f / 65535.0
unpackUnorm2x16(uint32_t u,float * f1,float * f2)877 inline void unpackUnorm2x16(uint32_t u, float *f1, float *f2)
878 {
879 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
880 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
881 *f1 = static_cast<float>(leastSignificantBits) / 65535.0f;
882 *f2 = static_cast<float>(mostSignificantBits) / 65535.0f;
883 }
884
885 // Helper functions intended to be used only here.
886 namespace priv
887 {
888
ToPackedUnorm8(float f)889 inline uint8_t ToPackedUnorm8(float f)
890 {
891 return static_cast<uint8_t>(roundf(clamp(f, 0.0f, 1.0f) * 255.0f));
892 }
893
ToPackedSnorm8(float f)894 inline int8_t ToPackedSnorm8(float f)
895 {
896 return static_cast<int8_t>(roundf(clamp(f, -1.0f, 1.0f) * 127.0f));
897 }
898
899 } // namespace priv
900
901 // Packs 4 normalized unsigned floating-point values to a single 32-bit unsigned integer. Works
902 // similarly to packUnorm2x16. The floats are clamped to the range 0.0 to 1.0, and written to the
903 // unsigned integer starting from the least significant bits.
PackUnorm4x8(float f1,float f2,float f3,float f4)904 inline uint32_t PackUnorm4x8(float f1, float f2, float f3, float f4)
905 {
906 uint8_t bits[4];
907 bits[0] = priv::ToPackedUnorm8(f1);
908 bits[1] = priv::ToPackedUnorm8(f2);
909 bits[2] = priv::ToPackedUnorm8(f3);
910 bits[3] = priv::ToPackedUnorm8(f4);
911 uint32_t result = 0u;
912 for (int i = 0; i < 4; ++i)
913 {
914 int shift = i * 8;
915 result |= (static_cast<uint32_t>(bits[i]) << shift);
916 }
917 return result;
918 }
919
920 // Unpacks 4 normalized unsigned floating-point values from a single 32-bit unsigned integer into f.
921 // Works similarly to unpackUnorm2x16. The floats are unpacked starting from the least significant
922 // bits.
UnpackUnorm4x8(uint32_t u,float * f)923 inline void UnpackUnorm4x8(uint32_t u, float *f)
924 {
925 for (int i = 0; i < 4; ++i)
926 {
927 int shift = i * 8;
928 uint8_t bits = static_cast<uint8_t>((u >> shift) & 0xFF);
929 f[i] = static_cast<float>(bits) / 255.0f;
930 }
931 }
932
933 // Packs 4 normalized signed floating-point values to a single 32-bit unsigned integer. The floats
934 // are clamped to the range -1.0 to 1.0, and written to the unsigned integer starting from the least
935 // significant bits.
PackSnorm4x8(float f1,float f2,float f3,float f4)936 inline uint32_t PackSnorm4x8(float f1, float f2, float f3, float f4)
937 {
938 int8_t bits[4];
939 bits[0] = priv::ToPackedSnorm8(f1);
940 bits[1] = priv::ToPackedSnorm8(f2);
941 bits[2] = priv::ToPackedSnorm8(f3);
942 bits[3] = priv::ToPackedSnorm8(f4);
943 uint32_t result = 0u;
944 for (int i = 0; i < 4; ++i)
945 {
946 int shift = i * 8;
947 result |= ((static_cast<uint32_t>(bits[i]) & 0xFF) << shift);
948 }
949 return result;
950 }
951
952 // Unpacks 4 normalized signed floating-point values from a single 32-bit unsigned integer into f.
953 // Works similarly to unpackSnorm2x16. The floats are unpacked starting from the least significant
954 // bits, and clamped to the range -1.0 to 1.0.
UnpackSnorm4x8(uint32_t u,float * f)955 inline void UnpackSnorm4x8(uint32_t u, float *f)
956 {
957 for (int i = 0; i < 4; ++i)
958 {
959 int shift = i * 8;
960 int8_t bits = static_cast<int8_t>((u >> shift) & 0xFF);
961 f[i] = clamp(static_cast<float>(bits) / 127.0f, -1.0f, 1.0f);
962 }
963 }
964
965 // Returns an unsigned integer obtained by converting the two floating-point values to the 16-bit
966 // floating-point representation found in the OpenGL ES Specification, and then packing these
967 // two 16-bit integers into a 32-bit unsigned integer.
968 // f1: The 16 least-significant bits of the result;
969 // f2: The 16 most-significant bits.
packHalf2x16(float f1,float f2)970 inline uint32_t packHalf2x16(float f1, float f2)
971 {
972 uint16_t leastSignificantBits = static_cast<uint16_t>(float32ToFloat16(f1));
973 uint16_t mostSignificantBits = static_cast<uint16_t>(float32ToFloat16(f2));
974 return static_cast<uint32_t>(mostSignificantBits) << 16 |
975 static_cast<uint32_t>(leastSignificantBits);
976 }
977
978 // Returns two floating-point values obtained by unpacking a 32-bit unsigned integer into a pair of
979 // 16-bit values, interpreting those values as 16-bit floating-point numbers according to the OpenGL
980 // ES Specification, and converting them to 32-bit floating-point values. The first float value is
981 // obtained from the 16 least-significant bits of u; the second component is obtained from the 16
982 // most-significant bits of u.
unpackHalf2x16(uint32_t u,float * f1,float * f2)983 inline void unpackHalf2x16(uint32_t u, float *f1, float *f2)
984 {
985 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
986 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
987
988 *f1 = float16ToFloat32(leastSignificantBits);
989 *f2 = float16ToFloat32(mostSignificantBits);
990 }
991
sRGBToLinear(uint8_t srgbValue)992 inline float sRGBToLinear(uint8_t srgbValue)
993 {
994 float value = srgbValue / 255.0f;
995 if (value <= 0.04045f)
996 {
997 value = value / 12.92f;
998 }
999 else
1000 {
1001 value = std::pow((value + 0.055f) / 1.055f, 2.4f);
1002 }
1003 ASSERT(value >= 0.0f && value <= 1.0f);
1004 return value;
1005 }
1006
linearToSRGB(float value)1007 inline uint8_t linearToSRGB(float value)
1008 {
1009 ASSERT(value >= 0.0f && value <= 1.0f);
1010 if (value < 0.0031308f)
1011 {
1012 value = value * 12.92f;
1013 }
1014 else
1015 {
1016 value = std::pow(value, 0.41666f) * 1.055f - 0.055f;
1017 }
1018 return static_cast<uint8_t>(value * 255.0f + 0.5f);
1019 }
1020
1021 // Reverse the order of the bits.
BitfieldReverse(uint32_t value)1022 inline uint32_t BitfieldReverse(uint32_t value)
1023 {
1024 // TODO(oetuaho@nvidia.com): Optimize this if needed. There don't seem to be compiler intrinsics
1025 // for this, and right now it's not used in performance-critical paths.
1026 uint32_t result = 0u;
1027 for (size_t j = 0u; j < 32u; ++j)
1028 {
1029 result |= (((value >> j) & 1u) << (31u - j));
1030 }
1031 return result;
1032 }
1033
1034 // Count the 1 bits.
1035 #if defined(_MSC_VER) && !defined(__clang__)
1036 # if defined(_M_IX86) || defined(_M_X64)
1037 namespace priv
1038 {
1039 // Check POPCNT instruction support and cache the result.
1040 // https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64#remarks
1041 static const bool kHasPopcnt = [] {
1042 int info[4];
1043 __cpuid(&info[0], 1);
1044 return static_cast<bool>(info[2] & 0x800000);
1045 }();
1046 } // namespace priv
1047
1048 // Polyfills for x86/x64 CPUs without POPCNT.
1049 // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
BitCountPolyfill(uint32_t bits)1050 inline int BitCountPolyfill(uint32_t bits)
1051 {
1052 bits = bits - ((bits >> 1) & 0x55555555);
1053 bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
1054 bits = ((bits + (bits >> 4) & 0x0F0F0F0F) * 0x01010101) >> 24;
1055 return static_cast<int>(bits);
1056 }
1057
BitCountPolyfill(uint64_t bits)1058 inline int BitCountPolyfill(uint64_t bits)
1059 {
1060 bits = bits - ((bits >> 1) & 0x5555555555555555ull);
1061 bits = (bits & 0x3333333333333333ull) + ((bits >> 2) & 0x3333333333333333ull);
1062 bits = ((bits + (bits >> 4) & 0x0F0F0F0F0F0F0F0Full) * 0x0101010101010101ull) >> 56;
1063 return static_cast<int>(bits);
1064 }
1065
BitCount(uint32_t bits)1066 inline int BitCount(uint32_t bits)
1067 {
1068 if (priv::kHasPopcnt)
1069 {
1070 return static_cast<int>(__popcnt(bits));
1071 }
1072 return BitCountPolyfill(bits);
1073 }
1074
BitCount(uint64_t bits)1075 inline int BitCount(uint64_t bits)
1076 {
1077 if (priv::kHasPopcnt)
1078 {
1079 # if defined(_M_X64)
1080 return static_cast<int>(__popcnt64(bits));
1081 # else // x86
1082 return static_cast<int>(__popcnt(static_cast<uint32_t>(bits >> 32)) +
1083 __popcnt(static_cast<uint32_t>(bits)));
1084 # endif // defined(_M_X64)
1085 }
1086 return BitCountPolyfill(bits);
1087 }
1088
1089 # elif defined(_M_ARM) || defined(_M_ARM64)
1090
1091 // MSVC's _CountOneBits* intrinsics are not defined for ARM64, moreover they do not use dedicated
1092 // NEON instructions.
1093
BitCount(uint32_t bits)1094 inline int BitCount(uint32_t bits)
1095 {
1096 // cast bits to 8x8 datatype and use VCNT on it
1097 const uint8x8_t vsum = vcnt_u8(vcreate_u8(static_cast<uint64_t>(bits)));
1098
1099 // pairwise sums: 8x8 -> 16x4 -> 32x2
1100 return static_cast<int>(vget_lane_u32(vpaddl_u16(vpaddl_u8(vsum)), 0));
1101 }
1102
BitCount(uint64_t bits)1103 inline int BitCount(uint64_t bits)
1104 {
1105 // cast bits to 8x8 datatype and use VCNT on it
1106 const uint8x8_t vsum = vcnt_u8(vcreate_u8(bits));
1107
1108 // pairwise sums: 8x8 -> 16x4 -> 32x2 -> 64x1
1109 return static_cast<int>(vget_lane_u64(vpaddl_u32(vpaddl_u16(vpaddl_u8(vsum))), 0));
1110 }
1111 # endif // defined(_M_IX86) || defined(_M_X64)
1112 #endif // defined(_MSC_VER) && !defined(__clang__)
1113
1114 #if defined(ANGLE_PLATFORM_POSIX) || defined(__clang__) || defined(__GNUC__)
BitCount(uint32_t bits)1115 inline int BitCount(uint32_t bits)
1116 {
1117 return __builtin_popcount(bits);
1118 }
1119
BitCount(uint64_t bits)1120 inline int BitCount(uint64_t bits)
1121 {
1122 return __builtin_popcountll(bits);
1123 }
1124 #endif // defined(ANGLE_PLATFORM_POSIX) || defined(__clang__) || defined(__GNUC__)
1125
BitCount(uint8_t bits)1126 inline int BitCount(uint8_t bits)
1127 {
1128 return BitCount(static_cast<uint32_t>(bits));
1129 }
1130
BitCount(uint16_t bits)1131 inline int BitCount(uint16_t bits)
1132 {
1133 return BitCount(static_cast<uint32_t>(bits));
1134 }
1135
1136 #if defined(ANGLE_PLATFORM_WINDOWS)
1137 // Return the index of the least significant bit set. Indexing is such that bit 0 is the least
1138 // significant bit. Implemented for different bit widths on different platforms.
ScanForward(uint32_t bits)1139 inline unsigned long ScanForward(uint32_t bits)
1140 {
1141 ASSERT(bits != 0u);
1142 unsigned long firstBitIndex = 0ul;
1143 unsigned char ret = _BitScanForward(&firstBitIndex, bits);
1144 ASSERT(ret != 0u);
1145 return firstBitIndex;
1146 }
1147
ScanForward(uint64_t bits)1148 inline unsigned long ScanForward(uint64_t bits)
1149 {
1150 ASSERT(bits != 0u);
1151 unsigned long firstBitIndex = 0ul;
1152 # if defined(ANGLE_IS_64_BIT_CPU)
1153 unsigned char ret = _BitScanForward64(&firstBitIndex, bits);
1154 # else
1155 unsigned char ret;
1156 if (static_cast<uint32_t>(bits) == 0)
1157 {
1158 ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits >> 32));
1159 firstBitIndex += 32ul;
1160 }
1161 else
1162 {
1163 ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits));
1164 }
1165 # endif // defined(ANGLE_IS_64_BIT_CPU)
1166 ASSERT(ret != 0u);
1167 return firstBitIndex;
1168 }
1169
1170 // Return the index of the most significant bit set. Indexing is such that bit 0 is the least
1171 // significant bit.
ScanReverse(uint32_t bits)1172 inline unsigned long ScanReverse(uint32_t bits)
1173 {
1174 ASSERT(bits != 0u);
1175 unsigned long lastBitIndex = 0ul;
1176 unsigned char ret = _BitScanReverse(&lastBitIndex, bits);
1177 ASSERT(ret != 0u);
1178 return lastBitIndex;
1179 }
1180
ScanReverse(uint64_t bits)1181 inline unsigned long ScanReverse(uint64_t bits)
1182 {
1183 ASSERT(bits != 0u);
1184 unsigned long lastBitIndex = 0ul;
1185 # if defined(ANGLE_IS_64_BIT_CPU)
1186 unsigned char ret = _BitScanReverse64(&lastBitIndex, bits);
1187 # else
1188 unsigned char ret;
1189 if (static_cast<uint32_t>(bits >> 32) == 0)
1190 {
1191 ret = _BitScanReverse(&lastBitIndex, static_cast<uint32_t>(bits));
1192 }
1193 else
1194 {
1195 ret = _BitScanReverse(&lastBitIndex, static_cast<uint32_t>(bits >> 32));
1196 lastBitIndex += 32ul;
1197 }
1198 # endif // defined(ANGLE_IS_64_BIT_CPU)
1199 ASSERT(ret != 0u);
1200 return lastBitIndex;
1201 }
1202 #endif // defined(ANGLE_PLATFORM_WINDOWS)
1203
1204 #if defined(ANGLE_PLATFORM_POSIX)
ScanForward(uint32_t bits)1205 inline unsigned long ScanForward(uint32_t bits)
1206 {
1207 ASSERT(bits != 0u);
1208 return static_cast<unsigned long>(__builtin_ctz(bits));
1209 }
1210
ScanForward(uint64_t bits)1211 inline unsigned long ScanForward(uint64_t bits)
1212 {
1213 ASSERT(bits != 0u);
1214 # if defined(ANGLE_IS_64_BIT_CPU)
1215 return static_cast<unsigned long>(__builtin_ctzll(bits));
1216 # else
1217 return static_cast<unsigned long>(static_cast<uint32_t>(bits) == 0
1218 ? __builtin_ctz(static_cast<uint32_t>(bits >> 32)) + 32
1219 : __builtin_ctz(static_cast<uint32_t>(bits)));
1220 # endif // defined(ANGLE_IS_64_BIT_CPU)
1221 }
1222
ScanReverse(uint32_t bits)1223 inline unsigned long ScanReverse(uint32_t bits)
1224 {
1225 ASSERT(bits != 0u);
1226 return static_cast<unsigned long>(sizeof(uint32_t) * CHAR_BIT - 1 - __builtin_clz(bits));
1227 }
1228
ScanReverse(uint64_t bits)1229 inline unsigned long ScanReverse(uint64_t bits)
1230 {
1231 ASSERT(bits != 0u);
1232 # if defined(ANGLE_IS_64_BIT_CPU)
1233 return static_cast<unsigned long>(sizeof(uint64_t) * CHAR_BIT - 1 - __builtin_clzll(bits));
1234 # else
1235 if (static_cast<uint32_t>(bits >> 32) == 0)
1236 {
1237 return sizeof(uint32_t) * CHAR_BIT - 1 - __builtin_clz(static_cast<uint32_t>(bits));
1238 }
1239 else
1240 {
1241 return sizeof(uint32_t) * CHAR_BIT - 1 - __builtin_clz(static_cast<uint32_t>(bits >> 32)) +
1242 32;
1243 }
1244 # endif // defined(ANGLE_IS_64_BIT_CPU)
1245 }
1246 #endif // defined(ANGLE_PLATFORM_POSIX)
1247
ScanForward(uint8_t bits)1248 inline unsigned long ScanForward(uint8_t bits)
1249 {
1250 return ScanForward(static_cast<uint32_t>(bits));
1251 }
1252
ScanForward(uint16_t bits)1253 inline unsigned long ScanForward(uint16_t bits)
1254 {
1255 return ScanForward(static_cast<uint32_t>(bits));
1256 }
1257
ScanReverse(uint8_t bits)1258 inline unsigned long ScanReverse(uint8_t bits)
1259 {
1260 return ScanReverse(static_cast<uint32_t>(bits));
1261 }
1262
ScanReverse(uint16_t bits)1263 inline unsigned long ScanReverse(uint16_t bits)
1264 {
1265 return ScanReverse(static_cast<uint32_t>(bits));
1266 }
1267
1268 // Returns -1 on 0, otherwise the index of the least significant 1 bit as in GLSL.
1269 template <typename T>
FindLSB(T bits)1270 int FindLSB(T bits)
1271 {
1272 static_assert(std::is_integral<T>::value, "must be integral type.");
1273 if (bits == 0u)
1274 {
1275 return -1;
1276 }
1277 else
1278 {
1279 return static_cast<int>(ScanForward(bits));
1280 }
1281 }
1282
1283 // Returns -1 on 0, otherwise the index of the most significant 1 bit as in GLSL.
1284 template <typename T>
FindMSB(T bits)1285 int FindMSB(T bits)
1286 {
1287 static_assert(std::is_integral<T>::value, "must be integral type.");
1288 if (bits == 0u)
1289 {
1290 return -1;
1291 }
1292 else
1293 {
1294 return static_cast<int>(ScanReverse(bits));
1295 }
1296 }
1297
1298 // Returns whether the argument is Not a Number.
1299 // IEEE 754 single precision NaN representation: Exponent(8 bits) - 255, Mantissa(23 bits) -
1300 // non-zero.
isNaN(float f)1301 inline bool isNaN(float f)
1302 {
1303 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
1304 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
1305 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) &&
1306 (bitCast<uint32_t>(f) & 0x7fffffu);
1307 }
1308
1309 // Returns whether the argument is infinity.
1310 // IEEE 754 single precision infinity representation: Exponent(8 bits) - 255, Mantissa(23 bits) -
1311 // zero.
isInf(float f)1312 inline bool isInf(float f)
1313 {
1314 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
1315 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
1316 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) &&
1317 !(bitCast<uint32_t>(f) & 0x7fffffu);
1318 }
1319
1320 namespace priv
1321 {
1322 template <unsigned int N, unsigned int R>
1323 struct iSquareRoot
1324 {
solveiSquareRoot1325 static constexpr unsigned int solve()
1326 {
1327 return (R * R > N)
1328 ? 0
1329 : ((R * R == N) ? R : static_cast<unsigned int>(iSquareRoot<N, R + 1>::value));
1330 }
1331 enum Result
1332 {
1333 value = iSquareRoot::solve()
1334 };
1335 };
1336
1337 template <unsigned int N>
1338 struct iSquareRoot<N, N>
1339 {
1340 enum result
1341 {
1342 value = N
1343 };
1344 };
1345
1346 } // namespace priv
1347
1348 template <unsigned int N>
1349 constexpr unsigned int iSquareRoot()
1350 {
1351 return priv::iSquareRoot<N, 1>::value;
1352 }
1353
1354 // Sum, difference and multiplication operations for signed ints that wrap on 32-bit overflow.
1355 //
1356 // Unsigned types are defined to do arithmetic modulo 2^n in C++. For signed types, overflow
1357 // behavior is undefined.
1358
1359 template <typename T>
1360 inline T WrappingSum(T lhs, T rhs)
1361 {
1362 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
1363 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
1364 return static_cast<T>(lhsUnsigned + rhsUnsigned);
1365 }
1366
1367 template <typename T>
1368 inline T WrappingDiff(T lhs, T rhs)
1369 {
1370 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
1371 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
1372 return static_cast<T>(lhsUnsigned - rhsUnsigned);
1373 }
1374
1375 inline int32_t WrappingMul(int32_t lhs, int32_t rhs)
1376 {
1377 int64_t lhsWide = static_cast<int64_t>(lhs);
1378 int64_t rhsWide = static_cast<int64_t>(rhs);
1379 // The multiplication is guaranteed not to overflow.
1380 int64_t resultWide = lhsWide * rhsWide;
1381 // Implement the desired wrapping behavior by masking out the high-order 32 bits.
1382 resultWide = resultWide & 0xffffffffLL;
1383 // Casting to a narrower signed type is fine since the casted value is representable in the
1384 // narrower type.
1385 return static_cast<int32_t>(resultWide);
1386 }
1387
1388 inline float scaleScreenDimensionToNdc(float dimensionScreen, float viewportDimension)
1389 {
1390 return 2.0f * dimensionScreen / viewportDimension;
1391 }
1392
1393 inline float scaleScreenCoordinateToNdc(float coordinateScreen, float viewportDimension)
1394 {
1395 float halfShifted = coordinateScreen / viewportDimension;
1396 return 2.0f * (halfShifted - 0.5f);
1397 }
1398
1399 } // namespace gl
1400
1401 namespace rx
1402 {
1403
1404 template <typename T>
1405 T roundUp(const T value, const T alignment)
1406 {
1407 auto temp = value + alignment - static_cast<T>(1);
1408 return temp - temp % alignment;
1409 }
1410
1411 template <typename T>
1412 constexpr T roundUpPow2(const T value, const T alignment)
1413 {
1414 ASSERT(gl::isPow2(alignment));
1415 return (value + alignment - 1) & ~(alignment - 1);
1416 }
1417
1418 template <typename T>
1419 constexpr T roundDownPow2(const T value, const T alignment)
1420 {
1421 ASSERT(gl::isPow2(alignment));
1422 return value & ~(alignment - 1);
1423 }
1424
1425 template <typename T>
1426 angle::CheckedNumeric<T> CheckedRoundUp(const T value, const T alignment)
1427 {
1428 angle::CheckedNumeric<T> checkedValue(value);
1429 angle::CheckedNumeric<T> checkedAlignment(alignment);
1430 return roundUp(checkedValue, checkedAlignment);
1431 }
1432
1433 inline constexpr unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor)
1434 {
1435 unsigned int divided = value / divisor;
1436 return (divided + ((value % divisor == 0) ? 0 : 1));
1437 }
1438
1439 #if defined(__has_builtin)
1440 # define ANGLE_HAS_BUILTIN(x) __has_builtin(x)
1441 #else
1442 # define ANGLE_HAS_BUILTIN(x) 0
1443 #endif
1444
1445 #if defined(_MSC_VER)
1446
1447 # define ANGLE_ROTL(x, y) _rotl(x, y)
1448 # define ANGLE_ROTL64(x, y) _rotl64(x, y)
1449 # define ANGLE_ROTR16(x, y) _rotr16(x, y)
1450
1451 #elif defined(__clang__) && ANGLE_HAS_BUILTIN(__builtin_rotateleft32) && \
1452 ANGLE_HAS_BUILTIN(__builtin_rotateleft64) && ANGLE_HAS_BUILTIN(__builtin_rotateright16)
1453
1454 # define ANGLE_ROTL(x, y) __builtin_rotateleft32(x, y)
1455 # define ANGLE_ROTL64(x, y) __builtin_rotateleft64(x, y)
1456 # define ANGLE_ROTR16(x, y) __builtin_rotateright16(x, y)
1457
1458 #else
1459
1460 inline uint32_t RotL(uint32_t x, int8_t r)
1461 {
1462 return (x << r) | (x >> (32 - r));
1463 }
1464
1465 inline uint64_t RotL64(uint64_t x, int8_t r)
1466 {
1467 return (x << r) | (x >> (64 - r));
1468 }
1469
1470 inline uint16_t RotR16(uint16_t x, int8_t r)
1471 {
1472 return (x >> r) | (x << (16 - r));
1473 }
1474
1475 # define ANGLE_ROTL(x, y) ::rx::RotL(x, y)
1476 # define ANGLE_ROTL64(x, y) ::rx::RotL64(x, y)
1477 # define ANGLE_ROTR16(x, y) ::rx::RotR16(x, y)
1478
1479 #endif // namespace rx
1480
1481 constexpr unsigned int Log2(unsigned int bytes)
1482 {
1483 return bytes == 1 ? 0 : (1 + Log2(bytes / 2));
1484 }
1485 } // namespace rx
1486
1487 #endif // COMMON_MATHUTIL_H_
1488