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
|
/*
* 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.
*/
package compiler.vectorapi;
import compiler.lib.ir_framework.*;
import java.util.Random;
import jdk.incubator.vector.DoubleVector;
import jdk.incubator.vector.FloatVector;
import jdk.incubator.vector.VectorMask;
import jdk.incubator.vector.VectorOperators;
import jdk.incubator.vector.VectorSpecies;
import jdk.test.lib.Asserts;
import jdk.test.lib.Utils;
/**
* @test
* @bug 8292587
* @key randomness
* @library /test/lib /
* @requires os.arch=="aarch64"
* @summary AArch64: Support SVE fabd instruction
* @modules jdk.incubator.vector
*
* @run driver compiler.vectorapi.VectorAbsDiffTest
*/
public class VectorAbsDiffTest {
private static final VectorSpecies<Double> D_SPECIES = DoubleVector.SPECIES_MAX;
private static final VectorSpecies<Float> F_SPECIES = FloatVector.SPECIES_MAX;
private static int LENGTH = 1024;
private static final Random RD = Utils.getRandomInstance();
private static float[] fa;
private static float[] fb;
private static float[] fr;
private static double[] da;
private static double[] db;
private static double[] dr;
private static boolean[] m;
static {
fa = new float[LENGTH];
fb = new float[LENGTH];
fr = new float[LENGTH];
da = new double[LENGTH];
db = new double[LENGTH];
dr = new double[LENGTH];
m = new boolean[LENGTH];
for (int i = 0; i < LENGTH; i++) {
fa[i] = RD.nextFloat((float) 25.0);
fb[i] = RD.nextFloat((float) 25.0);
da[i] = RD.nextDouble(25.0);
db[i] = RD.nextDouble(25.0);
m[i] = RD.nextBoolean();
}
}
@Test
@IR(counts = {IRNode.VFABD, "> 0"})
public static void testFloatAbsDiff() {
for (int i = 0; i < LENGTH; i += F_SPECIES.length()) {
FloatVector av = FloatVector.fromArray(F_SPECIES, fa, i);
FloatVector bv = FloatVector.fromArray(F_SPECIES, fb, i);
av.sub(bv).lanewise(VectorOperators.ABS).intoArray(fr, i);
}
}
@Run(test = "testFloatAbsDiff")
public static void testFloatAbsDiff_runner() {
testFloatAbsDiff();
for (int i = 0; i < LENGTH; i++) {
Asserts.assertEquals(Math.abs(fa[i] - fb[i]), fr[i]);
}
}
@Test
@IR(counts = {IRNode.VFABD_MASKED, "> 0"}, applyIf = {"UseSVE", "> 0"})
public static void testFloatAbsDiffMasked() {
for (int i = 0; i < LENGTH; i += F_SPECIES.length()) {
FloatVector av = FloatVector.fromArray(F_SPECIES, fa, i);
FloatVector bv = FloatVector.fromArray(F_SPECIES, fb, i);
VectorMask<Float> mask = VectorMask.fromArray(F_SPECIES, m, i);
av.lanewise(VectorOperators.SUB, bv, mask).lanewise(VectorOperators.ABS, mask).intoArray(fr, i);
}
}
@Run(test = "testFloatAbsDiffMasked")
public static void testFloatAbsDiffMasked_runner() {
testFloatAbsDiffMasked();
for (int i = 0; i < LENGTH; i++) {
if (m[i]) {
Asserts.assertEquals(Math.abs(fa[i] - fb[i]), fr[i]);
} else {
Asserts.assertEquals(fa[i], fr[i]);
}
}
}
@Test
@IR(counts = {IRNode.VFABD, "> 0"})
public static void testDoubleAbsDiff() {
for (int i = 0; i < LENGTH; i += D_SPECIES.length()) {
DoubleVector av = DoubleVector.fromArray(D_SPECIES, da, i);
DoubleVector bv = DoubleVector.fromArray(D_SPECIES, db, i);
av.sub(bv).lanewise(VectorOperators.ABS).intoArray(dr, i);
}
}
@Run(test = "testDoubleAbsDiff")
public static void testDoubleAbsDiff_runner() {
testDoubleAbsDiff();
for (int i = 0; i < LENGTH; i++) {
Asserts.assertEquals(Math.abs(da[i] - db[i]), dr[i]);
}
}
@Test
@IR(counts = {IRNode.VFABD_MASKED, "> 0"}, applyIf = {"UseSVE", "> 0"})
public static void testDoubleAbsDiffMasked() {
for (int i = 0; i < LENGTH; i += D_SPECIES.length()) {
DoubleVector av = DoubleVector.fromArray(D_SPECIES, da, i);
DoubleVector bv = DoubleVector.fromArray(D_SPECIES, db, i);
VectorMask<Double> mask = VectorMask.fromArray(D_SPECIES, m, i);
av.lanewise(VectorOperators.SUB, bv, mask).lanewise(VectorOperators.ABS, mask).intoArray(dr, i);
}
}
@Run(test = "testDoubleAbsDiffMasked")
public static void testDoubleAbsDiffMasked_runner() {
testDoubleAbsDiffMasked();
for (int i = 0; i < LENGTH; i++) {
if (m[i]) {
Asserts.assertEquals(Math.abs(da[i] - db[i]), dr[i]);
} else {
Asserts.assertEquals(da[i], dr[i]);
}
}
}
public static void main(String[] args) {
TestFramework.runWithFlags("--add-modules=jdk.incubator.vector");
}
}
|