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
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.
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.
28 // Flags: --allow-natives-syntax
32 return (function() { return x - 0.5; })();
36 assertEquals(0, Math.abs(0));
37 assertEquals(0, Math.abs(zero()));
38 assertEquals(0, Math.abs(-0));
39 assertEquals(Infinity, Math.abs(Infinity));
40 assertEquals(Infinity, Math.abs(-Infinity));
41 assertEquals(NaN, Math.abs(NaN));
42 assertEquals(NaN, Math.abs(-NaN));
43 assertEquals('Infinity', Math.abs(Number('+Infinity')).toString());
44 assertEquals('Infinity', Math.abs(Number('-Infinity')).toString());
45 assertEquals('NaN', Math.abs(NaN).toString());
46 assertEquals('NaN', Math.abs(-NaN).toString());
48 assertEquals(0.1, Math.abs(0.1));
49 assertEquals(0.5, Math.abs(0.5));
50 assertEquals(0.1, Math.abs(-0.1));
51 assertEquals(0.5, Math.abs(-0.5));
52 assertEquals(1, Math.abs(1));
53 assertEquals(1.1, Math.abs(1.1));
54 assertEquals(1.5, Math.abs(1.5));
55 assertEquals(1, Math.abs(-1));
56 assertEquals(1.1, Math.abs(-1.1));
57 assertEquals(1.5, Math.abs(-1.5));
59 assertEquals(Number.MIN_VALUE, Math.abs(Number.MIN_VALUE));
60 assertEquals(Number.MIN_VALUE, Math.abs(-Number.MIN_VALUE));
61 assertEquals(Number.MAX_VALUE, Math.abs(Number.MAX_VALUE));
62 assertEquals(Number.MAX_VALUE, Math.abs(-Number.MAX_VALUE));
64 // 2^30 is a smi boundary on arm and ia32.
67 assertEquals(two_30, Math.abs(two_30));
68 assertEquals(two_30, Math.abs(-two_30));
70 assertEquals(two_30 + 1, Math.abs(two_30 + 1));
71 assertEquals(two_30 + 1, Math.abs(-two_30 - 1));
73 assertEquals(two_30 - 1, Math.abs(two_30 - 1));
74 assertEquals(two_30 - 1, Math.abs(-two_30 + 1));
76 // 2^31 is a smi boundary on x64.
77 var two_31 = 2 * two_30;
79 assertEquals(two_31, Math.abs(two_31));
80 assertEquals(two_31, Math.abs(-two_31));
82 assertEquals(two_31 + 1, Math.abs(two_31 + 1));
83 assertEquals(two_31 + 1, Math.abs(-two_31 - 1));
85 assertEquals(two_31 - 1, Math.abs(two_31 - 1));
86 assertEquals(two_31 - 1, Math.abs(-two_31 + 1));
88 assertEquals(NaN, Math.abs("not a number"));
89 assertEquals(NaN, Math.abs([1, 2, 3]));
90 assertEquals(42, Math.abs({valueOf: function() { return 42; } }));
91 assertEquals(42, Math.abs({valueOf: function() { return -42; } }));
95 // Test in a loop to cover the custom IC and GC-related issues.
96 for (var i = 0; i < 500; i++) {
100 // Regression test for optimized version of Math.abs, see:
101 // http://codereview.chromium.org/6875002.
105 // Get some smi type feedback.
106 for(var i = 0; i < 1000; i++) {
109 assertEquals(42, foo(-42));
110 %OptimizeFunctionOnNextCall(foo)
111 assertEquals(42, foo(-42));
113 // Regression test for SMI input of Math.abs on X64, see:
114 // https://codereview.chromium.org/21180004/
117 return Math.abs(a[0]);
119 assertEquals(1, foo2());
120 assertEquals(1, foo2());
121 %OptimizeFunctionOnNextCall(foo2);
122 assertEquals(1, foo2());