sparcv9-gf2m.pl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #! /usr/bin/env perl
  2. # Copyright 2012-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. # October 2012
  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... Except that it has two code paths: one suitable
  21. # for all SPARCv9 processors and one for VIS3-capable ones. Former
  22. # delivers ~25-45% more, more for longer keys, heaviest DH and DSA
  23. # verify operations on venerable UltraSPARC II. On T4 VIS3 code is
  24. # ~100-230% faster than gcc-generated code and ~35-90% faster than
  25. # the pure SPARCv9 code path.
  26. $output = pop;
  27. open STDOUT,">$output";
  28. $locals=16*8;
  29. $tab="%l0";
  30. @T=("%g2","%g3");
  31. @i=("%g4","%g5");
  32. ($a1,$a2,$a4,$a8,$a12,$a48)=map("%o$_",(0..5));
  33. ($lo,$hi,$b)=("%g1",$a8,"%o7"); $a=$lo;
  34. $code.=<<___;
  35. #include <sparc_arch.h>
  36. #ifdef __arch64__
  37. .register %g2,#scratch
  38. .register %g3,#scratch
  39. #endif
  40. #ifdef __PIC__
  41. SPARC_PIC_THUNK(%g1)
  42. #endif
  43. .globl bn_GF2m_mul_2x2
  44. .align 16
  45. bn_GF2m_mul_2x2:
  46. SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
  47. ld [%g1+0],%g1 ! OPENSSL_sparcv9cap_P[0]
  48. andcc %g1, SPARCV9_VIS3, %g0
  49. bz,pn %icc,.Lsoftware
  50. nop
  51. sllx %o1, 32, %o1
  52. sllx %o3, 32, %o3
  53. or %o2, %o1, %o1
  54. or %o4, %o3, %o3
  55. .word 0x95b262ab ! xmulx %o1, %o3, %o2
  56. .word 0x99b262cb ! xmulxhi %o1, %o3, %o4
  57. srlx %o2, 32, %o1 ! 13 cycles later
  58. st %o2, [%o0+0]
  59. st %o1, [%o0+4]
  60. srlx %o4, 32, %o3
  61. st %o4, [%o0+8]
  62. retl
  63. st %o3, [%o0+12]
  64. .align 16
  65. .Lsoftware:
  66. save %sp,-STACK_FRAME-$locals,%sp
  67. sllx %i1,32,$a
  68. mov -1,$a12
  69. sllx %i3,32,$b
  70. or %i2,$a,$a
  71. srlx $a12,1,$a48 ! 0x7fff...
  72. or %i4,$b,$b
  73. srlx $a12,2,$a12 ! 0x3fff...
  74. add %sp,STACK_BIAS+STACK_FRAME,$tab
  75. sllx $a,2,$a4
  76. mov $a,$a1
  77. sllx $a,1,$a2
  78. srax $a4,63,@i[1] ! broadcast 61st bit
  79. and $a48,$a4,$a4 ! (a<<2)&0x7fff...
  80. srlx $a48,2,$a48
  81. srax $a2,63,@i[0] ! broadcast 62nd bit
  82. and $a12,$a2,$a2 ! (a<<1)&0x3fff...
  83. srax $a1,63,$lo ! broadcast 63rd bit
  84. and $a48,$a1,$a1 ! (a<<0)&0x1fff...
  85. sllx $a1,3,$a8
  86. and $b,$lo,$lo
  87. and $b,@i[0],@i[0]
  88. and $b,@i[1],@i[1]
  89. stx %g0,[$tab+0*8] ! tab[0]=0
  90. xor $a1,$a2,$a12
  91. stx $a1,[$tab+1*8] ! tab[1]=a1
  92. stx $a2,[$tab+2*8] ! tab[2]=a2
  93. xor $a4,$a8,$a48
  94. stx $a12,[$tab+3*8] ! tab[3]=a1^a2
  95. xor $a4,$a1,$a1
  96. stx $a4,[$tab+4*8] ! tab[4]=a4
  97. xor $a4,$a2,$a2
  98. stx $a1,[$tab+5*8] ! tab[5]=a1^a4
  99. xor $a4,$a12,$a12
  100. stx $a2,[$tab+6*8] ! tab[6]=a2^a4
  101. xor $a48,$a1,$a1
  102. stx $a12,[$tab+7*8] ! tab[7]=a1^a2^a4
  103. xor $a48,$a2,$a2
  104. stx $a8,[$tab+8*8] ! tab[8]=a8
  105. xor $a48,$a12,$a12
  106. stx $a1,[$tab+9*8] ! tab[9]=a1^a8
  107. xor $a4,$a1,$a1
  108. stx $a2,[$tab+10*8] ! tab[10]=a2^a8
  109. xor $a4,$a2,$a2
  110. stx $a12,[$tab+11*8] ! tab[11]=a1^a2^a8
  111. xor $a4,$a12,$a12
  112. stx $a48,[$tab+12*8] ! tab[12]=a4^a8
  113. srlx $lo,1,$hi
  114. stx $a1,[$tab+13*8] ! tab[13]=a1^a4^a8
  115. sllx $lo,63,$lo
  116. stx $a2,[$tab+14*8] ! tab[14]=a2^a4^a8
  117. srlx @i[0],2,@T[0]
  118. stx $a12,[$tab+15*8] ! tab[15]=a1^a2^a4^a8
  119. sllx @i[0],62,$a1
  120. sllx $b,3,@i[0]
  121. srlx @i[1],3,@T[1]
  122. and @i[0],`0xf<<3`,@i[0]
  123. sllx @i[1],61,$a2
  124. ldx [$tab+@i[0]],@i[0]
  125. srlx $b,4-3,@i[1]
  126. xor @T[0],$hi,$hi
  127. and @i[1],`0xf<<3`,@i[1]
  128. xor $a1,$lo,$lo
  129. ldx [$tab+@i[1]],@i[1]
  130. xor @T[1],$hi,$hi
  131. xor @i[0],$lo,$lo
  132. srlx $b,8-3,@i[0]
  133. xor $a2,$lo,$lo
  134. and @i[0],`0xf<<3`,@i[0]
  135. ___
  136. for($n=1;$n<14;$n++) {
  137. $code.=<<___;
  138. sllx @i[1],`$n*4`,@T[0]
  139. ldx [$tab+@i[0]],@i[0]
  140. srlx @i[1],`64-$n*4`,@T[1]
  141. xor @T[0],$lo,$lo
  142. srlx $b,`($n+2)*4`-3,@i[1]
  143. xor @T[1],$hi,$hi
  144. and @i[1],`0xf<<3`,@i[1]
  145. ___
  146. push(@i,shift(@i)); push(@T,shift(@T));
  147. }
  148. $code.=<<___;
  149. sllx @i[1],`$n*4`,@T[0]
  150. ldx [$tab+@i[0]],@i[0]
  151. srlx @i[1],`64-$n*4`,@T[1]
  152. xor @T[0],$lo,$lo
  153. sllx @i[0],`($n+1)*4`,@T[0]
  154. xor @T[1],$hi,$hi
  155. srlx @i[0],`64-($n+1)*4`,@T[1]
  156. xor @T[0],$lo,$lo
  157. xor @T[1],$hi,$hi
  158. srlx $lo,32,%i1
  159. st $lo,[%i0+0]
  160. st %i1,[%i0+4]
  161. srlx $hi,32,%i2
  162. st $hi,[%i0+8]
  163. st %i2,[%i0+12]
  164. ret
  165. restore
  166. .type bn_GF2m_mul_2x2,#function
  167. .size bn_GF2m_mul_2x2,.-bn_GF2m_mul_2x2
  168. .asciz "GF(2^m) Multiplication for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"
  169. .align 4
  170. ___
  171. $code =~ s/\`([^\`]*)\`/eval($1)/gem;
  172. print $code;
  173. close STDOUT;