1 /* crc_i386.c -- Microsoft 32-bit C/C++ adaptation of crc_i386.asm
2 * Created by Rodney Brown from crc_i386.asm, modified by Chr. Spieler.
3 * Last revised: 22-Mai-1998
5 * Original coded (in crc_i386.asm) and put into the public domain
6 * by Paul Kienitz and Christian Spieler.
8 * Revised 06-Oct-96, Scott Field (sfield@microsoft.com)
9 * fixed to assemble with masm by not using .model directive which makes
10 * assumptions about segment alignment. Also,
11 * avoid using loop, and j[e]cxz where possible. Use mov + inc, rather
12 * than lodsb, and other misc. changes resulting in the following performance
15 * unrolled loops NO_UNROLLED_LOOPS
18 * +54% +42% +35% +82% +52% +25%
20 * first item in each table is input buffer length, even multiple of 8
21 * second item in each table is input buffer length, > 8
22 * third item in each table is input buffer length, < 8
24 * Revised 02-Apr-97, Chr. Spieler, based on Rodney Brown (rdb@cmutual.com.au)
25 * Incorporated Rodney Brown's 32-bit-reads optimization as found in the
26 * UNIX AS source crc_i386.S. This new code can be disabled by defining
27 * the macro symbol NO_32_BIT_LOADS.
29 * Revised 12-Oct-97, Chr. Spieler, based on Rodney Brown (rdb@cmutual.com.au)
30 * Incorporated Rodney Brown's additional tweaks for 32-bit-optimized CPUs
31 * (like the Pentium Pro, Pentium II, and probably some Pentium clones).
32 * This optimization is controlled by the macro symbol __686 and is disabled
33 * by default. (This default is based on the assumption that most users
34 * do not yet work on a Pentium Pro or Pentium II machine ...)
36 * Revised 16-Nov-97, Chr. Spieler: Made code compatible with Borland C++
37 * 32-bit, removed unneeded kludge for potentially unknown movzx mnemonic,
38 * confirmed correct working with MS VC++ (32-bit).
40 * Revised 22-Mai-98, Peter Kunath, Chr. Spieler : The 16-Nov-97 revision broke
41 * MSVC 5.0. Inside preprocessor macros, each instruction is enclosed in its
42 * own __asm {...} construct. For MSVC, a "#pragma warning" was added to
43 * shut up the "no return value" warning message.
45 * FLAT memory model assumed.
47 * The loop unrolling can be disabled by defining the macro NO_UNROLLED_LOOPS.
48 * This results in shorter code at the expense of reduced performance.
60 /* Select wether the following inline-assember code is supported. */
61 #if (defined(_MSC_VER) && _MSC_VER >= 700)
62 #if (defined(_M_IX86) && _M_IX86 >= 300)
63 # define MSC_INLINE_ASM_32BIT_SUPPORT
64 /* Disable warning for no return value, typical of asm functions */
65 # pragma warning( disable : 4035 )
69 #if (defined(__BORLANDC__) && __BORLANDC__ >= 452)
70 # define MSC_INLINE_ASM_32BIT_SUPPORT
73 #ifdef MSC_INLINE_ASM_32BIT_SUPPORT
74 /* This code is intended for Microsoft C/C++ (32-bit) compatible compilers. */
77 * These two (three) macros make up the loop body of the CRC32 cruncher.
80 * esi : pointer to next data byte (or dword) "buf++"
82 * edi : pointer to base of crc_table array
84 * ebx : index into crc_table array
85 * (requires upper three bytes = 0 when __686 is undefined)
89 __asm { mov bl, al }; \
90 __asm { shr eax, 8 }; \
91 __asm { xor eax, [edi+ebx*4] }; }
94 __asm { movzx ebx, al }; \
95 __asm { shr eax, 8 }; \
96 __asm { xor eax, [edi+ebx*4] }; }
99 #define Do_CRC_byte { \
100 __asm { xor al, byte ptr [esi] }; \
104 #ifndef NO_32_BIT_LOADS
105 #define Do_CRC_dword { \
106 __asm { xor eax, dword ptr [esi] }; \
107 __asm { add esi, 4 }; \
112 #endif /* !NO_32_BIT_LOADS */
114 /* ========================================================================= */
115 ulg
crc32(crc
, buf
, len
)
116 ulg crc
; /* crc shift register */
117 ZCONST uch
*buf
; /* pointer to bytes to pump through */
118 extent len
; /* number of bytes in buf[] */
119 /* Run a set of bytes through the crc shift register. If buf is a NULL
120 pointer, then initialize the crc shift register contents instead.
121 Return the current crc in either case. */
127 mov esi
,buf
;/* 2nd arg: uch *buf */
128 sub eax
,eax
;/*> if (!buf) */
129 test esi
,esi
;/*> return 0; */
130 jz fine
;/*> else { */
134 mov eax
,crc
;/* 1st arg: ulg crc */
136 sub ebx
,ebx
;/* ebx=0; => bl usable as a dword */
138 mov ecx
,len
;/* 3rd arg: extent len */
139 not eax
;/*> c = ~crc; */
141 #ifndef NO_UNROLLED_LOOPS
142 # ifndef NO_32_BIT_LOADS
146 test esi
,3 ;/* align buf pointer on next */
147 jz aligned_now
;/* dword boundary */
154 # endif /* !NO_32_BIT_LOADS */
155 mov edx
,ecx
;/* save len in edx */
156 and edx
,000000007H
;/* edx = len % 8 */
157 shr ecx
,3 ;/* ecx = len / 8 */
159 ; align loop head at start of
486 internal cache line
!!
163 # ifndef NO_32_BIT_LOADS
166 # else /* NO_32_BIT_LOADS */
175 # endif /* ?NO_32_BIT_LOADS */
182 #endif /* NO_UNROLLED_LOOPS */
183 #ifndef NO_JECXZ_SUPPORT
184 jecxz bail
;/*> if (len) */
186 test ecx
,ecx
;/*> if (len) */
189 ; align loop head at start of
486 internal cache line
!!
193 Do_CRC_byte
;/* c = CRC32(c, *buf++); */
195 dec ecx
;/*> } while (--len); */
199 not eax
;/*> return ~c; */
208 #endif /* MSC_INLINE_ASM_32BIT_SUPPORT */
209 #if (defined(_MSC_VER) && _MSC_VER >= 700)
210 #if (defined(_M_IX86) && _M_IX86 >= 300)
211 /* Reenable missing return value warning */
212 # pragma warning( default : 4035 )
215 #endif /* !USE_ZLIB */