Debian Codegraph @

debian/rust-vendor/sha1/src/compress/loongarch64_asm.rs

raw
1 //! LoongArch64 assembly backend
2
3 use crate::consts::K;
4 use core::arch::asm;
5
6 #[cfg(not(target_arch = "loongarch64"))]
7 compile_error!("loongarch-asm backend can be used only on loongarch64 target arches");
8
9 macro_rules! c {
10 ($($l:expr)*) => {
11 concat!($($l ,)*)
12 };
13 }
14
15 macro_rules! round0a {
16 ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => {
17 c!(
18 "ld.w $t5, $a1, (" $i " * 4);"
19 "revb.2h $t5, $t5;"
20 "rotri.w $t5, $t5, 16;"
21 "add.w " $e ", " $e ", $t5;"
22 "st.w $t5, $sp, (" $i " * 4);"
23 "xor $t5, " $c "," $d ";"
24 "and $t5, $t5, " $b ";"
25 "xor $t5, $t5, " $d ";"
26 roundtail!($a, $b, $e, $i, "$a4")
27 )
28 };
29 }
30
31 macro_rules! scheldule {
32 ($i:literal, $e:literal) => {
33 c!(
34 "ld.w $t5, $sp, (((" $i " - 3) & 0xF) * 4);"
35 "ld.w $t6, $sp, (((" $i " - 8) & 0xF) * 4);"
36 "ld.w $t7, $sp, (((" $i " - 14) & 0xF) * 4);"
37 "ld.w $t8, $sp, (((" $i " - 16) & 0xF) * 4);"
38 "xor $t5, $t5, $t6;"
39 "xor $t5, $t5, $t7;"
40 "xor $t5, $t5, $t8;"
41 "rotri.w $t5, $t5, 31;"
42 "add.w " $e "," $e ", $t5;"
43 "st.w $t5, $sp, ((" $i " & 0xF) * 4);"
44 )
45 };
46 }
47
48 macro_rules! round0b {
49 ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => {
50 c!(
51 scheldule!($i, $e)
52 "xor $t5," $c "," $d ";"
53 "and $t5, $t5," $b ";"
54 "xor $t5, $t5," $d ";"
55 roundtail!($a, $b, $e, $i, "$a4")
56 )
57 };
58 }
59
60 macro_rules! round1 {
61 ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => {
62 c!(
63 scheldule!($i, $e)
64 "xor $t5," $b "," $c ";"
65 "xor $t5, $t5," $d ";"
66 roundtail!($a, $b, $e, $i, "$a5")
67 )
68 };
69 }
70
71 macro_rules! round2 {
72 ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => {
73 c!(
74 scheldule!($i, $e)
75 "or $t5," $c "," $d ";"
76 "and $t5, $t5, " $b ";"
77 "and $t7," $c "," $d ";"
78 "or $t5, $t5, $t7;"
79 roundtail!($a, $b, $e, $i, "$a6")
80 )
81 };
82 }
83
84 macro_rules! round3 {
85 ($a:literal, $b:literal, $c:literal, $d:literal, $e:literal, $i:literal) => {
86 c!(
87 scheldule!($i, $e)
88 "xor $t5," $b "," $c ";"
89 "xor $t5, $t5," $d ";"
90 roundtail!($a, $b, $e, $i, "$a7")
91 )
92 };
93 }
94
95 macro_rules! roundtail {
96 ($a:literal, $b:literal, $e:literal, $i:literal, $k:literal) => {
97 c!(
98 "rotri.w " $b "," $b ", 2;"
99 "add.w " $e "," $e ", $t5;"
100 "add.w " $e "," $e "," $k ";"
101 "rotri.w $t5," $a ", 27;"
102 "add.w " $e "," $e ", $t5;"
103 )
104 };
105 }
106
107 pub(crate) fn compress(state: &mut [u32; 5], blocks: &[[u8; 64]]) {
108 if blocks.is_empty() {
109 return;
110 }
111
112 unsafe {
113 asm!(
114 // Allocate scratch stack space
115 "addi.d $sp, $sp, -64;",
116
117 // Load state
118 "ld.w $t0, $a0, 0",
119 "ld.w $t1, $a0, 4",
120 "ld.w $t2, $a0, 8",
121 "ld.w $t3, $a0, 12",
122 "ld.w $t4, $a0, 16",
123
124 "42:",
125
126 round0a!("$t0", "$t1", "$t2", "$t3", "$t4", 0),
127 round0a!("$t4", "$t0", "$t1", "$t2", "$t3", 1),
128 round0a!("$t3", "$t4", "$t0", "$t1", "$t2", 2),
129 round0a!("$t2", "$t3", "$t4", "$t0", "$t1", 3),
130 round0a!("$t1", "$t2", "$t3", "$t4", "$t0", 4),
131 round0a!("$t0", "$t1", "$t2", "$t3", "$t4", 5),
132 round0a!("$t4", "$t0", "$t1", "$t2", "$t3", 6),
133 round0a!("$t3", "$t4", "$t0", "$t1", "$t2", 7),
134 round0a!("$t2", "$t3", "$t4", "$t0", "$t1", 8),
135 round0a!("$t1", "$t2", "$t3", "$t4", "$t0", 9),
136 round0a!("$t0", "$t1", "$t2", "$t3", "$t4", 10),
137 round0a!("$t4", "$t0", "$t1", "$t2", "$t3", 11),
138 round0a!("$t3", "$t4", "$t0", "$t1", "$t2", 12),
139 round0a!("$t2", "$t3", "$t4", "$t0", "$t1", 13),
140 round0a!("$t1", "$t2", "$t3", "$t4", "$t0", 14),
141 round0a!("$t0", "$t1", "$t2", "$t3", "$t4", 15),
142 round0b!("$t4", "$t0", "$t1", "$t2", "$t3", 16),
143 round0b!("$t3", "$t4", "$t0", "$t1", "$t2", 17),
144 round0b!("$t2", "$t3", "$t4", "$t0", "$t1", 18),
145 round0b!("$t1", "$t2", "$t3", "$t4", "$t0", 19),
146 round1!("$t0", "$t1", "$t2", "$t3", "$t4", 20),
147 round1!("$t4", "$t0", "$t1", "$t2", "$t3", 21),
148 round1!("$t3", "$t4", "$t0", "$t1", "$t2", 22),
149 round1!("$t2", "$t3", "$t4", "$t0", "$t1", 23),
150 round1!("$t1", "$t2", "$t3", "$t4", "$t0", 24),
151 round1!("$t0", "$t1", "$t2", "$t3", "$t4", 25),
152 round1!("$t4", "$t0", "$t1", "$t2", "$t3", 26),
153 round1!("$t3", "$t4", "$t0", "$t1", "$t2", 27),
154 round1!("$t2", "$t3", "$t4", "$t0", "$t1", 28),
155 round1!("$t1", "$t2", "$t3", "$t4", "$t0", 29),
156 round1!("$t0", "$t1", "$t2", "$t3", "$t4", 30),
157 round1!("$t4", "$t0", "$t1", "$t2", "$t3", 31),
158 round1!("$t3", "$t4", "$t0", "$t1", "$t2", 32),
159 round1!("$t2", "$t3", "$t4", "$t0", "$t1", 33),
160 round1!("$t1", "$t2", "$t3", "$t4", "$t0", 34),
161 round1!("$t0", "$t1", "$t2", "$t3", "$t4", 35),
162 round1!("$t4", "$t0", "$t1", "$t2", "$t3", 36),
163 round1!("$t3", "$t4", "$t0", "$t1", "$t2", 37),
164 round1!("$t2", "$t3", "$t4", "$t0", "$t1", 38),
165 round1!("$t1", "$t2", "$t3", "$t4", "$t0", 39),
166 round2!("$t0", "$t1", "$t2", "$t3", "$t4", 40),
167 round2!("$t4", "$t0", "$t1", "$t2", "$t3", 41),
168 round2!("$t3", "$t4", "$t0", "$t1", "$t2", 42),
169 round2!("$t2", "$t3", "$t4", "$t0", "$t1", 43),
170 round2!("$t1", "$t2", "$t3", "$t4", "$t0", 44),
171 round2!("$t0", "$t1", "$t2", "$t3", "$t4", 45),
172 round2!("$t4", "$t0", "$t1", "$t2", "$t3", 46),
173 round2!("$t3", "$t4", "$t0", "$t1", "$t2", 47),
174 round2!("$t2", "$t3", "$t4", "$t0", "$t1", 48),
175 round2!("$t1", "$t2", "$t3", "$t4", "$t0", 49),
176 round2!("$t0", "$t1", "$t2", "$t3", "$t4", 50),
177 round2!("$t4", "$t0", "$t1", "$t2", "$t3", 51),
178 round2!("$t3", "$t4", "$t0", "$t1", "$t2", 52),
179 round2!("$t2", "$t3", "$t4", "$t0", "$t1", 53),
180 round2!("$t1", "$t2", "$t3", "$t4", "$t0", 54),
181 round2!("$t0", "$t1", "$t2", "$t3", "$t4", 55),
182 round2!("$t4", "$t0", "$t1", "$t2", "$t3", 56),
183 round2!("$t3", "$t4", "$t0", "$t1", "$t2", 57),
184 round2!("$t2", "$t3", "$t4", "$t0", "$t1", 58),
185 round2!("$t1", "$t2", "$t3", "$t4", "$t0", 59),
186 round3!("$t0", "$t1", "$t2", "$t3", "$t4", 60),
187 round3!("$t4", "$t0", "$t1", "$t2", "$t3", 61),
188 round3!("$t3", "$t4", "$t0", "$t1", "$t2", 62),
189 round3!("$t2", "$t3", "$t4", "$t0", "$t1", 63),
190 round3!("$t1", "$t2", "$t3", "$t4", "$t0", 64),
191 round3!("$t0", "$t1", "$t2", "$t3", "$t4", 65),
192 round3!("$t4", "$t0", "$t1", "$t2", "$t3", 66),
193 round3!("$t3", "$t4", "$t0", "$t1", "$t2", 67),
194 round3!("$t2", "$t3", "$t4", "$t0", "$t1", 68),
195 round3!("$t1", "$t2", "$t3", "$t4", "$t0", 69),
196 round3!("$t0", "$t1", "$t2", "$t3", "$t4", 70),
197 round3!("$t4", "$t0", "$t1", "$t2", "$t3", 71),
198 round3!("$t3", "$t4", "$t0", "$t1", "$t2", 72),
199 round3!("$t2", "$t3", "$t4", "$t0", "$t1", 73),
200 round3!("$t1", "$t2", "$t3", "$t4", "$t0", 74),
201 round3!("$t0", "$t1", "$t2", "$t3", "$t4", 75),
202 round3!("$t4", "$t0", "$t1", "$t2", "$t3", 76),
203 round3!("$t3", "$t4", "$t0", "$t1", "$t2", 77),
204 round3!("$t2", "$t3", "$t4", "$t0", "$t1", 78),
205 round3!("$t1", "$t2", "$t3", "$t4", "$t0", 79),
206
207 // Update state registers
208 "ld.w $t5, $a0, 0", // a
209 "ld.w $t6, $a0, 4", // b
210 "ld.w $t7, $a0, 8", // c
211 "ld.w $t8, $a0, 12", // d
212 "add.w $t0, $t0, $t5",
213 "ld.w $t5, $a0, 16", // e
214 "add.w $t1, $t1, $t6",
215 "add.w $t2, $t2, $t7",
216 "add.w $t3, $t3, $t8",
217 "add.w $t4, $t4, $t5",
218
219 // Save updated state
220 "st.w $t0, $a0, 0",
221 "st.w $t1, $a0, 4",
222 "st.w $t2, $a0, 8",
223 "st.w $t3, $a0, 12",
224 "st.w $t4, $a0, 16",
225
226 // Looping over blocks
227 "addi.d $a1, $a1, 64",
228 "addi.d $a2, $a2, -1",
229 "bnez $a2, 42b",
230
231 // Restore stack register
232 "addi.d $sp, $sp, 64",
233
234 in("$a0") state,
235 inout("$a1") blocks.as_ptr() => _,
236 inout("$a2") blocks.len() => _,
237
238 in("$a4") K[0],
239 in("$a5") K[1],
240 in("$a6") K[2],
241 in("$a7") K[3],
242
243 // Clobbers
244 out("$t0") _,
245 out("$t1") _,
246 out("$t2") _,
247 out("$t3") _,
248 out("$t4") _,
249 out("$t5") _,
250 out("$t6") _,
251 out("$t7") _,
252 out("$t8") _,
253
254 options(preserves_flags),
255 );
256 }
257 }