xref: /freebsd/tests/sys/kern/libkern_crc32.c (revision e17f5b1d)
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__) && !defined(__aarch64__)
37 #error These tests are not supported on this platform
38 #endif
39 
40 ATF_TC_WITHOUT_HEAD(crc32c_basic_correctness);
41 ATF_TC_BODY(crc32c_basic_correctness, tc)
42 {
43 	const uint64_t inputs[] = {
44 		0xf408c634b3a9142,
45 		0x80539e8c7c352e2b,
46 		0x62e9121db6e4d649,
47 		0x899345850ed0a286,
48 		0x2302df11b4a43b15,
49 		0xe943de7b3d35d70,
50 		0xdf1ff2bf41abf56b,
51 		0x9bc138abae315de2,
52 		0x31cc82e56234f0ff,
53 		0xce63c0cd6988e847,
54 		0x3e42f6b78ee352fa,
55 		0xfa4085436078cfa6,
56 		0x53349558bf670a4b,
57 		0x2714e10e7d722c61,
58 		0xc0d3261addfc6908,
59 		0xd1567c3181d3a1bf,
60 	};
61 	const uint32_t results[] = {
62 		0x2ce33ede,
63 		0xc49cc573,
64 		0xb8683c96,
65 		0x6918660d,
66 		0xa904e522,
67 		0x52dbc42c,
68 		0x98863c22,
69 		0x894d5d2c,
70 		0xb003745d,
71 		0xfc496dbd,
72 		0x97d2fbb5,
73 		0x3c062ef1,
74 		0xcc2eff18,
75 		0x6a9b09f6,
76 		0x420242c1,
77 		0xfd562dc3,
78 	};
79 	size_t i;
80 	uint32_t act;
81 
82 	ATF_REQUIRE(nitems(inputs) == nitems(results));
83 
84 	for (i = 0; i < nitems(inputs); i++) {
85 #if defined(__amd64__) || defined(__i386__)
86 		act = sse42_crc32c(~0, (const void *)&inputs[i],
87 		    sizeof(inputs[0]));
88 #else
89 		act = armv8_crc32c(~0, (const void *)&inputs[i],
90 		    sizeof(inputs[0]));
91 #endif
92 		ATF_REQUIRE_MSG(act == results[i],
93 		    "crc32c(0x%jx) = 0x%08x, got 0x%08x", (uintmax_t)inputs[i],
94 		    results[i], act);
95 	}
96 }
97 
98 ATF_TC_WITHOUT_HEAD(crc32c_alignment);
99 ATF_TC_BODY(crc32c_alignment, tc)
100 {
101 	const uint64_t input = 0xf408c634b3a9142;
102 	const uint32_t result = 0x2ce33ede;
103 	unsigned char buf[15];
104 	size_t i;
105 	uint32_t act;
106 
107 
108 	for (i = 1; i < 8; i++) {
109 		memcpy(&buf[i], &input, sizeof(input));
110 
111 #if defined(__amd64__) || defined(__i386__)
112 		act = sse42_crc32c(~0, (const void *)&buf[i], sizeof(input));
113 #else
114 		act = armv8_crc32c(~0, (const void *)&buf[i], sizeof(input));
115 #endif
116 		ATF_REQUIRE_MSG(act == result,
117 		    "crc32c(0x%jx) = 0x%08x, got 0x%08x", (uintmax_t)input,
118 		    result, act);
119 	}
120 }
121 
122 ATF_TC_WITHOUT_HEAD(crc32c_trailing_bytes);
123 ATF_TC_BODY(crc32c_trailing_bytes, tc)
124 {
125 	const unsigned char input[] = {
126 		0x87, 0x54, 0x74, 0xd2, 0xb, 0x9b, 0xdd, 0xf6, 0x68, 0x37,
127 		0xd4, 0x4, 0x5e, 0xa9, 0xb3
128 	};
129 	const uint32_t result = 0xec638d62;
130 	uint32_t act;
131 
132 #if defined(__amd64__) || defined(__i386__)
133 	act = sse42_crc32c(~0, input, sizeof(input));
134 #else
135 	act = armv8_crc32c(~0, input, sizeof(input));
136 #endif
137 	ATF_REQUIRE_MSG(act == result, "expected 0x%08x, got 0x%08x", result,
138 	    act);
139 }
140 
141 ATF_TP_ADD_TCS(tp)
142 {
143 
144 	ATF_TP_ADD_TC(tp, crc32c_basic_correctness);
145 	ATF_TP_ADD_TC(tp, crc32c_alignment);
146 	ATF_TP_ADD_TC(tp, crc32c_trailing_bytes);
147 	return (atf_no_error());
148 }
149