1 /*
   2  * Copyright 2009 Sun Microsystems, 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 6823354
  27  * @summary These methods can be instrinsified by using bit scan, bit test, and population count instructions.
  28  *
  29  * @run main/othervm -Xcomp -XX:CompileOnly=Test6823354.lzcomp,Test6823354.tzcomp Test6823354
  30  */
  31 
  32 public class Test6823354 {
  33     // Arrays of corner case values.
  34     static final int[]  ia = new int[]  { 0,  -1,  Integer.MIN_VALUE, Integer.MAX_VALUE };
  35     static final long[] la = new long[] { 0L, -1L, Long.MIN_VALUE,    Long.MAX_VALUE    };
  36 
  37     public static void main(String[] args) {
  38         // Load the class and the methods.
  39         Integer.numberOfLeadingZeros(0);
  40         Integer.numberOfTrailingZeros(0);
  41         Long.numberOfLeadingZeros(0);
  42         Long.numberOfTrailingZeros(0);
  43 
  44         lz();
  45         tz();
  46     }
  47 
  48     static void lz() {
  49         // int
  50         // Test corner cases.
  51         for (int i = 0; i < ia.length; i++) {
  52             int x = ia[i];
  53             check(x, lzcomp(x), lzint(x));
  54         }
  55         for (int i = 0; i < Integer.SIZE; i++) {
  56             int x = 1 << i;
  57             check(x, lzcomp(x), lzint(x));
  58         }
  59 
  60         // long
  61         // Test corner cases.
  62         for (int i = 0; i < ia.length; i++) {
  63             long x = la[i];
  64             check(x, lzcomp(x), lzint(x));
  65         }
  66         for (int i = 0; i < Long.SIZE; i++) {
  67             long x = 1L << i;
  68             check(x, lzcomp(x), lzint(x));
  69         }
  70     }
  71 
  72     static void tz() {
  73         // int
  74         // Test corner cases.
  75         for (int i = 0; i < ia.length; i++) {
  76             int x = ia[i];
  77             check(x, tzcomp(x), tzint(x));
  78         }
  79         for (int i = 0; i < Integer.SIZE; i++) {
  80             int x = 1 << i;
  81             check(x, tzcomp(x), tzint(x));
  82         }
  83 
  84         // long
  85         // Test corner cases.
  86         for (int i = 0; i < la.length; i++) {
  87             long x = la[i];
  88             check(x, tzcomp(x), tzint(x));
  89         }
  90         for (int i = 0; i < Long.SIZE; i++) {
  91             long x = 1L << i;
  92             check(x, tzcomp(x), tzint(x));
  93         }
  94     }
  95 
  96     static void check(int value, int result, int expected) {
  97         //System.out.println(value + ": " + result + ", " + expected);
  98         if (result != expected)
  99             throw new InternalError(value + " failed: " + result + " != " + expected);
 100     }
 101 
 102     static void check(long value, long result, long expected) {
 103         //System.out.println(value + ": " + result + ", " + expected);
 104         if (result != expected)
 105             throw new InternalError(value + " failed: " + result + " != " + expected);
 106     }
 107 
 108     static int lzint (int i)  { return Integer.numberOfLeadingZeros(i); }
 109     static int lzcomp(int i)  { return Integer.numberOfLeadingZeros(i); }
 110 
 111     static int lzint (long l) { return Long.numberOfLeadingZeros(l); }
 112     static int lzcomp(long l) { return Long.numberOfLeadingZeros(l); }
 113 
 114     static int tzint (int i)  { return Integer.numberOfTrailingZeros(i); }
 115     static int tzcomp(int i)  { return Integer.numberOfTrailingZeros(i); }
 116 
 117     static int tzint (long l) { return Long.numberOfTrailingZeros(l); }
 118     static int tzcomp(long l) { return Long.numberOfTrailingZeros(l); }
 119 }
 120