2 * Copyright (c) 2017 Conrad Meyer <cem@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
35 #if defined(__amd64__) || defined(__i386__)
36 extern uint32_t sse42_crc32c(uint32_t, const unsigned char *, unsigned);
37 #elif defined(__aarch64__)
38 extern uint32_t armv8_crc32c(uint32_t, const unsigned char *, unsigned);
40 #error These tests are not supported on this platform
43 ATF_TC_WITHOUT_HEAD(crc32c_basic_correctness);
44 ATF_TC_BODY(crc32c_basic_correctness, tc)
46 const uint64_t inputs[] = {
64 const uint32_t results[] = {
85 ATF_REQUIRE(nitems(inputs) == nitems(results));
87 for (i = 0; i < nitems(inputs); i++) {
88 #if defined(__amd64__) || defined(__i386__)
89 act = sse42_crc32c(~0, (const void *)&inputs[i],
92 act = armv8_crc32c(~0, (const void *)&inputs[i],
95 ATF_REQUIRE_MSG(act == results[i],
96 "crc32c(0x%jx) = 0x%08x, got 0x%08x", (uintmax_t)inputs[i],
101 ATF_TC_WITHOUT_HEAD(crc32c_alignment);
102 ATF_TC_BODY(crc32c_alignment, tc)
104 const uint64_t input = 0xf408c634b3a9142;
105 const uint32_t result = 0x2ce33ede;
106 unsigned char buf[15];
111 for (i = 1; i < 8; i++) {
112 memcpy(&buf[i], &input, sizeof(input));
114 #if defined(__amd64__) || defined(__i386__)
115 act = sse42_crc32c(~0, (const void *)&buf[i], sizeof(input));
117 act = armv8_crc32c(~0, (const void *)&buf[i], sizeof(input));
119 ATF_REQUIRE_MSG(act == result,
120 "crc32c(0x%jx) = 0x%08x, got 0x%08x", (uintmax_t)input,
125 ATF_TC_WITHOUT_HEAD(crc32c_trailing_bytes);
126 ATF_TC_BODY(crc32c_trailing_bytes, tc)
128 const unsigned char input[] = {
129 0x87, 0x54, 0x74, 0xd2, 0xb, 0x9b, 0xdd, 0xf6, 0x68, 0x37,
130 0xd4, 0x4, 0x5e, 0xa9, 0xb3
132 const uint32_t result = 0xec638d62;
135 #if defined(__amd64__) || defined(__i386__)
136 act = sse42_crc32c(~0, input, sizeof(input));
138 act = armv8_crc32c(~0, input, sizeof(input));
140 ATF_REQUIRE_MSG(act == result, "expected 0x%08x, got 0x%08x", result,
147 ATF_TP_ADD_TC(tp, crc32c_basic_correctness);
148 ATF_TP_ADD_TC(tp, crc32c_alignment);
149 ATF_TP_ADD_TC(tp, crc32c_trailing_bytes);
150 return (atf_no_error());