use of jdk.incubator.foreign.SequenceLayout 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);
}
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests1 method test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_1.
@Test
public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_1() throws Throwable {
SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, C_FLOAT);
GroupLayout structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), floatArray.withName("array_elem2"));
MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray_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.setFloatAtOffset(structSegmt, 0, 111.11F);
MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F);
MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F);
float result = (float) mh.invokeExact(444.44F, structSegmt);
Assert.assertEquals(result, 1111.1F, 0.01F);
}
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests1 method test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_1.
@Test
public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
GroupLayout doubleStruct = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct);
GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2"));
MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_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.setDoubleAtOffset(structSegmt, 0, 111.111D);
MemoryAccess.setDoubleAtOffset(structSegmt, 8, 222.222D);
MemoryAccess.setDoubleAtOffset(structSegmt, 16, 333.333D);
MemoryAccess.setDoubleAtOffset(structSegmt, 24, 444.444D);
MemoryAccess.setDoubleAtOffset(structSegmt, 32, 555.555D);
double result = (double) mh.invokeExact(666.666D, structSegmt);
Assert.assertEquals(result, 2333.331D, 0.001D);
}
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests1 method test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_1.
@Test
public void test_addCharAndCharsFromStructWithNestedCharArray_reverseOrder_1() throws Throwable {
SequenceLayout charArray = MemoryLayout.sequenceLayout(2, C_SHORT);
GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), charArray.withName("array_elem2"), MemoryLayout.paddingLayout(C_SHORT.bitSize()));
MethodType mt = MethodType.methodType(char.class, char.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addCharAndCharsFromStructWithNestedCharArray_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.setCharAtOffset(structSegmt, 0, 'A');
MemoryAccess.setCharAtOffset(structSegmt, 2, 'B');
MemoryAccess.setCharAtOffset(structSegmt, 4, 'C');
char result = (char) mh.invokeExact('D', structSegmt);
Assert.assertEquals(result, 'G');
}
}
use of jdk.incubator.foreign.SequenceLayout in project openj9 by eclipse.
the class StructTests1 method test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_1.
@Test
public void test_addBoolAndBoolsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
GroupLayout boolStruct = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
SequenceLayout structArray = MemoryLayout.sequenceLayout(2, boolStruct);
GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), structArray.withName("struct_array_elem2"), MemoryLayout.paddingLayout(C_CHAR.bitSize() * 3));
MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_CHAR, C_CHAR, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addBoolAndBoolsFromStructWithNestedStructArray_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.setByteAtOffset(structSegmt, 0, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 1, (byte) 1);
MemoryAccess.setByteAtOffset(structSegmt, 2, (byte) 0);
MemoryAccess.setByteAtOffset(structSegmt, 3, (byte) 1);
MemoryAccess.setByteAtOffset(structSegmt, 4, (byte) 0);
boolean result = (boolean) mh.invokeExact(true, structSegmt);
Assert.assertEquals(result, true);
}
}
Aggregations