diff --git a/mfbt/FloatingPoint.h b/mfbt/FloatingPoint.h index d0177b31e91..b80ce266f1a 100644 --- a/mfbt/FloatingPoint.h +++ b/mfbt/FloatingPoint.h @@ -139,7 +139,12 @@ IsNegativeZero(double d) return bits == DoubleSignBit; } -/** Returns the exponent portion of the double. */ +/** + * Returns the exponent portion of the double. + * + * Zero is not special-cased, so ExponentComponent(0.0) is + * -int_fast16_t(DoubleExponentBias). + */ static MOZ_ALWAYS_INLINE int_fast16_t ExponentComponent(double d) { diff --git a/mfbt/tests/TestFloatingPoint.cpp b/mfbt/tests/TestFloatingPoint.cpp index 7ae9961fff6..844313f312a 100644 --- a/mfbt/tests/TestFloatingPoint.cpp +++ b/mfbt/tests/TestFloatingPoint.cpp @@ -8,6 +8,8 @@ #include using mozilla::DoublesAreIdentical; +using mozilla::DoubleExponentBias; +using mozilla::ExponentComponent; using mozilla::IsFinite; using mozilla::IsInfinite; using mozilla::IsNaN; @@ -102,6 +104,22 @@ TestDoublesAreIdentical() ShouldNotBeIdentical(UnspecifiedNaN(), NegativeInfinity()); } +static void +TestExponentComponent() +{ + MOZ_ASSERT(ExponentComponent(0.0) == -int_fast16_t(DoubleExponentBias)); + MOZ_ASSERT(ExponentComponent(-0.0) == -int_fast16_t(DoubleExponentBias)); + MOZ_ASSERT(ExponentComponent(0.125) == -3); + MOZ_ASSERT(ExponentComponent(0.5) == -1); + MOZ_ASSERT(ExponentComponent(1.0) == 0); + MOZ_ASSERT(ExponentComponent(1.5) == 0); + MOZ_ASSERT(ExponentComponent(2.0) == 1); + MOZ_ASSERT(ExponentComponent(7) == 2); + MOZ_ASSERT(ExponentComponent(PositiveInfinity()) == DoubleExponentBias + 1); + MOZ_ASSERT(ExponentComponent(NegativeInfinity()) == DoubleExponentBias + 1); + MOZ_ASSERT(ExponentComponent(UnspecifiedNaN()) == DoubleExponentBias + 1); +} + static void TestPredicates() { @@ -152,5 +170,6 @@ int main() { TestDoublesAreIdentical(); + TestExponentComponent(); TestPredicates(); }