< prev index next >

test/jdk/jdk/incubator/vector/IntMaxVectorTests.java

Print this page

   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 IntMaxVectorTests extends AbstractVectorTest {
  60 
  61     static final VectorSpecies<Integer> SPECIES =
  62                 IntVector.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 int CONST_SHIFT = Integer.SIZE / 2;
  73 
  74     static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
  75 
  76     static void assertArraysStrictlyEquals(int[] r, int[] 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         int apply(int a);

3064 
3065     @Test(dataProvider = "intUnaryOpMaskProvider")
3066     static void ROLIntMaxVectorTestsScalarShiftMaskedConst(IntFunction<int[]> fa,
3067                                           IntFunction<boolean[]> fm) {
3068         int[] a = fa.apply(SPECIES.length());
3069         int[] r = fr.apply(SPECIES.length());
3070         boolean[] mask = fm.apply(SPECIES.length());
3071         VectorMask<Integer> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3072 
3073         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3074             for (int i = 0; i < a.length; i += SPECIES.length()) {
3075                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3076                 av.lanewise(VectorOperators.ROL, CONST_SHIFT, vmask).intoArray(r, i);
3077             }
3078         }
3079 
3080         assertShiftConstEquals(r, a, mask, IntMaxVectorTests::ROL_binary_const);
3081     }
3082 
3083 
3084     static IntVector bv_MIN = IntVector.broadcast(SPECIES, (int)10);
3085 
3086     @Test(dataProvider = "intUnaryOpProvider")
3087     static void MINIntMaxVectorTestsWithMemOp(IntFunction<int[]> fa) {
3088         int[] a = fa.apply(SPECIES.length());
3089         int[] r = fr.apply(SPECIES.length());
3090 
3091         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3092             for (int i = 0; i < a.length; i += SPECIES.length()) {
3093                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3094                 av.lanewise(VectorOperators.MIN, bv_MIN).intoArray(r, i);
3095             }
3096         }
3097 
3098         assertArraysEquals(r, a, (int)10, IntMaxVectorTests::MIN);
3099     }
3100 
3101     static IntVector bv_min = IntVector.broadcast(SPECIES, (int)10);
3102 
3103     @Test(dataProvider = "intUnaryOpProvider")
3104     static void minIntMaxVectorTestsWithMemOp(IntFunction<int[]> fa) {
3105         int[] a = fa.apply(SPECIES.length());
3106         int[] r = fr.apply(SPECIES.length());
3107 
3108         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3109             for (int i = 0; i < a.length; i += SPECIES.length()) {
3110                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3111                 av.min(bv_min).intoArray(r, i);
3112             }
3113         }
3114 
3115         assertArraysEquals(r, a, (int)10, IntMaxVectorTests::min);
3116     }
3117 
3118     static IntVector bv_MIN_M = IntVector.broadcast(SPECIES, (int)10);
3119 
3120     @Test(dataProvider = "intUnaryOpMaskProvider")
3121     static void MINIntMaxVectorTestsMaskedWithMemOp(IntFunction<int[]> fa, IntFunction<boolean[]> fm) {
3122         int[] a = fa.apply(SPECIES.length());
3123         int[] r = fr.apply(SPECIES.length());
3124         boolean[] mask = fm.apply(SPECIES.length());
3125         VectorMask<Integer> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3126 
3127         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3128             for (int i = 0; i < a.length; i += SPECIES.length()) {
3129                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3130                 av.lanewise(VectorOperators.MIN, bv_MIN_M, vmask).intoArray(r, i);
3131             }
3132         }
3133 
3134         assertArraysEquals(r, a, (int)10, mask, IntMaxVectorTests::MIN);
3135     }
3136 
3137     static IntVector bv_MAX = IntVector.broadcast(SPECIES, (int)10);
3138 
3139     @Test(dataProvider = "intUnaryOpProvider")
3140     static void MAXIntMaxVectorTestsWithMemOp(IntFunction<int[]> fa) {
3141         int[] a = fa.apply(SPECIES.length());
3142         int[] r = fr.apply(SPECIES.length());
3143 
3144         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3145             for (int i = 0; i < a.length; i += SPECIES.length()) {
3146                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3147                 av.lanewise(VectorOperators.MAX, bv_MAX).intoArray(r, i);
3148             }
3149         }
3150 
3151         assertArraysEquals(r, a, (int)10, IntMaxVectorTests::MAX);
3152     }
3153 
3154     static IntVector bv_max = IntVector.broadcast(SPECIES, (int)10);
3155 
3156     @Test(dataProvider = "intUnaryOpProvider")
3157     static void maxIntMaxVectorTestsWithMemOp(IntFunction<int[]> fa) {
3158         int[] a = fa.apply(SPECIES.length());
3159         int[] r = fr.apply(SPECIES.length());
3160 
3161         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3162             for (int i = 0; i < a.length; i += SPECIES.length()) {
3163                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3164                 av.max(bv_max).intoArray(r, i);
3165             }
3166         }
3167 
3168         assertArraysEquals(r, a, (int)10, IntMaxVectorTests::max);
3169     }
3170 
3171     static IntVector bv_MAX_M = IntVector.broadcast(SPECIES, (int)10);
3172 
3173     @Test(dataProvider = "intUnaryOpMaskProvider")
3174     static void MAXIntMaxVectorTestsMaskedWithMemOp(IntFunction<int[]> fa, IntFunction<boolean[]> fm) {
3175         int[] a = fa.apply(SPECIES.length());
3176         int[] r = fr.apply(SPECIES.length());
3177         boolean[] mask = fm.apply(SPECIES.length());
3178         VectorMask<Integer> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3179 
3180         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3181             for (int i = 0; i < a.length; i += SPECIES.length()) {
3182                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3183                 av.lanewise(VectorOperators.MAX, bv_MAX_M, vmask).intoArray(r, i);
3184             }
3185         }
3186 
3187         assertArraysEquals(r, a, (int)10, mask, IntMaxVectorTests::MAX);
3188     }
3189 
3190     static int MIN(int a, int b) {
3191         return (int)(Math.min(a, b));
3192     }
3193 
3194     @Test(dataProvider = "intBinaryOpProvider")
3195     static void MINIntMaxVectorTests(IntFunction<int[]> fa, IntFunction<int[]> fb) {
3196         int[] a = fa.apply(SPECIES.length());
3197         int[] b = fb.apply(SPECIES.length());
3198         int[] r = fr.apply(SPECIES.length());
3199 
3200         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3201             for (int i = 0; i < a.length; i += SPECIES.length()) {
3202                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3203                 IntVector bv = IntVector.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 IntMaxVectorTests extends AbstractVectorTest {
  60 
  61     static final VectorSpecies<Integer> SPECIES =
  62                 IntVector.SPECIES_MAX;
  63 
  64     static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
  65 
  66     static IntVector bcast_vec = IntVector.broadcast(SPECIES, (int)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 int CONST_SHIFT = Integer.SIZE / 2;
  75 
  76     static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
  77 
  78     static void assertArraysStrictlyEquals(int[] r, int[] 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         int apply(int a);

3066 
3067     @Test(dataProvider = "intUnaryOpMaskProvider")
3068     static void ROLIntMaxVectorTestsScalarShiftMaskedConst(IntFunction<int[]> fa,
3069                                           IntFunction<boolean[]> fm) {
3070         int[] a = fa.apply(SPECIES.length());
3071         int[] r = fr.apply(SPECIES.length());
3072         boolean[] mask = fm.apply(SPECIES.length());
3073         VectorMask<Integer> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3074 
3075         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3076             for (int i = 0; i < a.length; i += SPECIES.length()) {
3077                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3078                 av.lanewise(VectorOperators.ROL, CONST_SHIFT, vmask).intoArray(r, i);
3079             }
3080         }
3081 
3082         assertShiftConstEquals(r, a, mask, IntMaxVectorTests::ROL_binary_const);
3083     }
3084 
3085 


3086     @Test(dataProvider = "intUnaryOpProvider")
3087     static void MINIntMaxVectorTestsWithMemOp(IntFunction<int[]> fa) {
3088         int[] a = fa.apply(SPECIES.length());
3089         int[] r = fr.apply(SPECIES.length());
3090 
3091         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3092             for (int i = 0; i < a.length; i += SPECIES.length()) {
3093                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3094                 av.lanewise(VectorOperators.MIN, bcast_vec).intoArray(r, i);
3095             }
3096         }
3097 
3098         assertArraysEquals(r, a, (int)10, IntMaxVectorTests::MIN);
3099     }
3100 


3101     @Test(dataProvider = "intUnaryOpProvider")
3102     static void minIntMaxVectorTestsWithMemOp(IntFunction<int[]> fa) {
3103         int[] a = fa.apply(SPECIES.length());
3104         int[] r = fr.apply(SPECIES.length());
3105 
3106         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3107             for (int i = 0; i < a.length; i += SPECIES.length()) {
3108                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3109                 av.min(bcast_vec).intoArray(r, i);
3110             }
3111         }
3112 
3113         assertArraysEquals(r, a, (int)10, IntMaxVectorTests::min);
3114     }
3115 


3116     @Test(dataProvider = "intUnaryOpMaskProvider")
3117     static void MINIntMaxVectorTestsMaskedWithMemOp(IntFunction<int[]> fa, IntFunction<boolean[]> fm) {
3118         int[] a = fa.apply(SPECIES.length());
3119         int[] r = fr.apply(SPECIES.length());
3120         boolean[] mask = fm.apply(SPECIES.length());
3121         VectorMask<Integer> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3122 
3123         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3124             for (int i = 0; i < a.length; i += SPECIES.length()) {
3125                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3126                 av.lanewise(VectorOperators.MIN, bcast_vec, vmask).intoArray(r, i);
3127             }
3128         }
3129 
3130         assertArraysEquals(r, a, (int)10, mask, IntMaxVectorTests::MIN);
3131     }
3132 


3133     @Test(dataProvider = "intUnaryOpProvider")
3134     static void MAXIntMaxVectorTestsWithMemOp(IntFunction<int[]> fa) {
3135         int[] a = fa.apply(SPECIES.length());
3136         int[] r = fr.apply(SPECIES.length());
3137 
3138         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3139             for (int i = 0; i < a.length; i += SPECIES.length()) {
3140                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3141                 av.lanewise(VectorOperators.MAX, bcast_vec).intoArray(r, i);
3142             }
3143         }
3144 
3145         assertArraysEquals(r, a, (int)10, IntMaxVectorTests::MAX);
3146     }
3147 


3148     @Test(dataProvider = "intUnaryOpProvider")
3149     static void maxIntMaxVectorTestsWithMemOp(IntFunction<int[]> fa) {
3150         int[] a = fa.apply(SPECIES.length());
3151         int[] r = fr.apply(SPECIES.length());
3152 
3153         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3154             for (int i = 0; i < a.length; i += SPECIES.length()) {
3155                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3156                 av.max(bcast_vec).intoArray(r, i);
3157             }
3158         }
3159 
3160         assertArraysEquals(r, a, (int)10, IntMaxVectorTests::max);
3161     }
3162 


3163     @Test(dataProvider = "intUnaryOpMaskProvider")
3164     static void MAXIntMaxVectorTestsMaskedWithMemOp(IntFunction<int[]> fa, IntFunction<boolean[]> fm) {
3165         int[] a = fa.apply(SPECIES.length());
3166         int[] r = fr.apply(SPECIES.length());
3167         boolean[] mask = fm.apply(SPECIES.length());
3168         VectorMask<Integer> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3169 
3170         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3171             for (int i = 0; i < a.length; i += SPECIES.length()) {
3172                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3173                 av.lanewise(VectorOperators.MAX, bcast_vec, vmask).intoArray(r, i);
3174             }
3175         }
3176 
3177         assertArraysEquals(r, a, (int)10, mask, IntMaxVectorTests::MAX);
3178     }
3179 
3180     static int MIN(int a, int b) {
3181         return (int)(Math.min(a, b));
3182     }
3183 
3184     @Test(dataProvider = "intBinaryOpProvider")
3185     static void MINIntMaxVectorTests(IntFunction<int[]> fa, IntFunction<int[]> fb) {
3186         int[] a = fa.apply(SPECIES.length());
3187         int[] b = fb.apply(SPECIES.length());
3188         int[] r = fr.apply(SPECIES.length());
3189 
3190         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3191             for (int i = 0; i < a.length; i += SPECIES.length()) {
3192                 IntVector av = IntVector.fromArray(SPECIES, a, i);
3193                 IntVector bv = IntVector.fromArray(SPECIES, b, i);
< prev index next >