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 e695570d658276bd5f1d91241b345d47e7c132a5 Author: ouyangxiangzhen <[email protected]> AuthorDate: Fri Nov 6 14:29:45 2026 +0800 lib/math32: implement rounding div_const variants as div_const wrappers div_const_roundup() and div_const_roundnearest() each duplicated the width dispatch and the magic-number call of div_const(), the only difference being the rounding bias folded into the dividend. Route both through div_const() with the biased dividend ((n) + (base) - 1 and (n) + (base) / 2), so the width dispatch and the div64_const() machinery live in one place and later dispatch changes only need to touch div_const(). The bias constants take the essential type of the divisor through MATH32_ONE() (base / base): a plain literal breaks MISRA C Rule 10.4 for the other argument kind, and an unsigned literal wraps negative signed dividends. Result values are unchanged on every path; the mixed narrow-dividend case now shares the magic path and returns the identical value. Evidence: differential test against a snapshot of the old macro expansions, 1804 checks over 12 constant bases x 4 dividend/divisor width combos x edge and pseudo-random dividends, 0 mismatches on gcc -m64, gcc -m32 and clang -m64, each with and without CONFIG_DEBUG_NOOPT. qemu-intel64 build passes. Assisted-By: mimo-v2.6-pro Signed-off-by: ouyangxiangzhen <[email protected]> --- include/nuttx/lib/math32.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h index f36700bf695..b9da9150f06 100644 --- a/include/nuttx/lib/math32.h +++ b/include/nuttx/lib/math32.h @@ -360,12 +360,16 @@ static inline_function uint64_t div64_const(uint64_t n, uint64_t base) #define div_const(n, base) \ ((sizeof((n)) == sizeof(uint64_t)) ? div64_const(n, base) : ((n) / (base))) +/* Unit constant in the essential type of x (x > 0), so MISRA C Rule 10.4 + * holds for both signed and unsigned arguments. + */ + +#define MATH32_ONE(x) ((x) / (x)) + #define div_const_roundup(n, base) \ - ((sizeof((n)) == sizeof(uint64_t)) ? div64_const((n) + (base) - 1u, base) : \ - (((n) + (base) - 1u) / (base))) + div_const((n) + (base) - MATH32_ONE(base), base) #define div_const_roundnearest(n, base) \ - ((sizeof((n)) == sizeof(uint64_t)) ? div64_const((n) + ((base) / 2u), base) : \ - (((n) + ((base) / 2u)) / (base))) + div_const((n) + ((base) / (MATH32_ONE(base) + MATH32_ONE(base))), base) /* Division optimizing method proposed by T. Granlund and L. Montgomery. * This method converts the runtime-invariant integer division
