Skip to content

Commit 2495130

Browse files
authored
[compiler-rt] Add missing carry to 128x128->256 wide multiply (#97257)
1 parent e33e087 commit 2495130

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

compiler-rt/lib/builtins/fp_lib.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
171171
(sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);
172172

173173
*lo = r0 + (r1 << 64);
174+
// The addition above can overflow, in which case `*lo` will be less than
175+
// `r0`. Carry any overflow into `hi`.
176+
const bool carry = *lo < r0;
174177
*hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
175-
(sum5 << 32) + (sum6 << 64);
178+
(sum5 << 32) + (sum6 << 64) + carry;
176179
}
177180
#undef Word_1
178181
#undef Word_2

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

+6
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)