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 | |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 12 | #include "common/debug.h" |
Geoff Lang | 44fa759 | 2014-05-30 11:50:07 -0400 | [diff] [blame] | 13 | #include "common/platform.h" |
shannonwoods@chromium.org | a2ecfcc | 2013-05-30 00:11:59 +0000 | [diff] [blame] | 14 | |
Jamie Madill | b155bbc | 2014-01-13 09:51:03 -0500 | [diff] [blame] | 15 | #include <limits> |
| 16 | #include <algorithm> |
Jamie Madill | 4f26786 | 2014-04-17 15:53:37 -0400 | [diff] [blame] | 17 | #include <string.h> |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 18 | #include <stdlib.h> |
Jamie Madill | b155bbc | 2014-01-13 09:51:03 -0500 | [diff] [blame] | 19 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 20 | namespace gl |
| 21 | { |
shannonwoods@chromium.org | 49ec992 | 2013-05-30 00:16:38 +0000 | [diff] [blame] | 22 | |
| 23 | const unsigned int Float32One = 0x3F800000; |
| 24 | const unsigned short Float16One = 0x3C00; |
| 25 | |
apatrick@chromium.org | 60dafe8 | 2012-09-05 22:26:10 +0000 | [diff] [blame] | 26 | struct 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.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 37 | inline bool isPow2(int x) |
| 38 | { |
| 39 | return (x & (x - 1)) == 0 && (x != 0); |
| 40 | } |
| 41 | |
| 42 | inline int log2(int x) |
| 43 | { |
| 44 | int r = 0; |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 45 | while ((x >> r) > 1) r++; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 46 | return r; |
| 47 | } |
| 48 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 49 | inline 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 Madill | b155bbc | 2014-01-13 09:51:03 -0500 | [diff] [blame] | 62 | inline 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 Madill | 70656a6 | 2014-03-05 15:01:26 -0500 | [diff] [blame] | 67 | template <typename DestT, typename SrcT> |
| 68 | inline 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.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 80 | template<typename T, typename MIN, typename MAX> |
| 81 | inline T clamp(T x, MIN min, MAX max) |
| 82 | { |
shannon.woods@transgaming.com | 31c4f23 | 2013-02-28 23:14:18 +0000 | [diff] [blame] | 83 | // Since NaNs fail all comparison tests, a NaN value will default to min |
| 84 | return x > min ? (x > max ? max : x) : min; |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 85 | } |
| 86 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 87 | inline float clamp01(float x) |
| 88 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 89 | return clamp(x, 0.0f, 1.0f); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | template<const int n> |
| 93 | inline unsigned int unorm(float x) |
| 94 | { |
| 95 | const unsigned int max = 0xFFFFFFFF >> (32 - n); |
| 96 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 97 | if (x > 1) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 98 | { |
| 99 | return max; |
| 100 | } |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 101 | else if (x < 0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 102 | { |
| 103 | return 0; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | return (unsigned int)(max * x + 0.5f); |
| 108 | } |
| 109 | } |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 110 | |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 111 | inline bool supportsSSE2() |
| 112 | { |
Jamie Madill | d46275f | 2014-11-06 13:54:53 -0500 | [diff] [blame] | 113 | #if defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 114 | 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.org | a2ecfcc | 2013-05-30 00:11:59 +0000 | [diff] [blame] | 124 | |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 125 | 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 Lang | 8321779 | 2014-01-16 09:52:38 -0500 | [diff] [blame] | 135 | #else |
| 136 | UNIMPLEMENTED(); |
| 137 | return false; |
| 138 | #endif |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 139 | } |
apatrick@chromium.org | aa48067 | 2012-09-05 19:32:38 +0000 | [diff] [blame] | 140 | |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 141 | template <typename destType, typename sourceType> |
| 142 | destType bitCast(const sourceType &source) |
| 143 | { |
Jamie Madill | 7ab02fa | 2014-02-04 16:04:08 -0500 | [diff] [blame] | 144 | size_t copySize = std::min(sizeof(destType), sizeof(sourceType)); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 145 | destType output; |
Jamie Madill | 7ab02fa | 2014-02-04 16:04:08 -0500 | [diff] [blame] | 146 | memcpy(&output, &source, copySize); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 147 | return output; |
| 148 | } |
| 149 | |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 150 | inline 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 Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 158 | return static_cast<unsigned short>(sign | 0x7FFF); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 159 | } |
| 160 | else if(abs < 0x38800000) // Denormal |
| 161 | { |
shannonwoods@chromium.org | a2ecfcc | 2013-05-30 00:11:59 +0000 | [diff] [blame] | 162 | unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000; |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 163 | 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 Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 174 | 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] | 175 | } |
| 176 | else |
| 177 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 178 | 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] | 179 | } |
| 180 | } |
| 181 | |
apatrick@chromium.org | aa48067 | 2012-09-05 19:32:38 +0000 | [diff] [blame] | 182 | float float16ToFloat32(unsigned short h); |
| 183 | |
shannonwoods@chromium.org | 92b9cd5 | 2013-05-30 00:14:48 +0000 | [diff] [blame] | 184 | unsigned int convertRGBFloatsTo999E5(float red, float green, float blue); |
| 185 | void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue); |
| 186 | |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 187 | inline 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 | |
| 256 | inline 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 | |
| 325 | inline 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 Kinross | b8af723 | 2015-03-16 22:33:25 -0700 | [diff] [blame] | 357 | exponent = static_cast<unsigned short>(-112); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | return bitCast<float>(((exponent + 112) << 23) | (mantissa << 17)); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | inline 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 Kinross | b8af723 | 2015-03-16 22:33:25 -0700 | [diff] [blame] | 396 | exponent = static_cast<unsigned short>(-112); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | return bitCast<float>(((exponent + 112) << 23) | (mantissa << 18)); |
| 400 | } |
| 401 | } |
| 402 | |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 403 | template <typename T> |
| 404 | inline float normalizedToFloat(T input) |
| 405 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 406 | 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] | 407 | |
| 408 | const float inverseMax = 1.0f / std::numeric_limits<T>::max(); |
| 409 | return input * inverseMax; |
| 410 | } |
| 411 | |
| 412 | template <unsigned int inputBitCount, typename T> |
| 413 | inline float normalizedToFloat(T input) |
| 414 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 415 | 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 Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 417 | |
| 418 | const float inverseMax = 1.0f / ((1 << inputBitCount) - 1); |
| 419 | return input * inverseMax; |
| 420 | } |
| 421 | |
| 422 | template <typename T> |
| 423 | inline T floatToNormalized(float input) |
| 424 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 425 | return static_cast<T>(std::numeric_limits<T>::max() * input + 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | template <unsigned int outputBitCount, typename T> |
| 429 | inline T floatToNormalized(float input) |
| 430 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 431 | 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] | 432 | return static_cast<T>(((1 << outputBitCount) - 1) * input + 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> |
| 436 | inline T getShiftedData(T input) |
| 437 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 438 | static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8), |
| 439 | "T must have at least as many bits as inputBitCount + inputBitStart."); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 440 | const T mask = (1 << inputBitCount) - 1; |
| 441 | return (input >> inputBitStart) & mask; |
| 442 | } |
| 443 | |
| 444 | template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> |
| 445 | inline T shiftData(T input) |
| 446 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 447 | static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8), |
| 448 | "T must have at least as many bits as inputBitCount + inputBitStart."); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 449 | const T mask = (1 << inputBitCount) - 1; |
| 450 | return (input & mask) << inputBitStart; |
| 451 | } |
| 452 | |
| 453 | |
| 454 | inline unsigned char average(unsigned char a, unsigned char b) |
| 455 | { |
| 456 | return ((a ^ b) >> 1) + (a & b); |
| 457 | } |
| 458 | |
| 459 | inline signed char average(signed char a, signed char b) |
| 460 | { |
| 461 | return ((short)a + (short)b) / 2; |
| 462 | } |
| 463 | |
| 464 | inline unsigned short average(unsigned short a, unsigned short b) |
| 465 | { |
| 466 | return ((a ^ b) >> 1) + (a & b); |
| 467 | } |
| 468 | |
| 469 | inline signed short average(signed short a, signed short b) |
| 470 | { |
| 471 | return ((int)a + (int)b) / 2; |
| 472 | } |
| 473 | |
| 474 | inline unsigned int average(unsigned int a, unsigned int b) |
| 475 | { |
| 476 | return ((a ^ b) >> 1) + (a & b); |
| 477 | } |
| 478 | |
| 479 | inline signed int average(signed int a, signed int b) |
| 480 | { |
| 481 | return ((long long)a + (long long)b) / 2; |
| 482 | } |
| 483 | |
| 484 | inline float average(float a, float b) |
| 485 | { |
| 486 | return (a + b) * 0.5f; |
| 487 | } |
| 488 | |
| 489 | inline unsigned short averageHalfFloat(unsigned short a, unsigned short b) |
| 490 | { |
| 491 | return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f); |
| 492 | } |
| 493 | |
| 494 | inline unsigned int averageFloat11(unsigned int a, unsigned int b) |
| 495 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 496 | 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] | 497 | } |
| 498 | |
| 499 | inline unsigned int averageFloat10(unsigned int a, unsigned int b) |
| 500 | { |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 501 | 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] | 502 | } |
| 503 | |
Corentin Wallez | ac6ff93 | 2014-11-12 06:16:53 -0800 | [diff] [blame] | 504 | // Represents intervals of the type [a, b) |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 505 | template <typename T> |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 506 | struct Range |
| 507 | { |
| 508 | Range() {} |
Jamie Madill | cc00239 | 2014-09-09 10:21:56 -0400 | [diff] [blame] | 509 | 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] | 510 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 511 | T start; |
| 512 | T end; |
| 513 | |
Jamie Madill | cc00239 | 2014-09-09 10:21:56 -0400 | [diff] [blame] | 514 | T length() const { return end - start; } |
Corentin Wallez | ac6ff93 | 2014-11-12 06:16:53 -0800 | [diff] [blame] | 515 | |
| 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 Madill | aa9e997 | 2015-06-22 13:57:16 -0400 | [diff] [blame^] | 527 | |
| 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.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 538 | }; |
| 539 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 540 | typedef Range<int> RangeI; |
| 541 | typedef Range<unsigned int> RangeUI; |
| 542 | |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | namespace rx |
| 546 | { |
| 547 | |
shannonwoods@chromium.org | 06d4e84 | 2013-05-30 00:04:42 +0000 | [diff] [blame] | 548 | template <typename T> |
| 549 | T roundUp(const T value, const T alignment) |
| 550 | { |
| 551 | return value + alignment - 1 - (value - 1) % alignment; |
| 552 | } |
| 553 | |
Jamie Madill | aff8084 | 2014-08-04 10:47:56 -0400 | [diff] [blame] | 554 | inline 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 Madill | 4f1a863 | 2013-09-24 10:13:04 -0400 | [diff] [blame] | 560 | template <class T> |
| 561 | inline bool IsUnsignedAdditionSafe(T lhs, T rhs) |
| 562 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 563 | static_assert(!std::numeric_limits<T>::is_signed, "T must be unsigned."); |
Jamie Madill | 4f1a863 | 2013-09-24 10:13:04 -0400 | [diff] [blame] | 564 | return (rhs <= std::numeric_limits<T>::max() - lhs); |
| 565 | } |
| 566 | |
| 567 | template <class T> |
| 568 | inline bool IsUnsignedMultiplicationSafe(T lhs, T rhs) |
| 569 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 570 | static_assert(!std::numeric_limits<T>::is_signed, "T must be unsigned."); |
Jamie Madill | 4f1a863 | 2013-09-24 10:13:04 -0400 | [diff] [blame] | 571 | return (lhs == T(0) || rhs == T(0) || (rhs <= std::numeric_limits<T>::max() / lhs)); |
| 572 | } |
| 573 | |
Jamie Madill | 1b2a8f9 | 2014-05-14 13:09:39 -0400 | [diff] [blame] | 574 | template <class SmallIntT, class BigIntT> |
| 575 | inline bool IsIntegerCastSafe(BigIntT bigValue) |
| 576 | { |
| 577 | return (static_cast<BigIntT>(static_cast<SmallIntT>(bigValue)) == bigValue); |
| 578 | } |
| 579 | |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 580 | #if defined(_MSC_VER) |
| 581 | |
| 582 | #define ANGLE_ROTL(x,y) _rotl(x,y) |
Austin Kinross | 405f347 | 2015-06-04 13:09:06 -0700 | [diff] [blame] | 583 | #define ANGLE_ROTR16(x,y) _rotr16(x,y) |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 584 | |
| 585 | #else |
| 586 | |
| 587 | inline uint32_t RotL(uint32_t x, int8_t r) |
| 588 | { |
| 589 | return (x << r) | (x >> (32 - r)); |
| 590 | } |
| 591 | |
Austin Kinross | 405f347 | 2015-06-04 13:09:06 -0700 | [diff] [blame] | 592 | inline uint16_t RotR16(uint16_t x, int8_t r) |
| 593 | { |
| 594 | return (x >> r) | (x << (16 - r)); |
| 595 | } |
| 596 | |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 597 | #define ANGLE_ROTL(x,y) RotL(x,y) |
Austin Kinross | 405f347 | 2015-06-04 13:09:06 -0700 | [diff] [blame] | 598 | #define ANGLE_ROTR16(x,y) RotR16(x,y) |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 599 | |
| 600 | #endif // namespace rx |
| 601 | |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Geoff Lang | 0a73dd8 | 2014-11-19 16:18:08 -0500 | [diff] [blame] | 604 | #endif // COMMON_MATHUTIL_H_ |