1/*
2Copyright (c) 2013 Andes Technology Corporation.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
8    Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10
11    Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14
15    The name of the company may not be used to endorse or promote
16    products derived from this software without specific prior written
17    permission.
18
19THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22DISCLAIMED.  IN NO EVENT SHALL RED HAT INCORPORATED BE LIABLE FOR ANY
23DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
31    Function:
32      memcpy - copy memory regions
33    Syntax:
34      void *memcpy(void *s1, const void *s2, size_t n);
35    Description:
36      The memcpy function copies n characters from the object pointed to
37      by s2 into the object pointed to by s1. If copying takes place
38      between objects that overlap, the behavior is undefined.
39    Return value:
40      The memcpy function returns the value of s1.
41*/
42	.text
43	.align	2
44	.globl	memcpy
45	.type	memcpy, @function
46memcpy:
47	/* Corner cases. If *s1 equals *s2
48	   or size_t is zero, just go return.  */
49	beq	$r0, $r1, .Lend_memcpy
50	beqz	$r2, .Lend_memcpy
51
52	/* Keep *s1 as return value.
53	   Set $r3 as how many words to copy.
54	   Set $r2 as how many bytes are less than a word.  */
55	move	$r5, $r0
56	srli	$r3, $r2, 2
57	andi	$r2, $r2, 3
58	beqz	$r3, .Lbyte_copy
59
60.Lword_copy:
61	/* Do the word copy $r3 times. Then, do the byte copy $r2 times.  */
62	lmw.bim	$r4, [$r1], $r4, 0
63	addi	$r3, $r3, -1
64	smw.bim	$r4, [$r5], $r4, 0
65	bnez	$r3, .Lword_copy	/* Loop again ?  */
66	beqz	$r2, .Lend_memcpy	/* Fall THRU or go return ?  */
67
68.Lbyte_copy:
69	/* Do the byte copy $r2 times.  */
70	lbi.bi	$r4, [$r1], 1
71	addi	$r2, $r2, -1
72	sbi.bi	$r4, [$r5], 1
73	bnez	$r2, .Lbyte_copy	/* Loop again ?  */
74
75.Lend_memcpy:
76	ret
77	.size   memcpy, .-memcpy
78