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

Comparing libecb/ecb.pod (file contents):
Revision 1.24 by root, Tue May 31 21:10:37 2011 UTC vs.
Revision 1.28 by root, Fri Jun 10 11:51:03 2011 UTC

57refers to any kind of boolean value, not a specific type. 57refers to any kind of boolean value, not a specific type.
58 58
59=head2 GCC ATTRIBUTES 59=head2 GCC ATTRIBUTES
60 60
61A major part of libecb deals with GCC attributes. These are additional 61A major part of libecb deals with GCC attributes. These are additional
62attributes that you cna assign to functions, variables and sometimes even 62attributes that you can assign to functions, variables and sometimes even
63types - much like C<const> or C<volatile> in C. 63types - much like C<const> or C<volatile> in C.
64 64
65While GCC allows declarations to show up in many surprising places, 65While GCC allows declarations to show up in many surprising places,
66but not in many expeted places, the safest way is to put attribute 66but not in many expected places, the safest way is to put attribute
67declarations before the whole declaration: 67declarations before the whole declaration:
68 68
69 ecb_const int mysqrt (int a); 69 ecb_const int mysqrt (int a);
70 ecb_unused int i; 70 ecb_unused int i;
71 71
184 184
185In addition to placing cold functions together (or at least away from hot 185In addition to placing cold functions together (or at least away from hot
186functions), this knowledge can be used in other ways, for example, the 186functions), this knowledge can be used in other ways, for example, the
187function will be optimised for size, as opposed to speed, and codepaths 187function will be optimised for size, as opposed to speed, and codepaths
188leading to calls to those functions can automatically be marked as if 188leading to calls to those functions can automatically be marked as if
189C<ecb_unlikely> had been used to reach them. 189C<ecb_expect_false> had been used to reach them.
190 190
191Good examples for such functions would be error reporting functions, or 191Good examples for such functions would be error reporting functions, or
192functions only called in exceptional or rare cases. 192functions only called in exceptional or rare cases.
193 193
194=item ecb_artificial 194=item ecb_artificial
256 256
257Evaluates C<expr> and returns it. In addition, it tells the compiler that 257Evaluates C<expr> and returns it. In addition, it tells the compiler that
258the C<expr> evaluates to C<value> a lot, which can be used for static 258the C<expr> evaluates to C<value> a lot, which can be used for static
259branch optimisations. 259branch optimisations.
260 260
261Usually, you want to use the more intuitive C<ecb_likely> and 261Usually, you want to use the more intuitive C<ecb_expect_true> and
262C<ecb_unlikely> functions instead. 262C<ecb_expect_false> functions instead.
263 263
264=item bool ecb_expect_true (cond)
265
264=item bool ecb_likely (cond) 266=item bool ecb_expect_false (cond)
265
266=item bool ecb_unlikely (cond)
267 267
268These two functions expect a expression that is true or false and return 268These two functions expect a expression that is true or false and return
269C<1> or C<0>, respectively, so when used in the condition of an C<if> or 269C<1> or C<0>, respectively, so when used in the condition of an C<if> or
270other conditional statement, it will not change the program: 270other conditional statement, it will not change the program:
271 271
272 /* these two do the same thing */ 272 /* these two do the same thing */
273 if (some_condition) ...; 273 if (some_condition) ...;
274 if (ecb_likely (some_condition)) ...; 274 if (ecb_expect_true (some_condition)) ...;
275 275
276However, by using C<ecb_likely>, you tell the compiler that the condition 276However, by using C<ecb_expect_true>, you tell the compiler that the
277is likely to be true (and for C<ecb_unlikely>, that it is unlikely to be 277condition is likely to be true (and for C<ecb_expect_false>, that it is
278true). 278unlikely to be true).
279 279
280For example, when you check for a null pointer and expect this to be a 280For example, when you check for a null pointer and expect this to be a
281rare, exceptional, case, then use C<ecb_unlikely>: 281rare, exceptional, case, then use C<ecb_expect_false>:
282 282
283 void my_free (void *ptr) 283 void my_free (void *ptr)
284 { 284 {
285 if (ecb_unlikely (ptr == 0)) 285 if (ecb_expect_false (ptr == 0))
286 return; 286 return;
287 } 287 }
288 288
289Consequent use of these functions to mark away exceptional cases or to 289Consequent use of these functions to mark away exceptional cases or to
290tell the compiler what the hot path through a function is can increase 290tell the compiler what the hot path through a function is can increase
291performance considerably. 291performance considerably.
292
293You might know these functions under the name C<likely> and C<unlikely>
294- while these are common aliases, we find that the expect name is easier
295to understand when quickly skimming code. If you wish, you can use
296C<ecb_likely> instead of C<ecb_expect_true> and C<ecb_unlikely> instead of
297C<ecb_expect_false> - these are simply aliases.
292 298
293A very good example is in a function that reserves more space for some 299A very good example is in a function that reserves more space for some
294memory block (for example, inside an implementation of a string stream) - 300memory block (for example, inside an implementation of a string stream) -
295each time something is added, you have to check for a buffer overrun, but 301each time something is added, you have to check for a buffer overrun, but
296you expect that most checks will turn out to be false: 302you expect that most checks will turn out to be false:
297 303
298 /* make sure we have "size" extra room in our buffer */ 304 /* make sure we have "size" extra room in our buffer */
299 ecb_inline void 305 ecb_inline void
300 reserve (int size) 306 reserve (int size)
301 { 307 {
302 if (ecb_unlikely (current + size > end)) 308 if (ecb_expect_false (current + size > end))
303 real_reserve_method (size); /* presumably noinline */ 309 real_reserve_method (size); /* presumably noinline */
304 } 310 }
305 311
306=item bool ecb_assume (cond) 312=item bool ecb_assume (cond)
307 313
310 316
311This can be used to teach the compiler about invariants or other 317This can be used to teach the compiler about invariants or other
312conditions that might improve code generation, but which are impossible to 318conditions that might improve code generation, but which are impossible to
313deduce form the code itself. 319deduce form the code itself.
314 320
315For example, the example reservation function from the C<ecb_unlikely> 321For example, the example reservation function from the C<ecb_expect_false>
316description could be written thus (only C<ecb_assume> was added): 322description could be written thus (only C<ecb_assume> was added):
317 323
318 ecb_inline void 324 ecb_inline void
319 reserve (int size) 325 reserve (int size)
320 { 326 {
321 if (ecb_unlikely (current + size > end)) 327 if (ecb_expect_false (current + size > end))
322 real_reserve_method (size); /* presumably noinline */ 328 real_reserve_method (size); /* presumably noinline */
323 329
324 ecb_assume (current + size <= end); 330 ecb_assume (current + size <= end);
325 } 331 }
326 332
432 438
433=over 4 439=over 4
434 440
435=item x = ecb_mod (m, n) 441=item x = ecb_mod (m, n)
436 442
437Returns the positive remainder of the modulo operation between C<m> and 443Returns C<m> modulo C<n>, which is the same as the positive remainder
438C<n>, using floored division. Unlike the C modulo operator C<%>, this 444of the division operation between C<m> and C<n>, using floored
439function ensures that the return value is always positive and that the two 445division. Unlike the C remainder operator C<%>, this function ensures that
446the return value is always positive and that the two numbers I<m> and
440numbers I<m> and I<m' = m + i * n> result in the same value modulo I<n> - 447I<m' = m + i * n> result in the same value modulo I<n> - in other words,
441the C<%> operator usually has a behaviour change at C<m = 0>. 448C<ecb_mod> implements the mathematical modulo operation, which is missing
449in the language.
442 450
443C<n> must be strictly positive (i.e. C<< >= 1 >>), while C<m> must be 451C<n> must be strictly positive (i.e. C<< >= 1 >>), while C<m> must be
444negatable, that is, both C<m> and C<-m> must be representable in its 452negatable, that is, both C<m> and C<-m> must be representable in its
445type. 453type (this typically includes the minimum signed integer value, the same
454limitation as for C</> and C<%> in C).
446 455
447Current GCC versions compile this into an efficient branchless sequence on 456Current GCC versions compile this into an efficient branchless sequence on
448many systems. 457almost all CPUs.
449 458
450For example, when you want to rotate forward through the members of an 459For example, when you want to rotate forward through the members of an
451array for increasing C<m> (which might be negative), then you should use 460array for increasing C<m> (which might be negative), then you should use
452C<ecb_mod>, as the C<%> operator might give either negative results, or 461C<ecb_mod>, as the C<%> operator might give either negative results, or
453change direction for negative values: 462change direction for negative values:

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines