use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests1 method test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_1.
@Test
public void test_addFloatFromPointerAndFloatsFromStruct_returnFloatPointer_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(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct_returnFloatPointer").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);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(floatSegmt.address(), structSegmt);
MemorySegment resultSegmt = resultAddr.asSegment(C_FLOAT.byteSize(), scope);
VarHandle floatHandle = MemoryHandles.varHandle(float.class, ByteOrder.nativeOrder());
float result = (float) floatHandle.get(resultSegmt, 0);
Assert.assertEquals(result, 49.69F, 0.01F);
Assert.assertEquals(resultSegmt.address().toRawLongValue(), floatSegmt.address().toRawLongValue());
}
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests1 method test_addShortAndShortsFromStructPointer_1.
@Test
public void test_addShortAndShortsFromStructPointer_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(short.class, short.class, MemoryAddress.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, C_POINTER);
Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
shortHandle1.set(structSegmt, (short) 22);
shortHandle2.set(structSegmt, (short) 44);
short result = (short) mh.invokeExact((short) 66, structSegmt.address());
Assert.assertEquals(result, 132);
}
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests1 method test_addLongAndLongsFromStructWithNestedStructArray_reverseOrder_1.
@Test
public void test_addLongAndLongsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
GroupLayout longStruct = MemoryLayout.structLayout(longLayout.withName("elem1"), longLayout.withName("elem2"));
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct);
GroupLayout structLayout = MemoryLayout.structLayout(longLayout.withName("elem1"), structArray.withName("struct_array_elem2"));
MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setLongAtOffset(structSegmt, 0, 111111111L);
MemoryAccess.setLongAtOffset(structSegmt, 8, 222222222L);
MemoryAccess.setLongAtOffset(structSegmt, 16, 333333333L);
MemoryAccess.setLongAtOffset(structSegmt, 24, 444444444L);
MemoryAccess.setLongAtOffset(structSegmt, 32, 555555555L);
long result = (long) mh.invokeExact(666666666L, structSegmt);
Assert.assertEquals(result, 2333333331L);
}
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests1 method test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder_1.
@Test
public void test_addIntAndIntsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
GroupLayout intStruct = MemoryLayout.structLayout(C_INT.withName("elem1"), C_INT.withName("elem2"));
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, intStruct);
GroupLayout structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), structArray.withName("struct_array_elem2"));
MethodType mt = MethodType.methodType(int.class, int.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructWithNestedStructArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 1111111);
MemoryAccess.setIntAtOffset(structSegmt, 4, 2222222);
MemoryAccess.setIntAtOffset(structSegmt, 8, 3333333);
MemoryAccess.setIntAtOffset(structSegmt, 12, 4444444);
MemoryAccess.setIntAtOffset(structSegmt, 16, 5555555);
int result = (int) mh.invokeExact(6666666, structSegmt);
Assert.assertEquals(result, 23333331);
}
}
use of jdk.incubator.foreign.GroupLayout in project openj9 by eclipse.
the class StructTests1 method test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_1.
@Test
public void test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable {
GroupLayout longStruct = MemoryLayout.structLayout(longLayout, longLayout);
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, longStruct);
GroupLayout structLayout = MemoryLayout.structLayout(structArray, longLayout);
MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setLongAtOffset(structSegmt, 0, 111111111L);
MemoryAccess.setLongAtOffset(structSegmt, 8, 222222222L);
MemoryAccess.setLongAtOffset(structSegmt, 16, 333333333L);
MemoryAccess.setLongAtOffset(structSegmt, 24, 444444444L);
MemoryAccess.setLongAtOffset(structSegmt, 32, 555555555L);
long result = (long) mh.invokeExact(666666666L, structSegmt);
Assert.assertEquals(result, 2333333331L);
}
}
Aggregations