2
0

md5-sparcv9.pl 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #!/usr/bin/env perl
  2. # ====================================================================
  3. # Written by Andy Polyakov <appro@openssl.org> for the OpenSSL
  4. # project. The module is, however, dual licensed under OpenSSL and
  5. # CRYPTOGAMS licenses depending on where you obtain it. For further
  6. # details see http://www.openssl.org/~appro/cryptogams/.
  7. #
  8. # Hardware SPARC T4 support by David S. Miller <davem@davemloft.net>.
  9. # ====================================================================
  10. # MD5 for SPARCv9, 6.9 cycles per byte on UltraSPARC, >40% faster than
  11. # code generated by Sun C 5.2.
  12. # SPARC T4 MD5 hardware achieves 3.20 cycles per byte, which is 2.1x
  13. # faster than software. Multi-process benchmark saturates at 12x
  14. # single-process result on 8-core processor, or ~11GBps per 2.85GHz
  15. # socket.
  16. $output=shift;
  17. open STDOUT,">$output";
  18. use integer;
  19. ($ctx,$inp,$len)=("%i0","%i1","%i2"); # input arguments
  20. # 64-bit values
  21. @X=("%o0","%o1","%o2","%o3","%o4","%o5","%o7","%g1","%g2");
  22. $tx="%g3";
  23. ($AB,$CD)=("%g4","%g5");
  24. # 32-bit values
  25. @V=($A,$B,$C,$D)=map("%l$_",(0..3));
  26. ($t1,$t2,$t3,$saved_asi)=map("%l$_",(4..7));
  27. ($shr,$shl1,$shl2)=("%i3","%i4","%i5");
  28. my @K=( 0xd76aa478,0xe8c7b756,0x242070db,0xc1bdceee,
  29. 0xf57c0faf,0x4787c62a,0xa8304613,0xfd469501,
  30. 0x698098d8,0x8b44f7af,0xffff5bb1,0x895cd7be,
  31. 0x6b901122,0xfd987193,0xa679438e,0x49b40821,
  32. 0xf61e2562,0xc040b340,0x265e5a51,0xe9b6c7aa,
  33. 0xd62f105d,0x02441453,0xd8a1e681,0xe7d3fbc8,
  34. 0x21e1cde6,0xc33707d6,0xf4d50d87,0x455a14ed,
  35. 0xa9e3e905,0xfcefa3f8,0x676f02d9,0x8d2a4c8a,
  36. 0xfffa3942,0x8771f681,0x6d9d6122,0xfde5380c,
  37. 0xa4beea44,0x4bdecfa9,0xf6bb4b60,0xbebfbc70,
  38. 0x289b7ec6,0xeaa127fa,0xd4ef3085,0x04881d05,
  39. 0xd9d4d039,0xe6db99e5,0x1fa27cf8,0xc4ac5665,
  40. 0xf4292244,0x432aff97,0xab9423a7,0xfc93a039,
  41. 0x655b59c3,0x8f0ccc92,0xffeff47d,0x85845dd1,
  42. 0x6fa87e4f,0xfe2ce6e0,0xa3014314,0x4e0811a1,
  43. 0xf7537e82,0xbd3af235,0x2ad7d2bb,0xeb86d391, 0 );
  44. sub R0 {
  45. my ($i,$a,$b,$c,$d) = @_;
  46. my $rot = (7,12,17,22)[$i%4];
  47. my $j = ($i+1)/2;
  48. if ($i&1) {
  49. $code.=<<___;
  50. srlx @X[$j],$shr,@X[$j] ! align X[`$i+1`]
  51. and $b,$t1,$t1 ! round $i
  52. sllx @X[$j+1],$shl1,$tx
  53. add $t2,$a,$a
  54. sllx $tx,$shl2,$tx
  55. xor $d,$t1,$t1
  56. or $tx,@X[$j],@X[$j]
  57. sethi %hi(@K[$i+1]),$t2
  58. add $t1,$a,$a
  59. or $t2,%lo(@K[$i+1]),$t2
  60. sll $a,$rot,$t3
  61. add @X[$j],$t2,$t2 ! X[`$i+1`]+K[`$i+1`]
  62. srl $a,32-$rot,$a
  63. add $b,$t3,$t3
  64. xor $b,$c,$t1
  65. add $t3,$a,$a
  66. ___
  67. } else {
  68. $code.=<<___;
  69. srlx @X[$j],32,$tx ! extract X[`2*$j+1`]
  70. and $b,$t1,$t1 ! round $i
  71. add $t2,$a,$a
  72. xor $d,$t1,$t1
  73. sethi %hi(@K[$i+1]),$t2
  74. add $t1,$a,$a
  75. or $t2,%lo(@K[$i+1]),$t2
  76. sll $a,$rot,$t3
  77. add $tx,$t2,$t2 ! X[`2*$j+1`]+K[`$i+1`]
  78. srl $a,32-$rot,$a
  79. add $b,$t3,$t3
  80. xor $b,$c,$t1
  81. add $t3,$a,$a
  82. ___
  83. }
  84. }
  85. sub R0_1 {
  86. my ($i,$a,$b,$c,$d) = @_;
  87. my $rot = (7,12,17,22)[$i%4];
  88. $code.=<<___;
  89. srlx @X[0],32,$tx ! extract X[1]
  90. and $b,$t1,$t1 ! round $i
  91. add $t2,$a,$a
  92. xor $d,$t1,$t1
  93. sethi %hi(@K[$i+1]),$t2
  94. add $t1,$a,$a
  95. or $t2,%lo(@K[$i+1]),$t2
  96. sll $a,$rot,$t3
  97. add $tx,$t2,$t2 ! X[1]+K[`$i+1`]
  98. srl $a,32-$rot,$a
  99. add $b,$t3,$t3
  100. andn $b,$c,$t1
  101. add $t3,$a,$a
  102. ___
  103. }
  104. sub R1 {
  105. my ($i,$a,$b,$c,$d) = @_;
  106. my $rot = (5,9,14,20)[$i%4];
  107. my $j = $i<31 ? (1+5*($i+1))%16 : (5+3*($i+1))%16;
  108. my $xi = @X[$j/2];
  109. $code.=<<___ if ($j&1 && ($xi=$tx));
  110. srlx @X[$j/2],32,$xi ! extract X[$j]
  111. ___
  112. $code.=<<___;
  113. and $b,$d,$t3 ! round $i
  114. add $t2,$a,$a
  115. or $t3,$t1,$t1
  116. sethi %hi(@K[$i+1]),$t2
  117. add $t1,$a,$a
  118. or $t2,%lo(@K[$i+1]),$t2
  119. sll $a,$rot,$t3
  120. add $xi,$t2,$t2 ! X[$j]+K[`$i+1`]
  121. srl $a,32-$rot,$a
  122. add $b,$t3,$t3
  123. `$i<31?"andn":"xor"` $b,$c,$t1
  124. add $t3,$a,$a
  125. ___
  126. }
  127. sub R2 {
  128. my ($i,$a,$b,$c,$d) = @_;
  129. my $rot = (4,11,16,23)[$i%4];
  130. my $j = $i<47 ? (5+3*($i+1))%16 : (0+7*($i+1))%16;
  131. my $xi = @X[$j/2];
  132. $code.=<<___ if ($j&1 && ($xi=$tx));
  133. srlx @X[$j/2],32,$xi ! extract X[$j]
  134. ___
  135. $code.=<<___;
  136. add $t2,$a,$a ! round $i
  137. xor $b,$t1,$t1
  138. sethi %hi(@K[$i+1]),$t2
  139. add $t1,$a,$a
  140. or $t2,%lo(@K[$i+1]),$t2
  141. sll $a,$rot,$t3
  142. add $xi,$t2,$t2 ! X[$j]+K[`$i+1`]
  143. srl $a,32-$rot,$a
  144. add $b,$t3,$t3
  145. xor $b,$c,$t1
  146. add $t3,$a,$a
  147. ___
  148. }
  149. sub R3 {
  150. my ($i,$a,$b,$c,$d) = @_;
  151. my $rot = (6,10,15,21)[$i%4];
  152. my $j = (0+7*($i+1))%16;
  153. my $xi = @X[$j/2];
  154. $code.=<<___;
  155. add $t2,$a,$a ! round $i
  156. ___
  157. $code.=<<___ if ($j&1 && ($xi=$tx));
  158. srlx @X[$j/2],32,$xi ! extract X[$j]
  159. ___
  160. $code.=<<___;
  161. orn $b,$d,$t1
  162. sethi %hi(@K[$i+1]),$t2
  163. xor $c,$t1,$t1
  164. or $t2,%lo(@K[$i+1]),$t2
  165. add $t1,$a,$a
  166. sll $a,$rot,$t3
  167. add $xi,$t2,$t2 ! X[$j]+K[`$i+1`]
  168. srl $a,32-$rot,$a
  169. add $b,$t3,$t3
  170. add $t3,$a,$a
  171. ___
  172. }
  173. $code.=<<___;
  174. #include "sparc_arch.h"
  175. #ifdef __arch64__
  176. .register %g2,#scratch
  177. .register %g3,#scratch
  178. #endif
  179. .section ".text",#alloc,#execinstr
  180. #ifdef __PIC__
  181. SPARC_PIC_THUNK(%g1)
  182. #endif
  183. .globl md5_block_asm_data_order
  184. .align 32
  185. md5_block_asm_data_order:
  186. SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
  187. ld [%g1+4],%g1 ! OPENSSL_sparcv9cap_P[1]
  188. andcc %g1, CFR_MD5, %g0
  189. be .Lsoftware
  190. nop
  191. mov 4, %g1
  192. andcc %o1, 0x7, %g0
  193. lda [%o0 + %g0]0x88, %f0 ! load context
  194. lda [%o0 + %g1]0x88, %f1
  195. add %o0, 8, %o0
  196. lda [%o0 + %g0]0x88, %f2
  197. lda [%o0 + %g1]0x88, %f3
  198. bne,pn %icc, .Lhwunaligned
  199. sub %o0, 8, %o0
  200. .Lhw_loop:
  201. ldd [%o1 + 0x00], %f8
  202. ldd [%o1 + 0x08], %f10
  203. ldd [%o1 + 0x10], %f12
  204. ldd [%o1 + 0x18], %f14
  205. ldd [%o1 + 0x20], %f16
  206. ldd [%o1 + 0x28], %f18
  207. ldd [%o1 + 0x30], %f20
  208. subcc %o2, 1, %o2 ! done yet?
  209. ldd [%o1 + 0x38], %f22
  210. add %o1, 0x40, %o1
  211. prefetch [%o1 + 63], 20
  212. .word 0x81b02800 ! MD5
  213. bne,pt SIZE_T_CC, .Lhw_loop
  214. nop
  215. .Lhwfinish:
  216. sta %f0, [%o0 + %g0]0x88 ! store context
  217. sta %f1, [%o0 + %g1]0x88
  218. add %o0, 8, %o0
  219. sta %f2, [%o0 + %g0]0x88
  220. sta %f3, [%o0 + %g1]0x88
  221. retl
  222. nop
  223. .align 8
  224. .Lhwunaligned:
  225. alignaddr %o1, %g0, %o1
  226. ldd [%o1 + 0x00], %f10
  227. .Lhwunaligned_loop:
  228. ldd [%o1 + 0x08], %f12
  229. ldd [%o1 + 0x10], %f14
  230. ldd [%o1 + 0x18], %f16
  231. ldd [%o1 + 0x20], %f18
  232. ldd [%o1 + 0x28], %f20
  233. ldd [%o1 + 0x30], %f22
  234. ldd [%o1 + 0x38], %f24
  235. subcc %o2, 1, %o2 ! done yet?
  236. ldd [%o1 + 0x40], %f26
  237. add %o1, 0x40, %o1
  238. prefetch [%o1 + 63], 20
  239. faligndata %f10, %f12, %f8
  240. faligndata %f12, %f14, %f10
  241. faligndata %f14, %f16, %f12
  242. faligndata %f16, %f18, %f14
  243. faligndata %f18, %f20, %f16
  244. faligndata %f20, %f22, %f18
  245. faligndata %f22, %f24, %f20
  246. faligndata %f24, %f26, %f22
  247. .word 0x81b02800 ! MD5
  248. bne,pt SIZE_T_CC, .Lhwunaligned_loop
  249. for %f26, %f26, %f10 ! %f10=%f26
  250. ba .Lhwfinish
  251. nop
  252. .align 16
  253. .Lsoftware:
  254. save %sp,-STACK_FRAME,%sp
  255. rd %asi,$saved_asi
  256. wr %g0,0x88,%asi ! ASI_PRIMARY_LITTLE
  257. and $inp,7,$shr
  258. andn $inp,7,$inp
  259. sll $shr,3,$shr ! *=8
  260. mov 56,$shl2
  261. ld [$ctx+0],$A
  262. sub $shl2,$shr,$shl2
  263. ld [$ctx+4],$B
  264. and $shl2,32,$shl1
  265. add $shl2,8,$shl2
  266. ld [$ctx+8],$C
  267. sub $shl2,$shl1,$shl2 ! shr+shl1+shl2==64
  268. ld [$ctx+12],$D
  269. nop
  270. .Loop:
  271. cmp $shr,0 ! was inp aligned?
  272. ldxa [$inp+0]%asi,@X[0] ! load little-endian input
  273. ldxa [$inp+8]%asi,@X[1]
  274. ldxa [$inp+16]%asi,@X[2]
  275. ldxa [$inp+24]%asi,@X[3]
  276. ldxa [$inp+32]%asi,@X[4]
  277. sllx $A,32,$AB ! pack A,B
  278. ldxa [$inp+40]%asi,@X[5]
  279. sllx $C,32,$CD ! pack C,D
  280. ldxa [$inp+48]%asi,@X[6]
  281. or $B,$AB,$AB
  282. ldxa [$inp+56]%asi,@X[7]
  283. or $D,$CD,$CD
  284. bnz,a,pn %icc,.+8
  285. ldxa [$inp+64]%asi,@X[8]
  286. srlx @X[0],$shr,@X[0] ! align X[0]
  287. sllx @X[1],$shl1,$tx
  288. sethi %hi(@K[0]),$t2
  289. sllx $tx,$shl2,$tx
  290. or $t2,%lo(@K[0]),$t2
  291. or $tx,@X[0],@X[0]
  292. xor $C,$D,$t1
  293. add @X[0],$t2,$t2 ! X[0]+K[0]
  294. ___
  295. for ($i=0;$i<15;$i++) { &R0($i,@V); unshift(@V,pop(@V)); }
  296. for (;$i<16;$i++) { &R0_1($i,@V); unshift(@V,pop(@V)); }
  297. for (;$i<32;$i++) { &R1($i,@V); unshift(@V,pop(@V)); }
  298. for (;$i<48;$i++) { &R2($i,@V); unshift(@V,pop(@V)); }
  299. for (;$i<64;$i++) { &R3($i,@V); unshift(@V,pop(@V)); }
  300. $code.=<<___;
  301. srlx $AB,32,$t1 ! unpack A,B,C,D and accumulate
  302. add $inp,64,$inp ! advance inp
  303. srlx $CD,32,$t2
  304. add $t1,$A,$A
  305. subcc $len,1,$len ! done yet?
  306. add $AB,$B,$B
  307. add $t2,$C,$C
  308. add $CD,$D,$D
  309. srl $B,0,$B ! clruw $B
  310. bne SIZE_T_CC,.Loop
  311. srl $D,0,$D ! clruw $D
  312. st $A,[$ctx+0] ! write out ctx
  313. st $B,[$ctx+4]
  314. st $C,[$ctx+8]
  315. st $D,[$ctx+12]
  316. wr %g0,$saved_asi,%asi
  317. ret
  318. restore
  319. .type md5_block_asm_data_order,#function
  320. .size md5_block_asm_data_order,(.-md5_block_asm_data_order)
  321. .asciz "MD5 block transform for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"
  322. .align 4
  323. ___
  324. # Purpose of these subroutines is to explicitly encode VIS instructions,
  325. # so that one can compile the module without having to specify VIS
  326. # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
  327. # Idea is to reserve for option to produce "universal" binary and let
  328. # programmer detect if current CPU is VIS capable at run-time.
  329. sub unvis {
  330. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  331. my $ref,$opf;
  332. my %visopf = ( "faligndata" => 0x048,
  333. "for" => 0x07c );
  334. $ref = "$mnemonic\t$rs1,$rs2,$rd";
  335. if ($opf=$visopf{$mnemonic}) {
  336. foreach ($rs1,$rs2,$rd) {
  337. return $ref if (!/%f([0-9]{1,2})/);
  338. $_=$1;
  339. if ($1>=32) {
  340. return $ref if ($1&1);
  341. # re-encode for upper double register addressing
  342. $_=($1|$1>>5)&31;
  343. }
  344. }
  345. return sprintf ".word\t0x%08x !%s",
  346. 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
  347. $ref;
  348. } else {
  349. return $ref;
  350. }
  351. }
  352. sub unalignaddr {
  353. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  354. my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
  355. my $ref="$mnemonic\t$rs1,$rs2,$rd";
  356. foreach ($rs1,$rs2,$rd) {
  357. if (/%([goli])([0-7])/) { $_=$bias{$1}+$2; }
  358. else { return $ref; }
  359. }
  360. return sprintf ".word\t0x%08x !%s",
  361. 0x81b00300|$rd<<25|$rs1<<14|$rs2,
  362. $ref;
  363. }
  364. foreach (split("\n",$code)) {
  365. s/\`([^\`]*)\`/eval $1/ge;
  366. s/\b(f[^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
  367. &unvis($1,$2,$3,$4)
  368. /ge;
  369. s/\b(alignaddr)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
  370. &unalignaddr($1,$2,$3,$4)
  371. /ge;
  372. print $_,"\n";
  373. }
  374. close STDOUT;