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

Comparing libecb/ecb.pod (file contents):
Revision 1.1 by root, Thu May 26 19:39:40 2011 UTC vs.
Revision 1.21 by root, Fri May 27 00:14:10 2011 UTC

1=head1 LIBECB - e-C-Builtins
2
3=head2 ABOUT LIBECB
4
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 aren't.
22
23More might come.
24
25=head2 ABOUT THE HEADER
26
27At the moment, all you have to do is copy F<ecb.h> somewhere where your
28compiler can find it and include it:
29
30 #include <ecb.h>
31
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.
1 57
2=head2 GCC ATTRIBUTES 58=head2 GCC ATTRIBUTES
3 59
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;
75
4=over 4 76=over 4
5 77
6=item ecb_attribute(attrlist) 78=item ecb_attribute ((attrs...))
7=item ecb_noinline ecb_attribute ((noinline))
8=item ecb_noreturn ecb_attribute ((noreturn))
9=item ecb_unused ecb_attribute ((unused))
10=item ecb_const ecb_attribute ((const))
11=item ecb_pure ecb_attribute ((pure))
12=item ecb_hot ecb_attribute ((hot)) /* 4.3 */
13=item ecb_cold ecb_attribute ((cold)) /* 4.3 */
14 79
80A simple wrapper that expands to C<__attribute__((attrs))> on GCC, and to
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);
87
88=item ecb_unused
89
90Marks a function or a variable as "unused", which simply suppresses a
91warning by GCC when it detects it as unused. This is useful when you e.g.
92declare a variable but do not always use it:
93
94 {
95 int var ecb_unused;
96
97 #ifdef SOMECONDITION
98 var = ...;
99 return var;
100 #else
101 return 0;
102 #endif
103 }
104
105=item ecb_noinline
106
107Prevent a function from being inlined - it might be optimised away, but
108not inlined into other functions. This is useful if you know your function
109is rarely called and large enough for inlining not to be helpful.
110
111=item ecb_noreturn
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
127=item ecb_const
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
148=item ecb_pure
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
164=item ecb_hot
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
177=item ecb_cold
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
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 };
219
15 =back 220=back
16 221
17=head2 OPTIMISATION HINTS 222=head2 OPTIMISATION HINTS
18 223
19=over 4 224=over 4
20 225
21=item bool ecb_is_constant(expr) 226=item bool ecb_is_constant(expr)
22 227
228Returns true iff the expression can be deduced to be a compile-time
229constant, and false otherwise.
230
231For example, when you have a C<rndm16> function that returns a 16 bit
232random number, and you have a function that maps this to a range from
2330..n-1, then you could use this inline function in a header file:
234
235 ecb_inline uint32_t
236 rndm (uint32_t n)
237 {
238 return (n * (uint32_t)rndm16 ()) >> 16;
239 }
240
241However, for powers of two, you could use a normal mask, but that is only
242worth it if, at compile time, you can detect this case. This is the case
243when the passed number is a constant and also a power of two (C<n & (n -
2441) == 0>):
245
246 ecb_inline uint32_t
247 rndm (uint32_t n)
248 {
249 return is_constant (n) && !(n & (n - 1))
250 ? rndm16 () & (num - 1)
251 : (n * (uint32_t)rndm16 ()) >> 16;
252 }
253
23=item bool ecb_expect(expr,value) 254=item bool ecb_expect (expr, value)
24 255
256Evaluates C<expr> and returns it. In addition, it tells the compiler that
257the C<expr> evaluates to C<value> a lot, which can be used for static
258branch optimisations.
259
260Usually, you want to use the more intuitive C<ecb_likely> and
261C<ecb_unlikely> functions instead.
262
263=item bool ecb_likely (cond)
264
25=item bool ecb_unlikely(bool) 265=item bool ecb_unlikely (cond)
26 266
27=item bool ecb_likely(bool) 267These two functions expect a expression that is true or false and return
268C<1> or C<0>, respectively, so when used in the condition of an C<if> or
269other conditional statement, it will not change the program:
28 270
271 /* these two do the same thing */
272 if (some_condition) ...;
273 if (ecb_likely (some_condition)) ...;
274
275However, by using C<ecb_likely>, you tell the compiler that the condition
276is likely to be true (and for C<ecb_unlikely>, that it is unlikely to be
277true).
278
279For example, when you check for a null pointer and expect this to be a
280rare, exceptional, case, then use C<ecb_unlikely>:
281
282 void my_free (void *ptr)
283 {
284 if (ecb_unlikely (ptr == 0))
285 return;
286 }
287
288Consequent use of these functions to mark away exceptional cases or to
289tell the compiler what the hot path through a function is can increase
290performance considerably.
291
292A very good example is in a function that reserves more space for some
293memory block (for example, inside an implementation of a string stream) -
294each time something is added, you have to check for a buffer overrun, but
295you expect that most checks will turn out to be false:
296
297 /* make sure we have "size" extra room in our buffer */
298 ecb_inline void
299 reserve (int size)
300 {
301 if (ecb_unlikely (current + size > end))
302 real_reserve_method (size); /* presumably noinline */
303 }
304
29=item bool ecb_assume(cond) 305=item bool ecb_assume (cond)
30 306
307Try to tell the compiler that some condition is true, even if it's not
308obvious.
309
310This can be used to teach the compiler about invariants or other
311conditions that might improve code generation, but which are impossible to
312deduce form the code itself.
313
314For example, the example reservation function from the C<ecb_unlikely>
315description could be written thus (only C<ecb_assume> was added):
316
317 ecb_inline void
318 reserve (int size)
319 {
320 if (ecb_unlikely (current + size > end))
321 real_reserve_method (size); /* presumably noinline */
322
323 ecb_assume (current + size <= end);
324 }
325
326If you then call this function twice, like this:
327
328 reserve (10);
329 reserve (1);
330
331Then the compiler I<might> be able to optimise out the second call
332completely, as it knows that C<< current + 1 > end >> is false and the
333call will never be executed.
334
31=item bool ecb_unreachable() 335=item bool ecb_unreachable ()
32 336
337This function does nothing itself, except tell the compiler that it will
338never be executed. Apart from suppressing a warning in some cases, this
339function can be used to implement C<ecb_assume> or similar functions.
340
33=item bool ecb_prefetch(addr,rw,locality) 341=item bool ecb_prefetch (addr, rw, locality)
34 342
343Tells the compiler to try to prefetch memory at the given C<addr>ess
344for either reading (C<rw> = 0) or writing (C<rw> = 1). A C<locality> of
345C<0> means that there will only be one access later, C<3> means that
346the data will likely be accessed very often, and values in between mean
347something... in between. The memory pointed to by the address does not
348need to be accessible (it could be a null pointer for example), but C<rw>
349and C<locality> must be compile-time constants.
350
351An obvious way to use this is to prefetch some data far away, in a big
352array you loop over. This prefetches memory some 128 array elements later,
353in the hope that it will be ready when the CPU arrives at that location.
354
355 int sum = 0;
356
357 for (i = 0; i < N; ++i)
358 {
359 sum += arr [i]
360 ecb_prefetch (arr + i + 128, 0, 0);
361 }
362
363It's hard to predict how far to prefetch, and most CPUs that can prefetch
364are often good enough to predict this kind of behaviour themselves. It
365gets more interesting with linked lists, especially when you do some fair
366processing on each list element:
367
368 for (node *n = start; n; n = n->next)
369 {
370 ecb_prefetch (n->next, 0, 0);
371 ... do medium amount of work with *n
372 }
373
374After processing the node, (part of) the next node might already be in
375cache.
376
35 =back 377=back
36 378
37=head2 BIT FIDDLING / BITSTUFFS 379=head2 BIT FIDDLING / BITSTUFFS
38 380
381=over 4
382
39bool ecb_big_endian (); 383=item bool ecb_big_endian ()
384
40bool ecb_little_endian (); 385=item bool ecb_little_endian ()
386
387These two functions return true if the byte order is big endian
388(most-significant byte first) or little endian (least-significant byte
389first) respectively.
390
41int ecb_ctz32 (uint32_t x); 391=item int ecb_ctz32 (uint32_t x)
392
393Returns the index of the least significant bit set in C<x> (or
394equivalently the number of bits set to 0 before the least significant
395bit set), starting from 0. If C<x> is 0 the result is undefined. A
396common use case is to compute the integer binary logarithm, i.e.,
397floor(log2(n)). For example:
398
399 ecb_ctz32 (3) = 0
400 ecb_ctz32 (6) = 1
401
42int ecb_popcount32 (uint32_t x); 402=item int ecb_popcount32 (uint32_t x)
43uint32_t ecb_bswap32 (uint32_t x); 403
404Returns the number of bits set to 1 in C<x>. For example:
405
406 ecb_popcount32 (7) = 3
407 ecb_popcount32 (255) = 8
408
44uint32_t ecb_bswap16 (uint32_t x); 409=item uint32_t ecb_bswap16 (uint32_t x)
410
411=item uint32_t ecb_bswap32 (uint32_t x)
412
413These two functions return the value of the 16-bit (32-bit) value C<x>
414after reversing the order of bytes (0x11223344 becomes 0x44332211).
415
45uint32_t ecb_rotr32 (uint32_t x, unsigned int count); 416=item uint32_t ecb_rotr32 (uint32_t x, unsigned int count)
417
46uint32_t ecb_rotl32 (uint32_t x, unsigned int count); 418=item uint32_t ecb_rotl32 (uint32_t x, unsigned int count)
419
420These two functions return the value of C<x> after shifting all the bits
421by C<count> positions to the right or left respectively.
422
423Current GCC versions understand these functions and usually compile them
424to "optimal" code (e.g. a single C<roll> on x86).
425
426=back
47 427
48=head2 ARITHMETIC 428=head2 ARITHMETIC
49 429
430=over 4
431
50x = ecb_mod (m, n) 432=item x = ecb_mod (m, n)
433
434Returns the positive remainder of the modulo operation between C<m> and
435C<n>. Unlike the C modulo operator C<%>, this function ensures that the
436return value is always positive - ISO C guarantees very little when
437negative numbers are used with C<%>.
438
439C<n> must be strictly positive (i.e. C<< >1 >>), while C<m> must be
440negatable, that is, both C<m> and C<-m> must be representable in its
441type.
442
443=back
51 444
52=head2 UTILITY 445=head2 UTILITY
53 446
54ecb_array_length (name) 447=over 4
55 448
449=item element_count = ecb_array_length (name) [MACRO]
56 450
451Returns 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
459=back
460
461

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines