From c0f1b7150aca2c9c5e9c6b5c5ee6853addcc9fe6 Mon Sep 17 00:00:00 2001 From: ChengJin01 Date: Wed, 1 Nov 2023 15:12:33 -0400 Subject: [PATCH] [FFI/Test_JDK22] Add test suites intended for union The changes add all the same test suites intended for union in JDK21 to JDK22 with the updated APIs introduced in JDK22. Signed-off-by: ChengJin01 --- .../jep442/downcall/UnionStructTests.java | 3 - .../jep442/upcall/UpcallMHWithUnionTests.java | 1 - test/functional/Java21Only/testng_210.xml | 4 + .../jep454/downcall/UnionStructTests.java | 943 ++++++ .../test/jep454/downcall/UnionTests.java | 2512 +++++++++++++++ .../upcall/UpcallMHWithUnionStructTests.java | 1030 +++++++ .../jep454/upcall/UpcallMHWithUnionTests.java | 2728 +++++++++++++++++ .../jep454/upcall/UpcallMethodHandles.java | 2122 ++++++++++++- test/functional/Java22andUp/testng_220.xml | 4 + 9 files changed, 9329 insertions(+), 18 deletions(-) create mode 100644 test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/UnionStructTests.java create mode 100644 test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/UnionTests.java create mode 100644 test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMHWithUnionStructTests.java create mode 100644 test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMHWithUnionTests.java diff --git a/test/functional/Java21Only/src/org/openj9/test/jep442/downcall/UnionStructTests.java b/test/functional/Java21Only/src/org/openj9/test/jep442/downcall/UnionStructTests.java index 126e8aa2818..e9a627537ea 100644 --- a/test/functional/Java21Only/src/org/openj9/test/jep442/downcall/UnionStructTests.java +++ b/test/functional/Java21Only/src/org/openj9/test/jep442/downcall/UnionStructTests.java @@ -26,7 +26,6 @@ import org.testng.AssertJUnit; import java.lang.invoke.MethodHandle; -import java.lang.invoke.VarHandle; import java.lang.foreign.Arena; import java.lang.foreign.Linker; @@ -825,8 +824,6 @@ public void test_addDoubleAndDoublesFromUnion_Nested4DoubleStruct() throws Throw @Test public void test_add2DoubleStructs_returnStruct_Nested2DoubleUnion() 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); MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleStructs_returnStruct_Nested2DoubleUnion").get(); diff --git a/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/UpcallMHWithUnionTests.java b/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/UpcallMHWithUnionTests.java index 8fec635af38..4b38f7ee88d 100644 --- a/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/UpcallMHWithUnionTests.java +++ b/test/functional/Java21Only/src/org/openj9/test/jep442/upcall/UpcallMHWithUnionTests.java @@ -46,7 +46,6 @@ */ @Test(groups = { "level.sanity" }) public class UpcallMHWithUnionTests { - private static boolean isAixOS = System.getProperty("os.name").toLowerCase().contains("aix"); private static Linker linker = Linker.nativeLinker(); static { diff --git a/test/functional/Java21Only/testng_210.xml b/test/functional/Java21Only/testng_210.xml index d159882498d..cf9236a1579 100644 --- a/test/functional/Java21Only/testng_210.xml +++ b/test/functional/Java21Only/testng_210.xml @@ -37,6 +37,8 @@ + + @@ -48,6 +50,8 @@ + + diff --git a/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/UnionStructTests.java b/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/UnionStructTests.java new file mode 100644 index 00000000000..4087e80ed85 --- /dev/null +++ b/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/UnionStructTests.java @@ -0,0 +1,943 @@ +/******************************************************************************* + * Copyright IBM Corp. and others 2023 + * + * This program and the accompanying materials are made available under + * the terms of the Eclipse Public License 2.0 which accompanies this + * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ + * or the Apache License, Version 2.0 which accompanies this distribution and + * is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * This Source Code may also be made available under the following + * Secondary Licenses when the conditions for such availability set + * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU + * General Public License, version 2 with the GNU Classpath + * Exception [1] and GNU General Public License, version 2 with the + * OpenJDK Assembly Exception [2]. + * + * [1] https://www.gnu.org/software/classpath/license.html + * [2] https://openjdk.org/legal/assembly-exception.html + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 + *******************************************************************************/ +package org.openj9.test.jep454.downcall; + +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.AssertJUnit; + +import java.lang.invoke.MethodHandle; + +import java.lang.foreign.Arena; +import java.lang.foreign.Linker; +import java.lang.foreign.FunctionDescriptor; +import java.lang.foreign.GroupLayout; +import java.lang.foreign.MemoryLayout; +import java.lang.foreign.MemoryLayout.PathElement; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.SegmentAllocator; +import java.lang.foreign.SequenceLayout; +import java.lang.foreign.SymbolLookup; +import java.lang.foreign.UnionLayout; +import java.lang.foreign.ValueLayout; +import static java.lang.foreign.ValueLayout.*; + +/** + * Test cases for JEP 454: Foreign Linker API for argument/return struct/union + * nested with struct/union in downcall. + * + * Note: + * The test suite is mainly intended for the following Linker API: + * MethodHandle downcallHandle(MemorySegment symbol, FunctionDescriptor function) + */ +@Test(groups = { "level.sanity" }) +public class UnionStructTests { + private static Linker linker = Linker.nativeLinker(); + + static { + System.loadLibrary("clinkerffitests"); + } + private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); + + @Test + public void test_addBoolAndBoolsFromStructWithXor_Nested2BoolUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithXor_Nested2BoolUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_BOOLEAN, 0, true); + structSegmt.set(JAVA_BOOLEAN, 1, false); + + boolean result = (boolean)mh.invokeExact(false, structSegmt); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithXor_Nested2BoolStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithXor_Nested2BoolStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, false); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invokeExact(true, unionSegmt); + Assert.assertEquals(result, false); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithXor_Nested4BoolStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), + JAVA_BOOLEAN.withName("elem2"), JAVA_BOOLEAN.withName("elem3"), JAVA_BOOLEAN.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithXor_Nested4BoolStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, false); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + unionSegmt.set(JAVA_BOOLEAN, 2, true); + unionSegmt.set(JAVA_BOOLEAN, 3, true); + + boolean result = (boolean)mh.invokeExact(true, unionSegmt); + Assert.assertEquals(result, false); + } + } + + @Test + public void test_add2BoolStructsWithXor_returnStruct_Nested2BoolUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2BoolStructsWithXor_returnStruct_Nested2BoolUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_BOOLEAN, 0, true); + structSegmt1.set(JAVA_BOOLEAN, 1, false); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_BOOLEAN, 0, true); + structSegmt2.set(JAVA_BOOLEAN, 1, true); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), false); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 1), true); + } + } + + @Test + public void test_add2BoolUnionsWithXor_returnUnion_Nested2BoolStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2BoolUnionsWithXor_returnUnion_Nested2BoolStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_BOOLEAN, 0, false); + unionSegmt1.set(JAVA_BOOLEAN, 1, true); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_BOOLEAN, 0, true); + unionSegmt2.set(JAVA_BOOLEAN, 1, false); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), true); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 1), true); + } + } + + @Test + public void test_addByteAndBytesFromStruct_Nested2ByteUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStruct_Nested2ByteUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_BYTE, 0, (byte)8); + structSegmt.set(JAVA_BYTE, 1, (byte)9); + + byte result = (byte)mh.invokeExact((byte)6, structSegmt); + Assert.assertEquals(result, 32); + } + } + + @Test + public void test_addByteAndBytesFromUnion_Nested2ByteStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnion_Nested2ByteStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)8); + unionSegmt.set(JAVA_BYTE, 1, (byte)9); + + byte result = (byte)mh.invokeExact((byte)6, unionSegmt); + Assert.assertEquals(result, 31); + } + } + + @Test + public void test_addByteAndBytesFromUnion_Nested4ByteStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), + JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnion_Nested4ByteStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)8); + unionSegmt.set(JAVA_BYTE, 1, (byte)9); + unionSegmt.set(JAVA_BYTE, 2, (byte)10); + unionSegmt.set(JAVA_BYTE, 3, (byte)11); + + byte result = (byte)mh.invokeExact((byte)6, unionSegmt); + Assert.assertEquals(result, 52); + } + } + + @Test + public void test_add2ByteStructs_returnStruct_Nested2ByteUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2ByteStructs_returnStruct_Nested2ByteUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_BYTE, 0, (byte)25); + structSegmt1.set(JAVA_BYTE, 1, (byte)11); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_BYTE, 0, (byte)24); + structSegmt2.set(JAVA_BYTE, 1, (byte)13); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), (byte)49); + Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 1), (byte)24); + } + } + + @Test + public void test_add2ByteUnions_returnUnion_Nested2ByteStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2ByteUnions_returnUnion_Nested2ByteStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_BYTE, 0, (byte)25); + unionSegmt1.set(JAVA_BYTE, 1, (byte)11); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_BYTE, 0, (byte)24); + unionSegmt2.set(JAVA_BYTE, 1, (byte)13); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), (byte)49); + Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 1), (byte)24); + } + } + + @Test + public void test_addCharAndCharsFromStruct_Nested2CharUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStruct_Nested2CharUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_CHAR, 0, 'A'); + structSegmt.set(JAVA_CHAR, 2, 'B'); + + char result = (char)mh.invokeExact('C', structSegmt); + Assert.assertEquals(result, 'E'); + } + } + + @Test + public void test_addCharAndCharsFromUnion_Nested2CharStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnion_Nested2CharStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'A'); + unionSegmt.set(JAVA_CHAR, 2, 'B'); + + char result = (char)mh.invokeExact('C', unionSegmt); + Assert.assertEquals(result, 'D'); + } + } + + @Test + public void test_addCharAndCharsFromUnion_Nested4CharStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), + JAVA_CHAR.withName("elem2"), JAVA_CHAR.withName("elem3"), JAVA_CHAR.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnion_Nested4CharStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'A'); + unionSegmt.set(JAVA_CHAR, 2, 'B'); + unionSegmt.set(JAVA_CHAR, 4, 'C'); + unionSegmt.set(JAVA_CHAR, 6, 'D'); + + char result = (char)mh.invokeExact('E', unionSegmt); + Assert.assertEquals(result, 'K'); + } + } + + @Test + public void test_add2CharStructs_returnStruct_Nested2CharUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2CharStructs_returnStruct_Nested2CharUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_CHAR, 0, 'A'); + structSegmt1.set(JAVA_CHAR, 2, 'B'); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_CHAR, 0, 'C'); + structSegmt2.set(JAVA_CHAR, 2, 'D'); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'C'); + Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 2), 'E'); + } + } + + @Test + public void test_add2CharUnions_returnUnion_Nested2CharStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2CharUnions_returnUnion_Nested2CharStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_CHAR, 0, 'A'); + unionSegmt1.set(JAVA_CHAR, 2, 'B'); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_CHAR, 0, 'C'); + unionSegmt2.set(JAVA_CHAR, 2, 'D'); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'C'); + Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 2), 'E'); + } + } + + public void test_addShortAndShortsFromStruct_Nested2ShortUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStruct_Nested2ShortUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_SHORT, 0, (short)8); + structSegmt.set(JAVA_SHORT, 2, (short)9); + + short result = (short)mh.invokeExact((short)6, structSegmt); + Assert.assertEquals(result, 32); + } + } + + @Test + public void test_addShortAndShortsFromUnion_Nested2ShortStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnion_Nested2ShortStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)8); + unionSegmt.set(JAVA_SHORT, 2, (short)9); + + short result = (short)mh.invokeExact((short)6, unionSegmt); + Assert.assertEquals(result, 31); + } + } + + @Test + public void test_addShortAndShortsFromUnion_Nested4ShortStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3"), JAVA_SHORT.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnion_Nested4ShortStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)1111); + unionSegmt.set(JAVA_SHORT, 2, (short)2222); + unionSegmt.set(JAVA_SHORT, 4, (short)3333); + unionSegmt.set(JAVA_SHORT, 6, (short)4444); + + short result = (short)mh.invokeExact((short)5555, unionSegmt); + Assert.assertEquals(result, 17776); + } + } + + @Test + public void test_add2ShortStructs_returnStruct_Nested2ShortUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2ShortStructs_returnStruct_Nested2ShortUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_SHORT, 0, (short)55); + structSegmt1.set(JAVA_SHORT, 2, (short)45); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_SHORT, 0, (short)78); + structSegmt2.set(JAVA_SHORT, 2, (short)67); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 133); + Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 2), 112); + } + } + + @Test + public void test_add2ShortUnions_returnUnion_Nested2ShortStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2ShortUnions_returnUnion_Nested2ShortStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_SHORT, 0, (short)55); + unionSegmt1.set(JAVA_SHORT, 2, (short)45); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_SHORT, 0, (short)78); + unionSegmt2.set(JAVA_SHORT, 2, (short)67); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 133); + Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 2), 112); + } + } + + @Test + public void test_addIntAndIntsFromStruct_Nested2IntUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStruct_Nested2IntUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_INT, 0, 1122334); + structSegmt.set(JAVA_INT, 4, 1234567); + + int result = (int)mh.invokeExact(2244668, structSegmt); + Assert.assertEquals(result, 5836136); + } + } + + @Test + public void test_addIntAndIntsFromUnion_Nested2IntStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnion_Nested2IntStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 112233445); + unionSegmt.set(JAVA_INT, 4, 1234567890); + + int result = (int)mh.invokeExact(2244668, unionSegmt); + Assert.assertEquals(result, 1461279448); + } + } + + @Test + public void test_addIntAndIntsFromUnion_Nested4IntStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), + JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3"), JAVA_INT.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnion_Nested4IntStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 11223344); + unionSegmt.set(JAVA_INT, 4, 55667788); + unionSegmt.set(JAVA_INT, 8, 99001122); + unionSegmt.set(JAVA_INT, 12, 33445566); + + int result = (int)mh.invokeExact(22446688, unionSegmt); + Assert.assertEquals(result, 233007852); + } + } + + @Test + public void test_add2IntStructs_returnStruct_Nested2IntUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStruct_Nested2IntUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_INT, 0, 11223344); + structSegmt1.set(JAVA_INT, 4, 55667788); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_INT, 0, 99001122); + structSegmt2.set(JAVA_INT, 4, 33445566); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 110224466); + Assert.assertEquals(resultSegmt.get(JAVA_INT, 4), 89113354); + } + } + + @Test + public void test_add2IntUnions_returnUnion_Nested2IntStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2IntUnions_returnUnion_Nested2IntStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_INT, 0, 11223344); + unionSegmt1.set(JAVA_INT, 4, 55667788); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_INT, 0, 99001122); + unionSegmt2.set(JAVA_INT, 4, 33445566); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 110224466); + Assert.assertEquals(resultSegmt.get(JAVA_INT, 4), 89113354); + } + } + + @Test + public void test_addLongAndLongsFromStruct_Nested2LongUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStruct_Nested2LongUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_LONG, 0, 1234567890L); + structSegmt.set(JAVA_LONG, 8, 9876543210L); + + long result = (long)mh.invokeExact(2468024680L, structSegmt); + Assert.assertEquals(result, 23455678990L); + } + } + + @Test + public void test_addLongAndLongsFromUnion_Nested2LongStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnion_Nested2LongStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 1111111L); + unionSegmt.set(JAVA_LONG, 8, 2222222L); + + long result = (long)mh.invokeExact(3333333L, unionSegmt); + Assert.assertEquals(result, 7777777L); + } + } + + @Test + public void test_addLongAndLongsFromUnion_Nested4LongStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), + JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3"), JAVA_LONG.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnion_Nested4LongStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 1111111L); + unionSegmt.set(JAVA_LONG, 8, 2222222L); + unionSegmt.set(JAVA_LONG, 16, 3333333L); + unionSegmt.set(JAVA_LONG, 24, 4444444L); + + long result = (long)mh.invokeExact(5555555L, unionSegmt); + Assert.assertEquals(result, 17777776L); + } + } + + @Test + public void test_add2LongStructs_returnStruct_Nested2LongUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2LongStructs_returnStruct_Nested2LongUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_LONG, 0, 987654321987L); + structSegmt1.set(JAVA_LONG, 8, 123456789123L); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_LONG, 0, 224466880022L); + structSegmt2.set(JAVA_LONG, 8, 113355779911L); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 1212121202009L); + Assert.assertEquals(resultSegmt.get(JAVA_LONG, 8), 236812569034L); + } + } + + @Test + public void test_add2LongUnions_returnUnion_Nested2LongStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2LongUnions_returnUnion_Nested2LongStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_LONG, 0, 987654321987L); + unionSegmt1.set(JAVA_LONG, 8, 123456789123L); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_LONG, 0, 224466880022L); + unionSegmt2.set(JAVA_LONG, 8, 113355779911L); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 1212121202009L); + Assert.assertEquals(resultSegmt.get(JAVA_LONG, 8), 236812569034L); + } + } + + @Test + public void test_addFloatAndFloatsFromStruct_Nested2FloatUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStruct_Nested2FloatUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_FLOAT, 0, 8.12F); + structSegmt.set(JAVA_FLOAT, 4, 9.24F); + + float result = (float)mh.invokeExact(6.56F, structSegmt); + Assert.assertEquals(result, 33.16F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnion_Nested2FloatStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnion_Nested2FloatStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 8.12F); + unionSegmt.set(JAVA_FLOAT, 4, 9.24F); + + float result = (float)mh.invokeExact(6.56F, unionSegmt); + Assert.assertEquals(result, 32.04F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnion_Nested4FloatStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_FLOAT.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnion_Nested4FloatStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 8.12F); + unionSegmt.set(JAVA_FLOAT, 4, 9.24F); + unionSegmt.set(JAVA_FLOAT, 8, 10.48F); + unionSegmt.set(JAVA_FLOAT, 12, 11.12F); + + float result = (float)mh.invokeExact(6.56F, unionSegmt); + Assert.assertEquals(result, 53.64F, 0.01F); + } + } + + @Test + public void test_add2FloatStructs_returnStruct_Nested2FloatUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2FloatStructs_returnStruct_Nested2FloatUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_FLOAT, 0, 25.12F); + structSegmt1.set(JAVA_FLOAT, 4, 11.23F); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_FLOAT, 0, 24.34F); + structSegmt2.set(JAVA_FLOAT, 4, 11.25F); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.46F, 0.01F); + Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 4), 22.48F, 0.01F); + } + } + + @Test + public void test_add2FloatUnions_returnUnion_Nested2FloatStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2FloatUnions_returnUnion_Nested2FloatStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_FLOAT, 0, 25.12F); + unionSegmt1.set(JAVA_FLOAT, 4, 11.23F); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_FLOAT, 0, 24.34F); + unionSegmt2.set(JAVA_FLOAT, 4, 13.45F); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.46F, 0.01F); + Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 4), 24.68F, 0.01F); + } + } + + @Test + public void test_addDoubleAndDoublesFromStruct_Nested2DoubleUnion() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStruct_Nested2DoubleUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_DOUBLE, 0, 2228.111D); + structSegmt.set(JAVA_DOUBLE, 8, 2229.221D); + + double result = (double)mh.invokeExact(3336.333D, structSegmt); + Assert.assertEquals(result, 10022.886D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnion_Nested2DoubleStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnion_Nested2DoubleStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 2228.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 2229.221D); + + double result = (double)mh.invokeExact(3336.333D, unionSegmt); + Assert.assertEquals(result, 10021.776D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnion_Nested4DoubleStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3"), JAVA_DOUBLE.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnion_Nested4DoubleStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 2228.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 2229.221D); + unionSegmt.set(JAVA_DOUBLE, 16, 2230.221D); + unionSegmt.set(JAVA_DOUBLE, 24, 2231.221D); + + double result = (double)mh.invokeExact(3336.333D, unionSegmt); + Assert.assertEquals(result, 14483.218D, 0.001D); + } + } + + @Test + public void test_add2DoubleStructs_returnStruct_Nested2DoubleUnion() throws Throwable { + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleStructs_returnStruct_Nested2DoubleUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_DOUBLE, 0, 11.222D); + structSegmt1.set(JAVA_DOUBLE, 8, 22.333D); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_DOUBLE, 0, 33.444D); + structSegmt2.set(JAVA_DOUBLE, 8, 44.555D); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); + Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); + } + } + + @Test + public void test_add2DoubleUnions_returnUnion_Nested2DoubleStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleUnions_returnUnion_Nested2DoubleStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_DOUBLE, 0, 11.222D); + unionSegmt1.set(JAVA_DOUBLE, 8, 22.333D); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_DOUBLE, 0, 33.444D); + unionSegmt2.set(JAVA_DOUBLE, 8, 44.555D); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); + Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); + } + } + + @Test + public void test_addShortAndShortByteFromUnion_Nested2ByteStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortByteFromUnion_Nested2ByteStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)12345); + + short result = (short)mh.invokeExact((short)11111, unionSegmt); + Assert.assertEquals(result, 23561); + } + } + + @Test + public void test_addShortAndBytesFromUnion_Nested4ByteStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), + JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndBytesFromUnion_Nested4ByteStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)11); + unionSegmt.set(JAVA_BYTE, 1, (byte)22); + unionSegmt.set(JAVA_BYTE, 2, (byte)33); + unionSegmt.set(JAVA_BYTE, 3, (byte)44); + + short result = (short)mh.invokeExact((short)11111, unionSegmt); + Assert.assertEquals(result, 11221); + } + } + + @Test + public void test_addIntAndIntShortFromUnion_Nested2ShortStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntShortFromUnion_Nested2ShortStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 1234567890); + + int result = (int)mh.invokeExact(22446688, unionSegmt); + Assert.assertEquals(result, 1257034138); + } + } + + @Test + public void test_addIntAndShortsFromUnion_Nested4ShortStruct() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), + JAVA_SHORT.withName("elem3"), JAVA_SHORT.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndShortsFromUnion_Nested4ShortStruct").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)11111); + unionSegmt.set(JAVA_SHORT, 2, (short)11112); + unionSegmt.set(JAVA_SHORT, 4, (short)11113); + unionSegmt.set(JAVA_SHORT, 6, (short)11114); + + int result = (int)mh.invokeExact(22446688, unionSegmt); + Assert.assertEquals(result, 22491138); + } + } +} diff --git a/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/UnionTests.java b/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/UnionTests.java new file mode 100644 index 00000000000..c8108a57d2f --- /dev/null +++ b/test/functional/Java22andUp/src/org/openj9/test/jep454/downcall/UnionTests.java @@ -0,0 +1,2512 @@ +/******************************************************************************* + * Copyright IBM Corp. and others 2023 + * + * This program and the accompanying materials are made available under + * the terms of the Eclipse Public License 2.0 which accompanies this + * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ + * or the Apache License, Version 2.0 which accompanies this distribution and + * is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * This Source Code may also be made available under the following + * Secondary Licenses when the conditions for such availability set + * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU + * General Public License, version 2 with the GNU Classpath + * Exception [1] and GNU General Public License, version 2 with the + * OpenJDK Assembly Exception [2]. + * + * [1] https://www.gnu.org/software/classpath/license.html + * [2] https://openjdk.org/legal/assembly-exception.html + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 + *******************************************************************************/ +package org.openj9.test.jep454.downcall; + +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.AssertJUnit; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.VarHandle; + +import java.lang.foreign.Arena; +import java.lang.foreign.Linker; +import java.lang.foreign.FunctionDescriptor; +import java.lang.foreign.MemoryLayout; +import java.lang.foreign.MemoryLayout.PathElement; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.SegmentAllocator; +import java.lang.foreign.SequenceLayout; +import java.lang.foreign.SymbolLookup; +import java.lang.foreign.UnionLayout; +import java.lang.foreign.ValueLayout; +import static java.lang.foreign.ValueLayout.*; + +/** + * Test cases for JEP 454: Foreign Linker API for argument/return union in downcall. + * + * Note: + * The test suite is mainly intended for the following Linker API: + * MethodHandle downcallHandle(MemorySegment symbol, FunctionDescriptor function) + */ +@Test(groups = { "level.sanity" }) +public class UnionTests { + private static Linker linker = Linker.nativeLinker(); + + static { + System.loadLibrary("clinkerffitests"); + } + private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); + + @Test + public void test_addBoolAndBoolsFromUnionWithXor() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithXor").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + boolHandle1.set(unionSegmt, 0L, true); + + boolean result = (boolean)mh.invokeExact(true, unionSegmt); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionPtrWithXor() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionPtrWithXor").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + boolHandle2.set(unionSegmt, 0L, false); + + boolean result = (boolean)mh.invoke(true, unionSegmt); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromNestedUnionWithXor() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_BOOLEAN.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedUnionWithXor").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, true); + + boolean result = (boolean)mh.invokeExact(false, unionSegmt); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromNestedUnionWithXor_reverseOrder() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedUnionWithXor_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, true); + + boolean result = (boolean)mh.invokeExact(true, unionSegmt); + Assert.assertEquals(result, false); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithNestedBoolArray() throws Throwable { + SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); + UnionLayout unionLayout = MemoryLayout.unionLayout(boolArray.withName("array_elem1"), JAVA_BOOLEAN.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithNestedBoolArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, true); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invokeExact(false, unionSegmt); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithNestedBoolArray_reverseOrder() throws Throwable { + SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), boolArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithNestedBoolArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, false); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invokeExact(false, unionSegmt); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithNestedUnionArray() throws Throwable { + UnionLayout boolUnion = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, boolUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_BOOLEAN.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithNestedUnionArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, true); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invokeExact(true, unionSegmt); + Assert.assertEquals(result, false); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithNestedUnionArray_reverseOrder() throws Throwable { + UnionLayout boolUnion = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, boolUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithNestedUnionArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, false); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invokeExact(true, unionSegmt); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_add2BoolUnionsWithXor_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2BoolUnionsWithXor_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + boolHandle1.set(unionSegmt1, 0L, true); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + boolHandle2.set(unionSegmt2, 0L, false); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(boolHandle1.get(resultSegmt, 0L), true); + Assert.assertEquals(boolHandle2.get(resultSegmt, 0L), true); + } + } + + @Test + public void test_add2BoolUnionsWithXor_returnUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2BoolUnionsWithXor_returnUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + boolHandle2.set(unionSegmt1, 0L, true); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + boolHandle1.set(unionSegmt2, 0L, true); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), true); + } + } + + @Test + public void test_add3BoolUnionsWithXor_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), + JAVA_BOOLEAN.withName("elem2"), JAVA_BOOLEAN.withName("elem3")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle boolHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add3BoolUnionsWithXor_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + boolHandle3.set(unionSegmt1, 0L, true); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + boolHandle2.set(unionSegmt2, 0L, false); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(boolHandle1.get(resultSegmt, 0L), true); + Assert.assertEquals(boolHandle2.get(resultSegmt, 0L), true); + Assert.assertEquals(boolHandle3.get(resultSegmt, 0L), true); + } + } + + @Test + public void test_addByteAndBytesFromUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt, 0L, (byte)8); + + byte result = (byte)mh.invokeExact((byte)6, unionSegmt); + Assert.assertEquals(result, 22); + } + } + + @Test + public void test_addByteAndBytesFromUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + byteHandle2.set(unionSegmt, 0L, (byte)12); + + byte result = (byte)mh.invoke((byte)13, unionSegmt); + Assert.assertEquals(result, 37); + } + } + + @Test + public void test_addByteAndBytesFromNestedUnion() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_BYTE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)11); + + byte result = (byte)mh.invokeExact((byte)33, unionSegmt); + Assert.assertEquals(result, 66); + } + } + + @Test + public void test_addByteAndBytesFromNestedUnion_reverseOrder() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedUnion_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)12); + + byte result = (byte)mh.invokeExact((byte)48, unionSegmt); + Assert.assertEquals(result, 84); + } + } + + @Test + public void test_addByteAndBytesFromUnionWithNestedByteArray() throws Throwable { + SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); + UnionLayout unionLayout = MemoryLayout.unionLayout(byteArray.withName("array_elem1"), JAVA_BYTE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionWithNestedByteArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)11); + unionSegmt.set(JAVA_BYTE, 1, (byte)12); + + byte result = (byte)mh.invokeExact((byte)14, unionSegmt); + Assert.assertEquals(result, 48); + } + } + + @Test + public void test_addByteAndBytesFromUnionWithNestedByteArray_reverseOrder() throws Throwable { + SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), byteArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionWithNestedByteArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)12); + unionSegmt.set(JAVA_BYTE, 1, (byte)14); + + byte result = (byte)mh.invokeExact((byte)18, unionSegmt); + Assert.assertEquals(result, 56); + } + } + + @Test + public void test_addByteAndBytesFromUnionWithNestedUnionArray() throws Throwable { + UnionLayout byteUnion = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, byteUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_BYTE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionWithNestedUnionArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)11); + unionSegmt.set(JAVA_BYTE, 1, (byte)12); + + byte result = (byte)mh.invokeExact((byte)16, unionSegmt); + Assert.assertEquals(result, 73); + } + } + + @Test + public void test_addByteAndBytesFromUnionWithNestedUnionArray_reverseOrder() throws Throwable { + UnionLayout byteUnion = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, byteUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionWithNestedUnionArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)12); + unionSegmt.set(JAVA_BYTE, 1, (byte)14); + + byte result = (byte)mh.invokeExact((byte)22, unionSegmt); + Assert.assertEquals(result, 86); + } + } + + @Test + public void test_add2ByteUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2ByteUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt1, 0L, (byte)25); + byteHandle2.set(unionSegmt1, 0L, (byte)11); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt2, 0L, (byte)24); + byteHandle2.set(unionSegmt2, 0L, (byte)13); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals((byte)byteHandle1.get(resultSegmt, 0L), (byte)24); + Assert.assertEquals((byte)byteHandle2.get(resultSegmt, 0L), (byte)24); + } + } + + @Test + public void test_add2ByteUnions_returnUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2ByteUnions_returnUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt1, 0L, (byte)25); + byteHandle2.set(unionSegmt1, 0L, (byte)11); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt2, 0L, (byte)24); + byteHandle2.set(unionSegmt2, 0L, (byte)13); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals((byte)byteHandle1.get(resultSegmt, 0L), (byte)37); + Assert.assertEquals((byte)byteHandle2.get(resultSegmt, 0L), (byte)37); + } + } + + @Test + public void test_add3ByteUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle byteHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add3ByteUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt1, 0L, (byte)25); + byteHandle2.set(unionSegmt1, 0L, (byte)11); + byteHandle3.set(unionSegmt1, 0L, (byte)12); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt2, 0L, (byte)24); + byteHandle2.set(unionSegmt2, 0L, (byte)13); + byteHandle3.set(unionSegmt2, 0L, (byte)16); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals((byte)byteHandle1.get(resultSegmt, 0L), (byte)28); + Assert.assertEquals((byte)byteHandle2.get(resultSegmt, 0L), (byte)28); + Assert.assertEquals((byte)byteHandle3.get(resultSegmt, 0L), (byte)28); + } + } + + @Test + public void test_addCharAndCharsFromUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + charHandle1.set(unionSegmt, 0L, 'A'); + charHandle2.set(unionSegmt, 0L, 'B'); + + char result = (char)mh.invokeExact('C', unionSegmt); + Assert.assertEquals(result, 'E'); + } + } + + @Test + public void test_addCharAndCharsFromUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + charHandle1.set(unionSegmt, 0L, 'H'); + charHandle2.set(unionSegmt, 0L, 'I'); + + char result = (char)mh.invoke('G', unionSegmt); + Assert.assertEquals(result, 'W'); + } + } + + @Test + public void test_addCharAndCharsFromNestedUnion() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_CHAR.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'E'); + + char result = (char)mh.invokeExact('H', unionSegmt); + Assert.assertEquals(result, 'T'); + } + } + + @Test + public void test_addCharAndCharsFromNestedUnion_reverseOrder() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedUnion_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'F'); + + char result = (char)mh.invokeExact('H', unionSegmt); + Assert.assertEquals(result, 'W'); + } + } + + @Test + public void test_addCharAndCharsFromUnionWithNestedCharArray() throws Throwable { + SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); + UnionLayout unionLayout = MemoryLayout.unionLayout(charArray.withName("array_elem1"), JAVA_CHAR.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionWithNestedCharArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'A'); + unionSegmt.set(JAVA_CHAR, 2, 'B'); + + char result = (char)mh.invokeExact('D', unionSegmt); + Assert.assertEquals(result, 'E'); + } + } + + @Test + public void test_addCharAndCharsFromUnionWithNestedCharArray_reverseOrder() throws Throwable { + SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), charArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionWithNestedCharArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'A'); + unionSegmt.set(JAVA_CHAR, 2, 'B'); + + char result = (char)mh.invokeExact('D', unionSegmt); + Assert.assertEquals(result, 'E'); + } + } + + @Test + public void test_addCharAndCharsFromUnionWithNestedUnionArray() throws Throwable { + UnionLayout charUnion = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, charUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_CHAR.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionWithNestedUnionArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'B'); + unionSegmt.set(JAVA_CHAR, 2, 'C'); + + char result = (char)mh.invokeExact('J', unionSegmt); + Assert.assertEquals(result, 'Q'); + } + } + + @Test + public void test_addCharAndCharsFromUnionWithNestedUnionArray_reverseOrder() throws Throwable { + UnionLayout charUnion = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, charUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionWithNestedUnionArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'B'); + unionSegmt.set(JAVA_CHAR, 2, 'C'); + + char result = (char)mh.invokeExact('J', unionSegmt); + Assert.assertEquals(result, 'Q'); + } + } + + @Test + public void test_add2CharUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2CharUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt1, 0L, 'A'); + charHandle2.set(unionSegmt1, 0L, 'B'); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt2, 0L, 'C'); + charHandle2.set(unionSegmt2, 0L, 'D'); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(charHandle1.get(resultSegmt, 0L), 'E'); + Assert.assertEquals(charHandle2.get(resultSegmt, 0L), 'E'); + } + } + + @Test + public void test_add2CharUnions_returnUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2CharUnions_returnUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt1, 0L, 'A'); + charHandle2.set(unionSegmt1, 0L, 'B'); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt2, 0L, 'B'); + charHandle2.set(unionSegmt2, 0L, 'C'); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals(charHandle1.get(resultSegmt, 0L), 'F'); + Assert.assertEquals(charHandle2.get(resultSegmt, 0L), 'F'); + } + } + + @Test + public void test_add3CharUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), + JAVA_CHAR.withName("elem2"), JAVA_CHAR.withName("elem3")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle charHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add3CharUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt1, 0L, 'A'); + charHandle2.set(unionSegmt1, 0L, 'B'); + charHandle3.set(unionSegmt1, 0L, 'C'); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt2, 0L, 'B'); + charHandle2.set(unionSegmt2, 0L, 'C'); + charHandle3.set(unionSegmt2, 0L, 'D'); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(charHandle1.get(resultSegmt, 0L), 'F'); + Assert.assertEquals(charHandle2.get(resultSegmt, 0L), 'F'); + Assert.assertEquals(charHandle3.get(resultSegmt, 0L), 'F'); + } + } + + @Test + public void test_addShortAndShortsFromUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + shortHandle2.set(unionSegmt, 0L, (short)9); + + short result = (short)mh.invokeExact((short)6, unionSegmt); + Assert.assertEquals(result, 24); + } + } + + @Test + public void test_addShortAndShortsFromUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt, 0L, (short)22); + + short result = (short)mh.invoke((short)66, unionSegmt); + Assert.assertEquals(result, 110); + } + } + + @Test + public void test_addShortAndShortsFromNestedUnion() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_SHORT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)31); + + short result = (short)mh.invokeExact((short)37, unionSegmt); + Assert.assertEquals(result, 130); + } + } + + @Test + public void test_addShortAndShortsFromNestedUnion_reverseOrder() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedUnion_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)31); + + short result = (short)mh.invokeExact((short)37, unionSegmt); + Assert.assertEquals(result, 130); + } + } + + @Test + public void test_addShortAndShortsFromUnionWithNestedShortArray() throws Throwable { + SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); + UnionLayout unionLayout = MemoryLayout.unionLayout(shortArray.withName("array_elem1"), JAVA_SHORT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionWithNestedShortArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)111); + unionSegmt.set(JAVA_SHORT, 2, (short)222); + + short result = (short)mh.invokeExact((short)444, unionSegmt); + Assert.assertEquals(result, 888); + } + } + + @Test + public void test_addShortAndShortsFromUnionWithNestedShortArray_reverseOrder() throws Throwable { + SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), shortArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionWithNestedShortArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)111); + unionSegmt.set(JAVA_SHORT, 2, (short)222); + + short result = (short)mh.invokeExact((short)444, unionSegmt); + Assert.assertEquals(result, 888); + } + } + + @Test + public void test_addShortAndShortsFromUnionWithNestedUnionArray() throws Throwable { + UnionLayout shortUnion = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, shortUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_SHORT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionWithNestedUnionArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)111); + unionSegmt.set(JAVA_SHORT, 2, (short)222); + + short result = (short)mh.invokeExact((short)666, unionSegmt); + Assert.assertEquals(result, 1443); + } + } + + @Test + public void test_addShortAndShortsFromUnionWithNestedUnionArray_reverseOrder() throws Throwable { + UnionLayout shortUnion = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, shortUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionWithNestedUnionArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)111); + unionSegmt.set(JAVA_SHORT, 2, (short)222); + + short result = (short)mh.invokeExact((short)666, unionSegmt); + Assert.assertEquals(result, 1443); + } + } + + @Test + public void test_add2ShortUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2ShortUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt1, 0L, (short)56); + shortHandle2.set(unionSegmt1, 0L, (short)45); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt2, 0L, (short)78); + shortHandle2.set(unionSegmt2, 0L, (short)67); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals((short)shortHandle1.get(resultSegmt, 0L), (short)112); + Assert.assertEquals((short)shortHandle2.get(resultSegmt, 0L), (short)112); + } + } + + @Test + public void test_add2ShortUnions_returnUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2ShortUnions_returnUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt1, 0L, (short)56); + shortHandle2.set(unionSegmt1, 0L, (short)45); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt2, 0L, (short)78); + shortHandle2.set(unionSegmt2, 0L, (short)67); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals((short)shortHandle1.get(resultSegmt, 0L), (short)179); + Assert.assertEquals((short)shortHandle2.get(resultSegmt, 0L), (short)179); + } + } + + @Test + public void test_add3ShortUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle shortHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add3ShortUnions_returnUnion").get(); + + try (Arena arena = Arena.ofConfined()) { + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt1, 0L, (short)25); + shortHandle2.set(unionSegmt1, 0L, (short)26); + shortHandle3.set(unionSegmt1, 0L, (short)27); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt2, 0L, (short)34); + shortHandle2.set(unionSegmt2, 0L, (short)35); + shortHandle3.set(unionSegmt2, 0L, (short)36); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals((short)shortHandle1.get(resultSegmt, 0L), (short)63); + Assert.assertEquals((short)shortHandle2.get(resultSegmt, 0L), (short)63); + Assert.assertEquals((short)shortHandle3.get(resultSegmt, 0L), (short)63); + } + } + + @Test + public void test_addIntAndIntsFromUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + intHandle2.set(unionSegmt, 0L, 1234567); + + int result = (int)mh.invokeExact(2244668, unionSegmt); + Assert.assertEquals(result, 4713802); + } + } + + @Test + public void test_addIntAndIntsFromUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + intHandle1.set(unionSegmt, 0L, 11121314); + intHandle2.set(unionSegmt, 0L, 15161718); + + int result = (int)mh.invoke(19202122, unionSegmt); + Assert.assertEquals(result, 49525558); + } + } + + @Test + public void test_addIntAndIntsFromNestedUnion() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_INT.withName("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 21222324); + + int result = (int)mh.invokeExact(33343536, unionSegmt); + Assert.assertEquals(result, 97010508); + } + } + + @Test + public void test_addIntAndIntsFromNestedUnion_reverseOrder() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedUnion_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 21222324); + + int result = (int)mh.invokeExact(33343536, unionSegmt); + Assert.assertEquals(result, 97010508); + } + } + + @Test + public void test_addIntAndIntsFromUnionWithNestedIntArray() throws Throwable { + SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); + UnionLayout unionLayout = MemoryLayout.unionLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionWithNestedIntArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 1111111); + unionSegmt.set(JAVA_INT, 4, 2222222); + + int result = (int)mh.invokeExact(4444444, unionSegmt); + Assert.assertEquals(result, 8888888); + } + } + + @Test + public void test_addIntAndIntsFromUnionWithNestedIntArray_reverseOrder() throws Throwable { + SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionWithNestedIntArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 1111111); + unionSegmt.set(JAVA_INT, 4, 2222222); + + int result = (int)mh.invokeExact(4444444, unionSegmt); + Assert.assertEquals(result, 8888888); + } + } + + @Test + public void test_addIntAndIntsFromUnionWithNestedUnionArray() throws Throwable { + UnionLayout intUnion = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, intUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_INT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionWithNestedUnionArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 2222222); + unionSegmt.set(JAVA_INT, 4, 3333333); + + int result = (int)mh.invokeExact(6666666, unionSegmt); + Assert.assertEquals(result, 19999998); + } + } + + @Test + public void test_addIntAndIntsFromUnionWithNestedUnionArray_reverseOrder() throws Throwable { + UnionLayout intUnion = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, intUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionWithNestedUnionArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 2222222); + unionSegmt.set(JAVA_INT, 4, 3333333); + + int result = (int)mh.invokeExact(6666666, unionSegmt); + Assert.assertEquals(result, 19999998); + } + } + + @Test + public void test_add2IntUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2IntUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt1, 0L, 11223344); + intHandle2.set(unionSegmt1, 0L, 55667788); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt2, 0L, 99001122); + intHandle2.set(unionSegmt2, 0L, 33445566); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(intHandle1.get(resultSegmt, 0L), 89113354); + Assert.assertEquals(intHandle2.get(resultSegmt, 0L), 89113354); + } + } + + @Test + public void test_add2IntUnions_returnUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2IntUnions_returnUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt1, 0L, 11223344); + intHandle2.set(unionSegmt1, 0L, 55667788); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt2, 0L, 99001122); + intHandle2.set(unionSegmt2, 0L, 33445566); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals(intHandle1.get(resultSegmt, 0L), 122558920); + Assert.assertEquals(intHandle2.get(resultSegmt, 0L), 122558920); + } + } + + @Test + public void test_add3IntUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle intHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add3IntUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt1, 0L, 11223344); + intHandle2.set(unionSegmt1, 0L, 55667788); + intHandle3.set(unionSegmt1, 0L, 99001122); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt2, 0L, 33445566); + intHandle2.set(unionSegmt2, 0L, 77889900); + intHandle3.set(unionSegmt2, 0L, 44332211); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(intHandle1.get(resultSegmt, 0L), 143333333); + Assert.assertEquals(intHandle2.get(resultSegmt, 0L), 143333333); + Assert.assertEquals(intHandle3.get(resultSegmt, 0L), 143333333); + } + } + + @Test + public void test_addLongAndLongsFromUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + longHandle1.set(unionSegmt, 0L, 1234567890L); + longHandle2.set(unionSegmt, 0L, 9876543210L); + + long result = (long)mh.invokeExact(2468024680L, unionSegmt); + Assert.assertEquals(result, 22221111100L); + } + } + + @Test + public void test_addLongAndLongsFromUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + longHandle1.set(unionSegmt, 0L, 224466880022L); + longHandle2.set(unionSegmt, 0L, 446688002244L); + + long result = (long)mh.invoke(668800224466L, unionSegmt); + Assert.assertEquals(result, 1562176228954L); + } + } + + @Test + public void test_addLongAndLongsFromNestedUnion() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 135791357913L); + + long result = (long)mh.invokeExact(778899001122L, unionSegmt); + Assert.assertEquals(result, 1186273074861L); + } + } + + @Test + public void test_addLongAndLongsFromNestedUnion_reverseOrder() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedUnion_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 135791357913L); + + long result = (long)mh.invokeExact(778899001122L, unionSegmt); + Assert.assertEquals(result, 1186273074861L); + } + } + + @Test + public void test_addLongAndLongsFromUnionWithNestedLongArray() throws Throwable { + SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); + UnionLayout unionLayout = MemoryLayout.unionLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionWithNestedLongArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 111111111L); + unionSegmt.set(JAVA_LONG, 8, 222222222L); + + long result = (long)mh.invokeExact(444444444L, unionSegmt); + Assert.assertEquals(result, 888888888L); + } + } + + @Test + public void test_addLongAndLongsFromUnionWithNestedLongArray_reverseOrder() throws Throwable { + SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionWithNestedLongArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 111111111L); + unionSegmt.set(JAVA_LONG, 8, 222222222L); + + long result = (long)mh.invokeExact(444444444L, unionSegmt); + Assert.assertEquals(result, 888888888L); + } + } + + @Test + public void test_addLongAndLongsFromUnionWithNestedUnionArray() throws Throwable { + UnionLayout longUnion = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, longUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_LONG.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionWithNestedUnionArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 111111111L); + unionSegmt.set(JAVA_LONG, 8, 222222222L); + + long result = (long)mh.invokeExact(666666666L, unionSegmt); + Assert.assertEquals(result, 1444444443L); + } + } + + @Test + public void test_addLongAndLongsFromUnionWithNestedUnionArray_reverseOrder() throws Throwable { + UnionLayout longUnion = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, longUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionWithNestedUnionArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 111111111L); + unionSegmt.set(JAVA_LONG, 8, 222222222L); + + long result = (long)mh.invokeExact(666666666L, unionSegmt); + Assert.assertEquals(result, 1444444443L); + } + } + + @Test + public void test_add2LongUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2LongUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt1, 0L, 987654321987L); + longHandle2.set(unionSegmt1, 0L, 123456789123L); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt2, 0L, 224466880022L); + longHandle2.set(unionSegmt2, 0L, 113355779911L); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(longHandle1.get(resultSegmt, 0L), 236812569034L); + Assert.assertEquals(longHandle2.get(resultSegmt, 0L), 236812569034L); + } + } + + @Test + public void test_add2LongUnions_returnUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2LongUnions_returnUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt1, 0L, 1122334455L); + longHandle2.set(unionSegmt1, 0L, 5566778899L); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt2, 0L, 9900112233L); + longHandle2.set(unionSegmt2, 0L, 3344556677L); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals(longHandle1.get(resultSegmt, 0L), 12255892253L); + Assert.assertEquals(longHandle2.get(resultSegmt, 0L), 12255892253L); + } + } + + @Test + public void test_add3LongUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle longHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add3LongUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt1, 0L, 987654321987L); + longHandle2.set(unionSegmt1, 0L, 123456789123L); + longHandle3.set(unionSegmt1, 0L, 112233445566L); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt2, 0L, 224466880022L); + longHandle2.set(unionSegmt2, 0L, 113355779911L); + longHandle3.set(unionSegmt2, 0L, 778899001122L); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals(longHandle1.get(resultSegmt, 0L), 891132446688L); + Assert.assertEquals(longHandle2.get(resultSegmt, 0L), 891132446688L); + Assert.assertEquals(longHandle3.get(resultSegmt, 0L), 891132446688L); + } + } + + @Test + public void test_addFloatAndFloatsFromUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt, 0L, 8.12F); + floatHandle2.set(unionSegmt, 0L, 9.24F); + + float result = (float)mh.invokeExact(6.56F, unionSegmt); + Assert.assertEquals(result, 25.04F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt, 0L, 35.11F); + floatHandle2.set(unionSegmt, 0L, 46.22F); + + float result = (float)mh.invoke(79.32F, unionSegmt); + Assert.assertEquals(result, 171.76F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromNestedUnion() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_FLOAT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 31.22F); + + float result = (float)mh.invokeExact(37.88F, unionSegmt); + Assert.assertEquals(result, 131.54F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromNestedUnion_reverseOrder() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedUnion_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 31.22F); + + float result = (float)mh.invokeExact(37.88F, unionSegmt); + Assert.assertEquals(result, 131.54F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionWithNestedFloatArray() throws Throwable { + SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); + UnionLayout unionLayout = MemoryLayout.unionLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionWithNestedFloatArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 111.11F); + unionSegmt.set(JAVA_FLOAT, 4, 222.22F); + + float result = (float)mh.invokeExact(444.44F, unionSegmt); + Assert.assertEquals(result, 888.88F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionWithNestedFloatArray_reverseOrder() throws Throwable { + SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionWithNestedFloatArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 111.11F); + unionSegmt.set(JAVA_FLOAT, 4, 222.22F); + + float result = (float)mh.invokeExact(444.44F, unionSegmt); + Assert.assertEquals(result, 888.88F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionWithNestedUnionArray() throws Throwable { + UnionLayout floatUnion = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, floatUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_FLOAT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionWithNestedUnionArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 444.44F); + unionSegmt.set(JAVA_FLOAT, 4, 555.55F); + + float result = (float)mh.invokeExact(666.66F, unionSegmt); + Assert.assertEquals(result, 3111.08F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionWithNestedUnionArray_reverseOrder() throws Throwable { + UnionLayout floatUnion = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, floatUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionWithNestedUnionArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 444.44F); + unionSegmt.set(JAVA_FLOAT, 4, 555.55F); + + float result = (float)mh.invokeExact(666.66F, unionSegmt); + Assert.assertEquals(result, 3111.08F, 0.01F); + } + } + + @Test + public void test_add2FloatUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2FloatUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt1, 0L, 25.12F); + floatHandle2.set(unionSegmt1, 0L, 11.23F); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt2, 0L, 24.34F); + floatHandle2.set(unionSegmt2, 0L, 13.45F); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals((float)floatHandle1.get(resultSegmt, 0L), 24.68F, 0.01F); + Assert.assertEquals((float)floatHandle2.get(resultSegmt, 0L), 24.68F, 0.01F); + } + } + + @Test + public void test_add2FloatUnions_returnUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2FloatUnions_returnUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt1, 0L, 25.12F); + floatHandle2.set(unionSegmt1, 0L, 11.23F); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt2, 0L, 24.34F); + floatHandle2.set(unionSegmt2, 0L, 13.47F); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals((float)floatHandle1.get(resultSegmt, 0L), 38.17F, 0.01F); + Assert.assertEquals((float)floatHandle2.get(resultSegmt, 0L), 38.17F, 0.01F); + } + } + + @Test + public void test_add3FloatUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle floatHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add3FloatUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt1, 0L, 25.12F); + floatHandle2.set(unionSegmt1, 0L, 11.23F); + floatHandle3.set(unionSegmt1, 0L, 45.67F); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt2, 0L, 24.34F); + floatHandle2.set(unionSegmt2, 0L, 13.45F); + floatHandle3.set(unionSegmt2, 0L, 69.72F); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals((float)floatHandle1.get(resultSegmt, 0L), 115.39F, 0.01F); + Assert.assertEquals((float)floatHandle2.get(resultSegmt, 0L), 115.39F, 0.01F); + Assert.assertEquals((float)floatHandle3.get(resultSegmt, 0L), 115.39F, 0.01F); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt, 0L, 2228.111D); + doubleHandle2.set(unionSegmt, 0L, 2229.221D); + + double result = (double)mh.invokeExact(3336.333D, unionSegmt); + Assert.assertEquals(result, 7794.775D, 0.001D); + } + } + + + @Test + public void test_addDoubleAndDoublesFromUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt, 0L, 2228.111D); + doubleHandle2.set(unionSegmt, 0L, 2229.221D); + + double result = (double)mh.invokeExact(3336.333D, unionSegmt); + Assert.assertEquals(result, 7794.775D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromNestedUnion() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_DOUBLE.withName("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 31.789D); + + double result = (double)mh.invokeExact(37.864D, unionSegmt); + Assert.assertEquals(result, 133.231D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromNestedUnion_reverseOrder() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedUnion_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 31.789D); + + double result = (double)mh.invokeExact(37.864D, unionSegmt); + Assert.assertEquals(result, 133.231D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionWithNestedDoubleArray() throws Throwable { + SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); + UnionLayout unionLayout = MemoryLayout.unionLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionWithNestedDoubleArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 111.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 222.222D); + + double result = (double)mh.invokeExact(444.444D, unionSegmt); + Assert.assertEquals(result, 888.888D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionWithNestedDoubleArray_reverseOrder() throws Throwable { + SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionWithNestedDoubleArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 111.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 222.222D); + + double result = (double)mh.invokeExact(444.444D, unionSegmt); + Assert.assertEquals(result, 888.888D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionWithNestedUnionArray() throws Throwable { + UnionLayout doubleUnion = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, doubleUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_DOUBLE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionWithNestedUnionArray").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 111.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 222.222D); + + double result = (double)mh.invokeExact(666.665D, unionSegmt); + Assert.assertEquals(result, 1444.442D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionWithNestedUnionArray_reverseOrder() throws Throwable { + UnionLayout doubleUnion = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, doubleUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionWithNestedUnionArray_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 111.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 222.222D); + + double result = (double)mh.invokeExact(666.665D, unionSegmt); + Assert.assertEquals(result, 1444.442D, 0.001D); + } + } + + @Test + public void test_add2DoubleUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt1, 0L, 11.222D); + doubleHandle2.set(unionSegmt1, 0L, 22.333D); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt2, 0L, 33.444D); + doubleHandle2.set(unionSegmt2, 0L, 44.555D); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals((double)doubleHandle1.get(resultSegmt, 0L), 66.888D, 0.001D); + Assert.assertEquals((double)doubleHandle2.get(resultSegmt, 0L), 66.888D, 0.001D); + } + } + + @Test + public void test_add2DoubleUnions_returnUnionPtr() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleUnions_returnUnionPtr").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt1, 0L, 11.222D); + doubleHandle2.set(unionSegmt1, 0L, 22.333D); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt2, 0L, 33.444D); + doubleHandle2.set(unionSegmt2, 0L, 44.555D); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals((double)doubleHandle1.get(resultSegmt, 0L), 111.443D, 0.001D); + Assert.assertEquals((double)doubleHandle2.get(resultSegmt, 0L), 111.443D, 0.001D); + } + } + + @Test + public void test_add3DoubleUnions_returnUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle doubleHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("add3DoubleUnions_returnUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt1, 0L, 11.222D); + doubleHandle2.set(unionSegmt1, 0L, 22.333D); + doubleHandle3.set(unionSegmt1, 0L, 33.123D); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt2, 0L, 33.444D); + doubleHandle2.set(unionSegmt2, 0L, 44.555D); + doubleHandle3.set(unionSegmt2, 0L, 55.456D); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2); + Assert.assertEquals((double)doubleHandle1.get(resultSegmt, 0L), 88.579D, 0.001D); + Assert.assertEquals((double)doubleHandle2.get(resultSegmt, 0L), 88.579D, 0.001D); + Assert.assertEquals((double)doubleHandle3.get(resultSegmt, 0L), 88.579D, 0.001D); + } + } + + @Test + public void test_addShortAndShortFromUnionWithByteShort() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromUnionWithByteShort").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (short)10001); + Assert.assertEquals((short)elemHandle2.get(unionSegmt, 0L), 10001); + + short result = (short)mh.invokeExact((short)10020, unionSegmt); + Assert.assertEquals(result, 20021); + } + } + + @Test + public void test_addShortAndShortFromUnionWithShortByte_reverseOrder() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromUnionWithShortByte_reverseOrder").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, (short)10001); + Assert.assertEquals((short)elemHandle1.get(unionSegmt, 0L), 10001); + + short result = (short)mh.invokeExact((short)10020, unionSegmt); + Assert.assertEquals(result, 20021); + } + } + + @Test + public void test_addIntAndByteFromUnionWithIntByte() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndByteFromUnionWithIntByte").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (byte)110); + Assert.assertEquals((byte)elemHandle2.get(unionSegmt, 0L), 110); + + int result = (int)mh.invokeExact(22334455, unionSegmt); + Assert.assertEquals(result, 22334565); + } + } + + @Test + public void test_addIntAndIntFromUnionWithByteInt() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromUnionWithByteInt").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 1122334); + Assert.assertEquals((int)elemHandle2.get(unionSegmt, 0L), 1122334); + + int result = (int)mh.invokeExact(11335577, unionSegmt); + Assert.assertEquals(result, 12457911); + } + } + + @Test + public void test_addIntAndShortFromUnionWithIntShort() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndShortFromUnionWithIntShort").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (short)32766); + Assert.assertEquals((short)elemHandle2.get(unionSegmt, 0L), 32766); + + int result = (int)mh.invokeExact(22334455, unionSegmt); + Assert.assertEquals(result, 22367221); + } + } + + @Test + public void test_addIntAndIntFromUnionWithShortInt() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromUnionWithShortInt").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 1122334); + Assert.assertEquals((int)elemHandle2.get(unionSegmt, 0L), 1122334); + + int result = (int)mh.invokeExact(11335577, unionSegmt); + Assert.assertEquals(result, 12457911); + } + } + + @Test + public void test_addIntAndByteFromUnionWithIntShortByte() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_SHORT.withName("elem2"), JAVA_BYTE.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndByteFromUnionWithIntShortByte").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, (byte)22); + Assert.assertEquals((byte)elemHandle3.get(unionSegmt, 0L), 22); + + int result = (int)mh.invokeExact(22334455, unionSegmt); + Assert.assertEquals(result, 22334477); + } + } + + @Test + public void test_addIntAndShortFromUnionWithByteShortInt() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_INT.withName("elem3")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndShortFromUnionWithByteShortInt").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (short)32766); + Assert.assertEquals((short)elemHandle2.get(unionSegmt, 0L), 32766); + + int result = (int)mh.invokeExact(11335577, unionSegmt); + Assert.assertEquals(result, 11368343); + } + } + + @Test + public void test_addIntAndLongFromUnionWithIntLong() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndLongFromUnionWithIntLong").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 667788990011L); + Assert.assertEquals((long)elemHandle2.get(unionSegmt, 0L), 667788990011L); + + long result = (long)mh.invokeExact(22446688, unionSegmt); + Assert.assertEquals(result, 667811436699L); + } + } + + @Test + public void test_addIntAndLongFromUnionWithLongInt() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndLongFromUnionWithLongInt").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 667788990011L); + Assert.assertEquals((long)elemHandle1.get(unionSegmt, 0L), 667788990011L); + + long result = (long)mh.invokeExact(1234567, unionSegmt); + Assert.assertEquals(result, 667790224578L); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithShortFloat() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithShortFloat").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 0.11F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 0.11F); + + float result = (float)mh.invokeExact(0.22F, unionSegmt); + Assert.assertEquals(result, 0.33F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithFloatShort() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithFloatShort").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 0.11F); + Assert.assertEquals((float)elemHandle1.get(unionSegmt, 0L), 0.11F, 0.01F); + + float result = (float)mh.invokeExact(0.22F, unionSegmt); + Assert.assertEquals(result, 0.33F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithIntFloat() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithIntFloat").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 0.11F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 0.11F, 0.01F); + + float result = (float)mh.invokeExact(0.22F, unionSegmt); + Assert.assertEquals(result, 0.33F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithFloatInt() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithFloatInt").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 0.11F); + Assert.assertEquals((float)elemHandle1.get(unionSegmt, 0L), 0.11F, 0.01F); + + float result = (float)mh.invokeExact(0.22F, unionSegmt); + Assert.assertEquals(result, 0.33F, 0.01F); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithFloatDouble() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithFloatDouble").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 18.444F); + Assert.assertEquals((float)elemHandle1.get(unionSegmt, 0L), 18.444F, 001F); + + double result = (double)mh.invokeExact(113.567D, unionSegmt); + Assert.assertEquals(result, 132.011D, 0.001D); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithDoubleFloat() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithDoubleFloat").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 19.22F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 19.22F, 0.01D); + + double result = (double)mh.invokeExact(216.666D, unionSegmt); + Assert.assertEquals(result, 235.886D, 0.001D); + } + } + + @Test + public void test_addDoubleAndIntFromUnionWithIntDouble() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndIntFromUnionWithIntDouble").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 1122334455); + Assert.assertEquals((int)elemHandle1.get(unionSegmt, 0L), 1122334455); + + double result = (double)mh.invokeExact(113.567D, unionSegmt); + Assert.assertEquals(result, 1122334568.567D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoubleFromUnionWithDoubleInt() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromUnionWithDoubleInt").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 218.555D); + Assert.assertEquals((double)elemHandle1.get(unionSegmt, 0L), 218.555D, 0.001D); + + double result = (double)mh.invokeExact(216.666D, unionSegmt); + Assert.assertEquals(result, 435.221D, 0.001D); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithFloatLong() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithFloatLong").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 18.4F); + Assert.assertEquals((float)elemHandle1.get(unionSegmt, 0L), 18.4F, 0.1F); + + float result = (float)mh.invokeExact(25.3F, unionSegmt); + Assert.assertEquals(result, 43.7F, 0.1F); + } + } + + @Test + public void test_addLongAndLongFromUnionWithLongFloat() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromUnionWithLongFloat").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 100000000005L); + Assert.assertEquals((long)elemHandle1.get(unionSegmt, 0L), 100000000005L); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt); + Assert.assertEquals(result, 101020302015L); + } + } + + @Test + public void test_addDoubleAndDoubleFromUnionWithLongDouble() throws Throwable { + UnionLayout unionLayout =MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromUnionWithLongDouble").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 222.333D); + Assert.assertEquals((double)elemHandle2.get(unionSegmt, 0L), 222.333D, 0.001D); + + double result = (double)mh.invokeExact(111.222D, unionSegmt); + Assert.assertEquals(result, 333.555D, 0.001D); + } + } + + @Test + public void test_addDoubleAndLongFromUnionWithDoubleLong() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndLongFromUnionWithDoubleLong").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 11223344556677L); + Assert.assertEquals((long)elemHandle2.get(unionSegmt, 0L), 11223344556677L); + + double result = (double)mh.invokeExact(222.33D, unionSegmt); + Assert.assertEquals(result, 11223344556899.33D, 0.01D); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithIntFloatDouble() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithIntFloatDouble").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 18.444F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 18.444F, 0.001F); + + double result = (double)mh.invokeExact(113.567D, unionSegmt); + Assert.assertEquals(result, 132.011D, 0.001D); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithIntDoubleFloat() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), JAVA_FLOAT.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithIntDoubleFloat").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 18.444F); + Assert.assertEquals((float)elemHandle3.get(unionSegmt, 0L), 18.444F, 0.001F); + + double result = (double)mh.invokeExact(113.567D, unionSegmt); + Assert.assertEquals(result, 132.011D, 0.001D); + } + } + + @Test + public void test_addLongAndLongFromUnionWithIntFloatLong() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_LONG.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromUnionWithIntFloatLong").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 100000000005L); + Assert.assertEquals((long)elemHandle3.get(unionSegmt, 0L), 100000000005L); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt); + Assert.assertEquals(result, 101020302015L); + } + } + + @Test + public void test_addLongAndLongFromUnionWithIntLongFloat() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_LONG.withName("elem2"), JAVA_FLOAT.withName("elem3")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromUnionWithIntLongFloat").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 100000000005L); + Assert.assertEquals((long)elemHandle2.get(unionSegmt, 0L), 100000000005L); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt); + Assert.assertEquals(result, 101020302015L); + } + } + + @Test + public void test_addDoubleAndDoubleFromUnionWithFloatLongDouble() throws Throwable { + UnionLayout unionLayout =MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), + JAVA_LONG.withName("elem2"), JAVA_DOUBLE.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromUnionWithFloatLongDouble").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 100.11D); + Assert.assertEquals((double)elemHandle3.get(unionSegmt, 0L), 100.11D, 01D); + + double result = (double)mh.invokeExact(222.44D, unionSegmt); + Assert.assertEquals(result, 322.55D, 0.01D); + } + } + + @Test + public void test_addDoubleAndLongFromUnionWithIntDoubleLong() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), JAVA_LONG.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndLongFromUnionWithIntDoubleLong").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 100000000001L); + Assert.assertEquals((long)elemHandle3.get(unionSegmt, 0L), 100000000001L); + + double result = (double)mh.invokeExact(222.44D, unionSegmt); + Assert.assertEquals(result, 100000000223.44D, 0.01D); + } + } + + @Test + public void test_addDoubleAndPtrValueFromUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), ADDRESS.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndPtrValueFromUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + MemorySegment longSegmt = arena.allocateFrom(JAVA_LONG, 112233445566778899L); + elemHandle3.set(unionSegmt, 0L, longSegmt); + MemorySegment elemSegmt = ((MemorySegment)elemHandle3.get(unionSegmt, 0L)).reinterpret(JAVA_LONG.byteSize()); + Assert.assertEquals(elemSegmt.get(JAVA_LONG, 0), 112233445566778899L); + + double result = (double)mh.invokeExact(222.44D, unionSegmt); + Assert.assertEquals(result, 112233445566779121.44D, 0.01D); + } + } + + @Test + public void test_addLongAndPtrValueFromUnion() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), ADDRESS.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndPtrValueFromUnion").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + MemorySegment longSegmt = arena.allocateFrom(JAVA_LONG, 112233445566778899L); + elemHandle3.set(unionSegmt, 0L, longSegmt); + MemorySegment elemSegmt = ((MemorySegment)elemHandle3.get(unionSegmt, 0L)).reinterpret(JAVA_LONG.byteSize()); + Assert.assertEquals(elemSegmt.get(JAVA_LONG, 0), 112233445566778899L); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt); + Assert.assertEquals(result, 112233446587080909L); + } + } + + @Test + public void test_addLongAndIntFromUnionWithByteShortIntLong() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_INT.withName("elem3"), JAVA_LONG.withName("elem4")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndIntFromUnionWithByteShortIntLong").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 11223344); + Assert.assertEquals((int)elemHandle3.get(unionSegmt, 0L), 11223344); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt); + Assert.assertEquals(result, 1031525354L); + } + } + + @Test + public void test_addLongAndIntFromUnionWithLongIntShortByte() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), + JAVA_INT.withName("elem2"), JAVA_SHORT.withName("elem3"), JAVA_BYTE.withName("elem4")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndIntFromUnionWithLongIntShortByte").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 11223344); + Assert.assertEquals((int)elemHandle2.get(unionSegmt, 0L), 11223344); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt); + Assert.assertEquals(result, 1031525354L); + } + } + + @Test + public void test_addDoubleAndShortFromUnionWithByteShortFloatDouble() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_DOUBLE.withName("elem4")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndShortFromUnionWithByteShortFloatDouble").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (short)32766); + Assert.assertEquals((short)elemHandle2.get(unionSegmt, 0L), 32766); + + double result = (double)mh.invokeExact(33333.57D, unionSegmt); + Assert.assertEquals(result, 66099.57D, 0.01D); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithDoubleFloatShortByte() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_SHORT.withName("elem3"), JAVA_BYTE.withName("elem4")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithDoubleFloatShortByte").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 1.001F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 1.001F, 0.001F); + + double result = (double)mh.invokeExact(33333.567D, unionSegmt); + Assert.assertEquals(result, 33334.568D, 0.001D); + } + } +} diff --git a/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMHWithUnionStructTests.java b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMHWithUnionStructTests.java new file mode 100644 index 00000000000..8490fdddf5b --- /dev/null +++ b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMHWithUnionStructTests.java @@ -0,0 +1,1030 @@ +/******************************************************************************* + * Copyright IBM Corp. and others 2023 + * + * This program and the accompanying materials are made available under + * the terms of the Eclipse Public License 2.0 which accompanies this + * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ + * or the Apache License, Version 2.0 which accompanies this distribution and + * is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * This Source Code may also be made available under the following + * Secondary Licenses when the conditions for such availability set + * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU + * General Public License, version 2 with the GNU Classpath + * Exception [1] and GNU General Public License, version 2 with the + * OpenJDK Assembly Exception [2]. + * + * [1] https://www.gnu.org/software/classpath/license.html + * [2] https://openjdk.org/legal/assembly-exception.html + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 + *******************************************************************************/ +package org.openj9.test.jep454.upcall; + +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.AssertJUnit; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.VarHandle; + +import java.lang.foreign.Arena; +import java.lang.foreign.Linker; +import java.lang.foreign.FunctionDescriptor; +import java.lang.foreign.GroupLayout; +import java.lang.foreign.MemoryLayout; +import java.lang.foreign.MemoryLayout.PathElement; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.SegmentAllocator; +import java.lang.foreign.SequenceLayout; +import java.lang.foreign.SymbolLookup; +import java.lang.foreign.UnionLayout; +import java.lang.foreign.ValueLayout; +import static java.lang.foreign.ValueLayout.*; + +/** + * Test cases for JEP 454: Foreign Linker API for argument/return struct/union + * nested with struct/union in upcall. + */ +@Test(groups = { "level.sanity" }) +public class UpcallMHWithUnionStructTests { + private static Linker linker = Linker.nativeLinker(); + + static { + System.loadLibrary("clinkerffitests"); + } + private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); + + @Test + public void test_addBoolAndBoolsFromStructWithXor_Nested2BoolUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromStructWithXor_Nested2BoolUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromStructWithXor_Nested2BoolUnion, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, structLayout), arena); + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_BOOLEAN, 0, true); + structSegmt.set(JAVA_BOOLEAN, 1, false); + + boolean result = (boolean)mh.invokeExact(false, structSegmt, upcallFuncAddr); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithXor_Nested2BoolStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithXor_Nested2BoolStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromUnionWithXor_Nested2BoolStruct, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, false); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invokeExact(true, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, false); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithXor_Nested4BoolStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), + JAVA_BOOLEAN.withName("elem2"), JAVA_BOOLEAN.withName("elem3"), JAVA_BOOLEAN.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithXor_Nested4BoolStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromUnionWithXor_Nested4BoolStruct, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, false); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + unionSegmt.set(JAVA_BOOLEAN, 2, true); + unionSegmt.set(JAVA_BOOLEAN, 3, true); + + boolean result = (boolean)mh.invokeExact(true, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, false); + } + } + + @Test + public void test_add2BoolStructsWithXor_returnStruct_Nested2BoolUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2BoolStructsWithXor_returnStruct_Nested2BoolUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolStructsWithXor_returnStruct_Nested2BoolUnion, + FunctionDescriptor.of(structLayout, structLayout, structLayout), arena); + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_BOOLEAN, 0, true); + structSegmt1.set(JAVA_BOOLEAN, 1, false); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_BOOLEAN, 0, true); + structSegmt2.set(JAVA_BOOLEAN, 1, true); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), false); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 1), true); + } + } + + @Test + public void test_add2BoolUnionsWithXor_returnUnion_Nested2BoolStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2BoolUnionsWithXor_returnUnion_Nested2BoolStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolUnionsWithXor_returnUnion_Nested2BoolStruct, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_BOOLEAN, 0, false); + unionSegmt1.set(JAVA_BOOLEAN, 1, true); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_BOOLEAN, 0, true); + unionSegmt2.set(JAVA_BOOLEAN, 1, false); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), true); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 1), true); + } + } + + @Test + public void test_addByteAndBytesFromStruct_Nested2ByteUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromStruct_Nested2ByteUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromStruct_Nested2ByteUnion, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, structLayout), arena); + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_BYTE, 0, (byte)8); + structSegmt.set(JAVA_BYTE, 1, (byte)9); + + byte result = (byte)mh.invokeExact((byte)6, structSegmt, upcallFuncAddr); + Assert.assertEquals(result, 32); + } + } + + @Test + public void test_addByteAndBytesFromUnion_Nested2ByteStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnion_Nested2ByteStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromUnion_Nested2ByteStruct, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)8); + unionSegmt.set(JAVA_BYTE, 1, (byte)9); + + byte result = (byte)mh.invokeExact((byte)6, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 31); + } + } + + @Test + public void test_addByteAndBytesFromUnion_Nested4ByteStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), + JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnion_Nested4ByteStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromUnion_Nested4ByteStruct, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)8); + unionSegmt.set(JAVA_BYTE, 1, (byte)9); + unionSegmt.set(JAVA_BYTE, 2, (byte)10); + unionSegmt.set(JAVA_BYTE, 3, (byte)11); + + byte result = (byte)mh.invokeExact((byte)6, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 52); + } + } + + @Test + public void test_add2ByteStructs_returnStruct_Nested2ByteUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2ByteStructs_returnStruct_Nested2ByteUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ByteStructs_returnStruct_Nested2ByteUnion, + FunctionDescriptor.of(structLayout, structLayout, structLayout), arena); + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_BYTE, 0, (byte)25); + structSegmt1.set(JAVA_BYTE, 1, (byte)11); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_BYTE, 0, (byte)24); + structSegmt2.set(JAVA_BYTE, 1, (byte)13); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), (byte)49); + Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 1), (byte)24); + } + } + + @Test + public void test_add2ByteUnions_returnUnion_Nested2ByteStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2ByteUnions_returnUnion_Nested2ByteStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ByteUnions_returnUnion_Nested2ByteStruct, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_BYTE, 0, (byte)25); + unionSegmt1.set(JAVA_BYTE, 1, (byte)11); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_BYTE, 0, (byte)24); + unionSegmt2.set(JAVA_BYTE, 1, (byte)13); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 0), (byte)49); + Assert.assertEquals(resultSegmt.get(JAVA_BYTE, 1), (byte)24); + } + } + + @Test + public void test_addCharAndCharsFromStruct_Nested2CharUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromStruct_Nested2CharUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromStruct_Nested2CharUnion, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, structLayout), arena); + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_CHAR, 0, 'A'); + structSegmt.set(JAVA_CHAR, 2, 'B'); + + char result = (char)mh.invokeExact('C', structSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'E'); + } + } + + @Test + public void test_addCharAndCharsFromUnion_Nested2CharStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnion_Nested2CharStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromUnion_Nested2CharStruct, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'A'); + unionSegmt.set(JAVA_CHAR, 2, 'B'); + + char result = (char)mh.invokeExact('C', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'D'); + } + } + + @Test + public void test_addCharAndCharsFromUnion_Nested4CharStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), + JAVA_CHAR.withName("elem2"), JAVA_CHAR.withName("elem3"), JAVA_CHAR.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnion_Nested4CharStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromUnion_Nested4CharStruct, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'A'); + unionSegmt.set(JAVA_CHAR, 2, 'B'); + unionSegmt.set(JAVA_CHAR, 4, 'C'); + unionSegmt.set(JAVA_CHAR, 6, 'D'); + + char result = (char)mh.invokeExact('E', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'K'); + } + } + + @Test + public void test_add2CharStructs_returnStruct_Nested2CharUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2CharStructs_returnStruct_Nested2CharUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2CharStructs_returnStruct_Nested2CharUnion, + FunctionDescriptor.of(structLayout, structLayout, structLayout), arena); + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_CHAR, 0, 'A'); + structSegmt1.set(JAVA_CHAR, 2, 'B'); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_CHAR, 0, 'C'); + structSegmt2.set(JAVA_CHAR, 2, 'D'); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'C'); + Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 2), 'E'); + } + } + + @Test + public void test_add2CharUnions_returnUnion_Nested2CharStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2CharUnions_returnUnion_Nested2CharStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2CharUnions_returnUnion_Nested2CharStruct, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_CHAR, 0, 'A'); + unionSegmt1.set(JAVA_CHAR, 2, 'B'); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_CHAR, 0, 'C'); + unionSegmt2.set(JAVA_CHAR, 2, 'D'); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 0), 'C'); + Assert.assertEquals(resultSegmt.get(JAVA_CHAR, 2), 'E'); + } + } + + public void test_addShortAndShortsFromStruct_Nested2ShortUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromStruct_Nested2ShortUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromStruct_Nested2ShortUnion, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, structLayout), arena); + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_SHORT, 0, (short)8); + structSegmt.set(JAVA_SHORT, 2, (short)9); + + short result = (short)mh.invokeExact((short)6, structSegmt, upcallFuncAddr); + Assert.assertEquals(result, 32); + } + } + + @Test + public void test_addShortAndShortsFromUnion_Nested2ShortStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnion_Nested2ShortStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromUnion_Nested2ShortStruct, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)8); + unionSegmt.set(JAVA_SHORT, 2, (short)9); + + short result = (short)mh.invokeExact((short)6, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 31); + } + } + + @Test + public void test_addShortAndShortsFromUnion_Nested4ShortStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3"), JAVA_SHORT.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnion_Nested4ShortStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromUnion_Nested4ShortStruct, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)1111); + unionSegmt.set(JAVA_SHORT, 2, (short)2222); + unionSegmt.set(JAVA_SHORT, 4, (short)3333); + unionSegmt.set(JAVA_SHORT, 6, (short)4444); + + short result = (short)mh.invokeExact((short)5555, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 17776); + } + } + + @Test + public void test_add2ShortStructs_returnStruct_Nested2ShortUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2ShortStructs_returnStruct_Nested2ShortUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ShortStructs_returnStruct_Nested2ShortUnion, + FunctionDescriptor.of(structLayout, structLayout, structLayout), arena); + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_SHORT, 0, (short)55); + structSegmt1.set(JAVA_SHORT, 2, (short)45); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_SHORT, 0, (short)78); + structSegmt2.set(JAVA_SHORT, 2, (short)67); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 133); + Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 2), 112); + } + } + + @Test + public void test_add2ShortUnions_returnUnion_Nested2ShortStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2ShortUnions_returnUnion_Nested2ShortStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ShortUnions_returnUnion_Nested2ShortStruct, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_SHORT, 0, (short)55); + unionSegmt1.set(JAVA_SHORT, 2, (short)45); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_SHORT, 0, (short)78); + unionSegmt2.set(JAVA_SHORT, 2, (short)67); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 0), 133); + Assert.assertEquals(resultSegmt.get(JAVA_SHORT, 2), 112); + } + } + + @Test + public void test_addShortAndShortByteFromUnion_Nested2ByteStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortByteFromUnion_Nested2ByteStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortByteFromUnion_Nested2ByteStruct, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)12345); + + short result = (short)mh.invokeExact((short)11111, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 23561); + } + } + + @Test + public void test_addShortAndBytesFromUnion_Nested4ByteStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2"), + JAVA_BYTE.withName("elem3"), JAVA_BYTE.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndBytesFromUnion_Nested4ByteStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndBytesFromUnion_Nested4ByteStruct, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)11); + unionSegmt.set(JAVA_BYTE, 1, (byte)22); + unionSegmt.set(JAVA_BYTE, 2, (byte)33); + unionSegmt.set(JAVA_BYTE, 3, (byte)44); + + short result = (short)mh.invokeExact((short)11111, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 11221); + } + } + + @Test + public void test_addIntAndIntsFromStruct_Nested2IntUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromStruct_Nested2IntUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromStruct_Nested2IntUnion, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, structLayout), arena); + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_INT, 0, 1122334); + structSegmt.set(JAVA_INT, 4, 1234567); + + int result = (int)mh.invokeExact(2244668, structSegmt, upcallFuncAddr); + Assert.assertEquals(result, 5836136); + } + } + + @Test + public void test_addIntAndIntsFromUnion_Nested2IntStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnion_Nested2IntStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromUnion_Nested2IntStruct, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 112233445); + unionSegmt.set(JAVA_INT, 4, 1234567890); + + int result = (int)mh.invokeExact(2244668, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1461279448); + } + } + + @Test + public void test_addIntAndIntsFromUnion_Nested4IntStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), + JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3"), JAVA_INT.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnion_Nested4IntStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromUnion_Nested4IntStruct, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 11223344); + unionSegmt.set(JAVA_INT, 4, 55667788); + unionSegmt.set(JAVA_INT, 8, 99001122); + unionSegmt.set(JAVA_INT, 12, 33445566); + + int result = (int)mh.invokeExact(22446688, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 233007852); + } + } + + @Test + public void test_add2IntStructs_returnStruct_Nested2IntUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2IntStructs_returnStruct_Nested2IntUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntStructs_returnStruct_Nested2IntUnion, + FunctionDescriptor.of(structLayout, structLayout, structLayout), arena); + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_INT, 0, 11223344); + structSegmt1.set(JAVA_INT, 4, 55667788); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_INT, 0, 99001122); + structSegmt2.set(JAVA_INT, 4, 33445566); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 110224466); + Assert.assertEquals(resultSegmt.get(JAVA_INT, 4), 89113354); + } + } + + @Test + public void test_add2IntUnions_returnUnion_Nested2IntStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2IntUnions_returnUnion_Nested2IntStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntUnions_returnUnion_Nested2IntStruct, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_INT, 0, 11223344); + unionSegmt1.set(JAVA_INT, 4, 55667788); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_INT, 0, 99001122); + unionSegmt2.set(JAVA_INT, 4, 33445566); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_INT, 0), 110224466); + Assert.assertEquals(resultSegmt.get(JAVA_INT, 4), 89113354); + } + } + + @Test + public void test_addIntAndIntShortFromUnion_Nested2ShortStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntShortFromUnion_Nested2ShortStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntShortFromUnion_Nested2ShortStruct, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 1234567890); + + int result = (int)mh.invokeExact(22446688, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1257034138); + } + } + + @Test + public void test_addIntAndShortsFromUnion_Nested4ShortStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2"), + JAVA_SHORT.withName("elem3"), JAVA_SHORT.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndShortsFromUnion_Nested4ShortStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndShortsFromUnion_Nested4ShortStruct, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)11111); + unionSegmt.set(JAVA_SHORT, 2, (short)11112); + unionSegmt.set(JAVA_SHORT, 4, (short)11113); + unionSegmt.set(JAVA_SHORT, 6, (short)11114); + + int result = (int)mh.invokeExact(22446688, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 22491138); + } + } + + @Test + public void test_addLongAndLongsFromStruct_Nested2LongUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromStruct_Nested2LongUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromStruct_Nested2LongUnion, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, structLayout), arena); + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_LONG, 0, 1234567890L); + structSegmt.set(JAVA_LONG, 8, 9876543210L); + + long result = (long)mh.invokeExact(2468024680L, structSegmt, upcallFuncAddr); + Assert.assertEquals(result, 23455678990L); + } + } + + @Test + public void test_addLongAndLongsFromUnion_Nested2LongStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnion_Nested2LongStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromUnion_Nested2LongStruct, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 1111111L); + unionSegmt.set(JAVA_LONG, 8, 2222222L); + + long result = (long)mh.invokeExact(3333333L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 7777777L); + } + } + + @Test + public void test_addLongAndLongsFromUnion_Nested4LongStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), + JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3"), JAVA_LONG.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnion_Nested4LongStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromUnion_Nested4LongStruct, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 1111111L); + unionSegmt.set(JAVA_LONG, 8, 2222222L); + unionSegmt.set(JAVA_LONG, 16, 3333333L); + unionSegmt.set(JAVA_LONG, 24, 4444444L); + + long result = (long)mh.invokeExact(5555555L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 17777776L); + } + } + + @Test + public void test_add2LongStructs_returnStruct_Nested2LongUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2LongStructs_returnStruct_Nested2LongUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2LongStructs_returnStruct_Nested2LongUnion, + FunctionDescriptor.of(structLayout, structLayout, structLayout), arena); + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_LONG, 0, 987654321987L); + structSegmt1.set(JAVA_LONG, 8, 123456789123L); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_LONG, 0, 224466880022L); + structSegmt2.set(JAVA_LONG, 8, 113355779911L); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 1212121202009L); + Assert.assertEquals(resultSegmt.get(JAVA_LONG, 8), 236812569034L); + } + } + + @Test + public void test_add2LongUnions_returnUnion_Nested2LongStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2LongUnions_returnUnion_Nested2LongStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2LongUnions_returnUnion_Nested2LongStruct, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_LONG, 0, 987654321987L); + unionSegmt1.set(JAVA_LONG, 8, 123456789123L); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_LONG, 0, 224466880022L); + unionSegmt2.set(JAVA_LONG, 8, 113355779911L); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_LONG, 0), 1212121202009L); + Assert.assertEquals(resultSegmt.get(JAVA_LONG, 8), 236812569034L); + } + } + + @Test + public void test_addFloatAndFloatsFromStruct_Nested2FloatUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromStruct_Nested2FloatUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromStruct_Nested2FloatUnion, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, structLayout), arena); + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_FLOAT, 0, 8.12F); + structSegmt.set(JAVA_FLOAT, 4, 9.24F); + + float result = (float)mh.invokeExact(6.56F, structSegmt, upcallFuncAddr); + Assert.assertEquals(result, 33.16F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnion_Nested2FloatStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnion_Nested2FloatStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromUnion_Nested2FloatStruct, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 8.12F); + unionSegmt.set(JAVA_FLOAT, 4, 9.24F); + + float result = (float)mh.invokeExact(6.56F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 32.04F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnion_Nested4FloatStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_FLOAT.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnion_Nested4FloatStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromUnion_Nested4FloatStruct, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 8.12F); + unionSegmt.set(JAVA_FLOAT, 4, 9.24F); + unionSegmt.set(JAVA_FLOAT, 8, 10.48F); + unionSegmt.set(JAVA_FLOAT, 12, 11.12F); + + float result = (float)mh.invokeExact(6.56F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 53.64F, 0.01F); + } + } + + @Test + public void test_add2FloatStructs_returnStruct_Nested2FloatUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(structLayout, structLayout, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2FloatStructs_returnStruct_Nested2FloatUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2FloatStructs_returnStruct_Nested2FloatUnion, + FunctionDescriptor.of(structLayout, structLayout, structLayout), arena); + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_FLOAT, 0, 25.12F); + structSegmt1.set(JAVA_FLOAT, 4, 11.23F); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_FLOAT, 0, 24.34F); + structSegmt2.set(JAVA_FLOAT, 4, 11.25F); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.46F, 0.01F); + Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 4), 22.48F, 0.01F); + } + } + + @Test + public void test_add2FloatUnions_returnUnion_Nested2FloatStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2FloatUnions_returnUnion_Nested2FloatStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2FloatUnions_returnUnion_Nested2FloatStruct, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_FLOAT, 0, 25.12F); + unionSegmt1.set(JAVA_FLOAT, 4, 11.23F); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_FLOAT, 0, 24.34F); + unionSegmt2.set(JAVA_FLOAT, 4, 13.45F); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 0), 49.46F, 0.01F); + Assert.assertEquals(resultSegmt.get(JAVA_FLOAT, 4), 24.68F, 0.01F); + } + } + + @Test + public void test_addDoubleAndDoublesFromStruct_Nested2DoubleUnionByUpcallMH() throws Throwable { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromStruct_Nested2DoubleUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromStruct_Nested2DoubleUnion, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, structLayout), arena); + MemorySegment structSegmt = arena.allocate(structLayout); + structSegmt.set(JAVA_DOUBLE, 0, 2228.111D); + structSegmt.set(JAVA_DOUBLE, 8, 2229.221D); + + double result = (double)mh.invokeExact(3336.333D, structSegmt, upcallFuncAddr); + Assert.assertEquals(result, 10022.886D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnion_Nested2DoubleStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnion_Nested2DoubleStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromUnion_Nested2DoubleStruct, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 2228.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 2229.221D); + + double result = (double)mh.invokeExact(3336.333D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 10021.776D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnion_Nested4DoubleStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3"), JAVA_DOUBLE.withName("elem4")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnion_Nested4DoubleStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromUnion_Nested4DoubleStruct, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 2228.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 2229.221D); + unionSegmt.set(JAVA_DOUBLE, 16, 2230.221D); + unionSegmt.set(JAVA_DOUBLE, 24, 2231.221D); + + double result = (double)mh.invokeExact(3336.333D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 14483.218D, 0.001D); + } + } + + @Test + public void test_add2DoubleStructs_returnStruct_Nested2DoubleUnionByUpcallMH() 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, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleStructs_returnStruct_Nested2DoubleUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2DoubleStructs_returnStruct_Nested2DoubleUnion, + FunctionDescriptor.of(structLayout, structLayout, structLayout), arena); + MemorySegment structSegmt1 = arena.allocate(structLayout); + structSegmt1.set(JAVA_DOUBLE, 0, 11.222D); + structSegmt1.set(JAVA_DOUBLE, 8, 22.333D); + MemorySegment structSegmt2 = arena.allocate(structLayout); + structSegmt2.set(JAVA_DOUBLE, 0, 33.444D); + structSegmt2.set(JAVA_DOUBLE, 8, 44.555D); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, structSegmt1, structSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); + Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); + } + } + + @Test + public void test_add2DoubleUnions_returnUnion_Nested2DoubleStructByUpcallMH() throws Throwable { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleUnions_returnUnion_Nested2DoubleStructByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2DoubleUnions_returnUnion_Nested2DoubleStruct, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + unionSegmt1.set(JAVA_DOUBLE, 0, 11.222D); + unionSegmt1.set(JAVA_DOUBLE, 8, 22.333D); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + unionSegmt2.set(JAVA_DOUBLE, 0, 33.444D); + unionSegmt2.set(JAVA_DOUBLE, 8, 44.555D); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 0), 44.666D, 0.001D); + Assert.assertEquals(resultSegmt.get(JAVA_DOUBLE, 8), 66.888D, 0.001D); + } + } +} diff --git a/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMHWithUnionTests.java b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMHWithUnionTests.java new file mode 100644 index 00000000000..69c57f1351c --- /dev/null +++ b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMHWithUnionTests.java @@ -0,0 +1,2728 @@ +/******************************************************************************* + * Copyright IBM Corp. and others 2023 + * + * This program and the accompanying materials are made available under + * the terms of the Eclipse Public License 2.0 which accompanies this + * distribution and is available at https://www.eclipse.org/legal/epl-2.0/ + * or the Apache License, Version 2.0 which accompanies this distribution and + * is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * This Source Code may also be made available under the following + * Secondary Licenses when the conditions for such availability set + * forth in the Eclipse Public License, v. 2.0 are satisfied: GNU + * General Public License, version 2 with the GNU Classpath + * Exception [1] and GNU General Public License, version 2 with the + * OpenJDK Assembly Exception [2]. + * + * [1] https://www.gnu.org/software/classpath/license.html + * [2] https://openjdk.org/legal/assembly-exception.html + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 OR GPL-2.0-only WITH OpenJDK-assembly-exception-1.0 + *******************************************************************************/ +package org.openj9.test.jep454.upcall; + +import org.testng.annotations.Test; +import org.testng.Assert; +import org.testng.AssertJUnit; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.VarHandle; + +import java.lang.foreign.Arena; +import java.lang.foreign.Linker; +import java.lang.foreign.FunctionDescriptor; +import java.lang.foreign.MemoryLayout; +import java.lang.foreign.MemoryLayout.PathElement; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.SegmentAllocator; +import java.lang.foreign.SequenceLayout; +import java.lang.foreign.SymbolLookup; +import java.lang.foreign.UnionLayout; +import java.lang.foreign.ValueLayout; +import static java.lang.foreign.ValueLayout.*; + +/** + * Test cases for JEP 454: Foreign Linker API for argument/return union in upcall. + */ +@Test(groups = { "level.sanity" }) +public class UpcallMHWithUnionTests { + private static Linker linker = Linker.nativeLinker(); + + static { + System.loadLibrary("clinkerffitests"); + } + private static final SymbolLookup nativeLibLookup = SymbolLookup.loaderLookup(); + + @Test + public void test_addBoolAndBoolsFromUnionWithXorByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithXorByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromUnionWithXor, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + boolHandle1.set(unionSegmt, 0L, true); + + boolean result = (boolean)mh.invoke(true, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionPtrWithXorByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionPtrWithXorByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromUnionPtrWithXor, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, ADDRESS), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + boolHandle2.set(unionSegmt, 0L, false); + + boolean result = (boolean)mh.invoke(true, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromNestedUnionWithXorByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_BOOLEAN.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedUnionWithXorByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromNestedUnionWithXor, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, true); + + boolean result = (boolean)mh.invoke(false, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromNestedUnionWithXor_reverseOrderByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromNestedUnionWithXor_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromNestedUnionWithXor_reverseOrder, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, true); + + boolean result = (boolean)mh.invoke(true, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, false); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithNestedBoolArrayByUpcallMH() throws Throwable { + SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); + UnionLayout unionLayout = MemoryLayout.unionLayout(boolArray.withName("array_elem1"), JAVA_BOOLEAN.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithNestedBoolArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromUnionWithNestedBoolArray, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, true); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invoke(false, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithNestedBoolArray_reverseOrderByUpcallMH() throws Throwable { + SequenceLayout boolArray = MemoryLayout.sequenceLayout(2, JAVA_BOOLEAN); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), boolArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithNestedBoolArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromUnionWithNestedBoolArray_reverseOrder, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, false); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invoke(false, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithNestedUnionArrayByUpcallMH() throws Throwable { + UnionLayout boolUnion = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, boolUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_BOOLEAN.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithNestedUnionArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromUnionWithNestedUnionArray, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, true); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invoke(true, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, false); + } + } + + @Test + public void test_addBoolAndBoolsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH() throws Throwable { + UnionLayout boolUnion = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, boolUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addBoolAndBoolsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addBoolAndBoolsFromUnionWithNestedUnionArray_reverseOrder, + FunctionDescriptor.of(JAVA_BOOLEAN, JAVA_BOOLEAN, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BOOLEAN, 0, false); + unionSegmt.set(JAVA_BOOLEAN, 1, true); + + boolean result = (boolean)mh.invoke(true, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, true); + } + } + + @Test + public void test_add2BoolUnionsWithXor_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2BoolUnionsWithXor_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolUnionsWithXor_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + boolHandle1.set(unionSegmt1, 0L, true); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + boolHandle2.set(unionSegmt2, 0L, false); + + MemorySegment resultSegmt = (MemorySegment)mh.invoke((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(boolHandle1.get(resultSegmt, 0L), true); + Assert.assertEquals(boolHandle2.get(resultSegmt, 0L), true); + } + } + + @Test + public void test_add2BoolUnionsWithXor_returnUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2BoolUnionsWithXor_returnUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2BoolUnionsWithXor_returnUnionPtr, + FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + boolHandle1.set(unionSegmt1, 0L, true); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + boolHandle2.set(unionSegmt2, 0L, true); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2, upcallFuncAddr); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals(resultSegmt.get(JAVA_BOOLEAN, 0), true); + } + } + + @Test + public void test_add3BoolUnionsWithXor_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), + JAVA_BOOLEAN.withName("elem2"), JAVA_BOOLEAN.withName("elem3")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle boolHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add3BoolUnionsWithXor_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3BoolUnionsWithXor_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + boolHandle1.set(unionSegmt1, 0L, true); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + boolHandle2.set(unionSegmt2, 0L, false); + + MemorySegment resultSegmt = (MemorySegment)mh.invoke((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(boolHandle1.get(resultSegmt, 0L), true); + Assert.assertEquals(boolHandle2.get(resultSegmt, 0L), true); + Assert.assertEquals(boolHandle3.get(resultSegmt, 0L), true); + } + } + + @Test + public void test_addByteAndBytesFromUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromUnion, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt, 0L, (byte)8); + + byte result = (byte)mh.invokeExact((byte)6, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 22); + } + } + + @Test + public void test_addByteAndBytesFromUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromUnionPtr, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, ADDRESS), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + byteHandle2.set(unionSegmt, 0L, (byte)12); + + byte result = (byte)mh.invoke((byte)13, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 37); + } + } + + @Test + public void test_addByteAndBytesFromNestedUnionByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_BYTE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromNestedUnion, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)11); + + byte result = (byte)mh.invoke((byte)33, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 66); + } + } + + @Test + public void test_addByteAndBytesFromNestedUnion_reverseOrderByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromNestedUnion_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromNestedUnion_reverseOrder, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)12); + + byte result = (byte)mh.invoke((byte)48, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 84); + } + } + + @Test + public void test_addByteAndBytesFromUnionWithNestedByteArrayByUpcallMH() throws Throwable { + SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); + UnionLayout unionLayout = MemoryLayout.unionLayout(byteArray.withName("array_elem1"), JAVA_BYTE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionWithNestedByteArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromUnionWithNestedByteArray, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)11); + unionSegmt.set(JAVA_BYTE, 1, (byte)12); + + byte result = (byte)mh.invoke((byte)14, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 48); + } + } + + @Test + public void test_addByteAndBytesFromUnionWithNestedByteArray_reverseOrderByUpcallMH() throws Throwable { + SequenceLayout byteArray = MemoryLayout.sequenceLayout(2, JAVA_BYTE); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), byteArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionWithNestedByteArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromUnionWithNestedByteArray_reverseOrder, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)12); + unionSegmt.set(JAVA_BYTE, 1, (byte)14); + + byte result = (byte)mh.invoke((byte)18, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 56); + } + } + + @Test + public void test_addByteAndBytesFromUnionWithNestedUnionArrayByUpcallMH() throws Throwable { + UnionLayout byteUnion = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, byteUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_BYTE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionWithNestedUnionArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromUnionWithNestedUnionArray, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)11); + unionSegmt.set(JAVA_BYTE, 1, (byte)12); + + byte result = (byte)mh.invoke((byte)16, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 73); + } + } + + @Test + public void test_addByteAndBytesFromUnionWithNestedUnionArray_reverseOrderByUpcallMH() throws Throwable { + UnionLayout byteUnion = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, byteUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addByteAndBytesFromUnionWithNestedUnionArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addByteAndBytesFromUnionWithNestedUnionArray_reverseOrder, + FunctionDescriptor.of(JAVA_BYTE, JAVA_BYTE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_BYTE, 0, (byte)12); + unionSegmt.set(JAVA_BYTE, 1, (byte)14); + + byte result = (byte)mh.invoke((byte)22, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 86); + } + } + + @Test + public void test_add2ByteUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2ByteUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ByteUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt1, 0L, (byte)25); + byteHandle2.set(unionSegmt1, 0L, (byte)11); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt2, 0L, (byte)24); + byteHandle2.set(unionSegmt2, 0L, (byte)13); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals((byte)byteHandle1.get(resultSegmt, 0L), (byte)24); + Assert.assertEquals((byte)byteHandle2.get(resultSegmt, 0L), (byte)24); + } + } + + @Test + public void test_add2ByteUnions_returnUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2ByteUnions_returnUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ByteUnions_returnUnionPtr, + FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt1, 0L, (byte)25); + byteHandle2.set(unionSegmt1, 0L, (byte)11); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt2, 0L, (byte)24); + byteHandle2.set(unionSegmt2, 0L, (byte)13); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2, upcallFuncAddr); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals((byte)byteHandle1.get(resultSegmt, 0L), (byte)37); + Assert.assertEquals((byte)byteHandle2.get(resultSegmt, 0L), (byte)37); + } + } + + @Test + public void test_add3ByteUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle byteHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add3ByteUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3ByteUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt1, 0L, (byte)25); + byteHandle2.set(unionSegmt1, 0L, (byte)11); + byteHandle3.set(unionSegmt1, 0L, (byte)12); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + byteHandle1.set(unionSegmt2, 0L, (byte)24); + byteHandle2.set(unionSegmt2, 0L, (byte)13); + byteHandle3.set(unionSegmt2, 0L, (byte)16); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals((byte)byteHandle1.get(resultSegmt, 0L), (byte)28); + Assert.assertEquals((byte)byteHandle2.get(resultSegmt, 0L), (byte)28); + Assert.assertEquals((byte)byteHandle3.get(resultSegmt, 0L), (byte)28); + } + } + + @Test + public void test_addCharAndCharsFromUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromUnion, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + charHandle1.set(unionSegmt, 0L, 'A'); + charHandle2.set(unionSegmt, 0L, 'B'); + + char result = (char)mh.invokeExact('C', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'E'); + } + } + + @Test + public void test_addCharAndCharsFromUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromUnionPtr, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, ADDRESS), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + charHandle1.set(unionSegmt, 0L, 'H'); + charHandle2.set(unionSegmt, 0L, 'I'); + + char result = (char)mh.invoke('G', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'W'); + } + } + + @Test + public void test_addCharAndCharsFromNestedUnionByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_CHAR.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromNestedUnion, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'E'); + + char result = (char)mh.invokeExact('H', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'T'); + } + } + + @Test + public void test_addCharAndCharsFromNestedUnion_reverseOrderByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromNestedUnion_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromNestedUnion_reverseOrder, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'F'); + + char result = (char)mh.invokeExact('H', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'W'); + } + } + + @Test + public void test_addCharAndCharsFromUnionWithNestedCharArrayByUpcallMH() throws Throwable { + SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); + UnionLayout unionLayout = MemoryLayout.unionLayout(charArray.withName("array_elem1"), JAVA_CHAR.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionWithNestedCharArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromUnionWithNestedCharArray, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'A'); + unionSegmt.set(JAVA_CHAR, 2, 'B'); + + char result = (char)mh.invokeExact('D', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'E'); + } + } + + @Test + public void test_addCharAndCharsFromUnionWithNestedCharArray_reverseOrderByUpcallMH() throws Throwable { + SequenceLayout charArray = MemoryLayout.sequenceLayout(2, JAVA_CHAR); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), charArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionWithNestedCharArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromUnionWithNestedCharArray_reverseOrder, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'A'); + unionSegmt.set(JAVA_CHAR, 2, 'B'); + + char result = (char)mh.invokeExact('D', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'E'); + } + } + + @Test + public void test_addCharAndCharsFromUnionWithNestedUnionArrayByUpcallMH() throws Throwable { + UnionLayout charUnion = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, charUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_CHAR.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionWithNestedUnionArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromUnionWithNestedUnionArray, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'B'); + unionSegmt.set(JAVA_CHAR, 2, 'C'); + + char result = (char)mh.invokeExact('J', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'Q'); + } + } + + @Test + public void test_addCharAndCharsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH() throws Throwable { + UnionLayout charUnion = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, charUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addCharAndCharsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addCharAndCharsFromUnionWithNestedUnionArray_reverseOrder, + FunctionDescriptor.of(JAVA_CHAR, JAVA_CHAR, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_CHAR, 0, 'B'); + unionSegmt.set(JAVA_CHAR, 2, 'C'); + + char result = (char)mh.invokeExact('J', unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 'Q'); + } + } + + @Test + public void test_add2CharUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2CharUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2CharUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt1, 0L, 'A'); + charHandle2.set(unionSegmt1, 0L, 'B'); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt2, 0L, 'C'); + charHandle2.set(unionSegmt2, 0L, 'D'); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(charHandle1.get(resultSegmt, 0L), 'E'); + Assert.assertEquals(charHandle2.get(resultSegmt, 0L), 'E'); + } + } + + @Test + public void test_add2CharUnions_returnUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2CharUnions_returnUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2CharUnions_returnUnionPtr, + FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt1, 0L, 'A'); + charHandle2.set(unionSegmt1, 0L, 'B'); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt2, 0L, 'B'); + charHandle2.set(unionSegmt2, 0L, 'C'); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2, upcallFuncAddr); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals(charHandle1.get(resultSegmt, 0L), 'F'); + Assert.assertEquals(charHandle2.get(resultSegmt, 0L), 'F'); + } + } + + @Test + public void test_add3CharUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), + JAVA_CHAR.withName("elem2"), JAVA_CHAR.withName("elem3")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle charHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add3CharUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3CharUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt1, 0L, 'A'); + charHandle2.set(unionSegmt1, 0L, 'B'); + charHandle3.set(unionSegmt1, 0L, 'C'); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + charHandle1.set(unionSegmt2, 0L, 'B'); + charHandle2.set(unionSegmt2, 0L, 'C'); + charHandle3.set(unionSegmt2, 0L, 'D'); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(charHandle1.get(resultSegmt, 0L), 'F'); + Assert.assertEquals(charHandle2.get(resultSegmt, 0L), 'F'); + Assert.assertEquals(charHandle3.get(resultSegmt, 0L), 'F'); + } + } + + @Test + public void test_addShortAndShortsFromUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromUnion, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + shortHandle2.set(unionSegmt, 0L, (short)9); + + short result = (short)mh.invokeExact((short)6, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 24); + } + } + + @Test + public void test_addShortAndShortsFromUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromUnionPtr, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, ADDRESS), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt, 0L, (short)22); + + short result = (short)mh.invoke((short)66, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 110); + } + } + + @Test + public void test_addShortAndShortsFromNestedUnionByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_SHORT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromNestedUnion, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)31); + + short result = (short)mh.invokeExact((short)37, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 130); + } + } + + @Test + public void test_addShortAndShortsFromNestedUnion_reverseOrderByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromNestedUnion_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromNestedUnion_reverseOrder, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)31); + + short result = (short)mh.invokeExact((short)37, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 130); + } + } + + @Test + public void test_addShortAndShortsFromUnionWithNestedShortArrayByUpcallMH() throws Throwable { + SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); + UnionLayout unionLayout = MemoryLayout.unionLayout(shortArray.withName("array_elem1"), JAVA_SHORT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionWithNestedShortArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromUnionWithNestedShortArray, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)111); + unionSegmt.set(JAVA_SHORT, 2, (short)222); + + short result = (short)mh.invokeExact((short)444, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 888); + } + } + + @Test + public void test_addShortAndShortsFromUnionWithNestedShortArray_reverseOrderByUpcallMH() throws Throwable { + SequenceLayout shortArray = MemoryLayout.sequenceLayout(2, JAVA_SHORT); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), shortArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionWithNestedShortArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromUnionWithNestedShortArray_reverseOrder, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)111); + unionSegmt.set(JAVA_SHORT, 2, (short)222); + + short result = (short)mh.invokeExact((short)444, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 888); + } + } + + @Test + public void test_addShortAndShortsFromUnionWithNestedUnionArrayByUpcallMH() throws Throwable { + UnionLayout shortUnion = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, shortUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_SHORT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionWithNestedUnionArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromUnionWithNestedUnionArray, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)111); + unionSegmt.set(JAVA_SHORT, 2, (short)222); + + short result = (short)mh.invokeExact((short)666, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1443); + } + } + + @Test + public void test_addShortAndShortsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH() throws Throwable { + UnionLayout shortUnion = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, shortUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortsFromUnionWithNestedUnionArray_reverseOrder, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_SHORT, 0, (short)111); + unionSegmt.set(JAVA_SHORT, 2, (short)222); + + short result = (short)mh.invokeExact((short)666, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1443); + } + } + + @Test + public void test_add2ShortUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2ShortUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ShortUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt1, 0L, (short)56); + shortHandle2.set(unionSegmt1, 0L, (short)45); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt2, 0L, (short)78); + shortHandle2.set(unionSegmt2, 0L, (short)67); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals((short)shortHandle1.get(resultSegmt, 0L), (short)112); + Assert.assertEquals((short)shortHandle2.get(resultSegmt, 0L), (short)112); + } + } + + @Test + public void test_add2ShortUnions_returnUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2ShortUnions_returnUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2ShortUnions_returnUnionPtr, + FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt1, 0L, (short)56); + shortHandle2.set(unionSegmt1, 0L, (short)45); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt2, 0L, (short)78); + shortHandle2.set(unionSegmt2, 0L, (short)67); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2, upcallFuncAddr); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals((short)shortHandle1.get(resultSegmt, 0L), (short)179); + Assert.assertEquals((short)shortHandle2.get(resultSegmt, 0L), (short)179); + } + } + + @Test + public void test_add3ShortUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle shortHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add3ShortUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3ShortUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt1, 0L, (short)25); + shortHandle2.set(unionSegmt1, 0L, (short)26); + shortHandle3.set(unionSegmt1, 0L, (short)27); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + shortHandle1.set(unionSegmt2, 0L, (short)34); + shortHandle2.set(unionSegmt2, 0L, (short)35); + shortHandle3.set(unionSegmt2, 0L, (short)36); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals((short)shortHandle1.get(resultSegmt, 0L), (short)63); + Assert.assertEquals((short)shortHandle2.get(resultSegmt, 0L), (short)63); + Assert.assertEquals((short)shortHandle3.get(resultSegmt, 0L), (short)63); + } + } + + @Test + public void test_addIntAndIntsFromUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromUnion, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + intHandle2.set(unionSegmt, 0L, 1234567); + + int result = (int)mh.invokeExact(2244668, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 4713802); + } + } + + @Test + public void test_addIntAndIntsFromUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromUnionPtr, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, ADDRESS), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + intHandle1.set(unionSegmt, 0L, 11121314); + intHandle2.set(unionSegmt, 0L, 15161718); + + int result = (int)mh.invoke(19202122, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 49525558); + } + } + + @Test + public void test_addIntAndIntsFromNestedUnionByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_INT.withName("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromNestedUnion, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 21222324); + + int result = (int)mh.invokeExact(33343536, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 97010508); + } + } + + @Test + public void test_addIntAndIntsFromNestedUnion_reverseOrderByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromNestedUnion_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromNestedUnion_reverseOrder, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 21222324); + + int result = (int)mh.invokeExact(33343536, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 97010508); + } + } + + @Test + public void test_addIntAndIntsFromUnionWithNestedIntArrayByUpcallMH() throws Throwable { + SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); + UnionLayout unionLayout = MemoryLayout.unionLayout(intArray.withName("array_elem1"), JAVA_INT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionWithNestedIntArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromUnionWithNestedIntArray, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 1111111); + unionSegmt.set(JAVA_INT, 4, 2222222); + + int result = (int)mh.invokeExact(4444444, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 8888888); + } + } + + @Test + public void test_addIntAndIntsFromUnionWithNestedIntArray_reverseOrderByUpcallMH() throws Throwable { + SequenceLayout intArray = MemoryLayout.sequenceLayout(2, JAVA_INT); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), intArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionWithNestedIntArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromUnionWithNestedIntArray_reverseOrder, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 1111111); + unionSegmt.set(JAVA_INT, 4, 2222222); + + int result = (int)mh.invokeExact(4444444, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 8888888); + } + } + + @Test + public void test_addIntAndIntsFromUnionWithNestedUnionArrayByUpcallMH() throws Throwable { + UnionLayout intUnion = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, intUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_INT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionWithNestedUnionArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromUnionWithNestedUnionArray, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 2222222); + unionSegmt.set(JAVA_INT, 4, 3333333); + + int result = (int)mh.invokeExact(6666666, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 19999998); + } + } + + @Test + public void test_addIntAndIntsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH() throws Throwable { + UnionLayout intUnion = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, intUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntsFromUnionWithNestedUnionArray_reverseOrder, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_INT, 0, 2222222); + unionSegmt.set(JAVA_INT, 4, 3333333); + + int result = (int)mh.invokeExact(6666666, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 19999998); + } + } + + @Test + public void test_add2IntUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2IntUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt1, 0L, 11223344); + intHandle2.set(unionSegmt1, 0L, 55667788); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt2, 0L, 99001122); + intHandle2.set(unionSegmt2, 0L, 33445566); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(intHandle1.get(resultSegmt, 0L), 89113354); + Assert.assertEquals(intHandle2.get(resultSegmt, 0L), 89113354); + } + } + + @Test + public void test_add2IntUnions_returnUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2IntUnions_returnUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2IntUnions_returnUnionPtr, + FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt1, 0L, 11223344); + intHandle2.set(unionSegmt1, 0L, 55667788); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt2, 0L, 99001122); + intHandle2.set(unionSegmt2, 0L, 33445566); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2, upcallFuncAddr); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals(intHandle1.get(resultSegmt, 0L), 122558920); + Assert.assertEquals(intHandle2.get(resultSegmt, 0L), 122558920); + } + } + + @Test + public void test_add3IntUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle intHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add3IntUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3IntUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt1, 0L, 11223344); + intHandle2.set(unionSegmt1, 0L, 55667788); + intHandle3.set(unionSegmt1, 0L, 99001122); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + intHandle1.set(unionSegmt2, 0L, 33445566); + intHandle2.set(unionSegmt2, 0L, 77889900); + intHandle3.set(unionSegmt2, 0L, 44332211); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(intHandle1.get(resultSegmt, 0L), 143333333); + Assert.assertEquals(intHandle2.get(resultSegmt, 0L), 143333333); + Assert.assertEquals(intHandle3.get(resultSegmt, 0L), 143333333); + } + } + + @Test + public void test_addLongAndLongsFromUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromUnion, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + longHandle1.set(unionSegmt, 0L, 1234567890L); + longHandle2.set(unionSegmt, 0L, 9876543210L); + + long result = (long)mh.invokeExact(2468024680L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 22221111100L); + } + } + + @Test + public void test_addLongAndLongsFromUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromUnionPtr, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, ADDRESS), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + longHandle1.set(unionSegmt, 0L, 224466880022L); + longHandle2.set(unionSegmt, 0L, 446688002244L); + + long result = (long)mh.invoke(668800224466L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1562176228954L); + } + } + + @Test + public void test_addLongAndLongsFromNestedUnionByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromNestedUnion, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 135791357913L); + + long result = (long)mh.invokeExact(778899001122L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1186273074861L); + } + } + + @Test + public void test_addLongAndLongsFromNestedUnion_reverseOrderByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromNestedUnion_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromNestedUnion_reverseOrder, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 135791357913L); + + long result = (long)mh.invokeExact(778899001122L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1186273074861L); + } + } + + @Test + public void test_addLongAndLongsFromUnionWithNestedLongArrayByUpcallMH() throws Throwable { + SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); + UnionLayout unionLayout = MemoryLayout.unionLayout(longArray.withName("array_elem1"), JAVA_LONG.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionWithNestedLongArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromUnionWithNestedLongArray, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 111111111L); + unionSegmt.set(JAVA_LONG, 8, 222222222L); + + long result = (long)mh.invokeExact(444444444L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 888888888L); + } + } + + @Test + public void test_addLongAndLongsFromUnionWithNestedLongArray_reverseOrderByUpcallMH() throws Throwable { + SequenceLayout longArray = MemoryLayout.sequenceLayout(2, JAVA_LONG); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), longArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionWithNestedLongArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromUnionWithNestedLongArray_reverseOrder, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 111111111L); + unionSegmt.set(JAVA_LONG, 8, 222222222L); + + long result = (long)mh.invokeExact(444444444L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 888888888L); + } + } + + @Test + public void test_addLongAndLongsFromUnionWithNestedUnionArrayByUpcallMH() throws Throwable { + UnionLayout longUnion = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, longUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_LONG.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionWithNestedUnionArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromUnionWithNestedUnionArray, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 111111111L); + unionSegmt.set(JAVA_LONG, 8, 222222222L); + + long result = (long)mh.invokeExact(666666666L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1444444443L); + } + } + + @Test + public void test_addLongAndLongsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH() throws Throwable { + UnionLayout longUnion = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, longUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongsFromUnionWithNestedUnionArray_reverseOrder, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_LONG, 0, 111111111L); + unionSegmt.set(JAVA_LONG, 8, 222222222L); + + long result = (long)mh.invokeExact(666666666L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1444444443L); + } + } + + @Test + public void test_add2LongUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2LongUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2LongUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt1, 0L, 987654321987L); + longHandle2.set(unionSegmt1, 0L, 123456789123L); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt2, 0L, 224466880022L); + longHandle2.set(unionSegmt2, 0L, 113355779911L); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(longHandle1.get(resultSegmt, 0L), 236812569034L); + Assert.assertEquals(longHandle2.get(resultSegmt, 0L), 236812569034L); + } + } + + @Test + public void test_add2LongUnions_returnUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2LongUnions_returnUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2LongUnions_returnUnionPtr, + FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt1, 0L, 1122334455L); + longHandle2.set(unionSegmt1, 0L, 5566778899L); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt2, 0L, 9900112233L); + longHandle2.set(unionSegmt2, 0L, 3344556677L); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2, upcallFuncAddr); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals(longHandle1.get(resultSegmt, 0L), 12255892253L); + Assert.assertEquals(longHandle2.get(resultSegmt, 0L), 12255892253L); + } + } + + @Test + public void test_add3LongUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle longHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add3LongUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3LongUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt1, 0L, 987654321987L); + longHandle2.set(unionSegmt1, 0L, 123456789123L); + longHandle3.set(unionSegmt1, 0L, 112233445566L); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + longHandle1.set(unionSegmt2, 0L, 224466880022L); + longHandle2.set(unionSegmt2, 0L, 113355779911L); + longHandle3.set(unionSegmt2, 0L, 778899001122L); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals(longHandle1.get(resultSegmt, 0L), 891132446688L); + Assert.assertEquals(longHandle2.get(resultSegmt, 0L), 891132446688L); + Assert.assertEquals(longHandle3.get(resultSegmt, 0L), 891132446688L); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromUnion, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt, 0L, 8.12F); + floatHandle2.set(unionSegmt, 0L, 9.24F); + + float result = (float)mh.invokeExact(6.56F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 25.04F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromUnionPtr, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, ADDRESS), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt, 0L, 35.11F); + floatHandle2.set(unionSegmt, 0L, 46.22F); + + float result = (float)mh.invoke(79.32F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 171.76F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromNestedUnionByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_FLOAT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromNestedUnion, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 31.22F); + + float result = (float)mh.invokeExact(37.88F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 131.54F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromNestedUnion_reverseOrderByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromNestedUnion_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromNestedUnion_reverseOrder, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 31.22F); + + float result = (float)mh.invokeExact(37.88F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 131.54F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionWithNestedFloatArrayByUpcallMH() throws Throwable { + SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); + UnionLayout unionLayout = MemoryLayout.unionLayout(floatArray.withName("array_elem1"), JAVA_FLOAT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionWithNestedFloatArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromUnionWithNestedFloatArray, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 111.11F); + unionSegmt.set(JAVA_FLOAT, 4, 222.22F); + + float result = (float)mh.invokeExact(444.44F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 888.88F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionWithNestedFloatArray_reverseOrderByUpcallMH() throws Throwable { + SequenceLayout floatArray = MemoryLayout.sequenceLayout(2, JAVA_FLOAT); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), floatArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionWithNestedFloatArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromUnionWithNestedFloatArray_reverseOrder, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 111.11F); + unionSegmt.set(JAVA_FLOAT, 4, 222.22F); + + float result = (float)mh.invokeExact(444.44F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 888.88F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionWithNestedUnionArrayByUpcallMH() throws Throwable { + UnionLayout floatUnion = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, floatUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_FLOAT.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionWithNestedUnionArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromUnionWithNestedUnionArray, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 444.44F); + unionSegmt.set(JAVA_FLOAT, 4, 555.55F); + + float result = (float)mh.invokeExact(666.66F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 3111.08F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH() throws Throwable { + UnionLayout floatUnion = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, floatUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatsFromUnionWithNestedUnionArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatsFromUnionWithNestedUnionArray_reverseOrder, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_FLOAT, 0, 444.44F); + unionSegmt.set(JAVA_FLOAT, 4, 555.55F); + + float result = (float)mh.invokeExact(666.66F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 3111.08F, 0.01F); + } + } + + @Test + public void test_add2FloatUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2FloatUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2FloatUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt1, 0L, 25.12F); + floatHandle2.set(unionSegmt1, 0L, 11.23F); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt2, 0L, 24.34F); + floatHandle2.set(unionSegmt2, 0L, 13.45F); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals((float)floatHandle1.get(resultSegmt, 0L), 24.68F, 0.01F); + Assert.assertEquals((float)floatHandle2.get(resultSegmt, 0L), 24.68F, 0.01F); + } + } + + @Test + public void test_add2FloatUnions_returnUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2FloatUnions_returnUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2FloatUnions_returnUnionPtr, + FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt1, 0L, 25.12F); + floatHandle2.set(unionSegmt1, 0L, 11.23F); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt2, 0L, 24.34F); + floatHandle2.set(unionSegmt2, 0L, 13.47F); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2, upcallFuncAddr); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals((float)floatHandle1.get(resultSegmt, 0L), 38.17F, 0.01F); + Assert.assertEquals((float)floatHandle2.get(resultSegmt, 0L), 38.17F, 0.01F); + } + } + + @Test + public void test_add3FloatUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle floatHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add3FloatUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3FloatUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt1, 0L, 25.12F); + floatHandle2.set(unionSegmt1, 0L, 11.23F); + floatHandle3.set(unionSegmt1, 0L, 45.67F); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + floatHandle1.set(unionSegmt2, 0L, 24.34F); + floatHandle2.set(unionSegmt2, 0L, 13.45F); + floatHandle3.set(unionSegmt2, 0L, 69.72F); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals((float)floatHandle1.get(resultSegmt, 0L), 115.39F, 0.01F); + Assert.assertEquals((float)floatHandle2.get(resultSegmt, 0L), 115.39F, 0.01F); + Assert.assertEquals((float)floatHandle3.get(resultSegmt, 0L), 115.39F, 0.01F); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromUnion, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt, 0L, 2228.111D); + doubleHandle2.set(unionSegmt, 0L, 2229.221D); + + double result = (double)mh.invokeExact(3336.333D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 7794.775D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromUnionPtr, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, ADDRESS), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt, 0L, 2228.111D); + doubleHandle2.set(unionSegmt, 0L, 2229.221D); + + double result = (double)mh.invokeExact(3336.333D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 7794.775D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromNestedUnionByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(nestedUnionLayout.withName("union_elem1"), JAVA_DOUBLE.withName("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromNestedUnion, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 31.789D); + + double result = (double)mh.invokeExact(37.864D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 133.231D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromNestedUnion_reverseOrderByUpcallMH() throws Throwable { + UnionLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromNestedUnion_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromNestedUnion_reverseOrder, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 31.789D); + + double result = (double)mh.invokeExact(37.864D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 133.231D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionWithNestedDoubleArrayByUpcallMH() throws Throwable { + SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); + UnionLayout unionLayout = MemoryLayout.unionLayout(doubleArray.withName("array_elem1"), JAVA_DOUBLE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionWithNestedDoubleArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromUnionWithNestedDoubleArray, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 111.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 222.222D); + + double result = (double)mh.invokeExact(444.444D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 888.888D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionWithNestedDoubleArray_reverseOrderByUpcallMH() throws Throwable { + SequenceLayout doubleArray = MemoryLayout.sequenceLayout(2, JAVA_DOUBLE); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), doubleArray.withName("array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionWithNestedDoubleArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromUnionWithNestedDoubleArray_reverseOrder, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 111.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 222.222D); + + double result = (double)mh.invokeExact(444.444D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 888.888D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionWithNestedUnionArrayByUpcallMH() throws Throwable { + UnionLayout doubleUnion = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, doubleUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(unionArray.withName("union_array_elem1"), JAVA_DOUBLE.withName("elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionWithNestedUnionArrayByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromUnionWithNestedUnionArray, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 111.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 222.222D); + + double result = (double)mh.invokeExact(666.665D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1444.442D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoublesFromUnionWithNestedUnionArray_reverseOrderByUpcallMH() throws Throwable { + UnionLayout doubleUnion = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + SequenceLayout unionArray = MemoryLayout.sequenceLayout(2, doubleUnion); + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), unionArray.withName("union_array_elem2")); + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoublesFromUnionWithNestedUnionArray_reverseOrderByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoublesFromUnionWithNestedUnionArray_reverseOrder, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + unionSegmt.set(JAVA_DOUBLE, 0, 111.111D); + unionSegmt.set(JAVA_DOUBLE, 8, 222.222D); + + double result = (double)mh.invokeExact(666.665D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1444.442D, 0.001D); + } + } + + @Test + public void test_add2DoubleUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2DoubleUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt1, 0L, 11.222D); + doubleHandle2.set(unionSegmt1, 0L, 22.333D); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt2, 0L, 33.444D); + doubleHandle2.set(unionSegmt2, 0L, 44.555D); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals((double)doubleHandle1.get(resultSegmt, 0L), 66.888D, 0.001D); + Assert.assertEquals((double)doubleHandle2.get(resultSegmt, 0L), 66.888D, 0.001D); + } + } + + @Test + public void test_add2DoubleUnions_returnUnionPtrByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add2DoubleUnions_returnUnionPtrByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add2DoubleUnions_returnUnionPtr, + FunctionDescriptor.of(ADDRESS, ADDRESS, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt1, 0L, 11.222D); + doubleHandle2.set(unionSegmt1, 0L, 22.333D); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt2, 0L, 33.444D); + doubleHandle2.set(unionSegmt2, 0L, 44.555D); + + MemorySegment resultAddr = (MemorySegment)mh.invoke(unionSegmt1, unionSegmt2, upcallFuncAddr); + MemorySegment resultSegmt = resultAddr.reinterpret(unionLayout.byteSize()); + Assert.assertEquals((double)doubleHandle1.get(resultSegmt, 0L), 111.443D, 0.001D); + Assert.assertEquals((double)doubleHandle2.get(resultSegmt, 0L), 111.443D, 0.001D); + } + } + + @Test + public void test_add3DoubleUnions_returnUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle doubleHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(unionLayout, unionLayout, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("add3DoubleUnions_returnUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_add3DoubleUnions_returnUnion, + FunctionDescriptor.of(unionLayout, unionLayout, unionLayout), arena); + MemorySegment unionSegmt1 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt1, 0L, 11.222D); + doubleHandle2.set(unionSegmt1, 0L, 22.333D); + doubleHandle3.set(unionSegmt1, 0L, 33.123D); + MemorySegment unionSegmt2 = arena.allocate(unionLayout); + doubleHandle1.set(unionSegmt2, 0L, 33.444D); + doubleHandle2.set(unionSegmt2, 0L, 44.555D); + doubleHandle3.set(unionSegmt2, 0L, 55.456D); + + MemorySegment resultSegmt = (MemorySegment)mh.invokeExact((SegmentAllocator)arena, unionSegmt1, unionSegmt2, upcallFuncAddr); + Assert.assertEquals((double)doubleHandle1.get(resultSegmt, 0L), 88.579D, 0.001D); + Assert.assertEquals((double)doubleHandle2.get(resultSegmt, 0L), 88.579D, 0.001D); + Assert.assertEquals((double)doubleHandle3.get(resultSegmt, 0L), 88.579D, 0.001D); + } + } + + @Test + public void test_addShortAndShortFromUnionWithByteShortByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addShortAndShortFromUnionWithByteShortByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addShortAndShortFromUnionWithByteShort, + FunctionDescriptor.of(JAVA_SHORT, JAVA_SHORT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (short)10001); + Assert.assertEquals((short)elemHandle2.get(unionSegmt, 0L), 10001); + + short result = (short)mh.invokeExact((short)10020, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 20021); + } + } + + @Test + public void test_addIntAndByteFromUnionWithIntByteByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndByteFromUnionWithIntByteByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndByteFromUnionWithIntByte, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (byte)110); + Assert.assertEquals((byte)elemHandle2.get(unionSegmt, 0L), 110); + + int result = (int)mh.invokeExact(22334455, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 22334565); + } + } + + @Test + public void test_addIntAndIntFromUnionWithByteIntByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromUnionWithByteIntByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromUnionWithByteInt, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 1122334); + Assert.assertEquals((int)elemHandle2.get(unionSegmt, 0L), 1122334); + + int result = (int)mh.invokeExact(11335577, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 12457911); + } + } + + @Test + public void test_addIntAndShortFromUnionWithIntShortByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndShortFromUnionWithIntShortByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndShortFromUnionWithIntShort, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (short)32766); + Assert.assertEquals((short)elemHandle2.get(unionSegmt, 0L), 32766); + + int result = (int)mh.invokeExact(22334455, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 22367221); + } + } + + @Test + public void test_addIntAndIntFromUnionWithShortIntByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndIntFromUnionWithShortIntByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndIntFromUnionWithShortInt, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 1122334); + Assert.assertEquals((int)elemHandle2.get(unionSegmt, 0L), 1122334); + + int result = (int)mh.invokeExact(11335577, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 12457911); + } + } + + @Test + public void test_addIntAndByteFromUnionWithIntShortByteByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_BYTE.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndByteFromUnionWithIntShortByteByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndByteFromUnionWithIntShortByte, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, (byte)22); + Assert.assertEquals((byte)elemHandle3.get(unionSegmt, 0L), 22); + + int result = (int)mh.invokeExact(22334455, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 22334477); + } + } + + @Test + public void test_addIntAndShortFromUnionWithByteShortIntByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_INT.withName("elem3")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndShortFromUnionWithByteShortIntByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndShortFromUnionWithByteShortInt, + FunctionDescriptor.of(JAVA_INT, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (short)32766); + Assert.assertEquals((short)elemHandle2.get(unionSegmt, 0L), 32766); + + int result = (int)mh.invokeExact(11335577, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 11368343); + } + } + + @Test + public void test_addIntAndLongFromUnionWithIntLongByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndLongFromUnionWithIntLongByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndLongFromUnionWithIntLong, + FunctionDescriptor.of(JAVA_LONG, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 667788990011L); + Assert.assertEquals((long)elemHandle2.get(unionSegmt, 0L), 667788990011L); + + long result = (long)mh.invokeExact(22446688, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 667811436699L); + } + } + + @Test + public void test_addIntAndLongFromUnionWithLongIntByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_INT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addIntAndLongFromUnionWithLongIntByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addIntAndLongFromUnionWithLongInt, + FunctionDescriptor.of(JAVA_LONG, JAVA_INT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 667788990011L); + Assert.assertEquals((long)elemHandle1.get(unionSegmt, 0L), 667788990011L); + + long result = (long)mh.invokeExact(1234567, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 667790224578L); + } + } + + @Test + public void test_addLongAndPtrValueFromUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), ADDRESS.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndPtrValueFromUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndPtrValueFromUnion, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + MemorySegment longSegmt = arena.allocateFrom(JAVA_LONG, 112233445566778899L); + elemHandle3.set(unionSegmt, 0L, longSegmt); + MemorySegment elemSegmt = ((MemorySegment)elemHandle3.get(unionSegmt, 0L)).reinterpret(JAVA_LONG.byteSize()); + Assert.assertEquals(elemSegmt.get(JAVA_LONG, 0), 112233445566778899L); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 112233446587080909L); + } + } + + @Test + public void test_addLongAndIntFromUnionWithByteShortIntLongByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_INT.withName("elem3"), JAVA_LONG.withName("elem4")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndIntFromUnionWithByteShortIntLongByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndIntFromUnionWithByteShortIntLong, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 11223344); + Assert.assertEquals((int)elemHandle3.get(unionSegmt, 0L), 11223344); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1031525354L); + } + } + + @Test + public void test_addLongAndIntFromUnionWithLongIntShortByteByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), + JAVA_INT.withName("elem2"), JAVA_SHORT.withName("elem3"), JAVA_BYTE.withName("elem4")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndIntFromUnionWithLongIntShortByteByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndIntFromUnionWithLongIntShortByte, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 11223344); + Assert.assertEquals((int)elemHandle2.get(unionSegmt, 0L), 11223344); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1031525354L); + } + } + + @Test + public void test_addLongAndLongFromUnionWithLongFloatByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromUnionWithLongFloatByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromUnionWithLongFloat, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 100000000005L); + Assert.assertEquals((long)elemHandle1.get(unionSegmt, 0L), 100000000005L); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 101020302015L); + } + } + + @Test + public void test_addLongAndLongFromUnionWithIntFloatLongByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_LONG.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromUnionWithIntFloatLongByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromUnionWithIntFloatLong, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 100000000005L); + Assert.assertEquals((long)elemHandle3.get(unionSegmt, 0L), 100000000005L); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 101020302015L); + } + } + + @Test + public void test_addLongAndLongFromUnionWithIntLongFloatByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_LONG.withName("elem2"), JAVA_FLOAT.withName("elem3")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addLongAndLongFromUnionWithIntLongFloatByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addLongAndLongFromUnionWithIntLongFloat, + FunctionDescriptor.of(JAVA_LONG, JAVA_LONG, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 100000000005L); + Assert.assertEquals((long)elemHandle2.get(unionSegmt, 0L), 100000000005L); + + long result = (long)mh.invokeExact(1020302010L, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 101020302015L); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithShortFloatByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithShortFloatByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromUnionWithShortFloat, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 0.11F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 0.11F); + + float result = (float)mh.invokeExact(0.22F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 0.33F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithFloatShortByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithFloatShortByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromUnionWithFloatShort, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 0.11F); + Assert.assertEquals((float)elemHandle1.get(unionSegmt, 0L), 0.11F, 0.01F); + + float result = (float)mh.invokeExact(0.22F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 0.33F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithIntFloatByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithIntFloatByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromUnionWithIntFloat, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 0.11F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 0.11F, 0.01F); + + float result = (float)mh.invokeExact(0.22F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 0.33F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithFloatIntByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithFloatIntByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromUnionWithFloatInt, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 0.11F); + Assert.assertEquals((float)elemHandle1.get(unionSegmt, 0L), 0.11F, 0.01F); + + float result = (float)mh.invokeExact(0.22F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 0.33F, 0.01F); + } + } + + @Test + public void test_addFloatAndFloatFromUnionWithFloatLongByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addFloatAndFloatFromUnionWithFloatLongByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addFloatAndFloatFromUnionWithFloatLong, + FunctionDescriptor.of(JAVA_FLOAT, JAVA_FLOAT, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 0.11F); + Assert.assertEquals((float)elemHandle1.get(unionSegmt, 0L), 0.11F, 0.1F); + + float result = (float)mh.invokeExact(0.22F, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 0.33F, 0.01F); + } + } + + @Test + public void test_addDoubleAndPtrValueFromUnionByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), ADDRESS.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndPtrValueFromUnionByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndPtrValueFromUnion, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + MemorySegment longSegmt = arena.allocateFrom(JAVA_LONG, 112233445566778899L); + elemHandle3.set(unionSegmt, 0L, longSegmt); + MemorySegment elemSegmt = ((MemorySegment)elemHandle3.get(unionSegmt, 0L)).reinterpret(JAVA_LONG.byteSize()); + Assert.assertEquals(elemSegmt.get(JAVA_LONG, 0), 112233445566778899L); + + double result = (double)mh.invokeExact(222.44D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 112233445566779121.44D, 0.01D); + } + } + + @Test + public void test_addDoubleAndIntFromUnionWithIntDoubleByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndIntFromUnionWithIntDoubleByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndIntFromUnionWithIntDouble, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 1122334455); + Assert.assertEquals((int)elemHandle1.get(unionSegmt, 0L), 1122334455); + + double result = (double)mh.invokeExact(113.567D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 1122334568.567D, 0.001D); + } + } + + @Test + public void test_addDoubleAndLongFromUnionWithDoubleLongByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndLongFromUnionWithDoubleLongByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndLongFromUnionWithDoubleLong, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 11223344556677L); + Assert.assertEquals((long)elemHandle2.get(unionSegmt, 0L), 11223344556677L); + + double result = (double)mh.invokeExact(222.33D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 11223344556899.33D, 0.01D); + } + } + + @Test + public void test_addDoubleAndLongFromUnionWithIntDoubleLongByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), JAVA_LONG.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndLongFromUnionWithIntDoubleLongByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndLongFromUnionWithIntDoubleLong, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 100000000001L); + Assert.assertEquals((long)elemHandle3.get(unionSegmt, 0L), 100000000001L); + + double result = (double)mh.invokeExact(222.44D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 100000000223.44D, 0.01D); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithFloatDoubleByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithFloatDoubleByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatFromUnionWithFloatDouble, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 18.444F); + Assert.assertEquals((float)elemHandle1.get(unionSegmt, 0L), 18.444F, 001F); + + double result = (double)mh.invokeExact(113.567D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 132.011D, 0.001D); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithDoubleFloatByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithDoubleFloatByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatFromUnionWithDoubleFloat, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 19.22F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 19.22F, 0.01D); + + double result = (double)mh.invokeExact(216.666D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 235.886D, 0.001D); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithIntFloatDoubleByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithIntFloatDoubleByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatFromUnionWithIntFloatDouble, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 18.444F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 18.444F, 0.001F); + + double result = (double)mh.invokeExact(113.567D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 132.011D, 0.001D); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithIntDoubleFloatByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), JAVA_FLOAT.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithIntDoubleFloatByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatFromUnionWithIntDoubleFloat, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 18.444F); + Assert.assertEquals((float)elemHandle3.get(unionSegmt, 0L), 18.444F, 0.001F); + + double result = (double)mh.invokeExact(113.567D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 132.011D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoubleFromUnionWithDoubleIntByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromUnionWithDoubleIntByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromUnionWithDoubleInt, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle1.set(unionSegmt, 0L, 218.555D); + Assert.assertEquals((double)elemHandle1.get(unionSegmt, 0L), 218.555D, 0.001D); + + double result = (double)mh.invokeExact(216.666D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 435.221D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoubleFromUnionWithLongDoubleByUpcallMH() throws Throwable { + UnionLayout unionLayout =MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromUnionWithLongDoubleByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromUnionWithLongDouble, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 222.333D); + Assert.assertEquals((double)elemHandle2.get(unionSegmt, 0L), 222.333D, 0.001D); + + double result = (double)mh.invokeExact(111.222D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 333.555D, 0.001D); + } + } + + @Test + public void test_addDoubleAndDoubleFromUnionWithFloatLongDoubleByUpcallMH() throws Throwable { + UnionLayout unionLayout =MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), + JAVA_LONG.withName("elem2"), JAVA_DOUBLE.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndDoubleFromUnionWithFloatLongDoubleByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndDoubleFromUnionWithFloatLongDouble, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle3.set(unionSegmt, 0L, 100.11D); + Assert.assertEquals((double)elemHandle3.get(unionSegmt, 0L), 100.11D, 01D); + + double result = (double)mh.invokeExact(222.44D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 322.55D, 0.01D); + } + } + + @Test + public void test_addDoubleAndShortFromUnionWithByteShortFloatDoubleByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_DOUBLE.withName("elem4")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndShortFromUnionWithByteShortFloatDoubleByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndShortFromUnionWithByteShortFloatDouble, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, (short)32766); + Assert.assertEquals((short)elemHandle2.get(unionSegmt, 0L), 32766); + + double result = (double)mh.invokeExact(33333.57D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 66099.57D, 0.01D); + } + } + + @Test + public void test_addDoubleAndFloatFromUnionWithDoubleFloatShortByteByUpcallMH() throws Throwable { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_SHORT.withName("elem3"), JAVA_BYTE.withName("elem4")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + FunctionDescriptor fd = FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout, ADDRESS); + MemorySegment functionSymbol = nativeLibLookup.find("addDoubleAndFloatFromUnionWithDoubleFloatShortByteByUpcallMH").get(); + MethodHandle mh = linker.downcallHandle(functionSymbol, fd); + + try (Arena arena = Arena.ofConfined()) { + MemorySegment upcallFuncAddr = linker.upcallStub(UpcallMethodHandles.MH_addDoubleAndFloatFromUnionWithDoubleFloatShortByte, + FunctionDescriptor.of(JAVA_DOUBLE, JAVA_DOUBLE, unionLayout), arena); + MemorySegment unionSegmt = arena.allocate(unionLayout); + elemHandle2.set(unionSegmt, 0L, 1.001F); + Assert.assertEquals((float)elemHandle2.get(unionSegmt, 0L), 1.001F, 0.001F); + + double result = (double)mh.invokeExact(33333.567D, unionSegmt, upcallFuncAddr); + Assert.assertEquals(result, 33334.568D, 0.001D); + } + } +} diff --git a/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMethodHandles.java b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMethodHandles.java index e26d33467a5..7f048241008 100644 --- a/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMethodHandles.java +++ b/test/functional/Java22andUp/src/org/openj9/test/jep454/upcall/UpcallMethodHandles.java @@ -28,6 +28,7 @@ import java.lang.invoke.MethodHandles.Lookup; import java.lang.invoke.MethodType; import static java.lang.invoke.MethodType.methodType; +import java.lang.invoke.VarHandle; import java.lang.foreign.Arena; import java.lang.foreign.FunctionDescriptor; @@ -39,6 +40,7 @@ import java.lang.foreign.SegmentAllocator; import java.lang.foreign.SequenceLayout; import java.lang.foreign.SymbolLookup; +import java.lang.foreign.UnionLayout; import java.lang.foreign.ValueLayout; import static java.lang.foreign.Linker.*; @@ -274,6 +276,192 @@ public class UpcallMethodHandles { public static final MethodHandle MH_addNegShortsFromStruct; public static final MethodHandle MH_captureTrivialOption; + public static final MethodHandle MH_addBoolAndBoolsFromUnionWithXor; + public static final MethodHandle MH_addBoolAndBoolsFromUnionPtrWithXor; + public static final MethodHandle MH_addBoolAndBoolsFromNestedUnionWithXor; + public static final MethodHandle MH_addBoolAndBoolsFromNestedUnionWithXor_reverseOrder; + public static final MethodHandle MH_addBoolAndBoolsFromUnionWithNestedBoolArray; + public static final MethodHandle MH_addBoolAndBoolsFromUnionWithNestedBoolArray_reverseOrder; + public static final MethodHandle MH_addBoolAndBoolsFromUnionWithNestedUnionArray; + public static final MethodHandle MH_addBoolAndBoolsFromUnionWithNestedUnionArray_reverseOrder; + public static final MethodHandle MH_add2BoolUnionsWithXor_returnUnion; + public static final MethodHandle MH_add2BoolUnionsWithXor_returnUnionPtr; + public static final MethodHandle MH_add3BoolUnionsWithXor_returnUnion; + + public static final MethodHandle MH_addByteAndBytesFromUnion; + public static final MethodHandle MH_addByteAndBytesFromUnionPtr; + public static final MethodHandle MH_addByteAndBytesFromNestedUnion; + public static final MethodHandle MH_addByteAndBytesFromNestedUnion_reverseOrder; + public static final MethodHandle MH_addByteAndBytesFromUnionWithNestedByteArray; + public static final MethodHandle MH_addByteAndBytesFromUnionWithNestedByteArray_reverseOrder; + public static final MethodHandle MH_addByteAndBytesFromUnionWithNestedUnionArray; + public static final MethodHandle MH_addByteAndBytesFromUnionWithNestedUnionArray_reverseOrder; + public static final MethodHandle MH_add2ByteUnions_returnUnion; + public static final MethodHandle MH_add2ByteUnions_returnUnionPtr; + public static final MethodHandle MH_add3ByteUnions_returnUnion; + + public static final MethodHandle MH_addCharAndCharsFromUnion; + public static final MethodHandle MH_addCharAndCharsFromUnionPtr; + public static final MethodHandle MH_addCharAndCharsFromNestedUnion; + public static final MethodHandle MH_addCharAndCharsFromNestedUnion_reverseOrder; + public static final MethodHandle MH_addCharAndCharsFromUnionWithNestedCharArray; + public static final MethodHandle MH_addCharAndCharsFromUnionWithNestedCharArray_reverseOrder; + public static final MethodHandle MH_addCharAndCharsFromUnionWithNestedUnionArray; + public static final MethodHandle MH_addCharAndCharsFromUnionWithNestedUnionArray_reverseOrder; + public static final MethodHandle MH_add2CharUnions_returnUnion; + public static final MethodHandle MH_add2CharUnions_returnUnionPtr; + public static final MethodHandle MH_add3CharUnions_returnUnion; + + public static final MethodHandle MH_addShortAndShortsFromUnion; + public static final MethodHandle MH_addShortAndShortsFromUnionPtr; + public static final MethodHandle MH_addShortAndShortsFromNestedUnion; + public static final MethodHandle MH_addShortAndShortsFromNestedUnion_reverseOrder; + public static final MethodHandle MH_addShortAndShortsFromUnionWithNestedShortArray; + public static final MethodHandle MH_addShortAndShortsFromUnionWithNestedShortArray_reverseOrder; + public static final MethodHandle MH_addShortAndShortsFromUnionWithNestedUnionArray; + public static final MethodHandle MH_addShortAndShortsFromUnionWithNestedUnionArray_reverseOrder; + public static final MethodHandle MH_add2ShortUnions_returnUnion; + public static final MethodHandle MH_add2ShortUnions_returnUnionPtr; + public static final MethodHandle MH_add3ShortUnions_returnUnion; + + public static final MethodHandle MH_addIntAndIntsFromUnion; + public static final MethodHandle MH_addIntAndIntsFromUnionPtr; + public static final MethodHandle MH_addIntAndIntsFromNestedUnion; + public static final MethodHandle MH_addIntAndIntsFromNestedUnion_reverseOrder; + public static final MethodHandle MH_addIntAndIntsFromUnionWithNestedIntArray; + public static final MethodHandle MH_addIntAndIntsFromUnionWithNestedIntArray_reverseOrder; + + public static final MethodHandle MH_addIntAndIntsFromUnionWithNestedUnionArray; + public static final MethodHandle MH_addIntAndIntsFromUnionWithNestedUnionArray_reverseOrder; + public static final MethodHandle MH_add2IntUnions_returnUnion; + public static final MethodHandle MH_add2IntUnions_returnUnionPtr; + public static final MethodHandle MH_add3IntUnions_returnUnion; + + public static final MethodHandle MH_addLongAndLongsFromUnion; + public static final MethodHandle MH_addLongAndLongsFromUnionPtr; + public static final MethodHandle MH_addLongAndLongsFromNestedUnion; + public static final MethodHandle MH_addLongAndLongsFromNestedUnion_reverseOrder; + public static final MethodHandle MH_addLongAndLongsFromUnionWithNestedLongArray; + public static final MethodHandle MH_addLongAndLongsFromUnionWithNestedLongArray_reverseOrder; + public static final MethodHandle MH_addLongAndLongsFromUnionWithNestedUnionArray; + public static final MethodHandle MH_addLongAndLongsFromUnionWithNestedUnionArray_reverseOrder; + public static final MethodHandle MH_add2LongUnions_returnUnion; + public static final MethodHandle MH_add2LongUnions_returnUnionPtr; + public static final MethodHandle MH_add3LongUnions_returnUnion; + + public static final MethodHandle MH_addFloatAndFloatsFromUnion; + public static final MethodHandle MH_addFloatAndFloatsFromUnionPtr; + public static final MethodHandle MH_addFloatAndFloatsFromNestedUnion; + public static final MethodHandle MH_addFloatAndFloatsFromNestedUnion_reverseOrder; + public static final MethodHandle MH_addFloatAndFloatsFromUnionWithNestedFloatArray; + public static final MethodHandle MH_addFloatAndFloatsFromUnionWithNestedFloatArray_reverseOrder; + public static final MethodHandle MH_addFloatAndFloatsFromUnionWithNestedUnionArray; + public static final MethodHandle MH_addFloatAndFloatsFromUnionWithNestedUnionArray_reverseOrder; + public static final MethodHandle MH_add2FloatUnions_returnUnion; + public static final MethodHandle MH_add2FloatUnions_returnUnionPtr; + public static final MethodHandle MH_add3FloatUnions_returnUnion; + + public static final MethodHandle MH_addDoubleAndDoublesFromUnion; + public static final MethodHandle MH_addDoubleAndDoublesFromUnionPtr; + public static final MethodHandle MH_addDoubleAndDoublesFromNestedUnion; + public static final MethodHandle MH_addDoubleAndDoublesFromNestedUnion_reverseOrder; + public static final MethodHandle MH_addDoubleAndDoublesFromUnionWithNestedDoubleArray; + public static final MethodHandle MH_addDoubleAndDoublesFromUnionWithNestedDoubleArray_reverseOrder; + public static final MethodHandle MH_addDoubleAndDoublesFromUnionWithNestedUnionArray; + public static final MethodHandle MH_addDoubleAndDoublesFromUnionWithNestedUnionArray_reverseOrder; + public static final MethodHandle MH_add2DoubleUnions_returnUnion; + public static final MethodHandle MH_add2DoubleUnions_returnUnionPtr; + public static final MethodHandle MH_add3DoubleUnions_returnUnion; + + public static final MethodHandle MH_addShortAndShortFromUnionWithByteShort; + public static final MethodHandle MH_addIntAndByteFromUnionWithIntByte; + public static final MethodHandle MH_addIntAndIntFromUnionWithByteInt; + public static final MethodHandle MH_addIntAndShortFromUnionWithIntShort; + public static final MethodHandle MH_addIntAndIntFromUnionWithShortInt; + public static final MethodHandle MH_addIntAndByteFromUnionWithIntShortByte; + public static final MethodHandle MH_addIntAndShortFromUnionWithByteShortInt; + public static final MethodHandle MH_addIntAndLongFromUnionWithIntLong; + public static final MethodHandle MH_addIntAndLongFromUnionWithLongInt; + + public static final MethodHandle MH_addLongAndPtrValueFromUnion; + public static final MethodHandle MH_addLongAndIntFromUnionWithByteShortIntLong; + public static final MethodHandle MH_addLongAndIntFromUnionWithLongIntShortByte; + public static final MethodHandle MH_addLongAndLongFromUnionWithLongFloat; + public static final MethodHandle MH_addLongAndLongFromUnionWithIntFloatLong; + public static final MethodHandle MH_addLongAndLongFromUnionWithIntLongFloat; + + public static final MethodHandle MH_addFloatAndFloatFromUnionWithShortFloat; + public static final MethodHandle MH_addFloatAndFloatFromUnionWithFloatShort; + public static final MethodHandle MH_addFloatAndFloatFromUnionWithIntFloat; + public static final MethodHandle MH_addFloatAndFloatFromUnionWithFloatInt; + public static final MethodHandle MH_addFloatAndFloatFromUnionWithFloatLong; + + public static final MethodHandle MH_addDoubleAndPtrValueFromUnion; + public static final MethodHandle MH_addDoubleAndIntFromUnionWithIntDouble; + public static final MethodHandle MH_addDoubleAndLongFromUnionWithDoubleLong; + public static final MethodHandle MH_addDoubleAndLongFromUnionWithIntDoubleLong; + public static final MethodHandle MH_addDoubleAndFloatFromUnionWithFloatDouble; + public static final MethodHandle MH_addDoubleAndFloatFromUnionWithDoubleFloat; + public static final MethodHandle MH_addDoubleAndFloatFromUnionWithIntFloatDouble; + public static final MethodHandle MH_addDoubleAndFloatFromUnionWithIntDoubleFloat; + public static final MethodHandle MH_addDoubleAndDoubleFromUnionWithDoubleInt; + public static final MethodHandle MH_addDoubleAndDoubleFromUnionWithLongDouble; + public static final MethodHandle MH_addDoubleAndDoubleFromUnionWithFloatLongDouble; + public static final MethodHandle MH_addDoubleAndShortFromUnionWithByteShortFloatDouble; + public static final MethodHandle MH_addDoubleAndFloatFromUnionWithDoubleFloatShortByte; + + public static final MethodHandle MH_addBoolAndBoolsFromStructWithXor_Nested2BoolUnion; + public static final MethodHandle MH_addBoolAndBoolsFromUnionWithXor_Nested2BoolStruct; + public static final MethodHandle MH_addBoolAndBoolsFromUnionWithXor_Nested4BoolStruct; + public static final MethodHandle MH_add2BoolStructsWithXor_returnStruct_Nested2BoolUnion; + public static final MethodHandle MH_add2BoolUnionsWithXor_returnUnion_Nested2BoolStruct; + + public static final MethodHandle MH_addByteAndBytesFromStruct_Nested2ByteUnion; + public static final MethodHandle MH_addByteAndBytesFromUnion_Nested2ByteStruct; + public static final MethodHandle MH_addByteAndBytesFromUnion_Nested4ByteStruct; + public static final MethodHandle MH_add2ByteStructs_returnStruct_Nested2ByteUnion; + public static final MethodHandle MH_add2ByteUnions_returnUnion_Nested2ByteStruct; + + public static final MethodHandle MH_addCharAndCharsFromStruct_Nested2CharUnion; + public static final MethodHandle MH_addCharAndCharsFromUnion_Nested2CharStruct; + public static final MethodHandle MH_addCharAndCharsFromUnion_Nested4CharStruct; + public static final MethodHandle MH_add2CharStructs_returnStruct_Nested2CharUnion; + public static final MethodHandle MH_add2CharUnions_returnUnion_Nested2CharStruct; + + public static final MethodHandle MH_addShortAndShortsFromStruct_Nested2ShortUnion; + public static final MethodHandle MH_addShortAndShortsFromUnion_Nested2ShortStruct; + public static final MethodHandle MH_addShortAndShortsFromUnion_Nested4ShortStruct; + public static final MethodHandle MH_add2ShortStructs_returnStruct_Nested2ShortUnion; + public static final MethodHandle MH_add2ShortUnions_returnUnion_Nested2ShortStruct; + public static final MethodHandle MH_addShortAndShortByteFromUnion_Nested2ByteStruct; + public static final MethodHandle MH_addShortAndBytesFromUnion_Nested4ByteStruct; + + public static final MethodHandle MH_addIntAndIntsFromStruct_Nested2IntUnion; + public static final MethodHandle MH_addIntAndIntsFromUnion_Nested2IntStruct; + public static final MethodHandle MH_addIntAndIntsFromUnion_Nested4IntStruct; + public static final MethodHandle MH_add2IntStructs_returnStruct_Nested2IntUnion; + public static final MethodHandle MH_add2IntUnions_returnUnion_Nested2IntStruct; + public static final MethodHandle MH_addIntAndIntShortFromUnion_Nested2ShortStruct; + public static final MethodHandle MH_addIntAndShortsFromUnion_Nested4ShortStruct; + + public static final MethodHandle MH_addLongAndLongsFromStruct_Nested2LongUnion; + public static final MethodHandle MH_addLongAndLongsFromUnion_Nested2LongStruct; + public static final MethodHandle MH_addLongAndLongsFromUnion_Nested4LongStruct; + public static final MethodHandle MH_add2LongStructs_returnStruct_Nested2LongUnion; + public static final MethodHandle MH_add2LongUnions_returnUnion_Nested2LongStruct; + + public static final MethodHandle MH_addFloatAndFloatsFromStruct_Nested2FloatUnion; + public static final MethodHandle MH_addFloatAndFloatsFromUnion_Nested2FloatStruct; + public static final MethodHandle MH_addFloatAndFloatsFromUnion_Nested4FloatStruct; + public static final MethodHandle MH_add2FloatStructs_returnStruct_Nested2FloatUnion; + public static final MethodHandle MH_add2FloatUnions_returnUnion_Nested2FloatStruct; + + public static final MethodHandle MH_addDoubleAndDoublesFromStruct_Nested2DoubleUnion; + public static final MethodHandle MH_addDoubleAndDoublesFromUnion_Nested2DoubleStruct; + public static final MethodHandle MH_addDoubleAndDoublesFromUnion_Nested4DoubleStruct; + public static final MethodHandle MH_add2DoubleStructs_returnStruct_Nested2DoubleUnion; + public static final MethodHandle MH_add2DoubleUnions_returnUnion_Nested2DoubleStruct; + private static Linker linker = Linker.nativeLinker(); static { @@ -488,6 +676,191 @@ public class UpcallMethodHandles { MH_addNegShortsFromStruct = lookup.findStatic(UpcallMethodHandles.class, "addNegShortsFromStruct", MT_Short_Short_MemSegmt.appendParameterTypes(short.class, short.class)); //$NON-NLS-1$ MH_captureTrivialOption = lookup.findStatic(UpcallMethodHandles.class, "captureTrivialOption", methodType(int.class, int.class)); //$NON-NLS-1$ + MH_addBoolAndBoolsFromUnionWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromUnionWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_addBoolAndBoolsFromUnionPtrWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromUnionPtrWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_addBoolAndBoolsFromNestedUnionWithXor = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromNestedUnionWithXor", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_addBoolAndBoolsFromNestedUnionWithXor_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromNestedUnionWithXor_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_addBoolAndBoolsFromUnionWithNestedBoolArray = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromUnionWithNestedBoolArray", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_addBoolAndBoolsFromUnionWithNestedBoolArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromUnionWithNestedBoolArray_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_addBoolAndBoolsFromUnionWithNestedUnionArray = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromUnionWithNestedUnionArray", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_addBoolAndBoolsFromUnionWithNestedUnionArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromUnionWithNestedUnionArray_reverseOrder", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_add2BoolUnionsWithXor_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add2BoolUnionsWithXor_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2BoolUnionsWithXor_returnUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "add2BoolUnionsWithXor_returnUnionPtr", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add3BoolUnionsWithXor_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add3BoolUnionsWithXor_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addByteAndBytesFromUnion = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromUnion", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_addByteAndBytesFromUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromUnionPtr", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_addByteAndBytesFromNestedUnion = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromNestedUnion", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_addByteAndBytesFromNestedUnion_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromNestedUnion_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_addByteAndBytesFromUnionWithNestedByteArray = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromUnionWithNestedByteArray", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_addByteAndBytesFromUnionWithNestedByteArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromUnionWithNestedByteArray_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_addByteAndBytesFromUnionWithNestedUnionArray = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromUnionWithNestedUnionArray", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_addByteAndBytesFromUnionWithNestedUnionArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromUnionWithNestedUnionArray_reverseOrder", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_add2ByteUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add2ByteUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2ByteUnions_returnUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "add2ByteUnions_returnUnionPtr", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add3ByteUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add3ByteUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addCharAndCharsFromUnion = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromUnion", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_addCharAndCharsFromUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromUnionPtr", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_addCharAndCharsFromNestedUnion = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromNestedUnion", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_addCharAndCharsFromNestedUnion_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromNestedUnion_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_addCharAndCharsFromUnionWithNestedCharArray = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromUnionWithNestedCharArray", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_addCharAndCharsFromUnionWithNestedCharArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromUnionWithNestedCharArray_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_addCharAndCharsFromUnionWithNestedUnionArray = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromUnionWithNestedUnionArray", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_addCharAndCharsFromUnionWithNestedUnionArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromUnionWithNestedUnionArray_reverseOrder", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_add2CharUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add2CharUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2CharUnions_returnUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "add2CharUnions_returnUnionPtr", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add3CharUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add3CharUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addShortAndShortsFromUnion = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromUnion", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortsFromUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromUnionPtr", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortsFromNestedUnion = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromNestedUnion", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortsFromNestedUnion_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromNestedUnion_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortsFromUnionWithNestedShortArray = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromUnionWithNestedShortArray", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortsFromUnionWithNestedShortArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromUnionWithNestedShortArray_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortsFromUnionWithNestedUnionArray = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromUnionWithNestedUnionArray", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortsFromUnionWithNestedUnionArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromUnionWithNestedUnionArray_reverseOrder", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_add2ShortUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add2ShortUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2ShortUnions_returnUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "add2ShortUnions_returnUnionPtr", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add3ShortUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add3ShortUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addIntAndIntsFromUnion = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromUnion", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntsFromUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromUnionPtr", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntsFromNestedUnion = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromNestedUnion", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntsFromNestedUnion_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromNestedUnion_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntsFromUnionWithNestedIntArray = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromUnionWithNestedIntArray", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntsFromUnionWithNestedIntArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromUnionWithNestedIntArray_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntsFromUnionWithNestedUnionArray = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromUnionWithNestedUnionArray", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntsFromUnionWithNestedUnionArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromUnionWithNestedUnionArray_reverseOrder", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_add2IntUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add2IntUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2IntUnions_returnUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "add2IntUnions_returnUnionPtr", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add3IntUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add3IntUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addLongAndLongsFromUnion = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromUnion", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongsFromUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromUnionPtr", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongsFromNestedUnion = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromNestedUnion", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongsFromNestedUnion_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromNestedUnion_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongsFromUnionWithNestedLongArray = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromUnionWithNestedLongArray", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongsFromUnionWithNestedLongArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromUnionWithNestedLongArray_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongsFromUnionWithNestedUnionArray = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromUnionWithNestedUnionArray", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongsFromUnionWithNestedUnionArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromUnionWithNestedUnionArray_reverseOrder", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_add2LongUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add2LongUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2LongUnions_returnUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "add2LongUnions_returnUnionPtr", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add3LongUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add3LongUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addFloatAndFloatsFromUnion = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromUnion", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatsFromUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromUnionPtr", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatsFromNestedUnion = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromNestedUnion", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatsFromNestedUnion_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromNestedUnion_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatsFromUnionWithNestedFloatArray = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromUnionWithNestedFloatArray", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatsFromUnionWithNestedFloatArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromUnionWithNestedFloatArray_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatsFromUnionWithNestedUnionArray = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromUnionWithNestedUnionArray", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatsFromUnionWithNestedUnionArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromUnionWithNestedUnionArray_reverseOrder", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_add2FloatUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add2FloatUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2FloatUnions_returnUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "add2FloatUnions_returnUnionPtr", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add3FloatUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add3FloatUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addDoubleAndDoublesFromUnion = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromUnion", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoublesFromUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromUnionPtr", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoublesFromNestedUnion = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromNestedUnion", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoublesFromNestedUnion_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromNestedUnion_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoublesFromUnionWithNestedDoubleArray = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromUnionWithNestedDoubleArray", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoublesFromUnionWithNestedDoubleArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromUnionWithNestedDoubleArray_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoublesFromUnionWithNestedUnionArray = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromUnionWithNestedUnionArray", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoublesFromUnionWithNestedUnionArray_reverseOrder = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromUnionWithNestedUnionArray_reverseOrder", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_add2DoubleUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2DoubleUnions_returnUnionPtr = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleUnions_returnUnionPtr", MT_Segmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add3DoubleUnions_returnUnion = lookup.findStatic(UpcallMethodHandles.class, "add3DoubleUnions_returnUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addShortAndShortFromUnionWithByteShort = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortFromUnionWithByteShort", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addIntAndByteFromUnionWithIntByte = lookup.findStatic(UpcallMethodHandles.class, "addIntAndByteFromUnionWithIntByte", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntFromUnionWithByteInt = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromUnionWithByteInt", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndShortFromUnionWithIntShort = lookup.findStatic(UpcallMethodHandles.class, "addIntAndShortFromUnionWithIntShort", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntFromUnionWithShortInt = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntFromUnionWithShortInt", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndByteFromUnionWithIntShortByte = lookup.findStatic(UpcallMethodHandles.class, "addIntAndByteFromUnionWithIntShortByte", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndShortFromUnionWithByteShortInt = lookup.findStatic(UpcallMethodHandles.class, "addIntAndShortFromUnionWithByteShortInt", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndLongFromUnionWithIntLong = lookup.findStatic(UpcallMethodHandles.class, "addIntAndLongFromUnionWithIntLong", MT_Long_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndLongFromUnionWithLongInt = lookup.findStatic(UpcallMethodHandles.class, "addIntAndLongFromUnionWithLongInt", MT_Long_Int_MemSegmt); //$NON-NLS-1$ + + MH_addLongAndPtrValueFromUnion = lookup.findStatic(UpcallMethodHandles.class, "addLongAndPtrValueFromUnion", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndIntFromUnionWithByteShortIntLong = lookup.findStatic(UpcallMethodHandles.class, "addLongAndIntFromUnionWithByteShortIntLong", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndIntFromUnionWithLongIntShortByte = lookup.findStatic(UpcallMethodHandles.class, "addLongAndIntFromUnionWithLongIntShortByte", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongFromUnionWithLongFloat = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromUnionWithLongFloat", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongFromUnionWithIntFloatLong = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromUnionWithIntFloatLong", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongFromUnionWithIntLongFloat = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongFromUnionWithIntLongFloat", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + + MH_addFloatAndFloatFromUnionWithShortFloat = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromUnionWithShortFloat", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatFromUnionWithFloatShort = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromUnionWithFloatShort", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatFromUnionWithIntFloat = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromUnionWithIntFloat", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatFromUnionWithFloatInt = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromUnionWithFloatInt", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatFromUnionWithFloatLong = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatFromUnionWithFloatLong", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + + MH_addDoubleAndPtrValueFromUnion = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndPtrValueFromUnion", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndIntFromUnionWithIntDouble = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndIntFromUnionWithIntDouble", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndLongFromUnionWithDoubleLong = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndLongFromUnionWithDoubleLong", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndLongFromUnionWithIntDoubleLong = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndLongFromUnionWithIntDoubleLong", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndFloatFromUnionWithFloatDouble = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatFromUnionWithFloatDouble", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndFloatFromUnionWithDoubleFloat = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatFromUnionWithDoubleFloat", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndFloatFromUnionWithIntFloatDouble = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatFromUnionWithIntFloatDouble", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndFloatFromUnionWithIntDoubleFloat = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatFromUnionWithIntDoubleFloat", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoubleFromUnionWithDoubleInt = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromUnionWithDoubleInt", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoubleFromUnionWithLongDouble = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromUnionWithLongDouble", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoubleFromUnionWithFloatLongDouble = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoubleFromUnionWithFloatLongDouble", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndShortFromUnionWithByteShortFloatDouble = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndShortFromUnionWithByteShortFloatDouble", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndFloatFromUnionWithDoubleFloatShortByte = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndFloatFromUnionWithDoubleFloatShortByte", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + + MH_addBoolAndBoolsFromStructWithXor_Nested2BoolUnion = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromStructWithXor_Nested2BoolUnion", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_addBoolAndBoolsFromUnionWithXor_Nested2BoolStruct = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromUnionWithXor_Nested2BoolStruct", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_addBoolAndBoolsFromUnionWithXor_Nested4BoolStruct = lookup.findStatic(UpcallMethodHandles.class, "addBoolAndBoolsFromUnionWithXor_Nested4BoolStruct", MT_Bool_Bool_MemSegmt); //$NON-NLS-1$ + MH_add2BoolStructsWithXor_returnStruct_Nested2BoolUnion = lookup.findStatic(UpcallMethodHandles.class, "add2BoolStructsWithXor_returnStruct_Nested2BoolUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2BoolUnionsWithXor_returnUnion_Nested2BoolStruct = lookup.findStatic(UpcallMethodHandles.class, "add2BoolUnionsWithXor_returnUnion_Nested2BoolStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addByteAndBytesFromStruct_Nested2ByteUnion = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromStruct_Nested2ByteUnion", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_addByteAndBytesFromUnion_Nested2ByteStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromUnion_Nested2ByteStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_addByteAndBytesFromUnion_Nested4ByteStruct = lookup.findStatic(UpcallMethodHandles.class, "addByteAndBytesFromUnion_Nested4ByteStruct", MT_Byte_Byte_MemSegmt); //$NON-NLS-1$ + MH_add2ByteStructs_returnStruct_Nested2ByteUnion = lookup.findStatic(UpcallMethodHandles.class, "add2ByteStructs_returnStruct_Nested2ByteUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2ByteUnions_returnUnion_Nested2ByteStruct = lookup.findStatic(UpcallMethodHandles.class, "add2ByteUnions_returnUnion_Nested2ByteStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addCharAndCharsFromStruct_Nested2CharUnion = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromStruct_Nested2CharUnion", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_addCharAndCharsFromUnion_Nested2CharStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromUnion_Nested2CharStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_addCharAndCharsFromUnion_Nested4CharStruct = lookup.findStatic(UpcallMethodHandles.class, "addCharAndCharsFromUnion_Nested4CharStruct", MT_Char_Char_MemSegmt); //$NON-NLS-1$ + MH_add2CharStructs_returnStruct_Nested2CharUnion = lookup.findStatic(UpcallMethodHandles.class, "add2CharStructs_returnStruct_Nested2CharUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2CharUnions_returnUnion_Nested2CharStruct = lookup.findStatic(UpcallMethodHandles.class, "add2CharUnions_returnUnion_Nested2CharStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addShortAndShortsFromStruct_Nested2ShortUnion = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromStruct_Nested2ShortUnion", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortsFromUnion_Nested2ShortStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromUnion_Nested2ShortStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortsFromUnion_Nested4ShortStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortsFromUnion_Nested4ShortStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_add2ShortStructs_returnStruct_Nested2ShortUnion = lookup.findStatic(UpcallMethodHandles.class, "add2ShortStructs_returnStruct_Nested2ShortUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2ShortUnions_returnUnion_Nested2ShortStruct = lookup.findStatic(UpcallMethodHandles.class, "add2ShortUnions_returnUnion_Nested2ShortStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_addShortAndShortByteFromUnion_Nested2ByteStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndShortByteFromUnion_Nested2ByteStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + MH_addShortAndBytesFromUnion_Nested4ByteStruct = lookup.findStatic(UpcallMethodHandles.class, "addShortAndBytesFromUnion_Nested4ByteStruct", MT_Short_Short_MemSegmt); //$NON-NLS-1$ + + MH_addIntAndIntsFromStruct_Nested2IntUnion = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromStruct_Nested2IntUnion", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntsFromUnion_Nested2IntStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromUnion_Nested2IntStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntsFromUnion_Nested4IntStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntsFromUnion_Nested4IntStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_add2IntStructs_returnStruct_Nested2IntUnion = lookup.findStatic(UpcallMethodHandles.class, "add2IntStructs_returnStruct_Nested2IntUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2IntUnions_returnUnion_Nested2IntStruct = lookup.findStatic(UpcallMethodHandles.class, "add2IntUnions_returnUnion_Nested2IntStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_addIntAndIntShortFromUnion_Nested2ShortStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndIntShortFromUnion_Nested2ShortStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + MH_addIntAndShortsFromUnion_Nested4ShortStruct = lookup.findStatic(UpcallMethodHandles.class, "addIntAndShortsFromUnion_Nested4ShortStruct", MT_Int_Int_MemSegmt); //$NON-NLS-1$ + + MH_addLongAndLongsFromStruct_Nested2LongUnion = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromStruct_Nested2LongUnion", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongsFromUnion_Nested2LongStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromUnion_Nested2LongStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_addLongAndLongsFromUnion_Nested4LongStruct = lookup.findStatic(UpcallMethodHandles.class, "addLongAndLongsFromUnion_Nested4LongStruct", MT_Long_Long_MemSegmt); //$NON-NLS-1$ + MH_add2LongStructs_returnStruct_Nested2LongUnion = lookup.findStatic(UpcallMethodHandles.class, "add2LongStructs_returnStruct_Nested2LongUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2LongUnions_returnUnion_Nested2LongStruct = lookup.findStatic(UpcallMethodHandles.class, "add2LongUnions_returnUnion_Nested2LongStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addFloatAndFloatsFromStruct_Nested2FloatUnion = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromStruct_Nested2FloatUnion", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatsFromUnion_Nested2FloatStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromUnion_Nested2FloatStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_addFloatAndFloatsFromUnion_Nested4FloatStruct = lookup.findStatic(UpcallMethodHandles.class, "addFloatAndFloatsFromUnion_Nested4FloatStruct", MT_Float_Float_MemSegmt); //$NON-NLS-1$ + MH_add2FloatStructs_returnStruct_Nested2FloatUnion = lookup.findStatic(UpcallMethodHandles.class, "add2FloatStructs_returnStruct_Nested2FloatUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2FloatUnions_returnUnion_Nested2FloatStruct = lookup.findStatic(UpcallMethodHandles.class, "add2FloatUnions_returnUnion_Nested2FloatStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + + MH_addDoubleAndDoublesFromStruct_Nested2DoubleUnion = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromStruct_Nested2DoubleUnion", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoublesFromUnion_Nested2DoubleStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromUnion_Nested2DoubleStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_addDoubleAndDoublesFromUnion_Nested4DoubleStruct = lookup.findStatic(UpcallMethodHandles.class, "addDoubleAndDoublesFromUnion_Nested4DoubleStruct", MT_Double_Double_MemSegmt); //$NON-NLS-1$ + MH_add2DoubleStructs_returnStruct_Nested2DoubleUnion = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleStructs_returnStruct_Nested2DoubleUnion", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + MH_add2DoubleUnions_returnUnion_Nested2DoubleStruct = lookup.findStatic(UpcallMethodHandles.class, "add2DoubleUnions_returnUnion_Nested2DoubleStruct", MT_MemSegmt_MemSegmt_MemSegmt); //$NON-NLS-1$ + } catch (IllegalAccessException | NoSuchMethodException e) { throw new InternalError(e); } @@ -542,8 +915,7 @@ public static MemorySegment createNewCharFromCharAndCharFromPtr_RetPtr(MemorySeg int diff = (charArg2 >= charArg1) ? (charArg2 - charArg1) : (charArg1 - charArg2); diff = (diff > 5) ? 5 : diff; char result = (char)(diff + 'A'); - MemorySegment resultSegmt = arena.allocate(JAVA_CHAR); - resultSegmt.set(JAVA_CHAR, 0, result); + MemorySegment resultSegmt = arena.allocateFrom(JAVA_CHAR, result); return resultSegmt; } @@ -573,8 +945,7 @@ public static MemorySegment addByteAndByteFromPtr_RetPtr(byte byteArg1, MemorySe MemorySegment byteArg2Segmt = byteArg2Addr.reinterpret(JAVA_BYTE.byteSize()); byte byteArg2 = byteArg2Segmt.get(JAVA_BYTE, 0); byte byteSum = (byte)(byteArg1 + byteArg2); - MemorySegment resultSegmt = arena.allocate(JAVA_BYTE); - resultSegmt.set(JAVA_BYTE, 0, byteSum); + MemorySegment resultSegmt = arena.allocateFrom(JAVA_BYTE, byteSum); return resultSegmt; } @@ -602,8 +973,7 @@ public static MemorySegment addShortAndShortFromPtr_RetPtr(MemorySegment shortAr MemorySegment shortArg1Segmt = shortArg1Addr.reinterpret(JAVA_SHORT.byteSize()); short shortArg1 = shortArg1Segmt.get(JAVA_SHORT, 0); short shortSum = (short)(shortArg1 + shortArg2); - MemorySegment resultSegmt = arena.allocate(JAVA_SHORT); - resultSegmt.set(JAVA_SHORT, 0, shortSum); + MemorySegment resultSegmt = arena.allocateFrom(JAVA_SHORT, shortSum); return resultSegmt; } @@ -631,8 +1001,7 @@ public static MemorySegment addIntAndIntFromPtr_RetPtr(int intArg1, MemorySegmen MemorySegment intArg2Segmt = intArg2Addr.reinterpret(JAVA_INT.byteSize()); int intArg2 = intArg2Segmt.get(JAVA_INT, 0); int intSum = intArg1 + intArg2; - MemorySegment resultSegmt = arena.allocate(JAVA_INT); - resultSegmt.set(JAVA_INT, 0, intSum); + MemorySegment resultSegmt = arena.allocateFrom(JAVA_INT, intSum); return resultSegmt; } @@ -675,8 +1044,7 @@ public static MemorySegment addLongAndLongFromPtr_RetPtr(MemorySegment longArg1A MemorySegment longArg1Segmt = longArg1Addr.reinterpret(JAVA_LONG.byteSize()); long longArg1 = longArg1Segmt.get(JAVA_LONG, 0); long longSum = longArg1 + longArg2; - MemorySegment resultSegmt = arena.allocate(JAVA_LONG); - resultSegmt.set(JAVA_LONG, 0, longSum); + MemorySegment resultSegmt = arena.allocateFrom(JAVA_LONG, longSum); return resultSegmt; } @@ -704,8 +1072,7 @@ public static MemorySegment addFloatAndFloatFromPtr_RetPtr(float floatArg1, Memo MemorySegment floatArg2Segmt = floatArg2Addr.reinterpret(JAVA_FLOAT.byteSize()); float floatArg2 = floatArg2Segmt.get(JAVA_FLOAT, 0); float floatSum = floatArg1 + floatArg2; - MemorySegment resultSegmt = arena.allocate(JAVA_FLOAT); - resultSegmt.set(JAVA_FLOAT, 0, floatSum); + MemorySegment resultSegmt = arena.allocateFrom(JAVA_FLOAT, floatSum); return resultSegmt; } @@ -733,8 +1100,7 @@ public static MemorySegment addDoubleAndDoubleFromPtr_RetPtr(MemorySegment doubl MemorySegment doubleArg1Segmt = doubleArg1Addr.reinterpret(JAVA_DOUBLE.byteSize()); double doubleArg1 = doubleArg1Segmt.get(JAVA_DOUBLE, 0); double doubleSum = doubleArg1 + doubleArg2; - MemorySegment resultSegmt = arena.allocate(JAVA_DOUBLE); - resultSegmt.set(JAVA_DOUBLE, 0, doubleSum); + MemorySegment resultSegmt = arena.allocateFrom(JAVA_DOUBLE, doubleSum); return resultSegmt; } @@ -2070,4 +2436,1732 @@ public static int captureTrivialOption(int intArg1) { Assert.fail("The method shouldn't be invoked during the trivial downcall."); return intArg1; } + + public static boolean addBoolAndBoolsFromUnionWithXor(boolean arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + boolean boolSum = arg1 ^ (boolean)boolHandle1.get(arg2, 0L) ^ (boolean)boolHandle2.get(arg2, 0L); + return boolSum; + } + + public static boolean addBoolAndBoolsFromUnionPtrWithXor(boolean arg1, MemorySegment arg2Addr) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg2Segmt = arg2Addr.reinterpret(unionLayout.byteSize()); + boolean boolSum = arg1 ^ (boolean)boolHandle1.get(arg2Segmt, 0L) ^ (boolean)boolHandle2.get(arg2Segmt, 0L); + return boolSum; + } + + public static boolean addBoolAndBoolsFromNestedUnionWithXor(boolean arg1, MemorySegment arg2) { + boolean nestedUnionElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionElem2 = arg2.get(JAVA_BOOLEAN, 0); + boolean unionElem2 = arg2.get(JAVA_BOOLEAN, 0); + boolean boolSum = arg1 ^ nestedUnionElem1 ^ nestedUnionElem2 ^ unionElem2; + return boolSum; + } + + public static boolean addBoolAndBoolsFromNestedUnionWithXor_reverseOrder(boolean arg1, MemorySegment arg2) { + boolean unionElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionElem2 = arg2.get(JAVA_BOOLEAN, 0); + boolean boolSum = arg1 ^ unionElem1 ^ nestedUnionElem1 ^ nestedUnionElem2; + return boolSum; + } + + public static boolean addBoolAndBoolsFromUnionWithNestedBoolArray(boolean arg1, MemorySegment arg2) { + boolean nestedBoolArrayElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedBoolArrayElem2 = arg2.get(JAVA_BOOLEAN, 1); + boolean unionElem2 = arg2.get(JAVA_BOOLEAN, 0); + + boolean boolSum = arg1 ^ nestedBoolArrayElem1 ^ nestedBoolArrayElem2 ^ unionElem2; + return boolSum; + } + + public static boolean addBoolAndBoolsFromUnionWithNestedBoolArray_reverseOrder(boolean arg1, MemorySegment arg2) { + boolean unionElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedBoolArrayElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedBoolArrayElem2 = arg2.get(JAVA_BOOLEAN, 1); + + boolean boolSum = arg1 ^ unionElem1 ^ nestedBoolArrayElem1 ^ nestedBoolArrayElem2; + return boolSum; + } + + public static boolean addBoolAndBoolsFromUnionWithNestedUnionArray(boolean arg1, MemorySegment arg2) { + boolean nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_BOOLEAN, 1); + boolean nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_BOOLEAN, 1); + boolean unionElem2 = arg2.get(JAVA_BOOLEAN, 0); + + boolean boolSum = arg1 ^ unionElem2 + ^ nestedUnionArrayElem1_Elem1 ^ nestedUnionArrayElem1_Elem2 + ^ nestedUnionArrayElem2_Elem1 ^ nestedUnionArrayElem2_Elem2; + return boolSum; + } + + public static boolean addBoolAndBoolsFromUnionWithNestedUnionArray_reverseOrder(boolean arg1, MemorySegment arg2) { + boolean unionElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_BOOLEAN, 1); + boolean nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_BOOLEAN, 1); + + boolean boolSum = arg1 ^ unionElem1 + ^ nestedUnionArrayElem1_Elem1 ^ nestedUnionArrayElem1_Elem2 + ^ nestedUnionArrayElem2_Elem1 ^ nestedUnionArrayElem2_Elem2; + return boolSum; + } + + public static MemorySegment add2BoolUnionsWithXor_returnUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment boolUnionSegmt = arena.allocate(unionLayout); + boolean boolUnion_Elem1 = (boolean)boolHandle1.get(arg1, 0L) ^ (boolean)boolHandle1.get(arg2, 0L); + boolean boolUnion_Elem2 = (boolean)boolHandle2.get(arg1, 0L) ^ (boolean)boolHandle2.get(arg2, 0L); + boolHandle1.set(boolUnionSegmt, 0L, boolUnion_Elem1); + boolHandle2.set(boolUnionSegmt, 0L, boolUnion_Elem2); + return boolUnionSegmt; + } + + public static MemorySegment add2BoolUnionsWithXor_returnUnionPtr(MemorySegment arg1Addr, MemorySegment arg2) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg1Segmt = arg1Addr.reinterpret(unionLayout.byteSize()); + boolean boolUnion_Elem1 = (boolean)boolHandle1.get(arg1Segmt, 0L) ^ (boolean)boolHandle1.get(arg2, 0L); + boolHandle1.set(arg1Segmt, 0L, boolUnion_Elem1); + boolean boolUnion_Elem2 = (boolean)boolHandle2.get(arg1Segmt, 0L) ^ (boolean)boolHandle2.get(arg2, 0L); + boolHandle2.set(arg1Segmt, 0L, boolUnion_Elem2); + return arg1Addr; + } + + public static MemorySegment add3BoolUnionsWithXor_returnUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), + JAVA_BOOLEAN.withName("elem2"), JAVA_BOOLEAN.withName("elem3")); + VarHandle boolHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle boolHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle boolHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + MemorySegment boolUnionSegmt = arena.allocate(unionLayout); + boolean boolUnion_Elem1 = (boolean)boolHandle1.get(arg1, 0L) ^ (boolean)boolHandle1.get(arg2, 0L); + boolean boolUnion_Elem2 = (boolean)boolHandle2.get(arg1, 0L) ^ (boolean)boolHandle2.get(arg2, 0L); + boolean boolUnion_Elem3 = (boolean)boolHandle3.get(arg1, 0L) ^ (boolean)boolHandle3.get(arg2, 0L); + boolHandle1.set(boolUnionSegmt, 0L, boolUnion_Elem1); + boolHandle2.set(boolUnionSegmt, 0L, boolUnion_Elem2); + boolHandle3.set(boolUnionSegmt, 0L, boolUnion_Elem3); + return boolUnionSegmt; + } + + public static byte addByteAndBytesFromUnion(byte arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + byte byteSum = (byte)(arg1 + (byte)byteHandle1.get(arg2, 0L) + (byte)byteHandle2.get(arg2, 0L)); + return byteSum; + } + + public static byte addByteAndBytesFromUnionPtr(byte arg1, MemorySegment arg2Addr) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg2Segmt = arg2Addr.reinterpret(unionLayout.byteSize()); + byte byteSum = (byte)(arg1 + (byte)byteHandle1.get(arg2Segmt, 0L) + (byte)byteHandle2.get(arg2Segmt, 0L)); + return byteSum; + } + + public static byte addByteAndBytesFromNestedUnion(byte arg1, MemorySegment arg2) { + byte nestedUnionElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedUnionElem2 = arg2.get(JAVA_BYTE, 0); + byte unionElem2 = arg2.get(JAVA_BYTE, 0); + + byte byteSum = (byte)(arg1 + nestedUnionElem1 + nestedUnionElem2 + unionElem2); + return byteSum; + } + + public static byte addByteAndBytesFromNestedUnion_reverseOrder(byte arg1, MemorySegment arg2) { + byte unionElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedUnionElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedUnionElem2 = arg2.get(JAVA_BYTE, 0); + + byte byteSum = (byte)(arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2); + return byteSum; + } + + public static byte addByteAndBytesFromUnionWithNestedByteArray(byte arg1, MemorySegment arg2) { + byte nestedByteArrayElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedByteArrayElem2 = arg2.get(JAVA_BYTE, 1); + byte unionElem2 = arg2.get(JAVA_BYTE, 0); + + byte byteSum = (byte)(arg1 + nestedByteArrayElem1 + nestedByteArrayElem2 + unionElem2); + return byteSum; + } + + public static byte addByteAndBytesFromUnionWithNestedByteArray_reverseOrder(byte arg1, MemorySegment arg2) { + byte unionElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedByteArrayElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedByteArrayElem2 = arg2.get(JAVA_BYTE, 1); + + byte byteSum = (byte)(arg1 + unionElem1 + nestedByteArrayElem1 + nestedByteArrayElem2); + return byteSum; + } + + public static byte addByteAndBytesFromUnionWithNestedUnionArray(byte arg1, MemorySegment arg2) { + byte nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_BYTE, 0); + byte nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_BYTE, 0); + byte nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_BYTE, 1); + byte nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_BYTE, 1); + byte unionElem2 = arg2.get(JAVA_BYTE, 0); + + byte byteSum = (byte)(arg1 + unionElem2 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2); + return byteSum; + } + + public static byte addByteAndBytesFromUnionWithNestedUnionArray_reverseOrder(byte arg1, MemorySegment arg2) { + byte unionElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_BYTE, 0); + byte nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_BYTE, 0); + byte nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_BYTE, 1); + byte nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_BYTE, 1); + + byte byteSum = (byte)(arg1 + unionElem1 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2); + return byteSum; + } + + public static MemorySegment add2ByteUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + byte byteUnion_Elem1 = (byte)((byte)byteHandle1.get(arg1, 0L) + (byte)byteHandle1.get(arg2, 0L)); + byte byteUnion_Elem2 = (byte)((byte)byteHandle2.get(arg1, 0L) + (byte)byteHandle2.get(arg2, 0L)); + MemorySegment byteUnionSegmt = arena.allocate(unionLayout); + byteHandle1.set(byteUnionSegmt, 0L, byteUnion_Elem1); + byteHandle2.set(byteUnionSegmt, 0L, byteUnion_Elem2); + return byteUnionSegmt; + } + + public static MemorySegment add2ByteUnions_returnUnionPtr(MemorySegment arg1Addr, MemorySegment arg2) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg1Segmt = arg1Addr.reinterpret(unionLayout.byteSize()); + byte byteUnion_Elem1 = (byte)((byte)byteHandle1.get(arg1Segmt, 0L) + (byte)byteHandle1.get(arg2, 0L)); + byteHandle1.set(arg1Segmt, 0L, byteUnion_Elem1); + byte byteUnion_Elem2 = (byte)((byte)byteHandle2.get(arg1Segmt, 0L) + (byte)byteHandle2.get(arg2, 0L)); + byteHandle2.set(arg1Segmt, 0L, byteUnion_Elem2); + return arg1Addr; + } + + public static MemorySegment add3ByteUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_BYTE.withName("elem2"), JAVA_BYTE.withName("elem3")); + VarHandle byteHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle byteHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle byteHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + byte byteUnion_Elem1 = (byte)((byte)byteHandle1.get(arg1, 0L) + (byte)byteHandle1.get(arg2, 0L)); + byte byteUnion_Elem2 = (byte)((byte)byteHandle2.get(arg1, 0L) + (byte)byteHandle2.get(arg2, 0L)); + byte byteUnion_Elem3 = (byte)((byte)byteHandle3.get(arg1, 0L) + (byte)byteHandle3.get(arg2, 0L)); + + MemorySegment byteUnionSegmt = arena.allocate(unionLayout); + byteHandle1.set(byteUnionSegmt, 0L, byteUnion_Elem1); + byteHandle2.set(byteUnionSegmt, 0L, byteUnion_Elem2); + byteHandle3.set(byteUnionSegmt, 0L, byteUnion_Elem3); + return byteUnionSegmt; + } + + public static char addCharAndCharsFromUnion(char arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + char result = (char)(arg1 + (char)charHandle1.get(arg2, 0L) + (char)charHandle2.get(arg2, 0L) - 2 * 'A'); + return result; + } + + public static char addCharAndCharsFromUnionPtr(char arg1, MemorySegment arg2Addr) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg2Segmt = arg2Addr.reinterpret(unionLayout.byteSize()); + char result = (char)(arg1 + (char)charHandle1.get(arg2Segmt, 0L) + (char)charHandle2.get(arg2Segmt, 0L) - 2 * 'A'); + return result; + } + + public static char addCharAndCharsFromNestedUnion(char arg1, MemorySegment arg2) { + char nestedUnionElem1 = arg2.get(JAVA_CHAR, 0); + char nestedUnionElem2 = arg2.get(JAVA_CHAR, 0); + char unionElem2 = arg2.get(JAVA_CHAR, 0); + + char result = (char)(arg1 + nestedUnionElem1 + nestedUnionElem2 + unionElem2 - 3 * 'A'); + return result; + } + + public static char addCharAndCharsFromNestedUnion_reverseOrder(char arg1, MemorySegment arg2) { + char unionElem1 = arg2.get(JAVA_CHAR, 0); + char nestedUnionElem1 = arg2.get(JAVA_CHAR, 0); + char nestedUnionElem2 = arg2.get(JAVA_CHAR, 0); + + char result = (char)(arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2 - 3 * 'A'); + return result; + } + + public static char addCharAndCharsFromUnionWithNestedCharArray(char arg1, MemorySegment arg2) { + char nestedCharArrayElem1 = arg2.get(JAVA_CHAR, 0); + char nestedCharArrayElem2 = arg2.get(JAVA_CHAR, 2); + char unionElem2 = arg2.get(JAVA_CHAR, 0); + + char result = (char)(arg1 + nestedCharArrayElem1 + nestedCharArrayElem2 + unionElem2 - 3 * 'A'); + return result; + } + + public static char addCharAndCharsFromUnionWithNestedCharArray_reverseOrder(char arg1, MemorySegment arg2) { + char unionElem1 = arg2.get(JAVA_CHAR, 0); + char nestedCharArrayElem1 = arg2.get(JAVA_CHAR, 0); + char nestedCharArrayElem2 = arg2.get(JAVA_CHAR, 2); + + char result = (char)(arg1 + unionElem1 + nestedCharArrayElem1 + nestedCharArrayElem2 - 3 * 'A'); + return result; + } + + public static char addCharAndCharsFromUnionWithNestedUnionArray(char arg1, MemorySegment arg2) { + char nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_CHAR, 0); + char nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_CHAR, 0); + char nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_CHAR, 2); + char nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_CHAR, 2); + char unionElem2 = arg2.get(JAVA_CHAR, 0); + + char result = (char)(arg1 + unionElem2 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2 - 5 * 'A'); + return result; + } + + public static char addCharAndCharsFromUnionWithNestedUnionArray_reverseOrder(char arg1, MemorySegment arg2) { + char unionElem1 = arg2.get(JAVA_CHAR, 0); + char nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_CHAR, 0); + char nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_CHAR, 0); + char nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_CHAR, 2); + char nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_CHAR, 2); + + char result = (char)(arg1 + unionElem1 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2 - 5 * 'A'); + return result; + } + + public static MemorySegment add2CharUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment charUnionSegmt = arena.allocate(unionLayout); + char charUnion_Elem1 = (char)((char)charHandle1.get(arg1, 0L) + (char)charHandle1.get(arg2, 0L) - 'A'); + char charUnion_Elem2 = (char)((char)charHandle2.get(arg1, 0L) + (char)charHandle2.get(arg2, 0L) - 'A'); + charHandle1.set(charUnionSegmt, 0L, charUnion_Elem1); + charHandle2.set(charUnionSegmt, 0L, charUnion_Elem2); + return charUnionSegmt; + } + + public static MemorySegment add2CharUnions_returnUnionPtr(MemorySegment arg1Addr, MemorySegment arg2) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg1Segmt = arg1Addr.reinterpret(unionLayout.byteSize()); + char charUnion_Elem1 = (char)((char)charHandle1.get(arg1Segmt, 0L) + (char)charHandle1.get(arg2, 0L) - 'A'); + charHandle1.set(arg1Segmt, 0L, charUnion_Elem1); + char charUnion_Elem2 = (char)((char)charHandle2.get(arg1Segmt, 0L) + (char)charHandle2.get(arg2, 0L) - 'A'); + charHandle2.set(arg1Segmt, 0L, charUnion_Elem2); + return arg1Addr; + } + + public static MemorySegment add3CharUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), + JAVA_CHAR.withName("elem2"), JAVA_CHAR.withName("elem3")); + VarHandle charHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle charHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle charHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + MemorySegment charUnionSegmt = arena.allocate(unionLayout); + char charUnion_Elem1 = (char)((char)charHandle1.get(arg1, 0L) + (char)charHandle1.get(arg2, 0L) - 'A'); + char charUnion_Elem2 = (char)((char)charHandle2.get(arg1, 0L) + (char)charHandle2.get(arg2, 0L) - 'A'); + char charUnion_Elem3 = (char)((char)charHandle3.get(arg1, 0L) + (char)charHandle3.get(arg2, 0L) - 'A'); + charHandle1.set(charUnionSegmt, 0L, charUnion_Elem1); + charHandle2.set(charUnionSegmt, 0L, charUnion_Elem2); + charHandle3.set(charUnionSegmt, 0L, charUnion_Elem3); + return charUnionSegmt; + } + + public static short addShortAndShortsFromUnion(short arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + short shortSum = (short)(arg1 + (short)shortHandle1.get(arg2, 0L) + (short)shortHandle2.get(arg2, 0L)); + return shortSum; + } + + public static short addShortAndShortsFromUnionPtr(short arg1, MemorySegment arg2Addr) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg2Segmt = arg2Addr.reinterpret(unionLayout.byteSize()); + short shortSum = (short)(arg1 + (short)shortHandle1.get(arg2Segmt, 0L) + (short)shortHandle2.get(arg2Segmt, 0L)); + return shortSum; + } + + public static short addShortAndShortsFromNestedUnion(short arg1, MemorySegment arg2) { + short nestedUnionElem1 = arg2.get(JAVA_SHORT, 0); + short nestedUnionElem2 = arg2.get(JAVA_SHORT, 0); + short unionElem2 = arg2.get(JAVA_SHORT, 0); + + short shortSum = (short)(arg1 + nestedUnionElem1 + nestedUnionElem2 + unionElem2); + return shortSum; + } + + public static short addShortAndShortsFromNestedUnion_reverseOrder(short arg1, MemorySegment arg2) { + short unionElem1 = arg2.get(JAVA_SHORT, 0); + short nestedUnionElem1 = arg2.get(JAVA_SHORT, 0); + short nestedUnionElem2 = arg2.get(JAVA_SHORT, 0); + + short shortSum = (short)(arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2); + return shortSum; + } + + public static short addShortAndShortsFromUnionWithNestedShortArray(short arg1, MemorySegment arg2) { + short nestedShortArrayElem1 = arg2.get(JAVA_SHORT, 0); + short nestedShortArrayElem2 = arg2.get(JAVA_SHORT, 2); + short unionElem2 = arg2.get(JAVA_SHORT, 0); + + short shortSum = (short)(arg1 + nestedShortArrayElem1 + nestedShortArrayElem2 + unionElem2); + return shortSum; + } + + public static short addShortAndShortsFromUnionWithNestedShortArray_reverseOrder(short arg1, MemorySegment arg2) { + short unionElem1 = arg2.get(JAVA_SHORT, 0); + short nestedShortArrayElem1 = arg2.get(JAVA_SHORT, 0); + short nestedShortArrayElem2 = arg2.get(JAVA_SHORT, 2); + + short shortSum = (short)(arg1 + unionElem1 + nestedShortArrayElem1 + nestedShortArrayElem2); + return shortSum; + } + + public static short addShortAndShortsFromUnionWithNestedUnionArray(short arg1, MemorySegment arg2) { + short nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_SHORT, 0); + short nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_SHORT, 0); + short nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_SHORT, 2); + short nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_SHORT, 2); + short unionElem2 = arg2.get(JAVA_SHORT, 0); + + short shortSum = (short)(arg1 + unionElem2 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2); + return shortSum; + } + + public static short addShortAndShortsFromUnionWithNestedUnionArray_reverseOrder(short arg1, MemorySegment arg2) { + short unionElem1 = arg2.get(JAVA_SHORT, 0); + short nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_SHORT, 0); + short nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_SHORT, 0); + short nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_SHORT, 2); + short nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_SHORT, 2); + + short shortSum = (short)(arg1 + unionElem1 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2); + return shortSum; + } + + public static MemorySegment add2ShortUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment shortUnionSegmt = arena.allocate(unionLayout); + short shortUnion_Elem1 = (short)((short)shortHandle1.get(arg1, 0L) + (short)shortHandle1.get(arg2, 0L)); + short shortUnion_Elem2 = (short)((short)shortHandle2.get(arg1, 0L) + (short)shortHandle2.get(arg2, 0L)); + shortHandle1.set(shortUnionSegmt, 0L, shortUnion_Elem1); + shortHandle2.set(shortUnionSegmt, 0L, shortUnion_Elem2); + return shortUnionSegmt; + } + + public static MemorySegment add2ShortUnions_returnUnionPtr(MemorySegment arg1Addr, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg1Segmt = arg1Addr.reinterpret(unionLayout.byteSize()); + short shortUnion_Elem1 = (short)((short)shortHandle1.get(arg1Segmt, 0L) + (short)shortHandle1.get(arg2, 0L)); + shortHandle1.set(arg1Segmt, 0L, shortUnion_Elem1); + short shortUnion_Elem2 = (short)((short)shortHandle2.get(arg1Segmt, 0L) + (short)shortHandle2.get(arg2, 0L)); + shortHandle2.set(arg1Segmt, 0L, shortUnion_Elem2); + return arg1Addr; + } + + public static MemorySegment add3ShortUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_SHORT.withName("elem3")); + VarHandle shortHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle shortHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle shortHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + MemorySegment shortUnionSegmt = arena.allocate(unionLayout); + short shortUnion_Elem1 = (short)((short)shortHandle1.get(arg1, 0L) + (short)shortHandle1.get(arg2, 0L)); + short shortUnion_Elem2 = (short)((short)shortHandle2.get(arg1, 0L) + (short)shortHandle2.get(arg2, 0L)); + short shortUnion_Elem3 = (short)((short)shortHandle3.get(arg1, 0L) + (short)shortHandle3.get(arg2, 0L)); + shortHandle1.set(shortUnionSegmt, 0L, shortUnion_Elem1); + shortHandle2.set(shortUnionSegmt, 0L, shortUnion_Elem2); + shortHandle3.set(shortUnionSegmt, 0L, shortUnion_Elem3); + return shortUnionSegmt; + } + + public static int addIntAndIntsFromUnion(int arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + int intSum = arg1 + (int)intHandle1.get(arg2, 0L) + (int)intHandle2.get(arg2, 0L); + return intSum; + } + + public static int addIntAndIntsFromUnionPtr(int arg1, MemorySegment arg2Addr) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg2Segmt = arg2Addr.reinterpret(unionLayout.byteSize()); + int intSum = arg1 + (int)intHandle1.get(arg2Segmt, 0L) + (int)intHandle2.get(arg2Segmt, 0L); + return intSum; + } + + public static int addIntAndIntsFromNestedUnion(int arg1, MemorySegment arg2) { + int nestedUnionElem1 = arg2.get(JAVA_INT, 0); + int nestedUnionElem2 = arg2.get(JAVA_INT, 0); + int unionElem2 = arg2.get(JAVA_INT, 0); + + int intSum = arg1 + nestedUnionElem1 + nestedUnionElem2 + unionElem2; + return intSum; + } + + public static int addIntAndIntsFromNestedUnion_reverseOrder(int arg1, MemorySegment arg2) { + int unionElem1 = arg2.get(JAVA_INT, 0); + int nestedUnionElem1 = arg2.get(JAVA_INT, 0); + int nestedUnionElem2 = arg2.get(JAVA_INT, 0); + + int intSum = arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2; + return intSum; + } + + public static int addIntAndIntsFromUnionWithNestedIntArray(int arg1, MemorySegment arg2) { + int nestedIntArrayElem1 = arg2.get(JAVA_INT, 0); + int nestedIntArrayElem2 = arg2.get(JAVA_INT, 4); + int unionElem2 = arg2.get(JAVA_INT, 0); + + int intSum = arg1 + nestedIntArrayElem1 + nestedIntArrayElem2 + unionElem2; + return intSum; + } + + public static int addIntAndIntsFromUnionWithNestedIntArray_reverseOrder(int arg1, MemorySegment arg2) { + int unionElem1 = arg2.get(JAVA_INT, 0); + int nestedIntArrayElem1 = arg2.get(JAVA_INT, 0); + int nestedIntArrayElem2 = arg2.get(JAVA_INT, 4); + + int intSum = arg1 + unionElem1 + nestedIntArrayElem1 + nestedIntArrayElem2; + return intSum; + } + + public static int addIntAndIntsFromUnionWithNestedUnionArray(int arg1, MemorySegment arg2) { + int nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_INT, 0); + int nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_INT, 0); + int nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_INT, 4); + int nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_INT, 4); + int unionElem2 = arg2.get(JAVA_INT, 0); + + int intSum = arg1 + unionElem2 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2; + return intSum; + } + + public static int addIntAndIntsFromUnionWithNestedUnionArray_reverseOrder(int arg1, MemorySegment arg2) { + int unionElem1 = arg2.get(JAVA_INT, 0); + int nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_INT, 0); + int nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_INT, 0); + int nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_INT, 4); + int nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_INT, 4); + + int intSum = arg1 + unionElem1 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2; + return intSum; + } + + public static MemorySegment add2IntUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment intUnionSegmt = arena.allocate(unionLayout); + int intUnion_Elem1 = (int)intHandle1.get(arg1, 0L) + (int)intHandle1.get(arg2, 0L); + int intUnion_Elem2 = (int)intHandle2.get(arg1, 0L) + (int)intHandle2.get(arg2, 0L); + intHandle1.set(intUnionSegmt, 0L, intUnion_Elem1); + intHandle2.set(intUnionSegmt, 0L, intUnion_Elem2); + return intUnionSegmt; + } + + public static MemorySegment add2IntUnions_returnUnionPtr(MemorySegment arg1Addr, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg1Segmt = arg1Addr.reinterpret(unionLayout.byteSize()); + int intUnion_Elem1 = (int)intHandle1.get(arg1Segmt, 0L) + (int)intHandle1.get(arg2, 0L); + intHandle1.set(arg1Segmt, 0L, intUnion_Elem1); + int intUnion_Elem2 = (int)intHandle2.get(arg1Segmt, 0L) + (int)intHandle2.get(arg2, 0L); + intHandle2.set(arg1Segmt, 0L, intUnion_Elem2); + return arg1Addr; + } + + public static MemorySegment add3IntUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_INT.withName("elem2"), JAVA_INT.withName("elem3")); + VarHandle intHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle intHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle intHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + MemorySegment intUnionSegmt = arena.allocate(unionLayout); + int intUnion_Elem1 = (int)intHandle1.get(arg1, 0L) + (int)intHandle1.get(arg2, 0L); + int intUnion_Elem2 = (int)intHandle2.get(arg1, 0L) + (int)intHandle2.get(arg2, 0L); + int intUnion_Elem3 = (int)intHandle3.get(arg1, 0L) + (int)intHandle3.get(arg2, 0L); + intHandle1.set(intUnionSegmt, 0L, intUnion_Elem1); + intHandle2.set(intUnionSegmt, 0L, intUnion_Elem2); + intHandle3.set(intUnionSegmt, 0L, intUnion_Elem3); + return intUnionSegmt; + } + + public static long addLongAndLongsFromUnion(long arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + long longSum = arg1 + (long)longHandle1.get(arg2, 0L) + (long)longHandle2.get(arg2, 0L); + return longSum; + } + + public static long addLongAndLongsFromUnionPtr(long arg1, MemorySegment arg2Addr) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg2Segmt = arg2Addr.reinterpret(unionLayout.byteSize()); + long longSum = arg1 + (long)longHandle1.get(arg2Segmt, 0L) + (long)longHandle2.get(arg2Segmt, 0L); + return longSum; + } + + public static long addLongAndLongsFromNestedUnion(long arg1, MemorySegment arg2) { + long nestedUnionElem1 = arg2.get(JAVA_LONG, 0); + long nestedUnionElem2 = arg2.get(JAVA_LONG, 0); + long unionElem2 = arg2.get(JAVA_LONG, 0); + + long longSum = arg1 + nestedUnionElem1 + nestedUnionElem2 + unionElem2; + return longSum; + } + + public static long addLongAndLongsFromNestedUnion_reverseOrder(long arg1, MemorySegment arg2) { + long unionElem1 = arg2.get(JAVA_LONG, 0); + long nestedUnionElem1 = arg2.get(JAVA_LONG, 0); + long nestedUnionElem2 = arg2.get(JAVA_LONG, 0); + + long longSum = arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2; + return longSum; + } + + public static long addLongAndLongsFromUnionWithNestedLongArray(long arg1, MemorySegment arg2) { + long nestedLongrrayElem1 = arg2.get(JAVA_LONG, 0); + long nestedLongrrayElem2 = arg2.get(JAVA_LONG, 8); + long unionElem2 = arg2.get(JAVA_LONG, 0); + + long longSum = arg1 + nestedLongrrayElem1 + nestedLongrrayElem2 + unionElem2; + return longSum; + } + + public static long addLongAndLongsFromUnionWithNestedLongArray_reverseOrder(long arg1, MemorySegment arg2) { + long unionElem1 = arg2.get(JAVA_LONG, 0); + long nestedLongrrayElem1 = arg2.get(JAVA_LONG, 0); + long nestedLongrrayElem2 = arg2.get(JAVA_LONG, 8); + + long longSum = arg1 + unionElem1 + nestedLongrrayElem1 + nestedLongrrayElem2; + return longSum; + } + + public static long addLongAndLongsFromUnionWithNestedUnionArray(long arg1, MemorySegment arg2) { + long nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_LONG, 0); + long nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_LONG, 0); + long nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_LONG, 8); + long nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_LONG, 8); + long unionElem2 = arg2.get(JAVA_LONG, 0); + + long longSum = arg1 + unionElem2 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2; + return longSum; + } + + public static long addLongAndLongsFromUnionWithNestedUnionArray_reverseOrder(long arg1, MemorySegment arg2) { + long unionElem1 = arg2.get(JAVA_LONG, 0); + long nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_LONG, 0); + long nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_LONG, 0); + long nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_LONG, 8); + long nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_LONG, 8); + + long longSum = arg1 + unionElem1 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2; + return longSum; + } + + public static MemorySegment add2LongUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment longUnionSegmt = arena.allocate(unionLayout); + long longUnion_Elem1 = (long)longHandle1.get(arg1, 0L) + (long)longHandle1.get(arg2, 0L); + long longUnion_Elem2 = (long)longHandle2.get(arg1, 0L) + (long)longHandle2.get(arg2, 0L); + longHandle1.set(longUnionSegmt, 0L, longUnion_Elem1); + longHandle2.set(longUnionSegmt, 0L, longUnion_Elem2); + return longUnionSegmt; + } + + public static MemorySegment add2LongUnions_returnUnionPtr(MemorySegment arg1Addr, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg1Segmt = arg1Addr.reinterpret(unionLayout.byteSize()); + long longUnion_Elem1 = (long)longHandle1.get(arg1Segmt, 0L) + (long)longHandle1.get(arg2, 0L); + longHandle1.set(arg1Segmt, 0L, longUnion_Elem1); + long longUnion_Elem2 = (long)longHandle2.get(arg1Segmt, 0L) + (long)longHandle2.get(arg2, 0L); + longHandle2.set(arg1Segmt, 0L, longUnion_Elem2); + return arg1Addr; + } + + public static MemorySegment add3LongUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2"), JAVA_LONG.withName("elem3")); + VarHandle longHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle longHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle longHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + MemorySegment longUnionSegmt = arena.allocate(unionLayout); + long longUnion_Elem1 = (long)longHandle1.get(arg1, 0L) + (long)longHandle1.get(arg2, 0L); + long longUnion_Elem2 = (long)longHandle2.get(arg1, 0L) + (long)longHandle2.get(arg2, 0L); + long longUnion_Elem3 = (long)longHandle3.get(arg1, 0L) + (long)longHandle3.get(arg2, 0L); + longHandle1.set(longUnionSegmt, 0L, longUnion_Elem1); + longHandle2.set(longUnionSegmt, 0L, longUnion_Elem2); + longHandle3.set(longUnionSegmt, 0L, longUnion_Elem3); + return longUnionSegmt; + } + + public static float addFloatAndFloatsFromUnion(float arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + float floatSum = arg1 + (float)floatHandle1.get(arg2, 0L) + (float)floatHandle2.get(arg2, 0L); + return floatSum; + } + + public static float addFloatAndFloatsFromUnionPtr(float arg1, MemorySegment arg2Addr) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg2Segmt = arg2Addr.reinterpret(unionLayout.byteSize()); + float floatSum = arg1 + (float)floatHandle1.get(arg2Segmt, 0L) + (float)floatHandle2.get(arg2Segmt, 0L); + return floatSum; + } + + public static float addFloatAndFloatsFromNestedUnion(float arg1, MemorySegment arg2) { + float nestedUnionElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedUnionElem2 = arg2.get(JAVA_FLOAT, 0); + float unionElem2 = arg2.get(JAVA_FLOAT, 0); + + float floatSum = arg1 + nestedUnionElem1 + nestedUnionElem2 + unionElem2; + return floatSum; + } + + public static float addFloatAndFloatsFromNestedUnion_reverseOrder(float arg1, MemorySegment arg2) { + float unionElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedUnionElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedUnionElem2 = arg2.get(JAVA_FLOAT, 0); + + float floatSum = arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2; + return floatSum; + } + + public static float addFloatAndFloatsFromUnionWithNestedFloatArray(float arg1, MemorySegment arg2) { + float nestedFloatArrayElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedFloatArrayElem2 = arg2.get(JAVA_FLOAT, 4); + float unionElem2 = arg2.get(JAVA_FLOAT, 0); + + float floatSum = arg1 + nestedFloatArrayElem1 + nestedFloatArrayElem2 + unionElem2; + return floatSum; + } + + public static float addFloatAndFloatsFromUnionWithNestedFloatArray_reverseOrder(float arg1, MemorySegment arg2) { + float unionElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedFloatArrayElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedFloatArrayElem2 = arg2.get(JAVA_FLOAT, 4); + + float floatSum = arg1 + unionElem1 + nestedFloatArrayElem1 + nestedFloatArrayElem2; + return floatSum; + } + + public static float addFloatAndFloatsFromUnionWithNestedUnionArray(float arg1, MemorySegment arg2) { + float nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_FLOAT, 0); + float nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_FLOAT, 0); + float nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_FLOAT, 4); + float nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_FLOAT, 4); + float unionElem2 = arg2.get(JAVA_FLOAT, 0); + + float floatSum = arg1 + unionElem2 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2; + return floatSum; + } + + public static float addFloatAndFloatsFromUnionWithNestedUnionArray_reverseOrder(float arg1, MemorySegment arg2) { + float unionElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_FLOAT, 0); + float nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_FLOAT, 0); + float nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_FLOAT, 4); + float nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_FLOAT, 4); + + float floatSum = arg1 + unionElem1 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2; + return floatSum; + } + + public static MemorySegment add2FloatUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment floatUnionSegmt = arena.allocate(unionLayout); + float floatUnion_Elem1 = (float)floatHandle1.get(arg1, 0L) + (float)floatHandle1.get(arg2, 0L); + float floatUnion_Elem2 = (float)floatHandle2.get(arg1, 0L) + (float)floatHandle2.get(arg2, 0L); + floatHandle1.set(floatUnionSegmt, 0L, floatUnion_Elem1); + floatHandle2.set(floatUnionSegmt, 0L, floatUnion_Elem2); + return floatUnionSegmt; + } + + public static MemorySegment add2FloatUnions_returnUnionPtr(MemorySegment arg1Addr, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg1Segmt = arg1Addr.reinterpret(unionLayout.byteSize()); + float floatUnion_Elem1 = (float)floatHandle1.get(arg1Segmt, 0L) + (float)floatHandle1.get(arg2, 0L); + floatHandle1.set(arg1Segmt, 0L, floatUnion_Elem1); + float floatUnion_Elem2 = (float)floatHandle2.get(arg1Segmt, 0L) + (float)floatHandle2.get(arg2, 0L); + floatHandle2.set(arg1Segmt, 0L, floatUnion_Elem2); + return arg1Addr; + } + + public static MemorySegment add3FloatUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2"), JAVA_FLOAT.withName("elem3")); + VarHandle floatHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle floatHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle floatHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + MemorySegment floatUnionSegmt = arena.allocate(unionLayout); + float floatUnion_Elem1 = (float)floatHandle1.get(arg1, 0L) + (float)floatHandle1.get(arg2, 0L); + float floatUnion_Elem2 = (float)floatHandle2.get(arg1, 0L) + (float)floatHandle2.get(arg2, 0L); + float floatUnion_Elem3 = (float)floatHandle3.get(arg1, 0L) + (float)floatHandle3.get(arg2, 0L); + floatHandle1.set(floatUnionSegmt, 0L, floatUnion_Elem1); + floatHandle2.set(floatUnionSegmt, 0L, floatUnion_Elem2); + floatHandle3.set(floatUnionSegmt, 0L, floatUnion_Elem3); + return floatUnionSegmt; + } + + public static double addDoubleAndDoublesFromUnion(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + double doubleSum = arg1 + (double)doubleHandle1.get(arg2, 0L) + (double)doubleHandle2.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndDoublesFromUnionPtr(double arg1, MemorySegment arg2Addr) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg2Segmt = arg2Addr.reinterpret(unionLayout.byteSize()); + double doubleSum = arg1 + (double)doubleHandle1.get(arg2Segmt, 0L) + (double)doubleHandle2.get(arg2Segmt, 0L); + return doubleSum; + } + + public static double addDoubleAndDoublesFromNestedUnion(double arg1, MemorySegment arg2) { + double nestedUnionElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedUnionElem2 = arg2.get(JAVA_DOUBLE, 0); + double unionElem2 = arg2.get(JAVA_DOUBLE, 0); + + double doubleSum = arg1 + nestedUnionElem1 + nestedUnionElem2 + unionElem2; + return doubleSum; + } + + public static double addDoubleAndDoublesFromNestedUnion_reverseOrder(double arg1, MemorySegment arg2) { + double unionElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedUnionElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedUnionElem2 = arg2.get(JAVA_DOUBLE, 0); + + double doubleSum = arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2; + return doubleSum; + } + + public static double addDoubleAndDoublesFromUnionWithNestedDoubleArray(double arg1, MemorySegment arg2) { + double nestedDoubleArrayElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedDoubleArrayElem2 = arg2.get(JAVA_DOUBLE, 8); + double unionElem2 = arg2.get(JAVA_DOUBLE, 0); + + double doubleSum = arg1 + nestedDoubleArrayElem1 + nestedDoubleArrayElem2 + unionElem2; + return doubleSum; + } + + public static double addDoubleAndDoublesFromUnionWithNestedDoubleArray_reverseOrder(double arg1, MemorySegment arg2) { + double unionElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedDoubleArrayElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedDoubleArrayElem2 = arg2.get(JAVA_DOUBLE, 8); + + double doubleSum = arg1 + unionElem1 + nestedDoubleArrayElem1 + nestedDoubleArrayElem2; + return doubleSum; + } + + public static double addDoubleAndDoublesFromUnionWithNestedUnionArray(double arg1, MemorySegment arg2) { + double nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_DOUBLE, 0); + double nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_DOUBLE, 8); + double nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_DOUBLE, 8); + double unionElem2 = arg2.get(JAVA_DOUBLE, 0); + + double doubleSum = arg1 + unionElem2 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2; + return doubleSum; + } + + public static double addDoubleAndDoublesFromUnionWithNestedUnionArray_reverseOrder(double arg1, MemorySegment arg2) { + double unionElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedUnionArrayElem1_Elem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedUnionArrayElem1_Elem2 = arg2.get(JAVA_DOUBLE, 0); + double nestedUnionArrayElem2_Elem1 = arg2.get(JAVA_DOUBLE, 8); + double nestedUnionArrayElem2_Elem2 = arg2.get(JAVA_DOUBLE, 8); + + double doubleSum = arg1 + unionElem1 + + nestedUnionArrayElem1_Elem1 + nestedUnionArrayElem1_Elem2 + + nestedUnionArrayElem2_Elem1 + nestedUnionArrayElem2_Elem2; + return doubleSum; + } + + public static MemorySegment add2DoubleUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment doubleUnionSegmt = arena.allocate(unionLayout); + double doubleUnion_Elem1 = (double)doubleHandle1.get(arg1, 0L) + (double)doubleHandle1.get(arg2, 0L); + double doubleUnion_Elem2 = (double)doubleHandle2.get(arg1, 0L) + (double)doubleHandle2.get(arg2, 0L); + doubleHandle1.set(doubleUnionSegmt, 0L, doubleUnion_Elem1); + doubleHandle2.set(doubleUnionSegmt, 0L, doubleUnion_Elem2); + return doubleUnionSegmt; + } + + public static MemorySegment add2DoubleUnions_returnUnionPtr(MemorySegment arg1Addr, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + MemorySegment arg1Segmt = arg1Addr.reinterpret(unionLayout.byteSize()); + double doubleUnion_Elem1 = (double)doubleHandle1.get(arg1Segmt, 0L) + (double)doubleHandle1.get(arg2, 0L); + doubleHandle1.set(arg1Segmt, 0L, doubleUnion_Elem1); + double doubleUnion_Elem2 = (double)doubleHandle2.get(arg1Segmt, 0L) + (double)doubleHandle2.get(arg2, 0L); + doubleHandle2.set(arg1Segmt, 0L, doubleUnion_Elem2); + return arg1Addr; + } + + public static MemorySegment add3DoubleUnions_returnUnion(MemorySegment arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2"), JAVA_DOUBLE.withName("elem3")); + VarHandle doubleHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + VarHandle doubleHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + VarHandle doubleHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + MemorySegment doubleUnionSegmt = arena.allocate(unionLayout); + double doubleUnion_Elem1 = (double)doubleHandle1.get(arg1, 0L) + (double)doubleHandle1.get(arg2, 0L); + double doubleUnion_Elem2 = (double)doubleHandle2.get(arg1, 0L) + (double)doubleHandle2.get(arg2, 0L); + double doubleUnion_Elem3 = (double)doubleHandle3.get(arg1, 0L) + (double)doubleHandle3.get(arg2, 0L); + doubleHandle1.set(doubleUnionSegmt, 0L, doubleUnion_Elem1); + doubleHandle2.set(doubleUnionSegmt, 0L, doubleUnion_Elem2); + doubleHandle3.set(doubleUnionSegmt, 0L, doubleUnion_Elem3); + return doubleUnionSegmt; + } + + public static short addShortAndShortFromUnionWithByteShort(short arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + short shortSum = (short)(arg1 + (short)elemHandle2.get(arg2, 0L)); + return shortSum; + } + + public static int addIntAndByteFromUnionWithIntByte(int arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_BYTE.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + int intSum = arg1 + (byte)elemHandle2.get(arg2, 0L); + return intSum; + } + + public static int addIntAndIntFromUnionWithByteInt(int arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + int intSum = arg1 + (int)elemHandle2.get(arg2, 0L); + return intSum; + } + + public static int addIntAndShortFromUnionWithIntShort(int arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + int intSum = arg1 + (short)elemHandle2.get(arg2, 0L); + return intSum; + } + + public static int addIntAndIntFromUnionWithShortInt(int arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + int intSum = arg1 + (int)elemHandle2.get(arg2, 0L); + return intSum; + } + + public static int addIntAndByteFromUnionWithIntShortByte(int arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_BYTE.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + int intSum = arg1 + (byte)elemHandle3.get(arg2, 0L); + return intSum; + } + + public static int addIntAndShortFromUnionWithByteShortInt(int arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_INT.withName("elem3")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + int intSum = arg1 + (short)elemHandle2.get(arg2, 0L); + return intSum; + } + + public static long addIntAndLongFromUnionWithIntLong(int arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + long longSum = arg1 + (long)elemHandle2.get(arg2, 0L); + return longSum; + } + + public static long addIntAndLongFromUnionWithLongInt(int arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + long longSum = arg1 + (long)elemHandle1.get(arg2, 0L); + return longSum; + } + + public static long addLongAndPtrValueFromUnion(long arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), ADDRESS.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + MemorySegment elemSegmt = ((MemorySegment)elemHandle3.get(arg2, 0L)).reinterpret(JAVA_LONG.byteSize()); + long longSum = arg1 + elemSegmt.get(JAVA_LONG, 0); + return longSum; + } + + public static long addLongAndIntFromUnionWithByteShortIntLong(long arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_INT.withName("elem3"), JAVA_LONG.withName("elem4")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + long longSum = arg1 + (int)elemHandle3.get(arg2, 0L); + return longSum; + } + + public static long addLongAndIntFromUnionWithLongIntShortByte(long arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), + JAVA_INT.withName("elem2"), JAVA_SHORT.withName("elem3"), JAVA_BYTE.withName("elem4")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + long longSum = arg1 + (int)elemHandle2.get(arg2, 0L); + return longSum; + } + + public static long addLongAndLongFromUnionWithLongFloat(long arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + long longSum = arg1 + (long)elemHandle1.get(arg2, 0L); + return longSum; + } + + public static long addLongAndLongFromUnionWithIntFloatLong(long arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_LONG.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + long longSum = arg1 + (long)elemHandle3.get(arg2, 0L); + return longSum; + } + + public static long addLongAndLongFromUnionWithIntLongFloat(long arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_LONG.withName("elem2"), JAVA_FLOAT.withName("elem3")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + long longSum = arg1 + (long)elemHandle2.get(arg2, 0L); + return longSum; + } + + public static float addFloatAndFloatFromUnionWithShortFloat(float arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + float floatSum = arg1 + (float)elemHandle2.get(arg2, 0L); + return floatSum; + } + + public static float addFloatAndFloatFromUnionWithFloatShort(float arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_SHORT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + float floatSum = arg1 + (float)elemHandle1.get(arg2, 0L); + return floatSum; + } + + public static float addFloatAndFloatFromUnionWithIntFloat(float arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + float floatSum = arg1 + (float)elemHandle2.get(arg2, 0L); + return floatSum; + } + + public static float addFloatAndFloatFromUnionWithFloatInt(float arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + float floatSum = arg1 + (float)elemHandle1.get(arg2, 0L); + return floatSum; + } + + public static float addFloatAndFloatFromUnionWithFloatLong(float arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + float floatSum = arg1 + (float)elemHandle1.get(arg2, 0L); + return floatSum; + } + + public static double addDoubleAndPtrValueFromUnion(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), ADDRESS.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + MemorySegment elemSegmt = ((MemorySegment)elemHandle3.get(arg2, 0L)).reinterpret(JAVA_DOUBLE.byteSize()); + double doubleSum = arg1 + elemSegmt.get(JAVA_LONG, 0); + return doubleSum; + } + + public static double addDoubleAndIntFromUnionWithIntDouble(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + double doubleSum = arg1 + (int)elemHandle1.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndLongFromUnionWithDoubleLong(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_LONG.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + double doubleSum = arg1 + (long)elemHandle2.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndLongFromUnionWithIntDoubleLong(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), JAVA_LONG.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + double doubleSum = arg1 + (long)elemHandle3.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndFloatFromUnionWithFloatDouble(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + double doubleSum = arg1 + (float)elemHandle1.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndFloatFromUnionWithDoubleFloat(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_FLOAT.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + double doubleSum = arg1 + (float)elemHandle2.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndFloatFromUnionWithIntFloatDouble(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_DOUBLE.withName("elem3")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + double doubleSum = arg1 + (float)elemHandle2.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndFloatFromUnionWithIntDoubleFloat(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), + JAVA_DOUBLE.withName("elem2"), JAVA_FLOAT.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + double doubleSum = arg1 + (float)elemHandle3.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndDoubleFromUnionWithDoubleInt(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_INT.withName("elem2")); + VarHandle elemHandle1 = unionLayout.varHandle(PathElement.groupElement("elem1")); + + double doubleSum = arg1 + (double)elemHandle1.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndDoubleFromUnionWithLongDouble(double arg1, MemorySegment arg2) { + UnionLayout unionLayout =MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + double doubleSum = arg1 + (double)elemHandle2.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndShortFromUnionWithByteShortFloatDouble(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), + JAVA_SHORT.withName("elem2"), JAVA_FLOAT.withName("elem3"), JAVA_DOUBLE.withName("elem4")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + double doubleSum = arg1 + (short)elemHandle2.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndDoubleFromUnionWithFloatLongDouble(double arg1, MemorySegment arg2) { + UnionLayout unionLayout =MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), + JAVA_LONG.withName("elem2"), JAVA_DOUBLE.withName("elem3")); + VarHandle elemHandle3 = unionLayout.varHandle(PathElement.groupElement("elem3")); + + double doubleSum = arg1 + (double)elemHandle3.get(arg2, 0L); + return doubleSum; + } + + public static double addDoubleAndFloatFromUnionWithDoubleFloatShortByte(double arg1, MemorySegment arg2) { + UnionLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), + JAVA_FLOAT.withName("elem2"), JAVA_SHORT.withName("elem3"), JAVA_BYTE.withName("elem4")); + VarHandle elemHandle2 = unionLayout.varHandle(PathElement.groupElement("elem2")); + + double doubleSum = arg1 + (float)elemHandle2.get(arg2, 0L); + return doubleSum; + } + + public static boolean addBoolAndBoolsFromStructWithXor_Nested2BoolUnion(boolean arg1, MemorySegment arg2) { + boolean unionElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionElem1 = arg2.get(JAVA_BOOLEAN, 1); + boolean nestedUnionElem2 = arg2.get(JAVA_BOOLEAN, 1); + boolean boolSum = arg1 ^ unionElem1 ^ nestedUnionElem1 ^ nestedUnionElem2; + return boolSum; + } + + public static boolean addBoolAndBoolsFromUnionWithXor_Nested2BoolStruct(boolean arg1, MemorySegment arg2) { + boolean unionElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedStructElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedStructElem2 = arg2.get(JAVA_BOOLEAN, 1); + boolean boolSum = arg1 ^ unionElem1 ^ nestedStructElem1 ^ nestedStructElem2; + return boolSum; + } + + public static boolean addBoolAndBoolsFromUnionWithXor_Nested4BoolStruct(boolean arg1, MemorySegment arg2) { + boolean unionElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedStructElem1 = arg2.get(JAVA_BOOLEAN, 0); + boolean nestedStructElem2 = arg2.get(JAVA_BOOLEAN, 1); + boolean nestedStructElem3 = arg2.get(JAVA_BOOLEAN, 2); + boolean nestedStructElem4 = arg2.get(JAVA_BOOLEAN, 3); + boolean boolSum = arg1 ^ unionElem1 ^ nestedStructElem1 + ^ nestedStructElem2 ^ nestedStructElem3 ^ nestedStructElem4; + return boolSum; + } + + public static MemorySegment add2BoolStructsWithXor_returnStruct_Nested2BoolUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + MemorySegment boolStructSegmt = arena.allocate(structLayout); + boolean structElem1 = arg1.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); + boolean nestedUnionElem1 = arg1.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); + boolean nestedUnionElem2 = arg1.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); + boolStructSegmt.set(JAVA_BOOLEAN, 0, structElem1); + boolStructSegmt.set(JAVA_BOOLEAN, 1, nestedUnionElem2); + return boolStructSegmt; + } + + public static MemorySegment add2BoolUnionsWithXor_returnUnion_Nested2BoolStruct(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BOOLEAN.withName("elem1"), JAVA_BOOLEAN.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BOOLEAN.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + + MemorySegment boolUnionSegmt = arena.allocate(unionLayout); + boolean unionElem1 = arg1.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); + boolean nestedStructElem1 = arg1.get(JAVA_BOOLEAN, 0) ^ arg2.get(JAVA_BOOLEAN, 0); + boolean nestedStructElem2 = arg1.get(JAVA_BOOLEAN, 1) ^ arg2.get(JAVA_BOOLEAN, 1); + boolUnionSegmt.set(JAVA_BOOLEAN, 0, unionElem1); + boolUnionSegmt.set(JAVA_BOOLEAN, 1, nestedStructElem2); + return boolUnionSegmt; + } + + public static byte addByteAndBytesFromStruct_Nested2ByteUnion(byte arg1, MemorySegment arg2) { + byte unionElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedUnionElem1 = arg2.get(JAVA_BYTE, 1); + byte nestedUnionElem2 = arg2.get(JAVA_BYTE, 1); + byte byteSum = (byte)(arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2); + return byteSum; + } + + public static byte addByteAndBytesFromUnion_Nested2ByteStruct(byte arg1, MemorySegment arg2) { + byte unionElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedStructElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedStructElem2 = arg2.get(JAVA_BYTE, 1); + byte byteSum = (byte)(arg1 + unionElem1 + nestedStructElem1 + nestedStructElem2); + return byteSum; + } + + public static byte addByteAndBytesFromUnion_Nested4ByteStruct(byte arg1, MemorySegment arg2) { + byte unionElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedStructElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedStructElem2 = arg2.get(JAVA_BYTE, 1); + byte nestedStructElem3 = arg2.get(JAVA_BYTE, 2); + byte nestedStructElem4 = arg2.get(JAVA_BYTE, 3); + byte byteSum = (byte)(arg1 + unionElem1 + nestedStructElem1 + + nestedStructElem2 + nestedStructElem3 + nestedStructElem4); + return byteSum; + } + + public static MemorySegment add2ByteStructs_returnStruct_Nested2ByteUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + MemorySegment byteStructSegmt = arena.allocate(structLayout); + byte structElem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); + byte nestedUnionElem1 = (byte)(arg1.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); + byte nestedUnionElem2 = (byte)(arg1.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); + byteStructSegmt.set(JAVA_BYTE, 0, structElem1); + byteStructSegmt.set(JAVA_BYTE, 1, nestedUnionElem2); + return byteStructSegmt; + } + + public static MemorySegment add2ByteUnions_returnUnion_Nested2ByteStruct(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_BYTE.withName("elem1"), JAVA_BYTE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_BYTE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + + MemorySegment byteUnionSegmt = arena.allocate(unionLayout); + byte unionElem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); + byte nestedStructElem1 = (byte)(arg1.get(JAVA_BYTE, 0) + arg2.get(JAVA_BYTE, 0)); + byte nestedStructElem2 = (byte)(arg1.get(JAVA_BYTE, 1) + arg2.get(JAVA_BYTE, 1)); + byteUnionSegmt.set(JAVA_BYTE, 0, unionElem1); + byteUnionSegmt.set(JAVA_BYTE, 1, nestedStructElem2); + return byteUnionSegmt; + } + + public static char addCharAndCharsFromStruct_Nested2CharUnion(char arg1, MemorySegment arg2) { + char unionElem1 = arg2.get(JAVA_CHAR, 0); + char nestedUnionElem1 = arg2.get(JAVA_CHAR, 2); + char nestedUnionElem2 = arg2.get(JAVA_CHAR, 2); + char charSum = (char)(arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2 - 3 * 'A'); + return charSum; + } + + public static char addCharAndCharsFromUnion_Nested2CharStruct(char arg1, MemorySegment arg2) { + char unionElem1 = arg2.get(JAVA_CHAR, 0); + char nestedStructElem1 = arg2.get(JAVA_CHAR, 0); + char nestedStructElem2 = arg2.get(JAVA_CHAR, 2); + char charSum = (char)(arg1 + unionElem1 + nestedStructElem1 + nestedStructElem2 - 3 * 'A'); + return charSum; + } + + public static char addCharAndCharsFromUnion_Nested4CharStruct(char arg1, MemorySegment arg2) { + char unionElem1 = arg2.get(JAVA_CHAR, 0); + char nestedStructElem1 = arg2.get(JAVA_CHAR, 0); + char nestedStructElem2 = arg2.get(JAVA_CHAR, 2); + char nestedStructElem3 = arg2.get(JAVA_CHAR, 4); + char nestedStructElem4 = arg2.get(JAVA_CHAR, 6); + char charSum = (char)(arg1 + unionElem1 + nestedStructElem1 + + nestedStructElem2 + nestedStructElem3 + nestedStructElem4 - 5 * 'A'); + return charSum; + } + + public static MemorySegment add2CharStructs_returnStruct_Nested2CharUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + MemorySegment charStructSegmt = arena.allocate(structLayout); + char structElem1 = (char)(arg1.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); + char nestedUnionElem1 = (char)(arg1.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); + char nestedUnionElem2 = (char)(arg1.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); + charStructSegmt.set(JAVA_CHAR, 0, structElem1); + charStructSegmt.set(JAVA_CHAR, 2, nestedUnionElem2); + return charStructSegmt; + } + + public static MemorySegment add2CharUnions_returnUnion_Nested2CharStruct(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_CHAR.withName("elem1"), JAVA_CHAR.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_CHAR.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + + MemorySegment charUnionSegmt = arena.allocate(unionLayout); + char unionElem1 = (char)(arg1.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); + char nestedStructElem1 = (char)(arg1.get(JAVA_CHAR, 0) + arg2.get(JAVA_CHAR, 0) - 'A'); + char nestedStructElem2 = (char)(arg1.get(JAVA_CHAR, 2) + arg2.get(JAVA_CHAR, 2) - 'A'); + charUnionSegmt.set(JAVA_CHAR, 0, unionElem1); + charUnionSegmt.set(JAVA_CHAR, 2, nestedStructElem2); + return charUnionSegmt; + } + + public static short addShortAndShortsFromStruct_Nested2ShortUnion(short arg1, MemorySegment arg2) { + short unionElem1 = arg2.get(JAVA_SHORT, 0); + short nestedUnionElem1 = arg2.get(JAVA_SHORT, 2); + short nestedUnionElem2 = arg2.get(JAVA_SHORT, 2); + short shortSum = (short)(arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2); + return shortSum; + } + + public static short addShortAndShortsFromUnion_Nested2ShortStruct(short arg1, MemorySegment arg2) { + short unionElem1 = arg2.get(JAVA_SHORT, 0); + short nestedStructElem1 = arg2.get(JAVA_SHORT, 0); + short nestedStructElem2 = arg2.get(JAVA_SHORT, 2); + short shortSum = (short)(arg1 + unionElem1 + nestedStructElem1 + nestedStructElem2); + return shortSum; + } + + public static short addShortAndShortsFromUnion_Nested4ShortStruct(short arg1, MemorySegment arg2) { + short unionElem1 = arg2.get(JAVA_SHORT, 0); + short nestedStructElem1 = arg2.get(JAVA_SHORT, 0); + short nestedStructElem2 = arg2.get(JAVA_SHORT, 2); + short nestedStructElem3 = arg2.get(JAVA_SHORT, 4); + short nestedStructElem4 = arg2.get(JAVA_SHORT, 6); + short shortSum = (short)(arg1 + unionElem1 + nestedStructElem1 + + nestedStructElem2 + nestedStructElem3 + nestedStructElem4); + return shortSum; + } + + public static MemorySegment add2ShortStructs_returnStruct_Nested2ShortUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + MemorySegment shortStructSegmt = arena.allocate(structLayout); + short structElem1 = (short)(arg1.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); + short nestedUnionElem1 = (short)(arg1.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); + short nestedUnionElem2 = (short)(arg1.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); + shortStructSegmt.set(JAVA_SHORT, 0, structElem1); + shortStructSegmt.set(JAVA_SHORT, 2, nestedUnionElem2); + return shortStructSegmt; + } + + public static MemorySegment add2ShortUnions_returnUnion_Nested2ShortStruct(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_SHORT.withName("elem1"), JAVA_SHORT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_SHORT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + + MemorySegment shortUnionSegmt = arena.allocate(unionLayout); + short unionElem1 = (short)(arg1.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); + short nestedStructElem1 = (short)(arg1.get(JAVA_SHORT, 0) + arg2.get(JAVA_SHORT, 0)); + short nestedStructElem2 = (short)(arg1.get(JAVA_SHORT, 2) + arg2.get(JAVA_SHORT, 2)); + shortUnionSegmt.set(JAVA_SHORT, 0, unionElem1); + shortUnionSegmt.set(JAVA_SHORT, 2, nestedStructElem2); + return shortUnionSegmt; + } + + public static short addShortAndShortByteFromUnion_Nested2ByteStruct(short arg1, MemorySegment arg2) { + short unionElem1 = arg2.get(JAVA_SHORT, 0); + byte nestedStructElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedStructElem2 = arg2.get(JAVA_BYTE, 1); + short shortSum = (short)(arg1 + unionElem1 + nestedStructElem1 + nestedStructElem2); + return shortSum; + } + + public static short addShortAndBytesFromUnion_Nested4ByteStruct(short arg1, MemorySegment arg2) { + byte nestedStructElem1 = arg2.get(JAVA_BYTE, 0); + byte nestedStructElem2 = arg2.get(JAVA_BYTE, 1); + byte nestedStructElem3 = arg2.get(JAVA_BYTE, 2); + byte nestedStructElem4 = arg2.get(JAVA_BYTE, 3); + short shortSum = (short)(arg1 + nestedStructElem1 + + nestedStructElem2 + nestedStructElem3 + nestedStructElem4); + return shortSum; + } + + public static int addIntAndIntsFromStruct_Nested2IntUnion(int arg1, MemorySegment arg2) { + int unionElem1 = arg2.get(JAVA_INT, 0); + int nestedUnionElem1 = arg2.get(JAVA_INT, 4); + int nestedUnionElem2 = arg2.get(JAVA_INT, 4); + int intSum = arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2; + return intSum; + } + + public static int addIntAndIntsFromUnion_Nested2IntStruct(int arg1, MemorySegment arg2) { + int unionElem1 = arg2.get(JAVA_INT, 0); + int nestedStructElem1 = arg2.get(JAVA_INT, 0); + int nestedStructElem2 = arg2.get(JAVA_INT, 4); + int intSum = arg1 + unionElem1 + nestedStructElem1 + nestedStructElem2; + return intSum; + } + + public static int addIntAndIntsFromUnion_Nested4IntStruct(int arg1, MemorySegment arg2) { + int unionElem1 = arg2.get(JAVA_INT, 0); + int nestedStructElem1 = arg2.get(JAVA_INT, 0); + int nestedStructElem2 = arg2.get(JAVA_INT, 4); + int nestedStructElem3 = arg2.get(JAVA_INT, 8); + int nestedStructElem4 = arg2.get(JAVA_INT, 12); + int intSum = arg1 + unionElem1 + nestedStructElem1 + + nestedStructElem2 + nestedStructElem3 + nestedStructElem4; + return intSum; + } + + public static MemorySegment add2IntStructs_returnStruct_Nested2IntUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + MemorySegment intStructSegmt = arena.allocate(structLayout); + int structElem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); + int nestedUnionElem1 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); + int nestedUnionElem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); + intStructSegmt.set(JAVA_INT, 0, structElem1); + intStructSegmt.set(JAVA_INT, 4, nestedUnionElem2); + return intStructSegmt; + } + + public static MemorySegment add2IntUnions_returnUnion_Nested2IntStruct(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_INT.withName("elem1"), JAVA_INT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_INT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + + MemorySegment intUnionSegmt = arena.allocate(unionLayout); + int unionElem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); + int nestedStructElem1 = arg1.get(JAVA_INT, 0) + arg2.get(JAVA_INT, 0); + int nestedStructElem2 = arg1.get(JAVA_INT, 4) + arg2.get(JAVA_INT, 4); + intUnionSegmt.set(JAVA_INT, 0, unionElem1); + intUnionSegmt.set(JAVA_INT, 4, nestedStructElem2); + return intUnionSegmt; + } + + public static int addIntAndIntShortFromUnion_Nested2ShortStruct(int arg1, MemorySegment arg2) { + int unionElem1 = arg2.get(JAVA_INT, 0); + short nestedStructElem1 = arg2.get(JAVA_SHORT, 0); + short nestedStructElem2 = arg2.get(JAVA_SHORT, 2); + int intSum = arg1 + unionElem1 + nestedStructElem1 + nestedStructElem2; + return intSum; + } + + public static int addIntAndShortsFromUnion_Nested4ShortStruct(int arg1, MemorySegment arg2) { + short nestedStructElem1 = arg2.get(JAVA_SHORT, 0); + short nestedStructElem2 = arg2.get(JAVA_SHORT, 2); + short nestedStructElem3 = arg2.get(JAVA_SHORT, 4); + short nestedStructElem4 = arg2.get(JAVA_SHORT, 6); + int intSum = arg1 + nestedStructElem1 + + nestedStructElem2 + nestedStructElem3 + nestedStructElem4; + return intSum; + } + + public static long addLongAndLongsFromStruct_Nested2LongUnion(long arg1, MemorySegment arg2) { + long unionElem1 = arg2.get(JAVA_LONG, 0); + long nestedUnionElem1 = arg2.get(JAVA_LONG, 8); + long nestedUnionElem2 = arg2.get(JAVA_LONG, 8); + long longSum = arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2; + return longSum; + } + + public static long addLongAndLongsFromUnion_Nested2LongStruct(long arg1, MemorySegment arg2) { + long unionElem1 = arg2.get(JAVA_LONG, 0); + long nestedStructElem1 = arg2.get(JAVA_LONG, 0); + long nestedStructElem2 = arg2.get(JAVA_LONG, 8); + long longSum = arg1 + unionElem1 + nestedStructElem1 + nestedStructElem2; + return longSum; + } + + public static long addLongAndLongsFromUnion_Nested4LongStruct(long arg1, MemorySegment arg2) { + long unionElem1 = arg2.get(JAVA_LONG, 0); + long nestedStructElem1 = arg2.get(JAVA_LONG, 0); + long nestedStructElem2 = arg2.get(JAVA_LONG, 8); + long nestedStructElem3 = arg2.get(JAVA_LONG, 16); + long nestedStructElem4 = arg2.get(JAVA_LONG, 24); + long longSum = arg1 + unionElem1 + nestedStructElem1 + + nestedStructElem2 + nestedStructElem3 + nestedStructElem4; + return longSum; + } + + public static MemorySegment add2LongStructs_returnStruct_Nested2LongUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + MemorySegment longStructSegmt = arena.allocate(structLayout); + long structElem1 = arg1.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); + long nestedUnionElem1 = arg1.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); + long nestedUnionElem2 = arg1.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); + longStructSegmt.set(JAVA_LONG, 0, structElem1); + longStructSegmt.set(JAVA_LONG, 8, nestedUnionElem2); + return longStructSegmt; + } + + public static MemorySegment add2LongUnions_returnUnion_Nested2LongStruct(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_LONG.withName("elem1"), JAVA_LONG.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_LONG.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + + MemorySegment longUnionSegmt = arena.allocate(unionLayout); + long unionElem1 = arg1.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); + long nestedStructElem1 = arg1.get(JAVA_LONG, 0) + arg2.get(JAVA_LONG, 0); + long nestedStructElem2 = arg1.get(JAVA_LONG, 8) + arg2.get(JAVA_LONG, 8); + longUnionSegmt.set(JAVA_LONG, 0, unionElem1); + longUnionSegmt.set(JAVA_LONG, 8, nestedStructElem2); + return longUnionSegmt; + } + + public static float addFloatAndFloatsFromStruct_Nested2FloatUnion(float arg1, MemorySegment arg2) { + float unionElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedUnionElem1 = arg2.get(JAVA_FLOAT, 4); + float nestedUnionElem2 = arg2.get(JAVA_FLOAT, 4); + float floatSum = arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2; + return floatSum; + } + + public static float addFloatAndFloatsFromUnion_Nested2FloatStruct(float arg1, MemorySegment arg2) { + float unionElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedStructElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedStructElem2 = arg2.get(JAVA_FLOAT, 4); + float floatSum = arg1 + unionElem1 + nestedStructElem1 + nestedStructElem2; + return floatSum; + } + + public static float addFloatAndFloatsFromUnion_Nested4FloatStruct(float arg1, MemorySegment arg2) { + float unionElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedStructElem1 = arg2.get(JAVA_FLOAT, 0); + float nestedStructElem2 = arg2.get(JAVA_FLOAT, 4); + float nestedStructElem3 = arg2.get(JAVA_FLOAT, 8); + float nestedStructElem4 = arg2.get(JAVA_FLOAT, 12); + float floatSum = arg1 + unionElem1 + nestedStructElem1 + + nestedStructElem2 + nestedStructElem3 + nestedStructElem4; + return floatSum; + } + + public static MemorySegment add2FloatStructs_returnStruct_Nested2FloatUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + MemorySegment floatStructSegmt = arena.allocate(structLayout); + float structElem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); + float nestedUnionElem1 = arg1.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); + float nestedUnionElem2 = arg1.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); + floatStructSegmt.set(JAVA_FLOAT, 0, structElem1); + floatStructSegmt.set(JAVA_FLOAT, 4, nestedUnionElem2); + return floatStructSegmt; + } + + public static MemorySegment add2FloatUnions_returnUnion_Nested2FloatStruct(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_FLOAT.withName("elem1"), JAVA_FLOAT.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_FLOAT.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + + MemorySegment floatUnionSegmt = arena.allocate(unionLayout); + float unionElem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); + float nestedStructElem1 = arg1.get(JAVA_FLOAT, 0) + arg2.get(JAVA_FLOAT, 0); + float nestedStructElem2 = arg1.get(JAVA_FLOAT, 4) + arg2.get(JAVA_FLOAT, 4); + floatUnionSegmt.set(JAVA_FLOAT, 0, unionElem1); + floatUnionSegmt.set(JAVA_FLOAT, 4, nestedStructElem2); + return floatUnionSegmt; + } + + public static double addDoubleAndDoublesFromStruct_Nested2DoubleUnion(double arg1, MemorySegment arg2) { + double unionElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedUnionElem1 = arg2.get(JAVA_DOUBLE, 8); + double nestedUnionElem2 = arg2.get(JAVA_DOUBLE, 8); + double doubleSum = arg1 + unionElem1 + nestedUnionElem1 + nestedUnionElem2; + return doubleSum; + } + + public static double addDoubleAndDoublesFromUnion_Nested2DoubleStruct(double arg1, MemorySegment arg2) { + double unionElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedStructElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedStructElem2 = arg2.get(JAVA_DOUBLE, 8); + double doubleSum = arg1 + unionElem1 + nestedStructElem1 + nestedStructElem2; + return doubleSum; + } + + public static double addDoubleAndDoublesFromUnion_Nested4DoubleStruct(double arg1, MemorySegment arg2) { + double unionElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedStructElem1 = arg2.get(JAVA_DOUBLE, 0); + double nestedStructElem2 = arg2.get(JAVA_DOUBLE, 8); + double nestedStructElem3 = arg2.get(JAVA_DOUBLE, 16); + double nestedStructElem4 = arg2.get(JAVA_DOUBLE, 24); + double doubleSum = arg1 + unionElem1 + nestedStructElem1 + + nestedStructElem2 + nestedStructElem3 + nestedStructElem4; + return doubleSum; + } + + public static MemorySegment add2DoubleStructs_returnStruct_Nested2DoubleUnion(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedUnionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + GroupLayout structLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), nestedUnionLayout.withName("union_elem2")); + + MemorySegment doubleStructSegmt = arena.allocate(structLayout); + double structElem1 = arg1.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); + double nestedUnionElem1 = arg1.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); + double nestedUnionElem2 = arg1.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); + doubleStructSegmt.set(JAVA_DOUBLE, 0, structElem1); + doubleStructSegmt.set(JAVA_DOUBLE, 8, nestedUnionElem2); + return doubleStructSegmt; + } + + public static MemorySegment add2DoubleUnions_returnUnion_Nested2DoubleStruct(MemorySegment arg1, MemorySegment arg2) { + GroupLayout nestedStructLayout = MemoryLayout.structLayout(JAVA_DOUBLE.withName("elem1"), JAVA_DOUBLE.withName("elem2")); + GroupLayout unionLayout = MemoryLayout.unionLayout(JAVA_DOUBLE.withName("elem1"), nestedStructLayout.withName("struct_elem2")); + + MemorySegment doubleUnionSegmt = arena.allocate(unionLayout); + double unionElem1 = arg1.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); + double nestedStructElem1 = arg1.get(JAVA_DOUBLE, 0) + arg2.get(JAVA_DOUBLE, 0); + double nestedStructElem2 = arg1.get(JAVA_DOUBLE, 8) + arg2.get(JAVA_DOUBLE, 8); + doubleUnionSegmt.set(JAVA_DOUBLE, 0, unionElem1); + doubleUnionSegmt.set(JAVA_DOUBLE, 8, nestedStructElem2); + return doubleUnionSegmt; + } } diff --git a/test/functional/Java22andUp/testng_220.xml b/test/functional/Java22andUp/testng_220.xml index dff39881abb..2d610d2fe2e 100644 --- a/test/functional/Java22andUp/testng_220.xml +++ b/test/functional/Java22andUp/testng_220.xml @@ -37,6 +37,8 @@ + + @@ -48,6 +50,8 @@ + +