1 /*
   2  * Copyright (c) 2011, 2013, 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 package org.graalvm.compiler.asm;
  24 
  25 // JaCoCo Exclude
  26 
  27 /**
  28  * A collection of static utility functions that check ranges of numbers.
  29  */
  30 public class NumUtil {
  31 
  32     public static boolean isShiftCount(int x) {
  33         return 0 <= x && x < 32;
  34     }
  35 
  36     /**
  37      * Determines if a given {@code int} value is the range of unsigned byte values.
  38      */
  39     public static boolean isUByte(int x) {
  40         return (x & 0xff) == x;
  41     }
  42 
  43     /**
  44      * Determines if a given {@code int} value is the range of signed byte values.
  45      */
  46     public static boolean isByte(int x) {
  47         return (byte) x == x;
  48     }
  49 
  50     /**
  51      * Determines if a given {@code long} value is the range of unsigned byte values.
  52      */
  53     public static boolean isUByte(long x) {
  54         return (x & 0xffL) == x;
  55     }
  56 
  57     /**
  58      * Determines if a given {@code long} value is the range of signed byte values.
  59      */
  60     public static boolean isByte(long l) {
  61         return (byte) l == l;
  62     }
  63 
  64     /**
  65      * Determines if a given {@code long} value is the range of unsigned int values.
  66      */
  67     public static boolean isUInt(long x) {
  68         return (x & 0xffffffffL) == x;
  69     }
  70 
  71     /**
  72      * Determines if a given {@code long} value is the range of signed int values.
  73      */
  74     public static boolean isInt(long l) {
  75         return (int) l == l;
  76     }
  77 
  78     /**
  79      * Determines if a given {@code int} value is the range of signed short values.
  80      */
  81     public static boolean isShort(int x) {
  82         return (short) x == x;
  83     }
  84 
  85     /**
  86      * Determines if a given {@code long} value is the range of signed short values.
  87      */
  88     public static boolean isShort(long x) {
  89         return (short) x == x;
  90     }
  91 
  92     public static boolean isUShort(int s) {
  93         return s == (s & 0xFFFF);
  94     }
  95 
  96     public static boolean isUShort(long s) {
  97         return s == (s & 0xFFFF);
  98     }
  99 
 100     public static boolean is32bit(long x) {
 101         return -0x80000000L <= x && x < 0x80000000L;
 102     }
 103 
 104     public static short safeToShort(int v) {
 105         assert isShort(v);
 106         return (short) v;
 107     }
 108 
 109     public static int roundUp(int number, int mod) {
 110         return ((number + mod - 1) / mod) * mod;
 111     }
 112 
 113     public static long roundUp(long number, long mod) {
 114         return ((number + mod - 1L) / mod) * mod;
 115     }
 116 
 117     public static int roundDown(int number, int mod) {
 118         return number / mod * mod;
 119     }
 120 
 121     public static long roundDown(long number, long mod) {
 122         return number / mod * mod;
 123     }
 124 
 125     public static int log2Ceil(int val) {
 126         int x = 1;
 127         int log2 = 0;
 128         while (x < val) {
 129             log2++;
 130             x *= 2;
 131         }
 132         return log2;
 133     }
 134 
 135     public static boolean isUnsignedNbit(int n, int value) {
 136         assert n > 0 && n < 32;
 137         return 32 - Integer.numberOfLeadingZeros(value) <= n;
 138     }
 139 
 140     public static boolean isUnsignedNbit(int n, long value) {
 141         assert n > 0 && n < 64;
 142         return 64 - Long.numberOfLeadingZeros(value) <= n;
 143     }
 144 
 145     public static boolean isSignedNbit(int n, int value) {
 146         assert n > 0 && n < 32;
 147         int min = -(1 << (n - 1));
 148         int max = (1 << (n - 1)) - 1;
 149         return value >= min && value <= max;
 150     }
 151 
 152     public static boolean isSignedNbit(int n, long value) {
 153         assert n > 0 && n < 64;
 154         long min = -(1L << (n - 1));
 155         long max = (1L << (n - 1)) - 1;
 156         return value >= min && value <= max;
 157     }
 158 
 159     /**
 160      *
 161      * @param n Number of bits that should be set to 1. Must be between 0 and 32 (inclusive).
 162      * @return A number with n bits set to 1.
 163      */
 164     public static int getNbitNumberInt(int n) {
 165         assert n >= 0 && n <= 32 : "0 <= n <= 32; instead: " + n;
 166         if (n < 32) {
 167             return (1 << n) - 1;
 168         } else {
 169             return 0xFFFFFFFF;
 170         }
 171     }
 172 
 173     /**
 174      *
 175      * @param n Number of bits that should be set to 1. Must be between 0 and 64 (inclusive).
 176      * @return A number with n bits set to 1.
 177      */
 178     public static long getNbitNumberLong(int n) {
 179         assert n >= 0 && n <= 64;
 180         if (n < 64) {
 181             return (1L << n) - 1;
 182         } else {
 183             return 0xFFFFFFFFFFFFFFFFL;
 184         }
 185     }
 186 }