xref: /freebsd-src/lib/libc/string/flsll.c (revision 559a218c9b257775fb249b67945fe4a05b7a6b9f)
14a723bd2SKonstantin Belousov /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
44a723bd2SKonstantin Belousov  * Copyright (c) 1990, 1993
54a723bd2SKonstantin Belousov  *	The Regents of the University of California.  All rights reserved.
6*ee8b0c43SRobert Clausecker  * Copyright (c) 2023 The FreeBSD Foundation
7*ee8b0c43SRobert Clausecker  *
8*ee8b0c43SRobert Clausecker  * Portions of this software were developed by Robert Clausecker
9*ee8b0c43SRobert Clausecker  * <fuz@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
104a723bd2SKonstantin Belousov  *
114a723bd2SKonstantin Belousov  * Redistribution and use in source and binary forms, with or without
124a723bd2SKonstantin Belousov  * modification, are permitted provided that the following conditions
134a723bd2SKonstantin Belousov  * are met:
144a723bd2SKonstantin Belousov  * 1. Redistributions of source code must retain the above copyright
154a723bd2SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer.
164a723bd2SKonstantin Belousov  * 2. Redistributions in binary form must reproduce the above copyright
174a723bd2SKonstantin Belousov  *    notice, this list of conditions and the following disclaimer in the
184a723bd2SKonstantin Belousov  *    documentation and/or other materials provided with the distribution.
193fb3b97cSEd Maste  * 3. Neither the name of the University nor the names of its contributors
204a723bd2SKonstantin Belousov  *    may be used to endorse or promote products derived from this software
214a723bd2SKonstantin Belousov  *    without specific prior written permission.
224a723bd2SKonstantin Belousov  *
234a723bd2SKonstantin Belousov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244a723bd2SKonstantin Belousov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254a723bd2SKonstantin Belousov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264a723bd2SKonstantin Belousov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274a723bd2SKonstantin Belousov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284a723bd2SKonstantin Belousov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294a723bd2SKonstantin Belousov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304a723bd2SKonstantin Belousov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314a723bd2SKonstantin Belousov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324a723bd2SKonstantin Belousov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334a723bd2SKonstantin Belousov  * SUCH DAMAGE.
344a723bd2SKonstantin Belousov  */
354a723bd2SKonstantin Belousov 
36*ee8b0c43SRobert Clausecker #include <limits.h>
374a723bd2SKonstantin Belousov #include <strings.h>
384a723bd2SKonstantin Belousov 
394a723bd2SKonstantin Belousov /*
404a723bd2SKonstantin Belousov  * Find Last Set bit
414a723bd2SKonstantin Belousov  */
424a723bd2SKonstantin Belousov int
flsll(long long mask)434a723bd2SKonstantin Belousov flsll(long long mask)
444a723bd2SKonstantin Belousov {
45*ee8b0c43SRobert Clausecker 	return (mask == 0 ? 0 : CHAR_BIT * sizeof(mask) - __builtin_clzll(mask));
464a723bd2SKonstantin Belousov }
47