| 1 | /* |
|---|
| 2 | * memcpy.c - architecture independent memory copy functions |
|---|
| 3 | * |
|---|
| 4 | * Author ALain Greiner (2016) |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| 7 | * |
|---|
| 8 | * This file is part of ALMOS-MKH. |
|---|
| 9 | * |
|---|
| 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
|---|
| 11 | * under the terms of the GNU General Public License as published by |
|---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
|---|
| 13 | * |
|---|
| 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
|---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 17 | * General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
|---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | #include <hal_kernel_types.h> |
|---|
| 25 | #include <printk.h> |
|---|
| 26 | #include <memcpy.h> |
|---|
| 27 | |
|---|
| 28 | ///////////////////////////////// |
|---|
| 29 | void * memcpy( void * dst, |
|---|
| 30 | const void * src, |
|---|
| 31 | uint32_t size) |
|---|
| 32 | { |
|---|
| 33 | uint32_t * wdst = dst; |
|---|
| 34 | const uint32_t * wsrc = src; |
|---|
| 35 | |
|---|
| 36 | // word per word copy if both addresses aligned |
|---|
| 37 | if (!((uint32_t) wdst & 3) && !((uint32_t) wsrc & 3) ) |
|---|
| 38 | { |
|---|
| 39 | while (size > 3) |
|---|
| 40 | { |
|---|
| 41 | *wdst++ = *wsrc++; |
|---|
| 42 | size -= 4; |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | unsigned char *cdst = (unsigned char*)wdst; |
|---|
| 47 | unsigned char *csrc = (unsigned char*)wsrc; |
|---|
| 48 | |
|---|
| 49 | // byte per byte for last bytes (or not aligned) |
|---|
| 50 | while (size--) |
|---|
| 51 | { |
|---|
| 52 | *cdst++ = *csrc++; |
|---|
| 53 | } |
|---|
| 54 | return dst; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | ////////////////////////////// |
|---|
| 58 | void * memset( void * dst, |
|---|
| 59 | uint32_t val, |
|---|
| 60 | uint32_t size) |
|---|
| 61 | { |
|---|
| 62 | // build 8 bits and 32 bits values |
|---|
| 63 | uint8_t byte = (uint8_t)(val & 0xFF); |
|---|
| 64 | uint32_t word = (val<<24) | (val<<16) | (val<<8) | val; |
|---|
| 65 | |
|---|
| 66 | // word per word if address aligned |
|---|
| 67 | uint32_t * wdst = (uint32_t *)dst; |
|---|
| 68 | |
|---|
| 69 | if( (((uint32_t)dst) & 0x3) == 0 ) |
|---|
| 70 | { |
|---|
| 71 | while( size > 3 ) |
|---|
| 72 | { |
|---|
| 73 | *wdst++ = word; |
|---|
| 74 | size -= 4; |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | // byte per byte for last bytes (or not aligned) |
|---|
| 79 | char * cdst = (char *)wdst; |
|---|
| 80 | |
|---|
| 81 | while( size-- ) |
|---|
| 82 | { |
|---|
| 83 | *cdst++ = byte; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | return dst; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | ////////////////////////////// |
|---|
| 90 | int memcmp( const void * s1, |
|---|
| 91 | const void * s2, |
|---|
| 92 | uint32_t n) |
|---|
| 93 | { |
|---|
| 94 | const uint8_t * cs1 = s1; |
|---|
| 95 | const uint8_t * cs2 = s2; |
|---|
| 96 | |
|---|
| 97 | while (n > 0) |
|---|
| 98 | { |
|---|
| 99 | if (*cs1++ != *cs2++) |
|---|
| 100 | return (*--cs1 - *--cs2); |
|---|
| 101 | n--; |
|---|
| 102 | } |
|---|
| 103 | return 0; |
|---|
| 104 | } |
|---|
| 105 | |
|---|