diff options
author | Jonathan Gray <jsg@cvs.openbsd.org> | 2013-07-11 01:20:33 +0000 |
---|---|---|
committer | Jonathan Gray <jsg@cvs.openbsd.org> | 2013-07-11 01:20:33 +0000 |
commit | 5452aa0c364a8aeed3df92836fc62f094ec72b40 (patch) | |
tree | f3797eee41f6c7c088fcd676029aaaf31b87025a /sys/lib | |
parent | a9e152251b8b18a9e73ec90b80e224e11e85e994 (diff) |
add fls/flsl functions to find the last bit set in a value
from FreeBSD
ok mikeb@ haesbaert@ deraadt@
Diffstat (limited to 'sys/lib')
-rw-r--r-- | sys/lib/libkern/fls.c | 46 | ||||
-rw-r--r-- | sys/lib/libkern/flsl.c | 46 | ||||
-rw-r--r-- | sys/lib/libkern/libkern.h | 4 |
3 files changed, 95 insertions, 1 deletions
diff --git a/sys/lib/libkern/fls.c b/sys/lib/libkern/fls.c new file mode 100644 index 00000000000..8de9d6eaed9 --- /dev/null +++ b/sys/lib/libkern/fls.c @@ -0,0 +1,46 @@ +/* $OpenBSD: fls.c,v 1.1 2013/07/11 01:20:32 jsg Exp $ */ +/*- + * 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. 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 <lib/libkern/libkern.h> + +/* + * Find Last Set bit + */ +int +fls(int mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; mask != 1; bit++) + mask = (unsigned int)mask >> 1; + return (bit); +} diff --git a/sys/lib/libkern/flsl.c b/sys/lib/libkern/flsl.c new file mode 100644 index 00000000000..7f4063eef48 --- /dev/null +++ b/sys/lib/libkern/flsl.c @@ -0,0 +1,46 @@ +/* $OpenBSD: flsl.c,v 1.1 2013/07/11 01:20:32 jsg Exp $ */ +/*- + * 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. 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 <lib/libkern/libkern.h> + +/* + * Find Last Set bit + */ +int +flsl(long mask) +{ + int bit; + + if (mask == 0) + return (0); + for (bit = 1; mask != 1; bit++) + mask = (unsigned long)mask >> 1; + return (bit); +} diff --git a/sys/lib/libkern/libkern.h b/sys/lib/libkern/libkern.h index cb9817bce7f..9de6fbf84b4 100644 --- a/sys/lib/libkern/libkern.h +++ b/sys/lib/libkern/libkern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: libkern.h,v 1.30 2013/06/08 19:06:50 sf Exp $ */ +/* $OpenBSD: libkern.h,v 1.31 2013/07/11 01:20:32 jsg Exp $ */ /* $NetBSD: libkern.h,v 1.7 1996/03/14 18:52:08 christos Exp $ */ /*- @@ -148,6 +148,8 @@ int bcmp(const void *, const void *, size_t); void bzero(void *, size_t); void explicit_bzero(void *, size_t); int ffs(int); +int fls(int); +int flsl(long); void *memchr(const void *, int, size_t); int memcmp(const void *, const void *, size_t); u_int32_t random(void); |