Split cpu/cpu.h in 3 files: irq, types and attr.
[bertos.git] / cfg / macros.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Common and handy function macros
34  *
35  * \version $Id$
36  * \author Bernardo Innocenti <bernie@develer.com>
37  * \author Giovanni Bajo <rasky@develer.com>
38  */
39
40 /*#*
41  *#* $Log$
42  *#* Revision 1.11  2007/02/06 15:22:12  asterix
43  *#* Add ROTL and ROTR macros for bit rotating.
44  *#*
45  *#* Revision 1.10  2006/09/13 18:31:37  bernie
46  *#* BV8(), BV16(), BV32(): New macros for CPUs with small word size; SWAP_T(): New macro to support old compilers.
47  *#*
48  *#* Revision 1.9  2006/07/19 12:56:25  bernie
49  *#* Convert to new Doxygen style.
50  *#*
51  *#* Revision 1.8  2006/03/13 02:06:55  bernie
52  *#* ROUND_UP2: Rename from ROUND2.
53  *#*
54  *#* Revision 1.7  2006/02/20 14:34:58  bernie
55  *#* Use portable type checking.
56  *#*
57  *#* Revision 1.6  2006/02/10 12:36:57  bernie
58  *#* Pacify IAR warnings for side-effects.
59  *#*
60  *#* Revision 1.5  2005/11/04 16:20:01  bernie
61  *#* Fix reference to README.devlib in header.
62  *#*
63  *#* Revision 1.4  2005/07/03 15:19:09  bernie
64  *#* Doxygen fix.
65  *#*
66  *#* Revision 1.3  2005/06/27 21:23:32  bernie
67  *#* ROUND_DOWN, ROUND_UP, ROUND_NEAREST: New macros.
68  *#*
69  *#* Revision 1.2  2005/04/11 19:10:27  bernie
70  *#* Include top-level headers from cfg/ subdir.
71  *#*
72  *#* Revision 1.1  2005/04/11 19:04:13  bernie
73  *#* Move top-level headers to cfg/ subdir.
74  *#*
75  *#* Revision 1.10  2005/01/22 04:19:50  bernie
76  *#* Use non-uglified typeof().
77  *#*
78  *#* Revision 1.9  2004/12/08 08:51:34  bernie
79  *#* Add type-generic macros for C++.
80  *#*
81  *#* Revision 1.8  2004/10/19 07:14:20  bernie
82  *#* Add macros to test for specific compiler features.
83  *#*
84  *#* Revision 1.7  2004/09/20 03:30:45  bernie
85  *#* C++ also has variadic macros.
86  *#*
87  *#* Revision 1.6  2004/09/14 21:02:04  bernie
88  *#* SWAP(), MINMAX(): New macros.
89  *#*
90  *#* Revision 1.5  2004/08/29 21:57:58  bernie
91  *#* Move back STATIC_ASSERT() to compiler.h as it's needed in cpu.h;
92  *#* iptr_t, const_iptr_t: Replace IPTR macro with a real typedef.
93  *#*
94  *#* Revision 1.3  2004/08/24 14:13:48  bernie
95  *#* Restore a few macros that were lost in the way.
96  *#*
97  *#* Revision 1.2  2004/08/24 13:32:14  bernie
98  *#* PP_CAT(), PP_STRINGIZE(): Move back to compiler.h to break circular dependency between cpu.h/compiler.h/macros.h;
99  *#* offsetof(), countof(): Move back to compiler.h to avoid including macros.h almost everywhere;
100  *#* Trim CVS log;
101  *#* Rename header guards;
102  *#* Don't include arch_config.h in compiler.h as it's not needed there.
103  *#*
104  *#* Revision 1.1  2004/08/14 19:37:57  rasky
105  *#* Merge da SC: macros.h, pool.h, BIT_CHANGE, nome dei processi, etc.
106  *#*
107  *#* Revision 1.4  2004/08/14 18:36:50  rasky
108  *#* Doxygen fix e un livello di parentesi aggiuntivi per la macro
109  *#*
110  *#* Revision 1.3  2004/08/12 20:01:32  rasky
111  *#* Aggiunte macro BIT_CHANGE e BIT_CHANGE_BV
112  *#*
113  *#* Revision 1.2  2004/08/10 21:36:14  rasky
114  *#* Aggiunto include macros.h dove serve
115  *#* Aggiunta dipendenza da compiler.h in macros.h
116  *#*
117  *#* Revision 1.1  2004/08/10 21:30:00  rasky
118  *#* Estratte le funzioni macro in macros.h
119  *#*
120  *#*/
121
122 #ifndef MACROS_H
123 #define MACROS_H
124
125 #include <cfg/compiler.h>
126
127 /* avr-gcc does not seem to support libstdc++ */
128 #if defined(__cplusplus) && !CPU_AVR
129         /* Type-generic macros implemented with template functions. */
130         #include <algorithm>
131
132         template<class T> inline T ABS(T n) { return n >= 0 ? n : -n; }
133         #define MIN(a,b)   std::min(a, b)
134         #define MAX(a,b)   std::max(a, b)
135         #define SWAP(a,b)  std::swap(a, b)
136 #elif (COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF)
137         /* Type-generic macros implemented with statement expressions. */
138         #define ABS(n) ({ \
139                 typeof(n) _n = (n); \
140                 (_n < 0) ? -_n : _n; \
141         })
142         #define MIN(a,b) ({ \
143                 typeof(a) _a = (a); \
144                 typeof(b) _b = (b); \
145                 ASSERT_TYPE_EQUAL(_a, _b); \
146                 (_a < _b) ? _a : _b; \
147         })
148         #define MAX(a,b) ({ \
149                 typeof(a) _a = (a); \
150                 typeof(b) _b = (b); \
151                 ASSERT_TYPE_EQUAL(_a, _b); \
152                 (_a > _b) ? _a : _b; \
153         })
154 #else /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */
155         /* Buggy macros for inferior compilers.  */
156         #define ABS(a)          (((a) < 0) ? -(a) : (a))
157         #define MIN(a,b)        (((a) < (b)) ? (a) : (b))
158         #define MAX(a,b)        (((a) > (b)) ? (a) : (b))
159 #endif /* !(COMPILER_STATEMENT_EXPRESSIONS && COMPILER_TYPEOF) */
160
161 /** Bound \a x between \a min and \a max. */
162 #define MINMAX(min,x,max)  (MIN(MAX(min, x), max))
163
164 #ifdef __cplusplus
165         /* Use standard implementation from <algorithm> */
166         #define SWAP(a,b)  std::swap(a, b)
167 #elif COMPILER_TYPEOF
168         /**
169          * Type-generic macro to swap \a a with \a b.
170          *
171          * \note Arguments are evaluated multiple times.
172          */
173         #define SWAP(a, b) \
174                 do { \
175                         typeof(a) tmp; \
176                         ASSERT_TYPE_EQUAL(a, b); \
177                         tmp = (a); \
178                         (a) = (b); \
179                         (b) = tmp; \
180                 } while (0)
181 #else /* !COMPILER_TYPEOF */
182         /* Sub-optimal implementation that only works with integral types. */
183         #define SWAP(a, b) \
184                 do { \
185                         (a) ^= (b); \
186                         (b) ^= (a); \
187                         (a) ^= (b); \
188                 } while (0)
189
190 #endif /* COMPILER_TYPEOF */
191
192 /**
193  * Macro to swap \a a with \a b, with explicit type \a T for dumb C89 compilers.
194  *
195  * \note Arguments are evaluated multiple times.
196  */
197 #define SWAP_T(a, b, T) \
198         do { \
199                 T tmp; \
200                 ASSERT_TYPE_IS(a, T); \
201                 ASSERT_TYPE_IS(b, T); \
202                 tmp = (a); \
203                 (a) = (b); \
204                 (b) = tmp; \
205         } while (0)
206
207
208 #ifndef BV
209         /** Convert a bit value to a binary flag. */
210         #define BV(x)  (1<<(x))
211 #endif
212
213 /** Same as BV() but with 32 bit result */
214 #define BV32(x)  ((uint32_t)1<<(x))
215
216 /** Same as BV() but with 16 bit result */
217 #define BV16(x)  ((uint16_t)1<<(x))
218
219 /** Same as BV() but with 8 bit result */
220 #define BV8(x)  ((uint8_t)1<<(x))
221
222
223
224 /** Round up \a x to an even multiple of the 2's power \a pad. */
225 #define ROUND_UP2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
226
227 /* OBSOLETE */
228 #define ROUND2 ROUND_UP2
229
230 /**
231  * \name Integer round macros.
232  *
233  * Round \a x to a multiple of \a base.
234  * \note If \a x is signed these macros generate a lot of code.
235  * \{
236  */
237 #define ROUND_DOWN(x, base)    ( (x) - ((x) % (base)) )
238 #define ROUND_UP(x, base)      ( ((x) + (base) - 1) - (((x) + (base) - 1) % (base)) )
239 #define ROUND_NEAREST(x, base) ( ((x) + (base) / 2) - (((x) + (base) / 2) % (base)) )
240 /* \} */
241
242 /** Check if \a x is an integer power of 2. */
243 #define IS_POW2(x)     (!(bool)((x) & ((x)-1)))
244
245 /** Calculate a compile-time log2 for a uint8_t */
246 #define UINT8_LOG2(x) \
247         ((x) < 2 ? 0 : \
248          ((x) < 4 ? 1 : \
249           ((x) < 8 ? 2 : \
250            ((x) < 16 ? 3 : \
251             ((x) < 32 ? 4 : \
252              ((x) < 64 ? 5 : \
253               ((x) < 128 ? 6 : 7)))))))
254
255 /** Calculate a compile-time log2 for a uint16_t */
256 #define UINT16_LOG2(x) \
257         ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
258
259 /** Calculate a compile-time log2 for a uint32_t */
260 #define UINT32_LOG2(x) \
261         ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
262
263 #if COMPILER_VARIADIC_MACROS
264         /** Count the number of arguments (up to 16). */
265         #define PP_COUNT(...) \
266                 PP_COUNT__(__VA_ARGS__,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
267         #define PP_COUNT__(a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,count,...) \
268                 count
269 #endif
270
271 #if COMPILER_VARIADIC_MACROS
272         /**
273          * \def BIT_CHANGE(reg, (mask, value), ...)
274          *
275          * This macro allows for efficient and compact bit toggling in a hardware
276          * register. It is meant to replace hand-coded cruft which toggles bits
277          * in sequence.
278          *
279          * It is possible to specify an unlimited pair of (mask, value) parameters.
280          * For instance:
281          *
282          * \code
283          * void set_timer(bool start)
284          * {
285          *     BIT_CHANGE(REG_CTRL_TIMER,
286          *        (TIMER_MODE, MODE_COUNT),
287          *        (OVL_IRQ, 1),
288          *        (CMP_IRQ, 1),
289          *        (START, start)
290          *     );
291          * }
292          * \endcode
293          *
294          * The macro expansion will be roughly the following:
295          *
296          * \code
297          * REG_CTRL_TIMER = (REG_CTRL_TIMER & ~(TIMER_MODE|OVL_IRQ|CMP_IRQ|START)
298          *                  | (MODE_COUNT|OVL_IRQ|CMP_IRQ|(start ? START : 0));
299          * \endcode
300          *
301          * It is up to the compiler to produce the optimal code. We checked that GCC produces
302          * the best code in most cases. We preferred this expansion over the use of a block
303          * with a local variable because CodeWarrior 6.1 was not able to remove completely the
304          * allocation of the local from the stack.
305          *
306          * \note This macro is available only in C99 because it makes use of variadic macros.
307          * It would be possible to make up an implementation with a slightly different syntax
308          * for use with C90 compilers, through Boost Preprocessor.
309          */
310
311         /**
312          * \def BIT_CHANGE_BV(reg, (bit, value), ...)
313          *
314          * Similar to BIT_CHANGE(), but get bits instead of masks (and applies BV() to convert
315          * them to masks).
316          */
317
318         #define BIT_EXTRACT_FLAG_0(bit, value)  bit
319         #define BIT_EXTRACT_FLAG_1(bit, value)  BV(bit)
320         #define BIT_EXTRACT_VALUE__(bit, value) value
321
322         #define BIT_MASK_SINGLE__(use_bv, index, max, arg) \
323                 ((index < max) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
324                 /**/
325
326         #define BIT_MASK_IF_SINGLE__(use_bv, index, max, arg) \
327                 (((index < max) && (BIT_EXTRACT_VALUE__ arg)) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
328                 /**/
329
330         #define BIT_ITER__2(macro, use_bv, max, a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15, ...) \
331                 (macro(use_bv, 0, max, a0) | \
332                 macro(use_bv, 1, max, a1) | \
333                 macro(use_bv, 2, max, a2) | \
334                 macro(use_bv, 3, max, a3) | \
335                 macro(use_bv, 4, max, a4) | \
336                 macro(use_bv, 5, max, a5) | \
337                 macro(use_bv, 6, max, a6) | \
338                 macro(use_bv, 7, max, a7) | \
339                 macro(use_bv, 8, max, a8) | \
340                 macro(use_bv, 9, max, a9) | \
341                 macro(use_bv, 10, max, a10) | \
342                 macro(use_bv, 11, max, a11) | \
343                 macro(use_bv, 12, max, a12) | \
344                 macro(use_bv, 13, max, a13) | \
345                 macro(use_bv, 14, max, a14) | \
346                 macro(use_bv, 15, max, a15)) \
347                 /**/
348
349         #define BIT_ITER__(macro, use_bv, ...) \
350                 BIT_ITER__2(macro, use_bv, PP_COUNT(__VA_ARGS__), __VA_ARGS__, (0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1),(0,1)) \
351                 /**/
352
353         #define BIT_MASKS__(use_bv, ...) \
354                 BIT_ITER__(BIT_MASK_SINGLE__, use_bv, __VA_ARGS__)
355                 /**/
356
357         #define BIT_MASKS_CONDITIONAL__(use_bv, ...) \
358                 BIT_ITER__(BIT_MASK_IF_SINGLE__, use_bv, __VA_ARGS__)
359                 /**/
360
361         #define BIT_CHANGE__(reg, use_bv, ...) \
362                 ((reg) = ((reg) & ~BIT_MASKS__(use_bv, __VA_ARGS__)) | BIT_MASKS_CONDITIONAL__(use_bv, __VA_ARGS__)) \
363                 /**/
364
365         #define BIT_CHANGE(reg, ...)        BIT_CHANGE__(reg, 0, __VA_ARGS__)
366         #define BIT_CHANGE_BV(reg, ...)     BIT_CHANGE__(reg, 1, __VA_ARGS__)
367
368 #endif /* COMPILER_VARIADIC_MACROS */
369
370 /**
371  * Macro for rotating bit left or right.
372  * \{
373  */
374 #define ROTR(var, rot) (((var) >> (rot)) | ((var) << ((sizeof(var) * 8) - (rot))))
375 #define ROTL(var, rot) (((var) << (rot)) | ((var) >> ((sizeof(var) * 8) - (rot))))
376 /*\}*/
377
378 #endif /* MACROS_H */
379