1 /*
  2  * Copyright (c) 2025, 2026, 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.sum;
 24 
 25 import jdk.internal.value.ValueClass;
 26 import jdk.internal.vm.annotation.LooselyConsistentValue;
 27 import jdk.internal.vm.annotation.NullRestricted;
 28 import org.openjdk.jmh.annotations.Benchmark;
 29 import org.openjdk.jmh.annotations.CompilerControl;
 30 import org.openjdk.jmh.annotations.Fork;
 31 import org.openjdk.jmh.annotations.Setup;
 32 
 33 @Fork(value = 3, jvmArgsAppend = {"--enable-preview", "--add-exports", "java.base/jdk.internal.value=ALL-UNNAMED"})
 34 public class ValueNullFreeNonAtomic extends SumBase {
 35 
 36     public interface InterfaceSum {
 37         public int sum();
 38     }
 39 
 40     @LooselyConsistentValue
 41     public static value class ValueInt implements InterfaceSum {
 42         public final int v0;
 43         public ValueInt(int value) {
 44             this.v0 = value;
 45         }
 46         public int sum() {
 47             return v0;
 48         }
 49     }
 50 
 51     @LooselyConsistentValue
 52     public static value class ValueInt2_w2_d0 implements InterfaceSum {
 53         public final int v0, v1;
 54 
 55         public ValueInt2_w2_d0(int v0, int v1) {
 56             this.v0 = v0;
 57             this.v1 = v1;
 58         }
 59 
 60         public int sum() {
 61             return v0 + v1;
 62         }
 63     }
 64 
 65     @LooselyConsistentValue
 66     public static value class ValueInt2_w2_d1 implements InterfaceSum {
 67         @NullRestricted
 68         public final ValueInt v0, v1;
 69 
 70         public ValueInt2_w2_d1(ValueInt v0, ValueInt v1) {
 71             this.v0 = v0;
 72             this.v1 = v1;
 73         }
 74 
 75         public ValueInt2_w2_d1(int v0, int v1) {
 76             this(new ValueInt(v0), new ValueInt(v1));
 77         }
 78 
 79         public int sum() {
 80             return v0.sum() + v1.sum();
 81         }
 82     }
 83 
 84     @LooselyConsistentValue
 85     public static value class ValueInt4_w4_d0 implements InterfaceSum {
 86         public final int v0, v1, v2, v3;
 87 
 88         public ValueInt4_w4_d0(int v0, int v1, int v2, int v3) {
 89             this.v0 = v0;
 90             this.v1 = v1;
 91             this.v2 = v2;
 92             this.v3 = v3;
 93         }
 94 
 95         public int sum() {
 96             return v0 + v1 + v2 + v3;
 97         }
 98     }
 99 
100     @LooselyConsistentValue
101     public static value class ValueInt4_w4_d1 implements InterfaceSum {
102         @NullRestricted
103         public final ValueInt v0, v1, v2, v3;
104 
105         public ValueInt4_w4_d1(ValueInt v0, ValueInt v1, ValueInt v2, ValueInt v3) {
106             this.v0 = v0;
107             this.v1 = v1;
108             this.v2 = v2;
109             this.v3 = v3;
110         }
111 
112         public ValueInt4_w4_d1(int v0, int v1, int v2, int v3) {
113             this(new ValueInt(v0), new ValueInt(v1), new ValueInt(v2), new ValueInt(v3));
114         }
115 
116         public int sum() {
117             return v0.sum() + v1.sum() + v2.sum() + v3.sum();
118         }
119     }
120 
121     @LooselyConsistentValue
122     public static value class ValueInt4_w2_d1 implements InterfaceSum {
123         @NullRestricted
124         public final ValueInt2_w2_d0 v0, v1;
125 
126         public ValueInt4_w2_d1(ValueInt2_w2_d0 v0, ValueInt2_w2_d0 v1) {
127             this.v0 = v0;
128             this.v1 = v1;
129         }
130 
131         public ValueInt4_w2_d1(int v0, int v1, int v2, int v3) {
132             this(new ValueInt2_w2_d0(v0, v1), new ValueInt2_w2_d0(v2, v3));
133         }
134 
135         public int sum() {
136             return v0.sum() + v1.sum();
137         }
138     }
139 
140     @LooselyConsistentValue
141     public static value class ValueInt4_w2_d2 implements InterfaceSum {
142         @NullRestricted
143         public final ValueInt2_w2_d1 v0, v1;
144 
145         public ValueInt4_w2_d2(ValueInt2_w2_d1 v0, ValueInt2_w2_d1 v1) {
146             this.v0 = v0;
147             this.v1 = v1;
148         }
149 
150         public ValueInt4_w2_d2(int v0, int v1, int v2, int v3) {
151             this(new ValueInt2_w2_d1(v0, v1), new ValueInt2_w2_d1(v2, v3));
152         }
153         public int sum() {
154             return v0.sum() + v1.sum();
155         }
156 
157     }
158 
159     public static class ValState_of_Int extends SizeState {
160         public ValueInt[] arr;
161         @Setup
162         public void setup() {
163             arr = (ValueInt[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt.class, size * 4, new ValueInt(0));
164             for (int i = 0; i < arr.length; i++) {
165                 arr[i] = new ValueInt(i);
166             }
167         }
168     }
169 
170     public static class IntState_of_Int extends SizeState {
171         public InterfaceSum[] arr;
172         @Setup
173         public void setup() {
174             arr = new InterfaceSum[size * 4];
175             for (int i = 0; i < arr.length; i++) {
176                 arr[i] = new ValueInt(i);
177             }
178         }
179     }
180 
181     public static class ValState_of_Int2_w2_d0 extends SizeState {
182         public ValueInt2_w2_d0[] arr;
183         @Setup
184         public void setup() {
185             arr = (ValueInt2_w2_d0[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt2_w2_d0.class, size * 2, new ValueInt2_w2_d0(0, 0));
186             for (int i = 0; i < arr.length; i++) {
187                 arr[i] = new ValueInt2_w2_d0(2 * i, 2 * i + 1);
188             }
189         }
190     }
191 
192     public static class IntState_of_Int2_w2_d0 extends SizeState {
193         public InterfaceSum[] arr;
194         @Setup
195         public void setup() {
196             arr = new InterfaceSum[size * 2];
197             for (int i = 0; i < arr.length; i++) {
198                 arr[i] = new ValueInt2_w2_d0(2 * i, 2 * i + 1);
199             }
200         }
201     }
202 
203     public static class ValState_of_Int2_w2_d1 extends SizeState {
204         public ValueInt2_w2_d1[] arr;
205         @Setup
206         public void setup() {
207             arr = (ValueInt2_w2_d1[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt2_w2_d1.class, size * 4, new ValueInt2_w2_d1(0, 0));
208             for (int i = 0; i < arr.length; i++) {
209                 arr[i] = new ValueInt2_w2_d1(2 * i, 2 * i + 1);
210             }
211         }
212     }
213 
214     public static class IntState_of_Int2_w2_d1 extends SizeState {
215         public InterfaceSum[] arr;
216         @Setup
217         public void setup() {
218             arr = new InterfaceSum[size * 2];
219             for (int i = 0; i < arr.length; i++) {
220                 arr[i] = new ValueInt2_w2_d1(2 * i, 2 * i + 1);
221             }
222         }
223     }
224 
225     public static class ValState_of_Int4_w4_d0 extends SizeState {
226         public ValueInt4_w4_d0[] arr;
227         @Setup
228         public void setup() {
229             arr = (ValueInt4_w4_d0[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt4_w4_d0.class, size, new ValueInt4_w4_d0(0, 0, 0,  0));
230             for (int i = 0; i < arr.length; i++) {
231                 arr[i] = new ValueInt4_w4_d0(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
232             }
233         }
234     }
235 
236     public static class IntState_of_Int4_w4_d0 extends SizeState {
237         public InterfaceSum[] arr;
238         @Setup
239         public void setup() {
240             arr = new InterfaceSum[size];
241             for (int i = 0; i < arr.length; i++) {
242                 arr[i] = new ValueInt4_w4_d0(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
243             }
244         }
245     }
246 
247     public static class ValState_of_Int4_w4_d1 extends SizeState {
248         public ValueInt4_w4_d1[] arr;
249         @Setup
250         public void setup() {
251             arr = (ValueInt4_w4_d1[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt4_w4_d1.class, size, new ValueInt4_w4_d1(0, 0, 0,  0));
252             for (int i = 0; i < arr.length; i++) {
253                 arr[i] = new ValueInt4_w4_d1(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
254             }
255         }
256     }
257 
258     public static class IntState_of_Int4_w4_d1 extends SizeState {
259         public InterfaceSum[] arr;
260         @Setup
261         public void setup() {
262             arr = new InterfaceSum[size];
263             for (int i = 0; i < arr.length; i++) {
264                 arr[i] = new ValueInt4_w4_d1(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
265             }
266         }
267     }
268 
269     public static class ValState_of_Int4_w2_d1 extends SizeState {
270         public ValueInt4_w2_d1[] arr;
271         @Setup
272         public void setup() {
273             arr = (ValueInt4_w2_d1[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt4_w2_d1.class, size, new ValueInt4_w2_d1(0, 0, 0,  0));
274             for (int i = 0; i < arr.length; i++) {
275                 arr[i] = new ValueInt4_w2_d1(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
276             }
277         }
278     }
279 
280     public static class IntState_of_Int4_w2_d1 extends SizeState {
281         public InterfaceSum[] arr;
282         @Setup
283         public void setup() {
284             arr = new InterfaceSum[size];
285             for (int i = 0; i < arr.length; i++) {
286                 arr[i] = new ValueInt4_w2_d1(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
287             }
288         }
289     }
290 
291     public static class ValState_of_Int4_w2_d2 extends SizeState {
292         public ValueInt4_w2_d2[] arr;
293         @Setup
294         public void setup() {
295             arr = (ValueInt4_w2_d2[])ValueClass.newNullRestrictedNonAtomicArray(ValueInt4_w2_d2.class, size, new ValueInt4_w2_d2(0, 0, 0,  0));
296             for (int i = 0; i < arr.length; i++) {
297                 arr[i] = new ValueInt4_w2_d2(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
298             }
299         }
300     }
301 
302     public static class IntState_of_Int4_w2_d2 extends SizeState {
303         public InterfaceSum[] arr;
304         @Setup
305         public void setup() {
306             arr = new InterfaceSum[size];
307             for (int i = 0; i < arr.length; i++) {
308                 arr[i] = new ValueInt4_w2_d2(4 * i, 4 * i + 1, 4 * i + 2, 4 * i + 3);
309             }
310         }
311     }
312 
313     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
314     public int sum_interface(InterfaceSum[] src) {
315         int s = 0;
316         for (var v : src) {
317             s += v.sum();
318         }
319         return s;
320     }
321 
322     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
323     public int sum_val_of_Int(ValueInt[] src) {
324         int s = 0;
325         for (var v : src) {
326             s += v.sum();
327         }
328         return s;
329     }
330 
331     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
332     public int sum_val_of_Int2_w2_d0(ValueInt2_w2_d0[] src) {
333         int s = 0;
334         for (var v : src) {
335             s += v.sum();
336         }
337         return s;
338     }
339 
340     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
341     public int sum_val_of_Int2_w2_d1(ValueInt2_w2_d1[] src) {
342         int s = 0;
343         for (var v : src) {
344             s += v.sum();
345         }
346         return s;
347     }
348 
349     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
350     public int sum_val_of_Int4_w4_d0(ValueInt4_w4_d0[] src) {
351         int s = 0;
352         for (var v : src) {
353             s += v.sum();
354         }
355         return s;
356     }
357 
358     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
359     public int sum_val_of_Int4_w4_d1(ValueInt4_w4_d1[] src) {
360         int s = 0;
361         for (var v : src) {
362             s += v.sum();
363         }
364         return s;
365     }
366 
367     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
368     public int sum_val_of_Int4_w2_d1(ValueInt4_w2_d1[] src) {
369         int s = 0;
370         for (var v : src) {
371             s += v.sum();
372         }
373         return s;
374     }
375 
376     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
377     public int sum_val_of_Int4_w2_d2(ValueInt4_w2_d2[] src) {
378         int s = 0;
379         for (var v : src) {
380             s += v.sum();
381         }
382         return s;
383     }
384 
385     @Benchmark
386     public int sum_interface_of_Int(IntState_of_Int st) {
387         return sum_interface(st.arr);
388     }
389 
390     @Benchmark
391     public int sum_interface_of_Int2_w2_d0(IntState_of_Int2_w2_d0 st) {
392         return sum_interface(st.arr);
393     }
394 
395     @Benchmark
396     public int sum_interface_of_Int2_w2_d1(IntState_of_Int2_w2_d1 st) {
397         return sum_interface(st.arr);
398     }
399 
400     @Benchmark
401     public int sum_interface_of_Int4_w4_d0(IntState_of_Int4_w4_d0 st) {
402         return sum_interface(st.arr);
403     }
404 
405     @Benchmark
406     public int sum_interface_of_Int4_w4_d1(IntState_of_Int4_w4_d1 st) {
407         return sum_interface(st.arr);
408     }
409 
410     @Benchmark
411     public int sum_interface_of_Int4_w2_d1(IntState_of_Int4_w2_d1 st) {
412         return sum_interface(st.arr);
413     }
414 
415     @Benchmark
416     public int sum_interface_of_Int4_w2_d2(IntState_of_Int4_w2_d2 st) {
417         return sum_interface(st.arr);
418     }
419 
420     @Benchmark
421     public int sum_val_of_Int(ValState_of_Int st) {
422         return sum_val_of_Int(st.arr);
423     }
424 
425     @Benchmark
426     public int sum_val_of_Int2_w2_d0(ValState_of_Int2_w2_d0 st) {
427         return sum_val_of_Int2_w2_d0(st.arr);
428     }
429 
430     @Benchmark
431     public int sum_val_of_Int2_w2_d1(ValState_of_Int2_w2_d1 st) {
432         return sum_val_of_Int2_w2_d1(st.arr);
433     }
434 
435     @Benchmark
436     public int sum_val_of_Int4_w4_d0(ValState_of_Int4_w4_d0 st) {
437         return sum_val_of_Int4_w4_d0(st.arr);
438     }
439 
440     @Benchmark
441     public int sum_val_of_Int4_w4_d1(ValState_of_Int4_w4_d1 st) {
442         return sum_val_of_Int4_w4_d1(st.arr);
443     }
444 
445     @Benchmark
446     public int sum_val_of_Int4_w2_d1(ValState_of_Int4_w2_d1 st) {
447         return sum_val_of_Int4_w2_d1(st.arr);
448     }
449 
450     @Benchmark
451     public int sum_val_of_Int4_w2_d2(ValState_of_Int4_w2_d2 st) {
452         return sum_val_of_Int4_w2_d2(st.arr);
453     }
454 
455 }