/* * 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; #if[!byte] import java.nio.$Type$Buffer; #end[!byte] import java.util.concurrent.ThreadLocalRandom; @SuppressWarnings("cast") public abstract class $abstractvectortype$ extends Vector<$Boxtype$,S> { $abstractvectortype$() {} // Unary operator interface FUnOp { $type$ apply(int i, $type$ a); } abstract $abstractvectortype$ uOp(FUnOp f); abstract $abstractvectortype$ uOp(Mask<$Boxtype$, S> m, FUnOp f); // Binary operator interface FBinOp { $type$ apply(int i, $type$ a, $type$ b); } abstract $abstractvectortype$ bOp(Vector<$Boxtype$,S> o, FBinOp f); abstract $abstractvectortype$ bOp(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m, FBinOp f); // Trinary operator interface FTriOp { $type$ apply(int i, $type$ a, $type$ b, $type$ c); } abstract $abstractvectortype$ tOp(Vector<$Boxtype$,S> o1, Vector<$Boxtype$,S> o2, FTriOp f); abstract $abstractvectortype$ tOp(Vector<$Boxtype$,S> o1, Vector<$Boxtype$,S> o2, Mask<$Boxtype$, S> m, FTriOp f); // Reduction operator abstract $type$ rOp($type$ v, FBinOp f); // Binary test interface FBinTest { boolean apply(int i, $type$ a, $type$ b); } abstract Mask<$Boxtype$, S> bTest(Vector<$Boxtype$,S> o, FBinTest f); // Foreach interface FUnCon { void apply(int i, $type$ a); } abstract void forEach(FUnCon f); abstract void forEach(Mask<$Boxtype$, S> m, FUnCon f); // @Override public $abstractvectortype$ add(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a + b)); } public abstract $abstractvectortype$ add($type$ o); @Override public $abstractvectortype$ add(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a + b)); } public abstract $abstractvectortype$ add($type$ o, Mask<$Boxtype$, S> m); @Override public $abstractvectortype$ addSaturate(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) ((a >= Integer.MAX_VALUE || Integer.MAX_VALUE - b > a) ? Integer.MAX_VALUE : a + b)); } public abstract $abstractvectortype$ addSaturate($type$ o); @Override public $abstractvectortype$ addSaturate(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) ((a >= Integer.MAX_VALUE || Integer.MAX_VALUE - b > a) ? Integer.MAX_VALUE : a + b)); } public abstract $abstractvectortype$ addSaturate($type$ o, Mask<$Boxtype$, S> m); @Override public $abstractvectortype$ sub(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a - b)); } public abstract $abstractvectortype$ sub($type$ o); @Override public $abstractvectortype$ sub(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a - b)); } public abstract $abstractvectortype$ sub($type$ o, Mask<$Boxtype$, S> m); @Override public $abstractvectortype$ subSaturate(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) ((a >= $Wideboxtype$.MIN_VALUE || $Wideboxtype$.MIN_VALUE + b > a) ? $Wideboxtype$.MAX_VALUE : a - b)); } public abstract $abstractvectortype$ subSaturate($type$ o); @Override public $abstractvectortype$ subSaturate(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) ((a >= $Wideboxtype$.MIN_VALUE || $Wideboxtype$.MIN_VALUE + b > a) ? $Wideboxtype$.MAX_VALUE : a - b)); } public abstract $abstractvectortype$ subSaturate($type$ o, Mask<$Boxtype$, S> m); @Override public $abstractvectortype$ mul(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a * b)); } public abstract $abstractvectortype$ mul($type$ o); @Override public $abstractvectortype$ mul(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a * b)); } public abstract $abstractvectortype$ mul($type$ o, Mask<$Boxtype$, S> m); @Override public $abstractvectortype$ neg() { return uOp((i, a) -> ($type$) (-a)); } @Override public $abstractvectortype$ neg(Mask<$Boxtype$, S> m) { return uOp(m, (i, a) -> ($type$) (-a)); } @Override public $abstractvectortype$ abs() { return uOp((i, a) -> ($type$) Math.abs(a)); } @Override public $abstractvectortype$ abs(Mask<$Boxtype$, S> m) { return uOp(m, (i, a) -> ($type$) Math.abs(a)); } @Override public $abstractvectortype$ min(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> (a <= b) ? a : b); } public abstract $abstractvectortype$ min($type$ o); @Override public $abstractvectortype$ max(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> (a >= b) ? a : b); } public abstract $abstractvectortype$ max($type$ o); @Override public Mask<$Boxtype$, S> equal(Vector<$Boxtype$,S> o) { return bTest(o, (i, a, b) -> a == b); } public abstract Mask<$Boxtype$, S> equal($type$ o); @Override public Mask<$Boxtype$, S> notEqual(Vector<$Boxtype$,S> o) { return bTest(o, (i, a, b) -> a != b); } public abstract Mask<$Boxtype$, S> notEqual($type$ o); @Override public Mask<$Boxtype$, S> lessThan(Vector<$Boxtype$,S> o) { return bTest(o, (i, a, b) -> a < b); } public abstract Mask<$Boxtype$, S> lessThan($type$ o); @Override public Mask<$Boxtype$, S> lessThanEq(Vector<$Boxtype$,S> o) { return bTest(o, (i, a, b) -> a <= b); } public abstract Mask<$Boxtype$, S> lessThanEq($type$ o); @Override public Mask<$Boxtype$, S> greaterThan(Vector<$Boxtype$,S> o) { return bTest(o, (i, a, b) -> a > b); } public abstract Mask<$Boxtype$, S> greaterThan($type$ o); @Override public Mask<$Boxtype$, S> greaterThanEq(Vector<$Boxtype$,S> o) { return bTest(o, (i, a, b) -> a >= b); } public abstract Mask<$Boxtype$, S> greaterThanEq($type$ o); @Override public $abstractvectortype$ blend(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, (i, a, b) -> m.getElement(i) ? b : a); } public abstract $abstractvectortype$ blend($type$ o, Mask<$Boxtype$, S> m); @Override public abstract $abstractvectortype$ shuffle(Vector<$Boxtype$,S> o, Shuffle<$Boxtype$, S> m); @Override public abstract $abstractvectortype$ swizzle(Shuffle<$Boxtype$, S> m); @Override @ForceInline public $abstractvectortype$ resize(Species<$Boxtype$, T> species) { return ($abstractvectortype$) species.resize(this); } @Override public abstract $abstractvectortype$ rotateEL(int i); @Override public abstract $abstractvectortype$ rotateER(int i); @Override public abstract $abstractvectortype$ shiftEL(int i); @Override public abstract $abstractvectortype$ shiftER(int i); #if[FP] public $abstractvectortype$ div(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a / b)); } public abstract $abstractvectortype$ div($type$ o); public $abstractvectortype$ div(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a / b)); } public abstract $abstractvectortype$ div($type$ o, Mask<$Boxtype$, S> m); public $abstractvectortype$ sqrt() { return uOp((i, a) -> ($type$) Math.sqrt((double) a)); } public $abstractvectortype$ sqrt(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.sqrt((double) a)); } public $abstractvectortype$ tan() { return uOp((i, a) -> ($type$) Math.tan((double) a)); } public $abstractvectortype$ tan(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.tan((double) a)); } public $abstractvectortype$ tanh() { return uOp((i, a) -> ($type$) Math.tanh((double) a)); } public $abstractvectortype$ tanh(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.tanh((double) a)); } public $abstractvectortype$ sin() { return uOp((i, a) -> ($type$) Math.sin((double) a)); } public $abstractvectortype$ sin(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.sin((double) a)); } public $abstractvectortype$ sinh() { return uOp((i, a) -> ($type$) Math.sinh((double) a)); } public $abstractvectortype$ sinh(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.sinh((double) a)); } public $abstractvectortype$ cos() { return uOp((i, a) -> ($type$) Math.cos((double) a)); } public $abstractvectortype$ cos(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.cos((double) a)); } public $abstractvectortype$ cosh() { return uOp((i, a) -> ($type$) Math.cosh((double) a)); } public $abstractvectortype$ cosh(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.cosh((double) a)); } public $abstractvectortype$ asin() { return uOp((i, a) -> ($type$) Math.asin((double) a)); } public $abstractvectortype$ asin(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.asin((double) a)); } public $abstractvectortype$ acos() { return uOp((i, a) -> ($type$) Math.acos((double) a)); } public $abstractvectortype$ acos(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.acos((double) a)); } public $abstractvectortype$ atan() { return uOp((i, a) -> ($type$) Math.atan((double) a)); } public $abstractvectortype$ atan(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.atan((double) a)); } public $abstractvectortype$ atan2(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) Math.atan2((double) a, (double) b)); } public abstract $abstractvectortype$ atan2($type$ o); public $abstractvectortype$ atan2(Vector<$Boxtype$,S> o, Mask<$Boxtype$,S> m) { return bOp(o, m, (i, a, b) -> ($type$) Math.atan2((double) a, (double) b)); } public abstract $abstractvectortype$ atan2($type$ o, Mask<$Boxtype$,S> m); public $abstractvectortype$ cbrt() { return uOp((i, a) -> ($type$) Math.cbrt((double) a)); } public $abstractvectortype$ cbrt(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.cbrt((double) a)); } public $abstractvectortype$ log() { return uOp((i, a) -> ($type$) Math.log((double) a)); } public $abstractvectortype$ log(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.log((double) a)); } public $abstractvectortype$ log10() { return uOp((i, a) -> ($type$) Math.log10((double) a)); } public $abstractvectortype$ log10(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.log10((double) a)); } public $abstractvectortype$ log1p() { return uOp((i, a) -> ($type$) Math.log1p((double) a)); } public $abstractvectortype$ log1p(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.log1p((double) a)); } public $abstractvectortype$ pow(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) Math.pow((double) a, (double) b)); } public abstract $abstractvectortype$ pow($type$ o); public $abstractvectortype$ pow(Vector<$Boxtype$,S> o, Mask<$Boxtype$,S> m) { return bOp(o, m, (i, a, b) -> ($type$) Math.pow((double) a, (double) b)); } public abstract $abstractvectortype$ pow($type$ o, Mask<$Boxtype$,S> m); public $abstractvectortype$ exp() { return uOp((i, a) -> ($type$) Math.exp((double) a)); } public $abstractvectortype$ exp(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.exp((double) a)); } public $abstractvectortype$ expm1() { return uOp((i, a) -> ($type$) Math.expm1((double) a)); } public $abstractvectortype$ expm1(Mask<$Boxtype$,S> m) { return uOp(m, (i, a) -> ($type$) Math.expm1((double) a)); } public $abstractvectortype$ fma(Vector<$Boxtype$,S> o1, Vector<$Boxtype$,S> o2) { return tOp(o1, o2, (i, a, b, c) -> Math.fma(a, b, c)); } public abstract $abstractvectortype$ fma($type$ o1, $type$ o2); public $abstractvectortype$ fma(Vector<$Boxtype$,S> o1, Vector<$Boxtype$,S> o2, Mask<$Boxtype$,S> m) { return tOp(o1, o2, m, (i, a, b, c) -> Math.fma(a, b, c)); } public abstract $abstractvectortype$ fma($type$ o1, $type$ o2, Mask<$Boxtype$,S> m); public $abstractvectortype$ hypot(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) Math.hypot((double) a, (double) b)); } public abstract $abstractvectortype$ hypot($type$ o); public $abstractvectortype$ hypot(Vector<$Boxtype$,S> o, Mask<$Boxtype$,S> m) { return bOp(o, m, (i, a, b) -> ($type$) Math.hypot((double) a, (double) b)); } public abstract $abstractvectortype$ hypot($type$ o, Mask<$Boxtype$,S> m); #end[FP] #if[BITWISE] public $abstractvectortype$ and(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a & b)); } public abstract $abstractvectortype$ and($type$ o); public $abstractvectortype$ and(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a & b)); } public abstract $abstractvectortype$ and($type$ o, Mask<$Boxtype$, S> m); public $abstractvectortype$ or(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a | b)); } public abstract $abstractvectortype$ or($type$ o); public $abstractvectortype$ or(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a | b)); } public abstract $abstractvectortype$ or($type$ o, Mask<$Boxtype$, S> m); public $abstractvectortype$ xor(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a ^ b)); } public abstract $abstractvectortype$ xor($type$ o); public $abstractvectortype$ xor(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a ^ b)); } public abstract $abstractvectortype$ xor($type$ o, Mask<$Boxtype$, S> m); public $abstractvectortype$ not() { return uOp((i, a) -> ($type$) (~a)); } public $abstractvectortype$ not(Mask<$Boxtype$, S> m) { return uOp(m, (i, a) -> ($type$) (~a)); } // logical shift left public $abstractvectortype$ shiftL(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a << b)); } public $abstractvectortype$ shiftL(int s) { return uOp((i, a) -> ($type$) (a << s)); } public $abstractvectortype$ shiftL(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a << b)); } public $abstractvectortype$ shiftL(int s, Mask<$Boxtype$, S> m) { return uOp(m, (i, a) -> ($type$) (a << s)); } // logical, or unsigned, shift right public $abstractvectortype$ shiftR(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a >>> b)); } public $abstractvectortype$ shiftR(int s) { return uOp((i, a) -> ($type$) (a >>> s)); } public $abstractvectortype$ shiftR(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a >>> b)); } public $abstractvectortype$ shiftR(int s, Mask<$Boxtype$, S> m) { return uOp(m, (i, a) -> ($type$) (a >>> s)); } // arithmetic, or signed, shift right public $abstractvectortype$ ashiftR(Vector<$Boxtype$,S> o) { return bOp(o, (i, a, b) -> ($type$) (a >> b)); } public $abstractvectortype$ aShiftR(int s) { return uOp((i, a) -> ($type$) (a >> s)); } public $abstractvectortype$ ashiftR(Vector<$Boxtype$,S> o, Mask<$Boxtype$, S> m) { return bOp(o, m, (i, a, b) -> ($type$) (a >> b)); } public $abstractvectortype$ aShiftR(int s, Mask<$Boxtype$, S> m) { return uOp(m, (i, a) -> ($type$) (a >> s)); } public $abstractvectortype$ rotateL(int j) { return uOp((i, a) -> ($type$) $Wideboxtype$.rotateLeft(a, j)); } public $abstractvectortype$ rotateR(int j) { return uOp((i, a) -> ($type$) $Wideboxtype$.rotateRight(a, j)); } #end[BITWISE] @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<$Boxtype$, S> m) { ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); intoByteBuffer(bb, m); } @Override public void intoByteBuffer(ByteBuffer bb) { $Type$Buffer fb = bb{#if[byte]?;:.as$Type$Buffer();} forEach((i, a) -> fb.put(a)); } @Override public void intoByteBuffer(ByteBuffer bb, Mask<$Boxtype$, S> m) { $Type$Buffer fb = bb{#if[byte]?;:.as$Type$Buffer();} 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); $Type$Buffer fb = bb{#if[byte]?;:.as$Type$Buffer();} forEach((i, a) -> fb.put(i, a)); } @Override public void intoByteBuffer(ByteBuffer bb, int ix, Mask<$Boxtype$, S> m) { bb = bb.duplicate().position(ix); $Type$Buffer fb = bb{#if[byte]?;:.as$Type$Buffer();} forEach(m, (i, a) -> fb.put(i, a)); } // Type specific horizontal reductions public $type$ addAll() { return rOp(($type$) 0, (i, a, b) -> ($type$) (a + b)); } public $type$ subAll() { return rOp(($type$) 0, (i, a, b) -> ($type$) (a - b)); } public $type$ mulAll() { return rOp(($type$) 1, (i, a, b) -> ($type$) (a * b)); } public $type$ minAll() { return rOp($Boxtype$.MAX_VALUE, (i, a, b) -> a > b ? b : a); } public $type$ maxAll() { return rOp($Boxtype$.MIN_VALUE, (i, a, b) -> a < b ? b : a); } #if[BITWISE] public $type$ orAll() { return rOp(($type$) 0, (i, a, b) -> ($type$) (a | b)); } public $type$ andAll() { return rOp(($type$) -1, (i, a, b) -> ($type$) (a & b)); } public $type$ xorAll() { return rOp(($type$) 0, (i, a, b) -> ($type$) (a ^ b)); } #end[BITWISE] // Type specific accessors public abstract $type$ get(int i); public abstract $abstractvectortype$ with(int i, $type$ e); // Type specific extractors @ForceInline public $type$[] toArray() { $type$[] a = new $type$[species().length()]; intoArray(a, 0); return a; } public void intoArray($type$[] a, int ax) { forEach((i, a_) -> a[ax + i] = a_); } public void intoArray($type$[] a, int ax, Mask<$Boxtype$, S> m) { forEach(m, (i, a_) -> a[ax + i] = a_); } public void intoArray($type$[] a, int ax, int[] indexMap, int mx) { forEach((i, a_) -> a[ax + indexMap[mx + i]] = a_); } public void intoArray($type$[] a, int ax, Mask<$Boxtype$, S> m, int[] indexMap, int mx) { forEach(m, (i, a_) -> a[ax + indexMap[mx + i]] = a_); } // Species @Override public abstract $Type$Species species(); public static abstract class $Type$Species extends Vector.Species<$Boxtype$, S> { interface FOp { $type$ apply(int i); } abstract $abstractvectortype$ op(FOp f); abstract $abstractvectortype$ op(Mask<$Boxtype$, S> m, FOp f); // Factories @Override public $abstractvectortype$ zero() { return op(i -> 0); } public $abstractvectortype$ broadcast($type$ e) { return op(i -> e); } public $abstractvectortype$ single($type$ e) { return op(i -> i == 0 ? e : ($type$) 0); } #if[intOrLong] public $abstractvectortype$ random() { ThreadLocalRandom r = ThreadLocalRandom.current(); return op(i -> r.next$Type$()); } #else[intOrLong] #if[FP] public $abstractvectortype$ random() { ThreadLocalRandom r = ThreadLocalRandom.current(); return op(i -> r.next$Type$()); } #else[FP] public $abstractvectortype$ random() { ThreadLocalRandom r = ThreadLocalRandom.current(); return op(i -> ($type$) r.nextInt()); } #end[FP] #end[intOrLong] public $abstractvectortype$ scalars($type$... es) { return op(i -> es[i]); } public $abstractvectortype$ fromArray($type$[] a, int ax) { return op(i -> a[ax + i]); } public $abstractvectortype$ fromArray($type$[] a, int ax, Mask<$Boxtype$, S> m) { return op(m, i -> a[ax + i]); } public $abstractvectortype$ fromArray($type$[] a, int ax, int[] indexMap, int mx) { return op(i -> a[ax + indexMap[mx + i]]); } public $abstractvectortype$ fromArray($type$[] a, int ax, Mask<$Boxtype$, S> m, int[] indexMap, int mx) { return op(m, i -> a[ax + indexMap[mx + i]]); } @Override public $abstractvectortype$ fromByteArray(byte[] a, int ix) { ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); return fromByteBuffer(bb); } @Override public $abstractvectortype$ fromByteArray(byte[] a, int ix, Mask<$Boxtype$, S> m) { ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); return fromByteBuffer(bb, m); } @Override public $abstractvectortype$ fromByteBuffer(ByteBuffer bb) { $Type$Buffer fb = bb{#if[byte]?;:.as$Type$Buffer();} return op(i -> fb.get()); } @Override public $abstractvectortype$ fromByteBuffer(ByteBuffer bb, Mask<$Boxtype$, S> m) { $Type$Buffer fb = bb{#if[byte]?;:.as$Type$Buffer();} return op(i -> { if(m.getElement(i)) return fb.get(); else { fb.position(fb.position() + 1); return ($type$) 0; } }); } @Override public $abstractvectortype$ fromByteBuffer(ByteBuffer bb, int ix) { bb = bb.duplicate().position(ix); $Type$Buffer fb = bb{#if[byte]?;:.as$Type$Buffer();} return op(i -> fb.get(i)); } @Override public $abstractvectortype$ fromByteBuffer(ByteBuffer bb, int ix, Mask<$Boxtype$, S> m) { bb = bb.duplicate().position(ix); $Type$Buffer fb = bb{#if[byte]?;:.as$Type$Buffer();} return op(m, i -> fb.get(i)); } @Override @ForceInline public $abstractvectortype$ 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 $abstractvectortype$ rebracket(Vector o) { return reshape(o); } @Override @ForceInline public $abstractvectortype$ resize(Vector<$Boxtype$, T> o) { return reshape(o); } @Override @SuppressWarnings("unchecked") public $abstractvectortype$ cast(Vector v) { // Allocate array of required size $type$[] a = new $type$[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] = ($type$) tv.get(i); } } else if (vtype == Short.class) { ShortVector tv = (ShortVector)v; for (int i = 0; i < limit; i++) { a[i] = ($type$) tv.get(i); } } else if (vtype == Integer.class) { IntVector tv = (IntVector)v; for (int i = 0; i < limit; i++) { a[i] = ($type$) tv.get(i); } } else if (vtype == Long.class){ LongVector tv = (LongVector)v; for (int i = 0; i < limit; i++) { a[i] = ($type$) tv.get(i); } } else if (vtype == Float.class){ FloatVector tv = (FloatVector)v; for (int i = 0; i < limit; i++) { a[i] = ($type$) tv.get(i); } } else if (vtype == Double.class){ DoubleVector tv = (DoubleVector)v; for (int i = 0; i < limit; i++) { a[i] = ($type$) tv.get(i); } } else { throw new UnsupportedOperationException("Bad lane type for casting."); } return scalars(a); } } }