Remove building with NOCRYPTO option
[minix.git] / common / dist / zlib / contrib / testzlib / testzlib.c
blobe131aa9d76288ba0df85564b72aae1e8ccf193cb
1 /* $NetBSD: testzlib.c,v 1.1.1.1 2006/01/14 20:11:02 christos Exp $ */
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <windows.h>
7 #include "zlib.h"
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;
15 else
17 R->LowPart = A.LowPart - B.LowPart;
18 R->HighPart --;
22 #ifdef _M_X64
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)
33 LARGE_INTEGER LIres;
34 unsigned _int64 res=__rdtsc()-((unsigned _int64)(beginTime64.QuadPart));
35 LIres.QuadPart=res;
36 // printf("rdtsc = %I64x\n",__rdtsc());
37 return LIres;
39 #else
40 #ifdef _M_IX86
41 void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
43 DWORD dwEdx,dwEax;
44 _asm
46 rdtsc
47 mov dwEax,eax
48 mov dwEdx,edx
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);
66 return LIres;
68 #else
69 void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)
73 void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)
77 LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)
79 LARGE_INTEGER lr;
80 lr.QuadPart=0;
81 return lr;
83 #endif
84 #endif
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;
99 DWORD dwLog=16+0;
100 DWORD dwRet;
101 if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(&endTime64)))
102 dwRet = (GetTickCount() - beginTime64.LowPart)*1;
103 else
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));
116 dwRet *=1;
118 return dwRet;
121 int ReadFileMemory(const char* filename,long* plFileSize,void** pFilePtr)
123 FILE* stream;
124 void* ptr;
125 int retVal=1;
126 stream=fopen(filename, "rb");
127 if (stream==NULL)
128 return 0;
130 fseek(stream,0,SEEK_END);
132 *plFileSize=ftell(stream);
133 fseek(stream,0,SEEK_SET);
134 ptr=malloc((*plFileSize)+1);
135 if (ptr==NULL)
136 retVal=0;
137 else
139 if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))
140 retVal=0;
142 fclose(stream);
143 *pFilePtr=ptr;
144 return retVal;
147 int main(int argc, char *argv[])
149 int BlockSizeCompress=0x8000;
150 int BlockSizeUncompress=0x8000;
151 int cprLevel=Z_DEFAULT_COMPRESSION ;
152 long lFileSize;
153 unsigned char* FilePtr;
154 long lBufferSizeCpr;
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;
163 if (argc<=1)
165 printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]\n");
166 return 0;
169 if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)
171 printf("error reading %s\n",argv[1]);
172 return 1;
174 else printf("file %s read, %u bytes\n",argv[1],lFileSize);
176 if (argc>=3)
177 BlockSizeCompress=atol(argv[2]);
179 if (argc>=4)
180 BlockSizeUncompress=atol(argv[3]);
182 if (argc>=5)
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);
194 z_stream zcpr;
195 int ret=Z_OK;
196 long lOrigToDo = lFileSize;
197 long lOrigDone = 0;
198 int step=0;
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);
214 step++;
215 } while (ret==Z_OK);
217 lSizeCpr=zcpr.total_out;
218 deflateEnd(&zcpr);
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);
235 z_stream zcpr;
236 int ret=Z_OK;
237 long lOrigToDo = lSizeCpr;
238 long lOrigDone = 0;
239 int step=0;
240 memset(&zcpr,0,sizeof(z_stream));
241 inflateInit(&zcpr);
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);
255 step++;
256 } while (ret==Z_OK);
258 lSizeUncpr=zcpr.total_out;
259 inflateEnd(&zcpr);
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");
276 return 0;