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