xref: /aosp_15_r20/external/llvm-libc/test/include/stdbit_test.c (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===-- Unittests for stdbit ----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDSList-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 /*
10  * The intent of this test is validate that:
11  * 1. We provide the definition of the various type generic macros of stdbit.h
12  * (the macros are transitively included from stdbit-macros.h by stdbit.h).
13  * 2. It dispatches to the correct underlying function.
14  * Because unit tests build without public packaging, the object files produced
15  * do not contain non-namespaced symbols.
16  */
17 
18 /*
19  * Declare these BEFORE including stdbit-macros.h so that this test may still be
20  * run even if a given target doesn't yet have these individual entrypoints
21  * enabled.
22  */
23 #include "stdbit_stub.h"
24 
25 #include "include/llvm-libc-macros/stdbit-macros.h"
26 
27 #include <assert.h>
28 
29 #define CHECK_FUNCTION(FUNC_NAME, VAL)                                         \
30   do {                                                                         \
31     assert(FUNC_NAME((unsigned char)0U) == VAL##AU);                           \
32     assert(FUNC_NAME((unsigned short)0U) == VAL##BU);                          \
33     assert(FUNC_NAME(0U) == VAL##CU);                                          \
34     assert(FUNC_NAME(0UL) == VAL##DU);                                         \
35     assert(FUNC_NAME(0ULL) == VAL##EU);                                        \
36   } while (0)
37 
main(void)38 int main(void) {
39   CHECK_FUNCTION(stdc_leading_zeros, 0xA);
40   CHECK_FUNCTION(stdc_leading_ones, 0xB);
41   CHECK_FUNCTION(stdc_trailing_zeros, 0xC);
42   CHECK_FUNCTION(stdc_trailing_ones, 0xD);
43   CHECK_FUNCTION(stdc_first_leading_zero, 0xE);
44   CHECK_FUNCTION(stdc_first_leading_one, 0xF);
45   CHECK_FUNCTION(stdc_first_trailing_zero, 0x0);
46   CHECK_FUNCTION(stdc_first_trailing_one, 0x1);
47   CHECK_FUNCTION(stdc_count_zeros, 0x2);
48   CHECK_FUNCTION(stdc_count_ones, 0x3);
49 
50   assert(!stdc_has_single_bit((unsigned char)1U));
51   assert(!stdc_has_single_bit((unsigned short)1U));
52   assert(!stdc_has_single_bit(1U));
53   assert(!stdc_has_single_bit(1UL));
54   assert(!stdc_has_single_bit(1ULL));
55 
56   CHECK_FUNCTION(stdc_bit_width, 0x4);
57   CHECK_FUNCTION(stdc_bit_floor, 0x5);
58   CHECK_FUNCTION(stdc_bit_ceil, 0x6);
59 
60   return 0;
61 }
62