blob: f9dd4c5dbe350cfc8ef3cc5796d5bcd5faba1089 [file] [log] [blame]
Mark Dykesad5ab072020-08-19 19:11:33 +00001/*
Andre Przywara16e7bcf2020-09-03 11:04:39 +01002 * Copyright (c) 2013-2020, ARM Limited and Contributors. All rights reserved.
Mark Dykesad5ab072020-08-19 19:11:33 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stddef.h>
8#include <string.h>
Andre Przywara16e7bcf2020-09-03 11:04:39 +01009#include <stdint.h>
Mark Dykesad5ab072020-08-19 19:11:33 +000010
11void *memset(void *dst, int val, size_t count)
12{
13 char *ptr = dst;
Andre Przywara16e7bcf2020-09-03 11:04:39 +010014 uint64_t *ptr64;
15 uint64_t fill = (unsigned char)val;
Mark Dykesad5ab072020-08-19 19:11:33 +000016
Andre Przywara16e7bcf2020-09-03 11:04:39 +010017 /* 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--) {
Mark Dykesad5ab072020-08-19 19:11:33 +000044 *ptr++ = val;
Andre Przywara16e7bcf2020-09-03 11:04:39 +010045 }
Mark Dykesad5ab072020-08-19 19:11:33 +000046
47 return dst;
48}