diff options
author | Martin Wilck <mwilck@suse.com> | 2018-04-17 19:35:06 -0400 |
---|---|---|
committer | Martin K. Petersen <martin.petersen@oracle.com> | 2018-04-20 15:57:32 -0400 |
commit | dbef91ec5482239055dd2db8ec656fc13d382add (patch) | |
tree | 713484f124faf033bd7970cfbb8cdcfd080891b2 | |
parent | 2217a47de42f85b69714c2a621af13cfeae35b40 (diff) |
scsi: ilog2: create truly constant version for sparse
Sparse emits errors about ilog2() in array indices because of the use of
__ilog2_32() and __ilog2_64(), rightly so
(https://www.spinics.net/lists/linux-sparse/msg03471.html).
Create a const_ilog2() variant that works with sparse for this scenario.
(Note: checkpatch.pl complains about missing parentheses, but that
appears to be a false positive. I can get rid of the warning simply by
inserting whitespace, making checkpatch "see" the whole macro).
Signed-off-by: Martin Wilck <mwilck@suse.com>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
-rw-r--r-- | include/linux/log2.h | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/include/linux/log2.h b/include/linux/log2.h index 41a1ae010993..2af7f77866d0 100644 --- a/include/linux/log2.h +++ b/include/linux/log2.h | |||
@@ -72,16 +72,13 @@ unsigned long __rounddown_pow_of_two(unsigned long n) | |||
72 | } | 72 | } |
73 | 73 | ||
74 | /** | 74 | /** |
75 | * ilog2 - log base 2 of 32-bit or a 64-bit unsigned value | 75 | * const_ilog2 - log base 2 of 32-bit or a 64-bit constant unsigned value |
76 | * @n: parameter | 76 | * @n: parameter |
77 | * | 77 | * |
78 | * constant-capable log of base 2 calculation | 78 | * Use this where sparse expects a true constant expression, e.g. for array |
79 | * - this can be used to initialise global variables from constant data, hence | 79 | * indices. |
80 | * the massive ternary operator construction | ||
81 | * | ||
82 | * selects the appropriately-sized optimised version depending on sizeof(n) | ||
83 | */ | 80 | */ |
84 | #define ilog2(n) \ | 81 | #define const_ilog2(n) \ |
85 | ( \ | 82 | ( \ |
86 | __builtin_constant_p(n) ? ( \ | 83 | __builtin_constant_p(n) ? ( \ |
87 | (n) < 2 ? 0 : \ | 84 | (n) < 2 ? 0 : \ |
@@ -147,10 +144,26 @@ unsigned long __rounddown_pow_of_two(unsigned long n) | |||
147 | (n) & (1ULL << 4) ? 4 : \ | 144 | (n) & (1ULL << 4) ? 4 : \ |
148 | (n) & (1ULL << 3) ? 3 : \ | 145 | (n) & (1ULL << 3) ? 3 : \ |
149 | (n) & (1ULL << 2) ? 2 : \ | 146 | (n) & (1ULL << 2) ? 2 : \ |
150 | 1 ) : \ | 147 | 1) : \ |
151 | (sizeof(n) <= 4) ? \ | 148 | -1) |
152 | __ilog2_u32(n) : \ | 149 | |
153 | __ilog2_u64(n) \ | 150 | /** |
151 | * ilog2 - log base 2 of 32-bit or a 64-bit unsigned value | ||
152 | * @n: parameter | ||
153 | * | ||
154 | * constant-capable log of base 2 calculation | ||
155 | * - this can be used to initialise global variables from constant data, hence | ||
156 | * the massive ternary operator construction | ||
157 | * | ||
158 | * selects the appropriately-sized optimised version depending on sizeof(n) | ||
159 | */ | ||
160 | #define ilog2(n) \ | ||
161 | ( \ | ||
162 | __builtin_constant_p(n) ? \ | ||
163 | const_ilog2(n) : \ | ||
164 | (sizeof(n) <= 4) ? \ | ||
165 | __ilog2_u32(n) : \ | ||
166 | __ilog2_u64(n) \ | ||
154 | ) | 167 | ) |
155 | 168 | ||
156 | /** | 169 | /** |