arch: implement brute force find_lsb_set()

On RISC-V 64-bit, GCC complains about undefined reference
to 'ffs' via __builtin_ffs(). So implement a brute force
way to do it. Once the toolchain has __builtin_ffs(),
this can be reverted.

Signed-off-by: Daniel Leung <daniel.leung@intel.com>
This commit is contained in:
Daniel Leung 2021-04-23 20:52:38 -07:00 committed by Anas Nashif
commit 783b20712e
2 changed files with 31 additions and 0 deletions

View file

@ -955,3 +955,9 @@ config BOARD
The Board is the first location where we search for a linker.ld file,
if not found we look for the linker file in
soc/<arch>/<family>/<series>
config TOOLCHAIN_HAS_BUILTIN_FFS
bool
default y if !(64BIT && RISCV)
help
Hidden option to signal that toolchain has __builtin_ffs*().

View file

@ -52,7 +52,32 @@ static ALWAYS_INLINE unsigned int find_msb_set(uint32_t op)
static ALWAYS_INLINE unsigned int find_lsb_set(uint32_t op)
{
#ifdef CONFIG_TOOLCHAIN_HAS_BUILTIN_FFS
return __builtin_ffs(op);
#else
/*
* Toolchain does not have __builtin_ffs().
* Need to do this manually.
*/
int bit;
if (op == 0) {
return 0;
}
for (bit = 0; bit < 32; bit++) {
if ((op & (1 << bit)) != 0) {
return (bit + 1);
}
}
/*
* This should never happen but we need to keep
* compiler happy.
*/
return 0;
#endif /* CONFIG_TOOLCHAIN_HAS_BUILTIN_FFS */
}
#ifdef __cplusplus