diff options
author | 2013-07-11 01:20:31 +0000 | |
---|---|---|
committer | 2013-07-11 01:20:31 +0000 | |
commit | 383d29890bd67d638eccc0aa1a82ed1dffc9662c (patch) | |
tree | 14c5869c347686aa7bd13acec1964aecb3746ada /sys/lib/libkern/flsl.c | |
parent | To prevent lock ordering problems with the kernel lock, we need to make sure (diff) | |
download | wireguard-openbsd-383d29890bd67d638eccc0aa1a82ed1dffc9662c.tar.xz wireguard-openbsd-383d29890bd67d638eccc0aa1a82ed1dffc9662c.zip |
add fls/flsl functions to find the last bit set in a value
from FreeBSD
ok mikeb@ haesbaert@ deraadt@
Diffstat (limited to 'sys/lib/libkern/flsl.c')
-rw-r--r-- | sys/lib/libkern/flsl.c | 46 |
1 files changed, 46 insertions, 0 deletions
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); +} |