Skip to content

Commit

Permalink
cleanup and _lowWord (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Mar 11, 2023
1 parent e6d240b commit 1484178
Show file tree
Hide file tree
Showing 12 changed files with 105 additions and 88 deletions.
8 changes: 4 additions & 4 deletions Sources/ANKFoundation/ANKBigEndianTextCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
// MARK: * ANK x Binary Integer x Big Endian Text Codable
//*============================================================================*

/// An integer type than can be decoded from and encoded to big endian text.
/// An integer type than can be converted to and from big-endian text.
///
/// - `Decode` big endian text with ``init(decoding:radix:)``.
/// - `Encode` big endian text with `String.init(encoding:radix:uppercase:)`.
/// - `Decode` big-endian text with ``init(decoding:radix:)``.
/// - `Encode` big-endian text with `String.init(encoding:radix:uppercase:)`.
///
/// - Note: The `BinaryInteger` protocol in the standard library does not provide
/// customization points for its binary integer coding methods. Converting to
/// and from big endian text happens to be particularly well suited for machine
/// and from big-endian text happens to be particularly well suited for machine
/// word arithmetic, however, so these methods were added instead.
///
public protocol ANKBigEndianTextCodable {
Expand Down
5 changes: 4 additions & 1 deletion Sources/ANKFullWidthKit/ANKFullWidth+Arithmetic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ extension ANKFullWidth {
// MARK: Utilities
//=------------------------------------------------------------------------=

/// The absolute value of the remainder of dividing this value by its bit width.
///
/// - Returns: `abs(self % Self.bitWidth)`
@inlinable var absoluteValueModuloBitWidth: Int {
///
@inlinable var moduloBitWidth: Int {
//=--------------------------------------=
if Self.bitWidth.isPowerOf2 {
return Int(bitPattern: self.first) & (Self.bitWidth &- 1)
Expand Down
2 changes: 1 addition & 1 deletion Sources/ANKFullWidthKit/ANKFullWidth+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ extension ANKFullWidth where High == High.Magnitude {
let increment = Plus1(descending: HL(UInt(), divisor._bitshiftedLeft(words: quotientIndex, bits: Int())))
var approximation = Plus1(descending: increment.low.multipliedFullWidth(by: digit) as HL<Digit, Magnitude>)
//=------------------------------=
// Decrement When Overestimated
// Decrement If Overestimated
//=------------------------------=
if approximation > remainder {
brrrrrrrrrrrrrrrrrrrrrrr: do { digit &-= 1; approximation &-= increment }
Expand Down
4 changes: 2 additions & 2 deletions Sources/ANKFullWidthKit/ANKFullWidth+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension ANKFullWidth {
//=------------------------------------------------------------------------=

@inlinable public static func &<<=(lhs: inout Self, rhs: Self) {
lhs._bitshiftLeft(by: rhs.absoluteValueModuloBitWidth)
lhs._bitshiftLeft(by: rhs.moduloBitWidth)
}

@_transparent public static func &<<(lhs: Self, rhs: Self) -> Self {
Expand Down Expand Up @@ -105,7 +105,7 @@ extension ANKFullWidth {
//=------------------------------------------------------------------------=

@inlinable public static func &>>=(lhs: inout Self, rhs: Self) {
lhs._bitshiftRight(by: rhs.absoluteValueModuloBitWidth)
lhs._bitshiftRight(by: rhs.moduloBitWidth)
}

@_transparent public static func &>>(lhs: Self, rhs: Self) -> Self {
Expand Down
14 changes: 14 additions & 0 deletions Sources/ANKFullWidthKit/ANKFullWidth+Words.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ extension ANKFullWidth {
@inlinable public var trailingZeroBitCount: Int {
self.low.isZero ? Low.bitWidth &+ self.high.trailingZeroBitCount : self.low.trailingZeroBitCount
}

//=------------------------------------------------------------------------=
// MARK: Accessors
//=------------------------------------------------------------------------=

/// The least significant word of this value.
///
/// This is a top-secret™ requirement of [BinaryInteger][].
///
/// []: https://github.com/apple/swift/blob/main/stdlib/public/core/Integers.swift
///
@_transparent public var _lowWord: UInt {
self.low._lowWord
}
}

//=----------------------------------------------------------------------------=
Expand Down
2 changes: 1 addition & 1 deletion Tests/ANKFullWidthKitBenchmarks/192+Complements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private typealias Y = ANK192X32

final class Int192BenchmarksOnComplements: XCTestCase {

typealias T = ANKInt192
typealias T = ANKInt192
typealias M = ANKUInt192

//=------------------------------------------------------------------------=
Expand Down
2 changes: 1 addition & 1 deletion Tests/ANKFullWidthKitTests/128.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ final class UInt128Tests: XCTestCase {
func testInitAscending() {
XCTAssertEqual(T(ascending: (ANKUInt64(1), ANKUInt64(2))), T(x64: X(1, 2)))
}

func testInitDescending() {
XCTAssertEqual(T(descending: (ANKUInt64(2), ANKUInt64(1))), T(x64: X(1, 2)))
}
Expand Down
52 changes: 26 additions & 26 deletions Tests/ANKFullWidthKitTests/192+Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ final class Int192TestsOnCollection: XCTestCase {

x0.withUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
}; XCTAssertEqual(x0, x1)
}

Expand All @@ -60,14 +60,14 @@ final class Int192TestsOnCollection: XCTestCase {

let y0 = T.fromUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
BYTES.indices.forEach({ BYTES[$0] = 0x00 })
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0x00 })
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
}; XCTAssertEqual(x0, y0)

let y1 = T.fromUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
}; XCTAssertEqual(x1, y1)
}

Expand Down Expand Up @@ -96,9 +96,9 @@ final class Int192TestsOnCollection: XCTestCase {

x0.withUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}

Expand Down Expand Up @@ -144,9 +144,9 @@ final class Int192TestsOnCollection: XCTestCase {

x0.withContiguousMutableStorageIfAvailable { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}
}
Expand Down Expand Up @@ -184,9 +184,9 @@ final class UInt192TestsOnCollection: XCTestCase {

x0.withUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
}; XCTAssertEqual(x0, x1)
}

Expand All @@ -196,14 +196,14 @@ final class UInt192TestsOnCollection: XCTestCase {

let y0 = T.fromUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
BYTES.indices.forEach({ BYTES[$0] = 0x00 })
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0x00 })
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
}; XCTAssertEqual(x0, y0)

let y1 = T.fromUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
}; XCTAssertEqual(x1, y1)
}

Expand Down Expand Up @@ -232,9 +232,9 @@ final class UInt192TestsOnCollection: XCTestCase {

x0.withUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}

Expand Down Expand Up @@ -280,9 +280,9 @@ final class UInt192TestsOnCollection: XCTestCase {

x0.withContiguousMutableStorageIfAvailable { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}
}
Expand Down
24 changes: 12 additions & 12 deletions Tests/ANKFullWidthKitTests/192+Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ final class Int192TestsOnText: XCTestCase {
XCTAssertEqual( "0o1234567012345670", T( 0o1234567012345670 as Int64))
XCTAssertEqual( "0b1010101010101010", T( 0b1010101010101010 as Int64))

XCTAssertEqual("+1234567890", T(+1234567890 as Int64))
XCTAssertEqual("+0x123456789abcdef0", T(+0x123456789abcdef0 as Int64))
XCTAssertEqual("+0o1234567012345670", T(+0o1234567012345670 as Int64))
XCTAssertEqual("+0b1010101010101010", T(+0b1010101010101010 as Int64))
XCTAssertEqual("+1234567890", T( 1234567890 as Int64))
XCTAssertEqual("+0x123456789abcdef0", T( 0x123456789abcdef0 as Int64))
XCTAssertEqual("+0o1234567012345670", T( 0o1234567012345670 as Int64))
XCTAssertEqual("+0b1010101010101010", T( 0b1010101010101010 as Int64))

XCTAssertEqual("-1234567890", T(-1234567890 as Int64))
XCTAssertEqual("-0x123456789abcdef0", T(-0x123456789abcdef0 as Int64))
Expand Down Expand Up @@ -213,15 +213,15 @@ final class UInt192TestsOnText: XCTestCase {
}

func testDecodingStringsWithOrWithoutSignAndRadixLiteral() {
XCTAssertEqual( "1234567890", T( 1234567890 as UInt64))
XCTAssertEqual( "0x123456789abcdef0", T( 0x123456789abcdef0 as UInt64))
XCTAssertEqual( "0o1234567012345670", T( 0o1234567012345670 as UInt64))
XCTAssertEqual( "0b1010101010101010", T( 0b1010101010101010 as UInt64))
XCTAssertEqual( "1234567890", T(1234567890 as UInt64))
XCTAssertEqual( "0x123456789abcdef0", T(0x123456789abcdef0 as UInt64))
XCTAssertEqual( "0o1234567012345670", T(0o1234567012345670 as UInt64))
XCTAssertEqual( "0b1010101010101010", T(0b1010101010101010 as UInt64))

XCTAssertEqual("+1234567890", T(+1234567890 as UInt64))
XCTAssertEqual("+0x123456789abcdef0", T(+0x123456789abcdef0 as UInt64))
XCTAssertEqual("+0o1234567012345670", T(+0o1234567012345670 as UInt64))
XCTAssertEqual("+0b1010101010101010", T(+0b1010101010101010 as UInt64))
XCTAssertEqual("+1234567890", T(1234567890 as UInt64))
XCTAssertEqual("+0x123456789abcdef0", T(0x123456789abcdef0 as UInt64))
XCTAssertEqual("+0o1234567012345670", T(0o1234567012345670 as UInt64))
XCTAssertEqual("+0b1010101010101010", T(0b1010101010101010 as UInt64))
}

func testDecodingPrefixingZerosHasNoEffect() {
Expand Down
4 changes: 2 additions & 2 deletions Tests/ANKFullWidthKitTests/256+Addition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ final class UInt256TestsOnAddition: XCTestCase {
XCTAssertEqual(T(0) + UInt(0), T(0))
XCTAssertEqual(T(0) + UInt(1), T(1))
XCTAssertEqual(T(0) + UInt(2), T(2))

XCTAssertEqual(T(1) + UInt(0), T(1))
XCTAssertEqual(T(1) + UInt(1), T(2))
XCTAssertEqual(T(1) + UInt(2), T(3))

XCTAssertEqual(T(x64: X(~0, ~0, ~0, 0)) + UInt(3), T(x64: X(2, 0, 0, 1)))
XCTAssertEqual(T(x64: X(~0, ~0, 0, 0)) + UInt(3), T(x64: X(2, 0, 1, 0)))
XCTAssertEqual(T(x64: X(~0, 0, 0, 0)) + UInt(3), T(x64: X(2, 1, 0, 0)))
Expand Down
52 changes: 26 additions & 26 deletions Tests/ANKFullWidthKitTests/256+Collection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ final class Int256TestsOnCollection: XCTestCase {

x0.withUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
}; XCTAssertEqual(x0, x1)
}

Expand All @@ -60,14 +60,14 @@ final class Int256TestsOnCollection: XCTestCase {

let y0 = T.fromUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
BYTES.indices.forEach({ BYTES[$0] = 0x00 })
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0x00 })
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
}; XCTAssertEqual(x0, y0)

let y1 = T.fromUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
}; XCTAssertEqual(x1, y1)
}

Expand Down Expand Up @@ -96,9 +96,9 @@ final class Int256TestsOnCollection: XCTestCase {

x0.withUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}

Expand Down Expand Up @@ -144,9 +144,9 @@ final class Int256TestsOnCollection: XCTestCase {

x0.withContiguousMutableStorageIfAvailable { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}
}
Expand Down Expand Up @@ -184,9 +184,9 @@ final class UInt256TestsOnCollection: XCTestCase {

x0.withUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
}; XCTAssertEqual(x0, x1)
}

Expand All @@ -196,14 +196,14 @@ final class UInt256TestsOnCollection: XCTestCase {

let y0 = T.fromUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
BYTES.indices.forEach({ BYTES[$0] = 0x00 })
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
BYTES.indices.forEach({ BYTES[$0] = 0x00 })
XCTAssert(BYTES.allSatisfy({ $0 == 0x00 }))
}; XCTAssertEqual(x0, y0)

let y1 = T.fromUnsafeMutableBytes { BYTES in
XCTAssertEqual(BYTES.count, T.bitWidth/8)
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
BYTES.indices.forEach({ BYTES[$0] = 0xff })
XCTAssert(BYTES.allSatisfy({ $0 == 0xff }))
}; XCTAssertEqual(x1, y1)
}

Expand Down Expand Up @@ -232,9 +232,9 @@ final class UInt256TestsOnCollection: XCTestCase {

x0.withUnsafeMutableWords { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}

Expand Down Expand Up @@ -280,9 +280,9 @@ final class UInt256TestsOnCollection: XCTestCase {

x0.withContiguousMutableStorageIfAvailable { WORDS in
XCTAssertEqual(WORDS.count, T.count)
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
XCTAssert(WORDS.allSatisfy({ $0 == UInt.min }))
WORDS.indices.forEach({ WORDS[$0] = UInt.max })
XCTAssert(WORDS.allSatisfy({ $0 == UInt.max }))
}; XCTAssertEqual(x0, x1)
}
}
Expand Down
Loading

0 comments on commit 1484178

Please sign in to comment.