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 java.util.Arrays; 28 29 abstract class AbstractMask<E> extends VectorMask<E> { 30 31 /*package-private*/ 32 abstract boolean[] getBits(); 33 34 // Unary operator 35 36 interface MUnOp { 37 boolean apply(int i, boolean a); 38 } 39 40 abstract AbstractMask<E> uOp(MUnOp f); 41 42 // Binary operator 43 44 interface MBinOp { 45 boolean apply(int i, boolean a, boolean b); 46 } 47 48 abstract AbstractMask<E> bOp(VectorMask<E> o, MBinOp f); 49 50 @Override 51 public String toString() { 52 return Arrays.toString(getBits()); 53 } 54 55 @Override 56 public boolean lane(int i) { 57 return getBits()[i]; 58 } 59 60 @Override 61 public long toLong() { 62 long res = 0; 63 long set = 1; 64 boolean[] bits = getBits(); 65 for (int i = 0; i < species().length(); i++) { 66 res = bits[i] ? res | set : res; 67 set = set << 1; 68 } 69 return res; 70 } 71 72 @Override 73 public void intoArray(boolean[] bits, int i) { 74 System.arraycopy(getBits(), 0, bits, i, species().length()); 75 } 76 77 @Override 78 public boolean[] toArray() { 79 return getBits().clone(); 80 } 81 82 @Override 83 public int trueCount() { 84 int c = 0; 85 for (boolean i : getBits()) { 86 if (i) c++; 87 } 88 return c; 89 } 90 91 @Override 92 public AbstractMask<E> and(VectorMask<E> o) { 93 return bOp(o, (i, a, b) -> a && b); 94 } 95 96 @Override 97 public AbstractMask<E> or(VectorMask<E> o) { 98 return bOp(o, (i, a, b) -> a || b); 99 } 100 101 @Override 102 public AbstractMask<E> not() { 103 return uOp((i, a) -> !a); 104 } 105 106 /*package-private*/ 107 static boolean anyTrueHelper(boolean[] bits) { 108 for (boolean i : bits) { 109 if (i) return true; 110 } 111 return false; 112 } 113 114 /*package-private*/ 115 static boolean allTrueHelper(boolean[] bits) { 116 for (boolean i : bits) { 117 if (!i) return false; 118 } 119 return true; 120 } 121 122 // @@@ This is a bad implementation -- makes lambdas capturing -- fix this 123 @SuppressWarnings("unchecked") 124 static <E> VectorMask<E> trueMask(VectorSpecies<E> species) { 125 Class<?> eType = species.elementType(); 126 127 if (eType == byte.class) { 128 if (species.boxType() == ByteMaxVector.class) 129 return (VectorMask<E>) ByteMaxVector.ByteMaxMask.TRUE_MASK; 130 switch (species.bitSize()) { 131 case 64: return (VectorMask<E>) Byte64Vector.Byte64Mask.TRUE_MASK; 132 case 128: return (VectorMask<E>) Byte128Vector.Byte128Mask.TRUE_MASK; 133 case 256: return (VectorMask<E>) Byte256Vector.Byte256Mask.TRUE_MASK; 134 case 512: return (VectorMask<E>) Byte512Vector.Byte512Mask.TRUE_MASK; 135 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 136 } 137 } else if (eType == short.class) { 138 if (species.boxType() == ShortMaxVector.class) 139 return (VectorMask<E>) ShortMaxVector.ShortMaxMask.TRUE_MASK; 140 switch (species.bitSize()) { 141 case 64: return (VectorMask<E>) Short64Vector.Short64Mask.TRUE_MASK; 142 case 128: return (VectorMask<E>) Short128Vector.Short128Mask.TRUE_MASK; 143 case 256: return (VectorMask<E>) Short256Vector.Short256Mask.TRUE_MASK; 144 case 512: return (VectorMask<E>) Short512Vector.Short512Mask.TRUE_MASK; 145 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 146 } 147 } else if (eType == int.class) { 148 if (species.boxType() == IntMaxVector.class) 149 return (VectorMask<E>) IntMaxVector.IntMaxMask.TRUE_MASK; 150 switch (species.bitSize()) { 151 case 64: return (VectorMask<E>) Int64Vector.Int64Mask.TRUE_MASK; 152 case 128: return (VectorMask<E>) Int128Vector.Int128Mask.TRUE_MASK; 153 case 256: return (VectorMask<E>) Int256Vector.Int256Mask.TRUE_MASK; 154 case 512: return (VectorMask<E>) Int512Vector.Int512Mask.TRUE_MASK; 155 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 156 } 157 } else if (eType == long.class) { 158 if (species.boxType() == LongMaxVector.class) 159 return (VectorMask<E>) LongMaxVector.LongMaxMask.TRUE_MASK; 160 switch (species.bitSize()) { 161 case 64: return (VectorMask<E>) Long64Vector.Long64Mask.TRUE_MASK; 162 case 128: return (VectorMask<E>) Long128Vector.Long128Mask.TRUE_MASK; 163 case 256: return (VectorMask<E>) Long256Vector.Long256Mask.TRUE_MASK; 164 case 512: return (VectorMask<E>) Long512Vector.Long512Mask.TRUE_MASK; 165 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 166 } 167 } else if (eType == float.class) { 168 if (species.boxType() == FloatMaxVector.class) 169 return (VectorMask<E>) FloatMaxVector.FloatMaxMask.TRUE_MASK; 170 switch (species.bitSize()) { 171 case 64: return (VectorMask<E>) Float64Vector.Float64Mask.TRUE_MASK; 172 case 128: return (VectorMask<E>) Float128Vector.Float128Mask.TRUE_MASK; 173 case 256: return (VectorMask<E>) Float256Vector.Float256Mask.TRUE_MASK; 174 case 512: return (VectorMask<E>) Float512Vector.Float512Mask.TRUE_MASK; 175 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 176 } 177 } else if (eType == double.class) { 178 if (species.boxType() == DoubleMaxVector.class) 179 return (VectorMask<E>) DoubleMaxVector.DoubleMaxMask.TRUE_MASK; 180 switch (species.bitSize()) { 181 case 64: return (VectorMask<E>) Double64Vector.Double64Mask.TRUE_MASK; 182 case 128: return (VectorMask<E>) Double128Vector.Double128Mask.TRUE_MASK; 183 case 256: return (VectorMask<E>) Double256Vector.Double256Mask.TRUE_MASK; 184 case 512: return (VectorMask<E>) Double512Vector.Double512Mask.TRUE_MASK; 185 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 186 } 187 } else { 188 throw new IllegalArgumentException("Bad element type of species"); 189 } 190 } 191 192 // @@@ This is a bad implementation -- makes lambdas capturing -- fix this 193 @SuppressWarnings("unchecked") 194 static <E> VectorMask<E> falseMask(VectorSpecies<E> species) { 195 Class<?> eType = species.elementType(); 196 197 if (eType == byte.class) { 198 if (species.boxType() == ByteMaxVector.class) 199 return (VectorMask<E>) ByteMaxVector.ByteMaxMask.FALSE_MASK; 200 switch (species.bitSize()) { 201 case 64: return (VectorMask<E>) Byte64Vector.Byte64Mask.FALSE_MASK; 202 case 128: return (VectorMask<E>) Byte128Vector.Byte128Mask.FALSE_MASK; 203 case 256: return (VectorMask<E>) Byte256Vector.Byte256Mask.FALSE_MASK; 204 case 512: return (VectorMask<E>) Byte512Vector.Byte512Mask.FALSE_MASK; 205 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 206 } 207 } else if (eType == short.class) { 208 if (species.boxType() == ShortMaxVector.class) 209 return (VectorMask<E>) ShortMaxVector.ShortMaxMask.FALSE_MASK; 210 switch (species.bitSize()) { 211 case 64: return (VectorMask<E>) Short64Vector.Short64Mask.FALSE_MASK; 212 case 128: return (VectorMask<E>) Short128Vector.Short128Mask.FALSE_MASK; 213 case 256: return (VectorMask<E>) Short256Vector.Short256Mask.FALSE_MASK; 214 case 512: return (VectorMask<E>) Short512Vector.Short512Mask.FALSE_MASK; 215 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 216 } 217 } else if (eType == int.class) { 218 if (species.boxType() == IntMaxVector.class) 219 return (VectorMask<E>) IntMaxVector.IntMaxMask.FALSE_MASK; 220 switch (species.bitSize()) { 221 case 64: return (VectorMask<E>) Int64Vector.Int64Mask.FALSE_MASK; 222 case 128: return (VectorMask<E>) Int128Vector.Int128Mask.FALSE_MASK; 223 case 256: return (VectorMask<E>) Int256Vector.Int256Mask.FALSE_MASK; 224 case 512: return (VectorMask<E>) Int512Vector.Int512Mask.FALSE_MASK; 225 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 226 } 227 } else if (eType == long.class) { 228 if (species.boxType() == LongMaxVector.class) 229 return (VectorMask<E>) LongMaxVector.LongMaxMask.FALSE_MASK; 230 switch (species.bitSize()) { 231 case 64: return (VectorMask<E>) Long64Vector.Long64Mask.FALSE_MASK; 232 case 128: return (VectorMask<E>) Long128Vector.Long128Mask.FALSE_MASK; 233 case 256: return (VectorMask<E>) Long256Vector.Long256Mask.FALSE_MASK; 234 case 512: return (VectorMask<E>) Long512Vector.Long512Mask.FALSE_MASK; 235 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 236 } 237 } else if (eType == float.class) { 238 if (species.boxType() == FloatMaxVector.class) 239 return (VectorMask<E>) FloatMaxVector.FloatMaxMask.FALSE_MASK; 240 switch (species.bitSize()) { 241 case 64: return (VectorMask<E>) Float64Vector.Float64Mask.FALSE_MASK; 242 case 128: return (VectorMask<E>) Float128Vector.Float128Mask.FALSE_MASK; 243 case 256: return (VectorMask<E>) Float256Vector.Float256Mask.FALSE_MASK; 244 case 512: return (VectorMask<E>) Float512Vector.Float512Mask.FALSE_MASK; 245 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 246 } 247 } else if (eType == double.class) { 248 if (species.boxType() == DoubleMaxVector.class) 249 return (VectorMask<E>) DoubleMaxVector.DoubleMaxMask.FALSE_MASK; 250 switch (species.bitSize()) { 251 case 64: return (VectorMask<E>) Double64Vector.Double64Mask.FALSE_MASK; 252 case 128: return (VectorMask<E>) Double128Vector.Double128Mask.FALSE_MASK; 253 case 256: return (VectorMask<E>) Double256Vector.Double256Mask.FALSE_MASK; 254 case 512: return (VectorMask<E>) Double512Vector.Double512Mask.FALSE_MASK; 255 default: throw new IllegalArgumentException(Integer.toString(species.bitSize())); 256 } 257 } else { 258 throw new IllegalArgumentException("Bad element type of species"); 259 } 260 } 261 }