comparison crc32-test.c @ 3:6483683ac857 default tip

*: add profiling code too; expand x86 to use all eight XMM registers basically ported verbatim from the assembly
author Paper <paper@tflc.us>
date Mon, 09 Feb 2026 21:30:30 -0500
parents ead9f84d11db
children
comparison
equal deleted inserted replaced
2:ead9f84d11db 3:6483683ac857
1 #include "crc32i.h" 1 #include "crc32i.h"
2 #include <stdio.h> 2 #include <stdio.h>
3 #include <inttypes.h>
4 #include <time.h>
3 5
4 /* Test implementations and make sure they agree with each other */ 6 /* Test implementations and make sure they agree with each other */
5 int crc32_test(void) 7 int crc32_test(void)
6 { 8 {
7 /* Force alignment :) */ 9 /* Force alignment :) */
8 static const __attribute__((__aligned__(CRC32_MAX_ALIGNMENT))) unsigned char testdata[1024] = 10 static const CRC32_ALIGN(CRC32_MAX_ALIGNMENT) unsigned char testdata[(1ul << 23) + 19] =
9 #define DOUBLE(x) x x 11 #define DOUBLE(x) x x
10 DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE("\x01\x02\x04\x08\x10\x20\x40\x80"))))))) 12 DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE(DOUBLE("\x01\x02\x04\x08\x10\x20\x40\x80"))))))))
11 #undef DOUBLE 13 #undef DOUBLE
12 ; 14 ;
13 static const crc32_r_spec crc[] = { 15 static const struct {
14 #define CRC32_IMPL(name) crc32##name##_r, 16 crc32_r_spec f;
17 const char *name;
18 } crc[] = {
19 #define CRC32_IMPL(name) {crc32##name##_r, #name},
15 #include "crc32-impls.h" 20 #include "crc32-impls.h"
16 }; 21 };
17 size_t i; 22 size_t i;
18 23
19 uint32_t crcc = crc32(testdata, sizeof(testdata)); 24 uint32_t crcc = crc32(testdata, sizeof(testdata));
20 25
21 for (i = 0; i < ARRAY_SIZE(crc); i++) { 26 for (i = 0; i < ARRAY_SIZE(crc); i++) {
22 uint32_t thiscrc = ~crc[i](0xFFFFFFFF, testdata, sizeof(testdata)); 27 clock_t start, end;
28
29 start = clock();
30 uint32_t thiscrc = crc[i].f(0xFFFFFFFF, testdata, sizeof(testdata));
31 end = clock();
32
33 printf("%s: took %f secs\n", crc[i].name, (double)(end - start) / CLOCKS_PER_SEC);
34
35 thiscrc = ~thiscrc;
23 36
24 if (thiscrc != crcc) { 37 if (thiscrc != crcc) {
25 fprintf(stderr, "%zu, mismatch: %08" PRIX32 ", %08" PRIx32 "\n", i, crcc, thiscrc); 38 fprintf(stderr, "%s: mismatch: %08" PRIX32 ", %08" PRIx32 "\n", crc[i].name, crcc, thiscrc);
26 return -1; 39 return -1;
27 } 40 }
28 } 41 }
29 42
30 return 0; 43 return 0;