This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 27bc54d1a108b02d8c7e319e036991eedc6b6f85 Author: ouyangxiangzhen <[email protected]> AuthorDate: Tue Nov 17 18:41:57 2026 +0800 lib/math32: give the invdiv initializer's constants an unsigned type MISRA C Rule 7.2 wants a "u" or "U" suffix on every integer constant that is represented in an unsigned type, and Rule 10.4 wants both operands of an operator to share an essential type category. The constants in invdiv_init_param64() were plain int literals while every operand around them is uint64_t (d, t, ip->mult) or _uint128_t, so the suffixes make the conversions explicit. The suffix is added where the unsigned type is certain: the normalizer computations (d - 1u, == 0u), the contract assertions (d > 1u, b > 1u, base > 0u), the biases (+ 1u), the 128-bit path (one128 = 1u, + 1u), the 64-bit unit shifts of both initializers ((uint64_t)1u << l) and the shift constant of invdiv_u64() (sh1 = 1u). Shift counts themselves keep their plain type: a shift does not perform the usual arithmetic conversions, so Rule 10.4 does not apply to it, and Rule 12.2 only requires the count to be smaller than the width. Left as plain int on purpose: the FLS(), LOG2_FLOOR(), LOG2_CEIL() and IS_POWER_OF_2() macros, whose argument may be signed -- OPEN_MAX and CONFIG_PID_INITIAL_COUNT are int -- and where a suffix would break Rule 10.4 for those callers instead of fixing it. Verified: the normalizer is unchanged for every d in [2, 5000000] and on the 2^63 boundaries (0 differences), s still equals clz(d) on the same set, and the 32-bit and 64-bit invdiv differential batteries keep 0 mismatches. nucleo-f072rb:nsh and rv-virt:smp64 (with CONFIG_ONESHOT_FAST_DIVISION, which instantiates invdiv_init_param64) build with no new warnings. Assisted-By: mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <[email protected]> --- include/nuttx/lib/math32.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h index 6423adafc0b..e52fb7432cf 100644 --- a/include/nuttx/lib/math32.h +++ b/include/nuttx/lib/math32.h @@ -284,12 +284,12 @@ static inline_function void invdiv_init_param32(uint32_t d, FAR invdiv_param32_t *ip) { int l = LOG2_CEIL(d); - uint64_t t1 = (uint64_t)1 << 32; - uint64_t t2 = (uint64_t)1 << l; + uint64_t t1 = (uint64_t)1u << 32; + uint64_t t2 = (uint64_t)1u << l; - DEBUGASSERT(d > 1); + DEBUGASSERT(d > 1u); - ip->mult = (t1 * (t2 - d)) / d + 1; + ip->mult = (t1 * (t2 - d)) / d + 1u; #if ULONG_MAX == 4294967295UL ip->shift = l - 1; #else @@ -473,18 +473,18 @@ static inline_function void invdiv_init_param64(uint64_t d, FAR invdiv_param64_t *ip) { int l = LOG2_CEIL(d); - uint64_t t = ((uint64_t)1 << l) - d; + uint64_t t = ((uint64_t)1u << l) - d; - DEBUGASSERT(d > 1 && d <= ((uint64_t)1 << 63)); + DEBUGASSERT(d > 1u && d <= ((uint64_t)1u << 63)); #ifdef __INT128_DEFINED - _uint128_t one128 = 1; + _uint128_t one128 = 1u; - ip->mult = (one128 << 64) * t / d + 1; + ip->mult = (one128 << 64) * t / d + 1u; #else - int s = 64 - l - (((d - 1) & d) == 0); /* = clz(d) */ + int s = 64 - l - (((d - 1u) & d) == 0u); /* = clz(d) */ - ip->mult = invdiv_udiv128_soft(t, 0, d, s) + 1; + ip->mult = invdiv_udiv128_soft(t, 0, d, s) + 1u; #endif ip->shift = l - 1; } @@ -512,7 +512,7 @@ uint64_t invdiv_u64(uint64_t n, FAR const invdiv_param64_t *ip) uint64_t t1; uint64_t t2; uint64_t m = ip->mult; - uint8_t sh1 = 1; + uint8_t sh1 = 1u; uint8_t sh2 = ip->shift; /* t1 is the high half of the 128-bit product n * m. Both operands @@ -529,7 +529,7 @@ static inline_function uint64_t div64_const32(uint64_t n, uint32_t b) { invdiv_param64_t ip; - DEBUGASSERT(b > 1); + DEBUGASSERT(b > 1u); invdiv_init_param(b, &ip); return invdiv_u64(n, &ip); @@ -553,7 +553,7 @@ static inline_function uint64_t div64_const32(uint64_t n, uint32_t b) static inline_function uint64_t div64_const(uint64_t n, uint64_t base) { - DEBUGASSERT(base > 0); + DEBUGASSERT(base > 0u); if (IS_POWER_OF_2(base)) {
