1 /*
2 * Copyright (c) 2025, 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.jmh.annotations.Benchmark;
26 import org.openjdk.jmh.annotations.Fork;
27 import org.openjdk.jmh.annotations.OperationsPerInvocation;
28
29 @Fork(value = 3, jvmArgsAppend = {"--enable-preview"})
30 public class Value extends AckermannBase {
31
32 private static ValueLong ack_value(ValueLong x, ValueLong y) {
33 return x.value() == 0 ?
34 ValueLong.valueOf(y.value() + 1) :
35 (y.value() == 0 ?
36 ack_value(ValueLong.valueOf(x.value() - 1), ValueLong.valueOf(1)) :
37 ack_value(ValueLong.valueOf(x.value() - 1), ack_value(x, ValueLong.valueOf(y.value() - 1))));
38 }
39
40 @Benchmark
41 @OperationsPerInvocation(OPI)
42 public long ackermann_value() {
43 return ack_value(ValueLong.valueOf(X1), ValueLong.valueOf(Y1)).value()
44 + ack_value(ValueLong.valueOf(X2), ValueLong.valueOf(Y2)).value()
45 + ack_value(ValueLong.valueOf(X3), ValueLong.valueOf(Y3)).value();
46 }
47
48 private static InterfaceLong ack_value_as_Int(InterfaceLong x, InterfaceLong y) {
49 return x.value() == 0 ?
50 ValueLong.valueOf(y.value() + 1) :
51 (y.value() == 0 ?
52 ack_value_as_Int(ValueLong.valueOf(x.value() - 1), ValueLong.valueOf(1)) :
53 ack_value_as_Int(ValueLong.valueOf(x.value() - 1), ack_value_as_Int(x, ValueLong.valueOf(y.value() - 1))));
54 }
55
56 @Benchmark
57 @OperationsPerInvocation(OPI)
58 public long ackermann_interface() {
59 return ack_value_as_Int(ValueLong.valueOf(X1), ValueLong.valueOf(Y1)).value()
60 + ack_value_as_Int(ValueLong.valueOf(X2), ValueLong.valueOf(Y2)).value()
61 + ack_value_as_Int(ValueLong.valueOf(X3), ValueLong.valueOf(Y3)).value();
62 }
63
64 public static interface InterfaceLong {
65 public long value();
66 }
67
68 public static value class ValueLong implements InterfaceLong {
69
70 public final long v0;
71
72 public ValueLong(long v0) {
73 this.v0 = v0;
74 }
75
76 public long value() {
77 return v0;
78 }
79
80 public static ValueLong valueOf(long value) {
81 return new ValueLong(value);
82 }
83
84 }
85
86
87 }