]> icculus.org git repositories - icculus/xz.git/blob - src/liblzma/check/crc64_x86.S
Put the interesting parts of XZ Utils into the public domain.
[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(__MSDOS__)
70         .type   LZMA_CRC64, @function
71 #endif
72
73         ALIGN(4, 16)
74 LZMA_CRC64:
75         /*
76          * Register usage:
77          * %eax crc LSB
78          * %edx crc MSB
79          * %esi buf
80          * %edi size or buf + size
81          * %ebx lzma_crc64_table
82          * %ebp Table index
83          * %ecx Temporary
84          */
85         pushl   %ebx
86         pushl   %esi
87         pushl   %edi
88         pushl   %ebp
89         movl    0x14(%esp), %esi /* buf */
90         movl    0x18(%esp), %edi /* size */
91         movl    0x1C(%esp), %eax /* crc LSB */
92         movl    0x20(%esp), %edx /* crc MSB */
93
94         /*
95          * Store the address of lzma_crc64_table to %ebx. This is needed to
96          * get position-independent code (PIC).
97          *
98          * The PIC macro is defined by libtool, while __PIC__ is defined
99          * by GCC but only on some systems. Testing for both makes it simpler
100          * to test this code without libtool, and keeps the code working also
101          * when built with libtool but using something else than GCC.
102          */
103 #if !defined(PIC) && !defined(__PIC__)
104         /* Not PIC */
105         movl    $LZMA_CRC64_TABLE, %ebx
106 #elif defined(__MACH__)
107         /* Mach-O */
108         call    .L_get_pc
109 .L_pic:
110         leal    .L_lzma_crc64_table$non_lazy_ptr-.L_pic(%ebx), %ebx
111         movl    (%ebx), %ebx
112 #else
113         /* ELF */
114         call    .L_get_pc
115         addl    $_GLOBAL_OFFSET_TABLE_, %ebx
116         movl    LZMA_CRC64_TABLE@GOT(%ebx), %ebx
117 #endif
118
119         /* Complement the initial value. */
120         notl    %eax
121         notl    %edx
122
123 .L_align:
124         /*
125          * Check if there is enough input to use slicing-by-four.
126          * We need eight bytes, because the loop pre-reads four bytes.
127          */
128         cmpl    $8, %edi
129         jl      .L_rest
130
131         /* Check if we have reached alignment of four bytes. */
132         testl   $3, %esi
133         jz      .L_slice
134
135         /* Calculate CRC of the next input byte. */
136         movzbl  (%esi), %ebp
137         incl    %esi
138         movzbl  %al, %ecx
139         xorl    %ecx, %ebp
140         shrdl   $8, %edx, %eax
141         xorl    (%ebx, %ebp, 8), %eax
142         shrl    $8, %edx
143         xorl    4(%ebx, %ebp, 8), %edx
144         decl    %edi
145         jmp     .L_align
146
147 .L_slice:
148         /*
149          * If we get here, there's at least eight bytes of aligned input
150          * available. Make %edi multiple of four bytes. Store the possible
151          * remainder over the "size" variable in the argument stack.
152          */
153         movl    %edi, 0x18(%esp)
154         andl    $-4, %edi
155         subl    %edi, 0x18(%esp)
156
157         /*
158          * Let %edi be buf + size - 4 while running the main loop. This way
159          * we can compare for equality to determine when exit the loop.
160          */
161         addl    %esi, %edi
162         subl    $4, %edi
163
164         /* Read in the first four aligned bytes. */
165         movl    (%esi), %ecx
166
167 .L_loop:
168         xorl    %eax, %ecx
169         movzbl  %cl, %ebp
170         movl    0x1800(%ebx, %ebp, 8), %eax
171         xorl    %edx, %eax
172         movl    0x1804(%ebx, %ebp, 8), %edx
173         movzbl  %ch, %ebp
174         xorl    0x1000(%ebx, %ebp, 8), %eax
175         xorl    0x1004(%ebx, %ebp, 8), %edx
176         shrl    $16, %ecx
177         movzbl  %cl, %ebp
178         xorl    0x0800(%ebx, %ebp, 8), %eax
179         xorl    0x0804(%ebx, %ebp, 8), %edx
180         movzbl  %ch, %ebp
181         addl    $4, %esi
182         xorl    (%ebx, %ebp, 8), %eax
183         xorl    4(%ebx, %ebp, 8), %edx
184
185         /* Check for end of aligned input. */
186         cmpl    %edi, %esi
187
188         /*
189          * Copy the next input byte to %ecx. It is slightly faster to
190          * read it here than at the top of the loop.
191          */
192         movl    (%esi), %ecx
193         jl      .L_loop
194
195         /*
196          * Process the remaining four bytes, which we have already
197          * copied to %ecx.
198          */
199         xorl    %eax, %ecx
200         movzbl  %cl, %ebp
201         movl    0x1800(%ebx, %ebp, 8), %eax
202         xorl    %edx, %eax
203         movl    0x1804(%ebx, %ebp, 8), %edx
204         movzbl  %ch, %ebp
205         xorl    0x1000(%ebx, %ebp, 8), %eax
206         xorl    0x1004(%ebx, %ebp, 8), %edx
207         shrl    $16, %ecx
208         movzbl  %cl, %ebp
209         xorl    0x0800(%ebx, %ebp, 8), %eax
210         xorl    0x0804(%ebx, %ebp, 8), %edx
211         movzbl  %ch, %ebp
212         addl    $4, %esi
213         xorl    (%ebx, %ebp, 8), %eax
214         xorl    4(%ebx, %ebp, 8), %edx
215
216         /* Copy the number of remaining bytes to %edi. */
217         movl    0x18(%esp), %edi
218
219 .L_rest:
220         /* Check for end of input. */
221         testl   %edi, %edi
222         jz      .L_return
223
224         /* Calculate CRC of the next input byte. */
225         movzbl  (%esi), %ebp
226         incl    %esi
227         movzbl  %al, %ecx
228         xorl    %ecx, %ebp
229         shrdl   $8, %edx, %eax
230         xorl    (%ebx, %ebp, 8), %eax
231         shrl    $8, %edx
232         xorl    4(%ebx, %ebp, 8), %edx
233         decl    %edi
234         jmp     .L_rest
235
236 .L_return:
237         /* Complement the final value. */
238         notl    %eax
239         notl    %edx
240
241         popl    %ebp
242         popl    %edi
243         popl    %esi
244         popl    %ebx
245         ret
246
247 #if defined(PIC) || defined(__PIC__)
248         ALIGN(4, 16)
249 .L_get_pc:
250         movl    (%esp), %ebx
251         ret
252 #endif
253
254 #if defined(__MACH__) && (defined(PIC) || defined(__PIC__))
255         /* Mach-O PIC */
256         .section __IMPORT,__pointers,non_lazy_symbol_pointers
257 .L_lzma_crc64_table$non_lazy_ptr:
258         .indirect_symbol LZMA_CRC64_TABLE
259         .long 0
260
261 #elif defined(_WIN32)
262 #       ifndef LZMA_API_STATIC
263         /* This is equivalent of __declspec(dllexport). */
264         .section .drectve
265         .ascii " -export:lzma_crc64"
266 #       endif
267
268 #elif !defined(__MSDOS__)
269         /* ELF */
270         .size   LZMA_CRC64, .-LZMA_CRC64
271 #endif
272
273 /*
274  * This is needed to support non-executable stack. It's ugly to
275  * use __linux__ here, but I don't know a way to detect when
276  * we are using GNU assembler.
277  */
278 #if defined(__ELF__) && defined(__linux__)
279         .section        .note.GNU-stack,"",@progbits
280 #endif