Bug 924637 - Document a somewhat counterintuitive case in ExponentComponent, and add test coverage for it. r=waldo
authorDan Gohman <sunfish@google.com>
Wed, 09 Oct 2013 06:41:42 -0700
changeset 164056 43a3626b2552dd1067e58b739940b7a356a79ca8
parent 164055 c083b8cc5d2f850f589009804f5aea2a767d6cce
child 164057 a23f880b3bcb0feb9080010adffeebc5a6312f4c
push id3066
push userakeybl@mozilla.com
push dateMon, 09 Dec 2013 19:58:46 +0000
treeherdermozilla-beta@a31a0dce83aa [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswaldo
bugs924637
milestone27.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 924637 - Document a somewhat counterintuitive case in ExponentComponent, and add test coverage for it. r=waldo
mfbt/FloatingPoint.h
mfbt/tests/TestFloatingPoint.cpp
--- a/mfbt/FloatingPoint.h
+++ b/mfbt/FloatingPoint.h
@@ -134,17 +134,22 @@ IsNegative(double d)
 static MOZ_ALWAYS_INLINE bool
 IsNegativeZero(double d)
 {
   /* Only the sign bit is set if the double is -0. */
   uint64_t bits = BitwiseCast<uint64_t>(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)
 {
   /*
    * The exponent component of a double is an unsigned number, biased from its
    * actual value.  Subtract the bias to retrieve the actual exponent.
    */
   uint64_t bits = BitwiseCast<uint64_t>(d);
--- a/mfbt/tests/TestFloatingPoint.cpp
+++ b/mfbt/tests/TestFloatingPoint.cpp
@@ -3,16 +3,18 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/FloatingPoint.h"
 
 #include <math.h>
 
 using mozilla::DoublesAreIdentical;
+using mozilla::DoubleExponentBias;
+using mozilla::ExponentComponent;
 using mozilla::IsFinite;
 using mozilla::IsInfinite;
 using mozilla::IsNaN;
 using mozilla::IsNegative;
 using mozilla::IsNegativeZero;
 using mozilla::NegativeInfinity;
 using mozilla::PositiveInfinity;
 using mozilla::SpecificNaN;
@@ -98,16 +100,32 @@ TestDoublesAreIdentical()
   ShouldNotBeIdentical(UnspecifiedNaN(), -0.0);
   ShouldNotBeIdentical(UnspecifiedNaN(), 1.0);
   ShouldNotBeIdentical(UnspecifiedNaN(), -1.0);
   ShouldNotBeIdentical(UnspecifiedNaN(), PositiveInfinity());
   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()
 {
   MOZ_ASSERT(IsNaN(UnspecifiedNaN()));
   MOZ_ASSERT(IsNaN(SpecificNaN(1, 17)));;
   MOZ_ASSERT(IsNaN(SpecificNaN(0, 0xfffffffffff0fULL)));
   MOZ_ASSERT(!IsNaN(0));
   MOZ_ASSERT(!IsNaN(-0.0));
   MOZ_ASSERT(!IsNaN(1.0));
@@ -147,10 +165,11 @@ TestPredicates()
   MOZ_ASSERT(!IsNegativeZero(-1.0));
   MOZ_ASSERT(!IsNegativeZero(1.0));
 }
 
 int
 main()
 {
   TestDoublesAreIdentical();
+  TestExponentComponent();
   TestPredicates();
 }