Skip to content

Commit 20aacbf

Browse files
SEmmmerimkiva
andcommitted
fix GCC __builtin_ffs
Co-authored-by: imkiva <[email protected]> Co-authored-by: SEmmmer <[email protected]>
1 parent c9c4080 commit 20aacbf

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/common/sdk/nvidia/inc/nvmisc.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,32 @@ extern "C" {
9696
((((n) & 0xCCCCCCCCCCCCCCCCULL) != 0U) ? 0x02U: 0U) | \
9797
((((n) & 0xAAAAAAAAAAAAAAAAULL) != 0U) ? 0x01U: 0U) )
9898

99+
#if defined(NVCPU_RISCV64) && __riscv_xlen == 64
100+
// On RISC-V 64-bit platform, GCC would expand __builtin_ffs() to ffs()
101+
// see: https://github.com/riscv-collab/riscv-newlib/blob/master/newlib/libc/machine/riscv/ffs.c
102+
// see: https://sourceware.org/pipermail/newlib/2017/014958.html
103+
104+
// in case GCC changes the behavior, we force the expansion
105+
#define __builtin_ffs(x) ffs(x)
106+
107+
static inline int ffs(unsigned int x)
108+
{
109+
unsigned int y;
110+
111+
// __builtin_ffs() is undefined at this point
112+
if (x == 0) return 0;
113+
114+
// count trailing zeros.
115+
// Copied and adapted from util_generic.h because I cannot
116+
// include that file directly. This should be refactored out.
117+
for (y = 0; !(x & 0x80000000); y++)
118+
x <<= 1;
119+
120+
// ffs = ctz + 1
121+
return y + 1;
122+
}
123+
#endif
124+
99125
/*!
100126
* DRF MACRO README:
101127
*

src/nvidia/inc/libraries/utils/nvprintf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ extern "C" {
6565
#define NV_PRINTF_LEVEL_ENABLED(level) ((level) >= NV_PRINTF_LEVEL)
6666
#endif
6767

68-
#if defined(GSP_PLUGIN_BUILD) || (defined(NVRM) && NVCPU_IS_RISCV64)
68+
#if defined(GSP_PLUGIN_BUILD) || (defined(NVRM) && NVCPU_IS_RISCV64 && !NVOS_IS_LINUX)
6969

7070
/**
7171
* GSPRM uses a different system for logging.

0 commit comments

Comments
 (0)