Path: blob/master/test/micro/org/openjdk/bench/vm/compiler/FpMinMaxIntrinsics.java
41161 views
/*1* Copyright (c) 2019, 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.vm.compiler;2324import org.openjdk.jmh.annotations.*;25import org.openjdk.jmh.infra.*;2627import java.util.concurrent.TimeUnit;28import java.util.Random;2930@BenchmarkMode(Mode.AverageTime)31@OutputTimeUnit(TimeUnit.NANOSECONDS)32@State(Scope.Thread)33public class FpMinMaxIntrinsics {34private static final int COUNT = 1000;3536private double[] doubles = new double[COUNT];37private float[] floats = new float[COUNT];3839private int c1, c2, s1, s2;4041private Random r = new Random();4243@Setup44public void init() {45c1 = s1 = step();46c2 = COUNT - (s2 = step());4748for (int i=0; i<COUNT; i++) {49floats[i] = r.nextFloat();50doubles[i] = r.nextDouble();51}52}5354private int step() {55return (r.nextInt() & 0xf) + 1;56}5758@Benchmark59public void dMax(Blackhole bh) {60for (int i=0; i<COUNT; i++)61bh.consume(dMaxBench());62}6364@Benchmark65public void dMin(Blackhole bh) {66for (int i=0; i<COUNT; i++)67bh.consume(dMinBench());68}6970@Benchmark71public void fMax(Blackhole bh) {72for (int i=0; i<COUNT; i++)73bh.consume(fMaxBench());74}7576@Benchmark77public void fMin(Blackhole bh) {78for (int i=0; i<COUNT; i++)79bh.consume(fMinBench());80}8182private double dMaxBench() {83inc();84return Math.max(doubles[c1], doubles[c2]);85}8687private double dMinBench() {88inc();89return Math.min(doubles[c1], doubles[c2]);90}9192private float fMaxBench() {93inc();94return Math.max(floats[c1], floats[c2]);95}9697private float fMinBench() {98inc();99return Math.min(floats[c1], floats[c2]);100}101102private void inc() {103c1 = c1 + s1 < COUNT ? c1 + s1 : (s1 = step());104c2 = c2 - s2 > 0 ? c2 - s2 : COUNT - (s2 = step());105}106107@Benchmark108public float fMinReduce() {109float result = Float.MAX_VALUE;110111for (int i=0; i<COUNT; i++)112result = Math.min(result, floats[i]);113114return result;115}116117@Benchmark118public double dMinReduce() {119double result = Double.MAX_VALUE;120121for (int i=0; i<COUNT; i++)122result = Math.min(result, doubles[i]);123124return result;125}126}127128129