Skip to content

Commit

Permalink
no minLastIndex[...] methods (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Jun 15, 2023
1 parent af28654 commit 554905a
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 484 deletions.
28 changes: 28 additions & 0 deletions Sources/ANKCoreKit/Private/Namespace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,32 @@

/// An unsafe pointer to a mutable collection of `UInt` machine words.
public typealias UnsafeMutableWords = UnsafeMutableBufferPointer<UInt>

//=------------------------------------------------------------------------=
// MARK: Aliases x Tuples
//=------------------------------------------------------------------------=

/// A 128-bit pattern, split into `UInt64` words.
public typealias U128X64 = (UInt64, UInt64)

/// A 128-bit pattern, split into `UInt32` words.
public typealias U128X32 = (UInt32, UInt32, UInt32, UInt32)

/// A 192-bit pattern, split into `UInt64` words.
public typealias U192X64 = (UInt64, UInt64, UInt64)

/// A 192-bit pattern, split into `UInt32` words.
public typealias U192X32 = (UInt32, UInt32, UInt32, UInt32, UInt32, UInt32)

/// A 256-bit pattern, split into `UInt64` words.
public typealias U256X64 = (UInt64, UInt64, UInt64, UInt64)

/// A 256-bit pattern, split into `UInt32` words.
public typealias U256X32 = (UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32)

/// A 384-bit pattern, split into `UInt64` words.
public typealias U384X64 = (UInt64, UInt64, UInt64, UInt64, UInt64, UInt64)

/// A 384-bit pattern, split into `UInt32` words.
public typealias U384X32 = (UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32, UInt32)
}
39 changes: 0 additions & 39 deletions Sources/ANKCoreKit/Private/Tuples.swift

This file was deleted.

41 changes: 22 additions & 19 deletions Sources/ANKFullWidthKit/ANKFullWidth+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,30 +124,29 @@ extension ANKFullWidth where High == High.Magnitude {
@_specialize(where Self == UInt384)
@_specialize(where Self == UInt512)
@inlinable func quotientAndRemainderReportingOverflow(dividingBy other: Self) -> PVO<QR<Self, Self>> {
let other_ = other.minLastIndexReportingIsZeroOrMinusOne()
//=--------------------------------------=
// divisor is zero
//=--------------------------------------=
if other_.isZeroOrMinusOne {
guard let otherMinLastIndex: Int = other.lastIndex(where:{ !$0.isZero }) else {
return PVO(QR(self, self), true)
}
//=--------------------------------------=
// divisor is greater than or equal
//=--------------------------------------=
if self <= other {
return self == other ? PVO(QR(1, Self.zero), false) : PVO(QR(Self.zero, self), false)
}
//=--------------------------------------=
// divisor is one word
//=--------------------------------------=
if other_.minLastIndex.isZero {
if otherMinLastIndex.isZero {
let qro: PVO<QR<Self, Digit>> = self.quotientAndRemainderReportingOverflow(dividingBy: other.first)
return PVO(QR(qro.partialValue.quotient, Self(digit: qro.partialValue.remainder)), qro.overflow)
}
//=--------------------------------------=
let self_ = self.minLastIndexReportingIsZeroOrMinusOne()
let minLastIndexGapSize = self_.minLastIndex &- other_.minLastIndex as Int
let shift: Int = other[unchecked: other_.minLastIndex].leadingZeroBitCount
// divisor is greater than or equal
//=--------------------------------------=
if self <= other {
return self == other ? PVO(QR(1, Self.zero), false) : PVO(QR(Self.zero, self), false)
}
//=--------------------------------------=
let thisMinLastIndex: Int = self.lastIndex(where:{ !$0.isZero })!
let minLastIndexGapSize = thisMinLastIndex &- otherMinLastIndex as Int
let shift = other[unchecked: otherMinLastIndex].leadingZeroBitCount as Int
//=--------------------------------------=
// shift to clamp approximation
//=--------------------------------------=
Expand All @@ -158,7 +157,7 @@ extension ANKFullWidth where High == High.Magnitude {
increment.low.bitshiftLeftUnchecked(words: minLastIndexGapSize, bits: shift)
assert(increment.high.isZero)

let discriminant: UInt = increment.low[unchecked: self_.minLastIndex]
let discriminant: UInt = increment.low[unchecked: thisMinLastIndex]
assert(discriminant.mostSignificantBit)
//=--------------------------------------=
// division
Expand All @@ -172,11 +171,11 @@ extension ANKFullWidth where High == High.Magnitude {
//=------------------------------=
// approximate quotient digit
//=------------------------------=
var digit: UInt = remainder.withUnsafeWords { remainder in
let remainderIndex = other_.minLastIndex &+ quotientIndex
let remainderLast0 = remainder[unchecked: remainderIndex &+ 1]
if remainderLast0 >= discriminant { return UInt.max }
let remainderLast1 = remainder[unchecked: remainderIndex]
var digit: UInt = remainder.withUnsafeWords { remainder in
let remainderIndex = otherMinLastIndex &+ quotientIndex
let remainderLast0 = remainder[unchecked: remainderIndex &+ 1]
if remainderLast0 >= discriminant { return UInt.max }
let remainderLast1 = remainder[unchecked: remainderIndex]
return discriminant.dividingFullWidth(HL(remainderLast0, remainderLast1)).quotient
}
//=------------------------------=
Expand All @@ -188,10 +187,14 @@ extension ANKFullWidth where High == High.Magnitude {
brrrrrrrrrrrrrrrrrrrrrrr: do { digit &-= 1; approximation &-= increment }
if approximation > remainder { digit &-= 1; approximation &-= increment }
}
//=------------------------------=

assert(approximation <= remainder)
//=------------------------------=
remainder &-= approximation
quotient[unchecked: quotientIndex] = digit
//=------------------------------=
guard !quotientIndex.isZero else { break }
assert(increment.low.first.isZero)
increment.low.bitshiftRightUnchecked(words: 1, bits: Int.zero)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/ANKFullWidthKit/ANKFullWidth+Text+Radix.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ extension ANKFullWidth where High == High.Magnitude {
}

@inlinable func description(radix: PerfectRadixUIntRoot, alphabet: MaxRadixAlphabetEncoder, prefix: ANK.UnsafeUTF8) -> String {
let minLastIndex: Int = self.minLastIndexReportingIsZeroOrMinusOne().minLastIndex
let chunks: Words.SubSequence = self[...minLastIndex]
let index = self.lastIndex(where:{ !$0.isZero }) ?? self.startIndex
let chunks: Words.SubSequence = self[...index]
return String.fromUTF8Unchecked(chunks: chunks, radix: radix, alphabet: alphabet, prefix: prefix)
}

Expand Down
52 changes: 0 additions & 52 deletions Sources/ANKFullWidthKit/ANKFullWidth+Words+Utilities.swift

This file was deleted.

97 changes: 0 additions & 97 deletions Tests/ANKFullWidthKitBenchmarks/192+Words.swift

This file was deleted.

Loading

0 comments on commit 554905a

Please sign in to comment.