Skip to content

Commit 74cb45e

Browse files
committed
Correct implementation of wi::clz
As diagnosed with Jakub and Richard in the analysis of PR 102134, the current implementation of wi::clz has incorrect/inconsistent behaviour. As mentioned by Richard in comment gcc-mirror#7, clz should (always) return zero for negative values, but the current implementation can only return 0 when precision is a multiple of HOST_BITS_PER_WIDE_INT. The fix is simply to reorder/shuffle the existing tests. 2021-09-06 Roger Sayle <[email protected]> gcc/ChangeLog * wide-int.cc (wi::clz): Reorder tests to ensure the result is zero for all negative values.
1 parent 1bc6601 commit 74cb45e

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

gcc/wide-int.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,10 @@ wi::arshift_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
20502050
int
20512051
wi::clz (const wide_int_ref &x)
20522052
{
2053+
if (x.sign_mask () < 0)
2054+
/* The upper bit is set, so there are no leading zeros. */
2055+
return 0;
2056+
20532057
/* Calculate how many bits there above the highest represented block. */
20542058
int count = x.precision - x.len * HOST_BITS_PER_WIDE_INT;
20552059

@@ -2058,9 +2062,6 @@ wi::clz (const wide_int_ref &x)
20582062
/* The upper -COUNT bits of HIGH are not part of the value.
20592063
Clear them out. */
20602064
high = (high << -count) >> -count;
2061-
else if (x.sign_mask () < 0)
2062-
/* The upper bit is set, so there are no leading zeros. */
2063-
return 0;
20642065

20652066
/* We don't need to look below HIGH. Either HIGH is nonzero,
20662067
or the top bit of the block below is nonzero; clz_hwi is

0 commit comments

Comments
 (0)