Skip to content

Instantly share code, notes, and snippets.

@WilliamBundy
Last active May 6, 2017 02:48
Show Gist options
  • Save WilliamBundy/aa2c434a54b1ec34c94ba01b7349b3ca to your computer and use it in GitHub Desktop.
Save WilliamBundy/aa2c434a54b1ec34c94ba01b7349b3ca to your computer and use it in GitHub Desktop.
/*
A memcpy replacement; uses SSE2 and _mm_lddqu_si128 from SSE3
It's okay, I guess.
Abuses rep movsb for sizes > 1024b and < 2mb--
--might not work very well on pre Sandy Bridge Intel or AMD in general.
On Skylake, it's about as fast as Microsoft's implementation.
Written by William Bundy
Thanks to Martin Wardener (@d7samurai), Jeroen van Rijn (@J_vanRijn), and Martins Mozeiko (@mmozeiko)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#include <intrin.h>
#include <xmmintrin.h>
#include <emmintrin.h>
#include <stdint.h>
/* Feel free to use find-and-replace to use your own types */
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
typedef ptrdiff_t isize;
typedef size_t usize;
typedef __m128 vf128;
typedef __m128i vi128;
#define wbssecpy_mv(a, b, d) \
head = _mm_lddqu_si128((vi128*)(src + (a))); \
head2 = _mm_lddqu_si128((vi128*)(src + (b))); \
tail = _mm_lddqu_si128((vi128*)(src + size - (b))); \
tail2 = _mm_lddqu_si128((vi128*)(src + size - (d))); \
_mm_storeu_si128((vi128*)(dst + (a)), head); \
_mm_storeu_si128((vi128*)(dst + size - (b)), tail); \
_mm_storeu_si128((vi128*)(dst + (b)), head2); \
_mm_storeu_si128((vi128*)(dst + size - (d)), tail2);
void* memcpy(void *dest, const void *source, size_t size)
{
u8* dst = dest;
const u8* src = source;
if(size <= 512) {
if(size <= 16) {
u32 src4;
u16 src2;
u8 src1;
u64 head, tail;
switch(size) {
case 0: break;
case 1: *dst = *src; break;
case 2: *(u16*)dst = *(u16*)src; break;
case 3: src2 = *(u16*)src;
src1 = *(src + 2);
*(u16*)dst = *(u16*)src;
*(dst + 2) = src1;
break;
case 4: *(u32*)dst = *(u32*)src; break;
case 5: src4 = *(u32*)src;
src1 = *(src + 4);
*(u32*)dst = src4;
*(dst + 4) = src1;
break;
case 6: src4 = *(u32*)src;
src2 = *(u16*)(src + 4);
*(u32*)dst = src4;
*(u16*)(dst + 4) = src2;
break;
case 7: src4 = *(u32*)src;
src2 = *(u16*)(src + 4);
src1 = *(src + 6);
*(u32*)dst = src4;
*(u16*)(dst + 4) = src2;
*(dst + 6) = src1;
break;
case 8: *(u64*)dst = *(u64*)src; break;
default: head = *(u64*)src;
tail = *(u64*)(src + size - 8);
*(u64*)dst = head;
*(u64*)(dst + size - 8) = tail;
break;
}
} else if(size <= 32) {
vi128 head = _mm_lddqu_si128((vi128*)src);
vi128 tail = _mm_lddqu_si128((vi128*)(src + size - 16));
_mm_storeu_si128((vi128*)dst, head);
_mm_storeu_si128((vi128*)(dst + size - 16), tail);
} else if(size <= 64) {
vi128 head, head2, tail, tail2;
wbssecpy_mv( 0, 16, 32);
} else if(size <= 128) {
vi128 head, head2, tail, tail2;
wbssecpy_mv( 0, 16, 32);
wbssecpy_mv(32, 48, 64);
} else if(size <= 256) {
vi128 head, head2, tail, tail2;
wbssecpy_mv( 0, 16, 32);
wbssecpy_mv(32, 48, 64);
wbssecpy_mv(64, 80, 96);
wbssecpy_mv(96, 112, 128);
} else if(size <= 512) {
vi128 head, head2, tail, tail2;
wbssecpy_mv(0, 16, 32);
wbssecpy_mv(32, 48, 64);
wbssecpy_mv(64, 80, 96);
wbssecpy_mv(96, 112, 128);
wbssecpy_mv(128, 144, 160);
wbssecpy_mv(160, 176, 192);
wbssecpy_mv(192, 208, 224);
wbssecpy_mv(224, 240, 256);
}
} else if(size <= (1 << 21)) {
__movsb(dst, src, size);
} else {
i32 dstalign = (((usize)dst + 15) & -16) == (usize)dst;
i32 srcalign = (((usize)src + 15) & -16) == (usize)src;
const vi128* srcm = (vi128*)src;
vi128* dstm = (vi128*)dst;
i64 itercount = size >> 4;
usize end = (usize)src + size - 32;
if(dstalign && srcalign && size > 32768) {
switch(itercount % 8) {
case 0: _mm_stream_si128(dstm++, _mm_load_si128(srcm++));
case 7: _mm_stream_si128(dstm++, _mm_load_si128(srcm++));
case 6: _mm_stream_si128(dstm++, _mm_load_si128(srcm++));
case 5: _mm_stream_si128(dstm++, _mm_load_si128(srcm++));
case 4: _mm_stream_si128(dstm++, _mm_load_si128(srcm++));
case 3: _mm_stream_si128(dstm++, _mm_load_si128(srcm++));
case 2: _mm_stream_si128(dstm++, _mm_load_si128(srcm++));
case 1: _mm_stream_si128(dstm++, _mm_load_si128(srcm++));
}
while((usize)srcm + 128 < end) {
_mm_stream_si128(dstm, _mm_load_si128(srcm));
_mm_stream_si128(dstm + 1, _mm_load_si128(srcm + 1));
_mm_stream_si128(dstm + 2, _mm_load_si128(srcm + 2));
_mm_stream_si128(dstm + 3, _mm_load_si128(srcm + 3));
_mm_stream_si128(dstm + 4, _mm_load_si128(srcm + 4));
_mm_stream_si128(dstm + 5, _mm_load_si128(srcm + 5));
_mm_stream_si128(dstm + 6, _mm_load_si128(srcm + 6));
_mm_stream_si128(dstm + 7, _mm_load_si128(srcm + 7));
dstm += 8;
srcm += 8;
}
} else {
switch(itercount % 8) {
case 0: _mm_storeu_si128(dstm++, _mm_lddqu_si128(srcm++));
case 7: _mm_storeu_si128(dstm++, _mm_lddqu_si128(srcm++));
case 6: _mm_storeu_si128(dstm++, _mm_lddqu_si128(srcm++));
case 5: _mm_storeu_si128(dstm++, _mm_lddqu_si128(srcm++));
case 4: _mm_storeu_si128(dstm++, _mm_lddqu_si128(srcm++));
case 3: _mm_storeu_si128(dstm++, _mm_lddqu_si128(srcm++));
case 2: _mm_storeu_si128(dstm++, _mm_lddqu_si128(srcm++));
case 1: _mm_storeu_si128(dstm++, _mm_lddqu_si128(srcm++));
}
while((usize)srcm + 128 < end) {
_mm_storeu_si128(dstm, _mm_lddqu_si128(srcm));
_mm_storeu_si128(dstm + 1, _mm_lddqu_si128(srcm + 1));
_mm_storeu_si128(dstm + 2, _mm_lddqu_si128(srcm + 2));
_mm_storeu_si128(dstm + 3, _mm_lddqu_si128(srcm + 3));
_mm_storeu_si128(dstm + 4, _mm_lddqu_si128(srcm + 4));
_mm_storeu_si128(dstm + 5, _mm_lddqu_si128(srcm + 5));
_mm_storeu_si128(dstm + 6, _mm_lddqu_si128(srcm + 6));
_mm_storeu_si128(dstm + 7, _mm_lddqu_si128(srcm + 7));
dstm += 8;
srcm += 8;
}
}
while((usize)srcm < end) {
_mm_storeu_si128(dstm++, _mm_lddqu_si128(srcm++));
}
vi128 tail = _mm_lddqu_si128((vi128*)(src + size - 16));
_mm_storeu_si128((vi128*)(dst + size - 16), tail);
tail = _mm_lddqu_si128((vi128*)(src + size - 32));
_mm_storeu_si128((vi128*)(dst + size - 32), tail);
}
return dst;
}
#pragma function(memcpy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment