<!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>[192855] trunk/Source</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/192855">192855</a></dd>
<dt>Author</dt> <dd>ggaren@apple.com</dd>
<dt>Date</dt> <dd>2015-11-30 19:39:59 -0800 (Mon, 30 Nov 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Use a better RNG for Math.random()
https://bugs.webkit.org/show_bug.cgi?id=151641

Reviewed by Anders Carlsson.

Source/JavaScriptCore:

Updated for interface change.

* runtime/JSGlobalObject.cpp:
(JSC::JSGlobalObject::setInputCursor):

Source/WTF:

Use 64 bits in the random number generator instead of 32 bit. (In
the end, JavaScript, which uses doubles, will only see 52 bits.) This
prevents programs that mulitply a random number by a large constant from
seeing non-random &quot;banding&quot; caused by zeroes in the low 20 bits.

I also took the opportunity to upgrade from GameRandom to Xorshift+,
since Xorshift+ passes more benchmarks for randomness, and is not any
slower or more complicated.

Now let us all remember the fateful words of Steve Weibe, who would be
King of Kong: &quot;The randomness went the opposite way that it usually goes.&quot;

* wtf/WeakRandom.h:
(WTF::WeakRandom::WeakRandom):
(WTF::WeakRandom::setSeed): Use standard naming.

(WTF::WeakRandom::seed): This function is safe now. &quot;Unsafe&quot; in function
names makes me itch.

(WTF::WeakRandom::get):
(WTF::WeakRandom::getUint32): Update to 64bit.

(WTF::WeakRandom::advance): The Xorshift+ algorithm.

(WTF::WeakRandom::initializeSeed): Deleted.
(WTF::WeakRandom::seedUnsafe): Deleted.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSGlobalObjectcpp">trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp</a></li>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfWeakRandomh">trunk/Source/WTF/wtf/WeakRandom.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (192854 => 192855)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-12-01 02:53:25 UTC (rev 192854)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-12-01 03:39:59 UTC (rev 192855)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2015-11-30  Geoffrey Garen  &lt;ggaren@apple.com&gt;
+
+        Use a better RNG for Math.random()
+        https://bugs.webkit.org/show_bug.cgi?id=151641
+
+        Reviewed by Anders Carlsson.
+
+        Updated for interface change.
+
+        * runtime/JSGlobalObject.cpp:
+        (JSC::JSGlobalObject::setInputCursor):
+
</ins><span class="cx"> 2015-11-30  Benjamin Poulain  &lt;bpoulain@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [JSC] Speed up Air Liveness Analysis on Tmps
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSGlobalObjectcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp (192854 => 192855)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp        2015-12-01 02:53:25 UTC (rev 192854)
+++ trunk/Source/JavaScriptCore/runtime/JSGlobalObject.cpp        2015-12-01 03:39:59 UTC (rev 192855)
</span><span class="lines">@@ -1014,10 +1014,10 @@
</span><span class="cx">     // Save or set the random seed. This performed here rather than the constructor
</span><span class="cx">     // to avoid threading the input cursor through all the abstraction layers.
</span><span class="cx">     if (cursor.isCapturing())
</span><del>-        cursor.appendInput&lt;SetRandomSeed&gt;(m_weakRandom.seedUnsafe());
</del><ins>+        cursor.appendInput&lt;SetRandomSeed&gt;(m_weakRandom.seed());
</ins><span class="cx">     else if (cursor.isReplaying()) {
</span><span class="cx">         if (SetRandomSeed* input = cursor.fetchInput&lt;SetRandomSeed&gt;())
</span><del>-            m_weakRandom.initializeSeed(static_cast&lt;unsigned&gt;(input-&gt;randomSeed()));
</del><ins>+            m_weakRandom.setSeed(static_cast&lt;unsigned&gt;(input-&gt;randomSeed()));
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (192854 => 192855)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2015-12-01 02:53:25 UTC (rev 192854)
+++ trunk/Source/WTF/ChangeLog        2015-12-01 03:39:59 UTC (rev 192855)
</span><span class="lines">@@ -1,3 +1,37 @@
</span><ins>+2015-11-30  Geoffrey Garen  &lt;ggaren@apple.com&gt;
+
+        Use a better RNG for Math.random()
+        https://bugs.webkit.org/show_bug.cgi?id=151641
+
+        Reviewed by Anders Carlsson.
+
+        Use 64 bits in the random number generator instead of 32 bit. (In
+        the end, JavaScript, which uses doubles, will only see 52 bits.) This
+        prevents programs that mulitply a random number by a large constant from
+        seeing non-random &quot;banding&quot; caused by zeroes in the low 20 bits.
+
+        I also took the opportunity to upgrade from GameRandom to Xorshift+,
+        since Xorshift+ passes more benchmarks for randomness, and is not any
+        slower or more complicated.
+
+        Now let us all remember the fateful words of Steve Weibe, who would be
+        King of Kong: &quot;The randomness went the opposite way that it usually goes.&quot;
+
+        * wtf/WeakRandom.h:
+        (WTF::WeakRandom::WeakRandom):
+        (WTF::WeakRandom::setSeed): Use standard naming.
+
+        (WTF::WeakRandom::seed): This function is safe now. &quot;Unsafe&quot; in function
+        names makes me itch.
+
+        (WTF::WeakRandom::get):
+        (WTF::WeakRandom::getUint32): Update to 64bit.
+
+        (WTF::WeakRandom::advance): The Xorshift+ algorithm.
+
+        (WTF::WeakRandom::initializeSeed): Deleted.
+        (WTF::WeakRandom::seedUnsafe): Deleted.
+
</ins><span class="cx"> 2015-11-30  Benjamin Poulain  &lt;bpoulain@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [JSC] Speed up Air Liveness Analysis on Tmps
</span></span></pre></div>
<a id="trunkSourceWTFwtfWeakRandomh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/WeakRandom.h (192854 => 192855)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/WeakRandom.h        2015-12-01 02:53:25 UTC (rev 192854)
+++ trunk/Source/WTF/wtf/WeakRandom.h        2015-12-01 03:39:59 UTC (rev 192855)
</span><span class="lines">@@ -22,76 +22,57 @@
</span><span class="cx">  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
</span><span class="cx">  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
</span><span class="cx">  *
</span><ins>+ * Vigna, Sebastiano (2014). &quot;Further scramblings of Marsaglia's xorshift
+ * generators&quot;. arXiv:1404.0390 (http://arxiv.org/abs/1404.0390)
</ins><span class="cx">  *
</span><del>- * Copyright (c) 2009 Ian C. Bullard
- * 
- * Permission is hereby granted, free of charge, to any person
- * obtaining a copy of this software and associated documentation
- * files (the &quot;Software&quot;), to deal in the Software without
- * restriction, including without limitation the rights to use,
- * copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following
- * conditions:
- * 
- * The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- * 
- * THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,
- * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
- * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
- * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
</del><ins>+ * See also https://en.wikipedia.org/wiki/Xorshift.
</ins><span class="cx">  */
</span><span class="cx"> 
</span><span class="cx"> #ifndef WeakRandom_h
</span><span class="cx"> #define WeakRandom_h
</span><span class="cx"> 
</span><span class="cx"> #include &lt;limits.h&gt;
</span><del>-#include &lt;wtf/RandomNumber.h&gt;
</del><ins>+#include &lt;wtf/CryptographicallyRandomNumber.h&gt;
</ins><span class="cx"> #include &lt;wtf/StdLibExtras.h&gt;
</span><span class="cx"> 
</span><span class="cx"> namespace WTF {
</span><span class="cx"> 
</span><span class="cx"> class WeakRandom {
</span><span class="cx"> public:
</span><del>-    WeakRandom()
</del><ins>+    WeakRandom(unsigned seed = cryptographicallyRandomNumber())
</ins><span class="cx">     {
</span><del>-        initializeSeed(randomNumber() * (std::numeric_limits&lt;unsigned&gt;::max() + 1.0));
</del><ins>+        setSeed(seed);
</ins><span class="cx">     }
</span><del>-    
-    WeakRandom(unsigned seed)
</del><ins>+
+    void setSeed(unsigned seed)
</ins><span class="cx">     {
</span><del>-        initializeSeed(seed);
-    }
-    
-    void initializeSeed(unsigned seed)
-    {
-        m_low = seed ^ 0x49616E42;
</del><ins>+        m_seed = seed;
+
+        // A zero seed would cause an infinite series of zeroes.
+        if (!seed)
+            seed = 1;
+
+        m_low = seed;
</ins><span class="cx">         m_high = seed;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    // Returns the seed provided that you've never called get() or getUint32().
-    unsigned seedUnsafe() const { return m_high; }
</del><ins>+    unsigned seed() const { return m_seed; }
</ins><span class="cx"> 
</span><span class="cx">     double get()
</span><span class="cx">     {
</span><del>-        return advance() / (UINT_MAX + 1.0);
</del><ins>+        return advance() / (std::numeric_limits&lt;uint64_t&gt;::max() + 1.0);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     unsigned getUint32()
</span><span class="cx">     {
</span><del>-        return advance();
</del><ins>+        return static_cast&lt;unsigned&gt;(advance());
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     unsigned getUint32(unsigned limit)
</span><span class="cx">     {
</span><span class="cx">         if (limit &lt;= 1)
</span><span class="cx">             return 0;
</span><del>-        uint64_t cutoff = (static_cast&lt;uint64_t&gt;(UINT_MAX) + 1) / limit * limit;
</del><ins>+        uint64_t cutoff = (static_cast&lt;uint64_t&gt;(std::numeric_limits&lt;unsigned&gt;::max()) + 1) / limit * limit;
</ins><span class="cx">         for (;;) {
</span><span class="cx">             uint64_t value = getUint32();
</span><span class="cx">             if (value &gt;= cutoff)
</span><span class="lines">@@ -101,16 +82,21 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx"> private:
</span><del>-    unsigned advance()
</del><ins>+    uint64_t advance()
</ins><span class="cx">     {
</span><del>-        m_high = (m_high &lt;&lt; 16) + (m_high &gt;&gt; 16);
-        m_high += m_low;
-        m_low += m_high;
-        return m_high;
</del><ins>+        uint64_t x = m_low;
+        uint64_t y = m_high;
+        m_low = y;
+        x ^= x &lt;&lt; 23;
+        x ^= x &gt;&gt; 17;
+        x ^= y ^ (y &gt;&gt; 26);
+        m_high = x;
+        return x + y;
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    unsigned m_low;
-    unsigned m_high;
</del><ins>+    unsigned m_seed;
+    uint64_t m_low;
+    uint64_t m_high;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WTF
</span></span></pre>
</div>
</div>

</body>
</html>