md5-sparcv9.pl 10 KB

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