Ken Liu | 8fb61f1 | 2021-12-14 08:32:10 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2019-2022, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "crt_impl_private.h" |
| 9 | |
| 10 | void *memcpy(void *dest, const void *src, size_t n) |
| 11 | { |
| 12 | union composite_addr_t p_dst, p_src; |
| 13 | |
| 14 | p_dst.uint_addr = (uintptr_t)dest; |
| 15 | p_src.uint_addr = (uintptr_t)src; |
| 16 | |
| 17 | /* Byte copy for unaligned address. check the last bit of address. */ |
| 18 | while (n && (ADDR_WORD_UNALIGNED(p_dst.uint_addr) || |
| 19 | ADDR_WORD_UNALIGNED(p_src.uint_addr))) { |
| 20 | *p_dst.p_byte++ = *p_src.p_byte++; |
| 21 | n--; |
| 22 | } |
| 23 | |
| 24 | /* Quad byte copy for aligned address. */ |
| 25 | while (n >= sizeof(uint32_t)) { |
| 26 | *(p_dst.p_word)++ = *(p_src.p_word)++; |
| 27 | n -= sizeof(uint32_t); |
| 28 | } |
| 29 | |
| 30 | /* Byte copy for the remaining bytes. */ |
| 31 | while (n--) { |
| 32 | *p_dst.p_byte++ = *p_src.p_byte++; |
| 33 | } |
| 34 | |
| 35 | return dest; |
| 36 | } |