2 * Speed-optimized CRC32 using slicing-by-eight algorithm
3 * Instruction set: i386
6 * This code has been put into the public domain by its authors:
7 * Original code by Igor Pavlov <http://7-zip.org/>
8 * Position-independent version by Lasse Collin <lasse.collin@tukaani.org>
10 * This code needs lzma_crc32_table, which can be created using the
13 uint32_t lzma_crc32_table[8][256];
19 static const uint32_t poly32 = UINT32_C(0xEDB88320);
21 // Castagnoli (CRC32C)
22 // static const uint32_t poly32 = UINT32_C(0x82F63B78);
25 // static const uint32_t poly32 = UINT32_C(0xEB31D82E);
27 for (size_t s = 0; s < 8; ++s) {
28 for (size_t b = 0; b < 256; ++b) {
29 uint32_t r = s == 0 ? b : lzma_crc32_table[s - 1][b];
31 for (size_t i = 0; i < 8; ++i) {
33 r = (r >> 1) ^ poly32;
38 lzma_crc32_table[s][b] = r;
43 * The prototype of the CRC32 function:
44 * extern uint32_t lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc);
48 * On some systems, the functions need to be prefixed. The prefix is
49 * usually an underscore.
51 #ifndef __USER_LABEL_PREFIX__
52 # define __USER_LABEL_PREFIX__
54 #define MAKE_SYM_CAT(prefix, sym) prefix ## sym
55 #define MAKE_SYM(prefix, sym) MAKE_SYM_CAT(prefix, sym)
56 #define LZMA_CRC32 MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32)
57 #define LZMA_CRC32_TABLE MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32_table)
60 * Solaris assembler doesn't have .p2align, and Darwin uses .align
61 * differently than GNU/Linux and Solaris.
64 # define ALIGN(pow2, abs) .align pow2
66 # define ALIGN(pow2, abs) .align abs
72 #if !defined(__MACH__) && !defined(_WIN32)
73 .type LZMA_CRC32, @function
82 * %edi size or buf + size
83 * %ebx lzma_crc32_table
92 movl 0x14(%esp), %esi /* buf */
93 movl 0x18(%esp), %edi /* size */
94 movl 0x1C(%esp), %eax /* crc */
97 * Store the address of lzma_crc32_table to %ebx. This is needed to
98 * get position-independent code (PIC).
100 * The PIC macro is defined by libtool, while __PIC__ is defined
101 * by GCC but only on some systems. Testing for both makes it simpler
102 * to test this code without libtool, and keeps the code working also
103 * when built with libtool but using something else than GCC.
105 #if !defined(PIC) && !defined(__PIC__)
107 movl $LZMA_CRC32_TABLE, %ebx
108 #elif defined(__MACH__)
112 leal .L_lzma_crc32_table$non_lazy_ptr-.L_pic(%ebx), %ebx
117 addl $_GLOBAL_OFFSET_TABLE_, %ebx
118 movl LZMA_CRC32_TABLE@GOT(%ebx), %ebx
121 /* Complement the initial value. */
127 * Check if there is enough input to use slicing-by-eight.
128 * We need 16 bytes, because the loop pre-reads eight bytes.
133 /* Check if we have reached alignment of eight bytes. */
137 /* Calculate CRC of the next input byte. */
143 xorl (%ebx, %ebp, 4), %eax
150 * If we get here, there's at least 16 bytes of aligned input
151 * available. Make %edi multiple of eight bytes. Store the possible
152 * remainder over the "size" variable in the argument stack.
154 movl %edi, 0x18(%esp)
156 subl %edi, 0x18(%esp)
159 * Let %edi be buf + size - 8 while running the main loop. This way
160 * we can compare for equality to determine when exit the loop.
165 /* Read in the first eight aligned bytes. */
171 movl 0x0C00(%ebx, %ebp, 4), %edx
173 xorl 0x0800(%ebx, %ebp, 4), %edx
177 xorl 0x0400(%ebx, %ebp, 4), %edx
179 xorl (%ebx, %ebp, 4), %edx
183 * Read the next four bytes, for which the CRC is calculated
184 * on the next interation of the loop.
188 xorl 0x1C00(%ebx, %ebp, 4), %edx
191 xorl 0x1800(%ebx, %ebp, 4), %edx
194 movl 0x1400(%ebx, %eax, 4), %eax
197 xorl 0x1000(%ebx, %ebp, 4), %eax
199 /* Check for end of aligned input. */
205 * Process the remaining eight bytes, which we have already
206 * copied to %ecx and %edx.
208 movl 0x0C00(%ebx, %ebp, 4), %edx
210 xorl 0x0800(%ebx, %ebp, 4), %edx
213 xorl 0x0400(%ebx, %ebp, 4), %edx
215 xorl (%ebx, %ebp, 4), %edx
218 xorl 0x1C00(%ebx, %ebp, 4), %edx
221 xorl 0x1800(%ebx, %ebp, 4), %edx
224 movl 0x1400(%ebx, %eax, 4), %eax
227 xorl 0x1000(%ebx, %ebp, 4), %eax
229 /* Copy the number of remaining bytes to %edi. */
230 movl 0x18(%esp), %edi
233 /* Check for end of input. */
237 /* Calculate CRC of the next input byte. */
243 xorl (%ebx, %ebp, 4), %eax
248 /* Complement the final value. */
257 #if defined(PIC) || defined(__PIC__)
264 #if defined(__MACH__) && (defined(PIC) || defined(__PIC__))
265 .section __IMPORT,__pointers,non_lazy_symbol_pointers
266 .L_lzma_crc32_table$non_lazy_ptr:
267 .indirect_symbol LZMA_CRC32_TABLE
270 #elif defined(_WIN32)
271 /* This is equivalent of __declspec(dllexport). */
273 .ascii " -export:lzma_crc32"
276 .size LZMA_CRC32, .-LZMA_CRC32
280 * This is needed to support non-executable stack. It's ugly to
281 * use __linux__ here, but I don't know a way to detect when
282 * we are using GNU assembler.
284 #if defined(__ELF__) && defined(__linux__)
285 .section .note.GNU-stack,"",@progbits