Re: [Fwd: faster strcpy()]

Richard B. Johnson (root@chaos.analogic.com)
Sun, 26 Apr 1998 20:03:50 -0400 (EDT)


On Sun, 26 Apr 1998, Samuel Leo wrote:

> Actually, I found strcpy() in SCO OpenServer 5 implemente as strlen + memcpy,
> Why glibc2 still use the stupid code.
>
> develop:/tmp# ar xv /usr/lib/libc.a strcpy.o
> x - strcpy.o
> develop:/tmp# dis strcpy.o
> **** DISASSEMBLER ****
>
>
> disassembly for strcpy.o
>
> section .text
> .text
> strcpy
> 0: 8b d7 movl %edi,%edx
> 2: 56 pushl %esi
> 3: 8b 7c 24 0c movl 12(%esp),%edi
> 7: 33 c0 xorl %eax,%eax ; AL = 0
> 9: b9 ff ff ff ff movl $0xffffffff,%ecx
> e: f2 ae repnz scasb %al,(%edi); look for 0
> 10: f7 d1 notl %ecx

; ecx is string length + 1.

> 12: 8b 74 24 0c movl 12(%esp),%esi
> 16: 8b 7c 24 08 movl 8(%esp),%edi
> 1a: 8b c1 movl %ecx,%eax
> 1c: c1 e9 02 shrl $0x2,%ecx

; Find number of longwords first then copy them.

> 1f: f3 a5 repz movsl (%esi),(%edi)
> 21: 8b c8 movl %eax,%ecx
> 23: 81 e1 03 00 00 00 andl $0x3,%ecx
;
; Now copy the remaining bytes ( if any ).
;
> 29: f3 a4 repz movsb (%esi),(%edi)
> 2b: 8b 44 24 08 movl 8(%esp),%eax
> 2f: 5e popl %esi
> 30: 8b fa movl %edx,%edi
> 32: c3 ret
> 33: 90 nop
> develop:/tmp#
>
>

Because this _is_ strlen() plus memcpy(). This is almost exactly
what Microsoft 'C' uses for its inline strcpy. It gets the length first
Then does the copy. It copies long words first, then bytes.

What this saves is the call overhead of calling strlen().

Cheers,
Dick Johnson
***** FILE SYSTEM MODIFIED *****
Penguin : Linux version 2.1.92 on an i586 machine (66.15 BogoMips).
Warning : It's hard to remain at the trailing edge of technology.

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