jdk-24/test/hotspot/jtreg/compiler/vectorization/runner/LoopReductionOpTest.java

190 lines
4.8 KiB
Java
Raw Normal View History

/*
* Copyright (c) 2022, Arm Limited. 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
* @summary Vectorization test on reduction operations
* @library /test/lib /
*
* @build sun.hotspot.WhiteBox
* compiler.vectorization.runner.VectorizationTestRunner
*
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
* @run main/othervm -Xbootclasspath/a:.
* -XX:+UnlockDiagnosticVMOptions
* -XX:+WhiteBoxAPI
* compiler.vectorization.runner.LoopReductionOpTest
*
* @requires vm.compiler2.enabled & vm.flagless
*/
package compiler.vectorization.runner;
import java.util.Random;
public class LoopReductionOpTest extends VectorizationTestRunner {
private static final int SIZE = 2345;
private int[] a;
private int[] b;
private int[] c;
private double[] d;
private float[] f;
private long[] l;
private int intInv;
public LoopReductionOpTest() {
a = new int[SIZE];
b = new int[SIZE];
c = new int[SIZE];
d = new double[SIZE];
f = new float[SIZE];
l = new long[SIZE];
for (int i = 0; i < SIZE; i++) {
a[i] = -531 * i;
b[i] = 2222 * i + 8;
c[i] = 2147480000;
d[i] = i * 2.5;
f[i] = i * -(333.3f);
l[i] = 444444444L * i;
}
Random ran = new Random(10001);
intInv = ran.nextInt();
}
@Test
public int reductionAddSumOfArray() {
int res = 0;
for (int i = 0; i < SIZE; i++) {
res += (a[i] + b[i]);
}
return res;
}
@Test
public int reductionAddIndex() {
int res = 0;
for (int i = 0; i < SIZE; i++) {
res += i;
}
return res;
}
@Test
// Note that adding constant in loop would be directly optimized to
// scalar operations, hence this case is not vectorized.
public int reductionAddConstant() {
int res = 0;
for (int i = 0; i < SIZE; i++) {
res += 222;
}
return res;
}
@Test
public int reductionAddLoopInv() {
int res = 0;
for (int i = 0; i < SIZE; i++) {
res += intInv;
}
return res;
}
@Test
public int reductionAddSumOfMultiple() {
int res = 0;
for (int i = 0; i < SIZE; i++) {
res += (a[i] + b[i]) * i << 2;
}
return res;
}
@Test
public int twoReductions() {
int res1 = 0;
int res2 = 0;
for (int i = 0; i < SIZE; i++) {
res1 += a[i];
res2 += b[i];
}
return res1 * res2;
}
@Test
public float twoReductionsSameElementSize() {
int res1 = 0;
float res2 = 0.0f;
for (int i = 0; i < SIZE; i++) {
res1 += a[i];
res2 += f[i];
}
return res1 * res2;
}
@Test
public double twoReductionsDifferentSizes1() {
int res1 = 0;
double res2 = 0.0;
for (int i = 0; i < SIZE; i++) {
res1 += a[i];
res2 += d[i];
}
return res1 * res2;
}
@Test
public double twoReductionsDifferentSizes2() {
long res1 = 0L;
float res2 = 0.0f;
for (int i = 0; i < SIZE; i++) {
res1 += l[i];
res2 += f[i];
}
return res1 * res2;
}
@Test
public long reductionWithNonReductionDifferentSizes() {
long res = 0L;
int[] arr = new int[SIZE];
for (int i = 0; i < SIZE; i++) {
arr[i] = a[i] + b[i];
res += l[i];
}
return res;
}
@Test
public long reductionLoopIndexSumDifferentSizes() {
int intSum = 0;
long longSum = 0L;
for (int i = 0; i < SIZE; i++) {
intSum += i;
longSum += i;
}
return intSum + 2 * longSum;
}
}