ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libecb/ecb.pod
(Generate patch)

Comparing cvsroot/libecb/ecb.pod (file contents):
Revision 1.9 by root, Thu May 26 20:54:31 2011 UTC vs.
Revision 1.17 by root, Thu May 26 23:55:58 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines