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