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/Types.h"
22 * It's reasonable to ask why we have this header at all. Don't isnan,
23 * copysign, the built-in comparison operators, and the like solve these
24 * problems? Unfortunately, they don't. We've found that various compilers
25 * (MSVC, MSVC when compiling with PGO, and GCC on OS X, at least) miscompile
26 * the standard methods in various situations, so we can't use them. Some of
27 * these compilers even have problems compiling seemingly reasonable bitwise
28 * algorithms! But with some care we've found algorithms that seem to not
29 * trigger those compiler bugs.
31 * For the aforementioned reasons, be very wary of making changes to any of
32 * these algorithms. If you must make changes, keep a careful eye out for
33 * compiler bustage, particularly PGO-specific bustage.
37 * These implementations all assume |double| is a 64-bit double format number
38 * type, compatible with the IEEE-754 standard. C/C++ don't require this to be
39 * the case. But we required this in implementations of these algorithms that
40 * preceded this header, so we shouldn't break anything if we continue doing so.
42 static_assert(sizeof(double) == sizeof(uint64_t), "double must be 64 bits");
44 const unsigned DoubleExponentBias
= 1023;
45 const unsigned DoubleExponentShift
= 52;
47 const uint64_t DoubleSignBit
= 0x8000000000000000ULL
;
48 const uint64_t DoubleExponentBits
= 0x7ff0000000000000ULL
;
49 const uint64_t DoubleSignificandBits
= 0x000fffffffffffffULL
;
51 static_assert((DoubleSignBit
& DoubleExponentBits
) == 0,
52 "sign bit doesn't overlap exponent bits");
53 static_assert((DoubleSignBit
& DoubleSignificandBits
) == 0,
54 "sign bit doesn't overlap significand bits");
55 static_assert((DoubleExponentBits
& DoubleSignificandBits
) == 0,
56 "exponent bits don't overlap significand bits");
58 static_assert((DoubleSignBit
| DoubleExponentBits
| DoubleSignificandBits
) ==
60 "all bits accounted for");
63 * Ditto for |float| that must be a 32-bit double format number type, compatible
64 * with the IEEE-754 standard.
66 static_assert(sizeof(float) == sizeof(uint32_t), "float must be 32bits");
68 const unsigned FloatExponentBias
= 127;
69 const unsigned FloatExponentShift
= 23;
71 const uint32_t FloatSignBit
= 0x80000000UL
;
72 const uint32_t FloatExponentBits
= 0x7F800000UL
;
73 const uint32_t FloatSignificandBits
= 0x007FFFFFUL
;
75 static_assert((FloatSignBit
& FloatExponentBits
) == 0,
76 "sign bit doesn't overlap exponent bits");
77 static_assert((FloatSignBit
& FloatSignificandBits
) == 0,
78 "sign bit doesn't overlap significand bits");
79 static_assert((FloatExponentBits
& FloatSignificandBits
) == 0,
80 "exponent bits don't overlap significand bits");
82 static_assert((FloatSignBit
| FloatExponentBits
| FloatSignificandBits
) ==
84 "all bits accounted for");
86 /** Determines whether a double is NaN. */
87 static MOZ_ALWAYS_INLINE
bool
91 * A double is NaN if all exponent bits are 1 and the significand contains at
92 * least one non-zero bit.
94 uint64_t bits
= BitwiseCast
<uint64_t>(d
);
95 return (bits
& DoubleExponentBits
) == DoubleExponentBits
&&
96 (bits
& DoubleSignificandBits
) != 0;
99 /** Determines whether a double is +Infinity or -Infinity. */
100 static MOZ_ALWAYS_INLINE
bool
103 /* Infinities have all exponent bits set to 1 and an all-0 significand. */
104 uint64_t bits
= BitwiseCast
<uint64_t>(d
);
105 return (bits
& ~DoubleSignBit
) == DoubleExponentBits
;
108 /** Determines whether a double is not NaN or infinite. */
109 static MOZ_ALWAYS_INLINE
bool
113 * NaN and Infinities are the only non-finite doubles, and both have all
114 * exponent bits set to 1.
116 uint64_t bits
= BitwiseCast
<uint64_t>(d
);
117 return (bits
& DoubleExponentBits
) != DoubleExponentBits
;
121 * Determines whether a double is negative. It is an error to call this method
122 * on a double which is NaN.
124 static MOZ_ALWAYS_INLINE
bool
127 MOZ_ASSERT(!IsNaN(d
), "NaN does not have a sign");
129 /* The sign bit is set if the double is negative. */
130 uint64_t bits
= BitwiseCast
<uint64_t>(d
);
131 return (bits
& DoubleSignBit
) != 0;
134 /** Determines whether a double represents -0. */
135 static MOZ_ALWAYS_INLINE
bool
136 IsNegativeZero(double d
)
138 /* Only the sign bit is set if the double is -0. */
139 uint64_t bits
= BitwiseCast
<uint64_t>(d
);
140 return bits
== DoubleSignBit
;
144 * Returns the exponent portion of the double.
146 * Zero is not special-cased, so ExponentComponent(0.0) is
147 * -int_fast16_t(DoubleExponentBias).
149 static MOZ_ALWAYS_INLINE
int_fast16_t
150 ExponentComponent(double d
)
153 * The exponent component of a double is an unsigned number, biased from its
154 * actual value. Subtract the bias to retrieve the actual exponent.
156 uint64_t bits
= BitwiseCast
<uint64_t>(d
);
157 return int_fast16_t((bits
& DoubleExponentBits
) >> DoubleExponentShift
) -
158 int_fast16_t(DoubleExponentBias
);
161 /** Returns +Infinity. */
162 static MOZ_ALWAYS_INLINE
double
166 * Positive infinity has all exponent bits set, sign bit set to 0, and no
169 return BitwiseCast
<double>(DoubleExponentBits
);
172 /** Returns -Infinity. */
173 static MOZ_ALWAYS_INLINE
double
177 * Negative infinity has all exponent bits set, sign bit set to 1, and no
180 return BitwiseCast
<double>(DoubleSignBit
| DoubleExponentBits
);
183 /** Constructs a NaN value with the specified sign bit and significand bits. */
184 static MOZ_ALWAYS_INLINE
double
185 SpecificNaN(int signbit
, uint64_t significand
)
187 MOZ_ASSERT(signbit
== 0 || signbit
== 1);
188 MOZ_ASSERT((significand
& ~DoubleSignificandBits
) == 0);
189 MOZ_ASSERT(significand
& DoubleSignificandBits
);
191 double d
= BitwiseCast
<double>((signbit
? DoubleSignBit
: 0) |
194 MOZ_ASSERT(IsNaN(d
));
198 /** Computes the smallest non-zero positive double value. */
199 static MOZ_ALWAYS_INLINE
double
202 return BitwiseCast
<double>(uint64_t(1));
206 * If d is equal to some int32_t value, set *i to that value and return true;
207 * otherwise return false.
209 * Note that negative zero is "equal" to zero here. To test whether a value can
210 * be losslessly converted to int32_t and back, use DoubleIsInt32 instead.
212 static MOZ_ALWAYS_INLINE
bool
213 DoubleEqualsInt32(double d
, int32_t* i
)
216 * XXX Casting a double that doesn't truncate to int32_t, to int32_t, induces
217 * undefined behavior. We should definitely fix this (bug 744965), but as
218 * apparently it "works" in practice, it's not a pressing concern now.
220 return d
== (*i
= int32_t(d
));
224 * If d can be converted to int32_t and back to an identical double value,
225 * set *i to that value and return true; otherwise return false.
227 * The difference between this and DoubleEqualsInt32 is that this method returns
228 * false for negative zero.
230 static MOZ_ALWAYS_INLINE
bool
231 DoubleIsInt32(double d
, int32_t* i
)
233 return !IsNegativeZero(d
) && DoubleEqualsInt32(d
, i
);
237 * Computes a NaN value. Do not use this method if you depend upon a particular
238 * NaN value being returned.
240 static MOZ_ALWAYS_INLINE
double
244 * If we can use any quiet NaN, we might as well use the all-ones NaN,
245 * since it's cheap to materialize on common platforms (such as x64, where
246 * this value can be represented in a 32-bit signed immediate field, allowing
247 * it to be stored to memory in a single instruction).
249 return SpecificNaN(1, 0xfffffffffffffULL
);
253 * Compare two doubles for equality, *without* equating -0 to +0, and equating
254 * any NaN value to any other NaN value. (The normal equality operators equate
255 * -0 with +0, and they equate NaN to no other value.)
258 DoublesAreIdentical(double d1
, double d2
)
262 return BitwiseCast
<uint64_t>(d1
) == BitwiseCast
<uint64_t>(d2
);
265 /** Determines whether a float is NaN. */
266 static MOZ_ALWAYS_INLINE
bool
270 * A float is NaN if all exponent bits are 1 and the significand contains at
271 * least one non-zero bit.
273 uint32_t bits
= BitwiseCast
<uint32_t>(f
);
274 return (bits
& FloatExponentBits
) == FloatExponentBits
&&
275 (bits
& FloatSignificandBits
) != 0;
278 /** Constructs a NaN value with the specified sign bit and significand bits. */
279 static MOZ_ALWAYS_INLINE
float
280 SpecificFloatNaN(int signbit
, uint32_t significand
)
282 MOZ_ASSERT(signbit
== 0 || signbit
== 1);
283 MOZ_ASSERT((significand
& ~FloatSignificandBits
) == 0);
284 MOZ_ASSERT(significand
& FloatSignificandBits
);
286 float f
= BitwiseCast
<float>((signbit
? FloatSignBit
: 0) |
289 MOZ_ASSERT(IsFloatNaN(f
));
294 * Returns true if the given value can be losslessly represented as an IEEE-754
295 * single format number, false otherwise. All NaN values are considered
296 * representable (notwithstanding that the exact bit pattern of a double format
297 * NaN value can't be exactly represented in single format).
299 * This function isn't inlined to avoid buggy optimizations by MSVC.
301 MOZ_WARN_UNUSED_RESULT
303 IsFloat32Representable(double x
);
305 } /* namespace mozilla */
307 #endif /* mozilla_FloatingPoint_h */