@@ -699,11 +699,12 @@ extension DoubleWidth : UnsignedInteger where Base : UnsignedInteger {
699
699
700
700
// Left shift both rhs and lhs, then divide and right shift the remainder.
701
701
let shift = rhs.leadingZeroBitCount
702
+ let high = (lhs >> (Magnitude.bitWidth &- shift)).low
702
703
let rhs = rhs &<< shift
703
- let high = (lhs &>> (Magnitude.bitWidth &- shift)).low
704
704
let lhs = lhs &<< shift
705
- let (quotient, remainder) =
706
- Magnitude._divide((high, lhs.high, lhs.low), by: rhs)
705
+ let (quotient, remainder) = high == (0 as Low)
706
+ ? (1, lhs &- rhs)
707
+ : Magnitude._divide((high, lhs.high, lhs.low), by: rhs)
707
708
return (Magnitude(0, quotient), remainder &>> shift)
708
709
}
709
710
}
@@ -805,6 +806,22 @@ dwTests.test("Arithmetic/unsigned") {
805
806
806
807
expectEqual(x % 3, 1)
807
808
expectEqual(x % y, x)
809
+
810
+ do {
811
+ let lhs = DoubleWidth<UInt8>((high: 0b0011_0000, low: 0))
812
+ let rhs = DoubleWidth<UInt8>((high: 0b0010_0000, low: 0))
813
+ expectEqual(lhs % rhs, 4096)
814
+ }
815
+ do {
816
+ let lhs = DoubleWidth<UInt64>((high: 0xa0c7d7165cf01386, low: 0xbf3f66a93056143f))
817
+ let rhs = DoubleWidth<UInt64>((high: 0x9ac3a19b1e7d6b83, low: 0x513929792d588736))
818
+ expectEqual(String(lhs % rhs), "7997221894243298914179865336050715913")
819
+ }
820
+ do {
821
+ let lhs = DoubleWidth<UInt64>((high: 0xea8a9116b7af33b7, low: 0x3d9d6779ddd22ca3))
822
+ let rhs = DoubleWidth<UInt64>((high: 0xc3673efc7f1f37cc, low: 0x312f661057d0ba94))
823
+ expectEqual(String(lhs % rhs), "52023287460685389410162512181093036559")
824
+ }
808
825
}
809
826
810
827
dwTests.test("Arithmetic/signed") {
0 commit comments