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")))
58 /* safe versions should be able to handle 0 */
59 if (rte_bsf32_safe(0, &pos) != 0)
60 FAIL("rte_bsf32_safe");
61 if (rte_bsf64_safe(0, &pos) != 0)
62 FAIL("rte_bsf64_safe");
64 for (shift = 0; shift < 63; shift++) {
68 val64 = 1ULL << shift;
69 if ((uint32_t)rte_bsf64(val64) != shift)
71 if (rte_bsf64_safe(val64, &pos) != 1)
72 FAIL("rte_bsf64_safe");
74 FAIL("rte_bsf64_safe");
80 if ((uint32_t)rte_bsf32(val32) != shift)
82 if (rte_bsf32_safe(val32, &pos) != 1)
83 FAIL("rte_bsf32_safe");
85 FAIL("rte_bsf32_safe");
94 char memdump[] = "memdump_test";
96 rte_memdump(stdout, "test", memdump, sizeof(memdump));
97 rte_hexdump(stdout, "test", memdump, sizeof(memdump));
107 #define FAIL_ALIGN(x, i, p)\
108 {printf(x "() test failed: %u %u\n", i, p);\
110 #define FAIL_ALIGN64(x, j, q)\
111 {printf(x "() test failed: %"PRIu64" %"PRIu64"\n", j, q);\
113 #define ERROR_FLOOR(res, i, pow) \
114 (res % pow) || /* check if not aligned */ \
115 ((res / pow) != (i / pow)) /* check if correct alignment */
116 #define ERROR_CEIL(res, i, pow) \
117 (res % pow) || /* check if not aligned */ \
118 ((i % pow) == 0 ? /* check if ceiling is invoked */ \
119 val / pow != i / pow : /* if aligned */ \
120 val / pow != (i / pow) + 1) /* if not aligned, hence +1 */
125 for (i = 1, p = 1; i <= MAX_NUM; i ++) {
126 if (rte_align32pow2(i) != p)
127 FAIL_ALIGN("rte_align32pow2", i, p);
132 for (i = 1, p = 1; i <= MAX_NUM; i++) {
133 if (rte_align32prevpow2(i) != p)
134 FAIL_ALIGN("rte_align32prevpow2", i, p);
135 if (rte_is_power_of_2(i + 1))
139 for (j = 1, q = 1; j <= MAX_NUM ; j++) {
140 if (rte_align64pow2(j) != q)
141 FAIL_ALIGN64("rte_align64pow2", j, q);
146 for (j = 1, q = 1; j <= MAX_NUM ; j++) {
147 if (rte_align64prevpow2(j) != q)
148 FAIL_ALIGN64("rte_align64prevpow2", j, q);
149 if (rte_is_power_of_2(j + 1))
153 for (p = 2; p <= MAX_NUM; p <<= 1) {
155 if (!rte_is_power_of_2(p))
156 FAIL("rte_is_power_of_2");
158 for (i = 1; i <= MAX_NUM; i++) {
160 if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
161 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
163 val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
164 if (ERROR_FLOOR(val, i, p))
165 FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
167 val = RTE_ALIGN_FLOOR(i, p);
168 if (ERROR_FLOOR(val, i, p))
169 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
172 val = RTE_PTR_ALIGN((uintptr_t) i, p);
173 if (ERROR_CEIL(val, i, p))
174 FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
176 val = RTE_ALIGN(i, p);
177 if (ERROR_CEIL(val, i, p))
178 FAIL_ALIGN("RTE_ALIGN", i, p);
180 val = RTE_ALIGN_CEIL(i, p);
181 if (ERROR_CEIL(val, i, p))
182 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
184 val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
185 if (ERROR_CEIL(val, i, p))
186 FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
188 /* by this point we know that val is aligned to p */
189 if (!rte_is_aligned((void*)(uintptr_t) val, p))
190 FAIL("rte_is_aligned");
194 for (p = 1; p <= MAX_NUM / 2; p++) {
195 for (i = 1; i <= MAX_NUM / 2; i++) {
196 val = RTE_ALIGN_MUL_CEIL(i, p);
197 if (val % p != 0 || val < i)
198 FAIL_ALIGN("RTE_ALIGN_MUL_CEIL", i, p);
199 val = RTE_ALIGN_MUL_FLOOR(i, p);
200 if (val % p != 0 || val > i)
201 FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p);
202 val = RTE_ALIGN_MUL_NEAR(i, p);
203 if (val % p != 0 || ((val != RTE_ALIGN_MUL_CEIL(i, p))
204 & (val != RTE_ALIGN_MUL_FLOOR(i, p))))
205 FAIL_ALIGN("RTE_ALIGN_MUL_NEAR", i, p);
215 uint32_t i, base, compare;
216 const uint32_t max = 0x10000;
217 const uint32_t step = 1;
219 compare = rte_log2_u32(0);
221 printf("Wrong rte_log2_u32(0) val %x, expected 0\n", compare);
225 compare = rte_log2_u64(0);
227 printf("Wrong rte_log2_u64(0) val %x, expected 0\n", compare);
231 for (i = 1; i < max; i = i + step) {
234 /* extend range for 64-bit */
235 i64 = (uint64_t)i << 32;
236 base = (uint32_t)ceilf(log2(i64));
237 compare = rte_log2_u64(i64);
238 if (base != compare) {
239 printf("Wrong rte_log2_u64(%" PRIx64 ") val %x, expected %x\n",
244 base = (uint32_t)ceilf(log2((uint32_t)i));
245 compare = rte_log2_u32((uint32_t)i);
246 if (base != compare) {
247 printf("Wrong rte_log2_u32(%x) val %x, expected %x\n",
251 compare = rte_log2_u64((uint64_t)i);
252 if (base != compare) {
253 printf("Wrong rte_log2_u64(%x) val %x, expected %x\n",
264 struct fls_test_vector {
271 const struct fls_test_vector test[] = {
278 for (i = 0; i < RTE_DIM(test); i++) {
282 rc = rte_fls_u32(arg);
283 expected = test[i].rc;
284 if (rc != expected) {
285 printf("Wrong rte_fls_u32(0x%x) rc=%d, expected=%d\n",
291 rc = rte_fls_u64(arg);
292 expected = test[i].rc;
293 if (rc != expected) {
294 printf("Wrong rte_fls_u64(0x%x) rc=%d, expected=%d\n",
298 /* 64-bit version shifted by 32 bits */
299 arg64 = (uint64_t)test[i].arg << 32;
300 rc = rte_fls_u64(arg64);
301 /* don't shift zero */
302 expected = test[i].rc == 0 ? 0 : test[i].rc + 32;
303 if (rc != expected) {
304 printf("Wrong rte_fls_u64(0x%" PRIx64 ") rc=%d, expected=%d\n",
305 arg64, rc, expected);
318 ret |= test_macros(0);
327 REGISTER_TEST_COMMAND(common_autotest, test_common);