Bug 1182370 - _BitScan* does not modify `*pIndex` if input is zero. - r=waldo
authorJeff Gilbert <jgilbert@mozilla.com>
Fri, 17 Jul 2015 10:24:30 -0700
changeset 253521 5b4a4d8e5a801ba292c589d0d47c4b30971b8448
parent 253520 37a7604e5e1e3b3ebb0de6c98f5b7e9ec6c0a75c
child 253522 cd1d4134f29c1b26f5c7f542a66d35247a2f934e
push id29067
push userkwierso@gmail.com
push dateSat, 18 Jul 2015 00:57:04 +0000
treeherdermozilla-central@e2f2eb9ecca0 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerswaldo
bugs1182370
milestone42.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 1182370 - _BitScan* does not modify `*pIndex` if input is zero. - r=waldo
mfbt/MathAlgorithms.h
--- a/mfbt/MathAlgorithms.h
+++ b/mfbt/MathAlgorithms.h
@@ -165,26 +165,28 @@ namespace mozilla {
 namespace detail {
 
 #if defined(MOZ_BITSCAN_WINDOWS)
 
 inline uint_fast8_t
 CountLeadingZeroes32(uint32_t aValue)
 {
   unsigned long index;
-  _BitScanReverse(&index, static_cast<unsigned long>(aValue));
+  if (!_BitScanReverse(&index, static_cast<unsigned long>(aValue)))
+      return 32;
   return uint_fast8_t(31 - index);
 }
 
 
 inline uint_fast8_t
 CountTrailingZeroes32(uint32_t aValue)
 {
   unsigned long index;
-  _BitScanForward(&index, static_cast<unsigned long>(aValue));
+  if (!_BitScanForward(&index, static_cast<unsigned long>(aValue)))
+      return 32;
   return uint_fast8_t(index);
 }
 
 inline uint_fast8_t
 CountPopulation32(uint32_t aValue)
 {
   uint32_t x = aValue - ((aValue >> 1) & 0x55555555);
   x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
@@ -197,33 +199,35 @@ CountPopulation64(uint64_t aValue)
                       CountPopulation32(aValue >> 32));
 }
 
 inline uint_fast8_t
 CountLeadingZeroes64(uint64_t aValue)
 {
 #if defined(MOZ_BITSCAN_WINDOWS64)
   unsigned long index;
-  _BitScanReverse64(&index, static_cast<unsigned __int64>(aValue));
+  if (!_BitScanReverse64(&index, static_cast<unsigned __int64>(aValue)))
+      return 64;
   return uint_fast8_t(63 - index);
 #else
   uint32_t hi = uint32_t(aValue >> 32);
   if (hi != 0) {
     return CountLeadingZeroes32(hi);
   }
   return 32u + CountLeadingZeroes32(uint32_t(aValue));
 #endif
 }
 
 inline uint_fast8_t
 CountTrailingZeroes64(uint64_t aValue)
 {
 #if defined(MOZ_BITSCAN_WINDOWS64)
   unsigned long index;
-  _BitScanForward64(&index, static_cast<unsigned __int64>(aValue));
+  if (!_BitScanForward64(&index, static_cast<unsigned __int64>(aValue)))
+      return 64;
   return uint_fast8_t(index);
 #else
   uint32_t lo = uint32_t(aValue);
   if (lo != 0) {
     return CountTrailingZeroes32(lo);
   }
   return 32u + CountTrailingZeroes32(uint32_t(aValue >> 32));
 #endif