yang.li | b06e0a8 | 2022-01-10 17:35:09 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021-2022 Amlogic, Inc. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | */ |
| 6 | |
Xiaohu.Huang | 60a7f2f | 2021-10-25 15:40:57 +0800 | [diff] [blame] | 7 | #include "aml_memset.h" |
| 8 | |
| 9 | void *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.huang | 1fd6f11 | 2022-05-24 11:02:05 +0800 | [diff] [blame] | 15 | char *const tail = (char *)dest + len; |
Xiaohu.Huang | 60a7f2f | 2021-10-25 15:40:57 +0800 | [diff] [blame] | 16 | /* Set 'body' to the last word boundary */ |
xiaohu.huang | 1fd6f11 | 2022-05-24 11:02:05 +0800 | [diff] [blame] | 17 | uint32_t *const body = (uint32_t *)((uintptr_t)tail & ~3); |
Xiaohu.Huang | 60a7f2f | 2021-10-25 15:40:57 +0800 | [diff] [blame] | 18 | |
xiaohu.huang | 1fd6f11 | 2022-05-24 11:02:05 +0800 | [diff] [blame] | 19 | c &= 0xff; /* Clear upper bits before ORing below */ |
Xiaohu.Huang | 60a7f2f | 2021-10-25 15:40:57 +0800 | [diff] [blame] | 20 | 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 | } |