2012-09-26 15:02:17 -04:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
Copyright(C) Jonas 'Sortie' Termansen 2011, 2012, 2014.
|
2012-09-26 15:02:17 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
This file is part of the Sortix C Library.
|
2012-09-26 15:02:17 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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.
|
2012-09-26 15:02:17 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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.
|
2012-09-26 15:02:17 -04:00
|
|
|
|
2013-07-10 09:26:01 -04:00
|
|
|
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/>.
|
2012-09-26 15:02:17 -04:00
|
|
|
|
2013-06-12 07:35:22 -04:00
|
|
|
string/memcpy.cpp
|
2013-07-10 09:26:01 -04:00
|
|
|
Copy memory between non-overlapping regions.
|
2012-09-26 15:02:17 -04:00
|
|
|
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
inline static
|
|
|
|
void* memcpy_slow(void* restrict dst_ptr,
|
|
|
|
const void* restrict src_ptr,
|
|
|
|
size_t size)
|
2012-09-26 15:02:17 -04:00
|
|
|
{
|
2014-09-25 09:15:36 -04:00
|
|
|
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];
|
2014-09-25 09:15:36 -04:00
|
|
|
return dst_ptr;
|
2012-09-26 15:02:17 -04:00
|
|
|
}
|
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
extern "C"
|
|
|
|
void* memcpy(void* restrict dst_ptr,
|
|
|
|
const void* restrict src_ptr,
|
|
|
|
size_t size)
|
2012-09-26 15:02:17 -04:00
|
|
|
{
|
2014-09-25 09:15:36 -04:00
|
|
|
#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;
|
2014-09-25 09:15:36 -04:00
|
|
|
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;
|
|
|
|
};
|
2014-09-25 09:15:36 -04:00
|
|
|
srcval = (unsigned long) src_ptr;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
unsigned long dstval;
|
2014-09-25 09:15:36 -04:00
|
|
|
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;
|
|
|
|
};
|
2014-09-25 09:15:36 -04:00
|
|
|
dstval = (unsigned long) dst_ptr;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( dst_unalign )
|
2012-09-26 15:02:17 -04:00
|
|
|
{
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( 1 <= size && !(dstval & (1-1)) && (dstval & (2-1)) )
|
2012-12-05 11:34:28 -05:00
|
|
|
*dst8++ = *src8++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 1;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( 2 <= size && !(dstval & (2-1)) && (dstval & (4-1)) )
|
2012-12-05 11:34:28 -05:00
|
|
|
*dst16++ = *src16++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 2;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
|
|
|
#if 8 <= __SIZEOF_LONG__
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( 4 <= size && !(dstval & (4-1)) && (dstval & (8-1)) )
|
2012-12-05 11:34:28 -05:00
|
|
|
*dst32++ = *src32++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 4;
|
2012-12-05 11:34:28 -05:00
|
|
|
#endif
|
2012-09-26 15:02:17 -04:00
|
|
|
}
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04: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++;
|
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= num_copies * sizeof(unsigned long);
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( size )
|
2012-09-26 15:02:17 -04:00
|
|
|
{
|
2012-12-05 11:34:28 -05:00
|
|
|
#if 8 <= __SIZEOF_LONG__
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( 4 <= size )
|
2012-12-05 11:34:28 -05:00
|
|
|
*dst32++ = *src32++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 4;
|
2012-12-05 11:34:28 -05:00
|
|
|
#endif
|
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( 2 <= size )
|
2012-12-05 11:34:28 -05:00
|
|
|
*dst16++ = *src16++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 2;
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
if ( 1 <= size )
|
2012-12-05 11:34:28 -05:00
|
|
|
*dst8++ = *src8++,
|
2014-09-25 09:15:36 -04:00
|
|
|
size -= 1;
|
2012-09-26 15:02:17 -04:00
|
|
|
}
|
2012-12-05 11:34:28 -05:00
|
|
|
|
2014-09-25 09:15:36 -04:00
|
|
|
return dst_ptr;
|
|
|
|
#endif
|
2012-09-26 15:02:17 -04:00
|
|
|
}
|