The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* [PATCH] i386: less assembly in strlen()
@ 2011-12-11 18:13 Alexey Dobriyan
  2011-12-12 18:37 ` [tip:x86/asm] x86/i386: Use less assembly in strlen(), speed things up a bit tip-bot for Alexey Dobriyan
  0 siblings, 1 reply; 3+ messages in thread
From: Alexey Dobriyan @ 2011-12-11 18:13 UTC (permalink / raw)
  To: tglx, mingo, hpa; +Cc: x86, linux-kernel

Current i386 strlen() hardcodes NOT/DEC sequence. DEC is mentioned
to be suboptimal on Core2. So, put only REPNE SCASB sequence in assembly,
compiler can do the rest.

The difference in generated code is like below (MCORE2=y):

	<strlen>:
		push   %edi
		mov    $0xffffffff,%ecx
		mov    %eax,%edi
		xor    %eax,%eax
		repnz scas %es:(%edi),%al
		not    %ecx

	-	dec    %ecx
	-	mov    %ecx,%eax
	+	lea    -0x1(%ecx),%eax

		pop    %edi
		ret

Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---

 arch/x86/lib/string_32.c |    8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

--- a/arch/x86/lib/string_32.c
+++ b/arch/x86/lib/string_32.c
@@ -164,15 +164,13 @@ EXPORT_SYMBOL(strchr);
 size_t strlen(const char *s)
 {
 	int d0;
-	int res;
+	size_t res;
 	asm volatile("repne\n\t"
-		"scasb\n\t"
-		"notl %0\n\t"
-		"decl %0"
+		"scasb"
 		: "=c" (res), "=&D" (d0)
 		: "1" (s), "a" (0), "0" (0xffffffffu)
 		: "memory");
-	return res;
+	return ~res - 1;
 }
 EXPORT_SYMBOL(strlen);
 #endif

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-12-12 18:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-12-11 18:13 [PATCH] i386: less assembly in strlen() Alexey Dobriyan
2011-12-12 18:37 ` [tip:x86/asm] x86/i386: Use less assembly in strlen(), speed things up a bit tip-bot for Alexey Dobriyan
2011-12-12 18:54   ` Linus Torvalds

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox