md5-sparcv9.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. #! /usr/bin/env perl
  2. # Copyright 2012-2021 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. #ifndef __ASSEMBLER__
  182. # define __ASSEMBLER__ 1
  183. #endif
  184. #include "crypto/sparc_arch.h"
  185. #ifdef __arch64__
  186. .register %g2,#scratch
  187. .register %g3,#scratch
  188. #endif
  189. .section ".text",#alloc,#execinstr
  190. #ifdef __PIC__
  191. SPARC_PIC_THUNK(%g1)
  192. #endif
  193. .globl ossl_md5_block_asm_data_order
  194. .align 32
  195. ossl_md5_block_asm_data_order:
  196. SPARC_LOAD_ADDRESS_LEAF(OPENSSL_sparcv9cap_P,%g1,%g5)
  197. ld [%g1+4],%g1 ! OPENSSL_sparcv9cap_P[1]
  198. andcc %g1, CFR_MD5, %g0
  199. be .Lsoftware
  200. nop
  201. mov 4, %g1
  202. andcc %o1, 0x7, %g0
  203. lda [%o0 + %g0]0x88, %f0 ! load context
  204. lda [%o0 + %g1]0x88, %f1
  205. add %o0, 8, %o0
  206. lda [%o0 + %g0]0x88, %f2
  207. lda [%o0 + %g1]0x88, %f3
  208. bne,pn %icc, .Lhwunaligned
  209. sub %o0, 8, %o0
  210. .Lhw_loop:
  211. ldd [%o1 + 0x00], %f8
  212. ldd [%o1 + 0x08], %f10
  213. ldd [%o1 + 0x10], %f12
  214. ldd [%o1 + 0x18], %f14
  215. ldd [%o1 + 0x20], %f16
  216. ldd [%o1 + 0x28], %f18
  217. ldd [%o1 + 0x30], %f20
  218. subcc %o2, 1, %o2 ! done yet?
  219. ldd [%o1 + 0x38], %f22
  220. add %o1, 0x40, %o1
  221. prefetch [%o1 + 63], 20
  222. .word 0x81b02800 ! MD5
  223. bne,pt SIZE_T_CC, .Lhw_loop
  224. nop
  225. .Lhwfinish:
  226. sta %f0, [%o0 + %g0]0x88 ! store context
  227. sta %f1, [%o0 + %g1]0x88
  228. add %o0, 8, %o0
  229. sta %f2, [%o0 + %g0]0x88
  230. sta %f3, [%o0 + %g1]0x88
  231. retl
  232. nop
  233. .align 8
  234. .Lhwunaligned:
  235. alignaddr %o1, %g0, %o1
  236. ldd [%o1 + 0x00], %f10
  237. .Lhwunaligned_loop:
  238. ldd [%o1 + 0x08], %f12
  239. ldd [%o1 + 0x10], %f14
  240. ldd [%o1 + 0x18], %f16
  241. ldd [%o1 + 0x20], %f18
  242. ldd [%o1 + 0x28], %f20
  243. ldd [%o1 + 0x30], %f22
  244. ldd [%o1 + 0x38], %f24
  245. subcc %o2, 1, %o2 ! done yet?
  246. ldd [%o1 + 0x40], %f26
  247. add %o1, 0x40, %o1
  248. prefetch [%o1 + 63], 20
  249. faligndata %f10, %f12, %f8
  250. faligndata %f12, %f14, %f10
  251. faligndata %f14, %f16, %f12
  252. faligndata %f16, %f18, %f14
  253. faligndata %f18, %f20, %f16
  254. faligndata %f20, %f22, %f18
  255. faligndata %f22, %f24, %f20
  256. faligndata %f24, %f26, %f22
  257. .word 0x81b02800 ! MD5
  258. bne,pt SIZE_T_CC, .Lhwunaligned_loop
  259. for %f26, %f26, %f10 ! %f10=%f26
  260. ba .Lhwfinish
  261. nop
  262. .align 16
  263. .Lsoftware:
  264. save %sp,-STACK_FRAME,%sp
  265. rd %asi,$saved_asi
  266. wr %g0,0x88,%asi ! ASI_PRIMARY_LITTLE
  267. and $inp,7,$shr
  268. andn $inp,7,$inp
  269. sll $shr,3,$shr ! *=8
  270. mov 56,$shl2
  271. ld [$ctx+0],$A
  272. sub $shl2,$shr,$shl2
  273. ld [$ctx+4],$B
  274. and $shl2,32,$shl1
  275. add $shl2,8,$shl2
  276. ld [$ctx+8],$C
  277. sub $shl2,$shl1,$shl2 ! shr+shl1+shl2==64
  278. ld [$ctx+12],$D
  279. nop
  280. .Loop:
  281. cmp $shr,0 ! was inp aligned?
  282. ldxa [$inp+0]%asi,@X[0] ! load little-endian input
  283. ldxa [$inp+8]%asi,@X[1]
  284. ldxa [$inp+16]%asi,@X[2]
  285. ldxa [$inp+24]%asi,@X[3]
  286. ldxa [$inp+32]%asi,@X[4]
  287. sllx $A,32,$AB ! pack A,B
  288. ldxa [$inp+40]%asi,@X[5]
  289. sllx $C,32,$CD ! pack C,D
  290. ldxa [$inp+48]%asi,@X[6]
  291. or $B,$AB,$AB
  292. ldxa [$inp+56]%asi,@X[7]
  293. or $D,$CD,$CD
  294. bnz,a,pn %icc,.+8
  295. ldxa [$inp+64]%asi,@X[8]
  296. srlx @X[0],$shr,@X[0] ! align X[0]
  297. sllx @X[1],$shl1,$tx
  298. sethi %hi(@K[0]),$t2
  299. sllx $tx,$shl2,$tx
  300. or $t2,%lo(@K[0]),$t2
  301. or $tx,@X[0],@X[0]
  302. xor $C,$D,$t1
  303. add @X[0],$t2,$t2 ! X[0]+K[0]
  304. ___
  305. for ($i=0;$i<15;$i++) { &R0($i,@V); unshift(@V,pop(@V)); }
  306. for (;$i<16;$i++) { &R0_1($i,@V); unshift(@V,pop(@V)); }
  307. for (;$i<32;$i++) { &R1($i,@V); unshift(@V,pop(@V)); }
  308. for (;$i<48;$i++) { &R2($i,@V); unshift(@V,pop(@V)); }
  309. for (;$i<64;$i++) { &R3($i,@V); unshift(@V,pop(@V)); }
  310. $code.=<<___;
  311. srlx $AB,32,$t1 ! unpack A,B,C,D and accumulate
  312. add $inp,64,$inp ! advance inp
  313. srlx $CD,32,$t2
  314. add $t1,$A,$A
  315. subcc $len,1,$len ! done yet?
  316. add $AB,$B,$B
  317. add $t2,$C,$C
  318. add $CD,$D,$D
  319. srl $B,0,$B ! clruw $B
  320. bne SIZE_T_CC,.Loop
  321. srl $D,0,$D ! clruw $D
  322. st $A,[$ctx+0] ! write out ctx
  323. st $B,[$ctx+4]
  324. st $C,[$ctx+8]
  325. st $D,[$ctx+12]
  326. wr %g0,$saved_asi,%asi
  327. ret
  328. restore
  329. .type ossl_md5_block_asm_data_order,#function
  330. .size ossl_md5_block_asm_data_order,(.-ossl_md5_block_asm_data_order)
  331. .asciz "MD5 block transform for SPARCv9, CRYPTOGAMS by <appro\@openssl.org>"
  332. .align 4
  333. ___
  334. # Purpose of these subroutines is to explicitly encode VIS instructions,
  335. # so that one can compile the module without having to specify VIS
  336. # extensions on compiler command line, e.g. -xarch=v9 vs. -xarch=v9a.
  337. # Idea is to reserve for option to produce "universal" binary and let
  338. # programmer detect if current CPU is VIS capable at run-time.
  339. sub unvis {
  340. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  341. my $ref,$opf;
  342. my %visopf = ( "faligndata" => 0x048,
  343. "for" => 0x07c );
  344. $ref = "$mnemonic\t$rs1,$rs2,$rd";
  345. if ($opf=$visopf{$mnemonic}) {
  346. foreach ($rs1,$rs2,$rd) {
  347. return $ref if (!/%f([0-9]{1,2})/);
  348. $_=$1;
  349. if ($1>=32) {
  350. return $ref if ($1&1);
  351. # re-encode for upper double register addressing
  352. $_=($1|$1>>5)&31;
  353. }
  354. }
  355. return sprintf ".word\t0x%08x !%s",
  356. 0x81b00000|$rd<<25|$rs1<<14|$opf<<5|$rs2,
  357. $ref;
  358. } else {
  359. return $ref;
  360. }
  361. }
  362. sub unalignaddr {
  363. my ($mnemonic,$rs1,$rs2,$rd)=@_;
  364. my %bias = ( "g" => 0, "o" => 8, "l" => 16, "i" => 24 );
  365. my $ref="$mnemonic\t$rs1,$rs2,$rd";
  366. foreach ($rs1,$rs2,$rd) {
  367. if (/%([goli])([0-7])/) { $_=$bias{$1}+$2; }
  368. else { return $ref; }
  369. }
  370. return sprintf ".word\t0x%08x !%s",
  371. 0x81b00300|$rd<<25|$rs1<<14|$rs2,
  372. $ref;
  373. }
  374. foreach (split("\n",$code)) {
  375. s/\`([^\`]*)\`/eval $1/ge;
  376. s/\b(f[^\s]*)\s+(%f[0-9]{1,2}),\s*(%f[0-9]{1,2}),\s*(%f[0-9]{1,2})/
  377. &unvis($1,$2,$3,$4)
  378. /ge;
  379. s/\b(alignaddr)\s+(%[goli][0-7]),\s*(%[goli][0-7]),\s*(%[goli][0-7])/
  380. &unalignaddr($1,$2,$3,$4)
  381. /ge;
  382. print $_,"\n";
  383. }
  384. close STDOUT or die "error closing STDOUT: $!";