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

Comparing libecb/ecb.pod (file contents):
Revision 1.14 by root, Thu May 26 23:23:08 2011 UTC vs.
Revision 1.21 by root, Fri May 27 00:14:10 2011 UTC

3=head2 ABOUT LIBECB 3=head2 ABOUT LIBECB
4 4
5Libecb is currently a simple header file that doesn't require any 5Libecb is currently a simple header file that doesn't require any
6configuration to use or include in your project. 6configuration to use or include in your project.
7 7
8It's part of the e-suite of libraries, other memembers of which include 8It's part of the e-suite of libraries, other members of which include
9libev and libeio. 9libev and libeio.
10 10
11Its homepage can be found here: 11Its homepage can be found here:
12 12
13 http://software.schmorp.de/pkg/libecb 13 http://software.schmorp.de/pkg/libecb
14 14
15It mainly provides a number of wrappers around GCC built-ins, together 15It mainly provides a number of wrappers around GCC built-ins, together
16with replacement functions for other compilers. In addition to this, 16with replacement functions for other compilers. In addition to this,
17it provides a number of other lowlevel C utilities, such endienness 17it provides a number of other lowlevel C utilities, such as endianness
18detection, byte swapping or bit rotations. 18detection, byte swapping or bit rotations.
19
20Or in other words, things that should be built-in into any standard C
21system, but aren't.
19 22
20More might come. 23More might come.
21 24
22=head2 ABOUT THE HEADER 25=head2 ABOUT THE HEADER
23 26
27 #include <ecb.h> 30 #include <ecb.h>
28 31
29The header should work fine for both C and C++ compilation, and gives you 32The header should work fine for both C and C++ compilation, and gives you
30all of F<inttypes.h> in addition to the ECB symbols. 33all of F<inttypes.h> in addition to the ECB symbols.
31 34
32There are currently no objetc files to link to - future versions might 35There are currently no object files to link to - future versions might
33come with an (optional) object code library to link against, to reduce 36come with an (optional) object code library to link against, to reduce
34code size or gain access to additional features. 37code size or gain access to additional features.
35 38
36It also currently includes everything from F<inttypes.h>. 39It also currently includes everything from F<inttypes.h>.
37 40
52is usually implemented as a macro. Specifically, a "bool" in this manual 55is usually implemented as a macro. Specifically, a "bool" in this manual
53refers to any kind of boolean value, not a specific type. 56refers to any kind of boolean value, not a specific type.
54 57
55=head2 GCC ATTRIBUTES 58=head2 GCC ATTRIBUTES
56 59
57blabla where to put, what others 60A major part of libecb deals with GCC attributes. These are additional
61attributes that you cna assign to functions, variables and sometimes even
62types - much like C<const> or C<volatile> in C.
63
64While GCC allows declarations to show up in many surprising places,
65but not in many expeted places, the safest way is to put attribute
66declarations before the whole declaration:
67
68 ecb_const int mysqrt (int a);
69 ecb_unused int i;
70
71For variables, it is often nicer to put the attribute after the name, and
72avoid multiple declarations using commas:
73
74 int i ecb_unused;
58 75
59=over 4 76=over 4
60 77
61=item ecb_attribute ((attrs...)) 78=item ecb_attribute ((attrs...))
62 79
63A simple wrapper that expands to C<__attribute__((attrs))> on GCC, and 80A simple wrapper that expands to C<__attribute__((attrs))> on GCC, and to
64to nothing on other compilers, so the effect is that only GCC sees these. 81nothing on other compilers, so the effect is that only GCC sees these.
82
83Example: use the C<deprecated> attribute on a function.
84
85 ecb_attribute((__deprecated__)) void
86 do_not_use_me_anymore (void);
65 87
66=item ecb_unused 88=item ecb_unused
67 89
68Marks a function or a variable as "unused", which simply suppresses a 90Marks a function or a variable as "unused", which simply suppresses a
69warning by GCC when it detects it as unused. This is useful when you e.g. 91warning by GCC when it detects it as unused. This is useful when you e.g.
70declare a variable but do not always use it: 92declare a variable but do not always use it:
71 93
72 { 94 {
73 int var ecb_unused; 95 int var ecb_unused;
74 96
75 #ifdef SOMECONDITION 97 #ifdef SOMECONDITION
76 var = ...; 98 var = ...;
77 return var; 99 return var;
78 #else 100 #else
79 return 0; 101 return 0;
80 #endif 102 #endif
81 } 103 }
82 104
83=item ecb_noinline 105=item ecb_noinline
84 106
85Prevent a function from being inlined - it might be optimised away, but 107Prevent a function from being inlined - it might be optimised away, but
86not inlined into other functions. This is useful if you know your function 108not inlined into other functions. This is useful if you know your function
87is rarely called and large enough for inlining not to be helpful. 109is rarely called and large enough for inlining not to be helpful.
88 110
89=item ecb_noreturn 111=item ecb_noreturn
90 112
113Marks a function as "not returning, ever". Some typical functions that
114don't return are C<exit> or C<abort> (which really works hard to not
115return), 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
124In this case, the compiler would probably be smart enough to deduce it on
125its own, so this is mainly useful for declarations.
126
91=item ecb_const 127=item ecb_const
92 128
129Declares that the function only depends on the values of its arguments,
130much like a mathematical function. It specifically does not read or write
131any memory any arguments might point to, global variables, or call any
132non-const functions. It also must not have any side effects.
133
134Such a function can be optimised much more aggressively by the compiler -
135for example, multiple calls with the same arguments can be optimised into
136a single call, which wouldn't be possible if the compiler would have to
137expect any side effects.
138
139It is best suited for functions in the sense of mathematical functions,
140such as a function returning the square root of its input argument.
141
142Not suited would be a function that calculates the hash of some memory
143area you pass in, prints some messages or looks at a global variable to
144decide on rounding.
145
146See C<ecb_pure> for a slightly less restrictive class of functions.
147
93=item ecb_pure 148=item ecb_pure
94 149
150Similar to C<ecb_const>, declares a function that has no side
151effects. Unlike C<ecb_const>, the function is allowed to examine global
152variables and any other memory areas (such as the ones passed to it via
153pointers).
154
155While these functions cannot be optimised as aggressively as C<ecb_const>
156functions, they can still be optimised away in many occasions, and the
157compiler has more freedom in moving calls to them around.
158
159Typical examples for such functions would be C<strlen> or C<memcmp>. A
160function that calculates the MD5 sum of some input and updates some MD5
161state passed as argument would I<NOT> be pure, however, as it would modify
162some memory area that is not the return value.
163
95=item ecb_hot 164=item ecb_hot
96 165
166This declares a function as "hot" with regards to the cache - the function
167is used so often, that it is very beneficial to keep it in the cache if
168possible.
169
170The compiler reacts by trying to place hot functions near to each other in
171memory.
172
173Whether a function is hot or not often depends on the whole program,
174and less on the function itself. C<ecb_cold> is likely more useful in
175practise.
176
97=item ecb_cold 177=item ecb_cold
98 178
179The opposite of C<ecb_hot> - declares a function as "cold" with regards to
180the cache, or in other words, this function is not called often, or not at
181speed-critical times, and keeping it in the cache might be a waste of said
182cache.
183
184In addition to placing cold functions together (or at least away from hot
185functions), this knowledge can be used in other ways, for example, the
186function will be optimised for size, as opposed to speed, and codepaths
187leading to calls to those functions can automatically be marked as if
188C<ecb_unlikely> had been used to reach them.
189
190Good examples for such functions would be error reporting functions, or
191functions only called in exceptional or rare cases.
192
99=item ecb_artificial 193=item ecb_artificial
194
195Declares the function as "artificial", in this case meaning that this
196function 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
198crash reported in such a method, or single-stepping through them, is not
199usually so helpful, especially when it's inlined to just a few instructions.
200
201Marking them as artificial will instruct the debugger about just this,
202leading to happier debugging and thus happier lives.
203
204Example: in some kind of smart-pointer class, mark the pointer accessor as
205artificial, so that the whole class acts more like a pointer and less like
206some 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 };
100 219
101=back 220=back
102 221
103=head2 OPTIMISATION HINTS 222=head2 OPTIMISATION HINTS
104 223
139branch optimisations. 258branch optimisations.
140 259
141Usually, you want to use the more intuitive C<ecb_likely> and 260Usually, you want to use the more intuitive C<ecb_likely> and
142C<ecb_unlikely> functions instead. 261C<ecb_unlikely> functions instead.
143 262
144=item bool ecb_likely (bool) 263=item bool ecb_likely (cond)
145 264
146=item bool ecb_unlikely (bool) 265=item bool ecb_unlikely (cond)
147 266
148These two functions expect a expression that is true or false and return 267These two functions expect a expression that is true or false and return
149C<1> or C<0>, respectively, so when used in the condition of an C<if> or 268C<1> or C<0>, respectively, so when used in the condition of an C<if> or
150other conditional statement, it will not change the program: 269other conditional statement, it will not change the program:
151 270
275equivalently the number of bits set to 0 before the least significant 394equivalently the number of bits set to 0 before the least significant
276bit set), starting from 0. If C<x> is 0 the result is undefined. A 395bit set), starting from 0. If C<x> is 0 the result is undefined. A
277common use case is to compute the integer binary logarithm, i.e., 396common use case is to compute the integer binary logarithm, i.e.,
278floor(log2(n)). For example: 397floor(log2(n)). For example:
279 398
280 ecb_ctz32(3) = 0 399 ecb_ctz32 (3) = 0
281 ecb_ctz32(6) = 1 400 ecb_ctz32 (6) = 1
282 401
283=item int ecb_popcount32 (uint32_t x) 402=item int ecb_popcount32 (uint32_t x)
284 403
285Returns the number of bits set to 1 in C<x>. For example: 404Returns the number of bits set to 1 in C<x>. For example:
286 405
287 ecb_popcount32(7) = 3 406 ecb_popcount32 (7) = 3
288 ecb_popcount32(255) = 8 407 ecb_popcount32 (255) = 8
289 408
290=item uint32_t ecb_bswap16 (uint32_t x) 409=item uint32_t ecb_bswap16 (uint32_t x)
291 410
292=item uint32_t ecb_bswap32 (uint32_t x) 411=item uint32_t ecb_bswap32 (uint32_t x)
293 412
294These two functions return the value of the 16-bit (32-bit) variable 413These two functions return the value of the 16-bit (32-bit) value C<x>
295C<x> after reversing the order of bytes. 414after reversing the order of bytes (0x11223344 becomes 0x44332211).
296 415
297=item uint32_t ecb_rotr32 (uint32_t x, unsigned int count) 416=item uint32_t ecb_rotr32 (uint32_t x, unsigned int count)
298 417
299=item uint32_t ecb_rotl32 (uint32_t x, unsigned int count) 418=item uint32_t ecb_rotl32 (uint32_t x, unsigned int count)
300 419
301These two functions return the value of C<x> after shifting all the bits 420These two functions return the value of C<x> after shifting all the bits
302by C<count> positions to the right or left respectively. 421by C<count> positions to the right or left respectively.
303 422
423Current GCC versions understand these functions and usually compile them
424to "optimal" code (e.g. a single C<roll> on x86).
425
304=back 426=back
305 427
306=head2 ARITHMETIC 428=head2 ARITHMETIC
307 429
308=over 4 430=over 4
309 431
310=item x = ecb_mod (m, n) 432=item x = ecb_mod (m, n)
311 433
312Returns the positive remainder of the modulo operation between C<m> and 434Returns the positive remainder of the modulo operation between C<m> and
313C<n>. Unlike the C moduloe operator C<%>, this function ensures that the 435C<n>. Unlike the C modulo operator C<%>, this function ensures that the
314return value is always positive). 436return value is always positive - ISO C guarantees very little when
437negative numbers are used with C<%>.
315 438
316C<n> must be strictly positive (i.e. C<< >1 >>), while C<m> must be 439C<n> must be strictly positive (i.e. C<< >1 >>), while C<m> must be
317negatable, that is, both C<m> and C<-m> must be representable in its 440negatable, that is, both C<m> and C<-m> must be representable in its
318type. 441type.
319 442

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines