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

Comparing libecb/ecb.pod (file contents):
Revision 1.3 by root, Thu May 26 20:04:38 2011 UTC vs.
Revision 1.14 by root, Thu May 26 23:23:08 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 memembers 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 endienness
18detection, byte swapping or bit rotations.
19
20More might come.
4 21
5=head2 ABOUT THE HEADER 22=head2 ABOUT THE HEADER
6 23
7- how to include it 24At the moment, all you have to do is copy F<ecb.h> somewhere where your
8- it includes inttypes.h 25compiler can find it and include it:
9- no .a 26
10- whats a bool 27 #include <ecb.h>
28
29The header should work fine for both C and C++ compilation, and gives you
30all of F<inttypes.h> in addition to the ECB symbols.
31
32There are currently no objetc files to link to - future versions might
33come with an (optional) object code library to link against, to reduce
34code size or gain access to additional features.
35
36It also currently includes everything from F<inttypes.h>.
37
38=head2 ABOUT THIS MANUAL / CONVENTIONS
39
40This manual mainly describes each (public) function available after
41including the F<ecb.h> header. The header might define other symbols than
42these, but these are not part of the public API, and not supported in any
43way.
44
45When the manual mentions a "function" then this could be defined either as
46as inline function, a macro, or an external symbol.
47
48When functions use a concrete standard type, such as C<int> or
49C<uint32_t>, then the corresponding function works only with that type. If
50only a generic name is used (C<expr>, C<cond>, C<value> and so on), then
51the corresponding function relies on C to implement the correct types, and
52is usually implemented as a macro. Specifically, a "bool" in this manual
53refers to any kind of boolean value, not a specific type.
11 54
12=head2 GCC ATTRIBUTES 55=head2 GCC ATTRIBUTES
13 56
14blabla where to put, what others 57blabla where to put, what others
15 58
37 #endif 80 #endif
38 } 81 }
39 82
40=item ecb_noinline 83=item ecb_noinline
41 84
42Prevent a function from being inlined - it might be optimsied away, but 85Prevent a function from being inlined - it might be optimised away, but
43not inlined into other functions. This is useful if you know your function 86not inlined into other functions. This is useful if you know your function
44is rarely called and large enough for inlining not to be helpful. 87is rarely called and large enough for inlining not to be helpful.
45 88
46=item ecb_noreturn 89=item ecb_noreturn
47 90
66Returns true iff the expression can be deduced to be a compile-time 109Returns true iff the expression can be deduced to be a compile-time
67constant, and false otherwise. 110constant, and false otherwise.
68 111
69For example, when you have a C<rndm16> function that returns a 16 bit 112For example, when you have a C<rndm16> function that returns a 16 bit
70random number, and you have a function that maps this to a range from 113random number, and you have a function that maps this to a range from
710..n-1, then you could use this inline fucntion in a header file: 1140..n-1, then you could use this inline function in a header file:
72 115
73 ecb_inline uint32_t 116 ecb_inline uint32_t
74 rndm (uint32_t n) 117 rndm (uint32_t n)
75 { 118 {
76 return n * (uint32_t)rndm16 ()) >> 16; 119 return (n * (uint32_t)rndm16 ()) >> 16;
77 } 120 }
78 121
79However, for powers of two, you could use a normal mask, but that is only 122However, for powers of two, you could use a normal mask, but that is only
80worth it if, at compile time, you can detect this case. This is the case 123worth it if, at compile time, you can detect this case. This is the case
81when the passed number is a constant and also a power of two (C<n & (n - 124when the passed number is a constant and also a power of two (C<n & (n -
84 ecb_inline uint32_t 127 ecb_inline uint32_t
85 rndm (uint32_t n) 128 rndm (uint32_t n)
86 { 129 {
87 return is_constant (n) && !(n & (n - 1)) 130 return is_constant (n) && !(n & (n - 1))
88 ? rndm16 () & (num - 1) 131 ? rndm16 () & (num - 1)
89 : (uint32_t)rndm16 ()) >> 16; 132 : (n * (uint32_t)rndm16 ()) >> 16;
90 }
91
92 133 }
93 134
94=item bool ecb_expect(expr,value) 135=item bool ecb_expect (expr, value)
95 136
137Evaluates C<expr> and returns it. In addition, it tells the compiler that
138the C<expr> evaluates to C<value> a lot, which can be used for static
139branch optimisations.
140
141Usually, you want to use the more intuitive C<ecb_likely> and
142C<ecb_unlikely> functions instead.
143
144=item bool ecb_likely (bool)
145
96=item bool ecb_unlikely(bool) 146=item bool ecb_unlikely (bool)
97 147
98=item bool ecb_likely(bool) 148These 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
150other conditional statement, it will not change the program:
99 151
152 /* these two do the same thing */
153 if (some_condition) ...;
154 if (ecb_likely (some_condition)) ...;
155
156However, by using C<ecb_likely>, you tell the compiler that the condition
157is likely to be true (and for C<ecb_unlikely>, that it is unlikely to be
158true).
159
160For example, when you check for a null pointer and expect this to be a
161rare, exceptional, case, then use C<ecb_unlikely>:
162
163 void my_free (void *ptr)
164 {
165 if (ecb_unlikely (ptr == 0))
166 return;
167 }
168
169Consequent use of these functions to mark away exceptional cases or to
170tell the compiler what the hot path through a function is can increase
171performance considerably.
172
173A very good example is in a function that reserves more space for some
174memory block (for example, inside an implementation of a string stream) -
175each time something is added, you have to check for a buffer overrun, but
176you expect that most checks will turn out to be false:
177
178 /* make sure we have "size" extra room in our buffer */
179 ecb_inline void
180 reserve (int size)
181 {
182 if (ecb_unlikely (current + size > end))
183 real_reserve_method (size); /* presumably noinline */
184 }
185
100=item bool ecb_assume(cond) 186=item bool ecb_assume (cond)
101 187
188Try to tell the compiler that some condition is true, even if it's not
189obvious.
190
191This can be used to teach the compiler about invariants or other
192conditions that might improve code generation, but which are impossible to
193deduce form the code itself.
194
195For example, the example reservation function from the C<ecb_unlikely>
196description could be written thus (only C<ecb_assume> was added):
197
198 ecb_inline void
199 reserve (int size)
200 {
201 if (ecb_unlikely (current + size > end))
202 real_reserve_method (size); /* presumably noinline */
203
204 ecb_assume (current + size <= end);
205 }
206
207If you then call this function twice, like this:
208
209 reserve (10);
210 reserve (1);
211
212Then the compiler I<might> be able to optimise out the second call
213completely, as it knows that C<< current + 1 > end >> is false and the
214call will never be executed.
215
102=item bool ecb_unreachable() 216=item bool ecb_unreachable ()
103 217
218This function does nothing itself, except tell the compiler that it will
219never be executed. Apart from suppressing a warning in some cases, this
220function can be used to implement C<ecb_assume> or similar functions.
221
104=item bool ecb_prefetch(addr,rw,locality) 222=item bool ecb_prefetch (addr, rw, locality)
223
224Tells the compiler to try to prefetch memory at the given C<addr>ess
225for either reading (C<rw> = 0) or writing (C<rw> = 1). A C<locality> of
226C<0> means that there will only be one access later, C<3> means that
227the data will likely be accessed very often, and values in between mean
228something... in between. The memory pointed to by the address does not
229need to be accessible (it could be a null pointer for example), but C<rw>
230and C<locality> must be compile-time constants.
231
232An obvious way to use this is to prefetch some data far away, in a big
233array you loop over. This prefetches memory some 128 array elements later,
234in the hope that it will be ready when the CPU arrives at that location.
235
236 int sum = 0;
237
238 for (i = 0; i < N; ++i)
239 {
240 sum += arr [i]
241 ecb_prefetch (arr + i + 128, 0, 0);
242 }
243
244It's hard to predict how far to prefetch, and most CPUs that can prefetch
245are often good enough to predict this kind of behaviour themselves. It
246gets more interesting with linked lists, especially when you do some fair
247processing on each list element:
248
249 for (node *n = start; n; n = n->next)
250 {
251 ecb_prefetch (n->next, 0, 0);
252 ... do medium amount of work with *n
253 }
254
255After processing the node, (part of) the next node might already be in
256cache.
105 257
106=back 258=back
107 259
108=head2 BIT FIDDLING / BITSTUFFS 260=head2 BIT FIDDLING / BITSTUFFS
109 261
262=over 4
263
110=item bool ecb_big_endian () 264=item bool ecb_big_endian ()
111 265
112=item bool ecb_little_endian () 266=item bool ecb_little_endian ()
113 267
268These two functions return true if the byte order is big endian
269(most-significant byte first) or little endian (least-significant byte
270first) respectively.
271
114=item int ecb_ctz32 (uint32_t x) 272=item int ecb_ctz32 (uint32_t x)
115 273
274Returns the index of the least significant bit set in C<x> (or
275equivalently 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
277common use case is to compute the integer binary logarithm, i.e.,
278floor(log2(n)). For example:
279
280 ecb_ctz32(3) = 0
281 ecb_ctz32(6) = 1
282
116=item int ecb_popcount32 (uint32_t x) 283=item int ecb_popcount32 (uint32_t x)
117 284
285Returns the number of bits set to 1 in C<x>. For example:
286
287 ecb_popcount32(7) = 3
288 ecb_popcount32(255) = 8
289
290=item uint32_t ecb_bswap16 (uint32_t x)
291
118=item uint32_t ecb_bswap32 (uint32_t x) 292=item uint32_t ecb_bswap32 (uint32_t x)
119 293
120=item uint32_t ecb_bswap16 (uint32_t x) 294These two functions return the value of the 16-bit (32-bit) variable
295C<x> after reversing the order of bytes.
121 296
122=item uint32_t ecb_rotr32 (uint32_t x, unsigned int count) 297=item uint32_t ecb_rotr32 (uint32_t x, unsigned int count)
123 298
124=item uint32_t ecb_rotl32 (uint32_t x, unsigned int count) 299=item uint32_t ecb_rotl32 (uint32_t x, unsigned int count)
125 300
301These two functions return the value of C<x> after shifting all the bits
302by C<count> positions to the right or left respectively.
303
126=back 304=back
127 305
128=head2 ARITHMETIC 306=head2 ARITHMETIC
129 307
130=over 4 308=over 4
131 309
132=item x = ecb_mod (m, n) [MACRO] 310=item x = ecb_mod (m, n)
311
312Returns the positive remainder of the modulo operation between C<m> and
313C<n>. Unlike the C moduloe operator C<%>, this function ensures that the
314return value is always positive).
315
316C<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
318type.
133 319
134=back 320=back
135 321
136=head2 UTILITY 322=head2 UTILITY
137 323
138=over 4 324=over 4
139 325
140=item ecb_array_length (name) [MACRO] 326=item element_count = ecb_array_length (name) [MACRO]
141 327
142=back 328Returns the number of elements in the array C<name>. For example:
143 329
330 int primes[] = { 2, 3, 5, 7, 11 };
331 int sum = 0;
144 332
333 for (i = 0; i < ecb_array_length (primes); i++)
334 sum += primes [i];
335
336=back
337
338

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines