Bug 913282: IsFloat32Representable function in mfbt; r=Waldo
authorBenjamin Bouvier <benj@benj.me>
Tue, 22 Oct 2013 14:55:35 +0200
changeset 166449 9d216ff3bbe644cfbf69444e0633a1245267d7dc
parent 166448 8cb7a6a709b832dcbf936dcd564d39fdd08201aa
child 166450 0267af0f436625d3d847b569ace2effa83865ce2
push id428
push userbbajaj@mozilla.com
push dateTue, 28 Jan 2014 00:16:25 +0000
treeherdermozilla-release@cd72a7ff3a75 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs913282
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 913282: IsFloat32Representable function in mfbt; r=Waldo
js/src/jit/MIR.cpp
mfbt/FloatingPoint.cpp
mfbt/FloatingPoint.h
mfbt/sources.mk
--- a/js/src/jit/MIR.cpp
+++ b/js/src/jit/MIR.cpp
@@ -22,16 +22,17 @@
 #include "jsatominlines.h"
 #include "jsinferinlines.h"
 #include "jsobjinlines.h"
 
 using namespace js;
 using namespace js::jit;
 
 using mozilla::DoublesAreIdentical;
+using mozilla::IsFloat32Representable;
 using mozilla::Maybe;
 
 void
 MDefinition::PrintOpcodeName(FILE *fp, MDefinition::Opcode op)
 {
     static const char * const names[] =
     {
 #define NAME(x) #x,
@@ -488,25 +489,16 @@ MConstant::printOpcode(FILE *fp) const
       case MIRType_Magic:
         fprintf(fp, "magic");
         break;
       default:
         MOZ_ASSUME_UNREACHABLE("unexpected type");
     }
 }
 
-// Needs a static function to avoid overzealous optimizations by certain compilers (MSVC).
-static bool
-IsFloat32Representable(double x)
-{
-    float asFloat = static_cast<float>(x);
-    double floatAsDouble = static_cast<double>(asFloat);
-    return floatAsDouble == x;
-}
-
 bool
 MConstant::canProduceFloat32() const
 {
     if (!IsNumberType(type()))
         return false;
 
     if (type() == MIRType_Int32)
         return IsFloat32Representable(static_cast<double>(value_.toInt32()));
new file mode 100644
--- /dev/null
+++ b/mfbt/FloatingPoint.cpp
@@ -0,0 +1,20 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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/. */
+
+/* Implementations of FloatingPoint functions */
+
+#include "mozilla/FloatingPoint.h"
+
+namespace mozilla {
+
+bool
+IsFloat32Representable(double x)
+{
+    float asFloat = static_cast<float>(x);
+    double floatAsDouble = static_cast<double>(asFloat);
+    return floatAsDouble == x;
+}
+
+} /* namespace mozilla */
--- a/mfbt/FloatingPoint.h
+++ b/mfbt/FloatingPoint.h
@@ -7,16 +7,17 @@
 /* Various predicates and operations on IEEE-754 floating point types. */
 
 #ifndef mozilla_FloatingPoint_h
 #define mozilla_FloatingPoint_h
 
 #include "mozilla/Assertions.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/Casting.h"
+#include "mozilla/Types.h"
 
 #include <stdint.h>
 
 namespace mozilla {
 
 /*
  * It's reasonable to ask why we have this header at all.  Don't isnan,
  * copysign, the built-in comparison operators, and the like solve these
@@ -264,11 +265,23 @@ SpecificFloatNaN(int signbit, uint32_t s
 
   float f = BitwiseCast<float>((signbit ? FloatSignBit : 0) |
                                  FloatExponentBits |
                                  significand);
   MOZ_ASSERT(IsFloatNaN(f));
   return f;
 }
 
+/**
+ * Returns true if the given value can be losslessly represented as an IEEE-754
+ * single format number, false otherwise.  All NaN values are considered
+ * representable (notwithstanding that the exact bit pattern of a double format
+ * NaN value can't be exactly represented in single format).
+ *
+ * This function isn't inlined to avoid buggy optimizations by MSVC.
+ */
+MOZ_WARN_UNUSED_RESULT
+extern MFBT_API bool
+IsFloat32Representable(double x);
+
 } /* namespace mozilla */
 
 #endif /* mozilla_FloatingPoint_h */
--- a/mfbt/sources.mk
+++ b/mfbt/sources.mk
@@ -4,16 +4,17 @@
 
 ifndef MFBT_ROOT
 $(error Before including this file, you must define MFBT_ROOT to point to \
 the MFBT source directory)
 endif
 
 CPPSRCS += \
   Compression.cpp \
+  FloatingPoint.cpp \
   HashFunctions.cpp \
   Poison.cpp \
   SHA1.cpp \
   $(NULL)
 
 # Imported double-conversion sources.
 VPATH += $(MFBT_ROOT)/double-conversion \
   $(NULL)