math-floor.js revision 053d10c438f14580aaf4ab1b2aad93a5a4fe8b82
1// Copyright 2010 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
29
30function zero() {
31  var x = 0.5;
32  return (function() { return x - 0.5; })();
33}
34
35function test() {
36  assertEquals(0, Math.floor(0));
37  assertEquals(0, Math.floor(zero()));
38  assertEquals(1/-0, 1/Math.floor(-0));  // 0 == -0, so we use reciprocals.
39  assertEquals(Infinity, Math.floor(Infinity));
40  assertEquals(-Infinity, Math.floor(-Infinity));
41  assertNaN(Math.floor(NaN));
42
43  assertEquals(0, Math.floor(0.1));
44  assertEquals(0, Math.floor(0.5));
45  assertEquals(0, Math.floor(0.7));
46  assertEquals(-1, Math.floor(-0.1));
47  assertEquals(-1, Math.floor(-0.5));
48  assertEquals(-1, Math.floor(-0.7));
49  assertEquals(1, Math.floor(1));
50  assertEquals(1, Math.floor(1.1));
51  assertEquals(1, Math.floor(1.5));
52  assertEquals(1, Math.floor(1.7));
53  assertEquals(-1, Math.floor(-1));
54  assertEquals(-2, Math.floor(-1.1));
55  assertEquals(-2, Math.floor(-1.5));
56  assertEquals(-2, Math.floor(-1.7));
57
58  assertEquals(0, Math.floor(Number.MIN_VALUE));
59  assertEquals(-1, Math.floor(-Number.MIN_VALUE));
60  assertEquals(Number.MAX_VALUE, Math.floor(Number.MAX_VALUE));
61  assertEquals(-Number.MAX_VALUE, Math.floor(-Number.MAX_VALUE));
62  assertEquals(Infinity, Math.floor(Infinity));
63  assertEquals(-Infinity, Math.floor(-Infinity));
64
65  // 2^30 is a smi boundary.
66  var two_30 = 1 << 30;
67
68  assertEquals(two_30, Math.floor(two_30));
69  assertEquals(two_30, Math.floor(two_30 + 0.1));
70  assertEquals(two_30, Math.floor(two_30 + 0.5));
71  assertEquals(two_30, Math.floor(two_30 + 0.7));
72
73  assertEquals(two_30 - 1, Math.floor(two_30 - 1));
74  assertEquals(two_30 - 1, Math.floor(two_30 - 1 + 0.1));
75  assertEquals(two_30 - 1, Math.floor(two_30 - 1 + 0.5));
76  assertEquals(two_30 - 1, Math.floor(two_30 - 1 + 0.7));
77
78  assertEquals(-two_30, Math.floor(-two_30));
79  assertEquals(-two_30, Math.floor(-two_30 + 0.1));
80  assertEquals(-two_30, Math.floor(-two_30 + 0.5));
81  assertEquals(-two_30, Math.floor(-two_30 + 0.7));
82
83  assertEquals(-two_30 + 1, Math.floor(-two_30 + 1));
84  assertEquals(-two_30 + 1, Math.floor(-two_30 + 1 + 0.1));
85  assertEquals(-two_30 + 1, Math.floor(-two_30 + 1 + 0.5));
86  assertEquals(-two_30 + 1, Math.floor(-two_30 + 1 + 0.7));
87
88  // 2^52 is a precision boundary.
89  var two_52 = (1 << 30) * (1 << 22);
90
91  assertEquals(two_52, Math.floor(two_52));
92  assertEquals(two_52, Math.floor(two_52 + 0.1));
93  assertEquals(two_52, two_52 + 0.5);
94  assertEquals(two_52, Math.floor(two_52 + 0.5));
95  assertEquals(two_52 + 1, two_52 + 0.7);
96  assertEquals(two_52 + 1, Math.floor(two_52 + 0.7));
97
98  assertEquals(two_52 - 1, Math.floor(two_52 - 1));
99  assertEquals(two_52 - 1, Math.floor(two_52 - 1 + 0.1));
100  assertEquals(two_52 - 1, Math.floor(two_52 - 1 + 0.5));
101  assertEquals(two_52 - 1, Math.floor(two_52 - 1 + 0.7));
102
103  assertEquals(-two_52, Math.floor(-two_52));
104  assertEquals(-two_52, Math.floor(-two_52 + 0.1));
105  assertEquals(-two_52, Math.floor(-two_52 + 0.5));
106  assertEquals(-two_52, Math.floor(-two_52 + 0.7));
107
108  assertEquals(-two_52 + 1, Math.floor(-two_52 + 1));
109  assertEquals(-two_52 + 1, Math.floor(-two_52 + 1 + 0.1));
110  assertEquals(-two_52 + 1, Math.floor(-two_52 + 1 + 0.5));
111  assertEquals(-two_52 + 1, Math.floor(-two_52 + 1 + 0.7));
112}
113
114
115// Test in a loop to cover the custom IC and GC-related issues.
116for (var i = 0; i < 500; i++) {
117  test();
118}
119