Skip to content

Commit

Permalink
modulo (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Jul 8, 2023
1 parent dc3b3ed commit 6e08ca4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Sources/ANKFullWidthKit/Private/Arithmetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ extension BinaryInteger {
}
//=--------------------------------------=
let minus = Self.isSigned && self < (0 as Self)
var residue = UInt.zero
var residue = (0 as UInt)

for word in self.magnitude.words.reversed() {
residue = modulus.dividingFullWidth(HL(residue, word)).remainder
}

return minus ? modulus &- residue : residue
return (minus && !residue.isZero) ? (modulus &- residue) : (residue)
}
}
20 changes: 16 additions & 4 deletions Tests/ANKFullWidthKitTests/192+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,23 @@ final class Int192TestsOnShifts: XCTestCase {
}

func testBitshiftingByMaskingIsEquivalentToBitshiftingModuloBitWidth() {
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), 192, T(x64: X(1, 2, 3)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), -192, T(x64: X(1, 2, 3)), signitude: S.self)

ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), 193, T(x64: X(2, 4, 6)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), -191, T(x64: X(2, 4, 6)), signitude: S.self)

ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), 256, T(x64: X(0, 1, 2)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), -128, T(x64: X(0, 1, 2)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), 192, T(x64: X(2, 4, 6)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), -192, T(x64: X(2, 4, 6)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), 193, T(x64: X(1, 2, 3)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), -191, T(x64: X(1, 2, 3)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(1, 2, 3)), 256, T(x64: X(2, 3, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(1, 2, 3)), -128, T(x64: X(2, 3, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), 256, T(x64: X(4, 6, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), -128, T(x64: X(4, 6, 0)), signitude: S.self)
}
}

Expand Down Expand Up @@ -222,17 +228,23 @@ final class UInt192TestsOnShifts: XCTestCase {
}

func testBitshiftingByMaskingIsEquivalentToBitshiftingModuloBitWidth() {
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), 192, T(x64: X(1, 2, 3)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), -192, T(x64: X(1, 2, 3)), signitude: S.self)

ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), 193, T(x64: X(2, 4, 6)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), -191, T(x64: X(2, 4, 6)), signitude: S.self)

ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), 256, T(x64: X(0, 1, 2)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3)), -128, T(x64: X(0, 1, 2)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), 192, T(x64: X(2, 4, 6)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), -192, T(x64: X(2, 4, 6)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), 193, T(x64: X(1, 2, 3)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), -191, T(x64: X(1, 2, 3)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(1, 2, 3)), 256, T(x64: X(2, 3, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(1, 2, 3)), -128, T(x64: X(2, 3, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), 256, T(x64: X(4, 6, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6)), -128, T(x64: X(4, 6, 0)), signitude: S.self)
}
}

Expand Down
20 changes: 16 additions & 4 deletions Tests/ANKFullWidthKitTests/256+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,23 @@ final class Int256TestsOnShifts: XCTestCase {
}

func testBitshiftingByMaskingIsEquivalentToBitshiftingModuloBitWidth() {
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), 256, T(x64: X(1, 2, 3, 4)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), -256, T(x64: X(1, 2, 3, 4)), signitude: S.self)

ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), 257, T(x64: X(2, 4, 6, 8)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), -255, T(x64: X(2, 4, 6, 8)), signitude: S.self)

ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), 320, T(x64: X(0, 1, 2, 3)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), -192, T(x64: X(0, 1, 2, 3)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), 256, T(x64: X(2, 4, 6, 8)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), -256, T(x64: X(2, 4, 6, 8)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), 257, T(x64: X(1, 2, 3, 4)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), -255, T(x64: X(1, 2, 3, 4)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(1, 2, 3, 4)), 320, T(x64: X(2, 3, 4, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(1, 2, 3, 4)), -192, T(x64: X(2, 3, 4, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), 320, T(x64: X(4, 6, 8, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), -192, T(x64: X(4, 6, 8, 0)), signitude: S.self)
}
}

Expand Down Expand Up @@ -236,17 +242,23 @@ final class UInt256TestsOnShifts: XCTestCase {
}

func testBitshiftingByMaskingIsEquivalentToBitshiftingModuloBitWidth() {
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), 256, T(x64: X(1, 2, 3, 4)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), -256, T(x64: X(1, 2, 3, 4)), signitude: S.self)

ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), 257, T(x64: X(2, 4, 6, 8)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), -255, T(x64: X(2, 4, 6, 8)), signitude: S.self)

ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), 320, T(x64: X(0, 1, 2, 3)), signitude: S.self)
ANKAssertShiftLeftByMasking (T(x64: X(1, 2, 3, 4)), -192, T(x64: X(0, 1, 2, 3)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), 256, T(x64: X(2, 4, 6, 8)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), -256, T(x64: X(2, 4, 6, 8)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), 257, T(x64: X(1, 2, 3, 4)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), -255, T(x64: X(1, 2, 3, 4)), signitude: S.self)

ANKAssertShiftRightByMasking(T(x64: X(1, 2, 3, 4)), 320, T(x64: X(2, 3, 4, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(1, 2, 3, 4)), -192, T(x64: X(2, 3, 4, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), 320, T(x64: X(4, 6, 8, 0)), signitude: S.self)
ANKAssertShiftRightByMasking(T(x64: X(2, 4, 6, 8)), -192, T(x64: X(4, 6, 8, 0)), signitude: S.self)
}
}

Expand Down
40 changes: 36 additions & 4 deletions Tests/ANKFullWidthKitTests/Private/Arithmetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,54 @@ final class ArithmeticTestsOnBinaryInteger: XCTestCase {

func testModuloPowerOf2() {
XCTAssertEqual( Int8.min.modulo(8), 0)
XCTAssertEqual( Int8(-1).modulo(8), 7)
XCTAssertEqual( Int8.max.modulo(8), 7)

XCTAssertEqual(UInt8.min.modulo(8), 0)
XCTAssertEqual(UInt8( 1).modulo(8), 1)
XCTAssertEqual(UInt8.max.modulo(8), 7)

XCTAssertEqual( Int8(-4).modulo(4), 0)
XCTAssertEqual( Int8(-3).modulo(4), 1)
XCTAssertEqual( Int8(-2).modulo(4), 2)
XCTAssertEqual( Int8(-1).modulo(4), 3)
XCTAssertEqual( Int8( 0).modulo(4), 0)
XCTAssertEqual( Int8( 1).modulo(4), 1)
XCTAssertEqual( Int8( 2).modulo(4), 2)
XCTAssertEqual( Int8( 3).modulo(4), 3)
XCTAssertEqual( Int8( 4).modulo(4), 0)

XCTAssertEqual(UInt8( 0).modulo(4), 0)
XCTAssertEqual(UInt8( 1).modulo(4), 1)
XCTAssertEqual(UInt8( 2).modulo(4), 2)
XCTAssertEqual(UInt8( 3).modulo(4), 3)
XCTAssertEqual(UInt8( 4).modulo(4), 0)
XCTAssertEqual(UInt8( 5).modulo(4), 1)
XCTAssertEqual(UInt8( 6).modulo(4), 2)
XCTAssertEqual(UInt8( 7).modulo(4), 3)
XCTAssertEqual(UInt8( 8).modulo(4), 0)
}

func testModuloNonPowerOf2() {
XCTAssertEqual( Int8.min.modulo(7), 5)
XCTAssertEqual( Int8(-1).modulo(7), 6)
XCTAssertEqual( Int8.max.modulo(7), 1)

XCTAssertEqual(UInt8.min.modulo(7), 0)
XCTAssertEqual(UInt8( 1).modulo(7), 1)
XCTAssertEqual(UInt8.max.modulo(7), 3)

XCTAssertEqual( Int8(-3).modulo(3), 0)
XCTAssertEqual( Int8(-2).modulo(3), 1)
XCTAssertEqual( Int8(-1).modulo(3), 2)
XCTAssertEqual( Int8( 0).modulo(3), 0)
XCTAssertEqual( Int8( 1).modulo(3), 1)
XCTAssertEqual( Int8( 2).modulo(3), 2)
XCTAssertEqual( Int8( 3).modulo(3), 0)

XCTAssertEqual(UInt8( 0).modulo(3), 0)
XCTAssertEqual(UInt8( 1).modulo(3), 1)
XCTAssertEqual(UInt8( 2).modulo(3), 2)
XCTAssertEqual(UInt8( 3).modulo(3), 0)
XCTAssertEqual(UInt8( 4).modulo(3), 1)
XCTAssertEqual(UInt8( 5).modulo(3), 2)
XCTAssertEqual(UInt8( 6).modulo(3), 0)
}
}

Expand Down

0 comments on commit 6e08ca4

Please sign in to comment.