Harmonize s390x assembler modules with "catch-all" rules from commit#19749.
[openssl.git] / crypto / bn / asm / s390x-mont.pl
1 #!/usr/bin/env perl
2
3 # ====================================================================
4 # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
5 # project. The module is, however, dual licensed under OpenSSL and
6 # CRYPTOGAMS licenses depending on where you obtain it. For further
7 # details see http://www.openssl.org/~appro/cryptogams/.
8 # ====================================================================
9
10 # April 2007.
11 #
12 # Performance improvement over vanilla C code varies from 85% to 45%
13 # depending on key length and benchmark. Unfortunately in this context
14 # these are not very impressive results [for code that utilizes "wide"
15 # 64x64=128-bit multiplication, which is not commonly available to C
16 # programmers], at least hand-coded bn_asm.c replacement is known to
17 # provide 30-40% better results for longest keys. Well, on a second
18 # thought it's not very surprising, because z-CPUs are single-issue
19 # and _strictly_ in-order execution, while bn_mul_mont is more or less
20 # dependent on CPU ability to pipe-line instructions and have several
21 # of them "in-flight" at the same time. I mean while other methods,
22 # for example Karatsuba, aim to minimize amount of multiplications at
23 # the cost of other operations increase, bn_mul_mont aim to neatly
24 # "overlap" multiplications and the other operations [and on most
25 # platforms even minimize the amount of the other operations, in
26 # particular references to memory]. But it's possible to improve this
27 # module performance by implementing dedicated squaring code-path and
28 # possibly by unrolling loops...
29
30 # January 2009.
31 #
32 # Reschedule to minimize/avoid Address Generation Interlock hazard,
33 # make inner loops counter-based.
34
35 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
36 open STDOUT,">$output";
37
38 $mn0="%r0";
39 $num="%r1";
40
41 # int bn_mul_mont(
42 $rp="%r2";              # BN_ULONG *rp,
43 $ap="%r3";              # const BN_ULONG *ap,
44 $bp="%r4";              # const BN_ULONG *bp,
45 $np="%r5";              # const BN_ULONG *np,
46 $n0="%r6";              # const BN_ULONG *n0,
47 #$num="160(%r15)"       # int num);
48
49 $bi="%r2";      # zaps rp
50 $j="%r7";
51
52 $ahi="%r8";
53 $alo="%r9";
54 $nhi="%r10";
55 $nlo="%r11";
56 $AHI="%r12";
57 $NHI="%r13";
58 $count="%r14";
59 $sp="%r15";
60
61 $code.=<<___;
62 .text
63 .globl  bn_mul_mont
64 .type   bn_mul_mont,\@function
65 bn_mul_mont:
66         lgf     $num,164($sp)   # pull $num
67         sla     $num,3          # $num to enumerate bytes
68         la      $bp,0($num,$bp)
69
70         stg     %r2,16($sp)
71
72         cghi    $num,16         #
73         lghi    %r2,0           #
74         blr     %r14            # if($num<16) return 0;
75         cghi    $num,128        #
76         bhr     %r14            # if($num>128) return 0;
77
78         stmg    %r3,%r15,24($sp)
79
80         lghi    $rp,-160-8      # leave room for carry bit
81         lcgr    $j,$num         # -$num
82         lgr     %r0,$sp
83         la      $rp,0($rp,$sp)
84         la      $sp,0($j,$rp)   # alloca
85         stg     %r0,0($sp)      # back chain
86
87         sra     $num,3          # restore $num
88         la      $bp,0($j,$bp)   # restore $bp
89         ahi     $num,-1         # adjust $num for inner loop
90         lg      $n0,0($n0)      # pull n0
91
92         lg      $bi,0($bp)
93         lg      $alo,0($ap)
94         mlgr    $ahi,$bi        # ap[0]*bp[0]
95         lgr     $AHI,$ahi
96
97         lgr     $mn0,$alo       # "tp[0]"*n0
98         msgr    $mn0,$n0
99
100         lg      $nlo,0($np)     #
101         mlgr    $nhi,$mn0       # np[0]*m1
102         algr    $nlo,$alo       # +="tp[0]"
103         lghi    $NHI,0
104         alcgr   $NHI,$nhi
105
106         la      $j,8(%r0)       # j=1
107         lr      $count,$num
108
109 .align  16
110 .L1st:
111         lg      $alo,0($j,$ap)
112         mlgr    $ahi,$bi        # ap[j]*bp[0]
113         algr    $alo,$AHI
114         lghi    $AHI,0
115         alcgr   $AHI,$ahi
116
117         lg      $nlo,0($j,$np)
118         mlgr    $nhi,$mn0       # np[j]*m1
119         algr    $nlo,$NHI
120         lghi    $NHI,0
121         alcgr   $nhi,$NHI       # +="tp[j]"
122         algr    $nlo,$alo
123         alcgr   $NHI,$nhi
124
125         stg     $nlo,160-8($j,$sp)      # tp[j-1]=
126         la      $j,8($j)        # j++
127         brct    $count,.L1st
128
129         algr    $NHI,$AHI
130         lghi    $AHI,0
131         alcgr   $AHI,$AHI       # upmost overflow bit
132         stg     $NHI,160-8($j,$sp)
133         stg     $AHI,160($j,$sp)
134         la      $bp,8($bp)      # bp++
135
136 .Louter:
137         lg      $bi,0($bp)      # bp[i]
138         lg      $alo,0($ap)
139         mlgr    $ahi,$bi        # ap[0]*bp[i]
140         alg     $alo,160($sp)   # +=tp[0]
141         lghi    $AHI,0
142         alcgr   $AHI,$ahi
143
144         lgr     $mn0,$alo
145         msgr    $mn0,$n0        # tp[0]*n0
146
147         lg      $nlo,0($np)     # np[0]
148         mlgr    $nhi,$mn0       # np[0]*m1
149         algr    $nlo,$alo       # +="tp[0]"
150         lghi    $NHI,0
151         alcgr   $NHI,$nhi
152
153         la      $j,8(%r0)       # j=1
154         lr      $count,$num
155
156 .align  16
157 .Linner:
158         lg      $alo,0($j,$ap)
159         mlgr    $ahi,$bi        # ap[j]*bp[i]
160         algr    $alo,$AHI
161         lghi    $AHI,0
162         alcgr   $ahi,$AHI
163         alg     $alo,160($j,$sp)# +=tp[j]
164         alcgr   $AHI,$ahi
165
166         lg      $nlo,0($j,$np)
167         mlgr    $nhi,$mn0       # np[j]*m1
168         algr    $nlo,$NHI
169         lghi    $NHI,0
170         alcgr   $nhi,$NHI
171         algr    $nlo,$alo       # +="tp[j]"
172         alcgr   $NHI,$nhi
173
174         stg     $nlo,160-8($j,$sp)      # tp[j-1]=
175         la      $j,8($j)        # j++
176         brct    $count,.Linner
177
178         algr    $NHI,$AHI
179         lghi    $AHI,0
180         alcgr   $AHI,$AHI
181         alg     $NHI,160($j,$sp)# accumulate previous upmost overflow bit
182         lghi    $ahi,0
183         alcgr   $AHI,$ahi       # new upmost overflow bit
184         stg     $NHI,160-8($j,$sp)
185         stg     $AHI,160($j,$sp)
186
187         la      $bp,8($bp)      # bp++
188         clg     $bp,160+8+32($j,$sp)    # compare to &bp[num]
189         jne     .Louter
190
191         lg      $rp,160+8+16($j,$sp)    # reincarnate rp
192         la      $ap,160($sp)
193         ahi     $num,1          # restore $num, incidentally clears "borrow"
194
195         la      $j,0(%r0)
196         lr      $count,$num
197 .Lsub:  lg      $alo,0($j,$ap)
198         slbg    $alo,0($j,$np)
199         stg     $alo,0($j,$rp)
200         la      $j,8($j)
201         brct    $count,.Lsub
202         lghi    $ahi,0
203         slbgr   $AHI,$ahi       # handle upmost carry
204
205         ngr     $ap,$AHI
206         lghi    $np,-1
207         xgr     $np,$AHI
208         ngr     $np,$rp
209         ogr     $ap,$np         # ap=borrow?tp:rp
210
211         la      $j,0(%r0)
212         lgr     $count,$num
213 .Lcopy: lg      $alo,0($j,$ap)  # copy or in-place refresh
214         stg     $j,160($j,$sp)  # zap tp
215         stg     $alo,0($j,$rp)
216         la      $j,8($j)
217         brct    $count,.Lcopy
218
219         la      %r1,160+8+48($j,$sp)
220         lmg     %r6,%r15,0(%r1)
221         lghi    %r2,1           # signal "processed"
222         br      %r14
223 .size   bn_mul_mont,.-bn_mul_mont
224 .string "Montgomery Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>"
225 ___
226
227 print $code;
228 close STDOUT;