sha512-ia64.pl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #!/usr/bin/env perl
  2. #
  3. # ====================================================================
  4. # Written by Andy Polyakov <appro@fy.chalmers.se> for the OpenSSL
  5. # project. The module is, however, dual licensed under OpenSSL and
  6. # CRYPTOGAMS licenses depending on where you obtain it. For further
  7. # details see http://www.openssl.org/~appro/cryptogams/.
  8. # ====================================================================
  9. #
  10. # SHA256/512_Transform for Itanium.
  11. #
  12. # sha512_block runs in 1003 cycles on Itanium 2, which is almost 50%
  13. # faster than gcc and >60%(!) faster than code generated by HP-UX
  14. # compiler (yes, HP-UX is generating slower code, because unlike gcc,
  15. # it failed to deploy "shift right pair," 'shrp' instruction, which
  16. # substitutes for 64-bit rotate).
  17. #
  18. # 924 cycles long sha256_block outperforms gcc by over factor of 2(!)
  19. # and HP-UX compiler - by >40% (yes, gcc won sha512_block, but lost
  20. # this one big time). Note that "formally" 924 is about 100 cycles
  21. # too much. I mean it's 64 32-bit rounds vs. 80 virtually identical
  22. # 64-bit ones and 1003*64/80 gives 802. Extra cycles, 2 per round,
  23. # are spent on extra work to provide for 32-bit rotations. 32-bit
  24. # rotations are still handled by 'shrp' instruction and for this
  25. # reason lower 32 bits are deposited to upper half of 64-bit register
  26. # prior 'shrp' issue. And in order to minimize the amount of such
  27. # operations, X[16] values are *maintained* with copies of lower
  28. # halves in upper halves, which is why you'll spot such instructions
  29. # as custom 'mux2', "parallel 32-bit add," 'padd4' and "parallel
  30. # 32-bit unsigned right shift," 'pshr4.u' instructions here.
  31. #
  32. # Rules of engagement.
  33. #
  34. # There is only one integer shifter meaning that if I have two rotate,
  35. # deposit or extract instructions in adjacent bundles, they shall
  36. # split [at run-time if they have to]. But note that variable and
  37. # parallel shifts are performed by multi-media ALU and *are* pairable
  38. # with rotates [and alike]. On the backside MMALU is rather slow: it
  39. # takes 2 extra cycles before the result of integer operation is
  40. # available *to* MMALU and 2(*) extra cycles before the result of MM
  41. # operation is available "back" *to* integer ALU, not to mention that
  42. # MMALU itself has 2 cycles latency. However! I explicitly scheduled
  43. # these MM instructions to avoid MM stalls, so that all these extra
  44. # latencies get "hidden" in instruction-level parallelism.
  45. #
  46. # (*) 2 cycles on Itanium 1 and 1 cycle on Itanium 2. But I schedule
  47. # for 2 in order to provide for best *overall* performance,
  48. # because on Itanium 1 stall on MM result is accompanied by
  49. # pipeline flush, which takes 6 cycles:-(
  50. #
  51. # Resulting performance numbers for 900MHz Itanium 2 system:
  52. #
  53. # The 'numbers' are in 1000s of bytes per second processed.
  54. # type 16 bytes 64 bytes 256 bytes 1024 bytes 8192 bytes
  55. # sha1(*) 6210.14k 20376.30k 52447.83k 85870.05k 105478.12k
  56. # sha256 7476.45k 20572.05k 41538.34k 56062.29k 62093.18k
  57. # sha512 4996.56k 20026.28k 47597.20k 85278.79k 111501.31k
  58. #
  59. # (*) SHA1 numbers are for HP-UX compiler and are presented purely
  60. # for reference purposes. I bet it can improved too...
  61. #
  62. # To generate code, pass the file name with either 256 or 512 in its
  63. # name and compiler flags.
  64. $output=shift;
  65. if ($output =~ /512.*\.[s|asm]/) {
  66. $SZ=8;
  67. $BITS=8*$SZ;
  68. $LDW="ld8";
  69. $STW="st8";
  70. $ADD="add";
  71. $SHRU="shr.u";
  72. $TABLE="K512";
  73. $func="sha512_block_data_order";
  74. @Sigma0=(28,34,39);
  75. @Sigma1=(14,18,41);
  76. @sigma0=(1, 8, 7);
  77. @sigma1=(19,61, 6);
  78. $rounds=80;
  79. } elsif ($output =~ /256.*\.[s|asm]/) {
  80. $SZ=4;
  81. $BITS=8*$SZ;
  82. $LDW="ld4";
  83. $STW="st4";
  84. $ADD="padd4";
  85. $SHRU="pshr4.u";
  86. $TABLE="K256";
  87. $func="sha256_block_data_order";
  88. @Sigma0=( 2,13,22);
  89. @Sigma1=( 6,11,25);
  90. @sigma0=( 7,18, 3);
  91. @sigma1=(17,19,10);
  92. $rounds=64;
  93. } else { die "nonsense $output"; }
  94. open STDOUT,">$output" || die "can't open $output: $!";
  95. if ($^O eq "hpux") {
  96. $ADDP="addp4";
  97. for (@ARGV) { $ADDP="add" if (/[\+DD|\-mlp]64/); }
  98. } else { $ADDP="add"; }
  99. for (@ARGV) { $big_endian=1 if (/\-DB_ENDIAN/);
  100. $big_endian=0 if (/\-DL_ENDIAN/); }
  101. if (!defined($big_endian))
  102. { $big_endian=(unpack('L',pack('N',1))==1); }
  103. $code=<<___;
  104. .ident \"$output, version 1.1\"
  105. .ident \"IA-64 ISA artwork by Andy Polyakov <appro\@fy.chalmers.se>\"
  106. .explicit
  107. .text
  108. pfssave=r2;
  109. lcsave=r3;
  110. prsave=r14;
  111. K=r15;
  112. A=r16; B=r17; C=r18; D=r19;
  113. E=r20; F=r21; G=r22; H=r23;
  114. T1=r24; T2=r25;
  115. s0=r26; s1=r27; t0=r28; t1=r29;
  116. Ktbl=r30;
  117. ctx=r31; // 1st arg
  118. input=r48; // 2nd arg
  119. num=r49; // 3rd arg
  120. sgm0=r50; sgm1=r51; // small constants
  121. A_=r54; B_=r55; C_=r56; D_=r57;
  122. E_=r58; F_=r59; G_=r60; H_=r61;
  123. // void $func (SHA_CTX *ctx, const void *in,size_t num[,int host])
  124. .global $func#
  125. .proc $func#
  126. .align 32
  127. $func:
  128. .prologue
  129. .save ar.pfs,pfssave
  130. { .mmi; alloc pfssave=ar.pfs,3,27,0,16
  131. $ADDP ctx=0,r32 // 1st arg
  132. .save ar.lc,lcsave
  133. mov lcsave=ar.lc }
  134. { .mmi; $ADDP input=0,r33 // 2nd arg
  135. mov num=r34 // 3rd arg
  136. .save pr,prsave
  137. mov prsave=pr };;
  138. .body
  139. { .mib; add r8=0*$SZ,ctx
  140. add r9=1*$SZ,ctx
  141. brp.loop.imp .L_first16,.L_first16_end-16 }
  142. { .mib; add r10=2*$SZ,ctx
  143. add r11=3*$SZ,ctx
  144. brp.loop.imp .L_rest,.L_rest_end-16 };;
  145. // load A-H
  146. .Lpic_point:
  147. { .mmi; $LDW A_=[r8],4*$SZ
  148. $LDW B_=[r9],4*$SZ
  149. mov Ktbl=ip }
  150. { .mmi; $LDW C_=[r10],4*$SZ
  151. $LDW D_=[r11],4*$SZ
  152. mov sgm0=$sigma0[2] };;
  153. { .mmi; $LDW E_=[r8]
  154. $LDW F_=[r9]
  155. add Ktbl=($TABLE#-.Lpic_point),Ktbl }
  156. { .mmi; $LDW G_=[r10]
  157. $LDW H_=[r11]
  158. cmp.ne p0,p16=0,r0 };; // used in sha256_block
  159. ___
  160. $code.=<<___ if ($BITS==64);
  161. { .mii; and r8=7,input
  162. and input=~7,input;;
  163. cmp.eq p9,p0=1,r8 }
  164. { .mmi; cmp.eq p10,p0=2,r8
  165. cmp.eq p11,p0=3,r8
  166. cmp.eq p12,p0=4,r8 }
  167. { .mmi; cmp.eq p13,p0=5,r8
  168. cmp.eq p14,p0=6,r8
  169. cmp.eq p15,p0=7,r8 };;
  170. ___
  171. $code.=<<___;
  172. .L_outer:
  173. .rotr X[16]
  174. { .mmi; mov A=A_
  175. mov B=B_
  176. mov ar.lc=14 }
  177. { .mmi; mov C=C_
  178. mov D=D_
  179. mov E=E_ }
  180. { .mmi; mov F=F_
  181. mov G=G_
  182. mov ar.ec=2 }
  183. { .mmi; ld1 X[15]=[input],$SZ // eliminated in 64-bit
  184. mov H=H_
  185. mov sgm1=$sigma1[2] };;
  186. ___
  187. $t0="t0", $t1="t1", $code.=<<___ if ($BITS==32);
  188. .align 32
  189. .L_first16:
  190. { .mmi; add r9=1-$SZ,input
  191. add r10=2-$SZ,input
  192. add r11=3-$SZ,input };;
  193. { .mmi; ld1 r9=[r9]
  194. ld1 r10=[r10]
  195. dep.z $t1=E,32,32 }
  196. { .mmi; $LDW K=[Ktbl],$SZ
  197. ld1 r11=[r11]
  198. zxt4 E=E };;
  199. { .mii; or $t1=$t1,E
  200. dep X[15]=X[15],r9,8,8
  201. dep r11=r10,r11,8,8 };;
  202. { .mmi; and T1=F,E
  203. and T2=A,B
  204. dep X[15]=X[15],r11,16,16 }
  205. { .mmi; andcm r8=G,E
  206. and r9=A,C
  207. mux2 $t0=A,0x44 };; // copy lower half to upper
  208. { .mmi; (p16) ld1 X[15-1]=[input],$SZ // prefetch
  209. xor T1=T1,r8 // T1=((e & f) ^ (~e & g))
  210. _rotr r11=$t1,$Sigma1[0] } // ROTR(e,14)
  211. { .mib; and r10=B,C
  212. xor T2=T2,r9 };;
  213. ___
  214. $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
  215. // in 64-bit mode I load whole X[16] at once and take care of alignment...
  216. { .mmi; add r8=1*$SZ,input
  217. add r9=2*$SZ,input
  218. add r10=3*$SZ,input };;
  219. { .mmb; $LDW X[15]=[input],4*$SZ
  220. $LDW X[14]=[r8],4*$SZ
  221. (p9) br.cond.dpnt.many .L1byte };;
  222. { .mmb; $LDW X[13]=[r9],4*$SZ
  223. $LDW X[12]=[r10],4*$SZ
  224. (p10) br.cond.dpnt.many .L2byte };;
  225. { .mmb; $LDW X[11]=[input],4*$SZ
  226. $LDW X[10]=[r8],4*$SZ
  227. (p11) br.cond.dpnt.many .L3byte };;
  228. { .mmb; $LDW X[ 9]=[r9],4*$SZ
  229. $LDW X[ 8]=[r10],4*$SZ
  230. (p12) br.cond.dpnt.many .L4byte };;
  231. { .mmb; $LDW X[ 7]=[input],4*$SZ
  232. $LDW X[ 6]=[r8],4*$SZ
  233. (p13) br.cond.dpnt.many .L5byte };;
  234. { .mmb; $LDW X[ 5]=[r9],4*$SZ
  235. $LDW X[ 4]=[r10],4*$SZ
  236. (p14) br.cond.dpnt.many .L6byte };;
  237. { .mmb; $LDW X[ 3]=[input],4*$SZ
  238. $LDW X[ 2]=[r8],4*$SZ
  239. (p15) br.cond.dpnt.many .L7byte };;
  240. { .mmb; $LDW X[ 1]=[r9],4*$SZ
  241. $LDW X[ 0]=[r10],4*$SZ
  242. br.many .L_first16 };;
  243. .L1byte:
  244. { .mmi; $LDW X[13]=[r9],4*$SZ
  245. $LDW X[12]=[r10],4*$SZ
  246. shrp X[15]=X[15],X[14],56 };;
  247. { .mmi; $LDW X[11]=[input],4*$SZ
  248. $LDW X[10]=[r8],4*$SZ
  249. shrp X[14]=X[14],X[13],56 }
  250. { .mmi; $LDW X[ 9]=[r9],4*$SZ
  251. $LDW X[ 8]=[r10],4*$SZ
  252. shrp X[13]=X[13],X[12],56 };;
  253. { .mmi; $LDW X[ 7]=[input],4*$SZ
  254. $LDW X[ 6]=[r8],4*$SZ
  255. shrp X[12]=X[12],X[11],56 }
  256. { .mmi; $LDW X[ 5]=[r9],4*$SZ
  257. $LDW X[ 4]=[r10],4*$SZ
  258. shrp X[11]=X[11],X[10],56 };;
  259. { .mmi; $LDW X[ 3]=[input],4*$SZ
  260. $LDW X[ 2]=[r8],4*$SZ
  261. shrp X[10]=X[10],X[ 9],56 }
  262. { .mmi; $LDW X[ 1]=[r9],4*$SZ
  263. $LDW X[ 0]=[r10],4*$SZ
  264. shrp X[ 9]=X[ 9],X[ 8],56 };;
  265. { .mii; $LDW T1=[input]
  266. shrp X[ 8]=X[ 8],X[ 7],56
  267. shrp X[ 7]=X[ 7],X[ 6],56 }
  268. { .mii; shrp X[ 6]=X[ 6],X[ 5],56
  269. shrp X[ 5]=X[ 5],X[ 4],56 };;
  270. { .mii; shrp X[ 4]=X[ 4],X[ 3],56
  271. shrp X[ 3]=X[ 3],X[ 2],56 }
  272. { .mii; shrp X[ 2]=X[ 2],X[ 1],56
  273. shrp X[ 1]=X[ 1],X[ 0],56 }
  274. { .mib; shrp X[ 0]=X[ 0],T1,56
  275. br.many .L_first16 };;
  276. .L2byte:
  277. { .mmi; $LDW X[11]=[input],4*$SZ
  278. $LDW X[10]=[r8],4*$SZ
  279. shrp X[15]=X[15],X[14],48 }
  280. { .mmi; $LDW X[ 9]=[r9],4*$SZ
  281. $LDW X[ 8]=[r10],4*$SZ
  282. shrp X[14]=X[14],X[13],48 };;
  283. { .mmi; $LDW X[ 7]=[input],4*$SZ
  284. $LDW X[ 6]=[r8],4*$SZ
  285. shrp X[13]=X[13],X[12],48 }
  286. { .mmi; $LDW X[ 5]=[r9],4*$SZ
  287. $LDW X[ 4]=[r10],4*$SZ
  288. shrp X[12]=X[12],X[11],48 };;
  289. { .mmi; $LDW X[ 3]=[input],4*$SZ
  290. $LDW X[ 2]=[r8],4*$SZ
  291. shrp X[11]=X[11],X[10],48 }
  292. { .mmi; $LDW X[ 1]=[r9],4*$SZ
  293. $LDW X[ 0]=[r10],4*$SZ
  294. shrp X[10]=X[10],X[ 9],48 };;
  295. { .mii; $LDW T1=[input]
  296. shrp X[ 9]=X[ 9],X[ 8],48
  297. shrp X[ 8]=X[ 8],X[ 7],48 }
  298. { .mii; shrp X[ 7]=X[ 7],X[ 6],48
  299. shrp X[ 6]=X[ 6],X[ 5],48 };;
  300. { .mii; shrp X[ 5]=X[ 5],X[ 4],48
  301. shrp X[ 4]=X[ 4],X[ 3],48 }
  302. { .mii; shrp X[ 3]=X[ 3],X[ 2],48
  303. shrp X[ 2]=X[ 2],X[ 1],48 }
  304. { .mii; shrp X[ 1]=X[ 1],X[ 0],48
  305. shrp X[ 0]=X[ 0],T1,48 }
  306. { .mfb; br.many .L_first16 };;
  307. .L3byte:
  308. { .mmi; $LDW X[ 9]=[r9],4*$SZ
  309. $LDW X[ 8]=[r10],4*$SZ
  310. shrp X[15]=X[15],X[14],40 };;
  311. { .mmi; $LDW X[ 7]=[input],4*$SZ
  312. $LDW X[ 6]=[r8],4*$SZ
  313. shrp X[14]=X[14],X[13],40 }
  314. { .mmi; $LDW X[ 5]=[r9],4*$SZ
  315. $LDW X[ 4]=[r10],4*$SZ
  316. shrp X[13]=X[13],X[12],40 };;
  317. { .mmi; $LDW X[ 3]=[input],4*$SZ
  318. $LDW X[ 2]=[r8],4*$SZ
  319. shrp X[12]=X[12],X[11],40 }
  320. { .mmi; $LDW X[ 1]=[r9],4*$SZ
  321. $LDW X[ 0]=[r10],4*$SZ
  322. shrp X[11]=X[11],X[10],40 };;
  323. { .mii; $LDW T1=[input]
  324. shrp X[10]=X[10],X[ 9],40
  325. shrp X[ 9]=X[ 9],X[ 8],40 }
  326. { .mii; shrp X[ 8]=X[ 8],X[ 7],40
  327. shrp X[ 7]=X[ 7],X[ 6],40 };;
  328. { .mii; shrp X[ 6]=X[ 6],X[ 5],40
  329. shrp X[ 5]=X[ 5],X[ 4],40 }
  330. { .mii; shrp X[ 4]=X[ 4],X[ 3],40
  331. shrp X[ 3]=X[ 3],X[ 2],40 }
  332. { .mii; shrp X[ 2]=X[ 2],X[ 1],40
  333. shrp X[ 1]=X[ 1],X[ 0],40 }
  334. { .mib; shrp X[ 0]=X[ 0],T1,40
  335. br.many .L_first16 };;
  336. .L4byte:
  337. { .mmi; $LDW X[ 7]=[input],4*$SZ
  338. $LDW X[ 6]=[r8],4*$SZ
  339. shrp X[15]=X[15],X[14],32 }
  340. { .mmi; $LDW X[ 5]=[r9],4*$SZ
  341. $LDW X[ 4]=[r10],4*$SZ
  342. shrp X[14]=X[14],X[13],32 };;
  343. { .mmi; $LDW X[ 3]=[input],4*$SZ
  344. $LDW X[ 2]=[r8],4*$SZ
  345. shrp X[13]=X[13],X[12],32 }
  346. { .mmi; $LDW X[ 1]=[r9],4*$SZ
  347. $LDW X[ 0]=[r10],4*$SZ
  348. shrp X[12]=X[12],X[11],32 };;
  349. { .mii; $LDW T1=[input]
  350. shrp X[11]=X[11],X[10],32
  351. shrp X[10]=X[10],X[ 9],32 }
  352. { .mii; shrp X[ 9]=X[ 9],X[ 8],32
  353. shrp X[ 8]=X[ 8],X[ 7],32 };;
  354. { .mii; shrp X[ 7]=X[ 7],X[ 6],32
  355. shrp X[ 6]=X[ 6],X[ 5],32 }
  356. { .mii; shrp X[ 5]=X[ 5],X[ 4],32
  357. shrp X[ 4]=X[ 4],X[ 3],32 }
  358. { .mii; shrp X[ 3]=X[ 3],X[ 2],32
  359. shrp X[ 2]=X[ 2],X[ 1],32 }
  360. { .mii; shrp X[ 1]=X[ 1],X[ 0],32
  361. shrp X[ 0]=X[ 0],T1,32 }
  362. { .mfb; br.many .L_first16 };;
  363. .L5byte:
  364. { .mmi; $LDW X[ 5]=[r9],4*$SZ
  365. $LDW X[ 4]=[r10],4*$SZ
  366. shrp X[15]=X[15],X[14],24 };;
  367. { .mmi; $LDW X[ 3]=[input],4*$SZ
  368. $LDW X[ 2]=[r8],4*$SZ
  369. shrp X[14]=X[14],X[13],24 }
  370. { .mmi; $LDW X[ 1]=[r9],4*$SZ
  371. $LDW X[ 0]=[r10],4*$SZ
  372. shrp X[13]=X[13],X[12],24 };;
  373. { .mii; $LDW T1=[input]
  374. shrp X[12]=X[12],X[11],24
  375. shrp X[11]=X[11],X[10],24 }
  376. { .mii; shrp X[10]=X[10],X[ 9],24
  377. shrp X[ 9]=X[ 9],X[ 8],24 };;
  378. { .mii; shrp X[ 8]=X[ 8],X[ 7],24
  379. shrp X[ 7]=X[ 7],X[ 6],24 }
  380. { .mii; shrp X[ 6]=X[ 6],X[ 5],24
  381. shrp X[ 5]=X[ 5],X[ 4],24 }
  382. { .mii; shrp X[ 4]=X[ 4],X[ 3],24
  383. shrp X[ 3]=X[ 3],X[ 2],24 }
  384. { .mii; shrp X[ 2]=X[ 2],X[ 1],24
  385. shrp X[ 1]=X[ 1],X[ 0],24 }
  386. { .mib; shrp X[ 0]=X[ 0],T1,24
  387. br.many .L_first16 };;
  388. .L6byte:
  389. { .mmi; $LDW X[ 3]=[input],4*$SZ
  390. $LDW X[ 2]=[r8],4*$SZ
  391. shrp X[15]=X[15],X[14],16 }
  392. { .mmi; $LDW X[ 1]=[r9],4*$SZ
  393. $LDW X[ 0]=[r10],4*$SZ
  394. shrp X[14]=X[14],X[13],16 };;
  395. { .mii; $LDW T1=[input]
  396. shrp X[13]=X[13],X[12],16
  397. shrp X[12]=X[12],X[11],16 }
  398. { .mii; shrp X[11]=X[11],X[10],16
  399. shrp X[10]=X[10],X[ 9],16 };;
  400. { .mii; shrp X[ 9]=X[ 9],X[ 8],16
  401. shrp X[ 8]=X[ 8],X[ 7],16 }
  402. { .mii; shrp X[ 7]=X[ 7],X[ 6],16
  403. shrp X[ 6]=X[ 6],X[ 5],16 }
  404. { .mii; shrp X[ 5]=X[ 5],X[ 4],16
  405. shrp X[ 4]=X[ 4],X[ 3],16 }
  406. { .mii; shrp X[ 3]=X[ 3],X[ 2],16
  407. shrp X[ 2]=X[ 2],X[ 1],16 }
  408. { .mii; shrp X[ 1]=X[ 1],X[ 0],16
  409. shrp X[ 0]=X[ 0],T1,16 }
  410. { .mfb; br.many .L_first16 };;
  411. .L7byte:
  412. { .mmi; $LDW X[ 1]=[r9],4*$SZ
  413. $LDW X[ 0]=[r10],4*$SZ
  414. shrp X[15]=X[15],X[14],8 };;
  415. { .mii; $LDW T1=[input]
  416. shrp X[14]=X[14],X[13],8
  417. shrp X[13]=X[13],X[12],8 }
  418. { .mii; shrp X[12]=X[12],X[11],8
  419. shrp X[11]=X[11],X[10],8 };;
  420. { .mii; shrp X[10]=X[10],X[ 9],8
  421. shrp X[ 9]=X[ 9],X[ 8],8 }
  422. { .mii; shrp X[ 8]=X[ 8],X[ 7],8
  423. shrp X[ 7]=X[ 7],X[ 6],8 }
  424. { .mii; shrp X[ 6]=X[ 6],X[ 5],8
  425. shrp X[ 5]=X[ 5],X[ 4],8 }
  426. { .mii; shrp X[ 4]=X[ 4],X[ 3],8
  427. shrp X[ 3]=X[ 3],X[ 2],8 }
  428. { .mii; shrp X[ 2]=X[ 2],X[ 1],8
  429. shrp X[ 1]=X[ 1],X[ 0],8 }
  430. { .mib; shrp X[ 0]=X[ 0],T1,8
  431. br.many .L_first16 };;
  432. .align 32
  433. .L_first16:
  434. { .mmi; $LDW K=[Ktbl],$SZ
  435. and T1=F,E
  436. and T2=A,B }
  437. { .mmi; //$LDW X[15]=[input],$SZ // X[i]=*input++
  438. andcm r8=G,E
  439. and r9=A,C };;
  440. { .mmi; xor T1=T1,r8 //T1=((e & f) ^ (~e & g))
  441. and r10=B,C
  442. _rotr r11=$t1,$Sigma1[0] } // ROTR(e,14)
  443. { .mmi; xor T2=T2,r9
  444. mux1 X[15]=X[15],\@rev };; // eliminated in big-endian
  445. ___
  446. $code.=<<___;
  447. { .mib; add T1=T1,H // T1=Ch(e,f,g)+h
  448. _rotr r8=$t1,$Sigma1[1] } // ROTR(e,18)
  449. { .mib; xor T2=T2,r10 // T2=((a & b) ^ (a & c) ^ (b & c))
  450. mov H=G };;
  451. { .mib; xor r11=r8,r11
  452. _rotr r9=$t1,$Sigma1[2] } // ROTR(e,41)
  453. { .mib; mov G=F
  454. mov F=E };;
  455. { .mib; xor r9=r9,r11 // r9=Sigma1(e)
  456. _rotr r10=$t0,$Sigma0[0] } // ROTR(a,28)
  457. { .mib; add T1=T1,K // T1=Ch(e,f,g)+h+K512[i]
  458. mov E=D };;
  459. { .mib; add T1=T1,r9 // T1+=Sigma1(e)
  460. _rotr r11=$t0,$Sigma0[1] } // ROTR(a,34)
  461. { .mib; mov D=C
  462. mov C=B };;
  463. { .mib; add T1=T1,X[15] // T1+=X[i]
  464. _rotr r8=$t0,$Sigma0[2] } // ROTR(a,39)
  465. { .mib; xor r10=r10,r11
  466. mux2 X[15]=X[15],0x44 };; // eliminated in 64-bit
  467. { .mmi; xor r10=r8,r10 // r10=Sigma0(a)
  468. mov B=A
  469. add A=T1,T2 };;
  470. { .mib; add E=E,T1
  471. add A=A,r10 // T2=Maj(a,b,c)+Sigma0(a)
  472. br.ctop.sptk .L_first16 };;
  473. .L_first16_end:
  474. { .mii; mov ar.lc=$rounds-17
  475. mov ar.ec=1 };;
  476. .align 32
  477. .L_rest:
  478. .rotr X[16]
  479. { .mib; $LDW K=[Ktbl],$SZ
  480. _rotr r8=X[15-1],$sigma0[0] } // ROTR(s0,1)
  481. { .mib; $ADD X[15]=X[15],X[15-9] // X[i&0xF]+=X[(i+9)&0xF]
  482. $SHRU s0=X[15-1],sgm0 };; // s0=X[(i+1)&0xF]>>7
  483. { .mib; and T1=F,E
  484. _rotr r9=X[15-1],$sigma0[1] } // ROTR(s0,8)
  485. { .mib; andcm r10=G,E
  486. $SHRU s1=X[15-14],sgm1 };; // s1=X[(i+14)&0xF]>>6
  487. { .mmi; xor T1=T1,r10 // T1=((e & f) ^ (~e & g))
  488. xor r9=r8,r9
  489. _rotr r10=X[15-14],$sigma1[0] };;// ROTR(s1,19)
  490. { .mib; and T2=A,B
  491. _rotr r11=X[15-14],$sigma1[1] }// ROTR(s1,61)
  492. { .mib; and r8=A,C };;
  493. ___
  494. $t0="t0", $t1="t1", $code.=<<___ if ($BITS==32);
  495. // I adhere to mmi; in order to hold Itanium 1 back and avoid 6 cycle
  496. // pipeline flush in last bundle. Note that even on Itanium2 the
  497. // latter stalls for one clock cycle...
  498. { .mmi; xor s0=s0,r9 // s0=sigma0(X[(i+1)&0xF])
  499. dep.z $t1=E,32,32 }
  500. { .mmi; xor r10=r11,r10
  501. zxt4 E=E };;
  502. { .mmi; or $t1=$t1,E
  503. xor s1=s1,r10 // s1=sigma1(X[(i+14)&0xF])
  504. mux2 $t0=A,0x44 };; // copy lower half to upper
  505. { .mmi; xor T2=T2,r8
  506. _rotr r9=$t1,$Sigma1[0] } // ROTR(e,14)
  507. { .mmi; and r10=B,C
  508. add T1=T1,H // T1=Ch(e,f,g)+h
  509. $ADD X[15]=X[15],s0 };; // X[i&0xF]+=sigma0(X[(i+1)&0xF])
  510. ___
  511. $t0="A", $t1="E", $code.=<<___ if ($BITS==64);
  512. { .mib; xor s0=s0,r9 // s0=sigma0(X[(i+1)&0xF])
  513. _rotr r9=$t1,$Sigma1[0] } // ROTR(e,14)
  514. { .mib; xor r10=r11,r10
  515. xor T2=T2,r8 };;
  516. { .mib; xor s1=s1,r10 // s1=sigma1(X[(i+14)&0xF])
  517. add T1=T1,H }
  518. { .mib; and r10=B,C
  519. $ADD X[15]=X[15],s0 };; // X[i&0xF]+=sigma0(X[(i+1)&0xF])
  520. ___
  521. $code.=<<___;
  522. { .mmi; xor T2=T2,r10 // T2=((a & b) ^ (a & c) ^ (b & c))
  523. mov H=G
  524. _rotr r8=$t1,$Sigma1[1] };; // ROTR(e,18)
  525. { .mmi; xor r11=r8,r9
  526. $ADD X[15]=X[15],s1 // X[i&0xF]+=sigma1(X[(i+14)&0xF])
  527. _rotr r9=$t1,$Sigma1[2] } // ROTR(e,41)
  528. { .mmi; mov G=F
  529. mov F=E };;
  530. { .mib; xor r9=r9,r11 // r9=Sigma1(e)
  531. _rotr r10=$t0,$Sigma0[0] } // ROTR(a,28)
  532. { .mib; add T1=T1,K // T1=Ch(e,f,g)+h+K512[i]
  533. mov E=D };;
  534. { .mib; add T1=T1,r9 // T1+=Sigma1(e)
  535. _rotr r11=$t0,$Sigma0[1] } // ROTR(a,34)
  536. { .mib; mov D=C
  537. mov C=B };;
  538. { .mmi; add T1=T1,X[15] // T1+=X[i]
  539. xor r10=r10,r11
  540. _rotr r8=$t0,$Sigma0[2] };; // ROTR(a,39)
  541. { .mmi; xor r10=r8,r10 // r10=Sigma0(a)
  542. mov B=A
  543. add A=T1,T2 };;
  544. { .mib; add E=E,T1
  545. add A=A,r10 // T2=Maj(a,b,c)+Sigma0(a)
  546. br.ctop.sptk .L_rest };;
  547. .L_rest_end:
  548. { .mmi; add A_=A_,A
  549. add B_=B_,B
  550. add C_=C_,C }
  551. { .mmi; add D_=D_,D
  552. add E_=E_,E
  553. cmp.ltu p16,p0=1,num };;
  554. { .mmi; add F_=F_,F
  555. add G_=G_,G
  556. add H_=H_,H }
  557. { .mmb; add Ktbl=-$SZ*$rounds,Ktbl
  558. (p16) add num=-1,num
  559. (p16) br.dptk.many .L_outer };;
  560. { .mib; add r8=0*$SZ,ctx
  561. add r9=1*$SZ,ctx }
  562. { .mib; add r10=2*$SZ,ctx
  563. add r11=3*$SZ,ctx };;
  564. { .mmi; $STW [r8]=A_,4*$SZ
  565. $STW [r9]=B_,4*$SZ
  566. mov ar.lc=lcsave }
  567. { .mmi; $STW [r10]=C_,4*$SZ
  568. $STW [r11]=D_,4*$SZ
  569. mov pr=prsave,0x1ffff };;
  570. { .mmb; $STW [r8]=E_
  571. $STW [r9]=F_ }
  572. { .mmb; $STW [r10]=G_
  573. $STW [r11]=H_
  574. br.ret.sptk.many b0 };;
  575. .endp $func#
  576. ___
  577. $code =~ s/\`([^\`]*)\`/eval $1/gem;
  578. $code =~ s/_rotr(\s+)([^=]+)=([^,]+),([0-9]+)/shrp$1$2=$3,$3,$4/gm;
  579. if ($BITS==64) {
  580. $code =~ s/mux2(\s+)\S+/nop.i$1 0x0/gm;
  581. $code =~ s/mux1(\s+)\S+/nop.i$1 0x0/gm if ($big_endian);
  582. $code =~ s/(shrp\s+X\[[^=]+)=([^,]+),([^,]+),([1-9]+)/$1=$3,$2,64-$4/gm
  583. if (!$big_endian);
  584. $code =~ s/ld1(\s+)X\[\S+/nop.m$1 0x0/gm;
  585. }
  586. print $code;
  587. print<<___ if ($BITS==32);
  588. .align 64
  589. .type K256#,\@object
  590. K256: data4 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5
  591. data4 0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5
  592. data4 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3
  593. data4 0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174
  594. data4 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc
  595. data4 0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da
  596. data4 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7
  597. data4 0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967
  598. data4 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13
  599. data4 0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85
  600. data4 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3
  601. data4 0xd192e819,0xd6990624,0xf40e3585,0x106aa070
  602. data4 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5
  603. data4 0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3
  604. data4 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208
  605. data4 0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2
  606. .size K256#,$SZ*$rounds
  607. stringz "SHA256 block transform for IA64, CRYPTOGAMS by <appro\@openssl.org>"
  608. ___
  609. print<<___ if ($BITS==64);
  610. .align 64
  611. .type K512#,\@object
  612. K512: data8 0x428a2f98d728ae22,0x7137449123ef65cd
  613. data8 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
  614. data8 0x3956c25bf348b538,0x59f111f1b605d019
  615. data8 0x923f82a4af194f9b,0xab1c5ed5da6d8118
  616. data8 0xd807aa98a3030242,0x12835b0145706fbe
  617. data8 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
  618. data8 0x72be5d74f27b896f,0x80deb1fe3b1696b1
  619. data8 0x9bdc06a725c71235,0xc19bf174cf692694
  620. data8 0xe49b69c19ef14ad2,0xefbe4786384f25e3
  621. data8 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
  622. data8 0x2de92c6f592b0275,0x4a7484aa6ea6e483
  623. data8 0x5cb0a9dcbd41fbd4,0x76f988da831153b5
  624. data8 0x983e5152ee66dfab,0xa831c66d2db43210
  625. data8 0xb00327c898fb213f,0xbf597fc7beef0ee4
  626. data8 0xc6e00bf33da88fc2,0xd5a79147930aa725
  627. data8 0x06ca6351e003826f,0x142929670a0e6e70
  628. data8 0x27b70a8546d22ffc,0x2e1b21385c26c926
  629. data8 0x4d2c6dfc5ac42aed,0x53380d139d95b3df
  630. data8 0x650a73548baf63de,0x766a0abb3c77b2a8
  631. data8 0x81c2c92e47edaee6,0x92722c851482353b
  632. data8 0xa2bfe8a14cf10364,0xa81a664bbc423001
  633. data8 0xc24b8b70d0f89791,0xc76c51a30654be30
  634. data8 0xd192e819d6ef5218,0xd69906245565a910
  635. data8 0xf40e35855771202a,0x106aa07032bbd1b8
  636. data8 0x19a4c116b8d2d0c8,0x1e376c085141ab53
  637. data8 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
  638. data8 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
  639. data8 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
  640. data8 0x748f82ee5defb2fc,0x78a5636f43172f60
  641. data8 0x84c87814a1f0ab72,0x8cc702081a6439ec
  642. data8 0x90befffa23631e28,0xa4506cebde82bde9
  643. data8 0xbef9a3f7b2c67915,0xc67178f2e372532b
  644. data8 0xca273eceea26619c,0xd186b8c721c0c207
  645. data8 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
  646. data8 0x06f067aa72176fba,0x0a637dc5a2c898a6
  647. data8 0x113f9804bef90dae,0x1b710b35131c471b
  648. data8 0x28db77f523047d84,0x32caab7b40c72493
  649. data8 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
  650. data8 0x4cc5d4becb3e42b6,0x597f299cfc657e2a
  651. data8 0x5fcb6fab3ad6faec,0x6c44198c4a475817
  652. .size K512#,$SZ*$rounds
  653. stringz "SHA512 block transform for IA64, CRYPTOGAMS by <appro\@openssl.org>"
  654. ___