Skip to content

Commit 2a6730e

Browse files
SEmmmerimkiva
andcommitted
fix GCC __builtin_ffs
Co-authored-by: imkiva <[email protected]> Co-authored-by: SEmmmer <[email protected]>
1 parent 0d9ec87 commit 2a6730e

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,45 @@ 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+
// this function is copied from linux kernel source
108+
static inline int ffs(int x)
109+
{
110+
int r = 1;
111+
112+
if (!x)
113+
return 0;
114+
if (!(x & 0xffff)) {
115+
x >>= 16;
116+
r += 16;
117+
}
118+
if (!(x & 0xff)) {
119+
x >>= 8;
120+
r += 8;
121+
}
122+
if (!(x & 0xf)) {
123+
x >>= 4;
124+
r += 4;
125+
}
126+
if (!(x & 3)) {
127+
x >>= 2;
128+
r += 2;
129+
}
130+
if (!(x & 1)) {
131+
x >>= 1;
132+
r += 1;
133+
}
134+
return r;
135+
}
136+
#endif
137+
99138
/*!
100139
* DRF MACRO README:
101140
*

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)