engine/bits.c

Go to the documentation of this file.
00001 #include "bits.h"
00002 #include <stdio.h>
00003 
00004 /***************************************************
00005 * Retrieve the value of a bit
00006 ***************************************************/
00007 inline int GetBit(unsigned char* BitField, int BitFieldLen, int BitNum){
00008         char    byte;
00009         
00010 #ifdef DEBUGPATH
00011         printf("in GetBit\n");
00012 #endif
00013 
00014         if (BitNum > BitFieldLen-1) return FALSE;
00015 
00016         byte=BitField[BitNum/8];
00017         switch (BitNum%8){
00018         case 0:
00019                 return byte & 0x80;
00020         case 1:
00021                 return byte & 0x40;
00022         case 2:
00023                 return byte & 0x20;
00024         case 3:
00025                 return byte & 0x10;
00026         case 4:
00027                 return byte & 0x08;
00028         case 5:
00029                 return byte & 0x04;
00030         case 6:
00031                 return byte & 0x02;
00032         case 7:
00033                 return byte & 0x01;
00034         }       
00035         
00036         return FALSE;
00037 }
00038 
00039 /***************************************************
00040 * Set the value of a bit
00041 ***************************************************/
00042 inline void SetBit(unsigned char* BitField, int BitFieldLen, int BitNum, char Value){
00043         unsigned char*  byte;
00044 
00045 #ifdef DEBUGPATH
00046         printf("In SetBit\n");
00047 #endif  
00048         
00049         byte=&BitField[BitNum/8];
00050         
00051         if (Value){
00052                 switch (BitNum%8){
00053                 case 0:
00054                         *byte |= 0x80;
00055                         break;
00056                 case 1:
00057                         *byte |= 0x40;
00058                         break;
00059                 case 2:
00060                         *byte |= 0x20;
00061                         break;
00062                 case 3:
00063                         *byte |= 0x10;
00064                         break;
00065                 case 4:
00066                         *byte |= 0x08;
00067                         break;
00068                 case 5:
00069                         *byte |= 0x04;
00070                         break;
00071                 case 6:
00072                         *byte |= 0x02;
00073                         break;
00074                 case 7:
00075                         *byte |= 0x01;
00076                         break;
00077                 }       
00078         }else{
00079                 switch (BitNum%8){
00080                 case 0:
00081                         *byte &= 0x7F;
00082                         break;
00083                 case 1:
00084                         *byte &= 0xBF;
00085                         break;
00086                 case 2:
00087                         *byte &= 0xDF;
00088                         break;
00089                 case 3:
00090                         *byte &= 0xEF;
00091                         break;
00092                 case 4:
00093                         *byte &= 0xF7;
00094                         break;
00095                 case 5:
00096                         *byte &= 0xFB;
00097                         break;
00098                 case 6:
00099                         *byte &= 0xFD;
00100                         break;
00101                 case 7:
00102                         *byte &= 0xFE;
00103                         break;
00104                 }       
00105         }       
00106 }
00107 
00108 /***************************************************
00109 * Set the value of a range of bits
00110 ***************************************************/
00111 inline void SetBits(unsigned char* BitField, int BitFieldLen, int StartBit, int EndBit, char Value){
00112 #ifdef DEBUGPATH
00113         printf("in SetBits\n");
00114 #endif
00115 
00116 }
00117 
00118 /***************************************************
00119 * Calculate the bitwise NOT AND of the two bitfields
00120 ***************************************************/
00121 inline void NotAndBitFields(unsigned char* BitField1, unsigned char* BitField2, unsigned char* TargetBitField, int BitFieldLen){
00122         unsigned int*   IntField1;
00123         unsigned int*   IntField2;
00124         unsigned int*   IntTarget;
00125         register int    i;
00126         register int    len;
00127         
00128 #ifdef DEBUGPATH
00129         printf("in NotAndBitFields\n");
00130 #endif
00131 
00132         IntField1=(unsigned int*)BitField1;
00133         IntField2=(unsigned int*)BitField2;
00134         IntTarget=(unsigned int*)TargetBitField;
00135         
00136         len = (BitFieldLen/32)+1;
00137         for (i=0;i<len;i++)
00138                 IntTarget[i]=(IntField2[i]^0xFFFFFFFF) & IntField1[i];
00139 }
00140 
00141 /***************************************************
00142 * Calculate the bitwise AND of the two bitfields
00143 ***************************************************/
00144 inline void AndBitFields(unsigned char* BitField1, unsigned char* BitField2, unsigned char* TargetBitField, int BitFieldLen){   
00145 #ifdef DEBUGPATH
00146         printf("in AndBitFields\n");
00147 #endif
00148 
00149 }
00150 
00151 /***************************************************
00152 * Calculate the bitwise OR of the two bitfields
00153 ***************************************************/
00154 inline void OrBitFields(unsigned char* BitField1, unsigned char* BitField2, unsigned char* TargetBitField, int BitFieldLen){
00155 #ifdef DEBUGPATH
00156         printf("in OrBitFields\n");
00157 #endif
00158 }
00159 
00160 /***************************************************
00161 * Calculate the bitwise OR of the two bitfields
00162 * This is the slow way.  Finish the faster way later.
00163 ***************************************************/
00164 int     CountBits(unsigned char* BitField, int BitFieldLen){
00165         int     i;
00166         int     count;
00167         
00168 #ifdef DEBUGPATH
00169         printf("In CountBits\n");
00170 #endif
00171 
00172         count=0;
00173         for (i=0;i<BitFieldLen;i++)
00174                 if (GetBit(BitField, BitFieldLen, i)) count++;
00175                 
00176         return count;
00177 }
00178 
00179 /***************************************************
00180 * return the number of bits that are true
00181 * TODO: finish this
00182 ***************************************************/
00183 int     CountBitsNot(unsigned char* BitField, int BitFieldLen){
00184         int i;
00185         int     count;
00186         
00187 #ifdef DEBUGPATH
00188         printf("In CountBits\n");
00189 #endif
00190         
00191         count = 0;
00192         for (i=0;i<(BitFieldLen/8);i++){
00193                 switch (BitField[i]){
00194                 case 0:
00195                         count+=0;
00196                         break;
00197                 case 1:
00198                 case 2:
00199                 case 4:
00200                 case 8:
00201                 case 16:
00202                 case 32:
00203                 case 64:
00204                 case 128:
00205                         count+=1;
00206                         break;
00207                 case 3:
00208                 case 5:
00209                 case 6:
00210                 case 9:
00211                 case 10:
00212                 case 12:
00213                 case 17:
00214                 case 18:
00215                 case 20:
00216                 case 24:
00217                 case 33:
00218                 case 34:
00219                 case 36:
00220                 case 40:
00221                 case 65:
00222                 case 129:
00223                         count+=2;
00224                         break;
00225                 case 7: 
00226                 case 11:
00227                 case 13:
00228                 case 14:
00229                 case 19:
00230                 case 21:
00231                 case 22:        
00232                 case 25:
00233                 case 26:
00234                 case 28:
00235                 case 35:
00236                 case 37:
00237                 case 38:        
00238                 case 41:
00239                 case 42:
00240                 case 44:
00241                         count+=3;
00242                         break;  
00243                 case 15:
00244                 case 23:
00245                 case 27:
00246                 case 29:
00247                 case 30:
00248                 case 39:
00249                 case 43:
00250                         count+=4;
00251                         break;
00252                 case 31:
00253                         count+=5;       
00254                 }
00255         }
00256 
00257         return count;
00258 }
00259 
00260 
00261 /************************************************************
00262 * Returns true if a bitfield is empty
00263 *************************************************************/
00264 int BitFieldIsEmpty(unsigned char* BitField, int BitFieldLen){
00265         int*    Field;
00266         int             i,j;
00267         
00268 #ifdef DEBUGPATH
00269         printf("In BitFieldIsEmpty\n");
00270 #endif
00271 
00272         Field=(int*)BitField;
00273 
00274         for (i=0;i<BitFieldLen/32;i++){
00275                 if (Field[i]!=0) return FALSE;
00276         }
00277         
00278         for (j=i*32;j<BitFieldLen;j++){
00279                 if (GetBit(BitField, BitFieldLen, j)) return FALSE;
00280         }
00281         
00282         return TRUE;
00283 }

Generated on Sat Jul 7 23:33:10 2007 for HLBR by  doxygen 1.5.2