Home | History | Annotate | Line # | Download | only in string
memmove.S revision 1.1
      1  1.1  matt /* $NetBSD: memmove.S,v 1.1 2014/09/03 19:34:25 matt Exp $ */
      2  1.1  matt 
      3  1.1  matt /* stropt/memmove.S, pl_string_common, pl_linux 10/11/04 11:45:37
      4  1.1  matt  * ==========================================================================
      5  1.1  matt  * Optimized memmove implementation for IBM PowerPC 405/440.
      6  1.1  matt  *
      7  1.1  matt  *	Copyright (c) 2003, IBM Corporation
      8  1.1  matt  *	All rights reserved.
      9  1.1  matt  *
     10  1.1  matt  *	Redistribution and use in source and binary forms, with or
     11  1.1  matt  *	without modification, are permitted provided that the following
     12  1.1  matt  *	conditions are met:
     13  1.1  matt  *
     14  1.1  matt  *	* Redistributions of source code must retain the above
     15  1.1  matt  *	copyright notice, this list of conditions and the following
     16  1.1  matt  *	disclaimer.
     17  1.1  matt  *	* Redistributions in binary form must reproduce the above
     18  1.1  matt  *	copyright notice, this list of conditions and the following
     19  1.1  matt  *	disclaimer in the documentation and/or other materials
     20  1.1  matt  *	provided with the distribution.
     21  1.1  matt  *	* Neither the name of IBM nor the names of its contributors
     22  1.1  matt  *	may be used to endorse or promote products derived from this
     23  1.1  matt  *	software without specific prior written permission.
     24  1.1  matt  *
     25  1.1  matt  *	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     26  1.1  matt  *	CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     27  1.1  matt  *	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     28  1.1  matt  *	MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     29  1.1  matt  *	DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     30  1.1  matt  *	BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     31  1.1  matt  *	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     32  1.1  matt  *	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     33  1.1  matt  *	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     34  1.1  matt  *	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  1.1  matt  *	(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     36  1.1  matt  *	USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  1.1  matt  *
     38  1.1  matt  * ==========================================================================
     39  1.1  matt  *
     40  1.1  matt  * Function: Move memory area (handles overlapping regions)
     41  1.1  matt  *
     42  1.1  matt  *		void *memmove(void * dest, const void * src, int n)
     43  1.1  matt  *
     44  1.1  matt  * Input:	r3 - destination address
     45  1.1  matt  *		r4 - source address
     46  1.1  matt  *		r5 - byte count
     47  1.1  matt  * Output:	r11 - destination address
     48  1.1  matt  *
     49  1.1  matt  * ==========================================================================
     50  1.1  matt  */
     51  1.1  matt 
     52  1.1  matt #include <machine/asm.h>
     53  1.1  matt 
     54  1.1  matt #ifdef _BCOPY
     55  1.1  matt /* bcopy = memcpy/memmove with arguments reversed. */
     56  1.1  matt /* LINTSTUB: Func: void bcopy(void *, void *, size_t) */
     57  1.1  matt ENTRY(bcopy)
     58  1.1  matt 	l.or	r6, r3, r0		/* swap src/dst */
     59  1.1  matt 	l.or	r3, r4, r0
     60  1.1  matt 	l.or	r4, r6, r0
     61  1.1  matt #else
     62  1.1  matt /* LINTSTUB: Func: void *memmove(void *, const void *, size_t) */
     63  1.1  matt ENTRY(memmove)
     64  1.1  matt #endif
     65  1.1  matt 
     66  1.1  matt 	l.or	r11, r3, r0		/* Save dst (return value)	*/
     67  1.1  matt 
     68  1.1  matt 	l.sfges	r4, r3			/* Branch to reverse if 	*/
     69  1.1  matt 	l.bnf	.Lreverse		/* src < dest. Don't want to	*/
     70  1.1  matt 					/* overwrite end of src with	*/
     71  1.1  matt 					/* start of dest 		*/
     72  1.1  matt 
     73  1.1  matt 	l.addi	r4, r4, -4		/* Back up src and dst pointers */
     74  1.1  matt 	l.addi	r3, r3, -4		/* due to auto-update of 'load' */
     75  1.1  matt 
     76  1.1  matt 	l.srli	r13, r5, 2		/* How many words in total cnt	*/
     77  1.1  matt 	l.sfeqi	r13, 0
     78  1.1  matt 	l.bf	.Llast1			/* Handle byte by byte if < 4	*/
     79  1.1  matt 					/* bytes total 			*/
     80  1.1  matt 	l.lwz	r7, 4(r4)		/* Preload first word		*/
     81  1.1  matt 	l.addi	r4, r4, 4
     82  1.1  matt 
     83  1.1  matt 	l.j	.Lg1
     84  1.1  matt 	l.nop
     85  1.1  matt 
     86  1.1  matt .Lg0:					/* Main loop			*/
     87  1.1  matt 
     88  1.1  matt 	l.lwz	r7, 4(r4)		/* Load a new word		*/
     89  1.1  matt 	l.sw	4(r3), r6		/* Store previous word		*/
     90  1.1  matt 	l.addi	r4, r4, 4		/* advance */
     91  1.1  matt 	l.addi	r3, r3, 4		/* advance */
     92  1.1  matt 
     93  1.1  matt .Lg1:
     94  1.1  matt 
     95  1.1  matt 	l.addi	r13, r13, -1
     96  1.1  matt 	l.sfeqi	r13, 0
     97  1.1  matt 	l.bf	.Llast			/* Dec cnt, and branch if just	*/
     98  1.1  matt 	l.nop
     99  1.1  matt 					/* one word to store		*/
    100  1.1  matt 	l.lwz	r6, 4(r4)		/* Load another word		*/
    101  1.1  matt 	l.sw	4(r3), r7		/* Store previous word		*/
    102  1.1  matt 	l.addi	r4, r4, 4		/* advance to next word		*/
    103  1.1  matt 	l.addi	r3, r3, 4		/* advance to next word		*/
    104  1.1  matt 	l.addi	r13, r13, -1		/* Decrement count		*/
    105  1.1  matt 	l.sfeqi	r13, 0			/* last word?			*/
    106  1.1  matt 	l.bnf	.Lg0			/*    no, loop, more words	*/
    107  1.1  matt 	l.nop
    108  1.1  matt 
    109  1.1  matt 	l.or	r7, r6, r0		/* If word count -> 0, then...	*/
    110  1.1  matt 
    111  1.1  matt .Llast:
    112  1.1  matt 
    113  1.1  matt 	l.sw	4(r3), r7		/* ... store last word		*/
    114  1.1  matt 	l.addi	r3, r3, 4
    115  1.1  matt 
    116  1.1  matt .Llast1:				/* Byte-by-byte copy		*/
    117  1.1  matt 
    118  1.1  matt 	l.andi	r5, r5, 3		/* get remaining byte count	*/
    119  1.1  matt 	l.sfeqi	r5, 0			/* is it 0?			*/
    120  1.1  matt 	l.bf	.Ldone			/*   yes, we're done		*/
    121  1.1  matt 	l.nop				/* -- delay slot --		*/
    122  1.1  matt 
    123  1.1  matt 	l.lbz	r6, 4(r4)		/* 1st byte: update addr by 4	*/
    124  1.1  matt 	l.sb	4(r3), r6		/* since we pre-adjusted by 4	*/
    125  1.1  matt 	l.addi	r4, r4, 4		/* advance to next word		*/
    126  1.1  matt 	l.addi	r3, r3, 4		/* advance to next word		*/
    127  1.1  matt 	l.addi	r5, r5, -1		/* decrement count		*/
    128  1.1  matt 	l.sfeqi	r5, 0			/* is it 0?			*/
    129  1.1  matt 	l.bf	.Ldone			/*    yes, we're done		*/
    130  1.1  matt 	l.nop				/* -- delay slot --		*/
    131  1.1  matt 
    132  1.1  matt .Llast2:
    133  1.1  matt 
    134  1.1  matt 	l.lbz	r6, 1(r4)		/* But handle the rest by	*/
    135  1.1  matt 	l.sb	1(r3), r6		/* updating addr by 1		*/
    136  1.1  matt 	l.addi	r4, r4, 1		/* advance to next word		*/
    137  1.1  matt 	l.addi	r3, r3, 1		/* advance to next word		*/
    138  1.1  matt 	l.addi	r5, r5, -1		/* decrement count		*/
    139  1.1  matt 	l.sfeqi	r5, 0			/* is it 0?			*/
    140  1.1  matt 	l.bnf	.Llast2			/*    yes, we're done		*/
    141  1.1  matt 	l.nop				/* -- delay slot --		*/
    142  1.1  matt .Ldone:
    143  1.1  matt 	l.jr	lr			/* return			*/
    144  1.1  matt 	l.nop				/* -- delay slot --		*/
    145  1.1  matt 
    146  1.1  matt 	/* We're here since src < dest. Don't want to overwrite end of	*/
    147  1.1  matt 	/* src with start of dest						*/
    148  1.1  matt 
    149  1.1  matt .Lreverse:
    150  1.1  matt 
    151  1.1  matt 	l.add	r4, r4, r5		/* Work from end to beginning	*/
    152  1.1  matt 	l.add	r3, r3, r5 		/* so add count to string ptrs	*/
    153  1.1  matt 	l.srli	r13, r5, 2		/* Words in total count		*/
    154  1.1  matt 	l.sfeqi	r13, 0
    155  1.1  matt 	l.bf	.Lrlast1		/* Handle byte by byte if < 4	*/
    156  1.1  matt 					/* bytes total 			*/
    157  1.1  matt 	l.nop
    158  1.1  matt 
    159  1.1  matt 	l.lwz	r7, -4(r4)		/* Preload first word		*/
    160  1.1  matt 	l.addi	r4, r4, -4		/* update pointer		*/
    161  1.1  matt 
    162  1.1  matt 	l.j	.Lrg1
    163  1.1  matt 
    164  1.1  matt .Lrg0:					/* Main loop			*/
    165  1.1  matt 
    166  1.1  matt 	l.lwz	r7, -4(r4)		/* Load a new word		*/
    167  1.1  matt 	l.sw	-4(r3), r6		/* Store previous word		*/
    168  1.1  matt 	l.addi	r4, r4, -4
    169  1.1  matt 	l.addi	r3, r3, -4
    170  1.1  matt 
    171  1.1  matt .Lrg1:
    172  1.1  matt 
    173  1.1  matt 	l.addi	r13, r13, -1		/* decrement count		*/
    174  1.1  matt 	l.sfeqi	r13, 0			/* just one pending word left?	*/
    175  1.1  matt 	l.bf	.Lrlast			/*    yes, deal with it		*/
    176  1.1  matt 
    177  1.1  matt 	l.lwz	r6, -4(r4)		/* Load another word		*/
    178  1.1  matt 	l.sw	-4(r3), r7		/* Store previous word		*/
    179  1.1  matt 	l.addi	r4, r4, -4
    180  1.1  matt 	l.addi	r3, r3, -4
    181  1.1  matt 
    182  1.1  matt 	l.addi	r13, r13, -1		/* decrement count		*/
    183  1.1  matt 	l.sfeqi	r13, 0			/* just one pending word left?	*/
    184  1.1  matt 	l.bnf	.Lrg0			/*    no, loop again more words	*/
    185  1.1  matt 	l.nop
    186  1.1  matt 
    187  1.1  matt 	l.or	r7, r6, r0		/* If word count -> 0, then...	*/
    188  1.1  matt 
    189  1.1  matt .Lrlast:
    190  1.1  matt 
    191  1.1  matt 	l.sw	-4(r3), r7		/* ... store last word		*/
    192  1.1  matt 	l.addi	r3, r3, -4		/* update pointer */
    193  1.1  matt 
    194  1.1  matt .Lrlast1:				/* Byte-by-byte copy		*/
    195  1.1  matt 
    196  1.1  matt 	l.andi	r5, r5, 3
    197  1.1  matt 	l.sfeqi	r5, 0
    198  1.1  matt 	l.bf	.Lrdone
    199  1.1  matt 
    200  1.1  matt .Lrlast2:
    201  1.1  matt 
    202  1.1  matt 	l.lbz	r6, -1(r4)		/* Handle the rest, byte by 	*/
    203  1.1  matt 	l.sb	-1(r3), r6		/* byte				*/
    204  1.1  matt 	l.addi	r4, r4, -1
    205  1.1  matt 	l.addi	r3, r3, -1
    206  1.1  matt 	l.addi	r5, r5, -1		/* decrement count		*/
    207  1.1  matt 	l.sfeqi	r5, 0			/* is it 0?			*/
    208  1.1  matt 	l.bnf	.Lrlast2		/*    no, loop again		*/
    209  1.1  matt 	l.nop
    210  1.1  matt .Lrdone:
    211  1.1  matt 	l.jr	lr
    212  1.1  matt 	l.nop
    213  1.1  matt 
    214  1.1  matt #ifdef _BCOPY
    215  1.1  matt END(bcopy)
    216  1.1  matt #else
    217  1.1  matt END(memmove)
    218  1.1  matt #endif
    219