home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / zip22.zip / win32 / crc_i386.asm next >
Assembly Source File  |  1997-10-11  |  7KB  |  213 lines

  1. ; crc_i386.asm, optimized CRC calculation function for Zip and UnZip, not
  2. ; copyrighted by Paul Kienitz and Christian Spieler.  Last revised 12 Oct 97.
  3. ;
  4. ; Revised 06-Oct-96, Scott Field (sfield@microsoft.com)
  5. ;   fixed to assemble with masm by not using .model directive which makes
  6. ;   assumptions about segment alignment.  Also,
  7. ;   avoid using loop, and j[e]cxz where possible.  Use mov + inc, rather
  8. ;   than lodsb, and other misc. changes resulting in the following performance
  9. ;   increases:
  10. ;
  11. ;      unrolled loops                NO_UNROLLED_LOOPS
  12. ;      *8    >8      <8              *8      >8      <8
  13. ;
  14. ;      +54%  +42%    +35%            +82%    +52%    +25%
  15. ;
  16. ;   first item in each table is input buffer length, even multiple of 8
  17. ;   second item in each table is input buffer length, > 8
  18. ;   third item in each table is input buffer length, < 8
  19. ;
  20. ; Revised 02-Apr-97, Chr. Spieler, based on Rodney Brown (rdb@cmutual.com.au)
  21. ;   Incorporated Rodney Brown's 32-bit-reads optimization as found in the
  22. ;   UNIX AS source crc_i386.S. This new code can be disabled by defining
  23. ;   the macro symbol NO_32_BIT_LOADS.
  24. ;
  25. ; Revised 12-Oct-97, Chr. Spieler, based on Rodney Brown (rdb@cmutual.com.au)
  26. ;   Incorporated Rodney Brown's additional tweaks for 32-bit-optimized CPUs
  27. ;   (like the Pentium Pro, Pentium II, and probably some Pentium clones).
  28. ;   This optimization is controlled by the macro symbol __686 and is disabled
  29. ;   by default. (This default is based on the assumption that most users
  30. ;   do not yet work on a Pentium Pro or Pentium II machine ...)
  31. ;
  32. ; FLAT memory model assumed.
  33. ;
  34. ; The loop unrolling can be disabled by defining the macro NO_UNROLLED_LOOPS.
  35. ; This results in shorter code at the expense of reduced performance.
  36. ;
  37. ;==============================================================================
  38. ;
  39. ; Do NOT assemble this source if external crc32 routine from zlib gets used.
  40. ;
  41.     IFNDEF USE_ZLIB
  42. ;
  43.         .386p
  44.         name    crc_i386
  45. ; don't make assumptions about segment align        .model flat
  46.  
  47. extrn   _get_crc_table:near    ; ulg near *get_crc_table(void);
  48.  
  49. ;
  50.     IFNDEF NO_STD_STACKFRAME
  51.         ; Use a `standard' stack frame setup on routine entry and exit.
  52.         ; Actually, this option is set as default, because it results
  53.         ; in smaller code !!
  54. STD_ENTRY       MACRO
  55.                 push    ebp
  56.                 mov     ebp,esp
  57.         ENDM
  58.  
  59.         Arg1    EQU     08H[ebp]
  60.         Arg2    EQU     0CH[ebp]
  61.         Arg3    EQU     10H[ebp]
  62.  
  63. STD_LEAVE       MACRO
  64.                 pop     ebp
  65.         ENDM
  66.  
  67.     ELSE  ; NO_STD_STACKFRAME
  68.  
  69. STD_ENTRY       MACRO
  70.         ENDM
  71.  
  72.         Arg1    EQU     18H[esp]
  73.         Arg2    EQU     1CH[esp]
  74.         Arg3    EQU     20H[esp]
  75.  
  76. STD_LEAVE       MACRO
  77.         ENDM
  78.  
  79.     ENDIF ; ?NO_STD_STACKFRAME
  80.  
  81. ; These two (three) macros make up the loop body of the CRC32 cruncher.
  82. ; registers modified:
  83. ;   eax  : crc value "c"
  84. ;   esi  : pointer to next data byte (or dword) "buf++"
  85. ; registers read:
  86. ;   edi  : pointer to base of crc_table array
  87. ; scratch registers:
  88. ;   ebx  : index into crc_table array
  89. ;          (requires upper three bytes = 0 when __686 is undefined)
  90.     IFNDEF  __686 ; optimize for 386, 486, Pentium
  91. Do_CRC  MACRO
  92.                 mov     bl,al                ; tmp = c & 0xFF
  93.                 shr     eax,8                ; c = (c >> 8)
  94.                 xor     eax,[edi+ebx*4]      ;  ^ table[tmp]
  95.         ENDM
  96.     ELSE ; __686 : optimize for Pentium Pro, Pentium II and compatible CPUs
  97. Do_CRC  MACRO
  98.                 movzx   ebx,al               ; tmp = c & 0xFF
  99.                 shr     eax,8                ; c = (c >> 8)
  100.                 xor     eax,[edi+ebx*4]      ;  ^ table[tmp]
  101.         ENDM
  102.     ENDIF ; ?__686
  103. Do_CRC_byte     MACRO
  104.                 xor     al, byte ptr [esi]   ; c ^= *buf
  105.                 inc     esi                  ; buf++
  106.                 Do_CRC                       ; c = (c >> 8) ^ table[c & 0xFF]
  107.         ENDM
  108.     IFNDEF  NO_32_BIT_LOADS
  109. Do_CRC_dword    MACRO
  110.                 xor     eax, dword ptr [esi] ; c ^= *(ulg *)buf
  111.                 add     esi, 4               ; ((ulg *)buf)++
  112.                 Do_CRC
  113.                 Do_CRC
  114.                 Do_CRC
  115.                 Do_CRC
  116.         ENDM
  117.     ENDIF ; !NO_32_BIT_LOADS
  118.  
  119. _TEXT   segment para
  120.         assume  CS: _TEXT
  121.  
  122.         public  _crc32
  123. _crc32          proc    near  ; ulg crc32(ulg crc, ZCONST uch *buf, extent len)
  124.                 STD_ENTRY
  125.                 push    edi
  126.                 push    esi
  127.                 push    ebx
  128.                 push    edx
  129.                 push    ecx
  130.  
  131.                 mov     esi,Arg2             ; 2nd arg: uch *buf
  132.                 sub     eax,eax              ;> if (!buf)
  133.                 test    esi,esi              ;>   return 0;
  134.                 jz      fine                 ;> else {
  135.  
  136.                 call    _get_crc_table
  137.                 mov     edi,eax
  138.                 mov     eax,Arg1             ; 1st arg: ulg crc
  139.     IFNDEF __686
  140.                 sub     ebx,ebx              ; ebx=0; make bl usable as a dword
  141.     ENDIF
  142.                 mov     ecx,Arg3             ; 3rd arg: extent len
  143.                 not     eax                  ;>   c = ~crc;
  144.  
  145.     IFNDEF  NO_UNROLLED_LOOPS
  146.     IFNDEF  NO_32_BIT_LOADS
  147.                 test    ecx,ecx
  148.                 je      bail
  149. align_loop:
  150.                 test    esi,3                ; align buf pointer on next
  151.                 jz      SHORT aligned_now    ;  dword boundary
  152.                 Do_CRC_byte
  153.                 dec     ecx
  154.                 jnz     align_loop
  155. aligned_now:
  156.     ENDIF ; !NO_32_BIT_LOADS
  157.                 mov     edx,ecx              ; save len in edx
  158.                 and     edx,000000007H       ; edx = len % 8
  159.                 shr     ecx,3                ; ecx = len / 8
  160.                 jz      SHORT No_Eights
  161. ; align loop head at start of 486 internal cache line !!
  162.                 align   16
  163. Next_Eight:
  164.     IFNDEF  NO_32_BIT_LOADS
  165.                 Do_CRC_dword
  166.                 Do_CRC_dword
  167.     ELSE ; NO_32_BIT_LOADS
  168.                 Do_CRC_byte
  169.                 Do_CRC_byte
  170.                 Do_CRC_byte
  171.                 Do_CRC_byte
  172.                 Do_CRC_byte
  173.                 Do_CRC_byte
  174.                 Do_CRC_byte
  175.                 Do_CRC_byte
  176.     ENDIF ; ?NO_32_BIT_LOADS
  177.                 dec     ecx
  178.                 jnz     Next_Eight
  179. No_Eights:
  180.                 mov     ecx,edx
  181.  
  182.     ENDIF ; NO_UNROLLED_LOOPS
  183.     IFNDEF  NO_JECXZ_SUPPORT
  184.                 jecxz   bail                 ;>   if (len)
  185.     ELSE
  186.                 test    ecx,ecx              ;>   if (len)
  187.                 jz      SHORT bail
  188.     ENDIF
  189. ; align loop head at start of 486 internal cache line !!
  190.                 align   16
  191. loupe:                                       ;>     do {
  192.                 Do_CRC_byte                  ;        c = CRC32(c, *buf++);
  193.                 dec     ecx                  ;>     } while (--len);
  194.                 jnz     loupe
  195.  
  196. bail:                                        ;> }
  197.                 not     eax                  ;> return ~c;
  198. fine:
  199.                 pop     ecx
  200.                 pop     edx
  201.                 pop     ebx
  202.                 pop     esi
  203.                 pop     edi
  204.                 STD_LEAVE
  205.                 ret
  206. _crc32          endp
  207.  
  208. _TEXT   ends
  209. ;
  210.     ENDIF ; !USE_ZLIB
  211. ;
  212. end
  213.