ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GBase.h
(Generate patch)
# Line 61 | Line 61
61  
62   typedef int32_t int32;
63   typedef uint32_t uint32;
64 + typedef int16_t int16;
65 + typedef uint16_t uint16;
66  
67   typedef unsigned char uchar;
68   typedef unsigned char byte;
# Line 167 | Line 169
169    return a-b;
170   }
171  
172 < int Gstrcmp(char* a, char* b);
172 > int Gstrcmp(const char* a, const char* b, int n=-1);
173   //same as strcmp but doesn't crash on NULL pointers
174  
175 < int Gstricmp(const char* a, const char* b);
175 > int Gstricmp(const char* a, const char* b, int n=-1);
176  
177 < inline void swap(int &arg1, int &arg2){
178 < //arg1 ^= arg2;
179 < //arg2 ^= arg1;
180 < //arg1 ^= arg2;
181 < register int swp=arg1;
182 < arg1=arg2; arg2=swp;
183 < }
182 <
183 < inline void swap(char* &arg1, char* &arg2){ //swap pointers!
184 < register char* swp=arg1;
185 < arg1=arg2; arg2=swp;
186 < }
187 <
188 < inline void swap(uint &arg1, uint &arg2) {
189 <  register uint swp=arg1;
190 <  arg1=arg2; arg2=swp;
191 <  }
192 <
193 < inline void swap(short &arg1, short &arg2) {
194 <  register short swp=arg1;
195 <  arg1=arg2; arg2=swp;
196 <  }
197 <
198 < inline void swap(unsigned short &arg1, unsigned short &arg2) {
199 <  register unsigned short swp=arg1;
200 <  arg1=arg2; arg2=swp;
201 <  }
177 > //basic swap template function
178 > template<class T> void Gswap(T& lhs, T& rhs) {
179 > //register T tmp=lhs;
180 > T tmp=lhs; //requires copy operator
181 > lhs=rhs;
182 > rhs=tmp;
183 > }
184  
185 < inline void swap(long &arg1, long &arg2) {
186 <  register long swp=arg1;
187 <  arg1=arg2; arg2=swp;
185 > /// bitCount_32 - this function counts the number of set bits in a value.
186 > /// Ex. CountPopulation(0xF000F000) = 8
187 > /// Returns 0 if the word is zero.
188 > inline uint bitCount_32(uint32_t Value) {
189 > #if __GNUC__ >= 4
190 >    return __builtin_popcount(Value);
191 > #else
192 >    uint32_t v = Value - ((Value >> 1) & 0x55555555);
193 >    v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
194 >    return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
195 > #endif
196    }
197  
198 < inline void swap(unsigned long &arg1, unsigned long &arg2) {
199 <  register unsigned long swp=arg1;
200 <  arg1=arg2; arg2=swp;
198 > /// bitCount_64 - this function counts the number of set bits in a value,
199 > /// (64 bit edition.)
200 > inline uint bitCount_64(uint64_t Value) {
201 > #if __GNUC__ >= 4
202 >    return __builtin_popcountll(Value);
203 > #else
204 >    uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
205 >    v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
206 >    v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
207 >    return uint((uint64_t)(v * 0x0101010101010101ULL) >> 56);
208 > #endif
209    }
210  
211 + /// CountTrailingZeros_32 - this function performs the platform optimal form of
212 + /// counting the number of zeros from the least significant bit to the first one
213 + /// bit.  Ex. CountTrailingZeros_32(0xFF00FF00) == 8.
214 + /// Returns 32 if the word is zero.
215 + inline unsigned bitCountTrailingZeros_32(uint32_t Value) {
216 + #if __GNUC__ >= 4
217 +  return Value ? __builtin_ctz(Value) : 32;
218 + #else
219 +  static const unsigned Mod37BitPosition[] = {
220 +    32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13,
221 +    4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9,
222 +    5, 20, 8, 19, 18
223 +  };
224 +  return Mod37BitPosition[(-Value & Value) % 37];
225 + #endif
226 + }
227  
228 < inline void swap(char &arg1, char &arg2) {
229 <  register char swp=arg1;
230 <  arg1=arg2; arg2=swp;
231 <  }
232 <
233 < inline void swap(unsigned char &arg1, unsigned char &arg2) {
234 <  register unsigned char swp=arg1;
235 <  arg1=arg2; arg2=swp;
236 <  }
228 > // CountTrailingZeros_64 - This function performs the platform optimal form
229 > /// of counting the number of zeros from the least significant bit to the first
230 > /// one bit (64 bit edition.)
231 > /// Returns 64 if the word is zero.
232 > inline unsigned bitCountTrailingZeros_64(uint64_t Value) {
233 > #if __GNUC__ >= 4
234 >  return Value ? __builtin_ctzll(Value) : 64;
235 > #else
236 >  static const unsigned Mod67Position[] = {
237 >    64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54,
238 >    4, 64, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55,
239 >    47, 5, 32, 65, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27,
240 >    29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56,
241 >    7, 48, 35, 6, 34, 33, 0
242 >  };
243 >  return Mod67Position[(-Value & Value) % 67];
244 > #endif
245 > }
246  
247   /**************** Memory management ***************************/
248  
# Line 330 | Line 353
353       }
354  
355    bool overlap(uint s, uint e) {
356 <     if (s>e) { swap(s,e); }
356 >     if (s>e) { Gswap(s,e); }
357       //return start<s ? (s<=end) : (start<=e);
358       return (start<=e && end>=s);
359       }
# Line 347 | Line 370
370          }
371       }
372    int overlapLen(uint rstart, uint rend) {
373 <     if (rstart>rend) { swap(rstart,rend); }
373 >     if (rstart>rend) { Gswap(rstart,rend); }
374       if (start<rstart) {
375          if (rstart>end) return 0;
376          return (rend>end) ? end-rstart+1 : rend-rstart+1;
# Line 369 | Line 392
392    bool operator==(GSeg& d){
393        return (start==d.start && end==d.end);
394        }
372  bool operator>(GSeg& d){
373     return (start==d.start)?(end>d.end):(start>d.start);
374     }
395    bool operator<(GSeg& d){
396       return (start==d.start)?(end<d.end):(start<d.start);
397       }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines