1 /*
   2  * Copyright (c) 2017, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have
  23  * questions.
  24  */
  25 package jdk.incubator.vector;
  26 
  27 import jdk.internal.vm.annotation.ForceInline;
  28 import jdk.internal.vm.annotation.Stable;
  29 import java.util.function.Function;
  30 import java.util.function.IntUnaryOperator;
  31 
  32 abstract class AbstractSpecies<E> implements VectorSpecies<E> {
  33     @FunctionalInterface
  34     interface fShuffleFromArray<E> {
  35         VectorShuffle<E> apply(int[] reorder, int idx);
  36     }
  37 
  38     final Function<boolean[], VectorMask<E>> maskFactory;
  39     final Function<IntUnaryOperator, VectorShuffle<E>> shuffleFromOpFactory;
  40     final fShuffleFromArray<E> shuffleFromArrayFactory;
  41 
  42     @Stable
  43     protected final VectorShape shape;
  44     @Stable
  45     protected final Class<E> elementType;
  46     @Stable
  47     protected final int elementSize;
  48     @Stable
  49     protected final Class<?> boxType;
  50     @Stable
  51     protected final Class<?> maskType;
  52     @Stable
  53     protected final VectorShape indexShape;
  54 
  55     AbstractSpecies(VectorShape shape, Class<E> elementType, int elementSize,
  56                     Class<?> boxType, Class<?> maskType, Function<boolean[], VectorMask<E>> maskFactory,
  57                     Function<IntUnaryOperator, VectorShuffle<E>> shuffleFromOpFactory,
  58                     fShuffleFromArray<E> shuffleFromArrayFactory) {
  59 
  60         this.maskFactory = maskFactory;
  61         this.shuffleFromArrayFactory = shuffleFromArrayFactory;
  62         this.shuffleFromOpFactory = shuffleFromOpFactory;
  63 
  64         this.shape = shape;
  65         this.elementType = elementType;
  66         this.elementSize = elementSize;
  67         this.boxType = boxType;
  68         this.maskType = maskType;
  69 
  70         if (boxType == Long64Vector.class || boxType == Double64Vector.class) {
  71             indexShape = VectorShape.S_64_BIT;
  72         }
  73         else {
  74             int bitSize = Vector.bitSizeForVectorLength(int.class, shape.bitSize() / elementSize);
  75             indexShape = VectorShape.forBitSize(bitSize);
  76         }
  77     }
  78 
  79     @Override
  80     @ForceInline
  81     public int bitSize() {
  82         return shape.bitSize();
  83     }
  84 
  85     @Override
  86     @ForceInline
  87     public int length() {
  88         return shape.bitSize() / elementSize;
  89     }
  90 
  91     @Override
  92     @ForceInline
  93     public Class<E> elementType() {
  94         return elementType;
  95     }
  96 
  97     @Override
  98     @ForceInline
  99     public Class<?> boxType() {
 100         return boxType;
 101     }
 102 
 103     @Override
 104     @ForceInline
 105     public Class<?> maskType() {
 106         return maskType;
 107     }
 108 
 109     @Override
 110     @ForceInline
 111     public int elementSize() {
 112         return elementSize;
 113     }
 114 
 115     @Override
 116     @ForceInline
 117     public VectorShape shape() {
 118         return shape;
 119     }
 120 
 121     @Override
 122     @ForceInline
 123     public VectorShape indexShape() { return indexShape; }
 124 
 125     @Override
 126     public String toString() {
 127         return new StringBuilder("Shape[")
 128                 .append(bitSize()).append(" bits, ")
 129                 .append(length()).append(" ").append(elementType.getSimpleName()).append("s x ")
 130                 .append(elementSize()).append(" bits")
 131                 .append("]")
 132                 .toString();
 133     }
 134 
 135     interface FOpm {
 136         boolean apply(int i);
 137     }
 138 
 139     VectorMask<E> opm(AbstractSpecies.FOpm f) {
 140         boolean[] res = new boolean[length()];
 141         for (int i = 0; i < length(); i++) {
 142             res[i] = f.apply(i);
 143         }
 144         return maskFactory.apply(res);
 145     }
 146 }