Skip to content

Commit a0d66e1

Browse files
xwuXiaodi Wu
authored andcommitted
[_TestSupport] Fix DoubleWidth think-os
1 parent 182c9b2 commit a0d66e1

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Sources/_TestSupport/DoubleWidth.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -735,11 +735,12 @@ extension DoubleWidth : UnsignedInteger where Base : UnsignedInteger {
735735

736736
// Left shift both rhs and lhs, then divide and right shift the remainder.
737737
let shift = rhs.leadingZeroBitCount
738+
let high = (lhs >> (Magnitude.bitWidth &- shift)).low
738739
let rhs = rhs &<< shift
739-
let high = (lhs &>> (Magnitude.bitWidth &- shift)).low
740740
let lhs = lhs &<< shift
741-
let (quotient, remainder) =
742-
Magnitude._divide((high, lhs.high, lhs.low), by: rhs)
741+
let (quotient, remainder) = high == (0 as Low)
742+
? (1, lhs &- rhs)
743+
: Magnitude._divide((high, lhs.high, lhs.low), by: rhs)
743744
return (Magnitude(0, quotient), remainder &>> shift)
744745
}
745746
}

Tests/IntegerUtilitiesTests/DoubleWidthTests.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,22 @@ final class DoubleWidthTests: XCTestCase {
129129

130130
XCTAssertEqual(x % 3, 1)
131131
XCTAssertEqual(x % y, x)
132+
133+
do {
134+
let lhs = _UInt16((high: 0b0011_0000, low: 0))
135+
let rhs = _UInt16((high: 0b0010_0000, low: 0))
136+
XCTAssertEqual(lhs % rhs, 4096)
137+
}
138+
do {
139+
let lhs = _UInt128((high: 0xa0c7d7165cf01386, low: 0xbf3f66a93056143f))
140+
let rhs = _UInt128((high: 0x9ac3a19b1e7d6b83, low: 0x513929792d588736))
141+
XCTAssertEqual(String(lhs % rhs), "7997221894243298914179865336050715913")
142+
}
143+
do {
144+
let lhs = _UInt128((high: 0xea8a9116b7af33b7, low: 0x3d9d6779ddd22ca3))
145+
let rhs = _UInt128((high: 0xc3673efc7f1f37cc, low: 0x312f661057d0ba94))
146+
XCTAssertEqual(String(lhs % rhs), "52023287460685389410162512181093036559")
147+
}
132148
}
133149

134150
func testArithmetic_Signed() {

0 commit comments

Comments
 (0)