1// Copyright 2012 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: --allow-natives-syntax 29 30function divp4(x) { 31 return x / 4; 32} 33 34divp4(8); 35divp4(8); 36%OptimizeFunctionOnNextCall(divp4); 37assertEquals(2, divp4(8)); 38assertEquals(0.5, divp4(2)); 39 40 41function divn4(x) { 42 return x / (-4); 43} 44 45divn4(8); 46divn4(8); 47%OptimizeFunctionOnNextCall(divn4); 48assertEquals(-2, divn4(8)); 49// Check for (0 / -x) 50assertEquals(-0, divn4(0)); 51 52 53// Check for (kMinInt / -1) 54function divn1(x) { 55 return x / (-1); 56} 57 58var two_31 = 1 << 31; 59divn1(2); 60divn1(2); 61%OptimizeFunctionOnNextCall(divn1); 62assertEquals(-2, divn1(2)); 63assertEquals(-two_31, divn1(two_31)); 64 65 66//Check for truncating to int32 case 67function divp4t(x) { 68 return (x / 4) | 0; 69} 70 71divp4t(8); 72divp4t(8); 73%OptimizeFunctionOnNextCall(divp4t); 74assertEquals(-1, divp4t(-5)); 75assertEquals(1, divp4t(5)); 76assertOptimized(divp4t); 77 78function divn4t(x) { 79 return (x / -4) | 0; 80} 81 82divn4t(8); 83divn4t(8); 84%OptimizeFunctionOnNextCall(divn4t); 85assertEquals(1, divn4t(-5)); 86assertEquals(-1, divn4t(5)); 87assertOptimized(divn4t); 88 89// Check kMinInt case. 90function div_by_two(x) { 91 return (x / 2) | 0; 92} 93 94div_by_two(12); 95div_by_two(34); 96%OptimizeFunctionOnNextCall(div_by_two); 97div_by_two(56); 98assertEquals(-(1 << 30), div_by_two(1 << 31)); 99