1 /*
   2  * Copyright (c) 2015, Red Hat, Inc. 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 
  24 /*
  25  * @test
  26  * @bug 8144028
  27  * @summary Use AArch64 bit-test instructions in C2
  28  * @modules java.base
  29  * @run main/othervm -Xbatch -XX:CompileCommand=dontinline,BitTests::* -XX:-TieredCompilation BitTests
  30  * @run main/othervm -Xbatch -XX:+TieredCompilation -XX:TieredStopAtLevel=1 BitTests
  31  * @run main/othervm -Xbatch -XX:+TieredCompilation BitTests
  32  *
  33  */
  34 
  35 // Try to ensure that the bit test instructions TBZ/TBNZ, TST/TSTW
  36 // don't generate incorrect code.  We can't guarantee that C2 will use
  37 // bit test instructions for this test and it's not a bug if it
  38 // doesn't.  However, these test cases are ideal candidates for each
  39 // of the instruction forms.
  40 public class BitTests {
  41 
  42     private final XorShift r = new XorShift();
  43 
  44     private final long increment(long ctr) {
  45         return ctr + 1;
  46     }
  47 
  48     private final int increment(int ctr) {
  49         return ctr + 1;
  50     }
  51 
  52     private final long testIntSignedBranch(long counter) {
  53         if ((int)r.nextLong() < 0) {
  54             counter = increment(counter);
  55         }
  56         return counter;
  57     }
  58 
  59     private final long testLongSignedBranch(long counter) {
  60         if (r.nextLong() < 0) {
  61             counter = increment(counter);
  62         }
  63         return counter;
  64     }
  65 
  66     private final long testIntBitBranch(long counter) {
  67         if (((int)r.nextLong() & (1 << 27)) != 0) {
  68             counter = increment(counter);
  69         }
  70         if (((int)r.nextLong() & (1 << 27)) != 0) {
  71             counter = increment(counter);
  72         }
  73         return counter;
  74     }
  75 
  76     private final long testLongBitBranch(long counter) {
  77         if ((r.nextLong() & (1l << 50)) != 0) {
  78             counter = increment(counter);
  79         }
  80         if ((r.nextLong() & (1l << 50)) != 0) {
  81             counter = increment(counter);
  82         }
  83         return counter;
  84     }
  85 
  86     private final long testLongMaskBranch(long counter) {
  87         if (((r.nextLong() & 0x0800000000l) != 0)) {
  88             counter++;
  89         }
  90        return counter;
  91     }
  92 
  93     private final long testIntMaskBranch(long counter) {
  94         if ((((int)r.nextLong() & 0x08) != 0)) {
  95             counter++;
  96         }
  97         return counter;
  98     }
  99 
 100     private final long testLongMaskBranch(long counter, long mask) {
 101         if (((r.nextLong() & mask) != 0)) {
 102             counter++;
 103         }
 104        return counter;
 105     }
 106 
 107     private final long testIntMaskBranch(long counter, int mask) {
 108         if ((((int)r.nextLong() & mask) != 0)) {
 109             counter++;
 110         }
 111         return counter;
 112     }
 113 
 114     private final long step(long counter) {
 115         counter = testIntSignedBranch(counter);
 116         counter = testLongSignedBranch(counter);
 117         counter = testIntBitBranch(counter);
 118         counter = testLongBitBranch(counter);
 119         counter = testIntMaskBranch(counter);
 120         counter = testLongMaskBranch(counter);
 121         counter = testIntMaskBranch(counter, 0x8000);
 122         counter = testLongMaskBranch(counter, 0x800000000l);
 123         return counter;
 124     }
 125 
 126 
 127     private final long finalBits = 3;
 128 
 129     private long bits = 7;
 130 
 131     public static void main(String[] args) {
 132         BitTests t = new BitTests();
 133 
 134         long counter = 0;
 135         for (int i = 0; i < 10000000; i++) {
 136             counter = t.step((int) counter);
 137         }
 138         if (counter != 50001495) {
 139             System.err.println("FAILED: counter = " + counter + ", should be 50001495.");
 140             System.exit(97);
 141         }
 142         System.out.println("PASSED");
 143     }
 144 
 145 }
 146 
 147 // Marsaglia's xor-shift generator, used here because it is
 148 // reproducible across all Java implementations.  It is also very
 149 // fast.
 150 class XorShift {
 151 
 152     private long y;
 153 
 154     XorShift() {
 155         y = 2463534242l;
 156     }
 157 
 158     public long nextLong() {
 159         y ^= (y << 13);
 160         y ^= (y >>> 17);
 161         return (y ^= (y << 5));
 162 
 163     }
 164 }