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 --allow-natives-syntax 29 30function zero() { 31 var x = 0.5; 32 return (function() { return x - 0.5; })(); 33} 34 35function test() { 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()); 47 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)); 58 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)); 63 64 // 2^30 is a smi boundary on arm and ia32. 65 var two_30 = 1 << 30; 66 67 assertEquals(two_30, Math.abs(two_30)); 68 assertEquals(two_30, Math.abs(-two_30)); 69 70 assertEquals(two_30 + 1, Math.abs(two_30 + 1)); 71 assertEquals(two_30 + 1, Math.abs(-two_30 - 1)); 72 73 assertEquals(two_30 - 1, Math.abs(two_30 - 1)); 74 assertEquals(two_30 - 1, Math.abs(-two_30 + 1)); 75 76 // 2^31 is a smi boundary on x64. 77 var two_31 = 2 * two_30; 78 79 assertEquals(two_31, Math.abs(two_31)); 80 assertEquals(two_31, Math.abs(-two_31)); 81 82 assertEquals(two_31 + 1, Math.abs(two_31 + 1)); 83 assertEquals(two_31 + 1, Math.abs(-two_31 - 1)); 84 85 assertEquals(two_31 - 1, Math.abs(two_31 - 1)); 86 assertEquals(two_31 - 1, Math.abs(-two_31 + 1)); 87 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; } })); 92} 93 94 95// Test in a loop to cover the custom IC and GC-related issues. 96for (var i = 0; i < 500; i++) { 97 test(); 98} 99 100// Regression test for optimized version of Math.abs, see: 101// http://codereview.chromium.org/6875002. 102function foo(x) { 103 return Math.abs(x); 104} 105// Get some smi type feedback. 106for(var i = 0; i < 1000; i++) { 107 foo(-i); 108} 109assertEquals(42, foo(-42)); 110%OptimizeFunctionOnNextCall(foo) 111assertEquals(42, foo(-42)); 112