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: --max-new-space-size=256 --allow-natives-syntax 29 30var test_id = 0; 31 32function testFloor(expect, input) { 33 var test = new Function('n', 34 '"' + (test_id++) + '";return Math.floor(n)'); 35 assertEquals(expect, test(input)); 36 assertEquals(expect, test(input)); 37 assertEquals(expect, test(input)); 38 %OptimizeFunctionOnNextCall(test); 39 assertEquals(expect, test(input)); 40} 41 42function zero() { 43 var x = 0.5; 44 return (function() { return x - 0.5; })(); 45} 46 47function test() { 48 testFloor(0, 0); 49 testFloor(0, zero()); 50 testFloor(-0, -0); 51 testFloor(Infinity, Infinity); 52 testFloor(-Infinity, -Infinity); 53 testFloor(NaN, NaN); 54 55 // Ensure that a negative zero coming from Math.floor is properly handled 56 // by other operations. 57 function ifloor(x) { 58 return 1 / Math.floor(x); 59 } 60 assertEquals(-Infinity, ifloor(-0)); 61 assertEquals(-Infinity, ifloor(-0)); 62 assertEquals(-Infinity, ifloor(-0)); 63 %OptimizeFunctionOnNextCall(ifloor); 64 assertEquals(-Infinity, ifloor(-0)); 65 66 testFloor(0, 0.1); 67 testFloor(0, 0.49999999999999994); 68 testFloor(0, 0.5); 69 testFloor(0, 0.7); 70 testFloor(-1, -0.1); 71 testFloor(-1, -0.49999999999999994); 72 testFloor(-1, -0.5); 73 testFloor(-1, -0.7); 74 testFloor(1, 1); 75 testFloor(1, 1.1); 76 testFloor(1, 1.5); 77 testFloor(1, 1.7); 78 testFloor(-1, -1); 79 testFloor(-2, -1.1); 80 testFloor(-2, -1.5); 81 testFloor(-2, -1.7); 82 83 testFloor(0, Number.MIN_VALUE); 84 testFloor(-1, -Number.MIN_VALUE); 85 testFloor(Number.MAX_VALUE, Number.MAX_VALUE); 86 testFloor(-Number.MAX_VALUE, -Number.MAX_VALUE); 87 testFloor(Infinity, Infinity); 88 testFloor(-Infinity, -Infinity); 89 90 // 2^30 is a smi boundary. 91 var two_30 = 1 << 30; 92 93 testFloor(two_30, two_30); 94 testFloor(two_30, two_30 + 0.1); 95 testFloor(two_30, two_30 + 0.5); 96 testFloor(two_30, two_30 + 0.7); 97 98 testFloor(two_30 - 1, two_30 - 1); 99 testFloor(two_30 - 1, two_30 - 1 + 0.1); 100 testFloor(two_30 - 1, two_30 - 1 + 0.5); 101 testFloor(two_30 - 1, two_30 - 1 + 0.7); 102 103 testFloor(-two_30, -two_30); 104 testFloor(-two_30, -two_30 + 0.1); 105 testFloor(-two_30, -two_30 + 0.5); 106 testFloor(-two_30, -two_30 + 0.7); 107 108 testFloor(-two_30 + 1, -two_30 + 1); 109 testFloor(-two_30 + 1, -two_30 + 1 + 0.1); 110 testFloor(-two_30 + 1, -two_30 + 1 + 0.5); 111 testFloor(-two_30 + 1, -two_30 + 1 + 0.7); 112 113 // 2^52 is a precision boundary. 114 var two_52 = (1 << 30) * (1 << 22); 115 116 testFloor(two_52, two_52); 117 testFloor(two_52, two_52 + 0.1); 118 assertEquals(two_52, two_52 + 0.5); 119 testFloor(two_52, two_52 + 0.5); 120 assertEquals(two_52 + 1, two_52 + 0.7); 121 testFloor(two_52 + 1, two_52 + 0.7); 122 123 testFloor(two_52 - 1, two_52 - 1); 124 testFloor(two_52 - 1, two_52 - 1 + 0.1); 125 testFloor(two_52 - 1, two_52 - 1 + 0.5); 126 testFloor(two_52 - 1, two_52 - 1 + 0.7); 127 128 testFloor(-two_52, -two_52); 129 testFloor(-two_52, -two_52 + 0.1); 130 testFloor(-two_52, -two_52 + 0.5); 131 testFloor(-two_52, -two_52 + 0.7); 132 133 testFloor(-two_52 + 1, -two_52 + 1); 134 testFloor(-two_52 + 1, -two_52 + 1 + 0.1); 135 testFloor(-two_52 + 1, -two_52 + 1 + 0.5); 136 testFloor(-two_52 + 1, -two_52 + 1 + 0.7); 137} 138 139 140// Test in a loop to cover the custom IC and GC-related issues. 141for (var i = 0; i < 500; i++) { 142 test(); 143} 144 145 146// Regression test for a bug where a negative zero coming from Math.floor 147// was not properly handled by other operations. 148function floorsum(i, n) { 149 var ret = Math.floor(n); 150 while (--i > 0) { 151 ret += Math.floor(n); 152 } 153 return ret; 154} 155assertEquals(-0, floorsum(1, -0)); 156%OptimizeFunctionOnNextCall(floorsum); 157// The optimized function will deopt. Run it with enough iterations to try 158// to optimize via OSR (triggering the bug). 159assertEquals(-0, floorsum(100000, -0)); 160