]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/dpdk/test/test/test_common.c
update download target update for octopus release
[ceph.git] / ceph / src / spdk / dpdk / test / test / test_common.c
CommitLineData
11fdf7f2
TL
1/* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
7c673cae
FG
3 */
4
5#include <stdio.h>
11fdf7f2 6#include <inttypes.h>
7c673cae 7#include <string.h>
11fdf7f2 8#include <math.h>
7c673cae
FG
9#include <rte_common.h>
10#include <rte_hexdump.h>
11fdf7f2 11#include <rte_pause.h>
7c673cae
FG
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 */
22static int
23test_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
53static int
54test_misc(void)
55{
56 char memdump[] = "memdump_test";
57 if (rte_bsf32(129))
58 FAIL("rte_bsf32");
59
60 rte_memdump(stdout, "test", memdump, sizeof(memdump));
61 rte_hexdump(stdout, "test", memdump, sizeof(memdump));
62
63 rte_pause();
64
65 return 0;
66}
67
68static int
69test_align(void)
70{
71#define FAIL_ALIGN(x, i, p)\
72 {printf(x "() test failed: %u %u\n", i, p);\
73 return -1;}
11fdf7f2
TL
74#define FAIL_ALIGN64(x, j, q)\
75 {printf(x "() test failed: %"PRIu64" %"PRIu64"\n", j, q);\
76 return -1; }
7c673cae
FG
77#define ERROR_FLOOR(res, i, pow) \
78 (res % pow) || /* check if not aligned */ \
79 ((res / pow) != (i / pow)) /* check if correct alignment */
80#define ERROR_CEIL(res, i, pow) \
81 (res % pow) || /* check if not aligned */ \
82 ((i % pow) == 0 ? /* check if ceiling is invoked */ \
83 val / pow != i / pow : /* if aligned */ \
84 val / pow != (i / pow) + 1) /* if not aligned, hence +1 */
85
86 uint32_t i, p, val;
11fdf7f2 87 uint64_t j, q;
7c673cae
FG
88
89 for (i = 1, p = 1; i <= MAX_NUM; i ++) {
90 if (rte_align32pow2(i) != p)
91 FAIL_ALIGN("rte_align32pow2", i, p);
92 if (i == p)
93 p <<= 1;
94 }
95
11fdf7f2
TL
96 for (i = 1, p = 1; i <= MAX_NUM; i++) {
97 if (rte_align32prevpow2(i) != p)
98 FAIL_ALIGN("rte_align32prevpow2", i, p);
99 if (rte_is_power_of_2(i + 1))
100 p = i + 1;
101 }
102
103 for (j = 1, q = 1; j <= MAX_NUM ; j++) {
104 if (rte_align64pow2(j) != q)
105 FAIL_ALIGN64("rte_align64pow2", j, q);
106 if (j == q)
107 q <<= 1;
108 }
109
110 for (j = 1, q = 1; j <= MAX_NUM ; j++) {
111 if (rte_align64prevpow2(j) != q)
112 FAIL_ALIGN64("rte_align64prevpow2", j, q);
113 if (rte_is_power_of_2(j + 1))
114 q = j + 1;
115 }
116
7c673cae
FG
117 for (p = 2; p <= MAX_NUM; p <<= 1) {
118
119 if (!rte_is_power_of_2(p))
120 FAIL("rte_is_power_of_2");
121
122 for (i = 1; i <= MAX_NUM; i++) {
123 /* align floor */
124 if (RTE_ALIGN_FLOOR((uintptr_t)i, p) % p)
125 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
126
127 val = RTE_PTR_ALIGN_FLOOR((uintptr_t) i, p);
128 if (ERROR_FLOOR(val, i, p))
129 FAIL_ALIGN("RTE_PTR_ALIGN_FLOOR", i, p);
130
131 val = RTE_ALIGN_FLOOR(i, p);
132 if (ERROR_FLOOR(val, i, p))
133 FAIL_ALIGN("RTE_ALIGN_FLOOR", i, p);
134
135 /* align ceiling */
136 val = RTE_PTR_ALIGN((uintptr_t) i, p);
137 if (ERROR_CEIL(val, i, p))
138 FAIL_ALIGN("RTE_PTR_ALIGN", i, p);
139
140 val = RTE_ALIGN(i, p);
141 if (ERROR_CEIL(val, i, p))
142 FAIL_ALIGN("RTE_ALIGN", i, p);
143
144 val = RTE_ALIGN_CEIL(i, p);
145 if (ERROR_CEIL(val, i, p))
146 FAIL_ALIGN("RTE_ALIGN_CEIL", i, p);
147
148 val = RTE_PTR_ALIGN_CEIL((uintptr_t)i, p);
149 if (ERROR_CEIL(val, i, p))
150 FAIL_ALIGN("RTE_PTR_ALIGN_CEIL", i, p);
151
152 /* by this point we know that val is aligned to p */
153 if (!rte_is_aligned((void*)(uintptr_t) val, p))
154 FAIL("rte_is_aligned");
155 }
156 }
11fdf7f2
TL
157
158 for (p = 1; p <= MAX_NUM / 2; p++) {
159 for (i = 1; i <= MAX_NUM / 2; i++) {
160 val = RTE_ALIGN_MUL_CEIL(i, p);
161 if (val % p != 0 || val < i)
162 FAIL_ALIGN("RTE_ALIGN_MUL_CEIL", i, p);
163 val = RTE_ALIGN_MUL_FLOOR(i, p);
164 if (val % p != 0 || val > i)
165 FAIL_ALIGN("RTE_ALIGN_MUL_FLOOR", i, p);
166 }
167 }
168
169 return 0;
170}
171
172static int
173test_log2(void)
174{
175 uint32_t i, base, compare;
176 const uint32_t max = 0x10000;
177 const uint32_t step = 1;
178
179 for (i = 0; i < max; i = i + step) {
180 base = (uint32_t)ceilf(log2((uint32_t)i));
181 compare = rte_log2_u32(i);
182 if (base != compare) {
183 printf("Wrong rte_log2_u32(%x) val %x, expected %x\n",
184 i, compare, base);
185 return TEST_FAILED;
186 }
187 }
7c673cae
FG
188 return 0;
189}
190
191static int
192test_common(void)
193{
194 int ret = 0;
195 ret |= test_align();
196 ret |= test_macros(0);
197 ret |= test_misc();
11fdf7f2 198 ret |= test_log2();
7c673cae
FG
199
200 return ret;
201}
202
203REGISTER_TEST_COMMAND(common_autotest, test_common);