Path: blob/master/test/hotspot/jtreg/compiler/codegen/BitTests.java
41149 views
/*1* Copyright (c) 2015, Red Hat, Inc. 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*/2223/*24* @test25* @bug 814402826* @summary Use AArch64 bit-test instructions in C227*28* @run main/othervm -Xbatch -XX:-TieredCompilation29* -XX:CompileCommand=dontinline,compiler.codegen.BitTests::*30* compiler.codegen.BitTests31* @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=132* compiler.codegen.BitTests33* @run main/othervm -Xbatch -XX:+TieredCompilation34* compiler.codegen.BitTests35*/3637package compiler.codegen;3839// Try to ensure that the bit test instructions TBZ/TBNZ, TST/TSTW40// don't generate incorrect code. We can't guarantee that C2 will use41// bit test instructions for this test and it's not a bug if it42// doesn't. However, these test cases are ideal candidates for each43// of the instruction forms.44public class BitTests {4546private final XorShift r = new XorShift();4748private final long increment(long ctr) {49return ctr + 1;50}5152private final int increment(int ctr) {53return ctr + 1;54}5556private final long testIntSignedBranch(long counter) {57if ((int) r.nextLong() < 0) {58counter = increment(counter);59}60return counter;61}6263private final long testLongSignedBranch(long counter) {64if (r.nextLong() < 0) {65counter = increment(counter);66}67return counter;68}6970private final long testIntBitBranch(long counter) {71if (((int) r.nextLong() & (1 << 27)) != 0) {72counter = increment(counter);73}74if (((int) r.nextLong() & (1 << 27)) != 0) {75counter = increment(counter);76}77return counter;78}7980private final long testLongBitBranch(long counter) {81if ((r.nextLong() & (1l << 50)) != 0) {82counter = increment(counter);83}84if ((r.nextLong() & (1l << 50)) != 0) {85counter = increment(counter);86}87return counter;88}8990private final long testLongMaskBranch(long counter) {91if (((r.nextLong() & 0x0800000000l) != 0)) {92counter++;93}94return counter;95}9697private final long testIntMaskBranch(long counter) {98if ((((int) r.nextLong() & 0x08) != 0)) {99counter++;100}101return counter;102}103104private final long testLongMaskBranch(long counter, long mask) {105if (((r.nextLong() & mask) != 0)) {106counter++;107}108return counter;109}110111private final long testIntMaskBranch(long counter, int mask) {112if ((((int) r.nextLong() & mask) != 0)) {113counter++;114}115return counter;116}117118private final long step(long counter) {119counter = testIntSignedBranch(counter);120counter = testLongSignedBranch(counter);121counter = testIntBitBranch(counter);122counter = testLongBitBranch(counter);123counter = testIntMaskBranch(counter);124counter = testLongMaskBranch(counter);125counter = testIntMaskBranch(counter, 0x8000);126counter = testLongMaskBranch(counter, 0x800000000l);127return counter;128}129130131private final long finalBits = 3;132133private long bits = 7;134135public static void main(String[] args) {136BitTests t = new BitTests();137138long counter = 0;139for (int i = 0; i < 10000000; i++) {140counter = t.step((int) counter);141}142if (counter != 50001495) {143System.err.println("FAILED: counter = " + counter + ", should be 50001495.");144System.exit(97);145}146System.out.println("PASSED");147}148149// Marsaglia's xor-shift generator, used here because it is150// reproducible across all Java implementations. It is also very151// fast.152static class XorShift {153154private long y;155156XorShift() {157y = 2463534242l;158}159160public long nextLong() {161y ^= (y << 13);162y ^= (y >>> 17);163return (y ^= (y << 5));164165}166}167}168169170