Summer Qin | f24dbb5 | 2020-07-23 14:53:54 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2019-2020, 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 tfm_mem_addr_t p_dest, p_src; |
| 13 | |
| 14 | p_dest.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 && (GET_MEM_ADDR_BIT0(p_dest.uint_addr) || |
| 19 | GET_MEM_ADDR_BIT0(p_src.uint_addr))) { |
| 20 | *p_dest.p_byte++ = *p_src.p_byte++; |
| 21 | n--; |
| 22 | } |
| 23 | |
| 24 | /* |
| 25 | * Double byte copy for aligned address. |
| 26 | * Check the 2nd last bit of address. |
| 27 | */ |
| 28 | while (n >= sizeof(uint16_t) && (GET_MEM_ADDR_BIT1(p_dest.uint_addr) || |
| 29 | GET_MEM_ADDR_BIT1(p_src.uint_addr))) { |
| 30 | *(p_dest.p_dbyte)++ = *(p_src.p_dbyte)++; |
| 31 | n -= sizeof(uint16_t); |
| 32 | } |
| 33 | |
| 34 | /* Quad byte copy for aligned address. */ |
| 35 | while (n >= sizeof(uint32_t)) { |
| 36 | *(p_dest.p_qbyte)++ = *(p_src.p_qbyte)++; |
| 37 | n -= sizeof(uint32_t); |
| 38 | } |
| 39 | |
| 40 | /* Byte copy for the remaining bytes. */ |
| 41 | while (n--) { |
| 42 | *p_dest.p_byte++ = *p_src.p_byte++; |
| 43 | } |
| 44 | |
| 45 | return dest; |
| 46 | } |