use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.
the class StructTests method test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName.
@Test
public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName() throws Throwable {
SequenceLayout longArray = MemoryLayout.ofSequence(2, longLayout);
GroupLayout structLayout = MemoryLayout.ofStruct(longArray, longLayout);
MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
Symbol functionSymbol = nativeLib.lookup("addLongAndLongsFromStructWithNestedLongArray").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);
long result = (long) mh.invokeExact(444444444L, structSegmt);
Assert.assertEquals(result, 1111111110L);
structSegmt.close();
}
use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.
the class StructTests method test_add3ShortStructs_returnStruct.
@Test
public void test_add3ShortStructs_returnStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
VarHandle shortHandle3 = structLayout.varHandle(short.class, PathElement.groupElement("elem3"));
MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Symbol functionSymbol = nativeLib.lookup("add3ShortStructs_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
shortHandle1.set(structSegmt1, (short) 25);
shortHandle2.set(structSegmt1, (short) 26);
shortHandle3.set(structSegmt1, (short) 27);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
shortHandle1.set(structSegmt2, (short) 34);
shortHandle2.set(structSegmt2, (short) 35);
shortHandle3.set(structSegmt2, (short) 36);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals((short) shortHandle1.get(resultSegmt), (short) 59);
Assert.assertEquals((short) shortHandle2.get(resultSegmt), (short) 61);
Assert.assertEquals((short) shortHandle3.get(resultSegmt), (short) 63);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.
the class StructTests method test_add2FloatStructs_returnStructPointer.
@Test
public void test_add2FloatStructs_returnStructPointer() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(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);
Symbol functionSymbol = nativeLib.lookup("add2FloatStructs_returnStructPointer").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
floatHandle1.set(structSegmt1, 25.12F);
floatHandle2.set(structSegmt1, 11.23F);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
floatHandle1.set(structSegmt2, 24.34F);
floatHandle2.set(structSegmt2, 13.45F);
MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.
the class StructTests method test_addFloatAndFloatsFromStructWithNestedStructArray.
@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray() throws Throwable {
GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct);
GroupLayout structLayout = MemoryLayout.ofStruct(structArray.withName("struct_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("addFloatAndFloatsFromStructWithNestedStructArray").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();
}
use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.
the class StructTests method test_add3ByteStructs_returnStruct.
@Test
public void test_add3ByteStructs_returnStruct() throws Throwable {
GroupLayout structLayout = MemoryLayout.ofStruct(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"), C_CHAR.withName("elem3"), MemoryLayout.ofPaddingBits(C_CHAR.bitSize()));
VarHandle byteHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
VarHandle byteHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
VarHandle byteHandle3 = structLayout.varHandle(byte.class, PathElement.groupElement("elem3"));
MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
Symbol functionSymbol = nativeLib.lookup("add3ByteStructs_returnStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
byteHandle1.set(structSegmt1, (byte) 25);
byteHandle2.set(structSegmt1, (byte) 11);
byteHandle3.set(structSegmt1, (byte) 12);
MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
byteHandle1.set(structSegmt2, (byte) 24);
byteHandle2.set(structSegmt2, (byte) 13);
byteHandle3.set(structSegmt2, (byte) 16);
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals((byte) byteHandle1.get(resultSegmt), (byte) 49);
Assert.assertEquals((byte) byteHandle2.get(resultSegmt), (byte) 24);
Assert.assertEquals((byte) byteHandle3.get(resultSegmt), (byte) 28);
structSegmt1.close();
structSegmt2.close();
resultSegmt.close();
}
Aggregations