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
|
/*
* Copyright (c) 2023, 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.*;
import jdk.test.lib.Asserts;
import jdk.test.lib.Utils;
/**
* @test
* @bug 8307795
* @key randomness
* @library /test/lib /
* @requires os.arch=="aarch64"
* @summary AArch64: Optimize VectorMask.truecount() on Neon
* @modules jdk.incubator.vector
*
* @run driver compiler.vectorapi.TestVectorMaskTrueCount
*/
public class TestVectorMaskTrueCount {
private static final VectorSpecies<Float> SPECIES_F = FloatVector.SPECIES_64;
private static final VectorSpecies<Double> SPECIES_D = DoubleVector.SPECIES_128;
private static final VectorSpecies<Integer> SPECIES_I = IntVector.SPECIES_128;
private static final int LENGTH = 1024;
private static final Random RD = new Random();
private static boolean[] ba;
private static boolean[] bb;
static {
ba = new boolean[LENGTH];
bb = new boolean[LENGTH];
for (int i = 0; i < LENGTH; i++) {
ba[i] = RD.nextBoolean();
bb[i] = RD.nextBoolean();
}
}
static int maskAndTrueCount(boolean[] a, boolean[] b, int idx, int count) {
int trueCount = 0;
boolean[] c = new boolean[count];
for (int i = idx; i < idx + count; i++) {
c[i - idx] = a[i] & b[i];
}
for (int i = 0; i < c.length; i++) {
trueCount += c[i] ? 1 : 0;
}
return trueCount;
}
static void assertArrayEqualsFloat(int[] r, boolean[] a, boolean[] b) {
for (int i = 0; i < a.length; i += SPECIES_F.length()) {
Asserts.assertEquals(r[i], maskAndTrueCount(a, b, i, SPECIES_F.length()));
}
}
static void assertArrayEqualsDouble(int[] r, boolean[] a, boolean[] b) {
for (int i = 0; i < a.length; i += SPECIES_D.length()) {
Asserts.assertEquals(r[i], maskAndTrueCount(a, b, i, SPECIES_D.length()));
}
}
static void assertArrayEqualsInteger(int[] r, boolean[] a, boolean[] b) {
for (int i = 0; i < a.length; i += SPECIES_I.length()) {
Asserts.assertEquals(r[i], maskAndTrueCount(a, b, i, SPECIES_I.length()));
}
}
@Test
@IR(counts = { IRNode.VSTOREMASK_TRUECOUNT, ">= 1" })
public static void testFloat() {
int[] r = new int[LENGTH];
for (int i = 0; i < LENGTH; i += SPECIES_F.length()) {
VectorMask<Float> ma = VectorMask.fromArray(SPECIES_F, ba, i);
VectorMask<Float> mb = VectorMask.fromArray(SPECIES_F, bb, i);
r[i] = ma.and(mb).trueCount();
}
assertArrayEqualsFloat(r, ba, bb);
}
@Test
@IR(counts = { IRNode.VSTOREMASK_TRUECOUNT, ">= 1" })
public static void testDouble() {
int[] r = new int[LENGTH];
for (int i = 0; i < LENGTH; i += SPECIES_D.length()) {
VectorMask<Double> ma = VectorMask.fromArray(SPECIES_D, ba, i);
VectorMask<Double> mb = VectorMask.fromArray(SPECIES_D, bb, i);
r[i] = ma.and(mb).trueCount();
}
assertArrayEqualsDouble(r, ba, bb);
}
@Test
@IR(counts = { IRNode.VSTOREMASK_TRUECOUNT, ">= 1" })
public static void testInt() {
int[] r = new int[LENGTH];
for (int i = 0; i < LENGTH; i += SPECIES_I.length()) {
VectorMask<Integer> ma = VectorMask.fromArray(SPECIES_I, ba, i);
VectorMask<Integer> mb = VectorMask.fromArray(SPECIES_I, bb, i);
r[i] = ma.and(mb).trueCount();
}
assertArrayEqualsInteger(r, ba, bb);
}
public static void main(String[] args) {
TestFramework testFramework = new TestFramework();
testFramework.setDefaultWarmup(10000)
.addFlags("--add-modules=jdk.incubator.vector")
.addFlags("-XX:UseSVE=0")
.start();
}
}
|