Search in sources :

Example 56 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests1 method test_addDoubleAndFloatDoubleFromStruct_1.

@Test
public void test_addDoubleAndFloatDoubleFromStruct_1() throws Throwable {
    GroupLayout structLayout = null;
    MemorySegment structSegmt = null;
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        /* 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.structLayout(C_FLOAT.withName("elem1"), C_DOUBLE.withName("elem2"));
            structSegmt = allocator.allocate(structLayout);
            MemoryAccess.setFloatAtOffset(structSegmt, 0, 18.444F);
            MemoryAccess.setDoubleAtOffset(structSegmt, 4, 619.777D);
        } else {
            structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), MemoryLayout.paddingLayout(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 = allocator.allocate(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);
        Addressable functionSymbol = nativeLibLookup.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);
    }
}
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 57 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests1 method test_add2FloatStructs_returnStruct_1.

@Test
public void test_add2FloatStructs_returnStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
    VarHandle floatHandle2 = structLayout.varHandle(float.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(MemorySegment.class, MemorySegment.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("add2FloatStructs_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);
        floatHandle1.set(structSegmt1, 25.12F);
        floatHandle2.set(structSegmt1, 11.23F);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        floatHandle1.set(structSegmt2, 24.34F);
        floatHandle2.set(structSegmt2, 13.45F);
        MemorySegment resultSegmt = (MemorySegment) mh.invokeExact(structSegmt1, structSegmt2);
        Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
        Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
    }
}
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 58 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests1 method test_addIntAndIntLongFromStruct_1.

@Test
public void test_addIntAndIntLongFromStruct_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), MemoryLayout.paddingLayout(C_INT.bitSize()), longLayout.withName("elem2"));
    VarHandle elemHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle elemHandle2 = structLayout.varHandle(long.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(long.class, int.class, MemorySegment.class);
    FunctionDescriptor fd = FunctionDescriptor.of(longLayout, C_INT, structLayout);
    Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntLongFromStruct").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        elemHandle1.set(structSegmt, 11223344);
        elemHandle2.set(structSegmt, 667788990011L);
        long result = (long) mh.invokeExact(22446688, structSegmt);
        Assert.assertEquals(result, 667822660043L);
    }
}
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 59 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests1 method test_addIntAndIntsFromStructPointer_1.

@Test
public void test_addIntAndIntsFromStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_INT.withName("elem1"), C_INT.withName("elem2"));
    VarHandle intHandle1 = structLayout.varHandle(int.class, PathElement.groupElement("elem1"));
    VarHandle intHandle2 = structLayout.varHandle(int.class, PathElement.groupElement("elem2"));
    MethodType mt = MethodType.methodType(int.class, int.class, MemoryAddress.class);
    FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, C_POINTER);
    Addressable functionSymbol = nativeLibLookup.lookup("addIntAndIntsFromStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt = allocator.allocate(structLayout);
        intHandle1.set(structSegmt, 11121314);
        intHandle2.set(structSegmt, 15161718);
        int result = (int) mh.invokeExact(19202122, structSegmt.address());
        Assert.assertEquals(result, 45485154);
    }
}
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 60 with VarHandle

use of java.lang.invoke.VarHandle in project openj9 by eclipse.

the class StructTests1 method test_add2FloatStructs_returnStructPointer_1.

@Test
public void test_add2FloatStructs_returnStructPointer_1() throws Throwable {
    GroupLayout structLayout = MemoryLayout.structLayout(C_FLOAT.withName("elem1"), C_FLOAT.withName("elem2"));
    VarHandle floatHandle1 = structLayout.varHandle(float.class, PathElement.groupElement("elem1"));
    VarHandle floatHandle2 = structLayout.varHandle(float.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("add2FloatStructs_returnStructPointer").get();
    MethodHandle mh = clinker.downcallHandle(functionSymbol, mt, fd);
    try (ResourceScope scope = ResourceScope.newConfinedScope()) {
        SegmentAllocator allocator = SegmentAllocator.ofScope(scope);
        MemorySegment structSegmt1 = allocator.allocate(structLayout);
        floatHandle1.set(structSegmt1, 25.12F);
        floatHandle2.set(structSegmt1, 11.23F);
        MemorySegment structSegmt2 = allocator.allocate(structLayout);
        floatHandle1.set(structSegmt2, 24.34F);
        floatHandle2.set(structSegmt2, 13.45F);
        MemoryAddress resultAddr = (MemoryAddress) mh.invokeExact(structSegmt1.address(), structSegmt2);
        MemorySegment resultSegmt = resultAddr.asSegment(structLayout.byteSize(), scope);
        Assert.assertEquals((float) floatHandle1.get(resultSegmt), 49.46F, 0.01F);
        Assert.assertEquals((float) floatHandle2.get(resultSegmt), 24.68F, 0.01F);
    }
}
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)

Aggregations

VarHandle (java.lang.invoke.VarHandle)400 Test (org.testng.annotations.Test)388 MethodHandle (java.lang.invoke.MethodHandle)319 FunctionDescriptor (jdk.incubator.foreign.FunctionDescriptor)318 GroupLayout (jdk.incubator.foreign.GroupLayout)318 MemorySegment (jdk.incubator.foreign.MemorySegment)318 ResourceScope (jdk.incubator.foreign.ResourceScope)254 SegmentAllocator (jdk.incubator.foreign.SegmentAllocator)254 MethodType (java.lang.invoke.MethodType)192 Addressable (jdk.incubator.foreign.Addressable)128 NativeSymbol (jdk.incubator.foreign.NativeSymbol)126 MemoryAddress (jdk.incubator.foreign.MemoryAddress)78 Symbol (jdk.incubator.foreign.LibraryLookup.Symbol)64 Test (org.junit.Test)8 VarHandleDesc (java.lang.invoke.VarHandle.VarHandleDesc)2 ClassDesc (java.lang.constant.ClassDesc)1 Field (java.lang.reflect.Field)1