use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_addFloatFromPointerAndFloatsFromStruct_1.
@Test
public void test_addFloatFromPointerAndFloatsFromStruct_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(float.class, MemoryAddress.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_POINTER, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addFloatFromPointerAndFloatsFromStruct").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);
float result = (float) mh.invokeExact(floatSegmt.address(), structSegmt);
Assert.assertEquals(result, 49.69F, 0.01F);
}
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_addShortAndShortsFromStructWithNestedShortArray_1.
@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_1() throws Throwable {
SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, C_SHORT);
GroupLayout structLayout = MemoryLayout.structLayout(shortArray.withName("array_elem1"), C_SHORT.withName("elem2"), MemoryLayout.paddingLayout(C_SHORT.bitSize()));
MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addShortAndShortsFromStructWithNestedShortArray").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
MemoryAccess.setShortAtOffset(structSegmt, 0, (short) 111);
MemoryAccess.setShortAtOffset(structSegmt, 2, (short) 222);
MemoryAccess.setShortAtOffset(structSegmt, 4, (short) 333);
short result = (short) mh.invokeExact((short) 444, structSegmt);
Assert.assertEquals(result, 1110);
}
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_addDoubleAndDoubleIntFromStruct_1.
@Test
public void test_addDoubleAndDoubleIntFromStruct_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_INT.withName("elem2"));
VarHandle elemHandle1 = structLayout.varHandle(double.class, PathElement.groupElement("elem1"));
VarHandle elemHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoubleIntFromStruct").get();
MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MemorySegment structSegmt = allocator.allocate(structLayout);
elemHandle1.set(structSegmt, 218.555D);
elemHandle2.set(structSegmt, 19);
double result = (double) mh.invokeExact(216.666D, structSegmt);
Assert.assertEquals(result, 454.221D, 0.001D);
}
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_1.
@Test
public void test_addLongAndLongsFromStructWithNestedLongArray_withoutLayoutName_1() throws Throwable {
SequenceLayout longArray = MemoryLayout.sequenceLayout(2, longLayout);
GroupLayout structLayout = MemoryLayout.structLayout(longArray, longLayout);
MethodType mt = MethodType.methodType(long.class, long.class, MemorySegment.class);
FunctionDescriptor fd = FunctionDescriptor.of(longLayout, longLayout, structLayout);
Addressable functionSymbol = nativeLibLookup.lookup("addLongAndLongsFromStructWithNestedLongArray").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);
long result = (long) mh.invokeExact(444444444L, structSegmt);
Assert.assertEquals(result, 1111111110L);
}
}
use of jdk.incubator.foreign.Addressable in project openj9 by eclipse.
the class StructTests1 method test_add3CharStructs_returnStruct_1.
@Test
public void test_add3CharStructs_returnStruct_1() throws Throwable {
GroupLayout structLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"), C_SHORT.withName("elem3"));
VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2"));
VarHandle charHandle3 = structLayout.varHandle(char.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("add3CharStructs_returnStruct").get();
try (ResourceScope scope = ResourceScope.newConfinedScope()) {
SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
MethodHandle mh = clinker.downcallHandle(functionSymbol, allocator, mt, fd);
MemorySegment structSegmt1 = allocator.allocate(structLayout);
charHandle1.set(structSegmt1, 'A');
charHandle2.set(structSegmt1, 'B');
charHandle3.set(structSegmt1, 'C');
MemorySegment structSegmt2 = allocator.allocate(structLayout);
charHandle1.set(structSegmt2, 'B');
charHandle2.set(structSegmt2, 'C');
charHandle3.set(structSegmt2, 'D');
MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
Assert.assertEquals(charHandle1.get(resultSegmt), 'B');
Assert.assertEquals(charHandle2.get(resultSegmt), 'D');
Assert.assertEquals(charHandle3.get(resultSegmt), 'F');
}
}
Aggregations