4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
33 * \brief Common and handy function macros
36 * \author Bernardo Innocenti <bernie@develer.com>
37 * \author Giovanni Bajo <rasky@develer.com>
42 *#* Revision 1.11 2007/02/06 15:22:12 asterix
43 *#* Add ROTL and ROTR macros for bit rotating.
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.
48 *#* Revision 1.9 2006/07/19 12:56:25 bernie
49 *#* Convert to new Doxygen style.
51 *#* Revision 1.8 2006/03/13 02:06:55 bernie
52 *#* ROUND_UP2: Rename from ROUND2.
54 *#* Revision 1.7 2006/02/20 14:34:58 bernie
55 *#* Use portable type checking.
57 *#* Revision 1.6 2006/02/10 12:36:57 bernie
58 *#* Pacify IAR warnings for side-effects.
60 *#* Revision 1.5 2005/11/04 16:20:01 bernie
61 *#* Fix reference to README.devlib in header.
63 *#* Revision 1.4 2005/07/03 15:19:09 bernie
66 *#* Revision 1.3 2005/06/27 21:23:32 bernie
67 *#* ROUND_DOWN, ROUND_UP, ROUND_NEAREST: New macros.
69 *#* Revision 1.2 2005/04/11 19:10:27 bernie
70 *#* Include top-level headers from cfg/ subdir.
72 *#* Revision 1.1 2005/04/11 19:04:13 bernie
73 *#* Move top-level headers to cfg/ subdir.
75 *#* Revision 1.10 2005/01/22 04:19:50 bernie
76 *#* Use non-uglified typeof().
78 *#* Revision 1.9 2004/12/08 08:51:34 bernie
79 *#* Add type-generic macros for C++.
81 *#* Revision 1.8 2004/10/19 07:14:20 bernie
82 *#* Add macros to test for specific compiler features.
84 *#* Revision 1.7 2004/09/20 03:30:45 bernie
85 *#* C++ also has variadic macros.
87 *#* Revision 1.6 2004/09/14 21:02:04 bernie
88 *#* SWAP(), MINMAX(): New macros.
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.
94 *#* Revision 1.3 2004/08/24 14:13:48 bernie
95 *#* Restore a few macros that were lost in the way.
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;
101 *#* Rename header guards;
102 *#* Don't include arch_config.h in compiler.h as it's not needed there.
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.
107 *#* Revision 1.4 2004/08/14 18:36:50 rasky
108 *#* Doxygen fix e un livello di parentesi aggiuntivi per la macro
110 *#* Revision 1.3 2004/08/12 20:01:32 rasky
111 *#* Aggiunte macro BIT_CHANGE e BIT_CHANGE_BV
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
117 *#* Revision 1.1 2004/08/10 21:30:00 rasky
118 *#* Estratte le funzioni macro in macros.h
125 #include <cfg/compiler.h>
127 /* avr-gcc does not seem to support libstdc++ */
128 #if defined(__cplusplus) && !CPU_AVR
129 /* Type-generic macros implemented with template functions. */
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. */
139 typeof(n) _n = (n); \
140 (_n < 0) ? -_n : _n; \
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; \
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; \
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) */
161 /** Bound \a x between \a min and \a max. */
162 #define MINMAX(min,x,max) (MIN(MAX(min, x), max))
165 /* Use standard implementation from <algorithm> */
166 #define SWAP(a,b) std::swap(a, b)
167 #elif COMPILER_TYPEOF
169 * Type-generic macro to swap \a a with \a b.
171 * \note Arguments are evaluated multiple times.
176 ASSERT_TYPE_EQUAL(a, b); \
181 #else /* !COMPILER_TYPEOF */
182 /* Sub-optimal implementation that only works with integral types. */
190 #endif /* COMPILER_TYPEOF */
193 * Macro to swap \a a with \a b, with explicit type \a T for dumb C89 compilers.
195 * \note Arguments are evaluated multiple times.
197 #define SWAP_T(a, b, T) \
200 ASSERT_TYPE_IS(a, T); \
201 ASSERT_TYPE_IS(b, T); \
209 /** Convert a bit value to a binary flag. */
210 #define BV(x) (1<<(x))
213 /** Same as BV() but with 32 bit result */
214 #define BV32(x) ((uint32_t)1<<(x))
216 /** Same as BV() but with 16 bit result */
217 #define BV16(x) ((uint16_t)1<<(x))
219 /** Same as BV() but with 8 bit result */
220 #define BV8(x) ((uint8_t)1<<(x))
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))
228 #define ROUND2 ROUND_UP2
231 * \name Integer round macros.
233 * Round \a x to a multiple of \a base.
234 * \note If \a x is signed these macros generate a lot of code.
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)) )
242 /** Check if \a x is an integer power of 2. */
243 #define IS_POW2(x) (!(bool)((x) & ((x)-1)))
245 /** Calculate a compile-time log2 for a uint8_t */
246 #define UINT8_LOG2(x) \
253 ((x) < 128 ? 6 : 7)))))))
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)
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)
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,...) \
271 #if COMPILER_VARIADIC_MACROS
273 * \def BIT_CHANGE(reg, (mask, value), ...)
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
279 * It is possible to specify an unlimited pair of (mask, value) parameters.
283 * void set_timer(bool start)
285 * BIT_CHANGE(REG_CTRL_TIMER,
286 * (TIMER_MODE, MODE_COUNT),
294 * The macro expansion will be roughly the following:
297 * REG_CTRL_TIMER = (REG_CTRL_TIMER & ~(TIMER_MODE|OVL_IRQ|CMP_IRQ|START)
298 * | (MODE_COUNT|OVL_IRQ|CMP_IRQ|(start ? START : 0));
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.
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.
312 * \def BIT_CHANGE_BV(reg, (bit, value), ...)
314 * Similar to BIT_CHANGE(), but get bits instead of masks (and applies BV() to convert
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
322 #define BIT_MASK_SINGLE__(use_bv, index, max, arg) \
323 ((index < max) ? (PP_CAT(BIT_EXTRACT_FLAG_, use_bv) arg) : 0) \
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) \
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)) \
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)) \
353 #define BIT_MASKS__(use_bv, ...) \
354 BIT_ITER__(BIT_MASK_SINGLE__, use_bv, __VA_ARGS__)
357 #define BIT_MASKS_CONDITIONAL__(use_bv, ...) \
358 BIT_ITER__(BIT_MASK_IF_SINGLE__, use_bv, __VA_ARGS__)
361 #define BIT_CHANGE__(reg, use_bv, ...) \
362 ((reg) = ((reg) & ~BIT_MASKS__(use_bv, __VA_ARGS__)) | BIT_MASKS_CONDITIONAL__(use_bv, __VA_ARGS__)) \
365 #define BIT_CHANGE(reg, ...) BIT_CHANGE__(reg, 0, __VA_ARGS__)
366 #define BIT_CHANGE_BV(reg, ...) BIT_CHANGE__(reg, 1, __VA_ARGS__)
368 #endif /* COMPILER_VARIADIC_MACROS */
371 * Macro for rotating bit left or right.
374 #define ROTR(var, rot) (((var) >> (rot)) | ((var) << ((sizeof(var) * 8) - (rot))))
375 #define ROTL(var, rot) (((var) << (rot)) | ((var) >> ((sizeof(var) * 8) - (rot))))
378 #endif /* MACROS_H */