2
0

s390x-gf2m.pl 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #! /usr/bin/env perl
  2. # Copyright 2011-2016 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. # ====================================================================
  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 heaviest 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. # $output is the last argument if it looks like a file (it has an extension)
  34. # $flavour is the first argument if it doesn't look like a file
  35. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  36. $flavour = $#ARGV >= 0 && $ARGV[0] !~ m|\.| ? shift : undef;
  37. if ($flavour =~ /3[12]/) {
  38. $SIZE_T=4;
  39. $g="";
  40. } else {
  41. $SIZE_T=8;
  42. $g="g";
  43. }
  44. $output and open STDOUT,">$output";
  45. $stdframe=16*$SIZE_T+4*8;
  46. $rp="%r2";
  47. $a1="%r3";
  48. $a0="%r4";
  49. $b1="%r5";
  50. $b0="%r6";
  51. $ra="%r14";
  52. $sp="%r15";
  53. @T=("%r0","%r1");
  54. @i=("%r12","%r13");
  55. ($a1,$a2,$a4,$a8,$a12,$a48)=map("%r$_",(6..11));
  56. ($lo,$hi,$b)=map("%r$_",(3..5)); $a=$lo; $mask=$a8;
  57. $code.=<<___;
  58. .text
  59. .type _mul_1x1,\@function
  60. .align 16
  61. _mul_1x1:
  62. lgr $a1,$a
  63. sllg $a2,$a,1
  64. sllg $a4,$a,2
  65. sllg $a8,$a,3
  66. srag $lo,$a1,63 # broadcast 63rd bit
  67. nihh $a1,0x1fff
  68. srag @i[0],$a2,63 # broadcast 62nd bit
  69. nihh $a2,0x3fff
  70. srag @i[1],$a4,63 # broadcast 61st bit
  71. nihh $a4,0x7fff
  72. ngr $lo,$b
  73. ngr @i[0],$b
  74. ngr @i[1],$b
  75. lghi @T[0],0
  76. lgr $a12,$a1
  77. stg @T[0],`$stdframe+0*8`($sp) # tab[0]=0
  78. xgr $a12,$a2
  79. stg $a1,`$stdframe+1*8`($sp) # tab[1]=a1
  80. lgr $a48,$a4
  81. stg $a2,`$stdframe+2*8`($sp) # tab[2]=a2
  82. xgr $a48,$a8
  83. stg $a12,`$stdframe+3*8`($sp) # tab[3]=a1^a2
  84. xgr $a1,$a4
  85. stg $a4,`$stdframe+4*8`($sp) # tab[4]=a4
  86. xgr $a2,$a4
  87. stg $a1,`$stdframe+5*8`($sp) # tab[5]=a1^a4
  88. xgr $a12,$a4
  89. stg $a2,`$stdframe+6*8`($sp) # tab[6]=a2^a4
  90. xgr $a1,$a48
  91. stg $a12,`$stdframe+7*8`($sp) # tab[7]=a1^a2^a4
  92. xgr $a2,$a48
  93. stg $a8,`$stdframe+8*8`($sp) # tab[8]=a8
  94. xgr $a12,$a48
  95. stg $a1,`$stdframe+9*8`($sp) # tab[9]=a1^a8
  96. xgr $a1,$a4
  97. stg $a2,`$stdframe+10*8`($sp) # tab[10]=a2^a8
  98. xgr $a2,$a4
  99. stg $a12,`$stdframe+11*8`($sp) # tab[11]=a1^a2^a8
  100. xgr $a12,$a4
  101. stg $a48,`$stdframe+12*8`($sp) # tab[12]=a4^a8
  102. srlg $hi,$lo,1
  103. stg $a1,`$stdframe+13*8`($sp) # tab[13]=a1^a4^a8
  104. sllg $lo,$lo,63
  105. stg $a2,`$stdframe+14*8`($sp) # tab[14]=a2^a4^a8
  106. srlg @T[0],@i[0],2
  107. stg $a12,`$stdframe+15*8`($sp) # tab[15]=a1^a2^a4^a8
  108. lghi $mask,`0xf<<3`
  109. sllg $a1,@i[0],62
  110. sllg @i[0],$b,3
  111. srlg @T[1],@i[1],3
  112. ngr @i[0],$mask
  113. sllg $a2,@i[1],61
  114. srlg @i[1],$b,4-3
  115. xgr $hi,@T[0]
  116. ngr @i[1],$mask
  117. xgr $lo,$a1
  118. xgr $hi,@T[1]
  119. xgr $lo,$a2
  120. xg $lo,$stdframe(@i[0],$sp)
  121. srlg @i[0],$b,8-3
  122. ngr @i[0],$mask
  123. ___
  124. for($n=1;$n<14;$n++) {
  125. $code.=<<___;
  126. lg @T[1],$stdframe(@i[1],$sp)
  127. srlg @i[1],$b,`($n+2)*4`-3
  128. sllg @T[0],@T[1],`$n*4`
  129. ngr @i[1],$mask
  130. srlg @T[1],@T[1],`64-$n*4`
  131. xgr $lo,@T[0]
  132. xgr $hi,@T[1]
  133. ___
  134. push(@i,shift(@i)); push(@T,shift(@T));
  135. }
  136. $code.=<<___;
  137. lg @T[1],$stdframe(@i[1],$sp)
  138. sllg @T[0],@T[1],`$n*4`
  139. srlg @T[1],@T[1],`64-$n*4`
  140. xgr $lo,@T[0]
  141. xgr $hi,@T[1]
  142. lg @T[0],$stdframe(@i[0],$sp)
  143. sllg @T[1],@T[0],`($n+1)*4`
  144. srlg @T[0],@T[0],`64-($n+1)*4`
  145. xgr $lo,@T[1]
  146. xgr $hi,@T[0]
  147. br $ra
  148. .size _mul_1x1,.-_mul_1x1
  149. .globl bn_GF2m_mul_2x2
  150. .type bn_GF2m_mul_2x2,\@function
  151. .align 16
  152. bn_GF2m_mul_2x2:
  153. stm${g} %r3,%r15,3*$SIZE_T($sp)
  154. lghi %r1,-$stdframe-128
  155. la %r0,0($sp)
  156. la $sp,0(%r1,$sp) # alloca
  157. st${g} %r0,0($sp) # back chain
  158. ___
  159. if ($SIZE_T==8) {
  160. my @r=map("%r$_",(6..9));
  161. $code.=<<___;
  162. bras $ra,_mul_1x1 # a1·b1
  163. stmg $lo,$hi,16($rp)
  164. lg $a,`$stdframe+128+4*$SIZE_T`($sp)
  165. lg $b,`$stdframe+128+6*$SIZE_T`($sp)
  166. bras $ra,_mul_1x1 # a0·b0
  167. stmg $lo,$hi,0($rp)
  168. lg $a,`$stdframe+128+3*$SIZE_T`($sp)
  169. lg $b,`$stdframe+128+5*$SIZE_T`($sp)
  170. xg $a,`$stdframe+128+4*$SIZE_T`($sp)
  171. xg $b,`$stdframe+128+6*$SIZE_T`($sp)
  172. bras $ra,_mul_1x1 # (a0+a1)·(b0+b1)
  173. lmg @r[0],@r[3],0($rp)
  174. xgr $lo,$hi
  175. xgr $hi,@r[1]
  176. xgr $lo,@r[0]
  177. xgr $hi,@r[2]
  178. xgr $lo,@r[3]
  179. xgr $hi,@r[3]
  180. xgr $lo,$hi
  181. stg $hi,16($rp)
  182. stg $lo,8($rp)
  183. ___
  184. } else {
  185. $code.=<<___;
  186. sllg %r3,%r3,32
  187. sllg %r5,%r5,32
  188. or %r3,%r4
  189. or %r5,%r6
  190. bras $ra,_mul_1x1
  191. rllg $lo,$lo,32
  192. rllg $hi,$hi,32
  193. stmg $lo,$hi,0($rp)
  194. ___
  195. }
  196. $code.=<<___;
  197. lm${g} %r6,%r15,`$stdframe+128+6*$SIZE_T`($sp)
  198. br $ra
  199. .size bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2
  200. .string "GF(2^m) Multiplication for s390x, CRYPTOGAMS by <appro\@openssl.org>"
  201. ___
  202. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  203. print $code;
  204. close STDOUT or die "error closing STDOUT";