1 /*
2  * Copyright 2023 Meta
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/sys/util.h>
8 #include <zephyr/ztest.h>
9 
10 static constexpr uint8_t val = LOG2CEIL(32);
11 static constexpr uint8_t val64 = LOG2CEIL(42 + BIT64(32));
12 
log2ceil(T x)13 template <typename T> static inline constexpr uint8_t log2ceil(T x)
14 {
15 	return LOG2CEIL(x);
16 }
17 
ZTEST(pot,test_constexpr_LOG2CEIL)18 ZTEST(pot, test_constexpr_LOG2CEIL)
19 {
20 	zassert_equal(0, LOG2CEIL(LLONG_MIN));
21 	zassert_equal(0, LOG2CEIL(LONG_MIN));
22 	zassert_equal(0, LOG2CEIL(INT_MIN));
23 	zassert_equal(0, LOG2CEIL(-1));
24 	zassert_equal(0, LOG2CEIL(0));
25 	zassert_equal(0, LOG2CEIL(1));
26 	zassert_equal(1, LOG2CEIL(2));
27 	zassert_equal(2, LOG2CEIL(3));
28 	zassert_equal(2, LOG2CEIL(4));
29 	zassert_equal(3, LOG2CEIL(5));
30 	zassert_equal(31, LOG2CEIL(BIT(31)));
31 	zassert_equal(32, LOG2CEIL(BIT(31) + 1));
32 	zassert_equal(32, LOG2CEIL(UINT32_MAX));
33 	zassert_equal(32, LOG2CEIL(BIT64(32)));
34 	zassert_equal(33, LOG2CEIL(BIT64(32) + 1));
35 	zassert_equal(63, LOG2CEIL(BIT64(63) - 1));
36 	zassert_equal(63, LOG2CEIL(BIT64(63)));
37 	zassert_equal(64, LOG2CEIL(BIT64(63) + 1));
38 	zassert_equal(64, LOG2CEIL(UINT64_MAX));
39 
40 	zassert_equal(5, val);
41 	zassert_equal(5, log2ceil(32));
42 	zassert_equal(33, val64);
43 	zassert_equal(33, log2ceil(42 + BIT64(32)));
44 }
45