… | |
… | |
11 | #define PP_STRINGIFY_(a) #a |
11 | #define PP_STRINGIFY_(a) #a |
12 | #define PP_STRINGIFY(a) PP_STRINGIFY_(a) |
12 | #define PP_STRINGIFY(a) PP_STRINGIFY_(a) |
13 | |
13 | |
14 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
14 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
15 | |
15 | |
16 | #ifndef __attribute__ |
|
|
17 | # if __GNUC__ |
16 | #if __GNUC__ >= 4 |
18 | # if (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || (__GNUC__ < 2) |
17 | # define rxvt_attribute(x) __attribute__(x) |
19 | # define __attribute__(x) |
18 | #else |
20 | # endif |
|
|
21 | # endif |
|
|
22 | # define __attribute__(x) |
19 | # define rxvt_attribute(x) |
23 | #endif |
20 | #endif |
24 | |
21 | |
25 | #define NORETURN __attribute__ ((noreturn)) |
22 | #define NORETURN rxvt_attribute ((noreturn)) |
26 | #define UNUSED __attribute__ ((unused)) |
23 | #define UNUSED rxvt_attribute ((unused)) |
27 | #define CONST __attribute__ ((const)) |
24 | #define CONST rxvt_attribute ((const)) |
28 | |
25 | |
29 | // increases code size unless -fno-enforce-eh-specs |
26 | // increases code size unless -fno-enforce-eh-specs |
30 | #if __GNUC__ |
27 | #if __GNUC__ |
31 | # define NOTHROW |
28 | # define NOTHROW |
32 | # define THROW(x) |
29 | # define THROW(x) |
… | |
… | |
65 | T lerp (T a, U b, P p) |
62 | T lerp (T a, U b, P p) |
66 | { |
63 | { |
67 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
64 | return (long(a) * long(100 - p) + long(b) * long(p) + 50) / 100; |
68 | } |
65 | } |
69 | |
66 | |
|
|
67 | template <typename I, typename T> |
|
|
68 | I find (I first, I last, const T& value) |
|
|
69 | { |
|
|
70 | while (first != last && *first != value) |
|
|
71 | ++first; |
|
|
72 | |
|
|
73 | return first; |
|
|
74 | } |
|
|
75 | |
|
|
76 | // return a very temporary (and never deallocated) buffer. keep small. |
|
|
77 | void *rxvt_temp_buf (int len); |
|
|
78 | |
|
|
79 | template<typename T> |
|
|
80 | static inline T * |
|
|
81 | rxvt_temp_buf (int len) |
|
|
82 | { |
|
|
83 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
|
|
84 | } |
|
|
85 | |
70 | // some bit functions, xft fuck me plenty |
86 | // some bit functions, xft fuck me plenty |
71 | #if HAVE_GCC_BUILTINS |
87 | #if HAVE_GCC_BUILTINS |
72 | static inline int ctz (unsigned int x) CONST { return __builtin_ctz (x); } |
88 | static inline int ctz (unsigned int x) { return __builtin_ctz (x); } |
73 | static inline int popcount (unsigned int x) CONST { return __builtin_popcount (x); } |
89 | static inline int popcount (unsigned int x) { return __builtin_popcount (x); } |
74 | #else |
90 | #else |
75 | // count trailing zero bits and count # of one bits |
91 | // count trailing zero bits and count # of one bits |
76 | int ctz (unsigned int x) CONST; |
92 | int ctz (unsigned int x) CONST; |
77 | int popcount (unsigned int x) CONST; |
93 | int popcount (unsigned int x) CONST; |
78 | #endif |
94 | #endif |
… | |
… | |
82 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
98 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
83 | |
99 | |
84 | // in range excluding end |
100 | // in range excluding end |
85 | #define IN_RANGE_EXC(val,beg,end) \ |
101 | #define IN_RANGE_EXC(val,beg,end) \ |
86 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
102 | ((unsigned int)(val) - (unsigned int)(beg) < (unsigned int)(end) - (unsigned int)(beg)) |
|
|
103 | |
|
|
104 | // for m >= -n, ensure remainder lies between 0..n-1 |
|
|
105 | #define MOD(m,n) (((m) + (n)) % (n)) |
87 | |
106 | |
88 | // makes dynamically allocated objects zero-initialised |
107 | // makes dynamically allocated objects zero-initialised |
89 | struct zero_initialized { |
108 | struct zero_initialized { |
90 | void *operator new (size_t s); |
109 | void *operator new (size_t s); |
91 | void operator delete (void *p, size_t s); |
110 | void operator delete (void *p, size_t s); |
… | |
… | |
346 | } |
365 | } |
347 | |
366 | |
348 | |
367 | |
349 | template<typename T> |
368 | template<typename T> |
350 | struct vector : simplevec<T> |
369 | struct vector : simplevec<T> |
351 | { }; |
370 | { |
|
|
371 | }; |
|
|
372 | |
|
|
373 | struct stringvec : simplevec<char *> |
|
|
374 | { |
|
|
375 | ~stringvec () |
|
|
376 | { |
|
|
377 | for (char **c = begin (); c != end (); c++) |
|
|
378 | free (*c); |
|
|
379 | } |
|
|
380 | }; |
352 | |
381 | |
353 | #if 0 |
382 | #if 0 |
354 | template<typename T> |
383 | template<typename T> |
355 | struct rxvt_vec : simplevec<void *> { |
384 | struct rxvt_vec : simplevec<void *> { |
356 | typedef T *iterator; |
385 | typedef T *iterator; |
… | |
… | |
364 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
393 | T &operator [] (int i) { return * (T *) (& ((* (simplevec<void *> *)this)[i])); } |
365 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
394 | const T &operator [] (int i) const { return * (const T *) (& ((* (const simplevec<void *> *)this)[i])); } |
366 | }; |
395 | }; |
367 | #endif |
396 | #endif |
368 | |
397 | |
369 | template <typename I, typename T> |
|
|
370 | I find (I first, I last, const T& value) |
|
|
371 | { |
|
|
372 | while (first != last && *first != value) |
|
|
373 | ++first; |
|
|
374 | |
|
|
375 | return first; |
|
|
376 | } |
|
|
377 | |
|
|
378 | template<typename T> |
398 | template<typename T> |
379 | struct auto_ptr { |
399 | struct auto_ptr { |
380 | T *p; |
400 | T *p; |
381 | |
401 | |
382 | auto_ptr () : p (0) { } |
402 | auto_ptr () : p (0) { } |
… | |
… | |
433 | } |
453 | } |
434 | }; |
454 | }; |
435 | |
455 | |
436 | typedef auto_ptr<char> auto_str; |
456 | typedef auto_ptr<char> auto_str; |
437 | |
457 | |
438 | struct stringvec : simplevec<char *> |
|
|
439 | { |
|
|
440 | ~stringvec () |
|
|
441 | { |
|
|
442 | for (char **c = begin (); c != end (); c++) |
|
|
443 | free (*c); |
|
|
444 | } |
|
|
445 | }; |
|
|
446 | |
|
|
447 | // return a very temporary (and never deallocated) buffer. keep small. |
|
|
448 | void *rxvt_temp_buf (int len); |
|
|
449 | |
|
|
450 | template<typename T> |
|
|
451 | static inline T * |
|
|
452 | rxvt_temp_buf (int len) |
|
|
453 | { |
|
|
454 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
|
|
455 | } |
|
|
456 | |
|
|
457 | #endif |
458 | #endif |
458 | |
459 | |