From 41459f3e98fa0df2c8fe5b17f7a4d8f4b73950e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oscar=20Bystr=C3=B6m=20Ericsson?= Date: Mon, 24 Apr 2023 08:11:09 +0200 Subject: [PATCH] cleanup --- Sources/ANKFoundation/ANKBinaryInteger.swift | 23 +++++++++++++++---- .../ANKBitPatternConvertible.swift | 8 ------- .../ANKFoundation/ANKFixedWidthInteger.swift | 21 ++++++++++++++--- .../ANKFoundation/ANKLargeBinaryInteger.swift | 21 ++++++++++++++--- .../ANKLargeFixedWidthInteger.swift | 21 ++++++++++++++--- Sources/ANKFoundation/Aliases.swift | 2 +- .../ANKFullWidth+Division+Digit.swift | 2 +- .../ANKFullWidth+Division.swift | 2 +- Tests/ANKFullWidthKitTests/192+Division.swift | 4 ++-- Tests/ANKFullWidthKitTests/256+Division.swift | 4 ++-- .../ANKSigned+Division.swift | 2 +- 11 files changed, 81 insertions(+), 29 deletions(-) diff --git a/Sources/ANKFoundation/ANKBinaryInteger.swift b/Sources/ANKFoundation/ANKBinaryInteger.swift index 1a966074..a4a71127 100644 --- a/Sources/ANKFoundation/ANKBinaryInteger.swift +++ b/Sources/ANKFoundation/ANKBinaryInteger.swift @@ -15,7 +15,12 @@ /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKBinaryInteger: ANKBitPatternConvertible, BinaryInteger, Sendable where Magnitude: ANKUnsignedInteger { @@ -308,7 +313,12 @@ extension ANKBinaryInteger where Self: FixedWidthInteger { /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKSignedInteger: ANKBinaryInteger, SignedInteger { } @@ -318,8 +328,13 @@ public protocol ANKSignedInteger: ANKBinaryInteger, SignedInteger { } /// An awesome, unsigned, binary integer. /// -/// ### Two's Complement +/// .### Two's Complement +/// +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKUnsignedInteger: ANKBinaryInteger, UnsignedInteger { } diff --git a/Sources/ANKFoundation/ANKBitPatternConvertible.swift b/Sources/ANKFoundation/ANKBitPatternConvertible.swift index b028af0f..55e7d0fc 100644 --- a/Sources/ANKFoundation/ANKBitPatternConvertible.swift +++ b/Sources/ANKFoundation/ANKBitPatternConvertible.swift @@ -37,10 +37,6 @@ public protocol ANKBitPatternConvertible { /// @inlinable init(bitPattern source: some ANKBitPatternConvertible) - //=------------------------------------------------------------------------= - // MARK: Accessors - //=------------------------------------------------------------------------= - /// The bit pattern of this value. /// /// ```swift @@ -66,10 +62,6 @@ extension ANKBitPatternConvertible where BitPattern == Self { self = source.bitPattern } - //=------------------------------------------------------------------------= - // MARK: Accessors - //=------------------------------------------------------------------------= - @_transparent public var bitPattern: BitPattern { self } diff --git a/Sources/ANKFoundation/ANKFixedWidthInteger.swift b/Sources/ANKFoundation/ANKFixedWidthInteger.swift index 75f1c80a..13e94094 100644 --- a/Sources/ANKFoundation/ANKFixedWidthInteger.swift +++ b/Sources/ANKFoundation/ANKFixedWidthInteger.swift @@ -15,7 +15,12 @@ /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKFixedWidthInteger: ANKBinaryInteger, FixedWidthInteger where Magnitude: ANKUnsignedFixedWidthInteger, Magnitude.BitPattern == BitPattern { @@ -335,7 +340,12 @@ extension ANKFixedWidthInteger { /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKSignedFixedWidthInteger: ANKFixedWidthInteger, ANKSignedInteger { @@ -399,6 +409,11 @@ extension ANKSignedFixedWidthInteger { /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKUnsignedFixedWidthInteger: ANKFixedWidthInteger, ANKUnsignedInteger { } diff --git a/Sources/ANKFoundation/ANKLargeBinaryInteger.swift b/Sources/ANKFoundation/ANKLargeBinaryInteger.swift index 5cfd3be8..0f9d2c79 100644 --- a/Sources/ANKFoundation/ANKLargeBinaryInteger.swift +++ b/Sources/ANKFoundation/ANKLargeBinaryInteger.swift @@ -15,7 +15,12 @@ /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKLargeBinaryInteger: ANKBinaryInteger where Magnitude: ANKUnsignedLargeBinaryInteger { @@ -342,7 +347,12 @@ extension ANKLargeBinaryInteger { /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKSignedLargeBinaryInteger: ANKLargeBinaryInteger, ANKSignedInteger where Digit: ANKSignedInteger { } @@ -355,7 +365,12 @@ ANKSignedInteger where Digit: ANKSignedInteger { } /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKUnsignedLargeBinaryInteger: ANKLargeBinaryInteger, ANKUnsignedInteger where Digit: ANKUnsignedInteger, Digit.Magnitude == Digit { } diff --git a/Sources/ANKFoundation/ANKLargeFixedWidthInteger.swift b/Sources/ANKFoundation/ANKLargeFixedWidthInteger.swift index bc6094ae..f4a8a8b0 100644 --- a/Sources/ANKFoundation/ANKLargeFixedWidthInteger.swift +++ b/Sources/ANKFoundation/ANKLargeFixedWidthInteger.swift @@ -15,7 +15,12 @@ /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKLargeFixedWidthInteger: ANKFixedWidthInteger, ANKLargeBinaryInteger where Digit: ANKFixedWidthInteger, Magnitude: ANKUnsignedLargeFixedWidthInteger { @@ -297,7 +302,12 @@ extension ANKLargeFixedWidthInteger { /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKSignedLargeFixedWidthInteger: ANKLargeFixedWidthInteger, ANKSignedFixedWidthInteger, ANKSignedLargeBinaryInteger where Digit: ANKSignedFixedWidthInteger { } @@ -310,7 +320,12 @@ ANKSignedFixedWidthInteger, ANKSignedLargeBinaryInteger where Digit: ANKSignedFi /// /// ### Two's Complement /// -/// Like `BinaryInteger`, its bitwise operations have two's complement semantics. +/// Like `BinaryInteger`, it has [two's complement][2s] semantics. +/// +/// - The two's complement representation of `+0` is an infinite sequence of `0s`. +/// - The two's complement representation of `-1` is an infinite sequence of `1s`. +/// +/// [2s]: https://en.wikipedia.org/wiki/Two%27s_complement /// public protocol ANKUnsignedLargeFixedWidthInteger: ANKLargeFixedWidthInteger, ANKUnsignedFixedWidthInteger, ANKUnsignedLargeBinaryInteger where Digit: ANKUnsignedFixedWidthInteger { } diff --git a/Sources/ANKFoundation/Aliases.swift b/Sources/ANKFoundation/Aliases.swift index e0bbfa4c..cda771b6 100644 --- a/Sources/ANKFoundation/Aliases.swift +++ b/Sources/ANKFoundation/Aliases.swift @@ -27,7 +27,7 @@ public typealias HL = (high: H, low: L) /// A low and a high value. public typealias LH = (low: L, high: H) -/// A partial value and a value indicating whether overflow occurred. +/// A partial value and an overflow indicator. public typealias PVO = (partialValue: PV, overflow: Bool) /// A quotient and a remainder, relating to division. diff --git a/Sources/ANKFullWidthKit/ANKFullWidth+Division+Digit.swift b/Sources/ANKFullWidthKit/ANKFullWidth+Division+Digit.swift index a51d1ee1..b3f50ed7 100644 --- a/Sources/ANKFullWidthKit/ANKFullWidth+Division+Digit.swift +++ b/Sources/ANKFullWidthKit/ANKFullWidth+Division+Digit.swift @@ -59,7 +59,7 @@ extension ANKFullWidth { qro.partialValue.quotient.formTwosComplement() } - if dividendIsLessThanZero, divisorIsLessThanZero, qro.partialValue.quotient.isLessThanZero { + if dividendIsLessThanZero && divisorIsLessThanZero && qro.partialValue.quotient.isLessThanZero { assert(Self.isSigned && self == Self.min && divisor == -1) assert(qro.partialValue.quotient == self) assert(qro.partialValue.remainder == Digit()) diff --git a/Sources/ANKFullWidthKit/ANKFullWidth+Division.swift b/Sources/ANKFullWidthKit/ANKFullWidth+Division.swift index 5bc9c71d..b0cd9355 100644 --- a/Sources/ANKFullWidthKit/ANKFullWidth+Division.swift +++ b/Sources/ANKFullWidthKit/ANKFullWidth+Division.swift @@ -59,7 +59,7 @@ extension ANKFullWidth { qro.partialValue.quotient.formTwosComplement() } - if dividendIsLessThanZero, divisorIsLessThanZero, qro.partialValue.quotient.isLessThanZero { + if dividendIsLessThanZero && divisorIsLessThanZero && qro.partialValue.quotient.isLessThanZero { assert(Self.isSigned && self == Self.min && divisor == -1) assert(qro.partialValue.quotient == self) assert(qro.partialValue.remainder == Self()) diff --git a/Tests/ANKFullWidthKitTests/192+Division.swift b/Tests/ANKFullWidthKitTests/192+Division.swift index a8573376..be0495ac 100644 --- a/Tests/ANKFullWidthKitTests/192+Division.swift +++ b/Tests/ANKFullWidthKitTests/192+Division.swift @@ -261,7 +261,7 @@ final class Int192TestsOnDivision: XCTestCase { XCTAssertNotNil(x % 2) XCTAssertNotNil(x.dividedReportingOverflow(by: 1)) XCTAssertNotNil(x.remainderReportingOverflow(dividingBy: 2)) - XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 1)) + XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 3)) XCTAssertNotNil(x.quotientAndRemainderReportingOverflow(dividingBy: 3)) XCTAssertNotNil(x.dividingFullWidth((1, 2))) } @@ -429,7 +429,7 @@ final class UInt192TestsOnDivision: XCTestCase { XCTAssertNotNil(x % 2) XCTAssertNotNil(x.dividedReportingOverflow(by: 1)) XCTAssertNotNil(x.remainderReportingOverflow(dividingBy: 2)) - XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 1)) + XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 3)) XCTAssertNotNil(x.quotientAndRemainderReportingOverflow(dividingBy: 3)) XCTAssertNotNil(x.dividingFullWidth((1, 2))) } diff --git a/Tests/ANKFullWidthKitTests/256+Division.swift b/Tests/ANKFullWidthKitTests/256+Division.swift index d6901ee9..274032d9 100644 --- a/Tests/ANKFullWidthKitTests/256+Division.swift +++ b/Tests/ANKFullWidthKitTests/256+Division.swift @@ -261,7 +261,7 @@ final class Int256TestsOnDivision: XCTestCase { XCTAssertNotNil(x % 2) XCTAssertNotNil(x.dividedReportingOverflow(by: 1)) XCTAssertNotNil(x.remainderReportingOverflow(dividingBy: 2)) - XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 1)) + XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 3)) XCTAssertNotNil(x.quotientAndRemainderReportingOverflow(dividingBy: 3)) XCTAssertNotNil(x.dividingFullWidth((1, 2))) } @@ -429,7 +429,7 @@ final class UInt256TestsOnDivision: XCTestCase { XCTAssertNotNil(x % 2) XCTAssertNotNil(x.dividedReportingOverflow(by: 1)) XCTAssertNotNil(x.remainderReportingOverflow(dividingBy: 2)) - XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 1)) + XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 3)) XCTAssertNotNil(x.quotientAndRemainderReportingOverflow(dividingBy: 3)) XCTAssertNotNil(x.dividingFullWidth((1, 2))) } diff --git a/Tests/ANKSignedKitTests/ANKSigned+Division.swift b/Tests/ANKSignedKitTests/ANKSigned+Division.swift index d3a26429..4b1a2e55 100644 --- a/Tests/ANKSignedKitTests/ANKSigned+Division.swift +++ b/Tests/ANKSignedKitTests/ANKSigned+Division.swift @@ -257,7 +257,7 @@ final class ANKSignedTestsOnDivision: XCTestCase { XCTAssertNotNil(x % 2) XCTAssertNotNil(x.dividedReportingOverflow(by: 1)) XCTAssertNotNil(x.remainderReportingOverflow(dividingBy: 2)) - XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 1)) + XCTAssertNotNil(x.quotientAndRemainder(dividingBy: 3)) XCTAssertNotNil(x.quotientAndRemainderReportingOverflow(dividingBy: 3)) XCTAssertNotNil(x.dividingFullWidth((1, 2))) }