diff --git a/include/strings.h b/include/strings.h index cb7793803106..895a6a4ed359 100644 --- a/include/strings.h +++ b/include/strings.h @@ -42,6 +42,9 @@ int bcmp(const void *, const void *, size_t); /* LEGACY */ void bcopy(const void *, void *, size_t); /* LEGACY */ void bzero(void *, size_t); /* LEGACY */ int ffs(int); +int ffsl(long); +int fls(int); +int flsl(long); char *index(const char *, int); /* LEGACY */ char *rindex(const char *, int); /* LEGACY */ int strcasecmp(const char *, const char *); diff --git a/lib/libc/Makefile b/lib/libc/Makefile index e39540644c38..1b008110a264 100644 --- a/lib/libc/Makefile +++ b/lib/libc/Makefile @@ -83,8 +83,8 @@ SRCS+= ${_src} KQSRCS= adddi3.c anddi3.c ashldi3.c ashrdi3.c cmpdi2.c divdi3.c iordi3.c \ lshldi3.c lshrdi3.c moddi3.c muldi3.c negdi2.c notdi2.c qdivrem.c \ subdi3.c ucmpdi2.c udivdi3.c umoddi3.c xordi3.c -KSRCS= bcmp.c ffs.c index.c mcount.c rindex.c strcat.c strcmp.c strcpy.c \ - strlen.c strncpy.c +KSRCS= bcmp.c ffs.c ffsl.c fls.c flsl.c index.c mcount.c rindex.c \ + strcat.c strcmp.c strcpy.c strlen.c strncpy.c libkern: libkern.gen libkern.${MACHINE_ARCH} diff --git a/lib/libc/string/Makefile.inc b/lib/libc/string/Makefile.inc index a1cc7d441d4b..a2d172264a90 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 5b5fb6f00e21..cb9c6cc3931f 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 f08e0d124e33..abd214693911 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 000000000000..a0d81d178e69 --- /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 000000000000..71a971a531d5 --- /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 000000000000..e881e93cf23a --- /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); +}