Bug 1228182 - Use division directly for generating double in XorShift128PlusRNG. r=Waldo
authorXidorn Quan <quanxunzhen@gmail.com>
Thu, 03 Dec 2015 12:16:18 +1100
changeset 275380 2d2e1828198272efcc76d0a62b54cfd146143534
parent 275379 d873179a7da024a8d42b73e571d64b1eb53efa63
child 275381 3af84d7471c84aa7f29cac7dab591f7de4ff103b
push id16532
push usercbook@mozilla.com
push dateThu, 03 Dec 2015 11:09:04 +0000
treeherderfx-team@68a90b9e9293 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersWaldo
bugs1228182
milestone45.0a1
Bug 1228182 - Use division directly for generating double in XorShift128PlusRNG. r=Waldo
mfbt/XorShift128PlusRNG.h
--- a/mfbt/XorShift128PlusRNG.h
+++ b/mfbt/XorShift128PlusRNG.h
@@ -5,21 +5,20 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* The xorshift128+ pseudo-random number generator. */
 
 #ifndef mozilla_XorShift128Plus_h
 #define mozilla_XorShift128Plus_h
 
 #include "mozilla/Assertions.h"
+#include "mozilla/Attributes.h"
 #include "mozilla/FloatingPoint.h"
 
-#include <math.h>
-#include <memory.h>
-#include <stdint.h>
+#include <inttypes.h>
 
 namespace mozilla {
 namespace non_crypto {
 
 /*
  * A stream of pseudo-random numbers generated using the xorshift+ technique
  * described here:
  *
@@ -74,20 +73,20 @@ class XorShift128PlusRNG {
   double nextDouble() {
     /*
      * Because the IEEE 64-bit floating point format stores the leading '1' bit
      * of the mantissa implicitly, it effectively represents a mantissa in the
      * range [0, 2**53) in only 52 bits. FloatingPoint<double>::kExponentShift
      * is the width of the bitfield in the in-memory format, so we must add one
      * to get the mantissa's range.
      */
-    static const int kMantissaBits =
+    static MOZ_CONSTEXPR_VAR int kMantissaBits =
       mozilla::FloatingPoint<double>::kExponentShift + 1;
-    uint64_t mantissa = next() & ((1ULL << kMantissaBits) - 1);
-    return ldexp(static_cast<double>(mantissa), -kMantissaBits);
+    uint64_t mantissa = next() & ((UINT64_C(1) << kMantissaBits) - 1);
+    return double(mantissa) / (UINT64_C(1) << kMantissaBits);
   }
 
   /*
    * Set the stream's current state to |aState0| and |aState1|. These must not
    * both be zero; ideally, they should have an almost even mix of zero and one
    * bits.
    */
   void setState(uint64_t aState0, uint64_t aState1) {