compiler: support get_bits/replace_bits,sizeof_field,offsetofend,struct_group etc.
authorChristian Lamparter <chunkeey@gmail.com>
Sun, 21 May 2023 20:48:29 +0000 (22:48 +0200)
committerChristian Lamparter <chunkeey@gmail.com>
Sun, 21 May 2023 21:03:20 +0000 (23:03 +0200)
Taken from linux. Removed be* support.

Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
include/linux/compiler.h

index e2656ae2cfc198270c956a8cdccdf1fb4139660b..e3390d2682395eaeb11be84ecd40bb7e46aafe64 100644 (file)
 #define BIT(b)                 (1 << (b))
 #define MASK(w)                        (BIT(w) - 1)
 
+/**
+ * __struct_group() - Create a mirrored named and anonyomous struct
+ *
+ * @TAG: The tag name for the named sub-struct (usually empty)
+ * @NAME: The identifier name of the mirrored sub-struct
+ * @ATTRS: Any struct attributes (usually empty)
+ * @MEMBERS: The member declarations for the mirrored structs
+ *
+ * Used to create an anonymous union of two structs with identical layout
+ * and size: one anonymous and one named. The former's members can be used
+ * normally without sub-struct naming, and the latter can be used to
+ * reason about the start, end, and size of the group of struct members.
+ * The named struct can also be explicitly tagged for layer reuse, as well
+ * as both having struct attributes appended.
+ */
+#define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \
+       union { \
+               struct { MEMBERS } ATTRS; \
+               struct TAG { MEMBERS } ATTRS NAME; \
+       }
+
+/**
+ * struct_group() - Wrap a set of declarations in a mirrored struct
+ *
+ * @NAME: The identifier name of the mirrored sub-struct
+ * @MEMBERS: The member declarations for the mirrored structs
+ *
+ * Used to create an anonymous union of two structs with identical
+ * layout and size: one anonymous and one named. The former can be
+ * used normally without sub-struct naming, and the latter can be
+ * used to reason about the start, end, and size of the group of
+ * struct members.
+ */
+#define struct_group(NAME, MEMBERS...)  \
+       __struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS)
+
 #undef offsetof
 #ifdef __compiler_offsetof
 # define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
 # define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
 #endif
 
+/**
+ * sizeof_field() - Report the size of a struct field in bytes
+ *
+ * @TYPE: The structure containing the field of interest
+ * @MEMBER: The field to return the size of
+ */
+#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER))
+
+/**
+ * offsetofend() - Report the offset of a struct field within the struct
+ *
+ * @TYPE: The type of the structure
+ * @MEMBER: The member within the structure to get the end offset of
+ */
+#define offsetofend(TYPE, MEMBER) \
+       (offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER))
+
 #define likely(x)      __builtin_expect(!!(x), 1)
 #define unlikely(x)    __builtin_expect(!!(x), 0)
 
@@ -142,4 +195,55 @@ static inline unsigned int hweight16(unsigned int w)
                 (typeof(_mask))((_mask) >> __bf_shf(_mask));            \
         })
 
+#if __has_attribute(__error__)
+# define __compiletime_error(msg)       __attribute__((__error__(msg)))
+#else
+# define __compiletime_error(msg)
+#endif
+
+extern void __compiletime_error("value doesn't fit into mask")
+__field_overflow(void);
+extern void __compiletime_error("bad bitfield mask")
+__bad_mask(void);
+static inline u64 field_multiplier(u32 field)
+{
+       if ((field | (field - 1)) & ((field | (field - 1)) + 1))
+               __bad_mask();
+       return field & -field;
+}
+static inline u64 field_mask(const u32 field)
+{
+       return field / field_multiplier(field);
+}
+#define field_max(field)       ((typeof(field))field_mask(field))
+#define ____MAKE_OP(type,base,to,from)                                 \
+static inline __##type type##_encode_bits(base v, base field)  \
+{                                                                      \
+       if (__builtin_constant_p(v) && (v & ~field_mask(field)))        \
+               __field_overflow();                                     \
+       return to((v & field_mask(field)) * field_multiplier(field));   \
+}                                                                      \
+static inline __##type type##_replace_bits(__##type old,       \
+                                       base val, base field)           \
+{                                                                      \
+       return (old & ~to(field)) | type##_encode_bits(val, field);     \
+}                                                                      \
+static inline void type##p_replace_bits(__##type *p,           \
+                                       base val, base field)           \
+{                                                                      \
+       *p = (*p & ~to(field)) | type##_encode_bits(val, field);        \
+}                                                                      \
+static inline base type##_get_bits(__##type v, base field)     \
+{                                                                      \
+       return (from(v) & field)/field_multiplier(field);               \
+}
+#define __MAKE_OP(size)                                                        \
+       ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \
+       ____MAKE_OP(u##size,u##size,,)
+____MAKE_OP(u8,u8,,)
+__MAKE_OP(16)
+__MAKE_OP(32)
+#undef __MAKE_OP
+#undef ____MAKE_OP
+
 #endif /* __SHARED_COMPILER_H */