added subtractNoWrap(BigInteger)

This commit is contained in:
ghidragon
2026-05-05 13:53:43 -04:00
committed by dragonmacher
parent c9671f2a49
commit 62dc049cb0
4 changed files with 41 additions and 2 deletions
@@ -414,6 +414,26 @@ abstract class AbstractAddressSpace implements AddressSpace {
long resultOffset = NumericUtilities.bigIntegerToUnsignedLong(newOffset); long resultOffset = NumericUtilities.bigIntegerToUnsignedLong(newOffset);
return getUncheckedAddress(resultOffset); return getUncheckedAddress(resultOffset);
} }
@Override
public Address subtractNoWrap(GenericAddress addr, BigInteger displacement)
throws AddressOverflowException {
if (displacement.equals(BigInteger.ZERO)) {
return addr;
}
testAddressSpace(addr);
BigInteger addrOff = addr.getOffsetAsBigInteger();
BigInteger maxOff = maxAddress.getOffsetAsBigInteger();
BigInteger minOff = minAddress.getOffsetAsBigInteger();
BigInteger newOffset = addrOff.subtract(displacement);
if (newOffset.compareTo(minOff) < 0 || newOffset.compareTo(maxOff) > 0) {
throw new AddressOverflowException(
"Address Overflow in add: " + addr + " + " + displacement);
}
long resultOffset = NumericUtilities.bigIntegerToUnsignedLong(newOffset);
return getUncheckedAddress(resultOffset);
}
@Override @Override
public Address add(Address addr, long displacement) throws AddressOutOfBoundsException { public Address add(Address addr, long displacement) throws AddressOutOfBoundsException {
@@ -254,6 +254,7 @@ public interface Address extends Comparable<Address> {
public Address addNoWrap(long displacement) throws AddressOverflowException; public Address addNoWrap(long displacement) throws AddressOverflowException;
public Address addNoWrap(BigInteger displacement) throws AddressOverflowException; public Address addNoWrap(BigInteger displacement) throws AddressOverflowException;
public Address subtractNoWrap(BigInteger displacement) throws AddressOverflowException;
/** /**
* Creates a new address (possibly in a new space) by adding the displacement to this address. * Creates a new address (possibly in a new space) by adding the displacement to this address.
@@ -363,6 +363,17 @@ public interface AddressSpace extends Comparable<AddressSpace> {
public Address addNoWrap(GenericAddress addr, BigInteger displacement) public Address addNoWrap(GenericAddress addr, BigInteger displacement)
throws AddressOverflowException; throws AddressOverflowException;
/**
* Creates a new address by subtracting the displacement to the given address. The
* new address will NOT wrap!
* @param addr the original address.
* @param displacement the displacement to subtract.
* @return The new address created by subtracting the displacement to addr.offset.
* @throws AddressOverflowException if the addition would cause a wrap,
*/
public Address subtractNoWrap(GenericAddress addr, BigInteger displacement)
throws AddressOverflowException;
/** /**
* Creates a new address (possibly in a new space) by adding the given * Creates a new address (possibly in a new space) by adding the given
* displacement from the given address. * displacement from the given address.
@@ -176,6 +176,13 @@ public class GenericAddress implements Address {
} }
return addrSpace.addNoWrap(this, displacement); return addrSpace.addNoWrap(this, displacement);
} }
@Override
public Address subtractNoWrap(BigInteger displacement) throws AddressOverflowException {
if (displacement.equals(BigInteger.ZERO)) {
return this;
}
return addrSpace.subtractNoWrap(this, displacement);
}
@Override @Override
public Address add(long displacement) { public Address add(long displacement) {