From 5bb8ead0a7115d7f19f92f02145d8d39705b0296 Mon Sep 17 00:00:00 2001 From: Xiaodi Wu <13952+xwu@users.noreply.github.com> Date: Thu, 26 Oct 2023 16:25:11 -0400 Subject: [PATCH 1/2] [test/Prototypes] Fix DoubleWidth think-o that trips an assert --- test/Prototypes/DoubleWidth.swift.gyb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/Prototypes/DoubleWidth.swift.gyb b/test/Prototypes/DoubleWidth.swift.gyb index 7b6cf0dfdcfe7..e08f440c549e8 100644 --- a/test/Prototypes/DoubleWidth.swift.gyb +++ b/test/Prototypes/DoubleWidth.swift.gyb @@ -702,8 +702,9 @@ extension DoubleWidth : UnsignedInteger where Base : UnsignedInteger { let rhs = rhs &<< shift let high = (lhs &>> (Magnitude.bitWidth &- shift)).low let lhs = lhs &<< shift - let (quotient, remainder) = - Magnitude._divide((high, lhs.high, lhs.low), by: rhs) + let (quotient, remainder) = high == (0 as Low) + ? (1, lhs &- rhs) + : Magnitude._divide((high, lhs.high, lhs.low), by: rhs) return (Magnitude(0, quotient), remainder &>> shift) } } @@ -805,6 +806,12 @@ dwTests.test("Arithmetic/unsigned") { expectEqual(x % 3, 1) expectEqual(x % y, x) + + do { + let lhs = DoubleWidth((high: 0b0011_0000, low: 0)) + let rhs = DoubleWidth((high: 0b0010_0000, low: 0)) + expectEqual(lhs % rhs, 4096) + } } dwTests.test("Arithmetic/signed") { From c6eba6b1615ad441afcb6ac7ddecf2105bb41160 Mon Sep 17 00:00:00 2001 From: Xiaodi Wu <13952+xwu@users.noreply.github.com> Date: Mon, 30 Oct 2023 16:25:52 -0400 Subject: [PATCH 2/2] [test/Prototypes] Fix second DoubleWidth think-o that trips an assert --- test/Prototypes/DoubleWidth.swift.gyb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/Prototypes/DoubleWidth.swift.gyb b/test/Prototypes/DoubleWidth.swift.gyb index e08f440c549e8..4da689872e68c 100644 --- a/test/Prototypes/DoubleWidth.swift.gyb +++ b/test/Prototypes/DoubleWidth.swift.gyb @@ -699,8 +699,8 @@ extension DoubleWidth : UnsignedInteger where Base : UnsignedInteger { // Left shift both rhs and lhs, then divide and right shift the remainder. let shift = rhs.leadingZeroBitCount + let high = (lhs >> (Magnitude.bitWidth &- shift)).low let rhs = rhs &<< shift - let high = (lhs &>> (Magnitude.bitWidth &- shift)).low let lhs = lhs &<< shift let (quotient, remainder) = high == (0 as Low) ? (1, lhs &- rhs) @@ -812,6 +812,16 @@ dwTests.test("Arithmetic/unsigned") { let rhs = DoubleWidth((high: 0b0010_0000, low: 0)) expectEqual(lhs % rhs, 4096) } + do { + let lhs = DoubleWidth((high: 0xa0c7d7165cf01386, low: 0xbf3f66a93056143f)) + let rhs = DoubleWidth((high: 0x9ac3a19b1e7d6b83, low: 0x513929792d588736)) + expectEqual(String(lhs % rhs), "7997221894243298914179865336050715913") + } + do { + let lhs = DoubleWidth((high: 0xea8a9116b7af33b7, low: 0x3d9d6779ddd22ca3)) + let rhs = DoubleWidth((high: 0xc3673efc7f1f37cc, low: 0x312f661057d0ba94)) + expectEqual(String(lhs % rhs), "52023287460685389410162512181093036559") + } } dwTests.test("Arithmetic/signed") {