Skip to content
Low-level programming · Higher-level thinking
Login Register
Forum / Samples

MMX strlcpy

A community discussion post from the original code:zIRON forum.

Colin admin
Posts
928
Threads
49
Location
Bulgaria

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]

Replies

Colin admin
Posts
928
Threads
49
Location
Bulgaria

slightly improved:

//
// Copyright (c) 2012. OverHertz Ltd
//
inline procedure mmx_strlcpy(dest, source, len) {
	push edi
	push esi
	push ecx
	push edx

	edi = $dest;
	esi = $source;	
	ecx = $len;
	edx = ecx;
	
	shr ecx, 6
	if (ecx <> 0) {
		@loop1:
			movq mm0, qword[esi+0]
			movq mm1, qword[esi+8]
			movq mm2, qword[esi+16]
			movq mm3, qword[esi+24]
			movq mm4, qword[esi+32]
			movq mm5, qword[esi+40]
			movq mm6, qword[esi+48]
			movq mm7, qword[esi+56]
			
			movq qword[edi+0], mm0
			movq qword[edi+8], mm1
			movq qword[edi+16], mm2
			movq qword[edi+24], mm3
			movq qword[edi+32], mm4
			movq qword[edi+40], mm5
			movq qword[edi+48], mm6
			movq qword[edi+56], mm7
			
			esi += 64;
			edi += 64;
			
			edx -= 64;
			
			ecx--;
		jnz @loop1;	
	}
	
	ecx = edx;	
	shr ecx, 3
	
	if (ecx <> 0) {
		@loop2:
			movq mm0, qword[esi]
			movq qword[edi], mm0
			
			esi += 8;
			edi += 8;
			
			ecx--;
		jnz @loop2;
	}

	ecx = edx;
	and ecx, 3
	rep movsb

	pop edx
	pop ecx
	pop esi
	pop edi
	
	emms
}

test app:

program WIN32CUI 'test';

#include 'ch.zir';

//
// Copyright (c) 2012. OverHertz Ltd
//
inline procedure mmx_strlcpy2(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
}

//
// Copyright (c) 2012. OverHertz Ltd
//
inline procedure mmx_strlcpy(dest, source, len) {
	push edi
	push esi
	push ecx
	push edx

	edi = $dest;
	esi = $source;	
	ecx = $len;
	edx = ecx;
	
	shr ecx, 6
	if (ecx <> 0) {
		@loop1:
			movq mm0, qword[esi+0]
			movq mm1, qword[esi+8]
			movq mm2, qword[esi+16]
			movq mm3, qword[esi+24]
			movq mm4, qword[esi+32]
			movq mm5, qword[esi+40]
			movq mm6, qword[esi+48]
			movq mm7, qword[esi+56]
			
			movq qword[edi+0], mm0
			movq qword[edi+8], mm1
			movq qword[edi+16], mm2
			movq qword[edi+24], mm3
			movq qword[edi+32], mm4
			movq qword[edi+40], mm5
			movq qword[edi+48], mm6
			movq qword[edi+56], mm7
			
			esi += 64;
			edi += 64;
			
			edx -= 64;
			
			ecx--;
		jnz @loop1;	
	}
	
	ecx = edx;	
	shr ecx, 3
	
	if (ecx <> 0) {
		@loop2:
			movq mm0, qword[esi]
			movq qword[edi], mm0
			
			esi += 8;
			edi += 8;
			
			ecx--;
		jnz @loop2;
	}

	ecx = edx;
	and ecx, 3
	rep movsb

	pop edx
	pop ecx
	pop esi
	pop edi
	
	emms
}

char str1[8192];
char str2[8192];

////////////

edi = GetTickCount();
for (esi = 1 to 500000) {
	strlcpy(@str2, @str1, 8192);
}
eax = GetTickCount();
eax -= edi;

print('strlcpy: ', eax, 'ms\r\n');

//////////

edi = GetTickCount();
for (esi = 1 to 500000) {
	mmx_strlcpy2(@str2, @str1, 8192);
}
eax = GetTickCount();
eax -= edi;

print('mmx_strlcpy (old): ', eax, 'ms\r\n');

//////////

edi = GetTickCount();
for (esi = 1 to 500000) {
	mmx_strlcpy(@str2, @str1, 8192);
}
eax = GetTickCount();
eax -= edi;

print('mmx_strlcpy: ', eax, 'ms\r\n');

wait_key(0);
	  
ExitProcess(0);

output:

strlcpy: 3531ms
mmx_strlcpy (old): 2969ms
mmx_strlcpy: 1328ms

njoy