From ec1fd605cbbcb4bc5ec851a6775a363b01d56029 Mon Sep 17 00:00:00 2001 From: des Date: Tue, 13 Jan 2004 16:05:47 +0000 Subject: Add and document ffsl(), fls() and flsl(). --- lib/libc/string/Makefile.inc | 6 ++++- lib/libc/string/ffs.3 | 41 +++++++++++++++++++++++++++++----- lib/libc/string/ffs.c | 6 ++--- lib/libc/string/ffsl.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ lib/libc/string/fls.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ lib/libc/string/flsl.c | 52 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 lib/libc/string/ffsl.c create mode 100644 lib/libc/string/fls.c create mode 100644 lib/libc/string/flsl.c (limited to 'lib/libc/string') diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index a1cc7d4..a2d1722 100644 --- a/lib/libc/string/Makefile.inc +++ b/lib/libc/string/Makefile.inc @@ -6,7 +6,8 @@ CFLAGS+= -I${.CURDIR}/locale # machine-independent string sources -MISRCS+=bcmp.c bcopy.c bzero.c ffs.c index.c memccpy.c memchr.c memcmp.c \ +MISRCS+=bcmp.c bcopy.c bzero.c ffs.c ffsl.c fls.c flsl.c index.c memccpy.c \ + memchr.c memcmp.c \ memcpy.c memmove.c memset.c rindex.c stpcpy.c strcasecmp.c strcat.c \ strchr.c strcmp.c strcoll.c strcpy.c strcspn.c strdup.c strerror.c \ strlcat.c strlcpy.c strlen.c strmode.c strncat.c strncmp.c strncpy.c \ @@ -32,6 +33,9 @@ MAN+= bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 index.3 memccpy.3 memchr.3 \ strspn.3 strstr.3 strtok.3 strxfrm.3 swab.3 wcscoll.3 wcstok.3 \ wcswidth.3 wcsxfrm.3 wmemchr.3 +MLINKS+=ffs.3 ffsl.3 +MLINKS+=ffs.3 fls.3 +MLINKS+=ffs.3 flsl.3 MLINKS+=index.3 rindex.3 MLINKS+=strcasecmp.3 strncasecmp.3 MLINKS+=strcat.3 strncat.3 diff --git a/lib/libc/string/ffs.3 b/lib/libc/string/ffs.3 index 5b5fb6f..cb9c6cc 100644 --- a/lib/libc/string/ffs.3 +++ b/lib/libc/string/ffs.3 @@ -38,23 +38,44 @@ .Dt FFS 3 .Os .Sh NAME -.Nm ffs -.Nd find first bit set in a bit string +.Nm ffs , +.Nm ffsl , +.Nm fls , +.Nm flsl +.Nd find first or last bit set in a bit string .Sh LIBRARY .Lb libc .Sh SYNOPSIS .In strings.h .Ft int .Fn ffs "int value" +.Ft int +.Fn ffsl "long value" +.Ft int +.Fn fls "int value" +.Ft int +.Fn flsl "long value" .Sh DESCRIPTION The .Fn ffs -function finds the first bit set in +and +.Fn ffsl +functions find the first bit set in .Fa value -and returns the index of that bit. +and return the index of that bit. +.Pp +The +.Fn fls +and +.Fn flsl +functions find the last bit set in +.Fa value +and return the index of that bit. +.Pp Bits are numbered starting from 1, starting at the right-most -bit. -A return value of 0 means that the argument was zero. +(least significant) bit. +A return value of zero from any of these functions means that the +argument was zero. .Sh SEE ALSO .Xr bitstring 3 .Sh HISTORY @@ -69,3 +90,11 @@ before it was moved to for .St -p1003.1-2001 compliance. +.Pp +The +.Fn ffsl , +.Fn fls +and +.Fn flsl +functions appeared in +.Fx 5.3 . diff --git a/lib/libc/string/ffs.c b/lib/libc/string/ffs.c index f08e0d1..abd2146 100644 --- a/lib/libc/string/ffs.c +++ b/lib/libc/string/ffs.c @@ -40,7 +40,7 @@ __FBSDID("$FreeBSD$"); #include /* - * ffs -- vax ffs instruction + * Find First Set bit */ int ffs(int mask) @@ -50,6 +50,6 @@ ffs(int mask) if (mask == 0) return(0); for (bit = 1; !(mask & 1); bit++) - mask >>= 1; - return(bit); + (unsigned int)mask >>= 1; + return (bit); } diff --git a/lib/libc/string/ffsl.c b/lib/libc/string/ffsl.c new file mode 100644 index 0000000..a0d81d1 --- /dev/null +++ b/lib/libc/string/ffsl.c @@ -0,0 +1,52 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. 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 + * 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include + +/* + * Find First Set bit + */ +int +ffsl(long mask) +{ + int bit; + + if (mask == 0) + return(0); + for (bit = 1; !(mask & 1); bit++) + (unsigned long)mask >>= 1; + return (bit); +} diff --git a/lib/libc/string/fls.c b/lib/libc/string/fls.c new file mode 100644 index 0000000..71a971a --- /dev/null +++ b/lib/libc/string/fls.c @@ -0,0 +1,52 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. 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 + * 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include + +/* + * Find Last Set bit + */ +int +fls(int mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; mask != 1; bit++) + (unsigned int)mask >>= 1; + return (bit); +} diff --git a/lib/libc/string/flsl.c b/lib/libc/string/flsl.c new file mode 100644 index 0000000..e881e93 --- /dev/null +++ b/lib/libc/string/flsl.c @@ -0,0 +1,52 @@ +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. 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. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. 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 + * 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. + */ + +#include +__FBSDID("$FreeBSD$"); + +#include + +/* + * Find Last Set bit + */ +int +flsl(long mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; mask != 1; bit++) + (unsigned long)mask >>= 1; + return (bit); +} -- cgit v1.1