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