Home | History | Annotate | Line # | Download | only in string
strcpy_arm.S revision 1.5
      1  1.1      matt /*-
      2  1.1      matt  * Copyright (c) 2013 The NetBSD Foundation, Inc.
      3  1.1      matt  * All rights reserved.
      4  1.1      matt  *
      5  1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1      matt  * by Matt Thomas of 3am Software Foundry.
      7  1.1      matt  *
      8  1.1      matt  * Redistribution and use in source and binary forms, with or without
      9  1.1      matt  * modification, are permitted provided that the following conditions
     10  1.1      matt  * are met:
     11  1.1      matt  * 1. Redistributions of source code must retain the above copyright
     12  1.1      matt  *    notice, this list of conditions and the following disclaimer.
     13  1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     15  1.1      matt  *    documentation and/or other materials provided with the distribution.
     16  1.1      matt  *
     17  1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1      matt  */
     29  1.1      matt 
     30  1.1      matt #include <machine/asm.h>
     31  1.1      matt 
     32  1.5  christos RCSID("$NetBSD: strcpy_arm.S,v 1.5 2017/01/14 03:00:13 christos Exp $")
     33  1.1      matt 
     34  1.1      matt #ifdef STRLCPY
     35  1.1      matt #ifdef _LIBC
     36  1.1      matt WEAK_ALIAS(strlcpy, _strlcpy)
     37  1.1      matt #endif
     38  1.4  christos #define	FUNCNAME	_strlcpy
     39  1.1      matt #elif defined(STRNCPY)
     40  1.5  christos WEAK_ALIAS(strncpy, _strncpy)
     41  1.4  christos #define	FUNCNAME	_strncpy
     42  1.1      matt #else
     43  1.5  christos WEAK_ALIAS(strcpy, _strcpy)
     44  1.4  christos #define	FUNCNAME	_strcpy
     45  1.1      matt #endif
     46  1.1      matt 
     47  1.1      matt #ifdef __ARMEL__
     48  1.1      matt #define	lslo	lsr		/* shift to lower address */
     49  1.1      matt #define	lshi	lsl		/* shift to higher address */
     50  1.1      matt #define	BYTE0	0x000000ff
     51  1.1      matt #define	BYTE1	0x0000ff00
     52  1.1      matt #define	BYTE2	0x00ff0000
     53  1.1      matt #define	BYTE3	0xff000000
     54  1.1      matt #else
     55  1.1      matt #define	lslo	lsl		/* shift to lower address */
     56  1.1      matt #define	lshi	lsr		/* shift to higher address */
     57  1.1      matt #define	BYTE0	0xff000000
     58  1.1      matt #define	BYTE1	0x00ff0000
     59  1.1      matt #define	BYTE2	0x0000ff00
     60  1.1      matt #define	BYTE3	0x000000ff
     61  1.1      matt #endif
     62  1.1      matt 
     63  1.1      matt /*
     64  1.1      matt  * On armv6 and later, to quickly determine if a word contains a NUL (0) byte,
     65  1.1      matt  * we add 254 to each byte using the UQADD8 (unsigned saturating add 8)
     66  1.1      matt  * instruction.  For every non-NUL byte, the result for that byte will become
     67  1.1      matt  * 255.  For NUL, it will be 254.  When we complement the result of all 4 adds,
     68  1.1      matt  * if the result is non-0 then we must have encountered a NUL.
     69  1.1      matt  *
     70  1.1      matt  * For earlier architecture, we just use tst on all 4 bytes.  There are other
     71  1.1      matt  * algorithms to detect NULs but they take longer and use more instructions.
     72  1.1      matt  */
     73  1.1      matt 
     74  1.1      matt /*
     75  1.1      matt  * char *strcpy(char *dst, const char *src);
     76  1.1      matt  * char *strncpy(char *dst, const char *src, size_t len);
     77  1.1      matt  * size_t strlcpy(char *dst, const char *src, size_t len);
     78  1.1      matt  */
     79  1.1      matt 
     80  1.1      matt 	.text
     81  1.1      matt ENTRY(FUNCNAME)
     82  1.1      matt #if defined(STRLCPY)
     83  1.1      matt 	cmp	r2, #1			/* is length 1 or less? */
     84  1.1      matt 	bhi	1f			/*   no, do normal */
     85  1.1      matt 	moveq	r3, #0			/*   = 1? load NUL */
     86  1.3      matt 	strbeq	r3, [r0]		/*   = 1? write NUL to dst */
     87  1.1      matt 	mov	r0, r1			/* move src to r0 */
     88  1.1      matt 	b	PLT_SYM(_C_LABEL(strlen)) /* and tailcall strlen */
     89  1.1      matt 1:
     90  1.1      matt 	sub	r2, r2, #1		/* leave one byte for NUL */
     91  1.1      matt #endif
     92  1.1      matt #if defined(STRNCPY)
     93  1.1      matt 	cmp	r2, #0			/* 0 length? */
     94  1.1      matt 	RETc(eq)			/*   yes, just return */
     95  1.1      matt #endif
     96  1.1      matt 	push	{r4-r9}			/* save some registers */
     97  1.1      matt #ifdef _ARM_ARCH_6
     98  1.1      matt #ifdef _ARM_ARCH_7
     99  1.1      matt 	movw	r7, #0xfefe		/* magic constant; 254 in each byte */
    100  1.1      matt #else
    101  1.1      matt 	mov	r7, #0xfe		/* put 254 in low byte */
    102  1.1      matt 	orr	r7, r7, r7, lsl #8	/* move to next byte */
    103  1.1      matt #endif
    104  1.1      matt 	orr	r7, r7, r7, lsl #16	/* move to next halfword */
    105  1.1      matt #endif
    106  1.1      matt 
    107  1.1      matt #if defined(STRLCPY)
    108  1.1      matt 	add	r6, r1, #1		/* save for return (deal with NUL) */
    109  1.1      matt #else
    110  1.1      matt 	mov	r6, r0			/* save for return */
    111  1.1      matt #endif
    112  1.1      matt 
    113  1.1      matt .Ldst_align:
    114  1.1      matt 	tst	r0, #3			/* check for dst alignment */
    115  1.1      matt 	beq	.Ldst_aligned		/*   ok, proceed to next check */
    116  1.1      matt 	ldrb	r5, [r1], #1		/* load a byte */
    117  1.1      matt #if defined(STRNCPY)
    118  1.1      matt 	subs	r2, r2, #1		/* subtract out from count */
    119  1.1      matt 	bmi	.Ldst_full		/*   zero? the dst has no more room */
    120  1.1      matt #endif
    121  1.1      matt 	strb	r5, [r0], #1		/* store a byte */
    122  1.1      matt 	teq	r5, #0			/* was it a NUL? */
    123  1.1      matt 	beq	.Lend_of_string		/*   yes, we are done */
    124  1.1      matt #if defined(STRLCPY)
    125  1.1      matt 	subs	r2, r2, #1		/* subtract one from count */
    126  1.3      matt 	strbeq	r2, [r0], #1		/*    zero? write trailing NUL */
    127  1.1      matt 	beq	.Ldst_full		/*    zero? the dst has no more room */
    128  1.1      matt #endif
    129  1.1      matt 	b	.Ldst_align		/* loop around for next byte */
    130  1.1      matt .Ldst_aligned:
    131  1.1      matt 	tst	r1, #3			/* get the misalignment of src */
    132  1.1      matt 	bne	.Lincongruent		/*  !=? incongruent (slower) */
    133  1.1      matt 
    134  1.1      matt 	/*   =?   congruent (faster) */
    135  1.1      matt 
    136  1.1      matt .Lcongruent:
    137  1.1      matt #if defined(STRLCPY)
    138  1.1      matt 	add	r6, r6, #3		/* compensate for word post-inc */
    139  1.1      matt #endif
    140  1.1      matt 	b	.Lcongruent_mainloop_load
    141  1.1      matt .Lcongruent_mainloop:
    142  1.1      matt #if defined(STRLCPY) || defined(STRNCPY)
    143  1.1      matt 	subs	r2, r2, #4		/* subtract 4 from the count */
    144  1.1      matt 	bmi	.Lno_more_room
    145  1.1      matt #endif
    146  1.1      matt 	str	r5, [r0], #4		/* store word into dst */
    147  1.1      matt #if defined(STRLCPY)
    148  1.1      matt 	beq	.Lno_more_room		/*   count is 0? no room in dst */
    149  1.1      matt #endif
    150  1.1      matt #if defined(STRNCPY)
    151  1.1      matt 	beq	.Ldst_full_word_aligned	/*   count is 0? no room in dst */
    152  1.1      matt #endif
    153  1.1      matt .Lcongruent_mainloop_load:
    154  1.1      matt 	ldr	r5, [r1], #4		/* load word from source */
    155  1.1      matt #if defined(_ARM_ARCH_6)
    156  1.1      matt 	uqadd8	r3, r5, r7		/* magic happens here */
    157  1.1      matt 	mvns	r3, r3			/* is the complemented result 0? */
    158  1.1      matt 	beq	.Lcongruent_mainloop	/*    yes, no NULs, do it again */
    159  1.1      matt #else
    160  1.1      matt 	tst	r5, #BYTE0		/* does byte 0 contain a NUL? */
    161  1.1      matt 	tstne	r5, #BYTE1		/*   no, does byte 1 contain a NUL? */
    162  1.1      matt 	tstne	r5, #BYTE2		/*   no, does byte 2 contain a NUL? */
    163  1.1      matt 	tstne	r5, #BYTE3		/*   no, does byte 3 contain a NUL? */
    164  1.1      matt 	bne	.Lcongruent_mainloop	/*    yes, no NULs, do it again */
    165  1.1      matt #endif
    166  1.1      matt #if defined(STRLCPY) && 0
    167  1.1      matt 	sub	r1, r1, #3		/* back up src pointer */
    168  1.1      matt #endif
    169  1.1      matt #if defined(_ARM_ARCH_6)
    170  1.1      matt #ifdef __ARMEL__
    171  1.1      matt 	rev	r3, r3			/* CLZ needs BE data */
    172  1.1      matt #endif
    173  1.1      matt 	clz	r3, r3			/* count leading zeros */
    174  1.1      matt #else
    175  1.1      matt 	mov	r3, #0			/* assume NUL is in byte 0 */
    176  1.1      matt 	tst	r5, #BYTE0		/* is NUL in byte 2? */
    177  1.1      matt 	beq	.Lcongruent_last_bytes	/*   yes, done searching. */
    178  1.1      matt 	mov	r3, #8			/* assume NUL is in byte 1 */
    179  1.1      matt 	tst	r5, #BYTE1		/* is NUL in byte 2? */
    180  1.1      matt 	beq	.Lcongruent_last_bytes	/*   yes, done searching. */
    181  1.1      matt 	mov	r3, #16			/* assume NUL is in byte 2 */
    182  1.1      matt 	tst	r5, #BYTE2		/* is NUL in byte 2? */
    183  1.1      matt #if !defined(STRLCPY)
    184  1.1      matt 	beq	.Lcongruent_last_bytes	/*   yes, done searching. */
    185  1.1      matt 	mov	r3, #24			/* NUL must be in byte 3 */
    186  1.1      matt #else
    187  1.1      matt 	movne	r3, #24			/*    no, then NUL is in byte 3 */
    188  1.1      matt #endif
    189  1.1      matt #endif /* _ARM_ARCH_6 */
    190  1.1      matt #if defined(STRLCPY)
    191  1.1      matt .Lcongruent_last_bytes:
    192  1.1      matt #endif
    193  1.1      matt #if defined(STRLCPY)
    194  1.1      matt 	add	r1, r1, r3, lsr #3	/* position to point at NUL + 4 */
    195  1.1      matt #endif
    196  1.1      matt 	b	.Llast_bytes		/* store the last bytes */
    197  1.1      matt 
    198  1.1      matt 
    199  1.1      matt .Lincongruent:
    200  1.1      matt 	/*
    201  1.1      matt 	 * At this point dst is word aligned by src is not.  Read bytes
    202  1.1      matt 	 * from src until it is read aligned.
    203  1.1      matt 	 */
    204  1.1      matt 	and	r3, r1, #3		/* extract misalignment */
    205  1.1      matt 	mov	r9, r3, lsl #3		/* calculate discard shift */
    206  1.1      matt 	rsb	r8, r9, #32		/* calculate insertion shift */
    207  1.1      matt #if defined(STRLCPY)
    208  1.1      matt 	add	r6, r6, #3		/* compensate for word post-inc */
    209  1.1      matt #endif
    210  1.1      matt 	bic	r1, r1, #3		/* word align src */
    211  1.1      matt 	ldr	r5, [r1], #4		/* load word frm src */
    212  1.1      matt 	mov	r4, r5, lslo r9		/* discard lo bytes from src */
    213  1.1      matt 	tst	r4, #BYTE0		/* does byte 0 contain a NUL? */
    214  1.1      matt #if defined(STRNCPY)
    215  1.1      matt 	beq	.Lend_of_string		/*   yes, zero fill rest of string */
    216  1.1      matt #else
    217  1.1      matt 	moveq	r3, r9			/*   yes, set offset */
    218  1.1      matt 	beq	.Lincongruent_end_of_string /*   yes, deal with the last bytes */
    219  1.1      matt #endif
    220  1.1      matt 	/*
    221  1.1      matt 	 * To make our test for NULs below do not generate false positives,
    222  1.1      matt 	 * fill the bytes in the word we don't want to match with all 1s.
    223  1.1      matt 	 */
    224  1.1      matt 	mvn	r3, #0			/* create a mask */
    225  1.2      matt 	mov	r3, r3, lslo r8		/* zero out bytes being kept */
    226  1.2      matt 	orr	r5, r5, r3		/* merge src and mask */
    227  1.1      matt #ifdef _ARM_ARCH_6
    228  1.2      matt 	uqadd8	r3, r5, r7		/* NUL detection magic happens */
    229  1.1      matt 	mvns	r3, r3			/* is the complemented result 0? */
    230  1.1      matt 	beq	.Lincongruent_mainloop_load /*   yes, no NUL encountered! */
    231  1.1      matt #ifdef __ARMEL__
    232  1.1      matt 	rev	r3, r3			/* CLZ wants BE input */
    233  1.1      matt #endif
    234  1.1      matt 	clz	r3, r3			/* count leading zeros */
    235  1.1      matt #else
    236  1.1      matt 	/*
    237  1.1      matt 	 * We already tested for byte 0 above so we don't need to it again.
    238  1.1      matt 	 */
    239  1.1      matt 	mov	r3, #24			/* assume NUL is in byte 3 */
    240  1.1      matt 	tst	r5, #BYTE1		/* did we find a NUL in byte 1? */
    241  1.1      matt 	subeq	r3, r3, #8		/*   yes, decremnt byte position */
    242  1.1      matt 	tstne	r5, #BYTE2		/*   no, did we find a NUL in byte 2? */
    243  1.1      matt 	subeq	r3, r3, #8		/*   yes, decremnt byte position */
    244  1.1      matt 	tstne	r5, #BYTE3		/*   no, did we find a NUL in byte 3? */
    245  1.1      matt 	bne	.Lincongruent_mainloop_load /*   no, no NUL encountered! */
    246  1.1      matt #endif
    247  1.1      matt 	mov	r5, r4			/* discard already dealt with bytes */
    248  1.1      matt .Lincongruent_end_of_string:
    249  1.1      matt #if defined(STRLCPY)
    250  1.1      matt 	add	r1, r1, r3, lsr #3	/* then add offset to NUL */
    251  1.1      matt #endif
    252  1.1      matt 	sub	r3, r3, r9		/* adjust NUL offset */
    253  1.1      matt 	b	.Llast_bytes		/* NUL encountered! finish up */
    254  1.1      matt 
    255  1.1      matt #if defined(STRLCPY) || defined(STRNCPY)
    256  1.1      matt .Lincongruent_no_more_room:
    257  1.1      matt 	mov	r5, r4			/* move data to be stored to r5 */
    258  1.1      matt 	b	.Lno_more_room		/* fill remaining space */
    259  1.1      matt #endif /* STRLCPY || STRNCPY */
    260  1.1      matt 
    261  1.1      matt 	/*
    262  1.1      matt 	 * At this point both dst and src are word aligned and r4 contains
    263  1.1      matt 	 * partial contents from src.
    264  1.1      matt 	 */
    265  1.1      matt .Lincongruent_mainloop:
    266  1.1      matt 	orr	r4, r4, r5, lshi r8	/* put new src data into dst word */
    267  1.1      matt #if defined(STRLCPY) || defined(STRNCPY)
    268  1.1      matt 	subs	r2, r2, #4		/* subtract 4 from count */
    269  1.1      matt 	bmi	.Lincongruent_no_more_room /*   count < 0? dst will be full */
    270  1.1      matt #endif
    271  1.1      matt 	str	r4, [r0], #4		/* store word in dst */
    272  1.1      matt #if defined(STRLCPY)
    273  1.1      matt 	beq	.Lno_more_room		/*   space left is 0? stop copy */
    274  1.1      matt #endif
    275  1.1      matt #if defined(STRNCPY)
    276  1.1      matt 	beq	.Ldst_full_word_aligned	/*   space left is 0? stop copy */
    277  1.1      matt #endif
    278  1.1      matt 	mov	r4, r5, lslo r9		/* move rest of src into dst word */
    279  1.1      matt .Lincongruent_mainloop_load:
    280  1.1      matt 	ldr	r5, [r1], #4		/* read src */
    281  1.1      matt #ifdef _ARM_ARCH_6
    282  1.1      matt 	uqadd8	r3, r5, r7		/* magic happens here */
    283  1.1      matt 	mvns	r3, r3			/* is the complemented result 0? */
    284  1.1      matt 	beq	.Lincongruent_mainloop	/*   yes, no NUL encountered! */
    285  1.1      matt 	/*
    286  1.1      matt 	 * fall into this since we encountered a NULL.  At this point we have
    287  1.1      matt 	 * from 1-5 bytes (excluding trailing NUL) to write.
    288  1.1      matt 	 */
    289  1.1      matt #ifdef __ARMEL__
    290  1.1      matt 	rev	r3, r3			/* CLZ works on BE data */
    291  1.1      matt #endif
    292  1.1      matt 	clz	r3, r3			/* count leading zeroes */
    293  1.1      matt #else
    294  1.1      matt 	tst	r5, #BYTE0		/* does byte 0 contain a NUL? */
    295  1.1      matt 	tstne	r5, #BYTE1		/*   no, does byte 1 contain a NUL? */
    296  1.1      matt 	tstne	r5, #BYTE2		/*   no, does byte 2 contain a NUL? */
    297  1.1      matt 	tstne	r5, #BYTE3		/*   no, does byte 3 contain a NUL? */
    298  1.1      matt 	bne	.Lincongruent_mainloop	/*   no, no NUL encountered! */
    299  1.1      matt 	/*
    300  1.1      matt 	 * fall into this since we encountered a NULL.  At this point we have
    301  1.1      matt 	 * from 1-5 bytes (excluding trailing NUL) to write.
    302  1.1      matt 	 */
    303  1.1      matt 	mov	r3, #0			/* assume a NUL is in byte 0 */
    304  1.1      matt 	tst	r5, #BYTE0		/* is there a NUL in byte 0? */
    305  1.1      matt 	beq	1f			/*   yes, found a NUL! */
    306  1.1      matt 	mov	r3, #8			/* assume a NUL is in byte 1 */
    307  1.1      matt 	tst	r5, #BYTE1		/* is there a NUL in byte 0? */
    308  1.1      matt 	beq	1f			/*   yes, found a NUL! */
    309  1.1      matt 	tst	r5, #BYTE2		/* is there a NUL in byte 2? */
    310  1.1      matt 	moveq	r3, #16			/*   yes, mark its position */
    311  1.1      matt 	movne	r3, #24			/*   no, it must be in byte 3 */
    312  1.1      matt 1:
    313  1.1      matt #endif
    314  1.1      matt 	orr	r4, r4, r5, lshi r8	/* merge new and old src words */
    315  1.1      matt #if defined(STRLCPY)
    316  1.1      matt 	add	r1, r1, r3, lsr #3	/* adjust src to point to NUL */
    317  1.1      matt #endif
    318  1.1      matt 	add	r3, r3, r8		/* add remainder bytes worth */
    319  1.1      matt 	cmp	r3, #32			/* do we have at least one word to write? */
    320  1.1      matt 	movlt	r5, r4			/*   no, move source bytes to expected reg */
    321  1.1      matt 	blt	.Llast_bytes		/*   no, deal with them */
    322  1.1      matt #if defined(STRLCPY)
    323  1.1      matt 	subs	r2, r2, #4		/* subtract 4 from count */
    324  1.1      matt 	bpl	1f			/*   we have space for at least 4 */
    325  1.1      matt 	/*
    326  1.1      matt 	 * Since the space just went minus, we don't have enough room to
    327  1.1      matt 	 * write all 4 bytes.  In fact, the most we can write is 3 so just
    328  1.1      matt 	 * just lie and say we have 3 bytes to write and discard the rest.
    329  1.1      matt 	 */
    330  1.1      matt 	add	r2, r2, #4		/* add 4 back */
    331  1.1      matt 	mov	r3, #24			/* say we have 3 bytes */
    332  1.1      matt 	mov	r5, r4			/* discard the bytes we can't store */
    333  1.1      matt 	b	.Llast_bytes		/* and treat this as our last word */
    334  1.1      matt 1:
    335  1.1      matt #elif defined(STRNCPY)
    336  1.1      matt 	subs	r2, r2, #4		/* subtract 4 from count */
    337  1.1      matt 	bmi	.Lincongruent_no_more_room /*   count < 0? dst will be full */
    338  1.1      matt #endif
    339  1.1      matt 	str	r4, [r0], #4		/* store dst word */
    340  1.1      matt #if defined(STRNCPY)
    341  1.1      matt 	beq	.Ldst_full_word_aligned	/*   space left is 0? stop copy */
    342  1.1      matt #endif
    343  1.1      matt #if defined(STRLCPY)
    344  1.1      matt 	bne	1f			/* we still have space remaining */
    345  1.1      matt 	strb	r2, [r0]		/* write final NUL */
    346  1.1      matt 	b	.Lend_of_string		/* we are done */
    347  1.1      matt 1:
    348  1.1      matt #endif
    349  1.1      matt 	/*
    350  1.1      matt 	 * Subtract the 32 bits just written from the number of bits left
    351  1.1      matt 	 * to write.  If 0 bits are left and not doing strncpy, just write
    352  1.1      matt 	 * the trailing NUL and be done.
    353  1.1      matt 	 */
    354  1.1      matt 	subs	r3, r3, #32		/* we wrote one word */
    355  1.1      matt #if !defined(STRNCPY)
    356  1.1      matt 	bne	1f			/* no more data? */
    357  1.1      matt 	strb	r3, [r0]		/* write final NUL */
    358  1.1      matt 	b	.Lend_of_string		/* we are done */
    359  1.1      matt 1:
    360  1.1      matt #endif
    361  1.1      matt 	/*
    362  1.1      matt 	 * At this point after writing 4 bytes, we have 0 or 1 bytes left to
    363  1.1      matt 	 * write (excluding the trailing NUL).
    364  1.1      matt 	 */
    365  1.1      matt 	mov	r5, r5, lslo r9		/* get remainder of src */
    366  1.1      matt 
    367  1.1      matt 	/* fall into .Llast_bytes */
    368  1.1      matt 
    369  1.1      matt #if !defined(STRLCPY)
    370  1.1      matt .Lcongruent_last_bytes:
    371  1.1      matt #endif
    372  1.1      matt .Llast_bytes:
    373  1.1      matt 	/*
    374  1.1      matt 	 * r5 contains the last word and is in host byte order.
    375  1.1      matt 	 * r3 contains number of bits left to copy (0..31).
    376  1.1      matt 	 * r1 should point to the NUL + 4.
    377  1.1      matt 	 */
    378  1.1      matt 	bics	ip, r3, #7		/* truncate bits, is result 0? */
    379  1.1      matt #if !defined(STRNCPY)
    380  1.1      matt 	bne	1f			/*   no, have to write some bytes */
    381  1.1      matt 	strb	ip, [r0]		/*   yes, write trailing NUL */
    382  1.1      matt 	b	.Lend_of_string		/*   yes, and we are the end */
    383  1.1      matt 1:
    384  1.1      matt #endif
    385  1.1      matt #if defined(STRLCPY) || defined(STRNCPY)
    386  1.1      matt 	cmp	r2, ip, lsr #3		/* is there enough room? */
    387  1.1      matt 	movlt	ip, r2, lsl #3		/*   no, only fill remaining space */
    388  1.1      matt #endif
    389  1.1      matt 	mvn	r3, #0			/* create a mask */
    390  1.1      matt 	mov	r3, r3, lshi ip		/* clear leading bytes */
    391  1.1      matt 	bic	r5, r5, r3		/* clear trailing bytes */
    392  1.1      matt #if defined(STRNCPY)
    393  1.1      matt 	cmp	r2, #4			/* room for 4 bytes? */
    394  1.1      matt 	movge	ip, #32			/*   yes, we will write 4 bytes */
    395  1.1      matt 	bge	2f			/*   yes, and go do it */
    396  1.1      matt 	mvn	r3, #0			/* create a mask (again) */
    397  1.1      matt 	mov	ip, r2, lsl #3		/* remaining space bytes -> bits */
    398  1.1      matt 	mov	r3, r3, lshi ip		/* clear remaining bytes */
    399  1.1      matt #elif defined(STRLCPY)
    400  1.1      matt 	cmp	r2, #3			/* do we have room for 3 bytes & NUL? */
    401  1.1      matt 	bge	2f			/*   yes, just clear out dst */
    402  1.1      matt 	mov	r3, r3, lshi #8		/* mask out trailing NUL */
    403  1.1      matt #else
    404  1.1      matt 	cmp	ip, #24			/* are we writing 3 bytes & a NUL? */
    405  1.1      matt 	bge	2f			/*   yes, just overwrite dst */
    406  1.1      matt 	mov	r3, r3, lshi #8		/* mask out trailing NUL */
    407  1.1      matt #endif /* !STRNCPY */
    408  1.1      matt 	ldr	r4, [r0]		/* fetch dst word */
    409  1.1      matt 	and	r4, r4, r3		/* preserve trailing bytes */
    410  1.1      matt 	orr	r5, r5, r4		/* merge dst with src */
    411  1.1      matt 2:	str	r5, [r0], #4		/* store last word */
    412  1.1      matt #if defined(STRNCPY)
    413  1.1      matt 	subs	r2, r2, ip, lsr #3	/* subtract bytes cleared from count */
    414  1.1      matt 	beq	.Ldst_full_word_aligned
    415  1.1      matt #endif
    416  1.1      matt 	b	.Lend_of_string
    417  1.1      matt 
    418  1.1      matt #if defined(STRLCPY) || defined(STRNCPY)
    419  1.1      matt .Lno_more_room:
    420  1.1      matt #if defined(STRLCPY)
    421  1.1      matt 	cmp	r2, #-1			/* tried to write 3 bytes? */
    422  1.1      matt 	blt	1f			/*   less, partial word write */
    423  1.1      matt 	cmp	r2, #0			/* no space left? */
    424  1.3      matt 	strbeq	r2, [r0]		/* write the final NUL */
    425  1.1      matt 	bicne	r5, r5, #BYTE3		/* clear trailing NUL */
    426  1.1      matt 	strne	r5, [r0]		/* write last word */
    427  1.1      matt 	b	.Ldst_full_word_aligned	/* the dst buffer is full */
    428  1.1      matt 1:
    429  1.1      matt #endif /* STRLCPY */
    430  1.1      matt 	add	r2, r2, #4		/* restore remaining space */
    431  1.1      matt 	ldr	r4, [r0]		/* load dst */
    432  1.1      matt 	mvn	r3, #0			/* create a mask */
    433  1.1      matt 	mov	r2, r2, lsl #3		/* bytes -> bits */
    434  1.1      matt 	mov	r3, r3, lshi r2		/* clear leading bytes */
    435  1.1      matt 	bic	r5, r5, r3		/* clear trailing bytes from src */
    436  1.1      matt #if defined(STRLCPY)
    437  1.1      matt 	mov	r3, r3, lshi #8		/* mask out trailing NUL */
    438  1.1      matt #endif /* STRLCPY */
    439  1.1      matt 	and	r4, r4, r3		/* preserve trailing bytes in dst */
    440  1.1      matt 	orr	r4, r4, r5		/* merge src with dst */
    441  1.1      matt 	str	r4, [r0], #4		/* write last word */
    442  1.1      matt 	b	.Ldst_full_word_aligned
    443  1.1      matt #endif /* STRLCPY || STRNCPY */
    444  1.1      matt 
    445  1.1      matt #if defined(STRLCPY)
    446  1.1      matt 	/*
    447  1.1      matt 	 * Destination was filled (and NUL terminated).
    448  1.1      matt 	 * All that's left is count the number of bytes left in src.
    449  1.1      matt 	 */
    450  1.1      matt .Ldst_full:
    451  1.1      matt 1:	tst	r1, #3			/* dst word aligned? */
    452  1.1      matt 	beq	2f			/*   yes, so do it word by word */
    453  1.1      matt 	ldrb	r5, [r1], #1		/* load next byte */
    454  1.1      matt 	teq	r5, #0			/* is it a NUL? */
    455  1.1      matt 	bne	1b			/*   no, check alignment */
    456  1.1      matt 	b	.Lend_of_string		/* and return */
    457  1.1      matt 2:	add	r6, r6, #3		/* compensate for post-inc */
    458  1.1      matt .Ldst_full_word_aligned:
    459  1.1      matt 3:	ldr	r5, [r1], #4		/* load word from src */
    460  1.1      matt #ifdef _ARM_ARCH_6
    461  1.1      matt 	uqadd8	r5, r5, r7		/* perform NUL magic */
    462  1.1      matt 	mvns	r5, r5			/* complement all 0s? */
    463  1.1      matt 	beq	3b			/*   yes, no NUL so get next word */
    464  1.1      matt #else
    465  1.1      matt 	tst	r5, #BYTE0		/* does byte 0 contain a NUL? */
    466  1.1      matt 	tstne	r5, #BYTE1		/*   no, does byte 1 contain a NUL? */
    467  1.1      matt 	tstne	r5, #BYTE2		/*   no, does byte 2 contain a NUL? */
    468  1.1      matt 	tstne	r5, #BYTE3		/*   no, does byte 3 contain a NUL? */
    469  1.1      matt 	bne	3b			/*   no, no NUL encountered! */
    470  1.1      matt #endif
    471  1.1      matt #ifdef _ARM_ARCH_6
    472  1.1      matt #ifdef __ARMEL__
    473  1.1      matt 	rev	r5, r5			/* CLZ needs BE data */
    474  1.1      matt #endif
    475  1.1      matt 	clz	r5, r5			/* count leading zeros */
    476  1.1      matt 	add	r1, r1, r5, lsr #3	/* add offset to NUL to src pointer */
    477  1.1      matt #else
    478  1.1      matt 	tst	r5, #BYTE0		/* is there a NUL in byte 0? */
    479  1.1      matt 	beq	4f			/*   yes, don't check any further */
    480  1.1      matt 	add	r1, r1, #1		/*   no, advance src pointer by 1 */
    481  1.1      matt 	tst	r5, #BYTE1		/* is there a NUL in byte 1? */
    482  1.1      matt 	beq	4f			/*   yes, don't check any further */
    483  1.1      matt 	add	r1, r1, #1		/*   no, advance src pointer by 1 */
    484  1.1      matt 	tst	r5, #BYTE2		/* is there a NUL in byte 2? */
    485  1.1      matt 	addne	r1, r1, #1		/*   no, there must be in byte 3 */
    486  1.1      matt 4:
    487  1.1      matt #endif /* _ARM_ARCH_6 */
    488  1.1      matt .Lend_of_string:
    489  1.1      matt 	sub	r0, r1, r6		/* subtract start from finish */
    490  1.1      matt 	pop	{r4-r9}			/* restore registers */
    491  1.1      matt 	RET
    492  1.1      matt #elif defined(STRNCPY)
    493  1.1      matt .Lend_of_string:
    494  1.1      matt 	teq	r2, #0			/* any bytes left to zero? */
    495  1.1      matt 	beq	3f 			/*   no, just return. */
    496  1.1      matt 	mov	r1, #0			/*   yes, prepare to zero */
    497  1.1      matt 	cmp	r2, #16			/* some, but not a lot? */
    498  1.1      matt 	ble	1f
    499  1.1      matt 	mov	r4, lr			/* preserve lr */
    500  1.1      matt 	bl	PLT_SYM(_C_LABEL(memset)) /*   yes, and let memset do it */
    501  1.1      matt 	mov	lr, r4			/* restore lr */
    502  1.1      matt 	b	3f			/* return */
    503  1.1      matt 1:	add	ip, r0, r2		/* calculate stopping point */
    504  1.1      matt 2:	strb	r1, [r0], #1		/* clear a byte */
    505  1.1      matt 	cmp	r0, ip			/* done? */
    506  1.1      matt 	blt	2b			/*   no, clear next byte */
    507  1.1      matt 3:	mov	r0, r6			/* restore dst pointer */
    508  1.1      matt 	pop	{r4-r9}			/* restore registers */
    509  1.1      matt 	RET
    510  1.1      matt .Ldst_full:
    511  1.1      matt .Ldst_full_word_aligned:
    512  1.1      matt 	/*
    513  1.1      matt 	 * Destination was filled (but not NUL terminated).
    514  1.1      matt 	 * All that's left is return the start of dst
    515  1.1      matt 	 */
    516  1.1      matt 	mov	r0, r6			/* restore dst pointer */
    517  1.1      matt 	pop	{r4-r9}			/* restore registers */
    518  1.1      matt 	RET
    519  1.1      matt #else
    520  1.1      matt .Lend_of_string:
    521  1.1      matt 	mov	r0, r6			/* restore dst pointer */
    522  1.1      matt 	pop	{r4-r9}			/* restore registers */
    523  1.1      matt 	RET
    524  1.1      matt #endif
    525  1.1      matt END(FUNCNAME)
    526