Search in sources :

Example 91 with GroupLayout

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

the class StructTests1 method test_add3BoolStructsWithXor_returnStruct_1.

@Test
public void test_add3BoolStructsWithXor_returnStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"), C_CHAR.withName("elem3"), MemoryLayout.paddingLayout(C_CHAR.bitSize()));
    VarHandle boolHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle boolHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    VarHandle boolHandle3 = structLayout.varHandle(byte.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("add3BoolStructsWithXor_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);
        boolHandle1.set(structSegmt1, (byte) 1);
        boolHandle2.set(structSegmt1, (byte) 0);
        boolHandle3.set(structSegmt1, (byte) 1);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt2, (byte) 1);
        boolHandle2.set(structSegmt2, (byte) 1);
        boolHandle3.set(structSegmt2, (byte) 0);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
        Assert.assertEquals((byte) boolHandle1.get(resultSegmt), (byte) 0);
        Assert.assertEquals((byte) boolHandle2.get(resultSegmt), (byte) 1);
        Assert.assertEquals((byte) boolHandle3.get(resultSegmt), (byte) 1);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 92 with GroupLayout

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

the class StructTests1 method test_addShortAndShortsFromNestedStruct_1.

@Test
public void test_addShortAndShortsFromNestedStruct_1() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.structLayout(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.structLayout(nestedStructLayout.withName("struct_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("addShortAndShortsFromNestedStruct").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) 31);
        MemoryAccess.setShortAtOffset(structSegmt, 2, (short) 33);
        MemoryAccess.setShortAtOffset(structSegmt, 4, (short) 35);
        short result = (short) mh.invokeExact((short) 37, structSegmt);
        Assert.assertEquals(result, 136);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 93 with GroupLayout

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

the class StructTests1 method test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_1.

@Test
public void test_addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder_1() throws Throwable {
    SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, C_FLOAT);
    GroupLayout structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), floatArray.withName("array_elem2"));
    MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addFloatAndFloatsFromStructWithNestedFloatArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(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);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) 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 94 with GroupLayout

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

the class StructTests1 method test_add2BoolStructsWithXor_returnStructPointer_1.

@Test
public void test_add2BoolStructsWithXor_returnStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_CHAR.withName("elem1"), C_CHAR.withName("elem2"));
    VarHandle boolHandle1 = structLayout.varHandle(byte.class, PathElement.groupElement("elem1"));
    VarHandle boolHandle2 = structLayout.varHandle(byte.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add2BoolStructsWithXor_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt1, (byte) 1);
        boolHandle2.set(structSegmt1, (byte) 0);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        boolHandle1.set(structSegmt2, (byte) 1);
        boolHandle2.set(structSegmt2, (byte) 1);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
        MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
        Assert.assertEquals((byte) boolHandle1.get(resultSegmt), (byte) 0);
        Assert.assertEquals((byte) boolHandle2.get(resultSegmt), (byte) 1);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) FunctionDescriptor(jdk.incubator.foreign.FunctionDescriptor) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 95 with GroupLayout

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

the class StructTests1 method test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_1.

@Test
public void test_addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder_1() throws Throwable {
    GroupLayout doubleStruct = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), C_DOUBLE.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.sequenceLayout(2, doubleStruct);
    GroupLayout structLayout = MemoryLayout.structLayout(C_DOUBLE.withName("elem1"), structArray.withName("struct_array_elem2"));
    MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addDoubleAndDoublesFromStructWithNestedStructArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(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);
    }
}
Also used : MethodType(java.lang.invoke.MethodType) ResourceScope(jdk.incubator.foreign.ResourceScope) SegmentAllocator(jdk.incubator.foreign.SegmentAllocator) GroupLayout(jdk.incubator.foreign.GroupLayout) Addressable(jdk.incubator.foreign.Addressable) 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

GroupLayout (jdk.incubator.foreign.GroupLayout)677 MethodHandle (java.lang.invoke.MethodHandle)675 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)675 MemorySegment (jdk.incubator.foreign.MemorySegment)675 Test (org.testng.annotations.Test)675 ResourceScope (jdk.incubator.foreign.ResourceScope)540 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)540 MethodType (java.lang.invoke.MethodType)405 VarHandle (java.lang.invoke.VarHandle)318 Addressable (jdk.incubator.foreign.Addressable)270 NativeSymbol (jdk.incubator.foreign.NativeSymbol)270 SequenceLayout (jdk.incubator.foreign.SequenceLayout)241 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)135 MemoryAddress (jdk.incubator.foreign.MemoryAddress)80 MemoryLayout (jdk.incubator.foreign.MemoryLayout)2 ValueLayout (jdk.incubator.foreign.ValueLayout)1