1 /* $NetBSD: testzlib.c,v 1.1.1.1 2006/01/14 20:11:02 christos Exp $ */
10 void MyDoMinus64(LARGE_INTEGER
*R
,LARGE_INTEGER A
,LARGE_INTEGER B
)
12 R
->HighPart
= A
.HighPart
- B
.HighPart
;
13 if (A
.LowPart
>= B
.LowPart
)
14 R
->LowPart
= A
.LowPart
- B
.LowPart
;
17 R
->LowPart
= A
.LowPart
- B
.LowPart
;
23 // see http://msdn2.microsoft.com/library/twchhe95(en-us,vs.80).aspx for __rdtsc
24 unsigned __int64
__rdtsc(void);
25 void BeginCountRdtsc(LARGE_INTEGER
* pbeginTime64
)
27 // printf("rdtsc = %I64x\n",__rdtsc());
28 pbeginTime64
->QuadPart
=__rdtsc();
31 LARGE_INTEGER
GetResRdtsc(LARGE_INTEGER beginTime64
,BOOL fComputeTimeQueryPerf
)
34 unsigned _int64 res
=__rdtsc()-((unsigned _int64
)(beginTime64
.QuadPart
));
36 // printf("rdtsc = %I64x\n",__rdtsc());
41 void myGetRDTSC32(LARGE_INTEGER
* pbeginTime64
)
50 pbeginTime64
->LowPart
=dwEax
;
51 pbeginTime64
->HighPart
=dwEdx
;
54 void BeginCountRdtsc(LARGE_INTEGER
* pbeginTime64
)
56 myGetRDTSC32(pbeginTime64
);
59 LARGE_INTEGER
GetResRdtsc(LARGE_INTEGER beginTime64
,BOOL fComputeTimeQueryPerf
)
61 LARGE_INTEGER LIres
,endTime64
;
62 myGetRDTSC32(&endTime64
);
64 LIres
.LowPart
=LIres
.HighPart
=0;
65 MyDoMinus64(&LIres
,endTime64
,beginTime64
);
69 void myGetRDTSC32(LARGE_INTEGER
* pbeginTime64
)
73 void BeginCountRdtsc(LARGE_INTEGER
* pbeginTime64
)
77 LARGE_INTEGER
GetResRdtsc(LARGE_INTEGER beginTime64
,BOOL fComputeTimeQueryPerf
)
86 void BeginCountPerfCounter(LARGE_INTEGER
* pbeginTime64
,BOOL fComputeTimeQueryPerf
)
88 if ((!fComputeTimeQueryPerf
) || (!QueryPerformanceCounter(pbeginTime64
)))
90 pbeginTime64
->LowPart
= GetTickCount();
91 pbeginTime64
->HighPart
= 0;
95 DWORD
GetMsecSincePerfCounter(LARGE_INTEGER beginTime64
,BOOL fComputeTimeQueryPerf
)
97 LARGE_INTEGER endTime64
,ticksPerSecond
,ticks
;
98 DWORDLONG ticksShifted
,tickSecShifted
;
101 if ((!fComputeTimeQueryPerf
) || (!QueryPerformanceCounter(&endTime64
)))
102 dwRet
= (GetTickCount() - beginTime64
.LowPart
)*1;
105 MyDoMinus64(&ticks
,endTime64
,beginTime64
);
106 QueryPerformanceFrequency(&ticksPerSecond
);
110 ticksShifted
= Int64ShrlMod32(*(DWORDLONG
*)&ticks
,dwLog
);
111 tickSecShifted
= Int64ShrlMod32(*(DWORDLONG
*)&ticksPerSecond
,dwLog
);
115 dwRet
= (DWORD
)((((DWORD
)ticksShifted
)*1000)/(DWORD
)(tickSecShifted
));
121 int ReadFileMemory(const char* filename
,long* plFileSize
,void** pFilePtr
)
126 stream
=fopen(filename
, "rb");
130 fseek(stream
,0,SEEK_END
);
132 *plFileSize
=ftell(stream
);
133 fseek(stream
,0,SEEK_SET
);
134 ptr
=malloc((*plFileSize
)+1);
139 if (fread(ptr
, 1, *plFileSize
,stream
) != (*plFileSize
))
147 int main(int argc
, char *argv
[])
149 int BlockSizeCompress
=0x8000;
150 int BlockSizeUncompress
=0x8000;
151 int cprLevel
=Z_DEFAULT_COMPRESSION
;
153 unsigned char* FilePtr
;
155 long lBufferSizeUncpr
;
156 long lCompressedSize
=0;
157 unsigned char* CprPtr
;
158 unsigned char* UncprPtr
;
159 long lSizeCpr
,lSizeUncpr
;
160 DWORD dwGetTick
,dwMsecQP
;
161 LARGE_INTEGER li_qp
,li_rdtsc
,dwResRdtsc
;
165 printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]\n");
169 if (ReadFileMemory(argv
[1],&lFileSize
,&FilePtr
)==0)
171 printf("error reading %s\n",argv
[1]);
174 else printf("file %s read, %u bytes\n",argv
[1],lFileSize
);
177 BlockSizeCompress
=atol(argv
[2]);
180 BlockSizeUncompress
=atol(argv
[3]);
183 cprLevel
=(int)atol(argv
[4]);
185 lBufferSizeCpr
= lFileSize
+ (lFileSize
/0x10) + 0x200;
186 lBufferSizeUncpr
= lBufferSizeCpr
;
188 CprPtr
=(unsigned char*)malloc(lBufferSizeCpr
+ BlockSizeCompress
);
190 BeginCountPerfCounter(&li_qp
,TRUE
);
191 dwGetTick
=GetTickCount();
192 BeginCountRdtsc(&li_rdtsc
);
196 long lOrigToDo
= lFileSize
;
199 memset(&zcpr
,0,sizeof(z_stream
));
200 deflateInit(&zcpr
,cprLevel
);
202 zcpr
.next_in
= FilePtr
;
203 zcpr
.next_out
= CprPtr
;
208 long all_read_before
= zcpr
.total_in
;
209 zcpr
.avail_in
= min(lOrigToDo
,BlockSizeCompress
);
210 zcpr
.avail_out
= BlockSizeCompress
;
211 ret
=deflate(&zcpr
,(zcpr
.avail_in
==lOrigToDo
) ? Z_FINISH
: Z_SYNC_FLUSH
);
212 lOrigDone
+= (zcpr
.total_in
-all_read_before
);
213 lOrigToDo
-= (zcpr
.total_in
-all_read_before
);
217 lSizeCpr
=zcpr
.total_out
;
219 dwGetTick
=GetTickCount()-dwGetTick
;
220 dwMsecQP
=GetMsecSincePerfCounter(li_qp
,TRUE
);
221 dwResRdtsc
=GetResRdtsc(li_rdtsc
,TRUE
);
222 printf("total compress size = %u, in %u step\n",lSizeCpr
,step
);
223 printf("time = %u msec = %f sec\n",dwGetTick
,dwGetTick
/(double)1000.);
224 printf("defcpr time QP = %u msec = %f sec\n",dwMsecQP
,dwMsecQP
/(double)1000.);
225 printf("defcpr result rdtsc = %I64x\n\n",dwResRdtsc
.QuadPart
);
228 CprPtr
=(unsigned char*)realloc(CprPtr
,lSizeCpr
);
229 UncprPtr
=(unsigned char*)malloc(lBufferSizeUncpr
+ BlockSizeUncompress
);
231 BeginCountPerfCounter(&li_qp
,TRUE
);
232 dwGetTick
=GetTickCount();
233 BeginCountRdtsc(&li_rdtsc
);
237 long lOrigToDo
= lSizeCpr
;
240 memset(&zcpr
,0,sizeof(z_stream
));
243 zcpr
.next_in
= CprPtr
;
244 zcpr
.next_out
= UncprPtr
;
249 long all_read_before
= zcpr
.total_in
;
250 zcpr
.avail_in
= min(lOrigToDo
,BlockSizeUncompress
);
251 zcpr
.avail_out
= BlockSizeUncompress
;
252 ret
=inflate(&zcpr
,Z_SYNC_FLUSH
);
253 lOrigDone
+= (zcpr
.total_in
-all_read_before
);
254 lOrigToDo
-= (zcpr
.total_in
-all_read_before
);
258 lSizeUncpr
=zcpr
.total_out
;
260 dwGetTick
=GetTickCount()-dwGetTick
;
261 dwMsecQP
=GetMsecSincePerfCounter(li_qp
,TRUE
);
262 dwResRdtsc
=GetResRdtsc(li_rdtsc
,TRUE
);
263 printf("total uncompress size = %u, in %u step\n",lSizeUncpr
,step
);
264 printf("time = %u msec = %f sec\n",dwGetTick
,dwGetTick
/(double)1000.);
265 printf("uncpr time QP = %u msec = %f sec\n",dwMsecQP
,dwMsecQP
/(double)1000.);
266 printf("uncpr result rdtsc = %I64x\n\n",dwResRdtsc
.QuadPart
);
269 if (lSizeUncpr
==lFileSize
)
271 if (memcmp(FilePtr
,UncprPtr
,lFileSize
)==0)
272 printf("compare ok\n");