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