|
99 | 99 | #define __careful_cmp(op, x, y) \
|
100 | 100 | __careful_cmp_once(op, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_))
|
101 | 101 |
|
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 |
| - |
118 | 102 | /**
|
119 | 103 | * min - return minimum of two values of the same or compatible types
|
120 | 104 | * @x: first value
|
|
170 | 154 | #define max3(x, y, z) \
|
171 | 155 | __careful_op3(max, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_))
|
172 | 156 |
|
| 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 | + |
173 | 173 | /**
|
174 | 174 | * min_not_zero - return the minimum that is _not_ zero, unless both are zero
|
175 | 175 | * @x: value1
|
|
180 | 180 | typeof(y) __y = (y); \
|
181 | 181 | __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); })
|
182 | 182 |
|
| 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 | + |
183 | 199 | /**
|
184 | 200 | * clamp - return a value clamped to a given range with strict typechecking
|
185 | 201 | * @val: current value
|
|
191 | 207 | */
|
192 | 208 | #define clamp(val, lo, hi) __careful_clamp(val, lo, hi)
|
193 | 209 |
|
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 |
| - |
201 | 210 | /**
|
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. |
206 | 219 | */
|
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)) |
208 | 221 |
|
209 | 222 | /**
|
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. |
214 | 232 | */
|
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) |
216 | 234 |
|
217 | 235 | /*
|
218 | 236 | * Do not check the array parameter using __must_be_array().
|
|
257 | 275 | */
|
258 | 276 | #define max_array(array, len) __minmax_array(max, array, len)
|
259 | 277 |
|
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 |
| - |
285 | 278 | static inline bool in_range64(u64 val, u64 start, u64 len)
|
286 | 279 | {
|
287 | 280 | return (val - start) < len;
|
|
0 commit comments