git: 1544cd332b96 - releng/13.1 - amd64: bring back asm bcmp, shared with memcmp

From: Mateusz Guzik <mjg_at_FreeBSD.org>
Date: Tue, 29 Mar 2022 12:41:06 UTC
The branch releng/13.1 has been updated by mjg:

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

commit 1544cd332b9635a4a3c1821d895c3ad40bc630f9
Author:     Mateusz Guzik <mjg@FreeBSD.org>
AuthorDate: 2022-03-25 15:04:04 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2022-03-29 12:40:32 +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
    Approved by:    re (gjb)
    
    (cherry picked from commit fbc002cb72d2d9bb435cce99630d5d7da9f59390)
    (cherry picked from commit c5890784f6d315c501586b39cf8c0c80b609be5b)
---
 lib/libc/amd64/string/bcmp.S   | 122 ++---------------------------------------
 lib/libc/amd64/string/memcmp.S |  26 ++++++++-
 2 files changed, 28 insertions(+), 120 deletions(-)

diff --git a/lib/libc/amd64/string/bcmp.S b/lib/libc/amd64/string/bcmp.S
index efdc6d33e4dd..f7a4603f6c5a 100644
--- a/lib/libc/amd64/string/bcmp.S
+++ b/lib/libc/amd64/string/bcmp.S
@@ -1,121 +1,7 @@
 /*-
- * Copyright (c) 2018 The FreeBSD Foundation
- *
- * This software was developed by Mateusz Guzik <mjg@FreeBSD.org>
- * under sponsorship from the FreeBSD Foundation.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * $FreeBSD$
+ * Written by Mateusz Guzik <mjg@freebsd.org>
+ * Public domain.
  */
 
-#include <machine/asm.h>
-__FBSDID("$FreeBSD$");
-
-ENTRY(bcmp)
-	cmpq	$16,%rdx
-	jae	5f
-1:
-	testq	%rdx,%rdx
-	je	3f
-	xorl	%ecx,%ecx
-2:
-	movzbl	(%rdi,%rcx,1),%eax
-	movzbl	(%rsi,%rcx,1),%r8d
-	cmpb	%r8b,%al
-	jne	4f
-	addq	$1,%rcx
-	cmpq	%rcx,%rdx
-	jz	3f
-	movzbl	(%rdi,%rcx,1),%eax
-	movzbl	(%rsi,%rcx,1),%r8d
-	cmpb	%r8b,%al
-	jne	4f
-	addq	$1,%rcx
-	cmpq	%rcx,%rdx
-	jz	3f
-	movzbl	(%rdi,%rcx,1),%eax
-	movzbl	(%rsi,%rcx,1),%r8d
-	cmpb	%r8b,%al
-	jne	4f
-	addq	$1,%rcx
-	cmpq	%rcx,%rdx
-	jz	3f
-	movzbl	(%rdi,%rcx,1),%eax
-	movzbl	(%rsi,%rcx,1),%r8d
-	cmpb	%r8b,%al
-	jne	4f
-	addq	$1,%rcx
-	cmpq	%rcx,%rdx
-	jne	2b
-3:
-	xorl	%eax,%eax
-	ret
-4:
-	movl	$1,%eax
-	ret
-5:
-	cmpq	$32,%rdx
-	jae	7f
-6:
-	/*
-	 * 8 bytes
-	 */
-	movq	(%rdi),%r8
-	movq	(%rsi),%r9
-	cmpq	%r8,%r9
-	jne	4b
-	leaq	8(%rdi),%rdi
-	leaq	8(%rsi),%rsi
-	subq	$8,%rdx
-	cmpq	$8,%rdx
-	jae	6b
-	jl	1b
-	jmp	3b
-7:
-	/*
-	 * 32 bytes
-	 */
-	movq	(%rsi),%r8
-	movq	8(%rsi),%r9
-	subq	(%rdi),%r8
-	subq	8(%rdi),%r9
-	or	%r8,%r9
-	jnz	4b
-
-	movq	16(%rsi),%r8
-	movq	24(%rsi),%r9
-	subq	16(%rdi),%r8
-	subq	24(%rdi),%r9
-	or	%r8,%r9
-	jnz	4b
-
-	leaq	32(%rdi),%rdi
-	leaq	32(%rsi),%rsi
-	subq	$32,%rdx
-	cmpq	$32,%rdx
-	jae	7b
-	jnz	1b
-	jmp	3b
-END(bcmp)
-
-	.section .note.GNU-stack,"",%progbits
+#define BCMP
+#include "memcmp.S"
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