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