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.
37 #include <rte_common.h>
38 #include <rte_hexdump.h>
39 #include <rte_pause.h>
43 #define MAX_NUM 1 << 20
46 {printf(x "() test failed!\n");\
49 /* this is really a sanity check */
51 test_macros(int __rte_unused unused_parm)
53 #define SMALLER 0x1000U
54 #define BIGGER 0x2000U
55 #define PTR_DIFF BIGGER - SMALLER
56 #define FAIL_MACRO(x)\
57 {printf(#x "() test failed!\n");\
64 if ((uintptr_t)RTE_PTR_ADD(SMALLER, PTR_DIFF) != BIGGER)
65 FAIL_MACRO(RTE_PTR_ADD);
66 if ((uintptr_t)RTE_PTR_SUB(BIGGER, PTR_DIFF) != SMALLER)
67 FAIL_MACRO(RTE_PTR_SUB);
68 if (RTE_PTR_DIFF(BIGGER, SMALLER) != PTR_DIFF)
69 FAIL_MACRO(RTE_PTR_DIFF);
70 if (RTE_MAX(SMALLER, BIGGER) != BIGGER)
72 if (RTE_MIN(SMALLER, BIGGER) != SMALLER)
75 if (strncmp(RTE_STR(test), "test", sizeof("test")))
84 char memdump[] = "memdump_test";
88 rte_memdump(stdout, "test", memdump, sizeof(memdump));
89 rte_hexdump(stdout, "test", memdump, sizeof(memdump));
99 #define FAIL_ALIGN(x, i, p)\
100 {printf(x "() test failed: %u %u\n", i, p);\
102 #define ERROR_FLOOR(res, i, pow) \
103 (res % pow) || /* check if not aligned */ \
104 ((res / pow) != (i / pow)) /* check if correct alignment */
105 #define ERROR_CEIL(res, i, pow) \
106 (res % pow) || /* check if not aligned */ \
107 ((i % pow) == 0 ? /* check if ceiling is invoked */ \
108 val / pow != i / pow : /* if aligned */ \
109 val / pow != (i / pow) + 1) /* if not aligned, hence +1 */
113 for (i = 1, p = 1; i <= MAX_NUM; i ++) {
114 if (rte_align32pow2(i) != p)
115 FAIL_ALIGN("rte_align32pow2", i, p);
120 for (p = 2; p <= MAX_NUM; p <<= 1) {
122 if (!rte_is_power_of_2(p))
123 FAIL("rte_is_power_of_2");
125 for (i = 1; i <= MAX_NUM; i++) {
127 if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
128 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
130 val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
131 if (ERROR_FLOOR(val, i, p))
132 FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
134 val = RTE_ALIGN_FLOOR(i, p);
135 if (ERROR_FLOOR(val, i, p))
136 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
139 val = RTE_PTR_ALIGN((uintptr_t) i, p);
140 if (ERROR_CEIL(val, i, p))
141 FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
143 val = RTE_ALIGN(i, p);
144 if (ERROR_CEIL(val, i, p))
145 FAIL_ALIGN("RTE_ALIGN", i, p);
147 val = RTE_ALIGN_CEIL(i, p);
148 if (ERROR_CEIL(val, i, p))
149 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
151 val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
152 if (ERROR_CEIL(val, i, p))
153 FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
155 /* by this point we know that val is aligned to p */
156 if (!rte_is_aligned((void*)(uintptr_t) val, p))
157 FAIL("rte_is_aligned");
166 uint32_t i, base, compare;
167 const uint32_t max = 0x10000;
168 const uint32_t step = 1;
170 for (i = 0; i < max; i = i + step) {
171 base = (uint32_t)ceilf(log2((uint32_t)i));
172 compare = rte_log2_u32(i);
173 if (base != compare) {
174 printf("Wrong rte_log2_u32(%x) val %x, expected %x\n",
187 ret |= test_macros(0);
194 REGISTER_TEST_COMMAND(common_autotest, test_common);