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 # ====================================================================
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...
32 # Reschedule to minimize/avoid Address Generation Interlock hazard,
33 # make inner loops counter-based.
37 # Adapt for -m31 build. If kernel supports what's called "highgprs"
38 # feature on Linux [see /proc/cpuinfo], it's possible to use 64-bit
39 # instructions and achieve "64-bit" performance even in 31-bit legacy
40 # application context. The feature is not specific to any particular
41 # processor, as long as it's "z-CPU". Latter implies that the code
42 # remains z/Architecture specific. Compatibility with 32-bit BN_ULONG
43 # is achieved by swapping words after 64-bit loads, follow _dswap-s.
44 # On z990 it was measured to perform 2.6-2.2 times better than
45 # compiler-generated code, less for longer keys...
49 if ($flavour =~ /3[12]/) {
57 while (($output=shift) && ($output!~/^\w[\w\-]*\.\w+$/)) {}
58 open STDOUT
,">$output";
60 $stdframe=16*$SIZE_T+4*8;
66 $rp="%r2"; # BN_ULONG *rp,
67 $ap="%r3"; # const BN_ULONG *ap,
68 $bp="%r4"; # const BN_ULONG *bp,
69 $np="%r5"; # const BN_ULONG *np,
70 $n0="%r6"; # const BN_ULONG *n0,
71 #$num="160(%r15)" # int num);
88 .type bn_mul_mont
,\
@function
90 lgf
$num,`$stdframe+$SIZE_T-4`($sp) # pull $num
91 sla
$num,`log($SIZE_T)/log(2)` # $num to enumerate bytes
94 st
${g
} %r2,2*$SIZE_T($sp)
98 blr
%r14 # if($num<16) return 0;
100 $code.=<<___
if ($flavour =~ /3[12]/);
102 bnzr
%r14 # if ($num&1) return 0;
104 $code.=<<___
if ($flavour !~ /3[12]/);
106 bhr
%r14 # if($num>96) return 0;
109 stm
${g
} %r3,%r15,3*$SIZE_T($sp)
111 lghi
$rp,-$stdframe-8 # leave room for carry bit
115 la
$sp,0($j,$rp) # alloca
116 st
${g
} %r0,0($sp) # back chain
118 sra
$num,3 # restore $num
119 la
$bp,0($j,$bp) # restore $bp
120 ahi
$num,-1 # adjust $num for inner loop
121 lg
$n0,0($n0) # pull n0
128 mlgr
$ahi,$bi # ap[0]*bp[0]
131 lgr
$mn0,$alo # "tp[0]"*n0
136 mlgr
$nhi,$mn0 # np[0]*m1
137 algr
$nlo,$alo # +="tp[0]"
148 mlgr
$ahi,$bi # ap[j]*bp[0]
155 mlgr
$nhi,$mn0 # np[j]*m1
158 alcgr
$nhi,$NHI # +="tp[j]"
162 stg
$nlo,$stdframe-8($j,$sp) # tp[j-1]=
168 alcgr
$AHI,$AHI # upmost overflow bit
169 stg
$NHI,$stdframe-8($j,$sp)
170 stg
$AHI,$stdframe($j,$sp)
174 lg
$bi,0($bp) # bp[i]
178 mlgr
$ahi,$bi # ap[0]*bp[i]
179 alg
$alo,$stdframe($sp) # +=tp[0]
184 msgr
$mn0,$n0 # tp[0]*n0
186 lg
$nlo,0($np) # np[0]
188 mlgr
$nhi,$mn0 # np[0]*m1
189 algr
$nlo,$alo # +="tp[0]"
200 mlgr
$ahi,$bi # ap[j]*bp[i]
204 alg
$alo,$stdframe($j,$sp)# +=tp[j]
209 mlgr
$nhi,$mn0 # np[j]*m1
213 algr
$nlo,$alo # +="tp[j]"
216 stg
$nlo,$stdframe-8($j,$sp) # tp[j-1]=
223 alg
$NHI,$stdframe($j,$sp)# accumulate previous upmost overflow bit
225 alcgr
$AHI,$ahi # new upmost overflow bit
226 stg
$NHI,$stdframe-8($j,$sp)
227 stg
$AHI,$stdframe($j,$sp)
230 cl
${g
} $bp,`$stdframe+8+4*$SIZE_T`($j,$sp) # compare to &bp[num]
233 l
${g
} $rp,`$stdframe+8+2*$SIZE_T`($j,$sp) # reincarnate rp
234 la
$ap,$stdframe($sp)
235 ahi
$num,1 # restore $num, incidentally clears "borrow"
239 .Lsub
: lg
$alo,0($j,$ap)
247 slbgr
$AHI,$ahi # handle upmost carry
253 ogr
$ap,$np # ap=borrow?tp:rp
257 .Lcopy
: lg
$alo,0($j,$ap) # copy or in-place refresh
259 stg
$j,$stdframe($j,$sp) # zap tp
264 la
%r1,`$stdframe+8+6*$SIZE_T`($j,$sp)
265 lm
${g
} %r6,%r15,0(%r1)
266 lghi
%r2,1 # signal "processed"
268 .size bn_mul_mont
,.-bn_mul_mont
269 .string
"Montgomery Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>"
272 foreach (split("\n",$code)) {
273 s/\`([^\`]*)\`/eval $1/ge;
274 s/_dswap\s+(%r[0-9]+)/sprintf("rllg\t%s,%s,32",$1,$1) if($SIZE_T==4)/e;