strncmp issue

Mark Tinguely tinguely at casselton.net
Mon May 4 13:01:16 UTC 2009


Hi, the propose code by both of you two:

/* if (len == 0) return 0 */
	cmp	r2, #0
	moveq	r0, #0
	moveq	pc, lr

/* ip == last src address to compare */
	add	ip, r0, r2
1:
	ldrb	r2, [r0], #1
	ldrb	r3, [r1], #1
	cmp	ip, r0
	cmp	r2, #1
	cmpcs	r2, r3
	beq	1b
	sub	r0, r2, r3

That was one of my failed attempts. I to was hoping to not add the branch
to cut down in cycles. A person has to test every possible call to strncmp.
This will fail on a positive string length less than strlen length of the
input strings:

strncmp("abcdefg", "abcdefh", 6)

Will return (-1) str1 < str2 at 6 characters which is wrong.


/* if (len == 0) return 0 */
	cmp	r2, #0
	moveq	r0, #0
	moveq	pc, lr

/* ip == last src address to compare */
	add	ip, r0, r2
1:
	ldrb	r2, [r0], #1
	ldrb	r3, [r1], #1
	cmp	ip, r0
	beq	2f		<- stops in the case where strlen(s1) > len
	cmp	r2, #1		<- stops thea NULL case, we can't just change
				   the comparison because below we loop on
				   an equality and can end up in a big loop
				
	cmpcs	r2, r3		<- compare characters.
	beq	1b
2:
	sub	r0, r2, r3



More information about the freebsd-arm mailing list