1257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch// Copyright 2011 the V8 project authors. All rights reserved.
20d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// Redistribution and use in source and binary forms, with or without
30d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// modification, are permitted provided that the following conditions are
40d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// met:
50d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//
60d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//     * Redistributions of source code must retain the above copyright
70d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//       notice, this list of conditions and the following disclaimer.
80d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//     * Redistributions in binary form must reproduce the above
90d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//       copyright notice, this list of conditions and the following
100d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//       disclaimer in the documentation and/or other materials provided
110d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//       with the distribution.
120d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//     * Neither the name of Google Inc. nor the names of its
130d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//       contributors may be used to endorse or promote products derived
140d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//       from this software without specific prior written permission.
150d5e116f6aee03185f237311a943491bb079a768Kristian Monsen//
160d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
170d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
180d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
190d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
200d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
210d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
220d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
230d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
240d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
250d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
260d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
270d5e116f6aee03185f237311a943491bb079a768Kristian Monsen
28b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch// Flags: --max-semi-space-size=1 --allow-natives-syntax
29257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
3069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdochvar test_id = 0;
3169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
32257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdochfunction testFloor(expect, input) {
3369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  var test = new Function('n',
3469a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch                          '"' + (test_id++) + '";return Math.floor(n)');
35257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  assertEquals(expect, test(input));
36257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  assertEquals(expect, test(input));
37257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  assertEquals(expect, test(input));
38257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  %OptimizeFunctionOnNextCall(test);
39257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  assertEquals(expect, test(input));
40b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch
411b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  var test_double_input = new Function(
421b268ca467c924004286c97bac133db489cf43d0Ben Murdoch      'n',
431b268ca467c924004286c97bac133db489cf43d0Ben Murdoch      '"' + (test_id++) + '";return Math.floor(+n)');
441b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  assertEquals(expect, test_double_input(input));
451b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  assertEquals(expect, test_double_input(input));
461b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  assertEquals(expect, test_double_input(input));
471b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  %OptimizeFunctionOnNextCall(test_double_input);
481b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  assertEquals(expect, test_double_input(input));
491b268ca467c924004286c97bac133db489cf43d0Ben Murdoch
50b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  var test_double_output = new Function(
51b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      'n',
52b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch      '"' + (test_id++) + '";return Math.floor(n) + -0.0');
53b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  assertEquals(expect, test_double_output(input));
54b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  assertEquals(expect, test_double_output(input));
55b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  assertEquals(expect, test_double_output(input));
56b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  %OptimizeFunctionOnNextCall(test_double_output);
57b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  assertEquals(expect, test_double_output(input));
581b268ca467c924004286c97bac133db489cf43d0Ben Murdoch
591b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  var test_via_ceil = new Function(
601b268ca467c924004286c97bac133db489cf43d0Ben Murdoch      'n',
611b268ca467c924004286c97bac133db489cf43d0Ben Murdoch      '"' + (test_id++) + '";return -Math.ceil(-n)');
621b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  assertEquals(expect, test_via_ceil(input));
631b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  assertEquals(expect, test_via_ceil(input));
641b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  assertEquals(expect, test_via_ceil(input));
651b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  %OptimizeFunctionOnNextCall(test_via_ceil);
661b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  assertEquals(expect, test_via_ceil(input));
671b268ca467c924004286c97bac133db489cf43d0Ben Murdoch
681b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  if (input >= 0) {
691b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    var test_via_trunc = new Function(
701b268ca467c924004286c97bac133db489cf43d0Ben Murdoch        'n',
711b268ca467c924004286c97bac133db489cf43d0Ben Murdoch        '"' + (test_id++) + '";return Math.trunc(n)');
721b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    assertEquals(expect, test_via_trunc(input));
731b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    assertEquals(expect, test_via_trunc(input));
741b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    assertEquals(expect, test_via_trunc(input));
751b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    %OptimizeFunctionOnNextCall(test_via_trunc);
761b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    assertEquals(expect, test_via_trunc(input));
771b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  }
78257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch}
790d5e116f6aee03185f237311a943491bb079a768Kristian Monsen
800d5e116f6aee03185f237311a943491bb079a768Kristian Monsenfunction zero() {
810d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  var x = 0.5;
820d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  return (function() { return x - 0.5; })();
830d5e116f6aee03185f237311a943491bb079a768Kristian Monsen}
840d5e116f6aee03185f237311a943491bb079a768Kristian Monsen
850d5e116f6aee03185f237311a943491bb079a768Kristian Monsenfunction test() {
8669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  // Ensure that a negative zero coming from Math.floor is properly handled
8769a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  // by other operations.
8869a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  function ifloor(x) {
8969a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch    return 1 / Math.floor(x);
9069a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  }
9169a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  assertEquals(-Infinity, ifloor(-0));
9269a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  assertEquals(-Infinity, ifloor(-0));
9369a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  assertEquals(-Infinity, ifloor(-0));
9469a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  %OptimizeFunctionOnNextCall(ifloor);
9569a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch  assertEquals(-Infinity, ifloor(-0));
9669a99ed0b2b2ef69d393c371b03db3a98aaf880eBen Murdoch
97257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(0, 0.1);
98257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(0, 0.49999999999999994);
99257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(0, 0.5);
100257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(0, 0.7);
101b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  testFloor(0, 1.0 - Number.EPSILON);
102257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-1, -0.1);
103257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-1, -0.49999999999999994);
104257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-1, -0.5);
105257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-1, -0.7);
106257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(1, 1);
107257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(1, 1.1);
108b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  testFloor(1, 1.0 + Number.EPSILON);
109257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(1, 1.5);
110257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(1, 1.7);
111257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-1, -1);
112b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  testFloor(-1, -1 + Number.EPSILON);
113b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  testFloor(-2, -1 - Number.EPSILON);
114257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-2, -1.1);
115257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-2, -1.5);
116257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-2, -1.7);
117257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch
118257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(0, Number.MIN_VALUE);
119257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-1, -Number.MIN_VALUE);
120257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(Number.MAX_VALUE, Number.MAX_VALUE);
121257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-Number.MAX_VALUE, -Number.MAX_VALUE);
122257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(Infinity, Infinity);
123257744e915dfc84d6d07a6b2accf8402d9ffc708Ben Murdoch  testFloor(-Infinity, -Infinity);
1240d5e116f6aee03185f237311a943491bb079a768Kristian Monsen}
1250d5e116f6aee03185f237311a943491bb079a768Kristian Monsen
1260d5e116f6aee03185f237311a943491bb079a768Kristian Monsen
1270d5e116f6aee03185f237311a943491bb079a768Kristian Monsen// Test in a loop to cover the custom IC and GC-related issues.
128b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdochfor (var i = 0; i < 10; i++) {
1290d5e116f6aee03185f237311a943491bb079a768Kristian Monsen  test();
130b8a8cc1952d61a2f3a2568848933943a543b5d3eBen Murdoch  new Array(i * 10000);
1310d5e116f6aee03185f237311a943491bb079a768Kristian Monsen}
132