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 */
46 import org.testng.annotations.DataProvider;
47 import org.testng.annotations.Test;
48
49 import java.lang.Integer;
50 import java.util.List;
51 import java.util.Arrays;
52 import java.util.function.BiFunction;
53 import java.util.function.IntFunction;
54 import java.util.Objects;
55 import java.util.stream.Collectors;
56 import java.util.stream.Stream;
57
58 @Test
59 public class ByteMaxVectorTests extends AbstractVectorTest {
60
61 static final VectorSpecies<Byte> SPECIES =
62 ByteVector.SPECIES_MAX;
63
64 static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
65
66 static VectorShape getMaxBit() {
67 return VectorShape.S_Max_BIT;
68 }
69
70 private static final int Max = 256; // juts so we can do N/Max
71
72 private static final byte CONST_SHIFT = Byte.SIZE / 2;
73
74 static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
75
76 static void assertArraysStrictlyEquals(byte[] r, byte[] a) {
77 for (int i = 0; i < a.length; i++) {
78 if (r[i] != a[i]) {
79 Assert.fail("at index #" + i + ", expected = " + a[i] + ", actual = " + r[i]);
80 }
81 }
82 }
83
84 interface FUnOp {
85 byte apply(byte a);
3020
3021 @Test(dataProvider = "byteUnaryOpMaskProvider")
3022 static void ROLByteMaxVectorTestsScalarShiftMaskedConst(IntFunction<byte[]> fa,
3023 IntFunction<boolean[]> fm) {
3024 byte[] a = fa.apply(SPECIES.length());
3025 byte[] r = fr.apply(SPECIES.length());
3026 boolean[] mask = fm.apply(SPECIES.length());
3027 VectorMask<Byte> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3028
3029 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3030 for (int i = 0; i < a.length; i += SPECIES.length()) {
3031 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3032 av.lanewise(VectorOperators.ROL, CONST_SHIFT, vmask).intoArray(r, i);
3033 }
3034 }
3035
3036 assertShiftConstEquals(r, a, mask, ByteMaxVectorTests::ROL_binary_const);
3037 }
3038
3039
3040 static ByteVector bv_MIN = ByteVector.broadcast(SPECIES, (byte)10);
3041
3042 @Test(dataProvider = "byteUnaryOpProvider")
3043 static void MINByteMaxVectorTestsWithMemOp(IntFunction<byte[]> fa) {
3044 byte[] a = fa.apply(SPECIES.length());
3045 byte[] r = fr.apply(SPECIES.length());
3046
3047 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3048 for (int i = 0; i < a.length; i += SPECIES.length()) {
3049 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3050 av.lanewise(VectorOperators.MIN, bv_MIN).intoArray(r, i);
3051 }
3052 }
3053
3054 assertArraysEquals(r, a, (byte)10, ByteMaxVectorTests::MIN);
3055 }
3056
3057 static ByteVector bv_min = ByteVector.broadcast(SPECIES, (byte)10);
3058
3059 @Test(dataProvider = "byteUnaryOpProvider")
3060 static void minByteMaxVectorTestsWithMemOp(IntFunction<byte[]> fa) {
3061 byte[] a = fa.apply(SPECIES.length());
3062 byte[] r = fr.apply(SPECIES.length());
3063
3064 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3065 for (int i = 0; i < a.length; i += SPECIES.length()) {
3066 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3067 av.min(bv_min).intoArray(r, i);
3068 }
3069 }
3070
3071 assertArraysEquals(r, a, (byte)10, ByteMaxVectorTests::min);
3072 }
3073
3074 static ByteVector bv_MIN_M = ByteVector.broadcast(SPECIES, (byte)10);
3075
3076 @Test(dataProvider = "byteUnaryOpMaskProvider")
3077 static void MINByteMaxVectorTestsMaskedWithMemOp(IntFunction<byte[]> fa, IntFunction<boolean[]> fm) {
3078 byte[] a = fa.apply(SPECIES.length());
3079 byte[] r = fr.apply(SPECIES.length());
3080 boolean[] mask = fm.apply(SPECIES.length());
3081 VectorMask<Byte> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3082
3083 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3084 for (int i = 0; i < a.length; i += SPECIES.length()) {
3085 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3086 av.lanewise(VectorOperators.MIN, bv_MIN_M, vmask).intoArray(r, i);
3087 }
3088 }
3089
3090 assertArraysEquals(r, a, (byte)10, mask, ByteMaxVectorTests::MIN);
3091 }
3092
3093 static ByteVector bv_MAX = ByteVector.broadcast(SPECIES, (byte)10);
3094
3095 @Test(dataProvider = "byteUnaryOpProvider")
3096 static void MAXByteMaxVectorTestsWithMemOp(IntFunction<byte[]> fa) {
3097 byte[] a = fa.apply(SPECIES.length());
3098 byte[] r = fr.apply(SPECIES.length());
3099
3100 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3101 for (int i = 0; i < a.length; i += SPECIES.length()) {
3102 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3103 av.lanewise(VectorOperators.MAX, bv_MAX).intoArray(r, i);
3104 }
3105 }
3106
3107 assertArraysEquals(r, a, (byte)10, ByteMaxVectorTests::MAX);
3108 }
3109
3110 static ByteVector bv_max = ByteVector.broadcast(SPECIES, (byte)10);
3111
3112 @Test(dataProvider = "byteUnaryOpProvider")
3113 static void maxByteMaxVectorTestsWithMemOp(IntFunction<byte[]> fa) {
3114 byte[] a = fa.apply(SPECIES.length());
3115 byte[] r = fr.apply(SPECIES.length());
3116
3117 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3118 for (int i = 0; i < a.length; i += SPECIES.length()) {
3119 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3120 av.max(bv_max).intoArray(r, i);
3121 }
3122 }
3123
3124 assertArraysEquals(r, a, (byte)10, ByteMaxVectorTests::max);
3125 }
3126
3127 static ByteVector bv_MAX_M = ByteVector.broadcast(SPECIES, (byte)10);
3128
3129 @Test(dataProvider = "byteUnaryOpMaskProvider")
3130 static void MAXByteMaxVectorTestsMaskedWithMemOp(IntFunction<byte[]> fa, IntFunction<boolean[]> fm) {
3131 byte[] a = fa.apply(SPECIES.length());
3132 byte[] r = fr.apply(SPECIES.length());
3133 boolean[] mask = fm.apply(SPECIES.length());
3134 VectorMask<Byte> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3135
3136 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3137 for (int i = 0; i < a.length; i += SPECIES.length()) {
3138 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3139 av.lanewise(VectorOperators.MAX, bv_MAX_M, vmask).intoArray(r, i);
3140 }
3141 }
3142
3143 assertArraysEquals(r, a, (byte)10, mask, ByteMaxVectorTests::MAX);
3144 }
3145
3146 static byte MIN(byte a, byte b) {
3147 return (byte)(Math.min(a, b));
3148 }
3149
3150 @Test(dataProvider = "byteBinaryOpProvider")
3151 static void MINByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) {
3152 byte[] a = fa.apply(SPECIES.length());
3153 byte[] b = fb.apply(SPECIES.length());
3154 byte[] r = fr.apply(SPECIES.length());
3155
3156 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3157 for (int i = 0; i < a.length; i += SPECIES.length()) {
3158 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3159 ByteVector bv = ByteVector.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 */
46 import org.testng.annotations.DataProvider;
47 import org.testng.annotations.Test;
48
49 import java.lang.Integer;
50 import java.util.List;
51 import java.util.Arrays;
52 import java.util.function.BiFunction;
53 import java.util.function.IntFunction;
54 import java.util.Objects;
55 import java.util.stream.Collectors;
56 import java.util.stream.Stream;
57
58 @Test
59 public class ByteMaxVectorTests extends AbstractVectorTest {
60
61 static final VectorSpecies<Byte> SPECIES =
62 ByteVector.SPECIES_MAX;
63
64 static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
65
66 static ByteVector bcast_vec = ByteVector.broadcast(SPECIES, (byte)10);
67
68 static VectorShape getMaxBit() {
69 return VectorShape.S_Max_BIT;
70 }
71
72 private static final int Max = 256; // juts so we can do N/Max
73
74 private static final byte CONST_SHIFT = Byte.SIZE / 2;
75
76 static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
77
78 static void assertArraysStrictlyEquals(byte[] r, byte[] a) {
79 for (int i = 0; i < a.length; i++) {
80 if (r[i] != a[i]) {
81 Assert.fail("at index #" + i + ", expected = " + a[i] + ", actual = " + r[i]);
82 }
83 }
84 }
85
86 interface FUnOp {
87 byte apply(byte a);
3022
3023 @Test(dataProvider = "byteUnaryOpMaskProvider")
3024 static void ROLByteMaxVectorTestsScalarShiftMaskedConst(IntFunction<byte[]> fa,
3025 IntFunction<boolean[]> fm) {
3026 byte[] a = fa.apply(SPECIES.length());
3027 byte[] r = fr.apply(SPECIES.length());
3028 boolean[] mask = fm.apply(SPECIES.length());
3029 VectorMask<Byte> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3030
3031 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3032 for (int i = 0; i < a.length; i += SPECIES.length()) {
3033 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3034 av.lanewise(VectorOperators.ROL, CONST_SHIFT, vmask).intoArray(r, i);
3035 }
3036 }
3037
3038 assertShiftConstEquals(r, a, mask, ByteMaxVectorTests::ROL_binary_const);
3039 }
3040
3041
3042 @Test(dataProvider = "byteUnaryOpProvider")
3043 static void MINByteMaxVectorTestsWithMemOp(IntFunction<byte[]> fa) {
3044 byte[] a = fa.apply(SPECIES.length());
3045 byte[] r = fr.apply(SPECIES.length());
3046
3047 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3048 for (int i = 0; i < a.length; i += SPECIES.length()) {
3049 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3050 av.lanewise(VectorOperators.MIN, bcast_vec).intoArray(r, i);
3051 }
3052 }
3053
3054 assertArraysEquals(r, a, (byte)10, ByteMaxVectorTests::MIN);
3055 }
3056
3057 @Test(dataProvider = "byteUnaryOpProvider")
3058 static void minByteMaxVectorTestsWithMemOp(IntFunction<byte[]> fa) {
3059 byte[] a = fa.apply(SPECIES.length());
3060 byte[] r = fr.apply(SPECIES.length());
3061
3062 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3063 for (int i = 0; i < a.length; i += SPECIES.length()) {
3064 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3065 av.min(bcast_vec).intoArray(r, i);
3066 }
3067 }
3068
3069 assertArraysEquals(r, a, (byte)10, ByteMaxVectorTests::min);
3070 }
3071
3072 @Test(dataProvider = "byteUnaryOpMaskProvider")
3073 static void MINByteMaxVectorTestsMaskedWithMemOp(IntFunction<byte[]> fa, IntFunction<boolean[]> fm) {
3074 byte[] a = fa.apply(SPECIES.length());
3075 byte[] r = fr.apply(SPECIES.length());
3076 boolean[] mask = fm.apply(SPECIES.length());
3077 VectorMask<Byte> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3078
3079 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3080 for (int i = 0; i < a.length; i += SPECIES.length()) {
3081 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3082 av.lanewise(VectorOperators.MIN, bcast_vec, vmask).intoArray(r, i);
3083 }
3084 }
3085
3086 assertArraysEquals(r, a, (byte)10, mask, ByteMaxVectorTests::MIN);
3087 }
3088
3089 @Test(dataProvider = "byteUnaryOpProvider")
3090 static void MAXByteMaxVectorTestsWithMemOp(IntFunction<byte[]> fa) {
3091 byte[] a = fa.apply(SPECIES.length());
3092 byte[] r = fr.apply(SPECIES.length());
3093
3094 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3095 for (int i = 0; i < a.length; i += SPECIES.length()) {
3096 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3097 av.lanewise(VectorOperators.MAX, bcast_vec).intoArray(r, i);
3098 }
3099 }
3100
3101 assertArraysEquals(r, a, (byte)10, ByteMaxVectorTests::MAX);
3102 }
3103
3104 @Test(dataProvider = "byteUnaryOpProvider")
3105 static void maxByteMaxVectorTestsWithMemOp(IntFunction<byte[]> fa) {
3106 byte[] a = fa.apply(SPECIES.length());
3107 byte[] r = fr.apply(SPECIES.length());
3108
3109 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3110 for (int i = 0; i < a.length; i += SPECIES.length()) {
3111 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3112 av.max(bcast_vec).intoArray(r, i);
3113 }
3114 }
3115
3116 assertArraysEquals(r, a, (byte)10, ByteMaxVectorTests::max);
3117 }
3118
3119 @Test(dataProvider = "byteUnaryOpMaskProvider")
3120 static void MAXByteMaxVectorTestsMaskedWithMemOp(IntFunction<byte[]> fa, IntFunction<boolean[]> fm) {
3121 byte[] a = fa.apply(SPECIES.length());
3122 byte[] r = fr.apply(SPECIES.length());
3123 boolean[] mask = fm.apply(SPECIES.length());
3124 VectorMask<Byte> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3125
3126 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3127 for (int i = 0; i < a.length; i += SPECIES.length()) {
3128 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3129 av.lanewise(VectorOperators.MAX, bcast_vec, vmask).intoArray(r, i);
3130 }
3131 }
3132
3133 assertArraysEquals(r, a, (byte)10, mask, ByteMaxVectorTests::MAX);
3134 }
3135
3136 static byte MIN(byte a, byte b) {
3137 return (byte)(Math.min(a, b));
3138 }
3139
3140 @Test(dataProvider = "byteBinaryOpProvider")
3141 static void MINByteMaxVectorTests(IntFunction<byte[]> fa, IntFunction<byte[]> fb) {
3142 byte[] a = fa.apply(SPECIES.length());
3143 byte[] b = fb.apply(SPECIES.length());
3144 byte[] r = fr.apply(SPECIES.length());
3145
3146 for (int ic = 0; ic < INVOC_COUNT; ic++) {
3147 for (int i = 0; i < a.length; i += SPECIES.length()) {
3148 ByteVector av = ByteVector.fromArray(SPECIES, a, i);
3149 ByteVector bv = ByteVector.fromArray(SPECIES, b, i);
|