Skip to contentSkip to conversation

include: types: Use __builtin_offsetof when supported

PATCH New Alex Richardson 4 participants 5 messages Jul 12, 2021 – Jul 17, 2021

lore
Share & tools
lore
#0 lore raw

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
index 38e3565..7fb1af7 100644
@@ -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
#1 lore raw

PATCH 1/1 include: types: Use __builtin_offsetof when supported

Quoted 19 lines · Alex Richardson

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 +2 -0
index 38e3565..7fb1af7 100644
@@ -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

#2 lore raw

PATCH 1/1 include: types: Use __builtin_offsetof when supported

Quoted 5 lines · Jessica Clarke

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

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."
#3 lore raw

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_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

No, it should use <stddef.h>.

OpenSBI is deliberately entirely standalone, unfortunately in this case.

Jess

Applied
#4 lore raw

PATCH 1/1 include: types: Use __builtin_offsetof when supported

In reply to Alex Richardson
Quoted 5 lines · Alex Richardson

Clang provides a __builtin_offsetof which can be detected using __has_builtin().

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
index 38e3565..7fb1af7 100644
@@ -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