s390x-gf2m.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2016 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. # ====================================================================
  10. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  11. # project. The module is, however, dual licensed under OpenSSL and
  12. # CRYPTOGAMS licenses depending on where you obtain it. For further
  13. # details see http://www.openssl.org/~appro/cryptogams/.
  14. # ====================================================================
  15. #
  16. # May 2011
  17. #
  18. # The module implements bn_GF2m_mul_2x2 polynomial multiplication used
  19. # in bn_gf2m.c. It's kind of low-hanging mechanical port from C for
  20. # the time being... gcc 4.3 appeared to generate poor code, therefore
  21. # the effort. And indeed, the module delivers 55%-90%(*) improvement
  22. # on haviest ECDSA verify and ECDH benchmarks for 163- and 571-bit
  23. # key lengths on z990, 30%-55%(*) - on z10, and 70%-110%(*) - on z196.
  24. # This is for 64-bit build. In 32-bit "highgprs" case improvement is
  25. # even higher, for example on z990 it was measured 80%-150%. ECDSA
  26. # sign is modest 9%-12% faster. Keep in mind that these coefficients
  27. # are not ones for bn_GF2m_mul_2x2 itself, as not all CPU time is
  28. # burnt in it...
  29. #
  30. # (*) gcc 4.1 was observed to deliver better results than gcc 4.3,
  31. # so that improvement coefficients can vary from one specific
  32. # setup to another.
  33. $flavour = shift;
  34. if ($flavour =~ /3[12]/) {
  35. $SIZE_T=4;
  36. $g="";
  37. } else {
  38. $SIZE_T=8;
  39. $g="g";
  40. }
  41. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  42. open STDOUT,">$output";
  43. $stdframe=16*$SIZE_T+4*8;
  44. $rp="%r2";
  45. $a1="%r3";
  46. $a0="%r4";
  47. $b1="%r5";
  48. $b0="%r6";
  49. $ra="%r14";
  50. $sp="%r15";
  51. @T=("%r0","%r1");
  52. @i=("%r12","%r13");
  53. ($a1,$a2,$a4,$a8,$a12,$a48)=map("%r$_",(6..11));
  54. ($lo,$hi,$b)=map("%r$_",(3..5)); $a=$lo; $mask=$a8;
  55. $code.=<<___;
  56. .text
  57. .type _mul_1x1,\@function
  58. .align 16
  59. _mul_1x1:
  60. lgr $a1,$a
  61. sllg $a2,$a,1
  62. sllg $a4,$a,2
  63. sllg $a8,$a,3
  64. srag $lo,$a1,63 # broadcast 63rd bit
  65. nihh $a1,0x1fff
  66. srag @i[0],$a2,63 # broadcast 62nd bit
  67. nihh $a2,0x3fff
  68. srag @i[1],$a4,63 # broadcast 61st bit
  69. nihh $a4,0x7fff
  70. ngr $lo,$b
  71. ngr @i[0],$b
  72. ngr @i[1],$b
  73. lghi @T[0],0
  74. lgr $a12,$a1
  75. stg @T[0],`$stdframe+0*8`($sp) # tab[0]=0
  76. xgr $a12,$a2
  77. stg $a1,`$stdframe+1*8`($sp) # tab[1]=a1
  78. lgr $a48,$a4
  79. stg $a2,`$stdframe+2*8`($sp) # tab[2]=a2
  80. xgr $a48,$a8
  81. stg $a12,`$stdframe+3*8`($sp) # tab[3]=a1^a2
  82. xgr $a1,$a4
  83. stg $a4,`$stdframe+4*8`($sp) # tab[4]=a4
  84. xgr $a2,$a4
  85. stg $a1,`$stdframe+5*8`($sp) # tab[5]=a1^a4
  86. xgr $a12,$a4
  87. stg $a2,`$stdframe+6*8`($sp) # tab[6]=a2^a4
  88. xgr $a1,$a48
  89. stg $a12,`$stdframe+7*8`($sp) # tab[7]=a1^a2^a4
  90. xgr $a2,$a48
  91. stg $a8,`$stdframe+8*8`($sp) # tab[8]=a8
  92. xgr $a12,$a48
  93. stg $a1,`$stdframe+9*8`($sp) # tab[9]=a1^a8
  94. xgr $a1,$a4
  95. stg $a2,`$stdframe+10*8`($sp) # tab[10]=a2^a8
  96. xgr $a2,$a4
  97. stg $a12,`$stdframe+11*8`($sp) # tab[11]=a1^a2^a8
  98. xgr $a12,$a4
  99. stg $a48,`$stdframe+12*8`($sp) # tab[12]=a4^a8
  100. srlg $hi,$lo,1
  101. stg $a1,`$stdframe+13*8`($sp) # tab[13]=a1^a4^a8
  102. sllg $lo,$lo,63
  103. stg $a2,`$stdframe+14*8`($sp) # tab[14]=a2^a4^a8
  104. srlg @T[0],@i[0],2
  105. stg $a12,`$stdframe+15*8`($sp) # tab[15]=a1^a2^a4^a8
  106. lghi $mask,`0xf<<3`
  107. sllg $a1,@i[0],62
  108. sllg @i[0],$b,3
  109. srlg @T[1],@i[1],3
  110. ngr @i[0],$mask
  111. sllg $a2,@i[1],61
  112. srlg @i[1],$b,4-3
  113. xgr $hi,@T[0]
  114. ngr @i[1],$mask
  115. xgr $lo,$a1
  116. xgr $hi,@T[1]
  117. xgr $lo,$a2
  118. xg $lo,$stdframe(@i[0],$sp)
  119. srlg @i[0],$b,8-3
  120. ngr @i[0],$mask
  121. ___
  122. for($n=1;$n<14;$n++) {
  123. $code.=<<___;
  124. lg @T[1],$stdframe(@i[1],$sp)
  125. srlg @i[1],$b,`($n+2)*4`-3
  126. sllg @T[0],@T[1],`$n*4`
  127. ngr @i[1],$mask
  128. srlg @T[1],@T[1],`64-$n*4`
  129. xgr $lo,@T[0]
  130. xgr $hi,@T[1]
  131. ___
  132. push(@i,shift(@i)); push(@T,shift(@T));
  133. }
  134. $code.=<<___;
  135. lg @T[1],$stdframe(@i[1],$sp)
  136. sllg @T[0],@T[1],`$n*4`
  137. srlg @T[1],@T[1],`64-$n*4`
  138. xgr $lo,@T[0]
  139. xgr $hi,@T[1]
  140. lg @T[0],$stdframe(@i[0],$sp)
  141. sllg @T[1],@T[0],`($n+1)*4`
  142. srlg @T[0],@T[0],`64-($n+1)*4`
  143. xgr $lo,@T[1]
  144. xgr $hi,@T[0]
  145. br $ra
  146. .size _mul_1x1,.-_mul_1x1
  147. .globl bn_GF2m_mul_2x2
  148. .type bn_GF2m_mul_2x2,\@function
  149. .align 16
  150. bn_GF2m_mul_2x2:
  151. stm${g} %r3,%r15,3*$SIZE_T($sp)
  152. lghi %r1,-$stdframe-128
  153. la %r0,0($sp)
  154. la $sp,0(%r1,$sp) # alloca
  155. st${g} %r0,0($sp) # back chain
  156. ___
  157. if ($SIZE_T==8) {
  158. my @r=map("%r$_",(6..9));
  159. $code.=<<___;
  160. bras $ra,_mul_1x1 # a1·b1
  161. stmg $lo,$hi,16($rp)
  162. lg $a,`$stdframe+128+4*$SIZE_T`($sp)
  163. lg $b,`$stdframe+128+6*$SIZE_T`($sp)
  164. bras $ra,_mul_1x1 # a0·b0
  165. stmg $lo,$hi,0($rp)
  166. lg $a,`$stdframe+128+3*$SIZE_T`($sp)
  167. lg $b,`$stdframe+128+5*$SIZE_T`($sp)
  168. xg $a,`$stdframe+128+4*$SIZE_T`($sp)
  169. xg $b,`$stdframe+128+6*$SIZE_T`($sp)
  170. bras $ra,_mul_1x1 # (a0+a1)·(b0+b1)
  171. lmg @r[0],@r[3],0($rp)
  172. xgr $lo,$hi
  173. xgr $hi,@r[1]
  174. xgr $lo,@r[0]
  175. xgr $hi,@r[2]
  176. xgr $lo,@r[3]
  177. xgr $hi,@r[3]
  178. xgr $lo,$hi
  179. stg $hi,16($rp)
  180. stg $lo,8($rp)
  181. ___
  182. } else {
  183. $code.=<<___;
  184. sllg %r3,%r3,32
  185. sllg %r5,%r5,32
  186. or %r3,%r4
  187. or %r5,%r6
  188. bras $ra,_mul_1x1
  189. rllg $lo,$lo,32
  190. rllg $hi,$hi,32
  191. stmg $lo,$hi,0($rp)
  192. ___
  193. }
  194. $code.=<<___;
  195. lm${g} %r6,%r15,`$stdframe+128+6*$SIZE_T`($sp)
  196. br $ra
  197. .size bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2
  198. .string "GF(2^m) Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>"
  199. ___
  200. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  201. print $code;
  202. close STDOUT;