|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * Copyright (c) 2025, Rivos Inc. All rights reserved. |
| 4 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 5 | + * |
| 6 | + * This code is free software; you can redistribute it and/or modify it |
| 7 | + * under the terms of the GNU General Public License version 2 only, as |
| 8 | + * published by the Free Software Foundation. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 21 | + * or visit www.oracle.com if you need additional information or have any |
| 22 | + * questions. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @test |
| 27 | + * @key randomness |
| 28 | + * @bug 8318220 |
| 29 | + * @summary Test ReverseI intrinsic |
| 30 | + * |
| 31 | + * @library /test/lib / |
| 32 | + * @requires os.arch == "riscv64" & vm.cpu.features ~= ".*zbkb.*" |
| 33 | + * @run main/othervm compiler.c2.riscv64.TestIntegerReverse |
| 34 | + */ |
| 35 | + |
| 36 | +package compiler.c2.riscv64; |
| 37 | + |
| 38 | +import compiler.lib.ir_framework.*; |
| 39 | +import java.util.Random; |
| 40 | +import jdk.test.lib.Asserts; |
| 41 | +import jdk.test.lib.Utils; |
| 42 | + |
| 43 | +import static compiler.lib.golden.GoldenReverse.golden_reverse_integer; |
| 44 | + |
| 45 | +public class TestIntegerReverse { |
| 46 | + |
| 47 | + static Random r = Utils.getRandomInstance(); |
| 48 | + static final int ITERS = 11000; |
| 49 | + static final int ARRLEN = 997; |
| 50 | + static int input[] = new int[ARRLEN]; |
| 51 | + static int outputI[] = new int[ARRLEN]; |
| 52 | + static long outputL[] = new long[ARRLEN]; |
| 53 | + static int err; |
| 54 | + |
| 55 | + public static void main(String args[]) { |
| 56 | + TestFramework.runWithFlags("-XX:-TieredCompilation", "-XX:CompileThresholdScaling=0.3", |
| 57 | + "-XX:+UnlockDiagnosticVMOptions", "-XX:-UseRVV"); // Only test scalar version |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + @IR(counts = {IRNode.REVERSE_I, "> 0"}) |
| 62 | + static void test_reverse_ia(int[] input, int[] outputI) { |
| 63 | + for (int i = 0; i < input.length; i+=1) { |
| 64 | + outputI[i] = Integer.reverse(input[i]); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + @IR(counts = {IRNode.REVERSE_I, "> 0"}) |
| 70 | + static void test_reverse_la(int[] input, long[] outputL) { |
| 71 | + for (int i = 0; i < input.length; i+=1) { |
| 72 | + outputL[i] = Integer.reverse(input[i]); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + @IR(counts = {IRNode.REVERSE_I, "> 0"}) |
| 78 | + static void test_reverse_l(int input, long expected) { |
| 79 | + if (Integer.reverse(input) != expected) { |
| 80 | + err++; |
| 81 | + System.out.println("Test failure, input: " + input + |
| 82 | + ", actual: " + Integer.reverse(input) + |
| 83 | + ", expected: " + expected); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Run(test = {"test_reverse_ia", "test_reverse_la", "test_reverse_l"}) |
| 88 | + @Warmup(ITERS) |
| 89 | + static void test(RunInfo runInfo) { |
| 90 | + // Initialize |
| 91 | + for (int i = 0; i < ARRLEN; i++) { |
| 92 | + input[i] = r.nextInt(); |
| 93 | + } |
| 94 | + input[0] = 0; |
| 95 | + input[1] = 1; |
| 96 | + input[2] = -1; |
| 97 | + input[3] = Integer.MIN_VALUE; |
| 98 | + input[4] = Integer.MAX_VALUE; |
| 99 | + |
| 100 | + test_reverse_ia(input, outputI); |
| 101 | + test_reverse_la(input, outputL); |
| 102 | + for (int i = 0; i < ARRLEN; i++) { |
| 103 | + test_reverse_l(input[i], golden_reverse_integer(input[i])); |
| 104 | + } |
| 105 | + // skip test/verify when warming up |
| 106 | + if (runInfo.isWarmUp()) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + test_reverse_ia(input, outputI); |
| 111 | + test_reverse_la(input, outputL); |
| 112 | + |
| 113 | + for (int i = 0; i < ARRLEN; i++) { |
| 114 | + int golden_val = golden_reverse_integer(input[i]); |
| 115 | + Asserts.assertEquals(outputI[i], golden_val, |
| 116 | + "Test failure (integer array), input: " + input[i] + |
| 117 | + ", actual: " + outputI[i] + |
| 118 | + ", expected: " + golden_val); |
| 119 | + Asserts.assertEquals(outputL[i], (long)golden_val, |
| 120 | + "Test failure (long array), input: " + input[i] + |
| 121 | + ", actual: " + outputL[i] + |
| 122 | + ", expected: " + (long)golden_val); |
| 123 | + } |
| 124 | + |
| 125 | + err = 0; |
| 126 | + for (int i = 0; i < ARRLEN; i++) { |
| 127 | + test_reverse_l(input[i], golden_reverse_integer(input[i])); |
| 128 | + } |
| 129 | + Asserts.assertTrue(err == 0, "Some tests(" + err + ") failed, check previous log for details"); |
| 130 | + } |
| 131 | +} |
0 commit comments