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 DoubleMaxVectorTests extends AbstractVectorTest {
59
60 static final VectorSpecies<Double> SPECIES =
61 DoubleVector.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 double RELATIVE_ROUNDING_ERROR_FACTOR_ADD = (double)10.0;
73
74 // for floating point multiplication reduction ops that may introduce rounding errors
75 private static final double RELATIVE_ROUNDING_ERROR_FACTOR_MUL = (double)50.0;
76
77 static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
78
79 static void assertArraysStrictlyEquals(double[] r, double[] a) {
80 for (int i = 0; i < a.length; i++) {
81 long ir = Double.doubleToRawLongBits(r[i]);
82 long ia = Double.doubleToRawLongBits(a[i]);
83 if (ir != ia) {
84 Assert.fail(String.format("at index #%d, expected = %016X, actual = %016X", i, ia, ir));
2084 assertBroadcastLongArraysEquals(r, a, b, DoubleMaxVectorTests::ADD);
2085 }
2086
2087 @Test(dataProvider = "doubleBinaryOpMaskProvider")
2088 static void ADDDoubleMaxVectorTestsBroadcastMaskedLongSmokeTest(IntFunction<double[]> fa, IntFunction<double[]> fb,
2089 IntFunction<boolean[]> fm) {
2090 double[] a = fa.apply(SPECIES.length());
2091 double[] b = fb.apply(SPECIES.length());
2092 double[] r = fr.apply(SPECIES.length());
2093 boolean[] mask = fm.apply(SPECIES.length());
2094 VectorMask<Double> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2095
2096 for (int i = 0; i < a.length; i += SPECIES.length()) {
2097 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2098 av.lanewise(VectorOperators.ADD, (long)b[i], vmask).intoArray(r, i);
2099 }
2100
2101 assertBroadcastLongArraysEquals(r, a, b, mask, DoubleMaxVectorTests::ADD);
2102 }
2103
2104 static DoubleVector bv_MIN = DoubleVector.broadcast(SPECIES, (double)10);
2105
2106 @Test(dataProvider = "doubleUnaryOpProvider")
2107 static void MINDoubleMaxVectorTestsWithMemOp(IntFunction<double[]> fa) {
2108 double[] a = fa.apply(SPECIES.length());
2109 double[] r = fr.apply(SPECIES.length());
2110
2111 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2112 for (int i = 0; i < a.length; i += SPECIES.length()) {
2113 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2114 av.lanewise(VectorOperators.MIN, bv_MIN).intoArray(r, i);
2115 }
2116 }
2117
2118 assertArraysEquals(r, a, (double)10, DoubleMaxVectorTests::MIN);
2119 }
2120
2121 static DoubleVector bv_min = DoubleVector.broadcast(SPECIES, (double)10);
2122
2123 @Test(dataProvider = "doubleUnaryOpProvider")
2124 static void minDoubleMaxVectorTestsWithMemOp(IntFunction<double[]> fa) {
2125 double[] a = fa.apply(SPECIES.length());
2126 double[] r = fr.apply(SPECIES.length());
2127
2128 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2129 for (int i = 0; i < a.length; i += SPECIES.length()) {
2130 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2131 av.min(bv_min).intoArray(r, i);
2132 }
2133 }
2134
2135 assertArraysEquals(r, a, (double)10, DoubleMaxVectorTests::min);
2136 }
2137
2138 static DoubleVector bv_MIN_M = DoubleVector.broadcast(SPECIES, (double)10);
2139
2140 @Test(dataProvider = "doubleUnaryOpMaskProvider")
2141 static void MINDoubleMaxVectorTestsMaskedWithMemOp(IntFunction<double[]> fa, IntFunction<boolean[]> fm) {
2142 double[] a = fa.apply(SPECIES.length());
2143 double[] r = fr.apply(SPECIES.length());
2144 boolean[] mask = fm.apply(SPECIES.length());
2145 VectorMask<Double> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2146
2147 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2148 for (int i = 0; i < a.length; i += SPECIES.length()) {
2149 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2150 av.lanewise(VectorOperators.MIN, bv_MIN_M, vmask).intoArray(r, i);
2151 }
2152 }
2153
2154 assertArraysEquals(r, a, (double)10, mask, DoubleMaxVectorTests::MIN);
2155 }
2156
2157 static DoubleVector bv_MAX = DoubleVector.broadcast(SPECIES, (double)10);
2158
2159 @Test(dataProvider = "doubleUnaryOpProvider")
2160 static void MAXDoubleMaxVectorTestsWithMemOp(IntFunction<double[]> fa) {
2161 double[] a = fa.apply(SPECIES.length());
2162 double[] r = fr.apply(SPECIES.length());
2163
2164 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2165 for (int i = 0; i < a.length; i += SPECIES.length()) {
2166 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2167 av.lanewise(VectorOperators.MAX, bv_MAX).intoArray(r, i);
2168 }
2169 }
2170
2171 assertArraysEquals(r, a, (double)10, DoubleMaxVectorTests::MAX);
2172 }
2173
2174 static DoubleVector bv_max = DoubleVector.broadcast(SPECIES, (double)10);
2175
2176 @Test(dataProvider = "doubleUnaryOpProvider")
2177 static void maxDoubleMaxVectorTestsWithMemOp(IntFunction<double[]> fa) {
2178 double[] a = fa.apply(SPECIES.length());
2179 double[] r = fr.apply(SPECIES.length());
2180
2181 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2182 for (int i = 0; i < a.length; i += SPECIES.length()) {
2183 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2184 av.max(bv_max).intoArray(r, i);
2185 }
2186 }
2187
2188 assertArraysEquals(r, a, (double)10, DoubleMaxVectorTests::max);
2189 }
2190
2191 static DoubleVector bv_MAX_M = DoubleVector.broadcast(SPECIES, (double)10);
2192
2193 @Test(dataProvider = "doubleUnaryOpMaskProvider")
2194 static void MAXDoubleMaxVectorTestsMaskedWithMemOp(IntFunction<double[]> fa, IntFunction<boolean[]> fm) {
2195 double[] a = fa.apply(SPECIES.length());
2196 double[] r = fr.apply(SPECIES.length());
2197 boolean[] mask = fm.apply(SPECIES.length());
2198 VectorMask<Double> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2199
2200 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2201 for (int i = 0; i < a.length; i += SPECIES.length()) {
2202 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2203 av.lanewise(VectorOperators.MAX, bv_MAX_M, vmask).intoArray(r, i);
2204 }
2205 }
2206
2207 assertArraysEquals(r, a, (double)10, mask, DoubleMaxVectorTests::MAX);
2208 }
2209
2210 static double MIN(double a, double b) {
2211 return (double)(Math.min(a, b));
2212 }
2213
2214 @Test(dataProvider = "doubleBinaryOpProvider")
2215 static void MINDoubleMaxVectorTests(IntFunction<double[]> fa, IntFunction<double[]> fb) {
2216 double[] a = fa.apply(SPECIES.length());
2217 double[] b = fb.apply(SPECIES.length());
2218 double[] r = fr.apply(SPECIES.length());
2219
2220 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2221 for (int i = 0; i < a.length; i += SPECIES.length()) {
2222 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2223 DoubleVector bv = DoubleVector.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 DoubleMaxVectorTests extends AbstractVectorTest {
59
60 static final VectorSpecies<Double> SPECIES =
61 DoubleVector.SPECIES_MAX;
62
63 static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
64
65 static DoubleVector bcast_vec = DoubleVector.broadcast(SPECIES, (double)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 double RELATIVE_ROUNDING_ERROR_FACTOR_ADD = (double)10.0;
75
76 // for floating point multiplication reduction ops that may introduce rounding errors
77 private static final double RELATIVE_ROUNDING_ERROR_FACTOR_MUL = (double)50.0;
78
79 static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
80
81 static void assertArraysStrictlyEquals(double[] r, double[] a) {
82 for (int i = 0; i < a.length; i++) {
83 long ir = Double.doubleToRawLongBits(r[i]);
84 long ia = Double.doubleToRawLongBits(a[i]);
85 if (ir != ia) {
86 Assert.fail(String.format("at index #%d, expected = %016X, actual = %016X", i, ia, ir));
2086 assertBroadcastLongArraysEquals(r, a, b, DoubleMaxVectorTests::ADD);
2087 }
2088
2089 @Test(dataProvider = "doubleBinaryOpMaskProvider")
2090 static void ADDDoubleMaxVectorTestsBroadcastMaskedLongSmokeTest(IntFunction<double[]> fa, IntFunction<double[]> fb,
2091 IntFunction<boolean[]> fm) {
2092 double[] a = fa.apply(SPECIES.length());
2093 double[] b = fb.apply(SPECIES.length());
2094 double[] r = fr.apply(SPECIES.length());
2095 boolean[] mask = fm.apply(SPECIES.length());
2096 VectorMask<Double> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2097
2098 for (int i = 0; i < a.length; i += SPECIES.length()) {
2099 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2100 av.lanewise(VectorOperators.ADD, (long)b[i], vmask).intoArray(r, i);
2101 }
2102
2103 assertBroadcastLongArraysEquals(r, a, b, mask, DoubleMaxVectorTests::ADD);
2104 }
2105
2106 @Test(dataProvider = "doubleUnaryOpProvider")
2107 static void MINDoubleMaxVectorTestsWithMemOp(IntFunction<double[]> fa) {
2108 double[] a = fa.apply(SPECIES.length());
2109 double[] r = fr.apply(SPECIES.length());
2110
2111 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2112 for (int i = 0; i < a.length; i += SPECIES.length()) {
2113 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2114 av.lanewise(VectorOperators.MIN, bcast_vec).intoArray(r, i);
2115 }
2116 }
2117
2118 assertArraysEquals(r, a, (double)10, DoubleMaxVectorTests::MIN);
2119 }
2120
2121 @Test(dataProvider = "doubleUnaryOpProvider")
2122 static void minDoubleMaxVectorTestsWithMemOp(IntFunction<double[]> fa) {
2123 double[] a = fa.apply(SPECIES.length());
2124 double[] r = fr.apply(SPECIES.length());
2125
2126 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2127 for (int i = 0; i < a.length; i += SPECIES.length()) {
2128 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2129 av.min(bcast_vec).intoArray(r, i);
2130 }
2131 }
2132
2133 assertArraysEquals(r, a, (double)10, DoubleMaxVectorTests::min);
2134 }
2135
2136 @Test(dataProvider = "doubleUnaryOpMaskProvider")
2137 static void MINDoubleMaxVectorTestsMaskedWithMemOp(IntFunction<double[]> fa, IntFunction<boolean[]> fm) {
2138 double[] a = fa.apply(SPECIES.length());
2139 double[] r = fr.apply(SPECIES.length());
2140 boolean[] mask = fm.apply(SPECIES.length());
2141 VectorMask<Double> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2142
2143 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2144 for (int i = 0; i < a.length; i += SPECIES.length()) {
2145 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2146 av.lanewise(VectorOperators.MIN, bcast_vec, vmask).intoArray(r, i);
2147 }
2148 }
2149
2150 assertArraysEquals(r, a, (double)10, mask, DoubleMaxVectorTests::MIN);
2151 }
2152
2153 @Test(dataProvider = "doubleUnaryOpProvider")
2154 static void MAXDoubleMaxVectorTestsWithMemOp(IntFunction<double[]> fa) {
2155 double[] a = fa.apply(SPECIES.length());
2156 double[] r = fr.apply(SPECIES.length());
2157
2158 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2159 for (int i = 0; i < a.length; i += SPECIES.length()) {
2160 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2161 av.lanewise(VectorOperators.MAX, bcast_vec).intoArray(r, i);
2162 }
2163 }
2164
2165 assertArraysEquals(r, a, (double)10, DoubleMaxVectorTests::MAX);
2166 }
2167
2168 @Test(dataProvider = "doubleUnaryOpProvider")
2169 static void maxDoubleMaxVectorTestsWithMemOp(IntFunction<double[]> fa) {
2170 double[] a = fa.apply(SPECIES.length());
2171 double[] r = fr.apply(SPECIES.length());
2172
2173 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2174 for (int i = 0; i < a.length; i += SPECIES.length()) {
2175 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2176 av.max(bcast_vec).intoArray(r, i);
2177 }
2178 }
2179
2180 assertArraysEquals(r, a, (double)10, DoubleMaxVectorTests::max);
2181 }
2182
2183 @Test(dataProvider = "doubleUnaryOpMaskProvider")
2184 static void MAXDoubleMaxVectorTestsMaskedWithMemOp(IntFunction<double[]> fa, IntFunction<boolean[]> fm) {
2185 double[] a = fa.apply(SPECIES.length());
2186 double[] r = fr.apply(SPECIES.length());
2187 boolean[] mask = fm.apply(SPECIES.length());
2188 VectorMask<Double> vmask = VectorMask.fromArray(SPECIES, mask, 0);
2189
2190 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2191 for (int i = 0; i < a.length; i += SPECIES.length()) {
2192 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2193 av.lanewise(VectorOperators.MAX, bcast_vec, vmask).intoArray(r, i);
2194 }
2195 }
2196
2197 assertArraysEquals(r, a, (double)10, mask, DoubleMaxVectorTests::MAX);
2198 }
2199
2200 static double MIN(double a, double b) {
2201 return (double)(Math.min(a, b));
2202 }
2203
2204 @Test(dataProvider = "doubleBinaryOpProvider")
2205 static void MINDoubleMaxVectorTests(IntFunction<double[]> fa, IntFunction<double[]> fb) {
2206 double[] a = fa.apply(SPECIES.length());
2207 double[] b = fb.apply(SPECIES.length());
2208 double[] r = fr.apply(SPECIES.length());
2209
2210 for (int ic = 0; ic < INVOC_COUNT; ic++) {
2211 for (int i = 0; i < a.length; i += SPECIES.length()) {
2212 DoubleVector av = DoubleVector.fromArray(SPECIES, a, i);
2213 DoubleVector bv = DoubleVector.fromArray(SPECIES, b, i);
|