Skip to content

Commit 10666e9

Browse files
david-laightakpm00
authored andcommitted
minmax.h: update some comments
- Change three to several. - Remove the comment about retaining constant expressions, no longer true. - Realign to nearer 80 columns and break on major punctiation. - Add a leading comment to the block before __signed_type() and __is_nonneg() Otherwise the block explaining the cast is a bit 'floating'. Reword the rest of that comment to improve readability. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: David Laight <[email protected]> Cc: Andy Shevchenko <[email protected]> Cc: Arnd Bergmann <[email protected]> Cc: Christoph Hellwig <[email protected]> Cc: Dan Carpenter <[email protected]> Cc: Jason A. Donenfeld <[email protected]> Cc: Jens Axboe <[email protected]> Cc: Lorenzo Stoakes <[email protected]> Cc: Mateusz Guzik <[email protected]> Cc: Matthew Wilcox <[email protected]> Cc: Pedro Falcato <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 71ee9b1 commit 10666e9

File tree

1 file changed

+24
-29
lines changed

1 file changed

+24
-29
lines changed

include/linux/minmax.h

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@
88
#include <linux/types.h>
99

1010
/*
11-
* min()/max()/clamp() macros must accomplish three things:
11+
* min()/max()/clamp() macros must accomplish several things:
1212
*
1313
* - Avoid multiple evaluations of the arguments (so side-effects like
1414
* "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).
1815
* - Perform signed v unsigned type-checking (to generate compile
1916
* errors instead of nasty runtime surprises).
2017
* - Unsigned char/short are always promoted to signed int and can be
@@ -31,45 +28,43 @@
3128
* bit #0 set if ok for unsigned comparisons
3229
* bit #1 set if ok for signed comparisons
3330
*
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.
3633
*
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.
4037
*
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.
4340
*
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).
4844
*
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.
5348
*/
5449
#define __signed_type_use(x, ux) (2 + __is_nonneg(x, ux))
5550
#define __unsigned_type_use(x, ux) (1 + 2 * (sizeof(ux) < 4))
5651
#define __sign_use(x, ux) (is_signed_type(typeof(ux)) ? \
5752
__signed_type_use(x, ux) : __unsigned_type_use(x, ux))
5853

5954
/*
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.
6256
*
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).
6559
*
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.
7064
*
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).
7368
*/
7469
#ifdef CONFIG_64BIT
7570
#define __signed_type(ux) long

0 commit comments

Comments
 (0)