Skip to contentSkip to conversation

ARM: byteorder: add optimized swab16 and swab32

RFC PATCH New Dirk Behme 3 participants 4 messages May 10, 2013 – May 11, 2013 Arm (32-bit)

lore
Share & tools
lore

Dirk Behme

author
#0 lore raw

RFC PATCH 1/1 ARM: byteorder: add optimized swab16 and swab32

Use the specialized ARM instructions for swapping 16 and 32 bit values instead of using the generic ones from include/linux/byteorder/swab.h.

The x86 version in arch/x86/include/asm/byteorder.h was taken as an example for this.

E.g. for the mx6qsabrelite target this results in ~4k less code.

---

This patch is marked as RFC due to two questions:

1) These ARM instructions are available in ARMv6 and above. Do we have to limit this code somehow to >= ARMv6? I couldn't find any #define to do this in U-Boot (?)

2) I'm not sure about the rev16. The rev16 instruction swaps *both* half words, the lower _and_ the upper one:

REV16 Rd, Rm => Rd[15:8] := Rm[7:0], Rd[7:0] := Rm[15:8], Rd[31:24] := Rm[23:16], Rd[23:16] := Rm[31:24]

I'm not sure if we want this? Most probably we only want to swap the lower half word with swap16()?

 arch/arm/include/asm/byteorder.h |   17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/arm/include/asm/byteorder.h +17 -0
index c3489f1..f21baf0 100644
@@ -15,6 +15,23 @@
 #ifndef __ASM_ARM_BYTEORDER_H
 #define __ASM_ARM_BYTEORDER_H
 
+static __inline__ __u32 ___arch__swab32(__u32 x)
+{
+	__asm__("rev %0,%1"		/* swap bytes */ \
+		: "=r" (x) \
+		:  "0" (x)); \
+		return x;
+}
+#define __arch__swab32(x) ___arch__swab32(x)
+
+static __inline__ __u16 ___arch__swab16(__u16 x)
+{
+	__asm__("rev16 %0,%1"		/* swap bytes */ \
+		: "=r" (x) \
+		:  "0" (x)); \
+		return x;
+}
+#define __arch__swab16(x) ___arch__swab16(x)
 
 #include <asm/types.h>
 
Signature · 1 line
1.7.10.4
#1 lore raw

RFC PATCH 1/1 ARM: byteorder: add optimized swab16 and swab32

Quoted 7 lines · Dirk Behme

Use the specialized ARM instructions for swapping 16 and 32 bit values instead of using the generic ones from include/linux/byteorder/swab.h.

The x86 version in arch/x86/include/asm/byteorder.h was taken as an example for this.

E.g. for the mx6qsabrelite target this results in ~4k less code.

Which compiler are you using? GCC 4.5 and later recognises the byteswap pattern and emits these instructions automatically. This gives better code than the inline asm since it allows the compiler to do proper instruction scheduling, and in the 16-bit case it avoids extraneous masking.

Signature · 2 lines
M?ns Rullg?rd
mans at mansr.com
author
#2 lore raw

RFC PATCH 1/1 ARM: byteorder: add optimized swab16 and swab32

Am 10.05.2013 18:56, schrieb M?ns Rullg?rd:

Quoted 11 lines
Quoted 7 lines · Dirk Behme

Use the specialized ARM instructions for swapping 16 and 32 bit values instead of using the generic ones from include/linux/byteorder/swab.h.

The x86 version in arch/x86/include/asm/byteorder.h was taken as an example for this.

E.g. for the mx6qsabrelite target this results in ~4k less code.

Which compiler are you using?

An older gcc 4.4.4 version.

Quoted 5 lines

GCC 4.5 and later recognises the byteswap
pattern and emits these instructions automatically. This gives better code than the inline asm since it allows the compiler to do proper instruction scheduling, and in the 16-bit case it avoids extraneous masking.

Thanks,

Dirk

#3 lore raw

RFC PATCH 1/1 ARM: byteorder: add optimized swab16 and swab32

Dear Dirk Behme,

Quoted 4 lines · In message <[email protected]> you wrote:
Quoted 2 lines

Which compiler are you using?

An older gcc 4.4.4 version.

We should not really care about these. And if so, then onlya fter testing we are really suing such old stuff, i. e. those who are using recent tools (eventually the majority of users) should not suffer from hacst to optimize for ancient code.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Only two things are infinite,  the universe and human stupidity,  and
I'm not sure about the former.                     -- Albert Einstein