1 /*
  2  * Copyright (c) 2020, 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.array;
 24 
 25 import org.openjdk.bench.valhalla.types.R64long;
 26 import org.openjdk.jmh.annotations.Scope;
 27 import org.openjdk.jmh.annotations.Setup;
 28 import org.openjdk.jmh.annotations.State;
 29 
 30 import java.util.BitSet;
 31 import java.util.Random;
 32 
 33 public class StatesR64long {
 34 
 35     public static final int SIZE = 100;
 36 
 37     private static BitSet indices(Random rnd, int bound, int size) {
 38         return rnd.ints(0, bound)
 39                 .distinct()
 40                 .limit(size)
 41                 .collect(BitSet::new, BitSet::set, BitSet::or);
 42     }
 43 
 44     private static void populate(Object[] arr1, Object[] arr2, int eq) {
 45         BitSet eqset = (eq > 0 && eq < 100) ? indices(new Random(42), SIZE, (eq * SIZE) / 100) : null;
 46         int samenulls = 0;
 47         int distinctnulls = 0;
 48         for (int i = 0; i < SIZE; i++) {
 49             if (eq > 0 && (eq >= 100 || eqset.get(i))) {
 50                 if (samenulls > 0) {
 51                     arr2[i] = arr1[i] = new R64long(i);
 52                 } else {
 53                     arr2[i] = arr1[i] = null;
 54                     samenulls++;
 55                 }
 56             } else {
 57                 if (distinctnulls > 1) {
 58                     arr1[i] = new R64long(2 * i);
 59                     arr2[i] = new R64long(2 * i + 1);
 60                 } else {
 61                     if (distinctnulls == 0) {
 62                         arr1[i] = null;
 63                         arr2[i] = new R64long(2 * i + 1);
 64                     } else {
 65                         arr1[i] = new R64long(2 * i);
 66                         arr2[i] = null;
 67                     }
 68                     distinctnulls++;
 69                 }
 70             }
 71         }
 72     }
 73 
 74     @State(Scope.Thread)
 75     public abstract static class ObjState {
 76         Object[] arr1, arr2;
 77     }
 78 
 79     @State(Scope.Thread)
 80     public abstract static class RefState {
 81         R64long[] arr1, arr2;
 82     }
 83 
 84     public static class ObjState00 extends ObjState {
 85         @Setup
 86         public void setup() {
 87             arr1 = new Object[SIZE];
 88             arr2 = new Object[SIZE];
 89             populate(arr1, arr2, 0);
 90         }
 91 
 92     }
 93 
 94     public static class ObjState25 extends ObjState {
 95         @Setup
 96         public void setup() {
 97             arr1 = new Object[SIZE];
 98             arr2 = new Object[SIZE];
 99             populate(arr1, arr2, 25);
100         }
101 
102     }
103 
104     public static class ObjState50 extends ObjState {
105         @Setup
106         public void setup() {
107             arr1 = new Object[SIZE];
108             arr2 = new Object[SIZE];
109             populate(arr1, arr2, 50);
110         }
111 
112     }
113 
114     public static class ObjState75 extends ObjState {
115         @Setup
116         public void setup() {
117             arr1 = new Object[SIZE];
118             arr2 = new Object[SIZE];
119             populate(arr1, arr2, 75);
120         }
121 
122     }
123 
124     public static class ObjState100 extends ObjState {
125         @Setup
126         public void setup() {
127             arr1 = new Object[SIZE];
128             arr2 = new Object[SIZE];
129             populate(arr1, arr2, 100);
130         }
131     }
132 
133     public static class RefState00 extends RefState {
134         @Setup
135         public void setup() {
136             arr1 = new R64long[SIZE];
137             arr2 = new R64long[SIZE];
138             populate(arr1, arr2, 0);
139         }
140     }
141 
142     public static class RefState25 extends RefState {
143         @Setup
144         public void setup() {
145             arr1 = new R64long[SIZE];
146             arr2 = new R64long[SIZE];
147             populate(arr1, arr2, 25);
148         }
149     }
150 
151     public static class RefState50 extends RefState {
152         @Setup
153         public void setup() {
154             arr1 = new R64long[SIZE];
155             arr2 = new R64long[SIZE];
156             populate(arr1, arr2, 50);
157         }
158     }
159 
160     public static class RefState75 extends RefState {
161         @Setup
162         public void setup() {
163             arr1 = new R64long[SIZE];
164             arr2 = new R64long[SIZE];
165             populate(arr1, arr2, 75);
166         }
167     }
168 
169     public static class RefState100 extends RefState {
170         @Setup
171         public void setup() {
172             arr1 = new R64long[SIZE];
173             arr2 = new R64long[SIZE];
174             populate(arr1, arr2, 100);
175         }
176     }
177 
178 }