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 uint32_t val = NHPOT(42);
11 static constexpr uint64_t val64 = NHPOT(42 + BIT64(32));
12
nhpot(T x)13 template <typename T> static inline constexpr uint64_t nhpot(T x)
14 {
15 return NHPOT(x);
16 }
17
ZTEST(pot,test_constexpr_NHPOT)18 ZTEST(pot, test_constexpr_NHPOT)
19 {
20 zassert_equal(1, NHPOT(LLONG_MIN));
21 zassert_equal(1, NHPOT(LONG_MIN));
22 zassert_equal(1, NHPOT(INT_MIN));
23 zassert_equal(1, NHPOT(-1));
24 zassert_equal(1, NHPOT(0));
25 zassert_equal(1, NHPOT(1));
26 zassert_equal(2, NHPOT(2));
27 zassert_equal(4, NHPOT(3));
28 zassert_equal(4, NHPOT(4));
29 zassert_equal(8, NHPOT(5));
30 zassert_equal(BIT(31), NHPOT(BIT(31)));
31 zassert_equal(BIT64(32), NHPOT(BIT(31) + 1));
32 zassert_equal(BIT64(32), NHPOT(UINT32_MAX));
33 zassert_equal(BIT64(32), NHPOT(BIT64(32)));
34 zassert_equal(0, (uint32_t)NHPOT(BIT64(32)));
35 zassert_equal(BIT64(33), NHPOT(BIT64(32) + 1));
36 zassert_equal(BIT64(63), NHPOT(BIT64(63) - 1));
37 zassert_equal(BIT64(63), NHPOT(BIT64(63)));
38 zassert_equal(0, NHPOT(BIT64(63) + 1));
39 zassert_equal(0, NHPOT(UINT64_MAX));
40
41 zassert_equal(64, val);
42 zassert_equal(64ULL, nhpot(42ULL));
43 zassert_equal(BIT64(33), val64);
44 zassert_equal(BIT64(33), nhpot(42 + BIT64(32)));
45 }
46