Debian Codegraph @

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

raw
1 use super::combine::{multmodp, x2nmodp};
2
3 // The constants here are taken from the AArch64 implementation.
4 // Playing around with them showed no gains from larger batches,
5 // but losses from smaller ones. No reason to deviate here.
6 const Z_BATCH: usize = 3990;
7 const Z_BATCH_ZEROS: u32 = x2nmodp(Z_BATCH as u64, 6);
8 const _: () = assert!(Z_BATCH_ZEROS == 0xa10d3d0c);
9 const Z_BATCH_MIN: usize = 800;
10
11 /// 2-way interleaved CRC32, inspired by the AArch64 implementation.
12 ///
13 /// Runs two independent crc32 streams simultaneously to exploit the
14 /// ~2-cycle throughput of the instruction, then combines them with
15 /// polynomial multiplication.
16 ///
17 /// Throughput was measured around 1.995 on a Loongson 3B6000.
18 pub fn crc32_loongarch64(crc: u32, buf: &[u8]) -> u32 {
19 let mut crc = !crc as i32;
20
21 // SAFETY: [u8; 8] safely transmutes into i64.
22 let (before, middle, after) = unsafe { buf.align_to::<i64>() };
23
24 crc = remainder(crc, before);
25
26 let mut words = middle;
27
28 while words.len() >= 2 * Z_BATCH {
29 let mut crc1: i32 = 0;
30 for i in 0..Z_BATCH {
31 crc = crc_w_d_w(words[i].to_le(), crc);
32 crc1 = crc_w_d_w(words[i + Z_BATCH].to_le(), crc1);
33 }
34 words = &words[2 * Z_BATCH..];
35 crc = multmodp(Z_BATCH_ZEROS, crc as u32) as i32 ^ crc1;
36 }
37
38 let last = words.len() / 2;
39 if last >= Z_BATCH_MIN {
40 let mut crc1: i32 = 0;
41 for i in 0..last {
42 crc = crc_w_d_w(words[i].to_le(), crc);
43 crc1 = crc_w_d_w(words[i + last].to_le(), crc1);
44 }
45 words = &words[2 * last..];
46 let val = x2nmodp(last as u64, 6);
47 crc = multmodp(val, crc as u32) as i32 ^ crc1;
48 }
49
50 for &w in words {
51 crc = crc_w_d_w(w.to_le(), crc);
52 }
53
54 crc = remainder(crc, after);
55
56 !crc as u32
57 }
58
59 #[inline]
60 fn remainder(mut c: i32, mut buf: &[u8]) -> i32 {
61 if let [b0, b1, b2, b3, rest @ ..] = buf {
62 c = crc_w_w_w(i32::from_le_bytes([*b0, *b1, *b2, *b3]), c);
63 buf = rest;
64 }
65
66 if let [b0, b1, rest @ ..] = buf {
67 c = crc_w_h_w(i16::from_le_bytes([*b0, *b1]), c);
68 buf = rest;
69 }
70
71 if let [b0, rest @ ..] = buf {
72 c = crc_w_b_w(*b0 as i8, c);
73 buf = rest;
74 }
75
76 debug_assert!(buf.is_empty());
77
78 c
79 }
80
81 crate::cfg_select! {
82 miri => {
83 use core::arch::loongarch64::{crc_w_b_w, crc_w_h_w, crc_w_w_w, crc_w_d_w};
84 }
85 _ => {
86 use asm::{crc_w_b_w, crc_w_h_w, crc_w_w_w, crc_w_d_w};
87 }
88 }
89
90 // FIXME: there are intrinsics for these in the standard library, but currently
91 // unstable behind the stdarch_loongarch feature
92 //
93 // CRC32 instructions are part of the basic integer operations and therefore
94 // always available.
95 mod asm {
96 /// CRC32 single round checksum for bytes (8 bits).
97 ///
98 /// [Loongson's documentation](https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#crc-check-instructions)
99 pub fn crc_w_b_w(data: i8, mut crc: i32) -> i32 {
100 unsafe {
101 core::arch::asm!(
102 "crc.w.b.w {crc}, {data}, {crc}",
103 crc = inout(reg) crc,
104 data = in(reg) data,
105 options(pure, nomem, nostack, preserves_flags)
106 );
107 }
108 crc
109 }
110
111 /// CRC32 single round checksum for half words (16 bits).
112 ///
113 /// [Loongson's documentation](https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#crc-check-instructions)
114 pub fn crc_w_h_w(data: i16, mut crc: i32) -> i32 {
115 unsafe {
116 core::arch::asm!(
117 "crc.w.h.w {crc}, {data}, {crc}",
118 crc = inout(reg) crc,
119 data = in(reg) data,
120 options(pure, nomem, nostack, preserves_flags)
121 );
122 }
123 crc
124 }
125
126 /// CRC32 single round checksum for words (32 bits).
127 ///
128 /// [Loongson's documentation](https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#crc-check-instructions)
129 pub fn crc_w_w_w(data: i32, mut crc: i32) -> i32 {
130 unsafe {
131 core::arch::asm!(
132 "crc.w.w.w {crc}, {data}, {crc}",
133 crc = inout(reg) crc,
134 data = in(reg) data,
135 options(pure, nomem, nostack, preserves_flags)
136 );
137 }
138 crc
139 }
140
141 /// CRC32 single round checksum for double words (64 bits).
142 ///
143 /// [Loongson's documentation](https://loongson.github.io/LoongArch-Documentation/LoongArch-Vol1-EN.html#crc-check-instructions)
144 pub fn crc_w_d_w(data: i64, mut crc: i32) -> i32 {
145 unsafe {
146 core::arch::asm!(
147 "crc.w.d.w {crc}, {data}, {crc}",
148 crc = inout(reg) crc,
149 data = in(reg) data,
150 options(pure, nomem, nostack, preserves_flags)
151 );
152 }
153 crc
154 }
155 }