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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines