Search in sources :

Example 66 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addFloatAndFloatsFromStructWithNestedFloatArray.

@Test
public void test_addFloatAndFloatsFromStructWithNestedFloatArray() throws Throwable {
    SequenceLayout floatArray = MemoryLayout.ofSequence(2, C_FLOAT);
    GroupLayout structLayout = MemoryLayout.ofStruct(floatArray.withName("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("addFloatAndFloatsFromStructWithNestedFloatArray").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);
    float result = (float) mh.invokeExact(444.44F, structSegmt);
    Assert.assertEquals(result, 1111.1F, 0.01F);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 67 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addDoubleAndIntDoubleFromStruct.

@Test
public void test_addDoubleAndIntDoubleFromStruct() throws Throwable {
    GroupLayout structLayout = null;
    MemorySegment structSegmt = null;
    /* 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.ofStruct(C_INT.withName("elem1"), C_DOUBLE.withName("elem2"));
        structSegmt = MemorySegment.allocateNative(structLayout);
        MemoryAccess.setIntAtOffset(structSegmt, 0, 18);
        MemoryAccess.setDoubleAtOffset(structSegmt, 4, 619.777D);
    } else {
        structLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), MemoryLayout.ofPaddingBits(C_INT.bitSize()), C_DOUBLE.withName("elem2"));
        VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
        VarHandle elemHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2"));
        structSegmt = MemorySegment.allocateNative(structLayout);
        elemHandle1.set(structSegmt, 18);
        elemHandle2.set(structSegmt, 619.777D);
    }
    MethodType mt = MethodType.methodType(double.class, double.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_DOUBLE, C_DOUBLE, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addDoubleAndIntDoubleFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    double result = (double) mh.invokeExact(113.567D, structSegmt);
    Assert.assertEquals(result, 751.344D, 0.001D);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 68 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder.

@Test
public void test_addFloatAndFloatsFromStructWithNestedStructArray_reverseOrder() throws Throwable {
    GroupLayout floatStruct = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    SequenceLayout structArray = MemoryLayout.ofSequence(2, floatStruct);
    GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), structArray.withName("struct_array_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_reverseOrder").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();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 69 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_add2CharStructs_returnStruct.

@Test
public void test_add2CharStructs_returnStruct() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    VarHandle charHandle1 = structLayout.varHandle(char.class, PathElement.groupElement("elem1"));
    VarHandle charHandle2 = structLayout.varHandle(char.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2CharStructs_returnStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt1, 'A');
    charHandle2.set(structSegmt1, 'B');
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    charHandle1.set(structSegmt2, 'C');
    charHandle2.set(structSegmt2, 'D');
    MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
    Assert.assertEquals(charHandle1.get(resultSegmt), 'C');
    Assert.assertEquals(charHandle2.get(resultSegmt), 'E');
    structSegmt1.close();
    structSegmt2.close();
    resultSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) VarHandle(java.lang.invoke.VarHandle) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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 70 with Symbol

use of jdk.incubator.foreign.LibraryLookup.Symbol in project openj9 by eclipse.

the class StructTests method test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder.

@Test
public void test_addShortAndShortsFromStructWithNestedShortArray_reverseOrder() throws Throwable {
    SequenceLayout shortArray = MemoryLayout.ofSequence(2, C_SHORT);
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), shortArray.withName("array_elem2"), MemoryLayout.ofPaddingBits(C_SHORT.bitSize()));
    MethodType mt = MethodType.methodType(short.class, short.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_SHORT, C_SHORT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addShortAndShortsFromStructWithNestedShortArray_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(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);
    structSegmt.close();
}
Also used : MethodType(java.lang.invoke.MethodType) Symbol(jdk.incubator.foreign.LibraryLookup.Symbol) 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)173 MethodType (java.lang.invoke.MethodType)173 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)173 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)173 Test (org.testng.annotations.Test)173 MemorySegment (jdk.incubator.foreign.MemorySegment)149 GroupLayout (jdk.incubator.foreign.GroupLayout)135 VarHandle (java.lang.invoke.VarHandle)64 SequenceLayout (jdk.incubator.foreign.SequenceLayout)48 MemoryAddress (jdk.incubator.foreign.MemoryAddress)30