]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/kern/libkern_crc32.c
mandoc: import version 1.14.6
[FreeBSD/FreeBSD.git] / tests / sys / kern / libkern_crc32.c
1 /*
2  * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/param.h>
30 #include <sys/gsb_crc32.h>
31
32 #include <stdint.h>
33
34 #include <atf-c.h>
35
36 #if defined(__amd64__) || defined(__i386__)
37 #include <machine/cpufunc.h>
38 #include <machine/specialreg.h>
39
40 static bool
41 have_sse42(void)
42 {
43         u_int cpu_registers[4];
44
45         do_cpuid(1, cpu_registers);
46
47         return ((cpu_registers[2] & CPUID2_SSE42) != 0);
48 }
49 #endif
50
51 static void
52 check_crc32c(uint32_t expected, uint32_t crc32c, const void *buffer,
53     size_t length)
54 {
55         uint32_t act;
56
57 #if defined(__amd64__) || defined(__i386__)
58         if (have_sse42()) {
59                 act = sse42_crc32c(crc32c, buffer, length);
60                 ATF_CHECK_EQ_MSG(expected, act,
61                     "sse42_crc32c expected 0x%08x, got 0x%08x", expected, act);
62         }
63 #elif defined(__aarch64__)
64         act = armv8_crc32c(crc32c, buffer, length);
65         ATF_CHECK_EQ_MSG(expected, act,
66             "armv8_crc32c expected 0x%08x, got 0x%08x", expected, act);
67 #endif
68         act = singletable_crc32c(crc32c, buffer, length);
69         ATF_CHECK_EQ_MSG(expected, act,
70             "singletable_crc32c expected 0x%08x, got 0x%08x", expected, act);
71         act = multitable_crc32c(crc32c, buffer, length);
72         ATF_CHECK_EQ_MSG(expected, act,
73             "multitable_crc32c expected 0x%08x, got 0x%08x", expected, act);
74 }
75
76 ATF_TC_WITHOUT_HEAD(crc32c_basic_correctness);
77 ATF_TC_BODY(crc32c_basic_correctness, tc)
78 {
79         const uint64_t inputs[] = {
80                 0xf408c634b3a9142,
81                 0x80539e8c7c352e2b,
82                 0x62e9121db6e4d649,
83                 0x899345850ed0a286,
84                 0x2302df11b4a43b15,
85                 0xe943de7b3d35d70,
86                 0xdf1ff2bf41abf56b,
87                 0x9bc138abae315de2,
88                 0x31cc82e56234f0ff,
89                 0xce63c0cd6988e847,
90                 0x3e42f6b78ee352fa,
91                 0xfa4085436078cfa6,
92                 0x53349558bf670a4b,
93                 0x2714e10e7d722c61,
94                 0xc0d3261addfc6908,
95                 0xd1567c3181d3a1bf,
96         };
97         const uint32_t results[] = {
98                 0x2ce33ede,
99                 0xc49cc573,
100                 0xb8683c96,
101                 0x6918660d,
102                 0xa904e522,
103                 0x52dbc42c,
104                 0x98863c22,
105                 0x894d5d2c,
106                 0xb003745d,
107                 0xfc496dbd,
108                 0x97d2fbb5,
109                 0x3c062ef1,
110                 0xcc2eff18,
111                 0x6a9b09f6,
112                 0x420242c1,
113                 0xfd562dc3,
114         };
115         size_t i;
116
117         ATF_REQUIRE(nitems(inputs) == nitems(results));
118
119         for (i = 0; i < nitems(inputs); i++) {
120                 check_crc32c(results[i], ~0u, &inputs[i], sizeof(inputs[0]));
121         }
122 }
123
124 ATF_TC_WITHOUT_HEAD(crc32c_alignment);
125 ATF_TC_BODY(crc32c_alignment, tc)
126 {
127         const uint64_t input = 0xf408c634b3a9142;
128         const uint32_t result = 0x2ce33ede;
129         unsigned char buf[15];
130         size_t i;
131
132         for (i = 1; i < 8; i++) {
133                 memcpy(&buf[i], &input, sizeof(input));
134                 check_crc32c(result, ~0u, &buf[i], sizeof(input));
135         }
136 }
137
138 ATF_TC_WITHOUT_HEAD(crc32c_trailing_bytes);
139 ATF_TC_BODY(crc32c_trailing_bytes, tc)
140 {
141         const unsigned char input[] = {
142                 0x87, 0x54, 0x74, 0xd2, 0xb, 0x9b, 0xdd, 0xf6, 0x68, 0x37,
143                 0xd4, 0x4, 0x5e, 0xa9, 0xb3
144         };
145         const uint32_t result = 0xec638d62;
146
147         check_crc32c(result, ~0u, input, sizeof(input));
148 }
149
150 ATF_TP_ADD_TCS(tp)
151 {
152
153         ATF_TP_ADD_TC(tp, crc32c_basic_correctness);
154         ATF_TP_ADD_TC(tp, crc32c_alignment);
155         ATF_TP_ADD_TC(tp, crc32c_trailing_bytes);
156         return (atf_no_error());
157 }