Skip to content

Commit

Permalink
[NBKFlexibleWidthKit] Merge through 5f459ca.
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Oct 23, 2023
2 parents 417832a + 5f459ca commit 40ab09e
Show file tree
Hide file tree
Showing 21 changed files with 316 additions and 1,249 deletions.
116 changes: 8 additions & 108 deletions Sources/NBKCoreKit/NBKFixedWidthInteger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ Digit: NBKFixedWidthInteger, Magnitude: NBKFixedWidthInteger, Magnitude.BitPatte
/// - Parameters:
/// - distance: `0 <= distance < Self.bitWidth`
///
@inlinable mutating func bitshiftLeft(by distance: Int)
@inlinable mutating func bitShiftLeft(by distance: Int)

/// Performs a left shift.
///
Expand All @@ -454,7 +454,7 @@ Digit: NBKFixedWidthInteger, Magnitude: NBKFixedWidthInteger, Magnitude.BitPatte
/// - Parameters:
/// - distance: `0 <= distance < Self.bitWidth`
///
@inlinable func bitshiftedLeft(by distance: Int) -> Self
@inlinable func bitShiftedLeft(by distance: Int) -> Self

/// Performs an un/signed right shift.
///
Expand All @@ -473,7 +473,7 @@ Digit: NBKFixedWidthInteger, Magnitude: NBKFixedWidthInteger, Magnitude.BitPatte
/// - Parameters:
/// - distance: `0 <= distance < Self.bitWidth`
///
@inlinable mutating func bitshiftRight(by distance: Int)
@inlinable mutating func bitShiftRight(by distance: Int)

/// Performs an un/signed right shift.
///
Expand All @@ -492,75 +492,7 @@ Digit: NBKFixedWidthInteger, Magnitude: NBKFixedWidthInteger, Magnitude.BitPatte
/// - Parameters:
/// - distance: `0 <= distance < Self.bitWidth`
///
@inlinable func bitshiftedRight(by distance: Int) -> Self

//=------------------------------------------------------------------------=
// MARK: Details x Rotations
//=------------------------------------------------------------------------=

/// Performs a left rotation.
///
/// ```
/// ┌────────────┬───────── → ───────────┐
/// │ self │ distance │ self │
/// ├────────────┼───────── → ───────────┤
/// │ Int256( 1) │ Int(255) │ Int256.min │
/// │ Int256.min │ Int( 1) │ Int256( 1) │
/// └────────────┴───────── → ───────────┘
/// ```
///
/// - Parameters:
/// - distance: `0 <= distance < Self.bitWidth`
///
@inlinable mutating func bitrotateLeft(by distance: Int)

/// Performs a left rotation.
///
/// ```
/// ┌────────────┬───────── → ───────────┐
/// │ self │ distance │ self │
/// ├────────────┼───────── → ───────────┤
/// │ Int256( 1) │ Int(255) │ Int256.min │
/// │ Int256.min │ Int( 1) │ Int256( 1) │
/// └────────────┴───────── → ───────────┘
/// ```
///
/// - Parameters:
/// - distance: `0 <= distance < Self.bitWidth`
///
@inlinable func bitrotatedLeft(by distance: Int) -> Self

/// Performs a right rotation.
///
/// ```
/// ┌────────────┬───────── → ───────────┐
/// │ self │ distance │ self │
/// ├────────────┼───────── → ───────────┤
/// │ Int256.min │ Int(255) │ Int256( 1) │
/// │ Int256( 1) │ Int( 1) │ Int256.min │
/// └────────────┴───────── → ───────────┘
/// ```
///
/// - Parameters:
/// - distance: `0 <= distance < Self.bitWidth`
///
@inlinable mutating func bitrotateRight(by distance: Int)

/// Performs a right rotation.
///
/// ```
/// ┌────────────┬───────── → ───────────┐
/// │ self │ distance │ self │
/// ├────────────┼───────── → ───────────┤
/// │ Int256.min │ Int(255) │ Int256( 1) │
/// │ Int256( 1) │ Int( 1) │ Int256.min │
/// └────────────┴───────── → ───────────┘
/// ```
///
/// - Parameters:
/// - distance: `0 <= distance < Self.bitWidth`
///
@inlinable func bitrotatedRight(by distance: Int) -> Self
@inlinable func bitShiftedRight(by distance: Int) -> Self
}

//=----------------------------------------------------------------------------=
Expand Down Expand Up @@ -739,57 +671,25 @@ extension NBKFixedWidthInteger {
// MARK: Details x Shifts
//=------------------------------------------------------------------------=

@inlinable public mutating func bitshiftLeft(by distance: Int) {
@inlinable public mutating func bitShiftLeft(by distance: Int) {
precondition(0 ..< self.bitWidth ~= distance, NBK.callsiteOutOfBoundsInfo())
self &<<= distance
}

@inlinable public func bitshiftedLeft(by distance: Int) -> Self {
@inlinable public func bitShiftedLeft(by distance: Int) -> Self {
precondition(0 ..< self.bitWidth ~= distance, NBK.callsiteOutOfBoundsInfo())
return self &<< distance
}

@inlinable public mutating func bitshiftRight(by distance: Int) {
@inlinable public mutating func bitShiftRight(by distance: Int) {
precondition(0 ..< self.bitWidth ~= distance, NBK.callsiteOutOfBoundsInfo())
self &>>= distance
}

@inlinable public func bitshiftedRight(by distance: Int) -> Self {
@inlinable public func bitShiftedRight(by distance: Int) -> Self {
precondition(0 ..< self.bitWidth ~= distance, NBK.callsiteOutOfBoundsInfo())
return self &>> distance
}

//=------------------------------------------------------------------------=
// MARK: Details x Rotations
//=------------------------------------------------------------------------=

@inlinable public mutating func bitrotateLeft(by distance: Int) {
self = self.bitrotatedLeft(by: distance)
}

@inlinable public func bitrotatedLeft(by distance: Int) -> Self {
precondition(0 ..< Self.bitWidth ~= distance, NBK.callsiteOutOfBoundsInfo())
//=--------------------------------------=
if distance.isZero { return self }
//=--------------------------------------=
let pushed = self &<< (distance)
let pulled = Magnitude(bitPattern: self) &>> (Self.bitWidth &- distance)
return pushed | Self(bitPattern: pulled)
}

@inlinable public mutating func bitrotateRight(by distance: Int) {
self = self.bitrotatedRight(by: distance)
}

@inlinable public func bitrotatedRight(by distance: Int) -> Self {
precondition(0 ..< Self.bitWidth ~= distance, NBK.callsiteOutOfBoundsInfo())
//=--------------------------------------=
if distance.isZero { return self }
//=--------------------------------------=
let pulled = self &<< (Self.bitWidth &- distance)
let pushed = Magnitude(bitPattern: self) &>> (distance)
return Self(bitPattern: pushed) | pulled
}

//=------------------------------------------------------------------------=
// MARK: Details x Text
Expand Down
16 changes: 8 additions & 8 deletions Sources/NBKCoreKit/Private/NBKStrictBinaryInteger+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension NBK.StrictBinaryInteger where Base: MutableCollection {
/// - environment: The element used to fill the void.
/// - major: `1 <= major < base.count`
///
@inlinable public static func bitshiftLeft(
@inlinable public static func bitShiftLeft(
_ base: inout Base, environment: Base.Element, majorAtLeastOne major: Int) {
//=--------------------------------------=
var destination = base.endIndex as Base.Index
Expand Down Expand Up @@ -70,9 +70,9 @@ extension NBK.StrictBinaryInteger where Base: MutableCollection {
/// - major: `0 <= major < base.count`
/// - minor: `1 <= minor < Base.Element.bitWidth`
///
@inlinable public static func bitshiftLeft(
@inlinable public static func bitShiftLeft(
_ base: inout Base, environment: Base.Element, major: Int, minorAtLeastOne minor: Int) {
self.bitshiftLeftCodeBlock(&base, environment: environment, major: major, minorAtLeastOne: minor)
self.bitShiftLeftCodeBlock(&base, environment: environment, major: major, minorAtLeastOne: minor)
}

/// Performs a left shift, assuming the `base` is ordered from least to most significant.
Expand All @@ -87,7 +87,7 @@ extension NBK.StrictBinaryInteger where Base: MutableCollection {
///
/// `@inline(always)` is required for `NBKDoubleWidth` performance reasons.
///
@inline(__always) @inlinable public static func bitshiftLeftCodeBlock(
@inline(__always) @inlinable public static func bitShiftLeftCodeBlock(
_ base: inout Base, environment: Base.Element, major: Int, minorAtLeastOne minor: Int) {
//=--------------------------------------=
precondition(1 <= minor && minor < Base.Element.bitWidth, NBK.callsiteOutOfBoundsInfo())
Expand Down Expand Up @@ -139,7 +139,7 @@ extension NBK.StrictBinaryInteger where Base: MutableCollection {
/// - environment: The element used to fill the void.
/// - major: `1 <= major < base.count`
///
@inlinable public static func bitshiftRight(
@inlinable public static func bitShiftRight(
_ base: inout Base, environment: Base.Element, majorAtLeastOne major: Int) {
//=--------------------------------------=
var destination = base.startIndex as Base.Index
Expand Down Expand Up @@ -181,9 +181,9 @@ extension NBK.StrictBinaryInteger where Base: MutableCollection {
/// - major: `0 <= major < base.count`
/// - minor: `1 <= minor < Base.Element.bitWidth`
///
@inlinable public static func bitshiftRight(
@inlinable public static func bitShiftRight(
_ base: inout Base, environment: Base.Element, major: Int, minorAtLeastOne minor: Int) {
Self.bitshiftRightCodeBlock(&base, environment: environment, major: major, minorAtLeastOne: minor)
Self.bitShiftRightCodeBlock(&base, environment: environment, major: major, minorAtLeastOne: minor)
}

/// Performs a right shift, assuming the `base` is ordered from least to most significant.
Expand All @@ -198,7 +198,7 @@ extension NBK.StrictBinaryInteger where Base: MutableCollection {
///
/// `@inline(always)` is required for `NBKDoubleWidth` performance reasons.
///
@inline(__always) @inlinable public static func bitshiftRightCodeBlock(
@inline(__always) @inlinable public static func bitShiftRightCodeBlock(
_ base: inout Base, environment: Base.Element, major: Int, minorAtLeastOne minor: Int) {
//=--------------------------------------=
precondition(1 <= minor && minor < Base.Element.bitWidth, NBK.callsiteOutOfBoundsInfo())
Expand Down
44 changes: 22 additions & 22 deletions Sources/NBKCoreKit/Private/NBKStrictUnsignedInteger+Shifts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ extension NBK.StrictUnsignedInteger where Base: MutableCollection {
/// - Parameters:
/// - distance: `0 <= distance < base.bitWidth`
///
@inlinable public static func bitshiftLeft(_ base: inout Base, by distance: Int) {
@inlinable public static func bitShiftLeft(_ base: inout Base, by distance: Int) {
let (major, minor) = distance.quotientAndRemainder(dividingBy: Base.Element.bitWidth)
return self.bitshiftLeft(&base, major: major, minor: minor)
return self.bitShiftLeft(&base, major: major, minor: minor)
}

/// Performs a left shift.
Expand All @@ -36,25 +36,25 @@ extension NBK.StrictUnsignedInteger where Base: MutableCollection {
/// - major: `0 <= major < base.endIndex`
/// - minor: `0 <= minor < Base.Element.bitWidth`
///
@inlinable public static func bitshiftLeft(_ base: inout Base, major: Int, minor: Int) {
@inlinable public static func bitShiftLeft(_ base: inout Base, major: Int, minor: Int) {
//=--------------------------------------=
if minor.isZero {
return self.bitshiftLeft(&base, major: major)
return self.bitShiftLeft(&base, major: major)
}
//=--------------------------------------=
self.bitshiftLeft(&base, major: major, minorAtLeastOne: minor)
self.bitShiftLeft(&base, major: major, minorAtLeastOne: minor)
}

/// Performs a left shift.
///
/// - Parameters:
/// - major: `0 <= major < base.endIndex`
///
@inlinable public static func bitshiftLeft(_ base: inout Base, major: Int) {
@inlinable public static func bitShiftLeft(_ base: inout Base, major: Int) {
//=--------------------------------------=
if major.isZero { return }
//=--------------------------------------=
self.bitshiftLeft(&base, majorAtLeastOne: major)
self.bitShiftLeft(&base, majorAtLeastOne: major)
}

//=------------------------------------------------------------------------=
Expand All @@ -67,17 +67,17 @@ extension NBK.StrictUnsignedInteger where Base: MutableCollection {
/// - major: `0 <= major < base.endIndex`
/// - minor: `1 <= minor < Base.Element.bitWidth`
///
@inlinable public static func bitshiftLeft(_ base: inout Base, major: Int, minorAtLeastOne minor: Int) {
Binary.bitshiftLeft(&base, environment: 0 as Base.Element, major: major, minorAtLeastOne: minor)
@inlinable public static func bitShiftLeft(_ base: inout Base, major: Int, minorAtLeastOne minor: Int) {
Binary.bitShiftLeft(&base, environment: 0 as Base.Element, major: major, minorAtLeastOne: minor)
}

/// Performs a left shift.
///
/// - Parameters:
/// - major: `1 <= major < base.endIndex`
///
@inlinable public static func bitshiftLeft(_ base: inout Base, majorAtLeastOne major: Int) {
Binary.bitshiftLeft(&base, environment: 0 as Base.Element, majorAtLeastOne: major)
@inlinable public static func bitShiftLeft(_ base: inout Base, majorAtLeastOne major: Int) {
Binary.bitShiftLeft(&base, environment: 0 as Base.Element, majorAtLeastOne: major)
}
}

Expand All @@ -96,9 +96,9 @@ extension NBK.StrictUnsignedInteger where Base: MutableCollection {
/// - Parameters:
/// - distance: `0 <= distance < base.bitWidth`
///
@inlinable public static func bitshiftRight(_ base: inout Base, by distance: Int) {
@inlinable public static func bitShiftRight(_ base: inout Base, by distance: Int) {
let (major, minor) = distance.quotientAndRemainder(dividingBy: Base.Element.bitWidth)
return self.bitshiftRight(&base, major: major, minor: minor)
return self.bitShiftRight(&base, major: major, minor: minor)
}

/// Performs an unsigned right shift.
Expand All @@ -107,25 +107,25 @@ extension NBK.StrictUnsignedInteger where Base: MutableCollection {
/// - major: `0 <= major < base.endIndex`
/// - bits: `0 <= minor < Base.Element.bitWidth`
///
@inlinable public static func bitshiftRight(_ base: inout Base, major: Int, minor: Int) {
@inlinable public static func bitShiftRight(_ base: inout Base, major: Int, minor: Int) {
//=--------------------------------------=
if minor.isZero {
return self.bitshiftRight(&base, major: major)
return self.bitShiftRight(&base, major: major)
}
//=--------------------------------------=
self.bitshiftRight(&base, major: major, minorAtLeastOne: minor)
self.bitShiftRight(&base, major: major, minorAtLeastOne: minor)
}

/// Performs an unsigned right shift.
///
/// - Parameters:
/// - major: `0 <= major < base.endIndex`
///
@inlinable public static func bitshiftRight(_ base: inout Base, major: Int) {
@inlinable public static func bitShiftRight(_ base: inout Base, major: Int) {
//=--------------------------------------=
if major.isZero { return }
//=--------------------------------------=
self.bitshiftRight(&base, majorAtLeastOne: major)
self.bitShiftRight(&base, majorAtLeastOne: major)
}

//=------------------------------------------------------------------------=
Expand All @@ -138,16 +138,16 @@ extension NBK.StrictUnsignedInteger where Base: MutableCollection {
/// - major: `0 <= major < base.endIndex`
/// - minor: `1 <= minor < Base.Element.bitWidth`
///
@inlinable public static func bitshiftRight(_ base: inout Base, major: Int, minorAtLeastOne minor: Int) {
Binary.bitshiftRight(&base, environment: 0 as Base.Element, major: major, minorAtLeastOne: minor)
@inlinable public static func bitShiftRight(_ base: inout Base, major: Int, minorAtLeastOne minor: Int) {
Binary.bitShiftRight(&base, environment: 0 as Base.Element, major: major, minorAtLeastOne: minor)
}

/// Performs an unsigned right shift.
///
/// - Parameters:
/// - major: `1 <= major < base.endIndex`
///
@inlinable public static func bitshiftRight(_ base: inout Base, majorAtLeastOne major: Int) {
Binary.bitshiftRight(&base, environment: 0 as Base.Element, majorAtLeastOne: major)
@inlinable public static func bitShiftRight(_ base: inout Base, majorAtLeastOne major: Int) {
Binary.bitShiftRight(&base, environment: 0 as Base.Element, majorAtLeastOne: major)
}
}
14 changes: 7 additions & 7 deletions Sources/NBKDoubleWidthKit/NBKDoubleWidth+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ extension NBKDoubleWidth where High == High.Magnitude {
let minor = NBK.remainder(dividing: shift, by: NBK.PowerOf2(bitWidth: UInt.self))

let top = shift.value.isZero ? High.zero : lhs.high &>> (High.bitWidth &- shift.value)
let lhs = lhs.bitshiftedLeft(major: major, minor: minor) as Self
let rhs = rhs.bitshiftedLeft(major: major, minor: minor) as Self
let lhs = lhs.bitShiftedLeft(major: major, minor: minor) as Self
let rhs = rhs.bitShiftedLeft(major: major, minor: minor) as Self
//=--------------------------------------=
// division: 3212 (normalized)
//=--------------------------------------=
let (quotient, remainder) = Self.divide3212MSBUnchecked(NBK.Wide3(top, lhs.high, lhs.low), by: rhs)
return QR(Self(low: quotient), remainder.bitshiftedRight(major: major, minor: minor))
return QR(Self(low: quotient), remainder.bitShiftedRight(major: major, minor: minor))
}

//=------------------------------------------------------------------------=
Expand Down Expand Up @@ -225,20 +225,20 @@ extension NBKDoubleWidth where High == High.Magnitude {
let major = NBK .quotient(dividing: shift, by: NBK.PowerOf2(bitWidth: UInt.self))
let minor = NBK.remainder(dividing: shift, by: NBK.PowerOf2(bitWidth: UInt.self))

let lhs = lhs.bitshiftedLeft(major: major, minor: minor) as NBKDoubleWidth<Self>
let rhs = rhs.bitshiftedLeft(major: major, minor: minor) as Self
let lhs = lhs.bitShiftedLeft(major: major, minor: minor) as NBKDoubleWidth<Self>
let rhs = rhs.bitShiftedLeft(major: major, minor: minor) as Self
//=--------------------------------------=
// division: 3212 (normalized)
//=--------------------------------------=
if lhs.high.high.isZero, rhs > Self(high: lhs.high.low, low: lhs.low.high) {
let (quotient, remainder) = Self.divide3212MSBUnchecked(NBK.Wide3(lhs.high.low, lhs.low.high, lhs.low.low), by: rhs)
return QR(Self(low: quotient), remainder.bitshiftedRight(major: major, minor: minor))
return QR(Self(low: quotient), remainder.bitShiftedRight(major: major, minor: minor))
}
//=--------------------------------------=
// division: 4222 (normalized)
//=--------------------------------------=
let (quotient, remainder) = Self.divide4222MSBUnchecked(lhs, by: rhs)
return QR(quotient, remainder.bitshiftedRight(major: major, minor: minor))
return QR(quotient, remainder.bitShiftedRight(major: major, minor: minor))
}

//=------------------------------------------------------------------------=
Expand Down
Loading

0 comments on commit 40ab09e

Please sign in to comment.