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<?> vectorType;
  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<?> vectorType, 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.vectorType = vectorType;
  68         this.maskType = maskType;
  69 
  70         if (vectorType == Long64Vector.class || vectorType == Double64Vector.class) {
  71             indexShape = VectorShape.S_64_BIT;
  72         } else {
  73             int bitSize = Vector.bitSizeForVectorLength(int.class, shape.bitSize() / elementSize);
  74             indexShape = VectorShape.forBitSize(bitSize);
  75         }
  76     }
  77 
  78     @Override
  79     @ForceInline
  80     public int bitSize() {
  81         return shape.bitSize();
  82     }
  83 
  84     @Override
  85     @ForceInline
  86     public int length() {
  87         return shape.bitSize() / elementSize;
  88     }
  89 
  90     @Override
  91     @ForceInline
  92     public Class<E> elementType() {
  93         return elementType;
  94     }
  95 
  96     @Override
  97     @ForceInline
  98     public Class<?> vectorType() {
  99         return vectorType;
 100     }
 101 
 102     @Override
 103     @ForceInline
 104     public Class<?> maskType() {
 105         return maskType;
 106     }
 107 
 108     @Override
 109     @ForceInline
 110     public int elementSize() {
 111         return elementSize;
 112     }
 113 
 114     @Override
 115     @ForceInline
 116     public VectorShape shape() {
 117         return shape;
 118     }
 119 
 120     @Override
 121     @ForceInline
 122     public VectorShape indexShape() { return indexShape; }
 123 
 124     @Override
 125     public String toString() {
 126         return new StringBuilder("Shape[")
 127                 .append(bitSize()).append(" bits, ")
 128                 .append(length()).append(" ").append(elementType.getSimpleName()).append("s x ")
 129                 .append(elementSize()).append(" bits")
 130                 .append("]")
 131                 .toString();
 132     }
 133 
 134     interface FOpm {
 135         boolean apply(int i);
 136     }
 137 
 138     VectorMask<E> opm(AbstractSpecies.FOpm f) {
 139         boolean[] res = new boolean[length()];
 140         for (int i = 0; i < length(); i++) {
 141             res[i] = f.apply(i);
 142         }
 143         return maskFactory.apply(res);
 144     }
 145 }