Search in sources :

Example 66 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_addTwoNegtiveShorts_1.

@Test
public void test_addTwoNegtiveShorts_1() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, JAVA_SHORT);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Shorts").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    short result = (short) mh.invokeExact((short) -24, (short) -32);
    Assert.assertEquals(result, (short) -56);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 67 with NativeSymbol

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

the class PrimitiveTypeTests1 method test_addTwoBytes_1.

@Test
public void test_addTwoBytes_1() throws Throwable {
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, JAVA_BYTE);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2Bytes").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    byte result = (byte) mh.invokeExact((byte) 6, (byte) 3);
    Assert.assertEquals(result, (byte) 9);
}
Also used : NativeSymbol(jdk.incubator.foreign.NativeSymbol) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 68 with NativeSymbol

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

the class StructTests1 method test_add2DoubleStructs_returnStruct_1.

@Test
public void test_add2DoubleStructs_returnStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"));
    VarHandle doubleHandle1 = structLayout.varHandle(PathElement.groupElement("elem1"));
    VarHandle doubleHandle2 = structLayout.varHandle(PathElement.groupElement("elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("add2DoubleStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.nativeAllocator(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        doubleHandle1.set(structSegmt1, 11.222D);
        doubleHandle2.set(structSegmt1, 22.333D);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        doubleHandle1.set(structSegmt2, 33.444D);
        doubleHandle2.set(structSegmt2, 44.555D);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(allocator, structSegmt1, structSegmt2);
        Assert.assertEquals((double) doubleHandle1.get(resultSegmt), 44.666D, 0.001D);
        Assert.assertEquals((double) doubleHandle2.get(resultSegmt), 66.888D, 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 69 with NativeSymbol

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

the class StructTests1 method test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_1.

@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
    GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), structArray.withName("struct_array_elem2"));
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder").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);
        structSegmt.set(JAVA_FLOAT, 12, 444.44F);
        structSegmt.set(JAVA_FLOAT, 16, 555.55F);
        float result = (float) mh.invokeExact(666.66F, structSegmt);
        Assert.assertEquals(result, 2333.31F, 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 70 with NativeSymbol

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

the class StructTests1 method test_addFloatAndFloatsFromStructWithNestedStructArray_withoutLayoutName_1.

@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray_withoutLayoutName_1() throws Throwable {
    GroupLayout floatStruct = MemoryLayout.structLayout(JAVA_FLOAT, JAVA_FLOAT);
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, floatStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(structArray, JAVA_FLOAT);
    FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout);
    NativeSymbol functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedStructArray").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);
        structSegmt.set(JAVA_FLOAT, 12, 444.44F);
        structSegmt.set(JAVA_FLOAT, 16, 555.55F);
        float result = (float) mh.invokeExact(666.66F, structSegmt);
        Assert.assertEquals(result, 2333.31F, 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)

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