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