daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
shannonwoods@chromium.org | 92b9cd5 | 2013-05-30 00:14:48 +0000 | [diff] [blame] | 2 | // Copyright (c) 2002-2013 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // mathutil.h: Math and bit manipulation functions. |
| 8 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 9 | #ifndef COMMON_MATHUTIL_H_ |
| 10 | #define COMMON_MATHUTIL_H_ |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 11 | |
Jamie Madill | b155bbc | 2014-01-13 09:51:03 -0500 | [diff] [blame] | 12 | #include <limits> |
| 13 | #include <algorithm> |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 14 | #include <math.h> |
Jamie Madill | 4f26786 | 2014-04-17 15:53:37 -0400 | [diff] [blame] | 15 | #include <string.h> |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 16 | #include <stdint.h> |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 17 | #include <stdlib.h> |
Jamie Madill | b155bbc | 2014-01-13 09:51:03 -0500 | [diff] [blame] | 18 | |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 19 | #include <base/numerics/safe_math.h> |
| 20 | |
| 21 | #include "common/debug.h" |
| 22 | #include "common/platform.h" |
| 23 | |
| 24 | namespace angle |
| 25 | { |
| 26 | using base::CheckedNumeric; |
| 27 | using base::IsValueInRangeForNumericType; |
| 28 | } |
| 29 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 30 | namespace gl |
| 31 | { |
shannonwoods@chromium.org | 49ec992 | 2013-05-30 00:16:38 +0000 | [diff] [blame] | 32 | |
| 33 | const unsigned int Float32One = 0x3F800000; |
| 34 | const unsigned short Float16One = 0x3C00; |
| 35 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 36 | struct 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.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 47 | inline bool isPow2(int x) |
| 48 | { |
| 49 | return (x & (x - 1)) == 0 && (x != 0); |
| 50 | } |
| 51 | |
| 52 | inline int log2(int x) |
| 53 | { |
| 54 | int r = 0; |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 55 | while ((x >> r) > 1) r++; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 56 | return r; |
| 57 | } |
| 58 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 59 | inline 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 Madill | b155bbc | 2014-01-13 09:51:03 -0500 | [diff] [blame] | 72 | inline 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 Madill | 70656a6 | 2014-03-05 15:01:26 -0500 | [diff] [blame] | 77 | template <typename DestT, typename SrcT> |
| 78 | inline DestT clampCast(SrcT value) |
| 79 | { |
Corentin Wallez | 630d85c | 2015-09-30 14:35:48 -0700 | [diff] [blame] | 80 | 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 Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 84 | |
Corentin Wallez | 630d85c | 2015-09-30 14:35:48 -0700 | [diff] [blame] | 85 | // 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 Madill | 70656a6 | 2014-03-05 15:01:26 -0500 | [diff] [blame] | 103 | } |
| 104 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 105 | template<typename T, typename MIN, typename MAX> |
| 106 | inline T clamp(T x, MIN min, MAX max) |
| 107 | { |
shannon.woods@transgaming.com | 31c4f23 | 2013-02-28 23:14:18 +0000 | [diff] [blame] | 108 | // Since NaNs fail all comparison tests, a NaN value will default to min |
| 109 | return x > min ? (x > max ? max : x) : min; |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 110 | } |
| 111 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 112 | inline float clamp01(float x) |
| 113 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 114 | return clamp(x, 0.0f, 1.0f); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | template<const int n> |
| 118 | inline unsigned int unorm(float x) |
| 119 | { |
| 120 | const unsigned int max = 0xFFFFFFFF >> (32 - n); |
| 121 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 122 | if (x > 1) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 123 | { |
| 124 | return max; |
| 125 | } |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 126 | else if (x < 0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 127 | { |
| 128 | return 0; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | return (unsigned int)(max * x + 0.5f); |
| 133 | } |
| 134 | } |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 135 | |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 136 | inline bool supportsSSE2() |
| 137 | { |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 138 | #if defined(ANGLE_USE_SSE) |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 139 | static bool checked = false; |
| 140 | static bool supports = false; |
| 141 | |
| 142 | if (checked) |
| 143 | { |
| 144 | return supports; |
| 145 | } |
| 146 | |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 147 | #if defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 148 | { |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 149 | int info[4]; |
| 150 | __cpuid(info, 0); |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 151 | |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 152 | if (info[0] >= 1) |
| 153 | { |
| 154 | __cpuid(info, 1); |
| 155 | |
| 156 | supports = (info[3] >> 26) & 1; |
| 157 | } |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 158 | } |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 159 | #endif // defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 160 | checked = true; |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 161 | return supports; |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 162 | #else // defined(ANGLE_USE_SSE) |
Geoff Lang | 8321779 | 2014-01-16 09:52:38 -0500 | [diff] [blame] | 163 | return false; |
| 164 | #endif |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 165 | } |
apatrick@chromium.org | aa48067 | 2012-09-05 19:32:38 +0000 | [diff] [blame] | 166 | |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 167 | template <typename destType, typename sourceType> |
| 168 | destType bitCast(const sourceType &source) |
| 169 | { |
Jamie Madill | 7ab02fa | 2014-02-04 16:04:08 -0500 | [diff] [blame] | 170 | size_t copySize = std::min(sizeof(destType), sizeof(sourceType)); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 171 | destType output; |
Jamie Madill | 7ab02fa | 2014-02-04 16:04:08 -0500 | [diff] [blame] | 172 | memcpy(&output, &source, copySize); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 173 | return output; |
| 174 | } |
| 175 | |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 176 | inline unsigned short float32ToFloat16(float fp32) |
| 177 | { |
hendrikw | 7578262 | 2015-09-25 11:28:50 -0700 | [diff] [blame] | 178 | unsigned int fp32i = bitCast<unsigned int>(fp32); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 179 | unsigned int sign = (fp32i & 0x80000000) >> 16; |
| 180 | unsigned int abs = fp32i & 0x7FFFFFFF; |
| 181 | |
| 182 | if(abs > 0x47FFEFFF) // Infinity |
| 183 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 184 | return static_cast<unsigned short>(sign | 0x7FFF); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 185 | } |
| 186 | else if(abs < 0x38800000) // Denormal |
| 187 | { |
shannonwoods@chromium.org | a2ecfcc | 2013-05-30 00:11:59 +0000 | [diff] [blame] | 188 | unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000; |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 189 | 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 Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 200 | return static_cast<unsigned short>(sign | (abs + 0x00000FFF + ((abs >> 13) & 1)) >> 13); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 201 | } |
| 202 | else |
| 203 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 204 | return static_cast<unsigned short>(sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
apatrick@chromium.org | aa48067 | 2012-09-05 19:32:38 +0000 | [diff] [blame] | 208 | float float16ToFloat32(unsigned short h); |
| 209 | |
shannonwoods@chromium.org | 92b9cd5 | 2013-05-30 00:14:48 +0000 | [diff] [blame] | 210 | unsigned int convertRGBFloatsTo999E5(float red, float green, float blue); |
| 211 | void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue); |
| 212 | |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 213 | inline 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 | |
| 282 | inline 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 | |
| 351 | inline 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 Kinross | b8af723 | 2015-03-16 22:33:25 -0700 | [diff] [blame] | 383 | exponent = static_cast<unsigned short>(-112); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | return bitCast<float>(((exponent + 112) << 23) | (mantissa << 17)); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | inline 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 Kinross | b8af723 | 2015-03-16 22:33:25 -0700 | [diff] [blame] | 422 | exponent = static_cast<unsigned short>(-112); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | return bitCast<float>(((exponent + 112) << 23) | (mantissa << 18)); |
| 426 | } |
| 427 | } |
| 428 | |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 429 | template <typename T> |
| 430 | inline float normalizedToFloat(T input) |
| 431 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 432 | static_assert(std::numeric_limits<T>::is_integer, "T must be an integer."); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 433 | |
| 434 | const float inverseMax = 1.0f / std::numeric_limits<T>::max(); |
| 435 | return input * inverseMax; |
| 436 | } |
| 437 | |
| 438 | template <unsigned int inputBitCount, typename T> |
| 439 | inline float normalizedToFloat(T input) |
| 440 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 441 | 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 Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 443 | |
| 444 | const float inverseMax = 1.0f / ((1 << inputBitCount) - 1); |
| 445 | return input * inverseMax; |
| 446 | } |
| 447 | |
| 448 | template <typename T> |
| 449 | inline T floatToNormalized(float input) |
| 450 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 451 | return static_cast<T>(std::numeric_limits<T>::max() * input + 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | template <unsigned int outputBitCount, typename T> |
| 455 | inline T floatToNormalized(float input) |
| 456 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 457 | static_assert(outputBitCount < (sizeof(T) * 8), "T must have more bits than outputBitCount."); |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 458 | return static_cast<T>(((1 << outputBitCount) - 1) * input + 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> |
| 462 | inline T getShiftedData(T input) |
| 463 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 464 | static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8), |
| 465 | "T must have at least as many bits as inputBitCount + inputBitStart."); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 466 | const T mask = (1 << inputBitCount) - 1; |
| 467 | return (input >> inputBitStart) & mask; |
| 468 | } |
| 469 | |
| 470 | template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> |
| 471 | inline T shiftData(T input) |
| 472 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 473 | static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8), |
| 474 | "T must have at least as many bits as inputBitCount + inputBitStart."); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 475 | const T mask = (1 << inputBitCount) - 1; |
| 476 | return (input & mask) << inputBitStart; |
| 477 | } |
| 478 | |
Olli Etuaho | 0f2b156 | 2016-05-13 16:15:35 +0300 | [diff] [blame] | 479 | inline 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 Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 516 | |
| 517 | inline unsigned char average(unsigned char a, unsigned char b) |
| 518 | { |
| 519 | return ((a ^ b) >> 1) + (a & b); |
| 520 | } |
| 521 | |
| 522 | inline signed char average(signed char a, signed char b) |
| 523 | { |
| 524 | return ((short)a + (short)b) / 2; |
| 525 | } |
| 526 | |
| 527 | inline unsigned short average(unsigned short a, unsigned short b) |
| 528 | { |
| 529 | return ((a ^ b) >> 1) + (a & b); |
| 530 | } |
| 531 | |
| 532 | inline signed short average(signed short a, signed short b) |
| 533 | { |
| 534 | return ((int)a + (int)b) / 2; |
| 535 | } |
| 536 | |
| 537 | inline unsigned int average(unsigned int a, unsigned int b) |
| 538 | { |
| 539 | return ((a ^ b) >> 1) + (a & b); |
| 540 | } |
| 541 | |
Corentin Wallez | ca311dd | 2015-12-07 15:07:48 -0500 | [diff] [blame] | 542 | inline int average(int a, int b) |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 543 | { |
Corentin Wallez | ca311dd | 2015-12-07 15:07:48 -0500 | [diff] [blame] | 544 | long long average = (static_cast<long long>(a) + static_cast<long long>(b)) / 2ll; |
| 545 | return static_cast<int>(average); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | inline float average(float a, float b) |
| 549 | { |
| 550 | return (a + b) * 0.5f; |
| 551 | } |
| 552 | |
| 553 | inline unsigned short averageHalfFloat(unsigned short a, unsigned short b) |
| 554 | { |
| 555 | return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f); |
| 556 | } |
| 557 | |
| 558 | inline unsigned int averageFloat11(unsigned int a, unsigned int b) |
| 559 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 560 | return float32ToFloat11((float11ToFloat32(static_cast<unsigned short>(a)) + float11ToFloat32(static_cast<unsigned short>(b))) * 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | inline unsigned int averageFloat10(unsigned int a, unsigned int b) |
| 564 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 565 | return float32ToFloat10((float10ToFloat32(static_cast<unsigned short>(a)) + float10ToFloat32(static_cast<unsigned short>(b))) * 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 566 | } |
| 567 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 568 | template <typename T> |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 569 | struct Range |
| 570 | { |
| 571 | Range() {} |
Jamie Madill | cc00239 | 2014-09-09 10:21:56 -0400 | [diff] [blame] | 572 | Range(T lo, T hi) : start(lo), end(hi) { ASSERT(lo <= hi); } |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 573 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 574 | T start; |
| 575 | T end; |
| 576 | |
Jamie Madill | cc00239 | 2014-09-09 10:21:56 -0400 | [diff] [blame] | 577 | T length() const { return end - start; } |
Corentin Wallez | ac6ff93 | 2014-11-12 06:16:53 -0800 | [diff] [blame] | 578 | |
| 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 Madill | aa9e997 | 2015-06-22 13:57:16 -0400 | [diff] [blame] | 590 | |
| 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.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 601 | }; |
| 602 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 603 | typedef Range<int> RangeI; |
| 604 | typedef Range<unsigned int> RangeUI; |
| 605 | |
Geoff Lang | 3edfe03 | 2015-09-04 16:38:24 -0400 | [diff] [blame] | 606 | struct 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 Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 626 | // 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) |
| 632 | inline uint32_t packSnorm2x16(float f1, float f2) |
| 633 | { |
Yuly Novikov | 451ed25 | 2016-01-21 21:16:45 -0500 | [diff] [blame] | 634 | 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 Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 638 | } |
| 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) |
| 646 | inline 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) |
| 660 | inline 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 |
| 673 | inline 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. |
| 686 | inline 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. |
| 698 | inline 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 Patole | 551279e | 2015-07-07 18:18:23 +0530 | [diff] [blame] | 707 | // Returns whether the argument is Not a Number. |
| 708 | // IEEE 754 single precision NaN representation: Exponent(8 bits) - 255, Mantissa(23 bits) - non-zero. |
| 709 | inline 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. |
| 718 | inline 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änen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 725 | namespace priv |
| 726 | { |
| 727 | template <unsigned int N, unsigned int R> |
| 728 | struct 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 | |
| 742 | template <unsigned int N> |
| 743 | struct iSquareRoot<N, N> |
| 744 | { |
| 745 | enum result |
| 746 | { |
| 747 | value = N |
| 748 | }; |
| 749 | }; |
| 750 | |
| 751 | } // namespace priv |
| 752 | |
| 753 | template <unsigned int N> |
| 754 | constexpr unsigned int iSquareRoot() |
| 755 | { |
| 756 | return priv::iSquareRoot<N, 1>::value; |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 757 | } |
| 758 | |
Olli Etuaho | 7f9a55f | 2016-10-03 14:32:08 +0100 | [diff] [blame^] | 759 | // 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 | |
| 764 | template <typename T> |
| 765 | inline 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 | |
| 772 | template <typename T> |
| 773 | inline 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 | |
| 780 | inline 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änen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 793 | } // namespace gl |
| 794 | |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 795 | namespace rx |
| 796 | { |
| 797 | |
shannonwoods@chromium.org | 06d4e84 | 2013-05-30 00:04:42 +0000 | [diff] [blame] | 798 | template <typename T> |
| 799 | T roundUp(const T value, const T alignment) |
| 800 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 801 | auto temp = value + alignment - static_cast<T>(1); |
| 802 | return temp - temp % alignment; |
| 803 | } |
| 804 | |
| 805 | template <typename T> |
| 806 | angle::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.org | 06d4e84 | 2013-05-30 00:04:42 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Jamie Madill | aff8084 | 2014-08-04 10:47:56 -0400 | [diff] [blame] | 813 | inline 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 Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 819 | #if defined(_MSC_VER) |
| 820 | |
| 821 | #define ANGLE_ROTL(x,y) _rotl(x,y) |
Austin Kinross | 405f347 | 2015-06-04 13:09:06 -0700 | [diff] [blame] | 822 | #define ANGLE_ROTR16(x,y) _rotr16(x,y) |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 823 | |
| 824 | #else |
| 825 | |
| 826 | inline uint32_t RotL(uint32_t x, int8_t r) |
| 827 | { |
| 828 | return (x << r) | (x >> (32 - r)); |
| 829 | } |
| 830 | |
Austin Kinross | 405f347 | 2015-06-04 13:09:06 -0700 | [diff] [blame] | 831 | inline uint16_t RotR16(uint16_t x, int8_t r) |
| 832 | { |
| 833 | return (x >> r) | (x << (16 - r)); |
| 834 | } |
| 835 | |
Geoff Lang | 6e4cfce | 2016-06-13 15:06:31 -0400 | [diff] [blame] | 836 | #define ANGLE_ROTL(x, y) ::rx::RotL(x, y) |
| 837 | #define ANGLE_ROTR16(x, y) ::rx::RotR16(x, y) |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 838 | |
| 839 | #endif // namespace rx |
| 840 | |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 843 | #endif // COMMON_MATHUTIL_H_ |