blob: 0b69897fdbec114e8060502c7d2661d174062a99 [file] [log] [blame]
Alexei Fedorov3ad26ae2020-08-16 16:01:13 +01001/*
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 .syntax unified
10 .global memset
11
12/* -----------------------------------------------------------------------
13 * void memset(void *dst, int val, size_t count)
14 *
15 * Copy the value of 'val' (converted to an unsigned char) into
16 * each of the first 'count' characters of the object pointed to by 'dst'.
17 *
18 * Returns the value of 'dst'.
19 * -----------------------------------------------------------------------
20 */
21func memset
22 cmp r2, #0
23 bxeq lr /* return if 'count' = 0 */
24 mov r12, r0 /* keep r0 */
25 tst r0, #3
26 beq aligned /* 4-bytes aligned */
27
28 /* Unaligned 'dst' */
29unaligned:
30 strb r1, [r12], #1
31 subs r2, r2, #1
32 bxeq lr /* return if 0 */
33 tst r12, #3
34 bne unaligned /* continue while unaligned */
35
36 /* 4-bytes aligned */
37aligned:bfi r1, r1, #8, #8 /* propagate 'val' */
38 bfi r1, r1, #16, #16
39
40 mov r3, r1
41
42 cmp r2, #16
43 blo less_16
44
45 push {r4, lr}
46 mov r4, r1
47 mov lr, r1
48
49 cmp r2, #32
50 blo less_32
51
52write_32:
53 stmia r12!, {r1, r3, r4, lr} /* write 32 bytes in a loop */
54 stmia r12!, {r1, r3, r4, lr}
55 subs r2, r2, #32
56 popeq {r4, pc} /* return if 0 */
57 cmp r2, #32
58 bhs write_32
59
60less_32:cmp r2, #16
61 stmiahs r12!, {r1, r3, r4, lr} /* write 16 bytes */
62 popeq {r4, pc} /* return if 16 */
63 pop {r4, lr}
64
65less_16:lsls r2, r2, #29 /* C = r2[3]; N = r2[2]; Z = r2[2:0] */
66 stmiacs r12!, {r1, r3} /* write 8 bytes */
67 bxeq lr /* return if 8 */
68 strmi r1, [r12], #4 /* write 4 bytes */
69 lsls r2, r2, #1 /* N = r2[1]; Z = r2[0] */
70 strhmi r1, [r12], #2 /* write 2 bytes */
71 strbne r1, [r12] /* write 1 byte */
72 bx lr
73
74endfunc memset