Debian Codegraph @

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