|
12 | 12 | *
|
13 | 13 | * - avoid multiple evaluations of the arguments (so side-effects like
|
14 | 14 | * "x++" happen only once) when non-constant.
|
15 |
| - * - perform strict type-checking (to generate warnings instead of |
16 |
| - * nasty runtime surprises). See the "unnecessary" pointer comparison |
17 |
| - * in __typecheck(). |
| 15 | + * - perform signed v unsigned type-checking (to generate compile |
| 16 | + * errors instead of nasty runtime surprises). |
18 | 17 | * - retain result as a constant expressions when called with only
|
19 | 18 | * constant expressions (to avoid tripping VLA warnings in stack
|
20 | 19 | * allocation usage).
|
21 | 20 | */
|
22 | 21 | #define __typecheck(x, y) \
|
23 | 22 | (!!(sizeof((typeof(x) *)1 == (typeof(y) *)1)))
|
24 | 23 |
|
25 |
| -#define __no_side_effects(x, y) \ |
26 |
| - (__is_constexpr(x) && __is_constexpr(y)) |
| 24 | +/* is_signed_type() isn't a constexpr for pointer types */ |
| 25 | +#define __is_signed(x) \ |
| 26 | + __builtin_choose_expr(__is_constexpr(is_signed_type(typeof(x))), \ |
| 27 | + is_signed_type(typeof(x)), 0) |
27 | 28 |
|
28 |
| -#define __safe_cmp(x, y) \ |
29 |
| - (__typecheck(x, y) && __no_side_effects(x, y)) |
| 29 | +#define __types_ok(x, y) \ |
| 30 | + (__is_signed(x) == __is_signed(y)) |
30 | 31 |
|
31 |
| -#define __cmp(x, y, op) ((x) op (y) ? (x) : (y)) |
| 32 | +#define __cmp_op_min < |
| 33 | +#define __cmp_op_max > |
32 | 34 |
|
33 |
| -#define __cmp_once(x, y, unique_x, unique_y, op) ({ \ |
| 35 | +#define __cmp(op, x, y) ((x) __cmp_op_##op (y) ? (x) : (y)) |
| 36 | + |
| 37 | +#define __cmp_once(op, x, y, unique_x, unique_y) ({ \ |
34 | 38 | typeof(x) unique_x = (x); \
|
35 | 39 | typeof(y) unique_y = (y); \
|
36 |
| - __cmp(unique_x, unique_y, op); }) |
| 40 | + static_assert(__types_ok(x, y), \ |
| 41 | + #op "(" #x ", " #y ") signedness error, fix types or consider u" #op "() before " #op "_t()"); \ |
| 42 | + __cmp(op, unique_x, unique_y); }) |
37 | 43 |
|
38 |
| -#define __careful_cmp(x, y, op) \ |
39 |
| - __builtin_choose_expr(__safe_cmp(x, y), \ |
40 |
| - __cmp(x, y, op), \ |
41 |
| - __cmp_once(x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y), op)) |
| 44 | +#define __careful_cmp(op, x, y) \ |
| 45 | + __builtin_choose_expr(__is_constexpr((x) - (y)), \ |
| 46 | + __cmp(op, x, y), \ |
| 47 | + __cmp_once(op, x, y, __UNIQUE_ID(__x), __UNIQUE_ID(__y))) |
42 | 48 |
|
43 | 49 | #define __clamp(val, lo, hi) \
|
44 | 50 | ((val) >= (hi) ? (hi) : ((val) <= (lo) ? (lo) : (val)))
|
|
47 | 53 | typeof(val) unique_val = (val); \
|
48 | 54 | typeof(lo) unique_lo = (lo); \
|
49 | 55 | typeof(hi) unique_hi = (hi); \
|
| 56 | + static_assert(__builtin_choose_expr(__is_constexpr((lo) > (hi)), \ |
| 57 | + (lo) <= (hi), true), \ |
| 58 | + "clamp() low limit " #lo " greater than high limit " #hi); \ |
| 59 | + static_assert(__types_ok(val, lo), "clamp() 'lo' signedness error"); \ |
| 60 | + static_assert(__types_ok(val, hi), "clamp() 'hi' signedness error"); \ |
50 | 61 | __clamp(unique_val, unique_lo, unique_hi); })
|
51 | 62 |
|
52 |
| -#define __clamp_input_check(lo, hi) \ |
53 |
| - (BUILD_BUG_ON_ZERO(__builtin_choose_expr( \ |
54 |
| - __is_constexpr((lo) > (hi)), (lo) > (hi), false))) |
55 |
| - |
56 | 63 | #define __careful_clamp(val, lo, hi) ({ \
|
57 |
| - __clamp_input_check(lo, hi) + \ |
58 |
| - __builtin_choose_expr(__typecheck(val, lo) && __typecheck(val, hi) && \ |
59 |
| - __typecheck(hi, lo) && __is_constexpr(val) && \ |
60 |
| - __is_constexpr(lo) && __is_constexpr(hi), \ |
| 64 | + __builtin_choose_expr(__is_constexpr((val) - (lo) + (hi)), \ |
61 | 65 | __clamp(val, lo, hi), \
|
62 | 66 | __clamp_once(val, lo, hi, __UNIQUE_ID(__val), \
|
63 | 67 | __UNIQUE_ID(__lo), __UNIQUE_ID(__hi))); })
|
|
67 | 71 | * @x: first value
|
68 | 72 | * @y: second value
|
69 | 73 | */
|
70 |
| -#define min(x, y) __careful_cmp(x, y, <) |
| 74 | +#define min(x, y) __careful_cmp(min, x, y) |
71 | 75 |
|
72 | 76 | /**
|
73 | 77 | * max - return maximum of two values of the same or compatible types
|
74 | 78 | * @x: first value
|
75 | 79 | * @y: second value
|
76 | 80 | */
|
77 |
| -#define max(x, y) __careful_cmp(x, y, >) |
| 81 | +#define max(x, y) __careful_cmp(max, x, y) |
78 | 82 |
|
79 | 83 | /**
|
80 | 84 | * umin - return minimum of two non-negative values
|
|
83 | 87 | * @y: second value
|
84 | 88 | */
|
85 | 89 | #define umin(x, y) \
|
86 |
| - __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, <) |
| 90 | + __careful_cmp(min, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) |
87 | 91 |
|
88 | 92 | /**
|
89 | 93 | * umax - return maximum of two non-negative values
|
90 | 94 | * @x: first value
|
91 | 95 | * @y: second value
|
92 | 96 | */
|
93 | 97 | #define umax(x, y) \
|
94 |
| - __careful_cmp((x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull, >) |
| 98 | + __careful_cmp(max, (x) + 0u + 0ul + 0ull, (y) + 0u + 0ul + 0ull) |
95 | 99 |
|
96 | 100 | /**
|
97 | 101 | * min3 - return minimum of three values
|
|
143 | 147 | * @x: first value
|
144 | 148 | * @y: second value
|
145 | 149 | */
|
146 |
| -#define min_t(type, x, y) __careful_cmp((type)(x), (type)(y), <) |
| 150 | +#define min_t(type, x, y) __careful_cmp(min, (type)(x), (type)(y)) |
147 | 151 |
|
148 | 152 | /**
|
149 | 153 | * max_t - return maximum of two values, using the specified type
|
150 | 154 | * @type: data type to use
|
151 | 155 | * @x: first value
|
152 | 156 | * @y: second value
|
153 | 157 | */
|
154 |
| -#define max_t(type, x, y) __careful_cmp((type)(x), (type)(y), >) |
| 158 | +#define max_t(type, x, y) __careful_cmp(max, (type)(x), (type)(y)) |
155 | 159 |
|
156 | 160 | /*
|
157 | 161 | * Do not check the array parameter using __must_be_array().
|
|
0 commit comments