]> git.proxmox.com Git - ceph.git/blame - ceph/src/isa-l/crc/crc64_funcs_test.c
Import ceph 15.2.8
[ceph.git] / ceph / src / isa-l / crc / crc64_funcs_test.c
CommitLineData
224ce89b
WB
1/**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28**********************************************************************/
29
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <stdint.h>
34#include "crc64.h"
35#include "types.h"
f91f0fd5 36#include "crc64_ref.h"
224ce89b
WB
37
38#ifndef TEST_SEED
39# define TEST_SEED 0x1234
40#endif
41
f91f0fd5
TL
42#define MAX_BUF 4096
43#define TEST_SIZE 32
224ce89b
WB
44
45typedef uint64_t u64;
46typedef uint32_t u32;
47typedef uint16_t u16;
48typedef uint8_t u8;
49
50typedef uint64_t(*crc64_func_t) (uint64_t, const uint8_t *, uint64_t);
51
52typedef struct func_case {
53 char *note;
54 crc64_func_t crc64_func_call;
f91f0fd5 55 crc64_func_t crc64_base_call;
224ce89b
WB
56 crc64_func_t crc64_ref_call;
57} func_case_t;
58
59func_case_t test_funcs[] = {
f91f0fd5
TL
60 {"crc64_ecma_norm", crc64_ecma_norm, crc64_ecma_norm_base, crc64_ecma_norm_ref},
61 {"crc64_ecma_refl", crc64_ecma_refl, crc64_ecma_refl_base, crc64_ecma_refl_ref},
62 {"crc64_iso_norm", crc64_iso_norm, crc64_iso_norm_base, crc64_iso_norm_ref},
63 {"crc64_iso_refl", crc64_iso_refl, crc64_iso_refl_base, crc64_iso_refl_ref},
64 {"crc64_jones_norm", crc64_jones_norm, crc64_jones_norm_base,
65 crc64_jones_norm_ref},
66 {"crc64_jones_refl", crc64_jones_refl, crc64_jones_refl_base, crc64_jones_refl_ref}
224ce89b
WB
67};
68
69// Generates pseudo-random data
70
71void rand_buffer(unsigned char *buf, long buffer_size)
72{
73 long i;
74 for (i = 0; i < buffer_size; i++)
75 buf[i] = rand();
76}
77
78// Test cases
79int zeros_test(func_case_t * test_func);
80
81int simple_pattern_test(func_case_t * test_func);
82
83int seeds_sizes_test(func_case_t * test_func);
84
85int eob_test(func_case_t * test_func);
86
87int update_test(func_case_t * test_func);
88
89int verbose = 0;
90void *buf_alloc = NULL;
91
92int main(int argc, char *argv[])
93{
94 int fail = 0, fail_case;
95 int i, ret;
96 func_case_t *test_func;
97
98 verbose = argc - 1;
99
f91f0fd5
TL
100 // Align to 32B boundary
101 ret = posix_memalign(&buf_alloc, TEST_SIZE, MAX_BUF * TEST_SIZE);
224ce89b
WB
102 if (ret) {
103 printf("alloc error: Fail");
104 return -1;
105 }
106 srand(TEST_SEED);
107 printf("CRC64 Tests\n");
108
109 for (i = 0; i < sizeof(test_funcs) / sizeof(test_funcs[0]); i++) {
110 fail_case = 0;
111 test_func = &test_funcs[i];
112
f91f0fd5 113 printf("Test %s\t", test_func->note);
224ce89b
WB
114 fail_case += zeros_test(test_func);
115 fail_case += simple_pattern_test(test_func);
116 fail_case += seeds_sizes_test(test_func);
117 fail_case += eob_test(test_func);
118 fail_case += update_test(test_func);
f91f0fd5 119 printf(" done: %s\n", fail_case ? "Fail" : "Pass");
224ce89b
WB
120
121 if (fail_case) {
122 printf("\n%s Failed %d tests\n", test_func->note, fail_case);
123 fail++;
124 }
125 }
126
127 printf("CRC64 Tests all done: %s\n", fail ? "Fail" : "Pass");
128
129 return fail;
130}
131
132// Test of all zeros
133int zeros_test(func_case_t * test_func)
134{
f91f0fd5 135 uint64_t crc_ref, crc_base, crc;
224ce89b
WB
136 int fail = 0;
137 unsigned char *buf = NULL;
138
139 buf = (unsigned char *)buf_alloc;
140 memset(buf, 0, MAX_BUF * 10);
224ce89b 141 crc_ref = test_func->crc64_ref_call(TEST_SEED, buf, MAX_BUF * 10);
f91f0fd5
TL
142 crc_base = test_func->crc64_base_call(TEST_SEED, buf, MAX_BUF * 10);
143 crc = test_func->crc64_func_call(TEST_SEED, buf, MAX_BUF * 10);
224ce89b 144
f91f0fd5 145 if ((crc_base != crc_ref) || (crc != crc_ref)) {
224ce89b
WB
146 fail++;
147 printf("\n opt ref\n");
148 printf(" ------ ------\n");
f91f0fd5 149 printf("crc zero = 0x%16lx 0x%16lx 0x%16lx \n", crc_ref, crc_base, crc);
224ce89b
WB
150 } else
151 printf(".");
152
153 return fail;
154}
155
156// Another simple test pattern
157int simple_pattern_test(func_case_t * test_func)
158{
f91f0fd5 159 uint64_t crc_ref, crc_base, crc;
224ce89b
WB
160 int fail = 0;
161 unsigned char *buf = NULL;
162
163 buf = (unsigned char *)buf_alloc;
164 memset(buf, 0x8a, MAX_BUF);
224ce89b 165 crc_ref = test_func->crc64_ref_call(TEST_SEED, buf, MAX_BUF);
f91f0fd5
TL
166 crc_base = test_func->crc64_base_call(TEST_SEED, buf, MAX_BUF);
167 crc = test_func->crc64_func_call(TEST_SEED, buf, MAX_BUF);
168
169 if ((crc_base != crc_ref) || (crc != crc_ref))
224ce89b
WB
170 fail++;
171 if (verbose)
f91f0fd5 172 printf("crc all 8a = 0x%16lx 0x%16lx 0x%16lx\n", crc_ref, crc_base, crc);
224ce89b
WB
173 else
174 printf(".");
175
176 return fail;
177}
178
179int seeds_sizes_test(func_case_t * test_func)
180{
f91f0fd5 181 uint64_t crc_ref, crc_base, crc;
224ce89b
WB
182 int fail = 0;
183 int i;
184 uint64_t r, s;
185 unsigned char *buf = NULL;
186
187 // Do a few random tests
188 buf = (unsigned char *)buf_alloc; //reset buf
189 r = rand();
190 rand_buffer(buf, MAX_BUF * TEST_SIZE);
191
192 for (i = 0; i < TEST_SIZE; i++) {
224ce89b 193 crc_ref = test_func->crc64_ref_call(r, buf, MAX_BUF);
f91f0fd5
TL
194 crc_base = test_func->crc64_base_call(r, buf, MAX_BUF);
195 crc = test_func->crc64_func_call(r, buf, MAX_BUF);
196
197 if ((crc_base != crc_ref) || (crc != crc_ref))
224ce89b
WB
198 fail++;
199 if (verbose)
f91f0fd5
TL
200 printf("crc rand%3d = 0x%16lx 0x%16lx 0x%16lx\n", i, crc_ref, crc_base,
201 crc);
202 else if (i % (TEST_SIZE / 8) == 0)
224ce89b
WB
203 printf(".");
204 buf += MAX_BUF;
205 }
206
207 // Do a few random sizes
208 buf = (unsigned char *)buf_alloc; //reset buf
209 r = rand();
210
211 for (i = MAX_BUF; i >= 0; i--) {
224ce89b 212 crc_ref = test_func->crc64_ref_call(r, buf, i);
f91f0fd5
TL
213 crc_base = test_func->crc64_base_call(r, buf, i);
214 crc = test_func->crc64_func_call(r, buf, i);
215
216 if ((crc_base != crc_ref) || (crc != crc_ref)) {
224ce89b 217 fail++;
f91f0fd5
TL
218 printf("fail random size%i 0x%16lx 0x%16lx 0x%16lx\n", i, crc_ref,
219 crc_base, crc);
220 } else if (i % (MAX_BUF / 8) == 0)
224ce89b
WB
221 printf(".");
222 }
223
224 // Try different seeds
225 for (s = 0; s < 20; s++) {
226 buf = (unsigned char *)buf_alloc; //reset buf
227
228 r = rand(); // just to get a new seed
229 rand_buffer(buf, MAX_BUF * TEST_SIZE); // new pseudo-rand data
230
231 if (verbose)
232 printf("seed = 0x%lx\n", r);
233
234 for (i = 0; i < TEST_SIZE; i++) {
224ce89b 235 crc_ref = test_func->crc64_ref_call(r, buf, MAX_BUF);
f91f0fd5
TL
236 crc_base = test_func->crc64_base_call(r, buf, MAX_BUF);
237 crc = test_func->crc64_func_call(r, buf, MAX_BUF);
238
239 if ((crc_base != crc_ref) || (crc != crc_ref))
224ce89b
WB
240 fail++;
241 if (verbose)
f91f0fd5
TL
242 printf("crc rand%3d = 0x%16lx 0x%16lx 0x%16lx\n", i, crc_ref,
243 crc_base, crc);
244 else if (i % (TEST_SIZE * 20 / 8) == 0)
224ce89b
WB
245 printf(".");
246 buf += MAX_BUF;
247 }
248 }
249
250 return fail;
251}
252
253// Run tests at end of buffer
254int eob_test(func_case_t * test_func)
255{
f91f0fd5 256 uint64_t crc_ref, crc_base, crc;
224ce89b
WB
257 int fail = 0;
258 int i;
259 unsigned char *buf = NULL;
260
f91f0fd5
TL
261 // Null test
262 if (0 != test_func->crc64_func_call(0, NULL, 0)) {
263 fail++;
264 printf("crc null test fail\n");
265 }
266
224ce89b
WB
267 buf = (unsigned char *)buf_alloc; //reset buf
268 buf = buf + ((MAX_BUF - 1) * TEST_SIZE); //Line up TEST_SIZE from end
f91f0fd5 269 for (i = 0; i <= TEST_SIZE; i++) {
224ce89b 270 crc_ref = test_func->crc64_ref_call(TEST_SEED, buf + i, TEST_SIZE - i);
f91f0fd5
TL
271 crc_base = test_func->crc64_base_call(TEST_SEED, buf + i, TEST_SIZE - i);
272 crc = test_func->crc64_func_call(TEST_SEED, buf + i, TEST_SIZE - i);
273
274 if ((crc_base != crc_ref) || (crc != crc_ref))
224ce89b
WB
275 fail++;
276 if (verbose)
f91f0fd5
TL
277 printf("crc eob rand%3d = 0x%16lx 0x%16lx 0x%16lx\n", i, crc_ref,
278 crc_base, crc);
279 else if (i % (TEST_SIZE / 8) == 0)
224ce89b
WB
280 printf(".");
281 }
282
283 return fail;
284}
285
286int update_test(func_case_t * test_func)
287{
f91f0fd5 288 uint64_t crc_ref, crc_base, crc;
224ce89b
WB
289 int fail = 0;
290 int i;
291 uint64_t r;
292 unsigned char *buf = NULL;
293
294 buf = (unsigned char *)buf_alloc; //reset buf
295 r = rand();
296 // Process the whole buf with reference func single call.
297 crc_ref = test_func->crc64_ref_call(r, buf, MAX_BUF * TEST_SIZE);
f91f0fd5 298 crc_base = test_func->crc64_base_call(r, buf, MAX_BUF * TEST_SIZE);
224ce89b
WB
299 // Process buf with update method.
300 for (i = 0; i < TEST_SIZE; i++) {
301 crc = test_func->crc64_func_call(r, buf, MAX_BUF);
302 // Update crc seeds and buf pointer.
303 r = crc;
304 buf += MAX_BUF;
305 }
306
f91f0fd5 307 if ((crc_base != crc_ref) || (crc != crc_ref))
224ce89b
WB
308 fail++;
309 if (verbose)
f91f0fd5 310 printf("crc rand%3d = 0x%16lx 0x%16lx 0x%16lx\n", i, crc_ref, crc_base, crc);
224ce89b
WB
311 else
312 printf(".");
313
314 return fail;
315}