FloatingPoint.h (22789B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* Various predicates and operations on IEEE-754 floating point types. */ 8 9 #ifndef mozilla_FloatingPoint_h 10 #define mozilla_FloatingPoint_h 11 12 #include "mozilla/Assertions.h" 13 #include "mozilla/Attributes.h" 14 #include "mozilla/Casting.h" 15 #include "mozilla/MathAlgorithms.h" 16 #include "mozilla/MemoryChecking.h" 17 #include "mozilla/Types.h" 18 19 #include <algorithm> 20 #include <cstdint> 21 #include <limits> 22 #include <type_traits> 23 24 namespace mozilla { 25 26 /* 27 * It's reasonable to ask why we have this header at all. Don't isnan, 28 * copysign, the built-in comparison operators, and the like solve these 29 * problems? Unfortunately, they don't. We've found that various compilers 30 * (MSVC, MSVC when compiling with PGO, and GCC on OS X, at least) miscompile 31 * the standard methods in various situations, so we can't use them. Some of 32 * these compilers even have problems compiling seemingly reasonable bitwise 33 * algorithms! But with some care we've found algorithms that seem to not 34 * trigger those compiler bugs. 35 * 36 * For the aforementioned reasons, be very wary of making changes to any of 37 * these algorithms. If you must make changes, keep a careful eye out for 38 * compiler bustage, particularly PGO-specific bustage. 39 */ 40 41 /* 42 * These implementations assume float/double are 32/64-bit single/double 43 * format number types compatible with the IEEE-754 standard. C++ doesn't 44 * require this, but we required it in implementations of these algorithms that 45 * preceded this header, so we shouldn't break anything to continue doing so. 46 */ 47 template <typename T> 48 struct FloatingPointTrait; 49 50 template <> 51 struct FloatingPointTrait<float> { 52 protected: 53 using Bits = uint32_t; 54 55 static constexpr unsigned kExponentWidth = 8; 56 static constexpr unsigned kSignificandWidth = 23; 57 }; 58 59 template <> 60 struct FloatingPointTrait<double> { 61 protected: 62 using Bits = uint64_t; 63 64 static constexpr unsigned kExponentWidth = 11; 65 static constexpr unsigned kSignificandWidth = 52; 66 }; 67 68 /* 69 * This struct contains details regarding the encoding of floating-point 70 * numbers that can be useful for direct bit manipulation. As of now, the 71 * template parameter has to be float or double. 72 * 73 * The nested typedef |Bits| is the unsigned integral type with the same size 74 * as T: uint32_t for float and uint64_t for double (static assertions 75 * double-check these assumptions). 76 * 77 * kExponentBias is the offset that is subtracted from the exponent when 78 * computing the value, i.e. one plus the opposite of the mininum possible 79 * exponent. 80 * kExponentShift is the shift that one needs to apply to retrieve the 81 * exponent component of the value. 82 * 83 * kSignBit contains a bits mask. Bit-and-ing with this mask will result in 84 * obtaining the sign bit. 85 * kExponentBits contains the mask needed for obtaining the exponent bits and 86 * kSignificandBits contains the mask needed for obtaining the significand 87 * bits. 88 * 89 * Full details of how floating point number formats are encoded are beyond 90 * the scope of this comment. For more information, see 91 * http://en.wikipedia.org/wiki/IEEE_floating_point 92 * http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers 93 */ 94 template <typename T> 95 struct FloatingPoint final : private FloatingPointTrait<T> { 96 private: 97 using Base = FloatingPointTrait<T>; 98 99 public: 100 /** 101 * An unsigned integral type suitable for accessing the bitwise representation 102 * of T. 103 */ 104 using Bits = typename Base::Bits; 105 106 static_assert(sizeof(T) == sizeof(Bits), "Bits must be same size as T"); 107 108 /** The bit-width of the exponent component of T. */ 109 using Base::kExponentWidth; 110 111 /** The bit-width of the significand component of T. */ 112 using Base::kSignificandWidth; 113 114 static_assert(1 + kExponentWidth + kSignificandWidth == CHAR_BIT * sizeof(T), 115 "sign bit plus bit widths should sum to overall bit width"); 116 117 /** 118 * The exponent field in an IEEE-754 floating point number consists of bits 119 * encoding an unsigned number. The *actual* represented exponent (for all 120 * values finite and not denormal) is that value, minus a bias |kExponentBias| 121 * so that a useful range of numbers is represented. 122 */ 123 static constexpr unsigned kExponentBias = (1U << (kExponentWidth - 1)) - 1; 124 125 /** 126 * The amount by which the bits of the exponent-field in an IEEE-754 floating 127 * point number are shifted from the LSB of the floating point type. 128 */ 129 static constexpr unsigned kExponentShift = kSignificandWidth; 130 131 /** The sign bit in the floating point representation. */ 132 static constexpr Bits kSignBit = static_cast<Bits>(1) 133 << (CHAR_BIT * sizeof(Bits) - 1); 134 135 /** The exponent bits in the floating point representation. */ 136 static constexpr Bits kExponentBits = 137 ((static_cast<Bits>(1) << kExponentWidth) - 1) << kSignificandWidth; 138 139 /** The significand bits in the floating point representation. */ 140 static constexpr Bits kSignificandBits = 141 (static_cast<Bits>(1) << kSignificandWidth) - 1; 142 143 static_assert((kSignBit & kExponentBits) == 0, 144 "sign bit shouldn't overlap exponent bits"); 145 static_assert((kSignBit & kSignificandBits) == 0, 146 "sign bit shouldn't overlap significand bits"); 147 static_assert((kExponentBits & kSignificandBits) == 0, 148 "exponent bits shouldn't overlap significand bits"); 149 150 static_assert((kSignBit | kExponentBits | kSignificandBits) == Bits(~0), 151 "all bits accounted for"); 152 }; 153 154 /** 155 * Determines whether a float/double is negative or -0. It is an error 156 * to call this method on a float/double which is NaN. 157 */ 158 template <typename T> 159 static MOZ_ALWAYS_INLINE bool IsNegative(T aValue) { 160 MOZ_ASSERT(!std::isnan(aValue), "NaN does not have a sign"); 161 return std::signbit(aValue); 162 } 163 164 /** Determines whether a float/double represents -0. */ 165 template <typename T> 166 static MOZ_ALWAYS_INLINE bool IsNegativeZero(T aValue) { 167 /* Only the sign bit is set if the value is -0. */ 168 typedef FloatingPoint<T> Traits; 169 typedef typename Traits::Bits Bits; 170 Bits bits = BitwiseCast<Bits>(aValue); 171 return bits == Traits::kSignBit; 172 } 173 174 /** Determines wether a float/double represents +0. */ 175 template <typename T> 176 static MOZ_ALWAYS_INLINE bool IsPositiveZero(T aValue) { 177 /* All bits are zero if the value is +0. */ 178 typedef FloatingPoint<T> Traits; 179 typedef typename Traits::Bits Bits; 180 Bits bits = BitwiseCast<Bits>(aValue); 181 return bits == 0; 182 } 183 184 /** 185 * Returns 0 if a float/double is NaN or infinite; 186 * otherwise, the float/double is returned. 187 */ 188 template <typename T> 189 static MOZ_ALWAYS_INLINE T ToZeroIfNonfinite(T aValue) { 190 return std::isfinite(aValue) ? aValue : 0; 191 } 192 193 /** 194 * Returns the exponent portion of the float/double. 195 * 196 * Zero is not special-cased, so ExponentComponent(0.0) is 197 * -int_fast16_t(Traits::kExponentBias). 198 */ 199 template <typename T> 200 static MOZ_ALWAYS_INLINE int_fast16_t ExponentComponent(T aValue) { 201 /* 202 * The exponent component of a float/double is an unsigned number, biased 203 * from its actual value. Subtract the bias to retrieve the actual exponent. 204 */ 205 typedef FloatingPoint<T> Traits; 206 typedef typename Traits::Bits Bits; 207 Bits bits = BitwiseCast<Bits>(aValue); 208 return int_fast16_t((bits & Traits::kExponentBits) >> 209 Traits::kExponentShift) - 210 int_fast16_t(Traits::kExponentBias); 211 } 212 213 /** Returns +Infinity. */ 214 template <typename T> 215 static constexpr MOZ_ALWAYS_INLINE T PositiveInfinity() { 216 return std::numeric_limits<T>::infinity(); 217 } 218 219 /** Returns -Infinity. */ 220 template <typename T> 221 static constexpr MOZ_ALWAYS_INLINE T NegativeInfinity() { 222 return -std::numeric_limits<T>::infinity(); 223 } 224 225 /** 226 * Computes the bit pattern for an infinity with the specified sign bit. 227 */ 228 template <typename T, int SignBit> 229 struct InfinityBits { 230 using Traits = FloatingPoint<T>; 231 232 static_assert(SignBit == 0 || SignBit == 1, "bad sign bit"); 233 static constexpr typename Traits::Bits value = 234 (SignBit * Traits::kSignBit) | Traits::kExponentBits; 235 }; 236 237 /** 238 * Computes the bit pattern for a NaN with the specified sign bit and 239 * significand bits. 240 */ 241 template <typename T, int SignBit, typename FloatingPoint<T>::Bits Significand> 242 struct SpecificNaNBits { 243 using Traits = FloatingPoint<T>; 244 245 static_assert(SignBit == 0 || SignBit == 1, "bad sign bit"); 246 static_assert((Significand & ~Traits::kSignificandBits) == 0, 247 "significand must only have significand bits set"); 248 static_assert(Significand & Traits::kSignificandBits, 249 "significand must be nonzero"); 250 251 static constexpr typename Traits::Bits value = 252 (SignBit * Traits::kSignBit) | Traits::kExponentBits | Significand; 253 }; 254 255 /** 256 * Computes the bit pattern for any floating point value. 257 */ 258 template <typename T, int SignBit, typename FloatingPoint<T>::Bits Exponent, 259 typename FloatingPoint<T>::Bits Significand> 260 struct SpecificFloatingPointBits { 261 using Traits = FloatingPoint<T>; 262 263 static_assert(SignBit == 0 || SignBit == 1, "bad sign bit"); 264 static_assert((Exponent & ~Traits::kExponentBias) == 0, 265 "exponent must only have exponent bits set"); 266 static_assert((Significand & ~Traits::kSignificandBits) == 0, 267 "significand must only have significand bits set"); 268 269 static constexpr typename Traits::Bits value = 270 (SignBit * Traits::kSignBit) | (Exponent << Traits::kExponentShift) | 271 Significand; 272 }; 273 274 /** 275 * Constructs a NaN value with the specified sign bit and significand bits. 276 * 277 * There is also a variant that returns the value directly. In most cases, the 278 * two variants should be identical. However, in the specific case of x86 279 * chips, the behavior differs: returning floating-point values directly is done 280 * through the x87 stack, and x87 loads and stores turn signaling NaNs into 281 * quiet NaNs... silently. Returning floating-point values via outparam, 282 * however, is done entirely within the SSE registers when SSE2 floating-point 283 * is enabled in the compiler, which has semantics-preserving behavior you would 284 * expect. 285 * 286 * If preserving the distinction between signaling NaNs and quiet NaNs is 287 * important to you, you should use the outparam version. In all other cases, 288 * you should use the direct return version. 289 */ 290 template <typename T> 291 static MOZ_ALWAYS_INLINE void SpecificNaN( 292 int signbit, typename FloatingPoint<T>::Bits significand, T* result) { 293 typedef FloatingPoint<T> Traits; 294 MOZ_ASSERT(signbit == 0 || signbit == 1); 295 MOZ_ASSERT((significand & ~Traits::kSignificandBits) == 0); 296 MOZ_ASSERT(significand & Traits::kSignificandBits); 297 298 BitwiseCast<T>( 299 (signbit ? Traits::kSignBit : 0) | Traits::kExponentBits | significand, 300 result); 301 MOZ_ASSERT(std::isnan(*result)); 302 } 303 304 template <typename T> 305 static MOZ_ALWAYS_INLINE T 306 SpecificNaN(int signbit, typename FloatingPoint<T>::Bits significand) { 307 T t; 308 SpecificNaN(signbit, significand, &t); 309 return t; 310 } 311 312 /** Computes the smallest non-zero positive float/double value. */ 313 template <typename T> 314 static constexpr MOZ_ALWAYS_INLINE T MinNumberValue() { 315 return std::numeric_limits<T>::denorm_min(); 316 } 317 318 /** Computes the largest positive float/double value. */ 319 template <typename T> 320 static constexpr MOZ_ALWAYS_INLINE T MaxNumberValue() { 321 return std::numeric_limits<T>::max(); 322 } 323 324 namespace detail { 325 326 template <typename Float, typename SignedInteger> 327 inline bool NumberEqualsSignedInteger(Float aValue, SignedInteger* aInteger) { 328 static_assert(std::is_same_v<Float, float> || std::is_same_v<Float, double>, 329 "Float must be an IEEE-754 floating point type"); 330 static_assert(std::is_signed_v<SignedInteger>, 331 "this algorithm only works for signed types: a different one " 332 "will be required for unsigned types"); 333 static_assert(sizeof(SignedInteger) >= sizeof(int), 334 "this function *might* require some finessing for signed types " 335 "subject to integral promotion before it can be used on them"); 336 337 MOZ_MAKE_MEM_UNDEFINED(aInteger, sizeof(*aInteger)); 338 339 // NaNs and infinities are not integers. 340 if (!std::isfinite(aValue)) { 341 return false; 342 } 343 344 // Otherwise do direct comparisons against the minimum/maximum |SignedInteger| 345 // values that can be encoded in |Float|. 346 347 constexpr SignedInteger MaxIntValue = 348 std::numeric_limits<SignedInteger>::max(); // e.g. INT32_MAX 349 constexpr SignedInteger MinValue = 350 std::numeric_limits<SignedInteger>::min(); // e.g. INT32_MIN 351 352 static_assert(IsPowerOfTwo(Abs(MinValue)), 353 "MinValue should be is a small power of two, thus exactly " 354 "representable in float/double both"); 355 356 constexpr unsigned SignedIntegerWidth = CHAR_BIT * sizeof(SignedInteger); 357 constexpr unsigned ExponentShift = FloatingPoint<Float>::kExponentShift; 358 359 // Careful! |MaxIntValue| may not be the maximum |SignedInteger| value that 360 // can be encoded in |Float|. Its |SignedIntegerWidth - 1| bits of precision 361 // may exceed |Float|'s |ExponentShift + 1| bits of precision. If necessary, 362 // compute the maximum |SignedInteger| that fits in |Float| from IEEE-754 363 // first principles. (|MinValue| doesn't have this problem because as a 364 // [relatively] small power of two it's always representable in |Float|.) 365 366 // Per C++11 [expr.const]p2, unevaluated subexpressions of logical AND/OR and 367 // conditional expressions *may* contain non-constant expressions, without 368 // making the enclosing expression not constexpr. MSVC implements this -- but 369 // it sometimes warns about undefined behavior in unevaluated subexpressions. 370 // This bites us if we initialize |MaxValue| the obvious way including an 371 // |uint64_t(1) << (SignedIntegerWidth - 2 - ExponentShift)| subexpression. 372 // Pull that shift-amount out and give it a not-too-huge value when it's in an 373 // unevaluated subexpression. 🙄 374 constexpr unsigned PrecisionExceededShiftAmount = 375 ExponentShift > SignedIntegerWidth - 1 376 ? 0 377 : SignedIntegerWidth - 2 - ExponentShift; 378 379 constexpr SignedInteger MaxValue = 380 ExponentShift > SignedIntegerWidth - 1 381 ? MaxIntValue 382 : SignedInteger((uint64_t(1) << (SignedIntegerWidth - 1)) - 383 (uint64_t(1) << PrecisionExceededShiftAmount)); 384 385 if (static_cast<Float>(MinValue) <= aValue && 386 aValue <= static_cast<Float>(MaxValue)) { 387 auto possible = static_cast<SignedInteger>(aValue); 388 if (static_cast<Float>(possible) == aValue) { 389 *aInteger = possible; 390 return true; 391 } 392 } 393 394 return false; 395 } 396 397 template <typename Float, typename SignedInteger> 398 inline bool NumberIsSignedInteger(Float aValue, SignedInteger* aInteger) { 399 static_assert(std::is_same_v<Float, float> || std::is_same_v<Float, double>, 400 "Float must be an IEEE-754 floating point type"); 401 static_assert(std::is_signed_v<SignedInteger>, 402 "this algorithm only works for signed types: a different one " 403 "will be required for unsigned types"); 404 static_assert(sizeof(SignedInteger) >= sizeof(int), 405 "this function *might* require some finessing for signed types " 406 "subject to integral promotion before it can be used on them"); 407 408 MOZ_MAKE_MEM_UNDEFINED(aInteger, sizeof(*aInteger)); 409 410 if (IsNegativeZero(aValue)) { 411 return false; 412 } 413 414 return NumberEqualsSignedInteger(aValue, aInteger); 415 } 416 417 } // namespace detail 418 419 /** 420 * If |aValue| is identical to some |int32_t| value, set |*aInt32| to that value 421 * and return true. Otherwise return false, leaving |*aInt32| in an 422 * indeterminate state. 423 * 424 * This method returns false for negative zero. If you want to consider -0 to 425 * be 0, use NumberEqualsInt32 below. 426 */ 427 template <typename T> 428 static MOZ_ALWAYS_INLINE bool NumberIsInt32(T aValue, int32_t* aInt32) { 429 return detail::NumberIsSignedInteger(aValue, aInt32); 430 } 431 432 /** 433 * If |aValue| is identical to some |int64_t| value, set |*aInt64| to that value 434 * and return true. Otherwise return false, leaving |*aInt64| in an 435 * indeterminate state. 436 * 437 * This method returns false for negative zero. If you want to consider -0 to 438 * be 0, use NumberEqualsInt64 below. 439 */ 440 template <typename T> 441 static MOZ_ALWAYS_INLINE bool NumberIsInt64(T aValue, int64_t* aInt64) { 442 return detail::NumberIsSignedInteger(aValue, aInt64); 443 } 444 445 /** 446 * If |aValue| is equal to some int32_t value (where -0 and +0 are considered 447 * equal), set |*aInt32| to that value and return true. Otherwise return false, 448 * leaving |*aInt32| in an indeterminate state. 449 * 450 * |NumberEqualsInt32(-0.0, ...)| will return true. To test whether a value can 451 * be losslessly converted to |int32_t| and back, use NumberIsInt32 above. 452 */ 453 template <typename T> 454 static MOZ_ALWAYS_INLINE bool NumberEqualsInt32(T aValue, int32_t* aInt32) { 455 return detail::NumberEqualsSignedInteger(aValue, aInt32); 456 } 457 458 /** 459 * If |aValue| is equal to some int64_t value (where -0 and +0 are considered 460 * equal), set |*aInt64| to that value and return true. Otherwise return false, 461 * leaving |*aInt64| in an indeterminate state. 462 * 463 * |NumberEqualsInt64(-0.0, ...)| will return true. To test whether a value can 464 * be losslessly converted to |int64_t| and back, use NumberIsInt64 above. 465 */ 466 template <typename T> 467 static MOZ_ALWAYS_INLINE bool NumberEqualsInt64(T aValue, int64_t* aInt64) { 468 return detail::NumberEqualsSignedInteger(aValue, aInt64); 469 } 470 471 /** 472 * Computes a NaN value. Do not use this method if you depend upon a particular 473 * NaN value being returned. 474 */ 475 template <typename T> 476 static MOZ_ALWAYS_INLINE T UnspecifiedNaN() { 477 /* 478 * If we can use any quiet NaN, we might as well use the all-ones NaN, 479 * since it's cheap to materialize on common platforms (such as x64, where 480 * this value can be represented in a 32-bit signed immediate field, allowing 481 * it to be stored to memory in a single instruction). 482 */ 483 typedef FloatingPoint<T> Traits; 484 return SpecificNaN<T>(1, Traits::kSignificandBits); 485 } 486 487 /** 488 * Compare two doubles for equality, *without* equating -0 to +0, and equating 489 * any NaN value to any other NaN value. (The normal equality operators equate 490 * -0 with +0, and they equate NaN to no other value.) 491 */ 492 template <typename T> 493 static inline bool NumbersAreIdentical(T aValue1, T aValue2) { 494 using Bits = typename FloatingPoint<T>::Bits; 495 if (std::isnan(aValue1)) { 496 return std::isnan(aValue2); 497 } 498 return BitwiseCast<Bits>(aValue1) == BitwiseCast<Bits>(aValue2); 499 } 500 501 /** 502 * Compare two floating point values for bit-wise equality. 503 */ 504 template <typename T> 505 static inline bool NumbersAreBitwiseIdentical(T aValue1, T aValue2) { 506 using Bits = typename FloatingPoint<T>::Bits; 507 return BitwiseCast<Bits>(aValue1) == BitwiseCast<Bits>(aValue2); 508 } 509 510 /** 511 * Return true iff |aValue| and |aValue2| are equal (ignoring sign if both are 512 * zero) or both NaN. 513 */ 514 template <typename T> 515 static inline bool EqualOrBothNaN(T aValue1, T aValue2) { 516 if (std::isnan(aValue1)) { 517 return std::isnan(aValue2); 518 } 519 return aValue1 == aValue2; 520 } 521 522 /** 523 * Return NaN if either |aValue1| or |aValue2| is NaN, or the minimum of 524 * |aValue1| and |aValue2| otherwise. 525 */ 526 template <typename T> 527 static inline T NaNSafeMin(T aValue1, T aValue2) { 528 if (std::isnan(aValue1) || std::isnan(aValue2)) { 529 return UnspecifiedNaN<T>(); 530 } 531 return std::min(aValue1, aValue2); 532 } 533 534 /** 535 * Return NaN if either |aValue1| or |aValue2| is NaN, or the maximum of 536 * |aValue1| and |aValue2| otherwise. 537 */ 538 template <typename T> 539 static inline T NaNSafeMax(T aValue1, T aValue2) { 540 if (std::isnan(aValue1) || std::isnan(aValue2)) { 541 return UnspecifiedNaN<T>(); 542 } 543 return std::max(aValue1, aValue2); 544 } 545 546 namespace detail { 547 548 template <typename T> 549 struct FuzzyEqualsEpsilon; 550 551 template <> 552 struct FuzzyEqualsEpsilon<float> { 553 // A number near 1e-5 that is exactly representable in a float. 554 static float value() { return 1.0f / (1 << 17); } 555 }; 556 557 template <> 558 struct FuzzyEqualsEpsilon<double> { 559 // A number near 1e-12 that is exactly representable in a double. 560 static double value() { return 1.0 / (1LL << 40); } 561 }; 562 563 } // namespace detail 564 565 /** 566 * Compare two floating point values for equality, modulo rounding error. That 567 * is, the two values are considered equal if they are both not NaN and if they 568 * are less than or equal to aEpsilon apart. The default value of aEpsilon is 569 * near 1e-5. 570 * 571 * For most scenarios you will want to use FuzzyEqualsMultiplicative instead, 572 * as it is more reasonable over the entire range of floating point numbers. 573 * This additive version should only be used if you know the range of the 574 * numbers you are dealing with is bounded and stays around the same order of 575 * magnitude. 576 */ 577 template <typename T> 578 static MOZ_ALWAYS_INLINE bool FuzzyEqualsAdditive( 579 T aValue1, T aValue2, T aEpsilon = detail::FuzzyEqualsEpsilon<T>::value()) { 580 static_assert(std::is_floating_point_v<T>, "floating point type required"); 581 return Abs(aValue1 - aValue2) <= aEpsilon; 582 } 583 584 /** 585 * Compare two floating point values for equality, allowing for rounding error 586 * relative to the magnitude of the values. That is, the two values are 587 * considered equal if they are both not NaN and they are less than or equal to 588 * some aEpsilon apart, where the aEpsilon is scaled by the smaller of the two 589 * argument values. 590 * 591 * In most cases you will want to use this rather than FuzzyEqualsAdditive, as 592 * this function effectively masks out differences in the bottom few bits of 593 * the floating point numbers being compared, regardless of what order of 594 * magnitude those numbers are at. 595 */ 596 template <typename T> 597 static MOZ_ALWAYS_INLINE bool FuzzyEqualsMultiplicative( 598 T aValue1, T aValue2, T aEpsilon = detail::FuzzyEqualsEpsilon<T>::value()) { 599 static_assert(std::is_floating_point_v<T>, "floating point type required"); 600 // can't use std::min because of bug 965340 601 T smaller = Abs(aValue1) < Abs(aValue2) ? Abs(aValue1) : Abs(aValue2); 602 return Abs(aValue1 - aValue2) <= aEpsilon * smaller; 603 } 604 605 /** 606 * Returns true if |aValue| can be losslessly represented as an IEEE-754 single 607 * precision number, false otherwise. All NaN values are considered 608 * representable (even though the bit patterns of double precision NaNs can't 609 * all be exactly represented in single precision). 610 */ 611 [[nodiscard]] extern MFBT_API bool IsFloat32Representable(double aValue); 612 613 } /* namespace mozilla */ 614 615 #endif /* mozilla_FloatingPoint_h */