Skip to content

Commit 36739f7

Browse files
author
Max Moiseev
committed
[stdlib] adding divRem to Integer in the prototype
1 parent 1747ac7 commit 36739f7

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

test/Prototypes/Integers.swift.gyb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,8 @@ public protocol Integer : ${IntegerBase} {
283283
/// by `Self`, and exact value of `source` otherwise.
284284
init<T : Integer>(clamping source: T)
285285

286-
/// Return n-th word in the underlying representation of `self`.
286+
/// Return n-th word (counting from the right) in the underlying
287+
/// representation of `self`.
287288
@warn_unused_result
288289
func nthWord(_ n: Word) -> UWord
289290

@@ -306,12 +307,24 @@ public protocol Integer : ${IntegerBase} {
306307
// implementation hook
307308
mutating func ${x.mutatingName}(${x.firstArg} rhs: Self)
308309
% end
310+
311+
/// An extension point to provide an efficient implementation of `divRem`
312+
/// operation, producing a pair of quotient and remainder of division of
313+
/// `self` by `rhs`.
314+
/// Default implementation simply invokes `divided(by:)` and
315+
/// `remainder(dividingBy:)`, which in case of built-in types will be fused
316+
/// into a single instruction by the compiler.
317+
func quotientAndRemainder(dividingBy rhs: Self) -> (Self, Self)
309318
}
310319

311320
extension Integer {
312321
public var countRepresentedWords: Word {
313322
return (self.bitWidth + ${word_bits} - 1) / ${word_bits}
314323
}
324+
325+
public func quotientAndRemainder(dividingBy rhs: Self) -> (Self, Self) {
326+
return (self.divided(by: rhs), self.remainder(dividingBy: rhs))
327+
}
315328
}
316329

317330
//===--- Homogeneous comparison -------------------------------------------===//

0 commit comments

Comments
 (0)