<!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>[199866] trunk/Source/JavaScriptCore</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/199866">199866</a></dd>
<dt>Author</dt> <dd>commit-queue@webkit.org</dd>
<dt>Date</dt> <dd>2016-04-21 21:46:53 -0700 (Thu, 21 Apr 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>[JSC] Commute FDiv-by-constant into FMul-by-reciprocal when it is safe
https://bugs.webkit.org/show_bug.cgi?id=156871

Patch by Benjamin Poulain &lt;bpoulain@webkit.org&gt; on 2016-04-21
Reviewed by Filip Pizlo.

FMul is significantly faster than FDiv.
For example, on Haswell, FMul has a latency of 5, a throughput of 1
while FDiv has latency 10-24, throughput 8-18.

Fortunately for us, Sunspider and Kraken have plenty of division
by a simple power of 2 constant. Those are just exponent operations
and can be easily reversed to use FMul instead of FDiv.

LLVM does something similar in InstCombine.

* dfg/DFGStrengthReductionPhase.cpp:
(JSC::DFG::StrengthReductionPhase::handleNode):
* jit/JITDivGenerator.cpp:
(JSC::JITDivGenerator::loadOperand):
(JSC::JITDivGenerator::generateFastPath):
* jit/SnippetOperand.h:
(JSC::SnippetOperand::asConstNumber):
* runtime/MathCommon.h:
(JSC::safeReciprocalForDivByConst):
* tests/stress/floating-point-div-to-mul.js: Added.
(opaqueDivBy2):
(opaqueDivBy3):
(opaqueDivBy4):
(opaqueDivBySafeMaxMinusOne):
(opaqueDivBySafeMax):
(opaqueDivBySafeMaxPlusOne):
(opaqueDivBySafeMin):
(opaqueDivBySafeMinMinusOne):
(i.catch):
(i.result.opaqueDivBySafeMin.valueOf):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoredfgDFGStrengthReductionPhasecpp">trunk/Source/JavaScriptCore/dfg/DFGStrengthReductionPhase.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorejitJITDivGeneratorcpp">trunk/Source/JavaScriptCore/jit/JITDivGenerator.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCorejitSnippetOperandh">trunk/Source/JavaScriptCore/jit/SnippetOperand.h</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeMathCommonh">trunk/Source/JavaScriptCore/runtime/MathCommon.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoretestsstressfloatingpointdivtomuljs">trunk/Source/JavaScriptCore/tests/stress/floating-point-div-to-mul.js</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (199865 => 199866)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/JavaScriptCore/ChangeLog        2016-04-22 04:46:53 UTC (rev 199866)
</span><span class="lines">@@ -1,3 +1,41 @@
</span><ins>+2016-04-21  Benjamin Poulain  &lt;bpoulain@webkit.org&gt;
+
+        [JSC] Commute FDiv-by-constant into FMul-by-reciprocal when it is safe
+        https://bugs.webkit.org/show_bug.cgi?id=156871
+
+        Reviewed by Filip Pizlo.
+
+        FMul is significantly faster than FDiv.
+        For example, on Haswell, FMul has a latency of 5, a throughput of 1
+        while FDiv has latency 10-24, throughput 8-18.
+
+        Fortunately for us, Sunspider and Kraken have plenty of division
+        by a simple power of 2 constant. Those are just exponent operations
+        and can be easily reversed to use FMul instead of FDiv.
+
+        LLVM does something similar in InstCombine.
+
+        * dfg/DFGStrengthReductionPhase.cpp:
+        (JSC::DFG::StrengthReductionPhase::handleNode):
+        * jit/JITDivGenerator.cpp:
+        (JSC::JITDivGenerator::loadOperand):
+        (JSC::JITDivGenerator::generateFastPath):
+        * jit/SnippetOperand.h:
+        (JSC::SnippetOperand::asConstNumber):
+        * runtime/MathCommon.h:
+        (JSC::safeReciprocalForDivByConst):
+        * tests/stress/floating-point-div-to-mul.js: Added.
+        (opaqueDivBy2):
+        (opaqueDivBy3):
+        (opaqueDivBy4):
+        (opaqueDivBySafeMaxMinusOne):
+        (opaqueDivBySafeMax):
+        (opaqueDivBySafeMaxPlusOne):
+        (opaqueDivBySafeMin):
+        (opaqueDivBySafeMinMinusOne):
+        (i.catch):
+        (i.result.opaqueDivBySafeMin.valueOf):
+
</ins><span class="cx"> 2016-04-21  Benjamin Poulain  &lt;benjamin@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         [JSC] Improve the absThunkGenerator() for 64bit
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoredfgDFGStrengthReductionPhasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/dfg/DFGStrengthReductionPhase.cpp (199865 => 199866)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/dfg/DFGStrengthReductionPhase.cpp        2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/JavaScriptCore/dfg/DFGStrengthReductionPhase.cpp        2016-04-22 04:46:53 UTC (rev 199866)
</span><span class="lines">@@ -36,6 +36,7 @@
</span><span class="cx"> #include &quot;DFGPredictionPropagationPhase.h&quot;
</span><span class="cx"> #include &quot;DFGVariableAccessDataDump.h&quot;
</span><span class="cx"> #include &quot;JSCInlines.h&quot;
</span><ins>+#include &quot;MathCommon.h&quot;
</ins><span class="cx"> #include &quot;RegExpConstructor.h&quot;
</span><span class="cx"> #include &quot;StringPrototype.h&quot;
</span><span class="cx"> #include &lt;cstdlib&gt;
</span><span class="lines">@@ -192,6 +193,25 @@
</span><span class="cx">             }
</span><span class="cx">             break;
</span><span class="cx"> 
</span><ins>+        case ArithDiv:
+            // Transform
+            //    ArithDiv(x, constant)
+            // Into
+            //    ArithMul(x, 1 / constant)
+            // if the operation has the same result.
+            if (m_node-&gt;isBinaryUseKind(DoubleRepUse)
+                &amp;&amp; m_node-&gt;child2()-&gt;isNumberConstant()) {
+
+                if (Optional&lt;double&gt; reciprocal = safeReciprocalForDivByConst(m_node-&gt;child2()-&gt;asNumber())) {
+                    Node* reciprocalNode = m_insertionSet.insertConstant(m_nodeIndex, m_node-&gt;origin, jsDoubleNumber(*reciprocal), DoubleConstant);
+                    m_node-&gt;setOp(ArithMul);
+                    m_node-&gt;child2() = Edge(reciprocalNode, DoubleRepUse);
+                    m_changed = true;
+                    break;
+                }
+            }
+            break;
+
</ins><span class="cx">         case ValueRep:
</span><span class="cx">         case Int52Rep:
</span><span class="cx">         case DoubleRep: {
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitJITDivGeneratorcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/JITDivGenerator.cpp (199865 => 199866)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/JITDivGenerator.cpp        2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/JavaScriptCore/jit/JITDivGenerator.cpp        2016-04-22 04:46:53 UTC (rev 199866)
</span><span class="lines">@@ -29,12 +29,17 @@
</span><span class="cx"> #if ENABLE(JIT)
</span><span class="cx"> 
</span><span class="cx"> #include &quot;JSCJSValueInlines.h&quot;
</span><ins>+#include &quot;MathCommon.h&quot;
</ins><span class="cx"> 
</span><span class="cx"> namespace JSC {
</span><span class="cx"> 
</span><span class="cx"> void JITDivGenerator::loadOperand(CCallHelpers&amp; jit, SnippetOperand&amp; opr, JSValueRegs oprRegs, FPRReg destFPR)
</span><span class="cx"> {
</span><span class="cx">     if (opr.isConstInt32()) {
</span><ins>+        // FIXME: this does not looks right.
+        //     -On x86_64, CVTSI2SD has partial register stall on its FPR.
+        //      A move or load might be a tiny bit larger but safer.
+        //     -On ARM64 we also have FMOV that can load small immediates.
</ins><span class="cx">         jit.move(CCallHelpers::Imm32(opr.asConstInt32()), m_scratchGPR);
</span><span class="cx">         jit.convertInt32ToDouble(m_scratchGPR, destFPR);
</span><span class="cx"> #if USE(JSVALUE64)
</span><span class="lines">@@ -74,10 +79,27 @@
</span><span class="cx">     ASSERT(!m_leftOperand.isConstInt32() || !m_rightOperand.isConstInt32());
</span><span class="cx">     m_didEmitFastPath = true;
</span><span class="cx">     loadOperand(jit, m_leftOperand, m_left, m_leftFPR);
</span><del>-    loadOperand(jit, m_rightOperand, m_right, m_rightFPR);
</del><span class="cx"> 
</span><del>-    jit.divDouble(m_rightFPR, m_leftFPR);
</del><ins>+#if USE(JSVALUE64)
+    Optional&lt;double&gt; safeReciprocal;
+    if (m_rightOperand.isConst()) {
+        double constant = m_rightOperand.asConstNumber();
+        safeReciprocal = safeReciprocalForDivByConst(constant);
+    }
</ins><span class="cx"> 
</span><ins>+    if (safeReciprocal) {
+        jit.move(CCallHelpers::Imm64(bitwise_cast&lt;int64_t&gt;(*safeReciprocal)), m_scratchGPR);
+        jit.move64ToDouble(m_scratchGPR, m_rightFPR);
+
+        jit.mulDouble(m_rightFPR, m_leftFPR);
+    } else
+#endif
+    {
+        loadOperand(jit, m_rightOperand, m_right, m_rightFPR);
+
+        jit.divDouble(m_rightFPR, m_leftFPR);
+    }
+
</ins><span class="cx">     // Is the result actually an integer? The DFG JIT would really like to know. If it's
</span><span class="cx">     // not an integer, we increment a count. If this together with the slow case counter
</span><span class="cx">     // are below threshold then the DFG JIT will compile this division with a speculation
</span></span></pre></div>
<a id="trunkSourceJavaScriptCorejitSnippetOperandh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/jit/SnippetOperand.h (199865 => 199866)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/jit/SnippetOperand.h        2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/JavaScriptCore/jit/SnippetOperand.h        2016-04-22 04:46:53 UTC (rev 199866)
</span><span class="lines">@@ -70,6 +70,14 @@
</span><span class="cx">         return m_val.doubleVal;
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    double asConstNumber() const
+    {
+        if (isConstInt32())
+            return asConstInt32();
+        ASSERT(isConstDouble());
+        return asConstDouble();
+    }
+
</ins><span class="cx">     void setConstInt32(int32_t value)
</span><span class="cx">     {
</span><span class="cx">         m_type = ConstInt32;
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeMathCommonh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/MathCommon.h (199865 => 199866)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/MathCommon.h        2016-04-22 04:29:02 UTC (rev 199865)
+++ trunk/Source/JavaScriptCore/runtime/MathCommon.h        2016-04-22 04:46:53 UTC (rev 199866)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2015 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2015-2016 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -27,6 +27,7 @@
</span><span class="cx"> #define MathCommon_h
</span><span class="cx"> 
</span><span class="cx"> #include &quot;JITOperations.h&quot;
</span><ins>+#include &lt;wtf/Optional.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> #ifndef JIT_OPERATION
</span><span class="cx"> #define JIT_OPERATION
</span><span class="lines">@@ -54,6 +55,34 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+inline Optional&lt;double&gt; safeReciprocalForDivByConst(double constant)
+{
+    // No &quot;weird&quot; numbers (NaN, Denormal, etc).
+    if (!constant || !isnormal(constant))
+        return Nullopt;
+
+    int exponent;
+    if (frexp(constant, &amp;exponent) != 0.5)
+        return Nullopt;
+
+    // Note that frexp() returns the value divided by two
+    // so we to offset this exponent by one.
+    exponent -= 1;
+
+    // A double exponent is between -1022 and 1023.
+    // Nothing we can do to invert 1023.
+    if (exponent == 1023)
+        return Nullopt;
+
+    double reciprocal = ldexp(1, -exponent);
+    ASSERT(isnormal(reciprocal));
+    ASSERT(1. / constant == reciprocal);
+    ASSERT(constant == 1. / reciprocal);
+    ASSERT(1. == constant * reciprocal);
+
+    return reciprocal;
+}
+
</ins><span class="cx"> extern &quot;C&quot; {
</span><span class="cx"> double JIT_OPERATION jsRound(double value) REFERENCED_FROM_ASM WTF_INTERNAL;
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoretestsstressfloatingpointdivtomuljs"></a>
<div class="addfile"><h4>Added: trunk/Source/JavaScriptCore/tests/stress/floating-point-div-to-mul.js (0 => 199866)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/tests/stress/floating-point-div-to-mul.js                                (rev 0)
+++ trunk/Source/JavaScriptCore/tests/stress/floating-point-div-to-mul.js        2016-04-22 04:46:53 UTC (rev 199866)
</span><span class="lines">@@ -0,0 +1,216 @@
</span><ins>+function opaqueDivBy2(a)
+{
+    return a / 2;
+}
+noInline(opaqueDivBy2);
+
+function opaqueDivBy3(a)
+{
+    return a / 3;
+}
+noInline(opaqueDivBy3);
+
+function opaqueDivBy4(a)
+{
+    return a / 4;
+}
+noInline(opaqueDivBy4);
+
+function opaqueDivBySafeMaxMinusOne(a)
+{
+    // 2^1022
+    return a / 44942328371557897693232629769725618340449424473557664318357520289433168951375240783177119330601884005280028469967848339414697442203604155623211857659868531094441973356216371319075554900311523529863270738021251442209537670585615720368478277635206809290837627671146574559986811484619929076208839082406056034304;
+}
+noInline(opaqueDivBySafeMaxMinusOne);
+
+function opaqueDivBySafeMax(a)
+{
+    // 2^1023
+    return a / 89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608;
+}
+noInline(opaqueDivBySafeMax);
+
+function opaqueDivBySafeMaxPlusOne(a)
+{
+    // 2^1024
+    return a / 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216;
+}
+noInline(opaqueDivBySafeMaxPlusOne);
+
+function opaqueDivBySafeMin(a)
+{
+    // 2^-1022
+    return a / (1 / 44942328371557897693232629769725618340449424473557664318357520289433168951375240783177119330601884005280028469967848339414697442203604155623211857659868531094441973356216371319075554900311523529863270738021251442209537670585615720368478277635206809290837627671146574559986811484619929076208839082406056034304);
+}
+noInline(opaqueDivBySafeMin);
+
+function opaqueDivBySafeMinMinusOne(a)
+{
+    // 2^-1023
+    return a / (1 / 89884656743115795386465259539451236680898848947115328636715040578866337902750481566354238661203768010560056939935696678829394884407208311246423715319737062188883946712432742638151109800623047059726541476042502884419075341171231440736956555270413618581675255342293149119973622969239858152417678164812112068608);
+}
+noInline(opaqueDivBySafeMinMinusOne);
+
+
+for (let i = 0; i &lt; 1e4; ++i) {
+    let result = opaqueDivBy2(Math.PI);
+    if (result !== 1.5707963267948966)
+        throw &quot;Failed opaqueDivBy2(Math.PI). Result = &quot; + result;
+    result = opaqueDivBy2(NaN);
+    if (result === result)
+        throw &quot;Failed opaqueDivBy2(NaN). Result = &quot; + result;
+    result = opaqueDivBy2(Infinity);
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBy2(Infinity). Result = &quot; + result;
+    result = opaqueDivBy2(-Infinity);
+    if (result !== -Infinity)
+        throw &quot;Failed opaqueDivBy2(-Infinity). Result = &quot; + result;
+    result = opaqueDivBy2(Math.E);
+    if (result !== 1.3591409142295225)
+        throw &quot;Failed opaqueDivBy2(Math.E). Result = &quot; + result;
+
+    result = opaqueDivBy3(Math.PI);
+    if (result !== 1.0471975511965976)
+        throw &quot;Failed opaqueDivBy3(Math.PI). Result = &quot; + result;
+    result = opaqueDivBy3(NaN);
+    if (result === result)
+        throw &quot;Failed opaqueDivBy3(NaN). Result = &quot; + result;
+    result = opaqueDivBy3(Infinity);
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBy3(Infinity). Result = &quot; + result;
+    result = opaqueDivBy3(-Infinity);
+    if (result !== -Infinity)
+        throw &quot;Failed opaqueDivBy3(-Infinity). Result = &quot; + result;
+    result = opaqueDivBy3(Math.E);
+    if (result !== 0.9060939428196817)
+        throw &quot;Failed opaqueDivBy3(Math.E). Result = &quot; + result;
+
+    result = opaqueDivBy4(Math.PI);
+    if (result !== 0.7853981633974483)
+        throw &quot;Failed opaqueDivBy4(Math.PI). Result = &quot; + result;
+    result = opaqueDivBy4(NaN);
+    if (result === result)
+        throw &quot;Failed opaqueDivBy4(NaN). Result = &quot; + result;
+    result = opaqueDivBy4(Infinity);
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBy4(Infinity). Result = &quot; + result;
+    result = opaqueDivBy4(-Infinity);
+    if (result !== -Infinity)
+        throw &quot;Failed opaqueDivBy4(-Infinity). Result = &quot; + result;
+    result = opaqueDivBy4(Math.E);
+    if (result !== 0.6795704571147613)
+        throw &quot;Failed opaqueDivBy4(Math.E). Result = &quot; + result;
+
+    result = opaqueDivBySafeMaxMinusOne(Math.PI);
+    if (result !== 6.990275687580919e-308)
+        throw &quot;Failed opaqueDivBySafeMaxMinusOne(Math.PI). Result = &quot; + result;
+    result = opaqueDivBySafeMaxMinusOne(NaN);
+    if (result === result)
+        throw &quot;Failed opaqueDivBySafeMaxMinusOne(NaN). Result = &quot; + result;
+    result = opaqueDivBySafeMaxMinusOne(Infinity);
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBySafeMaxMinusOne(Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMaxMinusOne(-Infinity);
+    if (result !== -Infinity)
+        throw &quot;Failed opaqueDivBySafeMaxMinusOne(-Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMaxMinusOne(Math.E);
+    if (result !== 6.048377836559378e-308)
+        throw &quot;Failed opaqueDivBySafeMaxMinusOne(Math.E). Result = &quot; + result;
+
+    result = opaqueDivBySafeMax(Math.PI);
+    if (result !== 3.4951378437904593e-308)
+        throw &quot;Failed opaqueDivBySafeMax(Math.PI). Result = &quot; + result;
+    result = opaqueDivBySafeMax(NaN);
+    if (result === result)
+        throw &quot;Failed opaqueDivBySafeMax(NaN). Result = &quot; + result;
+    result = opaqueDivBySafeMax(Infinity);
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBySafeMax(Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMax(-Infinity);
+    if (result !== -Infinity)
+        throw &quot;Failed opaqueDivBySafeMax(-Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMax(Math.E);
+    if (result !== 3.024188918279689e-308)
+        throw &quot;Failed opaqueDivBySafeMax(Math.E). Result = &quot; + result;
+
+    result = opaqueDivBySafeMaxPlusOne(Math.PI);
+    if (result !== 0)
+        throw &quot;Failed opaqueDivBySafeMaxPlusOne(Math.PI). Result = &quot; + result;
+    result = opaqueDivBySafeMaxPlusOne(NaN);
+    if (result === result)
+        throw &quot;Failed opaqueDivBySafeMaxPlusOne(NaN). Result = &quot; + result;
+    result = opaqueDivBySafeMaxPlusOne(Infinity);
+    if (result === result)
+        throw &quot;Failed opaqueDivBySafeMaxPlusOne(Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMaxPlusOne(-Infinity);
+    if (result === result)
+        throw &quot;Failed opaqueDivBySafeMaxPlusOne(-Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMaxPlusOne(Math.E);
+    if (result !== 0)
+        throw &quot;Failed opaqueDivBySafeMaxPlusOne(Math.E). Result = &quot; + result;
+
+    result = opaqueDivBySafeMin(Math.PI);
+    if (result !== 1.4119048864730642e+308)
+        throw &quot;Failed opaqueDivBySafeMin(Math.PI). Result = &quot; + result;
+    result = opaqueDivBySafeMin(NaN);
+    if (result === result)
+        throw &quot;Failed opaqueDivBySafeMin(NaN). Result = &quot; + result;
+    result = opaqueDivBySafeMin(Infinity);
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBySafeMin(Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMin(-Infinity);
+    if (result !== -Infinity)
+        throw &quot;Failed opaqueDivBySafeMin(-Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMin(Math.E);
+    if (result !== 1.2216591454104522e+308)
+        throw &quot;Failed opaqueDivBySafeMin(Math.E). Result = &quot; + result;
+
+    result = opaqueDivBySafeMinMinusOne(Math.PI);
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBySafeMinMinusOne(Math.PI). Result = &quot; + result;
+    result = opaqueDivBySafeMinMinusOne(NaN);
+    if (result === result)
+        throw &quot;Failed opaqueDivBySafeMinMinusOne(NaN). Result = &quot; + result;
+    result = opaqueDivBySafeMinMinusOne(Infinity);
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBySafeMinMinusOne(Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMinMinusOne(-Infinity);
+    if (result !== -Infinity)
+        throw &quot;Failed opaqueDivBySafeMinMinusOne(-Infinity). Result = &quot; + result;
+    result = opaqueDivBySafeMinMinusOne(Math.E);
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBySafeMinMinusOne(Math.E). Result = &quot; + result;
+}
+
+
+// Check that we don't do anything crazy with crazy types.
+for (let i = 0; i &lt; 1e3; ++i) {
+    let result = opaqueDivBy2();
+    if (result === result)
+        throw &quot;Failed opaqueDivBy2()&quot;;
+    result = opaqueDivBy4(null);
+    if (result !== 0)
+        throw &quot;Failed opaqueDivBy4(null)&quot;;
+    result = opaqueDivBySafeMaxMinusOne(&quot;WebKit!&quot;);
+    if (result === result)
+        throw &quot;Failed opaqueDivBy4(null)&quot;;
+    result = opaqueDivBySafeMin(&quot;&quot;);
+    if (result !== 0)
+        throw &quot;Failed opaqueDivBySafeMin('')&quot;;
+    try {
+        result = opaqueDivBy2(Symbol());
+        throw &quot;Failed opaqueDivBy2(Symbol())&quot;;
+    } catch (exception) {
+        if (exception != &quot;TypeError: Type error&quot;)
+            throw &quot;Wrong exception: &quot; + exception;
+    }
+    result = opaqueDivBy4(true);
+    if (result !== 0.25)
+        throw &quot;Failed opaqueDivBy4(true)&quot;;
+    result = opaqueDivBySafeMaxMinusOne(false);
+    if (result !== 0)
+        throw &quot;Failed opaqueDivBySafeMaxMinusOne(false)&quot;;
+    result = opaqueDivBySafeMin({ valueOf: function() { return 42; }});
+    if (result !== Infinity)
+        throw &quot;Failed opaqueDivBySafeMin({ valueOf: function() { return 42; }})&quot;;
+}
</ins></span></pre>
</div>
</div>

</body>
</html>