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