Skip to content

Commit 192e368

Browse files
committed
[compiler-rt] Add missing carry to 128x128->256 wide multiply
1 parent 8598bcb commit 192e368

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

compiler-rt/lib/builtins/fp_lib.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,11 @@ static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
180180
(sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
181181

182182
*lo = r0 + (r1 << 64);
183+
// The addition above can overflow, in which case `*lo` will be less than
184+
// `r0`. Carry any overflow into `hi`.
185+
const bool carry = *lo < r0;
183186
*hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
184-
(sum5 << 32) + (sum6 << 64);
187+
(sum5 << 32) + (sum6 << 64) + carry;
185188
}
186189
#undef Word_1
187190
#undef Word_2

compiler-rt/test/builtins/Unit/multf3_test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ int main()
7777
UINT64_C(0x0),
7878
UINT64_C(0x0)))
7979
return 1;
80+
// test carry between lo and hi in widening multiply
81+
if (test__multf3(0x0.7fffffffffffffffffffffffffffp-16382L,
82+
0x1.7fffffffffffffffffffffffffffp+1L,
83+
UINT64_C(0x00017fffffffffff),
84+
UINT64_C(0xfffffffffffffffc)))
85+
return 1;
8086

8187
#else
8288
printf("skipped\n");

0 commit comments

Comments
 (0)