1 /* SPDX-License-Identifier: 0BSD */
4 * Speed-optimized CRC64 using slicing-by-four algorithm
6 * This uses only i386 instructions, but it is optimized for i686 and later
7 * (including e.g. Pentium II/III/IV, Athlon XP, and Core 2).
9 * Authors: Igor Pavlov (original CRC32 assembly code)
10 * Lasse Collin (CRC64 adaptation of the modified CRC32 code)
12 * This code needs lzma_crc64_table, which can be created using the
15 uint64_t lzma_crc64_table[4][256];
21 static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
23 for (size_t s = 0; s < 4; ++s) {
24 for (size_t b = 0; b < 256; ++b) {
25 uint64_t r = s == 0 ? b : lzma_crc64_table[s - 1][b];
27 for (size_t i = 0; i < 8; ++i) {
29 r = (r >> 1) ^ poly64;
34 lzma_crc64_table[s][b] = r;
39 * The prototype of the CRC64 function:
40 * extern uint64_t lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc);
43 /* When Intel CET is enabled, include <cet.h> in assembly code to mark
52 * On some systems, the functions need to be prefixed. The prefix is
53 * usually an underscore.
55 #ifndef __USER_LABEL_PREFIX__
56 # define __USER_LABEL_PREFIX__
58 #define MAKE_SYM_CAT(prefix, sym) prefix ## sym
59 #define MAKE_SYM(prefix, sym) MAKE_SYM_CAT(prefix, sym)
60 #define LZMA_CRC64 MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc64)
61 #define LZMA_CRC64_TABLE MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc64_table)
64 * Solaris assembler doesn't have .p2align, and Darwin uses .align
65 * differently than GNU/Linux and Solaris.
67 #if defined(__APPLE__) || defined(__MSDOS__)
68 # define ALIGN(pow2, abs) .align pow2
70 # define ALIGN(pow2, abs) .align abs
76 #if !defined(__APPLE__) && !defined(_WIN32) && !defined(__CYGWIN__) \
77 && !defined(__MSDOS__)
78 .type LZMA_CRC64, @function
89 * %edi size or buf + size
90 * %ebx lzma_crc64_table
98 movl 0x14(%esp), %esi /* buf */
99 movl 0x18(%esp), %edi /* size */
100 movl 0x1C(%esp), %eax /* crc LSB */
101 movl 0x20(%esp), %edx /* crc MSB */
104 * Store the address of lzma_crc64_table to %ebx. This is needed to
105 * get position-independent code (PIC).
107 * The PIC macro is defined by libtool, while __PIC__ is defined
108 * by GCC but only on some systems. Testing for both makes it simpler
109 * to test this code without libtool, and keeps the code working also
110 * when built with libtool but using something else than GCC.
112 * I understood that libtool may define PIC on Windows even though
113 * the code in Windows DLLs is not PIC in sense that it is in ELF
114 * binaries, so we need a separate check to always use the non-PIC
117 #if (!defined(PIC) && !defined(__PIC__)) \
118 || (defined(_WIN32) || defined(__CYGWIN__))
120 movl $ LZMA_CRC64_TABLE, %ebx
121 #elif defined(__APPLE__)
125 leal .L_lzma_crc64_table$non_lazy_ptr-.L_pic(%ebx), %ebx
130 addl $_GLOBAL_OFFSET_TABLE_, %ebx
131 movl LZMA_CRC64_TABLE@GOT(%ebx), %ebx
134 /* Complement the initial value. */
140 * Check if there is enough input to use slicing-by-four.
141 * We need eight bytes, because the loop pre-reads four bytes.
146 /* Check if we have reached alignment of four bytes. */
150 /* Calculate CRC of the next input byte. */
156 xorl (%ebx, %ebp, 8), %eax
158 xorl 4(%ebx, %ebp, 8), %edx
164 * If we get here, there's at least eight bytes of aligned input
165 * available. Make %edi multiple of four bytes. Store the possible
166 * remainder over the "size" variable in the argument stack.
168 movl %edi, 0x18(%esp)
170 subl %edi, 0x18(%esp)
173 * Let %edi be buf + size - 4 while running the main loop. This way
174 * we can compare for equality to determine when exit the loop.
179 /* Read in the first four aligned bytes. */
185 movl 0x1800(%ebx, %ebp, 8), %eax
187 movl 0x1804(%ebx, %ebp, 8), %edx
189 xorl 0x1000(%ebx, %ebp, 8), %eax
190 xorl 0x1004(%ebx, %ebp, 8), %edx
193 xorl 0x0800(%ebx, %ebp, 8), %eax
194 xorl 0x0804(%ebx, %ebp, 8), %edx
197 xorl (%ebx, %ebp, 8), %eax
198 xorl 4(%ebx, %ebp, 8), %edx
200 /* Check for end of aligned input. */
204 * Copy the next input byte to %ecx. It is slightly faster to
205 * read it here than at the top of the loop.
211 * Process the remaining four bytes, which we have already
216 movl 0x1800(%ebx, %ebp, 8), %eax
218 movl 0x1804(%ebx, %ebp, 8), %edx
220 xorl 0x1000(%ebx, %ebp, 8), %eax
221 xorl 0x1004(%ebx, %ebp, 8), %edx
224 xorl 0x0800(%ebx, %ebp, 8), %eax
225 xorl 0x0804(%ebx, %ebp, 8), %edx
228 xorl (%ebx, %ebp, 8), %eax
229 xorl 4(%ebx, %ebp, 8), %edx
231 /* Copy the number of remaining bytes to %edi. */
232 movl 0x18(%esp), %edi
235 /* Check for end of input. */
239 /* Calculate CRC of the next input byte. */
245 xorl (%ebx, %ebp, 8), %eax
247 xorl 4(%ebx, %ebp, 8), %edx
252 /* Complement the final value. */
262 #if defined(PIC) || defined(__PIC__)
269 #if defined(__APPLE__) && (defined(PIC) || defined(__PIC__))
271 .section __IMPORT,__pointers,non_lazy_symbol_pointers
272 .L_lzma_crc64_table$non_lazy_ptr:
273 .indirect_symbol LZMA_CRC64_TABLE
276 #elif defined(_WIN32) || defined(__CYGWIN__)
278 /* This is equivalent of __declspec(dllexport). */
280 .ascii " -export:lzma_crc64"
283 #elif !defined(__MSDOS__)
285 .size LZMA_CRC64, .-LZMA_CRC64
289 * This is needed to support non-executable stack. It's ugly to
290 * use __FreeBSD__ and __linux__ here, but I don't know a way to detect when
291 * we are using GNU assembler.
293 #if defined(__ELF__) && (defined(__FreeBSD__) || defined(__linux__))
294 .section .note.GNU-stack,"",@progbits