Files
freebsd/lib/libc/stdbit/stdc_first_trailing_one.c
Robert Clausecker 6296500a85 libc: implement C23 <stdbit.h> functions
This new header complies with ISO/IEC 9899:2024 (C23).

Contrary to glibc, we do not provide inline definitions in
<stdbit.h> as we expect our system compiler to soon recognise
these as builtins anyway.

Relnotes:	yes
MFC after:	1 month
Reviewed by:	adrian
Approved by:	markj (mentor)
Differential Revision:	https://reviews.freebsd.org/D53657
2025-11-30 20:21:52 +01:00

53 lines
749 B
C

/*
* Copyright (c) 2025 Robert Clausecker <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <stdbit.h>
unsigned int
stdc_first_trailing_one_uc(unsigned char x)
{
if (x == 0)
return (0);
return (__builtin_ctz(x) + 1);
}
unsigned int
stdc_first_trailing_one_us(unsigned short x)
{
if (x == 0)
return (0);
return (__builtin_ctz(x) + 1);
}
unsigned int
stdc_first_trailing_one_ui(unsigned int x)
{
if (x == 0)
return (0);
return (__builtin_ctz(x) + 1);
}
unsigned int
stdc_first_trailing_one_ul(unsigned long x)
{
if (x == 0)
return (0);
return (__builtin_ctzl(x) + 1);
}
unsigned int
stdc_first_trailing_one_ull(unsigned long long x)
{
if (x == 0)
return (0);
return (__builtin_ctzll(x) + 1);
}