|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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.Fork; |
| 29 | +import org.openjdk.jmh.annotations.Measurement; |
| 30 | +import org.openjdk.jmh.annotations.Mode; |
| 31 | +import org.openjdk.jmh.annotations.Param; |
| 32 | +import org.openjdk.jmh.annotations.Scope; |
| 33 | +import org.openjdk.jmh.annotations.Setup; |
| 34 | +import org.openjdk.jmh.annotations.State; |
| 35 | +import org.openjdk.jmh.annotations.Level; |
| 36 | +import org.openjdk.jmh.annotations.Warmup; |
| 37 | +import org.openjdk.jmh.annotations.OperationsPerInvocation; |
| 38 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 39 | +import org.openjdk.jmh.runner.Runner; |
| 40 | +import org.openjdk.jmh.runner.RunnerException; |
| 41 | +import org.openjdk.jmh.runner.options.Options; |
| 42 | +import org.openjdk.jmh.runner.options.OptionsBuilder; |
| 43 | + |
| 44 | +import java.util.Random; |
| 45 | + |
| 46 | +public class TanhPerf { |
| 47 | + |
| 48 | + @Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.MILLISECONDS) |
| 49 | + @Measurement(iterations = 4, time = 5, timeUnit = TimeUnit.MILLISECONDS) |
| 50 | + @Fork(2) |
| 51 | + @BenchmarkMode(Mode.Throughput) |
| 52 | + @State(Scope.Thread) |
| 53 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 54 | + public static class TanhPerfRanges { |
| 55 | + public static int tanhInputCount = 2048; |
| 56 | + |
| 57 | + @Param({"0", "1", "2", "3"}) |
| 58 | + public int tanhRangeIndex; |
| 59 | + |
| 60 | + public double [] tanhPosRandInputs; |
| 61 | + public double [] tanhNegRandInputs; |
| 62 | + public int tanhInputIndex = 0; |
| 63 | + public double tanhRangeInputs[][] = {{0.0, 0x1.0P-55}, {0x1.0P-55, 1.0}, {1.0, 22.0}, {22.1, 1.7976931348623157E308} }; |
| 64 | + |
| 65 | + @Setup |
| 66 | + public void setupValues() { |
| 67 | + Random random = new Random(1023); |
| 68 | + |
| 69 | + // Fill the positive and negative tanh vectors with random values |
| 70 | + tanhPosRandInputs = new double[tanhInputCount]; |
| 71 | + tanhNegRandInputs = new double[tanhInputCount]; |
| 72 | + |
| 73 | + for (int i = 0; i < tanhInputCount; i++) { |
| 74 | + double tanhLowerBound = tanhRangeInputs[tanhRangeIndex][0]; |
| 75 | + double tanhUpperBound = tanhRangeInputs[tanhRangeIndex][1]; |
| 76 | + tanhPosRandInputs[i] = random.nextDouble(tanhLowerBound, tanhUpperBound); |
| 77 | + tanhNegRandInputs[i] = random.nextDouble(-tanhUpperBound, -tanhLowerBound); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @Benchmark |
| 82 | + @OperationsPerInvocation(2048) |
| 83 | + public double tanhPosRangeDouble() { |
| 84 | + double res = 0.0; |
| 85 | + for (int i = 0; i < tanhInputCount; i++) { |
| 86 | + res += Math.tanh(tanhPosRandInputs[i]); |
| 87 | + } |
| 88 | + return res; |
| 89 | + } |
| 90 | + |
| 91 | + @Benchmark |
| 92 | + @OperationsPerInvocation(2048) |
| 93 | + public double tanhNegRangeDouble() { |
| 94 | + double res = 0.0; |
| 95 | + for (int i = 0; i < tanhInputCount; i++) { |
| 96 | + res += Math.tanh(tanhNegRandInputs[i]); |
| 97 | + } |
| 98 | + return res; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Warmup(iterations = 3, time = 5, timeUnit = TimeUnit.SECONDS) |
| 103 | + @Measurement(iterations = 4, time = 5, timeUnit = TimeUnit.SECONDS) |
| 104 | + @Fork(2) |
| 105 | + @BenchmarkMode(Mode.Throughput) |
| 106 | + @State(Scope.Thread) |
| 107 | + @OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 108 | + public static class TanhPerfConstant { |
| 109 | + public static final double constDoubleTiny = 0x1.0P-57; |
| 110 | + public static final double constDoubleSmall = 0x1.0P-54; |
| 111 | + public static final double constDouble1 = 1.0; |
| 112 | + public static final double constDouble21 = 21.0; |
| 113 | + public static final double constDoubleLarge = 23.0; |
| 114 | + |
| 115 | + @Benchmark |
| 116 | + public double tanhConstDoubleTiny() { |
| 117 | + return Math.tanh(constDoubleTiny); |
| 118 | + } |
| 119 | + |
| 120 | + @Benchmark |
| 121 | + public double tanhConstDoubleSmall() { |
| 122 | + return Math.tanh(constDoubleSmall); |
| 123 | + } |
| 124 | + |
| 125 | + @Benchmark |
| 126 | + public double tanhConstDouble1() { |
| 127 | + return Math.tanh(constDouble1); |
| 128 | + } |
| 129 | + |
| 130 | + @Benchmark |
| 131 | + public double tanhConstDouble21() { |
| 132 | + return Math.tanh(constDouble21); |
| 133 | + } |
| 134 | + |
| 135 | + @Benchmark |
| 136 | + public double tanhConstDoubleLarge() { |
| 137 | + return Math.tanh(constDoubleLarge); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + public static void main(String[] args) throws RunnerException { |
| 142 | + Options opt = new OptionsBuilder() |
| 143 | + .include(TanhPerfRanges.class.getSimpleName()) |
| 144 | + .build(); |
| 145 | + |
| 146 | + new Runner(opt).run(); |
| 147 | + |
| 148 | + opt = new OptionsBuilder() |
| 149 | + .include(TanhPerfConstant.class.getSimpleName()) |
| 150 | + .build(); |
| 151 | + |
| 152 | + new Runner(opt).run(); |
| 153 | + } |
| 154 | +} |
0 commit comments