Debian Codegraph @

.cargo/vendor/sha2/src/sha512/loongarch64_asm.rs

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