xref: /illumos-gate/usr/src/common/util/ffs.c (revision c40a6cd785e883b3f052b122c332e21174fc1871)
169bb4bb4Scarlsonj /*
269bb4bb4Scarlsonj  * CDDL HEADER START
369bb4bb4Scarlsonj  *
469bb4bb4Scarlsonj  * The contents of this file are subject to the terms of the
569bb4bb4Scarlsonj  * Common Development and Distribution License (the "License").
669bb4bb4Scarlsonj  * You may not use this file except in compliance with the License.
769bb4bb4Scarlsonj  *
869bb4bb4Scarlsonj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
969bb4bb4Scarlsonj  * or http://www.opensolaris.org/os/licensing.
1069bb4bb4Scarlsonj  * See the License for the specific language governing permissions
1169bb4bb4Scarlsonj  * and limitations under the License.
1269bb4bb4Scarlsonj  *
1369bb4bb4Scarlsonj  * When distributing Covered Code, include this CDDL HEADER in each
1469bb4bb4Scarlsonj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1569bb4bb4Scarlsonj  * If applicable, add the following below this CDDL HEADER, with the
1669bb4bb4Scarlsonj  * fields enclosed by brackets "[]" replaced with your own identifying
1769bb4bb4Scarlsonj  * information: Portions Copyright [yyyy] [name of copyright owner]
1869bb4bb4Scarlsonj  *
1969bb4bb4Scarlsonj  * CDDL HEADER END
2069bb4bb4Scarlsonj  */
2169bb4bb4Scarlsonj 
2269bb4bb4Scarlsonj /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2469bb4bb4Scarlsonj  * Use is subject to license terms.
2569bb4bb4Scarlsonj  */
2669bb4bb4Scarlsonj 
2769bb4bb4Scarlsonj /*
2869bb4bb4Scarlsonj  * Common implementation of ffs for kernel, mdb, and libc.  Note that mdb
2969bb4bb4Scarlsonj  * renames ffs into mdb_ffs to avoid user-space clashes with the signature of
3069bb4bb4Scarlsonj  * ffs(3C).
3169bb4bb4Scarlsonj  */
3269bb4bb4Scarlsonj 
3369bb4bb4Scarlsonj #if defined(_KERNEL) || defined(ffs)
3469bb4bb4Scarlsonj #include <sys/int_types.h>
3569bb4bb4Scarlsonj #define	arg_t	uintmax_t
3669bb4bb4Scarlsonj #else
3769bb4bb4Scarlsonj #define	arg_t	int
38*7257d1b4Sraf #include "lint.h"
3969bb4bb4Scarlsonj #endif
4069bb4bb4Scarlsonj 
4169bb4bb4Scarlsonj int
ffs(arg_t bits)4269bb4bb4Scarlsonj ffs(arg_t bits)
4369bb4bb4Scarlsonj {
4469bb4bb4Scarlsonj 	int i;
4569bb4bb4Scarlsonj 
4669bb4bb4Scarlsonj 	if (bits == 0)
4769bb4bb4Scarlsonj 		return (0);
4869bb4bb4Scarlsonj 	for (i = 1; ; i++, bits >>= 1) {
4969bb4bb4Scarlsonj 		if (bits & 1)
5069bb4bb4Scarlsonj 			break;
5169bb4bb4Scarlsonj 	}
5269bb4bb4Scarlsonj 	return (i);
5369bb4bb4Scarlsonj }
54