]> git.saurik.com Git - apple/libplatform.git/blame - src/string/generic/ffsll.c
libplatform-254.40.4.tar.gz
[apple/libplatform.git] / src / string / generic / ffsll.c
CommitLineData
ada7c492
A
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30#include <TargetConditionals.h>
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: src/lib/libc/string/ffsll.c,v 1.1 2008/11/03 10:22:19 kib Exp $");
33
34#include <strings.h>
35
36/*
37 * Find First Set bit
38 */
39int
40ffsll(long long mask)
41{
42#if __has_builtin(__builtin_ffsll)
43 return __builtin_ffsll(mask);
44#elif __has_builtin(__builtin_ctzll)
45 if (mask == 0)
46 return (0);
47
48 return __builtin_ctzll(mask) + 1;
49#else
50 int bit;
51
52 if (mask == 0)
53 return (0);
54 for (bit = 1; !(mask & 1); bit++)
55 mask = (unsigned long long)mask >> 1;
56 return (bit);
57#endif
58}
59
438624e0 60#if VARIANT_DYLD && TARGET_OS_SIMULATOR
ada7c492
A
61int
62ffsl(long mask)
63{
64#if __has_builtin(__builtin_ffsl)
65 return __builtin_ffsl(mask);
66#elif __has_builtin(__builtin_ctzl)
67 if (mask == 0)
68 return (0);
69
70 return __builtin_ctzl(mask) + 1;
71#else
72 int bit;
73
74 if (mask == 0)
75 return (0);
76 for (bit = 1; !(mask & 1); bit++)
77 mask = (unsigned long)mask >> 1;
78 return (bit);
79#endif
80}
81
82int
83ffs(int mask)
84{
85#if __has_builtin(__builtin_ffs)
86 return __builtin_ffs(mask);
87#elif __has_builtin(__builtin_ctz)
88 if (mask == 0)
89 return (0);
90
91 return __builtin_ctz(mask) + 1;
92#else
93 int bit;
94
95 if (mask == 0)
96 return (0);
97 for (bit = 1; !(mask & 1); bit++)
98 mask = (unsigned)mask >> 1;
99 return (bit);
100#endif
101}
102#endif
103