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 <dongjiuzhu1@xiaomi.com>
This commit is contained in:
dongjiuzhu1 2025-12-31 14:35:54 +08:00 committed by Alan C. Assis
parent 710aef6bd0
commit c90b64ed8b

View file

@ -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]);