8269246: Scoped ByteBuffer vector access

Reviewed-by: mcimadamore
This commit is contained in:
Paul Sandoz 2021-06-24 16:02:22 +00:00
parent 3fb28d3074
commit 63bcd3336e
42 changed files with 360 additions and 269 deletions

View file

@ -33,6 +33,7 @@ import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.vector.VectorSupport;
@ -3562,15 +3563,14 @@ public abstract class ByteVector extends AbstractVector<Byte> {
final
ByteVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
ByteSpecies vsp = vspecies();
return VectorSupport.load(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.get(o + i * 1));
});
return ScopedMemoryAccess.loadFromByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.get(o + i * 1));
});
}
// Unchecked storing operations in native byte order.
@ -3613,15 +3613,14 @@ public abstract class ByteVector extends AbstractVector<Byte> {
final
void intoByteBuffer0(ByteBuffer bb, int offset) {
ByteSpecies vsp = vspecies();
VectorSupport.store(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.put(o + i * 1, e));
});
ScopedMemoryAccess.storeIntoByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.put(o + i * 1, e));
});
}
// End of low-level memory operations.

View file

@ -33,6 +33,7 @@ import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.vector.VectorSupport;
@ -3173,15 +3174,14 @@ public abstract class DoubleVector extends AbstractVector<Double> {
final
DoubleVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
DoubleSpecies vsp = vspecies();
return VectorSupport.load(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getDouble(o + i * 8));
});
return ScopedMemoryAccess.loadFromByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getDouble(o + i * 8));
});
}
// Unchecked storing operations in native byte order.
@ -3224,15 +3224,14 @@ public abstract class DoubleVector extends AbstractVector<Double> {
final
void intoByteBuffer0(ByteBuffer bb, int offset) {
DoubleSpecies vsp = vspecies();
VectorSupport.store(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putDouble(o + i * 8, e));
});
ScopedMemoryAccess.storeIntoByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putDouble(o + i * 8, e));
});
}
// End of low-level memory operations.

View file

@ -33,6 +33,7 @@ import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.vector.VectorSupport;
@ -3160,15 +3161,14 @@ public abstract class FloatVector extends AbstractVector<Float> {
final
FloatVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
FloatSpecies vsp = vspecies();
return VectorSupport.load(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getFloat(o + i * 4));
});
return ScopedMemoryAccess.loadFromByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getFloat(o + i * 4));
});
}
// Unchecked storing operations in native byte order.
@ -3211,15 +3211,14 @@ public abstract class FloatVector extends AbstractVector<Float> {
final
void intoByteBuffer0(ByteBuffer bb, int offset) {
FloatSpecies vsp = vspecies();
VectorSupport.store(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putFloat(o + i * 4, e));
});
ScopedMemoryAccess.storeIntoByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putFloat(o + i * 4, e));
});
}
// End of low-level memory operations.

View file

@ -33,6 +33,7 @@ import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.vector.VectorSupport;
@ -3269,15 +3270,14 @@ public abstract class IntVector extends AbstractVector<Integer> {
final
IntVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
IntSpecies vsp = vspecies();
return VectorSupport.load(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getInt(o + i * 4));
});
return ScopedMemoryAccess.loadFromByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getInt(o + i * 4));
});
}
// Unchecked storing operations in native byte order.
@ -3320,15 +3320,14 @@ public abstract class IntVector extends AbstractVector<Integer> {
final
void intoByteBuffer0(ByteBuffer bb, int offset) {
IntSpecies vsp = vspecies();
VectorSupport.store(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putInt(o + i * 4, e));
});
ScopedMemoryAccess.storeIntoByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putInt(o + i * 4, e));
});
}
// End of low-level memory operations.

View file

@ -33,6 +33,7 @@ import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.vector.VectorSupport;
@ -3172,15 +3173,14 @@ public abstract class LongVector extends AbstractVector<Long> {
final
LongVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
LongSpecies vsp = vspecies();
return VectorSupport.load(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getLong(o + i * 8));
});
return ScopedMemoryAccess.loadFromByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getLong(o + i * 8));
});
}
// Unchecked storing operations in native byte order.
@ -3223,15 +3223,14 @@ public abstract class LongVector extends AbstractVector<Long> {
final
void intoByteBuffer0(ByteBuffer bb, int offset) {
LongSpecies vsp = vspecies();
VectorSupport.store(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putLong(o + i * 8, e));
});
ScopedMemoryAccess.storeIntoByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putLong(o + i * 8, e));
});
}
// End of low-level memory operations.

View file

@ -33,6 +33,7 @@ import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.vector.VectorSupport;
@ -3549,15 +3550,14 @@ public abstract class ShortVector extends AbstractVector<Short> {
final
ShortVector fromByteBuffer0Template(ByteBuffer bb, int offset) {
ShortSpecies vsp = vspecies();
return VectorSupport.load(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getShort(o + i * 2));
});
return ScopedMemoryAccess.loadFromByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.getShort(o + i * 2));
});
}
// Unchecked storing operations in native byte order.
@ -3600,15 +3600,14 @@ public abstract class ShortVector extends AbstractVector<Short> {
final
void intoByteBuffer0(ByteBuffer bb, int offset) {
ShortSpecies vsp = vspecies();
VectorSupport.store(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putShort(o + i * 2, e));
});
ScopedMemoryAccess.storeIntoByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.putShort(o + i * 2, e));
});
}
// End of low-level memory operations.

View file

@ -24,33 +24,11 @@
*/
package jdk.incubator.vector;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.util.Objects;
/*non-public*/ class VectorIntrinsics {
static final Unsafe U = Unsafe.getUnsafe();
static final long BUFFER_ADDRESS
= U.objectFieldOffset(Buffer.class, "address");
// Buffer.limit
static final long BUFFER_LIMIT
= U.objectFieldOffset(Buffer.class, "limit");
// ByteBuffer.hb
static final long BYTE_BUFFER_HB
= U.objectFieldOffset(ByteBuffer.class, "hb");
// ByteBuffer.isReadOnly
static final long BYTE_BUFFER_IS_READ_ONLY
= U.objectFieldOffset(ByteBuffer.class, "isReadOnly");
/* ============================================================================ */
static final int VECTOR_ACCESS_OOB_CHECK = Integer.getInteger("jdk.incubator.vector.VECTOR_ACCESS_OOB_CHECK", 2);
@ForceInline
@ -133,18 +111,4 @@ import java.util.Objects;
return Math.floorMod(index, Math.abs(size));
}
}
/* ============================================================================ */
/*package-private*/
@ForceInline
static Object bufferBase(ByteBuffer bb) {
return U.getReference(bb, BYTE_BUFFER_HB);
}
/*package-private*/
@ForceInline
static long bufferAddress(ByteBuffer bb, long offset) {
return U.getLong(bb, BUFFER_ADDRESS) + offset;
}
}

View file

@ -33,6 +33,7 @@ import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import jdk.internal.misc.ScopedMemoryAccess;
import jdk.internal.misc.Unsafe;
import jdk.internal.vm.annotation.ForceInline;
import jdk.internal.vm.vector.VectorSupport;
@ -4517,15 +4518,14 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
final
$abstractvectortype$ fromByteBuffer0Template(ByteBuffer bb, int offset) {
$Type$Species vsp = vspecies();
return VectorSupport.load(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.get{#if[byte]?(:$Type$(}o + i * $sizeInBytes$));
});
return ScopedMemoryAccess.loadFromByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bb, offset, vsp,
(buf, off, s) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
return s.ldOp(wb, off,
(wb_, o, i) -> wb_.get{#if[byte]?(:$Type$(}o + i * $sizeInBytes$));
});
}
// Unchecked storing operations in native byte order.
@ -4568,15 +4568,14 @@ public abstract class $abstractvectortype$ extends AbstractVector<$Boxtype$> {
final
void intoByteBuffer0(ByteBuffer bb, int offset) {
$Type$Species vsp = vspecies();
VectorSupport.store(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
bufferBase(bb), bufferAddress(bb, offset),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.put{#if[byte]?(:$Type$(}o + i * $sizeInBytes$, e));
});
ScopedMemoryAccess.storeIntoByteBuffer(
vsp.vectorType(), vsp.elementType(), vsp.laneCount(),
this, bb, offset,
(buf, off, v) -> {
ByteBuffer wb = wrapper(buf, NATIVE_ENDIAN);
v.stOp(wb, off,
(wb_, o, i, e) -> wb_.put{#if[byte]?(:$Type$(}o + i * $sizeInBytes$, e));
});
}
// End of low-level memory operations.