On Sat, Mar 7, 2026 at 6:09 PM Collin Funk <[email protected]> wrote:

> Hi,
>
> While discussing Adhemerval's patch to use Gnulib's fts in glibc, there
> was a desire to avoid importing bitrotate.h which C2y has equivalent
> functionality for.
>
> I've attached patches adding the stdc_rotate_{left,right} macros and
> functions, but have not pushed them yet. It is probably worth noting
> that these functions are required to work when the shift is greater than
> the width of the word. This is not the case with the ones from
> bitrotate.h.
>
> Tested with GCC 4.8.5 which does not have the builtins for these
> functions and GCC 15 & 16 which do.
>
> I only modified stdbit_h.m4 slightly. It assumes that if stdbit.h exists
> it supports C2y or the subset provided by C23. If it only supports C23
> functionality, it will be generated. That feels reasonable to me, but
> wanted another set of eyes to look over it.


You may want to put an assert in the rotates to ensure the rotate amount is
less than the bit size.  That is, the rotate amount is 0 <= n < 32 for
32-bit data types.  Otherwise, it is undefined behavior.  The ICC compiler
is ruthless about removing shifts and rotates with undefined behavior.

The rotates should compile down to a single instruction on x86_64 and
friends.  For reference, here are the various discussions with the compiler
makers:

  * http://gcc.gnu.org/bugzilla/show_bug.cgi?id=57157
  * http://llvm.org/bugs/show_bug.cgi?id=24226
  * http://software.intel.com/en-us/forums/topic/580884

Jeff

Reply via email to