xref: /dflybsd-src/lib/libc/string/flsll.c (revision 0d5acd7467c4e95f792ef49fceb3ab8e917ce86b)
1716024cdSPeter Avalos /*-
2716024cdSPeter Avalos  * Copyright (c) 1990, 1993
3716024cdSPeter Avalos  *	The Regents of the University of California.  All rights reserved.
4716024cdSPeter Avalos  *
5716024cdSPeter Avalos  * Redistribution and use in source and binary forms, with or without
6716024cdSPeter Avalos  * modification, are permitted provided that the following conditions
7716024cdSPeter Avalos  * are met:
8716024cdSPeter Avalos  * 1. Redistributions of source code must retain the above copyright
9716024cdSPeter Avalos  *    notice, this list of conditions and the following disclaimer.
10716024cdSPeter Avalos  * 2. Redistributions in binary form must reproduce the above copyright
11716024cdSPeter Avalos  *    notice, this list of conditions and the following disclaimer in the
12716024cdSPeter Avalos  *    documentation and/or other materials provided with the distribution.
13dc71b7abSJustin C. Sherrill  * 3. Neither the name of the University nor the names of its contributors
14716024cdSPeter Avalos  *    may be used to endorse or promote products derived from this software
15716024cdSPeter Avalos  *    without specific prior written permission.
16716024cdSPeter Avalos  *
17716024cdSPeter Avalos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18716024cdSPeter Avalos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19716024cdSPeter Avalos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20716024cdSPeter Avalos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21716024cdSPeter Avalos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22716024cdSPeter Avalos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23716024cdSPeter Avalos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24716024cdSPeter Avalos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25716024cdSPeter Avalos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26716024cdSPeter Avalos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27716024cdSPeter Avalos  * SUCH DAMAGE.
28716024cdSPeter Avalos  *
29*0d5acd74SJohn Marino  * $FreeBSD: head/lib/libc/string/flsll.c 251069 2013-05-28 20:57:40Z emaste $
30716024cdSPeter Avalos  */
31716024cdSPeter Avalos 
32716024cdSPeter Avalos #include <strings.h>
33716024cdSPeter Avalos 
34716024cdSPeter Avalos /*
35716024cdSPeter Avalos  * Find Last Set bit
36716024cdSPeter Avalos  */
37716024cdSPeter Avalos int
flsll(long long mask)38716024cdSPeter Avalos flsll(long long mask)
39716024cdSPeter Avalos {
40716024cdSPeter Avalos 	int bit;
41716024cdSPeter Avalos 
42716024cdSPeter Avalos 	if (mask == 0)
43716024cdSPeter Avalos 		return (0);
44716024cdSPeter Avalos 	for (bit = 1; mask != 1; bit++)
45716024cdSPeter Avalos 		mask = (unsigned long long)mask >> 1;
46716024cdSPeter Avalos 	return (bit);
47716024cdSPeter Avalos }
48