summaryrefslogtreecommitdiff
path: root/src/lib/libc
diff options
context:
space:
mode:
authorclaudio <>2025-10-24 11:30:06 +0000
committerclaudio <>2025-10-24 11:30:06 +0000
commit8cbd4e746f40c750809e34d04c0298e0a5ff6f42 (patch)
treede417cfcebbdd39fd868b5b5a0569e386e25b92f /src/lib/libc
parent2e803cbe8e599c0ae52e9dda44c3d22e3dec105d (diff)
downloadopenbsd-8cbd4e746f40c750809e34d04c0298e0a5ff6f42.tar.gz
openbsd-8cbd4e746f40c750809e34d04c0298e0a5ff6f42.tar.bz2
openbsd-8cbd4e746f40c750809e34d04c0298e0a5ff6f42.zip
Implement ffsl() and ffsll() using the compiler builtin __builtin_ctzl
now that all archs use at least gcc4. ffsl() and ffsll() are now part of POSIX. OK deraadt@, input from miod@ and jsg@
Diffstat (limited to 'src/lib/libc')
-rw-r--r--src/lib/libc/string/Makefile.inc12
-rw-r--r--src/lib/libc/string/ffs.331
-rw-r--r--src/lib/libc/string/ffsl.c17
-rw-r--r--src/lib/libc/string/ffsll.c17
4 files changed, 66 insertions, 11 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc
index 204ca1b266..f8b6330453 100644
--- a/src/lib/libc/string/Makefile.inc
+++ b/src/lib/libc/string/Makefile.inc
@@ -1,13 +1,13 @@
1# $OpenBSD: Makefile.inc,v 1.40 2024/07/14 09:51:18 jca Exp $ 1# $OpenBSD: Makefile.inc,v 1.41 2025/10/24 11:30:06 claudio Exp $
2 2
3# string sources 3# string sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string
5 5
6SRCS+= explicit_bzero.c memccpy.c memmem.c memrchr.c stpcpy.c stpncpy.c \ 6SRCS+= explicit_bzero.c ffsl.c ffsll.c memccpy.c memmem.c memrchr.c \
7 strcasecmp.c strcasecmp_l.c strcasestr.c strcoll.c strcoll_l.c \ 7 stpcpy.c stpncpy.c strcasecmp.c strcasecmp_l.c strcasestr.c \
8 strdup.c strerror.c strerror_l.c strerror_r.c strmode.c \ 8 strcoll.c strcoll_l.c strdup.c strerror.c strerror_l.c strerror_r.c \
9 strndup.c strnlen.c strsignal.c strtok.c strxfrm.c strxfrm_l.c \ 9 strmode.c strndup.c strnlen.c strsignal.c strtok.c strxfrm.c \
10 timingsafe_bcmp.c timingsafe_memcmp.c \ 10 strxfrm_l.c timingsafe_bcmp.c timingsafe_memcmp.c \
11 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ 11 wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \
12 wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcsnlen.c wcspbrk.c wcsrchr.c \ 12 wcslen.c wcsncat.c wcsncmp.c wcsncpy.c wcsnlen.c wcspbrk.c wcsrchr.c \
13 wcsspn.c wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c \ 13 wcsspn.c wcsstr.c wcstok.c wcswcs.c wcswidth.c wmemchr.c wmemcmp.c \
diff --git a/src/lib/libc/string/ffs.3 b/src/lib/libc/string/ffs.3
index e78ab99e8f..630db1c584 100644
--- a/src/lib/libc/string/ffs.3
+++ b/src/lib/libc/string/ffs.3
@@ -27,22 +27,31 @@
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE. 28.\" SUCH DAMAGE.
29.\" 29.\"
30.\" $OpenBSD: ffs.3,v 1.11 2019/08/30 18:35:03 deraadt Exp $ 30.\" $OpenBSD: ffs.3,v 1.12 2025/10/24 11:30:06 claudio Exp $
31.\" 31.\"
32.Dd $Mdocdate: August 30 2019 $ 32.Dd $Mdocdate: October 24 2025 $
33.Dt FFS 3 33.Dt FFS 3
34.Os 34.Os
35.Sh NAME 35.Sh NAME
36.Nm ffs 36.Nm ffs ,
37.Nm ffsl ,
38.Nm ffsll
37.Nd find first bit set in a bit string 39.Nd find first bit set in a bit string
38.Sh SYNOPSIS 40.Sh SYNOPSIS
39.In strings.h 41.In strings.h
40.Ft int 42.Ft int
41.Fn ffs "int value" 43.Fn ffs "int value"
44.Ft int
45.Fn ffsl "long value"
46.Ft int
47.Fn ffsll "long long value"
42.Sh DESCRIPTION 48.Sh DESCRIPTION
43The 49The
44.Fn ffs 50.Fn ffs ,
45function finds the first bit set in 51.Fn ffsl
52and
53.Fn ffsll
54functions find the first bit set in
46.Fa value 55.Fa value
47and returns the index of that bit. 56and returns the index of that bit.
48Bits are numbered starting from 1, starting at the rightmost bit. 57Bits are numbered starting from 1, starting at the rightmost bit.
@@ -54,8 +63,20 @@ The
54.Fn ffs 63.Fn ffs
55function conforms to 64function conforms to
56.St -p1003.1-2008 . 65.St -p1003.1-2008 .
66The
67.Fn ffsl
68and
69.Fn ffsll
70functions conform to
71.St -p1003.1-2024 .
57.Sh HISTORY 72.Sh HISTORY
58The 73The
59.Fn ffs 74.Fn ffs
60function first appeared in 75function first appeared in
61.Bx 4.2 . 76.Bx 4.2 .
77The
78.Fn ffsl
79and
80.Fn ffsll
81functions first appeared in
82.Ox 7.9 .
diff --git a/src/lib/libc/string/ffsl.c b/src/lib/libc/string/ffsl.c
new file mode 100644
index 0000000000..182318c3d6
--- /dev/null
+++ b/src/lib/libc/string/ffsl.c
@@ -0,0 +1,17 @@
1/* $OpenBSD: ffsl.c,v 1.1 2025/10/24 11:30:06 claudio Exp $ */
2
3/*
4 * Public domain.
5 * Written by Claudio Jeker.
6 */
7
8#include <strings.h>
9
10/*
11 * ffs -- find the first (least significant) bit set
12 */
13int
14ffsl(long mask)
15{
16 return (mask ? __builtin_ctzl(mask) + 1 : 0);
17}
diff --git a/src/lib/libc/string/ffsll.c b/src/lib/libc/string/ffsll.c
new file mode 100644
index 0000000000..9370c5ae41
--- /dev/null
+++ b/src/lib/libc/string/ffsll.c
@@ -0,0 +1,17 @@
1/* $OpenBSD: ffsll.c,v 1.1 2025/10/24 11:30:06 claudio Exp $ */
2
3/*
4 * Public domain.
5 * Written by Claudio Jeker.
6 */
7
8#include <strings.h>
9
10/*
11 * ffs -- find the first (least significant) bit set
12 */
13int
14ffsll(long long mask)
15{
16 return (mask ? __builtin_ctzll(mask) + 1 : 0);
17}