]>
Commit | Line | Data |
---|---|---|
99a5535c DW |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* | |
3 | * Aug 8, 2011 Bob Pearson with help from Joakim Tjernlund and George Spelvin | |
4 | * cleaned up code to current version of sparse and added the slicing-by-8 | |
5 | * algorithm to the closely similar existing slicing-by-4 algorithm. | |
6 | * Oct 15, 2000 Matt Domsch <Matt_Domsch@dell.com> | |
7 | * Nicer crc32 functions/docs submitted by linux@horizon.com. Thanks! | |
8 | * Code was from the public domain, copyright abandoned. Code was | |
9 | * subsequently included in the kernel, thus was re-licensed under the | |
10 | * GNU GPL v2. | |
11 | * Oct 12, 2000 Matt Domsch <Matt_Domsch@dell.com> | |
12 | * Same crc32 function was used in 5 other places in the kernel. | |
13 | * I made one version, and deleted the others. | |
14 | * There are various incantations of crc32(). Some use a seed of 0 or ~0. | |
15 | * Some xor at the end with ~0. The generic crc32() function takes | |
16 | * seed as an argument, and doesn't xor at the end. Then individual | |
17 | * users can do whatever they need. | |
18 | * drivers/net/smc9194.c uses seed ~0, doesn't xor with ~0. | |
19 | * fs/jffs2 uses seed 0, doesn't xor with ~0. | |
20 | * fs/partitions/efi.c uses seed ~0, xor's with ~0. | |
21 | */ | |
22 | ||
23 | /* see: Documentation/crc32.txt for a description of algorithms */ | |
24 | ||
25 | /* | |
26 | * lifted from the 3.8-rc2 kernel source for xfsprogs. Killed CONFIG_X86 | |
27 | * specific bits for just the generic algorithm. Also removed the big endian | |
28 | * version of the algorithm as XFS only uses the little endian CRC version to | |
29 | * match the hardware acceleration available on Intel CPUs. | |
30 | */ | |
31 | ||
32 | /* This is just the crc32 self test bits from crc32.c. */ | |
d8a19f29 | 33 | #include "libfrog/randbytes.h" |
99a5535c | 34 | |
63153a95 DW |
35 | #ifndef __LIBFROG_CRC32CSELFTEST_H__ |
36 | #define __LIBFROG_CRC32CSELFTEST_H__ | |
99a5535c | 37 | |
99a5535c DW |
38 | /* 100 test cases */ |
39 | static struct crc_test { | |
40 | uint32_t crc; /* random starting crc */ | |
41 | uint32_t start; /* random 6 bit offset in buf */ | |
42 | uint32_t length; /* random 11 bit length of test */ | |
43 | uint32_t crc32c_le; /* expected crc32c_le result */ | |
b9d29568 | 44 | } crc_tests[] = |
99a5535c DW |
45 | { |
46 | {0x674bf11d, 0x00000038, 0x00000542, 0xf6e93d6c}, | |
47 | {0x35c672c6, 0x0000003a, 0x000001aa, 0x0fe92aca}, | |
48 | {0x496da28e, 0x00000039, 0x000005af, 0x52e1ebb8}, | |
49 | {0x09a9b90e, 0x00000027, 0x000001f8, 0x0798af9a}, | |
50 | {0xdc97e5a9, 0x00000025, 0x000003b6, 0x18eb3152}, | |
51 | {0x47c58900, 0x0000000a, 0x000000b9, 0xd00d08c7}, | |
52 | {0x292561e8, 0x0000000c, 0x00000403, 0x8ba966bc}, | |
53 | {0x415037f6, 0x00000003, 0x00000676, 0x11d694a2}, | |
54 | {0x3466e707, 0x00000026, 0x00000042, 0x6ab3208d}, | |
55 | {0xafd1281b, 0x00000023, 0x000002ee, 0xba4603c5}, | |
56 | {0xd3857b18, 0x00000028, 0x000004a2, 0xe6071c6f}, | |
57 | {0x1d825a8f, 0x0000002b, 0x0000050b, 0x179ec30a}, | |
58 | {0x5033e3bc, 0x0000000b, 0x00000078, 0x0903beb8}, | |
59 | {0x94f1fb5e, 0x0000000f, 0x000003a2, 0x6a7cb4fa}, | |
60 | {0xc9a0fe14, 0x00000009, 0x00000473, 0xdb535801}, | |
61 | {0x88a034b1, 0x0000001c, 0x000005ad, 0x92bed597}, | |
62 | {0xf0f72239, 0x00000020, 0x0000026d, 0x192a3f1b}, | |
63 | {0xcc20a5e3, 0x0000003b, 0x0000067a, 0xccbaec1a}, | |
64 | {0xce589c95, 0x0000002b, 0x00000641, 0x7eabae4d}, | |
65 | {0x78edc885, 0x00000035, 0x000005be, 0x28c72982}, | |
66 | {0x9d40a377, 0x0000003b, 0x00000038, 0xc3cd4d18}, | |
67 | {0x703d0e01, 0x0000003c, 0x000006f1, 0xbca8f0e7}, | |
68 | {0x776bf505, 0x0000000f, 0x000005b2, 0x713f60b3}, | |
69 | {0x4a3e7854, 0x00000027, 0x000004b8, 0xebd08fd5}, | |
70 | {0x209172dd, 0x0000003b, 0x00000356, 0x64406c59}, | |
71 | {0x3ba4cc5b, 0x0000002f, 0x00000203, 0x7421890e}, | |
72 | {0xfc62f297, 0x00000000, 0x00000079, 0xe9347603}, | |
73 | {0x64280b8b, 0x00000016, 0x000007ab, 0x1bef9060}, | |
74 | {0x97dd724b, 0x00000033, 0x000007ad, 0x34720072}, | |
75 | {0x61394b52, 0x00000035, 0x00000571, 0x48310f59}, | |
76 | {0x29b4faff, 0x00000024, 0x0000006e, 0x783a4213}, | |
77 | {0x29bfb1dc, 0x0000000b, 0x00000244, 0x9e8efd41}, | |
78 | {0x86ae934b, 0x00000035, 0x00000104, 0xfc3d34a5}, | |
79 | {0xc4c1024e, 0x0000002e, 0x000006b1, 0x17a52ae2}, | |
80 | {0x3287a80a, 0x00000026, 0x00000496, 0x886d935a}, | |
81 | {0xa4db423e, 0x00000023, 0x0000045d, 0xeaaeaeb2}, | |
82 | {0x7a1078df, 0x00000015, 0x0000014a, 0x8e900a4b}, | |
83 | {0x6048bd5b, 0x00000006, 0x0000006a, 0xd74662b1}, | |
84 | {0xd8f9ea20, 0x0000003d, 0x00000277, 0xd26752ba}, | |
85 | {0xea5ec3b4, 0x0000002a, 0x000004fe, 0x8b1fcd62}, | |
86 | {0x2dfb005d, 0x00000016, 0x00000345, 0xf54342fe}, | |
87 | {0x5a214ade, 0x00000020, 0x000005b6, 0x5b95b988}, | |
88 | {0xf0ab9cca, 0x00000032, 0x00000515, 0x2e1176be}, | |
89 | {0x91b444f9, 0x0000002e, 0x000007f8, 0x66120546}, | |
90 | {0x1b5d2ddb, 0x0000002e, 0x0000012c, 0xf256a5cc}, | |
91 | {0xd824d1bb, 0x0000003a, 0x000007b5, 0x4af1dd69}, | |
92 | {0x0470180c, 0x00000034, 0x000001f0, 0x56f0a04a}, | |
93 | {0xffaa3a3f, 0x00000036, 0x00000299, 0x74f6b6b2}, | |
94 | {0x6406cfeb, 0x00000023, 0x00000600, 0x085951fd}, | |
95 | {0xb24aaa38, 0x0000003e, 0x000004a1, 0xc65387eb}, | |
96 | {0x58b2ab7c, 0x00000039, 0x000002b4, 0x1ca9257b}, | |
97 | {0x3db85970, 0x00000006, 0x000002b6, 0xfd196d76}, | |
98 | {0x857830c5, 0x00000003, 0x00000590, 0x5ef88339}, | |
99 | {0xe1fcd978, 0x0000003e, 0x000007d8, 0x2c3714d9}, | |
100 | {0xb982a768, 0x00000016, 0x000006e0, 0x58576548}, | |
101 | {0x1d581ce8, 0x0000001e, 0x0000058b, 0xfd7c57de}, | |
102 | {0x2456719b, 0x00000025, 0x00000503, 0xd5fedd59}, | |
103 | {0xfae6d8f2, 0x00000000, 0x0000055d, 0x1cc3b17b}, | |
104 | {0xcba828e3, 0x00000039, 0x000002ce, 0x270eed73}, | |
105 | {0x13d25952, 0x0000000a, 0x0000072d, 0x91ecbb11}, | |
106 | {0x0342be3f, 0x00000015, 0x00000599, 0x05ed8d0c}, | |
107 | {0xeaa344e0, 0x00000014, 0x000004d8, 0x0b09ad5b}, | |
108 | {0xbbb52021, 0x0000003b, 0x00000272, 0xf8d511fb}, | |
109 | {0xb66384dc, 0x0000001d, 0x000007fc, 0x5ad832cc}, | |
110 | {0x616c01b6, 0x00000022, 0x000002c8, 0x1214d196}, | |
111 | {0xce2bdaad, 0x00000016, 0x0000062a, 0x5747218a}, | |
112 | {0x00fe84d7, 0x00000005, 0x00000205, 0xde8f14de}, | |
113 | {0xbebdcb4c, 0x00000006, 0x0000055d, 0x3563b7b9}, | |
114 | {0xd8b1a02a, 0x00000010, 0x00000387, 0x071475d0}, | |
115 | {0x3b96cad2, 0x00000036, 0x00000347, 0x54c79d60}, | |
116 | {0xc94c1ed7, 0x00000005, 0x0000038b, 0x4c53eee6}, | |
117 | {0x1aad454e, 0x00000025, 0x000002b2, 0x10137a3c}, | |
118 | {0xa4fec9a6, 0x00000000, 0x000006d6, 0xaa9d6c73}, | |
119 | {0x1bbe71e2, 0x0000001f, 0x000002fd, 0xb63d23e7}, | |
120 | {0x4201c7e4, 0x00000002, 0x000002b7, 0x7f53e9cf}, | |
121 | {0x23fddc96, 0x00000003, 0x00000627, 0x13c1cd83}, | |
122 | {0xd82ba25c, 0x00000016, 0x0000063e, 0x49ff5867}, | |
123 | {0x786f2032, 0x0000002d, 0x0000060f, 0x8467f211}, | |
124 | {0xfebe4e1f, 0x0000002a, 0x000004f2, 0x3f9683b2}, | |
125 | {0x1a6e0a39, 0x00000008, 0x00000672, 0x76a3f874}, | |
126 | {0x56000ab8, 0x0000000e, 0x000000e5, 0x863b702f}, | |
127 | {0x4717fe0c, 0x00000000, 0x000006ec, 0xdc6c58ff}, | |
128 | {0xd5d5d68e, 0x0000003c, 0x000003a3, 0x0622cc95}, | |
129 | {0xc25dd6c6, 0x00000024, 0x000006c0, 0xe85605cd}, | |
130 | {0xe9b11300, 0x00000023, 0x00000683, 0x31da5f06}, | |
131 | {0x95cd285e, 0x00000001, 0x00000047, 0xa1f2e784}, | |
132 | {0xd9245a25, 0x0000001e, 0x000003a6, 0xb07cc616}, | |
133 | {0x103279db, 0x00000006, 0x0000039b, 0xbf943b6c}, | |
134 | {0x1cba3172, 0x00000027, 0x000001c8, 0x2c01af1c}, | |
135 | {0x8f613739, 0x0000000c, 0x000001df, 0x0fe5f56d}, | |
136 | {0x1c6aa90d, 0x0000001b, 0x0000053c, 0xf8943b2d}, | |
137 | {0xaabe5b93, 0x0000003d, 0x00000715, 0xe4d89272}, | |
138 | {0xf15dd038, 0x00000006, 0x000006db, 0x7c2f6bbb}, | |
139 | {0x584dd49c, 0x00000020, 0x000007bc, 0xabbf388b}, | |
140 | {0x5d8c9506, 0x00000020, 0x00000470, 0x1dca1f4e}, | |
141 | {0xb80d17b0, 0x00000032, 0x00000346, 0x5c170e23}, | |
142 | {0xdaf0592e, 0x00000023, 0x000007b0, 0xc0e9d672}, | |
143 | {0x4793cc85, 0x0000000d, 0x00000706, 0xc18bdc86}, | |
144 | {0x82ebf64e, 0x00000009, 0x000007c3, 0xa874fcdd}, | |
145 | {0xb18a0319, 0x00000026, 0x000007db, 0x9dc0bb48}, | |
146 | }; | |
147 | ||
ca14a570 DW |
148 | /* Don't print anything to stdout. */ |
149 | #define CRC32CTEST_QUIET (1U << 0) | |
150 | ||
99a5535c | 151 | static int |
ca14a570 DW |
152 | crc32c_test( |
153 | unsigned int flags) | |
99a5535c | 154 | { |
ca14a570 DW |
155 | int i; |
156 | int errors = 0; | |
157 | int bytes = 0; | |
158 | struct timeval start, stop; | |
159 | uint64_t usec; | |
99a5535c DW |
160 | |
161 | /* keep static to prevent cache warming code from | |
162 | * getting eliminated by the compiler */ | |
ca14a570 | 163 | static uint32_t crc; |
99a5535c DW |
164 | |
165 | /* pre-warm the cache */ | |
166 | for (i = 0; i < 100; i++) { | |
b9d29568 | 167 | bytes += 2 * crc_tests[i].length; |
99a5535c | 168 | |
b9d29568 DW |
169 | crc ^= crc32c_le(crc_tests[i].crc, |
170 | randbytes_test_buf + crc_tests[i].start, | |
171 | crc_tests[i].length); | |
99a5535c DW |
172 | } |
173 | ||
174 | gettimeofday(&start, NULL); | |
175 | for (i = 0; i < 100; i++) { | |
b9d29568 DW |
176 | crc = crc32c_le(crc_tests[i].crc, |
177 | randbytes_test_buf + crc_tests[i].start, | |
178 | crc_tests[i].length); | |
179 | if (crc != crc_tests[i].crc32c_le) | |
99a5535c DW |
180 | errors++; |
181 | } | |
182 | gettimeofday(&stop, NULL); | |
183 | ||
184 | usec = stop.tv_usec - start.tv_usec + | |
185 | 1000000 * (stop.tv_sec - start.tv_sec); | |
186 | ||
ca14a570 DW |
187 | if (flags & CRC32CTEST_QUIET) |
188 | return errors; | |
189 | ||
99a5535c DW |
190 | if (errors) |
191 | printf("crc32c: %d self tests failed\n", errors); | |
192 | else { | |
193 | printf("crc32c: tests passed, %d bytes in %" PRIu64 " usec\n", | |
194 | bytes, usec); | |
195 | } | |
196 | ||
197 | return errors; | |
198 | } | |
199 | ||
63153a95 | 200 | #endif /* __LIBFROG_CRC32CSELFTEST_H__ */ |