use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_add2FloatStructs_returnStruct_2.
@Test
public void test_add2FloatStructs_returnStruct_2() 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(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_returnStruct").get();
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MethodHandle mh = clinker.downcallHandle(mt, fd);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
floatHandle1.set(structSegmt1, 25.12F);
floatHandle2.set(structSegmt1, 11.23F);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
floatHandle1.set(structSegmt2, 24.34F);
floatHandle2.set(structSegmt2, 13.45F);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2);
Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_addIntAndIntsFromStructWithNestedIntArray_reverseOrder_2.
@Test
public void test_addIntAndIntsFromStructWithNestedIntArray_reverseOrder_2() throws Throwable {
SequenceLayout intArray = MemoryLayout.sequenceLayout(2, C_INT);
GroupLayout structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), intArray.withName("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("addIntAndIntsFromStructWithNestedIntArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(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);
int result = (int) mh.invokeExact(functionSymbol, 4444444, structSegmt);
Assert.assertEquals(result, 11111110);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_add3DoubleStructs_returnStruct_2.
@Test
public void test_add3DoubleStructs_returnStruct_2() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"), C_DOUBLE.withName("elem3"));
VarHandle doubleHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
VarHandle doubleHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2"));
VarHandle doubleHandle3 = structLayout.varHandle(double.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("add3DoubleStructs_returnStruct").get();
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MethodHandle mh = clinker.downcallHandle(mt, fd);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
doubleHandle1.set(structSegmt1, 11.222D);
doubleHandle2.set(structSegmt1, 22.333D);
doubleHandle3.set(structSegmt1, 33.123D);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
doubleHandle1.set(structSegmt2, 33.444D);
doubleHandle2.set(structSegmt2, 44.555D);
doubleHandle3.set(structSegmt2, 55.456D);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(functionSymbol, allocator, structSegmt1, structSegmt2);
Assert.assertEquals((double) doubleHandle1.get(resultSegmt), 44.666D, 0.001D);
Assert.assertEquals((double) doubleHandle2.get(resultSegmt), 66.888D, 0.001D);
Assert.assertEquals((double) doubleHandle3.get(resultSegmt), 88.579D, 0.001D);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_2.
@Test
public void test_addLongAndLongsFromStructWithNestedStructArray_withoutLayoutName_2() 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(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(functionSymbol, 666666666L, structSegmt);
Assert.assertEquals(result, 2333333331L);
}
}
use of jdk.incubator.foreign.SegmentAllocator in project openj9 by eclipse.
the class StructTests2 method test_add2BoolStructsWithXor_returnStructPointer_2.
@Test
public void test_add2BoolStructsWithXor_returnStructPointer_2() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
VarHandle boolHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
VarHandle boolHandle2 = structLayout.varHandle(byte.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("add2BoolStructsWithXor_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
boolHandle1.set(structSegmt1, (byte) 1);
boolHandle2.set(structSegmt1, (byte) 0);
MemorySegment structSegmt2 = allocator.allocate(structLayout);
boolHandle1.set(structSegmt2, (byte) 1);
boolHandle2.set(structSegmt2, (byte) 1);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(functionSymbol, structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
Assert.assertEquals((byte) boolHandle1.get(resultSegmt), (byte) 0);
Assert.assertEquals((byte) boolHandle2.get(resultSegmt), (byte) 1);
}
}
Aggregations