Path: blob/master/test/hotspot/jtreg/compiler/codegen/Test6823354.java
41149 views
/*1* Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 682335426* @summary These methods can be instrinsified by using bit scan, bit test, and population count instructions.27* @modules java.base/jdk.internal.misc28* @library /test/lib29*30* @run main/othervm -Xcomp31* -XX:CompileCommand=compileonly,compiler.codegen.Test6823354::lzcomp32* -XX:CompileCommand=compileonly,compiler.codegen.Test6823354::tzcomp33* -XX:CompileCommand=compileonly,compiler.codegen.*::dolzcomp34* -XX:CompileCommand=compileonly,compiler.codegen.*::dotzcomp35* compiler.codegen.Test682335436*/3738package compiler.codegen;3940import jdk.test.lib.Utils;4142public class Test6823354 {43// Arrays of corner case values.44static final int[] ia = new int[] { 0, 1, -1, Integer.MIN_VALUE, Integer.MAX_VALUE };45static final long[] la = new long[] { 0L, 1L, -1L, Long.MIN_VALUE, Long.MAX_VALUE };4647public static void main(String[] args) throws Exception {48// Load the classes and the methods.49Integer.numberOfLeadingZeros(0);50Integer.numberOfTrailingZeros(0);51Long.numberOfLeadingZeros(0);52Long.numberOfTrailingZeros(0);5354lz();55tz();56}5758static void lz() throws Exception {59// int6061// Test corner cases.62for (int i = 0; i < ia.length; i++) {63int x = ia[i];64check(x, lzcomp(x), lzint(x));65}6667// Test all possible return values.68for (int i = 0; i < Integer.SIZE; i++) {69int x = 1 << i;70check(x, lzcomp(x), lzint(x));71}7273String classname = Test6823354.class.getName() + "$lzconI";7475// Test Ideal optimizations (constant values).76for (int i = 0; i < ia.length; i++) {77testclass(classname, ia[i]);78}7980// Test Ideal optimizations (constant values).81for (int i = 0; i < Integer.SIZE; i++) {82int x = 1 << i;83testclass(classname, x);84}858687// long8889// Test corner cases.90for (int i = 0; i < ia.length; i++) {91long x = la[i];92check(x, lzcomp(x), lzint(x));93}9495// Test all possible return values.96for (int i = 0; i < Long.SIZE; i++) {97long x = 1L << i;98check(x, lzcomp(x), lzint(x));99}100101classname = Test6823354.class.getName() + "$lzconL";102103// Test Ideal optimizations (constant values).104for (int i = 0; i < la.length; i++) {105testclass(classname, la[i]);106}107108// Test Ideal optimizations (constant values).109for (int i = 0; i < Long.SIZE; i++) {110long x = 1L << i;111testclass(classname, x);112}113}114115static void tz() throws Exception {116// int117118// Test corner cases.119for (int i = 0; i < ia.length; i++) {120int x = ia[i];121check(x, tzcomp(x), tzint(x));122}123124// Test all possible return values.125for (int i = 0; i < Integer.SIZE; i++) {126int x = 1 << i;127check(x, tzcomp(x), tzint(x));128}129130String classname = Test6823354.class.getName() + "$tzconI";131132// Test Ideal optimizations (constant values).133for (int i = 0; i < ia.length; i++) {134testclass(classname, ia[i]);135}136137// Test Ideal optimizations (constant values).138for (int i = 0; i < Integer.SIZE; i++) {139int x = 1 << i;140testclass(classname, x);141}142143144// long145146// Test corner cases.147for (int i = 0; i < la.length; i++) {148long x = la[i];149check(x, tzcomp(x), tzint(x));150}151152// Test all possible return values.153for (int i = 0; i < Long.SIZE; i++) {154long x = 1L << i;155check(x, tzcomp(x), tzint(x));156}157158classname = Test6823354.class.getName() + "$tzconL";159160// Test Ideal optimizations (constant values).161for (int i = 0; i < la.length; i++) {162testclass(classname, la[i]);163}164165// Test Ideal optimizations (constant values).166for (int i = 0; i < Long.SIZE; i++) {167long x = 1L << i;168testclass(classname, x);169}170}171172static void check(int value, int result, int expected) {173//System.out.println(value + ": " + result + ", " + expected);174if (result != expected)175throw new InternalError(value + " failed: " + result + " != " + expected);176}177178static void check(long value, long result, long expected) {179//System.out.println(value + ": " + result + ", " + expected);180if (result != expected)181throw new InternalError(value + " failed: " + result + " != " + expected);182}183184static int lzint( int i) { return Integer.numberOfLeadingZeros(i); }185static int lzcomp(int i) { return Integer.numberOfLeadingZeros(i); }186187static int lzint( long l) { return Long.numberOfLeadingZeros(l); }188static int lzcomp(long l) { return Long.numberOfLeadingZeros(l); }189190static int tzint( int i) { return Integer.numberOfTrailingZeros(i); }191static int tzcomp(int i) { return Integer.numberOfTrailingZeros(i); }192193static int tzint( long l) { return Long.numberOfTrailingZeros(l); }194static int tzcomp(long l) { return Long.numberOfTrailingZeros(l); }195196static void testclass(String classname, int x) throws Exception {197System.setProperty("value", "" + x);198loadandrunclass(classname);199}200201static void testclass(String classname, long x) throws Exception {202System.setProperty("value", "" + x);203loadandrunclass(classname);204}205206static void loadandrunclass(String classname) throws Exception {207Class cl = Class.forName(classname);208ClassLoader apploader = cl.getClassLoader();209ClassLoader loader210= Utils.getTestClassPathURLClassLoader(apploader.getParent());211Class c = loader.loadClass(classname);212Runnable r = (Runnable) c.newInstance();213r.run();214}215216public static class lzconI implements Runnable {217static final int VALUE;218219static {220int value = 0;221try {222value = Integer.decode(System.getProperty("value"));223} catch (Throwable e) {}224VALUE = value;225}226227public void run() { check(VALUE, lzint(VALUE), dolzcomp()); }228static int dolzcomp() { return lzcomp(VALUE); }229}230231public static class lzconL implements Runnable {232static final long VALUE;233234static {235long value = 0;236try {237value = Long.decode(System.getProperty("value"));238} catch (Throwable e) {}239VALUE = value;240}241242public void run() { check(VALUE, lzint(VALUE), dolzcomp()); }243static int dolzcomp() { return lzcomp(VALUE); }244}245246public static class tzconI implements Runnable {247static final int VALUE;248249static {250int value = 0;251try {252value = Integer.decode(System.getProperty("value"));253} catch (Throwable e) {}254VALUE = value;255}256257public void run() { check(VALUE, tzint(VALUE), dotzcomp()); }258static int dotzcomp() { return tzcomp(VALUE); }259}260261public static class tzconL implements Runnable {262static final long VALUE;263264static {265long value = 0;266try {267value = Long.decode(System.getProperty("value"));268} catch (Throwable e) {}269VALUE = value;270}271272public void run() { check(VALUE, tzint(VALUE), dotzcomp()); }273static int dotzcomp() { return tzcomp(VALUE); }274}275}276277278