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