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 | 5ea762a | 2017-06-07 14:59:51 -0400 | [diff] [blame^] | 19 | #include <anglebase/numerics/safe_math.h> |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 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 | |
Corentin Wallez | 0c7baf1 | 2016-12-19 15:43:10 -0500 | [diff] [blame] | 36 | template<typename T> |
| 37 | inline bool isPow2(T x) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 38 | { |
Corentin Wallez | 0c7baf1 | 2016-12-19 15:43:10 -0500 | [diff] [blame] | 39 | static_assert(std::is_integral<T>::value, "isPow2 must be called on an integer type."); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 40 | return (x & (x - 1)) == 0 && (x != 0); |
| 41 | } |
| 42 | |
| 43 | inline int log2(int x) |
| 44 | { |
| 45 | int r = 0; |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 46 | while ((x >> r) > 1) r++; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 47 | return r; |
| 48 | } |
| 49 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 50 | inline unsigned int ceilPow2(unsigned int x) |
| 51 | { |
| 52 | if (x != 0) x--; |
| 53 | x |= x >> 1; |
| 54 | x |= x >> 2; |
| 55 | x |= x >> 4; |
| 56 | x |= x >> 8; |
| 57 | x |= x >> 16; |
| 58 | x++; |
| 59 | |
| 60 | return x; |
| 61 | } |
| 62 | |
Jamie Madill | b155bbc | 2014-01-13 09:51:03 -0500 | [diff] [blame] | 63 | inline int clampToInt(unsigned int x) |
| 64 | { |
| 65 | return static_cast<int>(std::min(x, static_cast<unsigned int>(std::numeric_limits<int>::max()))); |
| 66 | } |
| 67 | |
Jamie Madill | 70656a6 | 2014-03-05 15:01:26 -0500 | [diff] [blame] | 68 | template <typename DestT, typename SrcT> |
| 69 | inline DestT clampCast(SrcT value) |
| 70 | { |
Corentin Wallez | 630d85c | 2015-09-30 14:35:48 -0700 | [diff] [blame] | 71 | static const DestT destLo = std::numeric_limits<DestT>::min(); |
| 72 | static const DestT destHi = std::numeric_limits<DestT>::max(); |
| 73 | static const SrcT srcLo = static_cast<SrcT>(destLo); |
| 74 | static const SrcT srcHi = static_cast<SrcT>(destHi); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 75 | |
Corentin Wallez | 630d85c | 2015-09-30 14:35:48 -0700 | [diff] [blame] | 76 | // When value is outside of or equal to the limits for DestT we use the DestT limit directly. |
| 77 | // This avoids undefined behaviors due to loss of precision when converting from floats to |
| 78 | // integers: |
| 79 | // destHi for ints is 2147483647 but the closest float number is around 2147483648, so when |
| 80 | // doing a conversion from float to int we run into an UB because the float is outside of the |
| 81 | // range representable by the int. |
| 82 | if (value <= srcLo) |
| 83 | { |
| 84 | return destLo; |
| 85 | } |
| 86 | else if (value >= srcHi) |
| 87 | { |
| 88 | return destHi; |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | return static_cast<DestT>(value); |
| 93 | } |
Jamie Madill | 70656a6 | 2014-03-05 15:01:26 -0500 | [diff] [blame] | 94 | } |
| 95 | |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 96 | template<typename T, typename MIN, typename MAX> |
| 97 | inline T clamp(T x, MIN min, MAX max) |
| 98 | { |
shannon.woods@transgaming.com | 31c4f23 | 2013-02-28 23:14:18 +0000 | [diff] [blame] | 99 | // Since NaNs fail all comparison tests, a NaN value will default to min |
| 100 | return x > min ? (x > max ? max : x) : min; |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 101 | } |
| 102 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 103 | inline float clamp01(float x) |
| 104 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 105 | return clamp(x, 0.0f, 1.0f); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | template<const int n> |
| 109 | inline unsigned int unorm(float x) |
| 110 | { |
| 111 | const unsigned int max = 0xFFFFFFFF >> (32 - n); |
| 112 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 113 | if (x > 1) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 114 | { |
| 115 | return max; |
| 116 | } |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 117 | else if (x < 0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 118 | { |
| 119 | return 0; |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | return (unsigned int)(max * x + 0.5f); |
| 124 | } |
| 125 | } |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 126 | |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 127 | inline bool supportsSSE2() |
| 128 | { |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 129 | #if defined(ANGLE_USE_SSE) |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 130 | static bool checked = false; |
| 131 | static bool supports = false; |
| 132 | |
| 133 | if (checked) |
| 134 | { |
| 135 | return supports; |
| 136 | } |
| 137 | |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 138 | #if defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 139 | { |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 140 | int info[4]; |
| 141 | __cpuid(info, 0); |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 142 | |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 143 | if (info[0] >= 1) |
| 144 | { |
| 145 | __cpuid(info, 1); |
| 146 | |
| 147 | supports = (info[3] >> 26) & 1; |
| 148 | } |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 149 | } |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 150 | #endif // defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 151 | checked = true; |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 152 | return supports; |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 153 | #else // defined(ANGLE_USE_SSE) |
Geoff Lang | 8321779 | 2014-01-16 09:52:38 -0500 | [diff] [blame] | 154 | return false; |
| 155 | #endif |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 156 | } |
apatrick@chromium.org | aa48067 | 2012-09-05 19:32:38 +0000 | [diff] [blame] | 157 | |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 158 | template <typename destType, typename sourceType> |
| 159 | destType bitCast(const sourceType &source) |
| 160 | { |
Jamie Madill | 7ab02fa | 2014-02-04 16:04:08 -0500 | [diff] [blame] | 161 | size_t copySize = std::min(sizeof(destType), sizeof(sourceType)); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 162 | destType output; |
Jamie Madill | 7ab02fa | 2014-02-04 16:04:08 -0500 | [diff] [blame] | 163 | memcpy(&output, &source, copySize); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 164 | return output; |
| 165 | } |
| 166 | |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 167 | inline unsigned short float32ToFloat16(float fp32) |
| 168 | { |
hendrikw | 7578262 | 2015-09-25 11:28:50 -0700 | [diff] [blame] | 169 | unsigned int fp32i = bitCast<unsigned int>(fp32); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 170 | unsigned int sign = (fp32i & 0x80000000) >> 16; |
| 171 | unsigned int abs = fp32i & 0x7FFFFFFF; |
| 172 | |
| 173 | if(abs > 0x47FFEFFF) // Infinity |
| 174 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 175 | return static_cast<unsigned short>(sign | 0x7FFF); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 176 | } |
| 177 | else if(abs < 0x38800000) // Denormal |
| 178 | { |
shannonwoods@chromium.org | a2ecfcc | 2013-05-30 00:11:59 +0000 | [diff] [blame] | 179 | unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000; |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 180 | int e = 113 - (abs >> 23); |
| 181 | |
| 182 | if(e < 24) |
| 183 | { |
| 184 | abs = mantissa >> e; |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | abs = 0; |
| 189 | } |
| 190 | |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 191 | 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] | 192 | } |
| 193 | else |
| 194 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 195 | 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] | 196 | } |
| 197 | } |
| 198 | |
apatrick@chromium.org | aa48067 | 2012-09-05 19:32:38 +0000 | [diff] [blame] | 199 | float float16ToFloat32(unsigned short h); |
| 200 | |
shannonwoods@chromium.org | 92b9cd5 | 2013-05-30 00:14:48 +0000 | [diff] [blame] | 201 | unsigned int convertRGBFloatsTo999E5(float red, float green, float blue); |
| 202 | void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue); |
| 203 | |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 204 | inline unsigned short float32ToFloat11(float fp32) |
| 205 | { |
| 206 | const unsigned int float32MantissaMask = 0x7FFFFF; |
| 207 | const unsigned int float32ExponentMask = 0x7F800000; |
| 208 | const unsigned int float32SignMask = 0x80000000; |
| 209 | const unsigned int float32ValueMask = ~float32SignMask; |
| 210 | const unsigned int float32ExponentFirstBit = 23; |
| 211 | const unsigned int float32ExponentBias = 127; |
| 212 | |
| 213 | const unsigned short float11Max = 0x7BF; |
| 214 | const unsigned short float11MantissaMask = 0x3F; |
| 215 | const unsigned short float11ExponentMask = 0x7C0; |
| 216 | const unsigned short float11BitMask = 0x7FF; |
| 217 | const unsigned int float11ExponentBias = 14; |
| 218 | |
| 219 | const unsigned int float32Maxfloat11 = 0x477E0000; |
| 220 | const unsigned int float32Minfloat11 = 0x38800000; |
| 221 | |
| 222 | const unsigned int float32Bits = bitCast<unsigned int>(fp32); |
| 223 | const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask; |
| 224 | |
| 225 | unsigned int float32Val = float32Bits & float32ValueMask; |
| 226 | |
| 227 | if ((float32Val & float32ExponentMask) == float32ExponentMask) |
| 228 | { |
| 229 | // INF or NAN |
| 230 | if ((float32Val & float32MantissaMask) != 0) |
| 231 | { |
| 232 | return float11ExponentMask | (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) & float11MantissaMask); |
| 233 | } |
| 234 | else if (float32Sign) |
| 235 | { |
| 236 | // -INF is clamped to 0 since float11 is positive only |
| 237 | return 0; |
| 238 | } |
| 239 | else |
| 240 | { |
| 241 | return float11ExponentMask; |
| 242 | } |
| 243 | } |
| 244 | else if (float32Sign) |
| 245 | { |
| 246 | // float11 is positive only, so clamp to zero |
| 247 | return 0; |
| 248 | } |
| 249 | else if (float32Val > float32Maxfloat11) |
| 250 | { |
| 251 | // The number is too large to be represented as a float11, set to max |
| 252 | return float11Max; |
| 253 | } |
| 254 | else |
| 255 | { |
| 256 | if (float32Val < float32Minfloat11) |
| 257 | { |
| 258 | // The number is too small to be represented as a normalized float11 |
| 259 | // Convert it to a denormalized value. |
| 260 | const unsigned int shift = (float32ExponentBias - float11ExponentBias) - (float32Val >> float32ExponentFirstBit); |
| 261 | float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift; |
| 262 | } |
| 263 | else |
| 264 | { |
| 265 | // Rebias the exponent to represent the value as a normalized float11 |
| 266 | float32Val += 0xC8000000; |
| 267 | } |
| 268 | |
| 269 | return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | inline unsigned short float32ToFloat10(float fp32) |
| 274 | { |
| 275 | const unsigned int float32MantissaMask = 0x7FFFFF; |
| 276 | const unsigned int float32ExponentMask = 0x7F800000; |
| 277 | const unsigned int float32SignMask = 0x80000000; |
| 278 | const unsigned int float32ValueMask = ~float32SignMask; |
| 279 | const unsigned int float32ExponentFirstBit = 23; |
| 280 | const unsigned int float32ExponentBias = 127; |
| 281 | |
| 282 | const unsigned short float10Max = 0x3DF; |
| 283 | const unsigned short float10MantissaMask = 0x1F; |
| 284 | const unsigned short float10ExponentMask = 0x3E0; |
| 285 | const unsigned short float10BitMask = 0x3FF; |
| 286 | const unsigned int float10ExponentBias = 14; |
| 287 | |
| 288 | const unsigned int float32Maxfloat10 = 0x477C0000; |
| 289 | const unsigned int float32Minfloat10 = 0x38800000; |
| 290 | |
| 291 | const unsigned int float32Bits = bitCast<unsigned int>(fp32); |
| 292 | const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask; |
| 293 | |
| 294 | unsigned int float32Val = float32Bits & float32ValueMask; |
| 295 | |
| 296 | if ((float32Val & float32ExponentMask) == float32ExponentMask) |
| 297 | { |
| 298 | // INF or NAN |
| 299 | if ((float32Val & float32MantissaMask) != 0) |
| 300 | { |
| 301 | return float10ExponentMask | (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) & float10MantissaMask); |
| 302 | } |
| 303 | else if (float32Sign) |
| 304 | { |
| 305 | // -INF is clamped to 0 since float11 is positive only |
| 306 | return 0; |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | return float10ExponentMask; |
| 311 | } |
| 312 | } |
| 313 | else if (float32Sign) |
| 314 | { |
| 315 | // float10 is positive only, so clamp to zero |
| 316 | return 0; |
| 317 | } |
| 318 | else if (float32Val > float32Maxfloat10) |
| 319 | { |
| 320 | // The number is too large to be represented as a float11, set to max |
| 321 | return float10Max; |
| 322 | } |
| 323 | else |
| 324 | { |
| 325 | if (float32Val < float32Minfloat10) |
| 326 | { |
| 327 | // The number is too small to be represented as a normalized float11 |
| 328 | // Convert it to a denormalized value. |
| 329 | const unsigned int shift = (float32ExponentBias - float10ExponentBias) - (float32Val >> float32ExponentFirstBit); |
| 330 | float32Val = ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift; |
| 331 | } |
| 332 | else |
| 333 | { |
| 334 | // Rebias the exponent to represent the value as a normalized float11 |
| 335 | float32Val += 0xC8000000; |
| 336 | } |
| 337 | |
| 338 | return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | inline float float11ToFloat32(unsigned short fp11) |
| 343 | { |
| 344 | unsigned short exponent = (fp11 >> 6) & 0x1F; |
| 345 | unsigned short mantissa = fp11 & 0x3F; |
| 346 | |
| 347 | if (exponent == 0x1F) |
| 348 | { |
| 349 | // INF or NAN |
| 350 | return bitCast<float>(0x7f800000 | (mantissa << 17)); |
| 351 | } |
| 352 | else |
| 353 | { |
| 354 | if (exponent != 0) |
| 355 | { |
| 356 | // normalized |
| 357 | } |
| 358 | else if (mantissa != 0) |
| 359 | { |
| 360 | // The value is denormalized |
| 361 | exponent = 1; |
| 362 | |
| 363 | do |
| 364 | { |
| 365 | exponent--; |
| 366 | mantissa <<= 1; |
| 367 | } |
| 368 | while ((mantissa & 0x40) == 0); |
| 369 | |
| 370 | mantissa = mantissa & 0x3F; |
| 371 | } |
| 372 | else // The value is zero |
| 373 | { |
Austin Kinross | b8af723 | 2015-03-16 22:33:25 -0700 | [diff] [blame] | 374 | exponent = static_cast<unsigned short>(-112); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | return bitCast<float>(((exponent + 112) << 23) | (mantissa << 17)); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | inline float float10ToFloat32(unsigned short fp11) |
| 382 | { |
| 383 | unsigned short exponent = (fp11 >> 5) & 0x1F; |
| 384 | unsigned short mantissa = fp11 & 0x1F; |
| 385 | |
| 386 | if (exponent == 0x1F) |
| 387 | { |
| 388 | // INF or NAN |
| 389 | return bitCast<float>(0x7f800000 | (mantissa << 17)); |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | if (exponent != 0) |
| 394 | { |
| 395 | // normalized |
| 396 | } |
| 397 | else if (mantissa != 0) |
| 398 | { |
| 399 | // The value is denormalized |
| 400 | exponent = 1; |
| 401 | |
| 402 | do |
| 403 | { |
| 404 | exponent--; |
| 405 | mantissa <<= 1; |
| 406 | } |
| 407 | while ((mantissa & 0x20) == 0); |
| 408 | |
| 409 | mantissa = mantissa & 0x1F; |
| 410 | } |
| 411 | else // The value is zero |
| 412 | { |
Austin Kinross | b8af723 | 2015-03-16 22:33:25 -0700 | [diff] [blame] | 413 | exponent = static_cast<unsigned short>(-112); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | return bitCast<float>(((exponent + 112) << 23) | (mantissa << 18)); |
| 417 | } |
| 418 | } |
| 419 | |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 420 | template <typename T> |
| 421 | inline float normalizedToFloat(T input) |
| 422 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 423 | 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] | 424 | |
| 425 | const float inverseMax = 1.0f / std::numeric_limits<T>::max(); |
| 426 | return input * inverseMax; |
| 427 | } |
| 428 | |
| 429 | template <unsigned int inputBitCount, 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."); |
| 433 | 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] | 434 | |
| 435 | const float inverseMax = 1.0f / ((1 << inputBitCount) - 1); |
| 436 | return input * inverseMax; |
| 437 | } |
| 438 | |
| 439 | template <typename T> |
| 440 | inline T floatToNormalized(float input) |
| 441 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 442 | return static_cast<T>(std::numeric_limits<T>::max() * input + 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | template <unsigned int outputBitCount, typename T> |
| 446 | inline T floatToNormalized(float input) |
| 447 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 448 | 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] | 449 | return static_cast<T>(((1 << outputBitCount) - 1) * input + 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> |
| 453 | inline T getShiftedData(T input) |
| 454 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 455 | static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8), |
| 456 | "T must have at least as many bits as inputBitCount + inputBitStart."); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 457 | const T mask = (1 << inputBitCount) - 1; |
| 458 | return (input >> inputBitStart) & mask; |
| 459 | } |
| 460 | |
| 461 | template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> |
| 462 | inline T shiftData(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 & mask) << inputBitStart; |
| 468 | } |
| 469 | |
Olli Etuaho | 0f2b156 | 2016-05-13 16:15:35 +0300 | [diff] [blame] | 470 | inline unsigned int CountLeadingZeros(uint32_t x) |
| 471 | { |
| 472 | // Use binary search to find the amount of leading zeros. |
| 473 | unsigned int zeros = 32u; |
| 474 | uint32_t y; |
| 475 | |
| 476 | y = x >> 16u; |
| 477 | if (y != 0) |
| 478 | { |
| 479 | zeros = zeros - 16u; |
| 480 | x = y; |
| 481 | } |
| 482 | y = x >> 8u; |
| 483 | if (y != 0) |
| 484 | { |
| 485 | zeros = zeros - 8u; |
| 486 | x = y; |
| 487 | } |
| 488 | y = x >> 4u; |
| 489 | if (y != 0) |
| 490 | { |
| 491 | zeros = zeros - 4u; |
| 492 | x = y; |
| 493 | } |
| 494 | y = x >> 2u; |
| 495 | if (y != 0) |
| 496 | { |
| 497 | zeros = zeros - 2u; |
| 498 | x = y; |
| 499 | } |
| 500 | y = x >> 1u; |
| 501 | if (y != 0) |
| 502 | { |
| 503 | return zeros - 2u; |
| 504 | } |
| 505 | return zeros - x; |
| 506 | } |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 507 | |
| 508 | inline unsigned char average(unsigned char a, unsigned char b) |
| 509 | { |
| 510 | return ((a ^ b) >> 1) + (a & b); |
| 511 | } |
| 512 | |
| 513 | inline signed char average(signed char a, signed char b) |
| 514 | { |
| 515 | return ((short)a + (short)b) / 2; |
| 516 | } |
| 517 | |
| 518 | inline unsigned short average(unsigned short a, unsigned short b) |
| 519 | { |
| 520 | return ((a ^ b) >> 1) + (a & b); |
| 521 | } |
| 522 | |
| 523 | inline signed short average(signed short a, signed short b) |
| 524 | { |
| 525 | return ((int)a + (int)b) / 2; |
| 526 | } |
| 527 | |
| 528 | inline unsigned int average(unsigned int a, unsigned int b) |
| 529 | { |
| 530 | return ((a ^ b) >> 1) + (a & b); |
| 531 | } |
| 532 | |
Corentin Wallez | ca311dd | 2015-12-07 15:07:48 -0500 | [diff] [blame] | 533 | inline int average(int a, int b) |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 534 | { |
Corentin Wallez | ca311dd | 2015-12-07 15:07:48 -0500 | [diff] [blame] | 535 | long long average = (static_cast<long long>(a) + static_cast<long long>(b)) / 2ll; |
| 536 | return static_cast<int>(average); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | inline float average(float a, float b) |
| 540 | { |
| 541 | return (a + b) * 0.5f; |
| 542 | } |
| 543 | |
| 544 | inline unsigned short averageHalfFloat(unsigned short a, unsigned short b) |
| 545 | { |
| 546 | return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f); |
| 547 | } |
| 548 | |
| 549 | inline unsigned int averageFloat11(unsigned int a, unsigned int b) |
| 550 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 551 | 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] | 552 | } |
| 553 | |
| 554 | inline unsigned int averageFloat10(unsigned int a, unsigned int b) |
| 555 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 556 | 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] | 557 | } |
| 558 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 559 | template <typename T> |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 560 | struct Range |
| 561 | { |
| 562 | Range() {} |
Jamie Madill | cc00239 | 2014-09-09 10:21:56 -0400 | [diff] [blame] | 563 | 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] | 564 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 565 | T start; |
| 566 | T end; |
| 567 | |
Jamie Madill | cc00239 | 2014-09-09 10:21:56 -0400 | [diff] [blame] | 568 | T length() const { return end - start; } |
Corentin Wallez | ac6ff93 | 2014-11-12 06:16:53 -0800 | [diff] [blame] | 569 | |
| 570 | bool intersects(Range<T> other) |
| 571 | { |
| 572 | if (start <= other.start) |
| 573 | { |
| 574 | return other.start < end; |
| 575 | } |
| 576 | else |
| 577 | { |
| 578 | return start < other.end; |
| 579 | } |
| 580 | } |
Jamie Madill | aa9e997 | 2015-06-22 13:57:16 -0400 | [diff] [blame] | 581 | |
| 582 | void extend(T value) |
| 583 | { |
| 584 | start = value > start ? value : start; |
| 585 | end = value < end ? value : end; |
| 586 | } |
| 587 | |
| 588 | bool empty() const |
| 589 | { |
| 590 | return end <= start; |
| 591 | } |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 592 | }; |
| 593 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 594 | typedef Range<int> RangeI; |
| 595 | typedef Range<unsigned int> RangeUI; |
| 596 | |
Geoff Lang | 3edfe03 | 2015-09-04 16:38:24 -0400 | [diff] [blame] | 597 | struct IndexRange |
| 598 | { |
| 599 | IndexRange() : IndexRange(0, 0, 0) {} |
| 600 | IndexRange(size_t start_, size_t end_, size_t vertexIndexCount_) |
| 601 | : start(start_), end(end_), vertexIndexCount(vertexIndexCount_) |
| 602 | { |
| 603 | ASSERT(start <= end); |
| 604 | } |
| 605 | |
| 606 | // Number of vertices in the range. |
| 607 | size_t vertexCount() const { return (end - start) + 1; } |
| 608 | |
| 609 | // Inclusive range of indices that are not primitive restart |
| 610 | size_t start; |
| 611 | size_t end; |
| 612 | |
| 613 | // Number of non-primitive restart indices |
| 614 | size_t vertexIndexCount; |
| 615 | }; |
| 616 | |
Olli Etuaho | 74da73f | 2017-02-01 15:37:48 +0000 | [diff] [blame] | 617 | // Combine a floating-point value representing a mantissa (x) and an integer exponent (exp) into a |
| 618 | // floating-point value. As in GLSL ldexp() built-in. |
| 619 | inline float Ldexp(float x, int exp) |
| 620 | { |
| 621 | if (exp > 128) |
| 622 | { |
| 623 | return std::numeric_limits<float>::infinity(); |
| 624 | } |
| 625 | if (exp < -126) |
| 626 | { |
| 627 | return 0.0f; |
| 628 | } |
| 629 | double result = static_cast<double>(x) * std::pow(2.0, static_cast<double>(exp)); |
| 630 | return static_cast<float>(result); |
| 631 | } |
| 632 | |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 633 | // First, both normalized floating-point values are converted into 16-bit integer values. |
| 634 | // Then, the results are packed into the returned 32-bit unsigned integer. |
| 635 | // The first float value will be written to the least significant bits of the output; |
| 636 | // the last float value will be written to the most significant bits. |
| 637 | // The conversion of each value to fixed point is done as follows : |
| 638 | // packSnorm2x16 : round(clamp(c, -1, +1) * 32767.0) |
| 639 | inline uint32_t packSnorm2x16(float f1, float f2) |
| 640 | { |
Yuly Novikov | 451ed25 | 2016-01-21 21:16:45 -0500 | [diff] [blame] | 641 | int16_t leastSignificantBits = static_cast<int16_t>(roundf(clamp(f1, -1.0f, 1.0f) * 32767.0f)); |
| 642 | int16_t mostSignificantBits = static_cast<int16_t>(roundf(clamp(f2, -1.0f, 1.0f) * 32767.0f)); |
| 643 | return static_cast<uint32_t>(mostSignificantBits) << 16 | |
| 644 | (static_cast<uint32_t>(leastSignificantBits) & 0xFFFF); |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then, each |
| 648 | // component is converted to a normalized floating-point value to generate the returned two float values. |
| 649 | // The first float value will be extracted from the least significant bits of the input; |
| 650 | // the last float value will be extracted from the most-significant bits. |
| 651 | // The conversion for unpacked fixed-point value to floating point is done as follows: |
| 652 | // unpackSnorm2x16 : clamp(f / 32767.0, -1, +1) |
| 653 | inline void unpackSnorm2x16(uint32_t u, float *f1, float *f2) |
| 654 | { |
| 655 | int16_t leastSignificantBits = static_cast<int16_t>(u & 0xFFFF); |
| 656 | int16_t mostSignificantBits = static_cast<int16_t>(u >> 16); |
| 657 | *f1 = clamp(static_cast<float>(leastSignificantBits) / 32767.0f, -1.0f, 1.0f); |
| 658 | *f2 = clamp(static_cast<float>(mostSignificantBits) / 32767.0f, -1.0f, 1.0f); |
| 659 | } |
| 660 | |
| 661 | // First, both normalized floating-point values are converted into 16-bit integer values. |
| 662 | // Then, the results are packed into the returned 32-bit unsigned integer. |
| 663 | // The first float value will be written to the least significant bits of the output; |
| 664 | // the last float value will be written to the most significant bits. |
| 665 | // The conversion of each value to fixed point is done as follows: |
| 666 | // packUnorm2x16 : round(clamp(c, 0, +1) * 65535.0) |
| 667 | inline uint32_t packUnorm2x16(float f1, float f2) |
| 668 | { |
| 669 | uint16_t leastSignificantBits = static_cast<uint16_t>(roundf(clamp(f1, 0.0f, 1.0f) * 65535.0f)); |
| 670 | uint16_t mostSignificantBits = static_cast<uint16_t>(roundf(clamp(f2, 0.0f, 1.0f) * 65535.0f)); |
| 671 | return static_cast<uint32_t>(mostSignificantBits) << 16 | static_cast<uint32_t>(leastSignificantBits); |
| 672 | } |
| 673 | |
| 674 | // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then, each |
| 675 | // component is converted to a normalized floating-point value to generate the returned two float values. |
| 676 | // The first float value will be extracted from the least significant bits of the input; |
| 677 | // the last float value will be extracted from the most-significant bits. |
| 678 | // The conversion for unpacked fixed-point value to floating point is done as follows: |
| 679 | // unpackUnorm2x16 : f / 65535.0 |
| 680 | inline void unpackUnorm2x16(uint32_t u, float *f1, float *f2) |
| 681 | { |
| 682 | uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF); |
| 683 | uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16); |
| 684 | *f1 = static_cast<float>(leastSignificantBits) / 65535.0f; |
| 685 | *f2 = static_cast<float>(mostSignificantBits) / 65535.0f; |
| 686 | } |
| 687 | |
Olli Etuaho | 25aef45 | 2017-01-29 16:15:44 -0800 | [diff] [blame] | 688 | // Helper functions intended to be used only here. |
| 689 | namespace priv |
| 690 | { |
| 691 | |
| 692 | inline uint8_t ToPackedUnorm8(float f) |
| 693 | { |
| 694 | return static_cast<uint8_t>(roundf(clamp(f, 0.0f, 1.0f) * 255.0f)); |
| 695 | } |
| 696 | |
| 697 | inline int8_t ToPackedSnorm8(float f) |
| 698 | { |
| 699 | return static_cast<int8_t>(roundf(clamp(f, -1.0f, 1.0f) * 127.0f)); |
| 700 | } |
| 701 | |
| 702 | } // namespace priv |
| 703 | |
| 704 | // Packs 4 normalized unsigned floating-point values to a single 32-bit unsigned integer. Works |
| 705 | // similarly to packUnorm2x16. The floats are clamped to the range 0.0 to 1.0, and written to the |
| 706 | // unsigned integer starting from the least significant bits. |
| 707 | inline uint32_t PackUnorm4x8(float f1, float f2, float f3, float f4) |
| 708 | { |
| 709 | uint8_t bits[4]; |
| 710 | bits[0] = priv::ToPackedUnorm8(f1); |
| 711 | bits[1] = priv::ToPackedUnorm8(f2); |
| 712 | bits[2] = priv::ToPackedUnorm8(f3); |
| 713 | bits[3] = priv::ToPackedUnorm8(f4); |
| 714 | uint32_t result = 0u; |
| 715 | for (int i = 0; i < 4; ++i) |
| 716 | { |
| 717 | int shift = i * 8; |
| 718 | result |= (static_cast<uint32_t>(bits[i]) << shift); |
| 719 | } |
| 720 | return result; |
| 721 | } |
| 722 | |
| 723 | // Unpacks 4 normalized unsigned floating-point values from a single 32-bit unsigned integer into f. |
| 724 | // Works similarly to unpackUnorm2x16. The floats are unpacked starting from the least significant |
| 725 | // bits. |
| 726 | inline void UnpackUnorm4x8(uint32_t u, float *f) |
| 727 | { |
| 728 | for (int i = 0; i < 4; ++i) |
| 729 | { |
| 730 | int shift = i * 8; |
| 731 | uint8_t bits = static_cast<uint8_t>((u >> shift) & 0xFF); |
| 732 | f[i] = static_cast<float>(bits) / 255.0f; |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | // Packs 4 normalized signed floating-point values to a single 32-bit unsigned integer. The floats |
| 737 | // are clamped to the range -1.0 to 1.0, and written to the unsigned integer starting from the least |
| 738 | // significant bits. |
| 739 | inline uint32_t PackSnorm4x8(float f1, float f2, float f3, float f4) |
| 740 | { |
| 741 | int8_t bits[4]; |
| 742 | bits[0] = priv::ToPackedSnorm8(f1); |
| 743 | bits[1] = priv::ToPackedSnorm8(f2); |
| 744 | bits[2] = priv::ToPackedSnorm8(f3); |
| 745 | bits[3] = priv::ToPackedSnorm8(f4); |
| 746 | uint32_t result = 0u; |
| 747 | for (int i = 0; i < 4; ++i) |
| 748 | { |
| 749 | int shift = i * 8; |
| 750 | result |= ((static_cast<uint32_t>(bits[i]) & 0xFF) << shift); |
| 751 | } |
| 752 | return result; |
| 753 | } |
| 754 | |
| 755 | // Unpacks 4 normalized signed floating-point values from a single 32-bit unsigned integer into f. |
| 756 | // Works similarly to unpackSnorm2x16. The floats are unpacked starting from the least significant |
| 757 | // bits, and clamped to the range -1.0 to 1.0. |
| 758 | inline void UnpackSnorm4x8(uint32_t u, float *f) |
| 759 | { |
| 760 | for (int i = 0; i < 4; ++i) |
| 761 | { |
| 762 | int shift = i * 8; |
| 763 | int8_t bits = static_cast<int8_t>((u >> shift) & 0xFF); |
| 764 | f[i] = clamp(static_cast<float>(bits) / 127.0f, -1.0f, 1.0f); |
| 765 | } |
| 766 | } |
| 767 | |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 768 | // Returns an unsigned integer obtained by converting the two floating-point values to the 16-bit |
| 769 | // floating-point representation found in the OpenGL ES Specification, and then packing these |
| 770 | // two 16-bit integers into a 32-bit unsigned integer. |
| 771 | // f1: The 16 least-significant bits of the result; |
| 772 | // f2: The 16 most-significant bits. |
| 773 | inline uint32_t packHalf2x16(float f1, float f2) |
| 774 | { |
| 775 | uint16_t leastSignificantBits = static_cast<uint16_t>(float32ToFloat16(f1)); |
| 776 | uint16_t mostSignificantBits = static_cast<uint16_t>(float32ToFloat16(f2)); |
| 777 | return static_cast<uint32_t>(mostSignificantBits) << 16 | static_cast<uint32_t>(leastSignificantBits); |
| 778 | } |
| 779 | |
| 780 | // Returns two floating-point values obtained by unpacking a 32-bit unsigned integer into a pair of 16-bit values, |
| 781 | // interpreting those values as 16-bit floating-point numbers according to the OpenGL ES Specification, |
| 782 | // and converting them to 32-bit floating-point values. |
| 783 | // The first float value is obtained from the 16 least-significant bits of u; |
| 784 | // the second component is obtained from the 16 most-significant bits of u. |
| 785 | inline void unpackHalf2x16(uint32_t u, float *f1, float *f2) |
| 786 | { |
| 787 | uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF); |
| 788 | uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16); |
| 789 | |
| 790 | *f1 = float16ToFloat32(leastSignificantBits); |
| 791 | *f2 = float16ToFloat32(mostSignificantBits); |
| 792 | } |
| 793 | |
Geoff Lang | 451b2b7 | 2017-05-15 15:32:02 -0400 | [diff] [blame] | 794 | inline uint8_t sRGBToLinear(uint8_t srgbValue) |
| 795 | { |
| 796 | float value = srgbValue / 255.0f; |
| 797 | if (value <= 0.04045f) |
| 798 | { |
| 799 | value = value / 12.92f; |
| 800 | } |
| 801 | else |
| 802 | { |
| 803 | value = std::pow((value + 0.055f) / 1.055f, 2.4f); |
| 804 | } |
| 805 | return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f)); |
| 806 | } |
| 807 | |
| 808 | inline uint8_t linearToSRGB(uint8_t linearValue) |
| 809 | { |
| 810 | float value = linearValue / 255.0f; |
| 811 | if (value <= 0.0f) |
| 812 | { |
| 813 | value = 0.0f; |
| 814 | } |
| 815 | else if (value < 0.0031308f) |
| 816 | { |
| 817 | value = value * 12.92f; |
| 818 | } |
| 819 | else if (value < 1.0f) |
| 820 | { |
| 821 | value = std::pow(value, 0.41666f) * 1.055f - 0.055f; |
| 822 | } |
| 823 | else |
| 824 | { |
| 825 | value = 1.0f; |
| 826 | } |
| 827 | return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f)); |
| 828 | } |
| 829 | |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 830 | // Reverse the order of the bits. |
| 831 | inline uint32_t BitfieldReverse(uint32_t value) |
| 832 | { |
| 833 | // TODO(oetuaho@nvidia.com): Optimize this if needed. There don't seem to be compiler intrinsics |
| 834 | // for this, and right now it's not used in performance-critical paths. |
| 835 | uint32_t result = 0u; |
| 836 | for (size_t j = 0u; j < 32u; ++j) |
| 837 | { |
| 838 | result |= (((value >> j) & 1u) << (31u - j)); |
| 839 | } |
| 840 | return result; |
| 841 | } |
| 842 | |
| 843 | // Count the 1 bits. |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 844 | #if defined(ANGLE_PLATFORM_WINDOWS) |
| 845 | inline int BitCount(uint32_t bits) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 846 | { |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 847 | return static_cast<int>(__popcnt(bits)); |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 848 | } |
| 849 | #if defined(ANGLE_X64_CPU) |
| 850 | inline int BitCount(uint64_t bits) |
| 851 | { |
| 852 | return static_cast<int>(__popcnt64(bits)); |
| 853 | } |
| 854 | #endif // defined(ANGLE_X64_CPU) |
| 855 | #endif // defined(ANGLE_PLATFORM_WINDOWS) |
| 856 | |
| 857 | #if defined(ANGLE_PLATFORM_POSIX) |
| 858 | inline int BitCount(uint32_t bits) |
| 859 | { |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 860 | return __builtin_popcount(bits); |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 863 | #if defined(ANGLE_X64_CPU) |
| 864 | inline int BitCount(uint64_t bits) |
| 865 | { |
| 866 | return __builtin_popcountll(bits); |
| 867 | } |
| 868 | #endif // defined(ANGLE_X64_CPU) |
| 869 | #endif // defined(ANGLE_PLATFORM_POSIX) |
| 870 | |
| 871 | #if defined(ANGLE_PLATFORM_WINDOWS) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 872 | // Return the index of the least significant bit set. Indexing is such that bit 0 is the least |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 873 | // significant bit. Implemented for different bit widths on different platforms. |
| 874 | inline unsigned long ScanForward(uint32_t bits) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 875 | { |
| 876 | ASSERT(bits != 0u); |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 877 | unsigned long firstBitIndex = 0ul; |
| 878 | unsigned char ret = _BitScanForward(&firstBitIndex, bits); |
| 879 | ASSERT(ret != 0u); |
| 880 | return firstBitIndex; |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 881 | } |
| 882 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 883 | #if defined(ANGLE_X64_CPU) |
| 884 | inline unsigned long ScanForward(uint64_t bits) |
| 885 | { |
| 886 | ASSERT(bits != 0u); |
| 887 | unsigned long firstBitIndex = 0ul; |
| 888 | unsigned char ret = _BitScanForward64(&firstBitIndex, bits); |
| 889 | ASSERT(ret != 0u); |
| 890 | return firstBitIndex; |
| 891 | } |
| 892 | #endif // defined(ANGLE_X64_CPU) |
| 893 | #endif // defined(ANGLE_PLATFORM_WINDOWS) |
| 894 | |
| 895 | #if defined(ANGLE_PLATFORM_POSIX) |
| 896 | inline unsigned long ScanForward(uint32_t bits) |
| 897 | { |
| 898 | ASSERT(bits != 0u); |
| 899 | return static_cast<unsigned long>(__builtin_ctz(bits)); |
| 900 | } |
| 901 | |
| 902 | #if defined(ANGLE_X64_CPU) |
| 903 | inline unsigned long ScanForward(uint64_t bits) |
| 904 | { |
| 905 | ASSERT(bits != 0u); |
| 906 | return static_cast<unsigned long>(__builtin_ctzll(bits)); |
| 907 | } |
| 908 | #endif // defined(ANGLE_X64_CPU) |
| 909 | #endif // defined(ANGLE_PLATFORM_POSIX) |
| 910 | |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 911 | // Return the index of the most significant bit set. Indexing is such that bit 0 is the least |
| 912 | // significant bit. |
| 913 | inline unsigned long ScanReverse(unsigned long bits) |
| 914 | { |
| 915 | ASSERT(bits != 0u); |
| 916 | #if defined(ANGLE_PLATFORM_WINDOWS) |
| 917 | unsigned long lastBitIndex = 0ul; |
| 918 | unsigned char ret = _BitScanReverse(&lastBitIndex, bits); |
| 919 | ASSERT(ret != 0u); |
| 920 | return lastBitIndex; |
| 921 | #elif defined(ANGLE_PLATFORM_POSIX) |
| 922 | return static_cast<unsigned long>(sizeof(unsigned long) * CHAR_BIT - 1 - __builtin_clzl(bits)); |
| 923 | #else |
| 924 | #error Please implement bit-scan-reverse for your platform! |
| 925 | #endif |
| 926 | } |
| 927 | |
| 928 | // Returns -1 on 0, otherwise the index of the least significant 1 bit as in GLSL. |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 929 | template <typename T> |
| 930 | int FindLSB(T bits) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 931 | { |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 932 | static_assert(std::is_integral<T>::value, "must be integral type."); |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 933 | if (bits == 0u) |
| 934 | { |
| 935 | return -1; |
| 936 | } |
| 937 | else |
| 938 | { |
| 939 | return static_cast<int>(ScanForward(bits)); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | // Returns -1 on 0, otherwise the index of the most significant 1 bit as in GLSL. |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 944 | template <typename T> |
| 945 | int FindMSB(T bits) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 946 | { |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 947 | static_assert(std::is_integral<T>::value, "must be integral type."); |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 948 | if (bits == 0u) |
| 949 | { |
| 950 | return -1; |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | return static_cast<int>(ScanReverse(bits)); |
| 955 | } |
| 956 | } |
| 957 | |
Arun Patole | 551279e | 2015-07-07 18:18:23 +0530 | [diff] [blame] | 958 | // Returns whether the argument is Not a Number. |
| 959 | // IEEE 754 single precision NaN representation: Exponent(8 bits) - 255, Mantissa(23 bits) - non-zero. |
| 960 | inline bool isNaN(float f) |
| 961 | { |
| 962 | // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u |
| 963 | // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu |
| 964 | return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) && (bitCast<uint32_t>(f) & 0x7fffffu); |
| 965 | } |
| 966 | |
| 967 | // Returns whether the argument is infinity. |
| 968 | // IEEE 754 single precision infinity representation: Exponent(8 bits) - 255, Mantissa(23 bits) - zero. |
| 969 | inline bool isInf(float f) |
| 970 | { |
| 971 | // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u |
| 972 | // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu |
| 973 | return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) && !(bitCast<uint32_t>(f) & 0x7fffffu); |
| 974 | } |
| 975 | |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 976 | namespace priv |
| 977 | { |
| 978 | template <unsigned int N, unsigned int R> |
| 979 | struct iSquareRoot |
| 980 | { |
| 981 | static constexpr unsigned int solve() |
| 982 | { |
| 983 | return (R * R > N) |
| 984 | ? 0 |
| 985 | : ((R * R == N) ? R : static_cast<unsigned int>(iSquareRoot<N, R + 1>::value)); |
| 986 | } |
| 987 | enum Result |
| 988 | { |
| 989 | value = iSquareRoot::solve() |
| 990 | }; |
| 991 | }; |
| 992 | |
| 993 | template <unsigned int N> |
| 994 | struct iSquareRoot<N, N> |
| 995 | { |
| 996 | enum result |
| 997 | { |
| 998 | value = N |
| 999 | }; |
| 1000 | }; |
| 1001 | |
| 1002 | } // namespace priv |
| 1003 | |
| 1004 | template <unsigned int N> |
| 1005 | constexpr unsigned int iSquareRoot() |
| 1006 | { |
| 1007 | return priv::iSquareRoot<N, 1>::value; |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 1008 | } |
| 1009 | |
Olli Etuaho | 7f9a55f | 2016-10-03 14:32:08 +0100 | [diff] [blame] | 1010 | // Sum, difference and multiplication operations for signed ints that wrap on 32-bit overflow. |
| 1011 | // |
| 1012 | // Unsigned types are defined to do arithmetic modulo 2^n in C++. For signed types, overflow |
| 1013 | // behavior is undefined. |
| 1014 | |
| 1015 | template <typename T> |
| 1016 | inline T WrappingSum(T lhs, T rhs) |
| 1017 | { |
| 1018 | uint32_t lhsUnsigned = static_cast<uint32_t>(lhs); |
| 1019 | uint32_t rhsUnsigned = static_cast<uint32_t>(rhs); |
| 1020 | return static_cast<T>(lhsUnsigned + rhsUnsigned); |
| 1021 | } |
| 1022 | |
| 1023 | template <typename T> |
| 1024 | inline T WrappingDiff(T lhs, T rhs) |
| 1025 | { |
| 1026 | uint32_t lhsUnsigned = static_cast<uint32_t>(lhs); |
| 1027 | uint32_t rhsUnsigned = static_cast<uint32_t>(rhs); |
| 1028 | return static_cast<T>(lhsUnsigned - rhsUnsigned); |
| 1029 | } |
| 1030 | |
| 1031 | inline int32_t WrappingMul(int32_t lhs, int32_t rhs) |
| 1032 | { |
| 1033 | int64_t lhsWide = static_cast<int64_t>(lhs); |
| 1034 | int64_t rhsWide = static_cast<int64_t>(rhs); |
| 1035 | // The multiplication is guaranteed not to overflow. |
| 1036 | int64_t resultWide = lhsWide * rhsWide; |
| 1037 | // Implement the desired wrapping behavior by masking out the high-order 32 bits. |
| 1038 | resultWide = resultWide & 0xffffffffll; |
| 1039 | // Casting to a narrower signed type is fine since the casted value is representable in the |
| 1040 | // narrower type. |
| 1041 | return static_cast<int32_t>(resultWide); |
| 1042 | } |
| 1043 | |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 1044 | } // namespace gl |
| 1045 | |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 1046 | namespace rx |
| 1047 | { |
| 1048 | |
shannonwoods@chromium.org | 06d4e84 | 2013-05-30 00:04:42 +0000 | [diff] [blame] | 1049 | template <typename T> |
| 1050 | T roundUp(const T value, const T alignment) |
| 1051 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 1052 | auto temp = value + alignment - static_cast<T>(1); |
| 1053 | return temp - temp % alignment; |
| 1054 | } |
| 1055 | |
| 1056 | template <typename T> |
| 1057 | angle::CheckedNumeric<T> CheckedRoundUp(const T value, const T alignment) |
| 1058 | { |
| 1059 | angle::CheckedNumeric<T> checkedValue(value); |
| 1060 | angle::CheckedNumeric<T> checkedAlignment(alignment); |
| 1061 | return roundUp(checkedValue, checkedAlignment); |
shannonwoods@chromium.org | 06d4e84 | 2013-05-30 00:04:42 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Jamie Madill | aff8084 | 2014-08-04 10:47:56 -0400 | [diff] [blame] | 1064 | inline unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor) |
| 1065 | { |
| 1066 | unsigned int divided = value / divisor; |
| 1067 | return (divided + ((value % divisor == 0) ? 0 : 1)); |
| 1068 | } |
| 1069 | |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 1070 | #if defined(_MSC_VER) |
| 1071 | |
| 1072 | #define ANGLE_ROTL(x,y) _rotl(x,y) |
Austin Kinross | 405f347 | 2015-06-04 13:09:06 -0700 | [diff] [blame] | 1073 | #define ANGLE_ROTR16(x,y) _rotr16(x,y) |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 1074 | |
| 1075 | #else |
| 1076 | |
| 1077 | inline uint32_t RotL(uint32_t x, int8_t r) |
| 1078 | { |
| 1079 | return (x << r) | (x >> (32 - r)); |
| 1080 | } |
| 1081 | |
Austin Kinross | 405f347 | 2015-06-04 13:09:06 -0700 | [diff] [blame] | 1082 | inline uint16_t RotR16(uint16_t x, int8_t r) |
| 1083 | { |
| 1084 | return (x >> r) | (x << (16 - r)); |
| 1085 | } |
| 1086 | |
Geoff Lang | 6e4cfce | 2016-06-13 15:06:31 -0400 | [diff] [blame] | 1087 | #define ANGLE_ROTL(x, y) ::rx::RotL(x, y) |
| 1088 | #define ANGLE_ROTR16(x, y) ::rx::RotR16(x, y) |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 1089 | |
| 1090 | #endif // namespace rx |
| 1091 | |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 1094 | #endif // COMMON_MATHUTIL_H_ |