8 void MyDoMinus64(LARGE_INTEGER *R,LARGE_INTEGER A,LARGE_INTEGER B)
\r
10 R->HighPart = A.HighPart - B.HighPart;
\r
11 if (A.LowPart >= B.LowPart)
\r
12 R->LowPart = A.LowPart - B.LowPart;
\r
15 R->LowPart = A.LowPart - B.LowPart;
\r
21 // see http://msdn2.microsoft.com/library/twchhe95(en-us,vs.80).aspx for __rdtsc
\r
22 unsigned __int64 __rdtsc(void);
\r
23 void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
\r
25 // printf("rdtsc = %I64x\n",__rdtsc());
\r
26 pbeginTime64->QuadPart=__rdtsc();
\r
29 LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
\r
31 LARGE_INTEGER LIres;
\r
32 unsigned _int64 res=__rdtsc()-((unsigned _int64)(beginTime64.QuadPart));
\r
34 // printf("rdtsc = %I64x\n",__rdtsc());
\r
39 void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
\r
48 pbeginTime64->LowPart=dwEax;
\r
49 pbeginTime64->HighPart=dwEdx;
\r
52 void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
\r
54 myGetRDTSC32(pbeginTime64);
\r
57 LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
\r
59 LARGE_INTEGER LIres,endTime64;
\r
60 myGetRDTSC32(&endTime64);
\r
62 LIres.LowPart=LIres.HighPart=0;
\r
63 MyDoMinus64(&LIres,endTime64,beginTime64);
\r
67 void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
\r
71 void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
\r
75 LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
\r
84 void BeginCountPerfCounter(LARGE_INTEGER * pbeginTime64,BOOL fComputeTimeQueryPerf)
\r
86 if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(pbeginTime64)))
\r
88 pbeginTime64->LowPart = GetTickCount();
\r
89 pbeginTime64->HighPart = 0;
\r
93 DWORD GetMsecSincePerfCounter(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
\r
95 LARGE_INTEGER endTime64,ticksPerSecond,ticks;
\r
96 DWORDLONG ticksShifted,tickSecShifted;
\r
99 if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(&endTime64)))
\r
100 dwRet = (GetTickCount() - beginTime64.LowPart)*1;
\r
103 MyDoMinus64(&ticks,endTime64,beginTime64);
\r
104 QueryPerformanceFrequency(&ticksPerSecond);
\r
108 ticksShifted = Int64ShrlMod32(*(DWORDLONG*)&ticks,dwLog);
\r
109 tickSecShifted = Int64ShrlMod32(*(DWORDLONG*)&ticksPerSecond,dwLog);
\r
113 dwRet = (DWORD)((((DWORD)ticksShifted)*1000)/(DWORD)(tickSecShifted));
\r
119 int ReadFileMemory(const char* filename,long* plFileSize,unsigned char** pFilePtr)
\r
122 unsigned char* ptr;
\r
124 stream=fopen(filename, "rb");
\r
128 fseek(stream,0,SEEK_END);
\r
130 *plFileSize=ftell(stream);
\r
131 fseek(stream,0,SEEK_SET);
\r
132 ptr=malloc((*plFileSize)+1);
\r
137 if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))
\r
145 int main(int argc, char *argv[])
\r
147 int BlockSizeCompress=0x8000;
\r
148 int BlockSizeUncompress=0x8000;
\r
149 int cprLevel=Z_DEFAULT_COMPRESSION ;
\r
151 unsigned char* FilePtr;
\r
152 long lBufferSizeCpr;
\r
153 long lBufferSizeUncpr;
\r
154 long lCompressedSize=0;
\r
155 unsigned char* CprPtr;
\r
156 unsigned char* UncprPtr;
\r
157 long lSizeCpr,lSizeUncpr;
\r
158 DWORD dwGetTick,dwMsecQP;
\r
159 LARGE_INTEGER li_qp,li_rdtsc,dwResRdtsc;
\r
163 printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]\n");
\r
167 if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)
\r
169 printf("error reading %s\n",argv[1]);
\r
172 else printf("file %s read, %u bytes\n",argv[1],lFileSize);
\r
175 BlockSizeCompress=atol(argv[2]);
\r
178 BlockSizeUncompress=atol(argv[3]);
\r
181 cprLevel=(int)atol(argv[4]);
\r
183 lBufferSizeCpr = lFileSize + (lFileSize/0x10) + 0x200;
\r
184 lBufferSizeUncpr = lBufferSizeCpr;
\r
186 CprPtr=(unsigned char*)malloc(lBufferSizeCpr + BlockSizeCompress);
\r
188 BeginCountPerfCounter(&li_qp,TRUE);
\r
189 dwGetTick=GetTickCount();
\r
190 BeginCountRdtsc(&li_rdtsc);
\r
194 long lOrigToDo = lFileSize;
\r
195 long lOrigDone = 0;
\r
197 memset(&zcpr,0,sizeof(z_stream));
\r
198 deflateInit(&zcpr,cprLevel);
\r
200 zcpr.next_in = FilePtr;
\r
201 zcpr.next_out = CprPtr;
\r
206 long all_read_before = zcpr.total_in;
\r
207 zcpr.avail_in = min(lOrigToDo,BlockSizeCompress);
\r
208 zcpr.avail_out = BlockSizeCompress;
\r
209 ret=deflate(&zcpr,(zcpr.avail_in==lOrigToDo) ? Z_FINISH : Z_SYNC_FLUSH);
\r
210 lOrigDone += (zcpr.total_in-all_read_before);
\r
211 lOrigToDo -= (zcpr.total_in-all_read_before);
\r
213 } while (ret==Z_OK);
\r
215 lSizeCpr=zcpr.total_out;
\r
217 dwGetTick=GetTickCount()-dwGetTick;
\r
218 dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
\r
219 dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
\r
220 printf("total compress size = %u, in %u step\n",lSizeCpr,step);
\r
221 printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
\r
222 printf("defcpr time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
\r
223 printf("defcpr result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
\r
226 CprPtr=(unsigned char*)realloc(CprPtr,lSizeCpr);
\r
227 UncprPtr=(unsigned char*)malloc(lBufferSizeUncpr + BlockSizeUncompress);
\r
229 BeginCountPerfCounter(&li_qp,TRUE);
\r
230 dwGetTick=GetTickCount();
\r
231 BeginCountRdtsc(&li_rdtsc);
\r
235 long lOrigToDo = lSizeCpr;
\r
236 long lOrigDone = 0;
\r
238 memset(&zcpr,0,sizeof(z_stream));
\r
239 inflateInit(&zcpr);
\r
241 zcpr.next_in = CprPtr;
\r
242 zcpr.next_out = UncprPtr;
\r
247 long all_read_before = zcpr.total_in;
\r
248 zcpr.avail_in = min(lOrigToDo,BlockSizeUncompress);
\r
249 zcpr.avail_out = BlockSizeUncompress;
\r
250 ret=inflate(&zcpr,Z_SYNC_FLUSH);
\r
251 lOrigDone += (zcpr.total_in-all_read_before);
\r
252 lOrigToDo -= (zcpr.total_in-all_read_before);
\r
254 } while (ret==Z_OK);
\r
256 lSizeUncpr=zcpr.total_out;
\r
258 dwGetTick=GetTickCount()-dwGetTick;
\r
259 dwMsecQP=GetMsecSincePerfCounter(li_qp,TRUE);
\r
260 dwResRdtsc=GetResRdtsc(li_rdtsc,TRUE);
\r
261 printf("total uncompress size = %u, in %u step\n",lSizeUncpr,step);
\r
262 printf("time = %u msec = %f sec\n",dwGetTick,dwGetTick/(double)1000.);
\r
263 printf("uncpr time QP = %u msec = %f sec\n",dwMsecQP,dwMsecQP/(double)1000.);
\r
264 printf("uncpr result rdtsc = %I64x\n\n",dwResRdtsc.QuadPart);
\r
267 if (lSizeUncpr==lFileSize)
\r
269 if (memcmp(FilePtr,UncprPtr,lFileSize)==0)
\r
270 printf("compare ok\n");
\r