lkml.org 
[lkml]   [1998]   [Apr]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: faster strcpy()
On Sun, 26 Apr 1998, Richard B. Johnson wrote:

>For all those who are so sure of themselves:

Ahh, it's for me ;)
From my system (Pentium 120 MHz):
Length = 4095
Normal strcpy() Count = 4799
Strange strcpy() Count = 5254
Length = 4031
Normal strcpy() Count = 4895
Strange strcpy() Count = 5368
Length = 3967
Normal strcpy() Count = 4975
Strange strcpy() Count = 5462
Length = 3903
Normal strcpy() Count = 5057
Strange strcpy() Count = 5551
Length = 3839

Why strlen+memcpy would be faster than strcpy? In my libc 5.4.24
strlen is fast and processes 4 bytes at once. strcpy instead is slow.
memcpy is probably fast too. So if strlen+memcpy is faster it's because
strcpy is slow, not because first checking the string length would be
faster generally.

Let me now show my routine. On lengthy strings (like in your test) it's
about 3 times faster than standard strcpy() (in libc 5.4.24).
On shorter strings it's still faster though not so much. Maybe 2 times.
The full package with benchmarking programs and everything can be
downloaded from http://www.ee.oulu.fi/~tuukkat/releases.html


%define B byte

global strcpy2
bits 32

strcpy2:
mov edi,[esp+1*4] ; EDI = destination
mov eax,edi
mov ecx,[esp+2*4] ; ECX = source

test al,11b ; Align source
jz .align4
mov dl,[ecx]
inc ecx
mov [edi],dl
inc edi
or dl,dl
jz .ret
test cl,11b
jz .align4
mov dl,[ecx]
inc ecx
mov [edi],dl
inc edi
or dl,dl
jz .ret
test cl,11b
jz .align4
mov dl,[ecx]
inc ecx
mov [edi],dl
inc edi
or dl,dl
jz .ret
.align4:
sub edi,B 4

.copyloop: mov edx,[ecx] ; Main loop
add edi,B 4
mov ebx,edx
sub ebx,01010101h
add ecx,B 4
xor ebx,edx
and ebx,80808080h
jnz .maybezero
.notzero: mov [edi],edx
jmp .copyloop
.maybezero:
mov esi,edx ; Mycroft's test for zero byte
mov ebx,edx
sub esi,01010101h
not ebx
and ebx,esi
and ebx,80808080h
jz .notzero

or dl,dl ; Copy rest bytes if any
jz .copy1
or dh,dh
jz .copy2
mov ebx,edx
shr ebx,16
or bl,bl
jz .copy3
mov [edi],edx
.ret: ret

.copy1: mov [edi],dl
ret

.copy3: mov [edi+2],bl
.copy2: mov [edi],dx
ret

--
| Tuukka Toivonen <tuukkat@ee.oulu.fi> [PGP public key
| Homepage: http://www.ee.oulu.fi/~tuukkat/ available]
| Try also finger -l tuukkat@ee.oulu.fi
| Studying information engineering at the University of Oulu
+-----------------------------------------------------------


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu

\
 
 \ /
  Last update: 2005-03-22 13:42    [W:0.353 / U:0.032 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site