Path: blob/main/lib/libc/stdbit/stdc_trailing_zeros.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/* Ensure we do not shift 1U out of range. */11static_assert(UCHAR_WIDTH < UINT_WIDTH,12"stdc_trailing_zeros_uc needs UCHAR_WIDTH < UINT_WIDTH");1314unsigned int15stdc_trailing_zeros_uc(unsigned char x)16{17return (__builtin_ctz(x | 1U << UCHAR_WIDTH));18}1920/* Ensure we do not shift 1U out of range. */21static_assert(USHRT_WIDTH < UINT_WIDTH,22"stdc_trailing_zeros_uc needs USHRT_WIDTH < UINT_WIDTH");2324unsigned int25stdc_trailing_zeros_us(unsigned short x)26{27return (__builtin_ctz(x | 1U << USHRT_WIDTH));28}2930unsigned int31stdc_trailing_zeros_ui(unsigned int x)32{33if (x == 0U)34return (UINT_WIDTH);3536return (__builtin_ctz(x));37}3839unsigned int40stdc_trailing_zeros_ul(unsigned long x)41{42if (x == 0UL)43return (ULONG_WIDTH);4445return (__builtin_ctzl(x));46}4748unsigned int49stdc_trailing_zeros_ull(unsigned long long x)50{51if (x == 0ULL)52return (ULLONG_WIDTH);5354return (__builtin_ctzll(x));55}565758