twisti@1210: /* trims@1907: * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. twisti@1210: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. twisti@1210: * twisti@1210: * This code is free software; you can redistribute it and/or modify it twisti@1210: * under the terms of the GNU General Public License version 2 only, as twisti@1210: * published by the Free Software Foundation. twisti@1210: * twisti@1210: * This code is distributed in the hope that it will be useful, but WITHOUT twisti@1210: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or twisti@1210: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License twisti@1210: * version 2 for more details (a copy is included in the LICENSE file that twisti@1210: * accompanied this code). twisti@1210: * twisti@1210: * You should have received a copy of the GNU General Public License version twisti@1210: * 2 along with this work; if not, write to the Free Software Foundation, twisti@1210: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. twisti@1210: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. twisti@1210: */ twisti@1210: twisti@1210: /** twisti@1210: * @test twisti@1210: * @bug 6823354 twisti@1210: * @summary These methods can be instrinsified by using bit scan, bit test, and population count instructions. twisti@1210: * twisti@1210: * @run main/othervm -Xcomp -XX:CompileOnly=Test6823354.lzcomp,Test6823354.tzcomp,.dolzcomp,.dotzcomp Test6823354 twisti@1210: */ twisti@1210: twisti@1210: import java.net.URLClassLoader; twisti@1210: twisti@1210: public class Test6823354 { twisti@1210: // Arrays of corner case values. twisti@1210: static final int[] ia = new int[] { 0, 1, -1, Integer.MIN_VALUE, Integer.MAX_VALUE }; twisti@1210: static final long[] la = new long[] { 0L, 1L, -1L, Long.MIN_VALUE, Long.MAX_VALUE }; twisti@1210: twisti@1210: public static void main(String[] args) throws Exception { twisti@1210: // Load the classes and the methods. twisti@1210: Integer.numberOfLeadingZeros(0); twisti@1210: Integer.numberOfTrailingZeros(0); twisti@1210: Long.numberOfLeadingZeros(0); twisti@1210: Long.numberOfTrailingZeros(0); twisti@1210: twisti@1210: lz(); twisti@1210: tz(); twisti@1210: } twisti@1210: twisti@1210: static void lz() throws Exception { twisti@1210: // int twisti@1210: twisti@1210: // Test corner cases. twisti@1210: for (int i = 0; i < ia.length; i++) { twisti@1210: int x = ia[i]; twisti@1210: check(x, lzcomp(x), lzint(x)); twisti@1210: } twisti@1210: twisti@1210: // Test all possible return values. twisti@1210: for (int i = 0; i < Integer.SIZE; i++) { twisti@1210: int x = 1 << i; twisti@1210: check(x, lzcomp(x), lzint(x)); twisti@1210: } twisti@1210: twisti@1210: String classname = "Test6823354$lzconI"; twisti@1210: twisti@1210: // Test Ideal optimizations (constant values). twisti@1210: for (int i = 0; i < ia.length; i++) { twisti@1210: testclass(classname, ia[i]); twisti@1210: } twisti@1210: twisti@1210: // Test Ideal optimizations (constant values). twisti@1210: for (int i = 0; i < Integer.SIZE; i++) { twisti@1210: int x = 1 << i; twisti@1210: testclass(classname, x); twisti@1210: } twisti@1210: twisti@1210: twisti@1210: // long twisti@1210: twisti@1210: // Test corner cases. twisti@1210: for (int i = 0; i < ia.length; i++) { twisti@1210: long x = la[i]; twisti@1210: check(x, lzcomp(x), lzint(x)); twisti@1210: } twisti@1210: twisti@1210: // Test all possible return values. twisti@1210: for (int i = 0; i < Long.SIZE; i++) { twisti@1210: long x = 1L << i; twisti@1210: check(x, lzcomp(x), lzint(x)); twisti@1210: } twisti@1210: twisti@1210: classname = "Test6823354$lzconL"; twisti@1210: twisti@1210: // Test Ideal optimizations (constant values). twisti@1210: for (int i = 0; i < la.length; i++) { twisti@1210: testclass(classname, la[i]); twisti@1210: } twisti@1210: twisti@1210: // Test Ideal optimizations (constant values). twisti@1210: for (int i = 0; i < Long.SIZE; i++) { twisti@1210: long x = 1L << i; twisti@1210: testclass(classname, x); twisti@1210: } twisti@1210: } twisti@1210: twisti@1210: static void tz() throws Exception { twisti@1210: // int twisti@1210: twisti@1210: // Test corner cases. twisti@1210: for (int i = 0; i < ia.length; i++) { twisti@1210: int x = ia[i]; twisti@1210: check(x, tzcomp(x), tzint(x)); twisti@1210: } twisti@1210: twisti@1210: // Test all possible return values. twisti@1210: for (int i = 0; i < Integer.SIZE; i++) { twisti@1210: int x = 1 << i; twisti@1210: check(x, tzcomp(x), tzint(x)); twisti@1210: } twisti@1210: twisti@1210: String classname = "Test6823354$tzconI"; twisti@1210: twisti@1210: // Test Ideal optimizations (constant values). twisti@1210: for (int i = 0; i < ia.length; i++) { twisti@1210: testclass(classname, ia[i]); twisti@1210: } twisti@1210: twisti@1210: // Test Ideal optimizations (constant values). twisti@1210: for (int i = 0; i < Integer.SIZE; i++) { twisti@1210: int x = 1 << i; twisti@1210: testclass(classname, x); twisti@1210: } twisti@1210: twisti@1210: twisti@1210: // long twisti@1210: twisti@1210: // Test corner cases. twisti@1210: for (int i = 0; i < la.length; i++) { twisti@1210: long x = la[i]; twisti@1210: check(x, tzcomp(x), tzint(x)); twisti@1210: } twisti@1210: twisti@1210: // Test all possible return values. twisti@1210: for (int i = 0; i < Long.SIZE; i++) { twisti@1210: long x = 1L << i; twisti@1210: check(x, tzcomp(x), tzint(x)); twisti@1210: } twisti@1210: twisti@1210: classname = "Test6823354$tzconL"; twisti@1210: twisti@1210: // Test Ideal optimizations (constant values). twisti@1210: for (int i = 0; i < la.length; i++) { twisti@1210: testclass(classname, la[i]); twisti@1210: } twisti@1210: twisti@1210: // Test Ideal optimizations (constant values). twisti@1210: for (int i = 0; i < Long.SIZE; i++) { twisti@1210: long x = 1L << i; twisti@1210: testclass(classname, x); twisti@1210: } twisti@1210: } twisti@1210: twisti@1210: static void check(int value, int result, int expected) { twisti@1210: //System.out.println(value + ": " + result + ", " + expected); twisti@1210: if (result != expected) twisti@1210: throw new InternalError(value + " failed: " + result + " != " + expected); twisti@1210: } twisti@1210: twisti@1210: static void check(long value, long result, long expected) { twisti@1210: //System.out.println(value + ": " + result + ", " + expected); twisti@1210: if (result != expected) twisti@1210: throw new InternalError(value + " failed: " + result + " != " + expected); twisti@1210: } twisti@1210: twisti@1210: static int lzint( int i) { return Integer.numberOfLeadingZeros(i); } twisti@1210: static int lzcomp(int i) { return Integer.numberOfLeadingZeros(i); } twisti@1210: twisti@1210: static int lzint( long l) { return Long.numberOfLeadingZeros(l); } twisti@1210: static int lzcomp(long l) { return Long.numberOfLeadingZeros(l); } twisti@1210: twisti@1210: static int tzint( int i) { return Integer.numberOfTrailingZeros(i); } twisti@1210: static int tzcomp(int i) { return Integer.numberOfTrailingZeros(i); } twisti@1210: twisti@1210: static int tzint( long l) { return Long.numberOfTrailingZeros(l); } twisti@1210: static int tzcomp(long l) { return Long.numberOfTrailingZeros(l); } twisti@1210: twisti@1210: static void testclass(String classname, int x) throws Exception { twisti@1210: System.setProperty("value", "" + x); twisti@1210: loadandrunclass(classname); twisti@1210: } twisti@1210: twisti@1210: static void testclass(String classname, long x) throws Exception { twisti@1210: System.setProperty("value", "" + x); twisti@1210: loadandrunclass(classname); twisti@1210: } twisti@1210: twisti@1210: static void loadandrunclass(String classname) throws Exception { twisti@1210: Class cl = Class.forName(classname); twisti@1210: URLClassLoader apploader = (URLClassLoader) cl.getClassLoader(); twisti@1210: ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent()); twisti@1210: Class c = loader.loadClass(classname); twisti@1210: Runnable r = (Runnable) c.newInstance(); twisti@1210: r.run(); twisti@1210: } twisti@1210: twisti@1210: public static class lzconI implements Runnable { twisti@1210: static final int VALUE; twisti@1210: twisti@1210: static { twisti@1210: int value = 0; twisti@1210: try { twisti@1210: value = Integer.decode(System.getProperty("value")); twisti@1210: } catch (Throwable e) {} twisti@1210: VALUE = value; twisti@1210: } twisti@1210: twisti@1210: public void run() { check(VALUE, lzint(VALUE), dolzcomp()); } twisti@1210: static int dolzcomp() { return lzcomp(VALUE); } twisti@1210: } twisti@1210: twisti@1210: public static class lzconL implements Runnable { twisti@1210: static final long VALUE; twisti@1210: twisti@1210: static { twisti@1210: long value = 0; twisti@1210: try { twisti@1210: value = Long.decode(System.getProperty("value")); twisti@1210: } catch (Throwable e) {} twisti@1210: VALUE = value; twisti@1210: } twisti@1210: twisti@1210: public void run() { check(VALUE, lzint(VALUE), dolzcomp()); } twisti@1210: static int dolzcomp() { return lzcomp(VALUE); } twisti@1210: } twisti@1210: twisti@1210: public static class tzconI implements Runnable { twisti@1210: static final int VALUE; twisti@1210: twisti@1210: static { twisti@1210: int value = 0; twisti@1210: try { twisti@1210: value = Integer.decode(System.getProperty("value")); twisti@1210: } catch (Throwable e) {} twisti@1210: VALUE = value; twisti@1210: } twisti@1210: twisti@1210: public void run() { check(VALUE, tzint(VALUE), dotzcomp()); } twisti@1210: static int dotzcomp() { return tzcomp(VALUE); } twisti@1210: } twisti@1210: twisti@1210: public static class tzconL implements Runnable { twisti@1210: static final long VALUE; twisti@1210: twisti@1210: static { twisti@1210: long value = 0; twisti@1210: try { twisti@1210: value = Long.decode(System.getProperty("value")); twisti@1210: } catch (Throwable e) {} twisti@1210: VALUE = value; twisti@1210: } twisti@1210: twisti@1210: public void run() { check(VALUE, tzint(VALUE), dotzcomp()); } twisti@1210: static int dotzcomp() { return tzcomp(VALUE); } twisti@1210: } twisti@1210: }