1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2019, Arm Limited and affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 
  26 
  27 package org.graalvm.compiler.core.test;
  28 
  29 import org.junit.Test;
  30 
  31 public class IntegerDivRemConstantTest extends GraalCompilerTest {
  32     public static int intDivPositiveConstant(int val) {
  33         return val / 5;
  34     }
  35 
  36     @Test
  37     public void testIntDivPositiveConstant() {
  38         test("intDivPositiveConstant", -10);
  39         test("intDivPositiveConstant", 0);
  40         test("intDivPositiveConstant", 4256);
  41         test("intDivPositiveConstant", Integer.MAX_VALUE);
  42         test("intDivPositiveConstant", Integer.MIN_VALUE);
  43     }
  44 
  45     public static int intDivIntegerMax(int val) {
  46         return val / Integer.MAX_VALUE;
  47     }
  48 
  49     @Test
  50     public void testIntDivIntegerMax() {
  51         test("intDivIntegerMax", -10);
  52         test("intDivIntegerMax", 0);
  53         test("intDivIntegerMax", 4256);
  54         test("intDivIntegerMax", Integer.MAX_VALUE);
  55         test("intDivIntegerMax", Integer.MIN_VALUE);
  56     }
  57 
  58     public static int intDivNegativeConstant(int val) {
  59         return val / -1234;
  60     }
  61 
  62     @Test
  63     public void testIntDivNegativeConstant() {
  64         test("intDivNegativeConstant", -123);
  65         test("intDivNegativeConstant", 0);
  66         test("intDivNegativeConstant", 123);
  67         test("intDivNegativeConstant", Integer.MAX_VALUE);
  68         test("intDivNegativeConstant", Integer.MIN_VALUE);
  69     }
  70 
  71     public static int intDivIntegerMinOdd(int val) {
  72         return val / (Integer.MIN_VALUE + 1);
  73     }
  74 
  75     @Test
  76     public void testIntDivIntegerMinOdd() {
  77         test("intDivIntegerMinOdd", -123);
  78         test("intDivIntegerMinOdd", 0);
  79         test("intDivIntegerMinOdd", 123);
  80         test("intDivIntegerMinOdd", Integer.MAX_VALUE);
  81         test("intDivIntegerMinOdd", Integer.MIN_VALUE);
  82     }
  83 
  84     public static long longDivPositiveConstant(long val) {
  85         return val / 35170432;
  86     }
  87 
  88     @Test
  89     public void testLongDivPositiveConstant() {
  90         test("longDivPositiveConstant", -1234L);
  91         test("longDivPositiveConstant", 0L);
  92         test("longDivPositiveConstant", 214423L);
  93         test("longDivPositiveConstant", Long.MAX_VALUE);
  94         test("longDivPositiveConstant", Long.MIN_VALUE);
  95     }
  96 
  97     public static long longDivLongMax(long val) {
  98         return val / Long.MAX_VALUE;
  99     }
 100 
 101     @Test
 102     public void testLongDivLongMax() {
 103         test("longDivLongMax", -1234L);
 104         test("longDivLongMax", 0L);
 105         test("longDivLongMax", 214423L);
 106         test("longDivLongMax", Long.MAX_VALUE);
 107         test("longDivLongMax", Long.MIN_VALUE);
 108     }
 109 
 110     public static long longDivNegativeConstant(long val) {
 111         return val / -413;
 112     }
 113 
 114     @Test
 115     public void testLongDivNegativeConstant() {
 116         test("longDivNegativeConstant", -43L);
 117         test("longDivNegativeConstant", 0L);
 118         test("longDivNegativeConstant", 147065L);
 119         test("longDivNegativeConstant", Long.MAX_VALUE);
 120         test("longDivNegativeConstant", Long.MIN_VALUE);
 121     }
 122 
 123     public static long longDivLongMinOdd(long val) {
 124         return val / (Long.MIN_VALUE + 1);
 125     }
 126 
 127     @Test
 128     public void testLongDivLongMinOdd() {
 129         test("longDivLongMinOdd", -1234L);
 130         test("longDivLongMinOdd", 0L);
 131         test("longDivLongMinOdd", 214423L);
 132         test("longDivLongMinOdd", Long.MAX_VALUE);
 133         test("longDivLongMinOdd", Long.MIN_VALUE);
 134     }
 135 
 136     public static int intRemPositiveConstant(int val) {
 137         return val % 139968;
 138     }
 139 
 140     @Test
 141     public void testIntRemPositiveConstant() {
 142         test("intRemPositiveConstant", -10);
 143         test("intRemPositiveConstant", 0);
 144         test("intRemPositiveConstant", 4256);
 145         test("intRemPositiveConstant", Integer.MAX_VALUE);
 146         test("intRemPositiveConstant", Integer.MIN_VALUE);
 147     }
 148 
 149     public static int intRemNegativeConstant(int val) {
 150         return val % -139968;
 151     }
 152 
 153     @Test
 154     public void testIntRemNegativeConstant() {
 155         test("intRemNegativeConstant", -10);
 156         test("intRemNegativeConstant", 0);
 157         test("intRemNegativeConstant", 4256);
 158         test("intRemNegativeConstant", Integer.MAX_VALUE);
 159         test("intRemNegativeConstant", Integer.MIN_VALUE);
 160     }
 161 
 162     @SuppressWarnings("divzero")
 163     public static int intRemZero(int val) {
 164         return val % 0;
 165     }
 166 
 167     @Test
 168     public void testIntRemZero() {
 169         test("intRemZero", -10);
 170         test("intRemZero", 0);
 171         test("intRemZero", 4256);
 172         test("intRemZero", Integer.MAX_VALUE);
 173         test("intRemZero", Integer.MIN_VALUE);
 174     }
 175 
 176     public static int intRemMax(int val) {
 177         return val % Integer.MAX_VALUE;
 178     }
 179 
 180     @Test
 181     public void testIntRemMax() {
 182         test("intRemMax", -10);
 183         test("intRemMax", 0);
 184         test("intRemMax", 4256);
 185         test("intRemMax", Integer.MAX_VALUE);
 186         test("intRemMax", Integer.MIN_VALUE);
 187     }
 188 
 189     public static int intRemMin(int val) {
 190         return val % Integer.MIN_VALUE;
 191     }
 192 
 193     @Test
 194     public void testIntRemMin() {
 195         test("intRemMin", -10);
 196         test("intRemMin", 0);
 197         test("intRemMin", 4256);
 198         test("intRemMin", Integer.MAX_VALUE);
 199         test("intRemMin", Integer.MIN_VALUE);
 200     }
 201 
 202     public static long longRemPositiveConstant(long val) {
 203         return val % 35170432;
 204     }
 205 
 206     public static int intRemPowerOf2(int val) {
 207         return val % 4;
 208     }
 209 
 210     @Test
 211     public void testIntRemPowerOf2() {
 212         test("intRemPowerOf2", -10);
 213         test("intRemPowerOf2", 0);
 214         test("intRemPowerOf2", 4256);
 215         test("intRemPowerOf2", Integer.MAX_VALUE);
 216         test("intRemPowerOf2", Integer.MIN_VALUE);
 217     }
 218 
 219     @Test
 220     public void testLongRemPositiveConstant() {
 221         test("longRemPositiveConstant", -1234L);
 222         test("longRemPositiveConstant", 0L);
 223         test("longRemPositiveConstant", 214423L);
 224         test("longRemPositiveConstant", Long.MAX_VALUE);
 225         test("longRemPositiveConstant", Long.MIN_VALUE);
 226     }
 227 
 228     public static long longRemNegativeConstant(long val) {
 229         return val % -413;
 230     }
 231 
 232     @Test
 233     public void testLongRemNegativeConstant() {
 234         test("longRemNegativeConstant", -43L);
 235         test("longRemNegativeConstant", 0L);
 236         test("longRemNegativeConstant", 147065L);
 237         test("longRemNegativeConstant", Long.MAX_VALUE);
 238         test("longRemNegativeConstant", Long.MIN_VALUE);
 239     }
 240 
 241     @SuppressWarnings("divzero")
 242     public static long longRemZero(long val) {
 243         return val % 0;
 244     }
 245 
 246     @Test
 247     public void testLongRemZero() {
 248         test("longRemZero", -43L);
 249         test("longRemZero", 0L);
 250         test("longRemZero", 147065L);
 251         test("longRemZero", Long.MAX_VALUE);
 252         test("longRemZero", Long.MIN_VALUE);
 253     }
 254 
 255     public static long longRemMax(long val) {
 256         return val % Long.MAX_VALUE;
 257     }
 258 
 259     @Test
 260     public void testLongRemMax() {
 261         test("longRemMax", -43L);
 262         test("longRemMax", 0L);
 263         test("longRemMax", 147065L);
 264         test("longRemMax", Long.MAX_VALUE);
 265         test("longRemMax", Long.MIN_VALUE);
 266     }
 267 
 268     public static long longRemMin(long val) {
 269         return val % Long.MIN_VALUE;
 270     }
 271 
 272     @Test
 273     public void testLongRemMin() {
 274         test("longRemMin", -43L);
 275         test("longRemMin", 0L);
 276         test("longRemMin", 147065L);
 277         test("longRemMin", Long.MAX_VALUE);
 278         test("longRemMin", Long.MIN_VALUE);
 279     }
 280 
 281     public static long longRemPowerOf2(long val) {
 282         return val % 4L;
 283     }
 284 
 285     @Test
 286     public void testLongRemPowerOf2() {
 287         test("longRemPowerOf2", -43L);
 288         test("longRemPowerOf2", 0L);
 289         test("longRemPowerOf2", 147065L);
 290         test("longRemPowerOf2", Long.MAX_VALUE);
 291         test("longRemPowerOf2", Long.MIN_VALUE);
 292     }
 293 }