xref: /rk3399_ARM-atf/lib/libc/memset.c (revision 75fab6496e5fce9a11b4e3a160ad2e797acc6ee9)
1 /*
2  * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stddef.h>
8 #include <string.h>
9 #include <stdint.h>
10 
11 void *memset(void *dst, int val, size_t count)
12 {
13 	char *ptr = dst;
14 	uint64_t *ptr64;
15 	uint64_t fill = (unsigned char)val;
16 
17 	/* Simplify code below by making sure we write at least one byte. */
18 	if (count == 0) {
19 		return dst;
20 	}
21 
22 	/* Handle the first part, until the pointer becomes 64-bit aligned. */
23 	while (((uintptr_t)ptr & 7)) {
24 		*ptr++ = val;
25 		if (--count == 0) {
26 			return dst;
27 		}
28 	}
29 
30 	/* Duplicate the fill byte to the rest of the 64-bit word. */
31 	fill |= fill << 8;
32 	fill |= fill << 16;
33 	fill |= fill << 32;
34 
35 	/* Use 64-bit writes for as long as possible. */
36 	ptr64 = (void *)ptr;
37 	for (; count >= 8; count -= 8) {
38 		*ptr64++ = fill;
39 	}
40 
41 	/* Handle the remaining part byte-per-byte. */
42 	ptr = (void *)ptr64;
43 	while (count--) {
44 		*ptr++ = val;
45 	}
46 
47 	return dst;
48 }
49