]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc64_x86.S
Basic support for building with Cygwin and MinGW using
[icculus/xz.git] / src / liblzma / check / crc64_x86.S
1 /*
2  * Speed-optimized CRC64 using slicing-by-four 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).
6  *
7  * Authors: Igor Pavlov (original CRC32 assembly code)
8  *          Lasse Collin (CRC64 adaptation of the modified CRC32 code)
9  *
10  * This file has been put into the public domain.
11  * You can do whatever you want with this file.
12  *
13  * This code needs lzma_crc64_table, which can be created using the
14  * following C code:
15
16 uint64_t lzma_crc64_table[4][256];
17
18 void
19 init_table(void)
20 {
21         // ECMA-182
22         static const uint64_t poly64 = UINT64_C(0xC96C5795D7870F42);
23
24         for (size_t s = 0; s < 4; ++s) {
25                 for (size_t b = 0; b < 256; ++b) {
26                         uint64_t r = s == 0 ? b : lzma_crc64_table[s - 1][b];
27
28                         for (size_t i = 0; i < 8; ++i) {
29                                 if (r & 1)
30                                         r = (r >> 1) ^ poly64;
31                                 else
32                                         r >>= 1;
33                         }
34
35                         lzma_crc64_table[s][b] = r;
36                 }
37         }
38 }
39
40  * The prototype of the CRC64 function:
41  * extern uint64_t lzma_crc64(const uint8_t *buf, size_t size, uint64_t crc);
42  */
43
44 /*
45  * On some systems, the functions need to be prefixed. The prefix is
46  * usually an underscore.
47  */
48 #ifndef __USER_LABEL_PREFIX__
49 #       define __USER_LABEL_PREFIX__
50 #endif
51 #define MAKE_SYM_CAT(prefix, sym) prefix ## sym
52 #define MAKE_SYM(prefix, sym) MAKE_SYM_CAT(prefix, sym)
53 #define LZMA_CRC64 MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc64)
54 #define LZMA_CRC64_TABLE MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc64_table)
55
56 /*
57  * Solaris assembler doesn't have .p2align, and Darwin uses .align
58  * differently than GNU/Linux and Solaris.
59  */
60 #if defined(__MACH__) || defined(__MSDOS__)
61 #       define ALIGN(pow2, abs) .align pow2
62 #else
63 #       define ALIGN(pow2, abs) .align abs
64 #endif
65
66         .text
67         .globl  LZMA_CRC64
68
69 #if !defined(__MACH__) && !defined(_WIN32) && !defined(__CYGWIN__) \
70                 && !defined(__MSDOS__)
71         .type   LZMA_CRC64, @function
72 #endif
73
74         ALIGN(4, 16)
75 LZMA_CRC64:
76         /*
77          * Register usage:
78          * %eax crc LSB
79          * %edx crc MSB
80          * %esi buf
81          * %edi size or buf + size
82          * %ebx lzma_crc64_table
83          * %ebp Table index
84          * %ecx Temporary
85          */
86         pushl   %ebx
87         pushl   %esi
88         pushl   %edi
89         pushl   %ebp
90         movl    0x14(%esp), %esi /* buf */
91         movl    0x18(%esp), %edi /* size */
92         movl    0x1C(%esp), %eax /* crc LSB */
93         movl    0x20(%esp), %edx /* crc MSB */
94
95         /*
96          * Store the address of lzma_crc64_table to %ebx. This is needed to
97          * get position-independent code (PIC).
98          *
99          * The PIC macro is defined by libtool, while __PIC__ is defined
100          * by GCC but only on some systems. Testing for both makes it simpler
101          * to test this code without libtool, and keeps the code working also
102          * when built with libtool but using something else than GCC.
103          */
104 #if !defined(PIC) && !defined(__PIC__)
105         /* Not PIC */
106         movl    $LZMA_CRC64_TABLE, %ebx
107 #elif defined(__MACH__)
108         /* Mach-O */
109         call    .L_get_pc
110 .L_pic:
111         leal    .L_lzma_crc64_table$non_lazy_ptr-.L_pic(%ebx), %ebx
112         movl    (%ebx), %ebx
113 #else
114         /* ELF */
115         call    .L_get_pc
116         addl    $_GLOBAL_OFFSET_TABLE_, %ebx
117         movl    LZMA_CRC64_TABLE@GOT(%ebx), %ebx
118 #endif
119
120         /* Complement the initial value. */
121         notl    %eax
122         notl    %edx
123
124 .L_align:
125         /*
126          * Check if there is enough input to use slicing-by-four.
127          * We need eight bytes, because the loop pre-reads four bytes.
128          */
129         cmpl    $8, %edi
130         jl      .L_rest
131
132         /* Check if we have reached alignment of four bytes. */
133         testl   $3, %esi
134         jz      .L_slice
135
136         /* Calculate CRC of the next input byte. */
137         movzbl  (%esi), %ebp
138         incl    %esi
139         movzbl  %al, %ecx
140         xorl    %ecx, %ebp
141         shrdl   $8, %edx, %eax
142         xorl    (%ebx, %ebp, 8), %eax
143         shrl    $8, %edx
144         xorl    4(%ebx, %ebp, 8), %edx
145         decl    %edi
146         jmp     .L_align
147
148 .L_slice:
149         /*
150          * If we get here, there's at least eight bytes of aligned input
151          * available. Make %edi multiple of four bytes. Store the possible
152          * remainder over the "size" variable in the argument stack.
153          */
154         movl    %edi, 0x18(%esp)
155         andl    $-4, %edi
156         subl    %edi, 0x18(%esp)
157
158         /*
159          * Let %edi be buf + size - 4 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    $4, %edi
164
165         /* Read in the first four aligned bytes. */
166         movl    (%esi), %ecx
167
168 .L_loop:
169         xorl    %eax, %ecx
170         movzbl  %cl, %ebp
171         movl    0x1800(%ebx, %ebp, 8), %eax
172         xorl    %edx, %eax
173         movl    0x1804(%ebx, %ebp, 8), %edx
174         movzbl  %ch, %ebp
175         xorl    0x1000(%ebx, %ebp, 8), %eax
176         xorl    0x1004(%ebx, %ebp, 8), %edx
177         shrl    $16, %ecx
178         movzbl  %cl, %ebp
179         xorl    0x0800(%ebx, %ebp, 8), %eax
180         xorl    0x0804(%ebx, %ebp, 8), %edx
181         movzbl  %ch, %ebp
182         addl    $4, %esi
183         xorl    (%ebx, %ebp, 8), %eax
184         xorl    4(%ebx, %ebp, 8), %edx
185
186         /* Check for end of aligned input. */
187         cmpl    %edi, %esi
188
189         /*
190          * Copy the next input byte to %ecx. It is slightly faster to
191          * read it here than at the top of the loop.
192          */
193         movl    (%esi), %ecx
194         jl      .L_loop
195
196         /*
197          * Process the remaining four bytes, which we have already
198          * copied to %ecx.
199          */
200         xorl    %eax, %ecx
201         movzbl  %cl, %ebp
202         movl    0x1800(%ebx, %ebp, 8), %eax
203         xorl    %edx, %eax
204         movl    0x1804(%ebx, %ebp, 8), %edx
205         movzbl  %ch, %ebp
206         xorl    0x1000(%ebx, %ebp, 8), %eax
207         xorl    0x1004(%ebx, %ebp, 8), %edx
208         shrl    $16, %ecx
209         movzbl  %cl, %ebp
210         xorl    0x0800(%ebx, %ebp, 8), %eax
211         xorl    0x0804(%ebx, %ebp, 8), %edx
212         movzbl  %ch, %ebp
213         addl    $4, %esi
214         xorl    (%ebx, %ebp, 8), %eax
215         xorl    4(%ebx, %ebp, 8), %edx
216
217         /* Copy the number of remaining bytes to %edi. */
218         movl    0x18(%esp), %edi
219
220 .L_rest:
221         /* Check for end of input. */
222         testl   %edi, %edi
223         jz      .L_return
224
225         /* Calculate CRC of the next input byte. */
226         movzbl  (%esi), %ebp
227         incl    %esi
228         movzbl  %al, %ecx
229         xorl    %ecx, %ebp
230         shrdl   $8, %edx, %eax
231         xorl    (%ebx, %ebp, 8), %eax
232         shrl    $8, %edx
233         xorl    4(%ebx, %ebp, 8), %edx
234         decl    %edi
235         jmp     .L_rest
236
237 .L_return:
238         /* Complement the final value. */
239         notl    %eax
240         notl    %edx
241
242         popl    %ebp
243         popl    %edi
244         popl    %esi
245         popl    %ebx
246         ret
247
248 #if defined(PIC) || defined(__PIC__)
249         ALIGN(4, 16)
250 .L_get_pc:
251         movl    (%esp), %ebx
252         ret
253 #endif
254
255 #if defined(__MACH__) && (defined(PIC) || defined(__PIC__))
256         /* Mach-O PIC */
257         .section __IMPORT,__pointers,non_lazy_symbol_pointers
258 .L_lzma_crc64_table$non_lazy_ptr:
259         .indirect_symbol LZMA_CRC64_TABLE
260         .long 0
261
262 #elif defined(_WIN32) || defined(__CYGWIN__)
263 #       ifdef DLL_EXPORT
264         /* This is equivalent of __declspec(dllexport). */
265         .section .drectve
266         .ascii " -export:lzma_crc64"
267 #       endif
268
269 #elif !defined(__MSDOS__)
270         /* ELF */
271         .size   LZMA_CRC64, .-LZMA_CRC64
272 #endif
273
274 /*
275  * This is needed to support non-executable stack. It's ugly to
276  * use __linux__ here, but I don't know a way to detect when
277  * we are using GNU assembler.
278  */
279 #if defined(__ELF__) && defined(__linux__)
280         .section        .note.GNU-stack,"",@progbits
281 #endif