Bug 1248555: Introduce variants of SpecificNaN / BitwiseCast that preserve the signaling NaN bit; r=froydnj

MozReview-Commit-ID: 5A8p06nBqyI

--HG--
extra : rebase_source : 87ebcc9792d42c6b2e6164a639340da37b4b602c
extra : histedit_source : b4ab7c6b9f4bb7fe2cc88a3208d6fa8e6fefce08
This commit is contained in:
Benjamin Bouvier 2016-08-01 20:02:05 +02:00
parent 36ab545d49
commit 23575fdec5
2 changed files with 57 additions and 11 deletions

View File

@ -17,16 +17,29 @@
namespace mozilla {
/**
* Return a value of type |To|, containing the underlying bit pattern of
* Sets the outparam value of type |To| with the same underlying bit pattern of
* |aFrom|.
*
* |To| and |From| must be types of the same size; be careful of cross-platform
* size differences, or this might fail to compile on some but not all
* platforms.
*
* 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.
*/
template<typename To, typename From>
inline To
BitwiseCast(const From aFrom)
inline void
BitwiseCast(const From aFrom, To* aResult)
{
static_assert(sizeof(From) == sizeof(To),
"To and From must have the same size");
@ -36,7 +49,16 @@ BitwiseCast(const From aFrom)
To mTo;
} u;
u.mFrom = aFrom;
return u.mTo;
*aResult = u.mTo;
}
template<typename To, typename From>
inline To
BitwiseCast(const From aFrom)
{
To temp;
BitwiseCast<To, From>(aFrom, &temp);
return temp;
}
namespace detail {

View File

@ -245,20 +245,44 @@ NegativeInfinity()
}
/** Constructs a NaN value with the specified sign bit and significand bits. */
/**
* 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.
*/
template<typename T>
static MOZ_ALWAYS_INLINE T
SpecificNaN(int signbit, typename FloatingPoint<T>::Bits significand)
static MOZ_ALWAYS_INLINE void
SpecificNaN(int signbit, typename FloatingPoint<T>::Bits significand, T* result)
{
typedef FloatingPoint<T> Traits;
MOZ_ASSERT(signbit == 0 || signbit == 1);
MOZ_ASSERT((significand & ~Traits::kSignificandBits) == 0);
MOZ_ASSERT(significand & Traits::kSignificandBits);
T t = BitwiseCast<T>((signbit ? Traits::kSignBit : 0) |
Traits::kExponentBits |
significand);
MOZ_ASSERT(IsNaN(t));
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);
return t;
}