]>
Commit | Line | Data |
---|---|---|
5ca28f79 L |
1 | #include <stdio.h>\r |
2 | #include <stdlib.h>\r | |
3 | #include <windows.h>\r | |
4 | \r | |
5 | #include "zlib.h"\r | |
6 | \r | |
7 | \r | |
8 | void MyDoMinus64(LARGE_INTEGER *R,LARGE_INTEGER A,LARGE_INTEGER B)\r | |
9 | {\r | |
10 | R->HighPart = A.HighPart - B.HighPart;\r | |
11 | if (A.LowPart >= B.LowPart)\r | |
12 | R->LowPart = A.LowPart - B.LowPart;\r | |
13 | else\r | |
14 | {\r | |
15 | R->LowPart = A.LowPart - B.LowPart;\r | |
16 | R->HighPart --;\r | |
17 | }\r | |
18 | }\r | |
19 | \r | |
20 | #ifdef _M_X64\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 | |
24 | {\r | |
25 | // printf("rdtsc = %I64x\n",__rdtsc());\r | |
26 | pbeginTime64->QuadPart=__rdtsc();\r | |
27 | }\r | |
28 | \r | |
29 | LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)\r | |
30 | {\r | |
31 | LARGE_INTEGER LIres;\r | |
32 | unsigned _int64 res=__rdtsc()-((unsigned _int64)(beginTime64.QuadPart));\r | |
33 | LIres.QuadPart=res;\r | |
34 | // printf("rdtsc = %I64x\n",__rdtsc());\r | |
35 | return LIres;\r | |
36 | }\r | |
37 | #else\r | |
38 | #ifdef _M_IX86\r | |
39 | void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)\r | |
40 | {\r | |
41 | DWORD dwEdx,dwEax;\r | |
42 | _asm\r | |
43 | {\r | |
44 | rdtsc\r | |
45 | mov dwEax,eax\r | |
46 | mov dwEdx,edx\r | |
47 | }\r | |
48 | pbeginTime64->LowPart=dwEax;\r | |
49 | pbeginTime64->HighPart=dwEdx;\r | |
50 | }\r | |
51 | \r | |
52 | void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)\r | |
53 | {\r | |
54 | myGetRDTSC32(pbeginTime64);\r | |
55 | }\r | |
56 | \r | |
57 | LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)\r | |
58 | {\r | |
59 | LARGE_INTEGER LIres,endTime64;\r | |
60 | myGetRDTSC32(&endTime64);\r | |
61 | \r | |
62 | LIres.LowPart=LIres.HighPart=0;\r | |
63 | MyDoMinus64(&LIres,endTime64,beginTime64);\r | |
64 | return LIres;\r | |
65 | }\r | |
66 | #else\r | |
67 | void myGetRDTSC32(LARGE_INTEGER * pbeginTime64)\r | |
68 | {\r | |
69 | }\r | |
70 | \r | |
71 | void BeginCountRdtsc(LARGE_INTEGER * pbeginTime64)\r | |
72 | {\r | |
73 | }\r | |
74 | \r | |
75 | LARGE_INTEGER GetResRdtsc(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)\r | |
76 | {\r | |
77 | LARGE_INTEGER lr;\r | |
78 | lr.QuadPart=0;\r | |
79 | return lr;\r | |
80 | }\r | |
81 | #endif\r | |
82 | #endif\r | |
83 | \r | |
84 | void BeginCountPerfCounter(LARGE_INTEGER * pbeginTime64,BOOL fComputeTimeQueryPerf)\r | |
85 | {\r | |
86 | if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(pbeginTime64)))\r | |
87 | {\r | |
88 | pbeginTime64->LowPart = GetTickCount();\r | |
89 | pbeginTime64->HighPart = 0;\r | |
90 | }\r | |
91 | }\r | |
92 | \r | |
93 | DWORD GetMsecSincePerfCounter(LARGE_INTEGER beginTime64,BOOL fComputeTimeQueryPerf)\r | |
94 | {\r | |
95 | LARGE_INTEGER endTime64,ticksPerSecond,ticks;\r | |
96 | DWORDLONG ticksShifted,tickSecShifted;\r | |
97 | DWORD dwLog=16+0;\r | |
98 | DWORD dwRet;\r | |
99 | if ((!fComputeTimeQueryPerf) || (!QueryPerformanceCounter(&endTime64)))\r | |
100 | dwRet = (GetTickCount() - beginTime64.LowPart)*1;\r | |
101 | else\r | |
102 | {\r | |
103 | MyDoMinus64(&ticks,endTime64,beginTime64);\r | |
104 | QueryPerformanceFrequency(&ticksPerSecond);\r | |
105 | \r | |
106 | \r | |
107 | {\r | |
108 | ticksShifted = Int64ShrlMod32(*(DWORDLONG*)&ticks,dwLog);\r | |
109 | tickSecShifted = Int64ShrlMod32(*(DWORDLONG*)&ticksPerSecond,dwLog);\r | |
110 | \r | |
111 | }\r | |
112 | \r | |
113 | dwRet = (DWORD)((((DWORD)ticksShifted)*1000)/(DWORD)(tickSecShifted));\r | |
114 | dwRet *=1;\r | |
115 | }\r | |
116 | return dwRet;\r | |
117 | }\r | |
118 | \r | |
da09a436 | 119 | int ReadFileMemory(const char* filename,long* plFileSize,unsigned char** pFilePtr)\r |
5ca28f79 L |
120 | {\r |
121 | FILE* stream;\r | |
da09a436 | 122 | unsigned char* ptr;\r |
5ca28f79 L |
123 | int retVal=1;\r |
124 | stream=fopen(filename, "rb");\r | |
125 | if (stream==NULL)\r | |
126 | return 0;\r | |
127 | \r | |
128 | fseek(stream,0,SEEK_END);\r | |
129 | \r | |
130 | *plFileSize=ftell(stream);\r | |
131 | fseek(stream,0,SEEK_SET);\r | |
132 | ptr=malloc((*plFileSize)+1);\r | |
133 | if (ptr==NULL)\r | |
134 | retVal=0;\r | |
135 | else\r | |
136 | {\r | |
137 | if (fread(ptr, 1, *plFileSize,stream) != (*plFileSize))\r | |
138 | retVal=0;\r | |
139 | }\r | |
140 | fclose(stream);\r | |
141 | *pFilePtr=ptr;\r | |
142 | return retVal;\r | |
143 | }\r | |
144 | \r | |
145 | int main(int argc, char *argv[])\r | |
146 | {\r | |
147 | int BlockSizeCompress=0x8000;\r | |
148 | int BlockSizeUncompress=0x8000;\r | |
149 | int cprLevel=Z_DEFAULT_COMPRESSION ;\r | |
150 | long lFileSize;\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 | |
160 | \r | |
161 | if (argc<=1)\r | |
162 | {\r | |
163 | printf("run TestZlib <File> [BlockSizeCompress] [BlockSizeUncompress] [compres. level]\n");\r | |
164 | return 0;\r | |
165 | }\r | |
166 | \r | |
167 | if (ReadFileMemory(argv[1],&lFileSize,&FilePtr)==0)\r | |
168 | {\r | |
169 | printf("error reading %s\n",argv[1]);\r | |
170 | return 1;\r | |
171 | }\r | |
172 | else printf("file %s read, %u bytes\n",argv[1],lFileSize);\r | |
173 | \r | |
174 | if (argc>=3)\r | |
175 | BlockSizeCompress=atol(argv[2]);\r | |
176 | \r | |
177 | if (argc>=4)\r | |
178 | BlockSizeUncompress=atol(argv[3]);\r | |
179 | \r | |
180 | if (argc>=5)\r | |
181 | cprLevel=(int)atol(argv[4]);\r | |
182 | \r | |
183 | lBufferSizeCpr = lFileSize + (lFileSize/0x10) + 0x200;\r | |
184 | lBufferSizeUncpr = lBufferSizeCpr;\r | |
185 | \r | |
186 | CprPtr=(unsigned char*)malloc(lBufferSizeCpr + BlockSizeCompress);\r | |
187 | \r | |
188 | BeginCountPerfCounter(&li_qp,TRUE);\r | |
189 | dwGetTick=GetTickCount();\r | |
190 | BeginCountRdtsc(&li_rdtsc);\r | |
191 | {\r | |
192 | z_stream zcpr;\r | |
193 | int ret=Z_OK;\r | |
194 | long lOrigToDo = lFileSize;\r | |
195 | long lOrigDone = 0;\r | |
196 | int step=0;\r | |
197 | memset(&zcpr,0,sizeof(z_stream));\r | |
198 | deflateInit(&zcpr,cprLevel);\r | |
199 | \r | |
200 | zcpr.next_in = FilePtr;\r | |
201 | zcpr.next_out = CprPtr;\r | |
202 | \r | |
203 | \r | |
204 | do\r | |
205 | {\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 | |
212 | step++;\r | |
213 | } while (ret==Z_OK);\r | |
214 | \r | |
215 | lSizeCpr=zcpr.total_out;\r | |
216 | deflateEnd(&zcpr);\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 | |
224 | }\r | |
225 | \r | |
226 | CprPtr=(unsigned char*)realloc(CprPtr,lSizeCpr);\r | |
227 | UncprPtr=(unsigned char*)malloc(lBufferSizeUncpr + BlockSizeUncompress);\r | |
228 | \r | |
229 | BeginCountPerfCounter(&li_qp,TRUE);\r | |
230 | dwGetTick=GetTickCount();\r | |
231 | BeginCountRdtsc(&li_rdtsc);\r | |
232 | {\r | |
233 | z_stream zcpr;\r | |
234 | int ret=Z_OK;\r | |
235 | long lOrigToDo = lSizeCpr;\r | |
236 | long lOrigDone = 0;\r | |
237 | int step=0;\r | |
238 | memset(&zcpr,0,sizeof(z_stream));\r | |
239 | inflateInit(&zcpr);\r | |
240 | \r | |
241 | zcpr.next_in = CprPtr;\r | |
242 | zcpr.next_out = UncprPtr;\r | |
243 | \r | |
244 | \r | |
245 | do\r | |
246 | {\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 | |
253 | step++;\r | |
254 | } while (ret==Z_OK);\r | |
255 | \r | |
256 | lSizeUncpr=zcpr.total_out;\r | |
257 | inflateEnd(&zcpr);\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 | |
265 | }\r | |
266 | \r | |
267 | if (lSizeUncpr==lFileSize)\r | |
268 | {\r | |
269 | if (memcmp(FilePtr,UncprPtr,lFileSize)==0)\r | |
270 | printf("compare ok\n");\r | |
271 | \r | |
272 | }\r | |
273 | \r | |
274 | return 0;\r | |
275 | }\r |