blob: 9e3413e865f50e2738791c87dc22049339d7b381 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
shannonwoods@chromium.org92b9cd52013-05-30 00:14:48 +00002// Copyright (c) 2002-2013 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
Jamie Madillb155bbc2014-01-13 09:51:03 -050012#include <limits>
13#include <algorithm>
Arun Patolecdfa8f52015-06-30 17:48:25 +053014#include <math.h>
Jamie Madill4f267862014-04-17 15:53:37 -040015#include <string.h>
Arun Patolecdfa8f52015-06-30 17:48:25 +053016#include <stdint.h>
Jamie Madill3b9bb722015-01-05 16:17:02 -050017#include <stdlib.h>
Jamie Madillb155bbc2014-01-13 09:51:03 -050018
Jamie Madille2e406c2016-06-02 13:04:10 -040019#include <base/numerics/safe_math.h>
20
21#include "common/debug.h"
22#include "common/platform.h"
23
24namespace angle
25{
26using base::CheckedNumeric;
27using base::IsValueInRangeForNumericType;
28}
29
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030namespace gl
31{
shannonwoods@chromium.org49ec9922013-05-30 00:16:38 +000032
33const unsigned int Float32One = 0x3F800000;
34const unsigned short Float16One = 0x3C00;
35
apatrick@chromium.org60dafe82012-09-05 22:26:10 +000036struct Vector4
37{
38 Vector4() {}
39 Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
40
41 float x;
42 float y;
43 float z;
44 float w;
45};
46
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047inline bool isPow2(int x)
48{
49 return (x & (x - 1)) == 0 && (x != 0);
50}
51
52inline int log2(int x)
53{
54 int r = 0;
daniel@transgaming.come2b22122010-03-11 19:22:14 +000055 while ((x >> r) > 1) r++;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000056 return r;
57}
58
daniel@transgaming.com81655a72010-05-20 19:18:17 +000059inline unsigned int ceilPow2(unsigned int x)
60{
61 if (x != 0) x--;
62 x |= x >> 1;
63 x |= x >> 2;
64 x |= x >> 4;
65 x |= x >> 8;
66 x |= x >> 16;
67 x++;
68
69 return x;
70}
71
Jamie Madillb155bbc2014-01-13 09:51:03 -050072inline int clampToInt(unsigned int x)
73{
74 return static_cast<int>(std::min(x, static_cast<unsigned int>(std::numeric_limits<int>::max())));
75}
76
Jamie Madill70656a62014-03-05 15:01:26 -050077template <typename DestT, typename SrcT>
78inline DestT clampCast(SrcT value)
79{
Corentin Wallez630d85c2015-09-30 14:35:48 -070080 static const DestT destLo = std::numeric_limits<DestT>::min();
81 static const DestT destHi = std::numeric_limits<DestT>::max();
82 static const SrcT srcLo = static_cast<SrcT>(destLo);
83 static const SrcT srcHi = static_cast<SrcT>(destHi);
Jamie Madill62d31cb2015-09-11 13:25:51 -040084
Corentin Wallez630d85c2015-09-30 14:35:48 -070085 // When value is outside of or equal to the limits for DestT we use the DestT limit directly.
86 // This avoids undefined behaviors due to loss of precision when converting from floats to
87 // integers:
88 // destHi for ints is 2147483647 but the closest float number is around 2147483648, so when
89 // doing a conversion from float to int we run into an UB because the float is outside of the
90 // range representable by the int.
91 if (value <= srcLo)
92 {
93 return destLo;
94 }
95 else if (value >= srcHi)
96 {
97 return destHi;
98 }
99 else
100 {
101 return static_cast<DestT>(value);
102 }
Jamie Madill70656a62014-03-05 15:01:26 -0500103}
104
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000105template<typename T, typename MIN, typename MAX>
106inline T clamp(T x, MIN min, MAX max)
107{
shannon.woods@transgaming.com31c4f232013-02-28 23:14:18 +0000108 // Since NaNs fail all comparison tests, a NaN value will default to min
109 return x > min ? (x > max ? max : x) : min;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000110}
111
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000112inline float clamp01(float x)
113{
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000114 return clamp(x, 0.0f, 1.0f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000115}
116
117template<const int n>
118inline unsigned int unorm(float x)
119{
120 const unsigned int max = 0xFFFFFFFF >> (32 - n);
121
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000122 if (x > 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123 {
124 return max;
125 }
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000126 else if (x < 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127 {
128 return 0;
129 }
130 else
131 {
132 return (unsigned int)(max * x + 0.5f);
133 }
134}
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000135
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000136inline bool supportsSSE2()
137{
Geoff Lang5695fc92016-07-05 14:47:30 -0400138#if defined(ANGLE_USE_SSE)
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000139 static bool checked = false;
140 static bool supports = false;
141
142 if (checked)
143 {
144 return supports;
145 }
146
Geoff Lang5695fc92016-07-05 14:47:30 -0400147#if defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM)
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000148 {
Geoff Lang5695fc92016-07-05 14:47:30 -0400149 int info[4];
150 __cpuid(info, 0);
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000151
Geoff Lang5695fc92016-07-05 14:47:30 -0400152 if (info[0] >= 1)
153 {
154 __cpuid(info, 1);
155
156 supports = (info[3] >> 26) & 1;
157 }
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000158 }
Geoff Lang5695fc92016-07-05 14:47:30 -0400159#endif // defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM)
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000160 checked = true;
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000161 return supports;
Geoff Lang5695fc92016-07-05 14:47:30 -0400162#else // defined(ANGLE_USE_SSE)
Geoff Lang83217792014-01-16 09:52:38 -0500163 return false;
164#endif
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000165}
apatrick@chromium.orgaa480672012-09-05 19:32:38 +0000166
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000167template <typename destType, typename sourceType>
168destType bitCast(const sourceType &source)
169{
Jamie Madill7ab02fa2014-02-04 16:04:08 -0500170 size_t copySize = std::min(sizeof(destType), sizeof(sourceType));
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000171 destType output;
Jamie Madill7ab02fa2014-02-04 16:04:08 -0500172 memcpy(&output, &source, copySize);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000173 return output;
174}
175
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000176inline unsigned short float32ToFloat16(float fp32)
177{
hendrikw75782622015-09-25 11:28:50 -0700178 unsigned int fp32i = bitCast<unsigned int>(fp32);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000179 unsigned int sign = (fp32i & 0x80000000) >> 16;
180 unsigned int abs = fp32i & 0x7FFFFFFF;
181
182 if(abs > 0x47FFEFFF) // Infinity
183 {
Minmin Gong794e0002015-04-07 18:31:54 -0700184 return static_cast<unsigned short>(sign | 0x7FFF);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000185 }
186 else if(abs < 0x38800000) // Denormal
187 {
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +0000188 unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000189 int e = 113 - (abs >> 23);
190
191 if(e < 24)
192 {
193 abs = mantissa >> e;
194 }
195 else
196 {
197 abs = 0;
198 }
199
Minmin Gong794e0002015-04-07 18:31:54 -0700200 return static_cast<unsigned short>(sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000201 }
202 else
203 {
Minmin Gong794e0002015-04-07 18:31:54 -0700204 return static_cast<unsigned short>(sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000205 }
206}
207
apatrick@chromium.orgaa480672012-09-05 19:32:38 +0000208float float16ToFloat32(unsigned short h);
209
shannonwoods@chromium.org92b9cd52013-05-30 00:14:48 +0000210unsigned int convertRGBFloatsTo999E5(float red, float green, float blue);
211void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue);
212
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000213inline unsigned short float32ToFloat11(float fp32)
214{
215 const unsigned int float32MantissaMask = 0x7FFFFF;
216 const unsigned int float32ExponentMask = 0x7F800000;
217 const unsigned int float32SignMask = 0x80000000;
218 const unsigned int float32ValueMask = ~float32SignMask;
219 const unsigned int float32ExponentFirstBit = 23;
220 const unsigned int float32ExponentBias = 127;
221
222 const unsigned short float11Max = 0x7BF;
223 const unsigned short float11MantissaMask = 0x3F;
224 const unsigned short float11ExponentMask = 0x7C0;
225 const unsigned short float11BitMask = 0x7FF;
226 const unsigned int float11ExponentBias = 14;
227
228 const unsigned int float32Maxfloat11 = 0x477E0000;
229 const unsigned int float32Minfloat11 = 0x38800000;
230
231 const unsigned int float32Bits = bitCast<unsigned int>(fp32);
232 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
233
234 unsigned int float32Val = float32Bits & float32ValueMask;
235
236 if ((float32Val & float32ExponentMask) == float32ExponentMask)
237 {
238 // INF or NAN
239 if ((float32Val & float32MantissaMask) != 0)
240 {
241 return float11ExponentMask | (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) & float11MantissaMask);
242 }
243 else if (float32Sign)
244 {
245 // -INF is clamped to 0 since float11 is positive only
246 return 0;
247 }
248 else
249 {
250 return float11ExponentMask;
251 }
252 }
253 else if (float32Sign)
254 {
255 // float11 is positive only, so clamp to zero
256 return 0;
257 }
258 else if (float32Val > float32Maxfloat11)
259 {
260 // The number is too large to be represented as a float11, set to max
261 return float11Max;
262 }
263 else
264 {
265 if (float32Val < float32Minfloat11)
266 {
267 // The number is too small to be represented as a normalized float11
268 // Convert it to a denormalized value.
269 const unsigned int shift = (float32ExponentBias - float11ExponentBias) - (float32Val >> float32ExponentFirstBit);
270 float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
271 }
272 else
273 {
274 // Rebias the exponent to represent the value as a normalized float11
275 float32Val += 0xC8000000;
276 }
277
278 return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
279 }
280}
281
282inline unsigned short float32ToFloat10(float fp32)
283{
284 const unsigned int float32MantissaMask = 0x7FFFFF;
285 const unsigned int float32ExponentMask = 0x7F800000;
286 const unsigned int float32SignMask = 0x80000000;
287 const unsigned int float32ValueMask = ~float32SignMask;
288 const unsigned int float32ExponentFirstBit = 23;
289 const unsigned int float32ExponentBias = 127;
290
291 const unsigned short float10Max = 0x3DF;
292 const unsigned short float10MantissaMask = 0x1F;
293 const unsigned short float10ExponentMask = 0x3E0;
294 const unsigned short float10BitMask = 0x3FF;
295 const unsigned int float10ExponentBias = 14;
296
297 const unsigned int float32Maxfloat10 = 0x477C0000;
298 const unsigned int float32Minfloat10 = 0x38800000;
299
300 const unsigned int float32Bits = bitCast<unsigned int>(fp32);
301 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
302
303 unsigned int float32Val = float32Bits & float32ValueMask;
304
305 if ((float32Val & float32ExponentMask) == float32ExponentMask)
306 {
307 // INF or NAN
308 if ((float32Val & float32MantissaMask) != 0)
309 {
310 return float10ExponentMask | (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) & float10MantissaMask);
311 }
312 else if (float32Sign)
313 {
314 // -INF is clamped to 0 since float11 is positive only
315 return 0;
316 }
317 else
318 {
319 return float10ExponentMask;
320 }
321 }
322 else if (float32Sign)
323 {
324 // float10 is positive only, so clamp to zero
325 return 0;
326 }
327 else if (float32Val > float32Maxfloat10)
328 {
329 // The number is too large to be represented as a float11, set to max
330 return float10Max;
331 }
332 else
333 {
334 if (float32Val < float32Minfloat10)
335 {
336 // The number is too small to be represented as a normalized float11
337 // Convert it to a denormalized value.
338 const unsigned int shift = (float32ExponentBias - float10ExponentBias) - (float32Val >> float32ExponentFirstBit);
339 float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
340 }
341 else
342 {
343 // Rebias the exponent to represent the value as a normalized float11
344 float32Val += 0xC8000000;
345 }
346
347 return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
348 }
349}
350
351inline float float11ToFloat32(unsigned short fp11)
352{
353 unsigned short exponent = (fp11 >> 6) & 0x1F;
354 unsigned short mantissa = fp11 & 0x3F;
355
356 if (exponent == 0x1F)
357 {
358 // INF or NAN
359 return bitCast<float>(0x7f800000 | (mantissa << 17));
360 }
361 else
362 {
363 if (exponent != 0)
364 {
365 // normalized
366 }
367 else if (mantissa != 0)
368 {
369 // The value is denormalized
370 exponent = 1;
371
372 do
373 {
374 exponent--;
375 mantissa <<= 1;
376 }
377 while ((mantissa & 0x40) == 0);
378
379 mantissa = mantissa & 0x3F;
380 }
381 else // The value is zero
382 {
Austin Kinrossb8af7232015-03-16 22:33:25 -0700383 exponent = static_cast<unsigned short>(-112);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000384 }
385
386 return bitCast<float>(((exponent + 112) << 23) | (mantissa << 17));
387 }
388}
389
390inline float float10ToFloat32(unsigned short fp11)
391{
392 unsigned short exponent = (fp11 >> 5) & 0x1F;
393 unsigned short mantissa = fp11 & 0x1F;
394
395 if (exponent == 0x1F)
396 {
397 // INF or NAN
398 return bitCast<float>(0x7f800000 | (mantissa << 17));
399 }
400 else
401 {
402 if (exponent != 0)
403 {
404 // normalized
405 }
406 else if (mantissa != 0)
407 {
408 // The value is denormalized
409 exponent = 1;
410
411 do
412 {
413 exponent--;
414 mantissa <<= 1;
415 }
416 while ((mantissa & 0x20) == 0);
417
418 mantissa = mantissa & 0x1F;
419 }
420 else // The value is zero
421 {
Austin Kinrossb8af7232015-03-16 22:33:25 -0700422 exponent = static_cast<unsigned short>(-112);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000423 }
424
425 return bitCast<float>(((exponent + 112) << 23) | (mantissa << 18));
426 }
427}
428
Geoff Lang446a4472013-06-04 10:03:14 -0400429template <typename T>
430inline float normalizedToFloat(T input)
431{
Geoff Langd4475812015-03-18 10:53:05 -0400432 static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
Geoff Lang446a4472013-06-04 10:03:14 -0400433
434 const float inverseMax = 1.0f / std::numeric_limits<T>::max();
435 return input * inverseMax;
436}
437
438template <unsigned int inputBitCount, typename T>
439inline float normalizedToFloat(T input)
440{
Geoff Langd4475812015-03-18 10:53:05 -0400441 static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
442 static_assert(inputBitCount < (sizeof(T) * 8), "T must have more bits than inputBitCount.");
Geoff Lang446a4472013-06-04 10:03:14 -0400443
444 const float inverseMax = 1.0f / ((1 << inputBitCount) - 1);
445 return input * inverseMax;
446}
447
448template <typename T>
449inline T floatToNormalized(float input)
450{
Minmin Gong794e0002015-04-07 18:31:54 -0700451 return static_cast<T>(std::numeric_limits<T>::max() * input + 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400452}
453
454template <unsigned int outputBitCount, typename T>
455inline T floatToNormalized(float input)
456{
Geoff Langd4475812015-03-18 10:53:05 -0400457 static_assert(outputBitCount < (sizeof(T) * 8), "T must have more bits than outputBitCount.");
Minmin Gong794e0002015-04-07 18:31:54 -0700458 return static_cast<T>(((1 << outputBitCount) - 1) * input + 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400459}
460
461template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
462inline T getShiftedData(T input)
463{
Geoff Langd4475812015-03-18 10:53:05 -0400464 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
465 "T must have at least as many bits as inputBitCount + inputBitStart.");
Geoff Lang446a4472013-06-04 10:03:14 -0400466 const T mask = (1 << inputBitCount) - 1;
467 return (input >> inputBitStart) & mask;
468}
469
470template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
471inline T shiftData(T input)
472{
Geoff Langd4475812015-03-18 10:53:05 -0400473 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
474 "T must have at least as many bits as inputBitCount + inputBitStart.");
Geoff Lang446a4472013-06-04 10:03:14 -0400475 const T mask = (1 << inputBitCount) - 1;
476 return (input & mask) << inputBitStart;
477}
478
Olli Etuaho0f2b1562016-05-13 16:15:35 +0300479inline unsigned int CountLeadingZeros(uint32_t x)
480{
481 // Use binary search to find the amount of leading zeros.
482 unsigned int zeros = 32u;
483 uint32_t y;
484
485 y = x >> 16u;
486 if (y != 0)
487 {
488 zeros = zeros - 16u;
489 x = y;
490 }
491 y = x >> 8u;
492 if (y != 0)
493 {
494 zeros = zeros - 8u;
495 x = y;
496 }
497 y = x >> 4u;
498 if (y != 0)
499 {
500 zeros = zeros - 4u;
501 x = y;
502 }
503 y = x >> 2u;
504 if (y != 0)
505 {
506 zeros = zeros - 2u;
507 x = y;
508 }
509 y = x >> 1u;
510 if (y != 0)
511 {
512 return zeros - 2u;
513 }
514 return zeros - x;
515}
Geoff Lang446a4472013-06-04 10:03:14 -0400516
517inline unsigned char average(unsigned char a, unsigned char b)
518{
519 return ((a ^ b) >> 1) + (a & b);
520}
521
522inline signed char average(signed char a, signed char b)
523{
524 return ((short)a + (short)b) / 2;
525}
526
527inline unsigned short average(unsigned short a, unsigned short b)
528{
529 return ((a ^ b) >> 1) + (a & b);
530}
531
532inline signed short average(signed short a, signed short b)
533{
534 return ((int)a + (int)b) / 2;
535}
536
537inline unsigned int average(unsigned int a, unsigned int b)
538{
539 return ((a ^ b) >> 1) + (a & b);
540}
541
Corentin Wallezca311dd2015-12-07 15:07:48 -0500542inline int average(int a, int b)
Geoff Lang446a4472013-06-04 10:03:14 -0400543{
Corentin Wallezca311dd2015-12-07 15:07:48 -0500544 long long average = (static_cast<long long>(a) + static_cast<long long>(b)) / 2ll;
545 return static_cast<int>(average);
Geoff Lang446a4472013-06-04 10:03:14 -0400546}
547
548inline float average(float a, float b)
549{
550 return (a + b) * 0.5f;
551}
552
553inline unsigned short averageHalfFloat(unsigned short a, unsigned short b)
554{
555 return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f);
556}
557
558inline unsigned int averageFloat11(unsigned int a, unsigned int b)
559{
Minmin Gong794e0002015-04-07 18:31:54 -0700560 return float32ToFloat11((float11ToFloat32(static_cast<unsigned short>(a)) + float11ToFloat32(static_cast<unsigned short>(b))) * 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400561}
562
563inline unsigned int averageFloat10(unsigned int a, unsigned int b)
564{
Minmin Gong794e0002015-04-07 18:31:54 -0700565 return float32ToFloat10((float10ToFloat32(static_cast<unsigned short>(a)) + float10ToFloat32(static_cast<unsigned short>(b))) * 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400566}
567
Jamie Madill39b43462014-08-18 16:39:50 -0400568template <typename T>
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000569struct Range
570{
571 Range() {}
Jamie Madillcc002392014-09-09 10:21:56 -0400572 Range(T lo, T hi) : start(lo), end(hi) { ASSERT(lo <= hi); }
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000573
Jamie Madill39b43462014-08-18 16:39:50 -0400574 T start;
575 T end;
576
Jamie Madillcc002392014-09-09 10:21:56 -0400577 T length() const { return end - start; }
Corentin Wallezac6ff932014-11-12 06:16:53 -0800578
579 bool intersects(Range<T> other)
580 {
581 if (start <= other.start)
582 {
583 return other.start < end;
584 }
585 else
586 {
587 return start < other.end;
588 }
589 }
Jamie Madillaa9e9972015-06-22 13:57:16 -0400590
591 void extend(T value)
592 {
593 start = value > start ? value : start;
594 end = value < end ? value : end;
595 }
596
597 bool empty() const
598 {
599 return end <= start;
600 }
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000601};
602
Jamie Madill39b43462014-08-18 16:39:50 -0400603typedef Range<int> RangeI;
604typedef Range<unsigned int> RangeUI;
605
Geoff Lang3edfe032015-09-04 16:38:24 -0400606struct IndexRange
607{
608 IndexRange() : IndexRange(0, 0, 0) {}
609 IndexRange(size_t start_, size_t end_, size_t vertexIndexCount_)
610 : start(start_), end(end_), vertexIndexCount(vertexIndexCount_)
611 {
612 ASSERT(start <= end);
613 }
614
615 // Number of vertices in the range.
616 size_t vertexCount() const { return (end - start) + 1; }
617
618 // Inclusive range of indices that are not primitive restart
619 size_t start;
620 size_t end;
621
622 // Number of non-primitive restart indices
623 size_t vertexIndexCount;
624};
625
Arun Patolecdfa8f52015-06-30 17:48:25 +0530626// First, both normalized floating-point values are converted into 16-bit integer values.
627// Then, the results are packed into the returned 32-bit unsigned integer.
628// The first float value will be written to the least significant bits of the output;
629// the last float value will be written to the most significant bits.
630// The conversion of each value to fixed point is done as follows :
631// packSnorm2x16 : round(clamp(c, -1, +1) * 32767.0)
632inline uint32_t packSnorm2x16(float f1, float f2)
633{
Yuly Novikov451ed252016-01-21 21:16:45 -0500634 int16_t leastSignificantBits = static_cast<int16_t>(roundf(clamp(f1, -1.0f, 1.0f) * 32767.0f));
635 int16_t mostSignificantBits = static_cast<int16_t>(roundf(clamp(f2, -1.0f, 1.0f) * 32767.0f));
636 return static_cast<uint32_t>(mostSignificantBits) << 16 |
637 (static_cast<uint32_t>(leastSignificantBits) & 0xFFFF);
Arun Patolecdfa8f52015-06-30 17:48:25 +0530638}
639
640// First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then, each
641// component is converted to a normalized floating-point value to generate the returned two float values.
642// The first float value will be extracted from the least significant bits of the input;
643// the last float value will be extracted from the most-significant bits.
644// The conversion for unpacked fixed-point value to floating point is done as follows:
645// unpackSnorm2x16 : clamp(f / 32767.0, -1, +1)
646inline void unpackSnorm2x16(uint32_t u, float *f1, float *f2)
647{
648 int16_t leastSignificantBits = static_cast<int16_t>(u & 0xFFFF);
649 int16_t mostSignificantBits = static_cast<int16_t>(u >> 16);
650 *f1 = clamp(static_cast<float>(leastSignificantBits) / 32767.0f, -1.0f, 1.0f);
651 *f2 = clamp(static_cast<float>(mostSignificantBits) / 32767.0f, -1.0f, 1.0f);
652}
653
654// First, both normalized floating-point values are converted into 16-bit integer values.
655// Then, the results are packed into the returned 32-bit unsigned integer.
656// The first float value will be written to the least significant bits of the output;
657// the last float value will be written to the most significant bits.
658// The conversion of each value to fixed point is done as follows:
659// packUnorm2x16 : round(clamp(c, 0, +1) * 65535.0)
660inline uint32_t packUnorm2x16(float f1, float f2)
661{
662 uint16_t leastSignificantBits = static_cast<uint16_t>(roundf(clamp(f1, 0.0f, 1.0f) * 65535.0f));
663 uint16_t mostSignificantBits = static_cast<uint16_t>(roundf(clamp(f2, 0.0f, 1.0f) * 65535.0f));
664 return static_cast<uint32_t>(mostSignificantBits) << 16 | static_cast<uint32_t>(leastSignificantBits);
665}
666
667// First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then, each
668// component is converted to a normalized floating-point value to generate the returned two float values.
669// The first float value will be extracted from the least significant bits of the input;
670// the last float value will be extracted from the most-significant bits.
671// The conversion for unpacked fixed-point value to floating point is done as follows:
672// unpackUnorm2x16 : f / 65535.0
673inline void unpackUnorm2x16(uint32_t u, float *f1, float *f2)
674{
675 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
676 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
677 *f1 = static_cast<float>(leastSignificantBits) / 65535.0f;
678 *f2 = static_cast<float>(mostSignificantBits) / 65535.0f;
679}
680
681// Returns an unsigned integer obtained by converting the two floating-point values to the 16-bit
682// floating-point representation found in the OpenGL ES Specification, and then packing these
683// two 16-bit integers into a 32-bit unsigned integer.
684// f1: The 16 least-significant bits of the result;
685// f2: The 16 most-significant bits.
686inline uint32_t packHalf2x16(float f1, float f2)
687{
688 uint16_t leastSignificantBits = static_cast<uint16_t>(float32ToFloat16(f1));
689 uint16_t mostSignificantBits = static_cast<uint16_t>(float32ToFloat16(f2));
690 return static_cast<uint32_t>(mostSignificantBits) << 16 | static_cast<uint32_t>(leastSignificantBits);
691}
692
693// Returns two floating-point values obtained by unpacking a 32-bit unsigned integer into a pair of 16-bit values,
694// interpreting those values as 16-bit floating-point numbers according to the OpenGL ES Specification,
695// and converting them to 32-bit floating-point values.
696// The first float value is obtained from the 16 least-significant bits of u;
697// the second component is obtained from the 16 most-significant bits of u.
698inline void unpackHalf2x16(uint32_t u, float *f1, float *f2)
699{
700 uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF);
701 uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16);
702
703 *f1 = float16ToFloat32(leastSignificantBits);
704 *f2 = float16ToFloat32(mostSignificantBits);
705}
706
Arun Patole551279e2015-07-07 18:18:23 +0530707// Returns whether the argument is Not a Number.
708// IEEE 754 single precision NaN representation: Exponent(8 bits) - 255, Mantissa(23 bits) - non-zero.
709inline bool isNaN(float f)
710{
711 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
712 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
713 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) && (bitCast<uint32_t>(f) & 0x7fffffu);
714}
715
716// Returns whether the argument is infinity.
717// IEEE 754 single precision infinity representation: Exponent(8 bits) - 255, Mantissa(23 bits) - zero.
718inline bool isInf(float f)
719{
720 // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u
721 // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu
722 return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) && !(bitCast<uint32_t>(f) & 0x7fffffu);
723}
724
Sami Väisänene45e53b2016-05-25 10:36:04 +0300725namespace priv
726{
727template <unsigned int N, unsigned int R>
728struct iSquareRoot
729{
730 static constexpr unsigned int solve()
731 {
732 return (R * R > N)
733 ? 0
734 : ((R * R == N) ? R : static_cast<unsigned int>(iSquareRoot<N, R + 1>::value));
735 }
736 enum Result
737 {
738 value = iSquareRoot::solve()
739 };
740};
741
742template <unsigned int N>
743struct iSquareRoot<N, N>
744{
745 enum result
746 {
747 value = N
748 };
749};
750
751} // namespace priv
752
753template <unsigned int N>
754constexpr unsigned int iSquareRoot()
755{
756 return priv::iSquareRoot<N, 1>::value;
Geoff Lang831b1952015-05-05 11:02:27 -0400757}
758
Olli Etuaho7f9a55f2016-10-03 14:32:08 +0100759// Sum, difference and multiplication operations for signed ints that wrap on 32-bit overflow.
760//
761// Unsigned types are defined to do arithmetic modulo 2^n in C++. For signed types, overflow
762// behavior is undefined.
763
764template <typename T>
765inline T WrappingSum(T lhs, T rhs)
766{
767 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
768 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
769 return static_cast<T>(lhsUnsigned + rhsUnsigned);
770}
771
772template <typename T>
773inline T WrappingDiff(T lhs, T rhs)
774{
775 uint32_t lhsUnsigned = static_cast<uint32_t>(lhs);
776 uint32_t rhsUnsigned = static_cast<uint32_t>(rhs);
777 return static_cast<T>(lhsUnsigned - rhsUnsigned);
778}
779
780inline int32_t WrappingMul(int32_t lhs, int32_t rhs)
781{
782 int64_t lhsWide = static_cast<int64_t>(lhs);
783 int64_t rhsWide = static_cast<int64_t>(rhs);
784 // The multiplication is guaranteed not to overflow.
785 int64_t resultWide = lhsWide * rhsWide;
786 // Implement the desired wrapping behavior by masking out the high-order 32 bits.
787 resultWide = resultWide & 0xffffffffll;
788 // Casting to a narrower signed type is fine since the casted value is representable in the
789 // narrower type.
790 return static_cast<int32_t>(resultWide);
791}
792
Sami Väisänene45e53b2016-05-25 10:36:04 +0300793} // namespace gl
794
Geoff Lang831b1952015-05-05 11:02:27 -0400795namespace rx
796{
797
shannonwoods@chromium.org06d4e842013-05-30 00:04:42 +0000798template <typename T>
799T roundUp(const T value, const T alignment)
800{
Jamie Madille2e406c2016-06-02 13:04:10 -0400801 auto temp = value + alignment - static_cast<T>(1);
802 return temp - temp % alignment;
803}
804
805template <typename T>
806angle::CheckedNumeric<T> CheckedRoundUp(const T value, const T alignment)
807{
808 angle::CheckedNumeric<T> checkedValue(value);
809 angle::CheckedNumeric<T> checkedAlignment(alignment);
810 return roundUp(checkedValue, checkedAlignment);
shannonwoods@chromium.org06d4e842013-05-30 00:04:42 +0000811}
812
Jamie Madillaff80842014-08-04 10:47:56 -0400813inline unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor)
814{
815 unsigned int divided = value / divisor;
816 return (divided + ((value % divisor == 0) ? 0 : 1));
817}
818
Jamie Madill3b9bb722015-01-05 16:17:02 -0500819#if defined(_MSC_VER)
820
821#define ANGLE_ROTL(x,y) _rotl(x,y)
Austin Kinross405f3472015-06-04 13:09:06 -0700822#define ANGLE_ROTR16(x,y) _rotr16(x,y)
Jamie Madill3b9bb722015-01-05 16:17:02 -0500823
824#else
825
826inline uint32_t RotL(uint32_t x, int8_t r)
827{
828 return (x << r) | (x >> (32 - r));
829}
830
Austin Kinross405f3472015-06-04 13:09:06 -0700831inline uint16_t RotR16(uint16_t x, int8_t r)
832{
833 return (x >> r) | (x << (16 - r));
834}
835
Geoff Lang6e4cfce2016-06-13 15:06:31 -0400836#define ANGLE_ROTL(x, y) ::rx::RotL(x, y)
837#define ANGLE_ROTR16(x, y) ::rx::RotR16(x, y)
Jamie Madill3b9bb722015-01-05 16:17:02 -0500838
839#endif // namespace rx
840
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000841}
842
Geoff Lang0a73dd82014-11-19 16:18:08 -0500843#endif // COMMON_MATHUTIL_H_