public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alejandro Colomar <alx.manpages@gmail.com>
To: Joseph Myers <joseph@codesourcery.com>
Cc: gcc@gcc.gnu.org, GNU C Library <libc-alpha@sourceware.org>,
	Yann Droneaud <ydroneaud@opteya.com>
Subject: Re: stdc_bit_ceil(3) and wrapping
Date: Fri, 30 Dec 2022 22:06:29 +0100	[thread overview]
Message-ID: <06c89f9d-e589-cafc-67e6-07f0a304416b@gmail.com> (raw)
In-Reply-To: <d6b60e0e-dcb3-c18a-d3fb-605d10522eb7@gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 1290 bytes --]

On 12/30/22 22:01, Alejandro Colomar wrote:
> On 12/30/22 21:56, Joseph Myers wrote:
>> On Fri, 30 Dec 2022, Alejandro Colomar via Gcc wrote:
>>
>>> For the C standard, shifts have wrap around semantics for unsigned types:
>>
>> Only if the shift count is nonnegative and strictly less than the width of
>> the type.  This is about shifting by an amount equal to the width of the
>> type, which has undefined behavior.
> 
> Ahhh, right, I forgot that detail.  Yep, I need to special-case then.
> 
> Thanks!
> 
> 
> 

This should work, I guess (and hopefully, the compiler will remove the special 
case in platforms that don't need it):


$ cat lib/bit.h
#include <limits.h>


inline int leading_zerosul(unsigned long x);
inline int bit_widthul(unsigned long x);
inline int bit_ceil_wrapul(unsigned long x);


inline int
leading_zerosul(unsigned long x)
{
	return (x == 0) ? ULONG_WIDTH : __builtin_clz(x);
}


inline int
bit_widthul(unsigned long x)
{
	return ULONG_WIDTH - leading_zerosul(x);
}


/* Similar to stdc_bit_ceilul(), but wrap around instead of UB.  */
inline int
bit_ceil_wrapul(unsigned long x)
{
	int b;

	b = bit_widthul(x - 1);

	return b < ULONG_WIDTH ? 1ul << b : 0;
}


-- 
<http://www.alejandro-colomar.es/>

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

      reply	other threads:[~2022-12-30 21:06 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-30 19:53 Alejandro Colomar
2022-12-30 20:18 ` Yann Droneaud
2022-12-30 20:33   ` Alejandro Colomar
2022-12-30 22:33     ` Alejandro Colomar
2023-01-06 20:08       ` Alejandro Colomar
2022-12-30 20:47   ` Alejandro Colomar
2022-12-30 20:38 ` Joseph Myers
2022-12-30 20:46   ` Alejandro Colomar
2022-12-30 20:56     ` Joseph Myers
2022-12-30 21:01       ` Alejandro Colomar
2022-12-30 21:06         ` Alejandro Colomar [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=06c89f9d-e589-cafc-67e6-07f0a304416b@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=joseph@codesourcery.com \
    --cc=libc-alpha@sourceware.org \
    --cc=ydroneaud@opteya.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).