2 # Copyright 2009-2016 The OpenSSL Project Authors. All Rights Reserved.
4 # Licensed under the OpenSSL license (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
10 # ====================================================================
11 # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
12 # project. The module is, however, dual licensed under OpenSSL and
13 # CRYPTOGAMS licenses depending on where you obtain it. For further
14 # details see http://www.openssl.org/~appro/cryptogams/.
15 # ====================================================================
17 # On PA-7100LC this module performs ~90-50% better, less for longer
18 # keys, than code generated by gcc 3.2 for PA-RISC 1.1. Latter means
19 # that compiler utilized xmpyu instruction to perform 32x32=64-bit
20 # multiplication, which in turn means that "baseline" performance was
21 # optimal in respect to instruction set capabilities. Fair comparison
22 # with vendor compiler is problematic, because OpenSSL doesn't define
23 # BN_LLONG [presumably] for historical reasons, which drives compiler
24 # toward 4 times 16x16=32-bit multiplications [plus complementary
25 # shifts and additions] instead. This means that you should observe
26 # several times improvement over code generated by vendor compiler
27 # for PA-RISC 1.1, but the "baseline" is far from optimal. The actual
28 # improvement coefficient was never collected on PA-7100LC, or any
29 # other 1.1 CPU, because I don't have access to such machine with
30 # vendor compiler. But to give you a taste, PA-RISC 1.1 code path
31 # reportedly outperformed code generated by cc +DA1.1 +O3 by factor
34 # On PA-RISC 2.0 it has to compete with pa-risc2[W].s, which is
35 # reportedly ~2x faster than vendor compiler generated code [according
36 # to comment in pa-risc2[W].s]. Here comes a catch. Execution core of
37 # this implementation is actually 32-bit one, in the sense that it
38 # operates on 32-bit values. But pa-risc2[W].s operates on arrays of
39 # 64-bit BN_LONGs... How do they interoperate then? No problem. This
40 # module picks halves of 64-bit values in reverse order and pretends
41 # they were 32-bit BN_LONGs. But can 32-bit core compete with "pure"
42 # 64-bit code such as pa-risc2[W].s then? Well, the thing is that
43 # 32x32=64-bit multiplication is the best even PA-RISC 2.0 can do,
44 # i.e. there is no "wider" multiplication like on most other 64-bit
45 # platforms. This means that even being effectively 32-bit, this
46 # implementation performs "64-bit" computational task in same amount
47 # of arithmetic operations, most notably multiplications. It requires
48 # more memory references, most notably to tp[num], but this doesn't
49 # seem to exhaust memory port capacity. And indeed, dedicated PA-RISC
50 # 2.0 code path provides virtually same performance as pa-risc2[W].s:
51 # it's ~10% better for shortest key length and ~10% worse for longest
54 # In case it wasn't clear. The module has two distinct code paths:
55 # PA-RISC 1.1 and PA-RISC 2.0 ones. Latter features carry-free 64-bit
56 # additions and 64-bit integer loads, not to mention specific
57 # instruction scheduling. In 64-bit build naturally only 2.0 code path
58 # is assembled. In 32-bit application context both code paths are
59 # assembled, PA-RISC 2.0 CPU is detected at run-time and proper path
60 # is taken automatically. Also, in 32-bit build the module imposes
61 # couple of limitations: vector lengths has to be even and vector
62 # addresses has to be 64-bit aligned. Normally neither is a problem:
63 # most common key lengths are even and vectors are commonly malloc-ed,
64 # which ensures alignment.
66 # Special thanks to polarhome.com for providing HP-UX account on
67 # PA-RISC 1.1 machine, and to correspondent who chose to remain
68 # anonymous for testing the code on PA-RISC 2.0 machine.
70 $0 =~ m/(.*[\/\\])[^\/\\]+$/; $dir=$1;
75 open STDOUT,">$output";
77 if ($flavour =~ /64/) {
88 $LEVEL ="1.1"; #$LEVEL.="\n\t.ALLOW\t2.0";
97 if (open CONF,"<${dir}../../opensslconf.h") {
99 if (m/#\s*define\s+SIXTY_FOUR_BIT/) {
109 $FRAME=8*$SIZE_T+$FRAME_MARKER; # 8 saved regs + frame marker
110 # [+ argument transfer]
111 $LOCALS=$FRAME-$FRAME_MARKER;
112 $FRAME+=32; # local variables
122 $n0="%r22"; # passed through stack in 32-bit
123 $num="%r21"; # passed through stack in 32-bit
136 $xfer=$n0; # accommodates [-16..15] offset in fld[dw]s
138 $fm0="%fr4"; $fti=$fm0;
141 $fai="%fr6"; $fab0="%fr7"; $fab1="%fr8";
142 $fni="%fr9"; $fnm0="%fr10"; $fnm1="%fr11";
147 .SUBSPA \$CODE\$,QUAD=0,ALIGN=8,ACCESS=0x2C,CODE_ONLY
149 .EXPORT bn_mul_mont,ENTRY,ARGW0=GR,ARGW1=GR,ARGW2=GR,ARGW3=GR
153 .CALLINFO FRAME=`$FRAME-8*$SIZE_T`,NO_CALLS,SAVE_RP,SAVE_SP,ENTRY_GR=6
155 $PUSH %r2,-$SAVED_RP(%sp) ; standard prologue
156 $PUSHMA %r3,$FRAME(%sp)
157 $PUSH %r4,`-$FRAME+1*$SIZE_T`(%sp)
158 $PUSH %r5,`-$FRAME+2*$SIZE_T`(%sp)
159 $PUSH %r6,`-$FRAME+3*$SIZE_T`(%sp)
160 $PUSH %r7,`-$FRAME+4*$SIZE_T`(%sp)
161 $PUSH %r8,`-$FRAME+5*$SIZE_T`(%sp)
162 $PUSH %r9,`-$FRAME+6*$SIZE_T`(%sp)
163 $PUSH %r10,`-$FRAME+7*$SIZE_T`(%sp)
166 $code.=<<___ if ($SIZE_T==4);
167 ldw `-$FRAME_MARKER-4`($fp),$n0
168 ldw `-$FRAME_MARKER-8`($fp),$num
172 $code.=<<___ if ($BN_SZ==4);
173 comiclr,<= 6,$num,%r0 ; are vectors long enough?
175 ldi 0,%r28 ; signal "unhandled"
176 add,ev %r0,$num,$num ; is $num even?
180 extru,= $ti1,31,3,%r0 ; are ap and np 64-bit aligned?
187 fldws,ma 4($bp),${fbi} ; bp[0]
189 $code.=<<___ if ($BN_SZ==8);
190 comib,> 3,$num,L\$abort ; are vectors long enough?
191 ldi 0,%r28 ; signal "unhandled"
192 addl $num,$num,$num ; I operate on 32-bit values
194 fldws 4($n0),${fn0} ; only low part of n0
195 fldws 4($bp),${fbi} ; bp[0] in flipped word order
198 fldds 0($ap),${fai} ; ap[0,1]
199 fldds 0($np),${fni} ; np[0,1]
201 sh2addl $num,%r0,$arrsz
203 ldo 36($arrsz),$hi1 ; space for tp[num+1]
204 andcm $hi1,$hi0,$hi1 ; align
206 $PUSH $fp,-$SIZE_T(%sp)
208 ldo `$LOCALS+16`($fp),$xfer
209 ldo `$LOCALS+32+4`($fp),$tp
211 xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[0]
212 xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[0]
213 xmpyu ${fn0},${fab0}R,${fm0}
215 addl $arrsz,$ap,$ap ; point at the end
217 subi 0,$arrsz,$idx ; j=0
218 ldo 8($idx),$idx ; j++++
220 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[0]*m
221 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[1]*m
222 fstds ${fab0},-16($xfer)
223 fstds ${fnm0},-8($xfer)
224 fstds ${fab1},0($xfer)
225 fstds ${fnm1},8($xfer)
226 flddx $idx($ap),${fai} ; ap[2,3]
227 flddx $idx($np),${fni} ; np[2,3]
229 $code.=<<___ if ($BN_SZ==4);
230 mtctl $hi0,%cr11 ; $hi0 still holds 31
231 extrd,u,*= $hi0,%sar,1,$hi0 ; executes on PA-RISC 1.0
235 $code.=<<___; # PA-RISC 2.0 code-path
236 xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[0]
237 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
239 fstds ${fab0},-16($xfer)
241 extrd,u $ab0,31,32,$hi0
242 extrd,u $ab0,63,32,$ab0
244 fstds ${fnm0},-8($xfer)
245 ldo 8($idx),$idx ; j++++
246 addl $ab0,$nm0,$nm0 ; low part is discarded
247 extrd,u $nm0,31,32,$hi1
250 xmpyu ${fai}R,${fbi},${fab1} ; ap[j+1]*bp[0]
251 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j+1]*m
253 fstds ${fab1},0($xfer)
255 extrd,u $ab1,31,32,$hi0
257 fstds ${fnm1},8($xfer)
258 extrd,u $ab1,63,32,$ab1
260 flddx $idx($ap),${fai} ; ap[j,j+1]
261 flddx $idx($np),${fni} ; np[j,j+1]
263 extrd,u $nm1,31,32,$hi1
265 xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[0]
266 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
268 fstds ${fab0},-16($xfer)
270 extrd,u $ab0,31,32,$hi0
272 fstds ${fnm0},-8($xfer)
273 extrd,u $ab0,63,32,$ab0
275 stw $nm1,-4($tp) ; tp[j-1]
277 stw,ma $nm0,8($tp) ; tp[j-1]
278 addib,<> 8,$idx,L\$1st ; j++++
279 extrd,u $nm0,31,32,$hi1
281 xmpyu ${fai}R,${fbi},${fab1} ; ap[j]*bp[0]
282 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j]*m
284 fstds ${fab1},0($xfer)
286 extrd,u $ab1,31,32,$hi0
288 fstds ${fnm1},8($xfer)
289 extrd,u $ab1,63,32,$ab1
294 extrd,u $nm1,31,32,$hi1
297 extrd,u $ab0,31,32,$hi0
298 stw $nm1,-4($tp) ; tp[j-1]
299 extrd,u $ab0,63,32,$ab0
304 extrd,u $nm0,31,32,$hi1
305 stw,ma $nm0,8($tp) ; tp[j-1]
307 ldo -1($num),$num ; i--
308 subi 0,$arrsz,$idx ; j=0
310 $code.=<<___ if ($BN_SZ==4);
311 fldws,ma 4($bp),${fbi} ; bp[1]
313 $code.=<<___ if ($BN_SZ==8);
314 fldws 0($bp),${fbi} ; bp[1] in flipped word order
317 flddx $idx($ap),${fai} ; ap[0,1]
318 flddx $idx($np),${fni} ; np[0,1]
319 fldws 8($xfer),${fti}R ; tp[0]
321 extrd,u $ab1,31,32,$hi0
322 extrd,u $ab1,63,32,$ab1
323 ldo 8($idx),$idx ; j++++
324 xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[1]
325 xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[1]
328 extrd,u $nm1,31,32,$hi1
329 fstws,mb ${fab0}L,-8($xfer) ; save high part
330 stw $nm1,-4($tp) ; tp[j-1]
332 fcpy,sgl %fr0,${fti}L ; zero high part
333 fcpy,sgl %fr0,${fab0}L
335 extrd,u $hi0,31,32,$hi1
336 fcnvxf,dbl,dbl ${fti},${fti} ; 32-bit unsigned int -> double
337 fcnvxf,dbl,dbl ${fab0},${fab0}
341 fadd,dbl ${fti},${fab0},${fab0} ; add tp[0]
342 fcnvfx,dbl,dbl ${fab0},${fab0} ; double -> 33-bit unsigned int
343 xmpyu ${fn0},${fab0}R,${fm0}
344 ldo `$LOCALS+32+4`($fp),$tp
346 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[0]*m
347 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[1]*m
348 fstds ${fab0},-16($xfer) ; 33-bit value
349 fstds ${fnm0},-8($xfer)
350 flddx $idx($ap),${fai} ; ap[2]
351 flddx $idx($np),${fni} ; np[2]
352 ldo 8($idx),$idx ; j++++
353 ldd -16($xfer),$ab0 ; 33-bit value
355 ldw 0($xfer),$hi0 ; high part
357 xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[i]
358 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
359 extrd,u $ab0,31,32,$ti0 ; carry bit
360 extrd,u $ab0,63,32,$ab0
361 fstds ${fab1},0($xfer)
362 addl $ti0,$hi0,$hi0 ; account carry bit
363 fstds ${fnm1},8($xfer)
364 addl $ab0,$nm0,$nm0 ; low part is discarded
365 ldw 0($tp),$ti1 ; tp[1]
366 extrd,u $nm0,31,32,$hi1
367 fstds ${fab0},-16($xfer)
368 fstds ${fnm0},-8($xfer)
371 xmpyu ${fai}R,${fbi},${fab1} ; ap[j+1]*bp[i]
372 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j+1]*m
374 fstds ${fab1},0($xfer)
378 fstds ${fnm1},8($xfer)
379 extrd,u $ab1,31,32,$hi0
380 extrd,u $ab1,63,32,$ab1
381 flddx $idx($ap),${fai} ; ap[j,j+1]
382 flddx $idx($np),${fni} ; np[j,j+1]
385 ldw 4($tp),$ti0 ; tp[j]
386 stw $nm1,-4($tp) ; tp[j-1]
388 xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[i]
389 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
391 fstds ${fab0},-16($xfer)
395 fstds ${fnm0},-8($xfer)
396 extrd,u $ab0,31,32,$hi0
397 extrd,u $nm1,31,32,$hi1
398 ldw 8($tp),$ti1 ; tp[j]
399 extrd,u $ab0,63,32,$ab0
402 stw,ma $nm0,8($tp) ; tp[j-1]
403 addib,<> 8,$idx,L\$inner ; j++++
404 extrd,u $nm0,31,32,$hi1
406 xmpyu ${fai}R,${fbi},${fab1} ; ap[j]*bp[i]
407 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j]*m
409 fstds ${fab1},0($xfer)
413 fstds ${fnm1},8($xfer)
414 extrd,u $ab1,31,32,$hi0
415 extrd,u $ab1,63,32,$ab1
416 ldw 4($tp),$ti0 ; tp[j]
421 extrd,u $nm1,31,32,$hi1
425 stw $nm1,-4($tp) ; tp[j-1]
426 extrd,u $ab0,31,32,$hi0
427 ldw 8($tp),$ti1 ; tp[j]
428 extrd,u $ab0,63,32,$ab0
433 extrd,u $nm0,31,32,$hi1
434 stw,ma $nm0,8($tp) ; tp[j-1]
436 addib,= -1,$num,L\$outerdone ; i--
437 subi 0,$arrsz,$idx ; j=0
439 $code.=<<___ if ($BN_SZ==4);
440 fldws,ma 4($bp),${fbi} ; bp[i]
442 $code.=<<___ if ($BN_SZ==8);
443 ldi 12,$ti0 ; bp[i] in flipped word order
444 addl,ev %r0,$num,$num
450 flddx $idx($ap),${fai} ; ap[0]
452 flddx $idx($np),${fni} ; np[0]
453 fldws 8($xfer),${fti}R ; tp[0]
455 extrd,u $ab1,31,32,$hi0
456 extrd,u $ab1,63,32,$ab1
458 ldo 8($idx),$idx ; j++++
459 xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[i]
460 xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[i]
461 ldw 4($tp),$ti0 ; tp[j]
464 fstws,mb ${fab0}L,-8($xfer) ; save high part
466 extrd,u $nm1,31,32,$hi1
467 fcpy,sgl %fr0,${fti}L ; zero high part
468 fcpy,sgl %fr0,${fab0}L
469 stw $nm1,-4($tp) ; tp[j-1]
471 fcnvxf,dbl,dbl ${fti},${fti} ; 32-bit unsigned int -> double
472 fcnvxf,dbl,dbl ${fab0},${fab0}
474 fadd,dbl ${fti},${fab0},${fab0} ; add tp[0]
476 extrd,u $hi0,31,32,$hi1
477 fcnvfx,dbl,dbl ${fab0},${fab0} ; double -> 33-bit unsigned int
480 xmpyu ${fn0},${fab0}R,${fm0}
483 ldo `$LOCALS+32+4`($fp),$tp
488 extrd,u $ab1,31,32,$hi0
489 extrd,u $ab1,63,32,$ab1
491 ldw 4($tp),$ti0 ; tp[j]
495 extrd,u $nm1,31,32,$hi1
496 stw $nm1,-4($tp) ; tp[j-1]
500 extrd,u $hi0,31,32,$hi1
504 ldo `$LOCALS+32`($fp),$tp
505 sub %r0,%r0,%r0 ; clear borrow
507 $code.=<<___ if ($BN_SZ==4);
509 extru,= $rp,31,3,%r0 ; is rp 64-bit aligned?
516 addib,<> 4,$idx,L\$sub
522 $code.=<<___ if ($BN_SZ==8);
526 shrpd $ti0,$ti0,32,$ti0 ; flip word order
527 std $ti0,-8($tp) ; save flipped value
528 sub,db $ti0,$hi0,$hi1
530 addib,<> 8,$idx,L\$sub
533 extrd,u $ti0,31,32,$ti0 ; carry in flipped word order
542 sub $rp,$arrsz,$rp ; rewind rp
544 ldo `$LOCALS+32`($fp),$tp
548 addib,<> 8,$idx,.-8 ; L\$copy
552 if ($BN_SZ==4) { # PA-RISC 1.1 code-path
566 xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[0]
567 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
572 fstds ${fab0},-16($xfer)
573 fstds ${fnm0},-8($xfer)
575 ldo 8($idx),$idx ; j++++
576 add $ablo,$nmlo0,$nmlo0 ; discarded
583 xmpyu ${fai}R,${fbi},${fab1} ; ap[j+1]*bp[0]
584 flddx $idx($ap),${fai} ; ap[j,j+1]
585 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j+1]*m
586 flddx $idx($np),${fni} ; np[j,j+1]
591 add $ablo,$nmlo1,$nmlo1
592 fstds ${fab1},0($xfer)
593 addc %r0,$nmhi1,$nmhi1
594 fstds ${fnm1},8($xfer)
595 add $hi1,$nmlo1,$nmlo1
600 xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[0]
602 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
605 stw $nmlo1,-4($tp) ; tp[j-1]
607 fstds ${fab0},-16($xfer)
608 add $ablo,$nmlo0,$nmlo0
609 fstds ${fnm0},-8($xfer)
610 addc %r0,$nmhi0,$nmhi0
612 add $hi1,$nmlo0,$nmlo0
614 stws,ma $nmlo0,8($tp) ; tp[j-1]
615 addib,<> 8,$idx,L\$1st_pa11 ; j++++
620 xmpyu ${fai}R,${fbi},${fab1} ; ap[j]*bp[0]
621 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j]*m
623 fstds ${fab1},0($xfer)
625 fstds ${fnm1},8($xfer)
626 add $ablo,$nmlo1,$nmlo1
628 addc %r0,$nmhi1,$nmhi1
630 add $hi1,$nmlo1,$nmlo1
636 stw $nmlo1,-4($tp) ; tp[j-1]
639 add $ablo,$nmlo0,$nmlo0
641 addc %r0,$nmhi0,$nmhi0
642 ldws,mb 8($xfer),$nmhi1
643 add $hi1,$nmlo0,$nmlo0
646 stws,ma $nmlo0,8($tp) ; tp[j-1]
648 ldo -1($num),$num ; i--
649 subi 0,$arrsz,$idx ; j=0
651 fldws,ma 4($bp),${fbi} ; bp[1]
652 flddx $idx($ap),${fai} ; ap[0,1]
653 flddx $idx($np),${fni} ; np[0,1]
654 fldws 8($xfer),${fti}R ; tp[0]
657 ldo 8($idx),$idx ; j++++
658 xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[1]
659 xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[1]
660 add $hi1,$nmlo1,$nmlo1
661 addc %r0,$nmhi1,$nmhi1
662 add $ablo,$nmlo1,$nmlo1
664 fstws,mb ${fab0}L,-8($xfer) ; save high part
665 stw $nmlo1,-4($tp) ; tp[j-1]
667 fcpy,sgl %fr0,${fti}L ; zero high part
668 fcpy,sgl %fr0,${fab0}L
671 fcnvxf,dbl,dbl ${fti},${fti} ; 32-bit unsigned int -> double
672 fcnvxf,dbl,dbl ${fab0},${fab0}
676 fadd,dbl ${fti},${fab0},${fab0} ; add tp[0]
677 fcnvfx,dbl,dbl ${fab0},${fab0} ; double -> 33-bit unsigned int
678 xmpyu ${fn0},${fab0}R,${fm0}
679 ldo `$LOCALS+32+4`($fp),$tp
681 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[0]*m
682 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[1]*m
683 fstds ${fab0},-16($xfer) ; 33-bit value
684 fstds ${fnm0},-8($xfer)
685 flddx $idx($ap),${fai} ; ap[2,3]
686 flddx $idx($np),${fni} ; np[2,3]
687 ldw -16($xfer),$abhi ; carry bit actually
688 ldo 8($idx),$idx ; j++++
692 ldw 0($xfer),$hi0 ; high part
694 xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[i]
695 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
696 fstds ${fab1},0($xfer)
697 addl $abhi,$hi0,$hi0 ; account carry bit
698 fstds ${fnm1},8($xfer)
699 add $ablo,$nmlo0,$nmlo0 ; discarded
700 ldw 0($tp),$ti1 ; tp[1]
702 fstds ${fab0},-16($xfer)
703 fstds ${fnm0},-8($xfer)
708 xmpyu ${fai}R,${fbi},${fab1} ; ap[j+1]*bp[i]
709 flddx $idx($ap),${fai} ; ap[j,j+1]
710 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j+1]*m
711 flddx $idx($np),${fni} ; np[j,j+1]
713 ldw 4($tp),$ti0 ; tp[j]
719 fstds ${fab1},0($xfer)
720 add $ablo,$nmlo1,$nmlo1
721 fstds ${fnm1},8($xfer)
722 addc %r0,$nmhi1,$nmhi1
724 add $hi1,$nmlo1,$nmlo1
728 xmpyu ${fai}L,${fbi},${fab0} ; ap[j]*bp[i]
729 ldw 8($tp),$ti1 ; tp[j]
730 xmpyu ${fni}L,${fm0}R,${fnm0} ; np[j]*m
735 stw $nmlo1,-4($tp) ; tp[j-1]
737 fstds ${fab0},-16($xfer)
739 fstds ${fnm0},-8($xfer)
740 add $ablo,$nmlo0,$nmlo0
742 addc %r0,$nmhi0,$nmhi0
744 add $hi1,$nmlo0,$nmlo0
745 stws,ma $nmlo0,8($tp) ; tp[j-1]
746 addib,<> 8,$idx,L\$inner_pa11 ; j++++
749 xmpyu ${fai}R,${fbi},${fab1} ; ap[j]*bp[i]
751 xmpyu ${fni}R,${fm0}R,${fnm1} ; np[j]*m
754 ldw 4($tp),$ti0 ; tp[j]
756 fstds ${fab1},0($xfer)
758 fstds ${fnm1},8($xfer)
761 add $ablo,$nmlo1,$nmlo1
763 addc %r0,$nmhi1,$nmhi1
765 add $hi1,$nmlo1,$nmlo1
770 stw $nmlo1,-4($tp) ; tp[j-1]
773 ldw 8($tp),$ti1 ; tp[j]
776 add $ablo,$nmlo0,$nmlo0
778 addc %r0,$nmhi0,$nmhi0
779 ldws,mb 8($xfer),$nmhi1
780 add $hi1,$nmlo0,$nmlo0
783 stws,ma $nmlo0,8($tp) ; tp[j-1]
785 addib,= -1,$num,L\$outerdone_pa11; i--
786 subi 0,$arrsz,$idx ; j=0
788 fldws,ma 4($bp),${fbi} ; bp[i]
789 flddx $idx($ap),${fai} ; ap[0]
792 flddx $idx($np),${fni} ; np[0]
793 fldws 8($xfer),${fti}R ; tp[0]
797 ldo 8($idx),$idx ; j++++
798 xmpyu ${fai}L,${fbi},${fab0} ; ap[0]*bp[i]
799 xmpyu ${fai}R,${fbi},${fab1} ; ap[1]*bp[i]
800 ldw 4($tp),$ti0 ; tp[j]
802 add $hi1,$nmlo1,$nmlo1
803 addc %r0,$nmhi1,$nmhi1
804 fstws,mb ${fab0}L,-8($xfer) ; save high part
805 add $ablo,$nmlo1,$nmlo1
807 fcpy,sgl %fr0,${fti}L ; zero high part
808 fcpy,sgl %fr0,${fab0}L
809 stw $nmlo1,-4($tp) ; tp[j-1]
811 fcnvxf,dbl,dbl ${fti},${fti} ; 32-bit unsigned int -> double
812 fcnvxf,dbl,dbl ${fab0},${fab0}
815 fadd,dbl ${fti},${fab0},${fab0} ; add tp[0]
818 fcnvfx,dbl,dbl ${fab0},${fab0} ; double -> 33-bit unsigned int
821 xmpyu ${fn0},${fab0}R,${fm0}
824 ldo `$LOCALS+32+4`($fp),$tp
832 ldw 4($tp),$ti0 ; tp[j]
834 add $hi1,$nmlo1,$nmlo1
835 addc %r0,$nmhi1,$nmhi1
836 add $ablo,$nmlo1,$nmlo1
838 stw $nmlo1,-4($tp) ; tp[j-1]
847 ldo `$LOCALS+32+4`($fp),$tp
848 sub %r0,%r0,%r0 ; clear borrow
855 addib,<> 4,$idx,L\$sub_pa11
864 sub $rp,$arrsz,$rp ; rewind rp
866 ldo `$LOCALS+32`($fp),$tp
870 addib,<> 4,$idx,L\$copy_pa11
879 ldi 1,%r28 ; signal "handled"
880 ldo $FRAME($fp),%sp ; destroy tp[num+1]
882 $POP `-$FRAME-$SAVED_RP`(%sp),%r2 ; standard epilogue
883 $POP `-$FRAME+1*$SIZE_T`(%sp),%r4
884 $POP `-$FRAME+2*$SIZE_T`(%sp),%r5
885 $POP `-$FRAME+3*$SIZE_T`(%sp),%r6
886 $POP `-$FRAME+4*$SIZE_T`(%sp),%r7
887 $POP `-$FRAME+5*$SIZE_T`(%sp),%r8
888 $POP `-$FRAME+6*$SIZE_T`(%sp),%r9
889 $POP `-$FRAME+7*$SIZE_T`(%sp),%r10
893 $POPMB -$FRAME(%sp),%r3
895 .STRINGZ "Montgomery Multiplication for PA-RISC, CRYPTOGAMS by <appro\@openssl.org>"
898 # Explicitly encode PA-RISC 2.0 instructions used in this module, so
899 # that it can be compiled with .LEVEL 1.0. It should be noted that I
900 # wouldn't have to do this, if GNU assembler understood .ALLOW 2.0
904 my ($mod,$args) = @_;
905 my $orig = "ldd$mod\t$args";
907 if ($args =~ /%r([0-9]+)\(%r([0-9]+)\),%r([0-9]+)/) # format 4
908 { my $opcode=(0x03<<26)|($2<<21)|($1<<16)|(3<<6)|$3;
909 sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
911 elsif ($args =~ /(\-?[0-9]+)\(%r([0-9]+)\),%r([0-9]+)/) # format 5
912 { my $opcode=(0x03<<26)|($2<<21)|(1<<12)|(3<<6)|$3;
913 $opcode|=(($1&0xF)<<17)|(($1&0x10)<<12); # encode offset
914 $opcode|=(1<<5) if ($mod =~ /^,m/);
915 $opcode|=(1<<13) if ($mod =~ /^,mb/);
916 sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
922 my ($mod,$args) = @_;
923 my $orig = "std$mod\t$args";
925 if ($args =~ /%r([0-9]+),(\-?[0-9]+)\(%r([0-9]+)\)/) # format 6
926 { my $opcode=(0x03<<26)|($3<<21)|($1<<16)|(1<<12)|(0xB<<6);
927 $opcode|=(($2&0xF)<<1)|(($2&0x10)>>4); # encode offset
928 $opcode|=(1<<5) if ($mod =~ /^,m/);
929 $opcode|=(1<<13) if ($mod =~ /^,mb/);
930 sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
936 my ($mod,$args) = @_;
937 my $orig = "extrd$mod\t$args";
939 # I only have ",u" completer, it's implicitly encoded...
940 if ($args =~ /%r([0-9]+),([0-9]+),([0-9]+),%r([0-9]+)/) # format 15
941 { my $opcode=(0x36<<26)|($1<<21)|($4<<16);
943 $opcode |= (($2&0x20)<<6)|(($2&0x1f)<<5); # encode pos
944 $opcode |= (($len&0x20)<<7)|($len&0x1f); # encode len
945 sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
947 elsif ($args =~ /%r([0-9]+),%sar,([0-9]+),%r([0-9]+)/) # format 12
948 { my $opcode=(0x34<<26)|($1<<21)|($3<<16)|(2<<11)|(1<<9);
950 $opcode |= (($len&0x20)<<3)|($len&0x1f); # encode len
951 $opcode |= (1<<13) if ($mod =~ /,\**=/);
952 sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
958 my ($mod,$args) = @_;
959 my $orig = "shrpd$mod\t$args";
961 if ($args =~ /%r([0-9]+),%r([0-9]+),([0-9]+),%r([0-9]+)/) # format 14
962 { my $opcode=(0x34<<26)|($2<<21)|($1<<16)|(1<<10)|$4;
964 $opcode |= (($cpos&0x20)<<6)|(($cpos&0x1f)<<5); # encode sa
965 sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig;
971 my ($mod,$args) = @_;
972 my $orig = "sub$mod\t$args";
974 if ($mod eq ",db" && $args =~ /%r([0-9]+),%r([0-9]+),%r([0-9]+)/) {
975 my $opcode=(0x02<<26)|($2<<21)|($1<<16)|$3;
976 $opcode|=(1<<10); # e1
977 $opcode|=(1<<8); # e2
979 sprintf "\t.WORD\t0x%08x\t; %s",$opcode,$orig
985 my ($mnemonic,$mod,$args)=@_;
986 my $opcode = eval("\$$mnemonic");
988 ref($opcode) eq 'CODE' ? &$opcode($mod,$args) : "\t$mnemonic$mod\t$args";
991 foreach (split("\n",$code)) {
992 s/\`([^\`]*)\`/eval $1/ge;
993 # flip word order in 64-bit mode...
994 s/(xmpyu\s+)($fai|$fni)([LR])/$1.$2.($3 eq "L"?"R":"L")/e if ($BN_SZ==8);
995 # assemble 2.0 instructions in 32-bit mode...
996 s/^\s+([a-z]+)([\S]*)\s+([\S]*)/&assemble($1,$2,$3)/e if ($BN_SZ==4);
998 s/\bbv\b/bve/gm if ($SIZE_T==8);