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