Skip to content

Commit

Permalink
cleanup n' smart-shift fix (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Apr 18, 2023
1 parent d4fab1a commit db6a3e7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Sources/ANKFullWidthKit/ANKFullWidth+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ extension ANKFullWidth where High == High.Magnitude {
return QR(qr.quotient, Self(digit: qr.remainder))
}
//=--------------------------------------=
let dividend_ = self.minLastIndexReportingIsZeroOrMinusOne()
let dividend_ = self.minLastIndexReportingIsZeroOrMinusOne()
let minLastIndexGapSize: Int = dividend_.minLastIndex &- divisor_.minLastIndex
let shift: Int = divisor[unchecked: divisor_.minLastIndex].leadingZeroBitCount
//=--------------------------------------=
Expand All @@ -185,7 +185,7 @@ extension ANKFullWidth where High == High.Magnitude {
QUOTIENT[quotientIndex] = UInt()
}
//=----------------------------------=
for quotientIndex in QUOTIENT.indices.prefix(through: minLastIndexGapSize).reversed() {
for quotientIndex in QUOTIENT.indices[...minLastIndexGapSize].reversed() {
//=------------------------------=
// Approximate Quotient Digit
//=------------------------------=
Expand Down
16 changes: 8 additions & 8 deletions Sources/ANKFullWidthKit/ANKFullWidth+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ extension ANKFullWidth {
/// - amount: `Int.min <= amount <= Int.max`
///
@inlinable mutating func _bitshiftLeftSmart(by amount: Int) {
let amountAbsoluteValue: Int = abs(amount)
switch (amount >= 0, amountAbsoluteValue < Self.bitWidth) {
case (true, true ): self._bitshiftLeft(by: amountAbsoluteValue)
let amountAbsoluteValue = amount.magnitude as UInt
switch (amount >= 0, amountAbsoluteValue < UInt(bitPattern: Self.bitWidth)) {
case (true, true ): self._bitshiftLeft(by: Int(bitPattern: amountAbsoluteValue))
case (true, false): self = Self(repeating: false)
case (false, true ): self._bitshiftRight(by: amountAbsoluteValue)
case (false, true ): self._bitshiftRight(by: Int(bitPattern: amountAbsoluteValue))
case (false, false): self = Self(repeating: self.isLessThanZero)
}
}
Expand Down Expand Up @@ -155,11 +155,11 @@ extension ANKFullWidth {
/// - amount: `Int.min <= amount <= Int.max`
///
@inlinable mutating func _bitshiftRightSmart(by amount: Int) {
let amountAbsoluteValue: Int = abs(amount)
switch (amount >= 0, amountAbsoluteValue < Self.bitWidth) {
case (true, true ): self._bitshiftRight(by: amountAbsoluteValue)
let amountAbsoluteValue = amount.magnitude as UInt
switch (amount >= 0, amountAbsoluteValue < UInt(bitPattern: Self.bitWidth)) {
case (true, true ): self._bitshiftRight(by: Int(bitPattern: amountAbsoluteValue))
case (true, false): self = Self(repeating: self.isLessThanZero)
case (false, true ): self._bitshiftLeft(by: amountAbsoluteValue)
case (false, true ): self._bitshiftLeft(by: Int(bitPattern: amountAbsoluteValue))
case (false, false): self = Self(repeating: false)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/ANKFullWidthKit/ANKFullWidth+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ extension String {
}
}
//=------------------------------=
assert(UTF8.distance(from: UTF8.startIndex, to: index) == count)
assert(UTF8[..<index].count == count)
return count
}
}
Expand Down
10 changes: 10 additions & 0 deletions Tests/ANKFullWidthKitTests/192+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ final class Int192TestsOnShifts: XCTestCase {
}
}

func testBitshiftingByMinAmountDoesNotTrap() {
XCTAssertEqual(T(repeating: true) << Int.min, T(repeating: true ))
XCTAssertEqual(T(repeating: true) >> Int.min, T(repeating: false))
}

func testBitshiftingByMaskingIsEquivalentToBitshiftingModuloBitWidth() {
for _ in 0 ..< 100 {
let x0 = UInt64.random(in: 0 ..< UInt64.max)
Expand Down Expand Up @@ -217,6 +222,11 @@ final class UInt192TestsOnShifts: XCTestCase {
}
}

func testBitshiftingByMinAmountDoesNotTrap() {
XCTAssertEqual(T(repeating: true) << Int.min, T(repeating: false))
XCTAssertEqual(T(repeating: true) >> Int.min, T(repeating: false))
}

func testBitshiftingByMaskingIsEquivalentToBitshiftingModuloBitWidth() {
for _ in 0 ..< 100 {
let x0 = UInt64.random(in: 0 ..< UInt64.max)
Expand Down
10 changes: 10 additions & 0 deletions Tests/ANKFullWidthKitTests/256+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ final class Int256TestsOnShifts: XCTestCase {
}
}

func testBitshiftingByMinAmountDoesNotTrap() {
XCTAssertEqual(T(repeating: true) << Int.min, T(repeating: true ))
XCTAssertEqual(T(repeating: true) >> Int.min, T(repeating: false))
}

func testBitshiftingByMaskingIsEquivalentToBitshiftingModuloBitWidth() {
for _ in 0 ..< 100 {
let x0 = UInt64.random(in: 0 ..< UInt64.max)
Expand Down Expand Up @@ -234,6 +239,11 @@ final class UInt256TestsOnShifts: XCTestCase {
}
}

func testBitshiftingByMinAmountDoesNotTrap() {
XCTAssertEqual(T(repeating: true) << Int.min, T(repeating: false))
XCTAssertEqual(T(repeating: true) >> Int.min, T(repeating: false))
}

func testBitshiftingByMaskingIsEquivalentToBitshiftingModuloBitWidth() {
for _ in 0 ..< 100 {
let x0 = UInt64.random(in: 0 ..< UInt64.max)
Expand Down

0 comments on commit db6a3e7

Please sign in to comment.