Skip to content

Commit c393987

Browse files
david-laightakpm00
authored andcommitted
minmax.h: move all the clamp() definitions after the min/max() ones
At some point the definitions for clamp() got added in the middle of the ones for min() and max(). Re-order the definitions so they are more sensibly grouped. Link: https://lkml.kernel.org/r/8bb285818e4846469121c8abc3dfb6e2@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 a5743f3 commit c393987

File tree

1 file changed

+51
-58
lines changed

1 file changed

+51
-58
lines changed

include/linux/minmax.h

Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,6 @@
9999
#define __careful_cmp(op, x, y) \
100100
__careful_cmp_once(op, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_))
101101

102-
#define __clamp(val, lo, hi) \
103-
((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
104-
105-
#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \
106-
__auto_type uval = (val); \
107-
__auto_type ulo = (lo); \
108-
__auto_type uhi = (hi); \
109-
BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \
110-
"clamp() low limit " #lo " greater than high limit " #hi); \
111-
BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \
112-
"clamp("#val", "#lo", "#hi") signedness error"); \
113-
__clamp(uval, ulo, uhi); })
114-
115-
#define __careful_clamp(val, lo, hi) \
116-
__clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_))
117-
118102
/**
119103
* min - return minimum of two values of the same or compatible types
120104
* @x: first value
@@ -170,6 +154,22 @@
170154
#define max3(x, y, z) \
171155
__careful_op3(max, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_))
172156

157+
/**
158+
* min_t - return minimum of two values, using the specified type
159+
* @type: data type to use
160+
* @x: first value
161+
* @y: second value
162+
*/
163+
#define min_t(type, x, y) __cmp_once(min, type, x, y)
164+
165+
/**
166+
* max_t - return maximum of two values, using the specified type
167+
* @type: data type to use
168+
* @x: first value
169+
* @y: second value
170+
*/
171+
#define max_t(type, x, y) __cmp_once(max, type, x, y)
172+
173173
/**
174174
* min_not_zero - return the minimum that is _not_ zero, unless both are zero
175175
* @x: value1
@@ -180,6 +180,22 @@
180180
typeof(y) __y = (y); \
181181
__x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
182182

183+
#define __clamp(val, lo, hi) \
184+
((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
185+
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); \
190+
BUILD_BUG_ON_MSG(statically_true(ulo > uhi), \
191+
"clamp() low limit " #lo " greater than high limit " #hi); \
192+
BUILD_BUG_ON_MSG(!__types_ok3(uval, ulo, uhi), \
193+
"clamp("#val", "#lo", "#hi") signedness error"); \
194+
__clamp(uval, ulo, uhi); })
195+
196+
#define __careful_clamp(val, lo, hi) \
197+
__clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_))
198+
183199
/**
184200
* clamp - return a value clamped to a given range with strict typechecking
185201
* @val: current value
@@ -191,28 +207,30 @@
191207
*/
192208
#define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
193209

194-
/*
195-
* ..and if you can't take the strict
196-
* types, you can specify one yourself.
197-
*
198-
* Or not use min/max/clamp at all, of course.
199-
*/
200-
201210
/**
202-
* min_t - return minimum of two values, using the specified type
203-
* @type: data type to use
204-
* @x: first value
205-
* @y: second value
211+
* clamp_t - return a value clamped to a given range using a given type
212+
* @type: the type of variable to use
213+
* @val: current value
214+
* @lo: minimum allowable value
215+
* @hi: maximum allowable value
216+
*
217+
* This macro does no typechecking and uses temporary variables of type
218+
* @type to make all the comparisons.
206219
*/
207-
#define min_t(type, x, y) __cmp_once(min, type, x, y)
220+
#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
208221

209222
/**
210-
* max_t - return maximum of two values, using the specified type
211-
* @type: data type to use
212-
* @x: first value
213-
* @y: second value
223+
* clamp_val - return a value clamped to a given range using val's type
224+
* @val: current value
225+
* @lo: minimum allowable value
226+
* @hi: maximum allowable value
227+
*
228+
* This macro does no typechecking and uses temporary variables of whatever
229+
* type the input argument @val is. This is useful when @val is an unsigned
230+
* type and @lo and @hi are literals that will otherwise be assigned a signed
231+
* integer type.
214232
*/
215-
#define max_t(type, x, y) __cmp_once(max, type, x, y)
233+
#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
216234

217235
/*
218236
* Do not check the array parameter using __must_be_array().
@@ -257,31 +275,6 @@
257275
*/
258276
#define max_array(array, len) __minmax_array(max, array, len)
259277

260-
/**
261-
* clamp_t - return a value clamped to a given range using a given type
262-
* @type: the type of variable to use
263-
* @val: current value
264-
* @lo: minimum allowable value
265-
* @hi: maximum allowable value
266-
*
267-
* This macro does no typechecking and uses temporary variables of type
268-
* @type to make all the comparisons.
269-
*/
270-
#define clamp_t(type, val, lo, hi) __careful_clamp((type)(val), (type)(lo), (type)(hi))
271-
272-
/**
273-
* clamp_val - return a value clamped to a given range using val's type
274-
* @val: current value
275-
* @lo: minimum allowable value
276-
* @hi: maximum allowable value
277-
*
278-
* This macro does no typechecking and uses temporary variables of whatever
279-
* type the input argument @val is. This is useful when @val is an unsigned
280-
* type and @lo and @hi are literals that will otherwise be assigned a signed
281-
* integer type.
282-
*/
283-
#define clamp_val(val, lo, hi) clamp_t(typeof(val), val, lo, hi)
284-
285278
static inline bool in_range64(u64 val, u64 start, u64 len)
286279
{
287280
return (val - start) < len;

0 commit comments

Comments
 (0)