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