1 /* 2 * Copyright (c) 2021, 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 */ 23 24 package org.openjdk.bench.jdk.incubator.vector.bigdata; 25 26 import jdk.incubator.vector.IntVector; 27 import jdk.incubator.vector.LongVector; 28 import jdk.incubator.vector.Vector; 29 import jdk.incubator.vector.VectorMask; 30 import jdk.incubator.vector.VectorOperators; 31 import jdk.incubator.vector.VectorShape; 32 import jdk.incubator.vector.VectorSpecies; 33 34 import java.util.Random; 35 import java.util.concurrent.TimeUnit; 36 37 import org.openjdk.jmh.annotations.*; 38 39 @BenchmarkMode(Mode.Throughput) 40 @OutputTimeUnit(TimeUnit.MILLISECONDS) 41 @State(Scope.Benchmark) 42 @Warmup(iterations = 3, time = 1) 43 @Measurement(iterations = 5, time = 1) 44 @Fork(value = 1, jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"}) 45 public class ValueRangeCheckAndCastL2I { 46 47 @Param("1024") 48 int ARRAY_LENGTH; 49 50 private long min = Integer.MIN_VALUE; 51 private long max = Integer.MAX_VALUE; 52 53 private static final VectorSpecies<Long> LONG_SPECIES = 54 LongVector.SPECIES_PREFERRED; 55 private static final VectorSpecies<Integer> INT_SPECIES = 56 VectorSpecies.of(int.class, VectorShape.forBitSize(LONG_SPECIES.vectorBitSize() / 2)); 57 58 private int[] intResult; 59 private long[] longArray; 60 61 @Setup 62 public void init() { 63 System.out.println("LONG_SPECIES's length: " + LONG_SPECIES.length()); 64 System.out.println("INT_SPECIES's length: " + INT_SPECIES.length()); 65 System.out.println("Min is: " + min + ". Max is: " + max); 66 67 longArray = new long[ARRAY_LENGTH]; 68 intResult = new int[ARRAY_LENGTH]; 69 70 Random rand = new Random(); 71 for (int i = 0; i < ARRAY_LENGTH; i++) { 72 intResult[i] = 0; 73 longArray[i] = rand.nextInt(Integer.MAX_VALUE); 74 } 75 } 76 77 @Benchmark 78 public boolean castL2I() { 79 for (int i = 0; i < longArray.length; i++) { 80 if (longArray[i] >= min && longArray[i] <= max) { 81 intResult[i] = (int)(longArray[i]); 82 } else { 83 return false; 84 } 85 } 86 return true; 87 } 88 89 @Benchmark 90 public boolean castL2I_vec() { 91 for (int i = 0; i < longArray.length; i += LONG_SPECIES.length()) { 92 LongVector av = LongVector.fromArray(LONG_SPECIES, longArray, i); 93 if (av.compare(VectorOperators.GE, min).and(av.compare(VectorOperators.LE, max)).allTrue()) { 94 ((IntVector) av.castShape(INT_SPECIES, 0)).intoArray(intResult, i); 95 } else { 96 return false; 97 } 98 } 99 return true; 100 } 101 102 } 103