1 /*
   2  * Copyright (c) 2011, 2012, 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.graalvm.compiler.jtt.hotspot;
  24 
  25 //@formatter:off
  26 
  27 /**
  28  * @test
  29  * @bug 6823354
  30  * @summary These methods can be instrinsified by using bit scan, bit test, and population count instructions.
  31  *
  32  * @run main/othervm -Xcomp -XX:CompileOnly=Test6823354.lzcomp,Test6823354.tzcomp,.dolzcomp,.dotzcomp Test6823354
  33  */
  34 
  35 import java.net.URLClassLoader;
  36 
  37 // Checkstyle: stop
  38 public class Test6823354 {
  39     // Arrays of corner case values.
  40     static final int[]  ia = new int[]  { 0,  1,  -1,  Integer.MIN_VALUE, Integer.MAX_VALUE };
  41     static final long[] la = new long[] { 0L, 1L, -1L, Long.MIN_VALUE,    Long.MAX_VALUE    };
  42 
  43     public static void main(String[] args) throws Exception {
  44         // Load the classes and the methods.
  45         Integer.numberOfLeadingZeros(0);
  46         Integer.numberOfTrailingZeros(0);
  47         Long.numberOfLeadingZeros(0);
  48         Long.numberOfTrailingZeros(0);
  49 
  50         lz();
  51         tz();
  52     }
  53 
  54     static void lz() throws Exception {
  55         // int
  56 
  57         // Test corner cases.
  58         for (int i = 0; i < ia.length; i++) {
  59             int x = ia[i];
  60             check(x, lzcomp(x), lzint(x));
  61         }
  62 
  63         // Test all possible return values.
  64         for (int i = 0; i < Integer.SIZE; i++) {
  65             int x = 1 << i;
  66             check(x, lzcomp(x), lzint(x));
  67         }
  68 
  69         String classname = Test6823354.class.getName() + "$lzconI";
  70 
  71         // Test Ideal optimizations (constant values).
  72         for (int i = 0; i < ia.length; i++) {
  73             testclass(classname, ia[i]);
  74         }
  75 
  76         // Test Ideal optimizations (constant values).
  77         for (int i = 0; i < Integer.SIZE; i++) {
  78             int x = 1 << i;
  79             testclass(classname, x);
  80         }
  81 
  82 
  83         // long
  84 
  85         // Test corner cases.
  86         for (int i = 0; i < ia.length; i++) {
  87             long x = la[i];
  88             check(x, lzcomp(x), lzint(x));
  89         }
  90 
  91         // Test all possible return values.
  92         for (int i = 0; i < Long.SIZE; i++) {
  93             long x = 1L << i;
  94             check(x, lzcomp(x), lzint(x));
  95         }
  96 
  97         classname = Test6823354.class.getName() + "$lzconL";
  98 
  99         // Test Ideal optimizations (constant values).
 100         for (int i = 0; i < la.length; i++) {
 101             testclass(classname, la[i]);
 102         }
 103 
 104         // Test Ideal optimizations (constant values).
 105         for (int i = 0; i < Long.SIZE; i++) {
 106             long x = 1L << i;
 107             testclass(classname, x);
 108         }
 109     }
 110 
 111     static void tz() throws Exception {
 112         // int
 113 
 114         // Test corner cases.
 115         for (int i = 0; i < ia.length; i++) {
 116             int x = ia[i];
 117             check(x, tzcomp(x), tzint(x));
 118         }
 119 
 120         // Test all possible return values.
 121         for (int i = 0; i < Integer.SIZE; i++) {
 122             int x = 1 << i;
 123             check(x, tzcomp(x), tzint(x));
 124         }
 125 
 126         String classname = Test6823354.class.getName() + "$tzconI";
 127 
 128         // Test Ideal optimizations (constant values).
 129         for (int i = 0; i < ia.length; i++) {
 130             testclass(classname, ia[i]);
 131         }
 132 
 133         // Test Ideal optimizations (constant values).
 134         for (int i = 0; i < Integer.SIZE; i++) {
 135             int x = 1 << i;
 136             testclass(classname, x);
 137         }
 138 
 139 
 140         // long
 141 
 142         // Test corner cases.
 143         for (int i = 0; i < la.length; i++) {
 144             long x = la[i];
 145             check(x, tzcomp(x), tzint(x));
 146         }
 147 
 148         // Test all possible return values.
 149         for (int i = 0; i < Long.SIZE; i++) {
 150             long x = 1L << i;
 151             check(x, tzcomp(x), tzint(x));
 152         }
 153 
 154         classname = Test6823354.class.getName() + "$tzconL";
 155 
 156         // Test Ideal optimizations (constant values).
 157         for (int i = 0; i < la.length; i++) {
 158             testclass(classname, la[i]);
 159         }
 160 
 161         // Test Ideal optimizations (constant values).
 162         for (int i = 0; i < Long.SIZE; i++) {
 163             long x = 1L << i;
 164             testclass(classname, x);
 165         }
 166     }
 167 
 168     static void check(int value, int result, int expected) {
 169         if (result != expected)
 170             throw new InternalError(value + " failed: " + result + " != " + expected);
 171     }
 172 
 173     static void check(long value, long result, long expected) {
 174         if (result != expected)
 175             throw new InternalError(value + " failed: " + result + " != " + expected);
 176     }
 177 
 178     static int lzint( int i)  { return Integer.numberOfLeadingZeros(i); }
 179     static int lzcomp(int i)  { return Integer.numberOfLeadingZeros(i); }
 180 
 181     static int lzint( long l) { return Long.numberOfLeadingZeros(l); }
 182     static int lzcomp(long l) { return Long.numberOfLeadingZeros(l); }
 183 
 184     static int tzint( int i)  { return Integer.numberOfTrailingZeros(i); }
 185     static int tzcomp(int i)  { return Integer.numberOfTrailingZeros(i); }
 186 
 187     static int tzint( long l) { return Long.numberOfTrailingZeros(l); }
 188     static int tzcomp(long l) { return Long.numberOfTrailingZeros(l); }
 189 
 190     static void testclass(String classname, int x) throws Exception {
 191         System.setProperty("value", "" + x);
 192         loadandrunclass(classname);
 193     }
 194 
 195     static void testclass(String classname, long x) throws Exception {
 196         System.setProperty("value", "" + x);
 197         loadandrunclass(classname);
 198     }
 199 
 200     static void loadandrunclass(String classname) throws Exception {
 201         Class<?> cl = Class.forName(classname);
 202         URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
 203         ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
 204         Class<?> c = loader.loadClass(classname);
 205         Runnable r = (Runnable) c.newInstance();
 206         r.run();
 207     }
 208 
 209     public static class lzconI implements Runnable {
 210         static final int VALUE;
 211 
 212         static {
 213             int value = 0;
 214             try {
 215                 value = Integer.decode(System.getProperty("value"));
 216             } catch (Throwable e) {}
 217             VALUE = value;
 218         }
 219 
 220         @Override
 221         public void run() { check(VALUE, lzint(VALUE), dolzcomp()); }
 222         static int dolzcomp() { return lzcomp(VALUE); }
 223     }
 224 
 225     public static class lzconL implements Runnable {
 226         static final long VALUE;
 227 
 228         static {
 229             long value = 0;
 230             try {
 231                 value = Long.decode(System.getProperty("value"));
 232             } catch (Throwable e) {}
 233             VALUE = value;
 234         }
 235 
 236         @Override
 237         public void run() { check(VALUE, lzint(VALUE), dolzcomp()); }
 238         static int dolzcomp() { return lzcomp(VALUE); }
 239     }
 240 
 241     public static class tzconI implements Runnable {
 242         static final int VALUE;
 243 
 244         static {
 245             int value = 0;
 246             try {
 247                 value = Integer.decode(System.getProperty("value"));
 248             } catch (Throwable e) {}
 249             VALUE = value;
 250         }
 251 
 252         @Override
 253         public void run() { check(VALUE, tzint(VALUE), dotzcomp()); }
 254         static int dotzcomp() { return tzcomp(VALUE); }
 255     }
 256 
 257     public static class tzconL implements Runnable {
 258         static final long VALUE;
 259 
 260         static {
 261             long value = 0;
 262             try {
 263                 value = Long.decode(System.getProperty("value"));
 264             } catch (Throwable e) {}
 265             VALUE = value;
 266         }
 267 
 268         @Override
 269         public void run() { check(VALUE, tzint(VALUE), dotzcomp()); }
 270         static int dotzcomp() { return tzcomp(VALUE); }
 271     }
 272 }