|
|
1 | =head1 LIBECB - e-C-Builtins |
|
|
2 | |
1 | =head1 LIBECB |
3 | =head2 ABOUT LIBECB |
2 | |
4 | |
3 | You suck, we don't(tm) |
5 | Libecb is currently a simple header file that doesn't require any |
|
|
6 | configuration to use or include in your project. |
|
|
7 | |
|
|
8 | It's part of the e-suite of libraries, other members of which include |
|
|
9 | libev and libeio. |
|
|
10 | |
|
|
11 | Its homepage can be found here: |
|
|
12 | |
|
|
13 | http://software.schmorp.de/pkg/libecb |
|
|
14 | |
|
|
15 | It mainly provides a number of wrappers around GCC built-ins, together |
|
|
16 | with replacement functions for other compilers. In addition to this, |
|
|
17 | it provides a number of other lowlevel C utilities, such as endianness |
|
|
18 | detection, byte swapping or bit rotations. |
|
|
19 | |
|
|
20 | Or in other words, things that should be built-in into any standard C |
|
|
21 | system, but aren't. |
|
|
22 | |
|
|
23 | More might come. |
4 | |
24 | |
5 | =head2 ABOUT THE HEADER |
25 | =head2 ABOUT THE HEADER |
6 | |
26 | |
7 | - how to include it |
27 | At the moment, all you have to do is copy F<ecb.h> somewhere where your |
8 | - it includes inttypes.h |
28 | compiler can find it and include it: |
9 | - no .a |
29 | |
10 | - whats a bool |
30 | #include <ecb.h> |
11 | - function mean macro or function |
31 | |
12 | - macro means untyped |
32 | The header should work fine for both C and C++ compilation, and gives you |
|
|
33 | all of F<inttypes.h> in addition to the ECB symbols. |
|
|
34 | |
|
|
35 | There are currently no object files to link to - future versions might |
|
|
36 | come with an (optional) object code library to link against, to reduce |
|
|
37 | code size or gain access to additional features. |
|
|
38 | |
|
|
39 | It also currently includes everything from F<inttypes.h>. |
|
|
40 | |
|
|
41 | =head2 ABOUT THIS MANUAL / CONVENTIONS |
|
|
42 | |
|
|
43 | This manual mainly describes each (public) function available after |
|
|
44 | including the F<ecb.h> header. The header might define other symbols than |
|
|
45 | these, but these are not part of the public API, and not supported in any |
|
|
46 | way. |
|
|
47 | |
|
|
48 | When the manual mentions a "function" then this could be defined either as |
|
|
49 | as inline function, a macro, or an external symbol. |
|
|
50 | |
|
|
51 | When functions use a concrete standard type, such as C<int> or |
|
|
52 | C<uint32_t>, then the corresponding function works only with that type. If |
|
|
53 | only a generic name is used (C<expr>, C<cond>, C<value> and so on), then |
|
|
54 | the corresponding function relies on C to implement the correct types, and |
|
|
55 | is usually implemented as a macro. Specifically, a "bool" in this manual |
|
|
56 | refers to any kind of boolean value, not a specific type. |
13 | |
57 | |
14 | =head2 GCC ATTRIBUTES |
58 | =head2 GCC ATTRIBUTES |
15 | |
59 | |
16 | blabla where to put, what others |
60 | A major part of libecb deals with GCC attributes. These are additional |
|
|
61 | attributes that you cna assign to functions, variables and sometimes even |
|
|
62 | types - much like C<const> or C<volatile> in C. |
|
|
63 | |
|
|
64 | While GCC allows declarations to show up in many surprising places, |
|
|
65 | but not in many expeted places, the safest way is to put attribute |
|
|
66 | declarations before the whole declaration: |
|
|
67 | |
|
|
68 | ecb_const int mysqrt (int a); |
|
|
69 | ecb_unused int i; |
|
|
70 | |
|
|
71 | For variables, it is often nicer to put the attribute after the name, and |
|
|
72 | avoid multiple declarations using commas: |
|
|
73 | |
|
|
74 | int i ecb_unused; |
17 | |
75 | |
18 | =over 4 |
76 | =over 4 |
19 | |
77 | |
20 | =item ecb_attribute ((attrs...)) |
78 | =item ecb_attribute ((attrs...)) |
21 | |
79 | |
22 | A simple wrapper that expands to C<__attribute__((attrs))> on GCC, and |
80 | A simple wrapper that expands to C<__attribute__((attrs))> on GCC, and to |
23 | to nothing on other compilers, so the effect is that only GCC sees these. |
81 | nothing on other compilers, so the effect is that only GCC sees these. |
|
|
82 | |
|
|
83 | Example: use the C<deprecated> attribute on a function. |
|
|
84 | |
|
|
85 | ecb_attribute((__deprecated__)) void |
|
|
86 | do_not_use_me_anymore (void); |
24 | |
87 | |
25 | =item ecb_unused |
88 | =item ecb_unused |
26 | |
89 | |
27 | Marks a function or a variable as "unused", which simply suppresses a |
90 | Marks a function or a variable as "unused", which simply suppresses a |
28 | warning by GCC when it detects it as unused. This is useful when you e.g. |
91 | warning by GCC when it detects it as unused. This is useful when you e.g. |
29 | declare a variable but do not always use it: |
92 | declare a variable but do not always use it: |
30 | |
93 | |
31 | { |
94 | { |
32 | int var ecb_unused; |
95 | int var ecb_unused; |
33 | |
96 | |
34 | #ifdef SOMECONDITION |
97 | #ifdef SOMECONDITION |
35 | var = ...; |
98 | var = ...; |
36 | return var; |
99 | return var; |
37 | #else |
100 | #else |
38 | return 0; |
101 | return 0; |
39 | #endif |
102 | #endif |
40 | } |
103 | } |
41 | |
104 | |
42 | =item ecb_noinline |
105 | =item ecb_noinline |
43 | |
106 | |
44 | Prevent a function from being inlined - it might be optimised away, but |
107 | Prevent a function from being inlined - it might be optimised away, but |
45 | not inlined into other functions. This is useful if you know your function |
108 | not inlined into other functions. This is useful if you know your function |
46 | is rarely called and large enough for inlining not to be helpful. |
109 | is rarely called and large enough for inlining not to be helpful. |
47 | |
110 | |
48 | =item ecb_noreturn |
111 | =item ecb_noreturn |
49 | |
112 | |
|
|
113 | Marks a function as "not returning, ever". Some typical functions that |
|
|
114 | don't return are C<exit> or C<abort> (which really works hard to not |
|
|
115 | return), and now you can make your own: |
|
|
116 | |
|
|
117 | ecb_noreturn void |
|
|
118 | my_abort (const char *errline) |
|
|
119 | { |
|
|
120 | puts (errline); |
|
|
121 | abort (); |
|
|
122 | } |
|
|
123 | |
|
|
124 | In this case, the compiler would probably be smart enough to deduce it on |
|
|
125 | its own, so this is mainly useful for declarations. |
|
|
126 | |
50 | =item ecb_const |
127 | =item ecb_const |
51 | |
128 | |
|
|
129 | Declares that the function only depends on the values of its arguments, |
|
|
130 | much like a mathematical function. It specifically does not read or write |
|
|
131 | any memory any arguments might point to, global variables, or call any |
|
|
132 | non-const functions. It also must not have any side effects. |
|
|
133 | |
|
|
134 | Such a function can be optimised much more aggressively by the compiler - |
|
|
135 | for example, multiple calls with the same arguments can be optimised into |
|
|
136 | a single call, which wouldn't be possible if the compiler would have to |
|
|
137 | expect any side effects. |
|
|
138 | |
|
|
139 | It is best suited for functions in the sense of mathematical functions, |
|
|
140 | such as a function returning the square root of its input argument. |
|
|
141 | |
|
|
142 | Not suited would be a function that calculates the hash of some memory |
|
|
143 | area you pass in, prints some messages or looks at a global variable to |
|
|
144 | decide on rounding. |
|
|
145 | |
|
|
146 | See C<ecb_pure> for a slightly less restrictive class of functions. |
|
|
147 | |
52 | =item ecb_pure |
148 | =item ecb_pure |
53 | |
149 | |
|
|
150 | Similar to C<ecb_const>, declares a function that has no side |
|
|
151 | effects. Unlike C<ecb_const>, the function is allowed to examine global |
|
|
152 | variables and any other memory areas (such as the ones passed to it via |
|
|
153 | pointers). |
|
|
154 | |
|
|
155 | While these functions cannot be optimised as aggressively as C<ecb_const> |
|
|
156 | functions, they can still be optimised away in many occasions, and the |
|
|
157 | compiler has more freedom in moving calls to them around. |
|
|
158 | |
|
|
159 | Typical examples for such functions would be C<strlen> or C<memcmp>. A |
|
|
160 | function that calculates the MD5 sum of some input and updates some MD5 |
|
|
161 | state passed as argument would I<NOT> be pure, however, as it would modify |
|
|
162 | some memory area that is not the return value. |
|
|
163 | |
54 | =item ecb_hot |
164 | =item ecb_hot |
55 | |
165 | |
|
|
166 | This declares a function as "hot" with regards to the cache - the function |
|
|
167 | is used so often, that it is very beneficial to keep it in the cache if |
|
|
168 | possible. |
|
|
169 | |
|
|
170 | The compiler reacts by trying to place hot functions near to each other in |
|
|
171 | memory. |
|
|
172 | |
|
|
173 | Whether a function is hot or not often depends on the whole program, |
|
|
174 | and less on the function itself. C<ecb_cold> is likely more useful in |
|
|
175 | practise. |
|
|
176 | |
56 | =item ecb_cold |
177 | =item ecb_cold |
57 | |
178 | |
|
|
179 | The opposite of C<ecb_hot> - declares a function as "cold" with regards to |
|
|
180 | the cache, or in other words, this function is not called often, or not at |
|
|
181 | speed-critical times, and keeping it in the cache might be a waste of said |
|
|
182 | cache. |
|
|
183 | |
|
|
184 | In addition to placing cold functions together (or at least away from hot |
|
|
185 | functions), this knowledge can be used in other ways, for example, the |
|
|
186 | function will be optimised for size, as opposed to speed, and codepaths |
|
|
187 | leading to calls to those functions can automatically be marked as if |
|
|
188 | C<ecb_unlikely> had been used to reach them. |
|
|
189 | |
|
|
190 | Good examples for such functions would be error reporting functions, or |
|
|
191 | functions only called in exceptional or rare cases. |
|
|
192 | |
58 | =item ecb_artificial |
193 | =item ecb_artificial |
59 | |
194 | |
|
|
195 | Declares the function as "artificial", in this case meaning that this |
|
|
196 | function is not really mean to be a function, but more like an accessor |
|
|
197 | - many methods in C++ classes are mere accessor functions, and having a |
|
|
198 | crash reported in such a method, or single-stepping through them, is not |
|
|
199 | usually so helpful, especially when it's inlined to just a few instructions. |
|
|
200 | |
|
|
201 | Marking them as artificial will instruct the debugger about just this, |
|
|
202 | leading to happier debugging and thus happier lives. |
|
|
203 | |
|
|
204 | Example: in some kind of smart-pointer class, mark the pointer accessor as |
|
|
205 | artificial, so that the whole class acts more like a pointer and less like |
|
|
206 | some C++ abstraction monster. |
|
|
207 | |
|
|
208 | template<typename T> |
|
|
209 | struct my_smart_ptr |
|
|
210 | { |
|
|
211 | T *value; |
|
|
212 | |
|
|
213 | ecb_artificial |
|
|
214 | operator T *() |
|
|
215 | { |
|
|
216 | return value; |
|
|
217 | } |
|
|
218 | }; |
|
|
219 | |
60 | =back |
220 | =back |
61 | |
221 | |
62 | =head2 OPTIMISATION HINTS |
222 | =head2 OPTIMISATION HINTS |
63 | |
223 | |
64 | =over 4 |
224 | =over 4 |
65 | |
225 | |
66 | =item bool ecb_is_constant(expr) [MACRO] |
226 | =item bool ecb_is_constant(expr) |
67 | |
227 | |
68 | Returns true iff the expression can be deduced to be a compile-time |
228 | Returns true iff the expression can be deduced to be a compile-time |
69 | constant, and false otherwise. |
229 | constant, and false otherwise. |
70 | |
230 | |
71 | For example, when you have a C<rndm16> function that returns a 16 bit |
231 | For example, when you have a C<rndm16> function that returns a 16 bit |
… | |
… | |
89 | return is_constant (n) && !(n & (n - 1)) |
249 | return is_constant (n) && !(n & (n - 1)) |
90 | ? rndm16 () & (num - 1) |
250 | ? rndm16 () & (num - 1) |
91 | : (n * (uint32_t)rndm16 ()) >> 16; |
251 | : (n * (uint32_t)rndm16 ()) >> 16; |
92 | } |
252 | } |
93 | |
253 | |
94 | =item bool ecb_expect (expr, value) [MACRO] |
254 | =item bool ecb_expect (expr, value) |
95 | |
255 | |
96 | Evaluates C<expr> and returns it. In addition, it tells the compiler that |
256 | Evaluates C<expr> and returns it. In addition, it tells the compiler that |
97 | the C<expr> evaluates to C<value> a lot, which can be used for static |
257 | the C<expr> evaluates to C<value> a lot, which can be used for static |
98 | branch optimisations. |
258 | branch optimisations. |
99 | |
259 | |
100 | Usually, you want to use the more intuitive C<ecb_likely> and |
260 | Usually, you want to use the more intuitive C<ecb_likely> and |
101 | C<ecb_unlikely> functions instead. |
261 | C<ecb_unlikely> functions instead. |
102 | |
262 | |
103 | =item bool ecb_likely (bool) [MACRO] |
263 | =item bool ecb_likely (cond) |
104 | |
264 | |
105 | =item bool ecb_unlikely (bool) [MACRO] |
265 | =item bool ecb_unlikely (cond) |
106 | |
266 | |
107 | These two functions expect a expression that is true or false and return |
267 | These two functions expect a expression that is true or false and return |
108 | C<1> or C<0>, respectively, so when used in the condition of an C<if> or |
268 | C<1> or C<0>, respectively, so when used in the condition of an C<if> or |
109 | other conditional statement, it will not change the program: |
269 | other conditional statement, it will not change the program: |
110 | |
270 | |
… | |
… | |
140 | { |
300 | { |
141 | if (ecb_unlikely (current + size > end)) |
301 | if (ecb_unlikely (current + size > end)) |
142 | real_reserve_method (size); /* presumably noinline */ |
302 | real_reserve_method (size); /* presumably noinline */ |
143 | } |
303 | } |
144 | |
304 | |
145 | =item bool ecb_assume (cond) [MACRO] |
305 | =item bool ecb_assume (cond) |
146 | |
306 | |
147 | Try to tell the compiler that some condition is true, even if it's not |
307 | Try to tell the compiler that some condition is true, even if it's not |
148 | obvious. |
308 | obvious. |
149 | |
309 | |
150 | This can be used to teach the compiler about invariants or other |
310 | This can be used to teach the compiler about invariants or other |
… | |
… | |
176 | |
336 | |
177 | This function does nothing itself, except tell the compiler that it will |
337 | This function does nothing itself, except tell the compiler that it will |
178 | never be executed. Apart from suppressing a warning in some cases, this |
338 | never be executed. Apart from suppressing a warning in some cases, this |
179 | function can be used to implement C<ecb_assume> or similar functions. |
339 | function can be used to implement C<ecb_assume> or similar functions. |
180 | |
340 | |
181 | =item bool ecb_prefetch (addr, rw, locality) [MACRO] |
341 | =item bool ecb_prefetch (addr, rw, locality) |
182 | |
342 | |
183 | Tells the compiler to try to prefetch memory at the given C<addr>ess |
343 | Tells the compiler to try to prefetch memory at the given C<addr>ess |
184 | for either reading (C<rw> = 0) or writing (C<rw> = 1). A C<locality> of |
344 | for either reading (C<rw> = 0) or writing (C<rw> = 1). A C<locality> of |
185 | C<0> means that there will only be one access later, C<3> means that |
345 | C<0> means that there will only be one access later, C<3> means that |
186 | the data will likely be accessed very often, and values in between mean |
346 | the data will likely be accessed very often, and values in between mean |
… | |
… | |
234 | equivalently the number of bits set to 0 before the least significant |
394 | equivalently the number of bits set to 0 before the least significant |
235 | bit set), starting from 0. If C<x> is 0 the result is undefined. A |
395 | bit set), starting from 0. If C<x> is 0 the result is undefined. A |
236 | common use case is to compute the integer binary logarithm, i.e., |
396 | common use case is to compute the integer binary logarithm, i.e., |
237 | floor(log2(n)). For example: |
397 | floor(log2(n)). For example: |
238 | |
398 | |
239 | ecb_ctz32(3) = 1 |
399 | ecb_ctz32 (3) = 0 |
240 | ecb_ctz32(6) = 2 |
400 | ecb_ctz32 (6) = 1 |
241 | |
401 | |
242 | =item int ecb_popcount32 (uint32_t x) |
402 | =item int ecb_popcount32 (uint32_t x) |
243 | |
403 | |
244 | Returns the number of bits set to 1 in C<x>. For example: |
404 | Returns the number of bits set to 1 in C<x>. For example: |
245 | |
405 | |
246 | ecb_popcount32(7) = 3 |
406 | ecb_popcount32 (7) = 3 |
247 | ecb_popcount32(255) = 8 |
407 | ecb_popcount32 (255) = 8 |
248 | |
408 | |
249 | =item uint32_t ecb_bswap16 (uint32_t x) |
409 | =item uint32_t ecb_bswap16 (uint32_t x) |
250 | |
410 | |
251 | =item uint32_t ecb_bswap32 (uint32_t x) |
411 | =item uint32_t ecb_bswap32 (uint32_t x) |
252 | |
412 | |
|
|
413 | These two functions return the value of the 16-bit (32-bit) value C<x> |
|
|
414 | after reversing the order of bytes (0x11223344 becomes 0x44332211). |
|
|
415 | |
253 | =item uint32_t ecb_rotr32 (uint32_t x, unsigned int count) |
416 | =item uint32_t ecb_rotr32 (uint32_t x, unsigned int count) |
254 | |
417 | |
255 | =item uint32_t ecb_rotl32 (uint32_t x, unsigned int count) |
418 | =item uint32_t ecb_rotl32 (uint32_t x, unsigned int count) |
256 | |
419 | |
257 | These two functions return the value of C<x> after shifting all the bits |
420 | These two functions return the value of C<x> after rotating all the bits |
258 | by C<count> positions to the right or left respectively. |
421 | by C<count> positions to the right or left respectively. |
259 | |
422 | |
|
|
423 | Current GCC versions understand these functions and usually compile them |
|
|
424 | to "optimal" code (e.g. a single C<roll> on x86). |
|
|
425 | |
260 | =back |
426 | =back |
261 | |
427 | |
262 | =head2 ARITHMETIC |
428 | =head2 ARITHMETIC |
263 | |
429 | |
264 | =over 4 |
430 | =over 4 |
265 | |
431 | |
266 | =item x = ecb_mod (m, n) [MACRO] |
432 | =item x = ecb_mod (m, n) |
267 | |
433 | |
268 | Returns the positive remainder of the modulo operation between C<m> |
434 | Returns the positive remainder of the modulo operation between C<m> and |
269 | and C<n>. |
435 | C<n>. Unlike the C modulo operator C<%>, this function ensures that the |
|
|
436 | return value is always positive - ISO C guarantees very little when |
|
|
437 | negative numbers are used with C<%>. |
|
|
438 | |
|
|
439 | C<n> must be strictly positive (i.e. C<< >1 >>), while C<m> must be |
|
|
440 | negatable, that is, both C<m> and C<-m> must be representable in its |
|
|
441 | type. |
270 | |
442 | |
271 | =back |
443 | =back |
272 | |
444 | |
273 | =head2 UTILITY |
445 | =head2 UTILITY |
274 | |
446 | |
275 | =over 4 |
447 | =over 4 |
276 | |
448 | |
277 | =item element_count = ecb_array_length (name) [MACRO] |
449 | =item element_count = ecb_array_length (name) [MACRO] |
278 | |
450 | |
|
|
451 | Returns the number of elements in the array C<name>. For example: |
|
|
452 | |
|
|
453 | int primes[] = { 2, 3, 5, 7, 11 }; |
|
|
454 | int sum = 0; |
|
|
455 | |
|
|
456 | for (i = 0; i < ecb_array_length (primes); i++) |
|
|
457 | sum += primes [i]; |
|
|
458 | |
279 | =back |
459 | =back |
280 | |
460 | |
281 | |
461 | |