Bug 1443342 - Remove HashFunctions.h's RotateBitsLeft32 and use the general RotateLeft function instead. r=froydnj
authorJeff Walden <jwalden@mit.edu>
Thu, 01 Mar 2018 17:05:58 -0800
changeset 406815 9581711718570bc55ce69f4eb397f462ab0a639e
parent 406814 353bb6fbb68354a90173f5b9a9188b6de756d8e0
child 406816 6c03114535c2f46772920fec5ac13f724537500d
push id100514
push userjwalden@mit.edu
push dateTue, 06 Mar 2018 22:58:07 +0000
treeherdermozilla-inbound@60164da23cf4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1443342
milestone60.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 1443342 - Remove HashFunctions.h's RotateBitsLeft32 and use the general RotateLeft function instead. r=froydnj
mfbt/HashFunctions.h
mfbt/MathAlgorithms.h
--- a/mfbt/HashFunctions.h
+++ b/mfbt/HashFunctions.h
@@ -51,31 +51,23 @@
 #include "mozilla/Attributes.h"
 #include "mozilla/Char16.h"
 #include "mozilla/MathAlgorithms.h"
 #include "mozilla/Types.h"
 #include "mozilla/WrappingOperations.h"
 
 #include <stdint.h>
 
-#ifdef __cplusplus
 namespace mozilla {
 
 /**
  * The golden ratio as a 32-bit fixed-point value.
  */
 static const uint32_t kGoldenRatioU32 = 0x9E3779B9U;
 
-inline uint32_t
-RotateBitsLeft32(uint32_t aValue, uint8_t aBits)
-{
-  MOZ_ASSERT(aBits < 32);
-  return (aValue << aBits) | (aValue >> (32 - aBits));
-}
-
 namespace detail {
 
 inline uint32_t
 AddU32ToHash(uint32_t aHash, uint32_t aValue)
 {
   /*
    * This is the meat of all our hash routines.  This hash function is not
    * particularly sophisticated, but it seems to work well for our mostly
@@ -99,31 +91,31 @@ AddU32ToHash(uint32_t aHash, uint32_t aV
    *
    *   mozilla::WrappingMultiply(kGoldenRatioU32, RotateBitsLeft(aHash, 5))
    *   |xor|
    *   aValue
    *
    * evaluates to |aValue|.
    *
    * (Number-theoretic aside: Because any odd number |m| is relatively prime to
-   * our modulus (2^32), the list
+   * our modulus (2**32), the list
    *
-   *    [x * m (mod 2^32) for 0 <= x < 2^32]
+   *    [x * m (mod 2**32) for 0 <= x < 2**32]
    *
    * has no duplicate elements.  This means that multiplying by |m| does not
    * cause us to skip any possible hash values.
    *
-   * It's also nice if |m| has large-ish order mod 2^32 -- that is, if the
-   * smallest k such that m^k == 1 (mod 2^32) is large -- so we can safely
+   * It's also nice if |m| has large-ish order mod 2**32 -- that is, if the
+   * smallest k such that m**k == 1 (mod 2**32) is large -- so we can safely
    * multiply our hash value by |m| a few times without negating the
-   * multiplicative effect.  Our golden ratio constant has order 2^29, which is
+   * multiplicative effect.  Our golden ratio constant has order 2**29, which is
    * more than enough for our purposes.)
    */
   return mozilla::WrappingMultiply(kGoldenRatioU32,
-                                   (RotateBitsLeft32(aHash, 5) ^ aValue));
+                                   RotateLeft(aHash, 5) ^ aValue);
 }
 
 /**
  * AddUintptrToHash takes sizeof(uintptr_t) as a template parameter.
  */
 template<size_t PtrSize>
 inline uint32_t
 AddUintptrToHash(uint32_t aHash, uintptr_t aValue)
@@ -378,11 +370,10 @@ private:
       mV2 = RotateLeft(mV2, 32);
     }
 
     uint64_t mV0, mV1, mV2, mV3;
   };
 };
 
 } /* namespace mozilla */
-#endif /* __cplusplus */
 
 #endif /* mozilla_HashFunctions_h */
--- a/mfbt/MathAlgorithms.h
+++ b/mfbt/MathAlgorithms.h
@@ -475,43 +475,49 @@ RoundUpPow2(size_t aValue)
              "can't round up -- will overflow!");
   return size_t(1) << CeilingLog2(aValue);
 }
 
 /**
  * Rotates the bits of the given value left by the amount of the shift width.
  */
 template<typename T>
+MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW
 inline T
 RotateLeft(const T aValue, uint_fast8_t aShift)
 {
+  static_assert(IsUnsigned<T>::value, "Rotates require unsigned values");
+
   MOZ_ASSERT(aShift < sizeof(T) * CHAR_BIT, "Shift value is too large!");
   MOZ_ASSERT(aShift > 0,
              "Rotation by value length is undefined behavior, but compilers "
              "do not currently fold a test into the rotate instruction. "
              "Please remove this restriction when compilers optimize the "
              "zero case (http://blog.regehr.org/archives/1063).");
-  static_assert(IsUnsigned<T>::value, "Rotates require unsigned values");
+
   return (aValue << aShift) | (aValue >> (sizeof(T) * CHAR_BIT - aShift));
 }
 
 /**
  * Rotates the bits of the given value right by the amount of the shift width.
  */
 template<typename T>
+MOZ_NO_SANITIZE_UNSIGNED_OVERFLOW
 inline T
 RotateRight(const T aValue, uint_fast8_t aShift)
 {
+  static_assert(IsUnsigned<T>::value, "Rotates require unsigned values");
+
   MOZ_ASSERT(aShift < sizeof(T) * CHAR_BIT, "Shift value is too large!");
   MOZ_ASSERT(aShift > 0,
              "Rotation by value length is undefined behavior, but compilers "
              "do not currently fold a test into the rotate instruction. "
              "Please remove this restriction when compilers optimize the "
              "zero case (http://blog.regehr.org/archives/1063).");
-  static_assert(IsUnsigned<T>::value, "Rotates require unsigned values");
+
   return (aValue >> aShift) | (aValue << (sizeof(T) * CHAR_BIT - aShift));
 }
 
 /**
  * Returns true if |x| is a power of two.
  * Zero is not an integer power of two. (-Inf is not an integer)
  */
 template<typename T>