glibc/bits
Joseph Myers b34b46b880 Implement C23 <stdbit.h>
C23 adds a header <stdbit.h> with various functions and type-generic
macros for bit-manipulation of unsigned integers (plus macro defines
related to endianness).  Implement this header for glibc.

The functions have both inline definitions in the header (referenced
by macros defined in the header) and copies with external linkage in
the library (which are implemented in terms of those macros to avoid
duplication).  They are documented in the glibc manual.  Tests, as
well as verifying results for various inputs (of both the macros and
the out-of-line functions), verify the types of those results (which
showed up a bug in an earlier version with the type-generic macro
stdc_has_single_bit wrongly returning a promoted type), that the
macros can be used at top level in a source file (so don't use ({})),
that they evaluate their arguments exactly once, and that the macros
for the type-specific functions have the expected implicit conversions
to the relevant argument type.

Jakub previously referred to -Wconversion warnings in type-generic
macros, so I've included a test with -Wconversion (but the only
warnings I saw and fixed from that test were actually in inline
functions in the <stdbit.h> header - not anything coming from use of
the type-generic macros themselves).

This implementation of the type-generic macros does not handle
unsigned __int128, or unsigned _BitInt types with a width other than
that of a standard integer type (and C23 doesn't require the header to
handle such types either).  Support for those types, using the new
type-generic built-in functions Jakub's added for GCC 14, can
reasonably be added in a followup (along of course with associated
tests).

This implementation doesn't do anything special to handle C++, or have
any tests of functionality in C++ beyond the existing tests that all
headers can be compiled in C++ code; it's not clear exactly what form
this header should take in C++, but probably not one using macros.

DIS ballot comment AT-107 asks for the word "count" to be added to the
names of the stdc_leading_zeros, stdc_leading_ones,
stdc_trailing_zeros and stdc_trailing_ones functions and macros.  I
don't think it's likely to be accepted (accepting any technical
comments would mean having an FDIS ballot), but if it is accepted at
the WG14 meeting (22-26 January in Strasbourg, starting with DIS
ballot comment handling) then there would still be time to update
glibc for the renaming before the 2.39 release.

The new functions and header are placed in the stdlib/ directory in
glibc, rather than creating a new toplevel stdbit/ or putting them in
string/ alongside ffs.

Tested for x86_64 and x86.
2024-01-03 12:07:14 +00:00
..
types Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
atomic_wide_counter.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
byteswap.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
confname.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dirent_ext.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dirent.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dl_find_object.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
dlfcn.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
elfclass.h Update. 1999-07-27 05:53:25 +00:00
endian.h
environments.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
errno.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
fcntl.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
fenv.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
floatn-common.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
floatn.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
flt-eval-method.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
fp-fast.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
fp-logb.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
hwcap.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
in.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
indirect-return.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
ioctl-types.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
ioctls.h
ipc.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
ipctypes.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
iscanonical.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
libc-header-start.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
libm-simd-decl-stubs.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
link_lavcurrent.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
link.h * csu/elf-init.c (__libc_csu_fini): Don't do anything here. 2005-01-06 22:40:27 +00:00
local_lim.h
long-double.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
math-vector.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
mathdef.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
mman_ext.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
mman.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
mqueue.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
msq.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
netdb.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
param.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
poll.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
posix_opt.h
pthreadtypes.h 2002-11-10 Roland McGrath <roland@redhat.com> 2002-11-10 23:22:35 +00:00
resource.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sched.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
select.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sem.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
setjmp.h
shm.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigaction.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigcontext.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigevent-consts.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
siginfo-consts.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
signal_ext.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
signum-arch.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
signum-generic.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigstack.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigstksz.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sigthread.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sockaddr.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
socket.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
spawn_ext.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
ss_flags.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
stat.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
statfs.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
statvfs.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
stdint-intn.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
stdint-least.h Implement C23 <stdbit.h> 2024-01-03 12:07:14 +00:00
stdint-uintn.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
stdlib-bsearch.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
syslog-path.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
sysmacros.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
termios.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
time64.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
time.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
timesize.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
typesizes.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
uintn-identity.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
uio_lim.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
uio-ext.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
unistd_ext.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
utmp.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
utsname.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
waitflags.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
waitstatus.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
wchar.h Update copyright dates with scripts/update-copyrights 2024-01-01 10:53:40 -08:00
wordsize.h Define wordsize.h macros everywhere 2016-11-04 09:37:44 -07:00