github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/hash/md5block_amd64.s (about)

     1  // Original source:
     2  //	http://www.zorinaq.com/papers/md5-amd64.html
     3  //	http://www.zorinaq.com/papers/md5-amd64.tar.bz2
     4  //
     5  // Translated from Perl generating GNU assembly into
     6  // #defines generating 6a assembly by the Go Authors.
     7  
     8  #include "textflag.h"
     9  
    10  // MD5 optimized for AMD64.
    11  //
    12  // Author: Marc Bevand <bevand_m (at) epita.fr>
    13  // Licence: I hereby disclaim the copyright on this code and place it
    14  // in the public domain.
    15  
    16  TEXT	·block(SB),NOSPLIT,$8-32
    17  	MOVQ	dig+0(FP),	BP
    18  	MOVQ	p+8(FP),	SI
    19  	MOVQ	p_len+16(FP), DX
    20  	SHRQ	$6,		DX
    21  	SHLQ	$6,		DX
    22  
    23  	LEAQ	(SI)(DX*1),	DI
    24  	MOVL	(0*4)(BP),	AX
    25  	MOVL	(1*4)(BP),	BX
    26  	MOVL	(2*4)(BP),	CX
    27  	MOVL	(3*4)(BP),	DX
    28  	MOVL	$0xffffffff,	R11
    29  
    30  	CMPQ	SI,		DI
    31  	JEQ	end
    32  
    33  loop:
    34  	MOVL	AX,		R12
    35  	MOVL	BX,		R13
    36  	MOVL	CX,		R14
    37  	MOVL	DX,		R15
    38  
    39  	MOVL	(0*4)(SI),	R8
    40  	MOVL	DX,		R9
    41  
    42  #define ROUND1(a, b, c, d, index, const, shift) \
    43  	XORL	c, R9; \
    44  	ADDL	$const, a; \
    45  	ADDL	R8, a; \
    46  	ANDL	b, R9; \
    47  	XORL	d, R9; \
    48  	MOVL	(index*4)(SI), R8; \
    49  	ADDL	R9, a; \
    50  	ROLL	$shift, a; \
    51  	MOVL	c, R9; \
    52  	ADDL	b, a
    53  
    54  	ROUND1(AX,BX,CX,DX, 1,0xd76aa478, 7);
    55  	ROUND1(DX,AX,BX,CX, 2,0xe8c7b756,12);
    56  	ROUND1(CX,DX,AX,BX, 3,0x242070db,17);
    57  	ROUND1(BX,CX,DX,AX, 4,0xc1bdceee,22);
    58  	ROUND1(AX,BX,CX,DX, 5,0xf57c0faf, 7);
    59  	ROUND1(DX,AX,BX,CX, 6,0x4787c62a,12);
    60  	ROUND1(CX,DX,AX,BX, 7,0xa8304613,17);
    61  	ROUND1(BX,CX,DX,AX, 8,0xfd469501,22);
    62  	ROUND1(AX,BX,CX,DX, 9,0x698098d8, 7);
    63  	ROUND1(DX,AX,BX,CX,10,0x8b44f7af,12);
    64  	ROUND1(CX,DX,AX,BX,11,0xffff5bb1,17);
    65  	ROUND1(BX,CX,DX,AX,12,0x895cd7be,22);
    66  	ROUND1(AX,BX,CX,DX,13,0x6b901122, 7);
    67  	ROUND1(DX,AX,BX,CX,14,0xfd987193,12);
    68  	ROUND1(CX,DX,AX,BX,15,0xa679438e,17);
    69  	ROUND1(BX,CX,DX,AX, 1,0x49b40821,22);
    70  
    71  	MOVL	DX,		R9
    72  	MOVL	DX,		R10
    73  
    74  // Uses https://github.com/animetosho/md5-optimisation#dependency-shortcut-in-g-function
    75  
    76  #define ROUND2(a, b, c, d, index, const, shift) \
    77  	XORL	R11, R9; \
    78  	ADDL	$const,	a; \
    79  	ADDL	R8,	a; \
    80  	ANDL	b,		R10; \
    81  	ANDL	c,		R9; \
    82  	MOVL	(index*4)(SI),R8; \
    83  	ADDL	R9,	a; \
    84  	ADDL	R10,	a; \
    85  	MOVL	c,		R9; \
    86  	MOVL	c,		R10; \
    87  	ROLL	$shift,	a; \
    88  	ADDL	b,		a
    89  
    90  	ROUND2(AX,BX,CX,DX, 6,0xf61e2562, 5);
    91  	ROUND2(DX,AX,BX,CX,11,0xc040b340, 9);
    92  	ROUND2(CX,DX,AX,BX, 0,0x265e5a51,14);
    93  	ROUND2(BX,CX,DX,AX, 5,0xe9b6c7aa,20);
    94  	ROUND2(AX,BX,CX,DX,10,0xd62f105d, 5);
    95  	ROUND2(DX,AX,BX,CX,15, 0x2441453, 9);
    96  	ROUND2(CX,DX,AX,BX, 4,0xd8a1e681,14);
    97  	ROUND2(BX,CX,DX,AX, 9,0xe7d3fbc8,20);
    98  	ROUND2(AX,BX,CX,DX,14,0x21e1cde6, 5);
    99  	ROUND2(DX,AX,BX,CX, 3,0xc33707d6, 9);
   100  	ROUND2(CX,DX,AX,BX, 8,0xf4d50d87,14);
   101  	ROUND2(BX,CX,DX,AX,13,0x455a14ed,20);
   102  	ROUND2(AX,BX,CX,DX, 2,0xa9e3e905, 5);
   103  	ROUND2(DX,AX,BX,CX, 7,0xfcefa3f8, 9);
   104  	ROUND2(CX,DX,AX,BX,12,0x676f02d9,14);
   105  	ROUND2(BX,CX,DX,AX, 5,0x8d2a4c8a,20);
   106  
   107  	MOVL	CX,		R9
   108  
   109  // Uses https://github.com/animetosho/md5-optimisation#h-function-re-use
   110  
   111  #define ROUND3FIRST(a, b, c, d, index, const, shift) \
   112  	MOVL	d,		R9; \
   113  	XORL	c,		R9; \
   114  	XORL	b,		R9; \
   115  	ADDL	$const,	a; \
   116  	ADDL	R8,		a; \
   117  	MOVL	(index*4)(SI),R8; \
   118  	ADDL	R9,		a; \
   119  	ROLL	$shift,		a; \
   120  	ADDL	b,		a
   121  
   122  #define ROUND3(a, b, c, d, index, const, shift) \
   123  	XORL	a,		R9; \
   124  	XORL	b,		R9; \
   125  	ADDL	$const,	a; \
   126  	ADDL	R8,		a; \
   127  	MOVL	(index*4)(SI),R8; \
   128  	ADDL	R9,		a; \
   129  	ROLL	$shift,		a; \
   130  	ADDL	b,		a
   131  
   132  	ROUND3FIRST(AX,BX,CX,DX, 8,0xfffa3942, 4);
   133  	ROUND3(DX,AX,BX,CX,11,0x8771f681,11);
   134  	ROUND3(CX,DX,AX,BX,14,0x6d9d6122,16);
   135  	ROUND3(BX,CX,DX,AX, 1,0xfde5380c,23);
   136  	ROUND3(AX,BX,CX,DX, 4,0xa4beea44, 4);
   137  	ROUND3(DX,AX,BX,CX, 7,0x4bdecfa9,11);
   138  	ROUND3(CX,DX,AX,BX,10,0xf6bb4b60,16);
   139  	ROUND3(BX,CX,DX,AX,13,0xbebfbc70,23);
   140  	ROUND3(AX,BX,CX,DX, 0,0x289b7ec6, 4);
   141  	ROUND3(DX,AX,BX,CX, 3,0xeaa127fa,11);
   142  	ROUND3(CX,DX,AX,BX, 6,0xd4ef3085,16);
   143  	ROUND3(BX,CX,DX,AX, 9, 0x4881d05,23);
   144  	ROUND3(AX,BX,CX,DX,12,0xd9d4d039, 4);
   145  	ROUND3(DX,AX,BX,CX,15,0xe6db99e5,11);
   146  	ROUND3(CX,DX,AX,BX, 2,0x1fa27cf8,16);
   147  	ROUND3(BX,CX,DX,AX, 0,0xc4ac5665,23);
   148  
   149  	MOVL	R11,	R9
   150  	XORL	DX,		R9
   151  
   152  #define ROUND4(a, b, c, d, index, const, shift) \
   153  	ADDL	$const,	a; \
   154  	ADDL	R8,		a; \
   155  	ORL		b,		R9; \
   156  	XORL	c,		R9; \
   157  	ADDL	R9,		a; \
   158  	MOVL	(index*4)(SI),R8; \
   159  	MOVL	$0xffffffff,	R9; \
   160  	ROLL	$shift,		a; \
   161  	XORL	c,		R9; \
   162  	ADDL	b,		a
   163  
   164  	ROUND4(AX,BX,CX,DX, 7,0xf4292244, 6);
   165  	ROUND4(DX,AX,BX,CX,14,0x432aff97,10);
   166  	ROUND4(CX,DX,AX,BX, 5,0xab9423a7,15);
   167  	ROUND4(BX,CX,DX,AX,12,0xfc93a039,21);
   168  	ROUND4(AX,BX,CX,DX, 3,0x655b59c3, 6);
   169  	ROUND4(DX,AX,BX,CX,10,0x8f0ccc92,10);
   170  	ROUND4(CX,DX,AX,BX, 1,0xffeff47d,15);
   171  	ROUND4(BX,CX,DX,AX, 8,0x85845dd1,21);
   172  	ROUND4(AX,BX,CX,DX,15,0x6fa87e4f, 6);
   173  	ROUND4(DX,AX,BX,CX, 6,0xfe2ce6e0,10);
   174  	ROUND4(CX,DX,AX,BX,13,0xa3014314,15);
   175  	ROUND4(BX,CX,DX,AX, 4,0x4e0811a1,21);
   176  	ROUND4(AX,BX,CX,DX,11,0xf7537e82, 6);
   177  	ROUND4(DX,AX,BX,CX, 2,0xbd3af235,10);
   178  	ROUND4(CX,DX,AX,BX, 9,0x2ad7d2bb,15);
   179  	ROUND4(BX,CX,DX,AX, 0,0xeb86d391,21);
   180  
   181  	ADDL	R12,	AX
   182  	ADDL	R13,	BX
   183  	ADDL	R14,	CX
   184  	ADDL	R15,	DX
   185  
   186  	ADDQ	$64,		SI
   187  	CMPQ	SI,		DI
   188  	JB	loop
   189  
   190  end:
   191  	MOVL	AX,		(0*4)(BP)
   192  	MOVL	BX,		(1*4)(BP)
   193  	MOVL	CX,		(2*4)(BP)
   194  	MOVL	DX,		(3*4)(BP)
   195  	RET