Skip to content

Commit d906376

Browse files
fabiobaltiericfriedt
authored andcommitted
sys: util: add Z_INTERNAL_MAX and Z_INTERNAL_MIN
Add two internal Z_INTERNAL_MAX and Z_INTERNAL_MIN macros and use those for implementing the actual MIN and MAX. This allows a downstream application to undefine and redefine MIN and MAX, for example to define them to min and max, which is useful if it relies on MIN and MAX to be the single call version. Signed-off-by: Fabio Baltieri <[email protected]>
1 parent 7e0d8f9 commit d906376

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

include/zephyr/sys/util.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,9 @@ extern "C" {
375375
/**
376376
* @cond INTERNAL_HIDDEN
377377
*/
378+
#define Z_INTERNAL_MAX(a, b) (((a) > (b)) ? (a) : (b))
379+
#define Z_INTERNAL_MIN(a, b) (((a) < (b)) ? (a) : (b))
380+
378381
#define _minmax_unique(op, a, b, ua, ub) ({ \
379382
__typeof__(a) ua = (a); \
380383
__typeof__(b) ub = (b); \
@@ -412,7 +415,7 @@ extern "C" {
412415
*
413416
* @returns Maximum value of @p a and @p b.
414417
*/
415-
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
418+
#define MAX(a, b) Z_INTERNAL_MAX(a, b)
416419
#endif
417420

418421
#ifndef __cplusplus
@@ -425,15 +428,15 @@ extern "C" {
425428
* - to generate constant integer, e.g. __aligned(max(4,5))
426429
* - static variable, e.g. array like static uint8_t array[max(...)];
427430
*/
428-
#define max(a, b) _minmax_cnt(MAX, a, b, __COUNTER__)
431+
#define max(a, b) _minmax_cnt(Z_INTERNAL_MAX, a, b, __COUNTER__)
429432
#endif
430433

431434
/** @brief Return larger value of three provided expressions.
432435
*
433436
* Macro ensures that expressions are evaluated only once. See @ref max for
434437
* macro limitations.
435438
*/
436-
#define max3(a, b, c) _minmax3_cnt(MAX, a, b, c, __COUNTER__)
439+
#define max3(a, b, c) _minmax3_cnt(Z_INTERNAL_MAX, a, b, c, __COUNTER__)
437440

438441
#ifndef MIN
439442
/**
@@ -447,7 +450,7 @@ extern "C" {
447450
*
448451
* @returns Minimum value of @p a and @p b.
449452
*/
450-
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
453+
#define MIN(a, b) Z_INTERNAL_MIN(a, b)
451454
#endif
452455

453456
#ifndef __cplusplus
@@ -456,15 +459,15 @@ extern "C" {
456459
* Macro ensures that expressions are evaluated only once. See @ref max for
457460
* macro limitations.
458461
*/
459-
#define min(a, b) _minmax_cnt(MIN, a, b, __COUNTER__)
462+
#define min(a, b) _minmax_cnt(Z_INTERNAL_MIN, a, b, __COUNTER__)
460463
#endif
461464

462465
/** @brief Return smaller value of three provided expressions.
463466
*
464467
* Macro ensures that expressions are evaluated only once. See @ref max for
465468
* macro limitations.
466469
*/
467-
#define min3(a, b, c) _minmax3_cnt(MIN, a, b, c, __COUNTER__)
470+
#define min3(a, b, c) _minmax3_cnt(Z_INTERNAL_MIN, a, b, c, __COUNTER__)
468471

469472

470473
#ifndef MAX_FROM_LIST
@@ -601,7 +604,7 @@ extern "C" {
601604
*
602605
* @returns Clamped value.
603606
*/
604-
#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
607+
#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : Z_INTERNAL_MIN(val, high))
605608
#endif
606609

607610
#ifndef __cplusplus

0 commit comments

Comments
 (0)