Path: blob/master/Utilities/cmliblzma/liblzma/check/crc32_x86.S
3153 views
/* SPDX-License-Identifier: 0BSD */12/*3* Speed-optimized CRC32 using slicing-by-eight algorithm4*5* This uses only i386 instructions, but it is optimized for i686 and later6* (including e.g. Pentium II/III/IV, Athlon XP, and Core 2). For i5867* (e.g. Pentium), slicing-by-four would be better, and even the C version8* of slicing-by-eight built with gcc -march=i586 tends to be a little bit9* better than this. Very few probably run this code on i586 or older x8610* so this shouldn't be a problem in practice.11*12* Authors: Igor Pavlov (original version)13* Lasse Collin (AT&T syntax, PIC support, better portability)14*15* This code needs lzma_crc32_table, which can be created using the16* following C code:1718uint32_t lzma_crc32_table[8][256];1920void21init_table(void)22{23// IEEE-802.324static const uint32_t poly32 = UINT32_C(0xEDB88320);2526// Castagnoli27// static const uint32_t poly32 = UINT32_C(0x82F63B78);2829// Koopman30// static const uint32_t poly32 = UINT32_C(0xEB31D82E);3132for (size_t s = 0; s < 8; ++s) {33for (size_t b = 0; b < 256; ++b) {34uint32_t r = s == 0 ? b : lzma_crc32_table[s - 1][b];3536for (size_t i = 0; i < 8; ++i) {37if (r & 1)38r = (r >> 1) ^ poly32;39else40r >>= 1;41}4243lzma_crc32_table[s][b] = r;44}45}46}4748* The prototype of the CRC32 function:49* extern uint32_t lzma_crc32(const uint8_t *buf, size_t size, uint32_t crc);50*/5152/* When Intel CET is enabled, include <cet.h> in assembly code to mark53Intel CET support. */54#ifdef __CET__55# include <cet.h>56#else57# define _CET_ENDBR58#endif5960/*61* On some systems, the functions need to be prefixed. The prefix is62* usually an underscore.63*/64#ifndef __USER_LABEL_PREFIX__65# define __USER_LABEL_PREFIX__66#endif67#define MAKE_SYM_CAT(prefix, sym) prefix ## sym68#define MAKE_SYM(prefix, sym) MAKE_SYM_CAT(prefix, sym)69#define LZMA_CRC32 MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32)70#define LZMA_CRC32_TABLE MAKE_SYM(__USER_LABEL_PREFIX__, lzma_crc32_table)7172/*73* Solaris assembler doesn't have .p2align, and Darwin uses .align74* differently than GNU/Linux and Solaris.75*/76#if defined(__APPLE__) || defined(__MSDOS__)77# define ALIGN(pow2, abs) .align pow278#else79# define ALIGN(pow2, abs) .align abs80#endif8182.text83.globl LZMA_CRC328485#if !defined(__APPLE__) && !defined(_WIN32) && !defined(__CYGWIN__) \86&& !defined(__MSDOS__)87.type LZMA_CRC32, @function88#endif8990ALIGN(4, 16)91LZMA_CRC32:92_CET_ENDBR93/*94* Register usage:95* %eax crc96* %esi buf97* %edi size or buf + size98* %ebx lzma_crc32_table99* %ebp Table index100* %ecx Temporary101* %edx Temporary102*/103pushl %ebx104pushl %esi105pushl %edi106pushl %ebp107movl 0x14(%esp), %esi /* buf */108movl 0x18(%esp), %edi /* size */109movl 0x1C(%esp), %eax /* crc */110111/*112* Store the address of lzma_crc32_table to %ebx. This is needed to113* get position-independent code (PIC).114*115* The PIC macro is defined by libtool, while __PIC__ is defined116* by GCC but only on some systems. Testing for both makes it simpler117* to test this code without libtool, and keeps the code working also118* when built with libtool but using something else than GCC.119*120* I understood that libtool may define PIC on Windows even though121* the code in Windows DLLs is not PIC in sense that it is in ELF122* binaries, so we need a separate check to always use the non-PIC123* code on Windows.124*/125#if (!defined(PIC) && !defined(__PIC__)) \126|| (defined(_WIN32) || defined(__CYGWIN__))127/* Not PIC */128movl $ LZMA_CRC32_TABLE, %ebx129#elif defined(__APPLE__)130/* Mach-O */131call .L_get_pc132.L_pic:133leal .L_lzma_crc32_table$non_lazy_ptr-.L_pic(%ebx), %ebx134movl (%ebx), %ebx135#else136/* ELF */137call .L_get_pc138addl $_GLOBAL_OFFSET_TABLE_, %ebx139movl LZMA_CRC32_TABLE@GOT(%ebx), %ebx140#endif141142/* Complement the initial value. */143notl %eax144145ALIGN(4, 16)146.L_align:147/*148* Check if there is enough input to use slicing-by-eight.149* We need 16 bytes, because the loop pre-reads eight bytes.150*/151cmpl $16, %edi152jb .L_rest153154/* Check if we have reached alignment of eight bytes. */155testl $7, %esi156jz .L_slice157158/* Calculate CRC of the next input byte. */159movzbl (%esi), %ebp160incl %esi161movzbl %al, %ecx162xorl %ecx, %ebp163shrl $8, %eax164xorl (%ebx, %ebp, 4), %eax165decl %edi166jmp .L_align167168ALIGN(2, 4)169.L_slice:170/*171* If we get here, there's at least 16 bytes of aligned input172* available. Make %edi multiple of eight bytes. Store the possible173* remainder over the "size" variable in the argument stack.174*/175movl %edi, 0x18(%esp)176andl $-8, %edi177subl %edi, 0x18(%esp)178179/*180* Let %edi be buf + size - 8 while running the main loop. This way181* we can compare for equality to determine when exit the loop.182*/183addl %esi, %edi184subl $8, %edi185186/* Read in the first eight aligned bytes. */187xorl (%esi), %eax188movl 4(%esi), %ecx189movzbl %cl, %ebp190191.L_loop:192movl 0x0C00(%ebx, %ebp, 4), %edx193movzbl %ch, %ebp194xorl 0x0800(%ebx, %ebp, 4), %edx195shrl $16, %ecx196xorl 8(%esi), %edx197movzbl %cl, %ebp198xorl 0x0400(%ebx, %ebp, 4), %edx199movzbl %ch, %ebp200xorl (%ebx, %ebp, 4), %edx201movzbl %al, %ebp202203/*204* Read the next four bytes, for which the CRC is calculated205* on the next iteration of the loop.206*/207movl 12(%esi), %ecx208209xorl 0x1C00(%ebx, %ebp, 4), %edx210movzbl %ah, %ebp211shrl $16, %eax212xorl 0x1800(%ebx, %ebp, 4), %edx213movzbl %ah, %ebp214movzbl %al, %eax215movl 0x1400(%ebx, %eax, 4), %eax216addl $8, %esi217xorl %edx, %eax218xorl 0x1000(%ebx, %ebp, 4), %eax219220/* Check for end of aligned input. */221cmpl %edi, %esi222movzbl %cl, %ebp223jne .L_loop224225/*226* Process the remaining eight bytes, which we have already227* copied to %ecx and %edx.228*/229movl 0x0C00(%ebx, %ebp, 4), %edx230movzbl %ch, %ebp231xorl 0x0800(%ebx, %ebp, 4), %edx232shrl $16, %ecx233movzbl %cl, %ebp234xorl 0x0400(%ebx, %ebp, 4), %edx235movzbl %ch, %ebp236xorl (%ebx, %ebp, 4), %edx237movzbl %al, %ebp238239xorl 0x1C00(%ebx, %ebp, 4), %edx240movzbl %ah, %ebp241shrl $16, %eax242xorl 0x1800(%ebx, %ebp, 4), %edx243movzbl %ah, %ebp244movzbl %al, %eax245movl 0x1400(%ebx, %eax, 4), %eax246addl $8, %esi247xorl %edx, %eax248xorl 0x1000(%ebx, %ebp, 4), %eax249250/* Copy the number of remaining bytes to %edi. */251movl 0x18(%esp), %edi252253.L_rest:254/* Check for end of input. */255testl %edi, %edi256jz .L_return257258/* Calculate CRC of the next input byte. */259movzbl (%esi), %ebp260incl %esi261movzbl %al, %ecx262xorl %ecx, %ebp263shrl $8, %eax264xorl (%ebx, %ebp, 4), %eax265decl %edi266jmp .L_rest267268.L_return:269/* Complement the final value. */270notl %eax271272popl %ebp273popl %edi274popl %esi275popl %ebx276ret277278#if defined(PIC) || defined(__PIC__)279ALIGN(4, 16)280.L_get_pc:281movl (%esp), %ebx282ret283#endif284285#if defined(__APPLE__) && (defined(PIC) || defined(__PIC__))286/* Mach-O PIC */287.section __IMPORT,__pointers,non_lazy_symbol_pointers288.L_lzma_crc32_table$non_lazy_ptr:289.indirect_symbol LZMA_CRC32_TABLE290.long 0291292#elif defined(_WIN32) || defined(__CYGWIN__)293# ifdef DLL_EXPORT294/* This is equivalent of __declspec(dllexport). */295.section .drectve296.ascii " -export:lzma_crc32"297# endif298299#elif !defined(__MSDOS__)300/* ELF */301.size LZMA_CRC32, .-LZMA_CRC32302#endif303304/*305* This is needed to support non-executable stack. It's ugly to306* use __FreeBSD__ and __linux__ here, but I don't know a way to detect when307* we are using GNU assembler.308*/309#if defined(__ELF__) && (defined(__FreeBSD__) || defined(__linux__))310.section .note.GNU-stack,"",@progbits311#endif312313314