istereo

annotate libs/zlib/inffast.c @ 28:c0ae8e668447

added vmath library
author John Tsiombikas <nuclear@mutantstargoat.com>
date Thu, 08 Sep 2011 08:30:42 +0300
parents
children
rev   line source
nuclear@26 1 /* inffast.c -- fast decoding
nuclear@26 2 * Copyright (C) 1995-2004 Mark Adler
nuclear@26 3 * For conditions of distribution and use, see copyright notice in zlib.h
nuclear@26 4 */
nuclear@26 5
nuclear@26 6 #include "zutil.h"
nuclear@26 7 #include "inftrees.h"
nuclear@26 8 #include "inflate.h"
nuclear@26 9 #include "inffast.h"
nuclear@26 10
nuclear@26 11 #ifndef ASMINF
nuclear@26 12
nuclear@26 13 /* Allow machine dependent optimization for post-increment or pre-increment.
nuclear@26 14 Based on testing to date,
nuclear@26 15 Pre-increment preferred for:
nuclear@26 16 - PowerPC G3 (Adler)
nuclear@26 17 - MIPS R5000 (Randers-Pehrson)
nuclear@26 18 Post-increment preferred for:
nuclear@26 19 - none
nuclear@26 20 No measurable difference:
nuclear@26 21 - Pentium III (Anderson)
nuclear@26 22 - M68060 (Nikl)
nuclear@26 23 */
nuclear@26 24 #ifdef POSTINC
nuclear@26 25 # define OFF 0
nuclear@26 26 # define PUP(a) *(a)++
nuclear@26 27 #else
nuclear@26 28 # define OFF 1
nuclear@26 29 # define PUP(a) *++(a)
nuclear@26 30 #endif
nuclear@26 31
nuclear@26 32 /*
nuclear@26 33 Decode literal, length, and distance codes and write out the resulting
nuclear@26 34 literal and match bytes until either not enough input or output is
nuclear@26 35 available, an end-of-block is encountered, or a data error is encountered.
nuclear@26 36 When large enough input and output buffers are supplied to inflate(), for
nuclear@26 37 example, a 16K input buffer and a 64K output buffer, more than 95% of the
nuclear@26 38 inflate execution time is spent in this routine.
nuclear@26 39
nuclear@26 40 Entry assumptions:
nuclear@26 41
nuclear@26 42 state->mode == LEN
nuclear@26 43 strm->avail_in >= 6
nuclear@26 44 strm->avail_out >= 258
nuclear@26 45 start >= strm->avail_out
nuclear@26 46 state->bits < 8
nuclear@26 47
nuclear@26 48 On return, state->mode is one of:
nuclear@26 49
nuclear@26 50 LEN -- ran out of enough output space or enough available input
nuclear@26 51 TYPE -- reached end of block code, inflate() to interpret next block
nuclear@26 52 BAD -- error in block data
nuclear@26 53
nuclear@26 54 Notes:
nuclear@26 55
nuclear@26 56 - The maximum input bits used by a length/distance pair is 15 bits for the
nuclear@26 57 length code, 5 bits for the length extra, 15 bits for the distance code,
nuclear@26 58 and 13 bits for the distance extra. This totals 48 bits, or six bytes.
nuclear@26 59 Therefore if strm->avail_in >= 6, then there is enough input to avoid
nuclear@26 60 checking for available input while decoding.
nuclear@26 61
nuclear@26 62 - The maximum bytes that a single length/distance pair can output is 258
nuclear@26 63 bytes, which is the maximum length that can be coded. inflate_fast()
nuclear@26 64 requires strm->avail_out >= 258 for each loop to avoid checking for
nuclear@26 65 output space.
nuclear@26 66 */
nuclear@26 67 void inflate_fast(strm, start)
nuclear@26 68 z_streamp strm;
nuclear@26 69 unsigned start; /* inflate()'s starting value for strm->avail_out */
nuclear@26 70 {
nuclear@26 71 struct inflate_state FAR *state;
nuclear@26 72 unsigned char FAR *in; /* local strm->next_in */
nuclear@26 73 unsigned char FAR *last; /* while in < last, enough input available */
nuclear@26 74 unsigned char FAR *out; /* local strm->next_out */
nuclear@26 75 unsigned char FAR *beg; /* inflate()'s initial strm->next_out */
nuclear@26 76 unsigned char FAR *end; /* while out < end, enough space available */
nuclear@26 77 #ifdef INFLATE_STRICT
nuclear@26 78 unsigned dmax; /* maximum distance from zlib header */
nuclear@26 79 #endif
nuclear@26 80 unsigned wsize; /* window size or zero if not using window */
nuclear@26 81 unsigned whave; /* valid bytes in the window */
nuclear@26 82 unsigned write; /* window write index */
nuclear@26 83 unsigned char FAR *window; /* allocated sliding window, if wsize != 0 */
nuclear@26 84 unsigned long hold; /* local strm->hold */
nuclear@26 85 unsigned bits; /* local strm->bits */
nuclear@26 86 code const FAR *lcode; /* local strm->lencode */
nuclear@26 87 code const FAR *dcode; /* local strm->distcode */
nuclear@26 88 unsigned lmask; /* mask for first level of length codes */
nuclear@26 89 unsigned dmask; /* mask for first level of distance codes */
nuclear@26 90 code this; /* retrieved table entry */
nuclear@26 91 unsigned op; /* code bits, operation, extra bits, or */
nuclear@26 92 /* window position, window bytes to copy */
nuclear@26 93 unsigned len; /* match length, unused bytes */
nuclear@26 94 unsigned dist; /* match distance */
nuclear@26 95 unsigned char FAR *from; /* where to copy match from */
nuclear@26 96
nuclear@26 97 /* copy state to local variables */
nuclear@26 98 state = (struct inflate_state FAR *)strm->state;
nuclear@26 99 in = strm->next_in - OFF;
nuclear@26 100 last = in + (strm->avail_in - 5);
nuclear@26 101 out = strm->next_out - OFF;
nuclear@26 102 beg = out - (start - strm->avail_out);
nuclear@26 103 end = out + (strm->avail_out - 257);
nuclear@26 104 #ifdef INFLATE_STRICT
nuclear@26 105 dmax = state->dmax;
nuclear@26 106 #endif
nuclear@26 107 wsize = state->wsize;
nuclear@26 108 whave = state->whave;
nuclear@26 109 write = state->write;
nuclear@26 110 window = state->window;
nuclear@26 111 hold = state->hold;
nuclear@26 112 bits = state->bits;
nuclear@26 113 lcode = state->lencode;
nuclear@26 114 dcode = state->distcode;
nuclear@26 115 lmask = (1U << state->lenbits) - 1;
nuclear@26 116 dmask = (1U << state->distbits) - 1;
nuclear@26 117
nuclear@26 118 /* decode literals and length/distances until end-of-block or not enough
nuclear@26 119 input data or output space */
nuclear@26 120 do {
nuclear@26 121 if (bits < 15) {
nuclear@26 122 hold += (unsigned long)(PUP(in)) << bits;
nuclear@26 123 bits += 8;
nuclear@26 124 hold += (unsigned long)(PUP(in)) << bits;
nuclear@26 125 bits += 8;
nuclear@26 126 }
nuclear@26 127 this = lcode[hold & lmask];
nuclear@26 128 dolen:
nuclear@26 129 op = (unsigned)(this.bits);
nuclear@26 130 hold >>= op;
nuclear@26 131 bits -= op;
nuclear@26 132 op = (unsigned)(this.op);
nuclear@26 133 if (op == 0) { /* literal */
nuclear@26 134 Tracevv((stderr, this.val >= 0x20 && this.val < 0x7f ?
nuclear@26 135 "inflate: literal '%c'\n" :
nuclear@26 136 "inflate: literal 0x%02x\n", this.val));
nuclear@26 137 PUP(out) = (unsigned char)(this.val);
nuclear@26 138 }
nuclear@26 139 else if (op & 16) { /* length base */
nuclear@26 140 len = (unsigned)(this.val);
nuclear@26 141 op &= 15; /* number of extra bits */
nuclear@26 142 if (op) {
nuclear@26 143 if (bits < op) {
nuclear@26 144 hold += (unsigned long)(PUP(in)) << bits;
nuclear@26 145 bits += 8;
nuclear@26 146 }
nuclear@26 147 len += (unsigned)hold & ((1U << op) - 1);
nuclear@26 148 hold >>= op;
nuclear@26 149 bits -= op;
nuclear@26 150 }
nuclear@26 151 Tracevv((stderr, "inflate: length %u\n", len));
nuclear@26 152 if (bits < 15) {
nuclear@26 153 hold += (unsigned long)(PUP(in)) << bits;
nuclear@26 154 bits += 8;
nuclear@26 155 hold += (unsigned long)(PUP(in)) << bits;
nuclear@26 156 bits += 8;
nuclear@26 157 }
nuclear@26 158 this = dcode[hold & dmask];
nuclear@26 159 dodist:
nuclear@26 160 op = (unsigned)(this.bits);
nuclear@26 161 hold >>= op;
nuclear@26 162 bits -= op;
nuclear@26 163 op = (unsigned)(this.op);
nuclear@26 164 if (op & 16) { /* distance base */
nuclear@26 165 dist = (unsigned)(this.val);
nuclear@26 166 op &= 15; /* number of extra bits */
nuclear@26 167 if (bits < op) {
nuclear@26 168 hold += (unsigned long)(PUP(in)) << bits;
nuclear@26 169 bits += 8;
nuclear@26 170 if (bits < op) {
nuclear@26 171 hold += (unsigned long)(PUP(in)) << bits;
nuclear@26 172 bits += 8;
nuclear@26 173 }
nuclear@26 174 }
nuclear@26 175 dist += (unsigned)hold & ((1U << op) - 1);
nuclear@26 176 #ifdef INFLATE_STRICT
nuclear@26 177 if (dist > dmax) {
nuclear@26 178 strm->msg = (char *)"invalid distance too far back";
nuclear@26 179 state->mode = BAD;
nuclear@26 180 break;
nuclear@26 181 }
nuclear@26 182 #endif
nuclear@26 183 hold >>= op;
nuclear@26 184 bits -= op;
nuclear@26 185 Tracevv((stderr, "inflate: distance %u\n", dist));
nuclear@26 186 op = (unsigned)(out - beg); /* max distance in output */
nuclear@26 187 if (dist > op) { /* see if copy from window */
nuclear@26 188 op = dist - op; /* distance back in window */
nuclear@26 189 if (op > whave) {
nuclear@26 190 strm->msg = (char *)"invalid distance too far back";
nuclear@26 191 state->mode = BAD;
nuclear@26 192 break;
nuclear@26 193 }
nuclear@26 194 from = window - OFF;
nuclear@26 195 if (write == 0) { /* very common case */
nuclear@26 196 from += wsize - op;
nuclear@26 197 if (op < len) { /* some from window */
nuclear@26 198 len -= op;
nuclear@26 199 do {
nuclear@26 200 PUP(out) = PUP(from);
nuclear@26 201 } while (--op);
nuclear@26 202 from = out - dist; /* rest from output */
nuclear@26 203 }
nuclear@26 204 }
nuclear@26 205 else if (write < op) { /* wrap around window */
nuclear@26 206 from += wsize + write - op;
nuclear@26 207 op -= write;
nuclear@26 208 if (op < len) { /* some from end of window */
nuclear@26 209 len -= op;
nuclear@26 210 do {
nuclear@26 211 PUP(out) = PUP(from);
nuclear@26 212 } while (--op);
nuclear@26 213 from = window - OFF;
nuclear@26 214 if (write < len) { /* some from start of window */
nuclear@26 215 op = write;
nuclear@26 216 len -= op;
nuclear@26 217 do {
nuclear@26 218 PUP(out) = PUP(from);
nuclear@26 219 } while (--op);
nuclear@26 220 from = out - dist; /* rest from output */
nuclear@26 221 }
nuclear@26 222 }
nuclear@26 223 }
nuclear@26 224 else { /* contiguous in window */
nuclear@26 225 from += write - op;
nuclear@26 226 if (op < len) { /* some from window */
nuclear@26 227 len -= op;
nuclear@26 228 do {
nuclear@26 229 PUP(out) = PUP(from);
nuclear@26 230 } while (--op);
nuclear@26 231 from = out - dist; /* rest from output */
nuclear@26 232 }
nuclear@26 233 }
nuclear@26 234 while (len > 2) {
nuclear@26 235 PUP(out) = PUP(from);
nuclear@26 236 PUP(out) = PUP(from);
nuclear@26 237 PUP(out) = PUP(from);
nuclear@26 238 len -= 3;
nuclear@26 239 }
nuclear@26 240 if (len) {
nuclear@26 241 PUP(out) = PUP(from);
nuclear@26 242 if (len > 1)
nuclear@26 243 PUP(out) = PUP(from);
nuclear@26 244 }
nuclear@26 245 }
nuclear@26 246 else {
nuclear@26 247 from = out - dist; /* copy direct from output */
nuclear@26 248 do { /* minimum length is three */
nuclear@26 249 PUP(out) = PUP(from);
nuclear@26 250 PUP(out) = PUP(from);
nuclear@26 251 PUP(out) = PUP(from);
nuclear@26 252 len -= 3;
nuclear@26 253 } while (len > 2);
nuclear@26 254 if (len) {
nuclear@26 255 PUP(out) = PUP(from);
nuclear@26 256 if (len > 1)
nuclear@26 257 PUP(out) = PUP(from);
nuclear@26 258 }
nuclear@26 259 }
nuclear@26 260 }
nuclear@26 261 else if ((op & 64) == 0) { /* 2nd level distance code */
nuclear@26 262 this = dcode[this.val + (hold & ((1U << op) - 1))];
nuclear@26 263 goto dodist;
nuclear@26 264 }
nuclear@26 265 else {
nuclear@26 266 strm->msg = (char *)"invalid distance code";
nuclear@26 267 state->mode = BAD;
nuclear@26 268 break;
nuclear@26 269 }
nuclear@26 270 }
nuclear@26 271 else if ((op & 64) == 0) { /* 2nd level length code */
nuclear@26 272 this = lcode[this.val + (hold & ((1U << op) - 1))];
nuclear@26 273 goto dolen;
nuclear@26 274 }
nuclear@26 275 else if (op & 32) { /* end-of-block */
nuclear@26 276 Tracevv((stderr, "inflate: end of block\n"));
nuclear@26 277 state->mode = TYPE;
nuclear@26 278 break;
nuclear@26 279 }
nuclear@26 280 else {
nuclear@26 281 strm->msg = (char *)"invalid literal/length code";
nuclear@26 282 state->mode = BAD;
nuclear@26 283 break;
nuclear@26 284 }
nuclear@26 285 } while (in < last && out < end);
nuclear@26 286
nuclear@26 287 /* return unused bytes (on entry, bits < 8, so in won't go too far back) */
nuclear@26 288 len = bits >> 3;
nuclear@26 289 in -= len;
nuclear@26 290 bits -= len << 3;
nuclear@26 291 hold &= (1U << bits) - 1;
nuclear@26 292
nuclear@26 293 /* update state and return */
nuclear@26 294 strm->next_in = in + OFF;
nuclear@26 295 strm->next_out = out + OFF;
nuclear@26 296 strm->avail_in = (unsigned)(in < last ? 5 + (last - in) : 5 - (in - last));
nuclear@26 297 strm->avail_out = (unsigned)(out < end ?
nuclear@26 298 257 + (end - out) : 257 - (out - end));
nuclear@26 299 state->hold = hold;
nuclear@26 300 state->bits = bits;
nuclear@26 301 return;
nuclear@26 302 }
nuclear@26 303
nuclear@26 304 /*
nuclear@26 305 inflate_fast() speedups that turned out slower (on a PowerPC G3 750CXe):
nuclear@26 306 - Using bit fields for code structure
nuclear@26 307 - Different op definition to avoid & for extra bits (do & for table bits)
nuclear@26 308 - Three separate decoding do-loops for direct, window, and write == 0
nuclear@26 309 - Special case for distance > 1 copies to do overlapped load and store copy
nuclear@26 310 - Explicit branch predictions (based on measured branch probabilities)
nuclear@26 311 - Deferring match copy and interspersed it with decoding subsequent codes
nuclear@26 312 - Swapping literal/length else
nuclear@26 313 - Swapping window/direct else
nuclear@26 314 - Larger unrolled copy loops (three is about right)
nuclear@26 315 - Moving len -= 3 statement into middle of loop
nuclear@26 316 */
nuclear@26 317
nuclear@26 318 #endif /* !ASMINF */