Skip to content

Commit 37717b2

Browse files
fabiobaltiericfriedt
authored andcommitted
sys: util: rename Z_MIN Z_MAX Z_CLAMP to min max and clamp
Rename these three macros to an unprefixed lower-case variant. This is normally not done for Zephyr macros (see container_of) but in this case it seems like a good idea to adopt the lowercase names to: 1. have the same convention as the equivalent Linux macros, helping devs working cross project recognizing (mis)use patterns. 2. make it somewhat intuitive that the lowercase ones are meant to be used in functions while the uppercase ones are to be used for static evaluation. Add few c++ guards to avoid colliding with std::min and std::max. Signed-off-by: Fabio Baltieri <[email protected]>
1 parent 056f3b0 commit 37717b2

File tree

8 files changed

+47
-33
lines changed

8 files changed

+47
-33
lines changed

doc/releases/migration-guide-4.3.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Base Libraries
3838
been moved from ``util.h`` to a separate
3939
:zephyr_file:`include/zephyr/sys/util_utf8.h` file.
4040

41+
* ``Z_MIN``, ``Z_MAX`` and ``Z_CLAMP`` macros have been renamed to
42+
:c:macro:`min` :c:macro:`max` and :c:macro:`clamp`.
43+
4144
Boards
4245
******
4346

drivers/usb/device/usb_dc_sam0.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ int usb_dc_ep_write(uint8_t ep, const uint8_t *buf, uint32_t len, uint32_t *ret_
608608
return -EAGAIN;
609609
}
610610

611-
len = Z_MIN(len, capacity);
611+
len = min(len, capacity);
612612

613613
/* Note that this code does not use the hardware's
614614
* multi-packet and automatic zero-length packet features as

include/zephyr/dsp/utils.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ extern "C" {
116116
* @return The converted Q7 fixed-point value.
117117
*/
118118
#define Z_SHIFT_F32_TO_Q7(src, m) \
119-
((q7_t)Z_CLAMP((int32_t)(src * (1U << 7)) >> m, INT8_MIN, INT8_MAX))
119+
((q7_t)clamp((int32_t)(src * (1U << 7)) >> m, INT8_MIN, INT8_MAX))
120120

121121
/**
122122
* @brief Convert a floating-point (float32_t) value to a Q15 fixed-point value with a right shift.
@@ -126,7 +126,7 @@ extern "C" {
126126
* @return The converted Q15 fixed-point value.
127127
*/
128128
#define Z_SHIFT_F32_TO_Q15(src, m) \
129-
((q15_t)Z_CLAMP((int32_t)(src * (1U << 15)) >> m, INT16_MIN, INT16_MAX))
129+
((q15_t)clamp((int32_t)(src * (1U << 15)) >> m, INT16_MIN, INT16_MAX))
130130

131131
/**
132132
* @brief Convert a floating-point (float32_t) value to a Q31 fixed-point value with a right shift.
@@ -136,7 +136,7 @@ extern "C" {
136136
* @return The converted Q31 fixed-point value.
137137
*/
138138
#define Z_SHIFT_F32_TO_Q31(src, m) \
139-
((q31_t)Z_CLAMP((int64_t)(src * (1U << 31)) >> m, INT32_MIN, INT32_MAX))
139+
((q31_t)clamp((int64_t)(src * (1U << 31)) >> m, INT32_MIN, INT32_MAX))
140140

141141
/**
142142
* @brief Convert a floating-point (float64_t) value to a Q7 fixed-point value with a right shift.
@@ -146,7 +146,7 @@ extern "C" {
146146
* @return The converted Q7 fixed-point value.
147147
*/
148148
#define Z_SHIFT_F64_TO_Q7(src, m) \
149-
((q7_t)Z_CLAMP((int32_t)(src * (1U << 7)) >> m, INT8_MIN, INT8_MAX))
149+
((q7_t)clamp((int32_t)(src * (1U << 7)) >> m, INT8_MIN, INT8_MAX))
150150

151151
/**
152152
* @brief Convert a floating-point (float64_t) value to a Q15 fixed-point value with a right shift.
@@ -156,7 +156,7 @@ extern "C" {
156156
* @return The converted Q15 fixed-point value.
157157
*/
158158
#define Z_SHIFT_F64_TO_Q15(src, m) \
159-
((q15_t)Z_CLAMP((int32_t)(src * (1U << 15)) >> m, INT16_MIN, INT16_MAX))
159+
((q15_t)clamp((int32_t)(src * (1U << 15)) >> m, INT16_MIN, INT16_MAX))
160160

161161
/**
162162
* @brief Convert a floating-point (float64_t) value to a Q31 fixed-point value with a right shift.
@@ -166,7 +166,7 @@ extern "C" {
166166
* @return The converted Q31 fixed-point value.
167167
*/
168168
#define Z_SHIFT_F64_TO_Q31(src, m) \
169-
((q31_t)Z_CLAMP((int64_t)(src * (1U << 31)) >> m, INT32_MIN, INT32_MAX))
169+
((q31_t)clamp((int64_t)(src * (1U << 31)) >> m, INT32_MIN, INT32_MAX))
170170

171171
/**
172172
* @}

include/zephyr/sys/util.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,8 @@ extern "C" {
376376
/**
377377
* @brief Obtain the maximum of two values.
378378
*
379-
* @note Arguments are evaluated twice. Use Z_MAX for a GCC-only, single
380-
* evaluation version
379+
* @note Arguments are evaluated twice. Use @ref max for a single evaluation
380+
* version.
381381
*
382382
* @param a First value.
383383
* @param b Second value.
@@ -387,28 +387,30 @@ extern "C" {
387387
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
388388
#endif
389389

390+
#ifndef __cplusplus
390391
/** @brief Return larger value of two provided expressions.
391392
*
392393
* Macro ensures that expressions are evaluated only once.
393394
*
394395
* @note Macro has limited usage compared to the standard macro as it cannot be
395396
* 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(...)];
397+
* - to generate constant integer, e.g. __aligned(max(4,5))
398+
* - static variable, e.g. array like static uint8_t array[max(...)];
398399
*/
399-
#define Z_MAX(a, b) ({ \
400+
#define max(a, b) ({ \
400401
/* random suffix to avoid naming conflict */ \
401402
__typeof__(a) _value_a_ = (a); \
402403
__typeof__(b) _value_b_ = (b); \
403404
(_value_a_ > _value_b_) ? _value_a_ : _value_b_; \
404405
})
406+
#endif
405407

406408
#ifndef MIN
407409
/**
408410
* @brief Obtain the minimum of two values.
409411
*
410-
* @note Arguments are evaluated twice. Use Z_MIN for a GCC-only, single
411-
* evaluation version
412+
* @note Arguments are evaluated twice. Use @ref min for a single evaluation
413+
* version.
412414
*
413415
* @param a First value.
414416
* @param b Second value.
@@ -418,17 +420,19 @@ extern "C" {
418420
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
419421
#endif
420422

423+
#ifndef __cplusplus
421424
/** @brief Return smaller value of two provided expressions.
422425
*
423-
* Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
426+
* Macro ensures that expressions are evaluated only once. See @ref max for
424427
* macro limitations.
425428
*/
426-
#define Z_MIN(a, b) ({ \
429+
#define min(a, b) ({ \
427430
/* random suffix to avoid naming conflict */ \
428431
__typeof__(a) _value_a_ = (a); \
429432
__typeof__(b) _value_b_ = (b); \
430433
(_value_a_ < _value_b_) ? _value_a_ : _value_b_; \
431434
})
435+
#endif
432436

433437
#ifndef MAX_FROM_LIST
434438
/**
@@ -555,8 +559,8 @@ extern "C" {
555559
/**
556560
* @brief Clamp a value to a given range.
557561
*
558-
* @note Arguments are evaluated multiple times. Use Z_CLAMP for a GCC-only,
559-
* single evaluation version.
562+
* @note Arguments are evaluated multiple times. Use @ref clamp for a single
563+
* evaluation version.
560564
*
561565
* @param val Value to be clamped.
562566
* @param low Lowest allowed value (inclusive).
@@ -567,12 +571,13 @@ extern "C" {
567571
#define CLAMP(val, low, high) (((val) <= (low)) ? (low) : MIN(val, high))
568572
#endif
569573

574+
#ifndef __cplusplus
570575
/** @brief Return a value clamped to a given range.
571576
*
572-
* Macro ensures that expressions are evaluated only once. See @ref Z_MAX for
577+
* Macro ensures that expressions are evaluated only once. See @ref max for
573578
* macro limitations.
574579
*/
575-
#define Z_CLAMP(val, low, high) ({ \
580+
#define clamp(val, low, high) ({ \
576581
/* random suffix to avoid naming conflict */ \
577582
__typeof__(val) _value_val_ = (val); \
578583
__typeof__(low) _value_low_ = (low); \
@@ -581,6 +586,7 @@ extern "C" {
581586
(_value_val_ > _value_high_) ? _value_high_ : \
582587
_value_val_; \
583588
})
589+
#endif
584590

585591
/**
586592
* @brief Checks if a value is within range.

subsys/net/lib/lwm2m/lwm2m_rd_client.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ static int64_t next_rx_off(void)
11761176
/** Return timestamp to next even whether it is RX_OFF or update event */
11771177
static int64_t calc_next_event(void)
11781178
{
1179-
return Z_MIN(next_update(), next_rx_off());
1179+
return min(next_update(), next_rx_off());
11801180
}
11811181

11821182
static void sm_registration_done(void)

subsys/shell/shell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ static void find_completion_candidates(const struct shell *sh,
346346
is_candidate = is_completion_candidate(candidate->syntax,
347347
incompl_cmd, incompl_cmd_len);
348348
if (is_candidate) {
349-
*longest = Z_MAX(strlen(candidate->syntax), *longest);
349+
*longest = max(strlen(candidate->syntax), *longest);
350350
if (*cnt == 0) {
351351
*first_idx = idx;
352352
}

subsys/shell/shell_help.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void z_shell_help_subcmd_print(const struct shell *sh,
187187

188188
/* Searching for the longest subcommand to print. */
189189
while ((entry = z_shell_cmd_get(parent, idx++, &dloc)) != NULL) {
190-
longest = Z_MAX(longest, z_shell_strlen(entry->syntax));
190+
longest = max(longest, z_shell_strlen(entry->syntax));
191191
}
192192

193193
/* No help to print */

tests/unit/util/main.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,26 +245,31 @@ static int inc_func(bool cleanup)
245245
return a++;
246246
}
247247

248-
/* Test checks if @ref Z_MAX, @ref Z_MIN and @ref Z_CLAMP return correct result
248+
/* Test checks if @ref max, @ref min and @ref clamp return correct result
249249
* and perform single evaluation of input arguments.
250250
*/
251-
ZTEST(util, test_z_max_z_min_z_clamp) {
252-
zassert_equal(Z_MAX(inc_func(true), 0), 1, "Unexpected macro result");
253-
/* Z_MAX should have call inc_func only once */
251+
ZTEST(util, test_max_min_clamp) {
252+
#ifdef __cplusplus
253+
/* C++ has its own std::min() and std::max(), min and max are not available. */
254+
ztest_test_skip();
255+
#else
256+
zassert_equal(max(inc_func(true), 0), 1, "Unexpected macro result");
257+
/* max should have call inc_func only once */
254258
zassert_equal(inc_func(false), 2, "Unexpected return value");
255259

256-
zassert_equal(Z_MIN(inc_func(false), 2), 2, "Unexpected macro result");
257-
/* Z_MIN should have call inc_func only once */
260+
zassert_equal(min(inc_func(false), 2), 2, "Unexpected macro result");
261+
/* min should have call inc_func only once */
258262
zassert_equal(inc_func(false), 4, "Unexpected return value");
259263

260-
zassert_equal(Z_CLAMP(inc_func(false), 1, 3), 3, "Unexpected macro result");
261-
/* Z_CLAMP should have call inc_func only once */
264+
zassert_equal(clamp(inc_func(false), 1, 3), 3, "Unexpected macro result");
265+
/* clamp should have call inc_func only once */
262266
zassert_equal(inc_func(false), 6, "Unexpected return value");
263267

264-
zassert_equal(Z_CLAMP(inc_func(false), 10, 15), 10,
268+
zassert_equal(clamp(inc_func(false), 10, 15), 10,
265269
"Unexpected macro result");
266-
/* Z_CLAMP should have call inc_func only once */
270+
/* clamp should have call inc_func only once */
267271
zassert_equal(inc_func(false), 8, "Unexpected return value");
272+
#endif
268273
}
269274

270275
ZTEST(util, test_max_from_list_macro) {

0 commit comments

Comments
 (0)