Skip to content

Commit

Permalink
[NBKFlexibleWidthKit] Dropped IntXL (#33) (#81).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Sep 16, 2023
1 parent 39a96aa commit fbe135f
Show file tree
Hide file tree
Showing 46 changed files with 41 additions and 4,408 deletions.
36 changes: 0 additions & 36 deletions Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Addition+Digit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,6 @@

import NBKCoreKit

//*============================================================================*
// MARK: * NBK x Flexible Width x Addition x Digit x Signed
//*============================================================================*

extension NBKFlexibleWidth {

//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=

@_disfavoredOverload @inlinable public static func +=(lhs: inout Self, rhs: Int) {
lhs.add(rhs, at: Int.zero)
}

@_disfavoredOverload @inlinable public static func +(lhs: Self, rhs: Int) -> Self {
lhs.adding(rhs, at: Int.zero)
}

//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=

@_disfavoredOverload @inlinable public mutating func add(_ other: Int, at index: Int) {
if self.sign == Sign(other.isLessThanZero) {
self.magnitude.add(other.magnitude, at: index)
} else if self.magnitude.subtractReportingOverflow(other.magnitude, at: index) {
self.sign.toggle()
self.magnitude.formTwosComplement()
}
}

@_disfavoredOverload @inlinable public func adding(_ other: Int, at index: Int) -> Self {
var result = self; result.add(other, at: index); return result
}
}

//*============================================================================*
// MARK: * NBK x Flexible Width x Addition x Digit x Unsigned
//*============================================================================*
Expand Down
36 changes: 0 additions & 36 deletions Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Addition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,6 @@

import NBKCoreKit

//*============================================================================*
// MARK: * NBK x Flexible Width x Addition x Signed
//*============================================================================*

extension NBKFlexibleWidth {

//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=

@inlinable public static func +=(lhs: inout Self, rhs: Self) {
lhs.add(rhs, at: Int.zero)
}

@inlinable public static func +(lhs: Self, rhs: Self) -> Self {
lhs.adding(rhs, at: Int.zero)
}

//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=

@inlinable public mutating func add(_ other: Self, at index: Int) {
if self.sign == other.sign {
self.magnitude.add(other.magnitude, at: index)
} else if self.magnitude.subtractReportingOverflow(other.magnitude, at: index) {
self.sign.toggle()
self.magnitude.formTwosComplement()
}
}

@inlinable public func adding(_ other: Self, at index: Int) -> Self {
var result = self; result.add(other, at: index); return result
}
}

//*============================================================================*
// MARK: * NBK x Flexible Width x Addition x Unsigned
//*============================================================================*
Expand Down
57 changes: 0 additions & 57 deletions Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Bits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,6 @@

import NBKCoreKit

//*============================================================================*
// MARK: * NBK x Flexible Width x Bits x Signed
//*============================================================================*

extension NBKFlexibleWidth {

//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=

@inlinable public init(bit: Bool) {
self.init(digit: Digit(bit: bit))
}

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

@inlinable public var bitWidth: Int {
self.magnitude.bitWidth + self.storageBitWidthNeeded
}

@inlinable public var nonzeroBitCount: Int {
if self.isLessThanZero {
let s = self.magnitude.storage.elements.withUnsafeBufferPointer(NBK.nonzeroBitCount(twosComplementOf:))
return s + self.storageBitWidthNeeded
} else {
return self.magnitude.nonzeroBitCount
}
}

@inlinable public var leadingZeroBitCount: Int {
self.isLessThanZero ? Int.zero : (self.magnitude.leadingZeroBitCount + self.storageBitWidthNeeded)
}

@inlinable public var trailingZeroBitCount: Int {
self.magnitude.trailingZeroBitCount
}

@inlinable public var mostSignificantBit: Bool {
self.isLessThanZero
}

@inlinable public var leastSignificantBit: Bool {
self.magnitude.leastSignificantBit
}

//=------------------------------------------------------------------------=
// MARK: Utilities x Private
//=------------------------------------------------------------------------=

/// The number of extra bits in `bitWidth` compared to `magnitude/bitWidth`.
@inlinable var storageBitWidthNeeded: Int {
self.storageNeedsOneMoreWord ? UInt.bitWidth : Int.zero
}
}

//*============================================================================*
// MARK: * NBK x Flexible Width x Bits x Unsigned
//*============================================================================*
Expand Down
97 changes: 0 additions & 97 deletions Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Comparisons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,103 +9,6 @@

import NBKCoreKit

//*============================================================================*
// MARK: * NBK x Flexible Width x Comparisons x Signed
//*============================================================================*

extension NBKFlexibleWidth {

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

@inlinable public var isZero: Bool {
self.magnitude.isZero
}

@inlinable public var isLessThanZero: Bool {
self.sign != Sign.plus && !self.isZero
}

@inlinable public var isMoreThanZero: Bool {
self.sign == Sign.plus && !self.isZero
}

@inlinable public var isPowerOf2: Bool {
self.sign == Sign.plus && self.magnitude.isPowerOf2
}

@inlinable public func signum() -> Int {
self.isZero ? 0 : self.sign == Sign.plus ? 1 : -1
}

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=

@inlinable public func hash(into hasher: inout Hasher) {
hasher.combine(self.sign == Sign.minus && self.isZero ? Sign.plus : self.sign)
hasher.combine(self.magnitude)
}

//=------------------------------------------------------------------------=
// MARK: Utilities
//=------------------------------------------------------------------------=

@inlinable public static func ==(lhs: Self, rhs: Self) -> Bool {
lhs.compared(to: rhs).isZero
}

@inlinable public static func <(lhs: Self, rhs: Self) -> Bool {
lhs.compared(to: rhs) == -1
}

@inlinable public func compared(to other: Self) -> Int {
Self.compare(self, to: other, magnitude:{ $0.compared(to: $1) })
}

@inlinable public func compared(to other: Self, at index: Int) -> Int {
Self.compare(self, to: other, magnitude:{ $0.compared(to: $1, at: index) })
}

@_disfavoredOverload @inlinable public func compared(to other: Digit) -> Int {
Self.compare(self, to: other, magnitude:{ $0.compared(to: $1) })
}

@_disfavoredOverload @inlinable public func compared(to other: Digit, at index: Int) -> Int {
Self.compare(self, to: other, magnitude:{ $0.compared(to: $1, at: index) })
}

//=------------------------------------------------------------------------=
// MARK: Accessors x Private
//=------------------------------------------------------------------------=

@inlinable var isTwosComplementMinValue: Bool {
self.compared(to: Int.min, at: self.magnitude.storage.elements.count - 1).isZero
}
}

//=----------------------------------------------------------------------------=
// MARK: + Algorithms
//=----------------------------------------------------------------------------=

extension NBKFlexibleWidth {

//=------------------------------------------------------------------------=
// MARK: Utilities x Private
//=------------------------------------------------------------------------=

@inlinable static func compare<T>(_ lhs: Self, to rhs: T, magnitude: (Magnitude, T.Magnitude) -> Int) -> Int where T: NBKBinaryInteger {
//=--------------------------------------=
if lhs.sign.bit != rhs.isLessThanZero {
return lhs.isZero && rhs.isZero ? 0 : lhs.sign.bit ? -1 : 1
}
//=--------------------------------------=
let magnitude = magnitude(lhs.magnitude, rhs.magnitude)
return lhs.sign.bit ? magnitude.negated() : magnitude
}
}

//*============================================================================*
// MARK: * NBK x Flexible Width x Comparisons x Unsigned
//*============================================================================*
Expand Down
61 changes: 0 additions & 61 deletions Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Complements.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,6 @@

import NBKCoreKit

//*============================================================================*
// MARK: * NBK x Flexible Width x Complements x Signed
//*============================================================================*

extension NBKFlexibleWidth {

//=------------------------------------------------------------------------=
// MARK: Details x Two's Complement
//=------------------------------------------------------------------------=

@inlinable public mutating func formOnesComplement() {
self.add(1 as Int, at: Int.zero)
self.negate()
}

@inlinable public func onesComplement() -> Self {
var result = self; result.formOnesComplement(); return result
}

//=------------------------------------------------------------------------=
// MARK: Details x Two's Complement
//=------------------------------------------------------------------------=

@inlinable public mutating func formTwosComplementReportingOverflow() -> Bool {
self.isTwosComplementMinValue || self.negateReportingOverflow()
}

@inlinable public func twosComplementReportingOverflow() -> PVO<Self> {
var partialValue = self
let overflow = partialValue.formTwosComplementReportingOverflow()
return PVO(partialValue, overflow)
}

@inlinable public mutating func formTwosComplementSubsequence(_ carry: Bool) -> Bool {
switch carry {
case true : return self.formTwosComplementReportingOverflow()
case false: self.formOnesComplement(); return false as Bool }
}

@inlinable public func twosComplementSubsequence(_ carry: Bool) -> PVO<Self> {
var partialValue = self
let overflow = partialValue.formTwosComplementSubsequence(carry)
return PVO(partialValue, overflow)
}

//=------------------------------------------------------------------------=
// MARK: Details x Additive Inverse
//=------------------------------------------------------------------------=

@inlinable public mutating func negateReportingOverflow() -> Bool {
self.sign.toggle()
return false as Bool
}

@inlinable public func negatedReportingOverflow() -> PVO<Self> {
var partialValue = self
let overflow = partialValue.negateReportingOverflow()
return PVO(partialValue, overflow)
}
}

//*============================================================================*
// MARK: * NBK x Flexible Width x Complements x Unsigned
//*============================================================================*
Expand Down
41 changes: 0 additions & 41 deletions Sources/NBKFlexibleWidthKit/NBKFlexibleWidth+Division+Digit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,6 @@

import NBKCoreKit

//*============================================================================*
// MARK: * NBK x Flexible Width x Division x Digit x Signed
//*============================================================================*

extension NBKFlexibleWidth {

//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=

@_disfavoredOverload @inlinable public mutating func divideReportingOverflow(by other: Digit) -> Bool {
let pvo: PVO<Self> = self.dividedReportingOverflow(by: other)
self = pvo.partialValue
return pvo.overflow as Bool
}

@_disfavoredOverload @inlinable public func dividedReportingOverflow(by other: Digit) -> PVO<Self> {
let qro: PVO<QR<Self, Digit>> = self.quotientAndRemainderReportingOverflow(dividingBy: other)
return PVO(qro.partialValue.quotient, qro.overflow)
}

@_disfavoredOverload @inlinable public mutating func formRemainderReportingOverflow(dividingBy other: Digit) -> Bool {
let pvo: PVO<Digit> = self.remainderReportingOverflow(dividingBy: other)
self = Self(digit: pvo.partialValue)
return pvo.overflow as Bool
}

@_disfavoredOverload @inlinable public func remainderReportingOverflow(dividingBy other: Digit) -> PVO<Digit> {
let qro: PVO<QR<Self, Digit>> = self.quotientAndRemainderReportingOverflow(dividingBy: other)
return PVO(qro.partialValue.remainder, qro.overflow)
}

@_disfavoredOverload @inlinable public func quotientAndRemainderReportingOverflow(dividingBy other: Digit) -> PVO<QR<Self, Digit>> {
let otherSign = Sign(other.isLessThanZero)
let qro: PVO<QR<Magnitude, Magnitude.Digit>> = self.magnitude.quotientAndRemainderReportingOverflow(dividingBy: other.magnitude)
let quotient = Self(sign: self.sign ^ otherSign, magnitude: qro.partialValue.quotient )
let remainder = Self.Digit.init(sign: self.sign, magnitude: qro.partialValue.remainder)!
return PVO(QR(quotient, remainder), qro.overflow)
}
}

//*============================================================================*
// MARK: * NBK x Flexible Width x Division x Digit x Unsigned
//*============================================================================*
Expand Down
Loading

0 comments on commit fbe135f

Please sign in to comment.