Search in sources :

Example 96 with Symbol

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

the class StructTests method test_add2ShortStructs_returnStructPointer.

@Test
public void test_add2ShortStructs_returnStructPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(C_SHORT.withName("elem1"), C_SHORT.withName("elem2"));
    VarHandle shortHandle1 = structLayout.varHandle(short.class, PathElement.groupElement("elem1"));
    VarHandle shortHandle2 = structLayout.varHandle(short.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2ShortStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    shortHandle1.set(structSegmt1, (short) 56);
    shortHandle2.set(structSegmt1, (short) 45);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    shortHandle1.set(structSegmt2, (short) 78);
    shortHandle2.set(structSegmt2, (short) 67);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
    Assert.assertEquals((short) shortHandle1.get(resultSegmt), (short) 134);
    Assert.assertEquals((short) shortHandle2.get(resultSegmt), (short) 112);
    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) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 97 with Symbol

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

the class StructTests method test_add2LongStructs_returnStructPointer.

@Test
public void test_add2LongStructs_returnStructPointer() throws Throwable {
    GroupLayout structLayout = MemoryLayout.ofStruct(longLayout.withName("elem1"), longLayout.withName("elem2"));
    VarHandle longHandle1 = structLayout.varHandle(long.class, PathElement.groupElement("elem1"));
    VarHandle longHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemoryAddress.class, MemoryAddress.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_POINTER, C_POINTER, structLayout);
    Symbol functionSymbol = nativeLib.lookup("add2LongStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt1 = MemorySegment.allocateNative(structLayout);
    longHandle1.set(structSegmt1, 1122334455L);
    longHandle2.set(structSegmt1, 5566778899L);
    MemorySegment structSegmt2 = MemorySegment.allocateNative(structLayout);
    longHandle1.set(structSegmt2, 9900112233L);
    longHandle2.set(structSegmt2, 3344556677L);
    MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
    MemorySegment resultSegmt = resultAddr.asSegmentRestricted(structLayout.byteSize());
    Assert.assertEquals(longHandle1.get(resultSegmt), 11022446688L);
    Assert.assertEquals(longHandle2.get(resultSegmt), 8911335576L);
    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) MemoryAddress(jdk.incubator.foreign.MemoryAddress) MemorySegment(jdk.incubator.foreign.MemorySegment) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 98 with Symbol

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

the class StructTests method test_addBoolAndBoolsFromNestedStructWithXor.

@Test
public void test_addBoolAndBoolsFromNestedStructWithXor() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_INT.withName("elem1"), C_INT.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.ofStruct(nestedStructLayout.withName("struct_elem1"), C_INT.withName("elem2"));
    MethodType mt = MethodType.methodType(boolean.class, boolean.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addBoolAndBoolsFromNestedStructWithXor").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setIntAtOffset(structSegmt, 0, 1);
    MemoryAccess.setIntAtOffset(structSegmt, 4, 0);
    MemoryAccess.setIntAtOffset(structSegmt, 8, 1);
    boolean result = (boolean) mh.invokeExact(true, structSegmt);
    Assert.assertEquals(result, true);
    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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 99 with Symbol

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

the class StructTests method test_addFloatAndFloatsFromNestedStruct_reverseOrder.

@Test
public void test_addFloatAndFloatsFromNestedStruct_reverseOrder() throws Throwable {
    GroupLayout nestedStructLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    GroupLayout structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2"));
    MethodType mt = MethodType.methodType(float.class, float.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_FLOAT, C_FLOAT, structLayout);
    Symbol functionSymbol = nativeLib.lookup("addFloatAndFloatsFromNestedStruct_reverseOrder").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    MemorySegment structSegmt = MemorySegment.allocateNative(structLayout);
    MemoryAccess.setFloatAtOffset(structSegmt, 0, 31.22F);
    MemoryAccess.setFloatAtOffset(structSegmt, 4, 33.44F);
    MemoryAccess.setFloatAtOffset(structSegmt, 8, 35.66F);
    float result = (float) mh.invokeExact(37.88F, structSegmt);
    Assert.assertEquals(result, 138.2F, 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) MethodHandle(java.lang.invoke.MethodHandle) Test(org.testng.annotations.Test)

Example 100 with Symbol

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

the class StructTests method test_addDoubleAndFloatDoubleFromStruct.

@Test
public void test_addDoubleAndFloatDoubleFromStruct() throws Throwable {
    GroupLayout structLayout = null;
    MemorySegment structSegmt = null;
    /* The size of [float, 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_FLOAT.withName("elem1"), C_DOUBLE.withName("elem2"));
        structSegmt = MemorySegment.allocateNative(structLayout);
        MemoryAccess.setFloatAtOffset(structSegmt, 0, 18.444F);
        MemoryAccess.setDoubleAtOffset(structSegmt, 4, 619.777D);
    } else {
        structLayout = MemoryLayout.ofStruct(C_FLOAT.withName("elem1"), MemoryLayout.ofPaddingBits(C_FLOAT.bitSize()), C_DOUBLE.withName("elem2"));
        VarHandle elemHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
        VarHandle elemHandle2 = structLayout.varHandle(double.class, PathElement.groupElement("elem2"));
        structSegmt = MemorySegment.allocateNative(structLayout);
        elemHandle1.set(structSegmt, 18.444F);
        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("addDoubleAndFloatDoubleFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    double result = (double) mh.invokeExact(113.567D, structSegmt);
    Assert.assertEquals(result, 751.788D, 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)

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