2013-07-24 07:41:39 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-01-23 11:43:16 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 07:41:39 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-23 11:43:16 +00:00
|
|
|
|
|
|
|
/* Various predicates and operations on IEEE-754 floating point types. */
|
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#ifndef mozilla_FloatingPoint_h
|
|
|
|
#define mozilla_FloatingPoint_h
|
2012-01-23 11:43:16 +00:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-07 20:22:45 +00:00
|
|
|
#include "mozilla/Casting.h"
|
2014-02-05 22:04:42 +00:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2018-02-16 01:22:14 +00:00
|
|
|
#include "mozilla/MemoryChecking.h"
|
2013-10-22 12:55:35 +00:00
|
|
|
#include "mozilla/Types.h"
|
2018-02-16 01:22:14 +00:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2013-07-30 14:25:31 +00:00
|
|
|
|
2018-02-16 01:22:14 +00:00
|
|
|
#include <limits>
|
2013-07-30 14:25:31 +00:00
|
|
|
#include <stdint.h>
|
2012-01-23 11:43:16 +00:00
|
|
|
|
2013-05-01 20:55:13 +00:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-01-23 11:43:16 +00:00
|
|
|
/*
|
|
|
|
* It's reasonable to ask why we have this header at all. Don't isnan,
|
|
|
|
* copysign, the built-in comparison operators, and the like solve these
|
|
|
|
* problems? Unfortunately, they don't. We've found that various compilers
|
|
|
|
* (MSVC, MSVC when compiling with PGO, and GCC on OS X, at least) miscompile
|
|
|
|
* the standard methods in various situations, so we can't use them. Some of
|
|
|
|
* these compilers even have problems compiling seemingly reasonable bitwise
|
|
|
|
* algorithms! But with some care we've found algorithms that seem to not
|
|
|
|
* trigger those compiler bugs.
|
|
|
|
*
|
|
|
|
* For the aforementioned reasons, be very wary of making changes to any of
|
|
|
|
* these algorithms. If you must make changes, keep a careful eye out for
|
|
|
|
* compiler bustage, particularly PGO-specific bustage.
|
|
|
|
*/
|
|
|
|
|
2018-06-07 20:25:12 +00:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These implementations assume float/double are 32/64-bit single/double
|
|
|
|
* format number types compatible with the IEEE-754 standard. C++ doesn't
|
|
|
|
* require this, but we required it in implementations of these algorithms that
|
|
|
|
* preceded this header, so we shouldn't break anything to continue doing so.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
struct FloatingPointTrait;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct FloatingPointTrait<float> {
|
|
|
|
protected:
|
2018-02-16 01:22:14 +00:00
|
|
|
using Bits = uint32_t;
|
2013-05-01 20:55:13 +00:00
|
|
|
|
2018-06-07 20:25:12 +00:00
|
|
|
static constexpr unsigned kExponentWidth = 8;
|
|
|
|
static constexpr unsigned kSignificandWidth = 23;
|
2014-01-30 14:54:46 +00:00
|
|
|
};
|
2012-01-23 11:43:16 +00:00
|
|
|
|
2018-06-07 20:25:12 +00:00
|
|
|
template <>
|
|
|
|
struct FloatingPointTrait<double> {
|
|
|
|
protected:
|
2018-02-16 01:22:14 +00:00
|
|
|
using Bits = uint64_t;
|
2013-09-20 19:08:49 +00:00
|
|
|
|
2018-06-07 20:25:12 +00:00
|
|
|
static constexpr unsigned kExponentWidth = 11;
|
|
|
|
static constexpr unsigned kSignificandWidth = 52;
|
2014-01-30 14:54:46 +00:00
|
|
|
};
|
2013-09-20 19:08:49 +00:00
|
|
|
|
2018-06-07 20:25:12 +00:00
|
|
|
} // namespace detail
|
2013-09-20 19:08:49 +00:00
|
|
|
|
2014-01-30 14:54:46 +00:00
|
|
|
/*
|
|
|
|
* This struct contains details regarding the encoding of floating-point
|
|
|
|
* numbers that can be useful for direct bit manipulation. As of now, the
|
|
|
|
* template parameter has to be float or double.
|
|
|
|
*
|
|
|
|
* The nested typedef |Bits| is the unsigned integral type with the same size
|
|
|
|
* as T: uint32_t for float and uint64_t for double (static assertions
|
|
|
|
* double-check these assumptions).
|
|
|
|
*
|
2014-06-13 06:34:08 +00:00
|
|
|
* kExponentBias is the offset that is subtracted from the exponent when
|
2014-01-30 14:54:46 +00:00
|
|
|
* computing the value, i.e. one plus the opposite of the mininum possible
|
|
|
|
* exponent.
|
2014-06-13 06:34:08 +00:00
|
|
|
* kExponentShift is the shift that one needs to apply to retrieve the
|
|
|
|
* exponent component of the value.
|
2014-01-30 14:54:46 +00:00
|
|
|
*
|
2014-06-13 06:34:08 +00:00
|
|
|
* kSignBit contains a bits mask. Bit-and-ing with this mask will result in
|
2014-01-30 14:54:46 +00:00
|
|
|
* obtaining the sign bit.
|
2014-06-13 06:34:08 +00:00
|
|
|
* kExponentBits contains the mask needed for obtaining the exponent bits and
|
|
|
|
* kSignificandBits contains the mask needed for obtaining the significand
|
|
|
|
* bits.
|
2014-01-30 14:54:46 +00:00
|
|
|
*
|
2014-06-13 06:34:08 +00:00
|
|
|
* Full details of how floating point number formats are encoded are beyond
|
|
|
|
* the scope of this comment. For more information, see
|
2014-01-30 14:54:46 +00:00
|
|
|
* http://en.wikipedia.org/wiki/IEEE_floating_point
|
|
|
|
* http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2018-06-07 20:25:12 +00:00
|
|
|
struct FloatingPoint final : private detail::FloatingPointTrait<T> {
|
|
|
|
private:
|
|
|
|
using Base = detail::FloatingPointTrait<T>;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* An unsigned integral type suitable for accessing the bitwise representation
|
|
|
|
* of T.
|
|
|
|
*/
|
2018-02-16 01:22:14 +00:00
|
|
|
using Bits = typename Base::Bits;
|
2014-06-13 06:34:08 +00:00
|
|
|
|
2018-06-07 20:25:12 +00:00
|
|
|
static_assert(sizeof(T) == sizeof(Bits), "Bits must be same size as T");
|
|
|
|
|
|
|
|
/** The bit-width of the exponent component of T. */
|
|
|
|
using Base::kExponentWidth;
|
|
|
|
|
|
|
|
/** The bit-width of the significand component of T. */
|
|
|
|
using Base::kSignificandWidth;
|
|
|
|
|
|
|
|
static_assert(1 + kExponentWidth + kSignificandWidth == CHAR_BIT * sizeof(T),
|
|
|
|
"sign bit plus bit widths should sum to overall bit width");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The exponent field in an IEEE-754 floating point number consists of bits
|
|
|
|
* encoding an unsigned number. The *actual* represented exponent (for all
|
|
|
|
* values finite and not denormal) is that value, minus a bias |kExponentBias|
|
|
|
|
* so that a useful range of numbers is represented.
|
|
|
|
*/
|
|
|
|
static constexpr unsigned kExponentBias = (1U << (kExponentWidth - 1)) - 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The amount by which the bits of the exponent-field in an IEEE-754 floating
|
|
|
|
* point number are shifted from the LSB of the floating point type.
|
|
|
|
*/
|
|
|
|
static constexpr unsigned kExponentShift = kSignificandWidth;
|
|
|
|
|
|
|
|
/** The sign bit in the floating point representation. */
|
|
|
|
static constexpr Bits kSignBit = static_cast<Bits>(1)
|
|
|
|
<< (CHAR_BIT * sizeof(Bits) - 1);
|
|
|
|
|
|
|
|
/** The exponent bits in the floating point representation. */
|
|
|
|
static constexpr Bits kExponentBits =
|
|
|
|
((static_cast<Bits>(1) << kExponentWidth) - 1) << kSignificandWidth;
|
|
|
|
|
|
|
|
/** The significand bits in the floating point representation. */
|
|
|
|
static constexpr Bits kSignificandBits =
|
|
|
|
(static_cast<Bits>(1) << kSignificandWidth) - 1;
|
|
|
|
|
|
|
|
static_assert((kSignBit & kExponentBits) == 0,
|
2014-06-13 06:34:08 +00:00
|
|
|
"sign bit shouldn't overlap exponent bits");
|
2018-06-07 20:25:12 +00:00
|
|
|
static_assert((kSignBit & kSignificandBits) == 0,
|
2014-06-13 06:34:08 +00:00
|
|
|
"sign bit shouldn't overlap significand bits");
|
2018-06-07 20:25:12 +00:00
|
|
|
static_assert((kExponentBits & kSignificandBits) == 0,
|
2014-06-13 06:34:08 +00:00
|
|
|
"exponent bits shouldn't overlap significand bits");
|
|
|
|
|
2018-06-07 20:25:12 +00:00
|
|
|
static_assert((kSignBit | kExponentBits | kSignificandBits) == ~Bits(0),
|
2014-06-13 06:34:08 +00:00
|
|
|
"all bits accounted for");
|
2014-01-30 14:54:46 +00:00
|
|
|
};
|
2013-09-20 19:08:49 +00:00
|
|
|
|
2014-12-22 00:16:49 +00:00
|
|
|
/** Determines whether a float/double is NaN. */
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
2016-10-28 16:47:30 +00:00
|
|
|
static MOZ_ALWAYS_INLINE bool IsNaN(T aValue) {
|
2012-01-23 11:43:16 +00:00
|
|
|
/*
|
2014-06-13 06:34:08 +00:00
|
|
|
* A float/double is NaN if all exponent bits are 1 and the significand
|
|
|
|
* contains at least one non-zero bit.
|
2012-01-23 11:43:16 +00:00
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-12-22 00:16:49 +00:00
|
|
|
return (BitwiseCast<Bits>(aValue) & Traits::kExponentBits) ==
|
|
|
|
Traits::kExponentBits &&
|
|
|
|
(BitwiseCast<Bits>(aValue) & Traits::kSignificandBits) != 0;
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 14:54:46 +00:00
|
|
|
/** Determines whether a float/double is +Infinity or -Infinity. */
|
|
|
|
template <typename T>
|
2013-05-01 20:55:13 +00:00
|
|
|
static MOZ_ALWAYS_INLINE bool IsInfinite(T aValue) {
|
2012-01-23 11:43:16 +00:00
|
|
|
/* Infinities have all exponent bits set to 1 and an all-0 significand. */
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 06:34:08 +00:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return (bits & ~Traits::kSignBit) == Traits::kExponentBits;
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 14:54:46 +00:00
|
|
|
/** Determines whether a float/double is not NaN or infinite. */
|
|
|
|
template <typename T>
|
2013-05-01 20:55:13 +00:00
|
|
|
static MOZ_ALWAYS_INLINE bool IsFinite(T aValue) {
|
2012-01-23 11:43:16 +00:00
|
|
|
/*
|
2014-06-13 06:34:08 +00:00
|
|
|
* NaN and Infinities are the only non-finite floats/doubles, and both have
|
|
|
|
* all exponent bits set to 1.
|
2012-01-23 11:43:16 +00:00
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 06:34:08 +00:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return (bits & Traits::kExponentBits) != Traits::kExponentBits;
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-05 16:38:40 +00:00
|
|
|
* Determines whether a float/double is negative or -0. It is an error
|
|
|
|
* to call this method on a float/double which is NaN.
|
2012-01-23 11:43:16 +00:00
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
2013-05-01 20:55:13 +00:00
|
|
|
static MOZ_ALWAYS_INLINE bool IsNegative(T aValue) {
|
2014-06-13 06:34:08 +00:00
|
|
|
MOZ_ASSERT(!IsNaN(aValue), "NaN does not have a sign");
|
2012-01-23 11:43:16 +00:00
|
|
|
|
|
|
|
/* The sign bit is set if the double is negative. */
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 06:34:08 +00:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return (bits & Traits::kSignBit) != 0;
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 14:54:46 +00:00
|
|
|
/** Determines whether a float/double represents -0. */
|
|
|
|
template <typename T>
|
2013-05-01 20:55:13 +00:00
|
|
|
static MOZ_ALWAYS_INLINE bool IsNegativeZero(T aValue) {
|
2014-01-30 14:54:46 +00:00
|
|
|
/* Only the sign bit is set if the value is -0. */
|
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 06:34:08 +00:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return bits == Traits::kSignBit;
|
2014-09-09 10:02:00 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 15:47:42 +00:00
|
|
|
/** Determines wether a float/double represents +0. */
|
|
|
|
template <typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE bool IsPositiveZero(T aValue) {
|
|
|
|
/* All bits are zero if the value is +0. */
|
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return bits == 0;
|
|
|
|
}
|
|
|
|
|
2014-09-09 10:02:00 +00:00
|
|
|
/**
|
|
|
|
* Returns 0 if a float/double is NaN or infinite;
|
|
|
|
* otherwise, the float/double is returned.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T ToZeroIfNonfinite(T aValue) {
|
|
|
|
return IsFinite(aValue) ? aValue : 0;
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
2013-10-09 13:41:42 +00:00
|
|
|
/**
|
2014-01-30 14:54:46 +00:00
|
|
|
* Returns the exponent portion of the float/double.
|
2013-10-09 13:41:42 +00:00
|
|
|
*
|
|
|
|
* Zero is not special-cased, so ExponentComponent(0.0) is
|
2014-06-13 06:34:08 +00:00
|
|
|
* -int_fast16_t(Traits::kExponentBias).
|
2013-10-09 13:41:42 +00:00
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
2012-01-23 11:43:16 +00:00
|
|
|
static MOZ_ALWAYS_INLINE int_fast16_t ExponentComponent(T aValue) {
|
|
|
|
/*
|
2014-06-13 06:34:08 +00:00
|
|
|
* The exponent component of a float/double is an unsigned number, biased
|
|
|
|
* from its actual value. Subtract the bias to retrieve the actual exponent.
|
2012-01-23 11:43:16 +00:00
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 06:34:08 +00:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return int_fast16_t((bits & Traits::kExponentBits) >>
|
|
|
|
Traits::kExponentShift) -
|
|
|
|
int_fast16_t(Traits::kExponentBias);
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns +Infinity. */
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T PositiveInfinity() {
|
2012-01-23 11:43:16 +00:00
|
|
|
/*
|
|
|
|
* Positive infinity has all exponent bits set, sign bit set to 0, and no
|
|
|
|
* significand.
|
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
2014-06-13 06:34:08 +00:00
|
|
|
return BitwiseCast<T>(Traits::kExponentBits);
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns -Infinity. */
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T NegativeInfinity() {
|
2012-01-23 11:43:16 +00:00
|
|
|
/*
|
|
|
|
* Negative infinity has all exponent bits set, sign bit set to 1, and no
|
|
|
|
* significand.
|
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
2014-06-13 06:34:08 +00:00
|
|
|
return BitwiseCast<T>(Traits::kSignBit | Traits::kExponentBits);
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 15:59:21 +00:00
|
|
|
/**
|
|
|
|
* Computes the bit pattern for an infinity with the specified sign bit.
|
|
|
|
*/
|
|
|
|
template <typename T, int SignBit>
|
|
|
|
struct InfinityBits {
|
|
|
|
using Traits = FloatingPoint<T>;
|
|
|
|
|
|
|
|
static_assert(SignBit == 0 || SignBit == 1, "bad sign bit");
|
|
|
|
static constexpr typename Traits::Bits value =
|
|
|
|
(SignBit * Traits::kSignBit) | Traits::kExponentBits;
|
|
|
|
};
|
|
|
|
|
2016-10-28 16:47:30 +00:00
|
|
|
/**
|
|
|
|
* Computes the bit pattern for a NaN with the specified sign bit and
|
|
|
|
* significand bits.
|
|
|
|
*/
|
|
|
|
template <typename T, int SignBit, typename FloatingPoint<T>::Bits Significand>
|
|
|
|
struct SpecificNaNBits {
|
|
|
|
using Traits = FloatingPoint<T>;
|
|
|
|
|
|
|
|
static_assert(SignBit == 0 || SignBit == 1, "bad sign bit");
|
|
|
|
static_assert((Significand & ~Traits::kSignificandBits) == 0,
|
|
|
|
"significand must only have significand bits set");
|
|
|
|
static_assert(Significand & Traits::kSignificandBits,
|
|
|
|
"significand must be nonzero");
|
|
|
|
|
|
|
|
static constexpr typename Traits::Bits value =
|
|
|
|
(SignBit * Traits::kSignBit) | Traits::kExponentBits | Significand;
|
|
|
|
};
|
2014-01-30 14:54:46 +00:00
|
|
|
|
2016-08-01 18:02:05 +00:00
|
|
|
/**
|
|
|
|
* Constructs a NaN value with the specified sign bit and significand bits.
|
|
|
|
*
|
|
|
|
* There is also a variant that returns the value directly. In most cases, the
|
|
|
|
* two variants should be identical. However, in the specific case of x86
|
|
|
|
* chips, the behavior differs: returning floating-point values directly is done
|
|
|
|
* through the x87 stack, and x87 loads and stores turn signaling NaNs into
|
|
|
|
* quiet NaNs... silently. Returning floating-point values via outparam,
|
|
|
|
* however, is done entirely within the SSE registers when SSE2 floating-point
|
|
|
|
* is enabled in the compiler, which has semantics-preserving behavior you would
|
|
|
|
* expect.
|
|
|
|
*
|
|
|
|
* If preserving the distinction between signaling NaNs and quiet NaNs is
|
|
|
|
* important to you, you should use the outparam version. In all other cases,
|
|
|
|
* you should use the direct return version.
|
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
2016-08-01 18:02:05 +00:00
|
|
|
static MOZ_ALWAYS_INLINE void SpecificNaN(
|
|
|
|
int signbit, typename FloatingPoint<T>::Bits significand, T* result) {
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
2012-01-23 11:43:16 +00:00
|
|
|
MOZ_ASSERT(signbit == 0 || signbit == 1);
|
2014-06-13 06:34:08 +00:00
|
|
|
MOZ_ASSERT((significand & ~Traits::kSignificandBits) == 0);
|
|
|
|
MOZ_ASSERT(significand & Traits::kSignificandBits);
|
2014-01-30 14:54:46 +00:00
|
|
|
|
2016-08-01 18:02:05 +00:00
|
|
|
BitwiseCast<T>(
|
|
|
|
(signbit ? Traits::kSignBit : 0) | Traits::kExponentBits | significand,
|
|
|
|
result);
|
|
|
|
MOZ_ASSERT(IsNaN(*result));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T
|
|
|
|
SpecificNaN(int signbit, typename FloatingPoint<T>::Bits significand) {
|
|
|
|
T t;
|
|
|
|
SpecificNaN(signbit, significand, &t);
|
2014-01-30 14:54:46 +00:00
|
|
|
return t;
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 14:54:46 +00:00
|
|
|
/** Computes the smallest non-zero positive float/double value. */
|
|
|
|
template <typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T MinNumberValue() {
|
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
|
|
|
return BitwiseCast<T>(Bits(1));
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 01:22:14 +00:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename Float, typename SignedInteger>
|
|
|
|
inline bool NumberEqualsSignedInteger(Float aValue, SignedInteger* aInteger) {
|
|
|
|
static_assert(IsSame<Float, float>::value || IsSame<Float, double>::value,
|
|
|
|
"Float must be an IEEE-754 floating point type");
|
|
|
|
static_assert(IsSigned<SignedInteger>::value,
|
|
|
|
"this algorithm only works for signed types: a different one "
|
|
|
|
"will be required for unsigned types");
|
|
|
|
static_assert(sizeof(SignedInteger) >= sizeof(int),
|
|
|
|
"this function *might* require some finessing for signed types "
|
|
|
|
"subject to integral promotion before it can be used on them");
|
|
|
|
|
|
|
|
MOZ_MAKE_MEM_UNDEFINED(aInteger, sizeof(*aInteger));
|
|
|
|
|
|
|
|
// NaNs and infinities are not integers.
|
|
|
|
if (!IsFinite(aValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise do direct comparisons against the minimum/maximum |SignedInteger|
|
|
|
|
// values that can be encoded in |Float|.
|
|
|
|
|
|
|
|
constexpr SignedInteger MaxIntValue =
|
|
|
|
std::numeric_limits<SignedInteger>::max(); // e.g. INT32_MAX
|
|
|
|
constexpr SignedInteger MinValue =
|
|
|
|
std::numeric_limits<SignedInteger>::min(); // e.g. INT32_MIN
|
|
|
|
|
|
|
|
static_assert(IsPowerOfTwo(Abs(MinValue)),
|
|
|
|
"MinValue should be is a small power of two, thus exactly "
|
|
|
|
"representable in float/double both");
|
|
|
|
|
|
|
|
constexpr unsigned SignedIntegerWidth = CHAR_BIT * sizeof(SignedInteger);
|
|
|
|
constexpr unsigned ExponentShift = FloatingPoint<Float>::kExponentShift;
|
|
|
|
|
|
|
|
// Careful! |MaxIntValue| may not be the maximum |SignedInteger| value that
|
|
|
|
// can be encoded in |Float|. Its |SignedIntegerWidth - 1| bits of precision
|
|
|
|
// may exceed |Float|'s |ExponentShift + 1| bits of precision. If necessary,
|
|
|
|
// compute the maximum |SignedInteger| that fits in |Float| from IEEE-754
|
|
|
|
// first principles. (|MinValue| doesn't have this problem because as a
|
|
|
|
// [relatively] small power of two it's always representable in |Float|.)
|
|
|
|
|
|
|
|
// Per C++11 [expr.const]p2, unevaluated subexpressions of logical AND/OR and
|
|
|
|
// conditional expressions *may* contain non-constant expressions, without
|
|
|
|
// making the enclosing expression not constexpr. MSVC implements this -- but
|
|
|
|
// it sometimes warns about undefined behavior in unevaluated subexpressions.
|
|
|
|
// This bites us if we initialize |MaxValue| the obvious way including an
|
|
|
|
// |uint64_t(1) << (SignedIntegerWidth - 2 - ExponentShift)| subexpression.
|
|
|
|
// Pull that shift-amount out and give it a not-too-huge value when it's in an
|
|
|
|
// unevaluated subexpression. 🙄
|
|
|
|
constexpr unsigned PrecisionExceededShiftAmount =
|
|
|
|
ExponentShift > SignedIntegerWidth - 1
|
|
|
|
? 0
|
|
|
|
: SignedIntegerWidth - 2 - ExponentShift;
|
|
|
|
|
|
|
|
constexpr SignedInteger MaxValue =
|
|
|
|
ExponentShift > SignedIntegerWidth - 1
|
|
|
|
? MaxIntValue
|
|
|
|
: SignedInteger((uint64_t(1) << (SignedIntegerWidth - 1)) -
|
|
|
|
(uint64_t(1) << PrecisionExceededShiftAmount));
|
|
|
|
|
|
|
|
if (static_cast<Float>(MinValue) <= aValue &&
|
|
|
|
aValue <= static_cast<Float>(MaxValue)) {
|
|
|
|
auto possible = static_cast<SignedInteger>(aValue);
|
|
|
|
if (static_cast<Float>(possible) == aValue) {
|
|
|
|
*aInteger = possible;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Float, typename SignedInteger>
|
|
|
|
inline bool NumberIsSignedInteger(Float aValue, SignedInteger* aInteger) {
|
|
|
|
static_assert(IsSame<Float, float>::value || IsSame<Float, double>::value,
|
|
|
|
"Float must be an IEEE-754 floating point type");
|
|
|
|
static_assert(IsSigned<SignedInteger>::value,
|
|
|
|
"this algorithm only works for signed types: a different one "
|
|
|
|
"will be required for unsigned types");
|
|
|
|
static_assert(sizeof(SignedInteger) >= sizeof(int),
|
|
|
|
"this function *might* require some finessing for signed types "
|
|
|
|
"subject to integral promotion before it can be used on them");
|
|
|
|
|
|
|
|
MOZ_MAKE_MEM_UNDEFINED(aInteger, sizeof(*aInteger));
|
|
|
|
|
|
|
|
if (IsNegativeZero(aValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NumberEqualsSignedInteger(aValue, aInteger);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2013-11-02 21:29:44 +00:00
|
|
|
/**
|
2018-02-16 01:22:14 +00:00
|
|
|
* If |aValue| is identical to some |int32_t| value, set |*aInt32| to that value
|
|
|
|
* and return true. Otherwise return false, leaving |*aInt32| in an
|
|
|
|
* indeterminate state.
|
2013-11-02 21:29:44 +00:00
|
|
|
*
|
2018-02-16 01:22:14 +00:00
|
|
|
* This method returns false for negative zero. If you want to consider -0 to
|
|
|
|
* be 0, use NumberEqualsInt32 below.
|
2013-11-02 21:29:44 +00:00
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
2018-02-16 01:22:14 +00:00
|
|
|
static MOZ_ALWAYS_INLINE bool NumberIsInt32(T aValue, int32_t* aInt32) {
|
|
|
|
return detail::NumberIsSignedInteger(aValue, aInt32);
|
2013-11-02 21:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-16 01:22:14 +00:00
|
|
|
* If |aValue| is equal to some int32_t value (where -0 and +0 are considered
|
|
|
|
* equal), set |*aInt32| to that value and return true. Otherwise return false,
|
|
|
|
* leaving |*aInt32| in an indeterminate state.
|
2013-11-02 21:29:44 +00:00
|
|
|
*
|
2018-02-16 01:22:14 +00:00
|
|
|
* |NumberEqualsInt32(-0.0, ...)| will return true. To test whether a value can
|
|
|
|
* be losslessly converted to |int32_t| and back, use NumberIsInt32 above.
|
2013-11-02 21:29:44 +00:00
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
2018-02-16 01:22:14 +00:00
|
|
|
static MOZ_ALWAYS_INLINE bool NumberEqualsInt32(T aValue, int32_t* aInt32) {
|
|
|
|
return detail::NumberEqualsSignedInteger(aValue, aInt32);
|
2013-05-01 20:55:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes a NaN value. Do not use this method if you depend upon a particular
|
|
|
|
* NaN value being returned.
|
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T UnspecifiedNaN() {
|
2013-09-20 01:47:55 +00:00
|
|
|
/*
|
|
|
|
* If we can use any quiet NaN, we might as well use the all-ones NaN,
|
|
|
|
* since it's cheap to materialize on common platforms (such as x64, where
|
|
|
|
* this value can be represented in a 32-bit signed immediate field, allowing
|
|
|
|
* it to be stored to memory in a single instruction).
|
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
2014-06-13 06:34:08 +00:00
|
|
|
return SpecificNaN<T>(1, Traits::kSignificandBits);
|
2012-01-23 11:43:16 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 22:35:03 +00:00
|
|
|
/**
|
|
|
|
* Compare two doubles for equality, *without* equating -0 to +0, and equating
|
|
|
|
* any NaN value to any other NaN value. (The normal equality operators equate
|
|
|
|
* -0 with +0, and they equate NaN to no other value.)
|
|
|
|
*/
|
2014-01-30 14:54:46 +00:00
|
|
|
template <typename T>
|
2014-06-13 06:34:08 +00:00
|
|
|
static inline bool NumbersAreIdentical(T aValue1, T aValue2) {
|
2014-01-30 14:54:46 +00:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 06:34:08 +00:00
|
|
|
if (IsNaN(aValue1)) {
|
|
|
|
return IsNaN(aValue2);
|
|
|
|
}
|
|
|
|
return BitwiseCast<Bits>(aValue1) == BitwiseCast<Bits>(aValue2);
|
2013-10-22 12:53:12 +00:00
|
|
|
}
|
|
|
|
|
2019-06-28 23:57:13 +00:00
|
|
|
/**
|
|
|
|
* Return true iff |aValue| and |aValue2| are equal (ignoring sign if both are
|
|
|
|
* zero) or both NaN.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static inline bool EqualOrBothNaN(T aValue1, T aValue2) {
|
|
|
|
if (IsNaN(aValue1)) {
|
|
|
|
return IsNaN(aValue2);
|
|
|
|
}
|
|
|
|
return aValue1 == aValue2;
|
|
|
|
}
|
|
|
|
|
2014-02-05 22:04:42 +00:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct FuzzyEqualsEpsilon;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct FuzzyEqualsEpsilon<float> {
|
2014-06-13 06:34:08 +00:00
|
|
|
// A number near 1e-5 that is exactly representable in a float.
|
2014-05-22 18:15:06 +00:00
|
|
|
static float value() { return 1.0f / (1 << 17); }
|
2014-02-05 22:04:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct FuzzyEqualsEpsilon<double> {
|
2014-06-13 06:34:08 +00:00
|
|
|
// A number near 1e-12 that is exactly representable in a double.
|
2014-05-22 18:15:06 +00:00
|
|
|
static double value() { return 1.0 / (1LL << 40); }
|
2014-02-05 22:04:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two floating point values for equality, modulo rounding error. That
|
|
|
|
* is, the two values are considered equal if they are both not NaN and if they
|
2014-06-13 06:34:08 +00:00
|
|
|
* are less than or equal to aEpsilon apart. The default value of aEpsilon is
|
|
|
|
* near 1e-5.
|
2014-02-05 22:04:42 +00:00
|
|
|
*
|
|
|
|
* For most scenarios you will want to use FuzzyEqualsMultiplicative instead,
|
|
|
|
* as it is more reasonable over the entire range of floating point numbers.
|
2014-06-13 06:34:08 +00:00
|
|
|
* This additive version should only be used if you know the range of the
|
|
|
|
* numbers you are dealing with is bounded and stays around the same order of
|
|
|
|
* magnitude.
|
2014-02-05 22:04:42 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE bool FuzzyEqualsAdditive(
|
2014-06-13 06:34:08 +00:00
|
|
|
T aValue1, T aValue2, T aEpsilon = detail::FuzzyEqualsEpsilon<T>::value()) {
|
2014-02-05 22:04:42 +00:00
|
|
|
static_assert(IsFloatingPoint<T>::value, "floating point type required");
|
2014-06-13 06:34:08 +00:00
|
|
|
return Abs(aValue1 - aValue2) <= aEpsilon;
|
2014-02-05 22:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two floating point values for equality, allowing for rounding error
|
|
|
|
* relative to the magnitude of the values. That is, the two values are
|
|
|
|
* considered equal if they are both not NaN and they are less than or equal to
|
2014-06-13 06:34:08 +00:00
|
|
|
* some aEpsilon apart, where the aEpsilon is scaled by the smaller of the two
|
2014-02-05 22:04:42 +00:00
|
|
|
* argument values.
|
|
|
|
*
|
|
|
|
* In most cases you will want to use this rather than FuzzyEqualsAdditive, as
|
|
|
|
* this function effectively masks out differences in the bottom few bits of
|
2014-06-13 06:34:08 +00:00
|
|
|
* the floating point numbers being compared, regardless of what order of
|
|
|
|
* magnitude those numbers are at.
|
2014-02-05 22:04:42 +00:00
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE bool FuzzyEqualsMultiplicative(
|
2014-06-13 06:34:08 +00:00
|
|
|
T aValue1, T aValue2, T aEpsilon = detail::FuzzyEqualsEpsilon<T>::value()) {
|
2014-02-05 22:04:42 +00:00
|
|
|
static_assert(IsFloatingPoint<T>::value, "floating point type required");
|
|
|
|
// can't use std::min because of bug 965340
|
2014-06-13 06:34:08 +00:00
|
|
|
T smaller = Abs(aValue1) < Abs(aValue2) ? Abs(aValue1) : Abs(aValue2);
|
|
|
|
return Abs(aValue1 - aValue2) <= aEpsilon * smaller;
|
2014-02-05 22:04:42 +00:00
|
|
|
}
|
|
|
|
|
2013-10-22 12:55:35 +00:00
|
|
|
/**
|
2018-06-06 23:03:47 +00:00
|
|
|
* Returns true if |aValue| can be losslessly represented as an IEEE-754 single
|
|
|
|
* precision number, false otherwise. All NaN values are considered
|
|
|
|
* representable (even though the bit patterns of double precision NaNs can't
|
|
|
|
* all be exactly represented in single precision).
|
2013-10-22 12:55:35 +00:00
|
|
|
*/
|
2016-04-27 04:16:50 +00:00
|
|
|
MOZ_MUST_USE
|
2018-06-06 23:03:47 +00:00
|
|
|
extern MFBT_API bool IsFloat32Representable(double aValue);
|
2013-10-22 12:55:35 +00:00
|
|
|
|
2013-05-01 20:55:13 +00:00
|
|
|
} /* namespace mozilla */
|
2012-01-23 11:43:16 +00:00
|
|
|
|
2013-07-24 07:41:39 +00:00
|
|
|
#endif /* mozilla_FloatingPoint_h */
|