s390x-mont.pl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #! /usr/bin/env perl
  2. # Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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
  8. # ====================================================================
  9. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  10. # project. The module is, however, dual licensed under OpenSSL and
  11. # CRYPTOGAMS licenses depending on where you obtain it. For further
  12. # details see http://www.openssl.org/~appro/cryptogams/.
  13. # ====================================================================
  14. # April 2007.
  15. #
  16. # Performance improvement over vanilla C code varies from 85% to 45%
  17. # depending on key length and benchmark. Unfortunately in this context
  18. # these are not very impressive results [for code that utilizes "wide"
  19. # 64x64=128-bit multiplication, which is not commonly available to C
  20. # programmers], at least hand-coded bn_asm.c replacement is known to
  21. # provide 30-40% better results for longest keys. Well, on a second
  22. # thought it's not very surprising, because z-CPUs are single-issue
  23. # and _strictly_ in-order execution, while bn_mul_mont is more or less
  24. # dependent on CPU ability to pipe-line instructions and have several
  25. # of them "in-flight" at the same time. I mean while other methods,
  26. # for example Karatsuba, aim to minimize amount of multiplications at
  27. # the cost of other operations increase, bn_mul_mont aim to neatly
  28. # "overlap" multiplications and the other operations [and on most
  29. # platforms even minimize the amount of the other operations, in
  30. # particular references to memory]. But it's possible to improve this
  31. # module performance by implementing dedicated squaring code-path and
  32. # possibly by unrolling loops...
  33. # January 2009.
  34. #
  35. # Reschedule to minimize/avoid Address Generation Interlock hazard,
  36. # make inner loops counter-based.
  37. # November 2010.
  38. #
  39. # Adapt for -m31 build. If kernel supports what's called "highgprs"
  40. # feature on Linux [see /proc/cpuinfo], it's possible to use 64-bit
  41. # instructions and achieve "64-bit" performance even in 31-bit legacy
  42. # application context. The feature is not specific to any particular
  43. # processor, as long as it's "z-CPU". Latter implies that the code
  44. # remains z/Architecture specific. Compatibility with 32-bit BN_ULONG
  45. # is achieved by swapping words after 64-bit loads, follow _dswap-s.
  46. # On z990 it was measured to perform 2.6-2.2 times better than
  47. # compiler-generated code, less for longer keys...
  48. # $output is the last argument if it looks like a file (it has an extension)
  49. # $flavour is the first argument if it doesn't look like a file
  50. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  51. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  52. if ($flavour =~ /3[12]/) {
  53. $SIZE_T=4;
  54. $g="";
  55. } else {
  56. $SIZE_T=8;
  57. $g="g";
  58. }
  59. $output and open STDOUT,">$output";
  60. $stdframe=16*$SIZE_T+4*8;
  61. $mn0="%r0";
  62. $num="%r1";
  63. # int bn_mul_mont(
  64. $rp="%r2"; # BN_ULONG *rp,
  65. $ap="%r3"; # const BN_ULONG *ap,
  66. $bp="%r4"; # const BN_ULONG *bp,
  67. $np="%r5"; # const BN_ULONG *np,
  68. $n0="%r6"; # const BN_ULONG *n0,
  69. #$num="160(%r15)" # int num);
  70. $bi="%r2"; # zaps rp
  71. $j="%r7";
  72. $ahi="%r8";
  73. $alo="%r9";
  74. $nhi="%r10";
  75. $nlo="%r11";
  76. $AHI="%r12";
  77. $NHI="%r13";
  78. $count="%r14";
  79. $sp="%r15";
  80. $code.=<<___;
  81. .text
  82. .globl bn_mul_mont
  83. .type bn_mul_mont,\@function
  84. bn_mul_mont:
  85. lgf $num,`$stdframe+$SIZE_T-4`($sp) # pull $num
  86. sla $num,`log($SIZE_T)/log(2)` # $num to enumerate bytes
  87. la $bp,0($num,$bp)
  88. st${g} %r2,2*$SIZE_T($sp)
  89. cghi $num,16 #
  90. lghi %r2,0 #
  91. blr %r14 # if($num<16) return 0;
  92. ___
  93. $code.=<<___ if ($flavour =~ /3[12]/);
  94. tmll $num,4
  95. bnzr %r14 # if ($num&1) return 0;
  96. ___
  97. $code.=<<___ if ($flavour !~ /3[12]/);
  98. cghi $num,96 #
  99. bhr %r14 # if($num>96) return 0;
  100. ___
  101. $code.=<<___;
  102. stm${g} %r3,%r15,3*$SIZE_T($sp)
  103. lghi $rp,-$stdframe-8 # leave room for carry bit
  104. lcgr $j,$num # -$num
  105. lgr %r0,$sp
  106. la $rp,0($rp,$sp)
  107. la $sp,0($j,$rp) # alloca
  108. st${g} %r0,0($sp) # back chain
  109. sra $num,3 # restore $num
  110. la $bp,0($j,$bp) # restore $bp
  111. ahi $num,-1 # adjust $num for inner loop
  112. lg $n0,0($n0) # pull n0
  113. _dswap $n0
  114. lg $bi,0($bp)
  115. _dswap $bi
  116. lg $alo,0($ap)
  117. _dswap $alo
  118. mlgr $ahi,$bi # ap[0]*bp[0]
  119. lgr $AHI,$ahi
  120. lgr $mn0,$alo # "tp[0]"*n0
  121. msgr $mn0,$n0
  122. lg $nlo,0($np) #
  123. _dswap $nlo
  124. mlgr $nhi,$mn0 # np[0]*m1
  125. algr $nlo,$alo # +="tp[0]"
  126. lghi $NHI,0
  127. alcgr $NHI,$nhi
  128. la $j,8 # j=1
  129. lr $count,$num
  130. .align 16
  131. .L1st:
  132. lg $alo,0($j,$ap)
  133. _dswap $alo
  134. mlgr $ahi,$bi # ap[j]*bp[0]
  135. algr $alo,$AHI
  136. lghi $AHI,0
  137. alcgr $AHI,$ahi
  138. lg $nlo,0($j,$np)
  139. _dswap $nlo
  140. mlgr $nhi,$mn0 # np[j]*m1
  141. algr $nlo,$NHI
  142. lghi $NHI,0
  143. alcgr $nhi,$NHI # +="tp[j]"
  144. algr $nlo,$alo
  145. alcgr $NHI,$nhi
  146. stg $nlo,$stdframe-8($j,$sp) # tp[j-1]=
  147. la $j,8($j) # j++
  148. brct $count,.L1st
  149. algr $NHI,$AHI
  150. lghi $AHI,0
  151. alcgr $AHI,$AHI # upmost overflow bit
  152. stg $NHI,$stdframe-8($j,$sp)
  153. stg $AHI,$stdframe($j,$sp)
  154. la $bp,8($bp) # bp++
  155. .Louter:
  156. lg $bi,0($bp) # bp[i]
  157. _dswap $bi
  158. lg $alo,0($ap)
  159. _dswap $alo
  160. mlgr $ahi,$bi # ap[0]*bp[i]
  161. alg $alo,$stdframe($sp) # +=tp[0]
  162. lghi $AHI,0
  163. alcgr $AHI,$ahi
  164. lgr $mn0,$alo
  165. msgr $mn0,$n0 # tp[0]*n0
  166. lg $nlo,0($np) # np[0]
  167. _dswap $nlo
  168. mlgr $nhi,$mn0 # np[0]*m1
  169. algr $nlo,$alo # +="tp[0]"
  170. lghi $NHI,0
  171. alcgr $NHI,$nhi
  172. la $j,8 # j=1
  173. lr $count,$num
  174. .align 16
  175. .Linner:
  176. lg $alo,0($j,$ap)
  177. _dswap $alo
  178. mlgr $ahi,$bi # ap[j]*bp[i]
  179. algr $alo,$AHI
  180. lghi $AHI,0
  181. alcgr $ahi,$AHI
  182. alg $alo,$stdframe($j,$sp)# +=tp[j]
  183. alcgr $AHI,$ahi
  184. lg $nlo,0($j,$np)
  185. _dswap $nlo
  186. mlgr $nhi,$mn0 # np[j]*m1
  187. algr $nlo,$NHI
  188. lghi $NHI,0
  189. alcgr $nhi,$NHI
  190. algr $nlo,$alo # +="tp[j]"
  191. alcgr $NHI,$nhi
  192. stg $nlo,$stdframe-8($j,$sp) # tp[j-1]=
  193. la $j,8($j) # j++
  194. brct $count,.Linner
  195. algr $NHI,$AHI
  196. lghi $AHI,0
  197. alcgr $AHI,$AHI
  198. alg $NHI,$stdframe($j,$sp)# accumulate previous upmost overflow bit
  199. lghi $ahi,0
  200. alcgr $AHI,$ahi # new upmost overflow bit
  201. stg $NHI,$stdframe-8($j,$sp)
  202. stg $AHI,$stdframe($j,$sp)
  203. la $bp,8($bp) # bp++
  204. cl${g} $bp,`$stdframe+8+4*$SIZE_T`($j,$sp) # compare to &bp[num]
  205. jne .Louter
  206. l${g} $rp,`$stdframe+8+2*$SIZE_T`($j,$sp) # reincarnate rp
  207. la $ap,$stdframe($sp)
  208. ahi $num,1 # restore $num, incidentally clears "borrow"
  209. la $j,0
  210. lr $count,$num
  211. .Lsub: lg $alo,0($j,$ap)
  212. lg $nlo,0($j,$np)
  213. _dswap $nlo
  214. slbgr $alo,$nlo
  215. stg $alo,0($j,$rp)
  216. la $j,8($j)
  217. brct $count,.Lsub
  218. lghi $ahi,0
  219. slbgr $AHI,$ahi # handle upmost carry
  220. lghi $NHI,-1
  221. xgr $NHI,$AHI
  222. la $j,0
  223. lgr $count,$num
  224. .Lcopy: lg $ahi,$stdframe($j,$sp) # conditional copy
  225. lg $alo,0($j,$rp)
  226. ngr $ahi,$AHI
  227. ngr $alo,$NHI
  228. ogr $alo,$ahi
  229. _dswap $alo
  230. stg $j,$stdframe($j,$sp) # zap tp
  231. stg $alo,0($j,$rp)
  232. la $j,8($j)
  233. brct $count,.Lcopy
  234. la %r1,`$stdframe+8+6*$SIZE_T`($j,$sp)
  235. lm${g} %r6,%r15,0(%r1)
  236. lghi %r2,1 # signal "processed"
  237. br %r14
  238. .size bn_mul_mont,.-bn_mul_mont
  239. .string "Montgomery Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>"
  240. ___
  241. foreach (split("\n",$code)) {
  242. s/\`([^\`]*)\`/eval $1/ge;
  243. s/_dswap\s+(%r[0-9]+)/sprintf("rllg\t%s,%s,32",$1,$1) if($SIZE_T==4)/e;
  244. print $_,"\n";
  245. }
  246. close STDOUT or die "error closing STDOUT: $!";