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