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