Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
minmax.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2014, Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
15
16#ifndef ZEPHYR_INCLUDE_SYS_MINMAX_H_
17#define ZEPHYR_INCLUDE_SYS_MINMAX_H_
18
19#include <zephyr/sys/util.h>
20
21#ifndef _ASMLANGUAGE
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
30#define _minmax_unique(op, a, b, ua, ub) ({ \
31 __typeof__(a) ua = (a); \
32 __typeof__(b) ub = (b); \
33 op(ua, ub); \
34 })
35
36#define _minmax_cnt(op, a, b, cnt) \
37 _minmax_unique(op, a, b, UTIL_CAT(_value_a_, cnt), UTIL_CAT(_value_b_, cnt))
38
39#define _minmax3_unique(op, a, b, c, ua, ub, uc) ({ \
40 __typeof__(a) ua = (a); \
41 __typeof__(b) ub = (b); \
42 __typeof__(c) uc = (c); \
43 op(ua, op(ub, uc)); \
44 })
45
46#define _minmax3_cnt(op, a, b, c, cnt) \
47 _minmax3_unique(op, a, b, c, \
48 UTIL_CAT(_value_a_, cnt), \
49 UTIL_CAT(_value_b_, cnt), \
50 UTIL_CAT(_value_c_, cnt))
54
55#ifndef __cplusplus
65#define max(a, b) _minmax_cnt(Z_INTERNAL_MAX, a, b, __COUNTER__)
66#endif
67
73#define max3(a, b, c) _minmax3_cnt(Z_INTERNAL_MAX, a, b, c, __COUNTER__)
74
75#ifndef __cplusplus
81#define min(a, b) _minmax_cnt(Z_INTERNAL_MIN, a, b, __COUNTER__)
82#endif
83
89#define min3(a, b, c) _minmax3_cnt(Z_INTERNAL_MIN, a, b, c, __COUNTER__)
90
91#ifndef __cplusplus
97#define clamp(val, low, high) ({ \
98 /* random suffix to avoid naming conflict */ \
99 __typeof__(val) _value_val_ = (val); \
100 __typeof__(low) _value_low_ = (low); \
101 __typeof__(high) _value_high_ = (high); \
102 (_value_val_ < _value_low_) ? _value_low_ : \
103 (_value_val_ > _value_high_) ? _value_high_ : \
104 _value_val_; \
105 })
106#endif
107
108#ifdef __cplusplus
109}
110#endif
111
112#endif /* _ASMLANGUAGE */
113
114#endif /* ZEPHYR_INCLUDE_SYS_MINMAX_H_ */
Misc utilities.