1 /*
2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2019, Arm Limited. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  */
24 
25 /*
26  * @test
27  * @bug 8232591
28  * @summary Test some cases of combined signed multiply long operation
29  * @library /test/lib
30  * @run main/othervm -XX:+UnlockDiagnosticVMOptions
31  *                   -Xcomp -XX:-TieredCompilation -XX:-Inline
32  *                   compiler.codegen.TestSignedMultiplyLong
33  */
34 
35 package compiler.codegen;
36 
37 import jdk.test.lib.Asserts;
38 
39 public class TestSignedMultiplyLong {
40 
41     private static final int   minInt = Integer.MIN_VALUE; // -2147483648
42     private static final int   maxInt = Integer.MAX_VALUE; //  2147483647
43     private static final long minLong =    Long.MIN_VALUE; // -9223372036854775808
44     private static final long maxLong =    Long.MAX_VALUE; //  9223372036854775807
45 
46     private static Case[] testCases = {
47         // case:     a       b       c         resSmaddl          resSmsubl          resSmnegl
48         new Case(  1000,   -200, 500000L,           300000L,           700000L,           200000L),
49         new Case(maxInt,      1,      1L, (long)maxInt + 1L, (long)minInt + 2L,  (long)minInt + 1L),
50         new Case(minInt,     -1,      1L, (long)maxInt + 2L, (long)minInt + 1L,       (long)minInt),
51         new Case(   -10, minInt,      1L,      21474836481L,     -21474836479L,      -21474836480L),
52         new Case(     1,      1, maxLong,           minLong,      maxLong - 1L,                -1L),
53         new Case(     1,     -1, minLong,           maxLong,      minLong + 1L,                 1L),
54         new Case(    -1,     -1, 0xffffffffeL << 32, 0xfffffffe00000001L, 0xfffffffdffffffffL, -1L)
55     };
56 
57     private static class Case {
58 
59         private int a;
60         private int b;
61         private long c;
62         private long resSmaddl;
63         private long resSmsubl;
64         private long resSmnegl;
65 
Case(int a, int b, long c, long resSmaddl, long resSmsubl, long resSmnegl)66         public Case(int a, int b, long c, long resSmaddl, long resSmsubl, long resSmnegl) {
67             this.a = a;
68             this.b = b;
69             this.c = c;
70             this.resSmaddl = resSmaddl;
71             this.resSmsubl = resSmsubl;
72             this.resSmnegl = resSmnegl;
73         }
74 
verify()75         public void verify() {
76             Asserts.assertEQ(smaddl(a, b, c), resSmaddl,
77                 "Unexpected result from signed multiply-add long.");
78             Asserts.assertEQ(smsubl(a, b, c), resSmsubl,
79                 "Unexpected result from signed multiply-sub long.");
80             Asserts.assertEQ(smnegl(a, b), resSmnegl,
81                 "Unexpected result from signed multiply-neg long.");
82         }
83     }
84 
smaddl(int a, int b, long c)85     private static long smaddl(int a, int b, long c) {
86         return c + a * (long) b;
87     }
88 
smsubl(int a, int b, long c)89     private static long smsubl(int a, int b, long c) {
90         return c - a * (long) b;
91     }
92 
smnegl(int a, int b)93     private static long smnegl(int a, int b) {
94         return a * (-(long) b);
95     }
96 
main(String[] args)97     public static void main(String[] args) {
98         for (Case c : testCases) {
99             c.verify();
100         }
101     }
102 }
103