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> extends Species<E> {
  33     @Stable
  34     protected final Shape shape;
  35     @Stable
  36     protected final Class<E> elementType;
  37     @Stable
  38     protected final int elementSize;
  39     @Stable
  40     protected final Class<?> boxType;
  41     @Stable
  42     protected final Class<?> maskType;
  43     @Stable
  44     protected final Shape indexShape;
  45 
  46     AbstractSpecies(Shape shape, Class<E> elementType, int elementSize,
  47                     Class<?> boxType, Class<?> maskType, Function<boolean[], Mask<E>> maskFactory,
  48                     Function<IntUnaryOperator, Shuffle<E>> shuffleFromArrayFactory,
  49                     fShuffleFromArray<E> shuffleFromOpFactory) {
  50         super(maskFactory, shuffleFromArrayFactory, shuffleFromOpFactory);
  51 
  52         this.shape = shape;
  53         this.elementType = elementType;
  54         this.elementSize = elementSize;
  55         this.boxType = boxType;
  56         this.maskType = maskType;
  57 
  58         if (boxType == Long64Vector.class || boxType == Double64Vector.class) {
  59             indexShape = Shape.S_64_BIT;
  60         }
  61         else {
  62             int bitSize = Vector.bitSizeForVectorLength(int.class, shape.bitSize() / elementSize);
  63             indexShape = Shape.forBitSize(bitSize);
  64         }
  65     }
  66 
  67     @Override
  68     @ForceInline
  69     public int bitSize() {
  70         return shape.bitSize();
  71     }
  72 
  73     @Override
  74     @ForceInline
  75     public int length() {
  76         return shape.bitSize() / elementSize;
  77     }
  78 
  79     @Override
  80     @ForceInline
  81     public Class<E> elementType() {
  82         return elementType;
  83     }
  84 
  85     @Override
  86     @ForceInline
  87     public Class<?> boxType() {
  88         return boxType;
  89     }
  90 
  91     @Override
  92     @ForceInline
  93     public Class<?> maskType() {
  94         return maskType;
  95     }
  96 
  97     @Override
  98     @ForceInline
  99     public int elementSize() {
 100         return elementSize;
 101     }
 102 
 103     @Override
 104     @ForceInline
 105     public Shape shape() {
 106         return shape;
 107     }
 108 
 109     @Override
 110     @ForceInline
 111     public Shape indexShape() { return indexShape; }
 112 
 113     @Override
 114     public String toString() {
 115         return new StringBuilder("Shape[")
 116                 .append(bitSize()).append(" bits, ")
 117                 .append(length()).append(" ").append(elementType.getSimpleName()).append("s x ")
 118                 .append(elementSize()).append(" bits")
 119                 .append("]")
 120                 .toString();
 121     }
 122 }