xref: /freebsd/lib/libc/tests/stdbit/stdc_first_leading_zero_test.c (revision 2fb8cbc6ef1b3cc6cd60e5db07f8305623f9b044)
1 /*
2  * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #define FUNCSTEM stdc_first_leading_zero
8 #define MKREFFUNC(name, type)						\
9 	static unsigned							\
10 	name(type value) 						\
11 	{ 								\
12 		type bit = 1;						\
13 		unsigned pos = 1;					\
14 									\
15 		value = ~value;						\
16 		if (value == 0)						\
17 			return (0);					\
18 									\
19 		while ((type)(bit << 1) != 0)				\
20 			bit <<= 1;					\
21 									\
22 		while ((bit & value) == 0) {				\
23 			bit >>= 1;					\
24 			pos++;						\
25 		}							\
26 									\
27 		return (pos);						\
28 	}
29 
30 #include "stdbit-test-framework.c"
31