< prev index next >

src/jdk.incubator.vector/share/classes/jdk/incubator/vector/AbstractVector.java

Print this page

  1 /*
  2  * Copyright (c) 2019, 2021, 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 any
 23  * questions.
 24  */
 25 package jdk.incubator.vector;
 26 

 27 import jdk.internal.vm.annotation.ForceInline;
 28 import jdk.internal.vm.vector.VectorSupport;
 29 
 30 import java.nio.ByteBuffer;
 31 import java.nio.ByteOrder;
 32 import java.util.function.IntUnaryOperator;
 33 
 34 import static jdk.incubator.vector.VectorOperators.*;
 35 
 36 @SuppressWarnings("cast")
 37 abstract class AbstractVector<E> extends Vector<E> {
 38     /**
 39      * The order of vector bytes when stored in natural,
 40      * array elements of the same lane type.
 41      * This is the also the behavior of the
 42      * VectorSupport load/store instructions.
 43      * If these instructions gain the capability to do
 44      * byte swapping on the fly, add a bit to those
 45      * instructions, but let this polarity be the
 46      * "neutral" or "default" setting of the bit.
 47      */
 48     /*package-private*/
 49     static final ByteOrder NATIVE_ENDIAN = ByteOrder.nativeOrder();
 50 

177     @ForceInline
178     ByteVector asByteVectorRawTemplate() {
179         return (ByteVector) asVectorRawTemplate(LaneType.BYTE);
180     }
181 
182 
183     abstract AbstractMask<E> maskFromArray(boolean[] bits);
184 
185     abstract AbstractShuffle<E> iotaShuffle();
186 
187     abstract AbstractShuffle<E> iotaShuffle(int start, int step, boolean wrap);
188 
189     /*do not alias this byte array*/
190     abstract AbstractShuffle<E> shuffleFromBytes(byte[] reorder);
191 
192     abstract AbstractShuffle<E> shuffleFromArray(int[] indexes, int i);
193 
194     abstract AbstractShuffle<E> shuffleFromOp(IntUnaryOperator fn);
195 
196     /*package-private*/
197     abstract AbstractVector<E> fromByteArray0(byte[] a, int offset);
198 
199     /*package-private*/
200     abstract AbstractVector<E> maybeSwap(ByteOrder bo);
201 
202     /*package-private*/
203     @ForceInline
204     VectorShuffle<Byte> swapBytesShuffle() {
205         return vspecies().swapBytesShuffle();
206     }
207 
208     /**
209      * {@inheritDoc} <!--workaround-->
210      */
211     @Override
212     @ForceInline
213     public ShortVector reinterpretAsShorts() {
214         return (ShortVector) asVectorRaw(LaneType.SHORT);
215     }
216 
217     /**

487         String msg = String.format("bad part number %d %s %s -> %s%s",
488                                    part, converting, dsp, rsp, laneChange);
489         return new ArrayIndexOutOfBoundsException(msg);
490     }
491 
492     /*package-private*/
493     ArithmeticException divZeroException() {
494         throw new ArithmeticException("zero vector lane in dividend "+this);
495     }
496 
497     /**
498      * Helper function for all sorts of byte-wise reinterpretation casts.
499      * This function kicks in after intrinsic failure.
500      */
501     /*package-private*/
502     @ForceInline
503     final <F>
504     AbstractVector<F> defaultReinterpret(AbstractSpecies<F> rsp) {
505         int blen = Math.max(this.bitSize(), rsp.vectorBitSize()) / Byte.SIZE;
506         ByteOrder bo = ByteOrder.nativeOrder();
507         ByteBuffer bb = ByteBuffer.allocate(blen);
508         this.intoByteBuffer(bb, 0, bo);
509         VectorMask<F> m = rsp.maskAll(true);
510         // enum-switches don't optimize properly JDK-8161245
511         switch (rsp.laneType.switchKey) {
512         case LaneType.SK_BYTE:
513             return ByteVector.fromByteBuffer(rsp.check(byte.class), bb, 0, bo, m.check(byte.class)).check0(rsp);
514         case LaneType.SK_SHORT:
515             return ShortVector.fromByteBuffer(rsp.check(short.class), bb, 0, bo, m.check(short.class)).check0(rsp);
516         case LaneType.SK_INT:
517             return IntVector.fromByteBuffer(rsp.check(int.class), bb, 0, bo, m.check(int.class)).check0(rsp);
518         case LaneType.SK_LONG:
519             return LongVector.fromByteBuffer(rsp.check(long.class), bb, 0, bo, m.check(long.class)).check0(rsp);
520         case LaneType.SK_FLOAT:
521             return FloatVector.fromByteBuffer(rsp.check(float.class), bb, 0, bo, m.check(float.class)).check0(rsp);
522         case LaneType.SK_DOUBLE:
523             return DoubleVector.fromByteBuffer(rsp.check(double.class), bb, 0, bo, m.check(double.class)).check0(rsp);
524         default:
525             throw new AssertionError(rsp.toString());
526         }
527     }
528 
529     /**
530      * Helper function for all sorts of lane-wise conversions.
531      * This function kicks in after intrinsic failure.
532      */
533     /*package-private*/
534     @ForceInline
535     final <F>
536     AbstractVector<F> defaultCast(AbstractSpecies<F> dsp) {
537         int rlength = dsp.laneCount;
538         if (vspecies().laneType.elementKind == 'F') {
539             // Buffer input values in a double array.
540             double[] lanes = toDoubleArray();
541             int limit = Math.min(lanes.length, rlength);
542             // enum-switches don't optimize properly JDK-8161245
543             switch (dsp.laneType.switchKey) {

713                     vtype, etype, vlength,
714                     rvtype, rtype, rlength,
715                     this, rsp,
716                     AbstractVector::defaultCast);
717         case 'X':  // reinterpret cast, not lane-wise if lane sizes differ
718             rtype = rsp.elementType();
719             rlength = rsp.laneCount();
720             etype = this.elementType(); // (profile)
721             vlength = this.length();  // (profile)
722             rvtype = rsp.dummyVector().getClass();  // (profile)
723             vtype = this.getClass();
724             return VectorSupport.convert(VectorSupport.VECTOR_OP_REINTERPRET,
725                     vtype, etype, vlength,
726                     rvtype, rtype, rlength,
727                     this, rsp,
728                     AbstractVector::defaultReinterpret);
729         }
730         throw new AssertionError();
731     }
732 
733     // Byte buffer wrappers.
734     static ByteBuffer wrapper(ByteBuffer bb, ByteOrder bo) {
735         return bb.duplicate().order(bo);
736     }
737 
738     static ByteBuffer wrapper(byte[] a, ByteOrder bo) {
739         return ByteBuffer.wrap(a).order(bo);
740     }
741 
742     static {
743         // Recode uses of VectorSupport.reinterpret if this assertion fails:
744         assert(REGISTER_ENDIAN == ByteOrder.LITTLE_ENDIAN);
745     }
746 }

  1 /*
  2  * Copyright (c) 2019, 2022, 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 any
 23  * questions.
 24  */
 25 package jdk.incubator.vector;
 26 
 27 import jdk.incubator.foreign.MemorySegment;
 28 import jdk.internal.vm.annotation.ForceInline;
 29 import jdk.internal.vm.vector.VectorSupport;
 30 

 31 import java.nio.ByteOrder;
 32 import java.util.function.IntUnaryOperator;
 33 
 34 import static jdk.incubator.vector.VectorOperators.*;
 35 
 36 @SuppressWarnings("cast")
 37 abstract class AbstractVector<E> extends Vector<E> {
 38     /**
 39      * The order of vector bytes when stored in natural,
 40      * array elements of the same lane type.
 41      * This is the also the behavior of the
 42      * VectorSupport load/store instructions.
 43      * If these instructions gain the capability to do
 44      * byte swapping on the fly, add a bit to those
 45      * instructions, but let this polarity be the
 46      * "neutral" or "default" setting of the bit.
 47      */
 48     /*package-private*/
 49     static final ByteOrder NATIVE_ENDIAN = ByteOrder.nativeOrder();
 50 

177     @ForceInline
178     ByteVector asByteVectorRawTemplate() {
179         return (ByteVector) asVectorRawTemplate(LaneType.BYTE);
180     }
181 
182 
183     abstract AbstractMask<E> maskFromArray(boolean[] bits);
184 
185     abstract AbstractShuffle<E> iotaShuffle();
186 
187     abstract AbstractShuffle<E> iotaShuffle(int start, int step, boolean wrap);
188 
189     /*do not alias this byte array*/
190     abstract AbstractShuffle<E> shuffleFromBytes(byte[] reorder);
191 
192     abstract AbstractShuffle<E> shuffleFromArray(int[] indexes, int i);
193 
194     abstract AbstractShuffle<E> shuffleFromOp(IntUnaryOperator fn);
195 
196     /*package-private*/
197     abstract AbstractVector<E> fromMemorySegment0(MemorySegment ms, long offset);
198 
199     /*package-private*/
200     abstract AbstractVector<E> maybeSwap(ByteOrder bo);
201 
202     /*package-private*/
203     @ForceInline
204     VectorShuffle<Byte> swapBytesShuffle() {
205         return vspecies().swapBytesShuffle();
206     }
207 
208     /**
209      * {@inheritDoc} <!--workaround-->
210      */
211     @Override
212     @ForceInline
213     public ShortVector reinterpretAsShorts() {
214         return (ShortVector) asVectorRaw(LaneType.SHORT);
215     }
216 
217     /**

487         String msg = String.format("bad part number %d %s %s -> %s%s",
488                                    part, converting, dsp, rsp, laneChange);
489         return new ArrayIndexOutOfBoundsException(msg);
490     }
491 
492     /*package-private*/
493     ArithmeticException divZeroException() {
494         throw new ArithmeticException("zero vector lane in dividend "+this);
495     }
496 
497     /**
498      * Helper function for all sorts of byte-wise reinterpretation casts.
499      * This function kicks in after intrinsic failure.
500      */
501     /*package-private*/
502     @ForceInline
503     final <F>
504     AbstractVector<F> defaultReinterpret(AbstractSpecies<F> rsp) {
505         int blen = Math.max(this.bitSize(), rsp.vectorBitSize()) / Byte.SIZE;
506         ByteOrder bo = ByteOrder.nativeOrder();
507         MemorySegment ms = MemorySegment.ofArray(new byte[blen]);
508         this.intoMemorySegment(ms, 0, bo);
509         VectorMask<F> m = rsp.maskAll(true);
510         // enum-switches don't optimize properly JDK-8161245
511         switch (rsp.laneType.switchKey) {
512         case LaneType.SK_BYTE:
513             return ByteVector.fromMemorySegment(rsp.check(byte.class), ms, 0, bo, m.check(byte.class)).check0(rsp);
514         case LaneType.SK_SHORT:
515             return ShortVector.fromMemorySegment(rsp.check(short.class), ms, 0, bo, m.check(short.class)).check0(rsp);
516         case LaneType.SK_INT:
517             return IntVector.fromMemorySegment(rsp.check(int.class), ms, 0, bo, m.check(int.class)).check0(rsp);
518         case LaneType.SK_LONG:
519             return LongVector.fromMemorySegment(rsp.check(long.class), ms, 0, bo, m.check(long.class)).check0(rsp);
520         case LaneType.SK_FLOAT:
521             return FloatVector.fromMemorySegment(rsp.check(float.class), ms, 0, bo, m.check(float.class)).check0(rsp);
522         case LaneType.SK_DOUBLE:
523             return DoubleVector.fromMemorySegment(rsp.check(double.class), ms, 0, bo, m.check(double.class)).check0(rsp);
524         default:
525             throw new AssertionError(rsp.toString());
526         }
527     }
528 
529     /**
530      * Helper function for all sorts of lane-wise conversions.
531      * This function kicks in after intrinsic failure.
532      */
533     /*package-private*/
534     @ForceInline
535     final <F>
536     AbstractVector<F> defaultCast(AbstractSpecies<F> dsp) {
537         int rlength = dsp.laneCount;
538         if (vspecies().laneType.elementKind == 'F') {
539             // Buffer input values in a double array.
540             double[] lanes = toDoubleArray();
541             int limit = Math.min(lanes.length, rlength);
542             // enum-switches don't optimize properly JDK-8161245
543             switch (dsp.laneType.switchKey) {

713                     vtype, etype, vlength,
714                     rvtype, rtype, rlength,
715                     this, rsp,
716                     AbstractVector::defaultCast);
717         case 'X':  // reinterpret cast, not lane-wise if lane sizes differ
718             rtype = rsp.elementType();
719             rlength = rsp.laneCount();
720             etype = this.elementType(); // (profile)
721             vlength = this.length();  // (profile)
722             rvtype = rsp.dummyVector().getClass();  // (profile)
723             vtype = this.getClass();
724             return VectorSupport.convert(VectorSupport.VECTOR_OP_REINTERPRET,
725                     vtype, etype, vlength,
726                     rvtype, rtype, rlength,
727                     this, rsp,
728                     AbstractVector::defaultReinterpret);
729         }
730         throw new AssertionError();
731     }
732 









733     static {
734         // Recode uses of VectorSupport.reinterpret if this assertion fails:
735         assert(REGISTER_ENDIAN == ByteOrder.LITTLE_ENDIAN);
736     }
737 }
< prev index next >