1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
|
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2024, Rivos Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @key randomness
* @bug 8321011
* @summary Test vector intrinsic for Math.round(double) with random input in 64 bits range, verify IR at the same time.
*
* @library /test/lib /
* @modules java.base/jdk.internal.math
* @requires os.arch == "riscv64" & vm.cpu.features ~= ".*rvv.*"
* @run main compiler.vectorization.TestRoundVectorDoubleRandom
*/
package compiler.vectorization;
import java.util.Random;
import static compiler.lib.golden.GoldenRound.golden_round;
import compiler.lib.ir_framework.IR;
import compiler.lib.ir_framework.IRNode;
import compiler.lib.ir_framework.Run;
import compiler.lib.ir_framework.RunInfo;
import compiler.lib.ir_framework.Test;
import compiler.lib.ir_framework.TestFramework;
import compiler.lib.ir_framework.Warmup;
public class TestRoundVectorDoubleRandom {
private static final Random rand = new Random();
private static final int ITERS = 11000;
private static final int ARRLEN = rand.nextInt(4096-997) + 997;
private static final double ADD_INIT = -7500.;
private static final double[] input = new double[ARRLEN];
private static final long [] res = new long[ARRLEN];
public static void main(String args[]) {
TestFramework.runWithFlags("-XX:-TieredCompilation", "-XX:CompileThresholdScaling=0.3");
TestFramework.runWithFlags("-XX:-TieredCompilation", "-XX:CompileThresholdScaling=0.3", "-XX:MaxVectorSize=8");
TestFramework.runWithFlags("-XX:-TieredCompilation", "-XX:CompileThresholdScaling=0.3", "-XX:MaxVectorSize=16");
TestFramework.runWithFlags("-XX:-TieredCompilation", "-XX:CompileThresholdScaling=0.3", "-XX:MaxVectorSize=32");
}
@Test
@IR(counts = {IRNode.ROUND_VD, "> 0"},
applyIf = {"MaxVectorSize", ">= 64"})
static void test_round(long[] a0, double[] a1) {
for (int i = 0; i < a0.length; i+=1) {
a0[i] = Math.round(a1[i]);
}
}
@Run(test = "test_round")
@Warmup(ITERS)
static void test_rounds(RunInfo runInfo) {
// Initialize
for (int i = 0; i < ARRLEN; i++) {
double val = ADD_INIT+(double)i;
input[i] = val;
}
test_round(res, input);
// skip test/verify when warming up
if (runInfo.isWarmUp()) {
return;
}
int errn = 0;
// a double precise float point is composed of 3 parts: sign/exponent/signicand
// exponent part of a float value
final int exponentShift = 52;
final int exponentWidth = 11;
final int exponentBound = 1 << exponentWidth;
// significant part of a float value
final int signicandWidth = exponentShift;
final long signicandBound = 1L << signicandWidth;
final int signicandNum = 256;
// prepare for data of significand part
long signicandValues[] = new long[signicandNum];
int signicandIdx = 0;
for (; signicandIdx < signicandWidth; signicandIdx++) {
signicandValues[signicandIdx] = 1L << signicandIdx;
}
for (; signicandIdx < signicandNum; signicandIdx++) {
signicandValues[signicandIdx] = rand.nextLong(signicandBound);
}
signicandValues[rand.nextInt(signicandNum)] = 0;
// generate input arrays for testing, then run tests & verify results
// generate input arrays by combining different parts
for (long sv : signicandValues) {
// generate test input by combining different parts:
// previously generated significand values,
// random value in exponent range,
// both positive and negative of previous combined values (exponent+significand)
final int exponentStart = rand.nextInt(9);
final int exponentStep = (1 << 3) + rand.nextInt(3);
// Here, we could have iterated the whole range of exponent values, but it would
// take more time to run the test, so just randomly choose some of exponent values.
int ev = exponentStart;
int inputIdx = 0;
for (; ev < exponentBound; ev += exponentStep) {
inputIdx = ev/exponentStep;
// combine exponent and significand
long bits = ((long)ev << exponentShift) + sv;
// combine sign(+/-) with exponent and significand
// positive values
input[inputIdx*2] = Double.longBitsToDouble(bits);
// negative values
bits = bits | (1L << 63);
input[inputIdx*2+1] = Double.longBitsToDouble(bits);
}
// add specific test cases where it looks like in binary format:
// s111 1111 1111 xxxx xxxx xxxx xxxx xxxx ...
// these are for the NaN and Inf.
inputIdx = inputIdx*2+2;
long bits = (1L << exponentWidth) - 1L;
bits <<= exponentShift;
input[inputIdx++] = Double.longBitsToDouble(bits);
bits = bits | (1L << 63);
input[inputIdx] = Double.longBitsToDouble(bits);
// run tests
test_round(res, input);
// verify results
ev = exponentStart;
inputIdx = ev/exponentStep;
for (; ev < exponentBound; ev += exponentStep) {
for (int sign = 0; sign < 2; sign++) {
int idx = inputIdx * 2 + sign;
if (res[idx] != golden_round(input[idx])) {
errn++;
System.err.println("round error, input: " + input[idx] +
", res: " + res[idx] + "expected: " + golden_round(input[idx]) +
", input hex: " + Double.doubleToLongBits(input[idx]) +
", fi: " + sv + ", ei: " + ev + ", sign: " + sign);
}
}
}
}
// generate pure random input arrays, which does not depend on significand/exponent values
for(int i = 0; i < 128; i++) {
for (int j = 0; j < ARRLEN; j++) {
input[j] = rand.nextDouble();
}
// run tests
test_round(res, input);
// verify results
for (int j = 0; j < ARRLEN; j++) {
if (res[j] != golden_round(input[j])) {
errn++;
System.err.println("round error, input: " + input[j] +
", res: " + res[j] + "expected: " + golden_round(input[j]) +
", input hex: " + Double.doubleToLongBits(input[j]));
}
}
}
// test cases for NaN, Inf, subnormal, and so on
{
Double[] dv = new Double[] {
+0.0,
-0.0,
Double.MAX_VALUE,
Double.MIN_VALUE,
Double.NEGATIVE_INFINITY,
Double.POSITIVE_INFINITY,
Double.NaN,
Double.longBitsToDouble(0x7ff0000000000001L), // another NaN
Double.MIN_NORMAL,
0x0.fffffffffffffp-1022, // Maximum Subnormal Value
1.5,
100.5,
10000.5,
-1.5,
-100.5,
-10000.5
};
for (int j = 0; j < ARRLEN; j++) {
input[j] = dv[rand.nextInt(dv.length)];
}
// run tests
test_round(res, input);
// verify results
for (int j = 0; j < ARRLEN; j++) {
if (res[j] != golden_round(input[j])) {
errn++;
System.err.println("round error, input: " + input[j] +
", res: " + res[j] + "expected: " + golden_round(input[j]) +
", input hex: " + Double.doubleToLongBits(input[j]));
}
}
}
if (errn > 0) {
throw new RuntimeException("There are some round error detected!");
}
}
}
|