public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Correct implementation of wi::clz
@ 2021-09-05 21:39 Roger Sayle
  2021-09-06 17:05 ` Jeff Law
  0 siblings, 1 reply; 2+ messages in thread
From: Roger Sayle @ 2021-09-05 21:39 UTC (permalink / raw)
  To: 'GCC Patches'

[-- Attachment #1: Type: text/plain, Size: 711 bytes --]


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 #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.

This patch has been tested on x86_64-pc-linux-gnu with "make bootstrap"
and "make -k check" with no new failures.

Ok for mainline?

2021-09-05  Roger Sayle  <roger@nextmovesoftware.com>

gcc/ChangeLog
	* gcc/wide-int.cc (wi::clz): Reorder tests to ensure the result
	is zero for all negative values.

Roger
--


[-- Attachment #2: patchz.txt --]
[-- Type: text/plain, Size: 954 bytes --]

diff --git a/gcc/wide-int.cc b/gcc/wide-int.cc
index 906f4ea..a142151 100644
--- a/gcc/wide-int.cc
+++ b/gcc/wide-int.cc
@@ -2050,6 +2050,10 @@ wi::arshift_large (HOST_WIDE_INT *val, const HOST_WIDE_INT *xval,
 int
 wi::clz (const wide_int_ref &x)
 {
+  if (x.sign_mask () < 0)
+    /* The upper bit is set, so there are no leading zeros.  */
+    return 0;
+
   /* Calculate how many bits there above the highest represented block.  */
   int count = x.precision - x.len * HOST_BITS_PER_WIDE_INT;
 
@@ -2058,9 +2062,6 @@ wi::clz (const wide_int_ref &x)
     /* The upper -COUNT bits of HIGH are not part of the value.
        Clear them out.  */
     high = (high << -count) >> -count;
-  else if (x.sign_mask () < 0)
-    /* The upper bit is set, so there are no leading zeros.  */
-    return 0;
 
   /* We don't need to look below HIGH.  Either HIGH is nonzero,
      or the top bit of the block below is nonzero; clz_hwi is

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Correct implementation of wi::clz
  2021-09-05 21:39 [PATCH] Correct implementation of wi::clz Roger Sayle
@ 2021-09-06 17:05 ` Jeff Law
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff Law @ 2021-09-06 17:05 UTC (permalink / raw)
  To: Roger Sayle, 'GCC Patches'



On 9/5/2021 3:39 PM, Roger Sayle wrote:
> 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 #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.
>
> This patch has been tested on x86_64-pc-linux-gnu with "make bootstrap"
> and "make -k check" with no new failures.
>
> Ok for mainline?
>
> 2021-09-05  Roger Sayle  <roger@nextmovesoftware.com>
>
> gcc/ChangeLog
> 	* gcc/wide-int.cc (wi::clz): Reorder tests to ensure the result
> 	is zero for all negative values.
OK
jeff


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-06 17:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-05 21:39 [PATCH] Correct implementation of wi::clz Roger Sayle
2021-09-06 17:05 ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).