Skip to content

Commit 25578a0

Browse files
fabiobaltiericfriedt
authored andcommitted
sys: util: move Z_MIN, Z_MAX, Z_CLAMP into util.h
These three are single evaluation versiono of MIN, MAX and CLAMP, they are defined in toolchain headers as they use typeof [1] and statement expressions [2], which are compiler extensions, but given that these are used by other macros in util.h as well, it seems to be fair to assume that these are supported by all compilers used with Zephyr today. [1] https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html [2] https://gcc.gnu.org/onlinedocs/gcc/Typeof.html Signed-off-by: Fabio Baltieri <[email protected]>
1 parent a0b6ec6 commit 25578a0

File tree

3 files changed

+43
-86
lines changed

3 files changed

+43
-86
lines changed

include/zephyr/sys/util.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,22 @@ extern "C" {
387387
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
388388
#endif
389389

390+
/** @brief Return larger value of two provided expressions.
391+
*
392+
* Macro ensures that expressions are evaluated only once.
393+
*
394+
* @note Macro has limited usage compared to the standard macro as it cannot be
395+
* used:
396+
* - to generate constant integer, e.g. __aligned(Z_MAX(4,5))
397+
* - static variable, e.g. array like static uint8_t array[Z_MAX(...)];
398+
*/
399+
#define Z_MAX(a, b) ({ \
400+
/* random suffix to avoid naming conflict */ \
401+
__typeof__(a) _value_a_ = (a); \
402+
__typeof__(b) _value_b_ = (b); \
403+
(_value_a_ > _value_b_) ? _value_a_ : _value_b_; \
404+
})
405+
390406
#ifndef MIN
391407
/**
392408
* @brief Obtain the minimum of two values.
@@ -402,6 +418,18 @@ extern "C" {
402418
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
403419
#endif
404420

421+
/** @brief Return smaller value of two provided expressions.
422+
*
423+
* Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
424+
* macro limitations.
425+
*/
426+
#define Z_MIN(a, b) ({ \
427+
/* random suffix to avoid naming conflict */ \
428+
__typeof__(a) _value_a_ = (a); \
429+
__typeof__(b) _value_b_ = (b); \
430+
(_value_a_ < _value_b_) ? _value_a_ : _value_b_; \
431+
})
432+
405433
#ifndef MAX_FROM_LIST
406434
/**
407435
* @brief Returns the maximum of a single value (base case).
@@ -539,6 +567,21 @@ extern "C" {
539567
#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
540568
#endif
541569

570+
/** @brief Return a value clamped to a given range.
571+
*
572+
* Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
573+
* macro limitations.
574+
*/
575+
#define Z_CLAMP(val, low, high) ({ \
576+
/* random suffix to avoid naming conflict */ \
577+
__typeof__(val) _value_val_ = (val); \
578+
__typeof__(low) _value_low_ = (low); \
579+
__typeof__(high) _value_high_ = (high); \
580+
(_value_val_ < _value_low_) ? _value_low_ : \
581+
(_value_val_ > _value_high_) ? _value_high_ : \
582+
_value_val_; \
583+
})
584+
542585
/**
543586
* @brief Checks if a value is within range.
544587
*

include/zephyr/toolchain/gcc.h

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -626,49 +626,6 @@ do { \
626626
__asm__ __volatile__ ("" ::: "memory"); \
627627
} while (false)
628628

629-
/** @brief Return larger value of two provided expressions.
630-
*
631-
* Macro ensures that expressions are evaluated only once.
632-
*
633-
* @note Macro has limited usage compared to the standard macro as it cannot be
634-
* used:
635-
* - to generate constant integer, e.g. __aligned(Z_MAX(4,5))
636-
* - static variable, e.g. array like static uint8_t array[Z_MAX(...)];
637-
*/
638-
#define Z_MAX(a, b) ({ \
639-
/* random suffix to avoid naming conflict */ \
640-
__typeof__(a) _value_a_ = (a); \
641-
__typeof__(b) _value_b_ = (b); \
642-
(_value_a_ > _value_b_) ? _value_a_ : _value_b_; \
643-
})
644-
645-
/** @brief Return smaller value of two provided expressions.
646-
*
647-
* Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
648-
* macro limitations.
649-
*/
650-
#define Z_MIN(a, b) ({ \
651-
/* random suffix to avoid naming conflict */ \
652-
__typeof__(a) _value_a_ = (a); \
653-
__typeof__(b) _value_b_ = (b); \
654-
(_value_a_ < _value_b_) ? _value_a_ : _value_b_; \
655-
})
656-
657-
/** @brief Return a value clamped to a given range.
658-
*
659-
* Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
660-
* macro limitations.
661-
*/
662-
#define Z_CLAMP(val, low, high) ({ \
663-
/* random suffix to avoid naming conflict */ \
664-
__typeof__(val) _value_val_ = (val); \
665-
__typeof__(low) _value_low_ = (low); \
666-
__typeof__(high) _value_high_ = (high); \
667-
(_value_val_ < _value_low_) ? _value_low_ : \
668-
(_value_val_ > _value_high_) ? _value_high_ : \
669-
_value_val_; \
670-
})
671-
672629
/**
673630
* @brief Calculate power of two ceiling for some nonzero value
674631
*

include/zephyr/toolchain/iar/iccarm.h

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -320,49 +320,6 @@ do { \
320320
__asm volatile("" ::: "memory"); \
321321
} while (false)
322322

323-
/** @brief Return larger value of two provided expressions.
324-
*
325-
* Macro ensures that expressions are evaluated only once.
326-
*
327-
* @note Macro has limited usage compared to the standard macro as it cannot be
328-
* used:
329-
* - to generate constant integer, e.g. __aligned(Z_MAX(4,5))
330-
* - static variable, e.g. array like static uint8_t array[Z_MAX(...)];
331-
*/
332-
#define Z_MAX(a, b) ({ \
333-
/* random suffix to avoid naming conflict */ \
334-
__typeof__(a) _value_a_ = (a); \
335-
__typeof__(b) _value_b_ = (b); \
336-
_value_a_ > _value_b_ ? _value_a_ : _value_b_; \
337-
})
338-
339-
/** @brief Return smaller value of two provided expressions.
340-
*
341-
* Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
342-
* macro limitations.
343-
*/
344-
#define Z_MIN(a, b) ({ \
345-
/* random suffix to avoid naming conflict */ \
346-
__typeof__(a) _value_a_ = (a); \
347-
__typeof__(b) _value_b_ = (b); \
348-
_value_a_ < _value_b_ ? _value_a_ : _value_b_; \
349-
})
350-
351-
/** @brief Return a value clamped to a given range.
352-
*
353-
* Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
354-
* macro limitations.
355-
*/
356-
#define Z_CLAMP(val, low, high) ({ \
357-
/* random suffix to avoid naming conflict */ \
358-
__typeof__(val) _value_val_ = (val); \
359-
__typeof__(low) _value_low_ = (low); \
360-
__typeof__(high) _value_high_ = (high); \
361-
(_value_val_ < _value_low_) ? _value_low_ : \
362-
(_value_val_ > _value_high_) ? _value_high_ : \
363-
_value_val_; \
364-
})
365-
366323
/**
367324
* @brief Calculate power of two ceiling for some nonzero value
368325
*

0 commit comments

Comments
 (0)