--- rxvt-unicode/src/rxvtutil.h 2006/01/29 20:51:28 1.21 +++ rxvt-unicode/src/rxvtutil.h 2007/12/04 16:33:42 1.35 @@ -4,11 +4,34 @@ #include #include +using namespace std; + #define PP_CONCAT_(a, b) a ## b #define PP_CONCAT(a, b) PP_CONCAT_(a, b) #define PP_STRINGIFY_(a) #a #define PP_STRINGIFY(a) PP_STRINGIFY_(a) +#define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) + +#if __GNUC__ >= 4 +# define rxvt_attribute(x) __attribute__(x) +#else +# define rxvt_attribute(x) +#endif + +#define NORETURN rxvt_attribute ((noreturn)) +#define UNUSED rxvt_attribute ((unused)) +#define CONST rxvt_attribute ((const)) + +// increases code size unless -fno-enforce-eh-specs +#if __GNUC__ +# define NOTHROW +# define THROW(x) +#else +# define NOTHROW throw() +# define THROW(x) throw x +#endif + extern class byteorder { static unsigned int e; // at least 32 bits public: @@ -31,14 +54,45 @@ template static inline void swap (T& a, U& b) { T t=a; a=(T)b; b=(U)t; } +template static inline T squared_diff (T a, T b) { return (a-b)*(a-b); } + // linear interpolation template static inline T lerp (T a, U b, P p) { - return (int(a) * int(p) + int(b) * int(100 - p)) / 100; + return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; } +template +I find (I first, I last, const T& value) +{ + while (first != last && *first != value) + ++first; + + return first; +} + +// return a very temporary (and never deallocated) buffer. keep small. +void *rxvt_temp_buf (int len); + +template +static inline T * +rxvt_temp_buf (int len) +{ + return (T *)rxvt_temp_buf (len * sizeof (T)); +} + +// some bit functions, xft fuck me plenty +#if HAVE_GCC_BUILTINS +static inline int ctz (unsigned int x) { return __builtin_ctz (x); } +static inline int popcount (unsigned int x) { return __builtin_popcount (x); } +#else +// count trailing zero bits and count # of one bits +int ctz (unsigned int x) CONST; +int popcount (unsigned int x) CONST; +#endif + // in range including end #define IN_RANGE_INC(val,beg,end) \ ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) @@ -54,7 +108,7 @@ }; /* simplevec taken (and heavily modified), from: - * + * * MICO --- a free CORBA implementation * Copyright (C) 1997-98 Kay Roemer & Arno Puder */ @@ -310,7 +364,17 @@ template struct vector : simplevec -{ }; +{ +}; + +struct stringvec : simplevec +{ + ~stringvec () + { + for (char **c = begin (); c != end (); c++) + free (*c); + } +}; #if 0 template @@ -328,15 +392,6 @@ }; #endif -template -I find (I first, I last, const T& value) -{ - while (first != last && *first != value) - ++first; - - return first; -} - template struct auto_ptr { T *p; @@ -397,24 +452,5 @@ typedef auto_ptr auto_str; -struct stringvec : simplevec -{ - ~stringvec () - { - for (char **c = begin (); c != end (); c++) - free (*c); - } -}; - -// return a very temporary (and never deallocated) buffer. keep small. -void *rxvt_temp_buf (int len); - -template -inline T * -rxvt_temp_buf (int len) -{ - return (T *)rxvt_temp_buf (len * sizeof (T)); -} - #endif