s390x assembler pack update.
[oweals/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 $mn0="%r0";
36 $num="%r1";
37
38 # int bn_mul_mont(
39 $rp="%r2";              # BN_ULONG *rp,
40 $ap="%r3";              # const BN_ULONG *ap,
41 $bp="%r4";              # const BN_ULONG *bp,
42 $np="%r5";              # const BN_ULONG *np,
43 $n0="%r6";              # const BN_ULONG *n0,
44 #$num="160(%r15)"       # int num);
45
46 $bi="%r2";      # zaps rp
47 $j="%r7";
48
49 $ahi="%r8";
50 $alo="%r9";
51 $nhi="%r10";
52 $nlo="%r11";
53 $AHI="%r12";
54 $NHI="%r13";
55 $count="%r14";
56 $sp="%r15";
57
58 $code.=<<___;
59 .text
60 .globl  bn_mul_mont
61 .type   bn_mul_mont,\@function
62 bn_mul_mont:
63         lgf     $num,164($sp)   # pull $num
64         sla     $num,3          # $num to enumerate bytes
65         la      $bp,0($num,$bp)
66
67         stmg    %r2,%r15,16($sp)
68
69         cghi    $num,16         #
70         lghi    %r2,0           #
71         blr     %r14            # if($num<16) return 0;
72         cghi    $num,128        #
73         bhr     %r14            # if($num>128) return 0;
74
75         lghi    $rp,-160-8      # leave room for carry bit
76         lcgr    $j,$num         # -$num
77         lgr     %r0,$sp
78         la      $rp,0($rp,$sp)
79         la      $sp,0($j,$rp)   # alloca
80         stg     %r0,0($sp)      # back chain
81
82         sra     $num,3          # restore $num
83         la      $bp,0($j,$bp)   # restore $bp
84         ahi     $num,-1         # adjust $num for inner loop
85         lg      $n0,0($n0)      # pull n0
86
87         lg      $bi,0($bp)
88         lg      $alo,0($ap)
89         mlgr    $ahi,$bi        # ap[0]*bp[0]
90         lgr     $AHI,$ahi
91
92         lgr     $mn0,$alo       # "tp[0]"*n0
93         msgr    $mn0,$n0
94
95         lg      $nlo,0($np)     #
96         mlgr    $nhi,$mn0       # np[0]*m1
97         algr    $nlo,$alo       # +="tp[0]"
98         lghi    $NHI,0
99         alcgr   $NHI,$nhi
100
101         la      $j,8(%r0)       # j=1
102         lr      $count,$num
103
104 .align  16
105 .L1st:
106         lg      $alo,0($j,$ap)
107         mlgr    $ahi,$bi        # ap[j]*bp[0]
108         algr    $alo,$AHI
109         lghi    $AHI,0
110         alcgr   $AHI,$ahi
111
112         lg      $nlo,0($j,$np)
113         mlgr    $nhi,$mn0       # np[j]*m1
114         algr    $nlo,$NHI
115         lghi    $NHI,0
116         alcgr   $nhi,$NHI       # +="tp[j]"
117         algr    $nlo,$alo
118         alcgr   $NHI,$nhi
119
120         stg     $nlo,160-8($j,$sp)      # tp[j-1]=
121         la      $j,8($j)        # j++
122         brct    $count,.L1st
123
124         algr    $NHI,$AHI
125         lghi    $AHI,0
126         alcgr   $AHI,$AHI       # upmost overflow bit
127         stg     $NHI,160-8($j,$sp)
128         stg     $AHI,160($j,$sp)
129         la      $bp,8($bp)      # bp++
130
131 .Louter:
132         lg      $bi,0($bp)      # bp[i]
133         lg      $alo,0($ap)
134         mlgr    $ahi,$bi        # ap[0]*bp[i]
135         alg     $alo,160($sp)   # +=tp[0]
136         lghi    $AHI,0
137         alcgr   $AHI,$ahi
138
139         lgr     $mn0,$alo
140         msgr    $mn0,$n0        # tp[0]*n0
141
142         lg      $nlo,0($np)     # np[0]
143         mlgr    $nhi,$mn0       # np[0]*m1
144         algr    $nlo,$alo       # +="tp[0]"
145         lghi    $NHI,0
146         alcgr   $NHI,$nhi
147
148         la      $j,8(%r0)       # j=1
149         lr      $count,$num
150
151 .align  16
152 .Linner:
153         lg      $alo,0($j,$ap)
154         mlgr    $ahi,$bi        # ap[j]*bp[i]
155         algr    $alo,$AHI
156         lghi    $AHI,0
157         alcgr   $ahi,$AHI
158         alg     $alo,160($j,$sp)# +=tp[j]
159         alcgr   $AHI,$ahi
160
161         lg      $nlo,0($j,$np)
162         mlgr    $nhi,$mn0       # np[j]*m1
163         algr    $nlo,$NHI
164         lghi    $NHI,0
165         alcgr   $nhi,$NHI
166         algr    $nlo,$alo       # +="tp[j]"
167         alcgr   $NHI,$nhi
168
169         stg     $nlo,160-8($j,$sp)      # tp[j-1]=
170         la      $j,8($j)        # j++
171         brct    $count,.Linner
172
173         algr    $NHI,$AHI
174         lghi    $AHI,0
175         alcgr   $AHI,$AHI
176         alg     $NHI,160($j,$sp)# accumulate previous upmost overflow bit
177         lghi    $ahi,0
178         alcgr   $AHI,$ahi       # new upmost overflow bit
179         stg     $NHI,160-8($j,$sp)
180         stg     $AHI,160($j,$sp)
181
182         la      $bp,8($bp)      # bp++
183         clg     $bp,160+8+32($j,$sp)    # compare to &bp[num]
184         jne     .Louter
185
186         lg      $rp,160+8+16($j,$sp)    # reincarnate rp
187         la      $ap,160($sp)
188         ahi     $num,1          # restore $num, incidentally clears "borrow"
189
190         la      $j,0(%r0)
191         lr      $count,$num
192 .Lsub:  lg      $alo,0($j,$ap)
193         slbg    $alo,0($j,$np)
194         stg     $alo,0($j,$rp)
195         la      $j,8($j)
196         brct    $count,.Lsub
197         lghi    $ahi,0
198         slbgr   $AHI,$ahi       # handle upmost carry
199
200         ngr     $ap,$AHI
201         lghi    $np,-1
202         xgr     $np,$AHI
203         ngr     $np,$rp
204         ogr     $ap,$np         # ap=borrow?tp:rp
205
206         la      $j,0(%r0)
207         lgr     $count,$num
208 .Lcopy: lg      $alo,0($j,$ap)  # copy or in-place refresh
209         stg     $j,160($j,$sp)  # zap tp
210         stg     $alo,0($j,$rp)
211         la      $j,8($j)
212         brct    $count,.Lcopy
213
214         la      %r1,160+8+48($j,$sp)
215         lmg     %r6,%r15,0(%r1)
216         lghi    %r2,1           # signal "processed"
217         br      %r14
218 .size   bn_mul_mont,.-bn_mul_mont
219 .string "Montgomery Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>"
220 ___
221
222 print $code;
223 close STDOUT;