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.acmp.field;
 24 
 25 import org.openjdk.bench.valhalla.types.Int64;
 26 import org.openjdk.bench.valhalla.types.Q64long;
 27 import org.openjdk.jmh.annotations.Scope;
 28 import org.openjdk.jmh.annotations.Setup;
 29 import org.openjdk.jmh.annotations.State;
 30 
 31 import java.util.BitSet;
 32 import java.util.Random;
 33 
 34 public class StatesQ64long {
 35 
 36     public static final int SIZE = 100;
 37 
 38     private static BitSet indices(Random rnd, int bound, int size) {
 39         return rnd.ints(0, bound)
 40                 .distinct()
 41                 .limit(size)
 42                 .collect(BitSet::new, BitSet::set, BitSet::or);
 43     }
 44 
 45     private static void populate(ObjWrapper[] arr1, ObjWrapper[] arr2, int eq) {
 46         BitSet eqset = (eq > 0 && eq < 100) ? indices(new Random(42), SIZE, (eq * SIZE) / 100) : null;
 47         for (int i = 0; i < SIZE; i++) {
 48             if (eq > 0 && (eq >= 100 || eqset.get(i))) {
 49                 arr2[i] = arr1[i] = new ObjWrapper(new Q64long(i));
 50             } else {
 51                 arr1[i] = new ObjWrapper(new Q64long(2 * i));
 52                 arr2[i] = new ObjWrapper(new Q64long(2 * i + 1));
 53             }
 54         }
 55     }
 56 
 57     private static void populate(IntWrapper[] arr1, IntWrapper[] arr2, int eq) {
 58         BitSet eqset = (eq > 0 && eq < 100) ? indices(new Random(42), SIZE, (eq * SIZE) / 100) : null;
 59         for (int i = 0; i < SIZE; i++) {
 60             if (eq > 0 && (eq >= 100 || eqset.get(i))) {
 61                 arr2[i] = arr1[i] = new IntWrapper(new Q64long(i));
 62             } else {
 63                 arr1[i] = new IntWrapper(new Q64long(2 * i));
 64                 arr2[i] = new IntWrapper(new Q64long(2 * i + 1));
 65             }
 66         }
 67     }
 68 
 69     private static void populate(RefWrapper[] arr1, RefWrapper[] arr2, int eq) {
 70         BitSet eqset = (eq > 0 && eq < 100) ? indices(new Random(42), SIZE, (eq * SIZE) / 100) : null;
 71         for (int i = 0; i < SIZE; i++) {
 72             if (eq > 0 && (eq >= 100 || eqset.get(i))) {
 73                 arr2[i] = arr1[i] = new RefWrapper(new Q64long(i));
 74             } else {
 75                 arr1[i] = new RefWrapper(new Q64long(2 * i));
 76                 arr2[i] = new RefWrapper(new Q64long(2 * i + 1));
 77             }
 78         }
 79     }
 80 
 81     private static void populate(ValWrapper[] arr1, ValWrapper[] arr2, int eq) {
 82         BitSet eqset = (eq > 0 && eq < 100) ? indices(new Random(42), SIZE, (eq * SIZE) / 100) : null;
 83         for (int i = 0; i < SIZE; i++) {
 84             if (eq > 0 && (eq >= 100 || eqset.get(i))) {
 85                 arr2[i] = arr1[i] = new ValWrapper(new Q64long(i));
 86             } else {
 87                 arr1[i] = new ValWrapper(new Q64long(2 * i));
 88                 arr2[i] = new ValWrapper(new Q64long(2 * i + 1));
 89             }
 90         }
 91     }
 92 
 93     public static class ObjWrapper {
 94         public Object f;
 95 
 96         public ObjWrapper(Object f) {
 97             this.f = f;
 98         }
 99     }
100 
101 
102     @State(Scope.Thread)
103     public abstract static class ObjState {
104         ObjWrapper[] arr1, arr2;
105     }
106 
107     public static class IntWrapper {
108         public Int64 f;
109 
110         public IntWrapper(Int64 f) {
111             this.f = f;
112         }
113     }
114 
115     @State(Scope.Thread)
116     public abstract static class IntState {
117         IntWrapper[] arr1, arr2;
118     }
119 
120     public static class RefWrapper {
121         public Q64long f;
122 
123         public RefWrapper(Q64long f) {
124             this.f = f;
125         }
126     }
127 
128 
129     @State(Scope.Thread)
130     public abstract static class RefState {
131         RefWrapper[] arr1, arr2;
132     }
133 
134     public static class ValWrapper {
135         public Q64long f;
136 
137         public ValWrapper(Q64long f) {
138             this.f = f;
139         }
140     }
141 
142 
143     @State(Scope.Thread)
144     public abstract static class ValState {
145         ValWrapper[] arr1, arr2;
146     }
147 
148     public static class ObjState00 extends ObjState {
149         @Setup
150         public void setup() {
151             arr1 = new ObjWrapper[SIZE];
152             arr2 = new ObjWrapper[SIZE];
153             populate(arr1, arr2, 0);
154         }
155 
156     }
157 
158     public static class ObjState25 extends ObjState {
159         @Setup
160         public void setup() {
161             arr1 = new ObjWrapper[SIZE];
162             arr2 = new ObjWrapper[SIZE];
163             populate(arr1, arr2, 25);
164         }
165 
166     }
167 
168     public static class ObjState50 extends ObjState {
169         @Setup
170         public void setup() {
171             arr1 = new ObjWrapper[SIZE];
172             arr2 = new ObjWrapper[SIZE];
173             populate(arr1, arr2, 50);
174         }
175 
176     }
177 
178     public static class ObjState75 extends ObjState {
179         @Setup
180         public void setup() {
181             arr1 = new ObjWrapper[SIZE];
182             arr2 = new ObjWrapper[SIZE];
183             populate(arr1, arr2, 75);
184         }
185 
186     }
187 
188     public static class ObjState100 extends ObjState {
189         @Setup
190         public void setup() {
191             arr1 = new ObjWrapper[SIZE];
192             arr2 = new ObjWrapper[SIZE];
193             populate(arr1, arr2, 100);
194         }
195     }
196 
197     public static class IntState00 extends IntState {
198         @Setup
199         public void setup() {
200             arr1 = new IntWrapper[SIZE];
201             arr2 = new IntWrapper[SIZE];
202             populate(arr1, arr2, 0);
203         }
204     }
205 
206     public static class IntState25 extends IntState {
207         @Setup
208         public void setup() {
209             arr1 = new IntWrapper[SIZE];
210             arr2 = new IntWrapper[SIZE];
211             populate(arr1, arr2, 25);
212         }
213     }
214 
215     public static class IntState50 extends IntState {
216         @Setup
217         public void setup() {
218             arr1 = new IntWrapper[SIZE];
219             arr2 = new IntWrapper[SIZE];
220             populate(arr1, arr2, 50);
221         }
222     }
223 
224     public static class IntState75 extends IntState {
225         @Setup
226         public void setup() {
227             arr1 = new IntWrapper[SIZE];
228             arr2 = new IntWrapper[SIZE];
229             populate(arr1, arr2, 75);
230         }
231     }
232 
233     public static class IntState100 extends IntState {
234         @Setup
235         public void setup() {
236             arr1 = new IntWrapper[SIZE];
237             arr2 = new IntWrapper[SIZE];
238             populate(arr1, arr2, 100);
239         }
240     }
241 
242     public static class RefState00 extends RefState {
243         @Setup
244         public void setup() {
245             arr1 = new RefWrapper[SIZE];
246             arr2 = new RefWrapper[SIZE];
247             populate(arr1, arr2, 0);
248         }
249     }
250 
251     public static class RefState25 extends RefState {
252         @Setup
253         public void setup() {
254             arr1 = new RefWrapper[SIZE];
255             arr2 = new RefWrapper[SIZE];
256             populate(arr1, arr2, 25);
257         }
258     }
259 
260     public static class RefState50 extends RefState {
261         @Setup
262         public void setup() {
263             arr1 = new RefWrapper[SIZE];
264             arr2 = new RefWrapper[SIZE];
265             populate(arr1, arr2, 50);
266         }
267     }
268 
269     public static class RefState75 extends RefState {
270         @Setup
271         public void setup() {
272             arr1 = new RefWrapper[SIZE];
273             arr2 = new RefWrapper[SIZE];
274             populate(arr1, arr2, 75);
275         }
276     }
277 
278     public static class RefState100 extends RefState {
279         @Setup
280         public void setup() {
281             arr1 = new RefWrapper[SIZE];
282             arr2 = new RefWrapper[SIZE];
283             populate(arr1, arr2, 100);
284         }
285     }
286 
287     public static class ValState00 extends ValState {
288         @Setup
289         public void setup() {
290             arr1 = new ValWrapper[SIZE];
291             arr2 = new ValWrapper[SIZE];
292             populate(arr1, arr2, 0);
293         }
294     }
295 
296     public static class ValState25 extends ValState {
297         @Setup
298         public void setup() {
299             arr1 = new ValWrapper[SIZE];
300             arr2 = new ValWrapper[SIZE];
301             populate(arr1, arr2, 25);
302         }
303     }
304 
305     public static class ValState50 extends ValState {
306         @Setup
307         public void setup() {
308             arr1 = new ValWrapper[SIZE];
309             arr2 = new ValWrapper[SIZE];
310             populate(arr1, arr2, 50);
311         }
312     }
313 
314     public static class ValState75 extends ValState {
315         @Setup
316         public void setup() {
317             arr1 = new ValWrapper[SIZE];
318             arr2 = new ValWrapper[SIZE];
319             populate(arr1, arr2, 75);
320         }
321     }
322 
323     public static class ValState100 extends ValState {
324         @Setup
325         public void setup() {
326             arr1 = new ValWrapper[SIZE];
327             arr2 = new ValWrapper[SIZE];
328             populate(arr1, arr2, 100);
329         }
330     }
331 
332 }