blob: 226c0babeead3cb36c3d8c40b5271869bbf87df7 [file] [log] [blame]
yang.lib06e0a82022-01-10 17:35:09 +08001/*
2 * Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: MIT
5 */
6
Xiaohu.Huang60a7f2f2021-10-25 15:40:57 +08007#include "aml_memset.h"
8
9void *memset(void *dest, int c, size_t len)
10{
11 char *d = (char *)dest;
12 uint32_t cccc;
13 uint32_t *dw;
14 char *head;
xiaohu.huang1fd6f112022-05-24 11:02:05 +080015 char *const tail = (char *)dest + len;
Xiaohu.Huang60a7f2f2021-10-25 15:40:57 +080016 /* Set 'body' to the last word boundary */
xiaohu.huang1fd6f112022-05-24 11:02:05 +080017 uint32_t *const body = (uint32_t *)((uintptr_t)tail & ~3);
Xiaohu.Huang60a7f2f2021-10-25 15:40:57 +080018
xiaohu.huang1fd6f112022-05-24 11:02:05 +080019 c &= 0xff; /* Clear upper bits before ORing below */
Xiaohu.Huang60a7f2f2021-10-25 15:40:57 +080020 cccc = c | (c << 8) | (c << 16) | (c << 24);
21
22 if ((uintptr_t)tail < (((uintptr_t)d + 3) & ~3))
23 /* len is shorter than the first word boundary */
24 head = tail;
25 else
26 /* Set 'head' to the first word boundary */
27 head = (char *)(((uintptr_t)d + 3) & ~3);
28
29 /* Copy head */
30 while (d < head)
31 *(d++) = c;
32
33 /* Copy body */
34 dw = (uint32_t *)d;
35 while (dw < body)
36 *(dw++) = cccc;
37
38 /* Copy tail */
39 d = (char *)dw;
40 while (d < tail)
41 *(d++) = c;
42
43 return dest;
44}