1f434fe12SDag-Erling Smørgrav /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 4f434fe12SDag-Erling Smørgrav * Copyright (c) 1990, 1993 5f434fe12SDag-Erling Smørgrav * 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. 10f434fe12SDag-Erling Smørgrav * 11f434fe12SDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without 12f434fe12SDag-Erling Smørgrav * modification, are permitted provided that the following conditions 13f434fe12SDag-Erling Smørgrav * are met: 14f434fe12SDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright 15f434fe12SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer. 16f434fe12SDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright 17f434fe12SDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the 18f434fe12SDag-Erling Smørgrav * documentation and/or other materials provided with the distribution. 193fb3b97cSEd Maste * 3. Neither the name of the University nor the names of its contributors 20f434fe12SDag-Erling Smørgrav * may be used to endorse or promote products derived from this software 21f434fe12SDag-Erling Smørgrav * without specific prior written permission. 22f434fe12SDag-Erling Smørgrav * 23f434fe12SDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24f434fe12SDag-Erling Smørgrav * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25f434fe12SDag-Erling Smørgrav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26f434fe12SDag-Erling Smørgrav * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27f434fe12SDag-Erling Smørgrav * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28f434fe12SDag-Erling Smørgrav * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29f434fe12SDag-Erling Smørgrav * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30f434fe12SDag-Erling Smørgrav * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31f434fe12SDag-Erling Smørgrav * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32f434fe12SDag-Erling Smørgrav * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33f434fe12SDag-Erling Smørgrav * SUCH DAMAGE. 34f434fe12SDag-Erling Smørgrav */ 35f434fe12SDag-Erling Smørgrav 36f434fe12SDag-Erling Smørgrav #include <sys/cdefs.h> 37f434fe12SDag-Erling Smørgrav __FBSDID("$FreeBSD$"); 38f434fe12SDag-Erling Smørgrav 39*ee8b0c43SRobert Clausecker #include <limits.h> 40f434fe12SDag-Erling Smørgrav #include <strings.h> 41f434fe12SDag-Erling Smørgrav 42f434fe12SDag-Erling Smørgrav /* 43f434fe12SDag-Erling Smørgrav * Find Last Set bit 44f434fe12SDag-Erling Smørgrav */ 45f434fe12SDag-Erling Smørgrav int 46f434fe12SDag-Erling Smørgrav fls(int mask) 47f434fe12SDag-Erling Smørgrav { 48*ee8b0c43SRobert Clausecker return (mask == 0 ? 0 : CHAR_BIT * sizeof(mask) - __builtin_clz(mask)); 49f434fe12SDag-Erling Smørgrav } 50