1 /*
 2  * Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
 3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 4  *
 5  * This code is free software; you can redistribute it and/or modify it
 6  * under the terms of the GNU General Public License version 2 only, as
 7  * published by the Free Software Foundation.
 8  *
 9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package org.openjdk.bench.valhalla.ackermann;
24 
25 import org.openjdk.bench.valhalla.types.Int64;
26 import org.openjdk.bench.valhalla.types.Q64int;
27 import org.openjdk.jmh.annotations.Benchmark;
28 import org.openjdk.jmh.annotations.OperationsPerInvocation;
29 
30 public class Inline64int extends AckermannBase {
31 
32     private static Q64int ack_value(Q64int x, Q64int y) {
33         return x.longValue() == 0 ?
34                 new Q64int(y.longValue() + 1) :
35                 (y.longValue() == 0 ?
36                         ack_value(new Q64int(x.longValue()-1) , new Q64int(1)) :
37                         ack_value(new Q64int(x.longValue()-1), ack_value(x, new Q64int(y.longValue()-1))));
38     }
39 
40     @Benchmark
41     @OperationsPerInvocation(OPI)
42     public long ack_Val() {
43         return ack_value(new Q64int(X1), new Q64int(Y1)).longValue()
44                 + ack_value(new Q64int(X2), new Q64int(Y2)).longValue()
45                 + ack_value(new Q64int(X3), new Q64int(Y3)).longValue();
46     }
47 
48     private static Q64int ack_ref(Q64int x, Q64int y) {
49         return x.longValue() == 0 ?
50                 new Q64int(y.longValue() + 1) :
51                 (y.longValue() == 0 ?
52                         ack_ref(new Q64int(x.longValue()-1) , new Q64int(1)) :
53                         ack_ref(new Q64int(x.longValue()-1), ack_ref(x, new Q64int(y.longValue()-1))));
54     }
55 
56     @Benchmark
57     @OperationsPerInvocation(OPI)
58     public long ack_Ref() {
59         return ack_ref(new Q64int(X1), new Q64int(Y1)).longValue()
60                 + ack_ref(new Q64int(X2), new Q64int(Y2)).longValue()
61                 + ack_ref(new Q64int(X3), new Q64int(Y3)).longValue();
62     }
63 
64     private static Int64 ack_inter(Int64 x, Int64 y) {
65         return x.longValue() == 0 ?
66                 new Q64int(y.longValue() + 1) :
67                 (y.longValue() == 0 ?
68                         ack_inter(new Q64int(x.longValue()-1) , new Q64int(1)) :
69                         ack_inter(new Q64int(x.longValue()-1), ack_inter(x, new Q64int(y.longValue()-1))));
70     }
71 
72     @Benchmark
73     @OperationsPerInvocation(OPI)
74     public long ack_Int() {
75         return ack_inter(new Q64int(X1), new Q64int(Y1)).longValue()
76                 + ack_inter(new Q64int(X2), new Q64int(Y2)).longValue()
77                 + ack_inter(new Q64int(X3), new Q64int(Y3)).longValue();
78     }
79 
80 }