Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
296 lines (264 loc) · 9.82 KB

File metadata and controls

296 lines (264 loc) · 9.82 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
* that is described in this document. In particular, and without limitation, these intellectual property
* rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
* more additional patents or pending patent applications in the U.S. and in other countries.
*
* U.S. Government Rights - Commercial software. Government users are subject to the Sun
* Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
* supplements.
*
* Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
* registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
* are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
* U.S. and other countries.
*
* UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
* Company, Ltd.
*/
package com.sun.max.lang;
import com.sun.max.annotate.*;
import com.sun.max.util.*;
/**
* Additional methods that one might want in java.lang.Integer
* and int array stuff.
*
* @author Bernd Mathiske
* @author Paul Caprioli
*/
public final class Ints {
// Utility classes should not be instantiated.
private Ints() {
}
public static final int SIZE = 4;
public static final int WIDTH = 32;
public static final Range VALUE_RANGE = new Range(Integer.MIN_VALUE, Integer.MAX_VALUE);
public static final int K = 1024;
public static final int M = K * K;
public static int compare(int greater, int lesser) {
if (greater > lesser) {
return 1;
}
if (greater == lesser) {
return 0;
}
return -1;
}
public static int numberOfEffectiveSignedBits(int signed) {
if (signed >= 0) {
return 33 - Integer.numberOfLeadingZeros(signed);
}
return 33 - Integer.numberOfLeadingZeros(~signed);
}
public static int numberOfEffectiveUnsignedBits(int unsigned) {
return 32 - Integer.numberOfLeadingZeros(unsigned);
}
/**
* Returns an integer with all the bits in its two's complement binary representation that are at index {@code
* highestBitIndex} or lower set to 1.
*
* @param highestBitIndex the index of the highest bit to be set in the returned value. Only the low 5 bits of {@code
* highestBitIndex} are used. That is, if {@code highestBitIndex > 31} or {@code highestBitIndex < 0} then
* the highest bit to be set is given by {@code highestBitSet & 0x1F}.
*/
public static int lowBitsSet(int highestBitIndex) {
final int n = highestBitIndex & 0x1f;
return (1 << n) | ((1 << n) - 1);
}
/**
* Returns an integer with all the bits in its two's complement binary representation that are at index {@code
* lowestBitIndex} or higher set to 1.
*
* @param lowestBitIndex the index of the lowest bit to be set in the returned value. Only the low 5 bits of {@code
* lowestBitIndex} are used. That is, if {@code lowestBitIndex > 31} or {@code lowestBitIndex < 0} then
* the lowest bit to be set is given by {@code lowestBitSet & 0x1F}.
*/
public static int highBitsSet(int lowestBitIndex) {
return ~((1 << lowestBitIndex) - 1);
}
/**
* Determines if a given number is zero or a power of two.
*/
public static boolean isPowerOfTwoOrZero(int n) {
return Integer.lowestOneBit(n) == n;
}
public static int log2(int n) {
if (n <= 0) {
throw new ArithmeticException();
}
return 31 - Integer.numberOfLeadingZeros(n);
}
public static int roundUp(int value, int by) {
final int rest = value % by;
if (rest == 0) {
return value;
}
if (value < 0) {
return value - rest;
}
return value + (by - rest);
}
/**
* Calculates an unsigned integer which is greater than or equal to {@code value} and
* is a multiple of {@code by}. Results are undefined if {@code by} is not
* a power of two.
* @param value the unsigned integer which is to be rounded upwards.
* @param by a positive power of two.
* @return the unsigned integer calculated by rounding upwards to a multiple of {@code by}.
*/
@INLINE
public static int roundUnsignedUpByPowerOfTwo(int value, int by) {
assert isPowerOfTwoOrZero(by);
final int mask = by - 1;
return (value + mask) & ~mask;
}
/**
* Returns the hexadecimal string representation of the given value with at least 8 digits, e.g. 0x0000CAFE.
*/
public static String toHexLiteral(int value) {
return "0x" + toPaddedHexString(value, '0');
}
/**
* Returns the given value as a hexadecimal number with at least 8 digits, e.g. 0000CAFE.
*/
public static String toPaddedHexString(int n, char pad) {
final String s = Integer.toHexString(n).toUpperCase();
return Strings.times(pad, 8 - s.length()) + s;
}
public static boolean contains(int[] array, int value) {
for (int element : array) {
if (element == value) {
return true;
}
}
return false;
}
public static int[] append(int[] array, int element) {
final int resultLength = array.length + 1;
final int[] result = new int[resultLength];
System.arraycopy(array, 0, result, 0, array.length);
result[array.length] = element;
return result;
}
public static int[] append(int[] head, int[] tail) {
final int[] result = new int[head.length + tail.length];
System.arraycopy(head, 0, result, 0, head.length);
System.arraycopy(tail, 0, result, head.length, tail.length);
return result;
}
public static int[] createRange(int first, int last) {
if (first > last) {
throw new IllegalArgumentException();
}
final int n = last + 1 - first;
final int[] result = new int[n];
for (int i = 0; i < n; i++) {
result[i] = first + i;
}
return result;
}
public static void copyAll(int[] fromArray, int[] toArray) {
for (int i = 0; i < fromArray.length; i++) {
toArray[i] = fromArray[i];
}
}
/**
* Returns a string representation of the contents of the specified array.
* Adjacent elements are separated by the specified separator. Elements are
* converted to strings as by <tt>String.valueOf(int)</tt>.
*
* @param array the array whose string representation to return
* @param separator the separator to use
* @return a string representation of <tt>array</tt>
* @throws NullPointerException if {@code array} or {@code separator} is null
*/
public static String toString(int[] array, String separator) {
if (array == null || separator == null) {
throw new NullPointerException();
}
if (array.length == 0) {
return "";
}
final StringBuilder buf = new StringBuilder();
buf.append(array[0]);
for (int i = 1; i < array.length; i++) {
buf.append(separator);
buf.append(array[i]);
}
return buf.toString();
}
private static final int [] sizeBase10Table = {
9,
99,
999,
9999,
99999,
999999,
9999999,
99999999,
999999999, Integer.MAX_VALUE
};
/**
* Computes the numbers of characters in the base-10 string representation of a given integer, including the '-'
* prefix for a negative integer. That is, this method computes the length of the String returned by
* {@link Integer#toString(int)} without requiring a String object to be created.
*
* @param i an integer
* @return the length of the string that would be returned by calling {@link Integer#toString(int)} with {@code i}
* as the argument
*/
public static int sizeOfBase10String(int x) {
if (x == Integer.MIN_VALUE) {
return "-2147483648".length();
}
final int posX = x < 0 ? -x : x;
for (int i = 0;; i++) {
if (posX <= sizeBase10Table[i]) {
if (x < 0) {
return i + 2;
}
return i + 1;
}
}
}
/**
* @see Longs#toUnitsString(long, boolean)
*/
public static String toUnitsString(long number, boolean onlyPowerOfTwo) {
return Longs.toUnitsString(number, onlyPowerOfTwo);
}
/**
* Computes the minimum value in an array of integers.
*
* @param ints the array of integers from which the minimum is computed. This array must have at least one element.
* @return the minimum value in {@code ints}
* @throws ArrayIndexOutOfBoundsException if {@code ints.length == 0}
*/
public static int min(int[] ints) {
int min = ints[0];
for (int n : ints) {
if (n < min) {
min = n;
}
}
return min;
}
/**
* Computes the maximum value in an array of integers.
*
* @param ints the array of integers from which the maximum is computed. This array must have at least one element.
* @return the maximum value in {@code ints}
* @throws ArrayIndexOutOfBoundsException if {@code ints.length == 0}
*/
public static int max(int[] ints) {
int max = ints[0];
for (int n : ints) {
if (n > max) {
max = n;
}
}
return max;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.