Bug 932076 - Add check for MediaExtractor creation failure. r=doublec
[gecko.git] / mfbt / FloatingPoint.h
blobeff26b4bb807a6deafd959c24782550631b10c92
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"
17 #include <stdint.h>
19 namespace mozilla {
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) ==
59 ~uint64_t(0),
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) ==
83 ~uint32_t(0),
84 "all bits accounted for");
86 /** Determines whether a double is NaN. */
87 static MOZ_ALWAYS_INLINE bool
88 IsNaN(double d)
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
101 IsInfinite(double d)
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
110 IsFinite(double d)
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
125 IsNegative(double d)
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
163 PositiveInfinity()
166 * Positive infinity has all exponent bits set, sign bit set to 0, and no
167 * significand.
169 return BitwiseCast<double>(DoubleExponentBits);
172 /** Returns -Infinity. */
173 static MOZ_ALWAYS_INLINE double
174 NegativeInfinity()
177 * Negative infinity has all exponent bits set, sign bit set to 1, and no
178 * significand.
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) |
192 DoubleExponentBits |
193 significand);
194 MOZ_ASSERT(IsNaN(d));
195 return d;
198 /** Computes the smallest non-zero positive double value. */
199 static MOZ_ALWAYS_INLINE double
200 MinDoubleValue()
202 return BitwiseCast<double>(uint64_t(1));
205 static MOZ_ALWAYS_INLINE bool
206 DoubleIsInt32(double d, int32_t* i)
209 * XXX Casting a double that doesn't truncate to int32_t, to int32_t, induces
210 * undefined behavior. We should definitely fix this (bug 744965), but as
211 * apparently it "works" in practice, it's not a pressing concern now.
213 return !IsNegativeZero(d) && d == (*i = int32_t(d));
217 * Computes a NaN value. Do not use this method if you depend upon a particular
218 * NaN value being returned.
220 static MOZ_ALWAYS_INLINE double
221 UnspecifiedNaN()
224 * If we can use any quiet NaN, we might as well use the all-ones NaN,
225 * since it's cheap to materialize on common platforms (such as x64, where
226 * this value can be represented in a 32-bit signed immediate field, allowing
227 * it to be stored to memory in a single instruction).
229 return SpecificNaN(1, 0xfffffffffffffULL);
233 * Compare two doubles for equality, *without* equating -0 to +0, and equating
234 * any NaN value to any other NaN value. (The normal equality operators equate
235 * -0 with +0, and they equate NaN to no other value.)
237 static inline bool
238 DoublesAreIdentical(double d1, double d2)
240 if (IsNaN(d1))
241 return IsNaN(d2);
242 return BitwiseCast<uint64_t>(d1) == BitwiseCast<uint64_t>(d2);
245 /** Determines whether a float is NaN. */
246 static MOZ_ALWAYS_INLINE bool
247 IsFloatNaN(float f)
250 * A float is NaN if all exponent bits are 1 and the significand contains at
251 * least one non-zero bit.
253 uint32_t bits = BitwiseCast<uint32_t>(f);
254 return (bits & FloatExponentBits) == FloatExponentBits &&
255 (bits & FloatSignificandBits) != 0;
258 /** Constructs a NaN value with the specified sign bit and significand bits. */
259 static MOZ_ALWAYS_INLINE float
260 SpecificFloatNaN(int signbit, uint32_t significand)
262 MOZ_ASSERT(signbit == 0 || signbit == 1);
263 MOZ_ASSERT((significand & ~FloatSignificandBits) == 0);
264 MOZ_ASSERT(significand & FloatSignificandBits);
266 float f = BitwiseCast<float>((signbit ? FloatSignBit : 0) |
267 FloatExponentBits |
268 significand);
269 MOZ_ASSERT(IsFloatNaN(f));
270 return f;
274 * Returns true if the given value can be losslessly represented as an IEEE-754
275 * single format number, false otherwise. All NaN values are considered
276 * representable (notwithstanding that the exact bit pattern of a double format
277 * NaN value can't be exactly represented in single format).
279 * This function isn't inlined to avoid buggy optimizations by MSVC.
281 MOZ_WARN_UNUSED_RESULT
282 extern MFBT_API bool
283 IsFloat32Representable(double x);
285 } /* namespace mozilla */
287 #endif /* mozilla_FloatingPoint_h */