libkern_crc32.c (e532a99901e26d3961f8f5c0e4969fce86f09456) libkern_crc32.c (83c20b8a2da04937cf4af127366b3dc92c855784)
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

--- 19 unchanged lines hidden (view full) ---

28
29#include <sys/param.h>
30#include <sys/gsb_crc32.h>
31
32#include <stdint.h>
33
34#include <atf-c.h>
35
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

--- 19 unchanged lines hidden (view full) ---

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
36#if defined(__amd64__) || defined(__i386__)
37#include <machine/cpufunc.h>
38#include <machine/specialreg.h>
39
40static bool
41have_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}
38#endif
39
49#endif
50
51static void
52check_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
40ATF_TC_WITHOUT_HEAD(crc32c_basic_correctness);
41ATF_TC_BODY(crc32c_basic_correctness, tc)
42{
43 const uint64_t inputs[] = {
44 0xf408c634b3a9142,
45 0x80539e8c7c352e2b,
46 0x62e9121db6e4d649,
47 0x899345850ed0a286,

--- 24 unchanged lines hidden (view full) ---

72 0x97d2fbb5,
73 0x3c062ef1,
74 0xcc2eff18,
75 0x6a9b09f6,
76 0x420242c1,
77 0xfd562dc3,
78 };
79 size_t i;
76ATF_TC_WITHOUT_HEAD(crc32c_basic_correctness);
77ATF_TC_BODY(crc32c_basic_correctness, tc)
78{
79 const uint64_t inputs[] = {
80 0xf408c634b3a9142,
81 0x80539e8c7c352e2b,
82 0x62e9121db6e4d649,
83 0x899345850ed0a286,

--- 24 unchanged lines hidden (view full) ---

108 0x97d2fbb5,
109 0x3c062ef1,
110 0xcc2eff18,
111 0x6a9b09f6,
112 0x420242c1,
113 0xfd562dc3,
114 };
115 size_t i;
80 uint32_t act;
81
82 ATF_REQUIRE(nitems(inputs) == nitems(results));
83
84 for (i = 0; i < nitems(inputs); i++) {
116
117 ATF_REQUIRE(nitems(inputs) == nitems(results));
118
119 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);
120 check_crc32c(results[i], ~0u, &inputs[i], sizeof(inputs[0]));
95 }
96}
97
98ATF_TC_WITHOUT_HEAD(crc32c_alignment);
99ATF_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;
121 }
122}
123
124ATF_TC_WITHOUT_HEAD(crc32c_alignment);
125ATF_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;
105 uint32_t act;
106
131
107
108 for (i = 1; i < 8; i++) {
109 memcpy(&buf[i], &input, sizeof(input));
132 for (i = 1; i < 8; i++) {
133 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);
134 check_crc32c(result, ~0u, &buf[i], sizeof(input));
119 }
120}
121
122ATF_TC_WITHOUT_HEAD(crc32c_trailing_bytes);
123ATF_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;
135 }
136}
137
138ATF_TC_WITHOUT_HEAD(crc32c_trailing_bytes);
139ATF_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;
130 uint32_t act;
131
146
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);
147 check_crc32c(result, ~0u, input, sizeof(input));
139}
140
141ATF_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}
148}
149
150ATF_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}