1
0
Fork 0
mirror of https://gitlab.com/sortix/sortix.git synced 2023-02-13 20:55:38 -05:00
sortix--sortix/libc/string/memcpy.cpp

120 lines
3.2 KiB
C++
Raw Normal View History

/*******************************************************************************
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014.
This file is part of the Sortix C Library.
The Sortix C Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
The Sortix C Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License for more details.
You should have received a copy of the GNU Lesser General Public License
along with the Sortix C Library. If not, see <http://www.gnu.org/licenses/>.
string/memcpy.cpp
Copy memory between non-overlapping regions.
*******************************************************************************/
#include <stdint.h>
#include <string.h>
inline static
void* memcpy_slow(void* restrict dst_ptr,
const void* restrict src_ptr,
size_t size)
{
unsigned char* restrict dst = (unsigned char* restrict) dst_ptr;
const unsigned char* restrict src = (const unsigned char* restrict) src_ptr;
for ( size_t i = 0; i < size; i++ )
2012-12-05 11:34:28 -05:00
dst[i] = src[i];
return dst_ptr;
}
extern "C"
void* memcpy(void* restrict dst_ptr,
const void* restrict src_ptr,
size_t size)
{
#if 8 < __SIZEOF_LONG__
#warning "you should add support for your unexpectedly large unsigned long."
return memcpy_slow(dst_ptr, src_ptr, size);
#else
unsigned long unalign_mask = sizeof(unsigned long) - 1;
unsigned long src_unalign = (unsigned long) src_ptr & unalign_mask;
unsigned long dst_unalign = (unsigned long) dst_ptr & unalign_mask;
if ( src_unalign != dst_unalign )
return memcpy_slow(dst_ptr, src_ptr, size);
2012-12-05 11:34:28 -05:00
union
{
unsigned long srcval;
const unsigned char* restrict src8;
2012-12-05 11:34:28 -05:00
const uint16_t* restrict src16;
const uint32_t* restrict src32;
const uint64_t* restrict src64;
const unsigned long* restrict srcul;
};
srcval = (unsigned long) src_ptr;
2012-12-05 11:34:28 -05:00
union
{
unsigned long dstval;
unsigned char* restrict dst8;
2012-12-05 11:34:28 -05:00
uint16_t* restrict dst16;
uint32_t* restrict dst32;
uint64_t* restrict dst64;
unsigned long* restrict dstul;
};
dstval = (unsigned long) dst_ptr;
2012-12-05 11:34:28 -05:00
if ( dst_unalign )
{
if ( 1 <= size && !(dstval & (1-1)) && (dstval & (2-1)) )
2012-12-05 11:34:28 -05:00
*dst8++ = *src8++,
size -= 1;
2012-12-05 11:34:28 -05:00
if ( 2 <= size && !(dstval & (2-1)) && (dstval & (4-1)) )
2012-12-05 11:34:28 -05:00
*dst16++ = *src16++,
size -= 2;
2012-12-05 11:34:28 -05:00
#if 8 <= __SIZEOF_LONG__
if ( 4 <= size && !(dstval & (4-1)) && (dstval & (8-1)) )
2012-12-05 11:34:28 -05:00
*dst32++ = *src32++,
size -= 4;
2012-12-05 11:34:28 -05:00
#endif
}
2012-12-05 11:34:28 -05:00
size_t num_copies = size / sizeof(unsigned long);
for ( size_t i = 0; i < num_copies; i++ )
2012-12-05 11:34:28 -05:00
*dstul++ = *srcul++;
size -= num_copies * sizeof(unsigned long);
2012-12-05 11:34:28 -05:00
if ( size )
{
2012-12-05 11:34:28 -05:00
#if 8 <= __SIZEOF_LONG__
if ( 4 <= size )
2012-12-05 11:34:28 -05:00
*dst32++ = *src32++,
size -= 4;
2012-12-05 11:34:28 -05:00
#endif
if ( 2 <= size )
2012-12-05 11:34:28 -05:00
*dst16++ = *src16++,
size -= 2;
2012-12-05 11:34:28 -05:00
if ( 1 <= size )
2012-12-05 11:34:28 -05:00
*dst8++ = *src8++,
size -= 1;
}
2012-12-05 11:34:28 -05:00
return dst_ptr;
#endif
}