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      memset - fill memory with a constant byte
33    Syntax:
34      void *memset(void *s, int c, size_t n);
35    Description:
36      The memset function copies the value of c (converted to an unsigned char)
37      into each of the first n characters of the object pointed to by s.
38    Return value:
39      The memset function returns the value of s.
40*/
41	.text
42	.align	2
43	.globl	memset
44	.type	memset, @function
45memset:
46	/* Corner case. If n is zero, just go return.  */
47	beqz    $r2, .Lend_memset
48
49	/* Keep $r0 as return value.
50	   Set $r4 as how many words to copy.
51	   Set $r2 as how many bytes are less than a word.  */
52	move    $r5, $r0
53	srli    $r4, $r2, 2
54	andi    $r2, $r2, 3
55	beqz    $r4, .Lbyte_set
56
57	/* Set $r1 a word-size pattern composed of the value of c
58	  (converted to an unsigned char). Convert ??????ab to abababab.  */
59	andi    $r1, $r1, 0xff	/* Set $r1 = 000000ab.  */
60	slli    $r3, $r1, 8	/* Set $r3 = 0000ab00.  */
61	or      $r1, $r1, $r3	/* Set $r1 = 0000abab.  */
62	slli    $r3, $r1, 16	/* Set $r3 = abab0000.  */
63	or      $r1, $r1, $r3	/* Set $r1 = abababab.  */
64
65.Lword_set:
66	/* Do the word set $r4 times. Then, do the byte set $r2 times.  */
67	addi    $r4, $r4, -1
68	smw.bim $r1, [$r5], $r1		/* Set a word-size.  */
69	bnez    $r4, .Lword_set		/* Loop again ?  */
70	beqz    $r2, .Lend_memset	/* Fall THRU or go return ?  */
71
72.Lbyte_set:
73	/* Do the byte set $r2 times.  */
74	addi    $r2, $r2, -1
75	sbi.p   $r1, [$r5], 1		/* Set a byte-size.  */
76	bnez    $r2, .Lbyte_set		/* Loop again ?  */
77
78.Lend_memset:
79	ret
80	.size   memset, .-memset
81