aboutsummaryrefslogtreecommitdiff
path: root/benchmarking/java-ubenchs/benchmarks/micro/Intrinsics.java
blob: 630aefe931bc1806cfd292dcb106135859323cc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 *    Copyright 2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package benchmarks.micro;

import java.lang.System;
import java.util.Random;
import java.util.EnumMap;
import java.util.Map;

public class Intrinsics {

  /* Invoke each intrinsic in question the same no. of times */
  private static final int NUM_INVOKES = 64;

  /* Random pool size */
  private static final int NUM_RANDS = 1000;

  /* Pre-allocated pool of random integers from [0, Integer.MAX_VALUE) */
  private static final int rand[] = new int[NUM_RANDS];

  static {
    // Allocate a pool of random integers to use in benchmarks that
    // want to try and force branch mispredicts without the overhead
    // of calling random.nextInt in times code. Same seed every time.
    Random random = new Random(42L);
    for (int i = 0; i < NUM_RANDS; i++) {
      rand[i] = random.nextInt(Integer.MAX_VALUE);
    }
  }

  /**
   * NumberOfLeadingZeros
   **/

  private static int[] resultsNumberOfLeadingZerosInteger = new int[NUM_INVOKES];
  private static int[] resultsNumberOfLeadingZerosLong = new int[NUM_INVOKES];
  private static int[] resultsNumberOfLeadingZerosIntegerRandom = new int[NUM_INVOKES];
  private static int[] resultsNumberOfLeadingZerosLongRandom = new int[NUM_INVOKES];

  public void timeNumberOfLeadingZerosInteger(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
        resultsNumberOfLeadingZerosInteger[i] =
          Integer.numberOfLeadingZeros(0x80000000 >>> i);
      }
    }
  }
  public void timeNumberOfLeadingZerosLong(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
        resultsNumberOfLeadingZerosLong[i] =
          Long.numberOfLeadingZeros(0x8000000000000000L >>> i);
      }
    }
  }
  public void timeNumberOfLeadingZerosIntegerRandom(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsNumberOfLeadingZerosIntegerRandom[i] =
            Integer.numberOfLeadingZeros(rand[i % NUM_RANDS] >>> rand[i % NUM_RANDS]);
      }
    }
  }
  public void timeNumberOfLeadingZerosLongRandom(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsNumberOfLeadingZerosLongRandom[i] =
            Long.numberOfLeadingZeros(1 << (rand[i % NUM_RANDS] % Long.SIZE));
      }
    }
  }

  /**
   * NumberOfTrailingZeros
   **/

  private static int[] resultsNumberOfTrailingZerosInteger = new int[NUM_INVOKES];
  private static int[] resultsNumberOfTrailingZerosLong = new int[NUM_INVOKES];
  private static int[] resultsNumberOfTrailingZerosIntegerRandom = new int[NUM_INVOKES];
  private static int[] resultsNumberOfTrailingZerosLongRandom = new int[NUM_INVOKES];

  public void timeNumberOfTrailingZerosInteger(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
        resultsNumberOfTrailingZerosInteger[i] =
            Integer.numberOfTrailingZeros(0x80000000 >>> i);
      }
    }
  }
  public void timeNumberOfTrailingZerosLong(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
        resultsNumberOfTrailingZerosLong[i] =
            Long.numberOfTrailingZeros(0x8000000000000000L >>> i);
      }
    }
  }
  public void timeNumberOfTrailingZerosIntegerRandom(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
        resultsNumberOfTrailingZerosIntegerRandom[i] =
            Integer.numberOfTrailingZeros(rand[i % NUM_RANDS] >>> rand[i % NUM_RANDS]);
      }
    }
  }
  public void timeNumberOfTrailingZerosLongRandom(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
        resultsNumberOfTrailingZerosLongRandom[i] =
            Long.numberOfTrailingZeros(1 << (rand[i % NUM_RANDS] % Long.SIZE));
      }
    }
  }

  /**
   * RotateRight
   **/

  private static int[] resultsRotateRightInteger = new int[NUM_INVOKES];
  private static int[] resultsRotateRightIntegerConstant = new int[NUM_INVOKES];
  private static long[] resultsRotateRightLong = new long[NUM_INVOKES];
  private static long[] resultsRotateRightLongConstant = new long[NUM_INVOKES];

  public void timeRotateRightInteger(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateRightInteger[i] = Integer.rotateRight(0xFF0000DD, i);
      }
    }
  }
  public void timeRotateRightIntegerConstant(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateRightIntegerConstant[i] = Integer.rotateRight(0xFF0000DD, 16);
      }
    }
  }
  public void timeRotateRightLong(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateRightLong[i] = Long.rotateRight(0xBBAAAADDFF0000DDL, i);
      }
    }
  }
  public void timeRotateRightLongConstant(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateRightLongConstant[i] = Long.rotateRight(0xBBAAAADDFF0000DDL, 48);
      }
    }
  }

  /**
   * RotateLeft
   **/

  private static int[] resultsRotateLeftInteger = new int[NUM_INVOKES];
  private static int[] resultsRotateLeftIntegerConstant = new int[NUM_INVOKES];
  private static long[] resultsRotateLeftLong = new long[NUM_INVOKES];
  private static long[] resultsRotateLeftLongConstant = new long[NUM_INVOKES];

  public void timeRotateLeftInteger(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateLeftInteger[i] = Integer.rotateLeft(0xFF0000DD, i);
      }
    }
  }
  public void timeRotateLeftIntegerConstant(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateLeftIntegerConstant[i] = Integer.rotateLeft(0xFF0000DD, 16);
      }
    }
  }
  public void timeRotateLeftLong(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateLeftLong[i] = Long.rotateLeft(0xBBAAAADDFF0000DDL, i);
      }
    }
  }
  public void timeRotateLeftLongConstant(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateLeftLongConstant[i] = Long.rotateLeft(0xBBAAAADDFF0000DDL, 48);
      }
    }
  }

  /**
   * RotateRandom
   **/

  private static int[] resultsRotateRandomInteger = new int[NUM_INVOKES];
  private static long[] resultsRotateRandomLong = new long[NUM_INVOKES];

  public void timeRotateRandomInteger(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateRandomInteger[i] = (rand[i % NUM_RANDS] % 2 > 0)
            ? Integer.rotateLeft(0xFF0000DD, rand[i % NUM_RANDS])
            : Integer.rotateRight(0xFF0000DD, rand[i % NUM_RANDS]);
      }
    }
  }

  public void timeRotateRandomLong(int iterations) {
    for(int iter = 0; iter < iterations; ++iter) {
      for(int i = 0; i < NUM_INVOKES; ++i) {
          resultsRotateRandomLong[i] = (rand[i % NUM_RANDS] % 2 > 0)
            ? Long.rotateLeft(0xBBAAAADDFF0000DDL, rand[i % NUM_RANDS])
            : Long.rotateRight(0xBBAAAADDFF0000DDL, rand[i % NUM_RANDS]);
      }
    }
  }

  /**
   * Verify
   **/

  /**
   * Called by the framework to assert the benchmarks have done the right thing.
   **/
  public boolean verify() {
    return true;
  }

  /**
   * *NOT* called by the framework by default, provided for direct use only.
   **/
  public static void main(String[] args) {
    Intrinsics obj = new Intrinsics();
    long before = System.currentTimeMillis();
    obj.timeNumberOfLeadingZerosInteger(100000);
    obj.timeNumberOfLeadingZerosIntegerRandom(100000);
    obj.timeNumberOfLeadingZerosLong(100000);
    obj.timeNumberOfLeadingZerosLongRandom(100000);

    obj.timeNumberOfTrailingZerosInteger(100000);
    obj.timeNumberOfTrailingZerosIntegerRandom(100000);
    obj.timeNumberOfTrailingZerosLong(100000);
    obj.timeNumberOfTrailingZerosLongRandom(100000);

    obj.timeRotateRightInteger(100000);
    obj.timeRotateRightLong(100000);
    obj.timeRotateLeftInteger(100000);
    obj.timeRotateLeftLong(100000);

    obj.timeRotateRandomInteger(100000);
    obj.timeRotateRandomLong(100000);

    long after = System.currentTimeMillis();
    System.out.println("Intrinsics: " + (after - before));
  }
}