4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <rte_common.h>
37 #include <rte_hexdump.h>
41 #define MAX_NUM 1 << 20
44 {printf(x "() test failed!\n");\
47 /* this is really a sanity check */
49 test_macros(int __rte_unused unused_parm)
51 #define SMALLER 0x1000U
52 #define BIGGER 0x2000U
53 #define PTR_DIFF BIGGER - SMALLER
54 #define FAIL_MACRO(x)\
55 {printf(#x "() test failed!\n");\
62 if ((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER)
63 FAIL_MACRO(RTE_PTR_ADD);
64 if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
65 FAIL_MACRO(RTE_PTR_SUB);
66 if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
67 FAIL_MACRO(RTE_PTR_DIFF);
68 if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
70 if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
73 if (strncmp(RTE_STR(test), "test", sizeof("test")))
82 char memdump[] = "memdump_test";
86 rte_memdump(stdout, "test", memdump, sizeof(memdump));
87 rte_hexdump(stdout, "test", memdump, sizeof(memdump));
97 #define FAIL_ALIGN(x, i, p)\
98 {printf(x "() test failed: %u %u\n", i, p);\
100 #define ERROR_FLOOR(res, i, pow) \
101 (res % pow) || /* check if not aligned */ \
102 ((res / pow) != (i / pow)) /* check if correct alignment */
103 #define ERROR_CEIL(res, i, pow) \
104 (res % pow) || /* check if not aligned */ \
105 ((i % pow) == 0 ? /* check if ceiling is invoked */ \
106 val / pow != i / pow : /* if aligned */ \
107 val / pow != (i / pow) + 1) /* if not aligned, hence +1 */
111 for (i = 1, p = 1; i <= MAX_NUM; i ++) {
112 if (rte_align32pow2(i) != p)
113 FAIL_ALIGN("rte_align32pow2", i, p);
118 for (p = 2; p <= MAX_NUM; p <<= 1) {
120 if (!rte_is_power_of_2(p))
121 FAIL("rte_is_power_of_2");
123 for (i = 1; i <= MAX_NUM; i++) {
125 if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
126 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
128 val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
129 if (ERROR_FLOOR(val, i, p))
130 FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
132 val = RTE_ALIGN_FLOOR(i, p);
133 if (ERROR_FLOOR(val, i, p))
134 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
137 val = RTE_PTR_ALIGN((uintptr_t) i, p);
138 if (ERROR_CEIL(val, i, p))
139 FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
141 val = RTE_ALIGN(i, p);
142 if (ERROR_CEIL(val, i, p))
143 FAIL_ALIGN("RTE_ALIGN", i, p);
145 val = RTE_ALIGN_CEIL(i, p);
146 if (ERROR_CEIL(val, i, p))
147 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
149 val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
150 if (ERROR_CEIL(val, i, p))
151 FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
153 /* by this point we know that val is aligned to p */
154 if (!rte_is_aligned((void*)(uintptr_t) val, p))
155 FAIL("rte_is_aligned");
166 ret |= test_macros(0);
172 static struct test_command common_cmd = {
173 .command = "common_autotest",
174 .callback = test_common,
176 REGISTER_TEST_COMMAND(common_cmd);