1// Copyright 2011 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// Flags: --allow-natives-syntax 29 30var test_id = 0; 31function testRound(expect, input) { 32 // Make source code different on each invocation to make 33 // sure it gets optimized each time. 34 var doRound = new Function('input', 35 '"' + (test_id++) + '";return Math.round(input)'); 36 assertEquals(expect, doRound(input)); 37 assertEquals(expect, doRound(input)); 38 assertEquals(expect, doRound(input)); 39 %OptimizeFunctionOnNextCall(doRound); 40 assertEquals(expect, doRound(input)); 41} 42 43testRound(0, 0); 44testRound(-0, -0); 45testRound(Infinity, Infinity); 46testRound(-Infinity, -Infinity); 47testRound(NaN, NaN); 48 49// Regression test for a bug where a negative zero coming from Math.round 50// was not properly handled by other operations. 51function roundsum(i, n) { 52 var ret = Math.round(n); 53 while (--i > 0) { 54 ret += Math.round(n); 55 } 56 return ret; 57} 58assertEquals(-0, roundsum(1, -0)); 59%OptimizeFunctionOnNextCall(roundsum); 60// The optimized function will deopt. Run it with enough iterations to try 61// to optimize via OSR (triggering the bug). 62assertEquals(-0, roundsum(100000, -0)); 63 64testRound(1, 0.5); 65testRound(1, 0.7); 66testRound(1, 1); 67testRound(1, 1.1); 68testRound(1, 1.49999); 69testRound(-0, -0.5); 70testRound(-1, -0.5000000000000001); 71testRound(-1, -0.7); 72testRound(-1, -1); 73testRound(-1, -1.1); 74testRound(-1, -1.49999); 75testRound(-1, -1.5); 76 77testRound(9007199254740990, 9007199254740990); 78testRound(9007199254740991, 9007199254740991); 79testRound(-9007199254740990, -9007199254740990); 80testRound(-9007199254740991, -9007199254740991); 81testRound(Number.MAX_VALUE, Number.MAX_VALUE); 82testRound(-Number.MAX_VALUE, -Number.MAX_VALUE); 83 84testRound(536870911, 536870910.5); 85testRound(536870911, 536870911); 86testRound(536870911, 536870911.4); 87testRound(536870912, 536870911.5); 88testRound(536870912, 536870912); 89testRound(536870912, 536870912.4); 90testRound(536870913, 536870912.5); 91testRound(536870913, 536870913); 92testRound(536870913, 536870913.4); 93testRound(1073741823, 1073741822.5); 94testRound(1073741823, 1073741823); 95testRound(1073741823, 1073741823.4); 96testRound(1073741824, 1073741823.5); 97testRound(1073741824, 1073741824); 98testRound(1073741824, 1073741824.4); 99testRound(1073741825, 1073741824.5); 100testRound(2147483647, 2147483646.5); 101testRound(2147483647, 2147483647); 102testRound(2147483647, 2147483647.4); 103testRound(2147483648, 2147483647.5); 104testRound(2147483648, 2147483648); 105testRound(2147483648, 2147483648.4); 106testRound(2147483649, 2147483648.5); 107 108// Tests based on WebKit LayoutTests 109 110testRound(0, 0.4); 111testRound(-0, -0.4); 112testRound(-0, -0.5); 113testRound(1, 0.6); 114testRound(-1, -0.6); 115testRound(2, 1.5); 116testRound(2, 1.6); 117testRound(-2, -1.6); 118testRound(8640000000000000, 8640000000000000); 119testRound(8640000000000001, 8640000000000001); 120testRound(8640000000000002, 8640000000000002); 121testRound(9007199254740990, 9007199254740990); 122testRound(9007199254740991, 9007199254740991); 123testRound(1.7976931348623157e+308, 1.7976931348623157e+308); 124testRound(-8640000000000000, -8640000000000000); 125testRound(-8640000000000001, -8640000000000001); 126testRound(-8640000000000002, -8640000000000002); 127testRound(-9007199254740990, -9007199254740990); 128testRound(-9007199254740991, -9007199254740991); 129testRound(-1.7976931348623157e+308, -1.7976931348623157e+308); 130testRound(Infinity, Infinity); 131testRound(-Infinity, -Infinity); 132 133 // Some special double number cases. 134var ulp = Math.pow(2, -1022 - 52); 135var max_denormal = (Math.pow(2, 52) - 1) * ulp; 136var min_normal = Math.pow(2, -1022); 137var max_fraction = Math.pow(2, 52) - 0.5; 138var min_nonfraction = Math.pow(2, 52); 139var max_non_infinite = Number.MAX_VALUE; 140 141var max_smi31 = Math.pow(2,30) - 1; 142var min_smi31 = -Math.pow(2,30); 143var max_smi32 = Math.pow(2,31) - 1; 144var min_smi32 = -Math.pow(2,31); 145 146testRound(0, ulp); 147testRound(0, max_denormal); 148testRound(0, min_normal); 149testRound(0, 0.49999999999999994); 150testRound(1, 0.5); 151testRound(Math.pow(2,52), max_fraction); 152testRound(min_nonfraction, min_nonfraction); 153testRound(max_non_infinite, max_non_infinite); 154 155testRound(max_smi31, max_smi31 - 0.5); 156testRound(max_smi31 + 1, max_smi31 + 0.5); 157testRound(max_smi32, max_smi32 - 0.5); 158testRound(max_smi32 + 1, max_smi32 + 0.5); 159 160testRound(-0, -ulp); 161testRound(-0, -max_denormal); 162testRound(-0, -min_normal); 163testRound(-0, -0.49999999999999994); 164testRound(-0, -0.5); 165testRound(-Math.pow(2,52)+1, -max_fraction); 166testRound(-min_nonfraction, -min_nonfraction); 167testRound(-max_non_infinite, -max_non_infinite); 168 169testRound(min_smi31, min_smi31 - 0.5); 170testRound(min_smi31 + 1, min_smi31 + 0.5); 171testRound(min_smi32, min_smi32 - 0.5); 172testRound(min_smi32 + 1, min_smi32 + 0.5); 173 174 175