blob: f47d217574483551125f1f87f836d22c52fd1fb6 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Stuart Morgan9d737962019-08-14 12:25:12 -07002// Copyright 2002 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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
Geoff Lang0a73dd82014-11-19 16:18:08 -05009#ifndef COMMON_MATHUTIL_H_
10#define COMMON_MATHUTIL_H_
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011
Arun Patolecdfa8f52015-06-30 17:48:25 +053012#include <math.h>
Arun Patolecdfa8f52015-06-30 17:48:25 +053013#include <stdint.h>
Jamie Madill3b9bb722015-01-05 16:17:02 -050014#include <stdlib.h>
Lingfeng Yang13b708f2018-03-21 12:14:10 -070015#include <string.h>
16#include <algorithm>
17#include <limits>
Jamie Madillb155bbc2014-01-13 09:51:03 -050018
Jamie Madill5ea762a2017-06-07 14:59:51 -040019#include <anglebase/numerics/safe_math.h>
Jamie Madille2e406c2016-06-02 13:04:10 -040020
21#include "common/debug.h"
22#include "common/platform.h"
23
24namespace angle
25{
26using base::CheckedNumeric;
27using base::IsValueInRangeForNumericType;
Jamie Madillb980c562018-11-27 11:34:27 -050028} // namespace angle
Jamie Madille2e406c2016-06-02 13:04:10 -040029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace gl
31{
shannonwoods@chromium.org49ec9922013-05-30 00:16:38 +000032
Lingfeng Yang13b708f2018-03-21 12:14:10 -070033const unsigned int Float32One = 0x3F800000;
shannonwoods@chromium.org49ec9922013-05-30 00:16:38 +000034const unsigned short Float16One = 0x3C00;
35
Lingfeng Yang13b708f2018-03-21 12:14:10 -070036template <typename T>
Jiacheng Lu3e493c42019-07-29 16:27:01 -060037inline constexpr bool isPow2(T x)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038{
Corentin Wallez0c7baf12016-12-19 15:43:10 -050039 static_assert(std::is_integral<T>::value, "isPow2 must be called on an integer type.");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040 return (x & (x - 1)) == 0 && (x != 0);
41}
42
Mohan Maiya01c0d6b2020-01-12 13:41:31 -080043template <typename T>
44inline int log2(T x)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000045{
Mohan Maiya01c0d6b2020-01-12 13:41:31 -080046 static_assert(std::is_integral<T>::value, "log2 must be called on an integer type.");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047 int r = 0;
Lingfeng Yang13b708f2018-03-21 12:14:10 -070048 while ((x >> r) > 1)
49 r++;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050 return r;
51}
52
daniel@transgaming.com81655a72010-05-20 19:18:17 +000053inline unsigned int ceilPow2(unsigned int x)
54{
Lingfeng Yang13b708f2018-03-21 12:14:10 -070055 if (x != 0)
56 x--;
daniel@transgaming.com81655a72010-05-20 19:18:17 +000057 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
Jamie Madill70656a62014-03-05 15:01:26 -050067template <typename DestT, typename SrcT>
68inline DestT clampCast(SrcT value)
69{
jchen10a99ed552017-09-22 08:10:32 +080070 // 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());
Jamie Madill62d31cb2015-09-11 13:25:51 -040079
jchen10a99ed552017-09-22 08:10:32 +080080 if (destHi < srcHi)
Corentin Wallez630d85c2015-09-30 14:35:48 -070081 {
jchen10a99ed552017-09-22 08:10:32 +080082 DestT destMax = std::numeric_limits<DestT>::max();
83 if (value >= static_cast<SrcT>(destMax))
84 {
85 return destMax;
86 }
Corentin Wallez630d85c2015-09-30 14:35:48 -070087 }
jchen10a99ed552017-09-22 08:10:32 +080088
89 if (destLo > srcLo)
Corentin Wallez630d85c2015-09-30 14:35:48 -070090 {
jchen10a99ed552017-09-22 08:10:32 +080091 DestT destLow = std::numeric_limits<DestT>::lowest();
92 if (value <= static_cast<SrcT>(destLow))
93 {
94 return destLow;
95 }
Corentin Wallez630d85c2015-09-30 14:35:48 -070096 }
jchen10a99ed552017-09-22 08:10:32 +080097
98 return static_cast<DestT>(value);
Jamie Madill70656a62014-03-05 15:01:26 -050099}
100
Olli Etuahoa87121f2017-10-06 14:07:27 +0200101// Specialize clampCast for bool->int conversion to avoid MSVS 2015 performance warning when the max
102// value is casted to the source type.
103template <>
104inline unsigned int clampCast(bool value)
105{
106 return static_cast<unsigned int>(value);
107}
108
109template <>
110inline int clampCast(bool value)
111{
112 return static_cast<int>(value);
113}
114
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700115template <typename T, typename MIN, typename MAX>
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000116inline T clamp(T x, MIN min, MAX max)
117{
shannon.woods@transgaming.com31c4f232013-02-28 23:14:18 +0000118 // Since NaNs fail all comparison tests, a NaN value will default to min
119 return x > min ? (x > max ? max : x) : min;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000120}
121
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122inline float clamp01(float x)
123{
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000124 return clamp(x, 0.0f, 1.0f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125}
126
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700127template <const int n>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128inline unsigned int unorm(float x)
129{
130 const unsigned int max = 0xFFFFFFFF >> (32 - n);
131
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000132 if (x > 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133 {
134 return max;
135 }
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000136 else if (x < 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 {
138 return 0;
139 }
140 else
141 {
142 return (unsigned int)(max * x + 0.5f);
143 }
144}
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000145
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000146inline bool supportsSSE2()
147{
Geoff Lang5695fc92016-07-05 14:47:30 -0400148#if defined(ANGLE_USE_SSE)
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700149 static bool checked = false;
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000150 static bool supports = false;
151
152 if (checked)
153 {
154 return supports;
155 }
156
Jamie Madillb980c562018-11-27 11:34:27 -0500157# if defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) && !defined(_M_ARM64)
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000158 {
Geoff Lang5695fc92016-07-05 14:47:30 -0400159 int info[4];
160 __cpuid(info, 0);
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000161
Geoff Lang5695fc92016-07-05 14:47:30 -0400162 if (info[0] >= 1)
163 {
164 __cpuid(info, 1);
165
166 supports = (info[3] >> 26) & 1;
167 }
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000168 }
Jamie Madillb980c562018-11-27 11:34:27 -0500169# endif // defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) && !defined(_M_ARM64)
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000170 checked = true;
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000171 return supports;
Geoff Lang5695fc92016-07-05 14:47:30 -0400172#else // defined(ANGLE_USE_SSE)
Geoff Lang83217792014-01-16 09:52:38 -0500173 return false;
174#endif
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000175}
apatrick@chromium.orgaa480672012-09-05 19:32:38 +0000176
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000177template <typename destType, typename sourceType>
178destType bitCast(const sourceType &source)
179{
Jamie Madill7ab02fa2014-02-04 16:04:08 -0500180 size_t copySize = std::min(sizeof(destType), sizeof(sourceType));
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000181 destType output;
Jamie Madill7ab02fa2014-02-04 16:04:08 -0500182 memcpy(&output, &source, copySize);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000183 return output;
184}
185
Tim Van Pattencbabea72019-07-11 17:24:17 -0600186// https://stackoverflow.com/a/37581284
187template <typename T>
188static constexpr double normalize(T value)
189{
190 return value < 0 ? -static_cast<double>(value) / std::numeric_limits<T>::min()
191 : static_cast<double>(value) / std::numeric_limits<T>::max();
192}
193
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000194inline unsigned short float32ToFloat16(float fp32)
195{
hendrikw75782622015-09-25 11:28:50 -0700196 unsigned int fp32i = bitCast<unsigned int>(fp32);
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700197 unsigned int sign = (fp32i & 0x80000000) >> 16;
198 unsigned int abs = fp32i & 0x7FFFFFFF;
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000199
Tim Van Pattendb990832019-06-26 16:05:02 -0600200 if (abs > 0x7F800000)
201 { // NaN
202 return 0x7FFF;
203 }
204 else if (abs > 0x47FFEFFF)
205 { // Infinity
206 return static_cast<uint16_t>(sign | 0x7C00);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000207 }
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700208 else if (abs < 0x38800000) // Denormal
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000209 {
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +0000210 unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700211 int e = 113 - (abs >> 23);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000212
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700213 if (e < 24)
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000214 {
215 abs = mantissa >> e;
216 }
217 else
218 {
219 abs = 0;
220 }
221
Minmin Gong794e0002015-04-07 18:31:54 -0700222 return static_cast<unsigned short>(sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000223 }
224 else
225 {
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700226 return static_cast<unsigned short>(
227 sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000228 }
229}
230
apatrick@chromium.orgaa480672012-09-05 19:32:38 +0000231float float16ToFloat32(unsigned short h);
232
shannonwoods@chromium.org92b9cd52013-05-30 00:14:48 +0000233unsigned int convertRGBFloatsTo999E5(float red, float green, float blue);
234void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue);
235
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000236inline unsigned short float32ToFloat11(float fp32)
237{
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700238 const unsigned int float32MantissaMask = 0x7FFFFF;
239 const unsigned int float32ExponentMask = 0x7F800000;
240 const unsigned int float32SignMask = 0x80000000;
241 const unsigned int float32ValueMask = ~float32SignMask;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000242 const unsigned int float32ExponentFirstBit = 23;
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700243 const unsigned int float32ExponentBias = 127;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000244
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700245 const unsigned short float11Max = 0x7BF;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000246 const unsigned short float11MantissaMask = 0x3F;
247 const unsigned short float11ExponentMask = 0x7C0;
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700248 const unsigned short float11BitMask = 0x7FF;
249 const unsigned int float11ExponentBias = 14;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000250
251 const unsigned int float32Maxfloat11 = 0x477E0000;
252 const unsigned int float32Minfloat11 = 0x38800000;
253
254 const unsigned int float32Bits = bitCast<unsigned int>(fp32);
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700255 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000256
257 unsigned int float32Val = float32Bits & float32ValueMask;
258
259 if ((float32Val & float32ExponentMask) == float32ExponentMask)
260 {
261 // INF or NAN
262 if ((float32Val & float32MantissaMask) != 0)
263 {
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700264 return float11ExponentMask |
265 (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) &
266 float11MantissaMask);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000267 }
268 else if (float32Sign)
269 {
270 // -INF is clamped to 0 since float11 is positive only
271 return 0;
272 }
273 else
274 {
275 return float11ExponentMask;
276 }
277 }
278 else if (float32Sign)
279 {
280 // float11 is positive only, so clamp to zero
281 return 0;
282 }
283 else if (float32Val > float32Maxfloat11)
284 {
285 // The number is too large to be represented as a float11, set to max
286 return float11Max;
287 }
288 else
289 {
290 if (float32Val < float32Minfloat11)
291 {
292 // The number is too small to be represented as a normalized float11
293 // Convert it to a denormalized value.
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700294 const unsigned int shift = (float32ExponentBias - float11ExponentBias) -
295 (float32Val >> float32ExponentFirstBit);
296 float32Val =
297 ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000298 }
299 else
300 {
301 // Rebias the exponent to represent the value as a normalized float11
302 float32Val += 0xC8000000;
303 }
304
305 return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
306 }
307}
308
309inline unsigned short float32ToFloat10(float fp32)
310{
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700311 const unsigned int float32MantissaMask = 0x7FFFFF;
312 const unsigned int float32ExponentMask = 0x7F800000;
313 const unsigned int float32SignMask = 0x80000000;
314 const unsigned int float32ValueMask = ~float32SignMask;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000315 const unsigned int float32ExponentFirstBit = 23;
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700316 const unsigned int float32ExponentBias = 127;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000317
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700318 const unsigned short float10Max = 0x3DF;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000319 const unsigned short float10MantissaMask = 0x1F;
320 const unsigned short float10ExponentMask = 0x3E0;
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700321 const unsigned short float10BitMask = 0x3FF;
322 const unsigned int float10ExponentBias = 14;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000323
324 const unsigned int float32Maxfloat10 = 0x477C0000;
325 const unsigned int float32Minfloat10 = 0x38800000;
326
327 const unsigned int float32Bits = bitCast<unsigned int>(fp32);
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700328 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000329
330 unsigned int float32Val = float32Bits & float32ValueMask;
331
332 if ((float32Val & float32ExponentMask) == float32ExponentMask)
333 {
334 // INF or NAN
335 if ((float32Val & float32MantissaMask) != 0)
336 {
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700337 return float10ExponentMask |
338 (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) &
339 float10MantissaMask);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000340 }
341 else if (float32Sign)
342 {
343 // -INF is clamped to 0 since float11 is positive only
344 return 0;
345 }
346 else
347 {
348 return float10ExponentMask;
349 }
350 }
351 else if (float32Sign)
352 {
353 // float10 is positive only, so clamp to zero
354 return 0;
355 }
356 else if (float32Val > float32Maxfloat10)
357 {
358 // The number is too large to be represented as a float11, set to max
359 return float10Max;
360 }
361 else
362 {
363 if (float32Val < float32Minfloat10)
364 {
365 // The number is too small to be represented as a normalized float11
366 // Convert it to a denormalized value.
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700367 const unsigned int shift = (float32ExponentBias - float10ExponentBias) -
368 (float32Val >> float32ExponentFirstBit);
369 float32Val =
370 ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000371 }
372 else
373 {
374 // Rebias the exponent to represent the value as a normalized float11
375 float32Val += 0xC8000000;
376 }
377
378 return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
379 }
380}
381
382inline float float11ToFloat32(unsigned short fp11)
383{
384 unsigned short exponent = (fp11 >> 6) & 0x1F;
385 unsigned short mantissa = fp11 & 0x3F;
386
387 if (exponent == 0x1F)
388 {
389 // INF or NAN
390 return bitCast<float>(0x7f800000 | (mantissa << 17));
391 }
392 else
393 {
394 if (exponent != 0)
395 {
396 // normalized
397 }
398 else if (mantissa != 0)
399 {
400 // The value is denormalized
401 exponent = 1;
402
403 do
404 {
405 exponent--;
406 mantissa <<= 1;
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700407 } while ((mantissa & 0x40) == 0);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000408
409 mantissa = mantissa & 0x3F;
410 }
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700411 else // The value is zero
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000412 {
Austin Kinrossb8af7232015-03-16 22:33:25 -0700413 exponent = static_cast<unsigned short>(-112);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000414 }
415
416 return bitCast<float>(((exponent + 112) << 23) | (mantissa << 17));
417 }
418}
419
420inline float float10ToFloat32(unsigned short fp11)
421{
422 unsigned short exponent = (fp11 >> 5) & 0x1F;
423 unsigned short mantissa = fp11 & 0x1F;
424
425 if (exponent == 0x1F)
426 {
427 // INF or NAN
428 return bitCast<float>(0x7f800000 | (mantissa << 17));
429 }
430 else
431 {
432 if (exponent != 0)
433 {
434 // normalized
435 }
436 else if (mantissa != 0)
437 {
438 // The value is denormalized
439 exponent = 1;
440
441 do
442 {
443 exponent--;
444 mantissa <<= 1;
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700445 } while ((mantissa & 0x20) == 0);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000446
447 mantissa = mantissa & 0x1F;
448 }
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700449 else // The value is zero
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000450 {
Austin Kinrossb8af7232015-03-16 22:33:25 -0700451 exponent = static_cast<unsigned short>(-112);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000452 }
453
454 return bitCast<float>(((exponent + 112) << 23) | (mantissa << 18));
455 }
456}
457
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700458// Convers to and from float and 16.16 fixed point format.
459
Le Quyend200a772019-10-10 00:44:01 +0800460inline float ConvertFixedToFloat(uint32_t fixedInput)
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700461{
462 return static_cast<float>(fixedInput) / 65536.0f;
463}
464
Le Quyend200a772019-10-10 00:44:01 +0800465inline uint32_t ConvertFloatToFixed(float floatInput)
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700466{
467 static constexpr uint32_t kHighest = 32767 * 65536 + 65535;
468 static constexpr uint32_t kLowest = static_cast<uint32_t>(-32768 * 65536 + 65535);
469
470 if (floatInput > 32767.65535)
471 {
472 return kHighest;
473 }
474 else if (floatInput < -32768.65535)
475 {
476 return kLowest;
477 }
478 else
479 {
480 return static_cast<uint32_t>(floatInput * 65536);
481 }
482}
483
Geoff Lang446a4472013-06-04 10:03:14 -0400484template <typename T>
485inline float normalizedToFloat(T input)
486{
Geoff Langd4475812015-03-18 10:53:05 -0400487 static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
Geoff Lang446a4472013-06-04 10:03:14 -0400488
Till Rathmannb8543632018-10-02 19:46:14 +0200489 if (sizeof(T) > 2)
490 {
491 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
492 constexpr double inverseMax = 1.0 / std::numeric_limits<T>::max();
493 return static_cast<float>(input * inverseMax);
494 }
495 else
496 {
497 constexpr float inverseMax = 1.0f / std::numeric_limits<T>::max();
498 return input * inverseMax;
499 }
Geoff Lang446a4472013-06-04 10:03:14 -0400500}
501
502template <unsigned int inputBitCount, typename T>
503inline float normalizedToFloat(T input)
504{
Geoff Langd4475812015-03-18 10:53:05 -0400505 static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
506 static_assert(inputBitCount < (sizeof(T) * 8), "T must have more bits than inputBitCount.");
Geoff Lang446a4472013-06-04 10:03:14 -0400507
Till Rathmannb8543632018-10-02 19:46:14 +0200508 if (inputBitCount > 23)
509 {
510 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
511 constexpr double inverseMax = 1.0 / ((1 << inputBitCount) - 1);
512 return static_cast<float>(input * inverseMax);
513 }
514 else
515 {
516 constexpr float inverseMax = 1.0f / ((1 << inputBitCount) - 1);
517 return input * inverseMax;
518 }
Geoff Lang446a4472013-06-04 10:03:14 -0400519}
520
521template <typename T>
522inline T floatToNormalized(float input)
523{
Till Rathmannb8543632018-10-02 19:46:14 +0200524 if (sizeof(T) > 2)
525 {
526 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
527 return static_cast<T>(std::numeric_limits<T>::max() * static_cast<double>(input) + 0.5);
528 }
529 else
530 {
531 return static_cast<T>(std::numeric_limits<T>::max() * input + 0.5f);
532 }
Geoff Lang446a4472013-06-04 10:03:14 -0400533}
534
535template <unsigned int outputBitCount, typename T>
536inline T floatToNormalized(float input)
537{
Geoff Langd4475812015-03-18 10:53:05 -0400538 static_assert(outputBitCount < (sizeof(T) * 8), "T must have more bits than outputBitCount.");
Till Rathmannb8543632018-10-02 19:46:14 +0200539
540 if (outputBitCount > 23)
541 {
542 // float has only a 23 bit mantissa, so we need to do the calculation in double precision
543 return static_cast<T>(((1 << outputBitCount) - 1) * static_cast<double>(input) + 0.5);
544 }
545 else
546 {
547 return static_cast<T>(((1 << outputBitCount) - 1) * input + 0.5f);
548 }
Geoff Lang446a4472013-06-04 10:03:14 -0400549}
550
551template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
552inline T getShiftedData(T input)
553{
Geoff Langd4475812015-03-18 10:53:05 -0400554 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
555 "T must have at least as many bits as inputBitCount + inputBitStart.");
Geoff Lang446a4472013-06-04 10:03:14 -0400556 const T mask = (1 << inputBitCount) - 1;
557 return (input >> inputBitStart) & mask;
558}
559
560template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
561inline T shiftData(T input)
562{
Geoff Langd4475812015-03-18 10:53:05 -0400563 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
564 "T must have at least as many bits as inputBitCount + inputBitStart.");
Geoff Lang446a4472013-06-04 10:03:14 -0400565 const T mask = (1 << inputBitCount) - 1;
566 return (input & mask) << inputBitStart;
567}
568
Olli Etuaho0f2b1562016-05-13 16:15:35 +0300569inline unsigned int CountLeadingZeros(uint32_t x)
570{
571 // Use binary search to find the amount of leading zeros.
572 unsigned int zeros = 32u;
573 uint32_t y;
574
575 y = x >> 16u;
576 if (y != 0)
577 {
578 zeros = zeros - 16u;
579 x = y;
580 }
581 y = x >> 8u;
582 if (y != 0)
583 {
584 zeros = zeros - 8u;
585 x = y;
586 }
587 y = x >> 4u;
588 if (y != 0)
589 {
590 zeros = zeros - 4u;
591 x = y;
592 }
593 y = x >> 2u;
594 if (y != 0)
595 {
596 zeros = zeros - 2u;
597 x = y;
598 }
599 y = x >> 1u;
600 if (y != 0)
601 {
602 return zeros - 2u;
603 }
604 return zeros - x;
605}
Geoff Lang446a4472013-06-04 10:03:14 -0400606
607inline unsigned char average(unsigned char a, unsigned char b)
608{
609 return ((a ^ b) >> 1) + (a & b);
610}
611
612inline signed char average(signed char a, signed char b)
613{
614 return ((short)a + (short)b) / 2;
615}
616
617inline unsigned short average(unsigned short a, unsigned short b)
618{
619 return ((a ^ b) >> 1) + (a & b);
620}
621
622inline signed short average(signed short a, signed short b)
623{
624 return ((int)a + (int)b) / 2;
625}
626
627inline unsigned int average(unsigned int a, unsigned int b)
628{
629 return ((a ^ b) >> 1) + (a & b);
630}
631
Corentin Wallezca311dd2015-12-07 15:07:48 -0500632inline int average(int a, int b)
Geoff Lang446a4472013-06-04 10:03:14 -0400633{
Corentin Wallezca311dd2015-12-07 15:07:48 -0500634 long long average = (static_cast<long long>(a) + static_cast<long long>(b)) / 2ll;
635 return static_cast<int>(average);
Geoff Lang446a4472013-06-04 10:03:14 -0400636}
637
638inline float average(float a, float b)
639{
640 return (a + b) * 0.5f;
641}
642
643inline unsigned short averageHalfFloat(unsigned short a, unsigned short b)
644{
645 return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f);
646}
647
648inline unsigned int averageFloat11(unsigned int a, unsigned int b)
649{
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700650 return float32ToFloat11((float11ToFloat32(static_cast<unsigned short>(a)) +
651 float11ToFloat32(static_cast<unsigned short>(b))) *
652 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400653}
654
655inline unsigned int averageFloat10(unsigned int a, unsigned int b)
656{
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700657 return float32ToFloat10((float10ToFloat32(static_cast<unsigned short>(a)) +
658 float10ToFloat32(static_cast<unsigned short>(b))) *
659 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400660}
661
Jamie Madill39b43462014-08-18 16:39:50 -0400662template <typename T>
Jamie Madill982f6e02017-06-07 14:33:04 -0400663class Range
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000664{
Jamie Madill982f6e02017-06-07 14:33:04 -0400665 public:
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000666 Range() {}
Jamie Madill982f6e02017-06-07 14:33:04 -0400667 Range(T lo, T hi) : mLow(lo), mHigh(hi) {}
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000668
Jamie Madill982f6e02017-06-07 14:33:04 -0400669 T length() const { return (empty() ? 0 : (mHigh - mLow)); }
Corentin Wallezac6ff932014-11-12 06:16:53 -0800670
671 bool intersects(Range<T> other)
672 {
Jamie Madill982f6e02017-06-07 14:33:04 -0400673 if (mLow <= other.mLow)
Corentin Wallezac6ff932014-11-12 06:16:53 -0800674 {
Jamie Madill982f6e02017-06-07 14:33:04 -0400675 return other.mLow < mHigh;
Corentin Wallezac6ff932014-11-12 06:16:53 -0800676 }
677 else
678 {
Jamie Madill982f6e02017-06-07 14:33:04 -0400679 return mLow < other.mHigh;
Corentin Wallezac6ff932014-11-12 06:16:53 -0800680 }
681 }
Jamie Madillaa9e9972015-06-22 13:57:16 -0400682
Jamie Madill982f6e02017-06-07 14:33:04 -0400683 // Assumes that end is non-inclusive.. for example, extending to 5 will make "end" 6.
Jamie Madillaa9e9972015-06-22 13:57:16 -0400684 void extend(T value)
685 {
Jamie Madill982f6e02017-06-07 14:33:04 -0400686 mLow = value < mLow ? value : mLow;
687 mHigh = value >= mHigh ? (value + 1) : mHigh;
Jamie Madillaa9e9972015-06-22 13:57:16 -0400688 }
689
Jamie Madill982f6e02017-06-07 14:33:04 -0400690 bool empty() const { return mHigh <= mLow; }
691
692 bool contains(T value) const { return value >= mLow && value < mHigh; }
693
694 class Iterator final
Jamie Madillaa9e9972015-06-22 13:57:16 -0400695 {
Jamie Madill982f6e02017-06-07 14:33:04 -0400696 public:
697 Iterator(T value) : mCurrent(value) {}
698
699 Iterator &operator++()
700 {
701 mCurrent++;
702 return *this;
703 }
704 bool operator==(const Iterator &other) const { return mCurrent == other.mCurrent; }
705 bool operator!=(const Iterator &other) const { return mCurrent != other.mCurrent; }
706 T operator*() const { return mCurrent; }
707
708 private:
709 T mCurrent;
710 };
711
712 Iterator begin() const { return Iterator(mLow); }
713
714 Iterator end() const { return Iterator(mHigh); }
715
716 T low() const { return mLow; }
717 T high() const { return mHigh; }
718
Jamie Madill8c3988c2017-12-21 14:44:56 -0500719 void invalidate()
720 {
721 mLow = std::numeric_limits<T>::max();
722 mHigh = std::numeric_limits<T>::min();
723 }
724
Jamie Madill982f6e02017-06-07 14:33:04 -0400725 private:
726 T mLow;
727 T mHigh;
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000728};
729
Jamie Madill39b43462014-08-18 16:39:50 -0400730typedef Range<int> RangeI;
731typedef Range<unsigned int> RangeUI;
732
Geoff Lang3edfe032015-09-04 16:38:24 -0400733struct IndexRange
734{
Markus Tavenrathcc37cbf2018-12-30 23:35:25 +0900735 struct Undefined
736 {};
737 IndexRange(Undefined) {}
Geoff Lang3edfe032015-09-04 16:38:24 -0400738 IndexRange() : IndexRange(0, 0, 0) {}
739 IndexRange(size_t start_, size_t end_, size_t vertexIndexCount_)
740 : start(start_), end(end_), vertexIndexCount(vertexIndexCount_)
741 {
742 ASSERT(start <= end);
743 }
744
745 // Number of vertices in the range.
746 size_t vertexCount() const { return (end - start) + 1; }
747
748 // Inclusive range of indices that are not primitive restart
749 size_t start;
750 size_t end;
751
752 // Number of non-primitive restart indices
753 size_t vertexIndexCount;
754};
755
Olli Etuaho74da73f2017-02-01 15:37:48 +0000756// Combine a floating-point value representing a mantissa (x) and an integer exponent (exp) into a
757// floating-point value. As in GLSL ldexp() built-in.
758inline float Ldexp(float x, int exp)
759{
760 if (exp > 128)
761 {
762 return std::numeric_limits<float>::infinity();
763 }
764 if (exp < -126)
765 {
766 return 0.0f;
767 }
768 double result = static_cast<double>(x) * std::pow(2.0, static_cast<double>(exp));
769 return static_cast<float>(result);
770}
771
Arun Patolecdfa8f52015-06-30 17:48:25 +0530772// First, both normalized floating-point values are converted into 16-bit integer values.
773// Then, the results are packed into the returned 32-bit unsigned integer.
774// The first float value will be written to the least significant bits of the output;
775// the last float value will be written to the most significant bits.
776// The conversion of each value to fixed point is done as follows :
777// packSnorm2x16 : round(clamp(c, -1, +1) * 32767.0)
778inline uint32_t packSnorm2x16(float f1, float f2)
779{
Yuly Novikov451ed252016-01-21 21:16:45 -0500780 int16_t leastSignificantBits = static_cast<int16_t>(roundf(clamp(f1, -1.0f, 1.0f) * 32767.0f));
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700781 int16_t mostSignificantBits = static_cast<int16_t>(roundf(clamp(f2, -1.0f, 1.0f) * 32767.0f));
Yuly Novikov451ed252016-01-21 21:16:45 -0500782 return static_cast<uint32_t>(mostSignificantBits) << 16 |
783 (static_cast<uint32_t>(leastSignificantBits) & 0xFFFF);
Arun Patolecdfa8f52015-06-30 17:48:25 +0530784}
785
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700786// First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then,
787// each component is converted to a normalized floating-point value to generate the returned two
788// float values. The first float value will be extracted from the least significant bits of the
789// input; the last float value will be extracted from the most-significant bits. The conversion for
790// unpacked fixed-point value to floating point is done as follows: unpackSnorm2x16 : clamp(f /
791// 32767.0, -1, +1)
Arun Patolecdfa8f52015-06-30 17:48:25 +0530792inline void unpackSnorm2x16(uint32_t u, float *f1, float *f2)
793{
794 int16_t leastSignificantBits = static_cast<int16_t>(u & 0xFFFF);
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700795 int16_t mostSignificantBits = static_cast<int16_t>(u >> 16);
Arun Patolecdfa8f52015-06-30 17:48:25 +0530796 *f1 = clamp(static_cast<float>(leastSignificantBits) / 32767.0f, -1.0f, 1.0f);
797 *f2 = clamp(static_cast<float>(mostSignificantBits) / 32767.0f, -1.0f, 1.0f);
798}
799
800// First, both normalized floating-point values are converted into 16-bit integer values.
801// Then, the results are packed into the returned 32-bit unsigned integer.
802// The first float value will be written to the least significant bits of the output;
803// the last float value will be written to the most significant bits.
804// The conversion of each value to fixed point is done as follows:
805// packUnorm2x16 : round(clamp(c, 0, +1) * 65535.0)
806inline uint32_t packUnorm2x16(float f1, float f2)
807{
808 uint16_t leastSignificantBits = static_cast<uint16_t>(roundf(clamp(f1, 0.0f, 1.0f) * 65535.0f));
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700809 uint16_t mostSignificantBits = static_cast<uint16_t>(roundf(clamp(f2, 0.0f, 1.0f) * 65535.0f));
810 return static_cast<uint32_t>(mostSignificantBits) << 16 |
811 static_cast<uint32_t>(leastSignificantBits);
Arun Patolecdfa8f52015-06-30 17:48:25 +0530812}
813
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700814// First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then,
815// each component is converted to a normalized floating-point value to generate the returned two
816// float values. The first float value will be extracted from the least significant bits of the
817// input; the last float value will be extracted from the most-significant bits. The conversion for
818// unpacked fixed-point value to floating point is done as follows: unpackUnorm2x16 : f / 65535.0
Arun Patolecdfa8f52015-06-30 17:48:25 +0530819inline void unpackUnorm2x16(uint32_t u, float *f1, float *f2)
820{
821 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700822 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
823 *f1 = static_cast<float>(leastSignificantBits) / 65535.0f;
824 *f2 = static_cast<float>(mostSignificantBits) / 65535.0f;
Arun Patolecdfa8f52015-06-30 17:48:25 +0530825}
826
Olli Etuaho25aef452017-01-29 16:15:44 -0800827// Helper functions intended to be used only here.
828namespace priv
829{
830
831inline uint8_t ToPackedUnorm8(float f)
832{
833 return static_cast<uint8_t>(roundf(clamp(f, 0.0f, 1.0f) * 255.0f));
834}
835
836inline int8_t ToPackedSnorm8(float f)
837{
838 return static_cast<int8_t>(roundf(clamp(f, -1.0f, 1.0f) * 127.0f));
839}
840
841} // namespace priv
842
843// Packs 4 normalized unsigned floating-point values to a single 32-bit unsigned integer. Works
844// similarly to packUnorm2x16. The floats are clamped to the range 0.0 to 1.0, and written to the
845// unsigned integer starting from the least significant bits.
846inline uint32_t PackUnorm4x8(float f1, float f2, float f3, float f4)
847{
848 uint8_t bits[4];
849 bits[0] = priv::ToPackedUnorm8(f1);
850 bits[1] = priv::ToPackedUnorm8(f2);
851 bits[2] = priv::ToPackedUnorm8(f3);
852 bits[3] = priv::ToPackedUnorm8(f4);
853 uint32_t result = 0u;
854 for (int i = 0; i < 4; ++i)
855 {
856 int shift = i * 8;
857 result |= (static_cast<uint32_t>(bits[i]) << shift);
858 }
859 return result;
860}
861
862// Unpacks 4 normalized unsigned floating-point values from a single 32-bit unsigned integer into f.
863// Works similarly to unpackUnorm2x16. The floats are unpacked starting from the least significant
864// bits.
865inline void UnpackUnorm4x8(uint32_t u, float *f)
866{
867 for (int i = 0; i < 4; ++i)
868 {
869 int shift = i * 8;
870 uint8_t bits = static_cast<uint8_t>((u >> shift) & 0xFF);
871 f[i] = static_cast<float>(bits) / 255.0f;
872 }
873}
874
875// Packs 4 normalized signed floating-point values to a single 32-bit unsigned integer. The floats
876// are clamped to the range -1.0 to 1.0, and written to the unsigned integer starting from the least
877// significant bits.
878inline uint32_t PackSnorm4x8(float f1, float f2, float f3, float f4)
879{
880 int8_t bits[4];
881 bits[0] = priv::ToPackedSnorm8(f1);
882 bits[1] = priv::ToPackedSnorm8(f2);
883 bits[2] = priv::ToPackedSnorm8(f3);
884 bits[3] = priv::ToPackedSnorm8(f4);
885 uint32_t result = 0u;
886 for (int i = 0; i < 4; ++i)
887 {
888 int shift = i * 8;
889 result |= ((static_cast<uint32_t>(bits[i]) & 0xFF) << shift);
890 }
891 return result;
892}
893
894// Unpacks 4 normalized signed floating-point values from a single 32-bit unsigned integer into f.
895// Works similarly to unpackSnorm2x16. The floats are unpacked starting from the least significant
896// bits, and clamped to the range -1.0 to 1.0.
897inline void UnpackSnorm4x8(uint32_t u, float *f)
898{
899 for (int i = 0; i < 4; ++i)
900 {
901 int shift = i * 8;
902 int8_t bits = static_cast<int8_t>((u >> shift) & 0xFF);
903 f[i] = clamp(static_cast<float>(bits) / 127.0f, -1.0f, 1.0f);
904 }
905}
906
Arun Patolecdfa8f52015-06-30 17:48:25 +0530907// Returns an unsigned integer obtained by converting the two floating-point values to the 16-bit
908// floating-point representation found in the OpenGL ES Specification, and then packing these
909// two 16-bit integers into a 32-bit unsigned integer.
910// f1: The 16 least-significant bits of the result;
911// f2: The 16 most-significant bits.
912inline uint32_t packHalf2x16(float f1, float f2)
913{
914 uint16_t leastSignificantBits = static_cast<uint16_t>(float32ToFloat16(f1));
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700915 uint16_t mostSignificantBits = static_cast<uint16_t>(float32ToFloat16(f2));
916 return static_cast<uint32_t>(mostSignificantBits) << 16 |
917 static_cast<uint32_t>(leastSignificantBits);
Arun Patolecdfa8f52015-06-30 17:48:25 +0530918}
919
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700920// Returns two floating-point values obtained by unpacking a 32-bit unsigned integer into a pair of
921// 16-bit values, interpreting those values as 16-bit floating-point numbers according to the OpenGL
922// ES Specification, and converting them to 32-bit floating-point values. The first float value is
923// obtained from the 16 least-significant bits of u; the second component is obtained from the 16
924// most-significant bits of u.
Arun Patolecdfa8f52015-06-30 17:48:25 +0530925inline void unpackHalf2x16(uint32_t u, float *f1, float *f2)
926{
927 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
Lingfeng Yang13b708f2018-03-21 12:14:10 -0700928 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
Arun Patolecdfa8f52015-06-30 17:48:25 +0530929
930 *f1 = float16ToFloat32(leastSignificantBits);
931 *f2 = float16ToFloat32(mostSignificantBits);
932}
933
Geoff Lang451b2b72017-05-15 15:32:02 -0400934inline uint8_t sRGBToLinear(uint8_t srgbValue)
935{
936 float value = srgbValue / 255.0f;
937 if (value <= 0.04045f)
938 {
939 value = value / 12.92f;
940 }
941 else
942 {
943 value = std::pow((value + 0.055f) / 1.055f, 2.4f);
944 }
945 return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f));
946}
947
948inline uint8_t linearToSRGB(uint8_t linearValue)
949{
950 float value = linearValue / 255.0f;
951 if (value <= 0.0f)
952 {
953 value = 0.0f;
954 }
955 else if (value < 0.0031308f)
956 {
957 value = value * 12.92f;
958 }
959 else if (value < 1.0f)
960 {
961 value = std::pow(value, 0.41666f) * 1.055f - 0.055f;
962 }
963 else
964 {
965 value = 1.0f;
966 }
967 return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f));
968}
969
Olli Etuaho9250cb22017-01-21 10:51:27 +0000970// Reverse the order of the bits.
971inline uint32_t BitfieldReverse(uint32_t value)
972{
973 // TODO(oetuaho@nvidia.com): Optimize this if needed. There don't seem to be compiler intrinsics
974 // for this, and right now it's not used in performance-critical paths.
975 uint32_t result = 0u;
976 for (size_t j = 0u; j < 32u; ++j)
977 {
978 result |= (((value >> j) & 1u) << (31u - j));
979 }
980 return result;
981}
982
983// Count the 1 bits.
Alexey Knyazev31e36a62020-03-14 21:40:58 +0400984#if defined(_MSC_VER) && !defined(__clang__)
985# if defined(_M_IX86) || defined(_M_X64)
986namespace priv
987{
988// Check POPCNT instruction support and cache the result.
989// https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64#remarks
990static const bool kHasPopcnt = [] {
991 int info[4];
992 __cpuid(&info[0], 1);
993 return static_cast<bool>(info[2] & 0x800000);
994}();
995} // namespace priv
996
997// Polyfills for x86/x64 CPUs without POPCNT.
998// https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
999inline int BitCountPolyfill(uint32_t bits)
1000{
1001 bits = bits - ((bits >> 1) & 0x55555555);
1002 bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
1003 bits = ((bits + (bits >> 4) & 0x0F0F0F0F) * 0x01010101) >> 24;
1004 return static_cast<int>(bits);
1005}
1006
1007inline int BitCountPolyfill(uint64_t bits)
1008{
1009 bits = bits - ((bits >> 1) & 0x5555555555555555ull);
1010 bits = (bits & 0x3333333333333333ull) + ((bits >> 2) & 0x3333333333333333ull);
1011 bits = ((bits + (bits >> 4) & 0x0F0F0F0F0F0F0F0Full) * 0x0101010101010101ull) >> 56;
1012 return static_cast<int>(bits);
1013}
1014
Jamie Madill6de51852017-04-12 09:53:01 -04001015inline int BitCount(uint32_t bits)
Olli Etuaho9250cb22017-01-21 10:51:27 +00001016{
Alexey Knyazev31e36a62020-03-14 21:40:58 +04001017 if (priv::kHasPopcnt)
1018 {
1019 return static_cast<int>(__popcnt(bits));
1020 }
1021 return BitCountPolyfill(bits);
Jamie Madill6de51852017-04-12 09:53:01 -04001022}
Alexey Knyazev31e36a62020-03-14 21:40:58 +04001023
Jamie Madill6de51852017-04-12 09:53:01 -04001024inline int BitCount(uint64_t bits)
1025{
Alexey Knyazev31e36a62020-03-14 21:40:58 +04001026 if (priv::kHasPopcnt)
1027 {
1028# if defined(_M_X64)
1029 return static_cast<int>(__popcnt64(bits));
1030# else // x86
1031 return static_cast<int>(__popcnt(static_cast<uint32_t>(bits >> 32)) +
1032 __popcnt(static_cast<uint32_t>(bits)));
1033# endif // defined(_M_X64)
1034 }
1035 return BitCountPolyfill(bits);
Jamie Madill6de51852017-04-12 09:53:01 -04001036}
Jamie Madill6de51852017-04-12 09:53:01 -04001037
Alexey Knyazev31e36a62020-03-14 21:40:58 +04001038# elif defined(_M_ARM) || defined(_M_ARM64)
1039
1040// MSVC's _CountOneBits* intrinsics are not defined for ARM64, moreover they do not use dedicated
1041// NEON instructions.
1042
1043inline int BitCount(uint32_t bits)
1044{
1045 // cast bits to 8x8 datatype and use VCNT on it
1046 const uint8x8_t vsum = vcnt_u8(vcreate_u8(static_cast<uint64_t>(bits)));
1047
1048 // pairwise sums: 8x8 -> 16x4 -> 32x2
1049 return static_cast<int>(vget_lane_u32(vpaddl_u16(vpaddl_u8(vsum)), 0));
1050}
1051
1052inline int BitCount(uint64_t bits)
1053{
1054 // cast bits to 8x8 datatype and use VCNT on it
1055 const uint8x8_t vsum = vcnt_u8(vcreate_u8(bits));
1056
1057 // pairwise sums: 8x8 -> 16x4 -> 32x2 -> 64x1
1058 return static_cast<int>(vget_lane_u64(vpaddl_u32(vpaddl_u16(vpaddl_u8(vsum))), 0));
1059}
1060# endif // defined(_M_IX86) || defined(_M_X64)
1061#endif // defined(_MSC_VER) && !defined(__clang__)
1062
1063#if defined(ANGLE_PLATFORM_POSIX) || defined(__clang__)
Jamie Madill6de51852017-04-12 09:53:01 -04001064inline int BitCount(uint32_t bits)
1065{
Olli Etuaho9250cb22017-01-21 10:51:27 +00001066 return __builtin_popcount(bits);
Olli Etuaho9250cb22017-01-21 10:51:27 +00001067}
1068
Jamie Madill6de51852017-04-12 09:53:01 -04001069inline int BitCount(uint64_t bits)
1070{
1071 return __builtin_popcountll(bits);
1072}
Alexey Knyazev31e36a62020-03-14 21:40:58 +04001073#endif // defined(ANGLE_PLATFORM_POSIX) || defined(__clang__)
Jeff Gilbertc0b82332018-09-26 18:04:05 -07001074
Jamie Madill7ce4a152018-06-12 10:19:49 -04001075inline int BitCount(uint8_t bits)
1076{
1077 return BitCount(static_cast<uint32_t>(bits));
1078}
1079
1080inline int BitCount(uint16_t bits)
1081{
1082 return BitCount(static_cast<uint32_t>(bits));
1083}
1084
Jamie Madill6de51852017-04-12 09:53:01 -04001085#if defined(ANGLE_PLATFORM_WINDOWS)
Olli Etuaho9250cb22017-01-21 10:51:27 +00001086// Return the index of the least significant bit set. Indexing is such that bit 0 is the least
Jamie Madill6de51852017-04-12 09:53:01 -04001087// significant bit. Implemented for different bit widths on different platforms.
1088inline unsigned long ScanForward(uint32_t bits)
Olli Etuaho9250cb22017-01-21 10:51:27 +00001089{
1090 ASSERT(bits != 0u);
Olli Etuaho9250cb22017-01-21 10:51:27 +00001091 unsigned long firstBitIndex = 0ul;
1092 unsigned char ret = _BitScanForward(&firstBitIndex, bits);
1093 ASSERT(ret != 0u);
1094 return firstBitIndex;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001095}
1096
Jamie Madill6de51852017-04-12 09:53:01 -04001097inline unsigned long ScanForward(uint64_t bits)
1098{
1099 ASSERT(bits != 0u);
1100 unsigned long firstBitIndex = 0ul;
Alexey Knyazeve6519442020-03-12 19:02:03 +04001101# if defined(ANGLE_IS_64_BIT_CPU)
1102 unsigned char ret = _BitScanForward64(&firstBitIndex, bits);
1103# else
1104 unsigned char ret;
1105 if (static_cast<uint32_t>(bits) == 0)
1106 {
1107 ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits >> 32));
1108 firstBitIndex += 32ul;
1109 }
1110 else
1111 {
1112 ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits));
1113 }
1114# endif // defined(ANGLE_IS_64_BIT_CPU)
Jamie Madill6de51852017-04-12 09:53:01 -04001115 ASSERT(ret != 0u);
1116 return firstBitIndex;
1117}
Alexey Knyazeve6519442020-03-12 19:02:03 +04001118#endif // defined(ANGLE_PLATFORM_WINDOWS)
Jamie Madill6de51852017-04-12 09:53:01 -04001119
1120#if defined(ANGLE_PLATFORM_POSIX)
1121inline unsigned long ScanForward(uint32_t bits)
1122{
1123 ASSERT(bits != 0u);
1124 return static_cast<unsigned long>(__builtin_ctz(bits));
1125}
1126
Jamie Madill6de51852017-04-12 09:53:01 -04001127inline unsigned long ScanForward(uint64_t bits)
1128{
1129 ASSERT(bits != 0u);
Alexey Knyazeve6519442020-03-12 19:02:03 +04001130# if defined(ANGLE_IS_64_BIT_CPU)
Jamie Madill6de51852017-04-12 09:53:01 -04001131 return static_cast<unsigned long>(__builtin_ctzll(bits));
Alexey Knyazeve6519442020-03-12 19:02:03 +04001132# else
1133 return static_cast<unsigned long>(static_cast<uint32_t>(bits) == 0
1134 ? __builtin_ctz(static_cast<uint32_t>(bits >> 32)) + 32
1135 : __builtin_ctz(static_cast<uint32_t>(bits)));
Jamie Madillb980c562018-11-27 11:34:27 -05001136# endif // defined(ANGLE_IS_64_BIT_CPU)
Alexey Knyazeve6519442020-03-12 19:02:03 +04001137}
1138#endif // defined(ANGLE_PLATFORM_POSIX)
Jamie Madill6de51852017-04-12 09:53:01 -04001139
Jamie Madill7ce4a152018-06-12 10:19:49 -04001140inline unsigned long ScanForward(uint8_t bits)
1141{
1142 return ScanForward(static_cast<uint32_t>(bits));
1143}
1144
1145inline unsigned long ScanForward(uint16_t bits)
1146{
1147 return ScanForward(static_cast<uint32_t>(bits));
1148}
1149
Olli Etuaho9250cb22017-01-21 10:51:27 +00001150// Return the index of the most significant bit set. Indexing is such that bit 0 is the least
1151// significant bit.
1152inline unsigned long ScanReverse(unsigned long bits)
1153{
1154 ASSERT(bits != 0u);
1155#if defined(ANGLE_PLATFORM_WINDOWS)
1156 unsigned long lastBitIndex = 0ul;
1157 unsigned char ret = _BitScanReverse(&lastBitIndex, bits);
1158 ASSERT(ret != 0u);
1159 return lastBitIndex;
1160#elif defined(ANGLE_PLATFORM_POSIX)
1161 return static_cast<unsigned long>(sizeof(unsigned long) * CHAR_BIT - 1 - __builtin_clzl(bits));
1162#else
Jamie Madillb980c562018-11-27 11:34:27 -05001163# error Please implement bit-scan-reverse for your platform!
Olli Etuaho9250cb22017-01-21 10:51:27 +00001164#endif
1165}
1166
1167// Returns -1 on 0, otherwise the index of the least significant 1 bit as in GLSL.
Jamie Madill6de51852017-04-12 09:53:01 -04001168template <typename T>
1169int FindLSB(T bits)
Olli Etuaho9250cb22017-01-21 10:51:27 +00001170{
Jamie Madill6de51852017-04-12 09:53:01 -04001171 static_assert(std::is_integral<T>::value, "must be integral type.");
Olli Etuaho9250cb22017-01-21 10:51:27 +00001172 if (bits == 0u)
1173 {
1174 return -1;
1175 }
1176 else
1177 {
1178 return static_cast<int>(ScanForward(bits));
1179 }
1180}
1181
1182// Returns -1 on 0, otherwise the index of the most significant 1 bit as in GLSL.
Jamie Madill6de51852017-04-12 09:53:01 -04001183template <typename T>
1184int FindMSB(T bits)
Olli Etuaho9250cb22017-01-21 10:51:27 +00001185{
Jamie Madill6de51852017-04-12 09:53:01 -04001186 static_assert(std::is_integral<T>::value, "must be integral type.");
Olli Etuaho9250cb22017-01-21 10:51:27 +00001187 if (bits == 0u)
1188 {
1189 return -1;
1190 }
1191 else
1192 {
1193 return static_cast<int>(ScanReverse(bits));
1194 }
1195}
1196
Arun Patole551279e2015-07-07 18:18:23 +05301197// Returns whether the argument is Not a Number.
Lingfeng Yang13b708f2018-03-21 12:14:10 -07001198// IEEE 754 single precision NaN representation: Exponent(8 bits) - 255, Mantissa(23 bits) -
1199// non-zero.
Arun Patole551279e2015-07-07 18:18:23 +05301200inline bool isNaN(float f)
1201{
1202 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
1203 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
Lingfeng Yang13b708f2018-03-21 12:14:10 -07001204 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) &&
1205 (bitCast<uint32_t>(f) & 0x7fffffu);
Arun Patole551279e2015-07-07 18:18:23 +05301206}
1207
1208// Returns whether the argument is infinity.
Lingfeng Yang13b708f2018-03-21 12:14:10 -07001209// IEEE 754 single precision infinity representation: Exponent(8 bits) - 255, Mantissa(23 bits) -
1210// zero.
Arun Patole551279e2015-07-07 18:18:23 +05301211inline bool isInf(float f)
1212{
1213 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
1214 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
Lingfeng Yang13b708f2018-03-21 12:14:10 -07001215 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) &&
1216 !(bitCast<uint32_t>(f) & 0x7fffffu);
Arun Patole551279e2015-07-07 18:18:23 +05301217}
1218
Sami Väisänene45e53b2016-05-25 10:36:04 +03001219namespace priv
1220{
1221template <unsigned int N, unsigned int R>
1222struct iSquareRoot
1223{
1224 static constexpr unsigned int solve()
1225 {
1226 return (R * R > N)
1227 ? 0
1228 : ((R * R == N) ? R : static_cast<unsigned int>(iSquareRoot<N, R + 1>::value));
1229 }
1230 enum Result
1231 {
1232 value = iSquareRoot::solve()
1233 };
1234};
1235
1236template <unsigned int N>
1237struct iSquareRoot<N, N>
1238{
1239 enum result
1240 {
1241 value = N
1242 };
1243};
1244
1245} // namespace priv
1246
1247template <unsigned int N>
1248constexpr unsigned int iSquareRoot()
1249{
1250 return priv::iSquareRoot<N, 1>::value;
Geoff Lang831b1952015-05-05 11:02:27 -04001251}
1252
Olli Etuaho7f9a55f2016-10-03 14:32:08 +01001253// Sum, difference and multiplication operations for signed ints that wrap on 32-bit overflow.
1254//
1255// Unsigned types are defined to do arithmetic modulo 2^n in C++. For signed types, overflow
1256// behavior is undefined.
1257
1258template <typename T>
1259inline T WrappingSum(T lhs, T rhs)
1260{
1261 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
1262 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
1263 return static_cast<T>(lhsUnsigned + rhsUnsigned);
1264}
1265
1266template <typename T>
1267inline T WrappingDiff(T lhs, T rhs)
1268{
1269 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
1270 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
1271 return static_cast<T>(lhsUnsigned - rhsUnsigned);
1272}
1273
1274inline int32_t WrappingMul(int32_t lhs, int32_t rhs)
1275{
1276 int64_t lhsWide = static_cast<int64_t>(lhs);
1277 int64_t rhsWide = static_cast<int64_t>(rhs);
1278 // The multiplication is guaranteed not to overflow.
1279 int64_t resultWide = lhsWide * rhsWide;
1280 // Implement the desired wrapping behavior by masking out the high-order 32 bits.
1281 resultWide = resultWide & 0xffffffffll;
1282 // Casting to a narrower signed type is fine since the casted value is representable in the
1283 // narrower type.
1284 return static_cast<int32_t>(resultWide);
1285}
1286
Lingfeng Yang0df813c2018-07-12 12:52:06 -07001287inline float scaleScreenDimensionToNdc(float dimensionScreen, float viewportDimension)
1288{
1289 return 2.0f * dimensionScreen / viewportDimension;
1290}
1291
1292inline float scaleScreenCoordinateToNdc(float coordinateScreen, float viewportDimension)
1293{
1294 float halfShifted = coordinateScreen / viewportDimension;
1295 return 2.0f * (halfShifted - 0.5f);
1296}
1297
Sami Väisänene45e53b2016-05-25 10:36:04 +03001298} // namespace gl
1299
Geoff Lang831b1952015-05-05 11:02:27 -04001300namespace rx
1301{
1302
shannonwoods@chromium.org06d4e842013-05-30 00:04:42 +00001303template <typename T>
1304T roundUp(const T value, const T alignment)
1305{
Jamie Madille2e406c2016-06-02 13:04:10 -04001306 auto temp = value + alignment - static_cast<T>(1);
1307 return temp - temp % alignment;
1308}
1309
1310template <typename T>
Jiacheng Lu3e493c42019-07-29 16:27:01 -06001311constexpr T roundUpPow2(const T value, const T alignment)
1312{
1313 ASSERT(gl::isPow2(alignment));
1314 return (value + alignment - 1) & ~(alignment - 1);
1315}
1316
1317template <typename T>
Jamie Madille2e406c2016-06-02 13:04:10 -04001318angle::CheckedNumeric<T> CheckedRoundUp(const T value, const T alignment)
1319{
1320 angle::CheckedNumeric<T> checkedValue(value);
1321 angle::CheckedNumeric<T> checkedAlignment(alignment);
1322 return roundUp(checkedValue, checkedAlignment);
shannonwoods@chromium.org06d4e842013-05-30 00:04:42 +00001323}
1324
Jamie Madill5993d892019-06-03 13:05:38 -04001325inline constexpr unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor)
Jamie Madillaff80842014-08-04 10:47:56 -04001326{
1327 unsigned int divided = value / divisor;
1328 return (divided + ((value % divisor == 0) ? 0 : 1));
1329}
1330
Jose Dapena Pazdd3de6f2019-07-22 21:17:52 +02001331#if defined(__has_builtin)
1332# define ANGLE_HAS_BUILTIN(x) __has_builtin(x)
1333#else
1334# define ANGLE_HAS_BUILTIN(x) 0
1335#endif
1336
Jamie Madill3b9bb722015-01-05 16:17:02 -05001337#if defined(_MSC_VER)
1338
Jamie Madillb980c562018-11-27 11:34:27 -05001339# define ANGLE_ROTL(x, y) _rotl(x, y)
Shahbaz Youssefie9111882019-07-12 16:15:31 -04001340# define ANGLE_ROTL64(x, y) _rotl64(x, y)
Jamie Madillb980c562018-11-27 11:34:27 -05001341# define ANGLE_ROTR16(x, y) _rotr16(x, y)
Jamie Madill3b9bb722015-01-05 16:17:02 -05001342
Jose Dapena Pazdd3de6f2019-07-22 21:17:52 +02001343#elif defined(__clang__) && ANGLE_HAS_BUILTIN(__builtin_rotateleft32) && \
1344 ANGLE_HAS_BUILTIN(__builtin_rotateleft64) && ANGLE_HAS_BUILTIN(__builtin_rotateright16)
Shahbaz Youssefie9111882019-07-12 16:15:31 -04001345
1346# define ANGLE_ROTL(x, y) __builtin_rotateleft32(x, y)
1347# define ANGLE_ROTL64(x, y) __builtin_rotateleft64(x, y)
1348# define ANGLE_ROTR16(x, y) __builtin_rotateright16(x, y)
1349
Jamie Madill3b9bb722015-01-05 16:17:02 -05001350#else
1351
1352inline uint32_t RotL(uint32_t x, int8_t r)
1353{
1354 return (x << r) | (x >> (32 - r));
1355}
1356
Shahbaz Youssefie9111882019-07-12 16:15:31 -04001357inline uint64_t RotL64(uint64_t x, int8_t r)
1358{
1359 return (x << r) | (x >> (64 - r));
1360}
1361
Austin Kinross405f3472015-06-04 13:09:06 -07001362inline uint16_t RotR16(uint16_t x, int8_t r)
1363{
1364 return (x >> r) | (x << (16 - r));
1365}
1366
Jamie Madillb980c562018-11-27 11:34:27 -05001367# define ANGLE_ROTL(x, y) ::rx::RotL(x, y)
Shahbaz Youssefie9111882019-07-12 16:15:31 -04001368# define ANGLE_ROTL64(x, y) ::rx::RotL64(x, y)
Jamie Madillb980c562018-11-27 11:34:27 -05001369# define ANGLE_ROTR16(x, y) ::rx::RotR16(x, y)
Jamie Madill3b9bb722015-01-05 16:17:02 -05001370
Lingfeng Yang13b708f2018-03-21 12:14:10 -07001371#endif // namespace rx
Jamie Madill3f0c4a52019-01-10 10:20:35 -05001372
1373constexpr unsigned int Log2(unsigned int bytes)
1374{
1375 return bytes == 1 ? 0 : (1 + Log2(bytes / 2));
1376}
Jamie Madillb980c562018-11-27 11:34:27 -05001377} // namespace rx
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +00001378
Lingfeng Yang13b708f2018-03-21 12:14:10 -07001379#endif // COMMON_MATHUTIL_H_