Skip to content

Commit b280bb2

Browse files
david-laightakpm00
authored andcommitted
minmax.h: reduce the #define expansion of min(), max() and clamp()
Since the test for signed values being non-negative only relies on __builtion_constant_p() (not is_constexpr()) it can use the 'ux' variable instead of the caller supplied expression. This means that the #define parameters are only expanded twice. Once in the code and once quoted in the error message. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: David Laight <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Dan Carpenter <[email protected]> Cc: Jason A. Donenfeld <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Cc: Mateusz Guzik <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Pedro Falcato <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 10666e9 commit b280bb2

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

include/linux/minmax.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
* comparison, and these expressions only need to be careful to not cause
4747
* warnings for pointer use.
4848
*/
49-
#define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux))
50-
#define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4))
51-
#define __sign_use(x, ux) (is_signed_type(typeof(ux)) ? \
52-
__signed_type_use(x, ux) : __unsigned_type_use(x, ux))
49+
#define __signed_type_use(ux) (2 + __is_nonneg(ux))
50+
#define __unsigned_type_use(ux) (1 + 2 * (sizeof(ux) < 4))
51+
#define __sign_use(ux) (is_signed_type(typeof(ux)) ? \
52+
__signed_type_use(ux) : __unsigned_type_use(ux))
5353

5454
/*
5555
* Check whether a signed value is always non-negative.
@@ -71,13 +71,13 @@
7171
#else
7272
#define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux) > 4, 1LL, 1L))
7373
#endif
74-
#define __is_nonneg(x, ux) statically_true((__signed_type(ux))(x) >= 0)
74+
#define __is_nonneg(ux) statically_true((__signed_type(ux))(ux) >= 0)
7575

76-
#define __types_ok(x, y, ux, uy) \
77-
(__sign_use(x, ux) & __sign_use(y, uy))
76+
#define __types_ok(ux, uy) \
77+
(__sign_use(ux) & __sign_use(uy))
7878

79-
#define __types_ok3(x, y, z, ux, uy, uz) \
80-
(__sign_use(x, ux) & __sign_use(y, uy) & __sign_use(z, uz))
79+
#define __types_ok3(ux, uy, uz) \
80+
(__sign_use(ux) & __sign_use(uy) & __sign_use(uz))
8181

8282
#define __cmp_op_min <
8383
#define __cmp_op_max >
@@ -92,7 +92,7 @@
9292

9393
#define __careful_cmp_once(op, x, y, ux, uy) ({ \
9494
__auto_type ux = (x); __auto_type uy = (y); \
95-
BUILD_BUG_ON_MSG(!__types_ok(x, y, ux, uy), \
95+
BUILD_BUG_ON_MSG(!__types_ok(ux, uy), \
9696
#op"("#x", "#y") signedness error"); \
9797
__cmp(op, ux, uy); })
9898

@@ -109,7 +109,7 @@
109109
static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \
110110
(lo) <= (hi), true), \
111111
"clamp() low limit " #lo " greater than high limit " #hi); \
112-
BUILD_BUG_ON_MSG(!__types_ok3(val, lo, hi, uval, ulo, uhi), \
112+
BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \
113113
"clamp("#val", "#lo", "#hi") signedness error"); \
114114
__clamp(uval, ulo, uhi); })
115115

@@ -149,7 +149,7 @@
149149

150150
#define __careful_op3(op, x, y, z, ux, uy, uz) ({ \
151151
__auto_type ux = (x); __auto_type uy = (y);__auto_type uz = (z);\
152-
BUILD_BUG_ON_MSG(!__types_ok3(x, y, z, ux, uy, uz), \
152+
BUILD_BUG_ON_MSG(!__types_ok3(ux, uy, uz), \
153153
#op"3("#x", "#y", "#z") signedness error"); \
154154
__cmp(op, ux, __cmp(op, uy, uz)); })
155155

0 commit comments

Comments
 (0)