1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
3 */
4
5 #include <stdio.h>
6 #include <inttypes.h>
7 #include <string.h>
8 #include <math.h>
9 #include <rte_common.h>
10 #include <rte_hexdump.h>
11 #include <rte_pause.h>
12
13 #include "test.h"
14
15 #define MAX_NUM 1 << 20
16
17 #define FAIL(x)\
18 {printf(x "() test failed!\n");\
19 return -1;}
20
21 /* this is really a sanity check */
22 static int
test_macros(int __rte_unused unused_parm)23 test_macros(int __rte_unused unused_parm)
24 {
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");\
30 return -1;}
31
32 uintptr_t unused = 0;
33
34 RTE_SET_USED(unused);
35
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)
43 FAIL_MACRO(RTE_MAX);
44 if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
45 FAIL_MACRO(RTE_MIN);
46
47 if (strncmp(RTE_STR(test), "test", sizeof("test")))
48 FAIL_MACRO(RTE_STR);
49
50 return 0;
51 }
52
53 static int
test_bsf(void)54 test_bsf(void)
55 {
56 uint32_t shift, pos;
57
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");
63
64 for (shift = 0; shift < 63; shift++) {
65 uint32_t val32;
66 uint64_t val64;
67
68 val64 = 1ULL << shift;
69 if ((uint32_t)rte_bsf64(val64) != shift)
70 FAIL("rte_bsf64");
71 if (rte_bsf64_safe(val64, &pos) != 1)
72 FAIL("rte_bsf64_safe");
73 if (pos != shift)
74 FAIL("rte_bsf64_safe");
75
76 if (shift > 31)
77 continue;
78
79 val32 = 1U << shift;
80 if ((uint32_t)rte_bsf32(val32) != shift)
81 FAIL("rte_bsf32");
82 if (rte_bsf32_safe(val32, &pos) != 1)
83 FAIL("rte_bsf32_safe");
84 if (pos != shift)
85 FAIL("rte_bsf32_safe");
86 }
87
88 return 0;
89 }
90
91 static int
test_misc(void)92 test_misc(void)
93 {
94 char memdump[] = "memdump_test";
95
96 rte_memdump(stdout, "test", memdump, sizeof(memdump));
97 rte_hexdump(stdout, "test", memdump, sizeof(memdump));
98
99 rte_pause();
100
101 return 0;
102 }
103
104 static int
test_align(void)105 test_align(void)
106 {
107 #define FAIL_ALIGN(x, i, p)\
108 {printf(x "() test failed: %u %u\n", i, p);\
109 return -1;}
110 #define FAIL_ALIGN64(x, j, q)\
111 {printf(x "() test failed: %"PRIu64" %"PRIu64"\n", j, q);\
112 return -1; }
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 */
121
122 uint32_t i, p, val;
123 uint64_t j, q;
124
125 for (i = 1, p = 1; i <= MAX_NUM; i ++) {
126 if (rte_align32pow2(i) != p)
127 FAIL_ALIGN("rte_align32pow2", i, p);
128 if (i == p)
129 p <<= 1;
130 }
131
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))
136 p = i + 1;
137 }
138
139 for (j = 1, q = 1; j <= MAX_NUM ; j++) {
140 if (rte_align64pow2(j) != q)
141 FAIL_ALIGN64("rte_align64pow2", j, q);
142 if (j == q)
143 q <<= 1;
144 }
145
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))
150 q = j + 1;
151 }
152
153 for (p = 2; p <= MAX_NUM; p <<= 1) {
154
155 if (!rte_is_power_of_2(p))
156 FAIL("rte_is_power_of_2");
157
158 for (i = 1; i <= MAX_NUM; i++) {
159 /* align floor */
160 if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
161 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
162
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);
166
167 val = RTE_ALIGN_FLOOR(i, p);
168 if (ERROR_FLOOR(val, i, p))
169 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
170
171 /* align ceiling */
172 val = RTE_PTR_ALIGN((uintptr_t) i, p);
173 if (ERROR_CEIL(val, i, p))
174 FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
175
176 val = RTE_ALIGN(i, p);
177 if (ERROR_CEIL(val, i, p))
178 FAIL_ALIGN("RTE_ALIGN", i, p);
179
180 val = RTE_ALIGN_CEIL(i, p);
181 if (ERROR_CEIL(val, i, p))
182 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
183
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);
187
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");
191 }
192 }
193
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);
206 }
207 }
208
209 return 0;
210 }
211
212 static int
test_log2(void)213 test_log2(void)
214 {
215 uint32_t i, base, compare;
216 const uint32_t max = 0x10000;
217 const uint32_t step = 1;
218
219 compare = rte_log2_u32(0);
220 if (compare != 0) {
221 printf("Wrong rte_log2_u32(0) val %x, expected 0\n", compare);
222 return TEST_FAILED;
223 }
224
225 compare = rte_log2_u64(0);
226 if (compare != 0) {
227 printf("Wrong rte_log2_u64(0) val %x, expected 0\n", compare);
228 return TEST_FAILED;
229 }
230
231 for (i = 1; i < max; i = i + step) {
232 uint64_t i64;
233
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",
240 i64, compare, base);
241 return TEST_FAILED;
242 }
243
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",
248 i, compare, base);
249 return TEST_FAILED;
250 }
251 compare = rte_log2_u64((uint64_t)i);
252 if (base != compare) {
253 printf("Wrong rte_log2_u64(%x) val %x, expected %x\n",
254 i, compare, base);
255 return TEST_FAILED;
256 }
257 }
258 return 0;
259 }
260
261 static int
test_fls(void)262 test_fls(void)
263 {
264 struct fls_test_vector {
265 uint32_t arg;
266 int rc;
267 };
268 int expected, rc;
269 uint32_t i, arg;
270
271 const struct fls_test_vector test[] = {
272 {0x0, 0},
273 {0x1, 1},
274 {0x4000, 15},
275 {0x80000000, 32},
276 };
277
278 for (i = 0; i < RTE_DIM(test); i++) {
279 uint64_t arg64;
280
281 arg = test[i].arg;
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",
286 arg, rc, expected);
287 return TEST_FAILED;
288 }
289 /* 64-bit version */
290 arg = test[i].arg;
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",
295 arg, rc, expected);
296 return TEST_FAILED;
297 }
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);
306 return TEST_FAILED;
307 }
308 }
309
310 return 0;
311 }
312
313 static int
test_common(void)314 test_common(void)
315 {
316 int ret = 0;
317 ret |= test_align();
318 ret |= test_macros(0);
319 ret |= test_misc();
320 ret |= test_bsf();
321 ret |= test_log2();
322 ret |= test_fls();
323
324 return ret;
325 }
326
327 REGISTER_TEST_COMMAND(common_autotest, test_common);
328