< prev index next >

test/jdk/jdk/incubator/vector/VectorHash.java

Print this page
rev 54658 : refactored mask and shuffle creation methods, moved classes to top-level


  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 /**
  25  * @test
  26  * @modules jdk.incubator.vector
  27  */
  28 
  29 import jdk.incubator.vector.ByteVector;
  30 import jdk.incubator.vector.IntVector;
  31 import jdk.incubator.vector.Vector.Shape;
  32 import jdk.incubator.vector.Vector.Species;
  33 import jdk.incubator.vector.Vector;
  34 
  35 import java.nio.charset.StandardCharsets;
  36 import java.util.Arrays;
  37 import java.util.Properties;
  38 import java.util.SplittableRandom;
  39 
  40 import static java.util.stream.Collectors.joining;
  41 
  42 public class VectorHash {
  43 
  44     public static void main(String[] args) {
  45         Properties p = System.getProperties();
  46         p.forEach((k, v) -> {
  47             byte[] bk = k.toString().getBytes(StandardCharsets.UTF_8);
  48             byte[] bv = v.toString().getBytes(StandardCharsets.UTF_8);
  49             assertHashCode(bk);
  50             assertHashCode(bv);
  51         });
  52 


 140             h = h * COEFF_31_TO_16 + x.mul(H_COEFF_16).addAll();
 141         }
 142 
 143         for (; i < a.length; i++) {
 144             h = 31 * h + a[i];
 145         }
 146         return h;
 147     }
 148 
 149     static int hashCodeVector512Shift(byte[] a) {
 150         return hashCodeVectorGenericShift(a,
 151                                           BYTE_128_SPECIES,
 152                                           BYTE_512_SPECIES,
 153                                           INT_512_SPECIES,
 154                                           COEFF_31_TO_16,
 155                                           H_COEFF_16);
 156     }
 157 
 158     static int hashCodeVectorGenericShift(
 159             byte[] a,
 160             Species<Byte> bytesForIntsSpecies,
 161             Species<Byte> byteSpecies, Species<Integer> intSpecies,
 162             int top_h_coeff,
 163             IntVector v_h_coeff) {
 164         assert bytesForIntsSpecies.length() == intSpecies.length();
 165 
 166         int h = 1;
 167         int i = 0;
 168         for (; i < (a.length & ~(byteSpecies.length() - 1)); i += byteSpecies.length()) {
 169             ByteVector b = ByteVector.fromArray(byteSpecies, a, i);
 170 
 171             for (int j = 0; j < byteSpecies.length() / intSpecies.length(); j++) {
 172                 // Reduce the size of the byte vector and then cast to int
 173                 IntVector x = (IntVector)(b.reshape(bytesForIntsSpecies)).cast(intSpecies);
 174 
 175                 h = h * top_h_coeff + x.mul(v_h_coeff).addAll();
 176 
 177                 b = b.shiftEL(intSpecies.length());
 178             }
 179         }
 180 
 181         for (; i < a.length; i++) {
 182             h = 31 * h + a[i];
 183         }
 184         return h;
 185     }
 186 
 187     static final Species<Integer> INT_512_SPECIES = IntVector.SPECIES_512;
 188     static final Species<Integer> INT_256_SPECIES = IntVector.SPECIES_256;
 189     static final int COEFF_31_TO_16;
 190     static final IntVector H_COEFF_16;
 191 
 192     static final Species<Byte> BYTE_512_SPECIES = ByteVector.SPECIES_512;
 193     static final Species<Byte> BYTE_128_SPECIES = ByteVector.SPECIES_128;
 194     static final Species<Byte> BYTE_64_SPECIES = ByteVector.SPECIES_64;
 195     static final int COEFF_31_TO_8;
 196     static final IntVector H_COEFF_8;
 197 
 198     static {
 199         int[] a = new int[INT_256_SPECIES.length()];
 200         a[a.length - 1] = 1;
 201         for (int i = 1; i < a.length; i++) {
 202             a[a.length - 1 - i] = a[a.length - 1 - i + 1] * 31;
 203         }
 204 
 205         COEFF_31_TO_8 = a[0] * 31;
 206         H_COEFF_8 = IntVector.fromArray(INT_256_SPECIES, a, 0);
 207 
 208 
 209         a = new int[INT_512_SPECIES.length()];
 210         a[a.length - 1] = 1;
 211         for (int i = 1; i < a.length; i++) {
 212             a[a.length - 1 - i] = a[a.length - 1 - i + 1] * 31;
 213         }
 214 


  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 /**
  25  * @test
  26  * @modules jdk.incubator.vector
  27  */
  28 
  29 import jdk.incubator.vector.ByteVector;
  30 import jdk.incubator.vector.IntVector;
  31 import jdk.incubator.vector.VectorShape;
  32 import jdk.incubator.vector.VectorSpecies;
  33 import jdk.incubator.vector.Vector;
  34 
  35 import java.nio.charset.StandardCharsets;
  36 import java.util.Arrays;
  37 import java.util.Properties;
  38 import java.util.SplittableRandom;
  39 
  40 import static java.util.stream.Collectors.joining;
  41 
  42 public class VectorHash {
  43 
  44     public static void main(String[] args) {
  45         Properties p = System.getProperties();
  46         p.forEach((k, v) -> {
  47             byte[] bk = k.toString().getBytes(StandardCharsets.UTF_8);
  48             byte[] bv = v.toString().getBytes(StandardCharsets.UTF_8);
  49             assertHashCode(bk);
  50             assertHashCode(bv);
  51         });
  52 


 140             h = h * COEFF_31_TO_16 + x.mul(H_COEFF_16).addAll();
 141         }
 142 
 143         for (; i < a.length; i++) {
 144             h = 31 * h + a[i];
 145         }
 146         return h;
 147     }
 148 
 149     static int hashCodeVector512Shift(byte[] a) {
 150         return hashCodeVectorGenericShift(a,
 151                                           BYTE_128_SPECIES,
 152                                           BYTE_512_SPECIES,
 153                                           INT_512_SPECIES,
 154                                           COEFF_31_TO_16,
 155                                           H_COEFF_16);
 156     }
 157 
 158     static int hashCodeVectorGenericShift(
 159             byte[] a,
 160             VectorSpecies<Byte> bytesForIntsSpecies,
 161             VectorSpecies<Byte> byteSpecies, VectorSpecies<Integer> intSpecies,
 162             int top_h_coeff,
 163             IntVector v_h_coeff) {
 164         assert bytesForIntsSpecies.length() == intSpecies.length();
 165 
 166         int h = 1;
 167         int i = 0;
 168         for (; i < (a.length & ~(byteSpecies.length() - 1)); i += byteSpecies.length()) {
 169             ByteVector b = ByteVector.fromArray(byteSpecies, a, i);
 170 
 171             for (int j = 0; j < byteSpecies.length() / intSpecies.length(); j++) {
 172                 // Reduce the size of the byte vector and then cast to int
 173                 IntVector x = (IntVector)(b.reshape(bytesForIntsSpecies)).cast(intSpecies);
 174 
 175                 h = h * top_h_coeff + x.mul(v_h_coeff).addAll();
 176 
 177                 b = b.shiftEL(intSpecies.length());
 178             }
 179         }
 180 
 181         for (; i < a.length; i++) {
 182             h = 31 * h + a[i];
 183         }
 184         return h;
 185     }
 186 
 187     static final VectorSpecies<Integer> INT_512_SPECIES = IntVector.SPECIES_512;
 188     static final VectorSpecies<Integer> INT_256_SPECIES = IntVector.SPECIES_256;
 189     static final int COEFF_31_TO_16;
 190     static final IntVector H_COEFF_16;
 191 
 192     static final VectorSpecies<Byte> BYTE_512_SPECIES = ByteVector.SPECIES_512;
 193     static final VectorSpecies<Byte> BYTE_128_SPECIES = ByteVector.SPECIES_128;
 194     static final VectorSpecies<Byte> BYTE_64_SPECIES = ByteVector.SPECIES_64;
 195     static final int COEFF_31_TO_8;
 196     static final IntVector H_COEFF_8;
 197 
 198     static {
 199         int[] a = new int[INT_256_SPECIES.length()];
 200         a[a.length - 1] = 1;
 201         for (int i = 1; i < a.length; i++) {
 202             a[a.length - 1 - i] = a[a.length - 1 - i + 1] * 31;
 203         }
 204 
 205         COEFF_31_TO_8 = a[0] * 31;
 206         H_COEFF_8 = IntVector.fromArray(INT_256_SPECIES, a, 0);
 207 
 208 
 209         a = new int[INT_512_SPECIES.length()];
 210         a[a.length - 1] = 1;
 211         for (int i = 1; i < a.length; i++) {
 212             a[a.length - 1 - i] = a[a.length - 1 - i + 1] * 31;
 213         }
 214 
< prev index next >