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