This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new c90b64ed8 nshlib/ls_handler: eliminate floating-point operations for 
human-readable sizes
c90b64ed8 is described below

commit c90b64ed8b18891cff4414f5e20f9a6c15b686a6
Author: dongjiuzhu1 <[email protected]>
AuthorDate: Wed Dec 31 14:35:54 2025 +0800

    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 <[email protected]>
---
 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 1c705befe..c1abcd357 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]);

Reply via email to