public inbox for [email protected]
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <[email protected]>
To: [email protected]
Cc: [email protected], [email protected], [email protected],
	"Ammar Faizi" <[email protected]>,
	"Alviro Iskandar Setiawan" <[email protected]>,
	"Willy Tarreau" <[email protected]>,
	"Thomas Weißschuh" <[email protected]>
Subject: [PATCH nolibc 07/19] tools/nolibc: x86-64: Use `rep stosb` for `memset()`
Date: Thu, 12 Oct 2023 12:32:21 -0700	[thread overview]
Message-ID: <[email protected]> (raw)
In-Reply-To: <b34ce3cf-3fcc-4eb0-a658-229c197455ef@paulmck-laptop>

From: Ammar Faizi <[email protected]>

Simplify memset() on the x86-64 arch.

The x86-64 arch has a 'rep stosb' instruction, which can perform
memset() using only a single instruction, given:

    %al  = value (just like the second argument of memset())
    %rdi = destination
    %rcx = length

Before this patch:
```
  00000000000010c9 <memset>:
    10c9: 48 89 f8              mov    %rdi,%rax
    10cc: 48 85 d2              test   %rdx,%rdx
    10cf: 74 0e                 je     10df <memset+0x16>
    10d1: 31 c9                 xor    %ecx,%ecx
    10d3: 40 88 34 08           mov    %sil,(%rax,%rcx,1)
    10d7: 48 ff c1              inc    %rcx
    10da: 48 39 ca              cmp    %rcx,%rdx
    10dd: 75 f4                 jne    10d3 <memset+0xa>
    10df: c3                    ret
```

After this patch:
```
  0000000000001511 <memset>:
    1511: 96                    xchg   %eax,%esi
    1512: 48 89 d1              mov    %rdx,%rcx
    1515: 57                    push   %rdi
    1516: f3 aa                 rep stos %al,%es:(%rdi)
    1518: 58                    pop    %rax
    1519: c3                    ret
```

v2:
  - Use pushq %rdi / popq %rax (Alviro).
  - Use xchg %eax, %esi (Willy).

Link: https://lore.kernel.org/lkml/[email protected]
Suggested-by: Alviro Iskandar Setiawan <[email protected]>
Suggested-by: Willy Tarreau <[email protected]>
Signed-off-by: Ammar Faizi <[email protected]>
Reviewed-by: Alviro Iskandar Setiawan <[email protected]>
Signed-off-by: Willy Tarreau <[email protected]>
Signed-off-by: Thomas Weißschuh <[email protected]>
---
 tools/include/nolibc/arch-x86_64.h | 13 +++++++++++++
 tools/include/nolibc/string.h      |  2 ++
 2 files changed, 15 insertions(+)

diff --git a/tools/include/nolibc/arch-x86_64.h b/tools/include/nolibc/arch-x86_64.h
index aece7d895153..68609f421934 100644
--- a/tools/include/nolibc/arch-x86_64.h
+++ b/tools/include/nolibc/arch-x86_64.h
@@ -179,6 +179,9 @@ void *memmove(void *dst, const void *src, size_t len);
 #define NOLIBC_ARCH_HAS_MEMCPY
 void *memcpy(void *dst, const void *src, size_t len);
 
+#define NOLIBC_ARCH_HAS_MEMSET
+void *memset(void *dst, int c, size_t len);
+
 __asm__ (
 ".section .text.nolibc_memmove_memcpy\n"
 ".weak memmove\n"
@@ -200,6 +203,16 @@ __asm__ (
 	"rep movsb\n\t"
 	"cld\n\t"
 	"retq\n"
+
+".section .text.nolibc_memset\n"
+".weak memset\n"
+"memset:\n"
+	"xchgl %eax, %esi\n\t"
+	"movq  %rdx, %rcx\n\t"
+	"pushq %rdi\n\t"
+	"rep stosb\n\t"
+	"popq  %rax\n\t"
+	"retq\n"
 );
 
 #endif /* _NOLIBC_ARCH_X86_64_H */
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
index 6eca267ec6fa..1bad6121ef8c 100644
--- a/tools/include/nolibc/string.h
+++ b/tools/include/nolibc/string.h
@@ -84,6 +84,7 @@ void *memcpy(void *dst, const void *src, size_t len)
 }
 #endif /* #ifndef NOLIBC_ARCH_HAS_MEMCPY */
 
+#ifndef NOLIBC_ARCH_HAS_MEMSET
 /* might be ignored by the compiler without -ffreestanding, then found as
  * missing.
  */
@@ -99,6 +100,7 @@ void *memset(void *dst, int b, size_t len)
 	}
 	return dst;
 }
+#endif /* #ifndef NOLIBC_ARCH_HAS_MEMSET */
 
 static __attribute__((unused))
 char *strchr(const char *s, int c)
-- 
2.40.1


  parent reply	other threads:[~2023-10-12 19:32 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-12 19:32 [PATCH nolibc 0/19] Updates to nolibc for v6.7 (and three for v6.6) Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 01/19] tools/nolibc: i386: Fix a stack misalign bug on _start Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 02/19] MAINTAINERS: nolibc: update tree location Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 03/19] tools/nolibc: mark start_c as weak Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 04/19] tools/nolibc: add stdarg.h header Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 05/19] selftests/nolibc: use -nostdinc for nolibc-test Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 06/19] tools/nolibc: x86-64: Use `rep movsb` for `memcpy()` and `memmove()` Paul E. McKenney
2023-10-12 19:32 ` Paul E. McKenney [this message]
2023-10-12 19:32 ` [PATCH nolibc 08/19] tools/nolibc: string: Remove the `_nolibc_memcpy_down()` function Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 09/19] tools/nolibc: string: Remove the `_nolibc_memcpy_up()` function Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 10/19] selftests/nolibc: libc-test: avoid -Wstringop-overflow warnings Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 11/19] selftests/nolibc: don't embed initramfs into kernel image Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 12/19] selftests/nolibc: allow building i386 with multiarch compiler Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 13/19] tools/nolibc: avoid unused parameter warnings for ENOSYS fallbacks Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 14/19] tools/nolibc: don't define new syscall number Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 15/19] tools/nolibc: automatically detect necessity to use pselect6 Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 16/19] tools/nolibc: drop test for getauxval(AT_PAGESZ) Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 17/19] tools/nolibc: add support for constructors and destructors Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 18/19] selftests/nolibc: use qemu-system-ppc64 for ppc64le Paul E. McKenney
2023-10-12 19:32 ` [PATCH nolibc 19/19] selftests/nolibc: add tests for multi-object linkage Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox