<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[243418] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/243418">243418</a></dd>
<dt>Author</dt> <dd>keith_miller@apple.com</dd>
<dt>Date</dt> <dd>2019-03-23 18:32:06 -0700 (Sat, 23 Mar 2019)</dd>
</dl>

<h3>Log Message</h3>
<pre>Refactor clz/ctz and fix getLSBSet.
https://bugs.webkit.org/show_bug.cgi?id=196162

Reviewed by Saam Barati.

Source/JavaScriptCore:

Refactor references of clz32/64 and ctz32 to use clz and ctz,
respectively.

* dfg/DFGAbstractInterpreterInlines.h:
(JSC::DFG::AbstractInterpreter<AbstractStateType>::executeEffects):
* dfg/DFGOperations.cpp:
* runtime/JSBigInt.cpp:
(JSC::JSBigInt::digitDiv):
(JSC::JSBigInt::absoluteDivWithBigIntDivisor):
(JSC::JSBigInt::calculateMaximumCharactersRequired):
(JSC::JSBigInt::toStringBasePowerOfTwo):
(JSC::JSBigInt::compareToDouble):
* runtime/MathObject.cpp:
(JSC::mathProtoFuncClz32):

Source/WTF:

This patch makes clz32/64 and ctz32 generic so they work on any
numeric type. Since these methods work on any type we don't need
to have a separate implementation of getLSBSet, which also
happened to be getMSBSet. This patch also adds getMSBSet as there
may be users who want that in the future.

* wtf/MathExtras.h:
(WTF::clz):
(WTF::ctz):
(WTF::getLSBSet):
(WTF::getMSBSet):
(getLSBSet): Deleted.
(WTF::clz32): Deleted.
(WTF::clz64): Deleted.
(WTF::ctz32): Deleted.

Tools:

Add tests for clz, ctz, getLSBSet, and getMSBSet.

* TestWebKitAPI/Tests/WTF/MathExtras.cpp:
(TestWebKitAPI::TEST):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGAbstractInterpreterInlinesh">trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGOperationscpp">trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSBigIntcpp">trunk/Source/JavaScriptCore/runtime/JSBigInt.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeMathObjectcpp">trunk/Source/JavaScriptCore/runtime/MathObject.cpp</a></li>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfMathExtrash">trunk/Source/WTF/wtf/MathExtras.h</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWTFMathExtrascpp">trunk/Tools/TestWebKitAPI/Tests/WTF/MathExtras.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (243417 => 243418)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog    2019-03-24 00:47:08 UTC (rev 243417)
+++ trunk/Source/JavaScriptCore/ChangeLog       2019-03-24 01:32:06 UTC (rev 243418)
</span><span class="lines">@@ -1,3 +1,25 @@
</span><ins>+2019-03-23  Keith Miller  <keith_miller@apple.com>
+
+        Refactor clz/ctz and fix getLSBSet.
+        https://bugs.webkit.org/show_bug.cgi?id=196162
+
+        Reviewed by Saam Barati.
+
+        Refactor references of clz32/64 and ctz32 to use clz and ctz,
+        respectively.
+
+        * dfg/DFGAbstractInterpreterInlines.h:
+        (JSC::DFG::AbstractInterpreter<AbstractStateType>::executeEffects):
+        * dfg/DFGOperations.cpp:
+        * runtime/JSBigInt.cpp:
+        (JSC::JSBigInt::digitDiv):
+        (JSC::JSBigInt::absoluteDivWithBigIntDivisor):
+        (JSC::JSBigInt::calculateMaximumCharactersRequired):
+        (JSC::JSBigInt::toStringBasePowerOfTwo):
+        (JSC::JSBigInt::compareToDouble):
+        * runtime/MathObject.cpp:
+        (JSC::mathProtoFuncClz32):
+
</ins><span class="cx"> 2019-03-23  Yusuke Suzuki  <ysuzuki@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [JSC] Shrink sizeof(RegExp)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGAbstractInterpreterInlinesh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h (243417 => 243418)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h  2019-03-24 00:47:08 UTC (rev 243417)
+++ trunk/Source/JavaScriptCore/dfg/DFGAbstractInterpreterInlines.h     2019-03-24 01:32:06 UTC (rev 243418)
</span><span class="lines">@@ -704,7 +704,7 @@
</span><span class="cx">                 break;
</span><span class="cx">             }
</span><span class="cx">             uint32_t value = toUInt32(*number);
</span><del>-            setConstant(node, jsNumber(clz32(value)));
</del><ins>+            setConstant(node, jsNumber(clz(value)));
</ins><span class="cx">             break;
</span><span class="cx">         }
</span><span class="cx">         switch (node->child1().useKind()) {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGOperationscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp (243417 => 243418)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp        2019-03-24 00:47:08 UTC (rev 243417)
+++ trunk/Source/JavaScriptCore/dfg/DFGOperations.cpp   2019-03-24 01:32:06 UTC (rev 243418)
</span><span class="lines">@@ -547,7 +547,7 @@
</span><span class="cx">     JSValue op1 = JSValue::decode(encodedOp1);
</span><span class="cx">     uint32_t value = op1.toUInt32(exec);
</span><span class="cx">     RETURN_IF_EXCEPTION(scope, 0);
</span><del>-    return clz32(value);
</del><ins>+    return clz(value);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> double JIT_OPERATION operationArithFRound(ExecState* exec, EncodedJSValue encodedOp1)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSBigIntcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSBigInt.cpp (243417 => 243418)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSBigInt.cpp 2019-03-24 00:47:08 UTC (rev 243417)
+++ trunk/Source/JavaScriptCore/runtime/JSBigInt.cpp    2019-03-24 01:32:06 UTC (rev 243418)
</span><span class="lines">@@ -647,11 +647,7 @@
</span><span class="cx"> #else
</span><span class="cx">     static constexpr Digit halfDigitBase = 1ull << halfDigitBits;
</span><span class="cx">     // Adapted from Warren, Hacker's Delight, p. 152.
</span><del>-#if USE(JSVALUE64)
-    unsigned s = clz64(divisor);
-#else
-    unsigned s = clz32(divisor);
-#endif
</del><ins>+    unsigned s = clz(divisor);
</ins><span class="cx">     // If {s} is digitBits here, it causes an undefined behavior.
</span><span class="cx">     // But {s} is never digitBits since {divisor} is never zero here.
</span><span class="cx">     ASSERT(s != digitBits);
</span><span class="lines">@@ -984,7 +980,7 @@
</span><span class="cx">     // overflowing (they take a two digits wide input, and return a one digit
</span><span class="cx">     // result).
</span><span class="cx">     Digit lastDigit = divisor->digit(n - 1);
</span><del>-    unsigned shift = sizeof(lastDigit) == 8 ? clz64(lastDigit) : clz32(lastDigit);
</del><ins>+    unsigned shift = clz(lastDigit);
</ins><span class="cx"> 
</span><span class="cx">     if (shift > 0) {
</span><span class="cx">         divisor = absoluteLeftShiftAlwaysCopy(exec, divisor, shift, LeftShiftMode::SameSizeResult);
</span><span class="lines">@@ -1445,11 +1441,7 @@
</span><span class="cx"> // Divide bit length of the BigInt by bits representable per character.
</span><span class="cx"> uint64_t JSBigInt::calculateMaximumCharactersRequired(unsigned length, unsigned radix, Digit lastDigit, bool sign)
</span><span class="cx"> {
</span><del>-    unsigned leadingZeros;
-    if (sizeof(lastDigit) == 8)
-        leadingZeros = clz64(lastDigit);
-    else
-        leadingZeros = clz32(lastDigit);
</del><ins>+    unsigned leadingZeros = clz(lastDigit);
</ins><span class="cx"> 
</span><span class="cx">     size_t bitLength = length * digitBits - leadingZeros;
</span><span class="cx"> 
</span><span class="lines">@@ -1482,18 +1474,14 @@
</span><span class="cx"> 
</span><span class="cx">     const unsigned length = x->length();
</span><span class="cx">     const bool sign = x->sign();
</span><del>-    const unsigned bitsPerChar = ctz32(radix);
</del><ins>+    const unsigned bitsPerChar = ctz(radix);
</ins><span class="cx">     const unsigned charMask = radix - 1;
</span><span class="cx">     // Compute the length of the resulting string: divide the bit length of the
</span><span class="cx">     // BigInt by the number of bits representable per character (rounding up).
</span><span class="cx">     const Digit msd = x->digit(length - 1);
</span><span class="cx"> 
</span><del>-#if USE(JSVALUE64)
-    const unsigned msdLeadingZeros = clz64(msd);
-#else
-    const unsigned msdLeadingZeros = clz32(msd);
-#endif
-    
</del><ins>+    const unsigned msdLeadingZeros = clz(msd);
+
</ins><span class="cx">     const size_t bitLength = length * digitBits - msdLeadingZeros;
</span><span class="cx">     const size_t charsRequired = (bitLength + bitsPerChar - 1) / bitsPerChar + sign;
</span><span class="cx"> 
</span><span class="lines">@@ -1876,7 +1864,7 @@
</span><span class="cx"> 
</span><span class="cx">     int xLength = x->length();
</span><span class="cx">     Digit xMSD = x->digit(xLength - 1);
</span><del>-    int msdLeadingZeros = sizeof(xMSD) == 8  ? clz64(xMSD) : clz32(xMSD);
</del><ins>+    int msdLeadingZeros = clz(xMSD);
</ins><span class="cx"> 
</span><span class="cx">     int xBitLength = xLength * digitBits - msdLeadingZeros;
</span><span class="cx">     int yBitLength = exponent + 1;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeMathObjectcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/MathObject.cpp (243417 => 243418)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/MathObject.cpp       2019-03-24 00:47:08 UTC (rev 243417)
+++ trunk/Source/JavaScriptCore/runtime/MathObject.cpp  2019-03-24 01:32:06 UTC (rev 243418)
</span><span class="lines">@@ -169,7 +169,7 @@
</span><span class="cx">     auto scope = DECLARE_THROW_SCOPE(vm);
</span><span class="cx">     uint32_t value = exec->argument(0).toUInt32(exec);
</span><span class="cx">     RETURN_IF_EXCEPTION(scope, encodedJSValue());
</span><del>-    return JSValue::encode(JSValue(clz32(value)));
</del><ins>+    return JSValue::encode(JSValue(clz(value)));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL mathProtoFuncCos(ExecState* exec)
</span></span></pre></div>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (243417 => 243418)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog       2019-03-24 00:47:08 UTC (rev 243417)
+++ trunk/Source/WTF/ChangeLog  2019-03-24 01:32:06 UTC (rev 243418)
</span><span class="lines">@@ -1,3 +1,26 @@
</span><ins>+2019-03-23  Keith Miller  <keith_miller@apple.com>
+
+        Refactor clz/ctz and fix getLSBSet.
+        https://bugs.webkit.org/show_bug.cgi?id=196162
+
+        Reviewed by Saam Barati.
+
+        This patch makes clz32/64 and ctz32 generic so they work on any
+        numeric type. Since these methods work on any type we don't need
+        to have a separate implementation of getLSBSet, which also
+        happened to be getMSBSet. This patch also adds getMSBSet as there
+        may be users who want that in the future.
+
+        * wtf/MathExtras.h:
+        (WTF::clz):
+        (WTF::ctz):
+        (WTF::getLSBSet):
+        (WTF::getMSBSet):
+        (getLSBSet): Deleted.
+        (WTF::clz32): Deleted.
+        (WTF::clz64): Deleted.
+        (WTF::ctz32): Deleted.
+
</ins><span class="cx"> 2019-03-22  Keith Rollin  <krollin@apple.com>
</span><span class="cx"> 
</span><span class="cx">         Enable ThinLTO support in Production builds
</span></span></pre></div>
<a id="trunkSourceWTFwtfMathExtrash"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/MathExtras.h (243417 => 243418)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/MathExtras.h        2019-03-24 00:47:08 UTC (rev 243417)
+++ trunk/Source/WTF/wtf/MathExtras.h   2019-03-24 01:32:06 UTC (rev 243418)
</span><span class="lines">@@ -26,6 +26,7 @@
</span><span class="cx"> #pragma once
</span><span class="cx"> 
</span><span class="cx"> #include <algorithm>
</span><ins>+#include <climits>
</ins><span class="cx"> #include <cmath>
</span><span class="cx"> #include <float.h>
</span><span class="cx"> #include <limits>
</span><span class="lines">@@ -301,22 +302,6 @@
</span><span class="cx">     return !hasZeroOrOneBitsSet(value);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-// FIXME: Some Darwin projects shamelessly include WTF headers and don't build with C++14... See: rdar://problem/45395767
-// Since C++11 and before don't support constexpr statements we can't mark this function constexpr.
-#if !defined(WTF_CPP_STD_VER) || WTF_CPP_STD_VER >= 14
-template <typename T> constexpr unsigned getLSBSet(T value)
-{
-    typedef typename std::make_unsigned<T>::type UnsignedT;
-    unsigned result = 0;
-
-    UnsignedT unsignedValue = static_cast<UnsignedT>(value);
-    while (unsignedValue >>= 1)
-        ++result;
-
-    return result;
-}
-#endif
-
</del><span class="cx"> template<typename T> inline T divideRoundedUp(T a, T b)
</span><span class="cx"> {
</span><span class="cx">     return (a + b - 1) / b;
</span><span class="lines">@@ -630,37 +615,19 @@
</span><span class="cx">     shuffleVector(vector, vector.size(), randomFunc);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline unsigned clz32(uint32_t number)
</del><ins>+template<typename T>
+inline unsigned clz(T value)
</ins><span class="cx"> {
</span><del>-#if COMPILER(GCC_COMPATIBLE)
-    if (number)
-        return __builtin_clz(number);
-    return 32;
-#elif COMPILER(MSVC)
-    // Visual Studio 2008 or upper have __lzcnt, but we can't detect Intel AVX at compile time.
-    // So we use bit-scan-reverse operation to calculate clz.
-    unsigned long ret = 0;
-    if (_BitScanReverse(&ret, number))
-        return 31 - ret;
-    return 32;
-#else
-    unsigned zeroCount = 0;
-    for (int i = 31; i >= 0; i--) {
-        if (!(number >> i))
-            zeroCount++;
-        else
-            break;
-    }
-    return zeroCount;
-#endif
-}
</del><ins>+    constexpr unsigned bitSize = sizeof(T) * CHAR_BIT;
+    constexpr unsigned bitSize64 = sizeof(uint64_t) * CHAR_BIT;
</ins><span class="cx"> 
</span><del>-inline unsigned clz64(uint64_t number)
-{
</del><ins>+    using UT = typename std::make_unsigned<T>::type;
+    UT uValue = value;
+
</ins><span class="cx"> #if COMPILER(GCC_COMPATIBLE)
</span><del>-    if (number)
-        return __builtin_clzll(number);
-    return 64;
</del><ins>+    if (uValue)
+        return __builtin_clzll(uValue) - (bitSize64 - bitSize);
+    return bitSize;
</ins><span class="cx"> #elif COMPILER(MSVC) && !CPU(X86)
</span><span class="cx">     // Visual Studio 2008 or upper have __lzcnt, but we can't detect Intel AVX at compile time.
</span><span class="cx">     // So we use bit-scan-reverse operation to calculate clz.
</span><span class="lines">@@ -667,12 +634,12 @@
</span><span class="cx">     // _BitScanReverse64 is defined in X86_64 and ARM in MSVC supported environments.
</span><span class="cx">     unsigned long ret = 0;
</span><span class="cx">     if (_BitScanReverse64(&ret, number))
</span><del>-        return 63 - ret;
-    return 64;
</del><ins>+        return bitSize - ret;
+    return bitSize;
</ins><span class="cx"> #else
</span><span class="cx">     unsigned zeroCount = 0;
</span><del>-    for (int i = 63; i >= 0; i--) {
-        if (!(number >> i))
</del><ins>+    for (int i = bitSize64 - 1; i >= 0; i--) {
+        if (!(static_cast<uint64_t>(uValue) >> i))
</ins><span class="cx">             zeroCount++;
</span><span class="cx">         else
</span><span class="cx">             break;
</span><span class="lines">@@ -681,30 +648,51 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-inline unsigned ctz32(uint32_t number)
</del><ins>+template<typename T>
+inline unsigned ctz(T value)
</ins><span class="cx"> {
</span><ins>+    constexpr unsigned bitSize = sizeof(T) * CHAR_BIT;
+
+    using UT = typename std::make_unsigned<T>::type;
+    UT uValue = value;
+
</ins><span class="cx"> #if COMPILER(GCC_COMPATIBLE)
</span><del>-    if (number)
-        return __builtin_ctz(number);
-    return 32;
</del><ins>+    if (uValue)
+        return __builtin_ctzll(uValue);
+    return bitSize;
</ins><span class="cx"> #elif COMPILER(MSVC) && !CPU(X86)
</span><span class="cx">     unsigned long ret = 0;
</span><del>-    if (_BitScanForward(&ret, number))
</del><ins>+    if (_BitScanForward64(&ret, number))
</ins><span class="cx">         return ret;
</span><del>-    return 32;
</del><ins>+    return bitSize;
</ins><span class="cx"> #else
</span><span class="cx">     unsigned zeroCount = 0;
</span><del>-    for (unsigned i = 0; i < 32; i++) {
-        if (number & 1)
</del><ins>+    for (unsigned i = 0; i < bitSize; i++) {
+        if (uValue & 1)
</ins><span class="cx">             break;
</span><span class="cx"> 
</span><span class="cx">         zeroCount++;
</span><del>-        number >>= 1;
</del><ins>+        uValue >>= 1;
</ins><span class="cx">     }
</span><span class="cx">     return zeroCount;
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+template<typename T>
+inline unsigned getLSBSet(T t)
+{
+    ASSERT(t);
+    return ctz(t);
+}
+
+template<typename T>
+inline unsigned getMSBSet(T t)
+{
+    constexpr unsigned bitSize = sizeof(T) * CHAR_BIT;
+    ASSERT(t);
+    return bitSize - 1 - clz(t);
+}
+
</ins><span class="cx"> } // namespace WTF
</span><span class="cx"> 
</span><span class="cx"> using WTF::opaque;
</span><span class="lines">@@ -712,6 +700,7 @@
</span><span class="cx"> using WTF::preciseIndexMaskShift;
</span><span class="cx"> using WTF::preciseIndexMaskShiftForSize;
</span><span class="cx"> using WTF::shuffleVector;
</span><del>-using WTF::clz32;
-using WTF::clz64;
-using WTF::ctz32;
</del><ins>+using WTF::clz;
+using WTF::ctz;
+using WTF::getLSBSet;
+using WTF::getMSBSet;
</ins></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (243417 => 243418)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2019-03-24 00:47:08 UTC (rev 243417)
+++ trunk/Tools/ChangeLog       2019-03-24 01:32:06 UTC (rev 243418)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2019-03-23  Keith Miller  <keith_miller@apple.com>
+
+        Refactor clz/ctz and fix getLSBSet.
+        https://bugs.webkit.org/show_bug.cgi?id=196162
+
+        Reviewed by Saam Barati.
+
+        Add tests for clz, ctz, getLSBSet, and getMSBSet.
+
+        * TestWebKitAPI/Tests/WTF/MathExtras.cpp:
+        (TestWebKitAPI::TEST):
+
</ins><span class="cx"> 2019-03-23  Carlos Garcia Campos  <cgarcia@igalia.com>
</span><span class="cx"> 
</span><span class="cx">         [GTK][WPE] check-webkit-style doesn't complain about identifiers with underscores in files under glib, gtk or wpe dirs
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWTFMathExtrascpp"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WTF/MathExtras.cpp (243417 => 243418)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WTF/MathExtras.cpp       2019-03-24 00:47:08 UTC (rev 243417)
+++ trunk/Tools/TestWebKitAPI/Tests/WTF/MathExtras.cpp  2019-03-24 01:32:06 UTC (rev 243418)
</span><span class="lines">@@ -433,4 +433,78 @@
</span><span class="cx">     EXPECT_EQ(WTF::roundUpToPowerOfTwo((1U << 31) + 1), 0U);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+TEST(WTF, clz)
+{
+    EXPECT_EQ(WTF::clz<int32_t>(1), 31U);
+    EXPECT_EQ(WTF::clz<int32_t>(42), 26U);
+    EXPECT_EQ(WTF::clz<uint32_t>(static_cast<uint32_t>(-1)), 0U);
+    EXPECT_EQ(WTF::clz<uint32_t>(static_cast<uint32_t>(std::numeric_limits<int32_t>::min()) >> 1), 1U);
+    EXPECT_EQ(WTF::clz<uint32_t>(0), 32U);
+
+    EXPECT_EQ(WTF::clz<int8_t>(42), 2U);
+    EXPECT_EQ(WTF::clz<int8_t>(3), 6U);
+    EXPECT_EQ(WTF::clz<uint8_t>(static_cast<uint8_t>(-1)), 0U);
+    EXPECT_EQ(WTF::clz<uint8_t>(0), 8U);
+
+    EXPECT_EQ(WTF::clz<int64_t>(-1), 0U);
+    EXPECT_EQ(WTF::clz<int64_t>(1), 63U);
+    EXPECT_EQ(WTF::clz<int64_t>(3), 62U);
+    EXPECT_EQ(WTF::clz<uint64_t>(42), 58U);
+    EXPECT_EQ(WTF::clz<uint64_t>(0), 64U);
+}
+
+TEST(WTF, ctz)
+{
+    EXPECT_EQ(WTF::ctz<int32_t>(1), 0U);
+    EXPECT_EQ(WTF::ctz<int32_t>(42), 1U);
+    EXPECT_EQ(WTF::ctz<uint32_t>(static_cast<uint32_t>(-1)), 0U);
+    EXPECT_EQ(WTF::ctz<uint32_t>(static_cast<uint32_t>(std::numeric_limits<int32_t>::min()) >> 1), 30U);
+    EXPECT_EQ(WTF::ctz<uint32_t>(0), 32U);
+
+    EXPECT_EQ(WTF::ctz<int8_t>(42), 1U);
+    EXPECT_EQ(WTF::ctz<int8_t>(3), 0U);
+    EXPECT_EQ(WTF::ctz<uint8_t>(static_cast<uint8_t>(-1)), 0U);
+    EXPECT_EQ(WTF::ctz<uint8_t>(0), 8U);
+
+    EXPECT_EQ(WTF::ctz<int64_t>(static_cast<uint32_t>(-1)), 0U);
+    EXPECT_EQ(WTF::ctz<int64_t>(1), 0U);
+    EXPECT_EQ(WTF::ctz<int64_t>(3), 0U);
+    EXPECT_EQ(WTF::ctz<uint64_t>(42), 1U);
+    EXPECT_EQ(WTF::ctz<uint64_t>(0), 64U);
+}
+
+TEST(WTF, getLSBSet)
+{
+    EXPECT_EQ(WTF::getLSBSet<int32_t>(1), 0U);
+    EXPECT_EQ(WTF::getLSBSet<int32_t>(42), 1U);
+    EXPECT_EQ(WTF::getLSBSet<uint32_t>(static_cast<uint32_t>(-1)), 0U);
+    EXPECT_EQ(WTF::getLSBSet<uint32_t>(static_cast<uint32_t>(std::numeric_limits<int32_t>::min()) >> 1), 30U);
+
+    EXPECT_EQ(WTF::getLSBSet<int8_t>(42), 1U);
+    EXPECT_EQ(WTF::getLSBSet<int8_t>(3), 0U);
+    EXPECT_EQ(WTF::getLSBSet<uint8_t>(static_cast<uint8_t>(-1)), 0U);
+
+    EXPECT_EQ(WTF::getLSBSet<int64_t>(-1), 0U);
+    EXPECT_EQ(WTF::getLSBSet<int64_t>(1), 0U);
+    EXPECT_EQ(WTF::getLSBSet<int64_t>(3), 0U);
+    EXPECT_EQ(WTF::getLSBSet<uint64_t>(42), 1U);
+}
+
+TEST(WTF, getMSBSet)
+{
+    EXPECT_EQ(WTF::getMSBSet<int32_t>(1), 0U);
+    EXPECT_EQ(WTF::getMSBSet<int32_t>(42), 5U);
+    EXPECT_EQ(WTF::getMSBSet<uint32_t>(static_cast<uint32_t>(-1)), 31U);
+    EXPECT_EQ(WTF::getMSBSet<uint32_t>(static_cast<uint32_t>(std::numeric_limits<int32_t>::min()) >> 1), 30U);
+
+    EXPECT_EQ(WTF::getMSBSet<int8_t>(42), 5U);
+    EXPECT_EQ(WTF::getMSBSet<int8_t>(3), 1U);
+    EXPECT_EQ(WTF::getMSBSet<uint8_t>(static_cast<uint8_t>(-1)), 7U);
+
+    EXPECT_EQ(WTF::getMSBSet<int64_t>(-1), 63U);
+    EXPECT_EQ(WTF::getMSBSet<int64_t>(1), 0U);
+    EXPECT_EQ(WTF::getMSBSet<int64_t>(3), 1U);
+    EXPECT_EQ(WTF::getMSBSet<uint64_t>(42), 5U);
+}
+
</ins><span class="cx"> } // namespace TestWebKitAPI
</span></span></pre>
</div>
</div>

</body>
</html>