|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +/* |
| 25 | + * @test |
| 26 | + * @bug 8257182 |
| 27 | + * @requires vm.compiler2.enabled |
| 28 | + * @summary Test RotateLeftNode and RotateRightNode with negative even numbers which produces a wrong result due to |
| 29 | + * applying an arithemtic instead of a logical shift. |
| 30 | + * @run main/othervm -XX:CompileCommand=compileonly,compiler.c2.TestRotateNegativeEvenValues::run |
| 31 | + * -XX:CompileCommand=inline,compiler.c2.TestRotateNegativeEvenValues::test |
| 32 | + * -Xcomp -XX:-TieredCompilation compiler.c2.TestRotateNegativeEvenValues |
| 33 | + */ |
| 34 | +package compiler.c2; |
| 35 | + |
| 36 | +public class TestRotateNegativeEvenValues { |
| 37 | + |
| 38 | + public static void run() { |
| 39 | + test(1 << 31); // INT_MIN |
| 40 | + test(1L << 63); // LONG_MIN |
| 41 | + test(-1 << 10); // -1024 |
| 42 | + test(-1L << 10); // -1024 |
| 43 | + test(-1 << 20); // -1048576 |
| 44 | + test(-1L << 20); // -1048576 |
| 45 | + test(-2); // 111...10 |
| 46 | + test(-2L); // 111...10 |
| 47 | + test(-3546); // Random minus even number |
| 48 | + test(-3546L); // Random minus even number |
| 49 | + } |
| 50 | + |
| 51 | + // Inlined such that negativeEvenNumber is a constant |
| 52 | + public static void test(int negativeEvenNumber) { |
| 53 | + for (int i = 1; i <= 1; i++) { |
| 54 | + int leftShift = negativeEvenNumber << -i; |
| 55 | + int rightShift = negativeEvenNumber >>> i; |
| 56 | + if ((leftShift | rightShift) != (rightShift | leftShift)) { |
| 57 | + int or1 = leftShift | rightShift; |
| 58 | + int or2 = rightShift | leftShift; |
| 59 | + throw new RuntimeException("Or operations are not equal:" + " " + or1 + " vs. "+ or2 |
| 60 | + + " - leftShift: " + leftShift + ", rightShift: " + rightShift); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Inlined such that negativeEvenNumber is a constant |
| 66 | + public static void test(long negativeEvenNumber) { |
| 67 | + for (int i = 1; i <= 1; i++) { |
| 68 | + long leftShift = negativeEvenNumber << -i; |
| 69 | + long rightShift = negativeEvenNumber >>> i; |
| 70 | + if ((leftShift | rightShift) != (rightShift | leftShift)) { |
| 71 | + long or1 = leftShift | rightShift; |
| 72 | + long or2 = rightShift | leftShift; |
| 73 | + throw new RuntimeException("Or operations are not equal:" + " " + or1 + " vs. "+ or2 |
| 74 | + + " - leftShift: " + leftShift + ", rightShift: " + rightShift); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + public static void main(String argv[]) { |
| 80 | + run(); |
| 81 | + } |
| 82 | +} |
0 commit comments