001/*
002 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.jtt.hotspot;
024
025//@formatter:off
026
027/**
028 * @test
029 * @bug 6823354
030 * @summary These methods can be instrinsified by using bit scan, bit test, and population count instructions.
031 *
032 * @run main/othervm -Xcomp -XX:CompileOnly=Test6823354.lzcomp,Test6823354.tzcomp,.dolzcomp,.dotzcomp Test6823354
033 */
034
035import java.net.*;
036
037// Checkstyle: stop
038public class Test6823354 {
039    // Arrays of corner case values.
040    static final int[]  ia = new int[]  { 0,  1,  -1,  Integer.MIN_VALUE, Integer.MAX_VALUE };
041    static final long[] la = new long[] { 0L, 1L, -1L, Long.MIN_VALUE,    Long.MAX_VALUE    };
042
043    public static void main(String[] args) throws Exception {
044        // Load the classes and the methods.
045        Integer.numberOfLeadingZeros(0);
046        Integer.numberOfTrailingZeros(0);
047        Long.numberOfLeadingZeros(0);
048        Long.numberOfTrailingZeros(0);
049
050        lz();
051        tz();
052    }
053
054    static void lz() throws Exception {
055        // int
056
057        // Test corner cases.
058        for (int i = 0; i < ia.length; i++) {
059            int x = ia[i];
060            check(x, lzcomp(x), lzint(x));
061        }
062
063        // Test all possible return values.
064        for (int i = 0; i < Integer.SIZE; i++) {
065            int x = 1 << i;
066            check(x, lzcomp(x), lzint(x));
067        }
068
069        String classname = Test6823354.class.getName() + "$lzconI";
070
071        // Test Ideal optimizations (constant values).
072        for (int i = 0; i < ia.length; i++) {
073            testclass(classname, ia[i]);
074        }
075
076        // Test Ideal optimizations (constant values).
077        for (int i = 0; i < Integer.SIZE; i++) {
078            int x = 1 << i;
079            testclass(classname, x);
080        }
081
082
083        // long
084
085        // Test corner cases.
086        for (int i = 0; i < ia.length; i++) {
087            long x = la[i];
088            check(x, lzcomp(x), lzint(x));
089        }
090
091        // Test all possible return values.
092        for (int i = 0; i < Long.SIZE; i++) {
093            long x = 1L << i;
094            check(x, lzcomp(x), lzint(x));
095        }
096
097        classname = Test6823354.class.getName() + "$lzconL";
098
099        // Test Ideal optimizations (constant values).
100        for (int i = 0; i < la.length; i++) {
101            testclass(classname, la[i]);
102        }
103
104        // Test Ideal optimizations (constant values).
105        for (int i = 0; i < Long.SIZE; i++) {
106            long x = 1L << i;
107            testclass(classname, x);
108        }
109    }
110
111    static void tz() throws Exception {
112        // int
113
114        // Test corner cases.
115        for (int i = 0; i < ia.length; i++) {
116            int x = ia[i];
117            check(x, tzcomp(x), tzint(x));
118        }
119
120        // Test all possible return values.
121        for (int i = 0; i < Integer.SIZE; i++) {
122            int x = 1 << i;
123            check(x, tzcomp(x), tzint(x));
124        }
125
126        String classname = Test6823354.class.getName() + "$tzconI";
127
128        // Test Ideal optimizations (constant values).
129        for (int i = 0; i < ia.length; i++) {
130            testclass(classname, ia[i]);
131        }
132
133        // Test Ideal optimizations (constant values).
134        for (int i = 0; i < Integer.SIZE; i++) {
135            int x = 1 << i;
136            testclass(classname, x);
137        }
138
139
140        // long
141
142        // Test corner cases.
143        for (int i = 0; i < la.length; i++) {
144            long x = la[i];
145            check(x, tzcomp(x), tzint(x));
146        }
147
148        // Test all possible return values.
149        for (int i = 0; i < Long.SIZE; i++) {
150            long x = 1L << i;
151            check(x, tzcomp(x), tzint(x));
152        }
153
154        classname = Test6823354.class.getName() + "$tzconL";
155
156        // Test Ideal optimizations (constant values).
157        for (int i = 0; i < la.length; i++) {
158            testclass(classname, la[i]);
159        }
160
161        // Test Ideal optimizations (constant values).
162        for (int i = 0; i < Long.SIZE; i++) {
163            long x = 1L << i;
164            testclass(classname, x);
165        }
166    }
167
168    static void check(int value, int result, int 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        if (result != expected)
175            throw new InternalError(value + " failed: " + result + " != " + expected);
176    }
177
178    static int lzint( int i)  { return Integer.numberOfLeadingZeros(i); }
179    static int lzcomp(int i)  { return Integer.numberOfLeadingZeros(i); }
180
181    static int lzint( long l) { return Long.numberOfLeadingZeros(l); }
182    static int lzcomp(long l) { return Long.numberOfLeadingZeros(l); }
183
184    static int tzint( int i)  { return Integer.numberOfTrailingZeros(i); }
185    static int tzcomp(int i)  { return Integer.numberOfTrailingZeros(i); }
186
187    static int tzint( long l) { return Long.numberOfTrailingZeros(l); }
188    static int tzcomp(long l) { return Long.numberOfTrailingZeros(l); }
189
190    static void testclass(String classname, int x) throws Exception {
191        System.setProperty("value", "" + x);
192        loadandrunclass(classname);
193    }
194
195    static void testclass(String classname, long x) throws Exception {
196        System.setProperty("value", "" + x);
197        loadandrunclass(classname);
198    }
199
200    static void loadandrunclass(String classname) throws Exception {
201        Class<?> cl = Class.forName(classname);
202        URLClassLoader apploader = (URLClassLoader) cl.getClassLoader();
203        ClassLoader loader = new URLClassLoader(apploader.getURLs(), apploader.getParent());
204        Class<?> c = loader.loadClass(classname);
205        Runnable r = (Runnable) c.newInstance();
206        r.run();
207    }
208
209    public static class lzconI implements Runnable {
210        static final int VALUE;
211
212        static {
213            int value = 0;
214            try {
215                value = Integer.decode(System.getProperty("value"));
216            } catch (Throwable e) {}
217            VALUE = value;
218        }
219
220        public void run() { check(VALUE, lzint(VALUE), dolzcomp()); }
221        static int dolzcomp() { return lzcomp(VALUE); }
222    }
223
224    public static class lzconL implements Runnable {
225        static final long VALUE;
226
227        static {
228            long value = 0;
229            try {
230                value = Long.decode(System.getProperty("value"));
231            } catch (Throwable e) {}
232            VALUE = value;
233        }
234
235        public void run() { check(VALUE, lzint(VALUE), dolzcomp()); }
236        static int dolzcomp() { return lzcomp(VALUE); }
237    }
238
239    public static class tzconI implements Runnable {
240        static final int VALUE;
241
242        static {
243            int value = 0;
244            try {
245                value = Integer.decode(System.getProperty("value"));
246            } catch (Throwable e) {}
247            VALUE = value;
248        }
249
250        public void run() { check(VALUE, tzint(VALUE), dotzcomp()); }
251        static int dotzcomp() { return tzcomp(VALUE); }
252    }
253
254    public static class tzconL implements Runnable {
255        static final long VALUE;
256
257        static {
258            long value = 0;
259            try {
260                value = Long.decode(System.getProperty("value"));
261            } catch (Throwable e) {}
262            VALUE = value;
263        }
264
265        public void run() { check(VALUE, tzint(VALUE), dotzcomp()); }
266        static int dotzcomp() { return tzcomp(VALUE); }
267    }
268}