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.misc.Unsafe;
  28 import jdk.internal.vm.annotation.ForceInline;
  29 import jdk.internal.vm.annotation.Stable;
  30 
  31 /**
  32  * A {@code VectorShape} governs the total size, in bits, of a
  33  * {@link Vector}, {@link VectorMask}, or {@link VectorShuffle}.  The shape in
  34  * combination with the element type together govern the number of lanes.
  35  */
  36 public enum VectorShape {
  37     /** Shape of length 64 bits */
  38     S_64_BIT(64),
  39     /** Shape of length 128 bits */
  40     S_128_BIT(128),
  41     /** Shape of length 256 bits */
  42     S_256_BIT(256),
  43     /** Shape of length 512 bits */
  44     S_512_BIT(512),
  45     /** Shape of maximum length supported on the platform */
  46     S_Max_BIT(Unsafe.getUnsafe().getMaxVectorSize(byte.class) * 8);
  47 
  48     @Stable
  49     final int bitSize;
  50 
  51     VectorShape(int bitSize) {
  52         this.bitSize = bitSize;
  53     }
  54 
  55     /**
  56      * Returns the size, in bits, of this shape.
  57      *
  58      * @return the size, in bits, of this shape.
  59      */
  60     @ForceInline
  61     public int bitSize() {
  62         return bitSize;
  63     }
  64 
  65     /**
  66      * Return the number of lanes of a vector of this shape and whose element
  67      * type is of the provided species
  68      *
  69      * @param s the species describing the element type
  70      * @return the number of lanes
  71      */
  72     int length(VectorSpecies<?> s) {
  73         return bitSize() / s.elementSize();
  74     }
  75 
  76     /**
  77      * Finds appropriate shape depending on bitsize.
  78      *
  79      * @param bitSize the size in bits
  80      * @return the shape corresponding to bitsize
  81      * @see #bitSize
  82      */
  83     public static VectorShape forBitSize(int bitSize) {
  84         switch (bitSize) {
  85             case 64:
  86                 return VectorShape.S_64_BIT;
  87             case 128:
  88                 return VectorShape.S_128_BIT;
  89             case 256:
  90                 return VectorShape.S_256_BIT;
  91             case 512:
  92                 return VectorShape.S_512_BIT;
  93             default:
  94                 if ((bitSize > 0) && (bitSize <= 2048) && (bitSize % 128 == 0)) {
  95                     return VectorShape.S_Max_BIT;
  96                 } else {
  97                     throw new IllegalArgumentException("Bad vector bit size: " + bitSize);
  98                 }
  99         }
 100     }
 101 }
 102