Path: blob/master/test/micro/org/openjdk/bench/java/lang/MathBench.java
41161 views
/*1* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.lang;2324import java.util.concurrent.TimeUnit;25import org.openjdk.jmh.annotations.Benchmark;26import org.openjdk.jmh.annotations.BenchmarkMode;27import org.openjdk.jmh.annotations.CompilerControl;28import org.openjdk.jmh.annotations.Fork;29import org.openjdk.jmh.annotations.Measurement;30import org.openjdk.jmh.annotations.Mode;31import org.openjdk.jmh.annotations.OutputTimeUnit;32import org.openjdk.jmh.annotations.Param;33import org.openjdk.jmh.annotations.Scope;34import org.openjdk.jmh.annotations.Setup;35import org.openjdk.jmh.annotations.State;36import org.openjdk.jmh.annotations.Threads;37import org.openjdk.jmh.annotations.Warmup;3839import java.util.Random;40import java.util.concurrent.TimeUnit;4142@Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS)43@Measurement(iterations = 4, time = 5, timeUnit = TimeUnit.SECONDS)44@Fork(2)45@OutputTimeUnit(TimeUnit.MILLISECONDS)46@BenchmarkMode(Mode.Throughput)47@State(Scope.Thread)48public class MathBench {4950@Param("0")51public long seed;5253public int dividend;54public int divisor;5556public long longDividend;57public long longDivisor;5859public int int1 = 1, int2 = 2, int42 = 42, int5 = 5;60public long long1 = 1L, long2 = 2L, long747 = 747L, long13 = 13L;61public float float1 = 1.0f, float2 = 2.0f, floatNegative99 = -99.0f, float7 = 7.0f, eFloat = 2.718f;62public double double1 = 1.0d, double2 = 2.0d, double81 = 81.0d, doubleNegative12 = -12.0d, double4Dot1 = 4.1d, double0Dot5 = 0.5d;6364@Setup65public void setupValues() {66Random random = new Random(seed);67dividend = Math.abs(random.nextInt() + 4711);68divisor = Math.abs(random.nextInt(dividend) + 17);69longDividend = Math.abs(random.nextLong() + 4711L);70longDivisor = Math.abs(random.nextLong() + longDividend);71}7273@Benchmark74public double absDouble() {75return Math.abs(doubleNegative12);76}7778@Benchmark79public float absFloat() {80return Math.abs(floatNegative99);81}8283@Benchmark84public int absExactInt() {85return Math.absExact(int2);86}8788@Benchmark89public long absExactLong() {90return Math.absExact(long2);91}9293@Benchmark94public int absInt() {95return Math.abs(int42);96}9798@Benchmark99public long absLong() {100return Math.abs(long13);101}102103@Benchmark104public double acosDouble() {105return Math.acos(double1);106}107108@Benchmark109public int addExactInt() {110return Math.addExact(int42, int5);111}112113@Benchmark114public long addExactLong() {115return Math.addExact(long2, long13);116}117118@Benchmark119public double asinDouble() {120return Math.asin(double1);121}122123@Benchmark124public double atanDouble() {125return Math.atan(double1);126}127128@Benchmark129public double atan2Double() {130return Math.atan2(double1, double2);131}132133@Benchmark134public double cbrt() {135return Math.cbrt(double81);136}137138@Benchmark139public double ceilDouble() {140return Math.ceil(double4Dot1);141}142143@Benchmark144public double copySignDouble() {145return Math.copySign(double81, doubleNegative12);146}147148@Benchmark149public float copySignFloat() {150return Math.copySign(floatNegative99, float1);151}152153@Benchmark154public double cosDouble() {155return Math.cos(double1);156}157158@Benchmark159public double coshDouble() {160return Math.cosh(double2);161}162163@Benchmark164public int decrementExactInt() {165return Math.decrementExact(int42);166}167168@Benchmark169public long decrementExactLong() {170return Math.decrementExact(long747);171}172173@Benchmark174public double expDouble() {175return Math.exp(double4Dot1);176}177178@Benchmark179public double expm1() {180return Math.expm1(doubleNegative12);181}182183@Benchmark184public double floorDouble() {185return Math.floor(doubleNegative12);186}187188@Benchmark189public int floorDivIntInt() {190return Math.floorDiv(int42, int5);191}192193@Benchmark194public long floorDivLongInt() {195return Math.floorDiv(long747, int42);196}197198@Benchmark199public long floorDivLongLong() {200return Math.floorDiv(long747, long13);201}202203@Benchmark204public int floorModIntInt() {205return Math.floorMod(int42, int5);206}207208@Benchmark209@CompilerControl(CompilerControl.Mode.DONT_INLINE)210public int floorModIntIntMultiple() {211return Math.floorMod( dividend, divisor) +212Math.floorMod( dividend, -divisor) +213Math.floorMod(-dividend, divisor) +214Math.floorMod(-dividend, -divisor);215}216217@Benchmark218public int floorModLongInt() {219return Math.floorMod(long747, int5);220}221222@Benchmark223@CompilerControl(CompilerControl.Mode.DONT_INLINE)224public int floorModLongIntMultiple() {225return Math.floorMod( longDividend, divisor) +226Math.floorMod( longDividend, -divisor) +227Math.floorMod(-longDividend, divisor) +228Math.floorMod(-longDividend, -divisor);229}230231@Benchmark232public long floorModLongLong() {233return Math.floorMod(long747, long13);234}235236@Benchmark237@CompilerControl(CompilerControl.Mode.DONT_INLINE)238public long floorModLongLongMultiple() {239return Math.floorMod( longDividend, longDivisor) +240Math.floorMod( longDividend, -longDivisor) +241Math.floorMod(-longDividend, longDivisor) +242Math.floorMod(-longDividend, -longDivisor);243}244245@Benchmark246public double fmaDouble() {247return Math.fma(double2, double81, double4Dot1);248}249250@Benchmark251public float fmaFloat() {252return Math.fma(float2, floatNegative99, float7);253}254255@Benchmark256public int getExponentDouble() {257return Math.getExponent(double81);258}259260@Benchmark261public int getExponentFloat() {262return Math.getExponent(float7);263}264265@Benchmark266public double hypotDouble() {267return Math.hypot(double2, double4Dot1);268}269270@Benchmark271public double IEEERemainderDouble() {272return Math.IEEEremainder(double81, double4Dot1);273}274275@Benchmark276public int incrementExactInt() {277return Math.incrementExact(int42);278}279280@Benchmark281public long incrementExactLong() {282return Math.incrementExact(long747);283}284285@Benchmark286public double logDouble() {287return Math.log(double81);288}289290@Benchmark291public double log10Double() {292return Math.log10(double81);293}294295@Benchmark296public double log1pDouble() {297return Math.log1p(double81);298}299300@Benchmark301public int maxInt() {302return Math.max(int1, int2);303}304305@Benchmark306public long maxLong() {307return Math.max(long1, long2);308}309310@Benchmark311public float maxFloat() {312return Math.max(float1, float2);313}314315@Benchmark316public double maxDouble() {317return Math.max(double1, doubleNegative12);318}319320@Benchmark321public int minInt() {322return Math.min(int1, int2);323}324325@Benchmark326public long minLong() {327return Math.min(long1, long2);328}329330@Benchmark331public float minFloat() {332return Math.min(float1, floatNegative99);333}334335@Benchmark336public double minDouble() {337return Math.min(double4Dot1, double2);338}339340@Benchmark341public int multiplyExactInt() {342return Math.multiplyExact(int42, int5);343}344345@Benchmark346public long multiplyExactLongInt() {347return Math.multiplyExact(long747, int42);348}349350@Benchmark351public long multiplyExactLongLong() {352return Math.multiplyExact(long747, long13);353}354355@Benchmark356public long multiplyFullIntInt() {357return Math.multiplyFull(int42, int5);358}359360@Benchmark361public long multiplyHighLongLog() {362return Math.multiplyHigh(long747, long13);363}364365@Benchmark366public int negateExactInt() {367return Math.negateExact(int42);368}369370@Benchmark371public long negateExactLong() {372return Math.negateExact(long747);373}374375@Benchmark376public double nextAfterDoubleDouble() {377return Math.nextAfter(double81, double4Dot1);378}379380@Benchmark381public float nextAfterFloatDouble() {382return Math.nextAfter(float7, doubleNegative12);383}384385@Benchmark386public double nextDownDouble() {387return Math.nextDown(float7);388}389390@Benchmark391public float nextDownFloat() {392return Math.nextDown(floatNegative99);393}394395@Benchmark396public double nextUpDouble() {397return Math.nextUp(double81);398}399400@Benchmark401public float nextUpFloat() {402return Math.nextUp(float7);403}404405@Benchmark406public double powDouble() {407return Math.pow(double4Dot1, double2);408}409410@Benchmark411public double powDoubleLoop() {412double sum = 0.0;413for (int i = 0; i < 1000; i++) {414for (int j = 0; j < 1000; j++) {415sum += i + Math.pow(j * 1.0, i * 1.0);416}417}418return sum;419}420421@Benchmark422public double powDouble0Dot5() {423return Math.pow(double4Dot1, double0Dot5);424}425426@Benchmark427public double powDouble0Dot5Const() {428return Math.pow(double4Dot1, 0.5);429}430431@Benchmark432public double powDouble0Dot5Loop() {433double sum = 0.0;434for (int i = 0; i < 1000; i++) {435for (int j = 0; j < 1000; j++) {436sum += i + Math.pow(j * 1.0, 0.5);437}438}439return sum;440}441442@Benchmark443public double random() {444return Math.random();445}446447@Benchmark448public double rintDouble() {449return Math.rint(double4Dot1);450}451452@Benchmark453public long roundDouble() {454return Math.round( Math.PI);455}456457@Benchmark458public int roundFloat() {459return Math.round(eFloat);460}461462@Benchmark463public double scalbDoubleInt() {464return Math.scalb(double81, int2);465}466467@Benchmark468public float scalbFloatInt() {469return Math.scalb(float7, int2);470}471472@Benchmark473public double sigNumDouble() {474return Math.signum(double4Dot1);475}476477@Benchmark478public double signumFloat() {479return Math.signum(floatNegative99);480}481482@Benchmark483public double sinDouble() {484return Math.sin(double1);485}486487@Benchmark488public double sinhDouble() {489return Math.sinh(double4Dot1);490}491492@Benchmark493public double sqrtDouble() {494return Math.sqrt(double4Dot1);495}496497@Benchmark498public double subtractExactIntInt() {499return Math.subtractExact(int42,int5);500}501502@Benchmark503public double subtractExactLongLong() {504return Math.subtractExact(long747,long13);505}506507@Benchmark508public double tanDouble() {509return Math.tan(double1);510}511512@Benchmark513public double tanhDouble() {514return Math.tanh(double1);515}516517@Benchmark518public double toDegreesDouble() {519return Math.toDegrees(double81);520}521522@Benchmark523public double toIntExactLong() {524return Math.toIntExact(long747);525}526527@Benchmark528public double toRadiansDouble() {529return Math.toRadians(double81);530}531532@Benchmark533public double ulpDouble() {534return Math.ulp(double4Dot1);535}536537@Benchmark538public double ulpFloat() {539return Math.ulp(float7);540}541542}543544545