Bug 924637 - Document a somewhat counterintuitive case in ExponentComponent, and add test coverage for it. r=waldo

This commit is contained in:
Dan Gohman 2013-10-09 06:41:42 -07:00
parent 773100dd6a
commit 77a1580baf
2 changed files with 25 additions and 1 deletions

View File

@ -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)
{

View File

@ -8,6 +8,8 @@
#include <math.h>
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();
}