2 * Speed-optimized CRC64 using slicing-by-four algorithm
3 * Instruction set: i386
6 * This code has been put into the public domain by its authors:
7 * Igor Pavlov <http://7-zip.org/>
8 * Lasse Collin <lasse.collin@tukaani.org>
10 * This code needs lzma_crc64_table, which can be created using the
13 uint64_t lzma_crc64_table[4][256];
18 static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
20 for (size_t s = 0; s < 4; ++s) {
21 for (size_t b = 0; b < 256; ++b) {
22 uint64_t r = s == 0 ? b : lzma_crc64_table[s - 1][b];
24 for (size_t i = 0; i < 8; ++i) {
26 r = (r >> 1) ^ poly64;
31 lzma_crc64_table[s][b] = r;
36 * The prototype of the CRC64 function:
37 * extern uint64_t lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc);
41 * On some systems, the functions need to be prefixed. The prefix is
42 * usually an underscore.
44 #ifndef __USER_LABEL_PREFIX__
45 # define __USER_LABEL_PREFIX__
47 #define MAKE_SYM_CAT(prefix, sym) prefix ## sym
48 #define MAKE_SYM(prefix, sym) MAKE_SYM_CAT(prefix, sym)
49 #define LZMA_CRC64 MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc64)
50 #define LZMA_CRC64_TABLE MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc64_table)
53 * Solaris assembler doesn't have .p2align, and Darwin uses .align
54 * differently than GNU/Linux and Solaris.
57 # define ALIGN(pow2, abs) .align pow2
59 # define ALIGN(pow2, abs) .align abs
65 #if !defined(__MACH__) && !defined(_WIN32)
66 .type LZMA_CRC64, @function
76 * %edi size or buf + size
77 * %ebx lzma_crc64_table
85 movl 0x14(%esp), %esi /* buf */
86 movl 0x18(%esp), %edi /* size */
87 movl 0x1C(%esp), %eax /* crc LSB */
88 movl 0x20(%esp), %edx /* crc MSB */
91 * Store the address of lzma_crc64_table to %ebx. This is needed to
92 * get position-independent code (PIC).
94 * The PIC macro is defined by libtool, while __PIC__ is defined
95 * by GCC but only on some systems. Testing for both makes it simpler
96 * to test this code without libtool, and keeps the code working also
97 * when built with libtool but using something else than GCC.
99 #if !defined(PIC) && !defined(__PIC__)
101 movl $LZMA_CRC64_TABLE, %ebx
102 #elif defined(__MACH__)
106 leal .L_lzma_crc64_table$non_lazy_ptr-.L_pic(%ebx), %ebx
111 addl $_GLOBAL_OFFSET_TABLE_, %ebx
112 movl LZMA_CRC64_TABLE@GOT(%ebx), %ebx
115 /* Complement the initial value. */
121 * Check if there is enough input to use slicing-by-four.
122 * We need eight bytes, because the loop pre-reads four bytes.
127 /* Check if we have reached alignment of four bytes. */
131 /* Calculate CRC of the next input byte. */
137 xorl (%ebx, %ebp, 8), %eax
139 xorl 4(%ebx, %ebp, 8), %edx
145 * If we get here, there's at least eight bytes of aligned input
146 * available. Make %edi multiple of four bytes. Store the possible
147 * remainder over the "size" variable in the argument stack.
149 movl %edi, 0x18(%esp)
151 subl %edi, 0x18(%esp)
154 * Let %edi be buf + size - 4 while running the main loop. This way
155 * we can compare for equality to determine when exit the loop.
160 /* Read in the first four aligned bytes. */
166 movl 0x1800(%ebx, %ebp, 8), %eax
168 movl 0x1804(%ebx, %ebp, 8), %edx
170 xorl 0x1000(%ebx, %ebp, 8), %eax
171 xorl 0x1004(%ebx, %ebp, 8), %edx
174 xorl 0x0800(%ebx, %ebp, 8), %eax
175 xorl 0x0804(%ebx, %ebp, 8), %edx
178 xorl (%ebx, %ebp, 8), %eax
179 xorl 4(%ebx, %ebp, 8), %edx
181 /* Check for end of aligned input. */
185 * Copy the next input byte to %ecx. It is slightly faster to
186 * read it here than at the top of the loop.
192 * Process the remaining four bytes, which we have already
197 movl 0x1800(%ebx, %ebp, 8), %eax
199 movl 0x1804(%ebx, %ebp, 8), %edx
201 xorl 0x1000(%ebx, %ebp, 8), %eax
202 xorl 0x1004(%ebx, %ebp, 8), %edx
205 xorl 0x0800(%ebx, %ebp, 8), %eax
206 xorl 0x0804(%ebx, %ebp, 8), %edx
209 xorl (%ebx, %ebp, 8), %eax
210 xorl 4(%ebx, %ebp, 8), %edx
212 /* Copy the number of remaining bytes to %edi. */
213 movl 0x18(%esp), %edi
216 /* Check for end of input. */
220 /* Calculate CRC of the next input byte. */
226 xorl (%ebx, %ebp, 8), %eax
228 xorl 4(%ebx, %ebp, 8), %edx
233 /* Complement the final value. */
243 #if defined(PIC) || defined(__PIC__)
250 #if defined(__MACH__) && (defined(PIC) || defined(__PIC__))
252 .section __IMPORT,__pointers,non_lazy_symbol_pointers
253 .L_lzma_crc64_table$non_lazy_ptr:
254 .indirect_symbol LZMA_CRC64_TABLE
257 #elif defined(_WIN32)
258 /* This is equivalent of __declspec(dllexport). */
260 .ascii " -export:lzma_crc64"
264 .size LZMA_CRC64, .-LZMA_CRC64
268 * This is needed to support non-executable stack. It's ugly to
269 * use __linux__ here, but I don't know a way to detect when
270 * we are using GNU assembler.
272 #if defined(__ELF__) && defined(__linux__)
273 .section .note.GNU-stack,"",@progbits