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