Skip to contentSkip to conversation

byteorder: add copy_{endian} helpers

PATCH Superseded Harvey Harrison 3 participants 7 messages Jun 2, 2008 – Jun 4, 2008 Generic include/asm header files

lore
Share & tools
lore
#0 lore raw

PATCH v4 6/6 byteorder: add copy_{endian} helpers

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

Can now be written as:
copy_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..8d3c847 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 copy_le16(ptr, val)	(*(__le16 *)(ptr) = cpu_to_le16((u16)(val));)
+#define copy_le32(ptr, val)	(*(__le32 *)(ptr) = cpu_to_le32((u32)(val));)
+#define copy_le64(ptr, val)	(*(__le64 *)(ptr) = cpu_to_le64((u64)(val));)
+#define copy_be16(ptr, val)	(*(__be16 *)(ptr) = cpu_to_be16((u16)(val));)
+#define copy_be32(ptr, val)	(*(__be32 *)(ptr) = cpu_to_be32((u32)(val));)
+#define copy_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.rc0.277.g804cf
#1 lore raw
Quoted 5 lines · Harvey Harrison

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

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

Copy a little endian 16-bit value?

It doesn't just copy. It does a byteswap during the copy.

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#2 lore raw
Quoted 11 lines · Geert Uytterhoeven
Quoted 5 lines · Harvey Harrison

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

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

Copy a little endian 16-bit value?

It doesn't just copy. It does a byteswap during the copy.

True, I had originally called this put_le16. akpm suggested copy_le16. What do people think of copy_as_le16?

Harvey

#3 lore raw
Quoted 8 lines · Harvey Harrison
Quoted 7 lines · Geert Uytterhoeven
Quoted 5 lines · Harvey Harrison

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

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

Quoted 6 lines
Quoted 3 lines

Copy a little endian 16-bit value?

It doesn't just copy. It does a byteswap during the copy.

True, I had originally called this put_le16. akpm suggested copy_le16. What do people think of copy_as_le16?

Sounds still a bit strange to me...

As get' and put' have connotations of reference counts, what about load' and store', e.g. load_le32()' and store_be16()'?

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#4 lore raw
Quoted 8 lines · Geert Uytterhoeven
Quoted 2 lines · Harvey Harrison
Quoted 1 line · Geert Uytterhoeven

On Mon, 2 Jun 2008, Harvey Harrison wrote:

Sounds still a bit strange to me...

As get' and put' have connotations of reference counts, what about load' and store', e.g. load_le32()' and store_be16()'?

Well, load is covered by le16_to_cpup and friends.

I could live with store_le16 though. I had originally added get_le16, put_le16 to match up with the get_unaligned_le16, put_unaligned_le16 functions. Maybe these would have been better as load_unaligned_* store_unaligned_* and during this change we could fix the argument order to be (ptr, val) allowing a gradual changeover without a flag day.

Andrew? What do you think of load/store?

Harvey

#5 lore raw
Quoted 11 lines · Harvey Harrison
Quoted 8 lines · Geert Uytterhoeven
Quoted 2 lines · Harvey Harrison
Quoted 1 line · Geert Uytterhoeven

On Mon, 2 Jun 2008, Harvey Harrison wrote:

Sounds still a bit strange to me...

As get' and put' have connotations of reference counts, what about load' and store', e.g. load_le32()' and store_be16()'?

Well, load is covered by le16_to_cpup and friends.

What about the logical cpu_to_le16p then?

Signature · 3 lines
Russell King
 Linux kernel    2.6 ARM Linux   - http://www.arm.linux.org.uk/
 maintainer of:
#6 lore raw
Quoted 7 lines · Russell King
Quoted 6 lines · Harvey Harrison
Quoted 5 lines · Geert Uytterhoeven
Quoted 2 lines · Harvey Harrison
Quoted 1 line · Geert Uytterhoeven

On Mon, 2 Jun 2008, Harvey Harrison wrote:

Sounds still a bit strange to me...

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

As get' and put' have connotations of reference counts, what about load' and store', e.g. load_le32()' and store_be16()'?

Well, load is covered by le16_to_cpup and friends.

What about the logical cpu_to_le16p then?

Already exists, but it is not like the proposed store_le16, it just has the types reversed.

u16 le16_to_cpup(__le16 *p)
__le16 cpu_to_le16p(u16 *p)

Currently you see things like:

*(__le16 *)ptr = cpu_to_le16(val);

Hence my idea for a:
store_le16(ptr, val);

Harvey