toolchain: Add single arguments evaluation macros for min and max
MAX() and MIN() were evaluating arguments twice. If arguments are functions they were called twice which resulted in bigger code and potential misbehavior. Added alternative macros (Z_MAX, Z_MIN) which can be used instead. Macros have usage limitations thus they are not replacements. They are also relying on GCC extension thus placed in gcc.h Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
parent
6768148cc5
commit
bea966dea1
2 changed files with 38 additions and 0 deletions
|
@ -89,10 +89,20 @@ constexpr size_t ARRAY_SIZE(T(&)[N]) { return N; }
|
||||||
#define INLINE
|
#define INLINE
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/** @brief Return larger value of two provided expressions.
|
||||||
|
*
|
||||||
|
* @note Arguments are evaluated twice. See Z_MAX for GCC only, single
|
||||||
|
* evaluation version.
|
||||||
|
*/
|
||||||
#ifndef MAX
|
#ifndef MAX
|
||||||
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/** @brief Return smaller value of two provided expressions.
|
||||||
|
*
|
||||||
|
* @note Arguments are evaluated twice. See Z_MIN for GCC only, single
|
||||||
|
* evaluation version.
|
||||||
|
*/
|
||||||
#ifndef MIN
|
#ifndef MIN
|
||||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -415,4 +415,32 @@ do { \
|
||||||
__asm__ __volatile__ ("" ::: "memory"); \
|
__asm__ __volatile__ ("" ::: "memory"); \
|
||||||
} while (false)
|
} while (false)
|
||||||
|
|
||||||
|
/** @brief Return larger value of two provided expressions.
|
||||||
|
*
|
||||||
|
* Macro ensures that expressions are evaluated only once.
|
||||||
|
*
|
||||||
|
* @note Macro has limited usage compared to the standard macro as it cannot be
|
||||||
|
* used:
|
||||||
|
* - to generate constant integer, e.g. __aligned(Z_MAX(4,5))
|
||||||
|
* - static variable, e.g. array like static u8_t array[Z_MAX(...)];
|
||||||
|
*/
|
||||||
|
#define Z_MAX(a, b) ({ \
|
||||||
|
/* random suffix to avoid naming conflict */ \
|
||||||
|
__typeof__(a) _value_a_ = (a); \
|
||||||
|
__typeof__(b) _value_b_ = (b); \
|
||||||
|
_value_a_ > _value_b_ ? _value_a_ : _value_b_; \
|
||||||
|
})
|
||||||
|
|
||||||
|
/** @brief Return smaller value of two provided expressions.
|
||||||
|
*
|
||||||
|
* Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
|
||||||
|
* macro limitations.
|
||||||
|
*/
|
||||||
|
#define Z_MIN(a, b) ({ \
|
||||||
|
/* random suffix to avoid naming conflict */ \
|
||||||
|
__typeof__(a) _value_a_ = (a); \
|
||||||
|
__typeof__(b) _value_b_ = (b); \
|
||||||
|
_value_a_ < _value_b_ ? _value_a_ : _value_b_; \
|
||||||
|
})
|
||||||
|
|
||||||
#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_ */
|
#endif /* ZEPHYR_INCLUDE_TOOLCHAIN_GCC_H_ */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue