|
| 1 | +/* |
| 2 | + * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product |
| 5 | + * that is described in this document. In particular, and without limitation, these intellectual property |
| 6 | + * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or |
| 7 | + * more additional patents or pending patent applications in the U.S. and in other countries. |
| 8 | + * |
| 9 | + * U.S. Government Rights - Commercial software. Government users are subject to the Sun |
| 10 | + * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its |
| 11 | + * supplements. |
| 12 | + * |
| 13 | + * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or |
| 14 | + * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks |
| 15 | + * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the |
| 16 | + * U.S. and other countries. |
| 17 | + * |
| 18 | + * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open |
| 19 | + * Company, Ltd. |
| 20 | + */ |
| 21 | +package com.sun.max.unsafe; |
| 22 | + |
| 23 | +import java.math.*; |
| 24 | + |
| 25 | +import com.sun.max.annotate.*; |
| 26 | + |
| 27 | +/** |
| 28 | + * Boxed version of Address. |
| 29 | + * |
| 30 | + * @see Address |
| 31 | + * |
| 32 | + * @author Bernd Mathiske |
| 33 | + */ |
| 34 | +@HOSTED_ONLY |
| 35 | +public final class BoxedAddress extends Address implements Boxed { |
| 36 | + |
| 37 | + private long nativeWord; |
| 38 | + |
| 39 | + public static final BoxedAddress ZERO = new BoxedAddress(0); |
| 40 | + public static final BoxedAddress MAX = new BoxedAddress(-1L); |
| 41 | + |
| 42 | + private static final class Cache { |
| 43 | + private Cache() { |
| 44 | + } |
| 45 | + |
| 46 | + static final int HIGHEST_VALUE = 1000; |
| 47 | + |
| 48 | + static final BoxedAddress[] cache = new BoxedAddress[HIGHEST_VALUE + 1]; |
| 49 | + |
| 50 | + static { |
| 51 | + for (int i = 0; i < cache.length; i++) { |
| 52 | + cache[i] = new BoxedAddress(i); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public static BoxedAddress from(long value) { |
| 58 | + if (value == 0) { |
| 59 | + return ZERO; |
| 60 | + } |
| 61 | + if (value >= 0 && value <= Cache.HIGHEST_VALUE) { |
| 62 | + return Cache.cache[(int) value]; |
| 63 | + } |
| 64 | + if (value == -1L) { |
| 65 | + return MAX; |
| 66 | + } |
| 67 | + return new BoxedAddress(value); |
| 68 | + } |
| 69 | + |
| 70 | + public static BoxedAddress from(int value) { |
| 71 | + return from(value & BoxedWord.INT_MASK); |
| 72 | + } |
| 73 | + |
| 74 | + private BoxedAddress(long value) { |
| 75 | + if (Word.width() == 64) { |
| 76 | + nativeWord = value; |
| 77 | + } else { |
| 78 | + nativeWord = value & BoxedWord.INT_MASK; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public long value() { |
| 83 | + return nativeWord; |
| 84 | + } |
| 85 | + |
| 86 | + private static BigInteger bi(long unsigned) { |
| 87 | + if (unsigned < 0) { |
| 88 | + long signBit = 0x8000000000000000L; |
| 89 | + long low63Bits = unsigned & ~signBit; |
| 90 | + return BigInteger.valueOf(low63Bits).setBit(63); |
| 91 | + } |
| 92 | + return BigInteger.valueOf(unsigned); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + private static long unsignedDivide(long dividend, long divisor) { |
| 97 | + if (dividend >= 0 && divisor >= 0) { |
| 98 | + return dividend / divisor; |
| 99 | + } |
| 100 | + return bi(dividend).divide(bi(divisor)).longValue(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public Address dividedByAddress(Address divisor) { |
| 105 | + final BoxedAddress box = (BoxedAddress) divisor.asAddress(); |
| 106 | + if (box.nativeWord == 0L) { |
| 107 | + throw new ArithmeticException(); |
| 108 | + } |
| 109 | + return new BoxedAddress(unsignedDivide(nativeWord, box.nativeWord)); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public Address dividedByInt(int divisor) { |
| 114 | + if (divisor == 0) { |
| 115 | + throw new ArithmeticException(); |
| 116 | + } |
| 117 | + return new BoxedAddress(unsignedDivide(nativeWord, divisor & BoxedWord.INT_MASK)); |
| 118 | + } |
| 119 | + |
| 120 | + private static long unsignedRemainder(long dividend, long divisor) { |
| 121 | + if (dividend >= 0 && divisor >= 0) { |
| 122 | + return dividend % divisor; |
| 123 | + } |
| 124 | + return bi(dividend).remainder(bi(divisor)).longValue(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Address remainderByAddress(Address divisor) { |
| 129 | + final BoxedAddress box = (BoxedAddress) divisor.asAddress(); |
| 130 | + if (box.nativeWord == 0L) { |
| 131 | + throw new ArithmeticException(); |
| 132 | + } |
| 133 | + return new BoxedAddress(unsignedRemainder(nativeWord, box.nativeWord)); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public int remainderByInt(int divisor) { |
| 138 | + if (divisor == 0) { |
| 139 | + throw new ArithmeticException(); |
| 140 | + } |
| 141 | + return (int) unsignedRemainder(nativeWord, divisor & BoxedWord.INT_MASK); |
| 142 | + } |
| 143 | +} |
0 commit comments