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.
6ee8b0c43SRobert Clausecker * Copyright (c) 2023 The FreeBSD Foundation
7ee8b0c43SRobert Clausecker *
8ee8b0c43SRobert Clausecker * Portions of this software were developed by Robert Clausecker
9ee8b0c43SRobert 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 <strings.h>
37f434fe12SDag-Erling Smørgrav
38f434fe12SDag-Erling Smørgrav /*
39f434fe12SDag-Erling Smørgrav * Find First Set bit
40f434fe12SDag-Erling Smørgrav */
41f434fe12SDag-Erling Smørgrav int
ffsl(long mask)42f434fe12SDag-Erling Smørgrav ffsl(long mask)
43f434fe12SDag-Erling Smørgrav {
44*3f5788e0SRobert Clausecker return (mask == 0 ? 0 : __builtin_ctzl(mask) + 1);
45f434fe12SDag-Erling Smørgrav }
46