Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
minmax.h File Reference

Lowercase min/max/clamp helpers. More...

#include <zephyr/sys/util.h>

Go to the source code of this file.

Macros

#define max(a, b)
 Return larger value of two provided expressions.
#define max3(a, b, c)
 Return larger value of three provided expressions.
#define min(a, b)
 Return smaller value of two provided expressions.
#define min3(a, b, c)
 Return smaller value of three provided expressions.
#define clamp(val, low, high)
 Return a value clamped to a given range.

Detailed Description

Lowercase min/max/clamp helpers.

These short-named macros are kept out of util.h so they are not pulled in transitively by broad headers such as <pthread.h>. Source files that need min, max, or clamp should include this header explicitly.

Macro Definition Documentation

◆ clamp

#define clamp ( val,
low,
high )
Value:
({ \
/* random suffix to avoid naming conflict */ \
__typeof__(val) _value_val_ = (val); \
__typeof__(low) _value_low_ = (low); \
__typeof__(high) _value_high_ = (high); \
(_value_val_ < _value_low_) ? _value_low_ : \
(_value_val_ > _value_high_) ? _value_high_ : \
_value_val_; \
})

Return a value clamped to a given range.

Macro ensures that expressions are evaluated only once. See max for macro limitations.

◆ max

#define max ( a,
b )
Value:
_minmax_cnt(Z_INTERNAL_MAX, a, b, __COUNTER__)

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(max(4,5))
  • static variable, e.g. array like static uint8_t array[max(...)];

◆ max3

#define max3 ( a,
b,
c )
Value:
_minmax3_cnt(Z_INTERNAL_MAX, a, b, c, __COUNTER__)

Return larger value of three provided expressions.

Macro ensures that expressions are evaluated only once. See max for macro limitations.

◆ min

#define min ( a,
b )
Value:
_minmax_cnt(Z_INTERNAL_MIN, a, b, __COUNTER__)

Return smaller value of two provided expressions.

Macro ensures that expressions are evaluated only once. See max for macro limitations.

◆ min3

#define min3 ( a,
b,
c )
Value:
_minmax3_cnt(Z_INTERNAL_MIN, a, b, c, __COUNTER__)

Return smaller value of three provided expressions.

Macro ensures that expressions are evaluated only once. See max for macro limitations.