|
48 | 48 | #define __typecheck(x, y) \
|
49 | 49 | (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
|
50 | 50 |
|
51 |
| -/* is_signed_type() isn't a constexpr for pointer types */ |
52 |
| -#define __is_signed(x) \ |
53 |
| - __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ |
54 |
| - is_signed_type(typeof(x)), 0) |
| 51 | +/* |
| 52 | + * __sign_use for integer expressions: |
| 53 | + * bit #0 set if ok for unsigned comparisons |
| 54 | + * bit #1 set if ok for signed comparisons |
| 55 | + * |
| 56 | + * In particular, statically non-negative signed integer |
| 57 | + * expressions are ok for both. |
| 58 | + * |
| 59 | + * NOTE! Unsigned types smaller than 'int' are implicitly |
| 60 | + * converted to 'int' in expressions, and are accepted for |
| 61 | + * signed conversions for now. This is debatable. |
| 62 | + * |
| 63 | + * Note that 'x' is the original expression, and 'ux' is |
| 64 | + * the unique variable that contains the value. |
| 65 | + * |
| 66 | + * We use 'ux' for pure type checking, and 'x' for when |
| 67 | + * we need to look at the value (but without evaluating |
| 68 | + * it for side effects! Careful to only ever evaluate it |
| 69 | + * with sizeof() or __builtin_constant_p() etc). |
| 70 | + * |
| 71 | + * Pointers end up being checked by the normal C type |
| 72 | + * rules at the actual comparison, and these expressions |
| 73 | + * only need to be careful to not cause warnings for |
| 74 | + * pointer use. |
| 75 | + */ |
| 76 | +#define __signed_type_use(x,ux) (2+__is_nonneg(x,ux)) |
| 77 | +#define __unsigned_type_use(x,ux) (1+2*(sizeof(ux)<4)) |
| 78 | +#define __sign_use(x,ux) (is_signed_type(typeof(ux))? \ |
| 79 | + __signed_type_use(x,ux):__unsigned_type_use(x,ux)) |
| 80 | + |
| 81 | +/* |
| 82 | + * To avoid warnings about casting pointers to integers |
| 83 | + * of different sizes, we need that special sign type. |
| 84 | + * |
| 85 | + * On 64-bit we can just always use 'long', since any |
| 86 | + * integer or pointer type can just be cast to that. |
| 87 | + * |
| 88 | + * This does not work for 128-bit signed integers since |
| 89 | + * the cast would truncate them, but we do not use s128 |
| 90 | + * types in the kernel (we do use 'u128', but they will |
| 91 | + * be handled by the !is_signed_type() case). |
| 92 | + * |
| 93 | + * NOTE! The cast is there only to avoid any warnings |
| 94 | + * from when values that aren't signed integer types. |
| 95 | + */ |
| 96 | +#ifdef CONFIG_64BIT |
| 97 | + #define __signed_type(ux) long |
| 98 | +#else |
| 99 | + #define __signed_type(ux) typeof(__builtin_choose_expr(sizeof(ux)>4,1LL,1L)) |
| 100 | +#endif |
| 101 | +#define __is_nonneg(x,ux) statically_true((__signed_type(ux))(x)>=0) |
55 | 102 |
|
56 |
| -/* True for a non-negative signed int constant */ |
57 |
| -#define __is_noneg_int(x) \ |
58 |
| - (__builtin_choose_expr(__is_constexpr(x) && __is_signed(x), x, -1) >= 0) |
| 103 | +#define __types_ok(x,y,ux,uy) \ |
| 104 | + (__sign_use(x,ux) & __sign_use(y,uy)) |
59 | 105 |
|
60 |
| -#define __types_ok(x, y) \ |
61 |
| - (__is_signed(x) == __is_signed(y) || \ |
62 |
| - __is_signed((x) + 0) == __is_signed((y) + 0) || \ |
63 |
| - __is_noneg_int(x) || __is_noneg_int(y)) |
| 106 | +#define __types_ok3(x,y,z,ux,uy,uz) \ |
| 107 | + (__sign_use(x,ux) & __sign_use(y,uy) & __sign_use(z,uz)) |
64 | 108 |
|
65 | 109 | #define __cmp_op_min <
|
66 | 110 | #define __cmp_op_max >
|
67 | 111 |
|
68 | 112 | #define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y))
|
69 | 113 |
|
70 |
| -#define __cmp_once(op, x, y, unique_x, unique_y) ({ \ |
71 |
| - typeof(x) unique_x = (x); \ |
72 |
| - typeof(y) unique_y = (y); \ |
73 |
| - static_assert(__types_ok(x, y), \ |
74 |
| - #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \ |
75 |
| - __cmp(op, unique_x, unique_y); }) |
| 114 | +#define __cmp_once_unique(op, type, x, y, ux, uy) \ |
| 115 | + ({ type ux = (x); type uy = (y); __cmp(op, ux, uy); }) |
| 116 | + |
| 117 | +#define __cmp_once(op, type, x, y) \ |
| 118 | + __cmp_once_unique(op, type, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_)) |
76 | 119 |
|
77 |
| -#define __careful_cmp(op, x, y) \ |
78 |
| - __builtin_choose_expr(__is_constexpr((x) - (y)), \ |
79 |
| - __cmp(op, x, y), \ |
80 |
| - __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))) |
| 120 | +#define __careful_cmp_once(op, x, y, ux, uy) ({ \ |
| 121 | + __auto_type ux = (x); __auto_type uy = (y); \ |
| 122 | + BUILD_BUG_ON_MSG(!__types_ok(x,y,ux,uy), \ |
| 123 | + #op"("#x", "#y") signedness error"); \ |
| 124 | + __cmp(op, ux, uy); }) |
| 125 | + |
| 126 | +#define __careful_cmp(op, x, y) \ |
| 127 | + __careful_cmp_once(op, x, y, __UNIQUE_ID(x_), __UNIQUE_ID(y_)) |
81 | 128 |
|
82 | 129 | #define __clamp(val, lo, hi) \
|
83 | 130 | ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
|
84 | 131 |
|
85 |
| -#define __clamp_once(val, lo, hi, unique_val, unique_lo, unique_hi) ({ \ |
86 |
| - typeof(val) unique_val = (val); \ |
87 |
| - typeof(lo) unique_lo = (lo); \ |
88 |
| - typeof(hi) unique_hi = (hi); \ |
| 132 | +#define __clamp_once(val, lo, hi, uval, ulo, uhi) ({ \ |
| 133 | + __auto_type uval = (val); \ |
| 134 | + __auto_type ulo = (lo); \ |
| 135 | + __auto_type uhi = (hi); \ |
89 | 136 | static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \
|
90 | 137 | (lo) <= (hi), true), \
|
91 | 138 | "clamp() low limit " #lo " greater than high limit " #hi); \
|
92 |
| - static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \ |
93 |
| - static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \ |
94 |
| - __clamp(unique_val, unique_lo, unique_hi); }) |
| 139 | + BUILD_BUG_ON_MSG(!__types_ok3(val,lo,hi,uval,ulo,uhi), \ |
| 140 | + "clamp("#val", "#lo", "#hi") signedness error"); \ |
| 141 | + __clamp(uval, ulo, uhi); }) |
95 | 142 |
|
96 |
| -#define __careful_clamp(val, lo, hi) ({ \ |
97 |
| - __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \ |
98 |
| - __clamp(val, lo, hi), \ |
99 |
| - __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \ |
100 |
| - __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); }) |
| 143 | +#define __careful_clamp(val, lo, hi) \ |
| 144 | + __clamp_once(val, lo, hi, __UNIQUE_ID(v_), __UNIQUE_ID(l_), __UNIQUE_ID(h_)) |
101 | 145 |
|
102 | 146 | #ifndef __QNX__
|
103 | 147 | /**
|
|
132 | 176 | #define umax(x, y) \
|
133 | 177 | __careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull)
|
134 | 178 |
|
| 179 | +#define __careful_op3(op, x, y, z, ux, uy, uz) ({ \ |
| 180 | + __auto_type ux = (x); __auto_type uy = (y);__auto_type uz = (z);\ |
| 181 | + BUILD_BUG_ON_MSG(!__types_ok3(x,y,z,ux,uy,uz), \ |
| 182 | + #op"3("#x", "#y", "#z") signedness error"); \ |
| 183 | + __cmp(op, ux, __cmp(op, uy, uz)); }) |
| 184 | + |
135 | 185 | /**
|
136 | 186 | * min3 - return minimum of three values
|
137 | 187 | * @x: first value
|
138 | 188 | * @y: second value
|
139 | 189 | * @z: third value
|
140 | 190 | */
|
141 |
| -#define min3(x, y, z) min((typeof(x))min(x, y), z) |
| 191 | +#define min3(x, y, z) \ |
| 192 | + __careful_op3(min, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_)) |
142 | 193 |
|
143 | 194 | /**
|
144 | 195 | * max3 - return maximum of three values
|
145 | 196 | * @x: first value
|
146 | 197 | * @y: second value
|
147 | 198 | * @z: third value
|
148 | 199 | */
|
149 |
| -#define max3(x, y, z) max((typeof(x))max(x, y), z) |
| 200 | +#define max3(x, y, z) \ |
| 201 | + __careful_op3(max, x, y, z, __UNIQUE_ID(x_), __UNIQUE_ID(y_), __UNIQUE_ID(z_)) |
150 | 202 |
|
151 | 203 | /**
|
152 | 204 | * min_not_zero - return the minimum that is _not_ zero, unless both are zero
|
|
182 | 234 | * @x: first value
|
183 | 235 | * @y: second value
|
184 | 236 | */
|
185 |
| -#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y)) |
| 237 | +#define min_t(type, x, y) __cmp_once(min, type, x, y) |
186 | 238 |
|
187 | 239 | /**
|
188 | 240 | * max_t - return maximum of two values, using the specified type
|
189 | 241 | * @type: data type to use
|
190 | 242 | * @x: first value
|
191 | 243 | * @y: second value
|
192 | 244 | */
|
193 |
| -#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y)) |
| 245 | +#define max_t(type, x, y) __cmp_once(max, type, x, y) |
194 | 246 |
|
195 | 247 | /*
|
196 | 248 | * Do not check the array parameter using __must_be_array().
|
@@ -294,4 +346,13 @@ static inline bool in_range32(u32 val, u32 start, u32 len)
|
294 | 346 | #define swap(a, b) \
|
295 | 347 | do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
|
296 | 348 |
|
| 349 | +/* |
| 350 | + * Use these carefully: no type checking, and uses the arguments |
| 351 | + * multiple times. Use for obvious constants only. |
| 352 | + */ |
| 353 | +#define MIN(a,b) __cmp(min,a,b) |
| 354 | +#define MAX(a,b) __cmp(max,a,b) |
| 355 | +#define MIN_T(type,a,b) __cmp(min,(type)(a),(type)(b)) |
| 356 | +#define MAX_T(type,a,b) __cmp(max,(type)(a),(type)(b)) |
| 357 | + |
297 | 358 | #endif /* _LINUX_MINMAX_H */
|
0 commit comments