xref: /openbsd-src/sys/lib/libkern/fls.c (revision 383d29890bd67d638eccc0aa1a82ed1dffc9662c)
1*383d2989Sjsg /*	$OpenBSD: fls.c,v 1.1 2013/07/11 01:20:32 jsg Exp $	*/
2*383d2989Sjsg /*-
3*383d2989Sjsg  * Copyright (c) 1990, 1993
4*383d2989Sjsg  *	The Regents of the University of California.  All rights reserved.
5*383d2989Sjsg  *
6*383d2989Sjsg  * Redistribution and use in source and binary forms, with or without
7*383d2989Sjsg  * modification, are permitted provided that the following conditions
8*383d2989Sjsg  * are met:
9*383d2989Sjsg  * 1. Redistributions of source code must retain the above copyright
10*383d2989Sjsg  *    notice, this list of conditions and the following disclaimer.
11*383d2989Sjsg  * 2. Redistributions in binary form must reproduce the above copyright
12*383d2989Sjsg  *    notice, this list of conditions and the following disclaimer in the
13*383d2989Sjsg  *    documentation and/or other materials provided with the distribution.
14*383d2989Sjsg  * 3. Neither the name of the University nor the names of its contributors
15*383d2989Sjsg  *    may be used to endorse or promote products derived from this software
16*383d2989Sjsg  *    without specific prior written permission.
17*383d2989Sjsg  *
18*383d2989Sjsg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*383d2989Sjsg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*383d2989Sjsg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*383d2989Sjsg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*383d2989Sjsg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*383d2989Sjsg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*383d2989Sjsg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*383d2989Sjsg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*383d2989Sjsg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*383d2989Sjsg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*383d2989Sjsg  * SUCH DAMAGE.
29*383d2989Sjsg  */
30*383d2989Sjsg 
31*383d2989Sjsg #include <lib/libkern/libkern.h>
32*383d2989Sjsg 
33*383d2989Sjsg /*
34*383d2989Sjsg  * Find Last Set bit
35*383d2989Sjsg  */
36*383d2989Sjsg int
fls(int mask)37*383d2989Sjsg fls(int mask)
38*383d2989Sjsg {
39*383d2989Sjsg 	int bit;
40*383d2989Sjsg 
41*383d2989Sjsg 	if (mask == 0)
42*383d2989Sjsg 		return (0);
43*383d2989Sjsg 	for (bit = 1; mask != 1; bit++)
44*383d2989Sjsg 		mask = (unsigned int)mask >> 1;
45*383d2989Sjsg 	return (bit);
46*383d2989Sjsg }
47