daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Stuart Morgan | 9d73796 | 2019-08-14 12:25:12 -0700 | [diff] [blame] | 2 | // Copyright 2002 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 | |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 12 | #include <math.h> |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 13 | #include <stdint.h> |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 14 | #include <stdlib.h> |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 15 | #include <string.h> |
| 16 | #include <algorithm> |
| 17 | #include <limits> |
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; |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 28 | } // namespace angle |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 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 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 33 | const unsigned int Float32One = 0x3F800000; |
shannonwoods@chromium.org | 49ec992 | 2013-05-30 00:16:38 +0000 | [diff] [blame] | 34 | const unsigned short Float16One = 0x3C00; |
| 35 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 36 | template <typename T> |
Jiacheng Lu | 3e493c4 | 2019-07-29 16:27:01 -0600 | [diff] [blame] | 37 | inline constexpr 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 | |
Mohan Maiya | 01c0d6b | 2020-01-12 13:41:31 -0800 | [diff] [blame] | 43 | template <typename T> |
| 44 | inline int log2(T x) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 45 | { |
Mohan Maiya | 01c0d6b | 2020-01-12 13:41:31 -0800 | [diff] [blame] | 46 | static_assert(std::is_integral<T>::value, "log2 must be called on an integer type."); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 47 | int r = 0; |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 48 | while ((x >> r) > 1) |
| 49 | r++; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 50 | return r; |
| 51 | } |
| 52 | |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 53 | inline unsigned int ceilPow2(unsigned int x) |
| 54 | { |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 55 | if (x != 0) |
| 56 | x--; |
daniel@transgaming.com | 81655a7 | 2010-05-20 19:18:17 +0000 | [diff] [blame] | 57 | x |= x >> 1; |
| 58 | x |= x >> 2; |
| 59 | x |= x >> 4; |
| 60 | x |= x >> 8; |
| 61 | x |= x >> 16; |
| 62 | x++; |
| 63 | |
| 64 | return x; |
| 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 | { |
jchen10 | a99ed55 | 2017-09-22 08:10:32 +0800 | [diff] [blame] | 70 | // For floating-point types with denormalization, min returns the minimum positive normalized |
| 71 | // value. To find the value that has no values less than it, use numeric_limits::lowest. |
| 72 | constexpr const long double destLo = |
| 73 | static_cast<long double>(std::numeric_limits<DestT>::lowest()); |
| 74 | constexpr const long double destHi = |
| 75 | static_cast<long double>(std::numeric_limits<DestT>::max()); |
| 76 | constexpr const long double srcLo = |
| 77 | static_cast<long double>(std::numeric_limits<SrcT>::lowest()); |
| 78 | constexpr long double srcHi = static_cast<long double>(std::numeric_limits<SrcT>::max()); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 79 | |
jchen10 | a99ed55 | 2017-09-22 08:10:32 +0800 | [diff] [blame] | 80 | if (destHi < srcHi) |
Corentin Wallez | 630d85c | 2015-09-30 14:35:48 -0700 | [diff] [blame] | 81 | { |
jchen10 | a99ed55 | 2017-09-22 08:10:32 +0800 | [diff] [blame] | 82 | DestT destMax = std::numeric_limits<DestT>::max(); |
| 83 | if (value >= static_cast<SrcT>(destMax)) |
| 84 | { |
| 85 | return destMax; |
| 86 | } |
Corentin Wallez | 630d85c | 2015-09-30 14:35:48 -0700 | [diff] [blame] | 87 | } |
jchen10 | a99ed55 | 2017-09-22 08:10:32 +0800 | [diff] [blame] | 88 | |
| 89 | if (destLo > srcLo) |
Corentin Wallez | 630d85c | 2015-09-30 14:35:48 -0700 | [diff] [blame] | 90 | { |
jchen10 | a99ed55 | 2017-09-22 08:10:32 +0800 | [diff] [blame] | 91 | DestT destLow = std::numeric_limits<DestT>::lowest(); |
| 92 | if (value <= static_cast<SrcT>(destLow)) |
| 93 | { |
| 94 | return destLow; |
| 95 | } |
Corentin Wallez | 630d85c | 2015-09-30 14:35:48 -0700 | [diff] [blame] | 96 | } |
jchen10 | a99ed55 | 2017-09-22 08:10:32 +0800 | [diff] [blame] | 97 | |
| 98 | return static_cast<DestT>(value); |
Jamie Madill | 70656a6 | 2014-03-05 15:01:26 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Olli Etuaho | a87121f | 2017-10-06 14:07:27 +0200 | [diff] [blame] | 101 | // Specialize clampCast for bool->int conversion to avoid MSVS 2015 performance warning when the max |
| 102 | // value is casted to the source type. |
| 103 | template <> |
| 104 | inline unsigned int clampCast(bool value) |
| 105 | { |
| 106 | return static_cast<unsigned int>(value); |
| 107 | } |
| 108 | |
| 109 | template <> |
| 110 | inline int clampCast(bool value) |
| 111 | { |
| 112 | return static_cast<int>(value); |
| 113 | } |
| 114 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 115 | template <typename T, typename MIN, typename MAX> |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 116 | inline T clamp(T x, MIN min, MAX max) |
| 117 | { |
shannon.woods@transgaming.com | 31c4f23 | 2013-02-28 23:14:18 +0000 | [diff] [blame] | 118 | // Since NaNs fail all comparison tests, a NaN value will default to min |
| 119 | return x > min ? (x > max ? max : x) : min; |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 120 | } |
| 121 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 122 | inline float clamp01(float x) |
| 123 | { |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 124 | return clamp(x, 0.0f, 1.0f); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 127 | template <const int n> |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 128 | inline unsigned int unorm(float x) |
| 129 | { |
| 130 | const unsigned int max = 0xFFFFFFFF >> (32 - n); |
| 131 | |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 132 | if (x > 1) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 133 | { |
| 134 | return max; |
| 135 | } |
daniel@transgaming.com | e2b2212 | 2010-03-11 19:22:14 +0000 | [diff] [blame] | 136 | else if (x < 0) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 137 | { |
| 138 | return 0; |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | return (unsigned int)(max * x + 0.5f); |
| 143 | } |
| 144 | } |
apatrick@chromium.org | b31f532 | 2011-01-19 19:02:52 +0000 | [diff] [blame] | 145 | |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 146 | inline bool supportsSSE2() |
| 147 | { |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 148 | #if defined(ANGLE_USE_SSE) |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 149 | static bool checked = false; |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 150 | static bool supports = false; |
| 151 | |
| 152 | if (checked) |
| 153 | { |
| 154 | return supports; |
| 155 | } |
| 156 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 157 | # if defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) && !defined(_M_ARM64) |
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 | int info[4]; |
| 160 | __cpuid(info, 0); |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 161 | |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 162 | if (info[0] >= 1) |
| 163 | { |
| 164 | __cpuid(info, 1); |
| 165 | |
| 166 | supports = (info[3] >> 26) & 1; |
| 167 | } |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 168 | } |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 169 | # endif // defined(ANGLE_PLATFORM_WINDOWS) && !defined(_M_ARM) && !defined(_M_ARM64) |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 170 | checked = true; |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 171 | return supports; |
Geoff Lang | 5695fc9 | 2016-07-05 14:47:30 -0400 | [diff] [blame] | 172 | #else // defined(ANGLE_USE_SSE) |
Geoff Lang | 8321779 | 2014-01-16 09:52:38 -0500 | [diff] [blame] | 173 | return false; |
| 174 | #endif |
jbauman@chromium.org | f1f28c8 | 2011-05-12 20:53:34 +0000 | [diff] [blame] | 175 | } |
apatrick@chromium.org | aa48067 | 2012-09-05 19:32:38 +0000 | [diff] [blame] | 176 | |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 177 | template <typename destType, typename sourceType> |
| 178 | destType bitCast(const sourceType &source) |
| 179 | { |
Jamie Madill | 7ab02fa | 2014-02-04 16:04:08 -0500 | [diff] [blame] | 180 | size_t copySize = std::min(sizeof(destType), sizeof(sourceType)); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 181 | destType output; |
Jamie Madill | 7ab02fa | 2014-02-04 16:04:08 -0500 | [diff] [blame] | 182 | memcpy(&output, &source, copySize); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 183 | return output; |
| 184 | } |
| 185 | |
Tim Van Patten | cbabea7 | 2019-07-11 17:24:17 -0600 | [diff] [blame] | 186 | // https://stackoverflow.com/a/37581284 |
| 187 | template <typename T> |
| 188 | static constexpr double normalize(T value) |
| 189 | { |
| 190 | return value < 0 ? -static_cast<double>(value) / std::numeric_limits<T>::min() |
| 191 | : static_cast<double>(value) / std::numeric_limits<T>::max(); |
| 192 | } |
| 193 | |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 194 | inline unsigned short float32ToFloat16(float fp32) |
| 195 | { |
hendrikw | 7578262 | 2015-09-25 11:28:50 -0700 | [diff] [blame] | 196 | unsigned int fp32i = bitCast<unsigned int>(fp32); |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 197 | unsigned int sign = (fp32i & 0x80000000) >> 16; |
| 198 | unsigned int abs = fp32i & 0x7FFFFFFF; |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 199 | |
Tim Van Patten | db99083 | 2019-06-26 16:05:02 -0600 | [diff] [blame] | 200 | if (abs > 0x7F800000) |
| 201 | { // NaN |
| 202 | return 0x7FFF; |
| 203 | } |
| 204 | else if (abs > 0x47FFEFFF) |
| 205 | { // Infinity |
| 206 | return static_cast<uint16_t>(sign | 0x7C00); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 207 | } |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 208 | else if (abs < 0x38800000) // Denormal |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 209 | { |
shannonwoods@chromium.org | a2ecfcc | 2013-05-30 00:11:59 +0000 | [diff] [blame] | 210 | unsigned int mantissa = (abs & 0x007FFFFF) | 0x00800000; |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 211 | int e = 113 - (abs >> 23); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 212 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 213 | if (e < 24) |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 214 | { |
| 215 | abs = mantissa >> e; |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | abs = 0; |
| 220 | } |
| 221 | |
Minmin Gong | 794e000 | 2015-04-07 18:31:54 -0700 | [diff] [blame] | 222 | 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] | 223 | } |
| 224 | else |
| 225 | { |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 226 | return static_cast<unsigned short>( |
| 227 | sign | (abs + 0xC8000000 + 0x00000FFF + ((abs >> 13) & 1)) >> 13); |
daniel@transgaming.com | 2e38b80 | 2012-10-17 18:30:06 +0000 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
apatrick@chromium.org | aa48067 | 2012-09-05 19:32:38 +0000 | [diff] [blame] | 231 | float float16ToFloat32(unsigned short h); |
| 232 | |
shannonwoods@chromium.org | 92b9cd5 | 2013-05-30 00:14:48 +0000 | [diff] [blame] | 233 | unsigned int convertRGBFloatsTo999E5(float red, float green, float blue); |
| 234 | void convert999E5toRGBFloats(unsigned int input, float *red, float *green, float *blue); |
| 235 | |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 236 | inline unsigned short float32ToFloat11(float fp32) |
| 237 | { |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 238 | const unsigned int float32MantissaMask = 0x7FFFFF; |
| 239 | const unsigned int float32ExponentMask = 0x7F800000; |
| 240 | const unsigned int float32SignMask = 0x80000000; |
| 241 | const unsigned int float32ValueMask = ~float32SignMask; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 242 | const unsigned int float32ExponentFirstBit = 23; |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 243 | const unsigned int float32ExponentBias = 127; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 244 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 245 | const unsigned short float11Max = 0x7BF; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 246 | const unsigned short float11MantissaMask = 0x3F; |
| 247 | const unsigned short float11ExponentMask = 0x7C0; |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 248 | const unsigned short float11BitMask = 0x7FF; |
| 249 | const unsigned int float11ExponentBias = 14; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 250 | |
| 251 | const unsigned int float32Maxfloat11 = 0x477E0000; |
| 252 | const unsigned int float32Minfloat11 = 0x38800000; |
| 253 | |
| 254 | const unsigned int float32Bits = bitCast<unsigned int>(fp32); |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 255 | const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 256 | |
| 257 | unsigned int float32Val = float32Bits & float32ValueMask; |
| 258 | |
| 259 | if ((float32Val & float32ExponentMask) == float32ExponentMask) |
| 260 | { |
| 261 | // INF or NAN |
| 262 | if ((float32Val & float32MantissaMask) != 0) |
| 263 | { |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 264 | return float11ExponentMask | |
| 265 | (((float32Val >> 17) | (float32Val >> 11) | (float32Val >> 6) | (float32Val)) & |
| 266 | float11MantissaMask); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 267 | } |
| 268 | else if (float32Sign) |
| 269 | { |
| 270 | // -INF is clamped to 0 since float11 is positive only |
| 271 | return 0; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | return float11ExponentMask; |
| 276 | } |
| 277 | } |
| 278 | else if (float32Sign) |
| 279 | { |
| 280 | // float11 is positive only, so clamp to zero |
| 281 | return 0; |
| 282 | } |
| 283 | else if (float32Val > float32Maxfloat11) |
| 284 | { |
| 285 | // The number is too large to be represented as a float11, set to max |
| 286 | return float11Max; |
| 287 | } |
| 288 | else |
| 289 | { |
| 290 | if (float32Val < float32Minfloat11) |
| 291 | { |
| 292 | // The number is too small to be represented as a normalized float11 |
| 293 | // Convert it to a denormalized value. |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 294 | const unsigned int shift = (float32ExponentBias - float11ExponentBias) - |
| 295 | (float32Val >> float32ExponentFirstBit); |
| 296 | float32Val = |
| 297 | ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 298 | } |
| 299 | else |
| 300 | { |
| 301 | // Rebias the exponent to represent the value as a normalized float11 |
| 302 | float32Val += 0xC8000000; |
| 303 | } |
| 304 | |
| 305 | return ((float32Val + 0xFFFF + ((float32Val >> 17) & 1)) >> 17) & float11BitMask; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | inline unsigned short float32ToFloat10(float fp32) |
| 310 | { |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 311 | const unsigned int float32MantissaMask = 0x7FFFFF; |
| 312 | const unsigned int float32ExponentMask = 0x7F800000; |
| 313 | const unsigned int float32SignMask = 0x80000000; |
| 314 | const unsigned int float32ValueMask = ~float32SignMask; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 315 | const unsigned int float32ExponentFirstBit = 23; |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 316 | const unsigned int float32ExponentBias = 127; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 317 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 318 | const unsigned short float10Max = 0x3DF; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 319 | const unsigned short float10MantissaMask = 0x1F; |
| 320 | const unsigned short float10ExponentMask = 0x3E0; |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 321 | const unsigned short float10BitMask = 0x3FF; |
| 322 | const unsigned int float10ExponentBias = 14; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 323 | |
| 324 | const unsigned int float32Maxfloat10 = 0x477C0000; |
| 325 | const unsigned int float32Minfloat10 = 0x38800000; |
| 326 | |
| 327 | const unsigned int float32Bits = bitCast<unsigned int>(fp32); |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 328 | const bool float32Sign = (float32Bits & float32SignMask) == float32SignMask; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 329 | |
| 330 | unsigned int float32Val = float32Bits & float32ValueMask; |
| 331 | |
| 332 | if ((float32Val & float32ExponentMask) == float32ExponentMask) |
| 333 | { |
| 334 | // INF or NAN |
| 335 | if ((float32Val & float32MantissaMask) != 0) |
| 336 | { |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 337 | return float10ExponentMask | |
| 338 | (((float32Val >> 18) | (float32Val >> 13) | (float32Val >> 3) | (float32Val)) & |
| 339 | float10MantissaMask); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 340 | } |
| 341 | else if (float32Sign) |
| 342 | { |
| 343 | // -INF is clamped to 0 since float11 is positive only |
| 344 | return 0; |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | return float10ExponentMask; |
| 349 | } |
| 350 | } |
| 351 | else if (float32Sign) |
| 352 | { |
| 353 | // float10 is positive only, so clamp to zero |
| 354 | return 0; |
| 355 | } |
| 356 | else if (float32Val > float32Maxfloat10) |
| 357 | { |
| 358 | // The number is too large to be represented as a float11, set to max |
| 359 | return float10Max; |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | if (float32Val < float32Minfloat10) |
| 364 | { |
| 365 | // The number is too small to be represented as a normalized float11 |
| 366 | // Convert it to a denormalized value. |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 367 | const unsigned int shift = (float32ExponentBias - float10ExponentBias) - |
| 368 | (float32Val >> float32ExponentFirstBit); |
| 369 | float32Val = |
| 370 | ((1 << float32ExponentFirstBit) | (float32Val & float32MantissaMask)) >> shift; |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 371 | } |
| 372 | else |
| 373 | { |
| 374 | // Rebias the exponent to represent the value as a normalized float11 |
| 375 | float32Val += 0xC8000000; |
| 376 | } |
| 377 | |
| 378 | return ((float32Val + 0x1FFFF + ((float32Val >> 18) & 1)) >> 18) & float10BitMask; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | inline float float11ToFloat32(unsigned short fp11) |
| 383 | { |
| 384 | unsigned short exponent = (fp11 >> 6) & 0x1F; |
| 385 | unsigned short mantissa = fp11 & 0x3F; |
| 386 | |
| 387 | if (exponent == 0x1F) |
| 388 | { |
| 389 | // INF or NAN |
| 390 | return bitCast<float>(0x7f800000 | (mantissa << 17)); |
| 391 | } |
| 392 | else |
| 393 | { |
| 394 | if (exponent != 0) |
| 395 | { |
| 396 | // normalized |
| 397 | } |
| 398 | else if (mantissa != 0) |
| 399 | { |
| 400 | // The value is denormalized |
| 401 | exponent = 1; |
| 402 | |
| 403 | do |
| 404 | { |
| 405 | exponent--; |
| 406 | mantissa <<= 1; |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 407 | } while ((mantissa & 0x40) == 0); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 408 | |
| 409 | mantissa = mantissa & 0x3F; |
| 410 | } |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 411 | else // The value is zero |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 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 << 17)); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | inline float float10ToFloat32(unsigned short fp11) |
| 421 | { |
| 422 | unsigned short exponent = (fp11 >> 5) & 0x1F; |
| 423 | unsigned short mantissa = fp11 & 0x1F; |
| 424 | |
| 425 | if (exponent == 0x1F) |
| 426 | { |
| 427 | // INF or NAN |
| 428 | return bitCast<float>(0x7f800000 | (mantissa << 17)); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | if (exponent != 0) |
| 433 | { |
| 434 | // normalized |
| 435 | } |
| 436 | else if (mantissa != 0) |
| 437 | { |
| 438 | // The value is denormalized |
| 439 | exponent = 1; |
| 440 | |
| 441 | do |
| 442 | { |
| 443 | exponent--; |
| 444 | mantissa <<= 1; |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 445 | } while ((mantissa & 0x20) == 0); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 446 | |
| 447 | mantissa = mantissa & 0x1F; |
| 448 | } |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 449 | else // The value is zero |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 450 | { |
Austin Kinross | b8af723 | 2015-03-16 22:33:25 -0700 | [diff] [blame] | 451 | exponent = static_cast<unsigned short>(-112); |
shannonwoods@chromium.org | a43d829 | 2013-05-30 00:15:50 +0000 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | return bitCast<float>(((exponent + 112) << 23) | (mantissa << 18)); |
| 455 | } |
| 456 | } |
| 457 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 458 | // Convers to and from float and 16.16 fixed point format. |
| 459 | |
Le Quyen | d200a77 | 2019-10-10 00:44:01 +0800 | [diff] [blame] | 460 | inline float ConvertFixedToFloat(uint32_t fixedInput) |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 461 | { |
| 462 | return static_cast<float>(fixedInput) / 65536.0f; |
| 463 | } |
| 464 | |
Le Quyen | d200a77 | 2019-10-10 00:44:01 +0800 | [diff] [blame] | 465 | inline uint32_t ConvertFloatToFixed(float floatInput) |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 466 | { |
| 467 | static constexpr uint32_t kHighest = 32767 * 65536 + 65535; |
| 468 | static constexpr uint32_t kLowest = static_cast<uint32_t>(-32768 * 65536 + 65535); |
| 469 | |
| 470 | if (floatInput > 32767.65535) |
| 471 | { |
| 472 | return kHighest; |
| 473 | } |
| 474 | else if (floatInput < -32768.65535) |
| 475 | { |
| 476 | return kLowest; |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | return static_cast<uint32_t>(floatInput * 65536); |
| 481 | } |
| 482 | } |
| 483 | |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 484 | template <typename T> |
| 485 | inline float normalizedToFloat(T input) |
| 486 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 487 | 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] | 488 | |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 489 | if (sizeof(T) > 2) |
| 490 | { |
| 491 | // float has only a 23 bit mantissa, so we need to do the calculation in double precision |
| 492 | constexpr double inverseMax = 1.0 / std::numeric_limits<T>::max(); |
| 493 | return static_cast<float>(input * inverseMax); |
| 494 | } |
| 495 | else |
| 496 | { |
| 497 | constexpr float inverseMax = 1.0f / std::numeric_limits<T>::max(); |
| 498 | return input * inverseMax; |
| 499 | } |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | template <unsigned int inputBitCount, typename T> |
| 503 | inline float normalizedToFloat(T input) |
| 504 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 505 | static_assert(std::numeric_limits<T>::is_integer, "T must be an integer."); |
| 506 | 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] | 507 | |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 508 | if (inputBitCount > 23) |
| 509 | { |
| 510 | // float has only a 23 bit mantissa, so we need to do the calculation in double precision |
| 511 | constexpr double inverseMax = 1.0 / ((1 << inputBitCount) - 1); |
| 512 | return static_cast<float>(input * inverseMax); |
| 513 | } |
| 514 | else |
| 515 | { |
| 516 | constexpr float inverseMax = 1.0f / ((1 << inputBitCount) - 1); |
| 517 | return input * inverseMax; |
| 518 | } |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | template <typename T> |
| 522 | inline T floatToNormalized(float input) |
| 523 | { |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 524 | if (sizeof(T) > 2) |
| 525 | { |
| 526 | // float has only a 23 bit mantissa, so we need to do the calculation in double precision |
| 527 | return static_cast<T>(std::numeric_limits<T>::max() * static_cast<double>(input) + 0.5); |
| 528 | } |
| 529 | else |
| 530 | { |
| 531 | return static_cast<T>(std::numeric_limits<T>::max() * input + 0.5f); |
| 532 | } |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | template <unsigned int outputBitCount, typename T> |
| 536 | inline T floatToNormalized(float input) |
| 537 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 538 | static_assert(outputBitCount < (sizeof(T) * 8), "T must have more bits than outputBitCount."); |
Till Rathmann | b854363 | 2018-10-02 19:46:14 +0200 | [diff] [blame] | 539 | |
| 540 | if (outputBitCount > 23) |
| 541 | { |
| 542 | // float has only a 23 bit mantissa, so we need to do the calculation in double precision |
| 543 | return static_cast<T>(((1 << outputBitCount) - 1) * static_cast<double>(input) + 0.5); |
| 544 | } |
| 545 | else |
| 546 | { |
| 547 | return static_cast<T>(((1 << outputBitCount) - 1) * input + 0.5f); |
| 548 | } |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> |
| 552 | inline T getShiftedData(T input) |
| 553 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 554 | static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8), |
| 555 | "T must have at least as many bits as inputBitCount + inputBitStart."); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 556 | const T mask = (1 << inputBitCount) - 1; |
| 557 | return (input >> inputBitStart) & mask; |
| 558 | } |
| 559 | |
| 560 | template <unsigned int inputBitCount, unsigned int inputBitStart, typename T> |
| 561 | inline T shiftData(T input) |
| 562 | { |
Geoff Lang | d447581 | 2015-03-18 10:53:05 -0400 | [diff] [blame] | 563 | static_assert(inputBitCount + inputBitStart <= (sizeof(T) * 8), |
| 564 | "T must have at least as many bits as inputBitCount + inputBitStart."); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 565 | const T mask = (1 << inputBitCount) - 1; |
| 566 | return (input & mask) << inputBitStart; |
| 567 | } |
| 568 | |
Olli Etuaho | 0f2b156 | 2016-05-13 16:15:35 +0300 | [diff] [blame] | 569 | inline unsigned int CountLeadingZeros(uint32_t x) |
| 570 | { |
| 571 | // Use binary search to find the amount of leading zeros. |
| 572 | unsigned int zeros = 32u; |
| 573 | uint32_t y; |
| 574 | |
| 575 | y = x >> 16u; |
| 576 | if (y != 0) |
| 577 | { |
| 578 | zeros = zeros - 16u; |
| 579 | x = y; |
| 580 | } |
| 581 | y = x >> 8u; |
| 582 | if (y != 0) |
| 583 | { |
| 584 | zeros = zeros - 8u; |
| 585 | x = y; |
| 586 | } |
| 587 | y = x >> 4u; |
| 588 | if (y != 0) |
| 589 | { |
| 590 | zeros = zeros - 4u; |
| 591 | x = y; |
| 592 | } |
| 593 | y = x >> 2u; |
| 594 | if (y != 0) |
| 595 | { |
| 596 | zeros = zeros - 2u; |
| 597 | x = y; |
| 598 | } |
| 599 | y = x >> 1u; |
| 600 | if (y != 0) |
| 601 | { |
| 602 | return zeros - 2u; |
| 603 | } |
| 604 | return zeros - x; |
| 605 | } |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 606 | |
| 607 | inline unsigned char average(unsigned char a, unsigned char b) |
| 608 | { |
| 609 | return ((a ^ b) >> 1) + (a & b); |
| 610 | } |
| 611 | |
| 612 | inline signed char average(signed char a, signed char b) |
| 613 | { |
| 614 | return ((short)a + (short)b) / 2; |
| 615 | } |
| 616 | |
| 617 | inline unsigned short average(unsigned short a, unsigned short b) |
| 618 | { |
| 619 | return ((a ^ b) >> 1) + (a & b); |
| 620 | } |
| 621 | |
| 622 | inline signed short average(signed short a, signed short b) |
| 623 | { |
| 624 | return ((int)a + (int)b) / 2; |
| 625 | } |
| 626 | |
| 627 | inline unsigned int average(unsigned int a, unsigned int b) |
| 628 | { |
| 629 | return ((a ^ b) >> 1) + (a & b); |
| 630 | } |
| 631 | |
Corentin Wallez | ca311dd | 2015-12-07 15:07:48 -0500 | [diff] [blame] | 632 | inline int average(int a, int b) |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 633 | { |
Corentin Wallez | ca311dd | 2015-12-07 15:07:48 -0500 | [diff] [blame] | 634 | long long average = (static_cast<long long>(a) + static_cast<long long>(b)) / 2ll; |
| 635 | return static_cast<int>(average); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | inline float average(float a, float b) |
| 639 | { |
| 640 | return (a + b) * 0.5f; |
| 641 | } |
| 642 | |
| 643 | inline unsigned short averageHalfFloat(unsigned short a, unsigned short b) |
| 644 | { |
| 645 | return float32ToFloat16((float16ToFloat32(a) + float16ToFloat32(b)) * 0.5f); |
| 646 | } |
| 647 | |
| 648 | inline unsigned int averageFloat11(unsigned int a, unsigned int b) |
| 649 | { |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 650 | return float32ToFloat11((float11ToFloat32(static_cast<unsigned short>(a)) + |
| 651 | float11ToFloat32(static_cast<unsigned short>(b))) * |
| 652 | 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | inline unsigned int averageFloat10(unsigned int a, unsigned int b) |
| 656 | { |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 657 | return float32ToFloat10((float10ToFloat32(static_cast<unsigned short>(a)) + |
| 658 | float10ToFloat32(static_cast<unsigned short>(b))) * |
| 659 | 0.5f); |
Geoff Lang | 446a447 | 2013-06-04 10:03:14 -0400 | [diff] [blame] | 660 | } |
| 661 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 662 | template <typename T> |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 663 | class Range |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 664 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 665 | public: |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 666 | Range() {} |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 667 | Range(T lo, T hi) : mLow(lo), mHigh(hi) {} |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 668 | |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 669 | T length() const { return (empty() ? 0 : (mHigh - mLow)); } |
Corentin Wallez | ac6ff93 | 2014-11-12 06:16:53 -0800 | [diff] [blame] | 670 | |
| 671 | bool intersects(Range<T> other) |
| 672 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 673 | if (mLow <= other.mLow) |
Corentin Wallez | ac6ff93 | 2014-11-12 06:16:53 -0800 | [diff] [blame] | 674 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 675 | return other.mLow < mHigh; |
Corentin Wallez | ac6ff93 | 2014-11-12 06:16:53 -0800 | [diff] [blame] | 676 | } |
| 677 | else |
| 678 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 679 | return mLow < other.mHigh; |
Corentin Wallez | ac6ff93 | 2014-11-12 06:16:53 -0800 | [diff] [blame] | 680 | } |
| 681 | } |
Jamie Madill | aa9e997 | 2015-06-22 13:57:16 -0400 | [diff] [blame] | 682 | |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 683 | // Assumes that end is non-inclusive.. for example, extending to 5 will make "end" 6. |
Jamie Madill | aa9e997 | 2015-06-22 13:57:16 -0400 | [diff] [blame] | 684 | void extend(T value) |
| 685 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 686 | mLow = value < mLow ? value : mLow; |
| 687 | mHigh = value >= mHigh ? (value + 1) : mHigh; |
Jamie Madill | aa9e997 | 2015-06-22 13:57:16 -0400 | [diff] [blame] | 688 | } |
| 689 | |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 690 | bool empty() const { return mHigh <= mLow; } |
| 691 | |
| 692 | bool contains(T value) const { return value >= mLow && value < mHigh; } |
| 693 | |
| 694 | class Iterator final |
Jamie Madill | aa9e997 | 2015-06-22 13:57:16 -0400 | [diff] [blame] | 695 | { |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 696 | public: |
| 697 | Iterator(T value) : mCurrent(value) {} |
| 698 | |
| 699 | Iterator &operator++() |
| 700 | { |
| 701 | mCurrent++; |
| 702 | return *this; |
| 703 | } |
| 704 | bool operator==(const Iterator &other) const { return mCurrent == other.mCurrent; } |
| 705 | bool operator!=(const Iterator &other) const { return mCurrent != other.mCurrent; } |
| 706 | T operator*() const { return mCurrent; } |
| 707 | |
| 708 | private: |
| 709 | T mCurrent; |
| 710 | }; |
| 711 | |
| 712 | Iterator begin() const { return Iterator(mLow); } |
| 713 | |
| 714 | Iterator end() const { return Iterator(mHigh); } |
| 715 | |
| 716 | T low() const { return mLow; } |
| 717 | T high() const { return mHigh; } |
| 718 | |
Jamie Madill | 8c3988c | 2017-12-21 14:44:56 -0500 | [diff] [blame] | 719 | void invalidate() |
| 720 | { |
| 721 | mLow = std::numeric_limits<T>::max(); |
| 722 | mHigh = std::numeric_limits<T>::min(); |
| 723 | } |
| 724 | |
Jamie Madill | 982f6e0 | 2017-06-07 14:33:04 -0400 | [diff] [blame] | 725 | private: |
| 726 | T mLow; |
| 727 | T mHigh; |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 728 | }; |
| 729 | |
Jamie Madill | 39b4346 | 2014-08-18 16:39:50 -0400 | [diff] [blame] | 730 | typedef Range<int> RangeI; |
| 731 | typedef Range<unsigned int> RangeUI; |
| 732 | |
Geoff Lang | 3edfe03 | 2015-09-04 16:38:24 -0400 | [diff] [blame] | 733 | struct IndexRange |
| 734 | { |
Markus Tavenrath | cc37cbf | 2018-12-30 23:35:25 +0900 | [diff] [blame] | 735 | struct Undefined |
| 736 | {}; |
| 737 | IndexRange(Undefined) {} |
Geoff Lang | 3edfe03 | 2015-09-04 16:38:24 -0400 | [diff] [blame] | 738 | IndexRange() : IndexRange(0, 0, 0) {} |
| 739 | IndexRange(size_t start_, size_t end_, size_t vertexIndexCount_) |
| 740 | : start(start_), end(end_), vertexIndexCount(vertexIndexCount_) |
| 741 | { |
| 742 | ASSERT(start <= end); |
| 743 | } |
| 744 | |
| 745 | // Number of vertices in the range. |
| 746 | size_t vertexCount() const { return (end - start) + 1; } |
| 747 | |
| 748 | // Inclusive range of indices that are not primitive restart |
| 749 | size_t start; |
| 750 | size_t end; |
| 751 | |
| 752 | // Number of non-primitive restart indices |
| 753 | size_t vertexIndexCount; |
| 754 | }; |
| 755 | |
Olli Etuaho | 74da73f | 2017-02-01 15:37:48 +0000 | [diff] [blame] | 756 | // Combine a floating-point value representing a mantissa (x) and an integer exponent (exp) into a |
| 757 | // floating-point value. As in GLSL ldexp() built-in. |
| 758 | inline float Ldexp(float x, int exp) |
| 759 | { |
| 760 | if (exp > 128) |
| 761 | { |
| 762 | return std::numeric_limits<float>::infinity(); |
| 763 | } |
| 764 | if (exp < -126) |
| 765 | { |
| 766 | return 0.0f; |
| 767 | } |
| 768 | double result = static_cast<double>(x) * std::pow(2.0, static_cast<double>(exp)); |
| 769 | return static_cast<float>(result); |
| 770 | } |
| 771 | |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 772 | // First, both normalized floating-point values are converted into 16-bit integer values. |
| 773 | // Then, the results are packed into the returned 32-bit unsigned integer. |
| 774 | // The first float value will be written to the least significant bits of the output; |
| 775 | // the last float value will be written to the most significant bits. |
| 776 | // The conversion of each value to fixed point is done as follows : |
| 777 | // packSnorm2x16 : round(clamp(c, -1, +1) * 32767.0) |
| 778 | inline uint32_t packSnorm2x16(float f1, float f2) |
| 779 | { |
Yuly Novikov | 451ed25 | 2016-01-21 21:16:45 -0500 | [diff] [blame] | 780 | int16_t leastSignificantBits = static_cast<int16_t>(roundf(clamp(f1, -1.0f, 1.0f) * 32767.0f)); |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 781 | int16_t mostSignificantBits = static_cast<int16_t>(roundf(clamp(f2, -1.0f, 1.0f) * 32767.0f)); |
Yuly Novikov | 451ed25 | 2016-01-21 21:16:45 -0500 | [diff] [blame] | 782 | return static_cast<uint32_t>(mostSignificantBits) << 16 | |
| 783 | (static_cast<uint32_t>(leastSignificantBits) & 0xFFFF); |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 784 | } |
| 785 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 786 | // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then, |
| 787 | // each component is converted to a normalized floating-point value to generate the returned two |
| 788 | // float values. The first float value will be extracted from the least significant bits of the |
| 789 | // input; the last float value will be extracted from the most-significant bits. The conversion for |
| 790 | // unpacked fixed-point value to floating point is done as follows: unpackSnorm2x16 : clamp(f / |
| 791 | // 32767.0, -1, +1) |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 792 | inline void unpackSnorm2x16(uint32_t u, float *f1, float *f2) |
| 793 | { |
| 794 | int16_t leastSignificantBits = static_cast<int16_t>(u & 0xFFFF); |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 795 | int16_t mostSignificantBits = static_cast<int16_t>(u >> 16); |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 796 | *f1 = clamp(static_cast<float>(leastSignificantBits) / 32767.0f, -1.0f, 1.0f); |
| 797 | *f2 = clamp(static_cast<float>(mostSignificantBits) / 32767.0f, -1.0f, 1.0f); |
| 798 | } |
| 799 | |
| 800 | // First, both normalized floating-point values are converted into 16-bit integer values. |
| 801 | // Then, the results are packed into the returned 32-bit unsigned integer. |
| 802 | // The first float value will be written to the least significant bits of the output; |
| 803 | // the last float value will be written to the most significant bits. |
| 804 | // The conversion of each value to fixed point is done as follows: |
| 805 | // packUnorm2x16 : round(clamp(c, 0, +1) * 65535.0) |
| 806 | inline uint32_t packUnorm2x16(float f1, float f2) |
| 807 | { |
| 808 | uint16_t leastSignificantBits = static_cast<uint16_t>(roundf(clamp(f1, 0.0f, 1.0f) * 65535.0f)); |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 809 | uint16_t mostSignificantBits = static_cast<uint16_t>(roundf(clamp(f2, 0.0f, 1.0f) * 65535.0f)); |
| 810 | return static_cast<uint32_t>(mostSignificantBits) << 16 | |
| 811 | static_cast<uint32_t>(leastSignificantBits); |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 812 | } |
| 813 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 814 | // First, unpacks a single 32-bit unsigned integer u into a pair of 16-bit unsigned integers. Then, |
| 815 | // each component is converted to a normalized floating-point value to generate the returned two |
| 816 | // float values. The first float value will be extracted from the least significant bits of the |
| 817 | // input; the last float value will be extracted from the most-significant bits. The conversion for |
| 818 | // unpacked fixed-point value to floating point is done as follows: unpackUnorm2x16 : f / 65535.0 |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 819 | inline void unpackUnorm2x16(uint32_t u, float *f1, float *f2) |
| 820 | { |
| 821 | uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF); |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 822 | uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16); |
| 823 | *f1 = static_cast<float>(leastSignificantBits) / 65535.0f; |
| 824 | *f2 = static_cast<float>(mostSignificantBits) / 65535.0f; |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 825 | } |
| 826 | |
Olli Etuaho | 25aef45 | 2017-01-29 16:15:44 -0800 | [diff] [blame] | 827 | // Helper functions intended to be used only here. |
| 828 | namespace priv |
| 829 | { |
| 830 | |
| 831 | inline uint8_t ToPackedUnorm8(float f) |
| 832 | { |
| 833 | return static_cast<uint8_t>(roundf(clamp(f, 0.0f, 1.0f) * 255.0f)); |
| 834 | } |
| 835 | |
| 836 | inline int8_t ToPackedSnorm8(float f) |
| 837 | { |
| 838 | return static_cast<int8_t>(roundf(clamp(f, -1.0f, 1.0f) * 127.0f)); |
| 839 | } |
| 840 | |
| 841 | } // namespace priv |
| 842 | |
| 843 | // Packs 4 normalized unsigned floating-point values to a single 32-bit unsigned integer. Works |
| 844 | // similarly to packUnorm2x16. The floats are clamped to the range 0.0 to 1.0, and written to the |
| 845 | // unsigned integer starting from the least significant bits. |
| 846 | inline uint32_t PackUnorm4x8(float f1, float f2, float f3, float f4) |
| 847 | { |
| 848 | uint8_t bits[4]; |
| 849 | bits[0] = priv::ToPackedUnorm8(f1); |
| 850 | bits[1] = priv::ToPackedUnorm8(f2); |
| 851 | bits[2] = priv::ToPackedUnorm8(f3); |
| 852 | bits[3] = priv::ToPackedUnorm8(f4); |
| 853 | uint32_t result = 0u; |
| 854 | for (int i = 0; i < 4; ++i) |
| 855 | { |
| 856 | int shift = i * 8; |
| 857 | result |= (static_cast<uint32_t>(bits[i]) << shift); |
| 858 | } |
| 859 | return result; |
| 860 | } |
| 861 | |
| 862 | // Unpacks 4 normalized unsigned floating-point values from a single 32-bit unsigned integer into f. |
| 863 | // Works similarly to unpackUnorm2x16. The floats are unpacked starting from the least significant |
| 864 | // bits. |
| 865 | inline void UnpackUnorm4x8(uint32_t u, float *f) |
| 866 | { |
| 867 | for (int i = 0; i < 4; ++i) |
| 868 | { |
| 869 | int shift = i * 8; |
| 870 | uint8_t bits = static_cast<uint8_t>((u >> shift) & 0xFF); |
| 871 | f[i] = static_cast<float>(bits) / 255.0f; |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | // Packs 4 normalized signed floating-point values to a single 32-bit unsigned integer. The floats |
| 876 | // are clamped to the range -1.0 to 1.0, and written to the unsigned integer starting from the least |
| 877 | // significant bits. |
| 878 | inline uint32_t PackSnorm4x8(float f1, float f2, float f3, float f4) |
| 879 | { |
| 880 | int8_t bits[4]; |
| 881 | bits[0] = priv::ToPackedSnorm8(f1); |
| 882 | bits[1] = priv::ToPackedSnorm8(f2); |
| 883 | bits[2] = priv::ToPackedSnorm8(f3); |
| 884 | bits[3] = priv::ToPackedSnorm8(f4); |
| 885 | uint32_t result = 0u; |
| 886 | for (int i = 0; i < 4; ++i) |
| 887 | { |
| 888 | int shift = i * 8; |
| 889 | result |= ((static_cast<uint32_t>(bits[i]) & 0xFF) << shift); |
| 890 | } |
| 891 | return result; |
| 892 | } |
| 893 | |
| 894 | // Unpacks 4 normalized signed floating-point values from a single 32-bit unsigned integer into f. |
| 895 | // Works similarly to unpackSnorm2x16. The floats are unpacked starting from the least significant |
| 896 | // bits, and clamped to the range -1.0 to 1.0. |
| 897 | inline void UnpackSnorm4x8(uint32_t u, float *f) |
| 898 | { |
| 899 | for (int i = 0; i < 4; ++i) |
| 900 | { |
| 901 | int shift = i * 8; |
| 902 | int8_t bits = static_cast<int8_t>((u >> shift) & 0xFF); |
| 903 | f[i] = clamp(static_cast<float>(bits) / 127.0f, -1.0f, 1.0f); |
| 904 | } |
| 905 | } |
| 906 | |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 907 | // Returns an unsigned integer obtained by converting the two floating-point values to the 16-bit |
| 908 | // floating-point representation found in the OpenGL ES Specification, and then packing these |
| 909 | // two 16-bit integers into a 32-bit unsigned integer. |
| 910 | // f1: The 16 least-significant bits of the result; |
| 911 | // f2: The 16 most-significant bits. |
| 912 | inline uint32_t packHalf2x16(float f1, float f2) |
| 913 | { |
| 914 | uint16_t leastSignificantBits = static_cast<uint16_t>(float32ToFloat16(f1)); |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 915 | uint16_t mostSignificantBits = static_cast<uint16_t>(float32ToFloat16(f2)); |
| 916 | return static_cast<uint32_t>(mostSignificantBits) << 16 | |
| 917 | static_cast<uint32_t>(leastSignificantBits); |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 918 | } |
| 919 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 920 | // Returns two floating-point values obtained by unpacking a 32-bit unsigned integer into a pair of |
| 921 | // 16-bit values, interpreting those values as 16-bit floating-point numbers according to the OpenGL |
| 922 | // ES Specification, and converting them to 32-bit floating-point values. The first float value is |
| 923 | // obtained from the 16 least-significant bits of u; the second component is obtained from the 16 |
| 924 | // most-significant bits of u. |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 925 | inline void unpackHalf2x16(uint32_t u, float *f1, float *f2) |
| 926 | { |
| 927 | uint16_t leastSignificantBits = static_cast<uint16_t>(u & 0xFFFF); |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 928 | uint16_t mostSignificantBits = static_cast<uint16_t>(u >> 16); |
Arun Patole | cdfa8f5 | 2015-06-30 17:48:25 +0530 | [diff] [blame] | 929 | |
| 930 | *f1 = float16ToFloat32(leastSignificantBits); |
| 931 | *f2 = float16ToFloat32(mostSignificantBits); |
| 932 | } |
| 933 | |
Geoff Lang | 451b2b7 | 2017-05-15 15:32:02 -0400 | [diff] [blame] | 934 | inline uint8_t sRGBToLinear(uint8_t srgbValue) |
| 935 | { |
| 936 | float value = srgbValue / 255.0f; |
| 937 | if (value <= 0.04045f) |
| 938 | { |
| 939 | value = value / 12.92f; |
| 940 | } |
| 941 | else |
| 942 | { |
| 943 | value = std::pow((value + 0.055f) / 1.055f, 2.4f); |
| 944 | } |
| 945 | return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f)); |
| 946 | } |
| 947 | |
| 948 | inline uint8_t linearToSRGB(uint8_t linearValue) |
| 949 | { |
| 950 | float value = linearValue / 255.0f; |
| 951 | if (value <= 0.0f) |
| 952 | { |
| 953 | value = 0.0f; |
| 954 | } |
| 955 | else if (value < 0.0031308f) |
| 956 | { |
| 957 | value = value * 12.92f; |
| 958 | } |
| 959 | else if (value < 1.0f) |
| 960 | { |
| 961 | value = std::pow(value, 0.41666f) * 1.055f - 0.055f; |
| 962 | } |
| 963 | else |
| 964 | { |
| 965 | value = 1.0f; |
| 966 | } |
| 967 | return static_cast<uint8_t>(clamp(value * 255.0f + 0.5f, 0.0f, 255.0f)); |
| 968 | } |
| 969 | |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 970 | // Reverse the order of the bits. |
| 971 | inline uint32_t BitfieldReverse(uint32_t value) |
| 972 | { |
| 973 | // TODO(oetuaho@nvidia.com): Optimize this if needed. There don't seem to be compiler intrinsics |
| 974 | // for this, and right now it's not used in performance-critical paths. |
| 975 | uint32_t result = 0u; |
| 976 | for (size_t j = 0u; j < 32u; ++j) |
| 977 | { |
| 978 | result |= (((value >> j) & 1u) << (31u - j)); |
| 979 | } |
| 980 | return result; |
| 981 | } |
| 982 | |
| 983 | // Count the 1 bits. |
Alexey Knyazev | 31e36a6 | 2020-03-14 21:40:58 +0400 | [diff] [blame] | 984 | #if defined(_MSC_VER) && !defined(__clang__) |
| 985 | # if defined(_M_IX86) || defined(_M_X64) |
| 986 | namespace priv |
| 987 | { |
| 988 | // Check POPCNT instruction support and cache the result. |
| 989 | // https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64#remarks |
| 990 | static const bool kHasPopcnt = [] { |
| 991 | int info[4]; |
| 992 | __cpuid(&info[0], 1); |
| 993 | return static_cast<bool>(info[2] & 0x800000); |
| 994 | }(); |
| 995 | } // namespace priv |
| 996 | |
| 997 | // Polyfills for x86/x64 CPUs without POPCNT. |
| 998 | // https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel |
| 999 | inline int BitCountPolyfill(uint32_t bits) |
| 1000 | { |
| 1001 | bits = bits - ((bits >> 1) & 0x55555555); |
| 1002 | bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333); |
| 1003 | bits = ((bits + (bits >> 4) & 0x0F0F0F0F) * 0x01010101) >> 24; |
| 1004 | return static_cast<int>(bits); |
| 1005 | } |
| 1006 | |
| 1007 | inline int BitCountPolyfill(uint64_t bits) |
| 1008 | { |
| 1009 | bits = bits - ((bits >> 1) & 0x5555555555555555ull); |
| 1010 | bits = (bits & 0x3333333333333333ull) + ((bits >> 2) & 0x3333333333333333ull); |
| 1011 | bits = ((bits + (bits >> 4) & 0x0F0F0F0F0F0F0F0Full) * 0x0101010101010101ull) >> 56; |
| 1012 | return static_cast<int>(bits); |
| 1013 | } |
| 1014 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1015 | inline int BitCount(uint32_t bits) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1016 | { |
Alexey Knyazev | 31e36a6 | 2020-03-14 21:40:58 +0400 | [diff] [blame] | 1017 | if (priv::kHasPopcnt) |
| 1018 | { |
| 1019 | return static_cast<int>(__popcnt(bits)); |
| 1020 | } |
| 1021 | return BitCountPolyfill(bits); |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1022 | } |
Alexey Knyazev | 31e36a6 | 2020-03-14 21:40:58 +0400 | [diff] [blame] | 1023 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1024 | inline int BitCount(uint64_t bits) |
| 1025 | { |
Alexey Knyazev | 31e36a6 | 2020-03-14 21:40:58 +0400 | [diff] [blame] | 1026 | if (priv::kHasPopcnt) |
| 1027 | { |
| 1028 | # if defined(_M_X64) |
| 1029 | return static_cast<int>(__popcnt64(bits)); |
| 1030 | # else // x86 |
| 1031 | return static_cast<int>(__popcnt(static_cast<uint32_t>(bits >> 32)) + |
| 1032 | __popcnt(static_cast<uint32_t>(bits))); |
| 1033 | # endif // defined(_M_X64) |
| 1034 | } |
| 1035 | return BitCountPolyfill(bits); |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1036 | } |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1037 | |
Alexey Knyazev | 31e36a6 | 2020-03-14 21:40:58 +0400 | [diff] [blame] | 1038 | # elif defined(_M_ARM) || defined(_M_ARM64) |
| 1039 | |
| 1040 | // MSVC's _CountOneBits* intrinsics are not defined for ARM64, moreover they do not use dedicated |
| 1041 | // NEON instructions. |
| 1042 | |
| 1043 | inline int BitCount(uint32_t bits) |
| 1044 | { |
| 1045 | // cast bits to 8x8 datatype and use VCNT on it |
| 1046 | const uint8x8_t vsum = vcnt_u8(vcreate_u8(static_cast<uint64_t>(bits))); |
| 1047 | |
| 1048 | // pairwise sums: 8x8 -> 16x4 -> 32x2 |
| 1049 | return static_cast<int>(vget_lane_u32(vpaddl_u16(vpaddl_u8(vsum)), 0)); |
| 1050 | } |
| 1051 | |
| 1052 | inline int BitCount(uint64_t bits) |
| 1053 | { |
| 1054 | // cast bits to 8x8 datatype and use VCNT on it |
| 1055 | const uint8x8_t vsum = vcnt_u8(vcreate_u8(bits)); |
| 1056 | |
| 1057 | // pairwise sums: 8x8 -> 16x4 -> 32x2 -> 64x1 |
| 1058 | return static_cast<int>(vget_lane_u64(vpaddl_u32(vpaddl_u16(vpaddl_u8(vsum))), 0)); |
| 1059 | } |
| 1060 | # endif // defined(_M_IX86) || defined(_M_X64) |
| 1061 | #endif // defined(_MSC_VER) && !defined(__clang__) |
| 1062 | |
| 1063 | #if defined(ANGLE_PLATFORM_POSIX) || defined(__clang__) |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1064 | inline int BitCount(uint32_t bits) |
| 1065 | { |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1066 | return __builtin_popcount(bits); |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1069 | inline int BitCount(uint64_t bits) |
| 1070 | { |
| 1071 | return __builtin_popcountll(bits); |
| 1072 | } |
Alexey Knyazev | 31e36a6 | 2020-03-14 21:40:58 +0400 | [diff] [blame] | 1073 | #endif // defined(ANGLE_PLATFORM_POSIX) || defined(__clang__) |
Jeff Gilbert | c0b8233 | 2018-09-26 18:04:05 -0700 | [diff] [blame] | 1074 | |
Jamie Madill | 7ce4a15 | 2018-06-12 10:19:49 -0400 | [diff] [blame] | 1075 | inline int BitCount(uint8_t bits) |
| 1076 | { |
| 1077 | return BitCount(static_cast<uint32_t>(bits)); |
| 1078 | } |
| 1079 | |
| 1080 | inline int BitCount(uint16_t bits) |
| 1081 | { |
| 1082 | return BitCount(static_cast<uint32_t>(bits)); |
| 1083 | } |
| 1084 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1085 | #if defined(ANGLE_PLATFORM_WINDOWS) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1086 | // 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] | 1087 | // significant bit. Implemented for different bit widths on different platforms. |
| 1088 | inline unsigned long ScanForward(uint32_t bits) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1089 | { |
| 1090 | ASSERT(bits != 0u); |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1091 | unsigned long firstBitIndex = 0ul; |
| 1092 | unsigned char ret = _BitScanForward(&firstBitIndex, bits); |
| 1093 | ASSERT(ret != 0u); |
| 1094 | return firstBitIndex; |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1097 | inline unsigned long ScanForward(uint64_t bits) |
| 1098 | { |
| 1099 | ASSERT(bits != 0u); |
| 1100 | unsigned long firstBitIndex = 0ul; |
Alexey Knyazev | e651944 | 2020-03-12 19:02:03 +0400 | [diff] [blame] | 1101 | # if defined(ANGLE_IS_64_BIT_CPU) |
| 1102 | unsigned char ret = _BitScanForward64(&firstBitIndex, bits); |
| 1103 | # else |
| 1104 | unsigned char ret; |
| 1105 | if (static_cast<uint32_t>(bits) == 0) |
| 1106 | { |
| 1107 | ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits >> 32)); |
| 1108 | firstBitIndex += 32ul; |
| 1109 | } |
| 1110 | else |
| 1111 | { |
| 1112 | ret = _BitScanForward(&firstBitIndex, static_cast<uint32_t>(bits)); |
| 1113 | } |
| 1114 | # endif // defined(ANGLE_IS_64_BIT_CPU) |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1115 | ASSERT(ret != 0u); |
| 1116 | return firstBitIndex; |
| 1117 | } |
Alexey Knyazev | e651944 | 2020-03-12 19:02:03 +0400 | [diff] [blame] | 1118 | #endif // defined(ANGLE_PLATFORM_WINDOWS) |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1119 | |
| 1120 | #if defined(ANGLE_PLATFORM_POSIX) |
| 1121 | inline unsigned long ScanForward(uint32_t bits) |
| 1122 | { |
| 1123 | ASSERT(bits != 0u); |
| 1124 | return static_cast<unsigned long>(__builtin_ctz(bits)); |
| 1125 | } |
| 1126 | |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1127 | inline unsigned long ScanForward(uint64_t bits) |
| 1128 | { |
| 1129 | ASSERT(bits != 0u); |
Alexey Knyazev | e651944 | 2020-03-12 19:02:03 +0400 | [diff] [blame] | 1130 | # if defined(ANGLE_IS_64_BIT_CPU) |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1131 | return static_cast<unsigned long>(__builtin_ctzll(bits)); |
Alexey Knyazev | e651944 | 2020-03-12 19:02:03 +0400 | [diff] [blame] | 1132 | # else |
| 1133 | return static_cast<unsigned long>(static_cast<uint32_t>(bits) == 0 |
| 1134 | ? __builtin_ctz(static_cast<uint32_t>(bits >> 32)) + 32 |
| 1135 | : __builtin_ctz(static_cast<uint32_t>(bits))); |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1136 | # endif // defined(ANGLE_IS_64_BIT_CPU) |
Alexey Knyazev | e651944 | 2020-03-12 19:02:03 +0400 | [diff] [blame] | 1137 | } |
| 1138 | #endif // defined(ANGLE_PLATFORM_POSIX) |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1139 | |
Jamie Madill | 7ce4a15 | 2018-06-12 10:19:49 -0400 | [diff] [blame] | 1140 | inline unsigned long ScanForward(uint8_t bits) |
| 1141 | { |
| 1142 | return ScanForward(static_cast<uint32_t>(bits)); |
| 1143 | } |
| 1144 | |
| 1145 | inline unsigned long ScanForward(uint16_t bits) |
| 1146 | { |
| 1147 | return ScanForward(static_cast<uint32_t>(bits)); |
| 1148 | } |
| 1149 | |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1150 | // Return the index of the most significant bit set. Indexing is such that bit 0 is the least |
| 1151 | // significant bit. |
| 1152 | inline unsigned long ScanReverse(unsigned long bits) |
| 1153 | { |
| 1154 | ASSERT(bits != 0u); |
| 1155 | #if defined(ANGLE_PLATFORM_WINDOWS) |
| 1156 | unsigned long lastBitIndex = 0ul; |
| 1157 | unsigned char ret = _BitScanReverse(&lastBitIndex, bits); |
| 1158 | ASSERT(ret != 0u); |
| 1159 | return lastBitIndex; |
| 1160 | #elif defined(ANGLE_PLATFORM_POSIX) |
| 1161 | return static_cast<unsigned long>(sizeof(unsigned long) * CHAR_BIT - 1 - __builtin_clzl(bits)); |
| 1162 | #else |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1163 | # error Please implement bit-scan-reverse for your platform! |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1164 | #endif |
| 1165 | } |
| 1166 | |
| 1167 | // 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] | 1168 | template <typename T> |
| 1169 | int FindLSB(T bits) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1170 | { |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1171 | static_assert(std::is_integral<T>::value, "must be integral type."); |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1172 | if (bits == 0u) |
| 1173 | { |
| 1174 | return -1; |
| 1175 | } |
| 1176 | else |
| 1177 | { |
| 1178 | return static_cast<int>(ScanForward(bits)); |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | // 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] | 1183 | template <typename T> |
| 1184 | int FindMSB(T bits) |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1185 | { |
Jamie Madill | 6de5185 | 2017-04-12 09:53:01 -0400 | [diff] [blame] | 1186 | static_assert(std::is_integral<T>::value, "must be integral type."); |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1187 | if (bits == 0u) |
| 1188 | { |
| 1189 | return -1; |
| 1190 | } |
| 1191 | else |
| 1192 | { |
| 1193 | return static_cast<int>(ScanReverse(bits)); |
| 1194 | } |
| 1195 | } |
| 1196 | |
Arun Patole | 551279e | 2015-07-07 18:18:23 +0530 | [diff] [blame] | 1197 | // Returns whether the argument is Not a Number. |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 1198 | // IEEE 754 single precision NaN representation: Exponent(8 bits) - 255, Mantissa(23 bits) - |
| 1199 | // non-zero. |
Arun Patole | 551279e | 2015-07-07 18:18:23 +0530 | [diff] [blame] | 1200 | inline bool isNaN(float f) |
| 1201 | { |
| 1202 | // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u |
| 1203 | // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 1204 | return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) && |
| 1205 | (bitCast<uint32_t>(f) & 0x7fffffu); |
Arun Patole | 551279e | 2015-07-07 18:18:23 +0530 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | // Returns whether the argument is infinity. |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 1209 | // IEEE 754 single precision infinity representation: Exponent(8 bits) - 255, Mantissa(23 bits) - |
| 1210 | // zero. |
Arun Patole | 551279e | 2015-07-07 18:18:23 +0530 | [diff] [blame] | 1211 | inline bool isInf(float f) |
| 1212 | { |
| 1213 | // Exponent mask: ((1u << 8) - 1u) << 23 = 0x7f800000u |
| 1214 | // Mantissa mask: ((1u << 23) - 1u) = 0x7fffffu |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 1215 | return ((bitCast<uint32_t>(f) & 0x7f800000u) == 0x7f800000u) && |
| 1216 | !(bitCast<uint32_t>(f) & 0x7fffffu); |
Arun Patole | 551279e | 2015-07-07 18:18:23 +0530 | [diff] [blame] | 1217 | } |
| 1218 | |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 1219 | namespace priv |
| 1220 | { |
| 1221 | template <unsigned int N, unsigned int R> |
| 1222 | struct iSquareRoot |
| 1223 | { |
| 1224 | static constexpr unsigned int solve() |
| 1225 | { |
| 1226 | return (R * R > N) |
| 1227 | ? 0 |
| 1228 | : ((R * R == N) ? R : static_cast<unsigned int>(iSquareRoot<N, R + 1>::value)); |
| 1229 | } |
| 1230 | enum Result |
| 1231 | { |
| 1232 | value = iSquareRoot::solve() |
| 1233 | }; |
| 1234 | }; |
| 1235 | |
| 1236 | template <unsigned int N> |
| 1237 | struct iSquareRoot<N, N> |
| 1238 | { |
| 1239 | enum result |
| 1240 | { |
| 1241 | value = N |
| 1242 | }; |
| 1243 | }; |
| 1244 | |
| 1245 | } // namespace priv |
| 1246 | |
| 1247 | template <unsigned int N> |
| 1248 | constexpr unsigned int iSquareRoot() |
| 1249 | { |
| 1250 | return priv::iSquareRoot<N, 1>::value; |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 1251 | } |
| 1252 | |
Olli Etuaho | 7f9a55f | 2016-10-03 14:32:08 +0100 | [diff] [blame] | 1253 | // Sum, difference and multiplication operations for signed ints that wrap on 32-bit overflow. |
| 1254 | // |
| 1255 | // Unsigned types are defined to do arithmetic modulo 2^n in C++. For signed types, overflow |
| 1256 | // behavior is undefined. |
| 1257 | |
| 1258 | template <typename T> |
| 1259 | inline T WrappingSum(T lhs, T rhs) |
| 1260 | { |
| 1261 | uint32_t lhsUnsigned = static_cast<uint32_t>(lhs); |
| 1262 | uint32_t rhsUnsigned = static_cast<uint32_t>(rhs); |
| 1263 | return static_cast<T>(lhsUnsigned + rhsUnsigned); |
| 1264 | } |
| 1265 | |
| 1266 | template <typename T> |
| 1267 | inline T WrappingDiff(T lhs, T rhs) |
| 1268 | { |
| 1269 | uint32_t lhsUnsigned = static_cast<uint32_t>(lhs); |
| 1270 | uint32_t rhsUnsigned = static_cast<uint32_t>(rhs); |
| 1271 | return static_cast<T>(lhsUnsigned - rhsUnsigned); |
| 1272 | } |
| 1273 | |
| 1274 | inline int32_t WrappingMul(int32_t lhs, int32_t rhs) |
| 1275 | { |
| 1276 | int64_t lhsWide = static_cast<int64_t>(lhs); |
| 1277 | int64_t rhsWide = static_cast<int64_t>(rhs); |
| 1278 | // The multiplication is guaranteed not to overflow. |
| 1279 | int64_t resultWide = lhsWide * rhsWide; |
| 1280 | // Implement the desired wrapping behavior by masking out the high-order 32 bits. |
| 1281 | resultWide = resultWide & 0xffffffffll; |
| 1282 | // Casting to a narrower signed type is fine since the casted value is representable in the |
| 1283 | // narrower type. |
| 1284 | return static_cast<int32_t>(resultWide); |
| 1285 | } |
| 1286 | |
Lingfeng Yang | 0df813c | 2018-07-12 12:52:06 -0700 | [diff] [blame] | 1287 | inline float scaleScreenDimensionToNdc(float dimensionScreen, float viewportDimension) |
| 1288 | { |
| 1289 | return 2.0f * dimensionScreen / viewportDimension; |
| 1290 | } |
| 1291 | |
| 1292 | inline float scaleScreenCoordinateToNdc(float coordinateScreen, float viewportDimension) |
| 1293 | { |
| 1294 | float halfShifted = coordinateScreen / viewportDimension; |
| 1295 | return 2.0f * (halfShifted - 0.5f); |
| 1296 | } |
| 1297 | |
Sami Väisänen | e45e53b | 2016-05-25 10:36:04 +0300 | [diff] [blame] | 1298 | } // namespace gl |
| 1299 | |
Geoff Lang | 831b195 | 2015-05-05 11:02:27 -0400 | [diff] [blame] | 1300 | namespace rx |
| 1301 | { |
| 1302 | |
shannonwoods@chromium.org | 06d4e84 | 2013-05-30 00:04:42 +0000 | [diff] [blame] | 1303 | template <typename T> |
| 1304 | T roundUp(const T value, const T alignment) |
| 1305 | { |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 1306 | auto temp = value + alignment - static_cast<T>(1); |
| 1307 | return temp - temp % alignment; |
| 1308 | } |
| 1309 | |
| 1310 | template <typename T> |
Jiacheng Lu | 3e493c4 | 2019-07-29 16:27:01 -0600 | [diff] [blame] | 1311 | constexpr T roundUpPow2(const T value, const T alignment) |
| 1312 | { |
| 1313 | ASSERT(gl::isPow2(alignment)); |
| 1314 | return (value + alignment - 1) & ~(alignment - 1); |
| 1315 | } |
| 1316 | |
| 1317 | template <typename T> |
Jamie Madill | e2e406c | 2016-06-02 13:04:10 -0400 | [diff] [blame] | 1318 | angle::CheckedNumeric<T> CheckedRoundUp(const T value, const T alignment) |
| 1319 | { |
| 1320 | angle::CheckedNumeric<T> checkedValue(value); |
| 1321 | angle::CheckedNumeric<T> checkedAlignment(alignment); |
| 1322 | return roundUp(checkedValue, checkedAlignment); |
shannonwoods@chromium.org | 06d4e84 | 2013-05-30 00:04:42 +0000 | [diff] [blame] | 1323 | } |
| 1324 | |
Jamie Madill | 5993d89 | 2019-06-03 13:05:38 -0400 | [diff] [blame] | 1325 | inline constexpr unsigned int UnsignedCeilDivide(unsigned int value, unsigned int divisor) |
Jamie Madill | aff8084 | 2014-08-04 10:47:56 -0400 | [diff] [blame] | 1326 | { |
| 1327 | unsigned int divided = value / divisor; |
| 1328 | return (divided + ((value % divisor == 0) ? 0 : 1)); |
| 1329 | } |
| 1330 | |
Jose Dapena Paz | dd3de6f | 2019-07-22 21:17:52 +0200 | [diff] [blame] | 1331 | #if defined(__has_builtin) |
| 1332 | # define ANGLE_HAS_BUILTIN(x) __has_builtin(x) |
| 1333 | #else |
| 1334 | # define ANGLE_HAS_BUILTIN(x) 0 |
| 1335 | #endif |
| 1336 | |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 1337 | #if defined(_MSC_VER) |
| 1338 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1339 | # define ANGLE_ROTL(x, y) _rotl(x, y) |
Shahbaz Youssefi | e911188 | 2019-07-12 16:15:31 -0400 | [diff] [blame] | 1340 | # define ANGLE_ROTL64(x, y) _rotl64(x, y) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1341 | # define ANGLE_ROTR16(x, y) _rotr16(x, y) |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 1342 | |
Jose Dapena Paz | dd3de6f | 2019-07-22 21:17:52 +0200 | [diff] [blame] | 1343 | #elif defined(__clang__) && ANGLE_HAS_BUILTIN(__builtin_rotateleft32) && \ |
| 1344 | ANGLE_HAS_BUILTIN(__builtin_rotateleft64) && ANGLE_HAS_BUILTIN(__builtin_rotateright16) |
Shahbaz Youssefi | e911188 | 2019-07-12 16:15:31 -0400 | [diff] [blame] | 1345 | |
| 1346 | # define ANGLE_ROTL(x, y) __builtin_rotateleft32(x, y) |
| 1347 | # define ANGLE_ROTL64(x, y) __builtin_rotateleft64(x, y) |
| 1348 | # define ANGLE_ROTR16(x, y) __builtin_rotateright16(x, y) |
| 1349 | |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 1350 | #else |
| 1351 | |
| 1352 | inline uint32_t RotL(uint32_t x, int8_t r) |
| 1353 | { |
| 1354 | return (x << r) | (x >> (32 - r)); |
| 1355 | } |
| 1356 | |
Shahbaz Youssefi | e911188 | 2019-07-12 16:15:31 -0400 | [diff] [blame] | 1357 | inline uint64_t RotL64(uint64_t x, int8_t r) |
| 1358 | { |
| 1359 | return (x << r) | (x >> (64 - r)); |
| 1360 | } |
| 1361 | |
Austin Kinross | 405f347 | 2015-06-04 13:09:06 -0700 | [diff] [blame] | 1362 | inline uint16_t RotR16(uint16_t x, int8_t r) |
| 1363 | { |
| 1364 | return (x >> r) | (x << (16 - r)); |
| 1365 | } |
| 1366 | |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1367 | # define ANGLE_ROTL(x, y) ::rx::RotL(x, y) |
Shahbaz Youssefi | e911188 | 2019-07-12 16:15:31 -0400 | [diff] [blame] | 1368 | # define ANGLE_ROTL64(x, y) ::rx::RotL64(x, y) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1369 | # define ANGLE_ROTR16(x, y) ::rx::RotR16(x, y) |
Jamie Madill | 3b9bb72 | 2015-01-05 16:17:02 -0500 | [diff] [blame] | 1370 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 1371 | #endif // namespace rx |
Jamie Madill | 3f0c4a5 | 2019-01-10 10:20:35 -0500 | [diff] [blame] | 1372 | |
| 1373 | constexpr unsigned int Log2(unsigned int bytes) |
| 1374 | { |
| 1375 | return bytes == 1 ? 0 : (1 + Log2(bytes / 2)); |
| 1376 | } |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 1377 | } // namespace rx |
shannon.woods@transgaming.com | b356025 | 2013-02-28 23:07:04 +0000 | [diff] [blame] | 1378 | |
Lingfeng Yang | 13b708f | 2018-03-21 12:14:10 -0700 | [diff] [blame] | 1379 | #endif // COMMON_MATHUTIL_H_ |