- * The method behaves as if the input vector is stored into a byte array - * and then the returned vector is loaded from the byte array. + * The method behaves as if the input vector is stored into a byte buffer + * and then the returned vector is loaded from the byte buffer using + * native byte ordering. The implication is that ByteBuffer reads bytes + * and then composes them based on the byte ordering so the result + * depends on this composition. + *

+ * For example, on a system with ByteOrder.LITTLE_ENDIAN, loading from + * byte array with values {0,1,2,3} and reshaping to int, leads to bytes + * being composed in order 0x3 0x2 0x1 0x0 which is decimal value 50462976. + * On a system with ByteOrder.BIG_ENDIAN, the value is instead 66051 because + * bytes are composed in order 0x0 0x1 0x2 0x3. + *

* The following pseudocode expresses the behaviour: *

```{@code
-         * int alen = Math.max(v.bitSize(), this.bitSize()) / Byte.SIZE;
-         * byte[] a = new byte[alen];
-         * v.intoByteArray(a, 0);
-         * return this.fromByteArray(a, 0);
+         * int blen = Math.max(v.bitSize(), bitSize()) / Byte.SIZE;
+         * ByteBuffer bb = ByteBuffer.allocate(blen).order(ByteOrder.nativeOrder());
+         * v.intoByteBuffer(bb, 0);
+         * return fromByteBuffer(bb, 0);
* }```
* * @param v the input vector @@ -1075,13 +1085,23 @@ * The underlying bits of the input vector are copied without * modification to the resulting vector. *

- * The method behaves as if the input vector is stored into a byte array - * and then the returned vector is loaded from the byte array. + * The method behaves as if the input vector is stored into a byte buffer + * and then the returned vector is loaded from the byte buffer using + * native byte ordering. The implication is that ByteBuffer reads bytes + * and then composes them based on the byte ordering so the result + * depends on this composition. + *

+ * For example, on a system with ByteOrder.LITTLE_ENDIAN, loading from + * byte array with values {0,1,2,3} and rebracketing to int, leads to bytes + * being composed in order 0x3 0x2 0x1 0x0 which is decimal value 50462976. + * On a system with ByteOrder.BIG_ENDIAN, the value is instead 66051 because + * bytes are composed in order 0x0 0x1 0x2 0x3. + *

* The following pseudocode expresses the behaviour: *

```{@code
-         * byte[] a = new byte[v.bitSize() / Byte.SIZE];
-         * v.intoByteArray(a, 0);
-         * return this.fromByteArray(a, 0);
+         * ByteBuffer bb = ByteBuffer.allocate(v.bitSize()).order(ByteOrder.nativeOrder());
+         * v.intoByteBuffer(bb, 0);
+         * return fromByteBuffer(bb, 0);
* }```
* * @param v the input vector diff -r 284a9dc9346b -r de0c85104155 src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template --- a/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template Wed Apr 11 14:55:00 2018 -0700 +++ b/src/jdk.incubator.vector/share/classes/jdk/incubator/vector/X-Vector.java.template Thu Apr 12 18:29:27 2018 -0700 @@ -602,13 +602,13 @@ @Override public void intoByteArray(byte[] a, int ix) { - ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); + ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix).order(ByteOrder.nativeOrder()); intoByteBuffer(bb); } @Override public void intoByteArray(byte[] a, int ix, Mask<\$Boxtype\$, S> m) { - ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); + ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix).order(ByteOrder.nativeOrder()); intoByteBuffer(bb, m); } @@ -1011,13 +1011,13 @@ @Override public \$abstractvectortype\$ fromByteArray(byte[] a, int ix) { - ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); + ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix).order(ByteOrder.nativeOrder()); return fromByteBuffer(bb); } @Override public \$abstractvectortype\$ fromByteArray(byte[] a, int ix, Mask<\$Boxtype\$, S> m) { - ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix); + ByteBuffer bb = ByteBuffer.wrap(a, ix, a.length - ix).order(ByteOrder.nativeOrder()); return fromByteBuffer(bb, m); } @@ -1042,14 +1042,14 @@ @Override public \$abstractvectortype\$ fromByteBuffer(ByteBuffer bb, int ix) { - bb = bb.duplicate().position(ix); + bb = bb.duplicate().order(bb.order()).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); + bb = bb.duplicate().order(bb.order()).position(ix); \$Type\$Buffer fb = bb{#if[byte]?;:.as\$Type\$Buffer();} return op(m, i -> fb.get(i)); }