From e3376985be71ae4022b080a45506514bec4261be Mon Sep 17 00:00:00 2001 From: dongjiuzhu1 Date: Wed, 31 Dec 2025 14:35:54 +0800 Subject: [PATCH] nshlib/ls_handler: eliminate floating-point operations for human-readable sizes Replace floating-point arithmetic with fixed-point integer math to avoid linking soft-float library (~2-3KB Flash) when displaying human-readable file sizes (ls -lh command). Changes: - Use integer division and modulo to calculate size components - Calculate decimal part: (remainder * 10) / unit for one decimal place - Refactor duplicated code: consolidate GB/MB/KB logic into single path - Remove CONFIG_HAVE_FLOAT dependency This eliminates calls to __aeabi_f2d, __aeabi_fmul, __aeabi_i2f and other ARM EABI floating-point helpers, reducing Flash footprint for systems compiled with -mfloat-abi=soft. Signed-off-by: dongjiuzhu1 --- nshlib/nsh_fscmds.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/nshlib/nsh_fscmds.c b/nshlib/nsh_fscmds.c index 1c705befebc..c1abcd357f2 100644 --- a/nshlib/nsh_fscmds.c +++ b/nshlib/nsh_fscmds.c @@ -518,24 +518,39 @@ static int ls_handler(FAR struct nsh_vtbl_s *vtbl, FAR const char *dirpath, if ((lsflags & LSFLAGS_SIZE) != 0) { -#ifdef CONFIG_HAVE_FLOAT if (lsflags & LSFLAGS_HUMANREADBLE && buf.st_size >= KB) { + uint32_t integer_part; + uint32_t decimal_part; + uint32_t unit; + char suffix; + + /* Determine the appropriate unit and suffix */ + if (buf.st_size >= GB) { - nsh_output(vtbl, "%11.1fG", (float)buf.st_size / GB); + unit = GB; + suffix = 'G'; } else if (buf.st_size >= MB) { - nsh_output(vtbl, "%11.1fM", (float)buf.st_size / MB); + unit = MB; + suffix = 'M'; } else { - nsh_output(vtbl, "%11.1fK", (float)buf.st_size / KB); + unit = KB; + suffix = 'K'; } + + /* Use integer arithmetic to avoid floating point */ + + integer_part = buf.st_size / unit; + decimal_part = ((buf.st_size % unit) * 10) / unit; + nsh_output(vtbl, "%10" PRIu32 ".%" PRIu32 "%c", + integer_part, decimal_part, suffix); } else -#endif { nsh_output(vtbl, "%12" PRIdOFF, buf.st_size); } @@ -1549,11 +1564,10 @@ int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, FAR char **argv) lsflags |= LSFLAGS_SIZE; break; -#ifdef CONFIG_HAVE_FLOAT case 'h': lsflags |= LSFLAGS_HUMANREADBLE; break; -#endif + case '?': default: nsh_error(vtbl, g_fmtarginvalid, argv[0]);