Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 1 | // Copyright 2021 The Tint Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
dan sinclair | b14a778 | 2023-07-20 09:21:10 +0000 | [diff] [blame] | 15 | #ifndef SRC_TINT_LANG_BASE_BUILTIN_NUMBER_H_ |
| 16 | #define SRC_TINT_LANG_BASE_BUILTIN_NUMBER_H_ |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 17 | |
| 18 | #include <stdint.h> |
Ben Clayton | cf52af7 | 2022-06-29 17:09:11 +0000 | [diff] [blame] | 19 | #include <cmath> |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 20 | #include <functional> |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 21 | #include <limits> |
dan sinclair | 0a4e2a1 | 2022-06-18 14:34:00 +0000 | [diff] [blame] | 22 | #include <optional> |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 23 | |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame^] | 24 | #include "src/tint/utils/macros/compiler.h" |
| 25 | #include "src/tint/utils/result/result.h" |
| 26 | #include "src/tint/utils/text/string_stream.h" |
| 27 | #include "src/tint/utils/traits/traits.h" |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 28 | |
| 29 | // Forward declaration |
| 30 | namespace tint { |
| 31 | /// Number wraps a integer or floating point number, enforcing explicit casting. |
| 32 | template <typename T> |
| 33 | struct Number; |
| 34 | } // namespace tint |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 35 | |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 36 | namespace tint::detail { |
Antonio Maiorano | c20c5df | 2022-09-01 14:57:39 +0000 | [diff] [blame] | 37 | /// Base template for IsNumber |
| 38 | template <typename T> |
| 39 | struct IsNumber : std::false_type {}; |
| 40 | |
| 41 | /// Specialization for IsNumber |
| 42 | template <typename T> |
| 43 | struct IsNumber<Number<T>> : std::true_type {}; |
| 44 | |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 45 | /// An empty structure used as a unique template type for Number when |
| 46 | /// specializing for the f16 type. |
| 47 | struct NumberKindF16 {}; |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 48 | |
| 49 | /// Helper for obtaining the underlying type for a Number. |
| 50 | template <typename T> |
| 51 | struct NumberUnwrapper { |
| 52 | /// When T is not a Number, then type defined to be T. |
| 53 | using type = T; |
| 54 | }; |
| 55 | |
| 56 | /// NumberUnwrapper specialization for Number<T>. |
| 57 | template <typename T> |
| 58 | struct NumberUnwrapper<Number<T>> { |
| 59 | /// The Number's underlying type. |
| 60 | using type = typename Number<T>::type; |
| 61 | }; |
| 62 | |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 63 | } // namespace tint::detail |
| 64 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 65 | namespace tint { |
| 66 | |
Antonio Maiorano | c20c5df | 2022-09-01 14:57:39 +0000 | [diff] [blame] | 67 | /// Evaluates to true iff T is a Number |
| 68 | template <typename T> |
Ben Clayton | 66805b0 | 2023-06-14 22:00:01 +0000 | [diff] [blame] | 69 | constexpr bool IsNumber = tint::detail::IsNumber<T>::value; |
Antonio Maiorano | c20c5df | 2022-09-01 14:57:39 +0000 | [diff] [blame] | 70 | |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 71 | /// Resolves to the underlying type for a Number. |
| 72 | template <typename T> |
Ben Clayton | 66805b0 | 2023-06-14 22:00:01 +0000 | [diff] [blame] | 73 | using UnwrapNumber = typename tint::detail::NumberUnwrapper<T>::type; |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 74 | |
Antonio Maiorano | 3740ac6 | 2022-09-03 22:42:51 +0000 | [diff] [blame] | 75 | /// Evaluates to true iff T or Number<T> is a floating-point type or is NumberKindF16. |
Antonio Maiorano | 6b3f4aa | 2022-10-04 22:40:32 +0000 | [diff] [blame] | 76 | template <typename T> |
| 77 | constexpr bool IsFloatingPoint = std::is_floating_point_v<UnwrapNumber<T>> || |
Ben Clayton | 66805b0 | 2023-06-14 22:00:01 +0000 | [diff] [blame] | 78 | std::is_same_v<UnwrapNumber<T>, tint::detail::NumberKindF16>; |
Antonio Maiorano | 3740ac6 | 2022-09-03 22:42:51 +0000 | [diff] [blame] | 79 | |
| 80 | /// Evaluates to true iff T or Number<T> is an integral type. |
Antonio Maiorano | 11e2571 | 2022-09-06 18:40:33 +0000 | [diff] [blame] | 81 | template <typename T> |
| 82 | constexpr bool IsIntegral = std::is_integral_v<UnwrapNumber<T>>; |
Antonio Maiorano | 3740ac6 | 2022-09-03 22:42:51 +0000 | [diff] [blame] | 83 | |
| 84 | /// Evaluates to true iff T or Number<T> is a signed integer type. |
Antonio Maiorano | 11e2571 | 2022-09-06 18:40:33 +0000 | [diff] [blame] | 85 | template <typename T> |
| 86 | constexpr bool IsSignedIntegral = |
| 87 | std::is_integral_v<UnwrapNumber<T>> && std::is_signed_v<UnwrapNumber<T>>; |
Antonio Maiorano | 3740ac6 | 2022-09-03 22:42:51 +0000 | [diff] [blame] | 88 | |
| 89 | /// Evaluates to true iff T or Number<T> is an unsigned integer type. |
Antonio Maiorano | 11e2571 | 2022-09-06 18:40:33 +0000 | [diff] [blame] | 90 | template <typename T> |
| 91 | constexpr bool IsUnsignedIntegral = |
| 92 | std::is_integral_v<UnwrapNumber<T>> && std::is_unsigned_v<UnwrapNumber<T>>; |
Antonio Maiorano | 3740ac6 | 2022-09-03 22:42:51 +0000 | [diff] [blame] | 93 | |
| 94 | /// Evaluates to true iff T is an integer type, floating-point type or is NumberKindF16. |
| 95 | template <typename T> |
| 96 | constexpr bool IsNumeric = IsIntegral<T> || IsFloatingPoint<T>; |
| 97 | |
Antonio Maiorano | 5f33fac | 2022-09-23 21:58:29 +0000 | [diff] [blame] | 98 | /// Returns the bit width of T |
| 99 | template <typename T> |
| 100 | constexpr size_t BitWidth = sizeof(UnwrapNumber<T>) * 8; |
| 101 | |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 102 | /// NumberBase is a CRTP base class for Number<T> |
| 103 | template <typename NumberT> |
| 104 | struct NumberBase { |
| 105 | /// @returns value of type `Number<T>` with the highest value for that type. |
| 106 | static NumberT Highest() { return NumberT(NumberT::kHighestValue); } |
| 107 | /// @returns value of type `Number<T>` with the lowest value for that type. |
| 108 | static NumberT Lowest() { return NumberT(NumberT::kLowestValue); } |
| 109 | /// @returns value of type `Number<T>` with the smallest value for that type. |
| 110 | static NumberT Smallest() { return NumberT(NumberT::kSmallestValue); } |
| 111 | /// @returns value of type `Number<T>` that represents NaN for that type. |
| 112 | static NumberT NaN() { |
| 113 | return NumberT(std::numeric_limits<UnwrapNumber<NumberT>>::quiet_NaN()); |
| 114 | } |
| 115 | /// @returns value of type `Number<T>` that represents infinity for that type. |
| 116 | static NumberT Inf() { return NumberT(std::numeric_limits<UnwrapNumber<NumberT>>::infinity()); } |
| 117 | }; |
| 118 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 119 | /// Number wraps a integer or floating point number, enforcing explicit casting. |
| 120 | template <typename T> |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 121 | struct Number : NumberBase<Number<T>> { |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 122 | static_assert(IsNumeric<T>, "Number<T> constructed with non-numeric type"); |
| 123 | |
| 124 | /// type is the underlying type of the Number |
| 125 | using type = T; |
| 126 | |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 127 | /// Number of bits in the number. |
| 128 | static constexpr size_t kNumBits = sizeof(T) * 8; |
| 129 | |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 130 | /// Highest finite representable value of this type. |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 131 | static constexpr type kHighestValue = std::numeric_limits<type>::max(); |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 132 | |
| 133 | /// Lowest finite representable value of this type. |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 134 | static constexpr type kLowestValue = std::numeric_limits<type>::lowest(); |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 135 | |
| 136 | /// Smallest positive normal value of this type. |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 137 | static constexpr type kSmallestValue = |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 138 | std::is_integral_v<type> ? 0 : std::numeric_limits<type>::min(); |
| 139 | |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 140 | /// Smallest positive subnormal value of this type, 0 for integral type. |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 141 | static constexpr type kSmallestSubnormalValue = |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 142 | std::is_integral_v<type> ? 0 : std::numeric_limits<type>::denorm_min(); |
| 143 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 144 | /// Constructor. The value is zero-initialized. |
| 145 | Number() = default; |
| 146 | |
| 147 | /// Constructor. |
| 148 | /// @param v the value to initialize this Number to |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 149 | template <typename U> |
| 150 | explicit Number(U v) : value(static_cast<T>(v)) {} |
| 151 | |
| 152 | /// Constructor. |
| 153 | /// @param v the value to initialize this Number to |
| 154 | template <typename U> |
| 155 | explicit Number(Number<U> v) : value(static_cast<T>(v.value)) {} |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 156 | |
| 157 | /// Conversion operator |
| 158 | /// @returns the value as T |
| 159 | operator T() const { return value; } |
| 160 | |
Ben Clayton | 636e3d0 | 2022-05-10 16:02:06 +0000 | [diff] [blame] | 161 | /// Negation operator |
| 162 | /// @returns the negative value of the number |
| 163 | Number operator-() const { return Number(-value); } |
| 164 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 165 | /// Assignment operator |
| 166 | /// @param v the new value |
| 167 | /// @returns this Number so calls can be chained |
| 168 | Number& operator=(T v) { |
| 169 | value = v; |
| 170 | return *this; |
| 171 | } |
| 172 | |
| 173 | /// The number value |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 174 | type value = {}; |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 177 | /// Writes the number to the ostream. |
dan sinclair | 6cc183c | 2023-03-02 21:28:45 +0000 | [diff] [blame] | 178 | /// @param out the stream to write to |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 179 | /// @param num the Number |
dan sinclair | 6cc183c | 2023-03-02 21:28:45 +0000 | [diff] [blame] | 180 | /// @return the stream so calls can be chained |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 181 | template <typename T> |
dan sinclair | 6cc183c | 2023-03-02 21:28:45 +0000 | [diff] [blame] | 182 | inline utils::StringStream& operator<<(utils::StringStream& out, Number<T> num) { |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 183 | return out << num.value; |
| 184 | } |
| 185 | |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 186 | /// The partial specification of Number for f16 type, storing the f16 value as float, |
| 187 | /// and enforcing proper explicit casting. |
| 188 | template <> |
Ben Clayton | 66805b0 | 2023-06-14 22:00:01 +0000 | [diff] [blame] | 189 | struct Number<tint::detail::NumberKindF16> : NumberBase<Number<tint::detail::NumberKindF16>> { |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 190 | /// C++ does not have a native float16 type, so we use a 32-bit float instead. |
| 191 | using type = float; |
| 192 | |
Ben Clayton | 46ee639 | 2022-11-09 22:04:11 +0000 | [diff] [blame] | 193 | /// Number of bits in the number. |
| 194 | static constexpr size_t kNumBits = 16; |
| 195 | |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 196 | /// Highest finite representable value of this type. |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 197 | static constexpr type kHighestValue = 65504.0f; // 2¹⁵ × (1 + 1023/1024) |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 198 | |
| 199 | /// Lowest finite representable value of this type. |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 200 | static constexpr type kLowestValue = -65504.0f; |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 201 | |
| 202 | /// Smallest positive normal value of this type. |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 203 | /// binary16 0_00001_0000000000, value is 2⁻¹⁴. |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 204 | static constexpr type kSmallestValue = 0x1p-14f; |
Zhaoming Jiang | 0fb4e2c | 2022-06-10 18:18:35 +0000 | [diff] [blame] | 205 | |
| 206 | /// Smallest positive subnormal value of this type. |
| 207 | /// binary16 0_00000_0000000001, value is 2⁻¹⁴ * 2⁻¹⁰ = 2⁻²⁴. |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 208 | static constexpr type kSmallestSubnormalValue = 0x1p-24f; |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 209 | |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 210 | /// Constructor. The value is zero-initialized. |
| 211 | Number() = default; |
| 212 | |
| 213 | /// Constructor. |
| 214 | /// @param v the value to initialize this Number to |
| 215 | template <typename U> |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 216 | explicit Number(U v) : value(Quantize(static_cast<type>(v))) {} |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 217 | |
| 218 | /// Constructor. |
| 219 | /// @param v the value to initialize this Number to |
| 220 | template <typename U> |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 221 | explicit Number(Number<U> v) : value(Quantize(static_cast<type>(v.value))) {} |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 222 | |
| 223 | /// Conversion operator |
| 224 | /// @returns the value as the internal representation type of F16 |
| 225 | operator float() const { return value; } |
| 226 | |
| 227 | /// Negation operator |
| 228 | /// @returns the negative value of the number |
Ben Clayton | 66805b0 | 2023-06-14 22:00:01 +0000 | [diff] [blame] | 229 | Number operator-() const { return Number<tint::detail::NumberKindF16>(-value); } |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 230 | |
| 231 | /// Assignment operator with parameter as native floating point type |
| 232 | /// @param v the new value |
| 233 | /// @returns this Number so calls can be chained |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 234 | Number& operator=(type v) { |
| 235 | value = Quantize(v); |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 236 | return *this; |
| 237 | } |
| 238 | |
Zhaoming Jiang | 2c7440a | 2022-07-07 03:29:11 +0000 | [diff] [blame] | 239 | /// Get the binary16 bit pattern in type uint16_t of this value. |
| 240 | /// @returns the binary16 bit pattern, in type uint16_t, of the stored quantized f16 value. If |
| 241 | /// the value is NaN, the returned value will be 0x7e00u. If the value is positive infinity, the |
| 242 | /// returned value will be 0x7c00u. If the input value is negative infinity, the returned value |
| 243 | /// will be 0xfc00u. |
| 244 | uint16_t BitsRepresentation() const; |
| 245 | |
dan sinclair | 00d0fd5 | 2022-11-09 20:03:09 +0000 | [diff] [blame] | 246 | /// Creates an f16 value from the uint16_t bit representation. |
| 247 | /// @param bits the bits to convert from |
| 248 | /// @returns the binary16 value based off the provided bit pattern. |
Ben Clayton | 66805b0 | 2023-06-14 22:00:01 +0000 | [diff] [blame] | 249 | static Number<tint::detail::NumberKindF16> FromBits(uint16_t bits); |
dan sinclair | 00d0fd5 | 2022-11-09 20:03:09 +0000 | [diff] [blame] | 250 | |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 251 | /// @param value the input float32 value |
| 252 | /// @returns the float32 value quantized to the smaller float16 value, through truncation of the |
| 253 | /// mantissa bits (no rounding). If the float32 value is too large (positive or negative) to be |
| 254 | /// represented by a float16 value, then the returned value will be positive or negative |
| 255 | /// infinity. |
| 256 | static type Quantize(type value); |
| 257 | |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 258 | /// The number value, stored as float |
Ben Clayton | c2eccfc | 2022-05-25 15:04:24 +0000 | [diff] [blame] | 259 | type value = {}; |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 260 | }; |
| 261 | |
Ben Clayton | 508e4a5 | 2022-05-10 22:08:04 +0000 | [diff] [blame] | 262 | /// `AInt` is a type alias to `Number<int64_t>`. |
| 263 | using AInt = Number<int64_t>; |
| 264 | /// `AFloat` is a type alias to `Number<double>`. |
| 265 | using AFloat = Number<double>; |
| 266 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 267 | /// `i32` is a type alias to `Number<int32_t>`. |
| 268 | using i32 = Number<int32_t>; |
| 269 | /// `u32` is a type alias to `Number<uint32_t>`. |
| 270 | using u32 = Number<uint32_t>; |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 271 | /// `f32` is a type alias to `Number<float>` |
| 272 | using f32 = Number<float>; |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 273 | /// `f16` is a type alias to `Number<detail::NumberKindF16>`, which should be IEEE 754 binary16. |
| 274 | /// However since C++ don't have native binary16 type, the value is stored as float. |
Ben Clayton | 66805b0 | 2023-06-14 22:00:01 +0000 | [diff] [blame] | 275 | using f16 = Number<tint::detail::NumberKindF16>; |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 276 | |
David Neto | 27c6d98 | 2023-06-19 16:49:10 +0000 | [diff] [blame] | 277 | /// The algorithms in this module require support for infinity and quiet NaNs on |
| 278 | /// floating point types. |
| 279 | static_assert(std::numeric_limits<float>::has_infinity); |
| 280 | static_assert(std::numeric_limits<float>::has_quiet_NaN); |
| 281 | static_assert(std::numeric_limits<double>::has_infinity); |
| 282 | static_assert(std::numeric_limits<double>::has_quiet_NaN); |
| 283 | |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 284 | template <typename T, utils::traits::EnableIf<IsFloatingPoint<T>>* = nullptr> |
dan sinclair | efe9c49 | 2022-11-21 18:00:01 +0000 | [diff] [blame] | 285 | inline const auto kPi = T(UnwrapNumber<T>(3.14159265358979323846)); |
| 286 | |
Antonio Maiorano | 5f33fac | 2022-09-23 21:58:29 +0000 | [diff] [blame] | 287 | /// True iff T is an abstract number type |
| 288 | template <typename T> |
| 289 | constexpr bool IsAbstract = std::is_same_v<T, AInt> || std::is_same_v<T, AFloat>; |
| 290 | |
Antonio Maiorano | c20c5df | 2022-09-01 14:57:39 +0000 | [diff] [blame] | 291 | /// @returns the friendly name of Number type T |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 292 | template <typename T, utils::traits::EnableIf<IsNumber<T>>* = nullptr> |
Antonio Maiorano | c20c5df | 2022-09-01 14:57:39 +0000 | [diff] [blame] | 293 | const char* FriendlyName() { |
| 294 | if constexpr (std::is_same_v<T, AInt>) { |
| 295 | return "abstract-int"; |
| 296 | } else if constexpr (std::is_same_v<T, AFloat>) { |
| 297 | return "abstract-float"; |
| 298 | } else if constexpr (std::is_same_v<T, i32>) { |
| 299 | return "i32"; |
| 300 | } else if constexpr (std::is_same_v<T, u32>) { |
| 301 | return "u32"; |
| 302 | } else if constexpr (std::is_same_v<T, f32>) { |
| 303 | return "f32"; |
| 304 | } else if constexpr (std::is_same_v<T, f16>) { |
| 305 | return "f16"; |
| 306 | } else { |
| 307 | static_assert(!sizeof(T), "Unhandled type"); |
| 308 | } |
| 309 | } |
| 310 | |
Antonio Maiorano | 29aa613 | 2022-09-07 19:34:44 +0000 | [diff] [blame] | 311 | /// @returns the friendly name of T when T is bool |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 312 | template <typename T, utils::traits::EnableIf<std::is_same_v<T, bool>>* = nullptr> |
Antonio Maiorano | 29aa613 | 2022-09-07 19:34:44 +0000 | [diff] [blame] | 313 | const char* FriendlyName() { |
| 314 | return "bool"; |
| 315 | } |
| 316 | |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 317 | /// Enumerator of failure reasons when converting from one number to another. |
| 318 | enum class ConversionFailure { |
| 319 | kExceedsPositiveLimit, // The value was too big (+'ve) to fit in the target type |
| 320 | kExceedsNegativeLimit, // The value was too big (-'ve) to fit in the target type |
| 321 | }; |
| 322 | |
| 323 | /// Writes the conversion failure message to the ostream. |
dan sinclair | 6cc183c | 2023-03-02 21:28:45 +0000 | [diff] [blame] | 324 | /// @param out the stream to write to |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 325 | /// @param failure the ConversionFailure |
dan sinclair | 6cc183c | 2023-03-02 21:28:45 +0000 | [diff] [blame] | 326 | /// @return the stream so calls can be chained |
| 327 | utils::StringStream& operator<<(utils::StringStream& out, ConversionFailure failure); |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 328 | |
| 329 | /// Converts a number from one type to another, checking that the value fits in the target type. |
| 330 | /// @returns the resulting value of the conversion, or a failure reason. |
| 331 | template <typename TO, typename FROM> |
| 332 | utils::Result<TO, ConversionFailure> CheckedConvert(Number<FROM> num) { |
Ben Clayton | 2b4df78 | 2022-06-29 11:31:41 +0000 | [diff] [blame] | 333 | // Use the highest-precision integer or floating-point type to perform the comparisons. |
| 334 | using T = std::conditional_t<IsFloatingPoint<UnwrapNumber<TO>> || IsFloatingPoint<FROM>, |
| 335 | AFloat::type, AInt::type>; |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 336 | const auto value = static_cast<T>(num.value); |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 337 | if (value > static_cast<T>(TO::kHighestValue)) { |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 338 | return ConversionFailure::kExceedsPositiveLimit; |
| 339 | } |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 340 | if (value < static_cast<T>(TO::kLowestValue)) { |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 341 | return ConversionFailure::kExceedsNegativeLimit; |
| 342 | } |
| 343 | return TO(value); // Success |
| 344 | } |
| 345 | |
Ben Clayton | cf52af7 | 2022-06-29 17:09:11 +0000 | [diff] [blame] | 346 | /// Equality operator. |
| 347 | /// @param a the LHS number |
| 348 | /// @param b the RHS number |
| 349 | /// @returns true if the numbers `a` and `b` are exactly equal. Also considers sign bit. |
| 350 | template <typename A, typename B> |
| 351 | bool operator==(Number<A> a, Number<B> b) { |
| 352 | // Use the highest-precision integer or floating-point type to perform the comparisons. |
| 353 | using T = |
| 354 | std::conditional_t<IsFloatingPoint<A> || IsFloatingPoint<B>, AFloat::type, AInt::type>; |
| 355 | auto va = static_cast<T>(a.value); |
| 356 | auto vb = static_cast<T>(b.value); |
| 357 | if constexpr (IsFloatingPoint<T>) { |
| 358 | if (std::signbit(va) != std::signbit(vb)) { |
| 359 | return false; |
| 360 | } |
| 361 | } |
| 362 | return std::equal_to<T>()(va, vb); |
| 363 | } |
| 364 | |
| 365 | /// Inequality operator. |
| 366 | /// @param a the LHS number |
| 367 | /// @param b the RHS number |
| 368 | /// @returns true if the numbers `a` and `b` are exactly unequal. Also considers sign bit. |
| 369 | template <typename A, typename B> |
| 370 | bool operator!=(Number<A> a, Number<B> b) { |
| 371 | return !(a == b); |
| 372 | } |
| 373 | |
| 374 | /// Equality operator. |
| 375 | /// @param a the LHS number |
| 376 | /// @param b the RHS number |
| 377 | /// @returns true if the numbers `a` and `b` are exactly equal. |
| 378 | template <typename A, typename B> |
| 379 | std::enable_if_t<IsNumeric<B>, bool> operator==(Number<A> a, B b) { |
| 380 | return a == Number<B>(b); |
| 381 | } |
| 382 | |
| 383 | /// Inequality operator. |
| 384 | /// @param a the LHS number |
| 385 | /// @param b the RHS number |
| 386 | /// @returns true if the numbers `a` and `b` are exactly unequal. |
| 387 | template <typename A, typename B> |
| 388 | std::enable_if_t<IsNumeric<B>, bool> operator!=(Number<A> a, B b) { |
| 389 | return !(a == b); |
| 390 | } |
| 391 | |
| 392 | /// Equality operator. |
| 393 | /// @param a the LHS number |
| 394 | /// @param b the RHS number |
| 395 | /// @returns true if the numbers `a` and `b` are exactly equal. |
| 396 | template <typename A, typename B> |
| 397 | std::enable_if_t<IsNumeric<A>, bool> operator==(A a, Number<B> b) { |
| 398 | return Number<A>(a) == b; |
| 399 | } |
| 400 | |
| 401 | /// Inequality operator. |
| 402 | /// @param a the LHS number |
| 403 | /// @param b the RHS number |
| 404 | /// @returns true if the numbers `a` and `b` are exactly unequal. |
| 405 | template <typename A, typename B> |
| 406 | std::enable_if_t<IsNumeric<A>, bool> operator!=(A a, Number<B> b) { |
| 407 | return !(a == b); |
| 408 | } |
| 409 | |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 410 | /// Define 'TINT_HAS_OVERFLOW_BUILTINS' if the compiler provide overflow checking builtins. |
| 411 | /// If the compiler does not support these builtins, then these are emulated with algorithms |
| 412 | /// described in: |
| 413 | /// https://wiki.sei.cmu.edu/confluence/display/c/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow |
| 414 | #if defined(__GNUC__) && __GNUC__ >= 5 |
| 415 | #define TINT_HAS_OVERFLOW_BUILTINS |
| 416 | #elif defined(__clang__) |
| 417 | #if __has_builtin(__builtin_add_overflow) && __has_builtin(__builtin_mul_overflow) |
| 418 | #define TINT_HAS_OVERFLOW_BUILTINS |
| 419 | #endif |
| 420 | #endif |
| 421 | |
Antonio Maiorano | 7f5b9d0 | 2022-12-13 16:29:06 +0000 | [diff] [blame] | 422 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635 |
| 423 | TINT_BEGIN_DISABLE_WARNING(MAYBE_UNINITIALIZED); |
| 424 | |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 425 | /// @returns a + b, or an empty optional if the resulting value overflowed the AInt |
| 426 | inline std::optional<AInt> CheckedAdd(AInt a, AInt b) { |
| 427 | int64_t result; |
| 428 | #ifdef TINT_HAS_OVERFLOW_BUILTINS |
| 429 | if (__builtin_add_overflow(a.value, b.value, &result)) { |
| 430 | return {}; |
| 431 | } |
| 432 | #else // TINT_HAS_OVERFLOW_BUILTINS |
| 433 | if (a.value >= 0) { |
Antonio Maiorano | f8a34d0 | 2022-08-17 12:46:39 +0000 | [diff] [blame] | 434 | if (b.value > AInt::kHighestValue - a.value) { |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 435 | return {}; |
| 436 | } |
| 437 | } else { |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 438 | if (b.value < AInt::kLowestValue - a.value) { |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 439 | return {}; |
| 440 | } |
| 441 | } |
| 442 | result = a.value + b.value; |
| 443 | #endif // TINT_HAS_OVERFLOW_BUILTINS |
| 444 | return AInt(result); |
| 445 | } |
| 446 | |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 447 | /// @returns a + b, or an empty optional if the resulting value overflowed the float value |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 448 | template <typename FloatingPointT, |
| 449 | typename = utils::traits::EnableIf<IsFloatingPoint<FloatingPointT>>> |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 450 | inline std::optional<FloatingPointT> CheckedAdd(FloatingPointT a, FloatingPointT b) { |
| 451 | auto result = FloatingPointT{a.value + b.value}; |
| 452 | if (!std::isfinite(result.value)) { |
Antonio Maiorano | 50940ae | 2022-08-03 21:20:26 +0000 | [diff] [blame] | 453 | return {}; |
| 454 | } |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 455 | return result; |
Antonio Maiorano | 50940ae | 2022-08-03 21:20:26 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Antonio Maiorano | f8a34d0 | 2022-08-17 12:46:39 +0000 | [diff] [blame] | 458 | /// @returns a - b, or an empty optional if the resulting value overflowed the AInt |
| 459 | inline std::optional<AInt> CheckedSub(AInt a, AInt b) { |
| 460 | int64_t result; |
| 461 | #ifdef TINT_HAS_OVERFLOW_BUILTINS |
| 462 | if (__builtin_sub_overflow(a.value, b.value, &result)) { |
| 463 | return {}; |
| 464 | } |
| 465 | #else // TINT_HAS_OVERFLOW_BUILTINS |
| 466 | if (b.value >= 0) { |
| 467 | if (a.value < AInt::kLowestValue + b.value) { |
| 468 | return {}; |
| 469 | } |
| 470 | } else { |
| 471 | if (a.value > AInt::kHighestValue + b.value) { |
| 472 | return {}; |
| 473 | } |
| 474 | } |
| 475 | result = a.value - b.value; |
| 476 | #endif // TINT_HAS_OVERFLOW_BUILTINS |
| 477 | return AInt(result); |
| 478 | } |
| 479 | |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 480 | /// @returns a + b, or an empty optional if the resulting value overflowed the float value |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 481 | template <typename FloatingPointT, |
| 482 | typename = utils::traits::EnableIf<IsFloatingPoint<FloatingPointT>>> |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 483 | inline std::optional<FloatingPointT> CheckedSub(FloatingPointT a, FloatingPointT b) { |
| 484 | auto result = FloatingPointT{a.value - b.value}; |
| 485 | if (!std::isfinite(result.value)) { |
Antonio Maiorano | f8a34d0 | 2022-08-17 12:46:39 +0000 | [diff] [blame] | 486 | return {}; |
| 487 | } |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 488 | return result; |
Antonio Maiorano | f8a34d0 | 2022-08-17 12:46:39 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 491 | /// @returns a * b, or an empty optional if the resulting value overflowed the AInt |
| 492 | inline std::optional<AInt> CheckedMul(AInt a, AInt b) { |
| 493 | int64_t result; |
| 494 | #ifdef TINT_HAS_OVERFLOW_BUILTINS |
| 495 | if (__builtin_mul_overflow(a.value, b.value, &result)) { |
| 496 | return {}; |
| 497 | } |
| 498 | #else // TINT_HAS_OVERFLOW_BUILTINS |
| 499 | if (a > 0) { |
| 500 | if (b > 0) { |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 501 | if (a > (AInt::kHighestValue / b)) { |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 502 | return {}; |
| 503 | } |
| 504 | } else { |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 505 | if (b < (AInt::kLowestValue / a)) { |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 506 | return {}; |
| 507 | } |
| 508 | } |
| 509 | } else { |
| 510 | if (b > 0) { |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 511 | if (a < (AInt::kLowestValue / b)) { |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 512 | return {}; |
| 513 | } |
| 514 | } else { |
Antonio Maiorano | d060f36 | 2022-07-29 17:12:01 +0000 | [diff] [blame] | 515 | if ((a != 0) && (b < (AInt::kHighestValue / a))) { |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 516 | return {}; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | result = a.value * b.value; |
| 521 | #endif // TINT_HAS_OVERFLOW_BUILTINS |
| 522 | return AInt(result); |
| 523 | } |
| 524 | |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 525 | /// @returns a * b, or an empty optional if the resulting value overflowed the float value |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 526 | template <typename FloatingPointT, |
| 527 | typename = utils::traits::EnableIf<IsFloatingPoint<FloatingPointT>>> |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 528 | inline std::optional<FloatingPointT> CheckedMul(FloatingPointT a, FloatingPointT b) { |
| 529 | auto result = FloatingPointT{a.value * b.value}; |
| 530 | if (!std::isfinite(result.value)) { |
Antonio Maiorano | c20c5df | 2022-09-01 14:57:39 +0000 | [diff] [blame] | 531 | return {}; |
| 532 | } |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 533 | return result; |
Antonio Maiorano | c20c5df | 2022-09-01 14:57:39 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Antonio Maiorano | 1741f44 | 2022-09-03 21:31:23 +0000 | [diff] [blame] | 536 | /// @returns a / b, or an empty optional if the resulting value overflowed the AInt |
| 537 | inline std::optional<AInt> CheckedDiv(AInt a, AInt b) { |
| 538 | if (b == 0) { |
| 539 | return {}; |
| 540 | } |
| 541 | |
| 542 | if (b == -1 && a == AInt::Lowest()) { |
| 543 | return {}; |
| 544 | } |
| 545 | |
| 546 | return AInt{a.value / b.value}; |
| 547 | } |
| 548 | |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 549 | /// @returns a / b, or an empty optional if the resulting value overflowed the float value |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 550 | template <typename FloatingPointT, |
| 551 | typename = utils::traits::EnableIf<IsFloatingPoint<FloatingPointT>>> |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 552 | inline std::optional<FloatingPointT> CheckedDiv(FloatingPointT a, FloatingPointT b) { |
Antonio Maiorano | deb2ec9 | 2023-03-24 18:44:02 +0000 | [diff] [blame] | 553 | if (b == FloatingPointT{0.0} || b == FloatingPointT{-0.0}) { |
| 554 | return {}; |
| 555 | } |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 556 | auto result = FloatingPointT{a.value / b.value}; |
| 557 | if (!std::isfinite(result.value)) { |
Antonio Maiorano | 1741f44 | 2022-09-03 21:31:23 +0000 | [diff] [blame] | 558 | return {}; |
| 559 | } |
Antonio Maiorano | 4d89ce1 | 2022-11-16 21:51:31 +0000 | [diff] [blame] | 560 | return result; |
Antonio Maiorano | 1741f44 | 2022-09-03 21:31:23 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Antonio Maiorano | cfea220 | 2022-11-30 14:18:51 +0000 | [diff] [blame] | 563 | namespace detail { |
| 564 | /// @returns the remainder of e1 / e2 |
| 565 | template <typename T> |
| 566 | inline T Mod(T e1, T e2) { |
Antonio Maiorano | 875d116 | 2022-12-05 17:16:15 +0000 | [diff] [blame] | 567 | if constexpr (IsIntegral<T>) { |
| 568 | return e1 % e2; |
| 569 | |
| 570 | } else { |
| 571 | return e1 - e2 * std::trunc(e1 / e2); |
| 572 | } |
Antonio Maiorano | cfea220 | 2022-11-30 14:18:51 +0000 | [diff] [blame] | 573 | } |
| 574 | } // namespace detail |
| 575 | |
| 576 | /// @returns the remainder of a / b, or an empty optional if the resulting value overflowed the AInt |
| 577 | inline std::optional<AInt> CheckedMod(AInt a, AInt b) { |
| 578 | if (b == 0) { |
| 579 | return {}; |
| 580 | } |
| 581 | |
| 582 | if (b == -1 && a == AInt::Lowest()) { |
| 583 | return {}; |
| 584 | } |
| 585 | |
Ben Clayton | 66805b0 | 2023-06-14 22:00:01 +0000 | [diff] [blame] | 586 | return AInt{tint::detail::Mod(a.value, b.value)}; |
Antonio Maiorano | cfea220 | 2022-11-30 14:18:51 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /// @returns the remainder of a / b, or an empty optional if the resulting value overflowed the |
| 590 | /// float value |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 591 | template <typename FloatingPointT, |
| 592 | typename = utils::traits::EnableIf<IsFloatingPoint<FloatingPointT>>> |
Antonio Maiorano | cfea220 | 2022-11-30 14:18:51 +0000 | [diff] [blame] | 593 | inline std::optional<FloatingPointT> CheckedMod(FloatingPointT a, FloatingPointT b) { |
Antonio Maiorano | deb2ec9 | 2023-03-24 18:44:02 +0000 | [diff] [blame] | 594 | if (b == FloatingPointT{0.0} || b == FloatingPointT{-0.0}) { |
| 595 | return {}; |
| 596 | } |
Ben Clayton | 66805b0 | 2023-06-14 22:00:01 +0000 | [diff] [blame] | 597 | auto result = FloatingPointT{tint::detail::Mod(a.value, b.value)}; |
Antonio Maiorano | cfea220 | 2022-11-30 14:18:51 +0000 | [diff] [blame] | 598 | if (!std::isfinite(result.value)) { |
| 599 | return {}; |
| 600 | } |
| 601 | return result; |
| 602 | } |
| 603 | |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 604 | /// @returns a * b + c, or an empty optional if the value overflowed the AInt |
| 605 | inline std::optional<AInt> CheckedMadd(AInt a, AInt b, AInt c) { |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 606 | if (auto mul = CheckedMul(a, b)) { |
| 607 | return CheckedAdd(mul.value(), c); |
| 608 | } |
| 609 | return {}; |
Ben Clayton | 61537d3 | 2022-05-31 13:14:29 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Antonio Maiorano | 7f5b9d0 | 2022-12-13 16:29:06 +0000 | [diff] [blame] | 612 | /// @returns the value of `base` raised to the power `exp`, or an empty optional if the operation |
| 613 | /// cannot be performed. |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 614 | template <typename FloatingPointT, |
| 615 | typename = utils::traits::EnableIf<IsFloatingPoint<FloatingPointT>>> |
Antonio Maiorano | 7f5b9d0 | 2022-12-13 16:29:06 +0000 | [diff] [blame] | 616 | inline std::optional<FloatingPointT> CheckedPow(FloatingPointT base, FloatingPointT exp) { |
| 617 | static_assert(IsNumber<FloatingPointT>); |
| 618 | if ((base < 0) || (base == 0 && exp <= 0)) { |
| 619 | return {}; |
| 620 | } |
| 621 | auto result = FloatingPointT{std::pow(base.value, exp.value)}; |
| 622 | if (!std::isfinite(result.value)) { |
| 623 | return {}; |
| 624 | } |
| 625 | return result; |
| 626 | } |
| 627 | |
| 628 | TINT_END_DISABLE_WARNING(MAYBE_UNINITIALIZED); |
| 629 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 630 | } // namespace tint |
| 631 | |
| 632 | namespace tint::number_suffixes { |
| 633 | |
Ben Clayton | 508e4a5 | 2022-05-10 22:08:04 +0000 | [diff] [blame] | 634 | /// Literal suffix for abstract integer literals |
Antonio Maiorano | 83fc247 | 2022-05-13 20:14:57 +0000 | [diff] [blame] | 635 | inline AInt operator""_a(unsigned long long int value) { // NOLINT |
Ben Clayton | 508e4a5 | 2022-05-10 22:08:04 +0000 | [diff] [blame] | 636 | return AInt(static_cast<int64_t>(value)); |
| 637 | } |
| 638 | |
| 639 | /// Literal suffix for abstract float literals |
Antonio Maiorano | 83fc247 | 2022-05-13 20:14:57 +0000 | [diff] [blame] | 640 | inline AFloat operator""_a(long double value) { // NOLINT |
Ben Clayton | 508e4a5 | 2022-05-10 22:08:04 +0000 | [diff] [blame] | 641 | return AFloat(static_cast<double>(value)); |
| 642 | } |
| 643 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 644 | /// Literal suffix for i32 literals |
Antonio Maiorano | 83fc247 | 2022-05-13 20:14:57 +0000 | [diff] [blame] | 645 | inline i32 operator""_i(unsigned long long int value) { // NOLINT |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 646 | return i32(static_cast<int32_t>(value)); |
| 647 | } |
| 648 | |
| 649 | /// Literal suffix for u32 literals |
Antonio Maiorano | 83fc247 | 2022-05-13 20:14:57 +0000 | [diff] [blame] | 650 | inline u32 operator""_u(unsigned long long int value) { // NOLINT |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 651 | return u32(static_cast<uint32_t>(value)); |
| 652 | } |
| 653 | |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 654 | /// Literal suffix for f32 literals |
Antonio Maiorano | 83fc247 | 2022-05-13 20:14:57 +0000 | [diff] [blame] | 655 | inline f32 operator""_f(long double value) { // NOLINT |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 656 | return f32(static_cast<double>(value)); |
| 657 | } |
| 658 | |
| 659 | /// Literal suffix for f32 literals |
Antonio Maiorano | 83fc247 | 2022-05-13 20:14:57 +0000 | [diff] [blame] | 660 | inline f32 operator""_f(unsigned long long int value) { // NOLINT |
Ben Clayton | 41285aa | 2022-05-10 14:55:34 +0000 | [diff] [blame] | 661 | return f32(static_cast<double>(value)); |
| 662 | } |
| 663 | |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 664 | /// Literal suffix for f16 literals |
Antonio Maiorano | 83fc247 | 2022-05-13 20:14:57 +0000 | [diff] [blame] | 665 | inline f16 operator""_h(long double value) { // NOLINT |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 666 | return f16(static_cast<double>(value)); |
| 667 | } |
| 668 | |
| 669 | /// Literal suffix for f16 literals |
Antonio Maiorano | 83fc247 | 2022-05-13 20:14:57 +0000 | [diff] [blame] | 670 | inline f16 operator""_h(unsigned long long int value) { // NOLINT |
Zhaoming Jiang | 62bfd31 | 2022-05-13 12:01:11 +0000 | [diff] [blame] | 671 | return f16(static_cast<double>(value)); |
| 672 | } |
| 673 | |
Ben Clayton | 0ce9ab0 | 2022-05-05 20:23:40 +0000 | [diff] [blame] | 674 | } // namespace tint::number_suffixes |
| 675 | |
Ben Clayton | 0c7c23b | 2022-08-31 23:51:48 +0000 | [diff] [blame] | 676 | namespace std { |
| 677 | |
| 678 | /// Custom std::hash specialization for tint::Number<T> |
| 679 | template <typename T> |
| 680 | class hash<tint::Number<T>> { |
| 681 | public: |
| 682 | /// @param n the Number |
| 683 | /// @return the hash value |
| 684 | inline std::size_t operator()(const tint::Number<T>& n) const { |
| 685 | return std::hash<decltype(n.value)>()(n.value); |
| 686 | } |
| 687 | }; |
| 688 | |
| 689 | } // namespace std |
| 690 | |
dan sinclair | b14a778 | 2023-07-20 09:21:10 +0000 | [diff] [blame] | 691 | #endif // SRC_TINT_LANG_BASE_BUILTIN_NUMBER_H_ |