001/*
002 * Copyright (c) 2011, 2013, 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.asm;
024
025// JaCoCo Exclude
026
027/**
028 * A collection of static utility functions that check ranges of numbers.
029 */
030public class NumUtil {
031
032    public static boolean isShiftCount(int x) {
033        return 0 <= x && x < 32;
034    }
035
036    /**
037     * Determines if a given {@code int} value is the range of unsigned byte values.
038     */
039    public static boolean isUByte(int x) {
040        return (x & 0xff) == x;
041    }
042
043    /**
044     * Determines if a given {@code int} value is the range of signed byte values.
045     */
046    public static boolean isByte(int x) {
047        return (byte) x == x;
048    }
049
050    /**
051     * Determines if a given {@code long} value is the range of unsigned byte values.
052     */
053    public static boolean isUByte(long x) {
054        return (x & 0xffL) == x;
055    }
056
057    /**
058     * Determines if a given {@code long} value is the range of signed byte values.
059     */
060    public static boolean isByte(long l) {
061        return (byte) l == l;
062    }
063
064    /**
065     * Determines if a given {@code long} value is the range of unsigned int values.
066     */
067    public static boolean isUInt(long x) {
068        return (x & 0xffffffffL) == x;
069    }
070
071    /**
072     * Determines if a given {@code long} value is the range of signed int values.
073     */
074    public static boolean isInt(long l) {
075        return (int) l == l;
076    }
077
078    /**
079     * Determines if a given {@code int} value is the range of signed short values.
080     */
081    public static boolean isShort(int x) {
082        return (short) x == x;
083    }
084
085    /**
086     * Determines if a given {@code long} value is the range of signed short values.
087     */
088    public static boolean isShort(long x) {
089        return (short) x == x;
090    }
091
092    public static boolean isUShort(int s) {
093        return s == (s & 0xFFFF);
094    }
095
096    public static boolean isUShort(long s) {
097        return s == (s & 0xFFFF);
098    }
099
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}