… | |
… | |
2 | #define RXVT_UTIL_H |
2 | #define RXVT_UTIL_H |
3 | |
3 | |
4 | #include <cstdlib> |
4 | #include <cstdlib> |
5 | #include <cstring> |
5 | #include <cstring> |
6 | #include <inttypes.h> |
6 | #include <inttypes.h> |
|
|
7 | #include "ecb.h" |
7 | |
8 | |
8 | using namespace std; |
9 | using namespace std; |
9 | |
|
|
10 | #define ARRAY_LENGTH(v) (sizeof (v) / sizeof ((v)[0])) |
|
|
11 | |
|
|
12 | #define PP_CONCAT_(a, b) a ## b |
|
|
13 | #define PP_CONCAT(a, b) PP_CONCAT_(a, b) |
|
|
14 | #define PP_STRINGIFY_(a) #a |
|
|
15 | #define PP_STRINGIFY(a) PP_STRINGIFY_(a) |
|
|
16 | |
|
|
17 | #define HAVE_GCC_BUILTINS (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ == 4)) |
|
|
18 | |
|
|
19 | #if __GNUC__ >= 4 |
|
|
20 | # define rxvt_attribute(x) __attribute__(x) |
|
|
21 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
22 | #else |
|
|
23 | # define rxvt_attribute(x) |
|
|
24 | # define expect(expr,value) (expr) |
|
|
25 | #endif |
|
|
26 | |
|
|
27 | // put into ifs if you are very sure that the expression |
|
|
28 | // is mostly true or mostly false. note that these return |
|
|
29 | // booleans, not the expression. |
|
|
30 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
31 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
32 | |
|
|
33 | #define NORETURN rxvt_attribute ((noreturn)) |
|
|
34 | #define UNUSED rxvt_attribute ((unused)) |
|
|
35 | #define CONST rxvt_attribute ((const)) |
|
|
36 | |
10 | |
37 | // increases code size unless -fno-enforce-eh-specs |
11 | // increases code size unless -fno-enforce-eh-specs |
38 | #if __GNUC__ |
12 | #if __GNUC__ |
39 | # define NOTHROW |
13 | # define NOTHROW |
40 | # define THROW(x) |
14 | # define THROW(x) |
41 | #else |
15 | #else |
42 | # define NOTHROW throw() |
16 | # define NOTHROW throw() |
43 | # define THROW(x) throw x |
17 | # define THROW(x) throw x |
44 | #endif |
18 | #endif |
45 | |
19 | |
46 | namespace byteorder { |
|
|
47 | static unsigned char e () |
|
|
48 | { |
|
|
49 | const uint32_t u = 0x11223344; |
|
|
50 | return *(unsigned char *)&u; |
|
|
51 | } |
|
|
52 | |
|
|
53 | static bool big_endian () { return e () == 0x11; }; |
|
|
54 | static bool network () { return big_endian (); }; |
|
|
55 | static bool little_endian () { return e () == 0x44; }; |
|
|
56 | static bool vax () { return little_endian (); }; |
|
|
57 | }; |
|
|
58 | |
|
|
59 | // various utility functions |
20 | // various utility functions |
60 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
21 | template<typename T, typename U> static inline T min (T a, U b) { return a < (T)b ? a : (T)b; } |
61 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
22 | template<typename T, typename U> static inline void min_it (T &a, U b) { a = a < (T)b ? a : (T)b; } |
62 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
23 | template<typename T, typename U> static inline T max (T a, U b) { return a > (T)b ? a : (T)b; } |
63 | template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } |
24 | template<typename T, typename U> static inline void max_it (T &a, U b) { a = a > (T)b ? a : (T)b; } |
… | |
… | |
93 | static inline T * |
54 | static inline T * |
94 | rxvt_temp_buf (int len) |
55 | rxvt_temp_buf (int len) |
95 | { |
56 | { |
96 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
57 | return (T *)rxvt_temp_buf (len * sizeof (T)); |
97 | } |
58 | } |
98 | |
|
|
99 | // some bit functions, xft fuck me plenty |
|
|
100 | #if HAVE_GCC_BUILTINS |
|
|
101 | /* netbsd stupidly defines popcount itself and puts it into string.h */ |
|
|
102 | static inline int rxvt_ctz (unsigned int x) { return __builtin_ctz (x); } |
|
|
103 | static inline int rxvt_popcount (unsigned int x) { return __builtin_popcount (x); } |
|
|
104 | #else |
|
|
105 | // count trailing zero bits and count # of one bits |
|
|
106 | int rxvt_ctz (unsigned int x) CONST; |
|
|
107 | int rxvt_popcount (unsigned int x) CONST; |
|
|
108 | #endif |
|
|
109 | |
59 | |
110 | // in range including end |
60 | // in range including end |
111 | #define IN_RANGE_INC(val,beg,end) \ |
61 | #define IN_RANGE_INC(val,beg,end) \ |
112 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
62 | ((unsigned int)(val) - (unsigned int)(beg) <= (unsigned int)(end) - (unsigned int)(beg)) |
113 | |
63 | |