mu578 commented on code in PR #10724:
URL: https://github.com/apache/nuttx/pull/10724#discussion_r1331980388
##########
arch/risc-v/src/common/riscv_pmp.c:
##########
@@ -100,32 +101,6 @@ typedef struct pmp_entry_s pmp_entry_t;
* Private Functions
****************************************************************************/
-/****************************************************************************
- * Name: log2ceil
- *
- * Description:
- * Calculate the up-rounded power-of-two for input.
- *
- * Input Parameters:
- * size - The size of the PMP region.
- *
- * Returned Value:
- * Power-of-two for argument, rounded up.
- *
- ****************************************************************************/
-
-static uintptr_t log2ceil(uintptr_t size)
-{
- uintptr_t pot = 0;
-
- for (size = size - 1; size > 1; size >>= 1)
- {
- pot++;
- }
-
- return pot;
-}
-
Review Comment:
In `mu0` you have a bit_floor and bit_ceil implementation matching c++
definition. `__mu0_bit_floor_u8__` is performed on `unsigned long long`.
Meanwhile size of `uintptr_t` must be pounded defined i.e arch32 vs arch64.
https://github.com/mu578/mu0
https://en.cppreference.com/w/cpp/numeric/bit_floor
https://en.cppreference.com/w/cpp/numeric/bit_ceil
https://en.cppreference.com/w/cpp/numeric/bit_width
https://en.cppreference.com/w/cpp/numeric/countl_zero
example:
```c
__mu0_static_inline__
const ___mu0_uint8_t___ __mu0_bit_floor_u8__(const ___mu0_uint8_t___ __x)
{
# if MU0_HAVE_C99 || MU0_HAVE_CPP11
const ___mu0_uint8_t___ one = 1ULL;
const ___mu0_uint8_t___ zero = 0ULL;
# else
const ___mu0_uint8_t___ one = 1UL;
const ___mu0_uint8_t___ zero = 0UL;
# endif
return __x != zero ? (one << (__mu0_bit_width_u8__(__x) - one)) : zero;
}
```
edit, if there is no counting leading zero (i.e CLZ instruction) available
on one target here a fair soft fallback implementation (you may ignore my
MU0_HAVE_C99 || MU0_HAVE_CPP11, there are just here to trigger errors in case
of long long is not supported or is an language extension, e.g mainly CPP98-03
or prior C99 try to use):
```c
__mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u8__(const
___mu0_uint8_t___ __x);
__mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_ux__(const
___mu0_uintx_t___ __x);
__mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u4__(const
___mu0_uint4_t___ __x);
__mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u2__(const
___mu0_uint2_t___ __x);
__mu0_static_inline__ const ___mu0_sint4_t___ __mu0_cntlz_u1__(const
___mu0_uint1_t___ __x);
__mu0_static_inline__
const ___mu0_sint4_t___ __mu0_cntlz_u8__(const ___mu0_uint8_t___ __x)
{
const ___mu0_sint4_t___ d = __mu0_bit_digits_u4__();
# if MU0_HAVE_C99 || MU0_HAVE_CPP11
return ((__x & 0xFFFFFFFF00000000ULL)
? __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___,
(__x >> d)))
: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFULL)
);
# else
return ((__x & 0xFFFFFFFF00000000UL)
? __mu0_cntlz_u4__(__mu0_const_cast__(___mu0_uint4_t___,
(__x >> d)))
: d + __mu0_cntlz_u4__(__x & 0xFFFFFFFFUL)
);
# endif
}
# if MU0_HAVE_LP64
__mu0_static_inline__
const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
{ return __mu0_cntlz_u8__(__x); }
# else
__mu0_static_inline__
const ___mu0_sint4_t___ __mu0_cntlz_ux__(const ___mu0_uintx_t___ __x)
{ return __mu0_cntlz_u4__(__x); }
# endif
__mu0_static_inline__
const ___mu0_sint4_t___ __mu0_cntlz_u4__(const ___mu0_uint4_t___ __x)
{
___mu0_uint1_t___ d = 0;
___mu0_uint4_t___ x = __x;
if (x) {
if (x & 0xFFFF0000U) { x = x >> 16U; } else { d += 16; }
return d + __mu0_cntlz_u2__ (x);
}
return __mu0_bit_digits_u4__();
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]