1 /*
   2  * Copyright (c) 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 /**
  26  * @test
  27  * @bug 8003585
  28  * @summary strength reduce or eliminate range checks for power-of-two sized arrays
  29  * @run main/othervm -XX:CompileCommand=compileonly,PowerOf2SizedArraysChecks::test* -XX:-BackgroundCompilation PowerOf2SizedArraysChecks
  30  *
  31  */
  32 
  33 import java.util.function.*;
  34 
  35 public class PowerOf2SizedArraysChecks {
  36 
  37     static void check_result(String name, int x, int m, boolean expected, boolean res) {
  38         if (expected != res) {
  39             throw new RuntimeException("Bad result in " + name + " for x =  " + x + " m = " + m + " expected " + expected  + " got " + res);
  40         }
  41     }
  42 
  43     static void helper(String name, BiFunction<Integer, int[], Boolean> test, int[] x_values, int[] m_values, boolean[][] expected) {
  44         for (int i = 0; i < x_values.length; i++) {
  45             int x = x_values[i];
  46             for (int j = 0; j < m_values.length; j++) {
  47                 int m = m_values[j];
  48                 int[] array = new int[m];
  49                 boolean res = test.apply(x, array);
  50                 check_result(name, x, m, expected[i][j], res);
  51             }
  52         }
  53     }
  54 
  55     // ((x & m) u<= m) is always true
  56     static boolean test1(int x, int[] array) {
  57         int m = array.length;
  58         if ((x & m) < 0 || (x & m) > m) {
  59             return false;
  60         }
  61         return true;
  62     }
  63 
  64     // ((x & (m - 1)) u< m) iff (m > 0)
  65     static boolean test2(int x, int[] array) {
  66         int m = array.length;
  67         if ((x & (m-1)) < 0 || (x & (m-1)) >= m) {
  68             return false;
  69         }
  70         return true;
  71     }
  72 
  73     static boolean test3(int x, int[] array) {
  74         try {
  75             int v = array[x & (array.length-1)];
  76         } catch(ArrayIndexOutOfBoundsException aioobe) {
  77             return false;
  78         }
  79         return true;
  80     }
  81 
  82     static public void main(String[] args) {
  83         int[] x_values = {-10, -5, 0, 5, 8, 16, 100};
  84         int[] m_values = { 16, 10, 0 };
  85         
  86         boolean[][] test1_expected = new boolean[x_values.length][m_values.length];
  87         for (int i = 0; i < x_values.length; i++) {
  88             for (int j = 0; j < m_values.length; j++) {
  89                 test1_expected[i][j] = true;
  90             }
  91         }
  92 
  93         boolean[][] test2_expected = new boolean[x_values.length][m_values.length];
  94         for (int i = 0; i < x_values.length; i++) {
  95             for (int j = 0; j < m_values.length; j++) {
  96                 test2_expected[i][j] = (m_values[j] > 0);
  97             }
  98         }
  99 
 100         for (int i = 0; i < 20000; i++) {
 101             helper("test1", PowerOf2SizedArraysChecks::test1, x_values, m_values, test1_expected);
 102             helper("test2", PowerOf2SizedArraysChecks::test2, x_values, m_values, test2_expected);
 103             helper("test3", PowerOf2SizedArraysChecks::test3, x_values, m_values, test2_expected);
 104         }
 105     }
 106 }