1 /*
2 * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
45 import org.testng.annotations.DataProvider;
46 import org.testng.annotations.Test;
47
48 import java.lang.Integer;
49 import java.util.List;
50 import java.util.Arrays;
51 import java.util.function.BiFunction;
52 import java.util.function.IntFunction;
53 import java.util.Objects;
54 import java.util.stream.Collectors;
55 import java.util.stream.Stream;
56
57 @Test
58 public class FloatMaxVectorTests extends AbstractVectorTest {
59
60 static final VectorSpecies<Float> SPECIES =
61 FloatVector.SPECIES_MAX;
62
63 static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
64
65 static VectorShape getMaxBit() {
66 return VectorShape.S_Max_BIT;
67 }
68
69 private static final int Max = 256; // juts so we can do N/Max
70
71 // for floating point addition reduction ops that may introduce rounding errors
72 private static final float RELATIVE_ROUNDING_ERROR_FACTOR_ADD = (float)10.0;
73
74 // for floating point multiplication reduction ops that may introduce rounding errors
75 private static final float RELATIVE_ROUNDING_ERROR_FACTOR_MUL = (float)50.0;
76
77 static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
78
79 static void assertArraysStrictlyEquals(float[] r, float[] a) {
80 for (int i = 0; i < a.length; i++) {
81 int ir = Float.floatToRawIntBits(r[i]);
82 int ia = Float.floatToRawIntBits(a[i]);
83 if (ir != ia) {
84 Assert.fail(String.format("at index #%d, expected = %08X, actual = %08X", i, ia, ir));
2095 assertBroadcastLongArraysEquals(r, a, b, FloatMaxVectorTests::ADD);
2096 }
2097
2098 @Test(dataProvider = "floatBinaryOpMaskProvider")
2099 static void ADDFloatMaxVectorTestsBroadcastMaskedLongSmokeTest(IntFunction<float[]> fa, IntFunction<float[]> fb,
2100 IntFunction<boolean[]> fm) {
2101 float[] a = fa.apply(SPECIES.length());
2102 float[] b = fb.apply(SPECIES.length());
2103 float[] r = fr.apply(SPECIES.length());
2104 boolean[] mask = fm.apply(SPECIES.length());
2105 VectorMask<Float> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2106
2107 for (int i = 0; i < a.length; i += SPECIES.length()) {
2108 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2109 av.lanewise(VectorOperators.ADD, (long)b[i], vmask).intoArray(r, i);
2110 }
2111
2112 assertBroadcastLongArraysEquals(r, a, b, mask, FloatMaxVectorTests::ADD);
2113 }
2114
2115 static FloatVector bv_MIN = FloatVector.broadcast(SPECIES, (float)10);
2116
2117 @Test(dataProvider = "floatUnaryOpProvider")
2118 static void MINFloatMaxVectorTestsWithMemOp(IntFunction<float[]> fa) {
2119 float[] a = fa.apply(SPECIES.length());
2120 float[] r = fr.apply(SPECIES.length());
2121
2122 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2123 for (int i = 0; i < a.length; i += SPECIES.length()) {
2124 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2125 av.lanewise(VectorOperators.MIN, bv_MIN).intoArray(r, i);
2126 }
2127 }
2128
2129 assertArraysEquals(r, a, (float)10, FloatMaxVectorTests::MIN);
2130 }
2131
2132 static FloatVector bv_min = FloatVector.broadcast(SPECIES, (float)10);
2133
2134 @Test(dataProvider = "floatUnaryOpProvider")
2135 static void minFloatMaxVectorTestsWithMemOp(IntFunction<float[]> fa) {
2136 float[] a = fa.apply(SPECIES.length());
2137 float[] r = fr.apply(SPECIES.length());
2138
2139 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2140 for (int i = 0; i < a.length; i += SPECIES.length()) {
2141 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2142 av.min(bv_min).intoArray(r, i);
2143 }
2144 }
2145
2146 assertArraysEquals(r, a, (float)10, FloatMaxVectorTests::min);
2147 }
2148
2149 static FloatVector bv_MIN_M = FloatVector.broadcast(SPECIES, (float)10);
2150
2151 @Test(dataProvider = "floatUnaryOpMaskProvider")
2152 static void MINFloatMaxVectorTestsMaskedWithMemOp(IntFunction<float[]> fa, IntFunction<boolean[]> fm) {
2153 float[] a = fa.apply(SPECIES.length());
2154 float[] r = fr.apply(SPECIES.length());
2155 boolean[] mask = fm.apply(SPECIES.length());
2156 VectorMask<Float> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2157
2158 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2159 for (int i = 0; i < a.length; i += SPECIES.length()) {
2160 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2161 av.lanewise(VectorOperators.MIN, bv_MIN_M, vmask).intoArray(r, i);
2162 }
2163 }
2164
2165 assertArraysEquals(r, a, (float)10, mask, FloatMaxVectorTests::MIN);
2166 }
2167
2168 static FloatVector bv_MAX = FloatVector.broadcast(SPECIES, (float)10);
2169
2170 @Test(dataProvider = "floatUnaryOpProvider")
2171 static void MAXFloatMaxVectorTestsWithMemOp(IntFunction<float[]> fa) {
2172 float[] a = fa.apply(SPECIES.length());
2173 float[] r = fr.apply(SPECIES.length());
2174
2175 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2176 for (int i = 0; i < a.length; i += SPECIES.length()) {
2177 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2178 av.lanewise(VectorOperators.MAX, bv_MAX).intoArray(r, i);
2179 }
2180 }
2181
2182 assertArraysEquals(r, a, (float)10, FloatMaxVectorTests::MAX);
2183 }
2184
2185 static FloatVector bv_max = FloatVector.broadcast(SPECIES, (float)10);
2186
2187 @Test(dataProvider = "floatUnaryOpProvider")
2188 static void maxFloatMaxVectorTestsWithMemOp(IntFunction<float[]> fa) {
2189 float[] a = fa.apply(SPECIES.length());
2190 float[] r = fr.apply(SPECIES.length());
2191
2192 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2193 for (int i = 0; i < a.length; i += SPECIES.length()) {
2194 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2195 av.max(bv_max).intoArray(r, i);
2196 }
2197 }
2198
2199 assertArraysEquals(r, a, (float)10, FloatMaxVectorTests::max);
2200 }
2201
2202 static FloatVector bv_MAX_M = FloatVector.broadcast(SPECIES, (float)10);
2203
2204 @Test(dataProvider = "floatUnaryOpMaskProvider")
2205 static void MAXFloatMaxVectorTestsMaskedWithMemOp(IntFunction<float[]> fa, IntFunction<boolean[]> fm) {
2206 float[] a = fa.apply(SPECIES.length());
2207 float[] r = fr.apply(SPECIES.length());
2208 boolean[] mask = fm.apply(SPECIES.length());
2209 VectorMask<Float> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2210
2211 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2212 for (int i = 0; i < a.length; i += SPECIES.length()) {
2213 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2214 av.lanewise(VectorOperators.MAX, bv_MAX_M, vmask).intoArray(r, i);
2215 }
2216 }
2217
2218 assertArraysEquals(r, a, (float)10, mask, FloatMaxVectorTests::MAX);
2219 }
2220
2221 static float MIN(float a, float b) {
2222 return (float)(Math.min(a, b));
2223 }
2224
2225 @Test(dataProvider = "floatBinaryOpProvider")
2226 static void MINFloatMaxVectorTests(IntFunction<float[]> fa, IntFunction<float[]> fb) {
2227 float[] a = fa.apply(SPECIES.length());
2228 float[] b = fb.apply(SPECIES.length());
2229 float[] r = fr.apply(SPECIES.length());
2230
2231 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2232 for (int i = 0; i < a.length; i += SPECIES.length()) {
2233 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2234 FloatVector bv = FloatVector.fromArray(SPECIES, b, i);
|
1 /*
2 * Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
45 import org.testng.annotations.DataProvider;
46 import org.testng.annotations.Test;
47
48 import java.lang.Integer;
49 import java.util.List;
50 import java.util.Arrays;
51 import java.util.function.BiFunction;
52 import java.util.function.IntFunction;
53 import java.util.Objects;
54 import java.util.stream.Collectors;
55 import java.util.stream.Stream;
56
57 @Test
58 public class FloatMaxVectorTests extends AbstractVectorTest {
59
60 static final VectorSpecies<Float> SPECIES =
61 FloatVector.SPECIES_MAX;
62
63 static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
64
65 static FloatVector bcast_vec = FloatVector.broadcast(SPECIES, (float)10);
66
67 static VectorShape getMaxBit() {
68 return VectorShape.S_Max_BIT;
69 }
70
71 private static final int Max = 256; // juts so we can do N/Max
72
73 // for floating point addition reduction ops that may introduce rounding errors
74 private static final float RELATIVE_ROUNDING_ERROR_FACTOR_ADD = (float)10.0;
75
76 // for floating point multiplication reduction ops that may introduce rounding errors
77 private static final float RELATIVE_ROUNDING_ERROR_FACTOR_MUL = (float)50.0;
78
79 static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
80
81 static void assertArraysStrictlyEquals(float[] r, float[] a) {
82 for (int i = 0; i < a.length; i++) {
83 int ir = Float.floatToRawIntBits(r[i]);
84 int ia = Float.floatToRawIntBits(a[i]);
85 if (ir != ia) {
86 Assert.fail(String.format("at index #%d, expected = %08X, actual = %08X", i, ia, ir));
2097 assertBroadcastLongArraysEquals(r, a, b, FloatMaxVectorTests::ADD);
2098 }
2099
2100 @Test(dataProvider = "floatBinaryOpMaskProvider")
2101 static void ADDFloatMaxVectorTestsBroadcastMaskedLongSmokeTest(IntFunction<float[]> fa, IntFunction<float[]> fb,
2102 IntFunction<boolean[]> fm) {
2103 float[] a = fa.apply(SPECIES.length());
2104 float[] b = fb.apply(SPECIES.length());
2105 float[] r = fr.apply(SPECIES.length());
2106 boolean[] mask = fm.apply(SPECIES.length());
2107 VectorMask<Float> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2108
2109 for (int i = 0; i < a.length; i += SPECIES.length()) {
2110 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2111 av.lanewise(VectorOperators.ADD, (long)b[i], vmask).intoArray(r, i);
2112 }
2113
2114 assertBroadcastLongArraysEquals(r, a, b, mask, FloatMaxVectorTests::ADD);
2115 }
2116
2117 @Test(dataProvider = "floatUnaryOpProvider")
2118 static void MINFloatMaxVectorTestsWithMemOp(IntFunction<float[]> fa) {
2119 float[] a = fa.apply(SPECIES.length());
2120 float[] r = fr.apply(SPECIES.length());
2121
2122 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2123 for (int i = 0; i < a.length; i += SPECIES.length()) {
2124 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2125 av.lanewise(VectorOperators.MIN, bcast_vec).intoArray(r, i);
2126 }
2127 }
2128
2129 assertArraysEquals(r, a, (float)10, FloatMaxVectorTests::MIN);
2130 }
2131
2132 @Test(dataProvider = "floatUnaryOpProvider")
2133 static void minFloatMaxVectorTestsWithMemOp(IntFunction<float[]> fa) {
2134 float[] a = fa.apply(SPECIES.length());
2135 float[] r = fr.apply(SPECIES.length());
2136
2137 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2138 for (int i = 0; i < a.length; i += SPECIES.length()) {
2139 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2140 av.min(bcast_vec).intoArray(r, i);
2141 }
2142 }
2143
2144 assertArraysEquals(r, a, (float)10, FloatMaxVectorTests::min);
2145 }
2146
2147 @Test(dataProvider = "floatUnaryOpMaskProvider")
2148 static void MINFloatMaxVectorTestsMaskedWithMemOp(IntFunction<float[]> fa, IntFunction<boolean[]> fm) {
2149 float[] a = fa.apply(SPECIES.length());
2150 float[] r = fr.apply(SPECIES.length());
2151 boolean[] mask = fm.apply(SPECIES.length());
2152 VectorMask<Float> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2153
2154 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2155 for (int i = 0; i < a.length; i += SPECIES.length()) {
2156 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2157 av.lanewise(VectorOperators.MIN, bcast_vec, vmask).intoArray(r, i);
2158 }
2159 }
2160
2161 assertArraysEquals(r, a, (float)10, mask, FloatMaxVectorTests::MIN);
2162 }
2163
2164 @Test(dataProvider = "floatUnaryOpProvider")
2165 static void MAXFloatMaxVectorTestsWithMemOp(IntFunction<float[]> fa) {
2166 float[] a = fa.apply(SPECIES.length());
2167 float[] r = fr.apply(SPECIES.length());
2168
2169 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2170 for (int i = 0; i < a.length; i += SPECIES.length()) {
2171 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2172 av.lanewise(VectorOperators.MAX, bcast_vec).intoArray(r, i);
2173 }
2174 }
2175
2176 assertArraysEquals(r, a, (float)10, FloatMaxVectorTests::MAX);
2177 }
2178
2179 @Test(dataProvider = "floatUnaryOpProvider")
2180 static void maxFloatMaxVectorTestsWithMemOp(IntFunction<float[]> fa) {
2181 float[] a = fa.apply(SPECIES.length());
2182 float[] r = fr.apply(SPECIES.length());
2183
2184 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2185 for (int i = 0; i < a.length; i += SPECIES.length()) {
2186 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2187 av.max(bcast_vec).intoArray(r, i);
2188 }
2189 }
2190
2191 assertArraysEquals(r, a, (float)10, FloatMaxVectorTests::max);
2192 }
2193
2194 @Test(dataProvider = "floatUnaryOpMaskProvider")
2195 static void MAXFloatMaxVectorTestsMaskedWithMemOp(IntFunction<float[]> fa, IntFunction<boolean[]> fm) {
2196 float[] a = fa.apply(SPECIES.length());
2197 float[] r = fr.apply(SPECIES.length());
2198 boolean[] mask = fm.apply(SPECIES.length());
2199 VectorMask<Float> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2200
2201 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2202 for (int i = 0; i < a.length; i += SPECIES.length()) {
2203 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2204 av.lanewise(VectorOperators.MAX, bcast_vec, vmask).intoArray(r, i);
2205 }
2206 }
2207
2208 assertArraysEquals(r, a, (float)10, mask, FloatMaxVectorTests::MAX);
2209 }
2210
2211 static float MIN(float a, float b) {
2212 return (float)(Math.min(a, b));
2213 }
2214
2215 @Test(dataProvider = "floatBinaryOpProvider")
2216 static void MINFloatMaxVectorTests(IntFunction<float[]> fa, IntFunction<float[]> fb) {
2217 float[] a = fa.apply(SPECIES.length());
2218 float[] b = fb.apply(SPECIES.length());
2219 float[] r = fr.apply(SPECIES.length());
2220
2221 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2222 for (int i = 0; i < a.length; i += SPECIES.length()) {
2223 FloatVector av = FloatVector.fromArray(SPECIES, a, i);
2224 FloatVector bv = FloatVector.fromArray(SPECIES, b, i);
|