Skip to content

Commit

Permalink
Add an n-by-1 divider method to MutableDataInt.Body (#64).
Browse files Browse the repository at this point in the history
  • Loading branch information
oscbyspro committed Aug 8, 2024
1 parent 3ca1ccd commit 39b26fb
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions Sources/CoreKit/Models/DataInt+Division.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension MutableDataInt.Body {
///
/// - Note: This operation does not need `write` access.
///
/// - Important: This is `unsigned` and `finite`.
/// - Important: This operation is `unsigned` and `finite`.
///
@inlinable public func remainder(_ divisor: borrowing Nonzero<Element>) -> Element {
var remainder = Element()
Expand All @@ -43,9 +43,9 @@ extension MutableDataInt.Body {
/// - Requires: The `divisor` must be nonzero.
///
/// - Returns: The `quotient` is stored in `self` and the `remainder` is returned.
/// Note that the value of the `remainder` is less than the `divisor`.
/// Note that a valid `remainder` is always less than the `divisor`.
///
/// - Important: This is `unsigned` and `finite`.
/// - Important: This operation is `unsigned` and `finite`.
///
@inlinable public func divisionSetQuotientGetRemainder(_ divisor: borrowing Nonzero<Element>) -> Element {
var remainder = Element()
Expand All @@ -59,6 +59,28 @@ extension MutableDataInt.Body {

return remainder as Element
}

/// Returns the `quotient` and `remainder` of dividing `self` by the `divisor`.
///
/// - Requires: The `divisor` must be nonzero.
///
/// - Returns: The `quotient` is stored in `self` and the `remainder` is returned.
/// Note that a valid `remainder` is always less than the `divisor`.
///
/// - Important: This operation is `unsigned` and `finite`.
///
@inlinable public func divisionSetQuotientGetRemainder(_ divider: borrowing Divider21<Element>) -> Element {
var remainder = Element()

for index: IX in self.indices.reversed() {
let dividend = Doublet(low: self[unchecked: index], high: remainder)
let division = divider.division(dividing: dividend).unchecked()
self[unchecked: index] = division.quotient
((((((remainder )))))) = division.remainder
}

return remainder as Element
}
}

//*============================================================================*
Expand Down

0 comments on commit 39b26fb

Please sign in to comment.