1/*
2 * Copyright (c) 2020, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <asm_macros.S>
8
9	.global	memset
10
11/* -----------------------------------------------------------------------
12 * void *memset(void *dst, int val, size_t count)
13 *
14 * Copy the value of 'val' (converted to an unsigned char) into
15 * each of the first 'count' characters of the object pointed to by 'dst'.
16 *
17 * Returns the value of 'dst'.
18 * -----------------------------------------------------------------------
19 */
20func memset
21	cbz	x2, exit		/* exit if 'count' = 0 */
22	mov	x3, x0			/* keep x0 */
23	tst	x0, #7
24	b.eq	aligned			/* 8-bytes aligned */
25
26	/* Unaligned 'dst' */
27unaligned:
28	strb	w1, [x3], #1
29	subs	x2, x2, #1
30	b.eq	exit			/* exit if 0 */
31	tst	x3, #7
32	b.ne	unaligned		/* continue while unaligned */
33
34	/* 8-bytes aligned */
35aligned:cbz	x1, x1_zero
36	bfi	w1, w1, #8, #8		/* propagate 'val' */
37	bfi	w1, w1, #16, #16
38	bfi	x1, x1, #32, #32
39
40x1_zero:ands	x4, x2, #~0x3f
41	b.eq	less_64
42
43write_64:
44	.rept	4
45	stp	x1, x1, [x3], #16	/* write 64 bytes in a loop */
46	.endr
47	subs	x4, x4, #64
48	b.ne	write_64
49less_64:tbz	w2, #5, less_32		/* < 32 bytes */
50	stp	x1, x1, [x3], #16	/* write 32 bytes */
51	stp	x1, x1, [x3], #16
52less_32:tbz	w2, #4, less_16		/* < 16 bytes */
53	stp	x1, x1, [x3], #16	/* write 16 bytes */
54less_16:tbz	w2, #3, less_8		/* < 8 bytes */
55	str	x1, [x3], #8		/* write 8 bytes */
56less_8:	tbz	w2, #2, less_4		/* < 4 bytes */
57	str	w1, [x3], #4		/* write 4 bytes */
58less_4:	tbz	w2, #1, less_2		/* < 2 bytes */
59	strh	w1, [x3], #2		/* write 2 bytes */
60less_2:	tbz	w2, #0, exit
61	strb	w1, [x3]		/* write 1 byte */
62exit:	ret
63
64endfunc	memset
65