sha1-s390x.pl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #! /usr/bin/env perl
  2. # Copyright 2007-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. # 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. # SHA1 block procedure for s390x.
  15. # April 2007.
  16. #
  17. # Performance is >30% better than gcc 3.3 generated code. But the real
  18. # twist is that SHA1 hardware support is detected and utilized. In
  19. # which case performance can reach further >4.5x for larger chunks.
  20. # January 2009.
  21. #
  22. # Optimize Xupdate for amount of memory references and reschedule
  23. # instructions to favour dual-issue z10 pipeline. On z10 hardware is
  24. # "only" ~2.3x faster than software.
  25. # November 2010.
  26. #
  27. # Adapt for -m31 build. If kernel supports what's called "highgprs"
  28. # feature on Linux [see /proc/cpuinfo], it's possible to use 64-bit
  29. # instructions and achieve "64-bit" performance even in 31-bit legacy
  30. # application context. The feature is not specific to any particular
  31. # processor, as long as it's "z-CPU". Latter implies that the code
  32. # remains z/Architecture specific. On z990 it was measured to perform
  33. # 23% better than code generated by gcc 4.3.
  34. $kimdfunc=1; # magic function code for kimd instruction
  35. $flavour = shift;
  36. if ($flavour =~ /3[12]/) {
  37. $SIZE_T=4;
  38. $g="";
  39. } else {
  40. $SIZE_T=8;
  41. $g="g";
  42. }
  43. while (($output=shift) && ($output!~/\w[\w\-]*\.\w+$/)) {}
  44. open STDOUT,">$output";
  45. $K_00_39="%r0"; $K=$K_00_39;
  46. $K_40_79="%r1";
  47. $ctx="%r2"; $prefetch="%r2";
  48. $inp="%r3";
  49. $len="%r4";
  50. $A="%r5";
  51. $B="%r6";
  52. $C="%r7";
  53. $D="%r8";
  54. $E="%r9"; @V=($A,$B,$C,$D,$E);
  55. $t0="%r10";
  56. $t1="%r11";
  57. @X=("%r12","%r13","%r14");
  58. $sp="%r15";
  59. $stdframe=16*$SIZE_T+4*8;
  60. $frame=$stdframe+16*4;
  61. sub Xupdate {
  62. my $i=shift;
  63. $code.=<<___ if ($i==15);
  64. lg $prefetch,$stdframe($sp) ### Xupdate(16) warm-up
  65. lr $X[0],$X[2]
  66. ___
  67. return if ($i&1); # Xupdate is vectorized and executed every 2nd cycle
  68. $code.=<<___ if ($i<16);
  69. lg $X[0],`$i*4`($inp) ### Xload($i)
  70. rllg $X[1],$X[0],32
  71. ___
  72. $code.=<<___ if ($i>=16);
  73. xgr $X[0],$prefetch ### Xupdate($i)
  74. lg $prefetch,`$stdframe+4*(($i+2)%16)`($sp)
  75. xg $X[0],`$stdframe+4*(($i+8)%16)`($sp)
  76. xgr $X[0],$prefetch
  77. rll $X[0],$X[0],1
  78. rllg $X[1],$X[0],32
  79. rll $X[1],$X[1],1
  80. rllg $X[0],$X[1],32
  81. lr $X[2],$X[1] # feedback
  82. ___
  83. $code.=<<___ if ($i<=70);
  84. stg $X[0],`$stdframe+4*($i%16)`($sp)
  85. ___
  86. unshift(@X,pop(@X));
  87. }
  88. sub BODY_00_19 {
  89. my ($i,$a,$b,$c,$d,$e)=@_;
  90. my $xi=$X[1];
  91. &Xupdate($i);
  92. $code.=<<___;
  93. alr $e,$K ### $i
  94. rll $t1,$a,5
  95. lr $t0,$d
  96. xr $t0,$c
  97. alr $e,$t1
  98. nr $t0,$b
  99. alr $e,$xi
  100. xr $t0,$d
  101. rll $b,$b,30
  102. alr $e,$t0
  103. ___
  104. }
  105. sub BODY_20_39 {
  106. my ($i,$a,$b,$c,$d,$e)=@_;
  107. my $xi=$X[1];
  108. &Xupdate($i);
  109. $code.=<<___;
  110. alr $e,$K ### $i
  111. rll $t1,$a,5
  112. lr $t0,$b
  113. alr $e,$t1
  114. xr $t0,$c
  115. alr $e,$xi
  116. xr $t0,$d
  117. rll $b,$b,30
  118. alr $e,$t0
  119. ___
  120. }
  121. sub BODY_40_59 {
  122. my ($i,$a,$b,$c,$d,$e)=@_;
  123. my $xi=$X[1];
  124. &Xupdate($i);
  125. $code.=<<___;
  126. alr $e,$K ### $i
  127. rll $t1,$a,5
  128. lr $t0,$b
  129. alr $e,$t1
  130. or $t0,$c
  131. lr $t1,$b
  132. nr $t0,$d
  133. nr $t1,$c
  134. alr $e,$xi
  135. or $t0,$t1
  136. rll $b,$b,30
  137. alr $e,$t0
  138. ___
  139. }
  140. $code.=<<___;
  141. #include "s390x_arch.h"
  142. .text
  143. .align 64
  144. .type Ktable,\@object
  145. Ktable: .long 0x5a827999,0x6ed9eba1,0x8f1bbcdc,0xca62c1d6
  146. .skip 48 #.long 0,0,0,0,0,0,0,0,0,0,0,0
  147. .size Ktable,.-Ktable
  148. .globl sha1_block_data_order
  149. .type sha1_block_data_order,\@function
  150. sha1_block_data_order:
  151. ___
  152. $code.=<<___ if ($kimdfunc);
  153. larl %r1,OPENSSL_s390xcap_P
  154. lg %r0,S390X_KIMD(%r1) # check kimd capabilities
  155. tmhh %r0,`0x8000>>$kimdfunc`
  156. jz .Lsoftware
  157. lghi %r0,$kimdfunc
  158. lgr %r1,$ctx
  159. lgr %r2,$inp
  160. sllg %r3,$len,6
  161. .long 0xb93e0002 # kimd %r0,%r2
  162. brc 1,.-4 # pay attention to "partial completion"
  163. br %r14
  164. .align 16
  165. .Lsoftware:
  166. ___
  167. $code.=<<___;
  168. lghi %r1,-$frame
  169. st${g} $ctx,`2*$SIZE_T`($sp)
  170. stm${g} %r6,%r15,`6*$SIZE_T`($sp)
  171. lgr %r0,$sp
  172. la $sp,0(%r1,$sp)
  173. st${g} %r0,0($sp)
  174. larl $t0,Ktable
  175. llgf $A,0($ctx)
  176. llgf $B,4($ctx)
  177. llgf $C,8($ctx)
  178. llgf $D,12($ctx)
  179. llgf $E,16($ctx)
  180. lg $K_00_39,0($t0)
  181. lg $K_40_79,8($t0)
  182. .Lloop:
  183. rllg $K_00_39,$K_00_39,32
  184. ___
  185. for ($i=0;$i<20;$i++) { &BODY_00_19($i,@V); unshift(@V,pop(@V)); }
  186. $code.=<<___;
  187. rllg $K_00_39,$K_00_39,32
  188. ___
  189. for (;$i<40;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  190. $code.=<<___; $K=$K_40_79;
  191. rllg $K_40_79,$K_40_79,32
  192. ___
  193. for (;$i<60;$i++) { &BODY_40_59($i,@V); unshift(@V,pop(@V)); }
  194. $code.=<<___;
  195. rllg $K_40_79,$K_40_79,32
  196. ___
  197. for (;$i<80;$i++) { &BODY_20_39($i,@V); unshift(@V,pop(@V)); }
  198. $code.=<<___;
  199. l${g} $ctx,`$frame+2*$SIZE_T`($sp)
  200. la $inp,64($inp)
  201. al $A,0($ctx)
  202. al $B,4($ctx)
  203. al $C,8($ctx)
  204. al $D,12($ctx)
  205. al $E,16($ctx)
  206. st $A,0($ctx)
  207. st $B,4($ctx)
  208. st $C,8($ctx)
  209. st $D,12($ctx)
  210. st $E,16($ctx)
  211. brct${g} $len,.Lloop
  212. lm${g} %r6,%r15,`$frame+6*$SIZE_T`($sp)
  213. br %r14
  214. .size sha1_block_data_order,.-sha1_block_data_order
  215. .string "SHA1 block transform for s390x, CRYPTOGAMS by <appro\@openssl.org>"
  216. ___
  217. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  218. print $code;
  219. close STDOUT;