Page MenuHomeFreeBSD
Paste P158

(An Untitled Masterwork)
ActivePublic

Authored by cem on Jan 10 2018, 1:04 AM.
--- sys/contrib/zstd/lib/freebsd/zstd_kfreebsd.h (revision 327712)
+++ sys/contrib/zstd/lib/freebsd/zstd_kfreebsd.h (working copy)
@@ -49,6 +49,44 @@
#define calloc(a,b) (malloc)((a)*(b), M_ZSTD, M_WAITOK | M_ZERO)
#endif
+#undef __builtin_ctz
+#undef __builtin_ctzll
+#undef __builtin_clz
+#undef __builtin_clzll
+#undef __builtin_bswap32
+#undef __builtin_bswap64
+
+#define __builtin_bswap32 bswap32
+#define __builtin_bswap64 bswap64
+
+static inline int
+__builtin_ctz(int x)
+{
+ if (x == 0)
+ return (sizeof(x) * NBBY);
+ return (ffs(x) - 1);
+}
+
+static inline long long
+__builtin_ctzll(long long x)
+{
+ if (x == 0)
+ return (sizeof(x) * NBBY);
+ return (ffsll(x) - 1);
+}
+
+static inline int
+__builtin_clz(int x)
+{
+ return (sizeof(x) * NBBY - fls(x));
+}
+
+static inline long long
+__builtin_clzll(long long x)
+{
+ return (sizeof(x) * NBBY - flsll(x));
+}
+
#ifdef __cplusplus
}
#endif

Event Timeline

cem created this paste.Jan 10 2018, 1:04 AM
cem created this object in space S1 Global.

A comment explaining why you're doing all this would be good.

cem added a comment.Jan 10 2018, 1:22 AM

Thanks Ravi. How is this?

The kernel as a standalone target does not link libgcc or libcompiler-rt. On platforms (e.g., MIPS and RISCV) that do not have a direct assembly implementation of the relevant builtin functions that zstd references, the compiler converts them into calls to the runtime library intrinsics.

The goal of all of this macro redefinition is to use supported kernel constructs to implement the same functionality, without adding diff to contrib code.

And then for the commit message only:

A subsequent enhancement might create a mini compiler-rt library for kernel use and implement e.g., __ctzsi2 (for use by __builtin_ctz) there instead.

The kernel as a standalone target does not link against libgcc or libcompiler-rt. On platforms (e.g., MIPS and RISCV) that do not have a direct assembly implementation of the relevant builtin functions that zstd references, the compiler converts them into calls to the runtime library intrinsics. Since the kernel doesn't link against the libraries, this results in a failure to link the kernel.

The goal of all of this macro redefinition is to use supported kernel constructs to implement the same functionality, without adding a diff to contrib code.

A one-sentence description of what each of the functions does would be good too (i.e. expand ctz => Count Trailing Zeros, etc).

cem added a comment.Jan 10 2018, 1:53 AM

Thanks -- incorporated.