Skip to contentSkip to conversation

byteorder: add store_{endian} helpers

PATCH New Harvey Harrison 1 participant 1 message Jun 24, 2008

lore
Share & tools
lore
#0 lore raw

PATCH 6/6 byteorder: add store_{endian} helpers

Add helpers for the idiom:
*(__le16 *)ptr = cpu_to_le16(val);

Can now be written as:
store_le16(ptr, val);

Implemented as macros to allow val to be byteswapped at compile-time when it is a constant.

---
 include/linux/byteorder.h |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
include/linux/byteorder.h +11 -0
index b4713ce..20abbf8 100644
@@ -278,6 +278,17 @@ static inline __be64 __cpu_to_be64p(const __u64 *p)
 # define htons(x) ___htons(x)
 # define ntohs(x) ___ntohs(x)
 
+/*
+ * Defined as macros to allow constant folding of the cpu_to_XXXX when
+ * possible.
+ */
+#define store_le16(ptr, val)	(*(__le16 *)(ptr) = cpu_to_le16((u16)(val));)
+#define store_le32(ptr, val)	(*(__le32 *)(ptr) = cpu_to_le32((u32)(val));)
+#define store_le64(ptr, val)	(*(__le64 *)(ptr) = cpu_to_le64((u64)(val));)
+#define store_be16(ptr, val)	(*(__be16 *)(ptr) = cpu_to_be16((u16)(val));)
+#define store_be32(ptr, val)	(*(__be32 *)(ptr) = cpu_to_be32((u32)(val));)
+#define store_be64(ptr, val)	(*(__be64 *)(ptr) = cpu_to_be64((u64)(val));)
+
 static inline void le16_add_cpu(__le16 *var, u16 val)
 {
 	*var = cpu_to_le16(le16_to_cpup(var) + val);
Signature · 1 line
1.5.6.290.gc4e15