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