2
0

sha512-ia64.pl 21 KB

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