< prev index next >

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

3011 
3012     @Test(dataProvider = "shortUnaryOpMaskProvider")
3013     static void ROLShortMaxVectorTestsScalarShiftMaskedConst(IntFunction<short[]> fa,
3014                                           IntFunction<boolean[]> fm) {
3015         short[] a = fa.apply(SPECIES.length());
3016         short[] r = fr.apply(SPECIES.length());
3017         boolean[] mask = fm.apply(SPECIES.length());
3018         VectorMask<Short> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3019 
3020         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3021             for (int i = 0; i < a.length; i += SPECIES.length()) {
3022                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3023                 av.lanewise(VectorOperators.ROL, CONST_SHIFT, vmask).intoArray(r, i);
3024             }
3025         }
3026 
3027         assertShiftConstEquals(r, a, mask, ShortMaxVectorTests::ROL_binary_const);
3028     }
3029 
3030 
3031     static ShortVector bv_MIN = ShortVector.broadcast(SPECIES, (short)10);
3032 
3033     @Test(dataProvider = "shortUnaryOpProvider")
3034     static void MINShortMaxVectorTestsWithMemOp(IntFunction<short[]> fa) {
3035         short[] a = fa.apply(SPECIES.length());
3036         short[] r = fr.apply(SPECIES.length());
3037 
3038         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3039             for (int i = 0; i < a.length; i += SPECIES.length()) {
3040                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3041                 av.lanewise(VectorOperators.MIN, bv_MIN).intoArray(r, i);
3042             }
3043         }
3044 
3045         assertArraysEquals(r, a, (short)10, ShortMaxVectorTests::MIN);
3046     }
3047 
3048     static ShortVector bv_min = ShortVector.broadcast(SPECIES, (short)10);
3049 
3050     @Test(dataProvider = "shortUnaryOpProvider")
3051     static void minShortMaxVectorTestsWithMemOp(IntFunction<short[]> fa) {
3052         short[] a = fa.apply(SPECIES.length());
3053         short[] r = fr.apply(SPECIES.length());
3054 
3055         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3056             for (int i = 0; i < a.length; i += SPECIES.length()) {
3057                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3058                 av.min(bv_min).intoArray(r, i);
3059             }
3060         }
3061 
3062         assertArraysEquals(r, a, (short)10, ShortMaxVectorTests::min);
3063     }
3064 
3065     static ShortVector bv_MIN_M = ShortVector.broadcast(SPECIES, (short)10);
3066 
3067     @Test(dataProvider = "shortUnaryOpMaskProvider")
3068     static void MINShortMaxVectorTestsMaskedWithMemOp(IntFunction<short[]> fa, IntFunction<boolean[]> fm) {
3069         short[] a = fa.apply(SPECIES.length());
3070         short[] r = fr.apply(SPECIES.length());
3071         boolean[] mask = fm.apply(SPECIES.length());
3072         VectorMask<Short> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3073 
3074         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3075             for (int i = 0; i < a.length; i += SPECIES.length()) {
3076                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3077                 av.lanewise(VectorOperators.MIN, bv_MIN_M, vmask).intoArray(r, i);
3078             }
3079         }
3080 
3081         assertArraysEquals(r, a, (short)10, mask, ShortMaxVectorTests::MIN);
3082     }
3083 
3084     static ShortVector bv_MAX = ShortVector.broadcast(SPECIES, (short)10);
3085 
3086     @Test(dataProvider = "shortUnaryOpProvider")
3087     static void MAXShortMaxVectorTestsWithMemOp(IntFunction<short[]> fa) {
3088         short[] a = fa.apply(SPECIES.length());
3089         short[] 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                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3094                 av.lanewise(VectorOperators.MAX, bv_MAX).intoArray(r, i);
3095             }
3096         }
3097 
3098         assertArraysEquals(r, a, (short)10, ShortMaxVectorTests::MAX);
3099     }
3100 
3101     static ShortVector bv_max = ShortVector.broadcast(SPECIES, (short)10);
3102 
3103     @Test(dataProvider = "shortUnaryOpProvider")
3104     static void maxShortMaxVectorTestsWithMemOp(IntFunction<short[]> fa) {
3105         short[] a = fa.apply(SPECIES.length());
3106         short[] 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                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3111                 av.max(bv_max).intoArray(r, i);
3112             }
3113         }
3114 
3115         assertArraysEquals(r, a, (short)10, ShortMaxVectorTests::max);
3116     }
3117 
3118     static ShortVector bv_MAX_M = ShortVector.broadcast(SPECIES, (short)10);
3119 
3120     @Test(dataProvider = "shortUnaryOpMaskProvider")
3121     static void MAXShortMaxVectorTestsMaskedWithMemOp(IntFunction<short[]> fa, IntFunction<boolean[]> fm) {
3122         short[] a = fa.apply(SPECIES.length());
3123         short[] r = fr.apply(SPECIES.length());
3124         boolean[] mask = fm.apply(SPECIES.length());
3125         VectorMask<Short> 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                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3130                 av.lanewise(VectorOperators.MAX, bv_MAX_M, vmask).intoArray(r, i);
3131             }
3132         }
3133 
3134         assertArraysEquals(r, a, (short)10, mask, ShortMaxVectorTests::MAX);
3135     }
3136 
3137     static short MIN(short a, short b) {
3138         return (short)(Math.min(a, b));
3139     }
3140 
3141     @Test(dataProvider = "shortBinaryOpProvider")
3142     static void MINShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) {
3143         short[] a = fa.apply(SPECIES.length());
3144         short[] b = fb.apply(SPECIES.length());
3145         short[] r = fr.apply(SPECIES.length());
3146 
3147         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3148             for (int i = 0; i < a.length; i += SPECIES.length()) {
3149                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3150                 ShortVector bv = ShortVector.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 ShortMaxVectorTests extends AbstractVectorTest {
  60 
  61     static final VectorSpecies<Short> SPECIES =
  62                 ShortVector.SPECIES_MAX;
  63 
  64     static final int INVOC_COUNT = Integer.getInteger("jdk.incubator.vector.test.loop-iterations", 100);
  65 
  66     static ShortVector bcast_vec = ShortVector.broadcast(SPECIES, (short)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 short CONST_SHIFT = Short.SIZE / 2;
  75 
  76     static final int BUFFER_REPS = Integer.getInteger("jdk.incubator.vector.test.buffer-vectors", 25000 / Max);
  77 
  78     static void assertArraysStrictlyEquals(short[] r, short[] 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         short apply(short a);

3013 
3014     @Test(dataProvider = "shortUnaryOpMaskProvider")
3015     static void ROLShortMaxVectorTestsScalarShiftMaskedConst(IntFunction<short[]> fa,
3016                                           IntFunction<boolean[]> fm) {
3017         short[] a = fa.apply(SPECIES.length());
3018         short[] r = fr.apply(SPECIES.length());
3019         boolean[] mask = fm.apply(SPECIES.length());
3020         VectorMask<Short> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3021 
3022         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3023             for (int i = 0; i < a.length; i += SPECIES.length()) {
3024                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3025                 av.lanewise(VectorOperators.ROL, CONST_SHIFT, vmask).intoArray(r, i);
3026             }
3027         }
3028 
3029         assertShiftConstEquals(r, a, mask, ShortMaxVectorTests::ROL_binary_const);
3030     }
3031 
3032 


3033     @Test(dataProvider = "shortUnaryOpProvider")
3034     static void MINShortMaxVectorTestsWithMemOp(IntFunction<short[]> fa) {
3035         short[] a = fa.apply(SPECIES.length());
3036         short[] r = fr.apply(SPECIES.length());
3037 
3038         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3039             for (int i = 0; i < a.length; i += SPECIES.length()) {
3040                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3041                 av.lanewise(VectorOperators.MIN, bcast_vec).intoArray(r, i);
3042             }
3043         }
3044 
3045         assertArraysEquals(r, a, (short)10, ShortMaxVectorTests::MIN);
3046     }
3047 


3048     @Test(dataProvider = "shortUnaryOpProvider")
3049     static void minShortMaxVectorTestsWithMemOp(IntFunction<short[]> fa) {
3050         short[] a = fa.apply(SPECIES.length());
3051         short[] r = fr.apply(SPECIES.length());
3052 
3053         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3054             for (int i = 0; i < a.length; i += SPECIES.length()) {
3055                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3056                 av.min(bcast_vec).intoArray(r, i);
3057             }
3058         }
3059 
3060         assertArraysEquals(r, a, (short)10, ShortMaxVectorTests::min);
3061     }
3062 


3063     @Test(dataProvider = "shortUnaryOpMaskProvider")
3064     static void MINShortMaxVectorTestsMaskedWithMemOp(IntFunction<short[]> fa, IntFunction<boolean[]> fm) {
3065         short[] a = fa.apply(SPECIES.length());
3066         short[] r = fr.apply(SPECIES.length());
3067         boolean[] mask = fm.apply(SPECIES.length());
3068         VectorMask<Short> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3069 
3070         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3071             for (int i = 0; i < a.length; i += SPECIES.length()) {
3072                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3073                 av.lanewise(VectorOperators.MIN, bcast_vec, vmask).intoArray(r, i);
3074             }
3075         }
3076 
3077         assertArraysEquals(r, a, (short)10, mask, ShortMaxVectorTests::MIN);
3078     }
3079 


3080     @Test(dataProvider = "shortUnaryOpProvider")
3081     static void MAXShortMaxVectorTestsWithMemOp(IntFunction<short[]> fa) {
3082         short[] a = fa.apply(SPECIES.length());
3083         short[] r = fr.apply(SPECIES.length());
3084 
3085         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3086             for (int i = 0; i < a.length; i += SPECIES.length()) {
3087                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3088                 av.lanewise(VectorOperators.MAX, bcast_vec).intoArray(r, i);
3089             }
3090         }
3091 
3092         assertArraysEquals(r, a, (short)10, ShortMaxVectorTests::MAX);
3093     }
3094 


3095     @Test(dataProvider = "shortUnaryOpProvider")
3096     static void maxShortMaxVectorTestsWithMemOp(IntFunction<short[]> fa) {
3097         short[] a = fa.apply(SPECIES.length());
3098         short[] 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                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3103                 av.max(bcast_vec).intoArray(r, i);
3104             }
3105         }
3106 
3107         assertArraysEquals(r, a, (short)10, ShortMaxVectorTests::max);
3108     }
3109 


3110     @Test(dataProvider = "shortUnaryOpMaskProvider")
3111     static void MAXShortMaxVectorTestsMaskedWithMemOp(IntFunction<short[]> fa, IntFunction<boolean[]> fm) {
3112         short[] a = fa.apply(SPECIES.length());
3113         short[] r = fr.apply(SPECIES.length());
3114         boolean[] mask = fm.apply(SPECIES.length());
3115         VectorMask<Short> vmask = VectorMask.fromArray(SPECIES, mask, 0);
3116 
3117         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3118             for (int i = 0; i < a.length; i += SPECIES.length()) {
3119                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3120                 av.lanewise(VectorOperators.MAX, bcast_vec, vmask).intoArray(r, i);
3121             }
3122         }
3123 
3124         assertArraysEquals(r, a, (short)10, mask, ShortMaxVectorTests::MAX);
3125     }
3126 
3127     static short MIN(short a, short b) {
3128         return (short)(Math.min(a, b));
3129     }
3130 
3131     @Test(dataProvider = "shortBinaryOpProvider")
3132     static void MINShortMaxVectorTests(IntFunction<short[]> fa, IntFunction<short[]> fb) {
3133         short[] a = fa.apply(SPECIES.length());
3134         short[] b = fb.apply(SPECIES.length());
3135         short[] r = fr.apply(SPECIES.length());
3136 
3137         for (int ic = 0; ic < INVOC_COUNT; ic++) {
3138             for (int i = 0; i < a.length; i += SPECIES.length()) {
3139                 ShortVector av = ShortVector.fromArray(SPECIES, a, i);
3140                 ShortVector bv = ShortVector.fromArray(SPECIES, b, i);
< prev index next >