include: types: Use __builtin_offsetof when supported
PATCH 1/1 include: types: Use __builtin_offsetof when supported
Clang provides a __builtin_offsetof which can be detected using __has_builtin().
--- include/sbi/sbi_types.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
include/sbi/sbi_types.h +7 -1
@@ -68,8 +68,14 @@ typedef unsigned long physical_size_t;#define likely (x) __builtin_expect((x), 1)#define unlikely (x) __builtin_expect((x), 0) +#ifndef __has_builtin +#define __has_builtin (...) 0 +#endif +#undef offsetof -#ifdef __compiler_offsetof +#if __has_builtin (__builtin_offsetof) +#define offsetof (TYPE ,MEMBER ) __builtin_offsetof(TYPE,MEMBER) +#elif defined(__compiler_offsetof)#define offsetof (TYPE ,MEMBER ) __compiler_offsetof(TYPE,MEMBER)#else #define offsetof (TYPE ,MEMBER ) ((size_t) &((TYPE *)0)->MEMBER)
Signature · 1 line
2.32.0
PATCH 1/1 include: types: Use __builtin_offsetof when supported
Quoted 19 lines · Alex Richardson
Clang provides a
__builtin_offsetofwhich can be detected using__has_builtin().Signed-off-by Alex Richardson <[email protected]>---
include/sbi/sbi_types.h| 8 +++++++-1 file changed, 7 insertions(+), 1 deletion(-)include/sbi/sbi_types.h +2
-0@@ -68,8 +68,14 @@ typedef unsigned long physical_size_t;#define likely (x) __builtin_expect((x), 1)#define unlikely (x) __builtin_expect((x), 0) +#ifndef __has_builtin +#define __has_builtin (...) 0
include/sbi/sbi_types.h typedef unsigned long physical_size_t; · line 81
Quoted 10 lines
include/sbi/sbi_types.h +5
-1+#endif +#undef offsetof -#ifdef __compiler_offsetof +#if __has_builtin (__builtin_offsetof) +#define offsetof (TYPE ,MEMBER ) __builtin_offsetof(TYPE,MEMBER) +#elif defined(__compiler_offsetof)#define offsetof (TYPE ,MEMBER ) __compiler_offsetof(TYPE,MEMBER)#else #define offsetof (TYPE ,MEMBER ) ((size_t) &((TYPE *)0)->MEMBER)
The original code was stolen for Linux, where __compiler_offsetof is (unconditionally, it seems) defined to __builtin_offsetof. Either __compiler_offsetof should be defined to __builtin_offsetof or it should be deleted, but having both still exist is wrong. Personally I?m of the view OpenSBI should just define offsetof to __builtin_offsetof directly, it?s already using other GNU C builtins above unconditionally and __builtin_offsetof has been implemented in both GCC and Clang for far far longer than RISC-V has existed.
Jess
PATCH 1/1 include: types: Use __builtin_offsetof when supported
Quoted 5 lines · Jessica Clarke
The original code was stolen for Linux, where
__compiler_offsetofis (unconditionally, it seems) defined to__builtin_offsetof. Either__compiler_offsetofshould be defined to__builtin_offsetofor it should be deleted, but having both still exist is wrong. Personally I?m of the view OpenSBI should just define offsetof to__builtin_offsetof
No, it should use <stddef.h>.
Andreas.
Signature · 3 lines
Andreas Schwab, schwab at linux-m68k.org GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1 "And now for something completely different."
PATCH 1/1 include: types: Use __builtin_offsetof when supported
Quoted 10 lines · Andreas Schwab
Quoted 5 lines · Jessica Clarke
The original code was stolen for Linux, where
__compiler_offsetofis (unconditionally, it seems) defined to__builtin_offsetof. Either__compiler_offsetofshould be defined to__builtin_offsetofor it should be deleted, but having both still exist is wrong. Personally I?m of the view OpenSBI should just define offsetof to__builtin_offsetofNo, it should use <
stddef.h>.
OpenSBI is deliberately entirely standalone, unfortunately in this case.
Jess
Anup Patel
AppliedPATCH 1/1 include: types: Use __builtin_offsetof when supported
In reply to Alex RichardsonQuoted 5 lines · Alex Richardson
Clang provides a
__builtin_offsetofwhich can be detected using__has_builtin().Signed-off-by Alex Richardson <[email protected]>
Looks good to me.
Applied this patch to the riscv/opensbi repo.
Thanks,
Anup
Quoted 32 lines
--- include/sbi/sbi_types.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)include/sbi/sbi_types.h +7
-2@@ -68,8 +68,14 @@ typedef unsigned long physical_size_t;#define likely (x) __builtin_expect((x), 1)#define unlikely (x) __builtin_expect((x), 0) +#ifndef __has_builtin +#define __has_builtin (...) 0 +#endif +#undef offsetof -#ifdef __compiler_offsetof +#if __has_builtin (__builtin_offsetof) +#define offsetof (TYPE ,MEMBER ) __builtin_offsetof(TYPE,MEMBER) +#elif defined(__compiler_offsetof)#define offsetof (TYPE ,MEMBER ) __compiler_offsetof(TYPE,MEMBER)#else #define offsetof (TYPE ,MEMBER ) ((size_t) &((TYPE *)0)->MEMBER) --2.32.0
--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi