Debian Codegraph @

debian/rust-vendor/sha2/src/sha512/loongarch64_asm.rs

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