Debian Codegraph @

debian/rust-vendor/zlib-rs/src/crc32/acle.rs

raw
1 //! # Safety
2 //!
3 //! The functions in this module must only be executed on an ARM system with the CRC feature.
4 use super::combine::{multmodp, x2nmodp};
5
6 // Constants empirically determined to maximize speed. These values are from
7 // measurements on a Cortex-A57. Your mileage may vary.
8 const Z_BATCH: usize = 3990;
9 const Z_BATCH_ZEROS: u32 = x2nmodp(Z_BATCH as u64, 6);
10 const _: () = assert!(Z_BATCH_ZEROS == 0xa10d3d0c);
11 const Z_BATCH_MIN: usize = 800;
12
13 /// 3-way interleaved ARM CRC32, ported from stock zlib's ARMCRC32 path.
14 ///
15 /// Runs three independent crc32x streams simultaneously to exploit the
16 /// ~3-cycle throughput of the instruction, then combines them with
17 /// polynomial multiplication.
18 ///
19 /// Based on the ARMCRC32 code path in stock zlib.
20 #[target_feature(enable = "crc")]
21 pub unsafe fn crc32_acle_aarch64(crc: u32, buf: &[u8]) -> u32 {
22 let mut crc = !crc;
23
24 // SAFETY: [u8; 8] safely transmutes into u64.
25 let (before, middle, after) = unsafe { buf.align_to::<u64>() };
26
27 // SAFETY: requires the "crc" feature.
28 crc = unsafe { remainder(crc, before) };
29
30 let mut words = middle;
31
32 while words.len() >= 3 * Z_BATCH {
33 let mut crc1: u32 = 0;
34 let mut crc2: u32 = 0;
35 for i in 0..Z_BATCH {
36 // SAFETY: requires the "crc" feature.
37 unsafe {
38 crc = __crc32d(crc, words[i].to_le());
39 crc1 = __crc32d(crc1, words[i + Z_BATCH].to_le());
40 crc2 = __crc32d(crc2, words[i + 2 * Z_BATCH].to_le());
41 }
42 }
43 words = &words[3 * Z_BATCH..];
44 crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc1;
45 crc = multmodp(Z_BATCH_ZEROS, crc) ^ crc2;
46 }
47
48 let last = words.len() / 3;
49 if last >= Z_BATCH_MIN {
50 let mut crc1: u32 = 0;
51 let mut crc2: u32 = 0;
52 for i in 0..last {
53 // SAFETY: requires the "crc" feature.
54 unsafe {
55 crc = __crc32d(crc, words[i].to_le());
56 crc1 = __crc32d(crc1, words[i + last].to_le());
57 crc2 = __crc32d(crc2, words[i + 2 * last].to_le());
58 }
59 }
60 words = &words[3 * last..];
61 let val = x2nmodp(last as u64, 6);
62 crc = multmodp(val, crc) ^ crc1;
63 crc = multmodp(val, crc) ^ crc2;
64 }
65
66 for &w in words {
67 // SAFETY: requires the "crc" feature.
68 crc = unsafe { __crc32d(crc, w.to_le()) };
69 }
70
71 // SAFETY: requires the "crc" feature.
72 crc = unsafe { remainder(crc, after) };
73
74 !crc
75 }
76
77 #[inline]
78 #[target_feature(enable = "crc")]
79 unsafe fn remainder(mut c: u32, mut buf: &[u8]) -> u32 {
80 if let [b0, b1, b2, b3, rest @ ..] = buf {
81 c = unsafe { __crc32w(c, u32::from_le_bytes([*b0, *b1, *b2, *b3])) };
82 buf = rest;
83 }
84
85 if let [b0, b1, rest @ ..] = buf {
86 c = unsafe { __crc32h(c, u16::from_le_bytes([*b0, *b1])) };
87 buf = rest;
88 }
89
90 if let [b0, rest @ ..] = buf {
91 c = unsafe { __crc32b(c, *b0) };
92 buf = rest;
93 }
94
95 debug_assert!(buf.is_empty());
96
97 c
98 }
99
100 crate::cfg_select! {
101 miri => {
102 use core::arch::aarch64::{__crc32b, __crc32h, __crc32d, __crc32w};
103 }
104 _ => {
105 use asm::{__crc32b, __crc32h, __crc32d, __crc32w};
106 }
107 }
108
109 // FIXME the intrinsics below are stable since rust 1.80.0: remove these and use the standard
110 // library versions once our MSRV reaches that version.
111 mod asm {
112
113 /// CRC32 single round checksum for bytes (8 bits).
114 ///
115 /// [Arm's documentation](https://developer.arm.com/architectures/instruction-sets/intrinsics/__crc32b)
116 #[target_feature(enable = "crc")]
117 #[cfg_attr(target_arch = "arm", target_feature(enable = "v8"))]
118 pub unsafe fn __crc32b(mut crc: u32, data: u8) -> u32 {
119 unsafe {
120 core::arch::asm!(
121 "crc32b {crc:w}, {crc:w}, {data:w}",
122 crc = inout(reg) crc,
123 data = in(reg) data,
124 options(pure, nomem, nostack, preserves_flags)
125 );
126 }
127 crc
128 }
129
130 /// CRC32 single round checksum for half words (16 bits).
131 ///
132 /// [Arm's documentation](https://developer.arm.com/architectures/instruction-sets/intrinsics/__crc32h)
133 #[target_feature(enable = "crc")]
134 #[cfg_attr(target_arch = "arm", target_feature(enable = "v8"))]
135 pub unsafe fn __crc32h(mut crc: u32, data: u16) -> u32 {
136 unsafe {
137 core::arch::asm!(
138 "crc32h {crc:w}, {crc:w}, {data:w}",
139 crc = inout(reg) crc,
140 data = in(reg) data,
141 options(pure, nomem, nostack, preserves_flags)
142 );
143 }
144 crc
145 }
146
147 /// CRC32 single round checksum for words (32 bits).
148 ///
149 /// [Arm's documentation](https://developer.arm.com/architectures/instruction-sets/intrinsics/__crc32w)
150 #[target_feature(enable = "crc")]
151 #[cfg_attr(target_arch = "arm", target_feature(enable = "v8"))]
152 pub unsafe fn __crc32w(mut crc: u32, data: u32) -> u32 {
153 unsafe {
154 core::arch::asm!(
155 "crc32w {crc:w}, {crc:w}, {data:w}",
156 crc = inout(reg) crc,
157 data = in(reg) data,
158 options(pure, nomem, nostack, preserves_flags)
159 );
160 }
161 crc
162 }
163
164 /// CRC32 single round checksum for double words (64 bits).
165 ///
166 /// [Arm's documentation](https://developer.arm.com/architectures/instruction-sets/intrinsics/__crc32d)
167 #[cfg(target_arch = "aarch64")]
168 #[target_feature(enable = "crc")]
169 pub unsafe fn __crc32d(mut crc: u32, data: u64) -> u32 {
170 unsafe {
171 core::arch::asm!(
172 "crc32x {crc:w}, {crc:w}, {data:x}",
173 crc = inout(reg) crc,
174 data = in(reg) data,
175 options(pure, nomem, nostack, preserves_flags)
176 );
177 }
178 crc
179 }
180 }
181
182 #[cfg(test)]
183 mod tests {
184 use super::*;
185
186 quickcheck::quickcheck! {
187 #[cfg(target_arch = "aarch64")]
188 fn crc32_acle_aarch64_is_crc32fast(v: Vec<u8>, start: u32) -> bool {
189 let mut h = crc32fast::Hasher::new_with_initial(start);
190 h.update(&v);
191
192 let a = unsafe { crc32_acle_aarch64(start, &v) };
193 let b = h.finalize();
194
195 a == b
196 }
197 }
198
199 #[test]
200 fn test_crc32b() {
201 if !crate::cpu_features::is_enabled_crc() {
202 return;
203 }
204
205 unsafe {
206 assert_eq!(__crc32b(0, 0), 0);
207 assert_eq!(__crc32b(0, 255), 755167117);
208 }
209 }
210
211 #[test]
212 fn test_crc32h() {
213 if !crate::cpu_features::is_enabled_crc() {
214 return;
215 }
216
217 unsafe {
218 assert_eq!(__crc32h(0, 0), 0);
219 assert_eq!(__crc32h(0, 16384), 1994146192);
220 }
221 }
222
223 #[test]
224 fn test_crc32w() {
225 if !crate::cpu_features::is_enabled_crc() {
226 return;
227 }
228
229 unsafe {
230 assert_eq!(__crc32w(0, 0), 0);
231 assert_eq!(__crc32w(0, 4294967295), 3736805603);
232 }
233 }
234
235 #[test]
236 #[cfg(target_arch = "aarch64")]
237 fn test_crc32d() {
238 if !crate::cpu_features::is_enabled_crc() {
239 return;
240 }
241
242 unsafe {
243 assert_eq!(__crc32d(0, 0), 0);
244 assert_eq!(__crc32d(0, 18446744073709551615), 1147535477);
245 }
246 }
247 }