1
0

diplomacy: simplify address range fragmentation

This commit is contained in:
Wesley W. Terpstra 2016-10-11 18:28:26 -07:00
parent 73e9508c09
commit b2a5d18e37

View File

@ -123,24 +123,15 @@ case class AddressSet(base: BigInt, mask: BigInt) extends Ordered[AddressSet]
object AddressSet object AddressSet
{ {
def misaligned(base: BigInt, size: BigInt): Seq[AddressSet] = { def misaligned(base: BigInt, size: BigInt, tail: Seq[AddressSet] = Seq()): Seq[AddressSet] = {
val largestPow2 = BigInt(1) << log2Floor(size) if (size == 0) tail.reverse else {
val mostZeros = (base + size - 1) & ~(largestPow2 - 1) val maxBaseAlignment = base & (-base) // 0 for infinite (LSB)
def splitLo(low: BigInt, high: BigInt, tail: Seq[AddressSet]): Seq[AddressSet] = { val maxSizeAlignment = BigInt(1) << log2Floor(size) // MSB of size
if (low == high) tail else { val step =
val toggleBits = low ^ high if (maxBaseAlignment == 0 || maxBaseAlignment > maxSizeAlignment)
val misalignment = toggleBits & (-toggleBits) maxSizeAlignment else maxBaseAlignment
splitLo(low+misalignment, high, AddressSet(low, misalignment-1) +: tail) misaligned(base+step, size-step, AddressSet(base, step-1) +: tail)
} }
} }
def splitHi(low: BigInt, high: BigInt, tail: Seq[AddressSet]): Seq[AddressSet] = {
if (low == high) tail else {
val toggleBits = low ^ high
val misalignment = toggleBits & (-toggleBits)
splitHi(low, high-misalignment, AddressSet(high-misalignment, misalignment-1) +: tail)
}
}
splitLo(base, mostZeros, splitHi(mostZeros, base+size, Seq())).sorted
}
} }