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