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