メインページ | アルファベット順一覧 | 構成 | ファイル一覧 | 構成メンバ | ファイルメンバ | 関連ページ

# dkcBit.h

説明を見る。

    00001
    00008 #ifndef DKUTIL_C_BIT_H
    00009 #define DKUTIL_C_BIT_H
    00010
    00011 #include "dkcMemoryStream.h"
    00012 #include "dkcSJISFileSystem.h"
    00013
    00014 #define DKUTIL_C_USE_BIT_TABLE 0
    00015
    00016
    00021 union dkc_NLZ8Byte{
    00022     uint32 asINT_[2];
    00023     double asDouble_;
    00024 };
    00028 union dkc_NLZ4Byte{
    00029     uint32 asINT_;
    00030     float asFloat;
    00031 };
    00032
    00034 //ハッカーの楽しみ p.86 図5-12より
    00035 //NumberOfLeadingZeros
    00036 DKC_FORCE_INLINE size_t dkcNLZ_IEEE(uint32 arg){
    00037     union dkc_NLZ8Byte t;
    00038     size_t n;
    00039
    00040     t.asDouble_ = (double)arg + 0.5;
    00041     n = 1054 - (t.asINT_[dkcdLITTLE_ENDIAN] >> 20);
    00042     return n;
    00043 }
    00045 DKC_FORCE_INLINE size_t /*dkcNLZ(unsigned x){/*/dkcNLZ(uint32 x){
    00046     int y,m,n;
    00047
    00048     y = -(int)(x >> 16);
    00049     m = (y >> 16) & 16;
    00050     n = 16 - m;
    00051     x = x >> m;
    00052
    00053     y = x - 0x100;
    00054     m = (y >> 16) & 8;
    00055     n = n + m;
    00056     x = x << m;
    00057
    00058     y = x - 0x1000;
    00059     m = (y >> 16) & 4;
    00060     n = n + m;
    00061     x = x << m;
    00062
    00063     y = x >> 14;
    00064     m = y & ~(y >> 1);
    00065     return n + 2 - m;
    00066 }
    00067
    00068
    00072
    00073
    00074 DKC_FORCE_INLINE size_t dkcPOP(uint32 /*bits/*/x){
    00075     /*
    00076   bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    00077   bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    00078   bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    00079   bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    00080   return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
    00081     */
    00082     x = x - ((x >> 1) & 0x55555555);
    00083     x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
    00084     x = (x + (x >> 4)) & 0x0f0f0f0f;
    00085     x = x + (x >> 8);
    00086     x = x + (x >> 16);
    00087     return x & 0x0000003f;
    00088 }
    00090 #define dkcNumOfBits( bits) dkcPOP(bits)
    00091 #define dkcBitCount(bits) dkcPOP(bits)
    00092
    00094 DKC_FORCE_INLINE size_t dkcNTZ(uint32 x){
    00095     return 32 - dkcNTZ(~x&(x-1));
    00096
    00097 }
    00098
    00099
    00100 #if DKUTIL_C_USE_BIT_TABLE == 1
    00101 DKUTIL_EXTERN const uint8 dkccvBitTable8[8];
    00102 DKUTIL_EXTERN const uint32 dkccvBitTable16[16];
    00103 DKUTIL_EXTERN const uint32 dkccvBitTable32[32];
    00104 DKUTIL_EXTERN const uint64 dkccvBitTable64[64]
    00105 #endif
    00106
    00107
    00108 enum edkcBitMemoryStream{
    00110     edkcBitMemoryStreamSeekCurrent = edkcSeekCurrent,
    00112     edkcBitMemoryStreamSeekEnd = edkcSeekEnd,
    00114     edkcBitMemoryStreamSeekSet = edkcSeekSet,
    00115 };
    00116
    00117
    00119 typedef struct dkc_BitMemoryStream{
    00120     int read_count;
    00121     size_t read_buff;
    00122     int write_count;
    00123     size_t write_buff;
    00124     DKC_MEMORYSTREAM *pms;
    00125 }DKC_BIT_MEMORYSTREAM;
    00126
    00127 DKC_INLINE DKC_BIT_MEMORYSTREAM *dkcAllocBitMemoryStream(size_t def_size)
    00128 {
    00129     DKC_BIT_MEMORYSTREAM *p = (DKC_BIT_MEMORYSTREAM *)dkcAllocate(sizeof(DKC_BIT_MEMORYSTREAM));
    00130     if(NULL==p) return NULL;
    00131     p->pms = dkcAllocMemoryStream(def_size);
    00132     if(NULL==p->pms)
    00133         goto End;
    00134
    00135     p->write_count = 8;
    00136
    00137     return p;
    00138 End:
    00139     dkcFree((void **)&p);
    00140     return NULL;
    00141 }
    00142
    00143 DKC_INLINE int dkcFreeBitMemoryStream(DKC_BIT_MEMORYSTREAM **p)
    00144 {
    00145     dkcmNOT_ASSERT(NULL==p || NULL==*p || NULL==(*p)->pms);
    00146     dkcFreeMemoryStream(&((*p)->pms));
    00147     return dkcFree((void **)p);
    00148 }
    00152 DKC_INLINE int dkcBitMemoryStreamWrite(DKC_BIT_MEMORYSTREAM *p,void *target_arg,int n)
    00153 {
    00154     int wc = p->write_count;
    00155     size_t wb = p->write_buff;
    00156     //save point
    00157     size_t sp = dkcMemoryStreamGetSeekPoint(p->pms);
    00158     uint32 *target =(uint32 *) target_arg;
    00159     if( n <= 0 || n >= 32 ) return edk_FAILED;
    00160     while( --n >= 0 ){
    00161         --wc;
    00162 #if DKUTIL_C_USE_BIT_TABLE == 1
    00163         if( dkccvBitTable32[n] & target ){
    00164             wb |= dkccvBitTable8[wc];
    00165         }
    00166 #else
    00167         if( (1<<n) & (*target) ){
    00168             wb |= (1 << wc);//dkccvBitTable8[wc];
    00169         }
    00170 #endif
    00171         if( !wc ){
    00172             if( DKUTIL_FAILED(dkcMemoryStreamDynamicWrite(p->pms, &wb,1 )) )
    00173             {
    00174                 //元の状態に戻す
    00175                 dkcMemoryStreamSeek(p->pms,sp,edkcMemoryStreamSeekSet);
    00176                 return edk_FAILED;
    00177             }
    00178             wc = 8;
    00179             wb = 0;
    00180         }
    00181     }
    00182     /*if( !wc ){
    00183         if( DKUTIL_FAILED(dkcMemoryStreamDynamicWrite(p->pms, &wb,1 )) )
    00184         {
    00185             //元の状態に戻す
    00186             dkcMemoryStreamSeek(p->pms,sp,edkcMemoryStreamSeekSet);
    00187             return edk_FAILED;
    00188         }
    00189         wc = 8;
    00190         wb = 0;
    00191     }*/
    00192     p->write_count = wc;
    00193     p->write_buff = wb;
    00194     return edk_SUCCEEDED;
    00195
    00196 }
    00197
    00198 DKC_INLINE int dkcBitMemoryStreamWriteLast(DKC_BIT_MEMORYSTREAM *p){
    00199     //return dkcMemoryStreamWrite(p->pms,&p->write_buff,1);
    00200     size_t sp = dkcMemoryStreamGetSeekPoint(p->pms);
    00201     int r = dkcMemoryStreamDynamicWrite(p->pms, &p->write_buff,1 );
    00202     if( DKUTIL_FAILED(r) )
    00203     {
    00204         //元の状態に戻す
    00205         dkcMemoryStreamSeek(p->pms,sp,edkcMemoryStreamSeekSet);
    00206         return r;
    00207     }
    00208     p->write_count = 8;
    00209     p->write_buff = 0;
    00210     return r;
    00211 }
    00212
    00213 #if 0
    00214
    00218 //#error unsignedなので--rcとかはエラー write read 両方を確認
    00219 DKC_INLINE int dkcBitMemoryStreamRead(DKC_BIT_MEMORYSTREAM *p,void *res_arg,int n)
    00220 {
    00221     int rc = p->read_count;
    00222     size_t rb = p->read_buff;
    00223     size_t result = 0;
    00224 //  size_t rs = 0;
    00225     //save point
    00226     size_t sp = dkcMemoryStreamGetSeekPoint(p->pms);
    00227     uint32 *res = (uint32 *)res_arg;
    00228     if( n <= 0 || n >= 32 ) return edk_FAILED;
    00229     while( --n >= 0 ){
    00230         if( --rc < 0 ){
    00231
    00232             //if( DKUTIL_FAILED(dkcMemoryStreamRead(p->pms,&rb,1,&rs)) ){
    00233             if( DKUTIL_FAILED(dkcMemoryStreamGet8(p->pms,(uint8 *)&rb))){
    00234                 //元の状態に戻す
    00235                 dkcMemoryStreamSeek(p->pms,sp,edkcMemoryStreamSeekSet);
    00236                 return edk_FAILED;
    00237             }
    00238             rc = 7;
    00239         }
    00240 #if DKUTIL_C_USE_BIT_TABLE == 1
    00241         if( dkccvBitTable8[rc] & rb ){
    00242             result |= dkccvBitTable32[n];
    00243         }
    00244 #else
    00245         if( (1 << rc) & rb){
    00246             result |= (1 << n);
    00247         }
    00248 #endif
    00249     }
    00250     *res = result;
    00251     p->read_count = rc;
    00252     p->read_buff = rb;
    00253     return edk_SUCCEEDED;
    00254
    00255
    00256 }
    00257
    00258 #else
    00259
    00260 DKC_INLINE int dkcBitMemoryStreamReadBase(DKC_BIT_MEMORYSTREAM *p,void *res_arg,int n,BOOL as_possible_as_much,int *pcount)
    00261 {
    00262     int rc = p->read_count;
    00263     size_t rb = p->read_buff;
    00264     size_t result = 0;
    00265     int cc = 0;//counter
    00266 //  size_t rs = 0;
    00267     //save point
    00268     size_t sp = dkcMemoryStreamGetSeekPoint(p->pms);
    00269     uint32 *res = (uint32 *)res_arg;
    00270     if( n <= 0 || n >= 32 ) return edk_FAILED;
    00271     {
    00272         while( --n >= 0 ){
    00273             cc++;
    00274             if( --rc < 0 ){
    00275
    00276                 //if( DKUTIL_FAILED(dkcMemoryStreamRead(p->pms,&rb,1,&rs)) ){
    00277                 if( DKUTIL_FAILED(dkcMemoryStreamGet8(p->pms,(uint8 *)&rb))){
    00278                     if(as_possible_as_much)//がんばって出来るだけ読み込んだよ。
    00279                         goto End;
    00280                     //元の状態に戻す
    00281                     dkcMemoryStreamSeek(p->pms,sp,edkcMemoryStreamSeekSet);
    00282                     return edk_FAILED;
    00283                 }
    00284                 rc = 7;
    00285             }
    00286     #if DKUTIL_C_USE_BIT_TABLE == 1
    00287             if( dkccvBitTable8[rc] & rb ){
    00288                 result |= dkccvBitTable32[n];
    00289             }
    00290     #else
    00291             if( (1 << rc) & rb){
    00292                 result |= (1 << n);
    00293             }
    00294     #endif
    00295         }
    00296     End:
    00297         *res = result;
    00298         p->read_count = rc;
    00299         p->read_buff = rb;
    00300         *pcount = cc;
    00301     }
    00302     return edk_SUCCEEDED;
    00303 }
    00304 DKC_INLINE int dkcBitMemoryStreamRead(DKC_BIT_MEMORYSTREAM *p,void *res_arg,int n)
    00305 {
    00306     int ressize;
    00307     return dkcBitMemoryStreamReadBase(p,res_arg,n,FALSE,&ressize);
    00308 }
    00309
    00310 #endif
    00311
    00313 DKC_INLINE int WINAPI dkcBitMemoryStreamReadLast(DKC_BIT_MEMORYSTREAM *p,uint32 *res,int *ressize)
    00314 {
    00315     if(p->read_count <= 0){
    00317         return dkcBitMemoryStreamReadBase(p,
    00318             res,32,TRUE,ressize);
    00319     }
    00320     *res = p->read_buff;
    00321     *ressize = p->read_count;
    00322     p->read_count = p->read_buff = 0;
    00323     return edk_SUCCEEDED;
    00324 }
    00325
    00326 /*DKC_FORCE_INLINE void WINAPI dkcBitMemoryStreamInit(DKC_BIT_MEMORYSTREAM *ptr){
    00327 }*/
    00328 #define dkcBitMemoryStreamGetMemoryStreamObj(p) p->pms
    00329
    00330 DKC_INLINE int WINAPI dkcBitMemoryStreamSeekByte(DKC_BIT_MEMORYSTREAM *ptr,int offset,int origin)
    00331 {
    00332     ptr->read_count =   ptr->read_buff = ptr->write_buff = 0;
    00333
    00334     ptr->write_count = 8;
    00335     return dkcMemoryStreamSeek(ptr->pms,offset,origin);
    00336 }
    00337 //#define dkcBitMemoryStreamSeekByte(ptr,offset,origin) dkcMemoryStreamSeek(ptr->pms,offset,origin)
    00339 #define dkcBitMemoryStreamTellByte(ptr) dkcMemoryStreamTell(ptr->pms)
    00340
    00341 DKC_INLINE int WINAPI dkcBitMemoryStreamTellBitCheck(DKC_BIT_MEMORYSTREAM *ptr){
    00342     size_t t = dkcBitMemoryStreamTellByte(ptr);
    00343     if(t > UINT_MAX / 8){
    00344         return edk_VariableOverFlow;
    00345     }
    00346     return edk_SUCCEEDED;
    00347 }
    00349 DKC_INLINE size_t WINAPI dkcBitMemoryStreamTellBit(DKC_BIT_MEMORYSTREAM *ptr,size_t *bit)
    00350 {
    00351     size_t t = dkcBitMemoryStreamTellByte(ptr);
    00352     dkcmNOT_ASSERT(t > UINT_MAX / 8);
    00353     return t * 8;
    00354 }
    00356 #define dkcBitMemoryStreamWriteToMemory(ptr,dest,size,flag) dkcMemoryStreamWriteToMemory(ptr->pms,dest,size,flag)
    00357
    00358 #define dkcBitMemoryStreamWriteToMemoryLast(ptr,dest,size,flag) dkcBitMemoryStreamWriteLast(ptr);dkcBitMemoryStreamWriteToMemory(ptr,dest,size,flag)
    00359
    00360 #define dkcBitMemoryStreamDump(ptr,filename,flag) dkcMemoryStreamDump(ptr->pms,filename,flag)
    00361
    00362 #define dkcBitMemoryStreamLoadFromFile(ptr,filename,permit_size,flag) dkcMemoryStreamLoadFromFile(ptr->pms,filename,permit_size,flag)
    00363
    00364 #define dkcBitMemoryStreamLoadFromMemory(ptr,buffer,buffsize) dkcMemoryStreamLoadFromMemory(ptr->pms,buffer,buffsize)
    00365
    00366
    00367 /*
    00368 DKC_FORCE_INLINE int dkcBitMemoryStreamLoadFormMemory(DKC_BIT_MEMORYSTREAM *ptr,const void *buffer,
    00369                                                                                                             size_t buffsize)
    00370 {
    00371
    00372
    00373
    00374 }
    00375 */
    00376
    00377
    00378 /*
    00379 @param flag[in] edkcStreamBufferAll または edkcStreamBufferFirst
    00380 */
    00381 /*
    00382 DKC_FORCE_INLINE int dkcBitMemoryStreamLoadFromFile(DKC_BIT_MEMORYSTREAM *ptr,const char *filename,size_t permit_size,UINT flag){
    00383
    00384
    00385     size_t size;
    00386     //許容サイズよりファイルサイズが大きい条件の結果をc1に入れる
    00387     int c1 ;
    00388     int r = edk_FAILED;
    00389     size_t readsize = 0;
    00390     size_t size_temp = 0;
    00391
    00392     if(FALSE==dkcFileExist(filename))
    00393         return edk_FileNotFound;
    00394
    00395
    00396     size  = dkcFileSize(filename);
    00397     c1 = (permit_size < size);
    00398     switch(flag){
    00399     case edkcStreamBufferAll:
    00400         if(c1){
    00401             return edk_FAILED;
    00402         }
    00403         size_temp = size;
    00404         break;
    00405     case edkcStreamBufferFirst:
    00406
    00407         if(c1){
    00408             size_temp = permit_size;
    00409         }else{
    00410             size_temp = size;
    00411         }
    00412         break;
    00413     default:
    00414         return edk_ArgumentException;
    00415     }
    00416
    00417
    00418     r = dkcMemoryStreamResize(ptr->pms,size_temp);
    00419     if(DKUTIL_FAILED(r)) goto End;
    00420     r = dkcLoadBinary(ptr->pms->mBuffer,size_temp,filename,&readsize);
    00421     if(DKUTIL_FAILED(r)) goto End;
    00422
    00423 End:
    00424     return r;
    00425
    00426 }
    00427 */
    00428
    00429
    00430
    00431 #endif //end of include once