Debian Codegraph @

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