Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/micro/org/openjdk/bench/java/lang/MathBench.java
41161 views
1
/*
2
* Copyright (c) 2014, 2021, 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.java.lang;
24
25
import java.util.concurrent.TimeUnit;
26
import org.openjdk.jmh.annotations.Benchmark;
27
import org.openjdk.jmh.annotations.BenchmarkMode;
28
import org.openjdk.jmh.annotations.CompilerControl;
29
import org.openjdk.jmh.annotations.Fork;
30
import org.openjdk.jmh.annotations.Measurement;
31
import org.openjdk.jmh.annotations.Mode;
32
import org.openjdk.jmh.annotations.OutputTimeUnit;
33
import org.openjdk.jmh.annotations.Param;
34
import org.openjdk.jmh.annotations.Scope;
35
import org.openjdk.jmh.annotations.Setup;
36
import org.openjdk.jmh.annotations.State;
37
import org.openjdk.jmh.annotations.Threads;
38
import org.openjdk.jmh.annotations.Warmup;
39
40
import java.util.Random;
41
import java.util.concurrent.TimeUnit;
42
43
@Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS)
44
@Measurement(iterations = 4, time = 5, timeUnit = TimeUnit.SECONDS)
45
@Fork(2)
46
@OutputTimeUnit(TimeUnit.MILLISECONDS)
47
@BenchmarkMode(Mode.Throughput)
48
@State(Scope.Thread)
49
public class MathBench {
50
51
@Param("0")
52
public long seed;
53
54
public int dividend;
55
public int divisor;
56
57
public long longDividend;
58
public long longDivisor;
59
60
public int int1 = 1, int2 = 2, int42 = 42, int5 = 5;
61
public long long1 = 1L, long2 = 2L, long747 = 747L, long13 = 13L;
62
public float float1 = 1.0f, float2 = 2.0f, floatNegative99 = -99.0f, float7 = 7.0f, eFloat = 2.718f;
63
public double double1 = 1.0d, double2 = 2.0d, double81 = 81.0d, doubleNegative12 = -12.0d, double4Dot1 = 4.1d, double0Dot5 = 0.5d;
64
65
@Setup
66
public void setupValues() {
67
Random random = new Random(seed);
68
dividend = Math.abs(random.nextInt() + 4711);
69
divisor = Math.abs(random.nextInt(dividend) + 17);
70
longDividend = Math.abs(random.nextLong() + 4711L);
71
longDivisor = Math.abs(random.nextLong() + longDividend);
72
}
73
74
@Benchmark
75
public double absDouble() {
76
return Math.abs(doubleNegative12);
77
}
78
79
@Benchmark
80
public float absFloat() {
81
return Math.abs(floatNegative99);
82
}
83
84
@Benchmark
85
public int absExactInt() {
86
return Math.absExact(int2);
87
}
88
89
@Benchmark
90
public long absExactLong() {
91
return Math.absExact(long2);
92
}
93
94
@Benchmark
95
public int absInt() {
96
return Math.abs(int42);
97
}
98
99
@Benchmark
100
public long absLong() {
101
return Math.abs(long13);
102
}
103
104
@Benchmark
105
public double acosDouble() {
106
return Math.acos(double1);
107
}
108
109
@Benchmark
110
public int addExactInt() {
111
return Math.addExact(int42, int5);
112
}
113
114
@Benchmark
115
public long addExactLong() {
116
return Math.addExact(long2, long13);
117
}
118
119
@Benchmark
120
public double asinDouble() {
121
return Math.asin(double1);
122
}
123
124
@Benchmark
125
public double atanDouble() {
126
return Math.atan(double1);
127
}
128
129
@Benchmark
130
public double atan2Double() {
131
return Math.atan2(double1, double2);
132
}
133
134
@Benchmark
135
public double cbrt() {
136
return Math.cbrt(double81);
137
}
138
139
@Benchmark
140
public double ceilDouble() {
141
return Math.ceil(double4Dot1);
142
}
143
144
@Benchmark
145
public double copySignDouble() {
146
return Math.copySign(double81, doubleNegative12);
147
}
148
149
@Benchmark
150
public float copySignFloat() {
151
return Math.copySign(floatNegative99, float1);
152
}
153
154
@Benchmark
155
public double cosDouble() {
156
return Math.cos(double1);
157
}
158
159
@Benchmark
160
public double coshDouble() {
161
return Math.cosh(double2);
162
}
163
164
@Benchmark
165
public int decrementExactInt() {
166
return Math.decrementExact(int42);
167
}
168
169
@Benchmark
170
public long decrementExactLong() {
171
return Math.decrementExact(long747);
172
}
173
174
@Benchmark
175
public double expDouble() {
176
return Math.exp(double4Dot1);
177
}
178
179
@Benchmark
180
public double expm1() {
181
return Math.expm1(doubleNegative12);
182
}
183
184
@Benchmark
185
public double floorDouble() {
186
return Math.floor(doubleNegative12);
187
}
188
189
@Benchmark
190
public int floorDivIntInt() {
191
return Math.floorDiv(int42, int5);
192
}
193
194
@Benchmark
195
public long floorDivLongInt() {
196
return Math.floorDiv(long747, int42);
197
}
198
199
@Benchmark
200
public long floorDivLongLong() {
201
return Math.floorDiv(long747, long13);
202
}
203
204
@Benchmark
205
public int floorModIntInt() {
206
return Math.floorMod(int42, int5);
207
}
208
209
@Benchmark
210
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
211
public int floorModIntIntMultiple() {
212
return Math.floorMod( dividend, divisor) +
213
Math.floorMod( dividend, -divisor) +
214
Math.floorMod(-dividend, divisor) +
215
Math.floorMod(-dividend, -divisor);
216
}
217
218
@Benchmark
219
public int floorModLongInt() {
220
return Math.floorMod(long747, int5);
221
}
222
223
@Benchmark
224
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
225
public int floorModLongIntMultiple() {
226
return Math.floorMod( longDividend, divisor) +
227
Math.floorMod( longDividend, -divisor) +
228
Math.floorMod(-longDividend, divisor) +
229
Math.floorMod(-longDividend, -divisor);
230
}
231
232
@Benchmark
233
public long floorModLongLong() {
234
return Math.floorMod(long747, long13);
235
}
236
237
@Benchmark
238
@CompilerControl(CompilerControl.Mode.DONT_INLINE)
239
public long floorModLongLongMultiple() {
240
return Math.floorMod( longDividend, longDivisor) +
241
Math.floorMod( longDividend, -longDivisor) +
242
Math.floorMod(-longDividend, longDivisor) +
243
Math.floorMod(-longDividend, -longDivisor);
244
}
245
246
@Benchmark
247
public double fmaDouble() {
248
return Math.fma(double2, double81, double4Dot1);
249
}
250
251
@Benchmark
252
public float fmaFloat() {
253
return Math.fma(float2, floatNegative99, float7);
254
}
255
256
@Benchmark
257
public int getExponentDouble() {
258
return Math.getExponent(double81);
259
}
260
261
@Benchmark
262
public int getExponentFloat() {
263
return Math.getExponent(float7);
264
}
265
266
@Benchmark
267
public double hypotDouble() {
268
return Math.hypot(double2, double4Dot1);
269
}
270
271
@Benchmark
272
public double IEEERemainderDouble() {
273
return Math.IEEEremainder(double81, double4Dot1);
274
}
275
276
@Benchmark
277
public int incrementExactInt() {
278
return Math.incrementExact(int42);
279
}
280
281
@Benchmark
282
public long incrementExactLong() {
283
return Math.incrementExact(long747);
284
}
285
286
@Benchmark
287
public double logDouble() {
288
return Math.log(double81);
289
}
290
291
@Benchmark
292
public double log10Double() {
293
return Math.log10(double81);
294
}
295
296
@Benchmark
297
public double log1pDouble() {
298
return Math.log1p(double81);
299
}
300
301
@Benchmark
302
public int maxInt() {
303
return Math.max(int1, int2);
304
}
305
306
@Benchmark
307
public long maxLong() {
308
return Math.max(long1, long2);
309
}
310
311
@Benchmark
312
public float maxFloat() {
313
return Math.max(float1, float2);
314
}
315
316
@Benchmark
317
public double maxDouble() {
318
return Math.max(double1, doubleNegative12);
319
}
320
321
@Benchmark
322
public int minInt() {
323
return Math.min(int1, int2);
324
}
325
326
@Benchmark
327
public long minLong() {
328
return Math.min(long1, long2);
329
}
330
331
@Benchmark
332
public float minFloat() {
333
return Math.min(float1, floatNegative99);
334
}
335
336
@Benchmark
337
public double minDouble() {
338
return Math.min(double4Dot1, double2);
339
}
340
341
@Benchmark
342
public int multiplyExactInt() {
343
return Math.multiplyExact(int42, int5);
344
}
345
346
@Benchmark
347
public long multiplyExactLongInt() {
348
return Math.multiplyExact(long747, int42);
349
}
350
351
@Benchmark
352
public long multiplyExactLongLong() {
353
return Math.multiplyExact(long747, long13);
354
}
355
356
@Benchmark
357
public long multiplyFullIntInt() {
358
return Math.multiplyFull(int42, int5);
359
}
360
361
@Benchmark
362
public long multiplyHighLongLog() {
363
return Math.multiplyHigh(long747, long13);
364
}
365
366
@Benchmark
367
public int negateExactInt() {
368
return Math.negateExact(int42);
369
}
370
371
@Benchmark
372
public long negateExactLong() {
373
return Math.negateExact(long747);
374
}
375
376
@Benchmark
377
public double nextAfterDoubleDouble() {
378
return Math.nextAfter(double81, double4Dot1);
379
}
380
381
@Benchmark
382
public float nextAfterFloatDouble() {
383
return Math.nextAfter(float7, doubleNegative12);
384
}
385
386
@Benchmark
387
public double nextDownDouble() {
388
return Math.nextDown(float7);
389
}
390
391
@Benchmark
392
public float nextDownFloat() {
393
return Math.nextDown(floatNegative99);
394
}
395
396
@Benchmark
397
public double nextUpDouble() {
398
return Math.nextUp(double81);
399
}
400
401
@Benchmark
402
public float nextUpFloat() {
403
return Math.nextUp(float7);
404
}
405
406
@Benchmark
407
public double powDouble() {
408
return Math.pow(double4Dot1, double2);
409
}
410
411
@Benchmark
412
public double powDoubleLoop() {
413
double sum = 0.0;
414
for (int i = 0; i < 1000; i++) {
415
for (int j = 0; j < 1000; j++) {
416
sum += i + Math.pow(j * 1.0, i * 1.0);
417
}
418
}
419
return sum;
420
}
421
422
@Benchmark
423
public double powDouble0Dot5() {
424
return Math.pow(double4Dot1, double0Dot5);
425
}
426
427
@Benchmark
428
public double powDouble0Dot5Const() {
429
return Math.pow(double4Dot1, 0.5);
430
}
431
432
@Benchmark
433
public double powDouble0Dot5Loop() {
434
double sum = 0.0;
435
for (int i = 0; i < 1000; i++) {
436
for (int j = 0; j < 1000; j++) {
437
sum += i + Math.pow(j * 1.0, 0.5);
438
}
439
}
440
return sum;
441
}
442
443
@Benchmark
444
public double random() {
445
return Math.random();
446
}
447
448
@Benchmark
449
public double rintDouble() {
450
return Math.rint(double4Dot1);
451
}
452
453
@Benchmark
454
public long roundDouble() {
455
return Math.round( Math.PI);
456
}
457
458
@Benchmark
459
public int roundFloat() {
460
return Math.round(eFloat);
461
}
462
463
@Benchmark
464
public double scalbDoubleInt() {
465
return Math.scalb(double81, int2);
466
}
467
468
@Benchmark
469
public float scalbFloatInt() {
470
return Math.scalb(float7, int2);
471
}
472
473
@Benchmark
474
public double sigNumDouble() {
475
return Math.signum(double4Dot1);
476
}
477
478
@Benchmark
479
public double signumFloat() {
480
return Math.signum(floatNegative99);
481
}
482
483
@Benchmark
484
public double sinDouble() {
485
return Math.sin(double1);
486
}
487
488
@Benchmark
489
public double sinhDouble() {
490
return Math.sinh(double4Dot1);
491
}
492
493
@Benchmark
494
public double sqrtDouble() {
495
return Math.sqrt(double4Dot1);
496
}
497
498
@Benchmark
499
public double subtractExactIntInt() {
500
return Math.subtractExact(int42,int5);
501
}
502
503
@Benchmark
504
public double subtractExactLongLong() {
505
return Math.subtractExact(long747,long13);
506
}
507
508
@Benchmark
509
public double tanDouble() {
510
return Math.tan(double1);
511
}
512
513
@Benchmark
514
public double tanhDouble() {
515
return Math.tanh(double1);
516
}
517
518
@Benchmark
519
public double toDegreesDouble() {
520
return Math.toDegrees(double81);
521
}
522
523
@Benchmark
524
public double toIntExactLong() {
525
return Math.toIntExact(long747);
526
}
527
528
@Benchmark
529
public double toRadiansDouble() {
530
return Math.toRadians(double81);
531
}
532
533
@Benchmark
534
public double ulpDouble() {
535
return Math.ulp(double4Dot1);
536
}
537
538
@Benchmark
539
public double ulpFloat() {
540
return Math.ulp(float7);
541
}
542
543
}
544
545