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/. */
7 /* Various predicates and operations on IEEE-754 floating point types. */
9 #ifndef mozilla_FloatingPoint_h
10 #define mozilla_FloatingPoint_h
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 #include "mozilla/TypeTraits.h"
26 * It's reasonable to ask why we have this header at all. Don't isnan,
27 * copysign, the built-in comparison operators, and the like solve these
28 * problems? Unfortunately, they don't. We've found that various compilers
29 * (MSVC, MSVC when compiling with PGO, and GCC on OS X, at least) miscompile
30 * the standard methods in various situations, so we can't use them. Some of
31 * these compilers even have problems compiling seemingly reasonable bitwise
32 * algorithms! But with some care we've found algorithms that seem to not
33 * trigger those compiler bugs.
35 * For the aforementioned reasons, be very wary of making changes to any of
36 * these algorithms. If you must make changes, keep a careful eye out for
37 * compiler bustage, particularly PGO-specific bustage.
43 * These implementations assume float/double are 32/64-bit single/double
44 * format number types compatible with the IEEE-754 standard. C++ doesn't
45 * require this, but we required it in implementations of these algorithms that
46 * preceded this header, so we shouldn't break anything to continue doing so.
49 struct FloatingPointTrait
;
52 struct FloatingPointTrait
<float> {
54 using Bits
= uint32_t;
56 static constexpr unsigned kExponentWidth
= 8;
57 static constexpr unsigned kSignificandWidth
= 23;
61 struct FloatingPointTrait
<double> {
63 using Bits
= uint64_t;
65 static constexpr unsigned kExponentWidth
= 11;
66 static constexpr unsigned kSignificandWidth
= 52;
72 * This struct contains details regarding the encoding of floating-point
73 * numbers that can be useful for direct bit manipulation. As of now, the
74 * template parameter has to be float or double.
76 * The nested typedef |Bits| is the unsigned integral type with the same size
77 * as T: uint32_t for float and uint64_t for double (static assertions
78 * double-check these assumptions).
80 * kExponentBias is the offset that is subtracted from the exponent when
81 * computing the value, i.e. one plus the opposite of the mininum possible
83 * kExponentShift is the shift that one needs to apply to retrieve the
84 * exponent component of the value.
86 * kSignBit contains a bits mask. Bit-and-ing with this mask will result in
87 * obtaining the sign bit.
88 * kExponentBits contains the mask needed for obtaining the exponent bits and
89 * kSignificandBits contains the mask needed for obtaining the significand
92 * Full details of how floating point number formats are encoded are beyond
93 * the scope of this comment. For more information, see
94 * http://en.wikipedia.org/wiki/IEEE_floating_point
95 * http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers
98 struct FloatingPoint final
: private detail::FloatingPointTrait
<T
> {
100 using Base
= detail::FloatingPointTrait
<T
>;
104 * An unsigned integral type suitable for accessing the bitwise representation
107 using Bits
= typename
Base::Bits
;
109 static_assert(sizeof(T
) == sizeof(Bits
), "Bits must be same size as T");
111 /** The bit-width of the exponent component of T. */
112 using Base::kExponentWidth
;
114 /** The bit-width of the significand component of T. */
115 using Base::kSignificandWidth
;
117 static_assert(1 + kExponentWidth
+ kSignificandWidth
== CHAR_BIT
* sizeof(T
),
118 "sign bit plus bit widths should sum to overall bit width");
121 * The exponent field in an IEEE-754 floating point number consists of bits
122 * encoding an unsigned number. The *actual* represented exponent (for all
123 * values finite and not denormal) is that value, minus a bias |kExponentBias|
124 * so that a useful range of numbers is represented.
126 static constexpr unsigned kExponentBias
= (1U << (kExponentWidth
- 1)) - 1;
129 * The amount by which the bits of the exponent-field in an IEEE-754 floating
130 * point number are shifted from the LSB of the floating point type.
132 static constexpr unsigned kExponentShift
= kSignificandWidth
;
134 /** The sign bit in the floating point representation. */
135 static constexpr Bits kSignBit
= static_cast<Bits
>(1)
136 << (CHAR_BIT
* sizeof(Bits
) - 1);
138 /** The exponent bits in the floating point representation. */
139 static constexpr Bits kExponentBits
=
140 ((static_cast<Bits
>(1) << kExponentWidth
) - 1) << kSignificandWidth
;
142 /** The significand bits in the floating point representation. */
143 static constexpr Bits kSignificandBits
=
144 (static_cast<Bits
>(1) << kSignificandWidth
) - 1;
146 static_assert((kSignBit
& kExponentBits
) == 0,
147 "sign bit shouldn't overlap exponent bits");
148 static_assert((kSignBit
& kSignificandBits
) == 0,
149 "sign bit shouldn't overlap significand bits");
150 static_assert((kExponentBits
& kSignificandBits
) == 0,
151 "exponent bits shouldn't overlap significand bits");
153 static_assert((kSignBit
| kExponentBits
| kSignificandBits
) == ~Bits(0),
154 "all bits accounted for");
157 /** Determines whether a float/double is NaN. */
158 template <typename T
>
159 static MOZ_ALWAYS_INLINE
bool IsNaN(T aValue
) {
161 * A float/double is NaN if all exponent bits are 1 and the significand
162 * contains at least one non-zero bit.
164 typedef FloatingPoint
<T
> Traits
;
165 typedef typename
Traits::Bits Bits
;
166 return (BitwiseCast
<Bits
>(aValue
) & Traits::kExponentBits
) ==
167 Traits::kExponentBits
&&
168 (BitwiseCast
<Bits
>(aValue
) & Traits::kSignificandBits
) != 0;
171 /** Determines whether a float/double is +Infinity or -Infinity. */
172 template <typename T
>
173 static MOZ_ALWAYS_INLINE
bool IsInfinite(T aValue
) {
174 /* Infinities have all exponent bits set to 1 and an all-0 significand. */
175 typedef FloatingPoint
<T
> Traits
;
176 typedef typename
Traits::Bits Bits
;
177 Bits bits
= BitwiseCast
<Bits
>(aValue
);
178 return (bits
& ~Traits::kSignBit
) == Traits::kExponentBits
;
181 /** Determines whether a float/double is not NaN or infinite. */
182 template <typename T
>
183 static MOZ_ALWAYS_INLINE
bool IsFinite(T aValue
) {
185 * NaN and Infinities are the only non-finite floats/doubles, and both have
186 * all exponent bits set to 1.
188 typedef FloatingPoint
<T
> Traits
;
189 typedef typename
Traits::Bits Bits
;
190 Bits bits
= BitwiseCast
<Bits
>(aValue
);
191 return (bits
& Traits::kExponentBits
) != Traits::kExponentBits
;
195 * Determines whether a float/double is negative or -0. It is an error
196 * to call this method on a float/double which is NaN.
198 template <typename T
>
199 static MOZ_ALWAYS_INLINE
bool IsNegative(T aValue
) {
200 MOZ_ASSERT(!IsNaN(aValue
), "NaN does not have a sign");
202 /* The sign bit is set if the double is negative. */
203 typedef FloatingPoint
<T
> Traits
;
204 typedef typename
Traits::Bits Bits
;
205 Bits bits
= BitwiseCast
<Bits
>(aValue
);
206 return (bits
& Traits::kSignBit
) != 0;
209 /** Determines whether a float/double represents -0. */
210 template <typename T
>
211 static MOZ_ALWAYS_INLINE
bool IsNegativeZero(T aValue
) {
212 /* Only the sign bit is set if the value is -0. */
213 typedef FloatingPoint
<T
> Traits
;
214 typedef typename
Traits::Bits Bits
;
215 Bits bits
= BitwiseCast
<Bits
>(aValue
);
216 return bits
== Traits::kSignBit
;
219 /** Determines wether a float/double represents +0. */
220 template <typename T
>
221 static MOZ_ALWAYS_INLINE
bool IsPositiveZero(T aValue
) {
222 /* All bits are zero if the value is +0. */
223 typedef FloatingPoint
<T
> Traits
;
224 typedef typename
Traits::Bits Bits
;
225 Bits bits
= BitwiseCast
<Bits
>(aValue
);
230 * Returns 0 if a float/double is NaN or infinite;
231 * otherwise, the float/double is returned.
233 template <typename T
>
234 static MOZ_ALWAYS_INLINE T
ToZeroIfNonfinite(T aValue
) {
235 return IsFinite(aValue
) ? aValue
: 0;
239 * Returns the exponent portion of the float/double.
241 * Zero is not special-cased, so ExponentComponent(0.0) is
242 * -int_fast16_t(Traits::kExponentBias).
244 template <typename T
>
245 static MOZ_ALWAYS_INLINE
int_fast16_t ExponentComponent(T aValue
) {
247 * The exponent component of a float/double is an unsigned number, biased
248 * from its actual value. Subtract the bias to retrieve the actual exponent.
250 typedef FloatingPoint
<T
> Traits
;
251 typedef typename
Traits::Bits Bits
;
252 Bits bits
= BitwiseCast
<Bits
>(aValue
);
253 return int_fast16_t((bits
& Traits::kExponentBits
) >>
254 Traits::kExponentShift
) -
255 int_fast16_t(Traits::kExponentBias
);
258 /** Returns +Infinity. */
259 template <typename T
>
260 static MOZ_ALWAYS_INLINE T
PositiveInfinity() {
262 * Positive infinity has all exponent bits set, sign bit set to 0, and no
265 typedef FloatingPoint
<T
> Traits
;
266 return BitwiseCast
<T
>(Traits::kExponentBits
);
269 /** Returns -Infinity. */
270 template <typename T
>
271 static MOZ_ALWAYS_INLINE T
NegativeInfinity() {
273 * Negative infinity has all exponent bits set, sign bit set to 1, and no
276 typedef FloatingPoint
<T
> Traits
;
277 return BitwiseCast
<T
>(Traits::kSignBit
| Traits::kExponentBits
);
281 * Computes the bit pattern for a NaN with the specified sign bit and
284 template <typename T
, int SignBit
, typename FloatingPoint
<T
>::Bits Significand
>
285 struct SpecificNaNBits
{
286 using Traits
= FloatingPoint
<T
>;
288 static_assert(SignBit
== 0 || SignBit
== 1, "bad sign bit");
289 static_assert((Significand
& ~Traits::kSignificandBits
) == 0,
290 "significand must only have significand bits set");
291 static_assert(Significand
& Traits::kSignificandBits
,
292 "significand must be nonzero");
294 static constexpr typename
Traits::Bits value
=
295 (SignBit
* Traits::kSignBit
) | Traits::kExponentBits
| Significand
;
299 * Constructs a NaN value with the specified sign bit and significand bits.
301 * There is also a variant that returns the value directly. In most cases, the
302 * two variants should be identical. However, in the specific case of x86
303 * chips, the behavior differs: returning floating-point values directly is done
304 * through the x87 stack, and x87 loads and stores turn signaling NaNs into
305 * quiet NaNs... silently. Returning floating-point values via outparam,
306 * however, is done entirely within the SSE registers when SSE2 floating-point
307 * is enabled in the compiler, which has semantics-preserving behavior you would
310 * If preserving the distinction between signaling NaNs and quiet NaNs is
311 * important to you, you should use the outparam version. In all other cases,
312 * you should use the direct return version.
314 template <typename T
>
315 static MOZ_ALWAYS_INLINE
void SpecificNaN(
316 int signbit
, typename FloatingPoint
<T
>::Bits significand
, T
* result
) {
317 typedef FloatingPoint
<T
> Traits
;
318 MOZ_ASSERT(signbit
== 0 || signbit
== 1);
319 MOZ_ASSERT((significand
& ~Traits::kSignificandBits
) == 0);
320 MOZ_ASSERT(significand
& Traits::kSignificandBits
);
323 (signbit
? Traits::kSignBit
: 0) | Traits::kExponentBits
| significand
,
325 MOZ_ASSERT(IsNaN(*result
));
328 template <typename T
>
329 static MOZ_ALWAYS_INLINE T
330 SpecificNaN(int signbit
, typename FloatingPoint
<T
>::Bits significand
) {
332 SpecificNaN(signbit
, significand
, &t
);
336 /** Computes the smallest non-zero positive float/double value. */
337 template <typename T
>
338 static MOZ_ALWAYS_INLINE T
MinNumberValue() {
339 typedef FloatingPoint
<T
> Traits
;
340 typedef typename
Traits::Bits Bits
;
341 return BitwiseCast
<T
>(Bits(1));
346 template <typename Float
, typename SignedInteger
>
347 inline bool NumberEqualsSignedInteger(Float aValue
, SignedInteger
* aInteger
) {
348 static_assert(IsSame
<Float
, float>::value
|| IsSame
<Float
, double>::value
,
349 "Float must be an IEEE-754 floating point type");
350 static_assert(IsSigned
<SignedInteger
>::value
,
351 "this algorithm only works for signed types: a different one "
352 "will be required for unsigned types");
353 static_assert(sizeof(SignedInteger
) >= sizeof(int),
354 "this function *might* require some finessing for signed types "
355 "subject to integral promotion before it can be used on them");
357 MOZ_MAKE_MEM_UNDEFINED(aInteger
, sizeof(*aInteger
));
359 // NaNs and infinities are not integers.
360 if (!IsFinite(aValue
)) {
364 // Otherwise do direct comparisons against the minimum/maximum |SignedInteger|
365 // values that can be encoded in |Float|.
367 constexpr SignedInteger MaxIntValue
=
368 std::numeric_limits
<SignedInteger
>::max(); // e.g. INT32_MAX
369 constexpr SignedInteger MinValue
=
370 std::numeric_limits
<SignedInteger
>::min(); // e.g. INT32_MIN
372 static_assert(IsPowerOfTwo(Abs(MinValue
)),
373 "MinValue should be is a small power of two, thus exactly "
374 "representable in float/double both");
376 constexpr unsigned SignedIntegerWidth
= CHAR_BIT
* sizeof(SignedInteger
);
377 constexpr unsigned ExponentShift
= FloatingPoint
<Float
>::kExponentShift
;
379 // Careful! |MaxIntValue| may not be the maximum |SignedInteger| value that
380 // can be encoded in |Float|. Its |SignedIntegerWidth - 1| bits of precision
381 // may exceed |Float|'s |ExponentShift + 1| bits of precision. If necessary,
382 // compute the maximum |SignedInteger| that fits in |Float| from IEEE-754
383 // first principles. (|MinValue| doesn't have this problem because as a
384 // [relatively] small power of two it's always representable in |Float|.)
386 // Per C++11 [expr.const]p2, unevaluated subexpressions of logical AND/OR and
387 // conditional expressions *may* contain non-constant expressions, without
388 // making the enclosing expression not constexpr. MSVC implements this -- but
389 // it sometimes warns about undefined behavior in unevaluated subexpressions.
390 // This bites us if we initialize |MaxValue| the obvious way including an
391 // |uint64_t(1) << (SignedIntegerWidth - 2 - ExponentShift)| subexpression.
392 // Pull that shift-amount out and give it a not-too-huge value when it's in an
393 // unevaluated subexpression. 🙄
394 constexpr unsigned PrecisionExceededShiftAmount
=
395 ExponentShift
> SignedIntegerWidth
- 1
397 : SignedIntegerWidth
- 2 - ExponentShift
;
399 constexpr SignedInteger MaxValue
=
400 ExponentShift
> SignedIntegerWidth
- 1
402 : SignedInteger((uint64_t(1) << (SignedIntegerWidth
- 1)) -
403 (uint64_t(1) << PrecisionExceededShiftAmount
));
405 if (static_cast<Float
>(MinValue
) <= aValue
&&
406 aValue
<= static_cast<Float
>(MaxValue
)) {
407 auto possible
= static_cast<SignedInteger
>(aValue
);
408 if (static_cast<Float
>(possible
) == aValue
) {
409 *aInteger
= possible
;
417 template <typename Float
, typename SignedInteger
>
418 inline bool NumberIsSignedInteger(Float aValue
, SignedInteger
* aInteger
) {
419 static_assert(IsSame
<Float
, float>::value
|| IsSame
<Float
, double>::value
,
420 "Float must be an IEEE-754 floating point type");
421 static_assert(IsSigned
<SignedInteger
>::value
,
422 "this algorithm only works for signed types: a different one "
423 "will be required for unsigned types");
424 static_assert(sizeof(SignedInteger
) >= sizeof(int),
425 "this function *might* require some finessing for signed types "
426 "subject to integral promotion before it can be used on them");
428 MOZ_MAKE_MEM_UNDEFINED(aInteger
, sizeof(*aInteger
));
430 if (IsNegativeZero(aValue
)) {
434 return NumberEqualsSignedInteger(aValue
, aInteger
);
437 } // namespace detail
440 * If |aValue| is identical to some |int32_t| value, set |*aInt32| to that value
441 * and return true. Otherwise return false, leaving |*aInt32| in an
442 * indeterminate state.
444 * This method returns false for negative zero. If you want to consider -0 to
445 * be 0, use NumberEqualsInt32 below.
447 template <typename T
>
448 static MOZ_ALWAYS_INLINE
bool NumberIsInt32(T aValue
, int32_t* aInt32
) {
449 return detail::NumberIsSignedInteger(aValue
, aInt32
);
453 * If |aValue| is equal to some int32_t value (where -0 and +0 are considered
454 * equal), set |*aInt32| to that value and return true. Otherwise return false,
455 * leaving |*aInt32| in an indeterminate state.
457 * |NumberEqualsInt32(-0.0, ...)| will return true. To test whether a value can
458 * be losslessly converted to |int32_t| and back, use NumberIsInt32 above.
460 template <typename T
>
461 static MOZ_ALWAYS_INLINE
bool NumberEqualsInt32(T aValue
, int32_t* aInt32
) {
462 return detail::NumberEqualsSignedInteger(aValue
, aInt32
);
466 * Computes a NaN value. Do not use this method if you depend upon a particular
467 * NaN value being returned.
469 template <typename T
>
470 static MOZ_ALWAYS_INLINE T
UnspecifiedNaN() {
472 * If we can use any quiet NaN, we might as well use the all-ones NaN,
473 * since it's cheap to materialize on common platforms (such as x64, where
474 * this value can be represented in a 32-bit signed immediate field, allowing
475 * it to be stored to memory in a single instruction).
477 typedef FloatingPoint
<T
> Traits
;
478 return SpecificNaN
<T
>(1, Traits::kSignificandBits
);
482 * Compare two doubles for equality, *without* equating -0 to +0, and equating
483 * any NaN value to any other NaN value. (The normal equality operators equate
484 * -0 with +0, and they equate NaN to no other value.)
486 template <typename T
>
487 static inline bool NumbersAreIdentical(T aValue1
, T aValue2
) {
488 typedef FloatingPoint
<T
> Traits
;
489 typedef typename
Traits::Bits Bits
;
490 if (IsNaN(aValue1
)) {
491 return IsNaN(aValue2
);
493 return BitwiseCast
<Bits
>(aValue1
) == BitwiseCast
<Bits
>(aValue2
);
498 template <typename T
>
499 struct FuzzyEqualsEpsilon
;
502 struct FuzzyEqualsEpsilon
<float> {
503 // A number near 1e-5 that is exactly representable in a float.
504 static float value() { return 1.0f
/ (1 << 17); }
508 struct FuzzyEqualsEpsilon
<double> {
509 // A number near 1e-12 that is exactly representable in a double.
510 static double value() { return 1.0 / (1LL << 40); }
513 } // namespace detail
516 * Compare two floating point values for equality, modulo rounding error. That
517 * is, the two values are considered equal if they are both not NaN and if they
518 * are less than or equal to aEpsilon apart. The default value of aEpsilon is
521 * For most scenarios you will want to use FuzzyEqualsMultiplicative instead,
522 * as it is more reasonable over the entire range of floating point numbers.
523 * This additive version should only be used if you know the range of the
524 * numbers you are dealing with is bounded and stays around the same order of
527 template <typename T
>
528 static MOZ_ALWAYS_INLINE
bool FuzzyEqualsAdditive(
529 T aValue1
, T aValue2
, T aEpsilon
= detail::FuzzyEqualsEpsilon
<T
>::value()) {
530 static_assert(IsFloatingPoint
<T
>::value
, "floating point type required");
531 return Abs(aValue1
- aValue2
) <= aEpsilon
;
535 * Compare two floating point values for equality, allowing for rounding error
536 * relative to the magnitude of the values. That is, the two values are
537 * considered equal if they are both not NaN and they are less than or equal to
538 * some aEpsilon apart, where the aEpsilon is scaled by the smaller of the two
541 * In most cases you will want to use this rather than FuzzyEqualsAdditive, as
542 * this function effectively masks out differences in the bottom few bits of
543 * the floating point numbers being compared, regardless of what order of
544 * magnitude those numbers are at.
546 template <typename T
>
547 static MOZ_ALWAYS_INLINE
bool FuzzyEqualsMultiplicative(
548 T aValue1
, T aValue2
, T aEpsilon
= detail::FuzzyEqualsEpsilon
<T
>::value()) {
549 static_assert(IsFloatingPoint
<T
>::value
, "floating point type required");
550 // can't use std::min because of bug 965340
551 T smaller
= Abs(aValue1
) < Abs(aValue2
) ? Abs(aValue1
) : Abs(aValue2
);
552 return Abs(aValue1
- aValue2
) <= aEpsilon
* smaller
;
556 * Returns true if |aValue| can be losslessly represented as an IEEE-754 single
557 * precision number, false otherwise. All NaN values are considered
558 * representable (even though the bit patterns of double precision NaNs can't
559 * all be exactly represented in single precision).
562 extern MFBT_API
bool IsFloat32Representable(double aValue
);
564 } /* namespace mozilla */
566 #endif /* mozilla_FloatingPoint_h */