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 <sumit6307@gmail.com>
This commit is contained in:
Sumit6307
2026-03-10 01:30:59 +05:30
committed by Alan C. Assis
parent 7bfd6ee33e
commit bd5e017ebd
2 changed files with 100 additions and 71 deletions
+60 -8
View File
@@ -345,14 +345,14 @@ static inline mfs_t mfs_ctz(const uint32_t x)
return 0; return 0;
} }
#if defined(__GNUC__) #if defined(__GNUC__)
return __builtin_ctz(x); return __builtin_ctz(x);
#else #else
uint32_t c; uint32_t c;
/* Credits: /* Credits:
* http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightBinSearch * http://graphics.stanford.edu/~seander/bithacks.html
* #ZerosOnRightBinSearch
*/ */
if (x & 0x1) if (x & 0x1)
@@ -369,27 +369,47 @@ static inline mfs_t mfs_ctz(const uint32_t x)
x >>= 16; x >>= 16;
c += 16; c += 16;
} }
if ((x & 0xff) == 0) if ((x & 0xff) == 0)
{ {
x >>= 8; x >>= 8;
c += 8; c += 8;
} }
if ((x & 0xf) == 0) if ((x & 0xf) == 0)
{ {
x >>= 4; x >>= 4;
c += 4; c += 4;
} }
if ((x & 0x3) == 0) if ((x & 0x3) == 0)
{ {
x >>= 2; x >>= 2;
c += 2; c += 2;
} }
c -= x & 0x1; c -= x & 0x1;
} }
return c; return c;
#endif #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) static inline mfs_t mfs_clz(const uint32_t x)
{ {
if (predict_false(x == UINT32_MAX)) if (predict_false(x == UINT32_MAX))
@@ -400,11 +420,47 @@ static inline mfs_t mfs_clz(const uint32_t x)
return 0; return 0;
} }
#if defined(__GNUC__) #if defined(__GNUC__)
return __builtin_clz(x); return __builtin_clz(x);
#else #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 #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); uint16_t mfs_hash(FAR const char *arr, ssize_t len);
/**************************************************************************** /****************************************************************************
-23
View File
@@ -87,35 +87,12 @@
* Public Functions * 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) uint16_t mfs_hash(FAR const char *arr, ssize_t len)
{ {
ssize_t l = 0; ssize_t l = 0;
ssize_t r = len - 1; ssize_t r = len - 1;
uint32_t hash = 0; uint32_t hash = 0;
/* TODO: Change the array checksum to be 16 bit long. */
while (l <= r) while (l <= r)
{ {
hash += arr[l] * arr[r] * (l + 1) * (r + 1); hash += arr[l] * arr[r] * (l + 1) * (r + 1);