Mercurial > hg > truffle
comparison graal/com.oracle.max.base/src/com/sun/max/lang/Ints.java @ 3733:e233f5660da4
Added Java files from Maxine project.
author | Thomas Wuerthinger <thomas.wuerthinger@oracle.com> |
---|---|
date | Sat, 17 Dec 2011 19:59:18 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3732:3e2e8b8abdaf | 3733:e233f5660da4 |
---|---|
1 /* | |
2 * Copyright (c) 2007, 2011, 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 com.sun.max.lang; | |
24 | |
25 import com.sun.max.util.*; | |
26 | |
27 /** | |
28 * Additional methods that one might want in java.lang.Integer | |
29 * and int array stuff. | |
30 */ | |
31 public final class Ints { | |
32 | |
33 // Utility classes should not be instantiated. | |
34 private Ints() { | |
35 } | |
36 | |
37 public static final int SIZE = 4; | |
38 public static final int WIDTH = 32; | |
39 | |
40 public static final Range VALUE_RANGE = new Range(Integer.MIN_VALUE, Integer.MAX_VALUE); | |
41 | |
42 public static final int K = 1024; | |
43 public static final int M = K * K; | |
44 | |
45 public static int compare(int greater, int lesser) { | |
46 if (greater > lesser) { | |
47 return 1; | |
48 } | |
49 if (greater == lesser) { | |
50 return 0; | |
51 } | |
52 return -1; | |
53 } | |
54 | |
55 public static int numberOfEffectiveSignedBits(int signed) { | |
56 if (signed >= 0) { | |
57 return 33 - Integer.numberOfLeadingZeros(signed); | |
58 } | |
59 return 33 - Integer.numberOfLeadingZeros(~signed); | |
60 } | |
61 | |
62 public static int numberOfEffectiveUnsignedBits(int unsigned) { | |
63 return 32 - Integer.numberOfLeadingZeros(unsigned); | |
64 } | |
65 | |
66 /** | |
67 * Returns an integer with all the bits in its two's complement binary representation that are at index {@code | |
68 * highestBitIndex} or lower set to 1. | |
69 * | |
70 * @param highestBitIndex the index of the highest bit to be set in the returned value. Only the low 5 bits of {@code | |
71 * highestBitIndex} are used. That is, if {@code highestBitIndex > 31} or {@code highestBitIndex < 0} then | |
72 * the highest bit to be set is given by {@code highestBitSet & 0x1F}. | |
73 */ | |
74 public static int lowBitsSet(int highestBitIndex) { | |
75 final int n = highestBitIndex & 0x1f; | |
76 return (1 << n) | ((1 << n) - 1); | |
77 } | |
78 | |
79 /** | |
80 * Returns an integer with all the bits in its two's complement binary representation that are at index {@code | |
81 * lowestBitIndex} or higher set to 1. | |
82 * | |
83 * @param lowestBitIndex the index of the lowest bit to be set in the returned value. Only the low 5 bits of {@code | |
84 * lowestBitIndex} are used. That is, if {@code lowestBitIndex > 31} or {@code lowestBitIndex < 0} then | |
85 * the lowest bit to be set is given by {@code lowestBitSet & 0x1F}. | |
86 */ | |
87 public static int highBitsSet(int lowestBitIndex) { | |
88 return ~((1 << lowestBitIndex) - 1); | |
89 } | |
90 | |
91 /** | |
92 * Determines if a given number is zero or a power of two. | |
93 */ | |
94 public static boolean isPowerOfTwoOrZero(int n) { | |
95 return Integer.lowestOneBit(n) == n; | |
96 } | |
97 | |
98 public static int log2(int n) { | |
99 if (n <= 0) { | |
100 throw new ArithmeticException(); | |
101 } | |
102 return 31 - Integer.numberOfLeadingZeros(n); | |
103 } | |
104 | |
105 public static int roundUp(int value, int by) { | |
106 final int rest = value % by; | |
107 if (rest == 0) { | |
108 return value; | |
109 } | |
110 if (value < 0) { | |
111 return value - rest; | |
112 } | |
113 return value + (by - rest); | |
114 } | |
115 | |
116 /** | |
117 * Calculates an unsigned integer which is greater than or equal to {@code value} and | |
118 * is a multiple of {@code by}. Results are undefined if {@code by} is not | |
119 * a power of two. | |
120 * @param value the unsigned integer which is to be rounded upwards. | |
121 * @param by a positive power of two. | |
122 * @return the unsigned integer calculated by rounding upwards to a multiple of {@code by}. | |
123 */ | |
124 public static int roundUnsignedUpByPowerOfTwo(int value, int by) { | |
125 assert isPowerOfTwoOrZero(by); | |
126 final int mask = by - 1; | |
127 return (value + mask) & ~mask; | |
128 } | |
129 | |
130 /** | |
131 * Returns the hexadecimal string representation of the given value with at least 8 digits, e.g. 0x0000CAFE. | |
132 */ | |
133 public static String toHexLiteral(int value) { | |
134 return "0x" + toPaddedHexString(value, '0'); | |
135 } | |
136 | |
137 /** | |
138 * Returns the given value as a hexadecimal number with at least 8 digits, e.g. 0000CAFE. | |
139 */ | |
140 public static String toPaddedHexString(int n, char pad) { | |
141 final String s = Integer.toHexString(n).toUpperCase(); | |
142 return Strings.times(pad, 8 - s.length()) + s; | |
143 } | |
144 | |
145 public static boolean contains(int[] array, int value) { | |
146 for (int element : array) { | |
147 if (element == value) { | |
148 return true; | |
149 } | |
150 } | |
151 return false; | |
152 } | |
153 | |
154 public static int[] append(int[] array, int element) { | |
155 final int resultLength = array.length + 1; | |
156 final int[] result = new int[resultLength]; | |
157 System.arraycopy(array, 0, result, 0, array.length); | |
158 result[array.length] = element; | |
159 return result; | |
160 } | |
161 | |
162 public static int[] append(int[] head, int[] tail) { | |
163 final int[] result = new int[head.length + tail.length]; | |
164 System.arraycopy(head, 0, result, 0, head.length); | |
165 System.arraycopy(tail, 0, result, head.length, tail.length); | |
166 return result; | |
167 } | |
168 | |
169 public static int[] createRange(int first, int last) { | |
170 if (first > last) { | |
171 throw new IllegalArgumentException(); | |
172 } | |
173 final int n = last + 1 - first; | |
174 final int[] result = new int[n]; | |
175 for (int i = 0; i < n; i++) { | |
176 result[i] = first + i; | |
177 } | |
178 return result; | |
179 } | |
180 | |
181 public static void copyAll(int[] fromArray, int[] toArray) { | |
182 for (int i = 0; i < fromArray.length; i++) { | |
183 toArray[i] = fromArray[i]; | |
184 } | |
185 } | |
186 | |
187 /** | |
188 * Returns a string representation of the contents of the specified array. | |
189 * Adjacent elements are separated by the specified separator. Elements are | |
190 * converted to strings as by <tt>String.valueOf(int)</tt>. | |
191 * | |
192 * @param array the array whose string representation to return | |
193 * @param separator the separator to use | |
194 * @return a string representation of <tt>array</tt> | |
195 * @throws NullPointerException if {@code array} or {@code separator} is null | |
196 */ | |
197 public static String toString(int[] array, String separator) { | |
198 if (array == null || separator == null) { | |
199 throw new NullPointerException(); | |
200 } | |
201 if (array.length == 0) { | |
202 return ""; | |
203 } | |
204 | |
205 final StringBuilder buf = new StringBuilder(); | |
206 buf.append(array[0]); | |
207 | |
208 for (int i = 1; i < array.length; i++) { | |
209 buf.append(separator); | |
210 buf.append(array[i]); | |
211 } | |
212 | |
213 return buf.toString(); | |
214 } | |
215 | |
216 private static final int [] sizeBase10Table = { | |
217 9, | |
218 99, | |
219 999, | |
220 9999, | |
221 99999, | |
222 999999, | |
223 9999999, | |
224 99999999, | |
225 999999999, Integer.MAX_VALUE | |
226 }; | |
227 | |
228 /** | |
229 * Computes the numbers of characters in the base-10 string representation of a given integer, including the '-' | |
230 * prefix for a negative integer. That is, this method computes the length of the String returned by | |
231 * {@link Integer#toString(int)} without requiring a String object to be created. | |
232 * | |
233 * @param i an integer | |
234 * @return the length of the string that would be returned by calling {@link Integer#toString(int)} with {@code i} | |
235 * as the argument | |
236 */ | |
237 public static int sizeOfBase10String(int x) { | |
238 if (x == Integer.MIN_VALUE) { | |
239 return "-2147483648".length(); | |
240 } | |
241 final int posX = x < 0 ? -x : x; | |
242 for (int i = 0;; i++) { | |
243 if (posX <= sizeBase10Table[i]) { | |
244 if (x < 0) { | |
245 return i + 2; | |
246 } | |
247 return i + 1; | |
248 } | |
249 } | |
250 } | |
251 | |
252 /** | |
253 * @see Longs#toUnitsString(long, boolean) | |
254 */ | |
255 public static String toUnitsString(long number, boolean onlyPowerOfTwo) { | |
256 return Longs.toUnitsString(number, onlyPowerOfTwo); | |
257 } | |
258 | |
259 /** | |
260 * Computes the minimum value in an array of integers. | |
261 * | |
262 * @param ints the array of integers from which the minimum is computed. This array must have at least one element. | |
263 * @return the minimum value in {@code ints} | |
264 * @throws ArrayIndexOutOfBoundsException if {@code ints.length == 0} | |
265 */ | |
266 public static int min(int[] ints) { | |
267 int min = ints[0]; | |
268 for (int n : ints) { | |
269 if (n < min) { | |
270 min = n; | |
271 } | |
272 } | |
273 return min; | |
274 } | |
275 | |
276 /** | |
277 * Computes the maximum value in an array of integers. | |
278 * | |
279 * @param ints the array of integers from which the maximum is computed. This array must have at least one element. | |
280 * @return the maximum value in {@code ints} | |
281 * @throws ArrayIndexOutOfBoundsException if {@code ints.length == 0} | |
282 */ | |
283 public static int max(int[] ints) { | |
284 int max = ints[0]; | |
285 for (int n : ints) { | |
286 if (n > max) { | |
287 max = n; | |
288 } | |
289 } | |
290 return max; | |
291 } | |
292 | |
293 } |