Skip to content

Commit 495bba1

Browse files
david-laightakpm00
authored andcommitted
minmax.h: simplify the variants of clamp()
Always pass a 'type' through to __clamp_once(), pass '__auto_type' from clamp() itself. The expansion of __types_ok3() is reasonable so it isn't worth the added complexity of avoiding it when a fixed type is used for all three values. Link: https://lkml.kernel.org/r/8f69f4deac014f558bab186444bac2e8@AcuMS.aculab.com Signed-off-by: David Laight <david.laight@aculab.com> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Cc: Arnd Bergmann <arnd@kernel.org> Cc: Christoph Hellwig <hch@infradead.org> Cc: Dan Carpenter <dan.carpenter@linaro.org> Cc: Jason A. Donenfeld <Jason@zx2c4.com> Cc: Jens Axboe <axboe@kernel.dk> Cc: Lorenzo Stoakes <lorenzo.stoakes@oracle.com> Cc: Mateusz Guzik <mjguzik@gmail.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Pedro Falcato <pedro.falcato@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent c393987 commit 495bba1

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
@@ -183,29 +183,29 @@
183183
#define __clamp(val, lo, hi) \
184184
((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
185185

186-
#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \
187-
__auto_type uval = (val); \
188-
__auto_type ulo = (lo); \
189-
__auto_type uhi = (hi); \
186+
#define __clamp_once(type, val, lo, hi, uval, ulo, uhi) ({ \
187+
type uval = (val); \
188+
type ulo = (lo); \
189+
type uhi = (hi); \
190190
BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \
191191
"clamp() low limit " #lo " greater than high limit " #hi); \
192192
BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \
193193
"clamp("#val", "#lo", "#hi") signedness error"); \
194194
__clamp(uval, ulo, uhi); })
195195

196-
#define __careful_clamp(val, lo, hi) \
197-
__clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_))
196+
#define __careful_clamp(type, val, lo, hi) \
197+
__clamp_once(type, val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_))
198198

199199
/**
200-
* clamp - return a value clamped to a given range with strict typechecking
200+
* clamp - return a value clamped to a given range with typechecking
201201
* @val: current value
202202
* @lo: lowest allowable value
203203
* @hi: highest allowable value
204204
*
205-
* This macro does strict typechecking of @lo/@hi to make sure they are of the
206-
* same type as @val. See the unnecessary pointer comparisons.
205+
* This macro checks @val/@lo/@hi to make sure they have compatible
206+
* signedness.
207207
*/
208-
#define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
208+
#define clamp(val, lo, hi) __careful_clamp(__auto_type, val, lo, hi)
209209

210210
/**
211211
* clamp_t - return a value clamped to a given range using a given type
@@ -217,7 +217,7 @@
217217
* This macro does no typechecking and uses temporary variables of type
218218
* @type to make all the comparisons.
219219
*/
220-
#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
220+
#define clamp_t(type, val, lo, hi) __careful_clamp(type, val, lo, hi)
221221

222222
/**
223223
* clamp_val - return a value clamped to a given range using val's type
@@ -230,7 +230,7 @@
230230
* type and @lo and @hi are literals that will otherwise be assigned a signed
231231
* integer type.
232232
*/
233-
#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
233+
#define clamp_val(val, lo, hi) __careful_clamp(typeof(val), val, lo, hi)
234234

235235
/*
236236
* Do not check the array parameter using __must_be_array().

0 commit comments

Comments
 (0)