blob: 4f9454fa73e3a4f583255ab059ebd9fc2dcbad71 [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
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +000012#include "common/debug.h"
Geoff Lang44fa7592014-05-30 11:50:07 -040013#include "common/platform.h"
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +000014
Jamie Madillb155bbc2014-01-13 09:51:03 -050015#include <limits>
16#include <algorithm>
Jamie Madill4f267862014-04-17 15:53:37 -040017#include <string.h>
Jamie Madill3b9bb722015-01-05 16:17:02 -050018#include <stdlib.h>
Jamie Madillb155bbc2014-01-13 09:51:03 -050019
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020namespace gl
21{
shannonwoods@chromium.org49ec9922013-05-30 00:16:38 +000022
23const unsigned int Float32One = 0x3F800000;
24const unsigned short Float16One = 0x3C00;
25
apatrick@chromium.org60dafe82012-09-05 22:26:10 +000026struct Vector4
27{
28 Vector4() {}
29 Vector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
30
31 float x;
32 float y;
33 float z;
34 float w;
35};
36
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037inline bool isPow2(int x)
38{
39 return (x & (x - 1)) == 0 && (x != 0);
40}
41
42inline int log2(int x)
43{
44 int r = 0;
daniel@transgaming.come2b22122010-03-11 19:22:14 +000045 while ((x >> r) > 1) r++;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046 return r;
47}
48
daniel@transgaming.com81655a72010-05-20 19:18:17 +000049inline unsigned int ceilPow2(unsigned int x)
50{
51 if (x != 0) x--;
52 x |= x >> 1;
53 x |= x >> 2;
54 x |= x >> 4;
55 x |= x >> 8;
56 x |= x >> 16;
57 x++;
58
59 return x;
60}
61
Jamie Madillb155bbc2014-01-13 09:51:03 -050062inline int clampToInt(unsigned int x)
63{
64 return static_cast<int>(std::min(x, static_cast<unsigned int>(std::numeric_limits<int>::max())));
65}
66
Jamie Madill70656a62014-03-05 15:01:26 -050067template <typename DestT, typename SrcT>
68inline DestT clampCast(SrcT value)
69{
70 // This assumes SrcT can properly represent DestT::min/max
71 // Unfortunately we can't use META_ASSERT without C++11 constexpr support
72 ASSERT(static_cast<DestT>(static_cast<SrcT>(std::numeric_limits<DestT>::min())) == std::numeric_limits<DestT>::min());
73 ASSERT(static_cast<DestT>(static_cast<SrcT>(std::numeric_limits<DestT>::max())) == std::numeric_limits<DestT>::max());
74
75 SrcT lo = static_cast<SrcT>(std::numeric_limits<DestT>::min());
76 SrcT hi = static_cast<SrcT>(std::numeric_limits<DestT>::max());
77 return static_cast<DestT>(value > lo ? (value > hi ? hi : value) : lo);
78}
79
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +000080template<typename T, typename MIN, typename MAX>
81inline T clamp(T x, MIN min, MAX max)
82{
shannon.woods@transgaming.com31c4f232013-02-28 23:14:18 +000083 // Since NaNs fail all comparison tests, a NaN value will default to min
84 return x > min ? (x > max ? max : x) : min;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +000085}
86
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000087inline float clamp01(float x)
88{
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +000089 return clamp(x, 0.0f, 1.0f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090}
91
92template<const int n>
93inline unsigned int unorm(float x)
94{
95 const unsigned int max = 0xFFFFFFFF >> (32 - n);
96
daniel@transgaming.come2b22122010-03-11 19:22:14 +000097 if (x > 1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000098 {
99 return max;
100 }
daniel@transgaming.come2b22122010-03-11 19:22:14 +0000101 else if (x < 0)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102 {
103 return 0;
104 }
105 else
106 {
107 return (unsigned int)(max * x + 0.5f);
108 }
109}
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +0000110
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000111inline bool supportsSSE2()
112{
Jamie Madilld46275f2014-11-06 13:54:53 -0500113#if defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM)
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000114 static bool checked = false;
115 static bool supports = false;
116
117 if (checked)
118 {
119 return supports;
120 }
121
122 int info[4];
123 __cpuid(info, 0);
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +0000124
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000125 if (info[0] >= 1)
126 {
127 __cpuid(info, 1);
128
129 supports = (info[3] >> 26) & 1;
130 }
131
132 checked = true;
133
134 return supports;
Geoff Lang83217792014-01-16 09:52:38 -0500135#else
136 UNIMPLEMENTED();
137 return false;
138#endif
jbauman@chromium.orgf1f28c82011-05-12 20:53:34 +0000139}
apatrick@chromium.orgaa480672012-09-05 19:32:38 +0000140
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000141template <typename destType, typename sourceType>
142destType bitCast(const sourceType &source)
143{
Jamie Madill7ab02fa2014-02-04 16:04:08 -0500144 size_t copySize = std::min(sizeof(destType), sizeof(sourceType));
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000145 destType output;
Jamie Madill7ab02fa2014-02-04 16:04:08 -0500146 memcpy(&output, &source, copySize);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000147 return output;
148}
149
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000150inline unsigned short float32ToFloat16(float fp32)
151{
152 unsigned int fp32i = (unsigned int&)fp32;
153 unsigned int sign = (fp32i & 0x80000000) >> 16;
154 unsigned int abs = fp32i & 0x7FFFFFFF;
155
156 if(abs > 0x47FFEFFF) // Infinity
157 {
Minmin Gong794e0002015-04-07 18:31:54 -0700158 return static_cast<unsigned short>(sign | 0x7FFF);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000159 }
160 else if(abs < 0x38800000) // Denormal
161 {
shannonwoods@chromium.orga2ecfcc2013-05-30 00:11:59 +0000162 unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000;
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000163 int e = 113 - (abs >> 23);
164
165 if(e < 24)
166 {
167 abs = mantissa >> e;
168 }
169 else
170 {
171 abs = 0;
172 }
173
Minmin Gong794e0002015-04-07 18:31:54 -0700174 return static_cast<unsigned short>(sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000175 }
176 else
177 {
Minmin Gong794e0002015-04-07 18:31:54 -0700178 return static_cast<unsigned short>(sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13);
daniel@transgaming.com2e38b802012-10-17 18:30:06 +0000179 }
180}
181
apatrick@chromium.orgaa480672012-09-05 19:32:38 +0000182float float16ToFloat32(unsigned short h);
183
shannonwoods@chromium.org92b9cd52013-05-30 00:14:48 +0000184unsigned int convertRGBFloatsTo999E5(float red, float green, float blue);
185void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue);
186
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000187inline unsigned short float32ToFloat11(float fp32)
188{
189 const unsigned int float32MantissaMask = 0x7FFFFF;
190 const unsigned int float32ExponentMask = 0x7F800000;
191 const unsigned int float32SignMask = 0x80000000;
192 const unsigned int float32ValueMask = ~float32SignMask;
193 const unsigned int float32ExponentFirstBit = 23;
194 const unsigned int float32ExponentBias = 127;
195
196 const unsigned short float11Max = 0x7BF;
197 const unsigned short float11MantissaMask = 0x3F;
198 const unsigned short float11ExponentMask = 0x7C0;
199 const unsigned short float11BitMask = 0x7FF;
200 const unsigned int float11ExponentBias = 14;
201
202 const unsigned int float32Maxfloat11 = 0x477E0000;
203 const unsigned int float32Minfloat11 = 0x38800000;
204
205 const unsigned int float32Bits = bitCast<unsigned int>(fp32);
206 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
207
208 unsigned int float32Val = float32Bits & float32ValueMask;
209
210 if ((float32Val & float32ExponentMask) == float32ExponentMask)
211 {
212 // INF or NAN
213 if ((float32Val & float32MantissaMask) != 0)
214 {
215 return float11ExponentMask | (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) & float11MantissaMask);
216 }
217 else if (float32Sign)
218 {
219 // -INF is clamped to 0 since float11 is positive only
220 return 0;
221 }
222 else
223 {
224 return float11ExponentMask;
225 }
226 }
227 else if (float32Sign)
228 {
229 // float11 is positive only, so clamp to zero
230 return 0;
231 }
232 else if (float32Val > float32Maxfloat11)
233 {
234 // The number is too large to be represented as a float11, set to max
235 return float11Max;
236 }
237 else
238 {
239 if (float32Val < float32Minfloat11)
240 {
241 // The number is too small to be represented as a normalized float11
242 // Convert it to a denormalized value.
243 const unsigned int shift = (float32ExponentBias - float11ExponentBias) - (float32Val >> float32ExponentFirstBit);
244 float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
245 }
246 else
247 {
248 // Rebias the exponent to represent the value as a normalized float11
249 float32Val += 0xC8000000;
250 }
251
252 return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask;
253 }
254}
255
256inline unsigned short float32ToFloat10(float fp32)
257{
258 const unsigned int float32MantissaMask = 0x7FFFFF;
259 const unsigned int float32ExponentMask = 0x7F800000;
260 const unsigned int float32SignMask = 0x80000000;
261 const unsigned int float32ValueMask = ~float32SignMask;
262 const unsigned int float32ExponentFirstBit = 23;
263 const unsigned int float32ExponentBias = 127;
264
265 const unsigned short float10Max = 0x3DF;
266 const unsigned short float10MantissaMask = 0x1F;
267 const unsigned short float10ExponentMask = 0x3E0;
268 const unsigned short float10BitMask = 0x3FF;
269 const unsigned int float10ExponentBias = 14;
270
271 const unsigned int float32Maxfloat10 = 0x477C0000;
272 const unsigned int float32Minfloat10 = 0x38800000;
273
274 const unsigned int float32Bits = bitCast<unsigned int>(fp32);
275 const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask;
276
277 unsigned int float32Val = float32Bits & float32ValueMask;
278
279 if ((float32Val & float32ExponentMask) == float32ExponentMask)
280 {
281 // INF or NAN
282 if ((float32Val & float32MantissaMask) != 0)
283 {
284 return float10ExponentMask | (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) & float10MantissaMask);
285 }
286 else if (float32Sign)
287 {
288 // -INF is clamped to 0 since float11 is positive only
289 return 0;
290 }
291 else
292 {
293 return float10ExponentMask;
294 }
295 }
296 else if (float32Sign)
297 {
298 // float10 is positive only, so clamp to zero
299 return 0;
300 }
301 else if (float32Val > float32Maxfloat10)
302 {
303 // The number is too large to be represented as a float11, set to max
304 return float10Max;
305 }
306 else
307 {
308 if (float32Val < float32Minfloat10)
309 {
310 // The number is too small to be represented as a normalized float11
311 // Convert it to a denormalized value.
312 const unsigned int shift = (float32ExponentBias - float10ExponentBias) - (float32Val >> float32ExponentFirstBit);
313 float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift;
314 }
315 else
316 {
317 // Rebias the exponent to represent the value as a normalized float11
318 float32Val += 0xC8000000;
319 }
320
321 return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask;
322 }
323}
324
325inline float float11ToFloat32(unsigned short fp11)
326{
327 unsigned short exponent = (fp11 >> 6) & 0x1F;
328 unsigned short mantissa = fp11 & 0x3F;
329
330 if (exponent == 0x1F)
331 {
332 // INF or NAN
333 return bitCast<float>(0x7f800000 | (mantissa << 17));
334 }
335 else
336 {
337 if (exponent != 0)
338 {
339 // normalized
340 }
341 else if (mantissa != 0)
342 {
343 // The value is denormalized
344 exponent = 1;
345
346 do
347 {
348 exponent--;
349 mantissa <<= 1;
350 }
351 while ((mantissa & 0x40) == 0);
352
353 mantissa = mantissa & 0x3F;
354 }
355 else // The value is zero
356 {
Austin Kinrossb8af7232015-03-16 22:33:25 -0700357 exponent = static_cast<unsigned short>(-112);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000358 }
359
360 return bitCast<float>(((exponent + 112) << 23) | (mantissa << 17));
361 }
362}
363
364inline float float10ToFloat32(unsigned short fp11)
365{
366 unsigned short exponent = (fp11 >> 5) & 0x1F;
367 unsigned short mantissa = fp11 & 0x1F;
368
369 if (exponent == 0x1F)
370 {
371 // INF or NAN
372 return bitCast<float>(0x7f800000 | (mantissa << 17));
373 }
374 else
375 {
376 if (exponent != 0)
377 {
378 // normalized
379 }
380 else if (mantissa != 0)
381 {
382 // The value is denormalized
383 exponent = 1;
384
385 do
386 {
387 exponent--;
388 mantissa <<= 1;
389 }
390 while ((mantissa & 0x20) == 0);
391
392 mantissa = mantissa & 0x1F;
393 }
394 else // The value is zero
395 {
Austin Kinrossb8af7232015-03-16 22:33:25 -0700396 exponent = static_cast<unsigned short>(-112);
shannonwoods@chromium.orga43d8292013-05-30 00:15:50 +0000397 }
398
399 return bitCast<float>(((exponent + 112) << 23) | (mantissa << 18));
400 }
401}
402
Geoff Lang446a4472013-06-04 10:03:14 -0400403template <typename T>
404inline float normalizedToFloat(T input)
405{
Geoff Langd4475812015-03-18 10:53:05 -0400406 static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
Geoff Lang446a4472013-06-04 10:03:14 -0400407
408 const float inverseMax = 1.0f / std::numeric_limits<T>::max();
409 return input * inverseMax;
410}
411
412template <unsigned int inputBitCount, typename T>
413inline float normalizedToFloat(T input)
414{
Geoff Langd4475812015-03-18 10:53:05 -0400415 static_assert(std::numeric_limits<T>::is_integer, "T must be an integer.");
416 static_assert(inputBitCount < (sizeof(T) * 8), "T must have more bits than inputBitCount.");
Geoff Lang446a4472013-06-04 10:03:14 -0400417
418 const float inverseMax = 1.0f / ((1 << inputBitCount) - 1);
419 return input * inverseMax;
420}
421
422template <typename T>
423inline T floatToNormalized(float input)
424{
Minmin Gong794e0002015-04-07 18:31:54 -0700425 return static_cast<T>(std::numeric_limits<T>::max() * input + 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400426}
427
428template <unsigned int outputBitCount, typename T>
429inline T floatToNormalized(float input)
430{
Geoff Langd4475812015-03-18 10:53:05 -0400431 static_assert(outputBitCount < (sizeof(T) * 8), "T must have more bits than outputBitCount.");
Minmin Gong794e0002015-04-07 18:31:54 -0700432 return static_cast<T>(((1 << outputBitCount) - 1) * input + 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400433}
434
435template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
436inline T getShiftedData(T input)
437{
Geoff Langd4475812015-03-18 10:53:05 -0400438 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
439 "T must have at least as many bits as inputBitCount + inputBitStart.");
Geoff Lang446a4472013-06-04 10:03:14 -0400440 const T mask = (1 << inputBitCount) - 1;
441 return (input >> inputBitStart) & mask;
442}
443
444template <unsigned int inputBitCount, unsigned int inputBitStart, typename T>
445inline T shiftData(T input)
446{
Geoff Langd4475812015-03-18 10:53:05 -0400447 static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8),
448 "T must have at least as many bits as inputBitCount + inputBitStart.");
Geoff Lang446a4472013-06-04 10:03:14 -0400449 const T mask = (1 << inputBitCount) - 1;
450 return (input & mask) << inputBitStart;
451}
452
453
454inline unsigned char average(unsigned char a, unsigned char b)
455{
456 return ((a ^ b) >> 1) + (a & b);
457}
458
459inline signed char average(signed char a, signed char b)
460{
461 return ((short)a + (short)b) / 2;
462}
463
464inline unsigned short average(unsigned short a, unsigned short b)
465{
466 return ((a ^ b) >> 1) + (a & b);
467}
468
469inline signed short average(signed short a, signed short b)
470{
471 return ((int)a + (int)b) / 2;
472}
473
474inline unsigned int average(unsigned int a, unsigned int b)
475{
476 return ((a ^ b) >> 1) + (a & b);
477}
478
479inline signed int average(signed int a, signed int b)
480{
481 return ((long long)a + (long long)b) / 2;
482}
483
484inline float average(float a, float b)
485{
486 return (a + b) * 0.5f;
487}
488
489inline unsigned short averageHalfFloat(unsigned short a, unsigned short b)
490{
491 return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f);
492}
493
494inline unsigned int averageFloat11(unsigned int a, unsigned int b)
495{
Minmin Gong794e0002015-04-07 18:31:54 -0700496 return float32ToFloat11((float11ToFloat32(static_cast<unsigned short>(a)) + float11ToFloat32(static_cast<unsigned short>(b))) * 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400497}
498
499inline unsigned int averageFloat10(unsigned int a, unsigned int b)
500{
Minmin Gong794e0002015-04-07 18:31:54 -0700501 return float32ToFloat10((float10ToFloat32(static_cast<unsigned short>(a)) + float10ToFloat32(static_cast<unsigned short>(b))) * 0.5f);
Geoff Lang446a4472013-06-04 10:03:14 -0400502}
503
Corentin Wallezac6ff932014-11-12 06:16:53 -0800504// Represents intervals of the type [a, b)
Jamie Madill39b43462014-08-18 16:39:50 -0400505template <typename T>
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000506struct Range
507{
508 Range() {}
Jamie Madillcc002392014-09-09 10:21:56 -0400509 Range(T lo, T hi) : start(lo), end(hi) { ASSERT(lo <= hi); }
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000510
Jamie Madill39b43462014-08-18 16:39:50 -0400511 T start;
512 T end;
513
Jamie Madillcc002392014-09-09 10:21:56 -0400514 T length() const { return end - start; }
Corentin Wallezac6ff932014-11-12 06:16:53 -0800515
516 bool intersects(Range<T> other)
517 {
518 if (start <= other.start)
519 {
520 return other.start < end;
521 }
522 else
523 {
524 return start < other.end;
525 }
526 }
Jamie Madillaa9e9972015-06-22 13:57:16 -0400527
528 void extend(T value)
529 {
530 start = value > start ? value : start;
531 end = value < end ? value : end;
532 }
533
534 bool empty() const
535 {
536 return end <= start;
537 }
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000538};
539
Jamie Madill39b43462014-08-18 16:39:50 -0400540typedef Range<int> RangeI;
541typedef Range<unsigned int> RangeUI;
542
Geoff Lang831b1952015-05-05 11:02:27 -0400543}
544
545namespace rx
546{
547
shannonwoods@chromium.org06d4e842013-05-30 00:04:42 +0000548template <typename T>
549T roundUp(const T value, const T alignment)
550{
551 return value + alignment - 1 - (value - 1) % alignment;
552}
553
Jamie Madillaff80842014-08-04 10:47:56 -0400554inline unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor)
555{
556 unsigned int divided = value / divisor;
557 return (divided + ((value % divisor == 0) ? 0 : 1));
558}
559
Jamie Madill4f1a8632013-09-24 10:13:04 -0400560template <class T>
561inline bool IsUnsignedAdditionSafe(T lhs, T rhs)
562{
Geoff Langd4475812015-03-18 10:53:05 -0400563 static_assert(!std::numeric_limits<T>::is_signed, "T must be unsigned.");
Jamie Madill4f1a8632013-09-24 10:13:04 -0400564 return (rhs <= std::numeric_limits<T>::max() - lhs);
565}
566
567template <class T>
568inline bool IsUnsignedMultiplicationSafe(T lhs, T rhs)
569{
Geoff Langd4475812015-03-18 10:53:05 -0400570 static_assert(!std::numeric_limits<T>::is_signed, "T must be unsigned.");
Jamie Madill4f1a8632013-09-24 10:13:04 -0400571 return (lhs == T(0) || rhs == T(0) || (rhs <= std::numeric_limits<T>::max() / lhs));
572}
573
Jamie Madill1b2a8f92014-05-14 13:09:39 -0400574template <class SmallIntT, class BigIntT>
575inline bool IsIntegerCastSafe(BigIntT bigValue)
576{
577 return (static_cast<BigIntT>(static_cast<SmallIntT>(bigValue)) == bigValue);
578}
579
Jamie Madill3b9bb722015-01-05 16:17:02 -0500580#if defined(_MSC_VER)
581
582#define ANGLE_ROTL(x,y) _rotl(x,y)
Austin Kinross405f3472015-06-04 13:09:06 -0700583#define ANGLE_ROTR16(x,y) _rotr16(x,y)
Jamie Madill3b9bb722015-01-05 16:17:02 -0500584
585#else
586
587inline uint32_t RotL(uint32_t x, int8_t r)
588{
589 return (x << r) | (x >> (32 - r));
590}
591
Austin Kinross405f3472015-06-04 13:09:06 -0700592inline uint16_t RotR16(uint16_t x, int8_t r)
593{
594 return (x >> r) | (x << (16 - r));
595}
596
Jamie Madill3b9bb722015-01-05 16:17:02 -0500597#define ANGLE_ROTL(x,y) RotL(x,y)
Austin Kinross405f3472015-06-04 13:09:06 -0700598#define ANGLE_ROTR16(x,y) RotR16(x,y)
Jamie Madill3b9bb722015-01-05 16:17:02 -0500599
600#endif // namespace rx
601
shannon.woods@transgaming.comb3560252013-02-28 23:07:04 +0000602}
603
Geoff Lang0a73dd82014-11-19 16:18:08 -0500604#endif // COMMON_MATHUTIL_H_