MMX strlcpy
OK so i decided i should write a sample using MMX instructions, so i've upgraded the standard strlcpy macro.
so here we have the standard macro packaged with ziron:
//
// Copyright (c) 2011. OverHertz Ltd
//
inline procedure strlcpy(dest, source, len) {
push edi
push esi
push ecx
//cld
edi = $dest;
esi = $source;
ecx = $len;
shr ecx, 2
rep movsd
ecx = $len;
and ecx, 3
rep movsb
pop ecx
pop esi
pop edi
}
and here we have the upgraded MMX macro:
//
// Copyright (c) 2012. OverHertz Ltd
//
inline procedure mmx_strlcpy(dest, source, len) {
push edi
push esi
push ecx
edi = $dest;
esi = $source;
ecx = $len;
shr ecx, 3
@loop:
movq mm0, qword[esi]
movq qword[edi], mm0
esi += 8;
edi += 8;
ecx--;
jnz @loop;
ecx = $len;
and ecx, 3
rep movsb
pop ecx
pop esi
pop edi
emms
}
a sample of using with speed test:
program WIN32CUI 'test';
#include 'ch.zir';
//
// Copyright (c) 2012. OverHertz Ltd
//
inline procedure mmx_strlcpy(dest, source, len) {
push edi
push esi
push ecx
edi = $dest;
esi = $source;
ecx = $len;
shr ecx, 3
@loop:
movq mm0, qword[esi]
movq qword[edi], mm0
esi += 8;
edi += 8;
ecx--;
jnz @loop;
ecx = $len;
and ecx, 3
rep movsb
pop ecx
pop esi
pop edi
emms
}
char str1[2048];
char str2[2048];
edi = GetTickCount();
for (esi = 1 to 5000000) {
mmx_strlcpy(@str2, @str1, 2048);
}
eax = GetTickCount();
eax -= edi;
print('mmx_strlcpy: ', eax, 'ms\r\n');
////////////
edi = GetTickCount();
for (esi = 1 to 5000000) {
strlcpy(@str2, @str1, 2048);
}
eax = GetTickCount();
eax -= edi;
print('strlcpy: ', eax, 'ms\r\n');
//////////
wait_key(0);
ExitProcess(0);
output on core 2 quad 2.4 with 2GB 800mhz GEIL ram:
mmx_strlcpy: 3268ms
strlcpy: 4172ms
[/code]