From nobody Mon Dec 13 00:28:10 2021 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id A4A5718F3E25; Mon, 13 Dec 2021 00:28:11 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4JC2RG2dF3z3Qp9; Mon, 13 Dec 2021 00:28:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 279E12751; Mon, 13 Dec 2021 00:28:10 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 1BD0SAA2059862; Mon, 13 Dec 2021 00:28:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 1BD0SAOu059861; Mon, 13 Dec 2021 00:28:10 GMT (envelope-from git) Date: Mon, 13 Dec 2021 00:28:10 GMT Message-Id: <202112130028.1BD0SAOu059861@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: bf4a70bb405c - stable/13 - libc: Use musl's optimized strchr and strchrnul List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: bf4a70bb405cbc5e0ecf1babe0e55c220964e46c Auto-Submitted: auto-generated ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1639355290; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=nCBi6H4gWsFvicUslQXBDeugMvjWw8KSjWGzUi0SNVc=; b=o4I4IJM7/Rbd7SXyXHcJsLJizCioa6lsQ64D/O3Xmt1nvpWPNaf0/b419qDzCiVdrxjGyi ddCMG/ETuqlb/d0wCd+OTx1k0Gu/usvmQNJBV09e+EIm+H9W0pkszgp76WPYb8aFu61VWT xRww/Kjatl1wqgD/xOJlhzflDcdAFWHYux2v+PeVXF7ykQzSxLZCIs0niWW21uiKhIHLx5 lxIhKncTeAcDgnpW51ovA540KHO7Ftk7RRDMyNH4A4eIngLO0Hx6aVGDR8AdTYUddMotjT T5ao4dI8iyYAqZ/8djkfzyh57v0QfSoiQegLlrWsl5/mMpCtFRRhs6wfW7MLxQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1639355290; a=rsa-sha256; cv=none; b=KN/3DgDvvrGTyMcZfjfz7DYy0i+K/CSXVag1GTui9dmKxl0Gf6Rs8PzHIR91KUKYmKDP+p rhCFcuavp46b3eIrtug2HIPKRsZLXqeVH4ia7BfldoXl8Xm6t0uP+AHag5JLcMiOLJmser MmoG6NTNeHMDshzurlu34dEeJbbfacyumaw2O97O3ZZgwlmYeURsfxM7tqLIPjcIW9IOFo 8bwEQdGz0gICbnOLahlnqiV1n+HXAMLITYbt/pNsuSM3oUVDNHhCTfbTSvushdtwo9FpfW 3iP7Lt/PI1svo+m6sgcdjLNipiRSFTA8fSAxZZbKmpo4ezI3uGUdIOpWiP8HQQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=bf4a70bb405cbc5e0ecf1babe0e55c220964e46c commit bf4a70bb405cbc5e0ecf1babe0e55c220964e46c Author: Ed Maste AuthorDate: 2021-03-02 01:57:36 +0000 Commit: Ed Maste CommitDate: 2021-12-12 22:32:33 +0000 libc: Use musl's optimized strchr and strchrnul Parentheses added to HASZERO macro to avoid a GCC warning, and formatted with clang-format as we have adopted these and don't consider them 'contrib' code. Obtained from: musl (snapshot at commit 4d0a82170a25) Reviewed by: kib (libc integration), mjg (both earlier) MFC after: 1 month Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D17630 (cherry picked from commit 7f72497ef756ff7d03d5560c2d1c1f92f73fcb52) --- lib/libc/string/strchr.c | 65 +++++++++++----------------- lib/libc/string/strchrnul.c | 75 +++++++++++++++++++-------------- libexec/rtld-elf/rtld-libc/Makefile.inc | 4 +- stand/libsa/Makefile | 2 +- 4 files changed, 70 insertions(+), 76 deletions(-) diff --git a/lib/libc/string/strchr.c b/lib/libc/string/strchr.c index 61244da4519c..4a20ea658252 100644 --- a/lib/libc/string/strchr.c +++ b/lib/libc/string/strchr.c @@ -1,56 +1,39 @@ /*- - * SPDX-License-Identifier: BSD-3-Clause + * SPDX-License-Identifier: MIT * - * Copyright (c) 1990, 1993 - * The Regents of the University of California. All rights reserved. + * Copyright (c) 2005-2014 Rich Felker, et al. * - * 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. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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. + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - -#if defined(LIBC_SCCS) && !defined(lint) -static char sccsid[] = "@(#)index.c 8.1 (Berkeley) 6/4/93"; -#endif /* LIBC_SCCS and not lint */ #include __FBSDID("$FreeBSD$"); -#include #include +char *__strchrnul(const char *, int); + char * -strchr(const char *p, int ch) +strchr(const char *s, int c) { - char c; - - c = ch; - for (;; ++p) { - if (*p == c) - return ((char *)p); - if (*p == '\0') - return (NULL); - } - /* NOTREACHED */ + char *r = __strchrnul(s, c); + return *(unsigned char *)r == (unsigned char)c ? r : NULL; } __weak_reference(strchr, index); diff --git a/lib/libc/string/strchrnul.c b/lib/libc/string/strchrnul.c index 1c777be1854f..e1fb83886042 100644 --- a/lib/libc/string/strchrnul.c +++ b/lib/libc/string/strchrnul.c @@ -1,51 +1,62 @@ /*- - * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * SPDX-License-Identifier: MIT * - * Copyright (c) 2013 Niclas Zeising + * Copyright (c) 2005-2014 Rich Felker, et al. * - * 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. + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: * - * 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. + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - #include __FBSDID("$FreeBSD$"); -#include +#include +#include #include -__weak_reference(__strchrnul, strchrnul); +#define ALIGN (sizeof(size_t)) +#define ONES ((size_t)-1 / UCHAR_MAX) +#define HIGHS (ONES * (UCHAR_MAX / 2 + 1)) +#define HASZERO(x) (((x)-ONES) & ~(x)&HIGHS) char *__strchrnul(const char *, int); char * -__strchrnul(const char *p, int ch) +__strchrnul(const char *s, int c) { - char c; + c = (unsigned char)c; + if (!c) + return (char *)s + strlen(s); - c = ch; - for (;; ++p) { - if (*p == c || *p == '\0') - return ((char *)p); - } - /* NOTREACHED */ +#ifdef __GNUC__ + typedef size_t __attribute__((__may_alias__)) word; + const word *w; + for (; (uintptr_t)s % ALIGN; s++) + if (!*s || *(unsigned char *)s == c) + return (char *)s; + size_t k = ONES * c; + for (w = (void *)s; !HASZERO(*w) && !HASZERO(*w ^ k); w++) + ; + s = (void *)w; +#endif + for (; *s && *(unsigned char *)s != c; s++) + ; + return (char *)s; } +__weak_reference(__strchrnul, strchrnul); diff --git a/libexec/rtld-elf/rtld-libc/Makefile.inc b/libexec/rtld-elf/rtld-libc/Makefile.inc index e3c7ee8ca5cf..ade2dc962aa2 100644 --- a/libexec/rtld-elf/rtld-libc/Makefile.inc +++ b/libexec/rtld-elf/rtld-libc/Makefile.inc @@ -41,8 +41,8 @@ CFLAGS.errlst.c+=-I${LIBC_SRCTOP}/include # Use the string and memory .o files from libc instead of rebuilding them (they # might be using optimized assembly and duplicating that logic here is awkward). _libc_string_objects= bcmp bcopy bzero memset memchr memcmp memcpy memmove \ - stpncpy strcat strchr strcmp stpcpy strcpy strcspn strdup strlcat strlcpy \ - strlen strncmp strncpy strrchr strsep strspn strstr strtok + stpncpy strcat strchr strchrnul strcmp stpcpy strcpy strcspn strdup \ + strlcat strlcpy strlen strncmp strncpy strrchr strsep strspn strstr strtok # Also use all the syscall .o files from libc_nossp_pic: _libc_other_objects= sigsetjmp lstat stat fstat fstatat fstatfs syscall \ cerror geteuid getegid sigfastblock munmap mprotect \ diff --git a/stand/libsa/Makefile b/stand/libsa/Makefile index 6fe916405fe4..838fefb260a8 100644 --- a/stand/libsa/Makefile +++ b/stand/libsa/Makefile @@ -29,7 +29,7 @@ SRCS+= ntoh.c .PATH: ${LIBCSRC}/string SRCS+= bcmp.c bcopy.c bzero.c ffs.c fls.c \ memccpy.c memchr.c memcmp.c memcpy.c memmove.c memset.c \ - strcat.c strchr.c strcmp.c strcpy.c stpcpy.c stpncpy.c \ + strcat.c strchr.c strchrnul.c strcmp.c strcpy.c stpcpy.c stpncpy.c \ strcspn.c strlcat.c strlcpy.c strlen.c strncat.c strncmp.c strncpy.c \ strnlen.c strpbrk.c strrchr.c strsep.c strspn.c strstr.c strtok.c swab.c