• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MINMAX_H
3 #define _LINUX_MINMAX_H
4 
5 #include <linux/const.h>
6 
7 /*
8  * min()/max()/clamp() macros must accomplish three things:
9  *
10  * - avoid multiple evaluations of the arguments (so side-effects like
11  *   "x++" happen only once) when non-constant.
12  * - perform strict type-checking (to generate warnings instead of
13  *   nasty runtime surprises). See the "unnecessary" pointer comparison
14  *   in __typecheck().
15  * - retain result as a constant expressions when called with only
16  *   constant expressions (to avoid tripping VLA warnings in stack
17  *   allocation usage).
18  */
19 #define __typecheck(x, y) \
20 	(!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
21 
22 #define __no_side_effects(x, y) \
23 		(__is_constexpr(x) && __is_constexpr(y))
24 
25 #define __safe_cmp(x, y) \
26 		(__typecheck(x, y) && __no_side_effects(x, y))
27 
28 #define __cmp(x, y, op)	((x) op (y) ? (x) : (y))
29 
30 #define __cmp_once(x, y, unique_x, unique_y, op) ({	\
31 		typeof(x) unique_x = (x);		\
32 		typeof(y) unique_y = (y);		\
33 		__cmp(unique_x, unique_y, op); })
34 
35 #define __careful_cmp(x, y, op) \
36 	__builtin_choose_expr(__safe_cmp(x, y), \
37 		__cmp(x, y, op), \
38 		__cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op))
39 
40 /**
41  * min - return minimum of two values of the same or compatible types
42  * @x: first value
43  * @y: second value
44  */
45 #define min(x, y)	__careful_cmp(x, y, <)
46 
47 /**
48  * max - return maximum of two values of the same or compatible types
49  * @x: first value
50  * @y: second value
51  */
52 #define max(x, y)	__careful_cmp(x, y, >)
53 
54 /**
55  * min3 - return minimum of three values
56  * @x: first value
57  * @y: second value
58  * @z: third value
59  */
60 #define min3(x, y, z) min((typeof(x))min(x, y), z)
61 
62 /**
63  * max3 - return maximum of three values
64  * @x: first value
65  * @y: second value
66  * @z: third value
67  */
68 #define max3(x, y, z) max((typeof(x))max(x, y), z)
69 
70 /**
71  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
72  * @x: value1
73  * @y: value2
74  */
75 #define min_not_zero(x, y) ({			\
76 	typeof(x) __x = (x);			\
77 	typeof(y) __y = (y);			\
78 	__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
79 
80 /**
81  * clamp - return a value clamped to a given range with strict typechecking
82  * @val: current value
83  * @lo: lowest allowable value
84  * @hi: highest allowable value
85  *
86  * This macro does strict typechecking of @lo/@hi to make sure they are of the
87  * same type as @val.  See the unnecessary pointer comparisons.
88  */
89 #define clamp(val, lo, hi) min((typeof(val))max(val, lo), hi)
90 
91 /*
92  * ..and if you can't take the strict
93  * types, you can specify one yourself.
94  *
95  * Or not use min/max/clamp at all, of course.
96  */
97 
98 /**
99  * min_t - return minimum of two values, using the specified type
100  * @type: data type to use
101  * @x: first value
102  * @y: second value
103  */
104 #define min_t(type, x, y)	__careful_cmp((type)(x), (type)(y), <)
105 
106 /**
107  * max_t - return maximum of two values, using the specified type
108  * @type: data type to use
109  * @x: first value
110  * @y: second value
111  */
112 #define max_t(type, x, y)	__careful_cmp((type)(x), (type)(y), >)
113 
114 /**
115  * clamp_t - return a value clamped to a given range using a given type
116  * @type: the type of variable to use
117  * @val: current value
118  * @lo: minimum allowable value
119  * @hi: maximum allowable value
120  *
121  * This macro does no typechecking and uses temporary variables of type
122  * @type to make all the comparisons.
123  */
124 #define clamp_t(type, val, lo, hi) min_t(type, max_t(type, val, lo), hi)
125 
126 /**
127  * clamp_val - return a value clamped to a given range using val's type
128  * @val: current value
129  * @lo: minimum allowable value
130  * @hi: maximum allowable value
131  *
132  * This macro does no typechecking and uses temporary variables of whatever
133  * type the input argument @val is.  This is useful when @val is an unsigned
134  * type and @lo and @hi are literals that will otherwise be assigned a signed
135  * integer type.
136  */
137 #define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
138 
139 /**
140  * swap - swap values of @a and @b
141  * @a: first value
142  * @b: second value
143  */
144 #define swap(a, b) \
145 	do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
146 
147 #endif	/* _LINUX_MINMAX_H */
148