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 d84bdda631480c8aa795d8b50c23028100791bff Author: wangzhi16 <[email protected]> AuthorDate: Tue Apr 8 16:49:29 2025 +0800 lib/math32: fix div64_const not compatible for pre-process macro div64_const() was a braced-group statement expression, which cannot be used in static initializers: boards/arm/cxd56xx/spresense/src/ cxd56_automount.c initializes fields with MSEC2TICK(CONFIG_...) and fails with "braced-group within expression allowed only inside a function". Convert div64_const() to a static inline function. With the sizeof() dispatch in div_const(), 32-bit constant expressions fold to a plain division and therefore work in static initializers; 64-bit constant expressions remain runtime-only, as a function call is not a constant expression. Runtime division keeps the optimized implementation. The CONFIG_HAVE_EXPRESSION_STATEMENT fallback is removed as the inline function has no statement-expression dependency. Assisted-By: mimo-v2.6-pro Co-authored-by: ouyangxiangzhen <[email protected]> Signed-off-by: wangzhi16 <[email protected]> Signed-off-by: ouyangxiangzhen <[email protected]> --- include/nuttx/lib/math32.h | 59 ++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/include/nuttx/lib/math32.h b/include/nuttx/lib/math32.h index 1dfe2f80934..c1b7e79ed5e 100644 --- a/include/nuttx/lib/math32.h +++ b/include/nuttx/lib/math32.h @@ -315,39 +315,32 @@ extern "C" } \ while (0) -#if defined(CONFIG_HAVE_EXPRESSION_STATEMENT) -# define div64_const(n, base) \ - ({ \ - uint64_t __n = (n); \ - uint32_t __base = (base); \ - if (IS_POWER_OF_2(__base)) \ - { \ - (__n) >>= LOG2_FLOOR(__base); \ - } \ - else if (UINTPTR_MAX == UINT32_MAX) \ - { \ - div64_const32(__n, __base); \ - } \ - else \ - { \ - __n /= __base; \ - } \ - __n; \ - }) - -# define div_const(n, base) \ - ((sizeof(typeof(n)) == sizeof(uint64_t)) ? div64_const(n, base) : ((n) / (base))) -# define div_const_roundup(n, base) \ - ((sizeof(typeof(n)) == sizeof(uint64_t)) ? div64_const((n) + (base) - 1, base) : \ - (((n) + (base) - 1) / (base))) -# define div_const_roundnearest(n, base) \ - ((sizeof(typeof(n)) == sizeof(uint64_t)) ? div64_const((n) + ((base) / 2), base) : \ - (((n) + ((base) / 2)) / (base))) -#else -# define div_const(n, base) ((n) / (base)) -# define div_const_roundup(n, base) (((n) + (base) - 1) / (base)) -# define div_const_roundnearest(n, base) (((n) + ((base) / 2)) / (base)) -#endif +static inline_function uint64_t div64_const(uint64_t n, uint32_t base) +{ + if (IS_POWER_OF_2(base)) + { + n >>= LOG2_FLOOR(base); + } + else if (UINTPTR_MAX == UINT32_MAX) + { + div64_const32(n, base); + } + else + { + n /= base; + } + + return n; +} + +#define div_const(n, base) \ + ((sizeof(typeof(n)) == sizeof(uint64_t)) ? div64_const(n, base) : ((n) / (base))) +#define div_const_roundup(n, base) \ + ((sizeof(typeof(n)) == sizeof(uint64_t)) ? div64_const((n) + (base) - 1, base) : \ + (((n) + (base) - 1) / (base))) +#define div_const_roundnearest(n, base) \ + ((sizeof(typeof(n)) == sizeof(uint64_t)) ? div64_const((n) + ((base) / 2), base) : \ + (((n) + ((base) / 2)) / (base))) /* Division optimizing method proposed by T. Granlund and L. Montgomery. * This method converts the runtime-invariant integer division
