flate

deflate implementation
git clone git://git.suckless.org/flate
Log | Files | Refs | README

commit e6de1ab18b761e304518e0b3bb50c39b8ad83452
parent 62cef5aaa820f7de3a20d9c7f641f2326047ad4e
Author: nsz@tpx <unknown>
Date:   Sun,  9 Aug 2009 15:25:43 +0200

deflate optimization: do while(), s/ushort/int/
Diffstat:
Makefile | 18+++++++++---------
deflate.c | 42+++++++++++++++++++++---------------------
2 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/Makefile b/Makefile @@ -1,5 +1,5 @@ -CFLAGS=-g -Wall -ansi -pedantic -#CFLAGS=-O3 -Wall -ansi -pedantic +#CFLAGS=-g -Wall -ansi -pedantic +CFLAGS=-O3 -Wall -ansi -pedantic LDFLAGS= SRC=inflate.c inflate_simple.c inflate_example.c inflate_callback.c \ deflate.c deflate_simple.c deflate_example.c @@ -25,10 +25,10 @@ clean: prof: profinf profdef -profinf: inflate.c - gcc -O3 -fprofile-arcs -ftest-coverage -pg -g -Wall $< +profinf: inflate_example.c inflate.c + gcc -O3 -fprofile-arcs -ftest-coverage -pg -g -Wall $+ ./a.out <a.dat >/dev/null - gcov -b $< >/dev/null + gcov -b $+ >/dev/null gprof a.out >$<.gprof # gcc -g -Wall $< # valgrind -v --leak-check=yes ./a.out <a.dat >/dev/null 2>a.valgrind @@ -36,9 +36,9 @@ profinf: inflate.c # grep alloc a.valgrind rm -f a.out a.valgrind *.gcno *.gcda gmon.out -profdef: deflate.c - gcc -O0 -fprofile-arcs -ftest-coverage -pg -g -Wall $< - ./a.out <a.pdf >/dev/null - gcov -b $< >/dev/null +profdef: deflate_example.c deflate.c + gcc -O3 -fprofile-arcs -ftest-coverage -pg -g -Wall $+ + ./a.out <g.tar >/dev/null + gcov -b $+ >/dev/null gprof a.out >$<.gprof rm -f a.out *.gcno *.gcda gmon.out diff --git a/deflate.c b/deflate.c @@ -500,15 +500,15 @@ static ushort addtochain(State *s, ushort pos) { return i; } -static Match getmatch(State *s, ushort pos, ushort mpos) { +static Match getmatch(State *s, int pos, int mpos) { Match m = {0, MinMatch-1}; - ushort len; - ushort maxlen = s->avail < MaxMatch ? s->avail : MaxMatch; - ushort limit = pos - MaxDist; - ushort chainlen = MaxChainLen; + int len; + int maxlen = s->avail < MaxMatch ? s->avail : MaxMatch; + int limit = pos - MaxDist; + int chainlen = MaxChainLen; uchar *q; uchar *p = s->win + pos; - uchar *end = p + MaxMatch; /* out of bound if avail < MaxMatch */ + uchar *end = p + MaxMatch; do { q = s->win + mpos; @@ -516,11 +516,12 @@ static Match getmatch(State *s, ushort pos, ushort mpos) { if (q[m.len] != p[m.len] || q[0] != p[0]) continue; /* loop unroll: MaxMatch % 10 == 8, overflow if avail < MaxMatch */ - while (*++q == *++p && *++q == *++p - && *++q == *++p && *++q == *++p - && *++q == *++p && *++q == *++p - && *++q == *++p && ++p != end && *++q == *p - && *++q == *++p && *++q == *++p); + /* only small gain over while (++p != end && *++q == *p); */ + do; while (*++q == *++p && *++q == *++p + && *++q == *++p && *++q == *++p + && *++q == *++p && *++q == *++p + && *++q == *++p && ++p != end && *++q == *p + && *++q == *++p && *++q == *++p); len = MaxMatch - (end - p); p -= len; if (len > m.len) { @@ -548,8 +549,7 @@ static void newblock(State *s) { } static int fillwin(State *s) { - int n; - int need, have; + int n, k; if (s->pos >= 2*MaxDist) { /* shift win */ @@ -563,15 +563,15 @@ static int fillwin(State *s) { } if (s->avail < MaxMatch) { /* fill win */ - need = WinSize - s->pos - s->avail; - have = s->srcend - s->src; - if (have > need) - have = need; - if (have == 0) + n = WinSize - s->pos - s->avail; + k = s->srcend - s->src; + if (k > n) + k = n; + if (k == 0) return FlateIn; - memcpy(s->win + s->pos + s->avail, s->src, have); - s->src += have; - s->avail += have; + memcpy(s->win + s->pos + s->avail, s->src, k); + s->src += k; + s->avail += k; } return FlateOk; }