|
8 | 8 | #include <linux/types.h>
|
9 | 9 |
|
10 | 10 | /*
|
11 |
| - * min()/max()/clamp() macros must accomplish three things: |
| 11 | + * min()/max()/clamp() macros must accomplish several things: |
12 | 12 | *
|
13 | 13 | * - Avoid multiple evaluations of the arguments (so side-effects like
|
14 | 14 | * "x++" happen only once) when non-constant.
|
15 |
| - * - Retain result as a constant expressions when called with only |
16 |
| - * constant expressions (to avoid tripping VLA warnings in stack |
17 |
| - * allocation usage). |
18 | 15 | * - Perform signed v unsigned type-checking (to generate compile
|
19 | 16 | * errors instead of nasty runtime surprises).
|
20 | 17 | * - Unsigned char/short are always promoted to signed int and can be
|
|
31 | 28 | * bit #0 set if ok for unsigned comparisons
|
32 | 29 | * bit #1 set if ok for signed comparisons
|
33 | 30 | *
|
34 |
| - * In particular, statically non-negative signed integer |
35 |
| - * expressions are ok for both. |
| 31 | + * In particular, statically non-negative signed integer expressions |
| 32 | + * are ok for both. |
36 | 33 | *
|
37 |
| - * NOTE! Unsigned types smaller than 'int' are implicitly |
38 |
| - * converted to 'int' in expressions, and are accepted for |
39 |
| - * signed conversions for now. This is debatable. |
| 34 | + * NOTE! Unsigned types smaller than 'int' are implicitly converted to 'int' |
| 35 | + * in expressions, and are accepted for signed conversions for now. |
| 36 | + * This is debatable. |
40 | 37 | *
|
41 |
| - * Note that 'x' is the original expression, and 'ux' is |
42 |
| - * the unique variable that contains the value. |
| 38 | + * Note that 'x' is the original expression, and 'ux' is the unique variable |
| 39 | + * that contains the value. |
43 | 40 | *
|
44 |
| - * We use 'ux' for pure type checking, and 'x' for when |
45 |
| - * we need to look at the value (but without evaluating |
46 |
| - * it for side effects! Careful to only ever evaluate it |
47 |
| - * with sizeof() or __builtin_constant_p() etc). |
| 41 | + * We use 'ux' for pure type checking, and 'x' for when we need to look at the |
| 42 | + * value (but without evaluating it for side effects! |
| 43 | + * Careful to only ever evaluate it with sizeof() or __builtin_constant_p() etc). |
48 | 44 | *
|
49 |
| - * Pointers end up being checked by the normal C type |
50 |
| - * rules at the actual comparison, and these expressions |
51 |
| - * only need to be careful to not cause warnings for |
52 |
| - * pointer use. |
| 45 | + * Pointers end up being checked by the normal C type rules at the actual |
| 46 | + * comparison, and these expressions only need to be careful to not cause |
| 47 | + * warnings for pointer use. |
53 | 48 | */
|
54 | 49 | #define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux))
|
55 | 50 | #define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4))
|
56 | 51 | #define __sign_use(x, ux) (is_signed_type(typeof(ux)) ? \
|
57 | 52 | __signed_type_use(x, ux) : __unsigned_type_use(x, ux))
|
58 | 53 |
|
59 | 54 | /*
|
60 |
| - * To avoid warnings about casting pointers to integers |
61 |
| - * of different sizes, we need that special sign type. |
| 55 | + * Check whether a signed value is always non-negative. |
62 | 56 | *
|
63 |
| - * On 64-bit we can just always use 'long', since any |
64 |
| - * integer or pointer type can just be cast to that. |
| 57 | + * A cast is needed to avoid any warnings from values that aren't signed |
| 58 | + * integer types (in which case the result doesn't matter). |
65 | 59 | *
|
66 |
| - * This does not work for 128-bit signed integers since |
67 |
| - * the cast would truncate them, but we do not use s128 |
68 |
| - * types in the kernel (we do use 'u128', but they will |
69 |
| - * be handled by the !is_signed_type() case). |
| 60 | + * On 64-bit any integer or pointer type can safely be cast to 'long'. |
| 61 | + * But on 32-bit we need to avoid warnings about casting pointers to integers |
| 62 | + * of different sizes without truncating 64-bit values so 'long' or 'long long' |
| 63 | + * must be used depending on the size of the value. |
70 | 64 | *
|
71 |
| - * NOTE! The cast is there only to avoid any warnings |
72 |
| - * from when values that aren't signed integer types. |
| 65 | + * This does not work for 128-bit signed integers since the cast would truncate |
| 66 | + * them, but we do not use s128 types in the kernel (we do use 'u128', |
| 67 | + * but they are handled by the !is_signed_type() case). |
73 | 68 | */
|
74 | 69 | #ifdef CONFIG_64BIT
|
75 | 70 | #define __signed_type(ux) long
|
|
0 commit comments