< prev index next >

test/jdk/java/foreign/TestIntrinsics.java

Print this page

 25  * @test
 26  * @requires os.arch=="amd64" | os.arch=="x86_64" | os.arch=="aarch64"
 27  * @run testng/othervm
 28  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=true
 29  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_INTRINSICS=true
 30  *   --enable-native-access=ALL-UNNAMED
 31  *   -Xbatch
 32  *   TestIntrinsics
 33  */
 34 
 35 import jdk.incubator.foreign.CLinker;
 36 import jdk.incubator.foreign.FunctionDescriptor;
 37 
 38 import java.lang.invoke.MethodHandle;
 39 import java.lang.invoke.MethodType;
 40 import java.util.ArrayList;
 41 import java.util.List;
 42 
 43 import jdk.incubator.foreign.MemoryAddress;
 44 import jdk.incubator.foreign.MemoryLayout;

 45 import jdk.incubator.foreign.SymbolLookup;
 46 import org.testng.annotations.*;
 47 
 48 import static java.lang.invoke.MethodType.methodType;
 49 import static jdk.incubator.foreign.CLinker.*;
 50 import static jdk.incubator.foreign.FunctionDescriptor.TRIVIAL_ATTRIBUTE_NAME;
 51 import static org.testng.Assert.assertEquals;
 52 
 53 public class TestIntrinsics {
 54 
 55     static final CLinker abi = CLinker.getInstance();
 56     static {
 57         System.loadLibrary("Intrinsics");
 58     }
 59 
 60     static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup();
 61 
 62     private interface RunnableX {
 63         void run() throws Throwable;
 64     }
 65 
 66     @Test(dataProvider = "tests")
 67     public void testIntrinsics(RunnableX test) throws Throwable {
 68         for (int i = 0; i < 20_000; i++) {
 69             test.run();
 70         }
 71     }
 72 
 73     @DataProvider
 74     public Object[][] tests() {
 75         List<RunnableX> testsList = new ArrayList<>();
 76 
 77         interface AddTest {
 78             void add(MethodHandle target, Object expectedResult, Object... args);
 79         }
 80 
 81         AddTest tests = (mh, expectedResult, args) -> testsList.add(() -> {
 82             Object actual = mh.invokeWithArguments(args);
 83             assertEquals(actual, expectedResult);
 84         });
 85 
 86         interface AddIdentity {
 87             void add(String name, Class<?> carrier, MemoryLayout layout, Object arg);
 88         }
 89 
 90         AddIdentity addIdentity = (name, carrier, layout, arg) -> {
 91             MemoryAddress ma = LOOKUP.lookup(name).get();
 92             MethodType mt = methodType(carrier, carrier);
 93             FunctionDescriptor fd = FunctionDescriptor.of(layout, layout);
 94 
 95             tests.add(abi.downcallHandle(ma, mt, fd), arg, arg);
 96             tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), arg, arg);
 97             tests.add(abi.downcallHandle(mt, fd), arg, ma, arg);
 98         };
 99 
100         { // empty
101             MemoryAddress ma = LOOKUP.lookup("empty").get();
102             MethodType mt = methodType(void.class);
103             FunctionDescriptor fd = FunctionDescriptor.ofVoid();
104             tests.add(abi.downcallHandle(ma, mt, fd), null);
105             tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), null);
106         }
107 
108         addIdentity.add("identity_char",   byte.class,   C_CHAR,   (byte) 10);
109         addIdentity.add("identity_short",  short.class,  C_SHORT, (short) 10);
110         addIdentity.add("identity_int",    int.class,    C_INT,           10);
111         addIdentity.add("identity_long",   long.class,   C_LONG_LONG,     10L);
112         addIdentity.add("identity_float",  float.class,  C_FLOAT,         10F);
113         addIdentity.add("identity_double", double.class, C_DOUBLE,        10D);

114 
115         { // identity_va
116             MemoryAddress ma = LOOKUP.lookup("identity_va").get();
117             MethodType mt = methodType(int.class, int.class, double.class, int.class, float.class, long.class);
118             FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT, asVarArg(C_DOUBLE),
119                     asVarArg(C_INT), asVarArg(C_FLOAT), asVarArg(C_LONG_LONG));
120             tests.add(abi.downcallHandle(ma, mt, fd), 1, 1, 10D, 2, 3F, 4L);
121             tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), 1, 1, 10D, 2, 3F, 4L);
122         }
123 
124         { // high_arity
125             MethodType baseMT = methodType(void.class, int.class, double.class, long.class, float.class, byte.class,
126                     short.class, char.class);
127             FunctionDescriptor baseFD = FunctionDescriptor.ofVoid(C_INT, C_DOUBLE, C_LONG_LONG, C_FLOAT, C_CHAR,
128                     C_SHORT, C_SHORT);
129             Object[] args = {1, 10D, 2L, 3F, (byte) 0, (short) 13, 'a'};
130             for (int i = 0; i < args.length; i++) {
131                 MemoryAddress ma = LOOKUP.lookup("invoke_high_arity" + i).get();
132                 MethodType mt = baseMT.changeReturnType(baseMT.parameterType(i));
133                 FunctionDescriptor fd = baseFD.withReturnLayout(baseFD.argumentLayouts().get(i));
134                 Object expected = args[i];
135                 tests.add(abi.downcallHandle(ma, mt, fd), expected, args);
136                 tests.add(abi.downcallHandle(ma, mt, fd.withAttribute(TRIVIAL_ATTRIBUTE_NAME, true)), expected, args);
137             }
138         }
139 
140         return testsList.stream().map(rx -> new Object[]{ rx }).toArray(Object[][]::new);
141     }
142 }

 25  * @test
 26  * @requires os.arch=="amd64" | os.arch=="x86_64" | os.arch=="aarch64"
 27  * @run testng/othervm
 28  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_SPEC=true
 29  *   -Djdk.internal.foreign.ProgrammableInvoker.USE_INTRINSICS=true
 30  *   --enable-native-access=ALL-UNNAMED
 31  *   -Xbatch
 32  *   TestIntrinsics
 33  */
 34 
 35 import jdk.incubator.foreign.CLinker;
 36 import jdk.incubator.foreign.FunctionDescriptor;
 37 
 38 import java.lang.invoke.MethodHandle;
 39 import java.lang.invoke.MethodType;
 40 import java.util.ArrayList;
 41 import java.util.List;
 42 
 43 import jdk.incubator.foreign.MemoryAddress;
 44 import jdk.incubator.foreign.MemoryLayout;
 45 import jdk.incubator.foreign.NativeSymbol;
 46 import jdk.incubator.foreign.SymbolLookup;
 47 import org.testng.annotations.*;
 48 
 49 import static java.lang.invoke.MethodType.methodType;
 50 import static jdk.incubator.foreign.ValueLayout.JAVA_CHAR;

 51 import static org.testng.Assert.assertEquals;
 52 
 53 public class TestIntrinsics extends NativeTestHelper {
 54 
 55     static final CLinker abi = CLinker.systemCLinker();
 56     static {
 57         System.loadLibrary("Intrinsics");
 58     }
 59 
 60     static final SymbolLookup LOOKUP = SymbolLookup.loaderLookup();
 61 
 62     private interface RunnableX {
 63         void run() throws Throwable;
 64     }
 65 
 66     @Test(dataProvider = "tests")
 67     public void testIntrinsics(RunnableX test) throws Throwable {
 68         for (int i = 0; i < 20_000; i++) {
 69             test.run();
 70         }
 71     }
 72 
 73     @DataProvider
 74     public Object[][] tests() {
 75         List<RunnableX> testsList = new ArrayList<>();
 76 
 77         interface AddTest {
 78             void add(MethodHandle target, Object expectedResult, Object... args);
 79         }
 80 
 81         AddTest tests = (mh, expectedResult, args) -> testsList.add(() -> {
 82             Object actual = mh.invokeWithArguments(args);
 83             assertEquals(actual, expectedResult);
 84         });
 85 
 86         interface AddIdentity {
 87             void add(String name, Class<?> carrier, MemoryLayout layout, Object arg);
 88         }
 89 
 90         AddIdentity addIdentity = (name, carrier, layout, arg) -> {
 91             NativeSymbol ma = LOOKUP.lookup(name).get();
 92             MethodType mt = methodType(carrier, carrier);
 93             FunctionDescriptor fd = FunctionDescriptor.of(layout, layout);
 94 
 95             tests.add(abi.downcallHandle(ma, fd), arg, arg);
 96             tests.add(abi.downcallHandle(fd), arg, ma, arg);

 97         };
 98 
 99         { // empty
100             NativeSymbol ma = LOOKUP.lookup("empty").get();
101             MethodType mt = methodType(void.class);
102             FunctionDescriptor fd = FunctionDescriptor.ofVoid();
103             tests.add(abi.downcallHandle(ma, fd), null);

104         }
105 
106         addIdentity.add("identity_bool",   boolean.class, C_BOOL,   true);
107         addIdentity.add("identity_char",   byte.class,    C_CHAR,   (byte) 10);
108         addIdentity.add("identity_short",  short.class,   C_SHORT, (short) 10);
109         addIdentity.add("identity_int",    int.class,     C_INT,           10);
110         addIdentity.add("identity_long",   long.class,    C_LONG_LONG,     10L);
111         addIdentity.add("identity_float",  float.class,   C_FLOAT,         10F);
112         addIdentity.add("identity_double", double.class,  C_DOUBLE,        10D);
113 
114         { // identity_va
115             NativeSymbol ma = LOOKUP.lookup("identity_va").get();
116             MethodType mt = methodType(int.class, int.class, double.class, int.class, float.class, long.class);
117             FunctionDescriptor fd = FunctionDescriptor.of(C_INT, C_INT).asVariadic(C_DOUBLE, C_INT, C_FLOAT, C_LONG_LONG);
118             tests.add(abi.downcallHandle(ma, fd), 1, 1, 10D, 2, 3F, 4L);


119         }
120 
121         { // high_arity
122             MethodType baseMT = methodType(void.class, int.class, double.class, long.class, float.class, byte.class,
123                     short.class, char.class);
124             FunctionDescriptor baseFD = FunctionDescriptor.ofVoid(C_INT, C_DOUBLE, C_LONG_LONG, C_FLOAT, C_CHAR,
125                     C_SHORT, JAVA_CHAR);
126             Object[] args = {1, 10D, 2L, 3F, (byte) 0, (short) 13, 'a'};
127             for (int i = 0; i < args.length; i++) {
128                 NativeSymbol ma = LOOKUP.lookup("invoke_high_arity" + i).get();
129                 MethodType mt = baseMT.changeReturnType(baseMT.parameterType(i));
130                 FunctionDescriptor fd = baseFD.withReturnLayout(baseFD.argumentLayouts().get(i));
131                 Object expected = args[i];
132                 tests.add(abi.downcallHandle(ma, fd), expected, args);

133             }
134         }
135 
136         return testsList.stream().map(rx -> new Object[]{ rx }).toArray(Object[][]::new);
137     }
138 }
< prev index next >