Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/md5/asm/md5-riscv64-zbb.pl

raw
1 #! /usr/bin/env perl
2 # This file is dual-licensed, meaning that you can use it under your
3 # choice of either of the following two licenses:
4 #
5 # Copyright 2025 The OpenSSL Project Authors. All Rights Reserved.
6 #
7 # Licensed under the Apache License 2.0 (the "License"). You can obtain
8 # a copy in the file LICENSE in the source distribution or at
9 # https://www.openssl.org/source/license.html
10 #
11 # or
12 #
13 # Copyright (c) 2025, Julian Zhu <[email protected]>
14 # All rights reserved.
15 #
16 # Redistribution and use in source and binary forms, with or without
17 # modification, are permitted provided that the following conditions
18 # are met:
19 # 1. Redistributions of source code must retain the above copyright
20 # notice, this list of conditions and the following disclaimer.
21 # 2. Redistributions in binary form must reproduce the above copyright
22 # notice, this list of conditions and the following disclaimer in the
23 # documentation and/or other materials provided with the distribution.
24 #
25 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
37 # The generated code of this file depends on the following RISC-V extensions:
38 # - RV64I
39 # Optional:
40 # - RISC-V Basic Bit-manipulation extension ('Zbb')
41
42 use strict;
43 use warnings;
44
45 use FindBin qw($Bin);
46 use lib "$Bin";
47 use lib "$Bin/../../perlasm";
48 use riscv;
49
50 # $output is the last argument if it looks like a file (it has an extension)
51 # $flavour is the first argument if it doesn't look like a file
52 my $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
53 my $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
54
55 my $use_zbb = $flavour && $flavour =~ /zbb/i ? 1 : 0;
56 my $isaext = "_" . ( $use_zbb ? "zbb" : "riscv64" );
57
58 $output and open STDOUT,">$output";
59
60 my $code=<<___;
61 .text
62 ___
63
64 # Function arguments
65 my ($CTX, $INP, $LEN, $A, $B, $C, $D) = ("a0", "a1", "a2", "a4", "a5", "a6", "a7");
66 my ($KT, $T0, $T1, $T2, $lA, $lB, $lC, $lD) = ("a3", "t0", "t1", "t2", "t3", "t4", "t5", "t6");
67 my ($C1, $C2, $C3, $C4, $C5, $C6, $C7, $C8) = ("s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7");
68
69 sub ROUND0 {
70 my ($a, $b, $c, $d, $x, $const, $shift, $is_odd) = @_;
71 my $code=<<___;
72 li $T0, $const
73 ___
74 if ($is_odd) {
75 $code .= <<___;
76 addw $a, $a, $T0
77 srli $T0, $x, 32
78 ___
79 } else {
80 $code .= <<___;
81 addw $a, $a, $x
82 ___
83 }
84 $code .= <<___;
85 addw $a, $a, $T0
86 xor $T0, $c, $d
87 and $T0, $T0, $b
88 xor $T0, $T0, $d
89 addw $a, $a, $T0
90 ___
91 if ($use_zbb) {
92 my $ror_part = <<___;
93 @{[roriw $a, $a, 32 - $shift]}
94 ___
95 $code .= $ror_part;
96 } else {
97 my $ror_part = <<___;
98 @{[roriw_rv64i $a, $a, $T1, $T2, 32 - $shift]}
99 ___
100 $code .= $ror_part;
101 }
102 $code .= <<___;
103 addw $a, $a, $b
104 ___
105 return $code;
106 }
107
108 sub ROUND1 {
109 my ($a, $b, $c, $d, $x, $const, $shift, $is_odd) = @_;
110 my $code=<<___;
111 li $T0, $const
112 ___
113 if ($is_odd) {
114 $code .= <<___;
115 addw $a, $a, $T0
116 srli $T0, $x, 32
117 ___
118 } else {
119 $code .= <<___;
120 addw $a, $a, $x
121 ___
122 }
123 $code .= <<___;
124 addw $a, $a, $T0
125 xor $T0, $b, $c
126 and $T0, $T0, $d
127 xor $T0, $T0, $c
128 addw $a, $a, $T0
129 ___
130 if ($use_zbb) {
131 my $ror_part = <<___;
132 @{[roriw $a, $a, 32 - $shift]}
133 ___
134 $code .= $ror_part;
135 } else {
136 my $ror_part = <<___;
137 @{[roriw_rv64i $a, $a, $T1, $T2, 32 - $shift]}
138 ___
139 $code .= $ror_part;
140 }
141 $code .= <<___;
142 addw $a, $a, $b
143 ___
144 return $code;
145 }
146
147 sub ROUND2 {
148 my ($a, $b, $c, $d, $x, $const, $shift, $is_odd) = @_;
149 my $code=<<___;
150 li $T0, $const
151 ___
152 if ($is_odd) {
153 $code .= <<___;
154 addw $a, $a, $T0
155 srli $T0, $x, 32
156 ___
157 } else {
158 $code .= <<___;
159 addw $a, $a, $x
160 ___
161 }
162 $code .= <<___;
163 addw $a, $a, $T0
164 xor $T0, $c, $d
165 xor $T0, $T0, $b
166 addw $a, $a, $T0
167 ___
168 if ($use_zbb) {
169 my $ror_part = <<___;
170 @{[roriw $a, $a, 32 - $shift]}
171 ___
172 $code .= $ror_part;
173 } else {
174 my $ror_part = <<___;
175 @{[roriw_rv64i $a, $a, $T1, $T2, 32 - $shift]}
176 ___
177 $code .= $ror_part;
178 }
179 $code .= <<___;
180 addw $a, $a, $b
181 ___
182 return $code;
183 }
184
185 sub ROUND3 {
186 my ($a, $b, $c, $d, $x, $const, $shift, $is_odd) = @_;
187 my $code=<<___;
188 li $T0, $const
189 ___
190 if ($is_odd) {
191 $code .= <<___;
192 addw $a, $a, $T0
193 srli $T0, $x, 32
194 ___
195 } else {
196 $code .= <<___;
197 addw $a, $a, $x
198 ___
199 }
200 $code .= <<___;
201 addw $a, $a, $T0
202 ___
203 if ($use_zbb) {
204 my $orn_part = <<___;
205 @{[orn $T0, $b, $d]}
206 ___
207 $code .= $orn_part;
208 } else {
209 my $orn_part = <<___;
210 @{[orn_rv64i $T0, $b, $d]}
211 ___
212 $code .= $orn_part;
213 }
214 $code .= <<___;
215 xor $T0, $T0, $c
216 addw $a, $a, $T0
217 ___
218 if ($use_zbb) {
219 my $ror_part = <<___;
220 @{[roriw $a, $a, 32 - $shift]}
221 ___
222 $code .= $ror_part;
223 } else {
224 my $ror_part = <<___;
225 @{[roriw_rv64i $a, $a, $T1, $T2, 32 - $shift]}
226 ___
227 $code .= $ror_part;
228 }
229 $code .= <<___;
230 addw $a, $a, $b
231 ___
232 return $code;
233 }
234
235 ################################################################################
236 # void ossl_md5_block_asm_data_order@{[$isaext]}(MD5_CTX *c, const void *p, size_t num)
237 $code .= <<___;
238 .p2align 3
239 .globl ossl_md5_block_asm_data_order@{[$isaext]}
240 .type ossl_md5_block_asm_data_order@{[$isaext]},\@function
241 ossl_md5_block_asm_data_order@{[$isaext]}:
242
243 addi sp, sp, -64
244
245 sd s0, 0(sp)
246 sd s1, 8(sp)
247 sd s2, 16(sp)
248 sd s3, 24(sp)
249 sd s4, 32(sp)
250 sd s5, 40(sp)
251 sd s6, 48(sp)
252 sd s7, 56(sp)
253
254 # load ctx
255 lw $A, 0($CTX)
256 lw $B, 4($CTX)
257 lw $C, 8($CTX)
258 lw $D, 12($CTX)
259
260 L_round_loop:
261
262 ld $C1, 0($INP)
263 ld $C2, 8($INP)
264 ld $C3, 16($INP)
265 ld $C4, 24($INP)
266 ld $C5, 32($INP)
267 ld $C6, 40($INP)
268 ld $C7, 48($INP)
269 ld $C8, 56($INP)
270
271 mv $lA, $A
272 mv $lB, $B
273 mv $lC, $C
274 mv $lD, $D
275
276 @{[ROUND0 $A, $B, $C, $D, $C1, 0xd76aa478, 7, 0]}
277 @{[ROUND0 $D, $A, $B, $C, $C1, 0xe8c7b756, 12, 1]}
278 @{[ROUND0 $C, $D, $A, $B, $C2, 0x242070db, 17, 0]}
279 @{[ROUND0 $B, $C, $D, $A, $C2, 0xc1bdceee, 22, 1]}
280 @{[ROUND0 $A, $B, $C, $D, $C3, 0xf57c0faf, 7, 0]}
281 @{[ROUND0 $D, $A, $B, $C, $C3, 0x4787c62a, 12, 1]}
282 @{[ROUND0 $C, $D, $A, $B, $C4, 0xa8304613, 17, 0]}
283 @{[ROUND0 $B, $C, $D, $A, $C4, 0xfd469501, 22, 1]}
284 @{[ROUND0 $A, $B, $C, $D, $C5, 0x698098d8, 7, 0]}
285 @{[ROUND0 $D, $A, $B, $C, $C5, 0x8b44f7af, 12, 1]}
286 @{[ROUND0 $C, $D, $A, $B, $C6, 0xffff5bb1, 17, 0]}
287 @{[ROUND0 $B, $C, $D, $A, $C6, 0x895cd7be, 22, 1]}
288 @{[ROUND0 $A, $B, $C, $D, $C7, 0x6b901122, 7, 0]}
289 @{[ROUND0 $D, $A, $B, $C, $C7, 0xfd987193, 12, 1]}
290 @{[ROUND0 $C, $D, $A, $B, $C8, 0xa679438e, 17, 0]}
291 @{[ROUND0 $B, $C, $D, $A, $C8, 0x49b40821, 22, 1]}
292
293 @{[ROUND1 $A, $B, $C, $D, $C1, 0xf61e2562, 5, 1]}
294 @{[ROUND1 $D, $A, $B, $C, $C4, 0xc040b340, 9, 0]}
295 @{[ROUND1 $C, $D, $A, $B, $C6, 0x265e5a51, 14, 1]}
296 @{[ROUND1 $B, $C, $D, $A, $C1, 0xe9b6c7aa, 20, 0]}
297 @{[ROUND1 $A, $B, $C, $D, $C3, 0xd62f105d, 5, 1]}
298 @{[ROUND1 $D, $A, $B, $C, $C6, 0x2441453, 9, 0]}
299 @{[ROUND1 $C, $D, $A, $B, $C8, 0xd8a1e681, 14, 1]}
300 @{[ROUND1 $B, $C, $D, $A, $C3, 0xe7d3fbc8, 20, 0]}
301 @{[ROUND1 $A, $B, $C, $D, $C5, 0x21e1cde6, 5, 1]}
302 @{[ROUND1 $D, $A, $B, $C, $C8, 0xc33707d6, 9, 0]}
303 @{[ROUND1 $C, $D, $A, $B, $C2, 0xf4d50d87, 14, 1]}
304 @{[ROUND1 $B, $C, $D, $A, $C5, 0x455a14ed, 20, 0]}
305 @{[ROUND1 $A, $B, $C, $D, $C7, 0xa9e3e905, 5, 1]}
306 @{[ROUND1 $D, $A, $B, $C, $C2, 0xfcefa3f8, 9, 0]}
307 @{[ROUND1 $C, $D, $A, $B, $C4, 0x676f02d9, 14, 1]}
308 @{[ROUND1 $B, $C, $D, $A, $C7, 0x8d2a4c8a, 20, 0]}
309
310 @{[ROUND2 $A, $B, $C, $D, $C3, 0xfffa3942, 4, 1]}
311 @{[ROUND2 $D, $A, $B, $C, $C5, 0x8771f681, 11, 0]}
312 @{[ROUND2 $C, $D, $A, $B, $C6, 0x6d9d6122, 16, 1]}
313 @{[ROUND2 $B, $C, $D, $A, $C8, 0xfde5380c, 23, 0]}
314 @{[ROUND2 $A, $B, $C, $D, $C1, 0xa4beea44, 4, 1]}
315 @{[ROUND2 $D, $A, $B, $C, $C3, 0x4bdecfa9, 11, 0]}
316 @{[ROUND2 $C, $D, $A, $B, $C4, 0xf6bb4b60, 16, 1]}
317 @{[ROUND2 $B, $C, $D, $A, $C6, 0xbebfbc70, 23, 0]}
318 @{[ROUND2 $A, $B, $C, $D, $C7, 0x289b7ec6, 4, 1]}
319 @{[ROUND2 $D, $A, $B, $C, $C1, 0xeaa127fa, 11, 0]}
320 @{[ROUND2 $C, $D, $A, $B, $C2, 0xd4ef3085, 16, 1]}
321 @{[ROUND2 $B, $C, $D, $A, $C4, 0x4881d05, 23, 0]}
322 @{[ROUND2 $A, $B, $C, $D, $C5, 0xd9d4d039, 4, 1]}
323 @{[ROUND2 $D, $A, $B, $C, $C7, 0xe6db99e5, 11, 0]}
324 @{[ROUND2 $C, $D, $A, $B, $C8, 0x1fa27cf8, 16, 1]}
325 @{[ROUND2 $B, $C, $D, $A, $C2, 0xc4ac5665, 23, 0]}
326
327 @{[ROUND3 $A, $B, $C, $D, $C1, 0xf4292244, 6, 0]}
328 @{[ROUND3 $D, $A, $B, $C, $C4, 0x432aff97, 10, 1]}
329 @{[ROUND3 $C, $D, $A, $B, $C8, 0xab9423a7, 15, 0]}
330 @{[ROUND3 $B, $C, $D, $A, $C3, 0xfc93a039, 21, 1]}
331 @{[ROUND3 $A, $B, $C, $D, $C7, 0x655b59c3, 6, 0]}
332 @{[ROUND3 $D, $A, $B, $C, $C2, 0x8f0ccc92, 10, 1]}
333 @{[ROUND3 $C, $D, $A, $B, $C6, 0xffeff47d, 15, 0]}
334 @{[ROUND3 $B, $C, $D, $A, $C1, 0x85845dd1, 21, 1]}
335 @{[ROUND3 $A, $B, $C, $D, $C5, 0x6fa87e4f, 6, 0]}
336 @{[ROUND3 $D, $A, $B, $C, $C8, 0xfe2ce6e0, 10, 1]}
337 @{[ROUND3 $C, $D, $A, $B, $C4, 0xa3014314, 15, 0]}
338 @{[ROUND3 $B, $C, $D, $A, $C7, 0x4e0811a1, 21, 1]}
339 @{[ROUND3 $A, $B, $C, $D, $C3, 0xf7537e82, 6, 0]}
340 @{[ROUND3 $D, $A, $B, $C, $C6, 0xbd3af235, 10, 1]}
341 @{[ROUND3 $C, $D, $A, $B, $C2, 0x2ad7d2bb, 15, 0]}
342 @{[ROUND3 $B, $C, $D, $A, $C5, 0xeb86d391, 21, 1]}
343
344 addw $A, $A, $lA
345 addw $B, $B, $lB
346 addw $C, $C, $lC
347 addw $D, $D, $lD
348
349 addi $INP, $INP, 64
350
351 addi $LEN, $LEN, -1
352
353 bnez $LEN, L_round_loop
354
355 sw $A, 0($CTX)
356 sw $B, 4($CTX)
357 sw $C, 8($CTX)
358 sw $D, 12($CTX)
359
360 ld s0, 0(sp)
361 ld s1, 8(sp)
362 ld s2, 16(sp)
363 ld s3, 24(sp)
364 ld s4, 32(sp)
365 ld s5, 40(sp)
366 ld s6, 48(sp)
367 ld s7, 56(sp)
368
369 addi sp, sp, 64
370
371 ret
372 .size ossl_md5_block_asm_data_order@{[$isaext]},.-ossl_md5_block_asm_data_order@{[$isaext]}
373 ___
374
375 print $code;
376
377 close STDOUT or die "error closing STDOUT: $!";