Add support for strnlen.

This commit is contained in:
matt 2013-01-23 06:59:55 +00:00
parent f2500cb86e
commit 6495fd5f51
2 changed files with 56 additions and 5 deletions

View File

@ -29,7 +29,7 @@
#include <machine/asm.h>
RCSID("$NetBSD: strlen_arm.S,v 1.2 2013/01/09 00:01:07 matt Exp $")
RCSID("$NetBSD: strlen_arm.S,v 1.3 2013/01/23 06:59:55 matt Exp $")
#ifdef __ARMEL__
#define BYTE0 0x000000ff
@ -41,18 +41,36 @@ RCSID("$NetBSD: strlen_arm.S,v 1.2 2013/01/09 00:01:07 matt Exp $")
#define BYTE1 0x00ff0000
#define BYTE2 0x0000ff00
#define BYTE3 0x000000ff
#endif
#ifdef STRNLEN
#define FUNCNAME strnlen
#else
#define FUNCNAME strlen
#endif
.text
ENTRY(strlen)
ENTRY(FUNCNAME)
#ifdef STRNLEN
push {r4,r5} /* save some registers */
add r5, r0, r1 /* get ptr to end of string */
mov r4, r1 /* save maxlen */
#endif
add ip, r0, #4 /* for the final post-inc */
1: tst r0, #3 /* test for word alignment */
beq .Lpre_main_loop /* finally word aligned */
#ifdef STRNLEN
cmp r0, r5 /* have we gone too far? */
beq .Lmaxed_out /* yes, return maxlen */
#endif
ldrb r3, [r0], #1 /* load a byte */
teq r3, #0 /* is it 0? */
bne 1b /* no, try next byte */
sub ip, ip, #3 /* subtract (4 - the NUL) */
sub r0, r0, ip /* subtract start */
#ifdef STRNLEN
pop {r4, r5} /* restore registers */
#endif
RET /* return */
.Lpre_main_loop:
#if defined(_ARM_ARCH_7)
@ -64,6 +82,10 @@ ENTRY(strlen)
orr r1, r1, r1, lsl #16 /* move to next halfword */
#endif /* _ARM_ARCH_6 */
.Lmain_loop:
#ifdef STRNLEN
cmp r0, r5 /* gone too far? */
bge .Lmaxed_out /* yes, return maxlen */
#endif
ldr r3, [r0], #4 /* load next word */
#if defined(_ARM_ARCH_6)
/*
@ -116,5 +138,17 @@ ENTRY(strlen)
* the post-inc.
*/
sub r0, r0, ip /* subtract start to get length */
RET
END(strlen)
#ifdef STRNLEN
cmp r0, r4 /* is it larger than maxlen? */
movgt r0, r4 /* yes, return maxlen */
pop {r4, r5} /* restore registers */
#endif
RET /* return */
#ifdef STRNLEN
.Lmaxed_out:
mov r0, r4 /* return maxlen */
pop {r4, r5} /* restore registers */
RET /* return */
#endif
END(FUNCNAME)

View File

@ -28,8 +28,24 @@
*/
#include <machine/asm.h>
RCSID("$NetBSD: strlen_naive.S,v 1.2 2013/01/08 16:58:59 pgoyette Exp $")
RCSID("$NetBSD: strlen_naive.S,v 1.3 2013/01/23 06:59:55 matt Exp $")
#ifdef STRNLEN
/* LINTSTUB: size_t strnlen(const char *, size_t) */
ENTRY(strnlen)
add ip, r0, #1 /* start of src + NUL */
add r2, r0, r1 /* &src[maxlen] */
1: cmp r0, r2 /* is this of string? */
beq 2f /* yes it is */
ldrb r3, [r0], #1 /* read a byte */
teq r3, #0 /* is it a NUL? */
bne 1b /* no, get next byte */
sub r0, r0, ip /* get difference between start and end */
RET /* return */
2: mov r0, r1 /* get maxlen */
RET /* return */
END(strnlen)
#else /* STRNLEN */
/* LINTSTUB: size_t strlen(const char *) */
ENTRY(strlen)
add ip, r0, #1 /* start of src + NUL */
@ -39,3 +55,4 @@ ENTRY(strlen)
sub r0, r0, ip /* return difference between start and end */
RET
END(strlen)
#endif /* !STRNLEN */