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     static void check_result(String name, int m, boolean expected, boolean res) {
  56         if (expected != res) {
  57             throw new RuntimeException("Bad result in " + name + " for m = " + m + " expected " + expected  + " got " + res);
  58         }
  59     }
  60 
  61     static void helper2(String name, Function<int[], Boolean> test, int[] m_values, boolean[] expected) {
  62         for (int j = 0; j < m_values.length; j++) {
  63             int m = m_values[j];
  64             int[] array = new int[m];
  65             boolean res = test.apply(array);
  66             check_result(name, m, expected[j], res);
  67         }
  68     }
  69 
  70     // ((x & m) u<= m) is always true
  71     static boolean test1(int x, int[] array) {
  72         int m = array.length;
  73         if ((x & m) < 0 || (x & m) > m) {
  74             return false;
  75         }
  76         return true;
  77     }
  78 
  79     // ((x & (m - 1)) u< m) iff (m > 0)
  80     static boolean test2(int x, int[] array) {
  81         int m = array.length;
  82         if ((x & (m-1)) < 0 || (x & (m-1)) >= m) {
  83             return false;
  84         }
  85         return true;
  86     }
  87 
  88     static boolean test3(int x, int[] array) {
  89         try {
  90             int v = array[x & (array.length-1)];
  91         } catch(ArrayIndexOutOfBoundsException aioobe) {
  92             return false;
  93         }
  94         return true;
  95     }
  96 
  97     // arraylength <= 0 to arraylength u<= 0
  98     static boolean test4(int[] array) {
  99         if (array.length <= 0) {
 100             return false;
 101         }
 102         return true;
 103     }
 104 
 105     // arraylength == 0 to arraylength u<= 0
 106     static boolean test5(int[] array) {
 107         if (array.length == 0) {
 108             return false;
 109         }
 110         return true;
 111     }
 112 
 113     // arraylength != 0 to arraylength u> 0
 114     static boolean test6(int[] array) {
 115         if (array.length != 0) {
 116             return false;
 117         }
 118         return true;
 119     }
 120 
 121     static public void main(String[] args) {
 122         int[] x_values = {-10, -5, 0, 5, 8, 16, 100};
 123         int[] m_values = { 16, 10, 0 };
 124 
 125         boolean[][] test1_expected = new boolean[x_values.length][m_values.length];
 126         for (int i = 0; i < x_values.length; i++) {
 127             for (int j = 0; j < m_values.length; j++) {
 128                 test1_expected[i][j] = true;
 129             }
 130         }
 131 
 132         boolean[][] test2_expected = new boolean[x_values.length][m_values.length];
 133         for (int i = 0; i < x_values.length; i++) {
 134             for (int j = 0; j < m_values.length; j++) {
 135                 test2_expected[i][j] = (m_values[j] > 0);
 136             }
 137         }
 138 
 139         boolean[] test4_expected = new boolean[m_values.length];
 140         for (int i = 0; i < m_values.length; i++) {
 141             test4_expected[i] = (m_values[i] > 0);
 142         }
 143         boolean[] test5_expected = new boolean[m_values.length];
 144         for (int i = 0; i < m_values.length; i++) {
 145             test5_expected[i] = (m_values[i] != 0);
 146         }
 147         boolean[] test6_expected = new boolean[m_values.length];
 148         for (int i = 0; i < m_values.length; i++) {
 149             test6_expected[i] = (m_values[i] == 0);
 150         }
 151 
 152         for (int i = 0; i < 20000; i++) {
 153             helper("test1", PowerOf2SizedArraysChecks::test1, x_values, m_values, test1_expected);
 154             helper("test2", PowerOf2SizedArraysChecks::test2, x_values, m_values, test2_expected);
 155             helper("test3", PowerOf2SizedArraysChecks::test3, x_values, m_values, test2_expected);
 156             helper2("test4", PowerOf2SizedArraysChecks::test4, m_values, test4_expected);
 157             helper2("test5", PowerOf2SizedArraysChecks::test5, m_values, test5_expected);
 158             helper2("test6", PowerOf2SizedArraysChecks::test6, m_values, test6_expected);
 159         }
 160     }
 161 }