use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class StructTests1 method test_addDoubleAndIntDoubleFromStruct_1.
@Test
public void test_addDoubleAndIntDoubleFromStruct_1() throws Throwable {
GroupLayout structLayout = null;
MemorySegment structSegmt = null;
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
/* The size of [int, double] on AIX/PPC 64-bit is 12 bytes without padding by default
* while the same struct is 16 bytes with padding on other platforms.
*/
if (isAixOS) {
structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), C_DOUBLE.withName("elem2"));
structSegmt = allocator.allocate(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 18);
MemoryAccess.setDoubleAtOffset(structSegmt, 4, 619.777D);
} else {
structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), MemoryLayout.paddingLayout(C_INT.bitSize()), C_DOUBLE.withName("elem2"));
VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
VarHandle elemHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2"));
structSegmt = allocator.allocate(structLayout);
elemHandle1.set(structSegmt, 18);
elemHandle2.set(structSegmt, 619.777D);
}
MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
double result = (double) mh.invokeExact(113.567D, structSegmt);
Assert.assertEquals(result, 751.344D, 0.001D);
}
}
use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class StructTests1 method test_addShortFromPointerAndShortsFromStruct_returnShortPointer_1.
@Test
public void test_addShortFromPointerAndShortsFromStruct_returnShortPointer_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addShortFromPointerAndShortsFromStruct_returnShortPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment shortSegmt = allocator.allocate(C_SHORT);
MemoryAccess.setShort(shortSegmt, (short) 12);
MemorySegment structSegmt = allocator.allocate(structLayout);
shortHandle1.set(structSegmt, (short) 18);
shortHandle2.set(structSegmt, (short) 19);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(shortSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_SHORT.byteSize(), scope);
VarHandle shortHandle = MemoryHandles.varHandle(short.class, ByteOrder.nativeOrder());
short result = (short) shortHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 49);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), shortSegmt.address().toRawLongValue());
}
}
use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class StructTests1 method test_addFloatFromPointerAndFloatsFromStruct_1.
@Test
public void test_addFloatFromPointerAndFloatsFromStruct_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(float.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment floatSegmt = allocator.allocate(C_FLOAT);
MemoryAccess.setFloat(floatSegmt, 12.12F);
MemorySegment structSegmt = allocator.allocate(structLayout);
floatHandle1.set(structSegmt, 18.23F);
floatHandle2.set(structSegmt, 19.34F);
float result = (float) mh.invokeExact(floatSegmt.address(), structSegmt);
Assert.assertEquals(result, 49.69F, 0.01F);
}
}
use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class StructTests1 method test_addDoubleAndDoubleIntFromStruct_1.
@Test
public void test_addDoubleAndDoubleIntFromStruct_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_INT.withName("elem2"));
VarHandle elemHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleIntFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
elemHandle1.set(structSegmt, 218.555D);
elemHandle2.set(structSegmt, 19);
double result = (double) mh.invokeExact(216.666D, structSegmt);
Assert.assertEquals(result, 454.221D, 0.001D);
}
}
use of java.lang.invoke.VarHandle in project openj9 by eclipse.
the class StructTests1 method test_add3CharStructs_returnStruct_1.
@Test
public void test_add3CharStructs_returnStruct_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"));
VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2"));
VarHandle charHandle3 = structLayout.varHandle(char.class, PathElement.groupElement("elem3"));
MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("add3CharStructs_returnStruct").get();
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
charHandle1.set(structSegmt1, 'A');
charHandle2.set(structSegmt1, 'B');
charHandle3.set(structSegmt1, 'C');
MemorySegment structSegmt2 = allocator.allocate(structLayout);
charHandle1.set(structSegmt2, 'B');
charHandle2.set(structSegmt2, 'C');
charHandle3.set(structSegmt2, 'D');
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals(charHandle1.get(resultSegmt), 'B');
Assert.assertEquals(charHandle2.get(resultSegmt), 'D');
Assert.assertEquals(charHandle3.get(resultSegmt), 'F');
}
}
Aggregations