xref: /illumos-gate/usr/src/common/util/ffs.c (revision 69bb4bb45c98da60d21839c4dc3c01ea1be60585)
1*69bb4bb4Scarlsonj /*
2*69bb4bb4Scarlsonj  * CDDL HEADER START
3*69bb4bb4Scarlsonj  *
4*69bb4bb4Scarlsonj  * The contents of this file are subject to the terms of the
5*69bb4bb4Scarlsonj  * Common Development and Distribution License (the "License").
6*69bb4bb4Scarlsonj  * You may not use this file except in compliance with the License.
7*69bb4bb4Scarlsonj  *
8*69bb4bb4Scarlsonj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*69bb4bb4Scarlsonj  * or http://www.opensolaris.org/os/licensing.
10*69bb4bb4Scarlsonj  * See the License for the specific language governing permissions
11*69bb4bb4Scarlsonj  * and limitations under the License.
12*69bb4bb4Scarlsonj  *
13*69bb4bb4Scarlsonj  * When distributing Covered Code, include this CDDL HEADER in each
14*69bb4bb4Scarlsonj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*69bb4bb4Scarlsonj  * If applicable, add the following below this CDDL HEADER, with the
16*69bb4bb4Scarlsonj  * fields enclosed by brackets "[]" replaced with your own identifying
17*69bb4bb4Scarlsonj  * information: Portions Copyright [yyyy] [name of copyright owner]
18*69bb4bb4Scarlsonj  *
19*69bb4bb4Scarlsonj  * CDDL HEADER END
20*69bb4bb4Scarlsonj  */
21*69bb4bb4Scarlsonj 
22*69bb4bb4Scarlsonj /*
23*69bb4bb4Scarlsonj  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*69bb4bb4Scarlsonj  * Use is subject to license terms.
25*69bb4bb4Scarlsonj  */
26*69bb4bb4Scarlsonj 
27*69bb4bb4Scarlsonj #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*69bb4bb4Scarlsonj 
29*69bb4bb4Scarlsonj /*
30*69bb4bb4Scarlsonj  * Common implementation of ffs for kernel, mdb, and libc.  Note that mdb
31*69bb4bb4Scarlsonj  * renames ffs into mdb_ffs to avoid user-space clashes with the signature of
32*69bb4bb4Scarlsonj  * ffs(3C).
33*69bb4bb4Scarlsonj  */
34*69bb4bb4Scarlsonj 
35*69bb4bb4Scarlsonj #if defined(_KERNEL) || defined(ffs)
36*69bb4bb4Scarlsonj #include <sys/int_types.h>
37*69bb4bb4Scarlsonj #define	arg_t	uintmax_t
38*69bb4bb4Scarlsonj #else
39*69bb4bb4Scarlsonj #pragma weak ffs = _ffs
40*69bb4bb4Scarlsonj #define	arg_t	int
41*69bb4bb4Scarlsonj #include "synonyms.h"
42*69bb4bb4Scarlsonj #endif
43*69bb4bb4Scarlsonj 
44*69bb4bb4Scarlsonj int
45*69bb4bb4Scarlsonj ffs(arg_t bits)
46*69bb4bb4Scarlsonj {
47*69bb4bb4Scarlsonj 	int i;
48*69bb4bb4Scarlsonj 
49*69bb4bb4Scarlsonj 	if (bits == 0)
50*69bb4bb4Scarlsonj 		return (0);
51*69bb4bb4Scarlsonj 	for (i = 1; ; i++, bits >>= 1) {
52*69bb4bb4Scarlsonj 		if (bits & 1)
53*69bb4bb4Scarlsonj 			break;
54*69bb4bb4Scarlsonj 	}
55*69bb4bb4Scarlsonj 	return (i);
56*69bb4bb4Scarlsonj }
57