ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/gclib/gclib/GBase.h
(Generate patch)
# Line 167 | Line 167
167    return a-b;
168   }
169  
170 < int Gstrcmp(char* a, char* b);
170 > int Gstrcmp(const char* a, const char* b, int n=-1);
171   //same as strcmp but doesn't crash on NULL pointers
172  
173 < int Gstricmp(const char* a, const char* b);
173 > int Gstricmp(const char* a, const char* b, int n=-1);
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 <  }
197 <
198 < inline void swap(unsigned short &arg1, unsigned short &arg2) {
199 <  register unsigned short swp=arg1;
200 <  arg1=arg2; arg2=swp;
201 <  }
175 > //basic swap template function
176 > template<class T> void Gswap(T& lhs, T& rhs) {
177 > //register T tmp=lhs;
178 > T tmp=lhs; //requires copy operator
179 > lhs=rhs;
180 > rhs=tmp;
181 > }
182  
183 < inline void swap(long &arg1, long &arg2) {
184 <  register long swp=arg1;
185 <  arg1=arg2; arg2=swp;
183 > /// bitCount_32 - this function counts the number of set bits in a value.
184 > /// Ex. CountPopulation(0xF000F000) = 8
185 > /// Returns 0 if the word is zero.
186 > inline uint bitCount_32(uint32_t Value) {
187 > #if __GNUC__ >= 4
188 >    return __builtin_popcount(Value);
189 > #else
190 >    uint32_t v = Value - ((Value >> 1) & 0x55555555);
191 >    v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
192 >    return ((v + (v >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
193 > #endif
194    }
195  
196 < inline void swap(unsigned long &arg1, unsigned long &arg2) {
197 <  register unsigned long swp=arg1;
198 <  arg1=arg2; arg2=swp;
196 > /// bitCount_64 - this function counts the number of set bits in a value,
197 > /// (64 bit edition.)
198 > inline uint bitCount_64(uint64_t Value) {
199 > #if __GNUC__ >= 4
200 >    return __builtin_popcountll(Value);
201 > #else
202 >    uint64_t v = Value - ((Value >> 1) & 0x5555555555555555ULL);
203 >    v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
204 >    v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
205 >    return uint((uint64_t)(v * 0x0101010101010101ULL) >> 56);
206 > #endif
207    }
208  
209 + /// CountTrailingZeros_32 - this function performs the platform optimal form of
210 + /// counting the number of zeros from the least significant bit to the first one
211 + /// bit.  Ex. CountTrailingZeros_32(0xFF00FF00) == 8.
212 + /// Returns 32 if the word is zero.
213 + inline unsigned bitCountTrailingZeros_32(uint32_t Value) {
214 + #if __GNUC__ >= 4
215 +  return Value ? __builtin_ctz(Value) : 32;
216 + #else
217 +  static const unsigned Mod37BitPosition[] = {
218 +    32, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13,
219 +    4, 7, 17, 0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9,
220 +    5, 20, 8, 19, 18
221 +  };
222 +  return Mod37BitPosition[(-Value & Value) % 37];
223 + #endif
224 + }
225  
226 < inline void swap(char &arg1, char &arg2) {
227 <  register char swp=arg1;
228 <  arg1=arg2; arg2=swp;
229 <  }
230 <
231 < inline void swap(unsigned char &arg1, unsigned char &arg2) {
232 <  register unsigned char swp=arg1;
233 <  arg1=arg2; arg2=swp;
234 <  }
226 > // CountTrailingZeros_64 - This function performs the platform optimal form
227 > /// of counting the number of zeros from the least significant bit to the first
228 > /// one bit (64 bit edition.)
229 > /// Returns 64 if the word is zero.
230 > inline unsigned bitCountTrailingZeros_64(uint64_t Value) {
231 > #if __GNUC__ >= 4
232 >  return Value ? __builtin_ctzll(Value) : 64;
233 > #else
234 >  static const unsigned Mod67Position[] = {
235 >    64, 0, 1, 39, 2, 15, 40, 23, 3, 12, 16, 59, 41, 19, 24, 54,
236 >    4, 64, 13, 10, 17, 62, 60, 28, 42, 30, 20, 51, 25, 44, 55,
237 >    47, 5, 32, 65, 38, 14, 22, 11, 58, 18, 53, 63, 9, 61, 27,
238 >    29, 50, 43, 46, 31, 37, 21, 57, 52, 8, 26, 49, 45, 36, 56,
239 >    7, 48, 35, 6, 34, 33, 0
240 >  };
241 >  return Mod67Position[(-Value & Value) % 67];
242 > #endif
243 > }
244  
245   /**************** Memory management ***************************/
246  
# Line 274 | Line 295
295   char* strchrs(const char* s, const char* chrs);
296   //strchr but with a set of chars instead of only one
297  
298 < char* rstrfind(const char* str, const char *substr); /* like rindex() but for strings
299 < or like the right side version of strstr()
300 < */
301 < //reverse character string or
281 < char* reverseChars(char* str, int slen=0);
298 > char* rstrfind(const char* str, const char *substr);
299 > // like rindex() but for strings;  right side version of strstr()
300 >
301 > char* reverseChars(char* str, int slen=0); //in place reversal of string
302  
303   char* rstrstr(const char* rstart, const char *lend, const char* substr);
304   /*the reversed, rightside equivalent of strstr: starts searching
# Line 331 | Line 351
351       }
352  
353    bool overlap(uint s, uint e) {
354 <     if (s>e) { swap(s,e); }
354 >     if (s>e) { Gswap(s,e); }
355       //return start<s ? (s<=end) : (start<=e);
356       return (start<=e && end>=s);
357       }
# Line 348 | Line 368
368          }
369       }
370    int overlapLen(uint rstart, uint rend) {
371 <     if (rstart>rend) { swap(rstart,rend); }
371 >     if (rstart>rend) { Gswap(rstart,rend); }
372       if (start<rstart) {
373          if (rstart>end) return 0;
374          return (rend>end) ? end-rstart+1 : rend-rstart+1;
# Line 370 | Line 390
390    bool operator==(GSeg& d){
391        return (start==d.start && end==d.end);
392        }
373  bool operator>(GSeg& d){
374     return (start==d.start)?(end>d.end):(start>d.start);
375     }
393    bool operator<(GSeg& d){
394       return (start==d.start)?(end<d.end):(start<d.start);
395       }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines