2013-07-24 11:41:39 +04: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 15:43:16 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2013-07-24 11:41:39 +04: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 15:43:16 +04:00
|
|
|
|
|
|
|
/* Various predicates and operations on IEEE-754 floating point types. */
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#ifndef mozilla_FloatingPoint_h
|
|
|
|
#define mozilla_FloatingPoint_h
|
2012-01-23 15:43:16 +04:00
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-08 00:22:45 +04:00
|
|
|
#include "mozilla/Casting.h"
|
2014-02-06 02:04:42 +04:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2013-10-22 16:55:35 +04:00
|
|
|
#include "mozilla/Types.h"
|
2013-07-30 18:25:31 +04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2012-01-23 15:43:16 +04:00
|
|
|
|
2013-05-02 00:55:13 +04:00
|
|
|
namespace mozilla {
|
|
|
|
|
2012-01-23 15:43:16 +04: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.
|
|
|
|
*/
|
|
|
|
|
2014-01-30 18:54:46 +04:00
|
|
|
struct FloatTypeTraits
|
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
typedef uint32_t Bits;
|
2013-05-02 00:55:13 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
static const unsigned kExponentBias = 127;
|
|
|
|
static const unsigned kExponentShift = 23;
|
2012-01-23 15:43:16 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
static const Bits kSignBit = 0x80000000UL;
|
|
|
|
static const Bits kExponentBits = 0x7F800000UL;
|
|
|
|
static const Bits kSignificandBits = 0x007FFFFFUL;
|
2014-01-30 18:54:46 +04:00
|
|
|
};
|
2012-01-23 15:43:16 +04:00
|
|
|
|
2014-01-30 18:54:46 +04:00
|
|
|
struct DoubleTypeTraits
|
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
typedef uint64_t Bits;
|
2013-09-20 23:08:49 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
static const unsigned kExponentBias = 1023;
|
|
|
|
static const unsigned kExponentShift = 52;
|
2013-09-20 23:08:49 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
static const Bits kSignBit = 0x8000000000000000ULL;
|
|
|
|
static const Bits kExponentBits = 0x7ff0000000000000ULL;
|
|
|
|
static const Bits kSignificandBits = 0x000fffffffffffffULL;
|
2014-01-30 18:54:46 +04:00
|
|
|
};
|
2013-09-20 23:08:49 +04:00
|
|
|
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T> struct SelectTrait;
|
|
|
|
template<> struct SelectTrait<float> : public FloatTypeTraits {};
|
|
|
|
template<> struct SelectTrait<double> : public DoubleTypeTraits {};
|
2013-09-20 23:08:49 +04:00
|
|
|
|
2014-01-30 18:54:46 +04: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 10:34:08 +04:00
|
|
|
* kExponentBias is the offset that is subtracted from the exponent when
|
2014-01-30 18:54:46 +04:00
|
|
|
* computing the value, i.e. one plus the opposite of the mininum possible
|
|
|
|
* exponent.
|
2014-06-13 10:34:08 +04:00
|
|
|
* kExponentShift is the shift that one needs to apply to retrieve the
|
|
|
|
* exponent component of the value.
|
2014-01-30 18:54:46 +04:00
|
|
|
*
|
2014-06-13 10:34:08 +04:00
|
|
|
* kSignBit contains a bits mask. Bit-and-ing with this mask will result in
|
2014-01-30 18:54:46 +04:00
|
|
|
* obtaining the sign bit.
|
2014-06-13 10:34:08 +04: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 18:54:46 +04:00
|
|
|
*
|
2014-06-13 10:34:08 +04: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 18:54:46 +04: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>
|
|
|
|
struct FloatingPoint : public SelectTrait<T>
|
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
typedef SelectTrait<T> Base;
|
|
|
|
typedef typename Base::Bits Bits;
|
|
|
|
|
|
|
|
static_assert((Base::kSignBit & Base::kExponentBits) == 0,
|
|
|
|
"sign bit shouldn't overlap exponent bits");
|
|
|
|
static_assert((Base::kSignBit & Base::kSignificandBits) == 0,
|
|
|
|
"sign bit shouldn't overlap significand bits");
|
|
|
|
static_assert((Base::kExponentBits & Base::kSignificandBits) == 0,
|
|
|
|
"exponent bits shouldn't overlap significand bits");
|
|
|
|
|
|
|
|
static_assert((Base::kSignBit | Base::kExponentBits | Base::kSignificandBits) ==
|
|
|
|
~Bits(0),
|
|
|
|
"all bits accounted for");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These implementations assume float/double are 32/64-bit single/double
|
|
|
|
* format number types compatible with the IEEE-754 standard. C++ don't
|
|
|
|
* require this to be the case. But we required this in implementations of
|
|
|
|
* these algorithms that preceded this header, so we shouldn't break anything
|
|
|
|
* if we keep doing so.
|
|
|
|
*/
|
|
|
|
static_assert(sizeof(T) == sizeof(Bits), "Bits must be same size as T");
|
2014-01-30 18:54:46 +04:00
|
|
|
};
|
2013-09-20 23:08:49 +04:00
|
|
|
|
2012-01-23 15:43:16 +04:00
|
|
|
/** Determines whether a double is NaN. */
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T>
|
2013-05-02 00:55:13 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-13 10:34:08 +04:00
|
|
|
IsNaN(T aValue)
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
|
|
|
/*
|
2014-06-13 10:34:08 +04: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 15:43:16 +04:00
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 10:34:08 +04:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return (bits & Traits::kExponentBits) == Traits::kExponentBits &&
|
|
|
|
(bits & Traits::kSignificandBits) != 0;
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
2014-01-30 18:54:46 +04:00
|
|
|
/** Determines whether a float/double is +Infinity or -Infinity. */
|
|
|
|
template<typename T>
|
2013-05-02 00:55:13 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-13 10:34:08 +04:00
|
|
|
IsInfinite(T aValue)
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
|
|
|
/* Infinities have all exponent bits set to 1 and an all-0 significand. */
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 10:34:08 +04:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return (bits & ~Traits::kSignBit) == Traits::kExponentBits;
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
2014-01-30 18:54:46 +04:00
|
|
|
/** Determines whether a float/double is not NaN or infinite. */
|
|
|
|
template<typename T>
|
2013-05-02 00:55:13 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-13 10:34:08 +04:00
|
|
|
IsFinite(T aValue)
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
|
|
|
/*
|
2014-06-13 10:34:08 +04:00
|
|
|
* NaN and Infinities are the only non-finite floats/doubles, and both have
|
|
|
|
* all exponent bits set to 1.
|
2012-01-23 15:43:16 +04:00
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 10:34:08 +04:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return (bits & Traits::kExponentBits) != Traits::kExponentBits;
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* Determines whether a float/double is negative. It is an error to call this
|
|
|
|
* method on a float/double which is NaN.
|
2012-01-23 15:43:16 +04:00
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T>
|
2013-05-02 00:55:13 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-13 10:34:08 +04:00
|
|
|
IsNegative(T aValue)
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
MOZ_ASSERT(!IsNaN(aValue), "NaN does not have a sign");
|
2012-01-23 15:43:16 +04:00
|
|
|
|
|
|
|
/* The sign bit is set if the double is negative. */
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 10:34:08 +04:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return (bits & Traits::kSignBit) != 0;
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
2014-01-30 18:54:46 +04:00
|
|
|
/** Determines whether a float/double represents -0. */
|
|
|
|
template<typename T>
|
2013-05-02 00:55:13 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-13 10:34:08 +04:00
|
|
|
IsNegativeZero(T aValue)
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
2014-01-30 18:54:46 +04:00
|
|
|
/* Only the sign bit is set if the value is -0. */
|
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 10:34:08 +04:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return bits == Traits::kSignBit;
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
2013-10-09 17:41:42 +04:00
|
|
|
/**
|
2014-01-30 18:54:46 +04:00
|
|
|
* Returns the exponent portion of the float/double.
|
2013-10-09 17:41:42 +04:00
|
|
|
*
|
|
|
|
* Zero is not special-cased, so ExponentComponent(0.0) is
|
2014-06-13 10:34:08 +04:00
|
|
|
* -int_fast16_t(Traits::kExponentBias).
|
2013-10-09 17:41:42 +04:00
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T>
|
2012-01-23 15:43:16 +04:00
|
|
|
static MOZ_ALWAYS_INLINE int_fast16_t
|
2014-06-13 10:34:08 +04:00
|
|
|
ExponentComponent(T aValue)
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
|
|
|
/*
|
2014-06-13 10:34:08 +04: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 15:43:16 +04:00
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 10:34:08 +04:00
|
|
|
Bits bits = BitwiseCast<Bits>(aValue);
|
|
|
|
return int_fast16_t((bits & Traits::kExponentBits) >> Traits::kExponentShift) -
|
|
|
|
int_fast16_t(Traits::kExponentBias);
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns +Infinity. */
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T
|
2013-05-02 00:55:13 +04:00
|
|
|
PositiveInfinity()
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Positive infinity has all exponent bits set, sign bit set to 0, and no
|
|
|
|
* significand.
|
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
2014-06-13 10:34:08 +04:00
|
|
|
return BitwiseCast<T>(Traits::kExponentBits);
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns -Infinity. */
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T
|
2013-05-02 00:55:13 +04:00
|
|
|
NegativeInfinity()
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Negative infinity has all exponent bits set, sign bit set to 1, and no
|
|
|
|
* significand.
|
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
2014-06-13 10:34:08 +04:00
|
|
|
return BitwiseCast<T>(Traits::kSignBit | Traits::kExponentBits);
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
2014-01-30 18:54:46 +04:00
|
|
|
|
2012-01-23 15:43:16 +04:00
|
|
|
/** Constructs a NaN value with the specified sign bit and significand bits. */
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T
|
|
|
|
SpecificNaN(int signbit, typename FloatingPoint<T>::Bits significand)
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
2012-01-23 15:43:16 +04:00
|
|
|
MOZ_ASSERT(signbit == 0 || signbit == 1);
|
2014-06-13 10:34:08 +04:00
|
|
|
MOZ_ASSERT((significand & ~Traits::kSignificandBits) == 0);
|
|
|
|
MOZ_ASSERT(significand & Traits::kSignificandBits);
|
2014-01-30 18:54:46 +04:00
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
T t = BitwiseCast<T>((signbit ? Traits::kSignBit : 0) |
|
|
|
|
Traits::kExponentBits |
|
2014-01-30 18:54:46 +04:00
|
|
|
significand);
|
|
|
|
MOZ_ASSERT(IsNaN(t));
|
|
|
|
return t;
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
2014-01-30 18:54:46 +04:00
|
|
|
/** Computes the smallest non-zero positive float/double value. */
|
|
|
|
template<typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T
|
|
|
|
MinNumberValue()
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
|
|
|
return BitwiseCast<T>(Bits(1));
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
2013-11-03 01:29:44 +04:00
|
|
|
/**
|
2014-06-13 10:34:08 +04:00
|
|
|
* If aValue is equal to some int32_t value, set *aInt32 to that value and
|
|
|
|
* return true; otherwise return false.
|
2013-11-03 01:29:44 +04:00
|
|
|
*
|
|
|
|
* Note that negative zero is "equal" to zero here. To test whether a value can
|
2014-01-30 18:54:46 +04:00
|
|
|
* be losslessly converted to int32_t and back, use NumberIsInt32 instead.
|
2013-11-03 01:29:44 +04:00
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T>
|
2013-05-02 00:55:13 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-13 10:34:08 +04:00
|
|
|
NumberEqualsInt32(T aValue, int32_t* aInt32)
|
2012-01-23 15:43:16 +04:00
|
|
|
{
|
|
|
|
/*
|
2014-01-30 18:54:46 +04:00
|
|
|
* XXX Casting a floating-point value that doesn't truncate to int32_t, to
|
|
|
|
* int32_t, induces undefined behavior. We should definitely fix this
|
|
|
|
* (bug 744965), but as apparently it "works" in practice, it's not a
|
|
|
|
* pressing concern now.
|
2012-01-23 15:43:16 +04:00
|
|
|
*/
|
2014-06-13 10:34:08 +04:00
|
|
|
return aValue == (*aInt32 = int32_t(aValue));
|
2013-11-03 01:29:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If d can be converted to int32_t and back to an identical double value,
|
2014-06-13 10:34:08 +04:00
|
|
|
* set *aInt32 to that value and return true; otherwise return false.
|
2013-11-03 01:29:44 +04:00
|
|
|
*
|
2014-01-30 18:54:46 +04:00
|
|
|
* The difference between this and NumberEqualsInt32 is that this method returns
|
2013-11-03 01:29:44 +04:00
|
|
|
* false for negative zero.
|
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T>
|
2013-11-03 01:29:44 +04:00
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-13 10:34:08 +04:00
|
|
|
NumberIsInt32(T aValue, int32_t* aInt32)
|
2013-11-03 01:29:44 +04:00
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
return !IsNegativeZero(aValue) && NumberEqualsInt32(aValue, aInt32);
|
2013-05-02 00:55:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Computes a NaN value. Do not use this method if you depend upon a particular
|
|
|
|
* NaN value being returned.
|
|
|
|
*/
|
2014-01-30 18:54:46 +04:00
|
|
|
template<typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE T
|
2013-05-02 00:55:13 +04:00
|
|
|
UnspecifiedNaN()
|
|
|
|
{
|
2013-09-20 05:47:55 +04: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 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
2014-06-13 10:34:08 +04:00
|
|
|
return SpecificNaN<T>(1, Traits::kSignificandBits);
|
2012-01-23 15:43:16 +04:00
|
|
|
}
|
|
|
|
|
2013-06-19 02:35:03 +04: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 18:54:46 +04:00
|
|
|
template<typename T>
|
2013-06-19 02:35:03 +04:00
|
|
|
static inline bool
|
2014-06-13 10:34:08 +04:00
|
|
|
NumbersAreIdentical(T aValue1, T aValue2)
|
2013-06-19 02:35:03 +04:00
|
|
|
{
|
2014-01-30 18:54:46 +04:00
|
|
|
typedef FloatingPoint<T> Traits;
|
|
|
|
typedef typename Traits::Bits Bits;
|
2014-06-13 10:34:08 +04:00
|
|
|
if (IsNaN(aValue1)) {
|
|
|
|
return IsNaN(aValue2);
|
|
|
|
}
|
|
|
|
return BitwiseCast<Bits>(aValue1) == BitwiseCast<Bits>(aValue2);
|
2013-10-22 16:53:12 +04:00
|
|
|
}
|
|
|
|
|
2014-02-06 02:04:42 +04:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct FuzzyEqualsEpsilon;
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct FuzzyEqualsEpsilon<float>
|
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
// A number near 1e-5 that is exactly representable in a float.
|
2014-05-22 22:15:06 +04:00
|
|
|
static float value() { return 1.0f / (1 << 17); }
|
2014-02-06 02:04:42 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct FuzzyEqualsEpsilon<double>
|
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
// A number near 1e-12 that is exactly representable in a double.
|
2014-05-22 22:15:06 +04:00
|
|
|
static double value() { return 1.0 / (1LL << 40); }
|
2014-02-06 02:04:42 +04: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 10:34:08 +04:00
|
|
|
* are less than or equal to aEpsilon apart. The default value of aEpsilon is
|
|
|
|
* near 1e-5.
|
2014-02-06 02:04:42 +04: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 10:34:08 +04: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-06 02:04:42 +04:00
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-13 10:34:08 +04:00
|
|
|
FuzzyEqualsAdditive(T aValue1, T aValue2,
|
|
|
|
T aEpsilon = detail::FuzzyEqualsEpsilon<T>::value())
|
2014-02-06 02:04:42 +04:00
|
|
|
{
|
|
|
|
static_assert(IsFloatingPoint<T>::value, "floating point type required");
|
2014-06-13 10:34:08 +04:00
|
|
|
return Abs(aValue1 - aValue2) <= aEpsilon;
|
2014-02-06 02:04:42 +04: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 10:34:08 +04:00
|
|
|
* some aEpsilon apart, where the aEpsilon is scaled by the smaller of the two
|
2014-02-06 02:04:42 +04: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 10:34:08 +04:00
|
|
|
* the floating point numbers being compared, regardless of what order of
|
|
|
|
* magnitude those numbers are at.
|
2014-02-06 02:04:42 +04:00
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
static MOZ_ALWAYS_INLINE bool
|
2014-06-13 10:34:08 +04:00
|
|
|
FuzzyEqualsMultiplicative(T aValue1, T aValue2,
|
|
|
|
T aEpsilon = detail::FuzzyEqualsEpsilon<T>::value())
|
2014-02-06 02:04:42 +04:00
|
|
|
{
|
|
|
|
static_assert(IsFloatingPoint<T>::value, "floating point type required");
|
|
|
|
// can't use std::min because of bug 965340
|
2014-06-13 10:34:08 +04:00
|
|
|
T smaller = Abs(aValue1) < Abs(aValue2) ? Abs(aValue1) : Abs(aValue2);
|
|
|
|
return Abs(aValue1 - aValue2) <= aEpsilon * smaller;
|
2014-02-06 02:04:42 +04:00
|
|
|
}
|
|
|
|
|
2013-10-22 16:55:35 +04:00
|
|
|
/**
|
|
|
|
* Returns true if the given value can be losslessly represented as an IEEE-754
|
|
|
|
* single format number, false otherwise. All NaN values are considered
|
|
|
|
* representable (notwithstanding that the exact bit pattern of a double format
|
|
|
|
* NaN value can't be exactly represented in single format).
|
|
|
|
*
|
|
|
|
* This function isn't inlined to avoid buggy optimizations by MSVC.
|
|
|
|
*/
|
|
|
|
MOZ_WARN_UNUSED_RESULT
|
|
|
|
extern MFBT_API bool
|
2014-06-13 10:34:08 +04:00
|
|
|
IsFloat32Representable(double aFloat32);
|
2013-10-22 16:55:35 +04:00
|
|
|
|
2013-05-02 00:55:13 +04:00
|
|
|
} /* namespace mozilla */
|
2012-01-23 15:43:16 +04:00
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_FloatingPoint_h */
|