1*44bedb31SLionel Sambuc /* $NetBSD: testzlib.c,v 1.1.1.1 2006/01/14 20:11:02 christos Exp $ */
2*44bedb31SLionel Sambuc
3*44bedb31SLionel Sambuc #include <stdio.h>
4*44bedb31SLionel Sambuc #include <stdlib.h>
5*44bedb31SLionel Sambuc #include <windows.h>
6*44bedb31SLionel Sambuc
7*44bedb31SLionel Sambuc #include "zlib.h"
8*44bedb31SLionel Sambuc
9*44bedb31SLionel Sambuc
MyDoMinus64(LARGE_INTEGER * R,LARGE_INTEGER A,LARGE_INTEGER B)10*44bedb31SLionel Sambuc void MyDoMinus64(LARGE_INTEGER *R,LARGE_INTEGER A,LARGE_INTEGER B)
11*44bedb31SLionel Sambuc {
12*44bedb31SLionel Sambuc R->HighPart = A.HighPart - B.HighPart;
13*44bedb31SLionel Sambuc if (A.LowPart >= B.LowPart)
14*44bedb31SLionel Sambuc R->LowPart = A.LowPart - B.LowPart;
15*44bedb31SLionel Sambuc else
16*44bedb31SLionel Sambuc {
17*44bedb31SLionel Sambuc R->LowPart = A.LowPart - B.LowPart;
18*44bedb31SLionel Sambuc R->HighPart --;
19*44bedb31SLionel Sambuc }
20*44bedb31SLionel Sambuc }
21*44bedb31SLionel Sambuc
22*44bedb31SLionel Sambuc #ifdef _M_X64
23*44bedb31SLionel Sambuc // see http://msdn2.microsoft.com/library/twchhe95(en-us,vs.80).aspx for __rdtsc
24*44bedb31SLionel Sambuc unsigned __int64 __rdtsc(void);
BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)25*44bedb31SLionel Sambuc void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
26*44bedb31SLionel Sambuc {
27*44bedb31SLionel Sambuc // printf("rdtsc = %I64x\n",__rdtsc());
28*44bedb31SLionel Sambuc pbeginTime64->QuadPart=__rdtsc();
29*44bedb31SLionel Sambuc }
30*44bedb31SLionel Sambuc
GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)31*44bedb31SLionel Sambuc LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
32*44bedb31SLionel Sambuc {
33*44bedb31SLionel Sambuc LARGE_INTEGER LIres;
34*44bedb31SLionel Sambuc unsigned _int64 res=__rdtsc()-((unsigned _int64)(beginTime64.QuadPart));
35*44bedb31SLionel Sambuc LIres.QuadPart=res;
36*44bedb31SLionel Sambuc // printf("rdtsc = %I64x\n",__rdtsc());
37*44bedb31SLionel Sambuc return LIres;
38*44bedb31SLionel Sambuc }
39*44bedb31SLionel Sambuc #else
40*44bedb31SLionel Sambuc #ifdef _M_IX86
myGetRDTSC32(LARGE_INTEGER * pbeginTime64)41*44bedb31SLionel Sambuc void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
42*44bedb31SLionel Sambuc {
43*44bedb31SLionel Sambuc DWORD dwEdx,dwEax;
44*44bedb31SLionel Sambuc _asm
45*44bedb31SLionel Sambuc {
46*44bedb31SLionel Sambuc rdtsc
47*44bedb31SLionel Sambuc mov dwEax,eax
48*44bedb31SLionel Sambuc mov dwEdx,edx
49*44bedb31SLionel Sambuc }
50*44bedb31SLionel Sambuc pbeginTime64->LowPart=dwEax;
51*44bedb31SLionel Sambuc pbeginTime64->HighPart=dwEdx;
52*44bedb31SLionel Sambuc }
53*44bedb31SLionel Sambuc
BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)54*44bedb31SLionel Sambuc void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
55*44bedb31SLionel Sambuc {
56*44bedb31SLionel Sambuc myGetRDTSC32(pbeginTime64);
57*44bedb31SLionel Sambuc }
58*44bedb31SLionel Sambuc
GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)59*44bedb31SLionel Sambuc LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
60*44bedb31SLionel Sambuc {
61*44bedb31SLionel Sambuc LARGE_INTEGER LIres,endTime64;
62*44bedb31SLionel Sambuc myGetRDTSC32(&endTime64);
63*44bedb31SLionel Sambuc
64*44bedb31SLionel Sambuc LIres.LowPart=LIres.HighPart=0;
65*44bedb31SLionel Sambuc MyDoMinus64(&LIres,endTime64,beginTime64);
66*44bedb31SLionel Sambuc return LIres;
67*44bedb31SLionel Sambuc }
68*44bedb31SLionel Sambuc #else
myGetRDTSC32(LARGE_INTEGER * pbeginTime64)69*44bedb31SLionel Sambuc void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
70*44bedb31SLionel Sambuc {
71*44bedb31SLionel Sambuc }
72*44bedb31SLionel Sambuc
BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)73*44bedb31SLionel Sambuc void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
74*44bedb31SLionel Sambuc {
75*44bedb31SLionel Sambuc }
76*44bedb31SLionel Sambuc
GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)77*44bedb31SLionel Sambuc LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
78*44bedb31SLionel Sambuc {
79*44bedb31SLionel Sambuc LARGE_INTEGER lr;
80*44bedb31SLionel Sambuc lr.QuadPart=0;
81*44bedb31SLionel Sambuc return lr;
82*44bedb31SLionel Sambuc }
83*44bedb31SLionel Sambuc #endif
84*44bedb31SLionel Sambuc #endif
85*44bedb31SLionel Sambuc
BeginCountPerfCounter(LARGE_INTEGER * pbeginTime64,BOOL fComputeTimeQueryPerf)86*44bedb31SLionel Sambuc void BeginCountPerfCounter(LARGE_INTEGER * pbeginTime64,BOOL fComputeTimeQueryPerf)
87*44bedb31SLionel Sambuc {
88*44bedb31SLionel Sambuc if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(pbeginTime64)))
89*44bedb31SLionel Sambuc {
90*44bedb31SLionel Sambuc pbeginTime64->LowPart = GetTickCount();
91*44bedb31SLionel Sambuc pbeginTime64->HighPart = 0;
92*44bedb31SLionel Sambuc }
93*44bedb31SLionel Sambuc }
94*44bedb31SLionel Sambuc
GetMsecSincePerfCounter(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)95*44bedb31SLionel Sambuc DWORD GetMsecSincePerfCounter(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
96*44bedb31SLionel Sambuc {
97*44bedb31SLionel Sambuc LARGE_INTEGER endTime64,ticksPerSecond,ticks;
98*44bedb31SLionel Sambuc DWORDLONG ticksShifted,tickSecShifted;
99*44bedb31SLionel Sambuc DWORD dwLog=16+0;
100*44bedb31SLionel Sambuc DWORD dwRet;
101*44bedb31SLionel Sambuc if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(&endTime64)))
102*44bedb31SLionel Sambuc dwRet = (GetTickCount() - beginTime64.LowPart)*1;
103*44bedb31SLionel Sambuc else
104*44bedb31SLionel Sambuc {
105*44bedb31SLionel Sambuc MyDoMinus64(&ticks,endTime64,beginTime64);
106*44bedb31SLionel Sambuc QueryPerformanceFrequency(&ticksPerSecond);
107*44bedb31SLionel Sambuc
108*44bedb31SLionel Sambuc
109*44bedb31SLionel Sambuc {
110*44bedb31SLionel Sambuc ticksShifted = Int64ShrlMod32(*(DWORDLONG*)&ticks,dwLog);
111*44bedb31SLionel Sambuc tickSecShifted = Int64ShrlMod32(*(DWORDLONG*)&ticksPerSecond,dwLog);
112*44bedb31SLionel Sambuc
113*44bedb31SLionel Sambuc }
114*44bedb31SLionel Sambuc
115*44bedb31SLionel Sambuc dwRet = (DWORD)((((DWORD)ticksShifted)*1000)/(DWORD)(tickSecShifted));
116*44bedb31SLionel Sambuc dwRet *=1;
117*44bedb31SLionel Sambuc }
118*44bedb31SLionel Sambuc return dwRet;
119*44bedb31SLionel Sambuc }
120*44bedb31SLionel Sambuc
ReadFileMemory(const char * filename,long * plFileSize,void ** pFilePtr)121*44bedb31SLionel Sambuc int ReadFileMemory(const char* filename,long* plFileSize,void** pFilePtr)
122*44bedb31SLionel Sambuc {
123*44bedb31SLionel Sambuc FILE* stream;
124*44bedb31SLionel Sambuc void* ptr;
125*44bedb31SLionel Sambuc int retVal=1;
126*44bedb31SLionel Sambuc stream=fopen(filename, "rb");
127*44bedb31SLionel Sambuc if (stream==NULL)
128*44bedb31SLionel Sambuc return 0;
129*44bedb31SLionel Sambuc
130*44bedb31SLionel Sambuc fseek(stream,0,SEEK_END);
131*44bedb31SLionel Sambuc
132*44bedb31SLionel Sambuc *plFileSize=ftell(stream);
133*44bedb31SLionel Sambuc fseek(stream,0,SEEK_SET);
134*44bedb31SLionel Sambuc ptr=malloc((*plFileSize)+1);
135*44bedb31SLionel Sambuc if (ptr==NULL)
136*44bedb31SLionel Sambuc retVal=0;
137*44bedb31SLionel Sambuc else
138*44bedb31SLionel Sambuc {
139*44bedb31SLionel Sambuc if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))
140*44bedb31SLionel Sambuc retVal=0;
141*44bedb31SLionel Sambuc }
142*44bedb31SLionel Sambuc fclose(stream);
143*44bedb31SLionel Sambuc *pFilePtr=ptr;
144*44bedb31SLionel Sambuc return retVal;
145*44bedb31SLionel Sambuc }
146*44bedb31SLionel Sambuc
main(int argc,char * argv[])147*44bedb31SLionel Sambuc int main(int argc, char *argv[])
148*44bedb31SLionel Sambuc {
149*44bedb31SLionel Sambuc int BlockSizeCompress=0x8000;
150*44bedb31SLionel Sambuc int BlockSizeUncompress=0x8000;
151*44bedb31SLionel Sambuc int cprLevel=Z_DEFAULT_COMPRESSION ;
152*44bedb31SLionel Sambuc long lFileSize;
153*44bedb31SLionel Sambuc unsigned char* FilePtr;
154*44bedb31SLionel Sambuc long lBufferSizeCpr;
155*44bedb31SLionel Sambuc long lBufferSizeUncpr;
156*44bedb31SLionel Sambuc long lCompressedSize=0;
157*44bedb31SLionel Sambuc unsigned char* CprPtr;
158*44bedb31SLionel Sambuc unsigned char* UncprPtr;
159*44bedb31SLionel Sambuc long lSizeCpr,lSizeUncpr;
160*44bedb31SLionel Sambuc DWORD dwGetTick,dwMsecQP;
161*44bedb31SLionel Sambuc LARGE_INTEGER li_qp,li_rdtsc,dwResRdtsc;
162*44bedb31SLionel Sambuc
163*44bedb31SLionel Sambuc if (argc<=1)
164*44bedb31SLionel Sambuc {
165*44bedb31SLionel Sambuc printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]\n");
166*44bedb31SLionel Sambuc return 0;
167*44bedb31SLionel Sambuc }
168*44bedb31SLionel Sambuc
169*44bedb31SLionel Sambuc if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)
170*44bedb31SLionel Sambuc {
171*44bedb31SLionel Sambuc printf("error reading %s\n",argv[1]);
172*44bedb31SLionel Sambuc return 1;
173*44bedb31SLionel Sambuc }
174*44bedb31SLionel Sambuc else printf("file %s read, %u bytes\n",argv[1],lFileSize);
175*44bedb31SLionel Sambuc
176*44bedb31SLionel Sambuc if (argc>=3)
177*44bedb31SLionel Sambuc BlockSizeCompress=atol(argv[2]);
178*44bedb31SLionel Sambuc
179*44bedb31SLionel Sambuc if (argc>=4)
180*44bedb31SLionel Sambuc BlockSizeUncompress=atol(argv[3]);
181*44bedb31SLionel Sambuc
182*44bedb31SLionel Sambuc if (argc>=5)
183*44bedb31SLionel Sambuc cprLevel=(int)atol(argv[4]);
184*44bedb31SLionel Sambuc
185*44bedb31SLionel Sambuc lBufferSizeCpr = lFileSize + (lFileSize/0x10) + 0x200;
186*44bedb31SLionel Sambuc lBufferSizeUncpr = lBufferSizeCpr;
187*44bedb31SLionel Sambuc
188*44bedb31SLionel Sambuc CprPtr=(unsigned char*)malloc(lBufferSizeCpr + BlockSizeCompress);
189*44bedb31SLionel Sambuc
190*44bedb31SLionel Sambuc BeginCountPerfCounter(&li_qp,TRUE);
191*44bedb31SLionel Sambuc dwGetTick=GetTickCount();
192*44bedb31SLionel Sambuc BeginCountRdtsc(&li_rdtsc);
193*44bedb31SLionel Sambuc {
194*44bedb31SLionel Sambuc z_stream zcpr;
195*44bedb31SLionel Sambuc int ret=Z_OK;
196*44bedb31SLionel Sambuc long lOrigToDo = lFileSize;
197*44bedb31SLionel Sambuc long lOrigDone = 0;
198*44bedb31SLionel Sambuc int step=0;
199*44bedb31SLionel Sambuc memset(&zcpr,0,sizeof(z_stream));
200*44bedb31SLionel Sambuc deflateInit(&zcpr,cprLevel);
201*44bedb31SLionel Sambuc
202*44bedb31SLionel Sambuc zcpr.next_in = FilePtr;
203*44bedb31SLionel Sambuc zcpr.next_out = CprPtr;
204*44bedb31SLionel Sambuc
205*44bedb31SLionel Sambuc
206*44bedb31SLionel Sambuc do
207*44bedb31SLionel Sambuc {
208*44bedb31SLionel Sambuc long all_read_before = zcpr.total_in;
209*44bedb31SLionel Sambuc zcpr.avail_in = min(lOrigToDo,BlockSizeCompress);
210*44bedb31SLionel Sambuc zcpr.avail_out = BlockSizeCompress;
211*44bedb31SLionel Sambuc ret=deflate(&zcpr,(zcpr.avail_in==lOrigToDo) ? Z_FINISH : Z_SYNC_FLUSH);
212*44bedb31SLionel Sambuc lOrigDone += (zcpr.total_in-all_read_before);
213*44bedb31SLionel Sambuc lOrigToDo -= (zcpr.total_in-all_read_before);
214*44bedb31SLionel Sambuc step++;
215*44bedb31SLionel Sambuc } while (ret==Z_OK);
216*44bedb31SLionel Sambuc
217*44bedb31SLionel Sambuc lSizeCpr=zcpr.total_out;
218*44bedb31SLionel Sambuc deflateEnd(&zcpr);
219*44bedb31SLionel Sambuc dwGetTick=GetTickCount()-dwGetTick;
220*44bedb31SLionel Sambuc dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
221*44bedb31SLionel Sambuc dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
222*44bedb31SLionel Sambuc printf("total compress size = %u, in %u step\n",lSizeCpr,step);
223*44bedb31SLionel Sambuc printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
224*44bedb31SLionel Sambuc printf("defcpr time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
225*44bedb31SLionel Sambuc printf("defcpr result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
226*44bedb31SLionel Sambuc }
227*44bedb31SLionel Sambuc
228*44bedb31SLionel Sambuc CprPtr=(unsigned char*)realloc(CprPtr,lSizeCpr);
229*44bedb31SLionel Sambuc UncprPtr=(unsigned char*)malloc(lBufferSizeUncpr + BlockSizeUncompress);
230*44bedb31SLionel Sambuc
231*44bedb31SLionel Sambuc BeginCountPerfCounter(&li_qp,TRUE);
232*44bedb31SLionel Sambuc dwGetTick=GetTickCount();
233*44bedb31SLionel Sambuc BeginCountRdtsc(&li_rdtsc);
234*44bedb31SLionel Sambuc {
235*44bedb31SLionel Sambuc z_stream zcpr;
236*44bedb31SLionel Sambuc int ret=Z_OK;
237*44bedb31SLionel Sambuc long lOrigToDo = lSizeCpr;
238*44bedb31SLionel Sambuc long lOrigDone = 0;
239*44bedb31SLionel Sambuc int step=0;
240*44bedb31SLionel Sambuc memset(&zcpr,0,sizeof(z_stream));
241*44bedb31SLionel Sambuc inflateInit(&zcpr);
242*44bedb31SLionel Sambuc
243*44bedb31SLionel Sambuc zcpr.next_in = CprPtr;
244*44bedb31SLionel Sambuc zcpr.next_out = UncprPtr;
245*44bedb31SLionel Sambuc
246*44bedb31SLionel Sambuc
247*44bedb31SLionel Sambuc do
248*44bedb31SLionel Sambuc {
249*44bedb31SLionel Sambuc long all_read_before = zcpr.total_in;
250*44bedb31SLionel Sambuc zcpr.avail_in = min(lOrigToDo,BlockSizeUncompress);
251*44bedb31SLionel Sambuc zcpr.avail_out = BlockSizeUncompress;
252*44bedb31SLionel Sambuc ret=inflate(&zcpr,Z_SYNC_FLUSH);
253*44bedb31SLionel Sambuc lOrigDone += (zcpr.total_in-all_read_before);
254*44bedb31SLionel Sambuc lOrigToDo -= (zcpr.total_in-all_read_before);
255*44bedb31SLionel Sambuc step++;
256*44bedb31SLionel Sambuc } while (ret==Z_OK);
257*44bedb31SLionel Sambuc
258*44bedb31SLionel Sambuc lSizeUncpr=zcpr.total_out;
259*44bedb31SLionel Sambuc inflateEnd(&zcpr);
260*44bedb31SLionel Sambuc dwGetTick=GetTickCount()-dwGetTick;
261*44bedb31SLionel Sambuc dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
262*44bedb31SLionel Sambuc dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
263*44bedb31SLionel Sambuc printf("total uncompress size = %u, in %u step\n",lSizeUncpr,step);
264*44bedb31SLionel Sambuc printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
265*44bedb31SLionel Sambuc printf("uncpr time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
266*44bedb31SLionel Sambuc printf("uncpr result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
267*44bedb31SLionel Sambuc }
268*44bedb31SLionel Sambuc
269*44bedb31SLionel Sambuc if (lSizeUncpr==lFileSize)
270*44bedb31SLionel Sambuc {
271*44bedb31SLionel Sambuc if (memcmp(FilePtr,UncprPtr,lFileSize)==0)
272*44bedb31SLionel Sambuc printf("compare ok\n");
273*44bedb31SLionel Sambuc
274*44bedb31SLionel Sambuc }
275*44bedb31SLionel Sambuc
276*44bedb31SLionel Sambuc return 0;
277*44bedb31SLionel Sambuc }
278