Skip to content

Commit ff1fec2

Browse files
committed
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>
1 parent a09dfcd commit ff1fec2

2 files changed

Lines changed: 100 additions & 71 deletions

File tree

fs/mnemofs/mnemofs.h

Lines changed: 100 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -338,73 +338,129 @@ static mfs_t inline mfs_blkremsz(FAR const struct mfs_sb_s * const sb,
338338
static inline mfs_t mfs_ctz(const uint32_t x)
339339
{
340340
if (predict_false(x == 0))
341-
{
342-
/* Special case, since we're using this for the CTZ skip list. The 0th
343-
* block has no pointers.
344-
*/
345-
346-
return 0;
347-
}
341+
{
342+
/* Special case, since we're using this for the CTZ skip list. The 0th
343+
* block has no pointers.
344+
*/
348345

346+
return 0;
347+
}
349348
#if defined(__GNUC__)
350349
return __builtin_ctz(x);
351350
#else
352351
uint32_t c;
353352

354-
/* Credits:
355-
* http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightBinSearch
356-
*/
353+
/* Credits:
354+
* http://graphics.stanford.edu/~seander/bithacks.html
355+
* #ZerosOnRightBinSearch
356+
*/
357357

358358
if (x & 0x1)
359-
{
360-
/* special case for odd x (assumed to happen half of the time) */
361-
362-
c = 0;
363-
}
364-
else
365-
{
366-
c = 1;
367-
if ((x & 0xffff) == 0)
368-
{
369-
x >>= 16;
370-
c += 16;
371-
}
372-
if ((x & 0xff) == 0)
373359
{
374-
x >>= 8;
375-
c += 8;
376-
}
377-
if ((x & 0xf) == 0)
378-
{
379-
x >>= 4;
380-
c += 4;
360+
/* special case for odd x (assumed to happen half of the time) */
361+
362+
c = 0;
381363
}
382-
if ((x & 0x3) == 0)
364+
else
383365
{
384-
x >>= 2;
385-
c += 2;
366+
c = 1;
367+
if ((x & 0xffff) == 0)
368+
{
369+
x >>= 16;
370+
c += 16;
371+
}
372+
373+
if ((x & 0xff) == 0)
374+
{
375+
x >>= 8;
376+
c += 8;
377+
}
378+
379+
if ((x & 0xf) == 0)
380+
{
381+
x >>= 4;
382+
c += 4;
383+
}
384+
385+
if ((x & 0x3) == 0)
386+
{
387+
x >>= 2;
388+
c += 2;
389+
}
390+
391+
c -= x & 0x1;
386392
}
387-
c -= x & 0x1;
388-
}
393+
389394
return c;
390395
#endif
391396
}
392397

398+
/****************************************************************************
399+
* Name: mfs_clz
400+
*
401+
* Description:
402+
* Count Leading Zeros. Returns the number of leading zeros in a 32-bit
403+
* integer.
404+
*
405+
* Input Parameters:
406+
* x - 32-bit integer to check.
407+
*
408+
* Returned Value:
409+
* The number of leading zeros.
410+
*
411+
****************************************************************************/
412+
393413
static inline mfs_t mfs_clz(const uint32_t x)
394414
{
395415
if (predict_false(x == UINT32_MAX))
396-
{
397-
/* Special case, since we're using this for the CTZ skip list. The 0th
398-
* block has no pointers.
399-
*/
400-
401-
return 0;
402-
}
416+
{
417+
/* Special case, since we're using this for the CTZ skip list. The 0th
418+
* block has no pointers.
419+
*/
403420

421+
return 0;
422+
}
404423
#if defined(__GNUC__)
405424
return __builtin_clz(x);
406425
#else
407-
return 0; /* TODO */
426+
uint32_t n = 0;
427+
uint32_t x_tmp = x;
428+
429+
if (x_tmp == 0)
430+
{
431+
return 32;
432+
}
433+
434+
if (x_tmp <= 0x0000ffff)
435+
{
436+
n += 16;
437+
x_tmp <<= 16;
438+
}
439+
440+
if (x_tmp <= 0x00ffffff)
441+
{
442+
n += 8;
443+
x_tmp <<= 8;
444+
}
445+
446+
if (x_tmp <= 0x0fffffff)
447+
{
448+
n += 4;
449+
x_tmp <<= 4;
450+
}
451+
452+
if (x_tmp <= 0x3fffffff)
453+
{
454+
n += 2;
455+
x_tmp <<= 2;
456+
}
457+
458+
if (x_tmp <= 0x7fffffff)
459+
{
460+
n += 1;
461+
}
462+
463+
return n;
408464
#endif
409465
}
410466

@@ -1041,10 +1097,6 @@ int mfs_erase_nblks(FAR const struct mfs_sb_s * const sb, const off_t blk,
10411097
*
10421098
****************************************************************************/
10431099

1044-
uint8_t mfs_arrhash(FAR const char *arr, ssize_t len);
1045-
1046-
/* TODO: Put below in place of above. */
1047-
10481100
uint16_t mfs_hash(FAR const char *arr, ssize_t len);
10491101

10501102
/****************************************************************************

fs/mnemofs/mnemofs_util.c

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -87,35 +87,12 @@
8787
* Public Functions
8888
****************************************************************************/
8989

90-
uint8_t mfs_arrhash(FAR const char *arr, ssize_t len)
91-
{
92-
ssize_t l = 0;
93-
ssize_t r = len - 1;
94-
uint16_t hash = 0;
95-
96-
/* TODO: Change the array checksum to be 16 bit long. */
97-
98-
while (l <= r)
99-
{
100-
hash += arr[l] * arr[r] * (l + 1) * (r + 1);
101-
l++;
102-
r--;
103-
hash %= (1 << 8);
104-
}
105-
106-
finfo("Hash calculated for size %zd to be %d.", len, hash % (1 << 8));
107-
108-
return hash % (1 << 8);
109-
}
110-
11190
uint16_t mfs_hash(FAR const char *arr, ssize_t len)
11291
{
11392
ssize_t l = 0;
11493
ssize_t r = len - 1;
11594
uint32_t hash = 0;
11695

117-
/* TODO: Change the array checksum to be 16 bit long. */
118-
11996
while (l <= r)
12097
{
12198
hash += arr[l] * arr[r] * (l + 1) * (r + 1);

0 commit comments

Comments
 (0)