git: fbc002cb72d2 - main - amd64: bring back asm bcmp, shared with memcmp

From: Mateusz Guzik <mjg_at_FreeBSD.org>
Date: Sat, 26 Mar 2022 09:13:51 UTC
The branch main has been updated by mjg:

URL: https://cgit.FreeBSD.org/src/commit/?id=fbc002cb72d2d9bb435cce99630d5d7da9f59390

commit fbc002cb72d2d9bb435cce99630d5d7da9f59390
Author:     Mateusz Guzik <mjg@FreeBSD.org>
AuthorDate: 2022-03-25 15:04:04 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2022-03-26 09:10:03 +0000

    amd64: bring back asm bcmp, shared with memcmp
    
    Turns out clang converts "memcmp(foo, bar, len) == 0" and similar to
    bcmp calls.
    
    Reviewed by:    emaste (previous version), jhb (previous version)
    Differential Revision:  https://reviews.freebsd.org/D34673
---
 lib/libc/amd64/string/Makefile.inc |  1 +
 lib/libc/amd64/string/bcmp.S       |  7 +++++++
 lib/libc/amd64/string/bcmp.c       | 16 ----------------
 lib/libc/amd64/string/memcmp.S     | 26 ++++++++++++++++++++++++--
 4 files changed, 32 insertions(+), 18 deletions(-)

diff --git a/lib/libc/amd64/string/Makefile.inc b/lib/libc/amd64/string/Makefile.inc
index b77079afc933..cb370bc6be1c 100644
--- a/lib/libc/amd64/string/Makefile.inc
+++ b/lib/libc/amd64/string/Makefile.inc
@@ -1,6 +1,7 @@
 # $FreeBSD$
 
 MDSRCS+= \
+	bcmp.S \
 	memcmp.S \
 	memcpy.S \
 	memmove.S \
diff --git a/lib/libc/amd64/string/bcmp.S b/lib/libc/amd64/string/bcmp.S
new file mode 100644
index 000000000000..f7a4603f6c5a
--- /dev/null
+++ b/lib/libc/amd64/string/bcmp.S
@@ -0,0 +1,7 @@
+/*-
+ * Written by Mateusz Guzik <mjg@freebsd.org>
+ * Public domain.
+ */
+
+#define BCMP
+#include "memcmp.S"
diff --git a/lib/libc/amd64/string/bcmp.c b/lib/libc/amd64/string/bcmp.c
deleted file mode 100644
index b45176dc2d56..000000000000
--- a/lib/libc/amd64/string/bcmp.c
+++ /dev/null
@@ -1,16 +0,0 @@
-/*-
- * Written by Mateusz Guzik <mjg@freebsd.org>
- * Public domain.
- */
-
-#include <sys/cdefs.h>
-__FBSDID("$FreeBSD$");
-
-#include <string.h>
-
-int
-bcmp(const void *b1, const void *b2, size_t len)
-{
-
-	return (memcmp(b1, b2, len));
-}
diff --git a/lib/libc/amd64/string/memcmp.S b/lib/libc/amd64/string/memcmp.S
index 0c8121f9d885..3e6df7966312 100644
--- a/lib/libc/amd64/string/memcmp.S
+++ b/lib/libc/amd64/string/memcmp.S
@@ -39,7 +39,11 @@ __FBSDID("$FreeBSD$");
 
 #define ALIGN_TEXT      .p2align 4,0x90 /* 16-byte alignment, nop filled */
 
+#ifdef BCMP
+ENTRY(bcmp)
+#else
 ENTRY(memcmp)
+#endif
 	xorl	%eax,%eax
 10:
 	cmpq	$16,%rdx
@@ -143,8 +147,25 @@ ENTRY(memcmp)
 
 /*
  * Mismatch was found.
- *
- * Before we compute it we narrow down the range (16 -> 8 -> 4 bytes).
+ */
+#ifdef BCMP
+	ALIGN_TEXT
+10320016:
+10320000:
+10081608:
+10163224:
+10163216:
+10163208:
+10040804:
+80:
+1:
+	leal	1(%eax),%eax
+	ret
+END(bcmp)
+#else
+/*
+ * We need to compute the difference between strings.
+ * Start with narrowing the range down (16 -> 8 -> 4 bytes).
  */
 	ALIGN_TEXT
 10320016:
@@ -214,5 +235,6 @@ ENTRY(memcmp)
 	subl	%r8d,%eax
 	ret
 END(memcmp)
+#endif
 
 	.section .note.GNU-stack,"",%progbits