Skip to contentSkip to conversation

csky: Fix ABIv2 memcmp() s1 alignment check

PATCH In review Hanlin Song 2 participants 4 messages Jun 23 – Jul 7 C-sky architecture

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 csky: Fix ABIv2 memcmp() s1 alignment check

memcmp() saves the original s1 pointer in r3 and then clears r0 for the return value. The following alignment check tests r0 instead of r3:

mov r3, r0
movi r0, 0
...
andi r13, r0, 3

As a result, the path for an unaligned s1 address is never taken, and memcmp() may issue word loads from an unaligned s1 address.

Test r3 instead, which still holds the original s1 pointer.

---
 arch/csky/abiv2/memcmp.S | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/csky/abiv2/memcmp.S +1 -1
index bf0d809f0..1c6ced74d 100644
@@ -12,7 +12,7 @@ ENTRY(memcmp)
 	cmplti	r2, 4
 	bt	.L_compare_by_byte
 
-	andi	r13, r0, 3
+	andi	r13, r3, 3
 	movi	r19, 4
 
 	/* Test if s1 is not 4 bytes aligned.  */
Signature · 1 line
2.25.1
#1 lore raw

Hi Guo Ren and C-SKY maintainers,

Gentle ping on this patch.

My understanding is that r3 holds the original s1 pointer after r0 is cleared, so the alignment check should use r3 instead of r0. I also tested this on a CK860f chip, and the patched version was faster in my unaligned memcmp test.

I may be missing some C-SKY ABIv2 detail, so any feedback would be appreciated.

Thanks,
Hanlin

maintainer
#2 lore raw

Hi Song,

Quoted 5 lines · Hanlin Song

Hi Guo Ren and C-SKY maintainers,

Gentle ping on this patch.

Quoted 8 lines

My understanding is that r3 holds the original s1 pointer after r0 is cleared, so the alignment check should use r3 instead of r0. I also tested this on a CK860f chip, and the patched version was faster in my unaligned memcmp test.

I may be missing some C-SKY ABIv2 detail, so any feedback would be appreciated.

Thanks for the patch, but this code is intentional. The CK810 and CK860 support misaligned load/store in their microarchitecture, so the alignment-check path is effectively dead code that exists only to keep this asm code. It has been carried over unchanged from the very first.

Signature · 2 lines
Best Regards
 Guo Ren
#3 lore raw

Hi Guo Ren,

Thanks for the clarification.

Hanlin