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