Search in sources :

Example 51 with NativeSymbol

use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.

the class StructTests1 method test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName_1.

@Test
public void test_addFloatAndFloatsFromStructWithNestedFloatArray_withoutLayoutName_1() throws Throwable {
    SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT);
    GroupLayout structLayout = MemoryLayout.structLayout(floatArray, JAVA_FLOAT);
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_FLOAT, 0, 111.11F);
        structSegmt.set(JAVA_FLOAT, 4, 222.22F);
        structSegmt.set(JAVA_FLOAT, 8, 333.33F);
        float result = (float) mh.invokeExact(444.44F, structSegmt);
        Assert.assertEquals(result, 1111.1F, 0.01F);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) SequenceLayout(jdk.incubator.foreign.SequenceLayout) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 52 with NativeSymbol

use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.

the class StructTests1 method test_addDoubleAndIntDoubleFromStruct_1.

@Test
public void test_addDoubleAndIntDoubleFromStruct_1() throws Throwable {
    GroupLayout structLayout = null;
    MemorySegment structSegmt = null;
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        /* 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.structLayout(JAVA_INT.withName("elem1"), JAVA_DOUBLE.withName("elem2"));
            structSegmt = allocator.allocate(structLayout);
            structSegmt.set(JAVA_INT, 0, 18);
            structSegmt.set(JAVA_DOUBLE, 4, 619.777D);
        } else {
            structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), MemoryLayout.paddingLayout(JAVA_INT.bitSize()), JAVA_DOUBLE.withName("elem2"));
            VarHandle elemHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
            VarHandle elemHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
            structSegmt = allocator.allocate(structLayout);
            elemHandle1.set(structSegmt, 18);
            elemHandle2.set(structSegmt, 619.777D);
        }
        FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout);
        NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndIntDoubleFromStruct").get();
        MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
        double result = (double) mh.invokeExact(113.567D, structSegmt);
        Assert.assertEquals(result, 751.344D, 0.001D);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 53 with NativeSymbol

use of jdk.incubator.foreign.NativeSymbol 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(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2"), JAVA_CHAR.withName("elem3"));
    VarHandle charHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle charHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    VarHandle charHandle3 = structLayout.varHandle(PathElement.groupElement("elem3"));
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add3CharStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        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(allocator, structSegmt1, structSegmt2);
        Assert.assertEquals(charHandle1.get(resultSegmt), 'B');
        Assert.assertEquals(charHandle2.get(resultSegmt), 'D');
        Assert.assertEquals(charHandle3.get(resultSegmt), 'F');
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 54 with NativeSymbol

use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.

the class StructTests1 method test_addDoubleAndDoublesFromStructWithNestedStructArray_1.

@Test
public void test_addDoubleAndDoublesFromStructWithNestedStructArray_1() throws Throwable {
    GroupLayout doubleStruct = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray.withName("struct_array_elem1"), JAVA_DOUBLE.withName("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        structSegmt.set(JAVA_DOUBLE, 0, 111.111D);
        structSegmt.set(JAVA_DOUBLE, 8, 222.222D);
        structSegmt.set(JAVA_DOUBLE, 16, 333.333D);
        structSegmt.set(JAVA_DOUBLE, 24, 444.444D);
        structSegmt.set(JAVA_DOUBLE, 32, 555.555D);
        double result = (double) mh.invokeExact(666.666D, structSegmt);
        Assert.assertEquals(result, 2333.331D, 0.001D);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) SequenceLayout(jdk.incubator.foreign.SequenceLayout) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 55 with NativeSymbol

use of jdk.incubator.foreign.NativeSymbol in project openj9 by eclipse.

the class StructTests1 method test_add3ShortStructs_returnStruct_1.

@Test
public void test_add3ShortStructs_returnStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3"), MemoryLayout.paddingLayout(JAVA_SHORT.bitSize()));
    VarHandle shortHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle shortHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    VarHandle shortHandle3 = structLayout.varHandle(PathElement.groupElement("elem3"));
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add3ShortStructs_returnStruct").get();
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        shortHandle1.set(structSegmt1, (short) 25);
        shortHandle2.set(structSegmt1, (short) 26);
        shortHandle3.set(structSegmt1, (short) 27);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        shortHandle1.set(structSegmt2, (short) 34);
        shortHandle2.set(structSegmt2, (short) 35);
        shortHandle3.set(structSegmt2, (short) 36);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(allocator, 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);
    }
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Aggregations

MethodHandle (java.lang.invoke.MethodHandle)334 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)334 NativeSymbol (jdk.incubator.foreign.NativeSymbol)334 Test (org.testng.annotations.Test)334 MemorySegment (jdk.incubator.foreign.MemorySegment)291 ResourceScope (jdk.incubator.foreign.ResourceScope)274 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)271 GroupLayout (jdk.incubator.foreign.GroupLayout)270 VarHandle (java.lang.invoke.VarHandle)126 SequenceLayout (jdk.incubator.foreign.SequenceLayout)96 MemoryAddress (jdk.incubator.foreign.MemoryAddress)34 VaList (jdk.incubator.foreign.VaList)4