… | |
… | |
55 | is usually implemented as a macro. Specifically, a "bool" in this manual |
55 | is usually implemented as a macro. Specifically, a "bool" in this manual |
56 | refers to any kind of boolean value, not a specific type. |
56 | refers to any kind of boolean value, not a specific type. |
57 | |
57 | |
58 | =head2 GCC ATTRIBUTES |
58 | =head2 GCC ATTRIBUTES |
59 | |
59 | |
60 | blabla where to put, what others |
60 | A major part of libecb deals with GCC attributes. These are additional |
|
|
61 | attributes that you cna assign to functions, variables and sometimes even |
|
|
62 | types - much like C<const> or C<volatile> in C. |
|
|
63 | |
|
|
64 | While GCC allows declarations to show up in many surprising places, |
|
|
65 | but not in many expeted places, the safest way is to put attribute |
|
|
66 | declarations before the whole declaration: |
|
|
67 | |
|
|
68 | ecb_const int mysqrt (int a); |
|
|
69 | ecb_unused int i; |
|
|
70 | |
|
|
71 | For variables, it is often nicer to put the attribute after the name, and |
|
|
72 | avoid multiple declarations using commas: |
|
|
73 | |
|
|
74 | int i ecb_unused; |
61 | |
75 | |
62 | =over 4 |
76 | =over 4 |
63 | |
77 | |
64 | =item ecb_attribute ((attrs...)) |
78 | =item ecb_attribute ((attrs...)) |
65 | |
79 | |
… | |
… | |
105 | { |
119 | { |
106 | puts (errline); |
120 | puts (errline); |
107 | abort (); |
121 | abort (); |
108 | } |
122 | } |
109 | |
123 | |
110 | In this case, the compiler would probbaly be smart enough to decude it on |
124 | In this case, the compiler would probably be smart enough to deduce it on |
111 | it's own, so this is mainly useful for declarations. |
125 | its own, so this is mainly useful for declarations. |
112 | |
126 | |
113 | =item ecb_const |
127 | =item ecb_const |
114 | |
128 | |
115 | Declares that the function only depends on the values of it's arguments, |
129 | Declares that the function only depends on the values of its arguments, |
116 | much like a mathematical function. It specifically does not read or write |
130 | much like a mathematical function. It specifically does not read or write |
117 | any memory any arguments might point to, global variables, or call any |
131 | any memory any arguments might point to, global variables, or call any |
118 | non-const functions. It also must not have any side effects. |
132 | non-const functions. It also must not have any side effects. |
119 | |
133 | |
120 | Such a function can be optimised much more aggressively by the compiler - |
134 | Such a function can be optimised much more aggressively by the compiler - |
121 | for example, multiple calls with the same arguments can be optimised into |
135 | for example, multiple calls with the same arguments can be optimised into |
122 | a single call, which wouldn't be possible if the compiler would have to |
136 | a single call, which wouldn't be possible if the compiler would have to |
123 | expect any side effects. |
137 | expect any side effects. |
124 | |
138 | |
125 | It is best suited for functions in the sense of mathematical functions, |
139 | It is best suited for functions in the sense of mathematical functions, |
126 | such as a function return the square root of its input argument. |
140 | such as a function returning the square root of its input argument. |
127 | |
141 | |
128 | Not suited would be a function that calculates the hash of some memory |
142 | Not suited would be a function that calculates the hash of some memory |
129 | area you pass in, prints some messages or looks at a global variable to |
143 | area you pass in, prints some messages or looks at a global variable to |
130 | decide on rounding. |
144 | decide on rounding. |
131 | |
145 | |
… | |
… | |
154 | possible. |
168 | possible. |
155 | |
169 | |
156 | The compiler reacts by trying to place hot functions near to each other in |
170 | The compiler reacts by trying to place hot functions near to each other in |
157 | memory. |
171 | memory. |
158 | |
172 | |
159 | Whether a function is hot or not often depend son the whole program, |
173 | Whether a function is hot or not often depends on the whole program, |
160 | and less on the function itself. C<ecb_cold> is likely more useful in |
174 | and less on the function itself. C<ecb_cold> is likely more useful in |
161 | practise. |
175 | practise. |
162 | |
176 | |
163 | =item ecb_cold |
177 | =item ecb_cold |
164 | |
178 | |
… | |
… | |
169 | |
183 | |
170 | In addition to placing cold functions together (or at least away from hot |
184 | In addition to placing cold functions together (or at least away from hot |
171 | functions), this knowledge can be used in other ways, for example, the |
185 | functions), this knowledge can be used in other ways, for example, the |
172 | function will be optimised for size, as opposed to speed, and codepaths |
186 | function will be optimised for size, as opposed to speed, and codepaths |
173 | leading to calls to those functions can automatically be marked as if |
187 | leading to calls to those functions can automatically be marked as if |
174 | C<ecb_unlikel> had been used to reach them. |
188 | C<ecb_unlikely> had been used to reach them. |
175 | |
189 | |
176 | Good examples for such functions would be error reporting functions, or |
190 | Good examples for such functions would be error reporting functions, or |
177 | functions only called in exceptional or rare cases. |
191 | functions only called in exceptional or rare cases. |
178 | |
192 | |
179 | =item ecb_artificial |
193 | =item ecb_artificial |
… | |
… | |
394 | |
408 | |
395 | =item uint32_t ecb_bswap16 (uint32_t x) |
409 | =item uint32_t ecb_bswap16 (uint32_t x) |
396 | |
410 | |
397 | =item uint32_t ecb_bswap32 (uint32_t x) |
411 | =item uint32_t ecb_bswap32 (uint32_t x) |
398 | |
412 | |
399 | These two functions return the value of the 16-bit (32-bit) variable |
413 | These two functions return the value of the 16-bit (32-bit) value C<x> |
400 | C<x> after reversing the order of bytes. |
414 | after reversing the order of bytes (0x11223344 becomes 0x44332211). |
401 | |
415 | |
402 | =item uint32_t ecb_rotr32 (uint32_t x, unsigned int count) |
416 | =item uint32_t ecb_rotr32 (uint32_t x, unsigned int count) |
403 | |
417 | |
404 | =item uint32_t ecb_rotl32 (uint32_t x, unsigned int count) |
418 | =item uint32_t ecb_rotl32 (uint32_t x, unsigned int count) |
405 | |
419 | |
406 | These two functions return the value of C<x> after shifting all the bits |
420 | These two functions return the value of C<x> after rotating all the bits |
407 | by C<count> positions to the right or left respectively. |
421 | by C<count> positions to the right or left respectively. |
|
|
422 | |
|
|
423 | Current GCC versions understand these functions and usually compile them |
|
|
424 | to "optimal" code (e.g. a single C<roll> on x86). |
408 | |
425 | |
409 | =back |
426 | =back |
410 | |
427 | |
411 | =head2 ARITHMETIC |
428 | =head2 ARITHMETIC |
412 | |
429 | |
… | |
… | |
414 | |
431 | |
415 | =item x = ecb_mod (m, n) |
432 | =item x = ecb_mod (m, n) |
416 | |
433 | |
417 | Returns the positive remainder of the modulo operation between C<m> and |
434 | Returns the positive remainder of the modulo operation between C<m> and |
418 | C<n>. Unlike the C modulo operator C<%>, this function ensures that the |
435 | C<n>. Unlike the C modulo operator C<%>, this function ensures that the |
419 | return value is always positive). |
436 | return value is always positive - ISO C guarantees very little when |
|
|
437 | negative numbers are used with C<%>. |
420 | |
438 | |
421 | C<n> must be strictly positive (i.e. C<< >1 >>), while C<m> must be |
439 | C<n> must be strictly positive (i.e. C<< >1 >>), while C<m> must be |
422 | negatable, that is, both C<m> and C<-m> must be representable in its |
440 | negatable, that is, both C<m> and C<-m> must be representable in its |
423 | type. |
441 | type. |
424 | |
442 | |