Path: blob/main/lib/libc/stdbit/stdc_trailing_ones.c
96290 views
/*1* Copyright (c) 2025 Robert Clausecker <[email protected]>2*3* SPDX-License-Identifier: BSD-2-Clause4*/56#include <assert.h>7#include <limits.h>8#include <stdbit.h>910/* Avoid triggering undefined behavior if x == ~0. */11static_assert(UCHAR_WIDTH < UINT_WIDTH,12"stdc_trailing_ones_uc needs UCHAR_WIDTH < UINT_WIDTH");1314unsigned int15stdc_trailing_ones_uc(unsigned char x)16{17return (__builtin_ctz(~x));18}1920/* Avoid triggering undefined behavior if x == ~0. */21static_assert(USHRT_WIDTH < UINT_WIDTH,22"stdc_trailing_ones_uc needs USHRT_WIDTH < UINT_WIDTH");2324unsigned int25stdc_trailing_ones_us(unsigned short x)26{27return (__builtin_ctz(~x));28}2930unsigned int31stdc_trailing_ones_ui(unsigned int x)32{33if (x == ~0U)34return (UINT_WIDTH);3536return (__builtin_ctz(~x));37}3839unsigned int40stdc_trailing_ones_ul(unsigned long x)41{42if (x == ~0UL)43return (ULONG_WIDTH);4445return (__builtin_ctzl(~x));46}4748unsigned int49stdc_trailing_ones_ull(unsigned long long x)50{51if (x == ~0ULL)52return (ULLONG_WIDTH);5354return (__builtin_ctzll(~x));55}565758