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
This commit is contained in:
Ed Maste 2021-03-01 20:57:36 -05:00
parent 8742817ba6
commit 7f72497ef7
4 changed files with 71 additions and 77 deletions

View File

@ -1,56 +1,39 @@
/*- /*-
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: MIT
* *
* Copyright (c) 1990, 1993 * Copyright (c) 2005-2014 Rich Felker, et al.
* The Regents of the University of California. All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Permission is hereby granted, free of charge, to any person obtaining
* modification, are permitted provided that the following conditions * a copy of this software and associated documentation files (the
* are met: * "Software"), to deal in the Software without restriction, including
* 1. Redistributions of source code must retain the above copyright * without limitation the rights to use, copy, modify, merge, publish,
* notice, this list of conditions and the following disclaimer. * distribute, sublicense, and/or sell copies of the Software, and to
* 2. Redistributions in binary form must reproduce the above copyright * permit persons to whom the Software is furnished to do so, subject to
* notice, this list of conditions and the following disclaimer in the * the following conditions:
* 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.
* *
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * The above copyright notice and this permission notice shall be
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * included in all copies or substantial portions of the Software.
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* SUCH DAMAGE.
*/ */
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)index.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include <stddef.h>
#include <string.h> #include <string.h>
char * char *__strchrnul(const char *, int);
strchr(const char *p, int ch)
{
char c;
c = ch; char *
for (;; ++p) { strchr(const char *s, int c)
if (*p == c) {
return ((char *)p); char *r = __strchrnul(s, c);
if (*p == '\0') return *(unsigned char *)r == (unsigned char)c ? r : NULL;
return (NULL);
}
/* NOTREACHED */
} }
__weak_reference(strchr, index); __weak_reference(strchr, index);

View File

@ -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 * Permission is hereby granted, free of charge, to any person obtaining
* modification, are permitted provided that the following conditions * a copy of this software and associated documentation files (the
* are met: * "Software"), to deal in the Software without restriction, including
* 1. Redistributions of source code must retain the above copyright * without limitation the rights to use, copy, modify, merge, publish,
* notice, this list of conditions and the following disclaimer. * distribute, sublicense, and/or sell copies of the Software, and to
* 2. Redistributions in binary form must reproduce the above copyright * permit persons to whom the Software is furnished to do so, subject to
* notice, this list of conditions and the following disclaimer in the * the following conditions:
* documentation and/or other materials provided with the distribution.
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * The above copyright notice and this permission notice shall be
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * included in all copies or substantial portions of the Software.
* 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 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 <sys/cdefs.h> #include <sys/cdefs.h>
__FBSDID("$FreeBSD$"); __FBSDID("$FreeBSD$");
#include <stddef.h> #include <limits.h>
#include <stdint.h>
#include <string.h> #include <string.h>
__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 *, int);
char * 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; #ifdef __GNUC__
for (;; ++p) { typedef size_t __attribute__((__may_alias__)) word;
if (*p == c || *p == '\0') const word *w;
return ((char *)p); for (; (uintptr_t)s % ALIGN; s++)
} if (!*s || *(unsigned char *)s == c)
/* NOTREACHED */ 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);

View File

@ -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 # 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). # might be using optimized assembly and duplicating that logic here is awkward).
_libc_string_objects= bcmp bcopy bzero memset memchr memcmp memcpy memmove \ _libc_string_objects= bcmp bcopy bzero memset memchr memcmp memcpy memmove \
stpncpy strcat strchr strcmp stpcpy strcpy strcspn strdup strlcat strlcpy \ stpncpy strcat strchr strchrnul strcmp stpcpy strcpy strcspn strdup \
strlen strncmp strncpy strrchr strsep strspn strstr strtok strlcat strlcpy strlen strncmp strncpy strrchr strsep strspn strstr strtok
# Also use all the syscall .o files from libc_nossp_pic: # Also use all the syscall .o files from libc_nossp_pic:
_libc_other_objects= sigsetjmp lstat stat fstat fstatat fstatfs syscall \ _libc_other_objects= sigsetjmp lstat stat fstat fstatat fstatfs syscall \
cerror geteuid getegid sigfastblock munmap mprotect \ cerror geteuid getegid sigfastblock munmap mprotect \

View File

@ -29,7 +29,7 @@ SRCS+= ntoh.c
.PATH: ${LIBCSRC}/string .PATH: ${LIBCSRC}/string
SRCS+= bcmp.c bcopy.c bzero.c ffs.c fls.c \ SRCS+= bcmp.c bcopy.c bzero.c ffs.c fls.c \
memccpy.c memchr.c memcmp.c memcpy.c memmove.c memset.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 \ 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 strnlen.c strpbrk.c strrchr.c strsep.c strspn.c strstr.c strtok.c swab.c