ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libecb/ecb.pod
Revision: 1.47
Committed: Tue May 29 16:51:24 2012 UTC (12 years, 1 month ago) by root
Branch: MAIN
Changes since 1.46: +7 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 LIBECB - e-C-Builtins
2
3 =head2 ABOUT LIBECB
4
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 into any standard C system,
21 but aren't, implemented as efficient as possible with GCC, and still
22 correct with other compilers.
23
24 More might come.
25
26 =head2 ABOUT THE HEADER
27
28 At the moment, all you have to do is copy F<ecb.h> somewhere where your
29 compiler can find it and include it:
30
31 #include <ecb.h>
32
33 The header should work fine for both C and C++ compilation, and gives you
34 all of F<inttypes.h> in addition to the ECB symbols.
35
36 There are currently no object files to link to - future versions might
37 come with an (optional) object code library to link against, to reduce
38 code size or gain access to additional features.
39
40 It also currently includes everything from F<inttypes.h>.
41
42 =head2 ABOUT THIS MANUAL / CONVENTIONS
43
44 This manual mainly describes each (public) function available after
45 including the F<ecb.h> header. The header might define other symbols than
46 these, but these are not part of the public API, and not supported in any
47 way.
48
49 When the manual mentions a "function" then this could be defined either as
50 as inline function, a macro, or an external symbol.
51
52 When functions use a concrete standard type, such as C<int> or
53 C<uint32_t>, then the corresponding function works only with that type. If
54 only a generic name is used (C<expr>, C<cond>, C<value> and so on), then
55 the corresponding function relies on C to implement the correct types, and
56 is usually implemented as a macro. Specifically, a "bool" in this manual
57 refers to any kind of boolean value, not a specific type.
58
59 =head2 TYPES / TYPE SUPPORT
60
61 ecb.h makes sure that the following types are defined (in the expected way):
62
63 int8_t uint8_t int16_t uint16_t
64 int32_t uint32_t int64_t uint64_t
65 intptr_t uintptr_t ptrdiff_t
66
67 The macro C<ECB_PTRSIZE> is defined to the size of a pointer on this
68 platform (currently C<4> or C<8>) and can be used in preprocessor
69 expressions.
70
71 =head2 LANGUAGE/COMPILER VERSIONS
72
73 All the following symbols expand to an expression that can be tested in
74 preprocessor instructions as well as treated as a boolean (use C<!!> to
75 ensure it's either C<0> or C<1> if you need that).
76
77 =over 4
78
79 =item ECB_C
80
81 True if the implementation defines the C<__STDC__> macro to a true value,
82 which is typically true for both C and C++ compilers.
83
84 =item ECB_C99
85
86 True if the implementation claims to be compliant to C99 (ISO/IEC
87 9899:1999) or any later version.
88
89 Note that later versions (ECB_C11) remove core features again (for
90 example, variable length arrays).
91
92 =item ECB_C11
93
94 True if the implementation claims to be compliant to C11 (ISO/IEC
95 9899:2011) or any later version.
96
97 =item ECB_CPP
98
99 True if the implementation defines the C<__cplusplus__> macro to a true
100 value, which is typically true for C++ compilers.
101
102 =item ECB_CPP98
103
104 True if the implementation claims to be compliant to ISO/IEC 14882:1998
105 (the first C++ ISO standard) or any later version. Typically true for all
106 C++ compilers.
107
108 =item ECB_CPP11
109
110 True if the implementation claims to be compliant to ISO/IEC 14882:2011
111 (C++11) or any later version.
112
113 =item ECB_GCC_VERSION(major,minor)
114
115 Expands to a true value (suitable for testing in by the preprocessor)
116 if the compiler used is GNU C and the version is the given version, or
117 higher.
118
119 This macro tries to return false on compilers that claim to be GCC
120 compatible but aren't.
121
122 =back
123
124 =head2 GCC ATTRIBUTES
125
126 A major part of libecb deals with GCC attributes. These are additional
127 attributes that you can assign to functions, variables and sometimes even
128 types - much like C<const> or C<volatile> in C.
129
130 While GCC allows declarations to show up in many surprising places,
131 but not in many expected places, the safest way is to put attribute
132 declarations before the whole declaration:
133
134 ecb_const int mysqrt (int a);
135 ecb_unused int i;
136
137 For variables, it is often nicer to put the attribute after the name, and
138 avoid multiple declarations using commas:
139
140 int i ecb_unused;
141
142 =over 4
143
144 =item ecb_attribute ((attrs...))
145
146 A simple wrapper that expands to C<__attribute__((attrs))> on GCC, and to
147 nothing on other compilers, so the effect is that only GCC sees these.
148
149 Example: use the C<deprecated> attribute on a function.
150
151 ecb_attribute((__deprecated__)) void
152 do_not_use_me_anymore (void);
153
154 =item ecb_unused
155
156 Marks a function or a variable as "unused", which simply suppresses a
157 warning by GCC when it detects it as unused. This is useful when you e.g.
158 declare a variable but do not always use it:
159
160 {
161 int var ecb_unused;
162
163 #ifdef SOMECONDITION
164 var = ...;
165 return var;
166 #else
167 return 0;
168 #endif
169 }
170
171 =item ecb_inline
172
173 This is not actually an attribute, but you use it like one. It expands
174 either to C<static inline> or to just C<static>, if inline isn't
175 supported. It should be used to declare functions that should be inlined,
176 for code size or speed reasons.
177
178 Example: inline this function, it surely will reduce codesize.
179
180 ecb_inline int
181 negmul (int a, int b)
182 {
183 return - (a * b);
184 }
185
186 =item ecb_noinline
187
188 Prevent a function from being inlined - it might be optimised away, but
189 not inlined into other functions. This is useful if you know your function
190 is rarely called and large enough for inlining not to be helpful.
191
192 =item ecb_noreturn
193
194 Marks a function as "not returning, ever". Some typical functions that
195 don't return are C<exit> or C<abort> (which really works hard to not
196 return), and now you can make your own:
197
198 ecb_noreturn void
199 my_abort (const char *errline)
200 {
201 puts (errline);
202 abort ();
203 }
204
205 In this case, the compiler would probably be smart enough to deduce it on
206 its own, so this is mainly useful for declarations.
207
208 =item ecb_const
209
210 Declares that the function only depends on the values of its arguments,
211 much like a mathematical function. It specifically does not read or write
212 any memory any arguments might point to, global variables, or call any
213 non-const functions. It also must not have any side effects.
214
215 Such a function can be optimised much more aggressively by the compiler -
216 for example, multiple calls with the same arguments can be optimised into
217 a single call, which wouldn't be possible if the compiler would have to
218 expect any side effects.
219
220 It is best suited for functions in the sense of mathematical functions,
221 such as a function returning the square root of its input argument.
222
223 Not suited would be a function that calculates the hash of some memory
224 area you pass in, prints some messages or looks at a global variable to
225 decide on rounding.
226
227 See C<ecb_pure> for a slightly less restrictive class of functions.
228
229 =item ecb_pure
230
231 Similar to C<ecb_const>, declares a function that has no side
232 effects. Unlike C<ecb_const>, the function is allowed to examine global
233 variables and any other memory areas (such as the ones passed to it via
234 pointers).
235
236 While these functions cannot be optimised as aggressively as C<ecb_const>
237 functions, they can still be optimised away in many occasions, and the
238 compiler has more freedom in moving calls to them around.
239
240 Typical examples for such functions would be C<strlen> or C<memcmp>. A
241 function that calculates the MD5 sum of some input and updates some MD5
242 state passed as argument would I<NOT> be pure, however, as it would modify
243 some memory area that is not the return value.
244
245 =item ecb_hot
246
247 This declares a function as "hot" with regards to the cache - the function
248 is used so often, that it is very beneficial to keep it in the cache if
249 possible.
250
251 The compiler reacts by trying to place hot functions near to each other in
252 memory.
253
254 Whether a function is hot or not often depends on the whole program,
255 and less on the function itself. C<ecb_cold> is likely more useful in
256 practise.
257
258 =item ecb_cold
259
260 The opposite of C<ecb_hot> - declares a function as "cold" with regards to
261 the cache, or in other words, this function is not called often, or not at
262 speed-critical times, and keeping it in the cache might be a waste of said
263 cache.
264
265 In addition to placing cold functions together (or at least away from hot
266 functions), this knowledge can be used in other ways, for example, the
267 function will be optimised for size, as opposed to speed, and codepaths
268 leading to calls to those functions can automatically be marked as if
269 C<ecb_expect_false> had been used to reach them.
270
271 Good examples for such functions would be error reporting functions, or
272 functions only called in exceptional or rare cases.
273
274 =item ecb_artificial
275
276 Declares the function as "artificial", in this case meaning that this
277 function is not really mean to be a function, but more like an accessor
278 - many methods in C++ classes are mere accessor functions, and having a
279 crash reported in such a method, or single-stepping through them, is not
280 usually so helpful, especially when it's inlined to just a few instructions.
281
282 Marking them as artificial will instruct the debugger about just this,
283 leading to happier debugging and thus happier lives.
284
285 Example: in some kind of smart-pointer class, mark the pointer accessor as
286 artificial, so that the whole class acts more like a pointer and less like
287 some C++ abstraction monster.
288
289 template<typename T>
290 struct my_smart_ptr
291 {
292 T *value;
293
294 ecb_artificial
295 operator T *()
296 {
297 return value;
298 }
299 };
300
301 =back
302
303 =head2 OPTIMISATION HINTS
304
305 =over 4
306
307 =item bool ecb_is_constant(expr)
308
309 Returns true iff the expression can be deduced to be a compile-time
310 constant, and false otherwise.
311
312 For example, when you have a C<rndm16> function that returns a 16 bit
313 random number, and you have a function that maps this to a range from
314 0..n-1, then you could use this inline function in a header file:
315
316 ecb_inline uint32_t
317 rndm (uint32_t n)
318 {
319 return (n * (uint32_t)rndm16 ()) >> 16;
320 }
321
322 However, for powers of two, you could use a normal mask, but that is only
323 worth it if, at compile time, you can detect this case. This is the case
324 when the passed number is a constant and also a power of two (C<n & (n -
325 1) == 0>):
326
327 ecb_inline uint32_t
328 rndm (uint32_t n)
329 {
330 return is_constant (n) && !(n & (n - 1))
331 ? rndm16 () & (num - 1)
332 : (n * (uint32_t)rndm16 ()) >> 16;
333 }
334
335 =item bool ecb_expect (expr, value)
336
337 Evaluates C<expr> and returns it. In addition, it tells the compiler that
338 the C<expr> evaluates to C<value> a lot, which can be used for static
339 branch optimisations.
340
341 Usually, you want to use the more intuitive C<ecb_expect_true> and
342 C<ecb_expect_false> functions instead.
343
344 =item bool ecb_expect_true (cond)
345
346 =item bool ecb_expect_false (cond)
347
348 These two functions expect a expression that is true or false and return
349 C<1> or C<0>, respectively, so when used in the condition of an C<if> or
350 other conditional statement, it will not change the program:
351
352 /* these two do the same thing */
353 if (some_condition) ...;
354 if (ecb_expect_true (some_condition)) ...;
355
356 However, by using C<ecb_expect_true>, you tell the compiler that the
357 condition is likely to be true (and for C<ecb_expect_false>, that it is
358 unlikely to be true).
359
360 For example, when you check for a null pointer and expect this to be a
361 rare, exceptional, case, then use C<ecb_expect_false>:
362
363 void my_free (void *ptr)
364 {
365 if (ecb_expect_false (ptr == 0))
366 return;
367 }
368
369 Consequent use of these functions to mark away exceptional cases or to
370 tell the compiler what the hot path through a function is can increase
371 performance considerably.
372
373 You might know these functions under the name C<likely> and C<unlikely>
374 - while these are common aliases, we find that the expect name is easier
375 to understand when quickly skimming code. If you wish, you can use
376 C<ecb_likely> instead of C<ecb_expect_true> and C<ecb_unlikely> instead of
377 C<ecb_expect_false> - these are simply aliases.
378
379 A very good example is in a function that reserves more space for some
380 memory block (for example, inside an implementation of a string stream) -
381 each time something is added, you have to check for a buffer overrun, but
382 you expect that most checks will turn out to be false:
383
384 /* make sure we have "size" extra room in our buffer */
385 ecb_inline void
386 reserve (int size)
387 {
388 if (ecb_expect_false (current + size > end))
389 real_reserve_method (size); /* presumably noinline */
390 }
391
392 =item bool ecb_assume (cond)
393
394 Try to tell the compiler that some condition is true, even if it's not
395 obvious.
396
397 This can be used to teach the compiler about invariants or other
398 conditions that might improve code generation, but which are impossible to
399 deduce form the code itself.
400
401 For example, the example reservation function from the C<ecb_expect_false>
402 description could be written thus (only C<ecb_assume> was added):
403
404 ecb_inline void
405 reserve (int size)
406 {
407 if (ecb_expect_false (current + size > end))
408 real_reserve_method (size); /* presumably noinline */
409
410 ecb_assume (current + size <= end);
411 }
412
413 If you then call this function twice, like this:
414
415 reserve (10);
416 reserve (1);
417
418 Then the compiler I<might> be able to optimise out the second call
419 completely, as it knows that C<< current + 1 > end >> is false and the
420 call will never be executed.
421
422 =item bool ecb_unreachable ()
423
424 This function does nothing itself, except tell the compiler that it will
425 never be executed. Apart from suppressing a warning in some cases, this
426 function can be used to implement C<ecb_assume> or similar functions.
427
428 =item bool ecb_prefetch (addr, rw, locality)
429
430 Tells the compiler to try to prefetch memory at the given C<addr>ess
431 for either reading (C<rw> = 0) or writing (C<rw> = 1). A C<locality> of
432 C<0> means that there will only be one access later, C<3> means that
433 the data will likely be accessed very often, and values in between mean
434 something... in between. The memory pointed to by the address does not
435 need to be accessible (it could be a null pointer for example), but C<rw>
436 and C<locality> must be compile-time constants.
437
438 An obvious way to use this is to prefetch some data far away, in a big
439 array you loop over. This prefetches memory some 128 array elements later,
440 in the hope that it will be ready when the CPU arrives at that location.
441
442 int sum = 0;
443
444 for (i = 0; i < N; ++i)
445 {
446 sum += arr [i]
447 ecb_prefetch (arr + i + 128, 0, 0);
448 }
449
450 It's hard to predict how far to prefetch, and most CPUs that can prefetch
451 are often good enough to predict this kind of behaviour themselves. It
452 gets more interesting with linked lists, especially when you do some fair
453 processing on each list element:
454
455 for (node *n = start; n; n = n->next)
456 {
457 ecb_prefetch (n->next, 0, 0);
458 ... do medium amount of work with *n
459 }
460
461 After processing the node, (part of) the next node might already be in
462 cache.
463
464 =back
465
466 =head2 BIT FIDDLING / BIT WIZARDRY
467
468 =over 4
469
470 =item bool ecb_big_endian ()
471
472 =item bool ecb_little_endian ()
473
474 These two functions return true if the byte order is big endian
475 (most-significant byte first) or little endian (least-significant byte
476 first) respectively.
477
478 On systems that are neither, their return values are unspecified.
479
480 =item int ecb_ctz32 (uint32_t x)
481
482 =item int ecb_ctz64 (uint64_t x)
483
484 Returns the index of the least significant bit set in C<x> (or
485 equivalently the number of bits set to 0 before the least significant bit
486 set), starting from 0. If C<x> is 0 the result is undefined.
487
488 For smaller types than C<uint32_t> you can safely use C<ecb_ctz32>.
489
490 For example:
491
492 ecb_ctz32 (3) = 0
493 ecb_ctz32 (6) = 1
494
495 =item bool ecb_is_pot32 (uint32_t x)
496
497 =item bool ecb_is_pot64 (uint32_t x)
498
499 Return true iff C<x> is a power of two or C<x == 0>.
500
501 For smaller types then C<uint32_t> you can safely use C<ecb_is_pot32>.
502
503 =item int ecb_ld32 (uint32_t x)
504
505 =item int ecb_ld64 (uint64_t x)
506
507 Returns the index of the most significant bit set in C<x>, or the number
508 of digits the number requires in binary (so that C<< 2**ld <= x <
509 2**(ld+1) >>). If C<x> is 0 the result is undefined. A common use case is
510 to compute the integer binary logarithm, i.e. C<floor (log2 (n))>, for
511 example to see how many bits a certain number requires to be encoded.
512
513 This function is similar to the "count leading zero bits" function, except
514 that that one returns how many zero bits are "in front" of the number (in
515 the given data type), while C<ecb_ld> returns how many bits the number
516 itself requires.
517
518 For smaller types than C<uint32_t> you can safely use C<ecb_ld32>.
519
520 =item int ecb_popcount32 (uint32_t x)
521
522 =item int ecb_popcount64 (uint64_t x)
523
524 Returns the number of bits set to 1 in C<x>.
525
526 For smaller types than C<uint32_t> you can safely use C<ecb_popcount32>.
527
528 For example:
529
530 ecb_popcount32 (7) = 3
531 ecb_popcount32 (255) = 8
532
533 =item uint8_t ecb_bitrev8 (uint8_t x)
534
535 =item uint16_t ecb_bitrev16 (uint16_t x)
536
537 =item uint32_t ecb_bitrev32 (uint32_t x)
538
539 Reverses the bits in x, i.e. the MSB becomes the LSB, MSB-1 becomes LSB+1
540 and so on.
541
542 Example:
543
544 ecb_bitrev8 (0xa7) = 0xea
545 ecb_bitrev32 (0xffcc4411) = 0x882233ff
546
547 =item uint32_t ecb_bswap16 (uint32_t x)
548
549 =item uint32_t ecb_bswap32 (uint32_t x)
550
551 =item uint64_t ecb_bswap64 (uint64_t x)
552
553 These functions return the value of the 16-bit (32-bit, 64-bit) value
554 C<x> after reversing the order of bytes (0x11223344 becomes 0x44332211 in
555 C<ecb_bswap32>).
556
557 =item uint8_t ecb_rotl8 (uint8_t x, unsigned int count)
558
559 =item uint16_t ecb_rotl16 (uint16_t x, unsigned int count)
560
561 =item uint32_t ecb_rotl32 (uint32_t x, unsigned int count)
562
563 =item uint64_t ecb_rotl64 (uint64_t x, unsigned int count)
564
565 =item uint8_t ecb_rotr8 (uint8_t x, unsigned int count)
566
567 =item uint16_t ecb_rotr16 (uint16_t x, unsigned int count)
568
569 =item uint32_t ecb_rotr32 (uint32_t x, unsigned int count)
570
571 =item uint64_t ecb_rotr64 (uint64_t x, unsigned int count)
572
573 These two families of functions return the value of C<x> after rotating
574 all the bits by C<count> positions to the right (C<ecb_rotr>) or left
575 (C<ecb_rotl>).
576
577 Current GCC versions understand these functions and usually compile them
578 to "optimal" code (e.g. a single C<rol> or a combination of C<shld> on
579 x86).
580
581 =back
582
583 =head2 ARITHMETIC
584
585 =over 4
586
587 =item x = ecb_mod (m, n)
588
589 Returns C<m> modulo C<n>, which is the same as the positive remainder
590 of the division operation between C<m> and C<n>, using floored
591 division. Unlike the C remainder operator C<%>, this function ensures that
592 the return value is always positive and that the two numbers I<m> and
593 I<m' = m + i * n> result in the same value modulo I<n> - in other words,
594 C<ecb_mod> implements the mathematical modulo operation, which is missing
595 in the language.
596
597 C<n> must be strictly positive (i.e. C<< >= 1 >>), while C<m> must be
598 negatable, that is, both C<m> and C<-m> must be representable in its
599 type (this typically excludes the minimum signed integer value, the same
600 limitation as for C</> and C<%> in C).
601
602 Current GCC versions compile this into an efficient branchless sequence on
603 almost all CPUs.
604
605 For example, when you want to rotate forward through the members of an
606 array for increasing C<m> (which might be negative), then you should use
607 C<ecb_mod>, as the C<%> operator might give either negative results, or
608 change direction for negative values:
609
610 for (m = -100; m <= 100; ++m)
611 int elem = myarray [ecb_mod (m, ecb_array_length (myarray))];
612
613 =item x = ecb_div_rd (val, div)
614
615 =item x = ecb_div_ru (val, div)
616
617 Returns C<val> divided by C<div> rounded down or up, respectively.
618 C<val> and C<div> must have integer types and C<div> must be strictly
619 positive. Note that these functions are implemented with macros in C
620 and with function templates in C++.
621
622 =back
623
624 =head2 UTILITY
625
626 =over 4
627
628 =item element_count = ecb_array_length (name)
629
630 Returns the number of elements in the array C<name>. For example:
631
632 int primes[] = { 2, 3, 5, 7, 11 };
633 int sum = 0;
634
635 for (i = 0; i < ecb_array_length (primes); i++)
636 sum += primes [i];
637
638 =back
639
640 =head2 SYMBOLS GOVERNING COMPILATION OF ECB.H ITSELF
641
642 These symbols need to be defined before including F<ecb.h> the first time.
643
644 =over 4
645
646 =item ECB_NO_THRADS
647
648 If F<ecb.h> is never used from multiple threads, then this symbol can
649 be defined, in which case memory fences (and similar constructs) are
650 completely removed, leading to more efficient code and fewer dependencies.
651
652 Setting this symbol to a true value implies C<ECB_NO_SMP>.
653
654 =item ECB_NO_SMP
655
656 The weaker version of C<ECB_NO_THREADS> - if F<ecb.h> is used from
657 multiple threads, but never concurrently (e.g. if the system the program
658 runs on has only a single CPU with a single core, no hyperthreading and so
659 on), then this symbol can be defined, leading to more efficient code and
660 fewer dependencies.
661
662 =back
663
664