blob: aaa67e97fe21ea4dffe94c38cea96794f4eb4a12 [file] [log] [blame]
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +00001// 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
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +000028// Flags: --allow-natives-syntax
29
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +000030function divp4(x) {
31 return x / 4;
32}
33
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +000034divp4(8);
35divp4(8);
36%OptimizeFunctionOnNextCall(divp4);
37assertEquals(2, divp4(8));
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +000038assertEquals(0.5, divp4(2));
39
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +000040
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +000041function divn4(x) {
42 return x / (-4);
43}
44
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +000045divn4(8);
46divn4(8);
47%OptimizeFunctionOnNextCall(divn4);
48assertEquals(-2, divn4(8));
49// Check for (0 / -x)
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +000050assertEquals(-0, divn4(0));
51
52
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +000053// Check for (kMinInt / -1)
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +000054function divn1(x) {
55 return x / (-1);
56}
57
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +000058var 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;
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +000069}
70
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +000071divp4t(8);
72divp4t(8);
73%OptimizeFunctionOnNextCall(divp4t);
74assertEquals(-1, divp4t(-5));
75assertEquals(1, divp4t(5));
76assertOptimized(divp4t);
jkummerow@chromium.org5323a9c2012-12-10 19:00:50 +000077
verwaest@chromium.org32cb9b22013-08-21 11:18:12 +000078function 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);