2 * Speed-optimized CRC32 using slicing-by-eight algorithm
4 * This uses only i386 instructions, but it is optimized for i686 and later
5 * (including e.g. Pentium II/III/IV, Athlon XP, and Core 2). For i586
6 * (e.g. Pentium), slicing-by-four would be better, and even the C version
7 * of slicing-by-eight built with gcc -march=i586 tends to be a little bit
8 * better than this. Very few probably run this code on i586 or older x86
9 * so this shouldn't be a problem in practice.
11 * Authors: Igor Pavlov (original version)
12 * Lasse Collin (AT&T syntax, PIC support, better portability)
14 * This file has been put into the public domain.
15 * You can do whatever you want with this file.
17 * This code needs lzma_crc32_table, which can be created using the
20 uint32_t lzma_crc32_table[8][256];
26 static const uint32_t poly32 = UINT32_C(0xEDB88320);
29 // static const uint32_t poly32 = UINT32_C(0x82F63B78);
32 // static const uint32_t poly32 = UINT32_C(0xEB31D82E);
34 for (size_t s = 0; s < 8; ++s) {
35 for (size_t b = 0; b < 256; ++b) {
36 uint32_t r = s == 0 ? b : lzma_crc32_table[s - 1][b];
38 for (size_t i = 0; i < 8; ++i) {
40 r = (r >> 1) ^ poly32;
45 lzma_crc32_table[s][b] = r;
50 * The prototype of the CRC32 function:
51 * extern uint32_t lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc);
55 * On some systems, the functions need to be prefixed. The prefix is
56 * usually an underscore.
58 #ifndef __USER_LABEL_PREFIX__
59 # define __USER_LABEL_PREFIX__
61 #define MAKE_SYM_CAT(prefix, sym) prefix ## sym
62 #define MAKE_SYM(prefix, sym) MAKE_SYM_CAT(prefix, sym)
63 #define LZMA_CRC32 MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32)
64 #define LZMA_CRC32_TABLE MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32_table)
67 * Solaris assembler doesn't have .p2align, and Darwin uses .align
68 * differently than GNU/Linux and Solaris.
70 #if defined(__MACH__) || defined(__MSDOS__)
71 # define ALIGN(pow2, abs) .align pow2
73 # define ALIGN(pow2, abs) .align abs
79 #if !defined(__MACH__) && !defined(_WIN32) && !defined(__MSDOS__)
80 .type LZMA_CRC32, @function
89 * %edi size or buf + size
90 * %ebx lzma_crc32_table
99 movl 0x14(%esp), %esi /* buf */
100 movl 0x18(%esp), %edi /* size */
101 movl 0x1C(%esp), %eax /* crc */
104 * Store the address of lzma_crc32_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 #if !defined(PIC) && !defined(__PIC__)
114 movl $LZMA_CRC32_TABLE, %ebx
115 #elif defined(__MACH__)
119 leal .L_lzma_crc32_table$non_lazy_ptr-.L_pic(%ebx), %ebx
124 addl $_GLOBAL_OFFSET_TABLE_, %ebx
125 movl LZMA_CRC32_TABLE@GOT(%ebx), %ebx
128 /* Complement the initial value. */
134 * Check if there is enough input to use slicing-by-eight.
135 * We need 16 bytes, because the loop pre-reads eight bytes.
140 /* Check if we have reached alignment of eight bytes. */
144 /* Calculate CRC of the next input byte. */
150 xorl (%ebx, %ebp, 4), %eax
157 * If we get here, there's at least 16 bytes of aligned input
158 * available. Make %edi multiple of eight bytes. Store the possible
159 * remainder over the "size" variable in the argument stack.
161 movl %edi, 0x18(%esp)
163 subl %edi, 0x18(%esp)
166 * Let %edi be buf + size - 8 while running the main loop. This way
167 * we can compare for equality to determine when exit the loop.
172 /* Read in the first eight aligned bytes. */
178 movl 0x0C00(%ebx, %ebp, 4), %edx
180 xorl 0x0800(%ebx, %ebp, 4), %edx
184 xorl 0x0400(%ebx, %ebp, 4), %edx
186 xorl (%ebx, %ebp, 4), %edx
190 * Read the next four bytes, for which the CRC is calculated
191 * on the next interation of the loop.
195 xorl 0x1C00(%ebx, %ebp, 4), %edx
198 xorl 0x1800(%ebx, %ebp, 4), %edx
201 movl 0x1400(%ebx, %eax, 4), %eax
204 xorl 0x1000(%ebx, %ebp, 4), %eax
206 /* Check for end of aligned input. */
212 * Process the remaining eight bytes, which we have already
213 * copied to %ecx and %edx.
215 movl 0x0C00(%ebx, %ebp, 4), %edx
217 xorl 0x0800(%ebx, %ebp, 4), %edx
220 xorl 0x0400(%ebx, %ebp, 4), %edx
222 xorl (%ebx, %ebp, 4), %edx
225 xorl 0x1C00(%ebx, %ebp, 4), %edx
228 xorl 0x1800(%ebx, %ebp, 4), %edx
231 movl 0x1400(%ebx, %eax, 4), %eax
234 xorl 0x1000(%ebx, %ebp, 4), %eax
236 /* Copy the number of remaining bytes to %edi. */
237 movl 0x18(%esp), %edi
240 /* Check for end of input. */
244 /* Calculate CRC of the next input byte. */
250 xorl (%ebx, %ebp, 4), %eax
255 /* Complement the final value. */
264 #if defined(PIC) || defined(__PIC__)
271 #if defined(__MACH__) && (defined(PIC) || defined(__PIC__))
273 .section __IMPORT,__pointers,non_lazy_symbol_pointers
274 .L_lzma_crc32_table$non_lazy_ptr:
275 .indirect_symbol LZMA_CRC32_TABLE
278 #elif defined(_WIN32)
279 # ifndef LZMA_API_STATIC
280 /* This is equivalent of __declspec(dllexport). */
282 .ascii " -export:lzma_crc32"
285 #elif !defined(__MSDOS__)
287 .size LZMA_CRC32, .-LZMA_CRC32
291 * This is needed to support non-executable stack. It's ugly to
292 * use __linux__ here, but I don't know a way to detect when
293 * we are using GNU assembler.
295 #if defined(__ELF__) && defined(__linux__)
296 .section .note.GNU-stack,"",@progbits