xref: /freebsd/lib/libc/tests/stdbit/stdbit-test-kernel.c (revision 2fb8cbc6ef1b3cc6cd60e5db07f8305623f9b044)
1 /*
2  * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 /*
8  * test kernel for stdbit functions.
9  * Requires the following macros to be defined:
10  *
11  * FUNCSTEM -- stem of the function name
12  * SUFFIX -- type suffic
13  * TYPE -- argument type
14  * MKREFFUNC(ref, type) -- reference function builder
15  */
16 
17 #define FUNC __CONCAT(FUNCSTEM, SUFFIX)
18 #define REF __CONCAT(FUNCSTEM, __CONCAT(SUFFIX, _ref))
19 
20 MKREFFUNC(REF, TYPE)
21 
22 ATF_TC_WITHOUT_HEAD1(FUNCSTEM, SUFFIX);
ATF_TC_BODY1(FUNCSTEM,SUFFIX,tc)23 ATF_TC_BODY1(FUNCSTEM, SUFFIX, tc)
24 {
25 	uintmax_t has, want;
26 	size_t i, j;
27 	TYPE value;
28 
29 	/* test all single-bit patterns */
30 	for (i = 0; i < TYPE_WIDTH; i++) {
31 		value = (TYPE)1 << i;
32 		has = FUNC(value);
33 		want = REF(value);
34 		ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
35 		    __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
36 	}
37 
38 	/* test all double-bit patterns */
39 	for (i = 0; i < TYPE_WIDTH; i++) {
40 		for (j = 0; j < i; j++) {
41 			value = (TYPE)1 << i | (TYPE)1 << j;
42 			has = FUNC(value);
43 			want = REF(value);
44 			ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
45 			    __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
46 		}
47 	}
48 
49 	/* test all barber-pole patterns */
50 	value = ~(TYPE)0;
51 	for (i = 0; i < TYPE_WIDTH; i++) {
52 		has = FUNC(value);
53 		want = REF(value);
54 		ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
55 		    __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
56 
57 		value = ~value;
58 		has = FUNC(value);
59 		want = REF(value);
60 		ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
61 		    __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
62 
63 		value = ~value << 1;
64 	}
65 }
66 
67 #undef REF
68 #undef FUNC
69