Skip to content

Commit

Permalink
Bug 1248555: Introduce variants of SpecificNaN / BitwiseCast that pre…
Browse files Browse the repository at this point in the history
…serve the signaling NaN bit; r=froydnj

MozReview-Commit-ID: 5A8p06nBqyI
  • Loading branch information
bnjbvr committed Aug 1, 2016
1 parent abfee96 commit 7d12f68
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
30 changes: 26 additions & 4 deletions mfbt/Casting.h
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 {
Expand Down
38 changes: 31 additions & 7 deletions mfbt/FloatingPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 7d12f68

Please sign in to comment.