Debian Codegraph @

third_party/rust/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
5 #[cfg_attr(not(target_arch = "aarch64"), allow(unused))]
6 #[target_feature(enable = "crc")]
7 pub unsafe fn crc32_acle_aarch64(crc: u32, buf: &[u8]) -> u32 {
8 let mut c = !crc;
9
10 // SAFETY: [u8; 8] safely transmutes into u64.
11 let (before, middle, after) = unsafe { buf.align_to::<u64>() };
12
13 c = remainder(c, before);
14
15 if middle.is_empty() && after.is_empty() {
16 return !c;
17 }
18
19 for d in middle {
20 c = unsafe { __crc32d(c, *d) };
21 }
22
23 c = remainder(c, after);
24
25 !c
26 }
27
28 #[inline]
29 #[target_feature(enable = "crc")]
30 unsafe fn remainder(mut c: u32, mut buf: &[u8]) -> u32 {
31 if let [b0, b1, b2, b3, rest @ ..] = buf {
32 c = unsafe { __crc32w(c, u32::from_le_bytes([*b0, *b1, *b2, *b3])) };
33 buf = rest;
34 }
35
36 if let [b0, b1, rest @ ..] = buf {
37 c = unsafe { __crc32h(c, u16::from_le_bytes([*b0, *b1])) };
38 buf = rest;
39 }
40
41 if let [b0, rest @ ..] = buf {
42 c = unsafe { __crc32b(c, *b0) };
43 buf = rest;
44 }
45
46 debug_assert!(buf.is_empty());
47
48 c
49 }
50
51 // FIXME the intrinsics below are stable since rust 1.80.0: remove these and use the standard
52 // library versions once our MSRV reaches that version.
53
54 /// CRC32 single round checksum for bytes (8 bits).
55 ///
56 /// [Arm's documentation](https://developer.arm.com/architectures/instruction-sets/intrinsics/__crc32b)
57 #[target_feature(enable = "crc")]
58 #[cfg_attr(target_arch = "arm", target_feature(enable = "v8"))]
59 unsafe fn __crc32b(mut crc: u32, data: u8) -> u32 {
60 unsafe {
61 core::arch::asm!("crc32b {crc:w}, {crc:w}, {data:w}", crc = inout(reg) crc, data = in(reg) data);
62 crc
63 }
64 }
65
66 /// CRC32 single round checksum for half words (16 bits).
67 ///
68 /// [Arm's documentation](https://developer.arm.com/architectures/instruction-sets/intrinsics/__crc32h)
69 #[target_feature(enable = "crc")]
70 #[cfg_attr(target_arch = "arm", target_feature(enable = "v8"))]
71 unsafe fn __crc32h(mut crc: u32, data: u16) -> u32 {
72 unsafe {
73 core::arch::asm!("crc32h {crc:w}, {crc:w}, {data:w}", crc = inout(reg) crc, data = in(reg) data);
74 crc
75 }
76 }
77
78 /// CRC32 single round checksum for words (32 bits).
79 ///
80 /// [Arm's documentation](https://developer.arm.com/architectures/instruction-sets/intrinsics/__crc32w)
81 #[target_feature(enable = "crc")]
82 #[cfg_attr(target_arch = "arm", target_feature(enable = "v8"))]
83 pub unsafe fn __crc32w(mut crc: u32, data: u32) -> u32 {
84 unsafe {
85 core::arch::asm!("crc32w {crc:w}, {crc:w}, {data:w}", crc = inout(reg) crc, data = in(reg) data);
86 crc
87 }
88 }
89
90 /// CRC32 single round checksum for double words (64 bits).
91 ///
92 /// [Arm's documentation](https://developer.arm.com/architectures/instruction-sets/intrinsics/__crc32d)
93 #[cfg(target_arch = "aarch64")]
94 #[target_feature(enable = "crc")]
95 unsafe fn __crc32d(mut crc: u32, data: u64) -> u32 {
96 unsafe {
97 core::arch::asm!("crc32x {crc:w}, {crc:w}, {data:x}", crc = inout(reg) crc, data = in(reg) data);
98 crc
99 }
100 }
101
102 #[cfg(test)]
103 mod tests {
104 use super::*;
105
106 quickcheck::quickcheck! {
107 #[cfg(target_arch = "aarch64")]
108 fn crc32_acle_aarch64_is_crc32fast(v: Vec<u8>, start: u32) -> bool {
109 let mut h = crc32fast::Hasher::new_with_initial(start);
110 h.update(&v);
111
112 let a = unsafe { crc32_acle_aarch64(start, &v) };
113 let b = h.finalize();
114
115 a == b
116 }
117 }
118
119 #[test]
120 fn test_crc32b() {
121 if !crate::cpu_features::is_enabled_crc() {
122 return;
123 }
124
125 unsafe {
126 assert_eq!(__crc32b(0, 0), 0);
127 assert_eq!(__crc32b(0, 255), 755167117);
128 }
129 }
130
131 #[test]
132 fn test_crc32h() {
133 if !crate::cpu_features::is_enabled_crc() {
134 return;
135 }
136
137 unsafe {
138 assert_eq!(__crc32h(0, 0), 0);
139 assert_eq!(__crc32h(0, 16384), 1994146192);
140 }
141 }
142
143 #[test]
144 fn test_crc32w() {
145 if !crate::cpu_features::is_enabled_crc() {
146 return;
147 }
148
149 unsafe {
150 assert_eq!(__crc32w(0, 0), 0);
151 assert_eq!(__crc32w(0, 4294967295), 3736805603);
152 }
153 }
154
155 #[test]
156 #[cfg(target_arch = "aarch64")]
157 fn test_crc32d() {
158 if !crate::cpu_features::is_enabled_crc() {
159 return;
160 }
161
162 unsafe {
163 assert_eq!(__crc32d(0, 0), 0);
164 assert_eq!(__crc32d(0, 18446744073709551615), 1147535477);
165 }
166 }
167 }