source: trunk/kernel/libk/memcpy.c @ 440

Last change on this file since 440 was 113, checked in by max@…, 7 years ago

add memcmp in libk; these mem* functions should probably be hal-
specific, for better performance

File size: 2.5 KB
Line 
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_types.h>
25#include <printk.h>
26
27/////////////////////////////////
28void * memcpy( void       * dst,
29               const void * src,
30               uint32_t     size)
31{
32    uint32_t       * wdst = dst;
33    const uint32_t * wsrc = src;
34
35    // word per word copy if both addresses aligned
36    if (!((uint32_t) wdst & 3) && !((uint32_t) wsrc & 3) )
37    {
38        while (size > 3)
39        {
40            *wdst++ = *wsrc++;
41            size -= 4;
42        }
43    }
44
45    unsigned char *cdst = (unsigned char*)wdst;
46    unsigned char *csrc = (unsigned char*)wsrc;
47
48    // byte per byte for last bytes (or not aligned)
49    while (size--)
50    {
51        *cdst++ = *csrc++;
52    }
53    return dst;
54}
55
56//////////////////////////////
57void * memset( void     * dst,
58               uint32_t   val,
59               uint32_t   size)
60{
61    // build 8 bits and 32 bits values
62    uint8_t    byte = (uint8_t)(val & 0xFF);
63    uint32_t   word = (val<<24) | (val<<16) | (val<<8) | val;
64
65    // word per word if address aligned
66    uint32_t * wdst = (uint32_t *)dst;
67
68    if( (((uint32_t)dst) & 0x3) == 0 )
69    {
70        while( size > 3 )
71        {
72            *wdst++ = word;
73            size -= 4;
74        }
75    }
76
77    // byte per byte for last bytes (or not aligned)
78    char * cdst = (char *)wdst;
79
80    while( size-- )
81    {
82        *cdst++ = byte;
83    }
84
85    return dst;
86}
87
88//////////////////////////////
89int memcmp( const void * s1,
90            const void * s2,
91            uint32_t     n)
92{
93    const uint8_t * cs1 = s1;
94    const uint8_t * cs2 = s2;
95
96    while (n > 0)
97    {
98        if (*cs1++ != *cs2++)
99            return (*--cs1 - *--cs2);
100        n--;
101    }
102    return 0;
103}
104
Note: See TracBrowser for help on using the repository browser.