From: Alejandro Colomar <alx.manpages@gmail.com>
To: Yann Droneaud <ydroneaud@opteya.com>,
Joseph Myers <joseph@codesourcery.com>
Cc: gcc@gcc.gnu.org, GNU C Library <libc-alpha@sourceware.org>
Subject: Re: stdc_bit_ceil(3) and wrapping
Date: Fri, 6 Jan 2023 21:08:39 +0100 [thread overview]
Message-ID: <0acb59af-b5fe-2aa4-e582-aa3aaad5866a@gmail.com> (raw)
In-Reply-To: <e07ba8ac-323e-3354-aec6-0598bb2a5aed@gmail.com>
[-- Attachment #1.1: Type: text/plain, Size: 1699 bytes --]
On 12/30/22 23:33, Alejandro Colomar wrote:
> Hi Yann,
>
> On 12/30/22 21:33, Alejandro Colomar wrote:
>> On 12/30/22 21:18, Yann Droneaud wrote:
>>> What's wrong with the following ?
>
> [...]
>
>>
>>>
>>> unsigned long max = upper_bound - 1;
>>> unsigned long mask = ULONG_MAX >> __builtin_clzl(max);
>>
>> I hate coding these magic operations out of a function, when I can give it a
>> meaningful name. That reads to me as a magic trick that many maintainers that
>> read it after me will blame me for having to parse it.
>>
>> Moreover, it requires you to have the special case for 0 at the top, which I
>> don't want.
>
> I reconsidered; my -1 was equally magic. And by calling it 'mask',
> ULONG_MAX >> n is something not so magic.
>
> The builtin still has the problem that it requires special-casing 0, so I prefer
> the C23 call, which provides the behavior I want for 0:
>
>
> unsigned long
> shadow_random_uniform(unsigned long upper_bound)
> {
> unsigned long r, max, mask;
>
> max = upper_bound - 1;
> mask = ULONG_MAX >> leading_zerosul(max);
I just realized that this isn't good either. Shifting right still has undefined
behavior for max = 0. I'll fall back to my original plan.
>
> do {
> r = shadow_random();
> r &= mask; // optimization
> } while (r > max);
>
> return r;
> }
>
>
> And, now I don't need to add a wrapper around bit_ceil() that removes the UB.
> stdc_leading_zerosul() is just fine for this use case.
>
> Cheers,
>
> Alex
>
--
<http://www.alejandro-colomar.es/>
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
next prev parent reply other threads:[~2023-01-06 20:08 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 [this message]
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
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=0acb59af-b5fe-2aa4-e582-aa3aaad5866a@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).