/* * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have * questions. */ package jdk.incubator.vector; import jdk.internal.vm.annotation.ForceInline; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.ShortBuffer; import java.util.concurrent.ThreadLocalRandom; @SuppressWarnings("cast") public abstract class ShortVector extends Vector { ShortVector() {} // Unary operator interface FUnOp { short apply(int i, short a); } abstract ShortVector uOp(FUnOp f); abstract ShortVector uOp(Mask m, FUnOp f); // Binary operator interface FBinOp { short apply(int i, short a, short b); } abstract ShortVector bOp(Vector o, FBinOp f); abstract ShortVector bOp(Vector o, Mask m, FBinOp f); // Trinary operator interface FTriOp { short apply(int i, short a, short b, short c); } abstract ShortVector tOp(Vector o1, Vector o2, FTriOp f); abstract ShortVector tOp(Vector o1, Vector o2, Mask m, FTriOp f); // Reduction operator abstract short rOp(short v, FBinOp f); // Binary test interface FBinTest { boolean apply(int i, short a, short b); } abstract Mask bTest(Vector o, FBinTest f); // Foreach interface FUnCon { void apply(int i, short a); } abstract void forEach(FUnCon f); abstract void forEach(Mask m, FUnCon f); // @Override public ShortVector add(Vector o) { return bOp(o, (i, a, b) -> (short) (a + b)); } public abstract ShortVector add(short o); @Override public ShortVector add(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) (a + b)); } public abstract ShortVector add(short o, Mask m); @Override public ShortVector addSaturate(Vector o) { return bOp(o, (i, a, b) -> (short) ((a >= Integer.MAX_VALUE || Integer.MAX_VALUE - b > a) ? Integer.MAX_VALUE : a + b)); } public abstract ShortVector addSaturate(short o); @Override public ShortVector addSaturate(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) ((a >= Integer.MAX_VALUE || Integer.MAX_VALUE - b > a) ? Integer.MAX_VALUE : a + b)); } public abstract ShortVector addSaturate(short o, Mask m); @Override public ShortVector sub(Vector o) { return bOp(o, (i, a, b) -> (short) (a - b)); } public abstract ShortVector sub(short o); @Override public ShortVector sub(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) (a - b)); } public abstract ShortVector sub(short o, Mask m); @Override public ShortVector subSaturate(Vector o) { return bOp(o, (i, a, b) -> (short) ((a >= Integer.MIN_VALUE || Integer.MIN_VALUE + b > a) ? Integer.MAX_VALUE : a - b)); } public abstract ShortVector subSaturate(short o); @Override public ShortVector subSaturate(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) ((a >= Integer.MIN_VALUE || Integer.MIN_VALUE + b > a) ? Integer.MAX_VALUE : a - b)); } public abstract ShortVector subSaturate(short o, Mask m); @Override public ShortVector mul(Vector o) { return bOp(o, (i, a, b) -> (short) (a * b)); } public abstract ShortVector mul(short o); @Override public ShortVector mul(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) (a * b)); } public abstract ShortVector mul(short o, Mask m); @Override public ShortVector neg() { return uOp((i, a) -> (short) (-a)); } @Override public ShortVector neg(Mask m) { return uOp(m, (i, a) -> (short) (-a)); } @Override public ShortVector abs() { return uOp((i, a) -> (short) Math.abs(a)); } @Override public ShortVector abs(Mask m) { return uOp(m, (i, a) -> (short) Math.abs(a)); } @Override public ShortVector min(Vector o) { return bOp(o, (i, a, b) -> (a <= b) ? a : b); } public abstract ShortVector min(short o); @Override public ShortVector max(Vector o) { return bOp(o, (i, a, b) -> (a >= b) ? a : b); } public abstract ShortVector max(short o); @Override public Mask equal(Vector o) { return bTest(o, (i, a, b) -> a == b); } public abstract Mask equal(short o); @Override public Mask notEqual(Vector o) { return bTest(o, (i, a, b) -> a != b); } public abstract Mask notEqual(short o); @Override public Mask lessThan(Vector o) { return bTest(o, (i, a, b) -> a < b); } public abstract Mask lessThan(short o); @Override public Mask lessThanEq(Vector o) { return bTest(o, (i, a, b) -> a <= b); } public abstract Mask lessThanEq(short o); @Override public Mask greaterThan(Vector o) { return bTest(o, (i, a, b) -> a > b); } public abstract Mask greaterThan(short o); @Override public Mask greaterThanEq(Vector o) { return bTest(o, (i, a, b) -> a >= b); } public abstract Mask greaterThanEq(short o); @Override public ShortVector blend(Vector o, Mask m) { return bOp(o, (i, a, b) -> m.getElement(i) ? b : a); } public abstract ShortVector blend(short o, Mask m); @Override public abstract ShortVector shuffle(Vector o, Shuffle m); @Override public abstract ShortVector swizzle(Shuffle m); @Override @ForceInline public ShortVector resize(Species species) { return (ShortVector) species.resize(this); } @Override public abstract ShortVector rotateEL(int i); @Override public abstract ShortVector rotateER(int i); @Override public abstract ShortVector shiftEL(int i); @Override public abstract ShortVector shiftER(int i); public ShortVector and(Vector o) { return bOp(o, (i, a, b) -> (short) (a & b)); } public abstract ShortVector and(short o); public ShortVector and(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) (a & b)); } public abstract ShortVector and(short o, Mask m); public ShortVector or(Vector o) { return bOp(o, (i, a, b) -> (short) (a | b)); } public abstract ShortVector or(short o); public ShortVector or(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) (a | b)); } public abstract ShortVector or(short o, Mask m); public ShortVector xor(Vector o) { return bOp(o, (i, a, b) -> (short) (a ^ b)); } public abstract ShortVector xor(short o); public ShortVector xor(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) (a ^ b)); } public abstract ShortVector xor(short o, Mask m); public ShortVector not() { return uOp((i, a) -> (short) (~a)); } public ShortVector not(Mask m) { return uOp(m, (i, a) -> (short) (~a)); } // logical shift left public ShortVector shiftL(Vector o) { return bOp(o, (i, a, b) -> (short) (a << b)); } public ShortVector shiftL(int s) { return uOp((i, a) -> (short) (a << s)); } public ShortVector shiftL(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) (a << b)); } public ShortVector shiftL(int s, Mask m) { return uOp(m, (i, a) -> (short) (a << s)); } // logical, or unsigned, shift right public ShortVector shiftR(Vector o) { return bOp(o, (i, a, b) -> (short) (a >>> b)); } public ShortVector shiftR(int s) { return uOp((i, a) -> (short) (a >>> s)); } public ShortVector shiftR(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) (a >>> b)); } public ShortVector shiftR(int s, Mask m) { return uOp(m, (i, a) -> (short) (a >>> s)); } // arithmetic, or signed, shift right public ShortVector ashiftR(Vector o) { return bOp(o, (i, a, b) -> (short) (a >> b)); } public ShortVector aShiftR(int s) { return uOp((i, a) -> (short) (a >> s)); } public ShortVector ashiftR(Vector o, Mask m) { return bOp(o, m, (i, a, b) -> (short) (a >> b)); } public ShortVector aShiftR(int s, Mask m) { return uOp(m, (i, a) -> (short) (a >> s)); } public ShortVector rotateL(int j) { return uOp((i, a) -> (short) Integer.rotateLeft(a, j)); } public ShortVector rotateR(int j) { return uOp((i, a) -> (short) Integer.rotateRight(a, j)); } @Override public void intoByteArray(byte[] a, int ix) { ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); intoByteBuffer(bb); } @Override public void intoByteArray(byte[] a, int ix, Mask m) { ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); intoByteBuffer(bb, m); } @Override public void intoByteBuffer(ByteBuffer bb) { ShortBuffer fb = bb.asShortBuffer(); forEach((i, a) -> fb.put(a)); } @Override public void intoByteBuffer(ByteBuffer bb, Mask m) { ShortBuffer fb = bb.asShortBuffer(); forEach((i, a) -> { if (m.getElement(i)) fb.put(a); else fb.position(fb.position() + 1); }); } @Override public void intoByteBuffer(ByteBuffer bb, int ix) { bb = bb.duplicate().position(ix); ShortBuffer fb = bb.asShortBuffer(); forEach((i, a) -> fb.put(i, a)); } @Override public void intoByteBuffer(ByteBuffer bb, int ix, Mask m) { bb = bb.duplicate().position(ix); ShortBuffer fb = bb.asShortBuffer(); forEach(m, (i, a) -> fb.put(i, a)); } // Type specific horizontal reductions public short addAll() { return rOp((short) 0, (i, a, b) -> (short) (a + b)); } public short subAll() { return rOp((short) 0, (i, a, b) -> (short) (a - b)); } public short mulAll() { return rOp((short) 1, (i, a, b) -> (short) (a * b)); } public short minAll() { return rOp(Short.MAX_VALUE, (i, a, b) -> a > b ? b : a); } public short maxAll() { return rOp(Short.MIN_VALUE, (i, a, b) -> a < b ? b : a); } public short orAll() { return rOp((short) 0, (i, a, b) -> (short) (a | b)); } public short andAll() { return rOp((short) -1, (i, a, b) -> (short) (a & b)); } public short xorAll() { return rOp((short) 0, (i, a, b) -> (short) (a ^ b)); } // Type specific accessors public abstract short get(int i); public abstract ShortVector with(int i, short e); // Type specific extractors @ForceInline public short[] toArray() { short[] a = new short[species().length()]; intoArray(a, 0); return a; } public void intoArray(short[] a, int ax) { forEach((i, a_) -> a[ax + i] = a_); } public void intoArray(short[] a, int ax, Mask m) { forEach(m, (i, a_) -> a[ax + i] = a_); } public void intoArray(short[] a, int ax, int[] indexMap, int mx) { forEach((i, a_) -> a[ax + indexMap[mx + i]] = a_); } public void intoArray(short[] a, int ax, Mask m, int[] indexMap, int mx) { forEach(m, (i, a_) -> a[ax + indexMap[mx + i]] = a_); } // Species @Override public abstract ShortSpecies species(); public static abstract class ShortSpecies extends Vector.Species { interface FOp { short apply(int i); } abstract ShortVector op(FOp f); abstract ShortVector op(Mask m, FOp f); // Factories @Override public ShortVector zero() { return op(i -> 0); } public ShortVector broadcast(short e) { return op(i -> e); } public ShortVector single(short e) { return op(i -> i == 0 ? e : (short) 0); } public ShortVector random() { ThreadLocalRandom r = ThreadLocalRandom.current(); return op(i -> (short) r.nextInt()); } public ShortVector scalars(short... es) { return op(i -> es[i]); } public ShortVector fromArray(short[] a, int ax) { return op(i -> a[ax + i]); } public ShortVector fromArray(short[] a, int ax, Mask m) { return op(m, i -> a[ax + i]); } public ShortVector fromArray(short[] a, int ax, int[] indexMap, int mx) { return op(i -> a[ax + indexMap[mx + i]]); } public ShortVector fromArray(short[] a, int ax, Mask m, int[] indexMap, int mx) { return op(m, i -> a[ax + indexMap[mx + i]]); } @Override public ShortVector fromByteArray(byte[] a, int ix) { ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); return fromByteBuffer(bb); } @Override public ShortVector fromByteArray(byte[] a, int ix, Mask m) { ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); return fromByteBuffer(bb, m); } @Override public ShortVector fromByteBuffer(ByteBuffer bb) { ShortBuffer fb = bb.asShortBuffer(); return op(i -> fb.get()); } @Override public ShortVector fromByteBuffer(ByteBuffer bb, Mask m) { ShortBuffer fb = bb.asShortBuffer(); return op(i -> { if(m.getElement(i)) return fb.get(); else { fb.position(fb.position() + 1); return (short) 0; } }); } @Override public ShortVector fromByteBuffer(ByteBuffer bb, int ix) { bb = bb.duplicate().position(ix); ShortBuffer fb = bb.asShortBuffer(); return op(i -> fb.get(i)); } @Override public ShortVector fromByteBuffer(ByteBuffer bb, int ix, Mask m) { bb = bb.duplicate().position(ix); ShortBuffer fb = bb.asShortBuffer(); return op(m, i -> fb.get(i)); } @Override @ForceInline public ShortVector reshape(Vector o) { int blen = Math.max(o.species().bitSize(), bitSize()) / Byte.SIZE; ByteBuffer bb = ByteBuffer.allocate(blen).order(ByteOrder.nativeOrder()); o.intoByteBuffer(bb, 0); return fromByteBuffer(bb, 0); } @Override @ForceInline public ShortVector rebracket(Vector o) { return reshape(o); } @Override @ForceInline public ShortVector resize(Vector o) { return reshape(o); } @Override @SuppressWarnings("unchecked") public ShortVector cast(Vector v) { // Allocate array of required size short[] a = new short[length()]; Class vtype = v.species().elementType(); int limit = Math.min(v.species().length(), length()); if (vtype == Byte.class) { ByteVector tv = (ByteVector)v; for (int i = 0; i < limit; i++) { a[i] = (short) tv.get(i); } } else if (vtype == Short.class) { ShortVector tv = (ShortVector)v; for (int i = 0; i < limit; i++) { a[i] = (short) tv.get(i); } } else if (vtype == Integer.class) { IntVector tv = (IntVector)v; for (int i = 0; i < limit; i++) { a[i] = (short) tv.get(i); } } else if (vtype == Long.class){ LongVector tv = (LongVector)v; for (int i = 0; i < limit; i++) { a[i] = (short) tv.get(i); } } else if (vtype == Float.class){ FloatVector tv = (FloatVector)v; for (int i = 0; i < limit; i++) { a[i] = (short) tv.get(i); } } else if (vtype == Double.class){ DoubleVector tv = (DoubleVector)v; for (int i = 0; i < limit; i++) { a[i] = (short) tv.get(i); } } else { throw new UnsupportedOperationException("Bad lane type for casting."); } return scalars(a); } } }