git: f4fc317c364f - main - lib/libc/amd64/string: implement strpbrk() through strcspn()

From: Robert Clausecker <fuz_at_FreeBSD.org>
Date: Mon, 25 Dec 2023 14:25:36 UTC
The branch main has been updated by fuz:

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

commit f4fc317c364f2c81ad3d36763d8e5a60393ddbd1
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2023-09-25 21:43:12 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2023-12-25 13:54:58 +0000

    lib/libc/amd64/string: implement strpbrk() through strcspn()
    
    This lets us use our optimised strcspn() routine for strpbrk() calls.
    
    Sponsored by:   The FreeBSD Foundation
    Tested by:      developers@, exp-run
    Approved by:    mjg
    MFC after:      1 month
    MFC to:         stable/14
    PR:             275785
    Differential Revision:  https://reviews.freebsd.org/D41980
---
 lib/libc/amd64/string/Makefile.inc |  1 +
 lib/libc/amd64/string/strcspn.S    | 18 +++++++++-------
 lib/libc/amd64/string/strpbrk.c    | 43 ++++++++++++++++++++++++++++++++++++++
 3 files changed, 54 insertions(+), 8 deletions(-)

diff --git a/lib/libc/amd64/string/Makefile.inc b/lib/libc/amd64/string/Makefile.inc
index 09bf7c8f251e..50c70007e99b 100644
--- a/lib/libc/amd64/string/Makefile.inc
+++ b/lib/libc/amd64/string/Makefile.inc
@@ -14,6 +14,7 @@ MDSRCS+= \
 	strcspn.S \
 	strlen.S \
 	strnlen.c \
+	strpbrk.c \
 	strspn.S \
 	timingsafe_bcmp.S \
 	timingsafe_memcmp.S
diff --git a/lib/libc/amd64/string/strcspn.S b/lib/libc/amd64/string/strcspn.S
index 648635529e5b..7ebd7a847d67 100644
--- a/lib/libc/amd64/string/strcspn.S
+++ b/lib/libc/amd64/string/strcspn.S
@@ -33,13 +33,15 @@
 
 #define ALIGN_TEXT	.p2align 4,0x90 /* 16-byte alignment, nop filled */
 
-ARCHFUNCS(strcspn)
-	ARCHFUNC(strcspn, scalar)
+	.weak strcspn
+	.set strcspn, __strcspn
+ARCHFUNCS(__strcspn)
+	ARCHFUNC(__strcspn, scalar)
 	NOARCHFUNC
-	ARCHFUNC(strcspn, x86_64_v2)
-ENDARCHFUNCS(strcspn)
+	ARCHFUNC(__strcspn, x86_64_v2)
+ENDARCHFUNCS(__strcspn)
 
-ARCHENTRY(strcspn, scalar)
+ARCHENTRY(__strcspn, scalar)
 	push	%rbp			# align stack to enable function call
 	mov	%rsp, %rbp
 	sub	$256, %rsp		# allocate space for lookup table
@@ -122,7 +124,7 @@ ARCHENTRY(strcspn, scalar)
 	sub	(%rsp), %rax		# length of prefix before match
 	leave
 	ret
-ARCHEND(strcspn, scalar)
+ARCHEND(__strcspn, scalar)
 
 	/*
 	 * This kernel uses pcmpistri to do the heavy lifting.
@@ -134,7 +136,7 @@ ARCHEND(strcspn, scalar)
 	 * 17--32: two pcmpistri per 16 bytes of input
 	 *   >=33: fall back to look up table
 	 */
-ARCHENTRY(strcspn, x86_64_v2)
+ARCHENTRY(__strcspn, x86_64_v2)
 	push		%rbp
 	mov		%rsp, %rbp
 	sub		$256, %rsp
@@ -389,6 +391,6 @@ ARCHENTRY(strcspn, x86_64_v2)
 2:	sub	%rdi, %rax		# number of characters preceding match
 	leave
 	ret
-ARCHEND(strcspn, x86_64_v2)
+ARCHEND(__strcspn, x86_64_v2)
 
 	.section .note.GNU-stack,"",%progbits
diff --git a/lib/libc/amd64/string/strpbrk.c b/lib/libc/amd64/string/strpbrk.c
new file mode 100644
index 000000000000..87f587789991
--- /dev/null
+++ b/lib/libc/amd64/string/strpbrk.c
@@ -0,0 +1,43 @@
+/*-
+ * Copyright (c) 2023 The FreeBSD Foundation
+ *
+ * This software was developed by Robert Clausecker <fuz@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
+ */
+
+#include <sys/cdefs.h>
+
+#include <string.h>
+
+size_t __strcspn(const char *, const char *);
+
+char *
+strpbrk(const char *s, const char *charset)
+{
+	size_t loc;
+
+	loc = __strcspn(s, charset);
+
+	return (s[loc] == '\0' ? NULL : (char *)&s[loc]);
+}