Skip to content

Commit 017fa3e

Browse files
committed
minmax: simplify and clarify min_t()/max_t() implementation
This simplifies the min_t() and max_t() macros by no longer making them work in the context of a C constant expression. That means that you can no longer use them for static initializers or for array sizes in type definitions, but there were only a couple of such uses, and all of them were converted (famous last words) to use MIN_T/MAX_T instead. Cc: David Laight <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 4477b39 commit 017fa3e

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

include/linux/minmax.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@
4545

4646
#define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
4747

48-
#define __cmp_once(op, x, y, unique_x, unique_y) ({ \
49-
typeof(x) unique_x = (x); \
50-
typeof(y) unique_y = (y); \
48+
#define __cmp_once_unique(op, type, x, y, ux, uy) \
49+
({ type ux = (x); type uy = (y); __cmp(op, ux, uy); })
50+
51+
#define __cmp_once(op, type, x, y) \
52+
__cmp_once_unique(op, type, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_))
53+
54+
#define __careful_cmp_once(op, x, y) ({ \
5155
static_assert(__types_ok(x, y), \
5256
#op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \
53-
__cmp(op, unique_x, unique_y); })
57+
__cmp_once(op, __auto_type, x, y); })
5458

5559
#define __careful_cmp(op, x, y) \
5660
__builtin_choose_expr(__is_constexpr((x) - (y)), \
57-
__cmp(op, x, y), \
58-
__cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y)))
61+
__cmp(op, x, y), __careful_cmp_once(op, x, y))
5962

6063
#define __clamp(val, lo, hi) \
6164
((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
@@ -158,15 +161,15 @@
158161
* @x: first value
159162
* @y: second value
160163
*/
161-
#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y))
164+
#define min_t(type, x, y) __cmp_once(min, type, x, y)
162165

163166
/**
164167
* max_t - return maximum of two values, using the specified type
165168
* @type: data type to use
166169
* @x: first value
167170
* @y: second value
168171
*/
169-
#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y))
172+
#define max_t(type, x, y) __cmp_once(max, type, x, y)
170173

171174
/*
172175
* Do not check the array parameter using __must_be_array().

0 commit comments

Comments
 (0)