Repository navigation
Expand file tree
/
Copy pathOffset.java
More file actions
358 lines (307 loc) · 9.37 KB
/
Copy pathOffset.java
File metadata and controls
358 lines (307 loc) · 9.37 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* 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.unsafe;
import com.sun.max.annotate.*;
import com.sun.max.lang.*;
import com.sun.max.program.*;
/**
* Offsets from addresses or pointers. Unlike an 'Address', an 'Offset' can be negative. Both types have the identical
* number of bits used for representation. However, 'Offset' uses twos complement, whereas 'Address' is simply unsigned.
*
* @author Bernd Mathiske
*/
public abstract class Offset extends Word {
protected Offset() {
}
@INLINE
public static Offset zero() {
return fromInt(0);
}
@INLINE
public static Offset fromUnsignedInt(int value) {
return Address.fromUnsignedInt(value).asOffset();
}
@INLINE
public static Offset fromInt(int value) {
if (Word.width() == 64) {
final long n = value;
return UnsafeCast.asOffset(n);
}
return UnsafeCast.asOffset(value);
}
@INLINE
public static Offset fromLong(long value) {
if (Word.width() == 64) {
return UnsafeCast.asOffset(value);
}
final int n = (int) value;
return UnsafeCast.asOffset(n);
}
@Override
public String toString() {
return "&" + toHexString();
}
@INLINE
public final int toInt() {
if (Word.width() == 64) {
final long n = UnsafeCast.asLong(this);
return (int) n;
}
return UnsafeCast.asInt(this);
}
@INLINE
public final long toLong() {
if (Word.width() == 64) {
return UnsafeCast.asLong(this);
}
return UnsafeCast.asInt(this);
}
public final int compareTo(Offset other) {
if (greaterThan(other)) {
return 1;
}
if (lessThan(other)) {
return -1;
}
return 0;
}
@INLINE
public final boolean equals(int other) {
return fromInt(other) == this;
}
@INLINE
public final boolean lessEqual(Offset other) {
if (Word.width() == 64) {
return toLong() <= other.toLong();
}
return toInt() <= other.toInt();
}
@INLINE
public final boolean lessEqual(int other) {
return lessEqual(fromInt(other));
}
@INLINE
public final boolean lessThan(Offset other) {
if (Word.width() == 64) {
return toLong() < other.toLong();
}
return toInt() < other.toInt();
}
@INLINE
public final boolean lessThan(int other) {
return lessThan(fromInt(other));
}
@INLINE
public final boolean greaterEqual(Offset other) {
if (Word.width() == 64) {
return toLong() >= other.toLong();
}
return toInt() >= other.toInt();
}
@INLINE
public final boolean greaterEqual(int other) {
return greaterEqual(fromInt(other));
}
@INLINE
public final boolean greaterThan(Offset other) {
if (Word.width() == 64) {
return toLong() > other.toLong();
}
return toInt() > other.toInt();
}
@INLINE
public final boolean greaterThan(int other) {
return greaterThan(fromInt(other));
}
@INLINE
public final Offset negate() {
if (Word.width() == 64) {
return fromLong(-toLong());
}
return fromInt(-toInt());
}
@INLINE
public final boolean isNegative() {
if (Word.width() == 64) {
return toLong() < 0L;
}
return toInt() < 0;
}
@INLINE
public final Offset plus(Offset addend) {
if (Word.width() == 64) {
return fromLong(toLong() + addend.toLong());
}
return fromInt(toInt() + addend.toInt());
}
@INLINE
public final Offset plus(Size addend) {
return plus(addend.asOffset());
}
@INLINE
public final Offset plus(int addend) {
return plus(fromInt(addend));
}
@INLINE
public final Offset plus(long addend) {
return plus(fromLong(addend));
}
@INLINE
public final Offset minus(Offset subtrahend) {
if (Word.width() == 64) {
return fromLong(toLong() - subtrahend.toLong());
}
return fromInt(toInt() - subtrahend.toInt());
}
@INLINE
public final Offset minus(Size subtrahend) {
return minus(subtrahend.asOffset());
}
@INLINE
public final Offset minus(int subtrahend) {
return minus(fromInt(subtrahend));
}
@INLINE
public final Offset minus(long subtrahend) {
return minus(fromLong(subtrahend));
}
@INLINE
public final Offset times(Offset factor) {
if (Word.width() == 64) {
return fromLong(toLong() * factor.toLong());
}
return fromInt(toInt() * factor.toInt());
}
@INLINE
public final Offset times(Address factor) {
return times(factor.asOffset());
}
@INLINE
public final Offset times(int factor) {
return times(fromInt(factor));
}
@INLINE
public final Offset dividedBy(Offset divisor) throws ArithmeticException {
if (Word.width() == 64) {
return fromLong(toLong() / divisor.toLong());
}
return fromInt(toInt() / divisor.toInt());
}
@INLINE
public final Offset dividedBy(int divisor) throws ArithmeticException {
return dividedBy(fromInt(divisor));
}
@INLINE
public final Offset remainder(Offset divisor) throws ArithmeticException {
if (Word.width() == 64) {
return fromLong(toLong() % divisor.toLong());
}
return fromInt(toInt() % divisor.toInt());
}
@INLINE
public final int remainder(int divisor) throws ArithmeticException {
return remainder(fromInt(divisor)).toInt();
}
@INLINE
public final boolean isRoundedBy(int numberOfBytes) {
return remainder(numberOfBytes) == 0;
}
public final Offset roundedUpBy(int numberOfBytes) {
if (isRoundedBy(numberOfBytes)) {
return this;
}
return plus(numberOfBytes - remainder(numberOfBytes));
}
@INLINE(override = true)
public Offset and(Offset operand) {
if (Word.width() == 64) {
return fromLong(toLong() & operand.toLong());
}
return fromInt(toInt() & operand.toInt());
}
@INLINE(override = true)
public Offset and(int operand) {
return and(fromInt(operand));
}
@INLINE(override = true)
public Offset and(long operand) {
return and(fromLong(operand));
}
@INLINE(override = true)
public Offset or(Offset operand) {
if (Word.width() == 64) {
return fromLong(toLong() | operand.toLong());
}
return fromInt(toInt() | operand.toInt());
}
@INLINE(override = true)
public Offset or(int operand) {
return or(fromInt(operand));
}
@INLINE(override = true)
public Offset or(long operand) {
return or(fromLong(operand));
}
@INLINE(override = true)
public Offset not() {
if (Word.width() == 64) {
return fromLong(~toLong());
}
return fromInt(~toInt());
}
@INLINE
public final Offset roundedDownBy(int numberOfBytes) {
return minus(remainder(numberOfBytes));
}
@INLINE(override = true)
public final Offset aligned() {
final int n = Word.size();
return plus(n - 1).and(Offset.fromInt(n - 1).not());
}
@INLINE(override = true)
public final boolean isAligned() {
final int n = Word.size();
return and(n - 1).equals(Offset.zero());
}
public final int numberOfEffectiveBits() {
if (Word.width() == 64) {
final long n = toLong();
if (n >= 0) {
return 64 - Long.numberOfLeadingZeros(n);
}
return 65 - Long.numberOfLeadingZeros(~n);
}
final int n = toInt();
if (n >= 0) {
return 32 - Integer.numberOfLeadingZeros(n);
}
return 33 - Integer.numberOfLeadingZeros(~n);
}
public final WordWidth effectiveWidth() {
final int bit = numberOfEffectiveBits();
for (WordWidth width : WordWidth.values()) {
if (bit < width.numberOfBits) {
return width;
}
}
throw ProgramError.unexpected();
}
}