xref: /onnv-gate/usr/src/common/util/ffs.c (revision 6812:febeba71273d)
12546Scarlsonj /*
22546Scarlsonj  * CDDL HEADER START
32546Scarlsonj  *
42546Scarlsonj  * The contents of this file are subject to the terms of the
52546Scarlsonj  * Common Development and Distribution License (the "License").
62546Scarlsonj  * You may not use this file except in compliance with the License.
72546Scarlsonj  *
82546Scarlsonj  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92546Scarlsonj  * or http://www.opensolaris.org/os/licensing.
102546Scarlsonj  * See the License for the specific language governing permissions
112546Scarlsonj  * and limitations under the License.
122546Scarlsonj  *
132546Scarlsonj  * When distributing Covered Code, include this CDDL HEADER in each
142546Scarlsonj  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152546Scarlsonj  * If applicable, add the following below this CDDL HEADER, with the
162546Scarlsonj  * fields enclosed by brackets "[]" replaced with your own identifying
172546Scarlsonj  * information: Portions Copyright [yyyy] [name of copyright owner]
182546Scarlsonj  *
192546Scarlsonj  * CDDL HEADER END
202546Scarlsonj  */
212546Scarlsonj 
222546Scarlsonj /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
242546Scarlsonj  * Use is subject to license terms.
252546Scarlsonj  */
262546Scarlsonj 
272546Scarlsonj #pragma ident	"%Z%%M%	%I%	%E% SMI"
282546Scarlsonj 
292546Scarlsonj /*
302546Scarlsonj  * Common implementation of ffs for kernel, mdb, and libc.  Note that mdb
312546Scarlsonj  * renames ffs into mdb_ffs to avoid user-space clashes with the signature of
322546Scarlsonj  * ffs(3C).
332546Scarlsonj  */
342546Scarlsonj 
352546Scarlsonj #if defined(_KERNEL) || defined(ffs)
362546Scarlsonj #include <sys/int_types.h>
372546Scarlsonj #define	arg_t	uintmax_t
382546Scarlsonj #else
392546Scarlsonj #define	arg_t	int
40*6812Sraf #include "lint.h"
412546Scarlsonj #endif
422546Scarlsonj 
432546Scarlsonj int
ffs(arg_t bits)442546Scarlsonj ffs(arg_t bits)
452546Scarlsonj {
462546Scarlsonj 	int i;
472546Scarlsonj 
482546Scarlsonj 	if (bits == 0)
492546Scarlsonj 		return (0);
502546Scarlsonj 	for (i = 1; ; i++, bits >>= 1) {
512546Scarlsonj 		if (bits & 1)
522546Scarlsonj 			break;
532546Scarlsonj 	}
542546Scarlsonj 	return (i);
552546Scarlsonj }
56