--- rxvt-unicode/src/rxvtutil.h 2006/01/29 22:27:04 1.22 +++ rxvt-unicode/src/rxvtutil.h 2011/01/03 18:52:41 1.44 @@ -3,26 +3,58 @@ #include #include +#include + +using namespace std; + +#define ARRAY_LENGTH(v) (sizeof (v) / sizeof ((v)[0])) #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) -// actually, some gcc-3.x versions work, too -#define HAVE_GCC_BUILTINS (__GNUC__ >= 4) -#define HAVE_GCC_BUILTINS 0 +#define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) -extern class byteorder { - static unsigned int e; // at least 32 bits -public: - byteorder (); +#if __GNUC__ >= 4 +# define rxvt_attribute(x) __attribute__(x) +# define expect(expr,value) __builtin_expect ((expr),(value)) +#else +# define rxvt_attribute(x) +# define expect(expr,value) (expr) +#endif - static bool big_endian () { return e == 0x11223344; }; - static bool network () { return e == 0x11223344; }; - static bool little_endian () { return e == 0x44332211; }; - static bool vax () { return e == 0x44332211; }; -} byteorder; +// put into ifs if you are very sure that the expression +// is mostly true or mostly false. note that these return +// booleans, not the expression. +#define expect_false(expr) expect ((expr) != 0, 0) +#define expect_true(expr) expect ((expr) != 0, 1) + +#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 + +namespace byteorder { + static unsigned char e () + { + const uint32_t u = 0x11223344; + return *(unsigned char *)&u; + } + + static bool big_endian () { return e () == 0x11; }; + static bool network () { return big_endian (); }; + static bool little_endian () { return e () == 0x44; }; + static bool vax () { return little_endian (); }; +}; // various utility functions template static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } @@ -35,22 +67,44 @@ 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); } +/* netbsd stupidly defines popcount itself and puts it into string.h */ +static inline int rxvt_ctz (unsigned int x) { return __builtin_ctz (x); } +static inline int rxvt_popcount (unsigned int x) { return __builtin_popcount (x); } #else // count trailing zero bits and count # of one bits -int ctz (unsigned int x); -int popcount (unsigned int x); +int rxvt_ctz (unsigned int x) CONST; +int rxvt_popcount (unsigned int x) CONST; #endif // in range including end @@ -61,19 +115,24 @@ #define IN_RANGE_EXC(val,beg,end) \ ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) +// for m >= -n, ensure remainder lies between 0..n-1 +#define MOD(m,n) (((m) + (n)) % (n)) + // makes dynamically allocated objects zero-initialised -struct zero_initialized { +struct zero_initialized +{ void *operator new (size_t s); void operator delete (void *p, size_t s); }; /* simplevec taken (and heavily modified), from: - * + * * MICO --- a free CORBA implementation * Copyright (C) 1997-98 Kay Roemer & Arno Puder */ template -struct simplevec { +struct simplevec +{ typedef T* iterator; typedef const T* const_iterator; typedef unsigned long size_type; @@ -324,11 +383,22 @@ template struct vector : simplevec -{ }; +{ +}; + +struct stringvec : simplevec +{ + ~stringvec () + { + for (char **c = begin (); c != end (); c++) + free (*c); + } +}; #if 0 template -struct rxvt_vec : simplevec { +struct rxvt_vec : simplevec +{ typedef T *iterator; void push_back (T d) { simplevec::push_back ((void *)d); } @@ -342,17 +412,9 @@ }; #endif -template -I find (I first, I last, const T& value) -{ - while (first != last && *first != value) - ++first; - - return first; -} - template -struct auto_ptr { +struct auto_ptr +{ T *p; auto_ptr () : p (0) { } @@ -411,24 +473,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