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