s390x-mont.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #! /usr/bin/env perl
  2. # Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  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
  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. $flavour = shift;
  49. if ($flavour =~ /3[12]/) {
  50. $SIZE_T=4;
  51. $g="";
  52. } else {
  53. $SIZE_T=8;
  54. $g="g";
  55. }
  56. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  57. open STDOUT,">$output";
  58. $stdframe=16*$SIZE_T+4*8;
  59. $mn0="%r0";
  60. $num="%r1";
  61. # int bn_mul_mont(
  62. $rp="%r2"; # BN_ULONG *rp,
  63. $ap="%r3"; # const BN_ULONG *ap,
  64. $bp="%r4"; # const BN_ULONG *bp,
  65. $np="%r5"; # const BN_ULONG *np,
  66. $n0="%r6"; # const BN_ULONG *n0,
  67. #$num="160(%r15)" # int num);
  68. $bi="%r2"; # zaps rp
  69. $j="%r7";
  70. $ahi="%r8";
  71. $alo="%r9";
  72. $nhi="%r10";
  73. $nlo="%r11";
  74. $AHI="%r12";
  75. $NHI="%r13";
  76. $count="%r14";
  77. $sp="%r15";
  78. $code.=<<___;
  79. .text
  80. .globl bn_mul_mont
  81. .type bn_mul_mont,\@function
  82. bn_mul_mont:
  83. lgf $num,`$stdframe+$SIZE_T-4`($sp) # pull $num
  84. sla $num,`log($SIZE_T)/log(2)` # $num to enumerate bytes
  85. la $bp,0($num,$bp)
  86. st${g} %r2,2*$SIZE_T($sp)
  87. cghi $num,16 #
  88. lghi %r2,0 #
  89. blr %r14 # if($num<16) return 0;
  90. ___
  91. $code.=<<___ if ($flavour =~ /3[12]/);
  92. tmll $num,4
  93. bnzr %r14 # if ($num&1) return 0;
  94. ___
  95. $code.=<<___ if ($flavour !~ /3[12]/);
  96. cghi $num,96 #
  97. bhr %r14 # if($num>96) return 0;
  98. ___
  99. $code.=<<___;
  100. stm${g} %r3,%r15,3*$SIZE_T($sp)
  101. lghi $rp,-$stdframe-8 # leave room for carry bit
  102. lcgr $j,$num # -$num
  103. lgr %r0,$sp
  104. la $rp,0($rp,$sp)
  105. la $sp,0($j,$rp) # alloca
  106. st${g} %r0,0($sp) # back chain
  107. sra $num,3 # restore $num
  108. la $bp,0($j,$bp) # restore $bp
  109. ahi $num,-1 # adjust $num for inner loop
  110. lg $n0,0($n0) # pull n0
  111. _dswap $n0
  112. lg $bi,0($bp)
  113. _dswap $bi
  114. lg $alo,0($ap)
  115. _dswap $alo
  116. mlgr $ahi,$bi # ap[0]*bp[0]
  117. lgr $AHI,$ahi
  118. lgr $mn0,$alo # "tp[0]"*n0
  119. msgr $mn0,$n0
  120. lg $nlo,0($np) #
  121. _dswap $nlo
  122. mlgr $nhi,$mn0 # np[0]*m1
  123. algr $nlo,$alo # +="tp[0]"
  124. lghi $NHI,0
  125. alcgr $NHI,$nhi
  126. la $j,8 # j=1
  127. lr $count,$num
  128. .align 16
  129. .L1st:
  130. lg $alo,0($j,$ap)
  131. _dswap $alo
  132. mlgr $ahi,$bi # ap[j]*bp[0]
  133. algr $alo,$AHI
  134. lghi $AHI,0
  135. alcgr $AHI,$ahi
  136. lg $nlo,0($j,$np)
  137. _dswap $nlo
  138. mlgr $nhi,$mn0 # np[j]*m1
  139. algr $nlo,$NHI
  140. lghi $NHI,0
  141. alcgr $nhi,$NHI # +="tp[j]"
  142. algr $nlo,$alo
  143. alcgr $NHI,$nhi
  144. stg $nlo,$stdframe-8($j,$sp) # tp[j-1]=
  145. la $j,8($j) # j++
  146. brct $count,.L1st
  147. algr $NHI,$AHI
  148. lghi $AHI,0
  149. alcgr $AHI,$AHI # upmost overflow bit
  150. stg $NHI,$stdframe-8($j,$sp)
  151. stg $AHI,$stdframe($j,$sp)
  152. la $bp,8($bp) # bp++
  153. .Louter:
  154. lg $bi,0($bp) # bp[i]
  155. _dswap $bi
  156. lg $alo,0($ap)
  157. _dswap $alo
  158. mlgr $ahi,$bi # ap[0]*bp[i]
  159. alg $alo,$stdframe($sp) # +=tp[0]
  160. lghi $AHI,0
  161. alcgr $AHI,$ahi
  162. lgr $mn0,$alo
  163. msgr $mn0,$n0 # tp[0]*n0
  164. lg $nlo,0($np) # np[0]
  165. _dswap $nlo
  166. mlgr $nhi,$mn0 # np[0]*m1
  167. algr $nlo,$alo # +="tp[0]"
  168. lghi $NHI,0
  169. alcgr $NHI,$nhi
  170. la $j,8 # j=1
  171. lr $count,$num
  172. .align 16
  173. .Linner:
  174. lg $alo,0($j,$ap)
  175. _dswap $alo
  176. mlgr $ahi,$bi # ap[j]*bp[i]
  177. algr $alo,$AHI
  178. lghi $AHI,0
  179. alcgr $ahi,$AHI
  180. alg $alo,$stdframe($j,$sp)# +=tp[j]
  181. alcgr $AHI,$ahi
  182. lg $nlo,0($j,$np)
  183. _dswap $nlo
  184. mlgr $nhi,$mn0 # np[j]*m1
  185. algr $nlo,$NHI
  186. lghi $NHI,0
  187. alcgr $nhi,$NHI
  188. algr $nlo,$alo # +="tp[j]"
  189. alcgr $NHI,$nhi
  190. stg $nlo,$stdframe-8($j,$sp) # tp[j-1]=
  191. la $j,8($j) # j++
  192. brct $count,.Linner
  193. algr $NHI,$AHI
  194. lghi $AHI,0
  195. alcgr $AHI,$AHI
  196. alg $NHI,$stdframe($j,$sp)# accumulate previous upmost overflow bit
  197. lghi $ahi,0
  198. alcgr $AHI,$ahi # new upmost overflow bit
  199. stg $NHI,$stdframe-8($j,$sp)
  200. stg $AHI,$stdframe($j,$sp)
  201. la $bp,8($bp) # bp++
  202. cl${g} $bp,`$stdframe+8+4*$SIZE_T`($j,$sp) # compare to &bp[num]
  203. jne .Louter
  204. l${g} $rp,`$stdframe+8+2*$SIZE_T`($j,$sp) # reincarnate rp
  205. la $ap,$stdframe($sp)
  206. ahi $num,1 # restore $num, incidentally clears "borrow"
  207. la $j,0
  208. lr $count,$num
  209. .Lsub: lg $alo,0($j,$ap)
  210. lg $nlo,0($j,$np)
  211. _dswap $nlo
  212. slbgr $alo,$nlo
  213. stg $alo,0($j,$rp)
  214. la $j,8($j)
  215. brct $count,.Lsub
  216. lghi $ahi,0
  217. slbgr $AHI,$ahi # handle upmost carry
  218. lghi $NHI,-1
  219. xgr $NHI,$AHI
  220. la $j,0
  221. lgr $count,$num
  222. .Lcopy: lg $ahi,$stdframe($j,$sp) # conditional copy
  223. lg $alo,0($j,$rp)
  224. ngr $ahi,$AHI
  225. ngr $alo,$NHI
  226. ogr $alo,$ahi
  227. _dswap $alo
  228. stg $j,$stdframe($j,$sp) # zap tp
  229. stg $alo,0($j,$rp)
  230. la $j,8($j)
  231. brct $count,.Lcopy
  232. la %r1,`$stdframe+8+6*$SIZE_T`($j,$sp)
  233. lm${g} %r6,%r15,0(%r1)
  234. lghi %r2,1 # signal "processed"
  235. br %r14
  236. .size bn_mul_mont,.-bn_mul_mont
  237. .string "Montgomery Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>"
  238. ___
  239. foreach (split("\n",$code)) {
  240. s/\`([^\`]*)\`/eval $1/ge;
  241. s/_dswap\s+(%r[0-9]+)/sprintf("rllg\t%s,%s,32",$1,$1) if($SIZE_T==4)/e;
  242. print $_,"\n";
  243. }
  244. close STDOUT or die "error closing STDOUT: $!";