use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addLongAndLongsFromStructWithNestedStructArray.
@Test
public void test_addLongAndLongsFromStructWithNestedStructArray() throws Throwable {
GroupLayout longStruct = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2"));
SequenceLayout structArray = MemoryLayout.ofSequence(2, longStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_array_elem1"), longLayout.withName("elem2"));
MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(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);
structSegmt.close();
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName.
@Test
public void test_addDoubleAndDoublesFromStructWithNestedStructArray_withoutLayoutName() throws Throwable {
GroupLayout doubleStruct = MemoryLayout.ofStruct(C_DOUBLE, C_DOUBLE);
SequenceLayout structArray = MemoryLayout.ofSequence(2, doubleStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(structArray, C_DOUBLE);
MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
Symbol functionSymbol = nativeLib.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(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);
structSegmt.close();
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addFloatAndFloatsFromStructWithNestedFloatArray.
@Test
public void test_addFloatAndFloatsFromStructWithNestedFloatArray() throws Throwable {
SequenceLayout floatArray = MemoryLayout.ofSequence(2, C_FLOAT);
GroupLayout structLayout = MemoryLayout.ofStruct(floatArray.withName("array_elem1"), C_FLOAT.withName("elem2"));
MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(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);
structSegmt.close();
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addDoubleAndIntDoubleFromStruct.
@Test
public void test_addDoubleAndIntDoubleFromStruct() throws Throwable {
GroupLayout structLayout = null;
MemorySegment structSegmt = null;
/* 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.ofStruct(C_INT.withName("elem1"), C_DOUBLE.withName("elem2"));
structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setIntAtOffset(structSegmt, 0, 18);
MemoryAccess.setDoubleAtOffset(structSegmt, 4, 619.777D);
} else {
structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), MemoryLayout.ofPaddingBits(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 = MemorySegment.allocateNative(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);
Symbol functionSymbol = nativeLib.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);
structSegmt.close();
}
use of jdk.incubator.foreign.FunctionDescriptor in project openj9 by eclipse.
the class StructTests method test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder.
@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder() throws Throwable {
GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2"));
MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
MemoryAccess.setFloatAtOffset(structSegmt, 0, 111.11F);
MemoryAccess.setFloatAtOffset(structSegmt, 4, 222.22F);
MemoryAccess.setFloatAtOffset(structSegmt, 8, 333.33F);
MemoryAccess.setFloatAtOffset(structSegmt, 12, 444.44F);
MemoryAccess.setFloatAtOffset(structSegmt, 16, 555.55F);
float result = (float) mh.invokeExact(666.66F, structSegmt);
Assert.assertEquals(result, 2333.31F, 0.01F);
structSegmt.close();
}
Aggregations