xref: /minix3/common/dist/zlib/contrib/masmx64/inffas8664.c (revision 44bedb31d842b4b0444105519bcf929a69fe2dc1)
1*44bedb31SLionel Sambuc /*	$NetBSD: inffas8664.c,v 1.1.1.1 2006/01/14 20:10:55 christos Exp $	*/
2*44bedb31SLionel Sambuc 
3*44bedb31SLionel Sambuc /* inffas8664.c is a hand tuned assembler version of inffast.c - fast decoding
4*44bedb31SLionel Sambuc  * version for AMD64 on Windows using Microsoft C compiler
5*44bedb31SLionel Sambuc  *
6*44bedb31SLionel Sambuc  * Copyright (C) 1995-2003 Mark Adler
7*44bedb31SLionel Sambuc  * For conditions of distribution and use, see copyright notice in zlib.h
8*44bedb31SLionel Sambuc  *
9*44bedb31SLionel Sambuc  * Copyright (C) 2003 Chris Anderson <christop@charm.net>
10*44bedb31SLionel Sambuc  * Please use the copyright conditions above.
11*44bedb31SLionel Sambuc  *
12*44bedb31SLionel Sambuc  * 2005 - Adaptation to Microsoft C Compiler for AMD64 by Gilles Vollant
13*44bedb31SLionel Sambuc  *
14*44bedb31SLionel Sambuc  * inffas8664.c call function inffas8664fnc in inffasx64.asm
15*44bedb31SLionel Sambuc  *  inffasx64.asm is automatically convert from AMD64 portion of inffas86.c
16*44bedb31SLionel Sambuc  *
17*44bedb31SLionel Sambuc  * Dec-29-2003 -- I added AMD64 inflate asm support.  This version is also
18*44bedb31SLionel Sambuc  * slightly quicker on x86 systems because, instead of using rep movsb to copy
19*44bedb31SLionel Sambuc  * data, it uses rep movsw, which moves data in 2-byte chunks instead of single
20*44bedb31SLionel Sambuc  * bytes.  I've tested the AMD64 code on a Fedora Core 1 + the x86_64 updates
21*44bedb31SLionel Sambuc  * from http://fedora.linux.duke.edu/fc1_x86_64
22*44bedb31SLionel Sambuc  * which is running on an Athlon 64 3000+ / Gigabyte GA-K8VT800M system with
23*44bedb31SLionel Sambuc  * 1GB ram.  The 64-bit version is about 4% faster than the 32-bit version,
24*44bedb31SLionel Sambuc  * when decompressing mozilla-source-1.3.tar.gz.
25*44bedb31SLionel Sambuc  *
26*44bedb31SLionel Sambuc  * Mar-13-2003 -- Most of this is derived from inffast.S which is derived from
27*44bedb31SLionel Sambuc  * the gcc -S output of zlib-1.2.0/inffast.c.  Zlib-1.2.0 is in beta release at
28*44bedb31SLionel Sambuc  * the moment.  I have successfully compiled and tested this code with gcc2.96,
29*44bedb31SLionel Sambuc  * gcc3.2, icc5.0, msvc6.0.  It is very close to the speed of inffast.S
30*44bedb31SLionel Sambuc  * compiled with gcc -DNO_MMX, but inffast.S is still faster on the P3 with MMX
31*44bedb31SLionel Sambuc  * enabled.  I will attempt to merge the MMX code into this version.  Newer
32*44bedb31SLionel Sambuc  * versions of this and inffast.S can be found at
33*44bedb31SLionel Sambuc  * http://www.eetbeetee.com/zlib/ and http://www.charm.net/~christop/zlib/
34*44bedb31SLionel Sambuc  *
35*44bedb31SLionel Sambuc  */
36*44bedb31SLionel Sambuc 
37*44bedb31SLionel Sambuc #include <stdio.h>
38*44bedb31SLionel Sambuc #include "zutil.h"
39*44bedb31SLionel Sambuc #include "inftrees.h"
40*44bedb31SLionel Sambuc #include "inflate.h"
41*44bedb31SLionel Sambuc #include "inffast.h"
42*44bedb31SLionel Sambuc 
43*44bedb31SLionel Sambuc /* Mark Adler's comments from inffast.c: */
44*44bedb31SLionel Sambuc 
45*44bedb31SLionel Sambuc /*
46*44bedb31SLionel Sambuc    Decode literal, length, and distance codes and write out the resulting
47*44bedb31SLionel Sambuc    literal and match bytes until either not enough input or output is
48*44bedb31SLionel Sambuc    available, an end-of-block is encountered, or a data error is encountered.
49*44bedb31SLionel Sambuc    When large enough input and output buffers are supplied to inflate(), for
50*44bedb31SLionel Sambuc    example, a 16K input buffer and a 64K output buffer, more than 95% of the
51*44bedb31SLionel Sambuc    inflate execution time is spent in this routine.
52*44bedb31SLionel Sambuc 
53*44bedb31SLionel Sambuc    Entry assumptions:
54*44bedb31SLionel Sambuc 
55*44bedb31SLionel Sambuc         state->mode == LEN
56*44bedb31SLionel Sambuc         strm->avail_in >= 6
57*44bedb31SLionel Sambuc         strm->avail_out >= 258
58*44bedb31SLionel Sambuc         start >= strm->avail_out
59*44bedb31SLionel Sambuc         state->bits < 8
60*44bedb31SLionel Sambuc 
61*44bedb31SLionel Sambuc    On return, state->mode is one of:
62*44bedb31SLionel Sambuc 
63*44bedb31SLionel Sambuc         LEN -- ran out of enough output space or enough available input
64*44bedb31SLionel Sambuc         TYPE -- reached end of block code, inflate() to interpret next block
65*44bedb31SLionel Sambuc         BAD -- error in block data
66*44bedb31SLionel Sambuc 
67*44bedb31SLionel Sambuc    Notes:
68*44bedb31SLionel Sambuc 
69*44bedb31SLionel Sambuc     - The maximum input bits used by a length/distance pair is 15 bits for the
70*44bedb31SLionel Sambuc       length code, 5 bits for the length extra, 15 bits for the distance code,
71*44bedb31SLionel Sambuc       and 13 bits for the distance extra.  This totals 48 bits, or six bytes.
72*44bedb31SLionel Sambuc       Therefore if strm->avail_in >= 6, then there is enough input to avoid
73*44bedb31SLionel Sambuc       checking for available input while decoding.
74*44bedb31SLionel Sambuc 
75*44bedb31SLionel Sambuc     - The maximum bytes that a single length/distance pair can output is 258
76*44bedb31SLionel Sambuc       bytes, which is the maximum length that can be coded.  inflate_fast()
77*44bedb31SLionel Sambuc       requires strm->avail_out >= 258 for each loop to avoid checking for
78*44bedb31SLionel Sambuc       output space.
79*44bedb31SLionel Sambuc  */
80*44bedb31SLionel Sambuc 
81*44bedb31SLionel Sambuc 
82*44bedb31SLionel Sambuc 
83*44bedb31SLionel Sambuc     typedef struct inffast_ar {
84*44bedb31SLionel Sambuc /* 64   32                               x86  x86_64 */
85*44bedb31SLionel Sambuc /* ar offset                              register */
86*44bedb31SLionel Sambuc /*  0    0 */ void *esp;                /* esp save */
87*44bedb31SLionel Sambuc /*  8    4 */ void *ebp;                /* ebp save */
88*44bedb31SLionel Sambuc /* 16    8 */ unsigned char FAR *in;    /* esi rsi  local strm->next_in */
89*44bedb31SLionel Sambuc /* 24   12 */ unsigned char FAR *last;  /*     r9   while in < last */
90*44bedb31SLionel Sambuc /* 32   16 */ unsigned char FAR *out;   /* edi rdi  local strm->next_out */
91*44bedb31SLionel Sambuc /* 40   20 */ unsigned char FAR *beg;   /*          inflate()'s init next_out */
92*44bedb31SLionel Sambuc /* 48   24 */ unsigned char FAR *end;   /*     r10  while out < end */
93*44bedb31SLionel Sambuc /* 56   28 */ unsigned char FAR *window;/*          size of window, wsize!=0 */
94*44bedb31SLionel Sambuc /* 64   32 */ code const FAR *lcode;    /* ebp rbp  local strm->lencode */
95*44bedb31SLionel Sambuc /* 72   36 */ code const FAR *dcode;    /*     r11  local strm->distcode */
96*44bedb31SLionel Sambuc /* 80   40 */ size_t /*unsigned long */hold;       /* edx rdx  local strm->hold */
97*44bedb31SLionel Sambuc /* 88   44 */ unsigned bits;            /* ebx rbx  local strm->bits */
98*44bedb31SLionel Sambuc /* 92   48 */ unsigned wsize;           /*          window size */
99*44bedb31SLionel Sambuc /* 96   52 */ unsigned write;           /*          window write index */
100*44bedb31SLionel Sambuc /*100   56 */ unsigned lmask;           /*     r12  mask for lcode */
101*44bedb31SLionel Sambuc /*104   60 */ unsigned dmask;           /*     r13  mask for dcode */
102*44bedb31SLionel Sambuc /*108   64 */ unsigned len;             /*     r14  match length */
103*44bedb31SLionel Sambuc /*112   68 */ unsigned dist;            /*     r15  match distance */
104*44bedb31SLionel Sambuc /*116   72 */ unsigned status;          /*          set when state chng*/
105*44bedb31SLionel Sambuc     } type_ar;
106*44bedb31SLionel Sambuc #ifdef ASMINF
107*44bedb31SLionel Sambuc 
inflate_fast(strm,start)108*44bedb31SLionel Sambuc void inflate_fast(strm, start)
109*44bedb31SLionel Sambuc z_streamp strm;
110*44bedb31SLionel Sambuc unsigned start;         /* inflate()'s starting value for strm->avail_out */
111*44bedb31SLionel Sambuc {
112*44bedb31SLionel Sambuc     struct inflate_state FAR *state;
113*44bedb31SLionel Sambuc     type_ar ar;
114*44bedb31SLionel Sambuc     void inffas8664fnc(struct inffast_ar * par);
115*44bedb31SLionel Sambuc 
116*44bedb31SLionel Sambuc 
117*44bedb31SLionel Sambuc 
118*44bedb31SLionel Sambuc #if (defined( __GNUC__ ) && defined( __amd64__ ) && ! defined( __i386 )) || (defined(_MSC_VER) && defined(_M_AMD64))
119*44bedb31SLionel Sambuc #define PAD_AVAIL_IN 6
120*44bedb31SLionel Sambuc #define PAD_AVAIL_OUT 258
121*44bedb31SLionel Sambuc #else
122*44bedb31SLionel Sambuc #define PAD_AVAIL_IN 5
123*44bedb31SLionel Sambuc #define PAD_AVAIL_OUT 257
124*44bedb31SLionel Sambuc #endif
125*44bedb31SLionel Sambuc 
126*44bedb31SLionel Sambuc     /* copy state to local variables */
127*44bedb31SLionel Sambuc     state = (struct inflate_state FAR *)strm->state;
128*44bedb31SLionel Sambuc 
129*44bedb31SLionel Sambuc     ar.in = strm->next_in;
130*44bedb31SLionel Sambuc     ar.last = ar.in + (strm->avail_in - PAD_AVAIL_IN);
131*44bedb31SLionel Sambuc     ar.out = strm->next_out;
132*44bedb31SLionel Sambuc     ar.beg = ar.out - (start - strm->avail_out);
133*44bedb31SLionel Sambuc     ar.end = ar.out + (strm->avail_out - PAD_AVAIL_OUT);
134*44bedb31SLionel Sambuc     ar.wsize = state->wsize;
135*44bedb31SLionel Sambuc     ar.write = state->write;
136*44bedb31SLionel Sambuc     ar.window = state->window;
137*44bedb31SLionel Sambuc     ar.hold = state->hold;
138*44bedb31SLionel Sambuc     ar.bits = state->bits;
139*44bedb31SLionel Sambuc     ar.lcode = state->lencode;
140*44bedb31SLionel Sambuc     ar.dcode = state->distcode;
141*44bedb31SLionel Sambuc     ar.lmask = (1U << state->lenbits) - 1;
142*44bedb31SLionel Sambuc     ar.dmask = (1U << state->distbits) - 1;
143*44bedb31SLionel Sambuc 
144*44bedb31SLionel Sambuc     /* decode literals and length/distances until end-of-block or not enough
145*44bedb31SLionel Sambuc        input data or output space */
146*44bedb31SLionel Sambuc 
147*44bedb31SLionel Sambuc     /* align in on 1/2 hold size boundary */
148*44bedb31SLionel Sambuc     while (((size_t)(void *)ar.in & (sizeof(ar.hold) / 2 - 1)) != 0) {
149*44bedb31SLionel Sambuc         ar.hold += (unsigned long)*ar.in++ << ar.bits;
150*44bedb31SLionel Sambuc         ar.bits += 8;
151*44bedb31SLionel Sambuc     }
152*44bedb31SLionel Sambuc 
153*44bedb31SLionel Sambuc     inffas8664fnc(&ar);
154*44bedb31SLionel Sambuc 
155*44bedb31SLionel Sambuc     if (ar.status > 1) {
156*44bedb31SLionel Sambuc         if (ar.status == 2)
157*44bedb31SLionel Sambuc             strm->msg = "invalid literal/length code";
158*44bedb31SLionel Sambuc         else if (ar.status == 3)
159*44bedb31SLionel Sambuc             strm->msg = "invalid distance code";
160*44bedb31SLionel Sambuc         else
161*44bedb31SLionel Sambuc             strm->msg = "invalid distance too far back";
162*44bedb31SLionel Sambuc         state->mode = BAD;
163*44bedb31SLionel Sambuc     }
164*44bedb31SLionel Sambuc     else if ( ar.status == 1 ) {
165*44bedb31SLionel Sambuc         state->mode = TYPE;
166*44bedb31SLionel Sambuc     }
167*44bedb31SLionel Sambuc 
168*44bedb31SLionel Sambuc     /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
169*44bedb31SLionel Sambuc     ar.len = ar.bits >> 3;
170*44bedb31SLionel Sambuc     ar.in -= ar.len;
171*44bedb31SLionel Sambuc     ar.bits -= ar.len << 3;
172*44bedb31SLionel Sambuc     ar.hold &= (1U << ar.bits) - 1;
173*44bedb31SLionel Sambuc 
174*44bedb31SLionel Sambuc     /* update state and return */
175*44bedb31SLionel Sambuc     strm->next_in = ar.in;
176*44bedb31SLionel Sambuc     strm->next_out = ar.out;
177*44bedb31SLionel Sambuc     strm->avail_in = (unsigned)(ar.in < ar.last ?
178*44bedb31SLionel Sambuc                                 PAD_AVAIL_IN + (ar.last - ar.in) :
179*44bedb31SLionel Sambuc                                 PAD_AVAIL_IN - (ar.in - ar.last));
180*44bedb31SLionel Sambuc     strm->avail_out = (unsigned)(ar.out < ar.end ?
181*44bedb31SLionel Sambuc                                  PAD_AVAIL_OUT + (ar.end - ar.out) :
182*44bedb31SLionel Sambuc                                  PAD_AVAIL_OUT - (ar.out - ar.end));
183*44bedb31SLionel Sambuc     state->hold = (unsigned long)ar.hold;
184*44bedb31SLionel Sambuc     state->bits = ar.bits;
185*44bedb31SLionel Sambuc     return;
186*44bedb31SLionel Sambuc }
187*44bedb31SLionel Sambuc 
188*44bedb31SLionel Sambuc #endif
189