Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Martin Wilck and committed by
Martin K. Petersen
dbef91ec 2217a47d

+24 -11
+24 -11
include/linux/log2.h
··· 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 76 * @n: parameter 77 77 * 78 - * constant-capable log of base 2 calculation 79 - * - this can be used to initialise global variables from constant data, hence 80 - * the massive ternary operator construction 81 - * 82 - * selects the appropriately-sized optimised version depending on sizeof(n) 78 + * Use this where sparse expects a true constant expression, e.g. for array 79 + * indices. 83 80 */ 84 - #define ilog2(n) \ 81 + #define const_ilog2(n) \ 85 82 ( \ 86 83 __builtin_constant_p(n) ? ( \ 87 84 (n) < 2 ? 0 : \ ··· 144 147 (n) & (1ULL << 4) ? 4 : \ 145 148 (n) & (1ULL << 3) ? 3 : \ 146 149 (n) & (1ULL << 2) ? 2 : \ 147 - 1 ) : \ 148 - (sizeof(n) <= 4) ? \ 149 - __ilog2_u32(n) : \ 150 - __ilog2_u64(n) \ 150 + 1) : \ 151 + -1) 152 + 153 + /** 154 + * ilog2 - log base 2 of 32-bit or a 64-bit unsigned value 155 + * @n: parameter 156 + * 157 + * constant-capable log of base 2 calculation 158 + * - this can be used to initialise global variables from constant data, hence 159 + * the massive ternary operator construction 160 + * 161 + * selects the appropriately-sized optimised version depending on sizeof(n) 162 + */ 163 + #define ilog2(n) \ 164 + ( \ 165 + __builtin_constant_p(n) ? \ 166 + const_ilog2(n) : \ 167 + (sizeof(n) <= 4) ? \ 168 + __ilog2_u32(n) : \ 169 + __ilog2_u64(n) \ 151 170 ) 152 171 153 172 /**