Home | History | Annotate | Line # | Download | only in string
      1 /* $NetBSD: memcpy.S,v 1.6 2014/03/04 17:05:14 macallan Exp $ */
      2 
      3 /* stropt/memcpy_440.S, pl_string_common, pl_linux 10/11/04 11:45:36
      4  * ==========================================================================
      5  * Optimized memcpy implementation for IBM PowerPC 440.
      6  *
      7  *  Copyright (c) 2003, IBM Corporation
      8  *  All rights reserved.
      9  *
     10  *  Redistribution and use in source and binary forms, with or
     11  *  without modification, are permitted provided that the following
     12  *  conditions are met:
     13  *
     14  *    * Redistributions of source code must retain the above
     15  *      copyright notice, this list of conditions and the following
     16  *      disclaimer.
     17  *    * Redistributions in binary form must reproduce the above
     18  *      copyright notice, this list of conditions and the following
     19  *      disclaimer in the documentation and/or other materials
     20  *      provided with the distribution.
     21  *    * Neither the name of IBM nor the names of its contributors
     22  *      may be used to endorse or promote products derived from this
     23  *      software without specific prior written permission.
     24  *
     25  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     26  *  CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     27  *  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     28  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     29  *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     30  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     31  *  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     32  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     33  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     34  *  OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     36  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  *
     38  * ==========================================================================
     39  *
     40  * Function: Copy n bytes of the source to the destination. Behavior is
     41  *	   undefined for objects that overlap.
     42  *
     43  *
     44  *	   void *memcpy(void * dest, const void * src, int n)
     45  *
     46  * Input:  r3 - destination address
     47  *	 r4 - source address
     48  *	 r5 - byte count
     49  * Output: r3 - destination address
     50  *
     51  * ==========================================================================
     52  */
     53 
     54 #include <machine/asm.h>
     55 #ifdef _KERNEL_OPT
     56 #include "opt_ppcarch.h"
     57 #endif
     58 
     59 	.text
     60 	.align 4
     61 /* LINTSTUB: Func: void *memcpy(void *, const void *, size_t) */
     62 ENTRY(memcpy)
     63 	/*
     64 	 * Check count passed in R5. If zero, return; otherwise continue.
     65 	 */
     66 	cmpwi	%r5,0
     67 	beqlr-
     68 
     69 #if defined(_KERNEL) && defined(PPC_OEA601)
     70 	/*
     71 	* 601 will generate alignment exceptions if operand crosses
     72 	 * 4k page boundary, so do byte copy when exception handler
     73 	 * not available.  Maybe want to have a different memcpy for 601
     74 	 * that checks for page boundaries/word alignment...
     75 	 */
     76 	mfspr   %r6, 287		/* mfpvbr %r6 PVR = 287        */
     77 	srwi    %r6, %r6, 0x10		/* get version field from PVR  */
     78 	cmpwi   %r6, 0x1		/* 601 CPU = 0x0001            */
     79 	bne     bnorm			/* skip byte-only unless 601   */
     80 
     81 	or	%r6, %r3, %r4		/* see if both source and dest */
     82 	andi.	%r6, %r6, 3		/* are 32bit aligned           */
     83 	beq	bnorm			/* skip byte-only if they are  */
     84 bcpy:
     85 	mtctr   %r5			/* byte copy everything */
     86 	li      %r6, 0
     87 bloop:
     88 	lbzx    %r7, %r4, %r6
     89 	stbx    %r7, %r3, %r6
     90 	addi    %r6, %r6, 1
     91 	bdnz    bloop
     92 	blr
     93 
     94 bnorm:
     95 
     96 #endif
     97 
     98 	mr	%r8, %r3		/* Copy dst (return value)	*/
     99 
    100 	addi	%r4, %r4, -4		/* Prepare for main loop's auto	*/
    101 	addi	%r8, %r8, -4		/* update		       */
    102 
    103 	srwi.	%r9,%r5,2		/* Word count -> r9 		*/
    104 	beq-	last1			/* Partial copy if <4 bytes	*/
    105 
    106 	mtctr	%r9			/* Word cnt in CTR for loop     */
    107 	lwzu	%r7, 4(%r4)		/* Preload for main loop	*/
    108 
    109 	b	g1
    110 
    111 g0:					/* Main loop			*/
    112 
    113 	lwzu	%r7, 4(%r4)		/* Load a new word		*/
    114 	stwu	%r6, 4(%r8)		/* Store previous word		*/
    115 
    116 g1:
    117 
    118 	bdz-	last			/* Dec ctr and exit loop if no  */
    119 					/* more words		   */
    120 	lwzu	%r6, 4(%r4)		/* Load another word		*/
    121 	stwu	%r7, 4(%r8)		/* Store previous word		*/
    122 	bdnz+	g0			/* Dec ctr and continue loop if */
    123 					/* more words		   */
    124 
    125 	mr	%r7, %r6
    126 
    127 last:
    128 
    129 	stwu	%r7, 4(%r8)		/* Store last word		*/
    130 
    131 last1:					/* Byte-by-byte copy		*/
    132 
    133 	clrlwi.	%r5,%r5,30
    134 	beqlr
    135 
    136 	mtctr	%r5
    137 
    138 	lbzu	%r6, 4(%r4)		/* 1st byte: update by word	*/
    139 	stbu	%r6, 4(%r8)
    140 	bdzlr-
    141 
    142 last2:
    143 
    144 	lbzu	%r6, 1(%r4)		/* Handle the rest		*/
    145 	stbu	%r6, 1(%r8)
    146 	bdnz+	last2
    147 
    148 	blr
    149 END(memcpy)
    150