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.Q64byte;
27 import org.openjdk.jmh.annotations.Benchmark;
28 import org.openjdk.jmh.annotations.OperationsPerInvocation;
29 
30 public class Inline64byte extends AckermannBase {
31 
32     private static Q64byte ack_value(Q64byte x, Q64byte y) {
33         return x.longValue() == 0 ?
34                 new Q64byte(y.longValue() + 1) :
35                 (y.longValue() == 0 ?
36                         ack_value(new Q64byte(x.longValue()-1) , new Q64byte(1)) :
37                         ack_value(new Q64byte(x.longValue()-1), ack_value(x, new Q64byte(y.longValue()-1))));
38     }
39 
40 //    @Benchmark
41 //    @OperationsPerInvocation(OPI)
42 // TODO fix StackOverflowException
43     public long ack_Val() {
44         return ack_value(new Q64byte(X1), new Q64byte(Y1)).longValue()
45                 + ack_value(new Q64byte(X2), new Q64byte(Y2)).longValue()
46                 + ack_value(new Q64byte(X3), new Q64byte(Y3)).longValue();
47     }
48 
49     private static Q64byte ack_ref(Q64byte x, Q64byte y) {
50         return x.longValue() == 0 ?
51                 new Q64byte(y.longValue() + 1) :
52                 (y.longValue() == 0 ?
53                         ack_ref(new Q64byte(x.longValue()-1) , new Q64byte(1)) :
54                         ack_ref(new Q64byte(x.longValue()-1), ack_ref(x, new Q64byte(y.longValue()-1))));
55     }
56 
57     @Benchmark
58     @OperationsPerInvocation(OPI)
59     public long ack_Ref() {
60         return ack_ref(new Q64byte(X1), new Q64byte(Y1)).longValue()
61                 + ack_ref(new Q64byte(X2), new Q64byte(Y2)).longValue()
62                 + ack_ref(new Q64byte(X3), new Q64byte(Y3)).longValue();
63     }
64 
65     private static Int64 ack_inter(Int64 x, Int64 y) {
66         return x.longValue() == 0 ?
67                 new Q64byte(y.longValue() + 1) :
68                 (y.longValue() == 0 ?
69                         ack_inter(new Q64byte(x.longValue()-1) , new Q64byte(1)) :
70                         ack_inter(new Q64byte(x.longValue()-1), ack_inter(x, new Q64byte(y.longValue()-1))));
71     }
72 
73     @Benchmark
74     @OperationsPerInvocation(OPI)
75     public long ack_Int() {
76         return ack_inter(new Q64byte(X1), new Q64byte(Y1)).longValue()
77                 + ack_inter(new Q64byte(X2), new Q64byte(Y2)).longValue()
78                 + ack_inter(new Q64byte(X3), new Q64byte(Y3)).longValue();
79     }
80 
81 }