1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* Various predicates and operations on IEEE-754 floating point types. */
8 #ifndef mozilla_FloatingPoint_h_
9 #define mozilla_FloatingPoint_h_
11 #include "mozilla/Assertions.h"
12 #include "mozilla/Attributes.h"
13 #include "mozilla/StandardInteger.h"
16 * It's reasonable to ask why we have this header at all. Don't isnan,
17 * copysign, the built-in comparison operators, and the like solve these
18 * problems? Unfortunately, they don't. We've found that various compilers
19 * (MSVC, MSVC when compiling with PGO, and GCC on OS X, at least) miscompile
20 * the standard methods in various situations, so we can't use them. Some of
21 * these compilers even have problems compiling seemingly reasonable bitwise
22 * algorithms! But with some care we've found algorithms that seem to not
23 * trigger those compiler bugs.
25 * For the aforementioned reasons, be very wary of making changes to any of
26 * these algorithms. If you must make changes, keep a careful eye out for
27 * compiler bustage, particularly PGO-specific bustage.
29 * Some users require that this file be C-compatible. Unfortunately, this means
30 * no mozilla namespace to contain everything, no detail namespace clarifying
31 * MozDoublePun to be an internal data structure, and so on.
35 * These implementations all assume |double| is a 64-bit double format number
36 * type, compatible with the IEEE-754 standard. C/C++ don't require this to be
37 * the case. But we required this in implementations of these algorithms that
38 * preceded this header, so we shouldn't break anything if we continue doing so.
40 MOZ_STATIC_ASSERT(sizeof(double) == sizeof(uint64_t), "double must be 64 bits");
43 * Constant expressions in C can't refer to consts, unfortunately, so #define
44 * these rather than use |const uint64_t|.
46 #define MOZ_DOUBLE_SIGN_BIT 0x8000000000000000ULL
47 #define MOZ_DOUBLE_EXPONENT_BITS 0x7ff0000000000000ULL
48 #define MOZ_DOUBLE_SIGNIFICAND_BITS 0x000fffffffffffffULL
50 #define MOZ_DOUBLE_EXPONENT_BIAS 1023
51 #define MOZ_DOUBLE_EXPONENT_SHIFT 52
53 MOZ_STATIC_ASSERT((MOZ_DOUBLE_SIGN_BIT
& MOZ_DOUBLE_EXPONENT_BITS
) == 0,
54 "sign bit doesn't overlap exponent bits");
55 MOZ_STATIC_ASSERT((MOZ_DOUBLE_SIGN_BIT
& MOZ_DOUBLE_SIGNIFICAND_BITS
) == 0,
56 "sign bit doesn't overlap significand bits");
57 MOZ_STATIC_ASSERT((MOZ_DOUBLE_EXPONENT_BITS
& MOZ_DOUBLE_SIGNIFICAND_BITS
) == 0,
58 "exponent bits don't overlap significand bits");
60 MOZ_STATIC_ASSERT((MOZ_DOUBLE_SIGN_BIT
| MOZ_DOUBLE_EXPONENT_BITS
| MOZ_DOUBLE_SIGNIFICAND_BITS
)
62 "all bits accounted for");
69 * This union is NOT a public data structure, and it is not to be used outside
74 * Every way to pun the bits of a double introduces an additional layer of
75 * complexity, across a multitude of platforms, architectures, and ABIs.
76 * Use *only* uint64_t to reduce complexity. Don't add new punning here
83 /** Determines whether a double is NaN. */
84 static MOZ_ALWAYS_INLINE
int
85 MOZ_DOUBLE_IS_NaN(double d
)
87 union MozDoublePun pun
;
91 * A double is NaN if all exponent bits are 1 and the significand contains at
92 * least one non-zero bit.
94 return (pun
.u
& MOZ_DOUBLE_EXPONENT_BITS
) == MOZ_DOUBLE_EXPONENT_BITS
&&
95 (pun
.u
& MOZ_DOUBLE_SIGNIFICAND_BITS
) != 0;
98 /** Determines whether a double is +Infinity or -Infinity. */
99 static MOZ_ALWAYS_INLINE
int
100 MOZ_DOUBLE_IS_INFINITE(double d
)
102 union MozDoublePun pun
;
105 /* Infinities have all exponent bits set to 1 and an all-0 significand. */
106 return (pun
.u
& ~MOZ_DOUBLE_SIGN_BIT
) == MOZ_DOUBLE_EXPONENT_BITS
;
109 /** Determines whether a double is not NaN or infinite. */
110 static MOZ_ALWAYS_INLINE
int
111 MOZ_DOUBLE_IS_FINITE(double d
)
113 union MozDoublePun pun
;
117 * NaN and Infinities are the only non-finite doubles, and both have all
118 * exponent bits set to 1.
120 return (pun
.u
& MOZ_DOUBLE_EXPONENT_BITS
) != MOZ_DOUBLE_EXPONENT_BITS
;
124 * Determines whether a double is negative. It is an error to call this method
125 * on a double which is NaN.
127 static MOZ_ALWAYS_INLINE
int
128 MOZ_DOUBLE_IS_NEGATIVE(double d
)
130 union MozDoublePun pun
;
133 MOZ_ASSERT(!MOZ_DOUBLE_IS_NaN(d
), "NaN does not have a sign");
135 /* The sign bit is set if the double is negative. */
136 return (pun
.u
& MOZ_DOUBLE_SIGN_BIT
) != 0;
139 /** Determines whether a double represents -0. */
140 static MOZ_ALWAYS_INLINE
int
141 MOZ_DOUBLE_IS_NEGATIVE_ZERO(double d
)
143 union MozDoublePun pun
;
146 /* Only the sign bit is set if the double is -0. */
147 return pun
.u
== MOZ_DOUBLE_SIGN_BIT
;
150 /** Returns the exponent portion of the double. */
151 static MOZ_ALWAYS_INLINE
int_fast16_t
152 MOZ_DOUBLE_EXPONENT(double d
)
154 union MozDoublePun pun
;
158 * The exponent component of a double is an unsigned number, biased from its
159 * actual value. Subtract the bias to retrieve the actual exponent.
161 return (int_fast16_t)((pun
.u
& MOZ_DOUBLE_EXPONENT_BITS
) >> MOZ_DOUBLE_EXPONENT_SHIFT
) -
162 MOZ_DOUBLE_EXPONENT_BIAS
;
165 /** Returns +Infinity. */
166 static MOZ_ALWAYS_INLINE
double
167 MOZ_DOUBLE_POSITIVE_INFINITY()
169 union MozDoublePun pun
;
172 * Positive infinity has all exponent bits set, sign bit set to 0, and no
175 pun
.u
= MOZ_DOUBLE_EXPONENT_BITS
;
179 /** Returns -Infinity. */
180 static MOZ_ALWAYS_INLINE
double
181 MOZ_DOUBLE_NEGATIVE_INFINITY()
183 union MozDoublePun pun
;
186 * Negative infinity has all exponent bits set, sign bit set to 1, and no
189 pun
.u
= MOZ_DOUBLE_SIGN_BIT
| MOZ_DOUBLE_EXPONENT_BITS
;
193 /** Constructs a NaN value with the specified sign bit and significand bits. */
194 static MOZ_ALWAYS_INLINE
double
195 MOZ_DOUBLE_SPECIFIC_NaN(int signbit
, uint64_t significand
)
197 union MozDoublePun pun
;
199 MOZ_ASSERT(signbit
== 0 || signbit
== 1);
200 MOZ_ASSERT((significand
& ~MOZ_DOUBLE_SIGNIFICAND_BITS
) == 0);
201 MOZ_ASSERT(significand
& MOZ_DOUBLE_SIGNIFICAND_BITS
);
203 pun
.u
= (signbit
? MOZ_DOUBLE_SIGN_BIT
: 0) |
204 MOZ_DOUBLE_EXPONENT_BITS
|
206 MOZ_ASSERT(MOZ_DOUBLE_IS_NaN(pun
.d
));
211 * Computes a NaN value. Do not use this method if you depend upon a particular
212 * NaN value being returned.
214 static MOZ_ALWAYS_INLINE
double
217 return MOZ_DOUBLE_SPECIFIC_NaN(0, 0xfffffffffffffULL
);
220 /** Computes the smallest non-zero positive double value. */
221 static MOZ_ALWAYS_INLINE
double
222 MOZ_DOUBLE_MIN_VALUE()
224 union MozDoublePun pun
;
229 static MOZ_ALWAYS_INLINE
int
230 MOZ_DOUBLE_IS_INT32(double d
, int32_t* i
)
233 * XXX Casting a double that doesn't truncate to int32_t, to int32_t, induces
234 * undefined behavior. We should definitely fix this (bug 744965), but as
235 * apparently it "works" in practice, it's not a pressing concern now.
237 return !MOZ_DOUBLE_IS_NEGATIVE_ZERO(d
) && d
== (*i
= (int32_t)d
);
244 #endif /* mozilla_FloatingPoint_h_ */