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 = LOG2(42);
11 static constexpr uint8_t val64 = LOG2(42 + BIT64(32));
12 
log2(T x)13 template <typename T> static inline constexpr uint8_t log2(T x)
14 {
15 	return LOG2(x);
16 }
17 
ZTEST(pot,test_constexpr_LOG2)18 ZTEST(pot, test_constexpr_LOG2)
19 {
20 	zassert_equal(-1, LOG2(0));
21 	zassert_equal(0, LOG2(1));
22 	zassert_equal(1, LOG2(2));
23 	zassert_equal(1, LOG2(3));
24 	zassert_equal(2, LOG2(4));
25 	zassert_equal(2, LOG2(5));
26 	zassert_equal(31, LOG2(BIT(31)));
27 	zassert_equal(31, LOG2(BIT(31) + 1));
28 	zassert_equal(31, LOG2(UINT32_MAX));
29 	zassert_equal(32, LOG2(BIT64(32)));
30 	zassert_equal(62, LOG2(BIT64(63) - 1));
31 	zassert_equal(63, LOG2(BIT64(63)));
32 	zassert_equal(63, LOG2(BIT64(63) + 1));
33 	zassert_equal(63, LOG2(UINT64_MAX));
34 
35 	zassert_equal(5, val);
36 	zassert_equal(5, log2(42));
37 	zassert_equal(32, val64);
38 	zassert_equal(32, log2(42 + BIT64(32)));
39 }
40