xref: /dpdk/app/test/test_common.c (revision a0a388a8)
1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson  * Copyright(c) 2010-2014 Intel Corporation
3a9de470cSBruce Richardson  */
4a9de470cSBruce Richardson 
5a9de470cSBruce Richardson #include <stdio.h>
6a9de470cSBruce Richardson #include <inttypes.h>
7a9de470cSBruce Richardson #include <string.h>
8a9de470cSBruce Richardson #include <math.h>
9a9de470cSBruce Richardson #include <rte_common.h>
10a9de470cSBruce Richardson #include <rte_hexdump.h>
11a9de470cSBruce Richardson #include <rte_pause.h>
12a9de470cSBruce Richardson 
13a9de470cSBruce Richardson #include "test.h"
14a9de470cSBruce Richardson 
15a9de470cSBruce Richardson #define MAX_NUM 1 << 20
16a9de470cSBruce Richardson 
17a9de470cSBruce Richardson #define FAIL(x)\
18a9de470cSBruce Richardson 	{printf(x "() test failed!\n");\
19a9de470cSBruce Richardson 	return -1;}
20a9de470cSBruce Richardson 
21a9de470cSBruce Richardson /* this is really a sanity check */
22a9de470cSBruce Richardson static int
test_macros(int __rte_unused unused_parm)23a9de470cSBruce Richardson test_macros(int __rte_unused unused_parm)
24a9de470cSBruce Richardson {
25a9de470cSBruce Richardson #define SMALLER 0x1000U
26a9de470cSBruce Richardson #define BIGGER 0x2000U
27a9de470cSBruce Richardson #define PTR_DIFF BIGGER - SMALLER
28a9de470cSBruce Richardson #define FAIL_MACRO(x)\
29a9de470cSBruce Richardson 	{printf(#x "() test failed!\n");\
30a9de470cSBruce Richardson 	return -1;}
31a9de470cSBruce Richardson 
32a9de470cSBruce Richardson 	uintptr_t unused = 0;
33*a0a388a8SShijith Thotton 	unsigned int smaller = SMALLER, bigger  = BIGGER;
34a9de470cSBruce Richardson 
35a9de470cSBruce Richardson 	RTE_SET_USED(unused);
36a9de470cSBruce Richardson 
37*a0a388a8SShijith Thotton 	RTE_SWAP(smaller, bigger);
38*a0a388a8SShijith Thotton 	if (smaller != BIGGER && bigger != SMALLER)
39*a0a388a8SShijith Thotton 		FAIL_MACRO(RTE_SWAP);
40a9de470cSBruce Richardson 	if ((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER)
41a9de470cSBruce Richardson 		FAIL_MACRO(RTE_PTR_ADD);
42a9de470cSBruce Richardson 	if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
43a9de470cSBruce Richardson 		FAIL_MACRO(RTE_PTR_SUB);
44a9de470cSBruce Richardson 	if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
45a9de470cSBruce Richardson 		FAIL_MACRO(RTE_PTR_DIFF);
46a9de470cSBruce Richardson 	if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
47a9de470cSBruce Richardson 		FAIL_MACRO(RTE_MAX);
48a9de470cSBruce Richardson 	if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
49a9de470cSBruce Richardson 		FAIL_MACRO(RTE_MIN);
50a9de470cSBruce Richardson 
51a9de470cSBruce Richardson 	if (strncmp(RTE_STR(test), "test", sizeof("test")))
52a9de470cSBruce Richardson 		FAIL_MACRO(RTE_STR);
53a9de470cSBruce Richardson 
54a9de470cSBruce Richardson 	return 0;
55a9de470cSBruce Richardson }
56a9de470cSBruce Richardson 
57a9de470cSBruce Richardson static int
test_bsf(void)58a9de470cSBruce Richardson test_bsf(void)
59a9de470cSBruce Richardson {
60a9de470cSBruce Richardson 	uint32_t shift, pos;
61a9de470cSBruce Richardson 
62a9de470cSBruce Richardson 	/* safe versions should be able to handle 0 */
63a9de470cSBruce Richardson 	if (rte_bsf32_safe(0, &pos) != 0)
64a9de470cSBruce Richardson 		FAIL("rte_bsf32_safe");
65a9de470cSBruce Richardson 	if (rte_bsf64_safe(0, &pos) != 0)
66a9de470cSBruce Richardson 		FAIL("rte_bsf64_safe");
67a9de470cSBruce Richardson 
68a9de470cSBruce Richardson 	for (shift = 0; shift < 63; shift++) {
69a9de470cSBruce Richardson 		uint32_t val32;
70a9de470cSBruce Richardson 		uint64_t val64;
71a9de470cSBruce Richardson 
72a9de470cSBruce Richardson 		val64 = 1ULL << shift;
73a9de470cSBruce Richardson 		if ((uint32_t)rte_bsf64(val64) != shift)
74a9de470cSBruce Richardson 			FAIL("rte_bsf64");
75a9de470cSBruce Richardson 		if (rte_bsf64_safe(val64, &pos) != 1)
76a9de470cSBruce Richardson 			FAIL("rte_bsf64_safe");
77a9de470cSBruce Richardson 		if (pos != shift)
78a9de470cSBruce Richardson 			FAIL("rte_bsf64_safe");
79a9de470cSBruce Richardson 
80a9de470cSBruce Richardson 		if (shift > 31)
81a9de470cSBruce Richardson 			continue;
82a9de470cSBruce Richardson 
83a9de470cSBruce Richardson 		val32 = 1U << shift;
84a9de470cSBruce Richardson 		if ((uint32_t)rte_bsf32(val32) != shift)
85a9de470cSBruce Richardson 			FAIL("rte_bsf32");
86a9de470cSBruce Richardson 		if (rte_bsf32_safe(val32, &pos) != 1)
87a9de470cSBruce Richardson 			FAIL("rte_bsf32_safe");
88a9de470cSBruce Richardson 		if (pos != shift)
89a9de470cSBruce Richardson 			FAIL("rte_bsf32_safe");
90a9de470cSBruce Richardson 	}
91a9de470cSBruce Richardson 
92a9de470cSBruce Richardson 	return 0;
93a9de470cSBruce Richardson }
94a9de470cSBruce Richardson 
95a9de470cSBruce Richardson static int
test_misc(void)96a9de470cSBruce Richardson test_misc(void)
97a9de470cSBruce Richardson {
98a9de470cSBruce Richardson 	char memdump[] = "memdump_test";
99a9de470cSBruce Richardson 
100a9de470cSBruce Richardson 	rte_memdump(stdout, "test", memdump, sizeof(memdump));
101a9de470cSBruce Richardson 	rte_hexdump(stdout, "test", memdump, sizeof(memdump));
102a9de470cSBruce Richardson 
103a9de470cSBruce Richardson 	rte_pause();
104a9de470cSBruce Richardson 
105a9de470cSBruce Richardson 	return 0;
106a9de470cSBruce Richardson }
107a9de470cSBruce Richardson 
108a9de470cSBruce Richardson static int
test_align(void)109a9de470cSBruce Richardson test_align(void)
110a9de470cSBruce Richardson {
111a9de470cSBruce Richardson #define FAIL_ALIGN(x, i, p)\
112a9de470cSBruce Richardson 	{printf(x "() test failed: %u %u\n", i, p);\
113a9de470cSBruce Richardson 	return -1;}
114a9de470cSBruce Richardson #define FAIL_ALIGN64(x, j, q)\
115a9de470cSBruce Richardson 	{printf(x "() test failed: %"PRIu64" %"PRIu64"\n", j, q);\
116a9de470cSBruce Richardson 	return -1; }
117a9de470cSBruce Richardson #define ERROR_FLOOR(res, i, pow) \
118a9de470cSBruce Richardson 		(res % pow) || 						/* check if not aligned */ \
119a9de470cSBruce Richardson 		((res / pow) != (i / pow))  		/* check if correct alignment */
120a9de470cSBruce Richardson #define ERROR_CEIL(res, i, pow) \
121a9de470cSBruce Richardson 		(res % pow) ||						/* check if not aligned */ \
122a9de470cSBruce Richardson 			((i % pow) == 0 ?				/* check if ceiling is invoked */ \
123a9de470cSBruce Richardson 			val / pow != i / pow :			/* if aligned */ \
124a9de470cSBruce Richardson 			val / pow != (i / pow) + 1)		/* if not aligned, hence +1 */
125a9de470cSBruce Richardson 
126a9de470cSBruce Richardson 	uint32_t i, p, val;
127a9de470cSBruce Richardson 	uint64_t j, q;
128a9de470cSBruce Richardson 
129a9de470cSBruce Richardson 	for (i = 1, p = 1; i <= MAX_NUM; i ++) {
130a9de470cSBruce Richardson 		if (rte_align32pow2(i) != p)
131a9de470cSBruce Richardson 			FAIL_ALIGN("rte_align32pow2", i, p);
132a9de470cSBruce Richardson 		if (i == p)
133a9de470cSBruce Richardson 			p <<= 1;
134a9de470cSBruce Richardson 	}
135a9de470cSBruce Richardson 
136a9de470cSBruce Richardson 	for (i = 1, p = 1; i <= MAX_NUM; i++) {
137a9de470cSBruce Richardson 		if (rte_align32prevpow2(i) != p)
138a9de470cSBruce Richardson 			FAIL_ALIGN("rte_align32prevpow2", i, p);
139a9de470cSBruce Richardson 		if (rte_is_power_of_2(i + 1))
140a9de470cSBruce Richardson 			p = i + 1;
141a9de470cSBruce Richardson 	}
142a9de470cSBruce Richardson 
143a9de470cSBruce Richardson 	for (j = 1, q = 1; j <= MAX_NUM ; j++) {
144a9de470cSBruce Richardson 		if (rte_align64pow2(j) != q)
145a9de470cSBruce Richardson 			FAIL_ALIGN64("rte_align64pow2", j, q);
146a9de470cSBruce Richardson 		if (j == q)
147a9de470cSBruce Richardson 			q <<= 1;
148a9de470cSBruce Richardson 	}
149a9de470cSBruce Richardson 
150a9de470cSBruce Richardson 	for (j = 1, q = 1; j <= MAX_NUM ; j++) {
151a9de470cSBruce Richardson 		if (rte_align64prevpow2(j) != q)
152a9de470cSBruce Richardson 			FAIL_ALIGN64("rte_align64prevpow2", j, q);
153a9de470cSBruce Richardson 		if (rte_is_power_of_2(j + 1))
154a9de470cSBruce Richardson 			q = j + 1;
155a9de470cSBruce Richardson 	}
156a9de470cSBruce Richardson 
157a9de470cSBruce Richardson 	for (p = 2; p <= MAX_NUM; p <<= 1) {
158a9de470cSBruce Richardson 
159a9de470cSBruce Richardson 		if (!rte_is_power_of_2(p))
160a9de470cSBruce Richardson 			FAIL("rte_is_power_of_2");
161a9de470cSBruce Richardson 
162a9de470cSBruce Richardson 		for (i = 1; i <= MAX_NUM; i++) {
163a9de470cSBruce Richardson 			/* align floor */
164a9de470cSBruce Richardson 			if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
165a9de470cSBruce Richardson 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
166a9de470cSBruce Richardson 
167a9de470cSBruce Richardson 			val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
168a9de470cSBruce Richardson 			if (ERROR_FLOOR(val, i, p))
169a9de470cSBruce Richardson 				FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
170a9de470cSBruce Richardson 
171a9de470cSBruce Richardson 			val = RTE_ALIGN_FLOOR(i, p);
172a9de470cSBruce Richardson 			if (ERROR_FLOOR(val, i, p))
173a9de470cSBruce Richardson 				FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
174a9de470cSBruce Richardson 
175a9de470cSBruce Richardson 			/* align ceiling */
176a9de470cSBruce Richardson 			val = RTE_PTR_ALIGN((uintptr_t) i, p);
177a9de470cSBruce Richardson 			if (ERROR_CEIL(val, i, p))
178a9de470cSBruce Richardson 				FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
179a9de470cSBruce Richardson 
180a9de470cSBruce Richardson 			val = RTE_ALIGN(i, p);
181a9de470cSBruce Richardson 			if (ERROR_CEIL(val, i, p))
182a9de470cSBruce Richardson 				FAIL_ALIGN("RTE_ALIGN", i, p);
183a9de470cSBruce Richardson 
184a9de470cSBruce Richardson 			val = RTE_ALIGN_CEIL(i, p);
185a9de470cSBruce Richardson 			if (ERROR_CEIL(val, i, p))
186a9de470cSBruce Richardson 				FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
187a9de470cSBruce Richardson 
188a9de470cSBruce Richardson 			val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
189a9de470cSBruce Richardson 			if (ERROR_CEIL(val, i, p))
190a9de470cSBruce Richardson 				FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
191a9de470cSBruce Richardson 
192a9de470cSBruce Richardson 			/* by this point we know that val is aligned to p */
193a9de470cSBruce Richardson 			if (!rte_is_aligned((void*)(uintptr_t) val, p))
194a9de470cSBruce Richardson 				FAIL("rte_is_aligned");
195a9de470cSBruce Richardson 		}
196a9de470cSBruce Richardson 	}
197a9de470cSBruce Richardson 
198a9de470cSBruce Richardson 	for (p = 1; p <= MAX_NUM / 2; p++) {
199a9de470cSBruce Richardson 		for (i = 1; i <= MAX_NUM / 2; i++) {
200a9de470cSBruce Richardson 			val = RTE_ALIGN_MUL_CEIL(i, p);
201a9de470cSBruce Richardson 			if (val % p != 0 || val < i)
202a9de470cSBruce Richardson 				FAIL_ALIGN("RTE_ALIGN_MUL_CEIL", i, p);
203a9de470cSBruce Richardson 			val = RTE_ALIGN_MUL_FLOOR(i, p);
204a9de470cSBruce Richardson 			if (val % p != 0 || val > i)
205a9de470cSBruce Richardson 				FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p);
206f56e5514SPavan Nikhilesh 			val = RTE_ALIGN_MUL_NEAR(i, p);
207f56e5514SPavan Nikhilesh 			if (val % p != 0 || ((val != RTE_ALIGN_MUL_CEIL(i, p))
208f56e5514SPavan Nikhilesh 				& (val != RTE_ALIGN_MUL_FLOOR(i, p))))
209f56e5514SPavan Nikhilesh 				FAIL_ALIGN("RTE_ALIGN_MUL_NEAR", i, p);
210a9de470cSBruce Richardson 		}
211a9de470cSBruce Richardson 	}
212a9de470cSBruce Richardson 
213a9de470cSBruce Richardson 	return 0;
214a9de470cSBruce Richardson }
215a9de470cSBruce Richardson 
216a9de470cSBruce Richardson static int
test_log2(void)217a9de470cSBruce Richardson test_log2(void)
218a9de470cSBruce Richardson {
219a9de470cSBruce Richardson 	uint32_t i, base, compare;
220a9de470cSBruce Richardson 	const uint32_t max = 0x10000;
221a9de470cSBruce Richardson 	const uint32_t step = 1;
222a9de470cSBruce Richardson 
22330a0df64SDavid Marchand 	compare = rte_log2_u32(0);
22430a0df64SDavid Marchand 	if (compare != 0) {
22530a0df64SDavid Marchand 		printf("Wrong rte_log2_u32(0) val %x, expected 0\n", compare);
22630a0df64SDavid Marchand 		return TEST_FAILED;
22730a0df64SDavid Marchand 	}
22830a0df64SDavid Marchand 
22930a0df64SDavid Marchand 	compare = rte_log2_u64(0);
23030a0df64SDavid Marchand 	if (compare != 0) {
23130a0df64SDavid Marchand 		printf("Wrong rte_log2_u64(0) val %x, expected 0\n", compare);
23230a0df64SDavid Marchand 		return TEST_FAILED;
23330a0df64SDavid Marchand 	}
23430a0df64SDavid Marchand 
23530a0df64SDavid Marchand 	for (i = 1; i < max; i = i + step) {
236a9de470cSBruce Richardson 		uint64_t i64;
237a9de470cSBruce Richardson 
238a9de470cSBruce Richardson 		/* extend range for 64-bit */
239a9de470cSBruce Richardson 		i64 = (uint64_t)i << 32;
240a9de470cSBruce Richardson 		base = (uint32_t)ceilf(log2(i64));
241a9de470cSBruce Richardson 		compare = rte_log2_u64(i64);
242a9de470cSBruce Richardson 		if (base != compare) {
243a9de470cSBruce Richardson 			printf("Wrong rte_log2_u64(%" PRIx64 ") val %x, expected %x\n",
244a9de470cSBruce Richardson 				i64, compare, base);
245a9de470cSBruce Richardson 			return TEST_FAILED;
246a9de470cSBruce Richardson 		}
247a9de470cSBruce Richardson 
248a9de470cSBruce Richardson 		base = (uint32_t)ceilf(log2((uint32_t)i));
249a9de470cSBruce Richardson 		compare = rte_log2_u32((uint32_t)i);
250a9de470cSBruce Richardson 		if (base != compare) {
251a9de470cSBruce Richardson 			printf("Wrong rte_log2_u32(%x) val %x, expected %x\n",
252a9de470cSBruce Richardson 				i, compare, base);
253a9de470cSBruce Richardson 			return TEST_FAILED;
254a9de470cSBruce Richardson 		}
255a9de470cSBruce Richardson 		compare = rte_log2_u64((uint64_t)i);
256a9de470cSBruce Richardson 		if (base != compare) {
257a9de470cSBruce Richardson 			printf("Wrong rte_log2_u64(%x) val %x, expected %x\n",
258a9de470cSBruce Richardson 				i, compare, base);
259a9de470cSBruce Richardson 			return TEST_FAILED;
260a9de470cSBruce Richardson 		}
261a9de470cSBruce Richardson 	}
262a9de470cSBruce Richardson 	return 0;
263a9de470cSBruce Richardson }
264a9de470cSBruce Richardson 
265a9de470cSBruce Richardson static int
test_fls(void)266a9de470cSBruce Richardson test_fls(void)
267a9de470cSBruce Richardson {
268a9de470cSBruce Richardson 	struct fls_test_vector {
269a9de470cSBruce Richardson 		uint32_t arg;
270a9de470cSBruce Richardson 		int rc;
271a9de470cSBruce Richardson 	};
272a9de470cSBruce Richardson 	int expected, rc;
273a9de470cSBruce Richardson 	uint32_t i, arg;
274a9de470cSBruce Richardson 
275a9de470cSBruce Richardson 	const struct fls_test_vector test[] = {
276a9de470cSBruce Richardson 		{0x0, 0},
277a9de470cSBruce Richardson 		{0x1, 1},
278a9de470cSBruce Richardson 		{0x4000, 15},
279a9de470cSBruce Richardson 		{0x80000000, 32},
280a9de470cSBruce Richardson 	};
281a9de470cSBruce Richardson 
282a9de470cSBruce Richardson 	for (i = 0; i < RTE_DIM(test); i++) {
283a9de470cSBruce Richardson 		uint64_t arg64;
284a9de470cSBruce Richardson 
285a9de470cSBruce Richardson 		arg = test[i].arg;
286a9de470cSBruce Richardson 		rc = rte_fls_u32(arg);
287a9de470cSBruce Richardson 		expected = test[i].rc;
288a9de470cSBruce Richardson 		if (rc != expected) {
289a9de470cSBruce Richardson 			printf("Wrong rte_fls_u32(0x%x) rc=%d, expected=%d\n",
290a9de470cSBruce Richardson 				arg, rc, expected);
291a9de470cSBruce Richardson 			return TEST_FAILED;
292a9de470cSBruce Richardson 		}
293a9de470cSBruce Richardson 		/* 64-bit version */
294a9de470cSBruce Richardson 		arg = test[i].arg;
295a9de470cSBruce Richardson 		rc = rte_fls_u64(arg);
296a9de470cSBruce Richardson 		expected = test[i].rc;
297a9de470cSBruce Richardson 		if (rc != expected) {
298a9de470cSBruce Richardson 			printf("Wrong rte_fls_u64(0x%x) rc=%d, expected=%d\n",
299a9de470cSBruce Richardson 				arg, rc, expected);
300a9de470cSBruce Richardson 			return TEST_FAILED;
301a9de470cSBruce Richardson 		}
302a9de470cSBruce Richardson 		/* 64-bit version shifted by 32 bits */
303a9de470cSBruce Richardson 		arg64 = (uint64_t)test[i].arg << 32;
304a9de470cSBruce Richardson 		rc = rte_fls_u64(arg64);
305a9de470cSBruce Richardson 		/* don't shift zero */
306a9de470cSBruce Richardson 		expected = test[i].rc == 0 ? 0 : test[i].rc + 32;
307a9de470cSBruce Richardson 		if (rc != expected) {
308a9de470cSBruce Richardson 			printf("Wrong rte_fls_u64(0x%" PRIx64 ") rc=%d, expected=%d\n",
309a9de470cSBruce Richardson 				arg64, rc, expected);
310a9de470cSBruce Richardson 			return TEST_FAILED;
311a9de470cSBruce Richardson 		}
312a9de470cSBruce Richardson 	}
313a9de470cSBruce Richardson 
314a9de470cSBruce Richardson 	return 0;
315a9de470cSBruce Richardson }
316a9de470cSBruce Richardson 
317a9de470cSBruce Richardson static int
test_common(void)318a9de470cSBruce Richardson test_common(void)
319a9de470cSBruce Richardson {
320a9de470cSBruce Richardson 	int ret = 0;
321a9de470cSBruce Richardson 	ret |= test_align();
322a9de470cSBruce Richardson 	ret |= test_macros(0);
323a9de470cSBruce Richardson 	ret |= test_misc();
324a9de470cSBruce Richardson 	ret |= test_bsf();
325a9de470cSBruce Richardson 	ret |= test_log2();
326a9de470cSBruce Richardson 	ret |= test_fls();
327a9de470cSBruce Richardson 
328a9de470cSBruce Richardson 	return ret;
329a9de470cSBruce Richardson }
330a9de470cSBruce Richardson 
331a9de470cSBruce Richardson REGISTER_TEST_COMMAND(common_autotest, test_common);
332