Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmliblzma/liblzma/check/crc32_x86.S
3153 views
1
/* SPDX-License-Identifier: 0BSD */
2
3
/*
4
* Speed-optimized CRC32 using slicing-by-eight algorithm
5
*
6
* This uses only i386 instructions, but it is optimized for i686 and later
7
* (including e.g. Pentium II/III/IV, Athlon XP, and Core 2). For i586
8
* (e.g. Pentium), slicing-by-four would be better, and even the C version
9
* of slicing-by-eight built with gcc -march=i586 tends to be a little bit
10
* better than this. Very few probably run this code on i586 or older x86
11
* so this shouldn't be a problem in practice.
12
*
13
* Authors: Igor Pavlov (original version)
14
* Lasse Collin (AT&T syntax, PIC support, better portability)
15
*
16
* This code needs lzma_crc32_table, which can be created using the
17
* following C code:
18
19
uint32_t lzma_crc32_table[8][256];
20
21
void
22
init_table(void)
23
{
24
// IEEE-802.3
25
static const uint32_t poly32 = UINT32_C(0xEDB88320);
26
27
// Castagnoli
28
// static const uint32_t poly32 = UINT32_C(0x82F63B78);
29
30
// Koopman
31
// static const uint32_t poly32 = UINT32_C(0xEB31D82E);
32
33
for (size_t s = 0; s < 8; ++s) {
34
for (size_t b = 0; b < 256; ++b) {
35
uint32_t r = s == 0 ? b : lzma_crc32_table[s - 1][b];
36
37
for (size_t i = 0; i < 8; ++i) {
38
if (r & 1)
39
r = (r >> 1) ^ poly32;
40
else
41
r >>= 1;
42
}
43
44
lzma_crc32_table[s][b] = r;
45
}
46
}
47
}
48
49
* The prototype of the CRC32 function:
50
* extern uint32_t lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc);
51
*/
52
53
/* When Intel CET is enabled, include <cet.h> in assembly code to mark
54
Intel CET support. */
55
#ifdef __CET__
56
# include <cet.h>
57
#else
58
# define _CET_ENDBR
59
#endif
60
61
/*
62
* On some systems, the functions need to be prefixed. The prefix is
63
* usually an underscore.
64
*/
65
#ifndef __USER_LABEL_PREFIX__
66
# define __USER_LABEL_PREFIX__
67
#endif
68
#define MAKE_SYM_CAT(prefix, sym) prefix ## sym
69
#define MAKE_SYM(prefix, sym) MAKE_SYM_CAT(prefix, sym)
70
#define LZMA_CRC32 MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32)
71
#define LZMA_CRC32_TABLE MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32_table)
72
73
/*
74
* Solaris assembler doesn't have .p2align, and Darwin uses .align
75
* differently than GNU/Linux and Solaris.
76
*/
77
#if defined(__APPLE__) || defined(__MSDOS__)
78
# define ALIGN(pow2, abs) .align pow2
79
#else
80
# define ALIGN(pow2, abs) .align abs
81
#endif
82
83
.text
84
.globl LZMA_CRC32
85
86
#if !defined(__APPLE__) && !defined(_WIN32) && !defined(__CYGWIN__) \
87
&& !defined(__MSDOS__)
88
.type LZMA_CRC32, @function
89
#endif
90
91
ALIGN(4, 16)
92
LZMA_CRC32:
93
_CET_ENDBR
94
/*
95
* Register usage:
96
* %eax crc
97
* %esi buf
98
* %edi size or buf + size
99
* %ebx lzma_crc32_table
100
* %ebp Table index
101
* %ecx Temporary
102
* %edx Temporary
103
*/
104
pushl %ebx
105
pushl %esi
106
pushl %edi
107
pushl %ebp
108
movl 0x14(%esp), %esi /* buf */
109
movl 0x18(%esp), %edi /* size */
110
movl 0x1C(%esp), %eax /* crc */
111
112
/*
113
* Store the address of lzma_crc32_table to %ebx. This is needed to
114
* get position-independent code (PIC).
115
*
116
* The PIC macro is defined by libtool, while __PIC__ is defined
117
* by GCC but only on some systems. Testing for both makes it simpler
118
* to test this code without libtool, and keeps the code working also
119
* when built with libtool but using something else than GCC.
120
*
121
* I understood that libtool may define PIC on Windows even though
122
* the code in Windows DLLs is not PIC in sense that it is in ELF
123
* binaries, so we need a separate check to always use the non-PIC
124
* code on Windows.
125
*/
126
#if (!defined(PIC) && !defined(__PIC__)) \
127
|| (defined(_WIN32) || defined(__CYGWIN__))
128
/* Not PIC */
129
movl $ LZMA_CRC32_TABLE, %ebx
130
#elif defined(__APPLE__)
131
/* Mach-O */
132
call .L_get_pc
133
.L_pic:
134
leal .L_lzma_crc32_table$non_lazy_ptr-.L_pic(%ebx), %ebx
135
movl (%ebx), %ebx
136
#else
137
/* ELF */
138
call .L_get_pc
139
addl $_GLOBAL_OFFSET_TABLE_, %ebx
140
movl LZMA_CRC32_TABLE@GOT(%ebx), %ebx
141
#endif
142
143
/* Complement the initial value. */
144
notl %eax
145
146
ALIGN(4, 16)
147
.L_align:
148
/*
149
* Check if there is enough input to use slicing-by-eight.
150
* We need 16 bytes, because the loop pre-reads eight bytes.
151
*/
152
cmpl $16, %edi
153
jb .L_rest
154
155
/* Check if we have reached alignment of eight bytes. */
156
testl $7, %esi
157
jz .L_slice
158
159
/* Calculate CRC of the next input byte. */
160
movzbl (%esi), %ebp
161
incl %esi
162
movzbl %al, %ecx
163
xorl %ecx, %ebp
164
shrl $8, %eax
165
xorl (%ebx, %ebp, 4), %eax
166
decl %edi
167
jmp .L_align
168
169
ALIGN(2, 4)
170
.L_slice:
171
/*
172
* If we get here, there's at least 16 bytes of aligned input
173
* available. Make %edi multiple of eight bytes. Store the possible
174
* remainder over the "size" variable in the argument stack.
175
*/
176
movl %edi, 0x18(%esp)
177
andl $-8, %edi
178
subl %edi, 0x18(%esp)
179
180
/*
181
* Let %edi be buf + size - 8 while running the main loop. This way
182
* we can compare for equality to determine when exit the loop.
183
*/
184
addl %esi, %edi
185
subl $8, %edi
186
187
/* Read in the first eight aligned bytes. */
188
xorl (%esi), %eax
189
movl 4(%esi), %ecx
190
movzbl %cl, %ebp
191
192
.L_loop:
193
movl 0x0C00(%ebx, %ebp, 4), %edx
194
movzbl %ch, %ebp
195
xorl 0x0800(%ebx, %ebp, 4), %edx
196
shrl $16, %ecx
197
xorl 8(%esi), %edx
198
movzbl %cl, %ebp
199
xorl 0x0400(%ebx, %ebp, 4), %edx
200
movzbl %ch, %ebp
201
xorl (%ebx, %ebp, 4), %edx
202
movzbl %al, %ebp
203
204
/*
205
* Read the next four bytes, for which the CRC is calculated
206
* on the next iteration of the loop.
207
*/
208
movl 12(%esi), %ecx
209
210
xorl 0x1C00(%ebx, %ebp, 4), %edx
211
movzbl %ah, %ebp
212
shrl $16, %eax
213
xorl 0x1800(%ebx, %ebp, 4), %edx
214
movzbl %ah, %ebp
215
movzbl %al, %eax
216
movl 0x1400(%ebx, %eax, 4), %eax
217
addl $8, %esi
218
xorl %edx, %eax
219
xorl 0x1000(%ebx, %ebp, 4), %eax
220
221
/* Check for end of aligned input. */
222
cmpl %edi, %esi
223
movzbl %cl, %ebp
224
jne .L_loop
225
226
/*
227
* Process the remaining eight bytes, which we have already
228
* copied to %ecx and %edx.
229
*/
230
movl 0x0C00(%ebx, %ebp, 4), %edx
231
movzbl %ch, %ebp
232
xorl 0x0800(%ebx, %ebp, 4), %edx
233
shrl $16, %ecx
234
movzbl %cl, %ebp
235
xorl 0x0400(%ebx, %ebp, 4), %edx
236
movzbl %ch, %ebp
237
xorl (%ebx, %ebp, 4), %edx
238
movzbl %al, %ebp
239
240
xorl 0x1C00(%ebx, %ebp, 4), %edx
241
movzbl %ah, %ebp
242
shrl $16, %eax
243
xorl 0x1800(%ebx, %ebp, 4), %edx
244
movzbl %ah, %ebp
245
movzbl %al, %eax
246
movl 0x1400(%ebx, %eax, 4), %eax
247
addl $8, %esi
248
xorl %edx, %eax
249
xorl 0x1000(%ebx, %ebp, 4), %eax
250
251
/* Copy the number of remaining bytes to %edi. */
252
movl 0x18(%esp), %edi
253
254
.L_rest:
255
/* Check for end of input. */
256
testl %edi, %edi
257
jz .L_return
258
259
/* Calculate CRC of the next input byte. */
260
movzbl (%esi), %ebp
261
incl %esi
262
movzbl %al, %ecx
263
xorl %ecx, %ebp
264
shrl $8, %eax
265
xorl (%ebx, %ebp, 4), %eax
266
decl %edi
267
jmp .L_rest
268
269
.L_return:
270
/* Complement the final value. */
271
notl %eax
272
273
popl %ebp
274
popl %edi
275
popl %esi
276
popl %ebx
277
ret
278
279
#if defined(PIC) || defined(__PIC__)
280
ALIGN(4, 16)
281
.L_get_pc:
282
movl (%esp), %ebx
283
ret
284
#endif
285
286
#if defined(__APPLE__) && (defined(PIC) || defined(__PIC__))
287
/* Mach-O PIC */
288
.section __IMPORT,__pointers,non_lazy_symbol_pointers
289
.L_lzma_crc32_table$non_lazy_ptr:
290
.indirect_symbol LZMA_CRC32_TABLE
291
.long 0
292
293
#elif defined(_WIN32) || defined(__CYGWIN__)
294
# ifdef DLL_EXPORT
295
/* This is equivalent of __declspec(dllexport). */
296
.section .drectve
297
.ascii " -export:lzma_crc32"
298
# endif
299
300
#elif !defined(__MSDOS__)
301
/* ELF */
302
.size LZMA_CRC32, .-LZMA_CRC32
303
#endif
304
305
/*
306
* This is needed to support non-executable stack. It's ugly to
307
* use __FreeBSD__ and __linux__ here, but I don't know a way to detect when
308
* we are using GNU assembler.
309
*/
310
#if defined(__ELF__) && (defined(__FreeBSD__) || defined(__linux__))
311
.section .note.GNU-stack,"",@progbits
312
#endif
313
314