Path: blob/master/test/micro/org/openjdk/bench/vm/compiler/DivRem.java
41161 views
/*1* Copyright (c) 2014, 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.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Scope;29import org.openjdk.jmh.annotations.Setup;30import org.openjdk.jmh.annotations.State;3132import java.util.Random;33import java.util.concurrent.TimeUnit;3435/**36* Tests speed of division and remainder calculations.37*/38@BenchmarkMode(Mode.AverageTime)39@OutputTimeUnit(TimeUnit.NANOSECONDS)40@State(Scope.Thread)41public class DivRem {4243private static final int ARRAYSIZE = 500;4445/* instance fields for the constant int division tests. */46public int[] intValues, intValues2;4748/* instance fields for the constant long division tests. */49public long[] longValues, longValues2;5051/* instance fields for the tests using the testdr-method. */52public long[] drLongValues1, drLongValues2;5354public long[] drLongValuesAsInts1, drLongValuesAsInts2;5556@Setup57public void setupSubclass() {58Random r = new Random(4711);5960intValues = new int[ARRAYSIZE];61intValues2 = new int[ARRAYSIZE];62longValues = new long[ARRAYSIZE];63longValues2 = new long[ARRAYSIZE];6465for (int i = 0; i < ARRAYSIZE; i++) {66intValues[i] = r.nextInt();67if (intValues[i] == 0) {68intValues[i] = 5353;69}70intValues2[i] = r.nextInt();7172longValues[i] = r.nextLong();73if (longValues[i] == 0) {74longValues[i] = 5353L;75}76longValues2[i] = r.nextLong();77}7879/* generate random longs for 32-64 tests */8081drLongValues1 = new long[ARRAYSIZE];82drLongValues2 = new long[ARRAYSIZE];83drLongValuesAsInts1 = new long[ARRAYSIZE];84drLongValuesAsInts2 = new long[ARRAYSIZE];85for (int i = 0; i < ARRAYSIZE; i++) {86long l = r.nextLong();87if (l == 0L) {88l++;89}90drLongValues1[i] = l;91drLongValuesAsInts1[i] = (long) (int) l;92l = r.nextLong();93if (l == 0L) {94l++;95}96drLongValues2[i] = l;97drLongValuesAsInts2[i] = (long) (int) l;98}99}100101/**102* Tests integer division with a constant divisor. Hopefully the JVM will do a Granlund-Montgomery and convert it to103* a multiplication instead.104*/105@Benchmark106public int testIntDivConstantDivisor() {107int dummy = 0;108for (int i = 0; i < intValues.length; i++) {109dummy += intValues[i] / 49;110}111return dummy;112}113114/**115* Tests long division with a constant divisor. Hopefully the JVM will do a Granlund-Montgomery and convert it to a116* multiplication instead.117*/118@Benchmark119public long testLongDivConstantDivisor() {120long dummy = 0;121for (int i = 0; i < longValues.length; i++) {122dummy += longValues[i] / 49L + longValues[i] / 0x4949494949L;123}124return dummy;125}126127/**128* Tests integer remainder with a constant divisor. Hopefully the JVM will do a Granlund-Montgomery and convert it to129* two multiplications instead.130*/131@Benchmark132public int testIntRemConstantDivisor() {133int dummy = 0;134for (int i = 0; i < intValues.length; i++) {135dummy += intValues[i] % 49;136}137return dummy;138}139140/**141* Tests long division with a constant divisor. Hopefully the JVM will do a Granlund-Montgomery and convert it to a142* multiplication instead.143*/144@Benchmark145public long testLongRemConstantDivisor() {146long dummy = 0;147for (int i = 0; i < longValues.length; i++) {148dummy += longValues[i] % 49L + longValues[i] % 0x4949494949L;149}150return dummy;151}152153/**154* Tests integer division with a variable divisor. This benchmark is mainly here to be a comparison against the155* benchmark that performs both divisions and remainder calculations.156*/157@Benchmark158public int testIntDivVariableDivisor() {159int dummy = 0;160for (int i = 0; i < intValues.length; i++) {161dummy += intValues2[i] / intValues[i];162}163return dummy;164}165166/**167* Tests integer division and remainder with a variable divisor. Both calculations are performed with the same168* divisor, so a JVM should not have to perform two complex calculations. Either a division followed by a169* multiplication, or on X86 using idiv, where the reminder is also returned from the idiv instruction.170*/171@Benchmark172public int testIntDivRemVariableDivisor() {173int dummy = 0;174for (int i = 0; i < intValues.length; i++) {175dummy += intValues2[i] / intValues[i];176dummy += intValues2[i] % intValues[i];177}178return dummy;179}180181@Benchmark182public long test64DivRem64() {183long dummy = 0;184for (int i = 0; i < drLongValues1.length; i++) {185long l1 = drLongValues1[i];186long l2 = drLongValues2[i];187dummy += l1 / l2;188dummy += l1 % l2;189}190return dummy;191}192193@Benchmark194public long test32DivRem32() {195long dummy = 0;196for (int i = 0; i < drLongValuesAsInts1.length; i++) {197long l1 = drLongValuesAsInts1[i];198long l2 = drLongValuesAsInts2[i];199dummy += l1 / l2;200dummy += l1 % l2;201}202return dummy;203}204205@Benchmark206public long test64DivRem32() {207long dummy = 0;208for (int i = 0; i < drLongValues1.length; i++) {209long l1 = drLongValues1[i];210long l2 = drLongValuesAsInts2[i];211dummy += l1 / l2;212dummy += l1 % l2;213}214return dummy;215}216}217218219