Module Name: src Committed By: riastradh Date: Fri Nov 1 18:35:12 UTC 2024
Modified Files: src/include: strings.h src/lib/libc/string: Makefile.inc src/tests/include/sys: Makefile t_bitops.c Added Files: src/common/lib/libc/string: ffsl.c ffsll.c Log Message: strings.h: Spruce up for POSIX 2024. 1. Add ffsl, ffsll. 2. Hide bcmp, bcopy, bzero, index, rindex for POSIX>=2024. 3. Expose ffs only for NetBSD or POSIX>=2008 with XSI option. 4. Hide popcount* NetBSD extensions for any POSIX. 5. Sprinkle __constfunc on ffs*. Add tests for ffs/ffsl/ffsll in tests/include/sys/t_bitops next to ffs32/ffs64 for convenience. XXX Still missing strcasecmp_l, strncasecmp_l, and locale_t. PR lib/58802: missing ffsl(), ffsll() functions from POSIX 2024 To generate a diff of this commit: cvs rdiff -u -r0 -r1.1 src/common/lib/libc/string/ffsl.c \ src/common/lib/libc/string/ffsll.c cvs rdiff -u -r1.18 -r1.19 src/include/strings.h cvs rdiff -u -r1.90 -r1.91 src/lib/libc/string/Makefile.inc cvs rdiff -u -r1.16 -r1.17 src/tests/include/sys/Makefile cvs rdiff -u -r1.21 -r1.22 src/tests/include/sys/t_bitops.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/include/strings.h diff -u src/include/strings.h:1.18 src/include/strings.h:1.19 --- src/include/strings.h:1.18 Mon Aug 22 01:24:15 2011 +++ src/include/strings.h Fri Nov 1 18:35:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: strings.h,v 1.18 2011/08/22 01:24:15 dholland Exp $ */ +/* $NetBSD: strings.h,v 1.19 2024/11/01 18:35:12 riastradh Exp $ */ /*- * Copyright (c) 1998 The NetBSD Foundation, Inc. @@ -49,17 +49,31 @@ typedef _BSD_SIZE_T_ size_t; #include <machine/int_types.h> __BEGIN_DECLS +#if defined(_NETBSD_SOURCE) || \ + (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE - 0 < 202405L) int bcmp(const void *, const void *, size_t); void bcopy(const void *, void *, size_t); void bzero(void *, size_t); -int ffs(int); -char *index(const char *, int); +#endif +#if defined(_NETBSD_SOURCE) || _XOPEN_SOURCE - 0 >= 700 +int ffs(int) __constfunc; +#endif +#if defined(_NETBSD_SOURCE) || _XOPEN_SOURCE - 0 >= 800 +int ffsl(long) __constfunc; +int ffsll(long long) __constfunc; +#endif +#ifdef _NETBSD_SOURCE unsigned int popcount(unsigned int) __constfunc; unsigned int popcountl(unsigned long) __constfunc; unsigned int popcountll(unsigned long long) __constfunc; unsigned int popcount32(__uint32_t) __constfunc; unsigned int popcount64(__uint64_t) __constfunc; +#endif +#if defined(_NETBSD_SOURCE) || \ + (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE - 0 < 202405L) +char *index(const char *, int); char *rindex(const char *, int); +#endif int strcasecmp(const char *, const char *); int strncasecmp(const char *, const char *, size_t); __END_DECLS Index: src/lib/libc/string/Makefile.inc diff -u src/lib/libc/string/Makefile.inc:1.90 src/lib/libc/string/Makefile.inc:1.91 --- src/lib/libc/string/Makefile.inc:1.90 Sat Jun 8 21:35:18 2024 +++ src/lib/libc/string/Makefile.inc Fri Nov 1 18:35:12 2024 @@ -1,5 +1,5 @@ # from: @(#)Makefile.inc 8.1 (Berkeley) 6/4/93 -# $NetBSD: Makefile.inc,v 1.90 2024/06/08 21:35:18 joerg Exp $ +# $NetBSD: Makefile.inc,v 1.91 2024/11/01 18:35:12 riastradh Exp $ # string sources .PATH: ${ARCHDIR}/string ${.CURDIR}/string @@ -15,7 +15,7 @@ SRCS+= bm.c stpcpy.c stpncpy.c \ strtok_r.c strxfrm.c __strsignal.c strerror_r.c strndup.c \ stresep.c memrchr.c -SRCS+= bcmp.c bcopy.c bzero.c ffs.c memchr.c memcmp.c memset.c +SRCS+= bcmp.c bcopy.c bzero.c ffs.c ffsl.c ffsll.c memchr.c memcmp.c memset.c SRCS+= strcat.c strcmp.c strcpy.c strcspn.c strlen.c SRCS+= strncat.c strncmp.c strncpy.c strpbrk.c strsep.c SRCS+= strspn.c strstr.c swab.c strnstr.c Index: src/tests/include/sys/Makefile diff -u src/tests/include/sys/Makefile:1.16 src/tests/include/sys/Makefile:1.17 --- src/tests/include/sys/Makefile:1.16 Sun May 31 16:36:07 2020 +++ src/tests/include/sys/Makefile Fri Nov 1 18:35:12 2024 @@ -1,4 +1,4 @@ -# $NetBSD: Makefile,v 1.16 2020/05/31 16:36:07 kamil Exp $ +# $NetBSD: Makefile,v 1.17 2024/11/01 18:35:12 riastradh Exp $ NOMAN= # defined @@ -14,6 +14,10 @@ TESTS_C+= t_pslist TESTS_C+= t_tree TESTS_C+= t_types +COPTS.t_bitops.c+= -fno-builtin-ffs +COPTS.t_bitops.c+= -fno-builtin-ffsl +COPTS.t_bitops.c+= -fno-builtin-ffsll + # NULL + 0 arithmetic raises LLVM UBSan warnings, specially in sys/pslist.h # in the type-safe macros _PSLIST_VALIDATE_PTRS and _PSLIST_VALIDATE_CONTAINER. # See also src/sys/rump/Makefile.rump Index: src/tests/include/sys/t_bitops.c diff -u src/tests/include/sys/t_bitops.c:1.21 src/tests/include/sys/t_bitops.c:1.22 --- src/tests/include/sys/t_bitops.c:1.21 Thu Jul 13 20:39:24 2023 +++ src/tests/include/sys/t_bitops.c Fri Nov 1 18:35:12 2024 @@ -1,4 +1,4 @@ -/* $NetBSD: t_bitops.c,v 1.21 2023/07/13 20:39:24 riastradh Exp $ */ +/* $NetBSD: t_bitops.c,v 1.22 2024/11/01 18:35:12 riastradh Exp $ */ /*- * Copyright (c) 2011, 2012 The NetBSD Foundation, Inc. @@ -29,7 +29,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ #include <sys/cdefs.h> -__RCSID("$NetBSD: t_bitops.c,v 1.21 2023/07/13 20:39:24 riastradh Exp $"); +__RCSID("$NetBSD: t_bitops.c,v 1.22 2024/11/01 18:35:12 riastradh Exp $"); #include <atf-c.h> @@ -40,6 +40,7 @@ __RCSID("$NetBSD: t_bitops.c,v 1.21 2023 #include <inttypes.h> #include <stdlib.h> #include <string.h> +#include <strings.h> static const struct { uint32_t val; @@ -164,18 +165,65 @@ ATF_TC_BODY(ffsfls, tc) uint8_t i; int n, m; + ATF_CHECK_EQ_MSG((n = ffs(0)), 0x00, "n=%d", n); + ATF_CHECK_EQ_MSG((n = ffsl(0)), 0x00, "n=%d", n); + ATF_CHECK_EQ_MSG((n = ffsll(0)), 0x00, "n=%d", n); + ATF_CHECK_EQ_MSG((n = ffs32(0)), 0x00, "n=%d", n); ATF_CHECK_EQ_MSG((n = fls32(0)), 0x00, "n=%d", n); ATF_CHECK_EQ_MSG((n = ffs64(0)), 0x00, "n=%d", n); ATF_CHECK_EQ_MSG((n = fls64(0)), 0x00, "n=%d", n); + ATF_CHECK_EQ_MSG((n = ffs(UINT_MAX)), 0x01, "n=%d\n", n); + ATF_CHECK_EQ_MSG((n = ffsl(ULONG_MAX)), 0x01, "n=%d\n", n); + ATF_CHECK_EQ_MSG((n = ffsll(ULLONG_MAX)), 0x01, "n=%d\n", n); + + ATF_CHECK_EQ_MSG((n = ffs((unsigned)INT_MAX + 1)), + CHAR_BIT*sizeof(int), + "n=%d\n", n); + ATF_CHECK_EQ_MSG((n = ffsl((unsigned long)LONG_MAX + 1)), + CHAR_BIT*sizeof(long), + "n=%d\n", n); + ATF_CHECK_EQ_MSG((n = ffsll((unsigned long long)LLONG_MAX + 1)), + CHAR_BIT*sizeof(long long), + "n=%d\n", n); + ATF_CHECK_EQ_MSG((n = ffs32(UINT32_MAX)), 0x01, "n=%d", n); ATF_CHECK_EQ_MSG((n = fls32(UINT32_MAX)), 0x20, "n=%d", n); ATF_CHECK_EQ_MSG((n = ffs64(UINT64_MAX)), 0x01, "n=%d", n); ATF_CHECK_EQ_MSG((n = fls64(UINT64_MAX)), 0x40, "n=%d", n); + for (i = 0; i < CHAR_BIT*sizeof(long long); i++) { + if (i < CHAR_BIT*sizeof(int)) { + ATF_CHECK_EQ_MSG((n = ffs(1U << i)), i + 1, "n=%d", n); + } + if (i < CHAR_BIT*sizeof(long)) { + ATF_CHECK_EQ_MSG((n = ffsl(1UL << i)), i + 1, + "n=%d", n); + } + if (i < CHAR_BIT*sizeof(long long)) { + ATF_CHECK_EQ_MSG((n = ffsll(1ULL << i)), i + 1, + "n=%d", n); + } + if (i < CHAR_BIT*sizeof(int32_t)) { + ATF_CHECK_EQ_MSG((n = ffs32((uint32_t)1 << i)), i + 1, + "n=%d", n); + } + if (i < CHAR_BIT*sizeof(int64_t)) { + ATF_CHECK_EQ_MSG((n = ffs64((uint64_t)1 << i)), i + 1, + "n=%d", n); + } + } + for (i = 1; i < __arraycount(bits); i++) { + ATF_CHECK_EQ_MSG((n = ffs(bits[i].val)), (m = bits[i].ffs), + "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffsl(bits[i].val)), (m = bits[i].ffs), + "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffsll(bits[i].val)), (m = bits[i].ffs), + "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffs32(bits[i].val)), (m = bits[i].ffs), "[i=%"PRIu8"] n=%d m=%d", i, n, m); ATF_CHECK_EQ_MSG((n = fls32(bits[i].val)), (m = bits[i].fls), @@ -185,6 +233,16 @@ ATF_TC_BODY(ffsfls, tc) ATF_CHECK_EQ_MSG((n = fls64(bits[i].val)), (m = bits[i].fls), "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffs(bits[i].val << 1)), + (m = bits[i].ffs + 1), + "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffsl(bits[i].val << 1)), + (m = bits[i].ffs + 1), + "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffsll(bits[i].val << 1)), + (m = bits[i].ffs + 1), + "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffs32(bits[i].val << 1)), (m = bits[i].ffs + 1), "[i=%"PRIu8"] n=%d m=%d", i, n, m); @@ -198,6 +256,16 @@ ATF_TC_BODY(ffsfls, tc) (m = bits[i].fls + 1), "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffs(bits[i].val << 9)), + (m = bits[i].ffs + 9), + "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffsl(bits[i].val << 9)), + (m = bits[i].ffs + 9), + "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffsll(bits[i].val << 9)), + (m = bits[i].ffs + 9), + "[i=%"PRIu8"] n=%d m=%d", i, n, m); + ATF_CHECK_EQ_MSG((n = ffs32(bits[i].val << 9)), (m = bits[i].ffs + 9), "[i=%"PRIu8"] n=%d m=%d", i, n, m); Added files: Index: src/common/lib/libc/string/ffsl.c diff -u /dev/null src/common/lib/libc/string/ffsl.c:1.1 --- /dev/null Fri Nov 1 18:35:12 2024 +++ src/common/lib/libc/string/ffsl.c Fri Nov 1 18:35:12 2024 @@ -0,0 +1,50 @@ +/* $NetBSD: ffsl.c,v 1.1 2024/11/01 18:35:12 riastradh Exp $ */ + +/*- + * Copyright (c) 2024 The NetBSD Foundation, Inc. + * All rights reserved. + * + * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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> +__RCSID("$NetBSD: ffsl.c,v 1.1 2024/11/01 18:35:12 riastradh Exp $"); + +#include <sys/bitops.h> + +#include <limits.h> +#include <strings.h> + +#undef ffsl /* paranoia */ + +int +ffsl(long x) +{ + +#ifdef _LP64 + __CTASSERT(CHAR_BIT*sizeof(x) == 64); + return ffs64(x); +#else + __CTASSERT(CHAR_BIT*sizeof(x) == 32); + return ffs32(x); +#endif +} Index: src/common/lib/libc/string/ffsll.c diff -u /dev/null src/common/lib/libc/string/ffsll.c:1.1 --- /dev/null Fri Nov 1 18:35:12 2024 +++ src/common/lib/libc/string/ffsll.c Fri Nov 1 18:35:12 2024 @@ -0,0 +1,45 @@ +/* $NetBSD: ffsll.c,v 1.1 2024/11/01 18:35:12 riastradh Exp $ */ + +/*- + * Copyright (c) 2024 The NetBSD Foundation, Inc. + * All rights reserved. + * + * 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 NETBSD FOUNDATION, INC. 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 FOUNDATION 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> +__RCSID("$NetBSD: ffsll.c,v 1.1 2024/11/01 18:35:12 riastradh Exp $"); + +#include <sys/bitops.h> + +#include <limits.h> +#include <strings.h> + +#undef ffsll /* paranoia */ + +int +ffsll(long long x) +{ + + __CTASSERT(CHAR_BIT*sizeof(x) == 64); + return ffs64(x); +}