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.git


The following commit(s) were added to refs/heads/master by this push:
     new bd5e017ebd7 fs/mnemofs: Add portable bit primitives and cleanup hash 
functions
bd5e017ebd7 is described below

commit bd5e017ebd78783a7d8dc4b8afec4dce9cdc3fae
Author: Sumit6307 <[email protected]>
AuthorDate: Tue Mar 10 01:30:59 2026 +0530

    fs/mnemofs: Add portable bit primitives and cleanup hash functions
    
    This PR addresses several portability and technical debt issues in the 
mnemofs filesystem by resolving source-level TODO items.
    
    Changes:
    - Implemented a portable fallback for mfs\_clz (Count Leading Zeros) in 
fs/mnemofs/mnemofs.h using a binary search approach. This ensures compatibility 
with non-GCC compilers.
    - Removed the redundant 8-bit mfs\_arrhash and consolidated hashing with 
the existing 16-bit mfs\_hash in mnemofs\_util.c.
    - Removed the related TODO comments in mnemofs.h and mnemofs\_util.c.
    - Fixed NuttX style (indentation and braces) in the fallback bit primitives.
    
    Signed-off-by: Sumit <[email protected]>
---
 fs/mnemofs/mnemofs.h      | 148 +++++++++++++++++++++++++++++++---------------
 fs/mnemofs/mnemofs_util.c |  23 -------
 2 files changed, 100 insertions(+), 71 deletions(-)

diff --git a/fs/mnemofs/mnemofs.h b/fs/mnemofs/mnemofs.h
index fa346eabc28..5a0a47c2152 100644
--- a/fs/mnemofs/mnemofs.h
+++ b/fs/mnemofs/mnemofs.h
@@ -338,73 +338,129 @@ static mfs_t inline mfs_blkremsz(FAR const struct 
mfs_sb_s * const sb,
 static inline mfs_t mfs_ctz(const uint32_t x)
 {
   if (predict_false(x == 0))
-  {
-/* Special case, since we're using this for the CTZ skip list. The 0th
- * block has no pointers.
- */
-
-    return 0;
-  }
+    {
+      /* Special case, since we're using this for the CTZ skip list. The 0th
+       * block has no pointers.
+       */
 
+      return 0;
+    }
 #if defined(__GNUC__)
   return __builtin_ctz(x);
 #else
   uint32_t c;
 
-/* Credits:
- * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightBinSearch
- */
+  /* Credits:
+   * http://graphics.stanford.edu/~seander/bithacks.html
+   * #ZerosOnRightBinSearch
+   */
 
   if (x & 0x1)
-  {
-    /* special case for odd x (assumed to happen half of the time) */
-
-    c = 0;
-  }
-  else
-  {
-    c = 1;
-    if ((x & 0xffff) == 0)
-    {
-      x >>= 16;
-      c += 16;
-    }
-    if ((x & 0xff) == 0)
     {
-      x >>= 8;
-      c += 8;
-    }
-    if ((x & 0xf) == 0)
-    {
-      x >>= 4;
-      c += 4;
+      /* special case for odd x (assumed to happen half of the time) */
+
+      c = 0;
     }
-    if ((x & 0x3) == 0)
+  else
     {
-      x >>= 2;
-      c += 2;
+      c = 1;
+      if ((x & 0xffff) == 0)
+        {
+          x >>= 16;
+          c += 16;
+        }
+
+      if ((x & 0xff) == 0)
+        {
+          x >>= 8;
+          c += 8;
+        }
+
+      if ((x & 0xf) == 0)
+        {
+          x >>= 4;
+          c += 4;
+        }
+
+      if ((x & 0x3) == 0)
+        {
+          x >>= 2;
+          c += 2;
+        }
+
+      c -= x & 0x1;
     }
-    c -= x & 0x1;
-  }
+
   return c;
 #endif
 }
 
+/****************************************************************************
+ * Name: mfs_clz
+ *
+ * Description:
+ *   Count Leading Zeros. Returns the number of leading zeros in a 32-bit
+ *   integer.
+ *
+ * Input Parameters:
+ *   x - 32-bit integer to check.
+ *
+ * Returned Value:
+ *   The number of leading zeros.
+ *
+ ****************************************************************************/
+
 static inline mfs_t mfs_clz(const uint32_t x)
 {
   if (predict_false(x == UINT32_MAX))
-  {
-/* Special case, since we're using this for the CTZ skip list. The 0th
- * block has no pointers.
- */
-
-    return 0;
-  }
+    {
+      /* Special case, since we're using this for the CTZ skip list. The 0th
+       * block has no pointers.
+       */
 
+      return 0;
+    }
 #if defined(__GNUC__)
   return __builtin_clz(x);
 #else
-  return 0; /* TODO */
+  uint32_t n = 0;
+  uint32_t x_tmp = x;
+
+  if (x_tmp == 0)
+    {
+      return 32;
+    }
+
+  if (x_tmp <= 0x0000ffff)
+    {
+      n += 16;
+      x_tmp <<= 16;
+    }
+
+  if (x_tmp <= 0x00ffffff)
+    {
+      n += 8;
+      x_tmp <<= 8;
+    }
+
+  if (x_tmp <= 0x0fffffff)
+    {
+      n += 4;
+      x_tmp <<= 4;
+    }
+
+  if (x_tmp <= 0x3fffffff)
+    {
+      n += 2;
+      x_tmp <<= 2;
+    }
+
+  if (x_tmp <= 0x7fffffff)
+    {
+      n += 1;
+    }
+
+  return n;
 #endif
 }
 
@@ -1041,10 +1097,6 @@ int mfs_erase_nblks(FAR const struct mfs_sb_s * const 
sb, const off_t blk,
  *
  ****************************************************************************/
 
-uint8_t mfs_arrhash(FAR const char *arr, ssize_t len);
-
-/* TODO: Put below in place of above. */
-
 uint16_t mfs_hash(FAR const char *arr, ssize_t len);
 
 /****************************************************************************
diff --git a/fs/mnemofs/mnemofs_util.c b/fs/mnemofs/mnemofs_util.c
index b6edb9cf197..b98661ee88e 100644
--- a/fs/mnemofs/mnemofs_util.c
+++ b/fs/mnemofs/mnemofs_util.c
@@ -87,35 +87,12 @@
  * Public Functions
  ****************************************************************************/
 
-uint8_t mfs_arrhash(FAR const char *arr, ssize_t len)
-{
-  ssize_t  l    = 0;
-  ssize_t  r    = len - 1;
-  uint16_t hash = 0;
-
-  /* TODO: Change the array checksum to be 16 bit long. */
-
-  while (l <= r)
-    {
-      hash += arr[l] * arr[r] * (l + 1) * (r + 1);
-      l++;
-      r--;
-      hash %= (1 << 8);
-    }
-
-  finfo("Hash calculated for size %zd to be %d.", len, hash % (1 << 8));
-
-  return hash % (1 << 8);
-}
-
 uint16_t mfs_hash(FAR const char *arr, ssize_t len)
 {
   ssize_t  l    = 0;
   ssize_t  r    = len - 1;
   uint32_t hash = 0;
 
-  /* TODO: Change the array checksum to be 16 bit long. */
-
   while (l <= r)
     {
       hash += arr[l] * arr[r] * (l + 1) * (r + 1);

Reply via email to