1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
9 #include <rte_common.h>
10 #include <rte_hexdump.h>
11 #include <rte_pause.h>
15 #define MAX_NUM 1 << 20
18 {printf(x "() test failed!\n");\
21 /* this is really a sanity check */
23 test_macros(int __rte_unused unused_parm)
25 #define SMALLER 0x1000U
26 #define BIGGER 0x2000U
27 #define PTR_DIFF BIGGER - SMALLER
28 #define FAIL_MACRO(x)\
29 {printf(#x "() test failed!\n");\
36 if ((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER)
37 FAIL_MACRO(RTE_PTR_ADD);
38 if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
39 FAIL_MACRO(RTE_PTR_SUB);
40 if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
41 FAIL_MACRO(RTE_PTR_DIFF);
42 if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
44 if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
47 if (strncmp(RTE_STR(test), "test", sizeof("test")))
56 char memdump[] = "memdump_test";
60 rte_memdump(stdout, "test", memdump, sizeof(memdump));
61 rte_hexdump(stdout, "test", memdump, sizeof(memdump));
71 #define FAIL_ALIGN(x, i, p)\
72 {printf(x "() test failed: %u %u\n", i, p);\
74 #define FAIL_ALIGN64(x, j, q)\
75 {printf(x "() test failed: %"PRIu64" %"PRIu64"\n", j, q);\
77 #define ERROR_FLOOR(res, i, pow) \
78 (res % pow) || /* check if not aligned */ \
79 ((res / pow) != (i / pow)) /* check if correct alignment */
80 #define ERROR_CEIL(res, i, pow) \
81 (res % pow) || /* check if not aligned */ \
82 ((i % pow) == 0 ? /* check if ceiling is invoked */ \
83 val / pow != i / pow : /* if aligned */ \
84 val / pow != (i / pow) + 1) /* if not aligned, hence +1 */
89 for (i = 1, p = 1; i <= MAX_NUM; i ++) {
90 if (rte_align32pow2(i) != p)
91 FAIL_ALIGN("rte_align32pow2", i, p);
96 for (i = 1, p = 1; i <= MAX_NUM; i++) {
97 if (rte_align32prevpow2(i) != p)
98 FAIL_ALIGN("rte_align32prevpow2", i, p);
99 if (rte_is_power_of_2(i + 1))
103 for (j = 1, q = 1; j <= MAX_NUM ; j++) {
104 if (rte_align64pow2(j) != q)
105 FAIL_ALIGN64("rte_align64pow2", j, q);
110 for (j = 1, q = 1; j <= MAX_NUM ; j++) {
111 if (rte_align64prevpow2(j) != q)
112 FAIL_ALIGN64("rte_align64prevpow2", j, q);
113 if (rte_is_power_of_2(j + 1))
117 for (p = 2; p <= MAX_NUM; p <<= 1) {
119 if (!rte_is_power_of_2(p))
120 FAIL("rte_is_power_of_2");
122 for (i = 1; i <= MAX_NUM; i++) {
124 if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
125 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
127 val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
128 if (ERROR_FLOOR(val, i, p))
129 FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
131 val = RTE_ALIGN_FLOOR(i, p);
132 if (ERROR_FLOOR(val, i, p))
133 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
136 val = RTE_PTR_ALIGN((uintptr_t) i, p);
137 if (ERROR_CEIL(val, i, p))
138 FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
140 val = RTE_ALIGN(i, p);
141 if (ERROR_CEIL(val, i, p))
142 FAIL_ALIGN("RTE_ALIGN", i, p);
144 val = RTE_ALIGN_CEIL(i, p);
145 if (ERROR_CEIL(val, i, p))
146 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
148 val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
149 if (ERROR_CEIL(val, i, p))
150 FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
152 /* by this point we know that val is aligned to p */
153 if (!rte_is_aligned((void*)(uintptr_t) val, p))
154 FAIL("rte_is_aligned");
158 for (p = 1; p <= MAX_NUM / 2; p++) {
159 for (i = 1; i <= MAX_NUM / 2; i++) {
160 val = RTE_ALIGN_MUL_CEIL(i, p);
161 if (val % p != 0 || val < i)
162 FAIL_ALIGN("RTE_ALIGN_MUL_CEIL", i, p);
163 val = RTE_ALIGN_MUL_FLOOR(i, p);
164 if (val % p != 0 || val > i)
165 FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p);
175 uint32_t i, base, compare;
176 const uint32_t max = 0x10000;
177 const uint32_t step = 1;
179 for (i = 0; i < max; i = i + step) {
180 base = (uint32_t)ceilf(log2((uint32_t)i));
181 compare = rte_log2_u32(i);
182 if (base != compare) {
183 printf("Wrong rte_log2_u32(%x) val %x, expected %x\n",
196 ret |= test_macros(0);
203 REGISTER_TEST_COMMAND(common_autotest, test_common);