]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc32_x86.S
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / src / liblzma / check / crc32_x86.S
1 /*
2  * Speed-optimized CRC32 using slicing-by-eight algorithm
3  *
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.
10  *
11  * Authors: Igor Pavlov (original version)
12  *          Lasse Collin (AT&T syntax, PIC support, better portability)
13  *
14  * This file has been put into the public domain.
15  * You can do whatever you want with this file.
16  *
17  * This code needs lzma_crc32_table, which can be created using the
18  * following C code:
19
20 uint32_t lzma_crc32_table[8][256];
21
22 void
23 init_table(void)
24 {
25         // IEEE-802.3
26         static const uint32_t poly32 = UINT32_C(0xEDB88320);
27
28         // Castagnoli
29         // static const uint32_t poly32 = UINT32_C(0x82F63B78);
30
31         // Koopman
32         // static const uint32_t poly32 = UINT32_C(0xEB31D82E);
33
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];
37
38                         for (size_t i = 0; i < 8; ++i) {
39                                 if (r & 1)
40                                         r = (r >> 1) ^ poly32;
41                                 else
42                                         r >>= 1;
43                         }
44
45                         lzma_crc32_table[s][b] = r;
46                 }
47         }
48 }
49
50  * The prototype of the CRC32 function:
51  * extern uint32_t lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc);
52  */
53
54 /*
55  * On some systems, the functions need to be prefixed. The prefix is
56  * usually an underscore.
57  */
58 #ifndef __USER_LABEL_PREFIX__
59 #       define __USER_LABEL_PREFIX__
60 #endif
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)
65
66 /*
67  * Solaris assembler doesn't have .p2align, and Darwin uses .align
68  * differently than GNU/Linux and Solaris.
69  */
70 #if defined(__MACH__) || defined(__MSDOS__)
71 #       define ALIGN(pow2, abs) .align pow2
72 #else
73 #       define ALIGN(pow2, abs) .align abs
74 #endif
75
76         .text
77         .globl  LZMA_CRC32
78
79 #if !defined(__MACH__) && !defined(_WIN32) && !defined(__MSDOS__)
80         .type   LZMA_CRC32, @function
81 #endif
82
83         ALIGN(4, 16)
84 LZMA_CRC32:
85         /*
86          * Register usage:
87          * %eax crc
88          * %esi buf
89          * %edi size or buf + size
90          * %ebx lzma_crc32_table
91          * %ebp Table index
92          * %ecx Temporary
93          * %edx Temporary
94          */
95         pushl   %ebx
96         pushl   %esi
97         pushl   %edi
98         pushl   %ebp
99         movl    0x14(%esp), %esi /* buf */
100         movl    0x18(%esp), %edi /* size */
101         movl    0x1C(%esp), %eax /* crc */
102
103         /*
104          * Store the address of lzma_crc32_table to %ebx. This is needed to
105          * get position-independent code (PIC).
106          *
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.
111          */
112 #if !defined(PIC) && !defined(__PIC__)
113         /* Not PIC */
114         movl    $LZMA_CRC32_TABLE, %ebx
115 #elif defined(__MACH__)
116         /* Mach-O */
117         call    .L_get_pc
118 .L_pic:
119         leal    .L_lzma_crc32_table$non_lazy_ptr-.L_pic(%ebx), %ebx
120         movl    (%ebx), %ebx
121 #else
122         /* ELF */
123         call    .L_get_pc
124         addl    $_GLOBAL_OFFSET_TABLE_, %ebx
125         movl    LZMA_CRC32_TABLE@GOT(%ebx), %ebx
126 #endif
127
128         /* Complement the initial value. */
129         notl    %eax
130
131         ALIGN(4, 16)
132 .L_align:
133         /*
134          * Check if there is enough input to use slicing-by-eight.
135          * We need 16 bytes, because the loop pre-reads eight bytes.
136          */
137         cmpl    $16, %edi
138         jl      .L_rest
139
140         /* Check if we have reached alignment of eight bytes. */
141         testl   $7, %esi
142         jz      .L_slice
143
144         /* Calculate CRC of the next input byte. */
145         movzbl  (%esi), %ebp
146         incl    %esi
147         movzbl  %al, %ecx
148         xorl    %ecx, %ebp
149         shrl    $8, %eax
150         xorl    (%ebx, %ebp, 4), %eax
151         decl    %edi
152         jmp     .L_align
153
154         ALIGN(2, 4)
155 .L_slice:
156         /*
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.
160          */
161         movl    %edi, 0x18(%esp)
162         andl    $-8, %edi
163         subl    %edi, 0x18(%esp)
164
165         /*
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.
168          */
169         addl    %esi, %edi
170         subl    $8, %edi
171
172         /* Read in the first eight aligned bytes. */
173         xorl    (%esi), %eax
174         movl    4(%esi), %ecx
175         movzbl  %cl, %ebp
176
177 .L_loop:
178         movl    0x0C00(%ebx, %ebp, 4), %edx
179         movzbl  %ch, %ebp
180         xorl    0x0800(%ebx, %ebp, 4), %edx
181         shrl    $16, %ecx
182         xorl    8(%esi), %edx
183         movzbl  %cl, %ebp
184         xorl    0x0400(%ebx, %ebp, 4), %edx
185         movzbl  %ch, %ebp
186         xorl    (%ebx, %ebp, 4), %edx
187         movzbl  %al, %ebp
188
189         /*
190          * Read the next four bytes, for which the CRC is calculated
191          * on the next interation of the loop.
192          */
193         movl    12(%esi), %ecx
194
195         xorl    0x1C00(%ebx, %ebp, 4), %edx
196         movzbl  %ah, %ebp
197         shrl    $16, %eax
198         xorl    0x1800(%ebx, %ebp, 4), %edx
199         movzbl  %ah, %ebp
200         movzbl  %al, %eax
201         movl    0x1400(%ebx, %eax, 4), %eax
202         addl    $8, %esi
203         xorl    %edx, %eax
204         xorl    0x1000(%ebx, %ebp, 4), %eax
205
206         /* Check for end of aligned input. */
207         cmpl    %edi, %esi
208         movzbl  %cl, %ebp
209         jne     .L_loop
210
211         /*
212          * Process the remaining eight bytes, which we have already
213          * copied to %ecx and %edx.
214          */
215         movl    0x0C00(%ebx, %ebp, 4), %edx
216         movzbl  %ch, %ebp
217         xorl    0x0800(%ebx, %ebp, 4), %edx
218         shrl    $16, %ecx
219         movzbl  %cl, %ebp
220         xorl    0x0400(%ebx, %ebp, 4), %edx
221         movzbl  %ch, %ebp
222         xorl    (%ebx, %ebp, 4), %edx
223         movzbl  %al, %ebp
224
225         xorl    0x1C00(%ebx, %ebp, 4), %edx
226         movzbl  %ah, %ebp
227         shrl    $16, %eax
228         xorl    0x1800(%ebx, %ebp, 4), %edx
229         movzbl  %ah, %ebp
230         movzbl  %al, %eax
231         movl    0x1400(%ebx, %eax, 4), %eax
232         addl    $8, %esi
233         xorl    %edx, %eax
234         xorl    0x1000(%ebx, %ebp, 4), %eax
235
236         /* Copy the number of remaining bytes to %edi. */
237         movl    0x18(%esp), %edi
238
239 .L_rest:
240         /* Check for end of input. */
241         testl   %edi, %edi
242         jz      .L_return
243
244         /* Calculate CRC of the next input byte. */
245         movzbl  (%esi), %ebp
246         incl    %esi
247         movzbl  %al, %ecx
248         xorl    %ecx, %ebp
249         shrl    $8, %eax
250         xorl    (%ebx, %ebp, 4), %eax
251         decl    %edi
252         jmp     .L_rest
253
254 .L_return:
255         /* Complement the final value. */
256         notl    %eax
257
258         popl    %ebp
259         popl    %edi
260         popl    %esi
261         popl    %ebx
262         ret
263
264 #if defined(PIC) || defined(__PIC__)
265         ALIGN(4, 16)
266 .L_get_pc:
267         movl    (%esp), %ebx
268         ret
269 #endif
270
271 #if defined(__MACH__) && (defined(PIC) || defined(__PIC__))
272         /* Mach-O PIC */
273         .section __IMPORT,__pointers,non_lazy_symbol_pointers
274 .L_lzma_crc32_table$non_lazy_ptr:
275         .indirect_symbol LZMA_CRC32_TABLE
276         .long 0
277
278 #elif defined(_WIN32)
279 #       ifndef LZMA_API_STATIC
280         /* This is equivalent of __declspec(dllexport). */
281         .section .drectve
282         .ascii " -export:lzma_crc32"
283 #       endif
284
285 #elif !defined(__MSDOS__)
286         /* ELF */
287         .size   LZMA_CRC32, .-LZMA_CRC32
288 #endif
289
290 /*
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.
294  */
295 #if defined(__ELF__) && defined(__linux__)
296         .section        .note.GNU-stack,"",@progbits
297 #endif