Skip to content

Commit

Permalink
[compiler-rt] Add missing carry to 128x128->256 wide multiply
Browse files Browse the repository at this point in the history
  • Loading branch information
beetrees committed Jul 1, 2024
1 parent 8598bcb commit f33d937
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion compiler-rt/lib/builtins/fp_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,11 @@ static __inline void wideMultiply(rep_t a, rep_t b, rep_t *hi, rep_t *lo) {
(sum2 & Word_FullMask) + ((sum3 << 32) & Word_HiMask);

*lo = r0 + (r1 << 64);
// The addition above can overflow, in which case `*lo` will be less than
// `r0`. Carry any overflow into `hi`.
const bool carry = *lo < r0;
*hi = (r1 >> 64) + (sum1 >> 96) + (sum2 >> 64) + (sum3 >> 32) + sum4 +
(sum5 << 32) + (sum6 << 64);
(sum5 << 32) + (sum6 << 64) + carry;
}
#undef Word_1
#undef Word_2
Expand Down
6 changes: 6 additions & 0 deletions compiler-rt/test/builtins/Unit/multf3_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ int main()
UINT64_C(0x0),
UINT64_C(0x0)))
return 1;
// test carry between lo and hi in widening multiply
if (test__multf3(0x0.7fffffffffffffffffffffffffffp-16382L,
0x1.7fffffffffffffffffffffffffffp+1L,
UINT64_C(0x00017fffffffffff),
UINT64_C(0xfffffffffffffffc)))
return 1;

#else
printf("skipped\n");
Expand Down

0 comments on commit f33d937

Please sign in to comment.