sha1-ia64.pl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #! /usr/bin/env perl
  2. # Copyright 2004-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. # Eternal question is what's wrong with compiler generated code? The
  17. # trick is that it's possible to reduce the number of shifts required
  18. # to perform rotations by maintaining copy of 32-bit value in upper
  19. # bits of 64-bit register. Just follow mux2 and shrp instructions...
  20. # Performance under big-endian OS such as HP-UX is 179MBps*1GHz, which
  21. # is >50% better than HP C and >2x better than gcc.
  22. # $output is the last argument if it looks like a file (it has an extension)
  23. $output = $#ARGV >= 0 && $ARGV[$#ARGV] =~ m|\.\w+$| ? pop : undef;
  24. $code=<<___;
  25. .ident \"sha1-ia64.s, version 1.3\"
  26. .ident \"IA-64 ISA artwork by Andy Polyakov <appro\@fy.chalmers.se>\"
  27. .explicit
  28. ___
  29. if ($^O eq "hpux") {
  30. $ADDP="addp4";
  31. for (@ARGV) { $ADDP="add" if (/[\+DD|\-mlp]64/); }
  32. } else { $ADDP="add"; }
  33. #$human=1;
  34. if ($human) { # useful for visual code auditing...
  35. ($A,$B,$C,$D,$E) = ("A","B","C","D","E");
  36. ($h0,$h1,$h2,$h3,$h4) = ("h0","h1","h2","h3","h4");
  37. ($K_00_19, $K_20_39, $K_40_59, $K_60_79) =
  38. ( "K_00_19","K_20_39","K_40_59","K_60_79" );
  39. @X= ( "X0", "X1", "X2", "X3", "X4", "X5", "X6", "X7",
  40. "X8", "X9","X10","X11","X12","X13","X14","X15" );
  41. }
  42. else {
  43. ($A,$B,$C,$D,$E) = ("loc0","loc1","loc2","loc3","loc4");
  44. ($h0,$h1,$h2,$h3,$h4) = ("loc5","loc6","loc7","loc8","loc9");
  45. ($K_00_19, $K_20_39, $K_40_59, $K_60_79) =
  46. ( "r14", "r15", "loc10", "loc11" );
  47. @X= ( "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  48. "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31" );
  49. }
  50. sub BODY_00_15 {
  51. local *code=shift;
  52. my ($i,$a,$b,$c,$d,$e)=@_;
  53. my $j=$i+1;
  54. my $Xn=@X[$j%16];
  55. $code.=<<___ if ($i==0);
  56. { .mmi; ld1 $X[$i]=[inp],2 // MSB
  57. ld1 tmp2=[tmp3],2 };;
  58. { .mmi; ld1 tmp0=[inp],2
  59. ld1 tmp4=[tmp3],2 // LSB
  60. dep $X[$i]=$X[$i],tmp2,8,8 };;
  61. ___
  62. if ($i<15) {
  63. $code.=<<___;
  64. { .mmi; ld1 $Xn=[inp],2 // forward Xload
  65. nop.m 0x0
  66. dep tmp1=tmp0,tmp4,8,8 };;
  67. { .mmi; ld1 tmp2=[tmp3],2 // forward Xload
  68. and tmp4=$c,$b
  69. dep $X[$i]=$X[$i],tmp1,16,16} //;;
  70. { .mmi; add $e=$e,$K_00_19 // e+=K_00_19
  71. andcm tmp1=$d,$b
  72. dep.z tmp5=$a,5,27 };; // a<<5
  73. { .mmi; add $e=$e,$X[$i] // e+=Xload
  74. or tmp4=tmp4,tmp1 // F_00_19(b,c,d)=(b&c)|(~b&d)
  75. extr.u tmp1=$a,27,5 };; // a>>27
  76. { .mmi; ld1 tmp0=[inp],2 // forward Xload
  77. add $e=$e,tmp4 // e+=F_00_19(b,c,d)
  78. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  79. { .mmi; ld1 tmp4=[tmp3],2 // forward Xload
  80. or tmp5=tmp1,tmp5 // ROTATE(a,5)
  81. mux2 tmp6=$a,0x44 };; // see b in next iteration
  82. { .mii; add $e=$e,tmp5 // e+=ROTATE(a,5)
  83. dep $Xn=$Xn,tmp2,8,8 // forward Xload
  84. mux2 $X[$i]=$X[$i],0x44 } //;;
  85. ___
  86. }
  87. else {
  88. $code.=<<___;
  89. { .mii; and tmp3=$c,$b
  90. dep tmp1=tmp0,tmp4,8,8;;
  91. dep $X[$i]=$X[$i],tmp1,16,16} //;;
  92. { .mmi; add $e=$e,$K_00_19 // e+=K_00_19
  93. andcm tmp1=$d,$b
  94. dep.z tmp5=$a,5,27 };; // a<<5
  95. { .mmi; add $e=$e,$X[$i] // e+=Xupdate
  96. or tmp4=tmp3,tmp1 // F_00_19(b,c,d)=(b&c)|(~b&d)
  97. extr.u tmp1=$a,27,5 } // a>>27
  98. { .mmi; xor $Xn=$Xn,$X[($j+2)%16] // forward Xupdate
  99. xor tmp3=$X[($j+8)%16],$X[($j+13)%16] // forward Xupdate
  100. nop.i 0 };;
  101. { .mmi; add $e=$e,tmp4 // e+=F_00_19(b,c,d)
  102. xor $Xn=$Xn,tmp3 // forward Xupdate
  103. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  104. { .mmi; or tmp1=tmp1,tmp5 // ROTATE(a,5)
  105. mux2 tmp6=$a,0x44 };; // see b in next iteration
  106. { .mii; add $e=$e,tmp1 // e+=ROTATE(a,5)
  107. shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
  108. mux2 $X[$i]=$X[$i],0x44 };;
  109. ___
  110. }
  111. }
  112. sub BODY_16_19 {
  113. local *code=shift;
  114. my ($i,$a,$b,$c,$d,$e)=@_;
  115. my $j=$i+1;
  116. my $Xn=@X[$j%16];
  117. $code.=<<___;
  118. { .mib; add $e=$e,$K_00_19 // e+=K_00_19
  119. dep.z tmp5=$a,5,27 } // a<<5
  120. { .mib; andcm tmp1=$d,$b
  121. and tmp0=$c,$b };;
  122. { .mmi; add $e=$e,$X[$i%16] // e+=Xupdate
  123. or tmp0=tmp0,tmp1 // F_00_19(b,c,d)=(b&c)|(~b&d)
  124. extr.u tmp1=$a,27,5 } // a>>27
  125. { .mmi; xor $Xn=$Xn,$X[($j+2)%16] // forward Xupdate
  126. xor tmp3=$X[($j+8)%16],$X[($j+13)%16] // forward Xupdate
  127. nop.i 0 };;
  128. { .mmi; add $e=$e,tmp0 // f+=F_00_19(b,c,d)
  129. xor $Xn=$Xn,tmp3 // forward Xupdate
  130. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  131. { .mmi; or tmp1=tmp1,tmp5 // ROTATE(a,5)
  132. mux2 tmp6=$a,0x44 };; // see b in next iteration
  133. { .mii; add $e=$e,tmp1 // e+=ROTATE(a,5)
  134. shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
  135. nop.i 0 };;
  136. ___
  137. }
  138. sub BODY_20_39 {
  139. local *code=shift;
  140. my ($i,$a,$b,$c,$d,$e,$Konst)=@_;
  141. $Konst = $K_20_39 if (!defined($Konst));
  142. my $j=$i+1;
  143. my $Xn=@X[$j%16];
  144. if ($i<79) {
  145. $code.=<<___;
  146. { .mib; add $e=$e,$Konst // e+=K_XX_XX
  147. dep.z tmp5=$a,5,27 } // a<<5
  148. { .mib; xor tmp0=$c,$b
  149. xor $Xn=$Xn,$X[($j+2)%16] };; // forward Xupdate
  150. { .mib; add $e=$e,$X[$i%16] // e+=Xupdate
  151. extr.u tmp1=$a,27,5 } // a>>27
  152. { .mib; xor tmp0=tmp0,$d // F_20_39(b,c,d)=b^c^d
  153. xor $Xn=$Xn,$X[($j+8)%16] };; // forward Xupdate
  154. { .mmi; add $e=$e,tmp0 // e+=F_20_39(b,c,d)
  155. xor $Xn=$Xn,$X[($j+13)%16] // forward Xupdate
  156. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  157. { .mmi; or tmp1=tmp1,tmp5 // ROTATE(a,5)
  158. mux2 tmp6=$a,0x44 };; // see b in next iteration
  159. { .mii; add $e=$e,tmp1 // e+=ROTATE(a,5)
  160. shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
  161. nop.i 0 };;
  162. ___
  163. }
  164. else {
  165. $code.=<<___;
  166. { .mib; add $e=$e,$Konst // e+=K_60_79
  167. dep.z tmp5=$a,5,27 } // a<<5
  168. { .mib; xor tmp0=$c,$b
  169. add $h1=$h1,$a };; // wrap up
  170. { .mib; add $e=$e,$X[$i%16] // e+=Xupdate
  171. extr.u tmp1=$a,27,5 } // a>>27
  172. { .mib; xor tmp0=tmp0,$d // F_20_39(b,c,d)=b^c^d
  173. add $h3=$h3,$c };; // wrap up
  174. { .mmi; add $e=$e,tmp0 // e+=F_20_39(b,c,d)
  175. or tmp1=tmp1,tmp5 // ROTATE(a,5)
  176. shrp $b=tmp6,tmp6,2 };; // b=ROTATE(b,30) ;;?
  177. { .mmi; add $e=$e,tmp1 // e+=ROTATE(a,5)
  178. add tmp3=1,inp // used in unaligned codepath
  179. add $h4=$h4,$d };; // wrap up
  180. ___
  181. }
  182. }
  183. sub BODY_40_59 {
  184. local *code=shift;
  185. my ($i,$a,$b,$c,$d,$e)=@_;
  186. my $j=$i+1;
  187. my $Xn=@X[$j%16];
  188. $code.=<<___;
  189. { .mib; add $e=$e,$K_40_59 // e+=K_40_59
  190. dep.z tmp5=$a,5,27 } // a<<5
  191. { .mib; and tmp1=$c,$d
  192. xor tmp0=$c,$d };;
  193. { .mmi; add $e=$e,$X[$i%16] // e+=Xupdate
  194. add tmp5=tmp5,tmp1 // a<<5+(c&d)
  195. extr.u tmp1=$a,27,5 } // a>>27
  196. { .mmi; and tmp0=tmp0,$b
  197. xor $Xn=$Xn,$X[($j+2)%16] // forward Xupdate
  198. xor tmp3=$X[($j+8)%16],$X[($j+13)%16] };; // forward Xupdate
  199. { .mmi; add $e=$e,tmp0 // e+=b&(c^d)
  200. add tmp5=tmp5,tmp1 // ROTATE(a,5)+(c&d)
  201. shrp $b=tmp6,tmp6,2 } // b=ROTATE(b,30)
  202. { .mmi; xor $Xn=$Xn,tmp3
  203. mux2 tmp6=$a,0x44 };; // see b in next iteration
  204. { .mii; add $e=$e,tmp5 // e+=ROTATE(a,5)+(c&d)
  205. shrp $Xn=$Xn,$Xn,31 // ROTATE(x[0]^x[2]^x[8]^x[13],1)
  206. nop.i 0x0 };;
  207. ___
  208. }
  209. sub BODY_60_79 { &BODY_20_39(@_,$K_60_79); }
  210. $code.=<<___;
  211. .text
  212. tmp0=r8;
  213. tmp1=r9;
  214. tmp2=r10;
  215. tmp3=r11;
  216. ctx=r32; // in0
  217. inp=r33; // in1
  218. // void sha1_block_data_order(SHA_CTX *c,const void *p,size_t num);
  219. .global sha1_block_data_order#
  220. .proc sha1_block_data_order#
  221. .align 32
  222. sha1_block_data_order:
  223. .prologue
  224. { .mmi; alloc tmp1=ar.pfs,3,14,0,0
  225. $ADDP tmp0=4,ctx
  226. .save ar.lc,r3
  227. mov r3=ar.lc }
  228. { .mmi; $ADDP ctx=0,ctx
  229. $ADDP inp=0,inp
  230. mov r2=pr };;
  231. tmp4=in2;
  232. tmp5=loc12;
  233. tmp6=loc13;
  234. .body
  235. { .mlx; ld4 $h0=[ctx],8
  236. movl $K_00_19=0x5a827999 }
  237. { .mlx; ld4 $h1=[tmp0],8
  238. movl $K_20_39=0x6ed9eba1 };;
  239. { .mlx; ld4 $h2=[ctx],8
  240. movl $K_40_59=0x8f1bbcdc }
  241. { .mlx; ld4 $h3=[tmp0]
  242. movl $K_60_79=0xca62c1d6 };;
  243. { .mmi; ld4 $h4=[ctx],-16
  244. add in2=-1,in2 // adjust num for ar.lc
  245. mov ar.ec=1 };;
  246. { .mmi; nop.m 0
  247. add tmp3=1,inp
  248. mov ar.lc=in2 };; // brp.loop.imp: too far
  249. .Ldtop:
  250. { .mmi; mov $A=$h0
  251. mov $B=$h1
  252. mux2 tmp6=$h1,0x44 }
  253. { .mmi; mov $C=$h2
  254. mov $D=$h3
  255. mov $E=$h4 };;
  256. ___
  257. { my $i;
  258. my @V=($A,$B,$C,$D,$E);
  259. for($i=0;$i<16;$i++) { &BODY_00_15(\$code,$i,@V); unshift(@V,pop(@V)); }
  260. for(;$i<20;$i++) { &BODY_16_19(\$code,$i,@V); unshift(@V,pop(@V)); }
  261. for(;$i<40;$i++) { &BODY_20_39(\$code,$i,@V); unshift(@V,pop(@V)); }
  262. for(;$i<60;$i++) { &BODY_40_59(\$code,$i,@V); unshift(@V,pop(@V)); }
  263. for(;$i<80;$i++) { &BODY_60_79(\$code,$i,@V); unshift(@V,pop(@V)); }
  264. (($V[0] eq $A) and ($V[4] eq $E)) or die; # double-check
  265. }
  266. $code.=<<___;
  267. { .mmb; add $h0=$h0,$A
  268. add $h2=$h2,$C
  269. br.ctop.dptk.many .Ldtop };;
  270. .Ldend:
  271. { .mmi; add tmp0=4,ctx
  272. mov ar.lc=r3 };;
  273. { .mmi; st4 [ctx]=$h0,8
  274. st4 [tmp0]=$h1,8 };;
  275. { .mmi; st4 [ctx]=$h2,8
  276. st4 [tmp0]=$h3 };;
  277. { .mib; st4 [ctx]=$h4,-16
  278. mov pr=r2,0x1ffff
  279. br.ret.sptk.many b0 };;
  280. .endp sha1_block_data_order#
  281. stringz "SHA1 block transform for IA64, CRYPTOGAMS by <appro\@openssl.org>"
  282. ___
  283. open STDOUT,">$output" if $output;
  284. print $code;