diff options
Diffstat (limited to 'src/lib/libc/string/ffs.c')
-rw-r--r-- | src/lib/libc/string/ffs.c | 77 |
1 files changed, 33 insertions, 44 deletions
diff --git a/src/lib/libc/string/ffs.c b/src/lib/libc/string/ffs.c index 42bc87ddea..7dec1613a8 100644 --- a/src/lib/libc/string/ffs.c +++ b/src/lib/libc/string/ffs.c | |||
@@ -1,55 +1,44 @@ | |||
1 | /*- | 1 | /* $OpenBSD: ffs.c,v 1.7 2005/08/08 08:05:37 espie Exp $ */ |
2 | * Copyright (c) 1990 The Regents of the University of California. | ||
3 | * 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 | * 3. All advertising materials mentioning features or use of this software | ||
14 | * must display the following acknowledgement: | ||
15 | * This product includes software developed by the University of | ||
16 | * California, Berkeley and its contributors. | ||
17 | * 4. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | */ | ||
33 | 2 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 3 | /* |
35 | /*static char *sccsid = "from: @(#)ffs.c 5.4 (Berkeley) 5/17/90";*/ | 4 | * Public domain. |
36 | static char *rcsid = "$Id: ffs.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | 5 | * Written by Dale Rahn. |
37 | #endif /* LIBC_SCCS and not lint */ | 6 | */ |
38 | 7 | ||
8 | #if !defined(_KERNEL) && !defined(_STANDALONE) | ||
39 | #include <string.h> | 9 | #include <string.h> |
10 | #else | ||
11 | #include <lib/libkern/libkern.h> | ||
12 | #endif | ||
40 | 13 | ||
41 | /* | 14 | /* |
42 | * ffs -- vax ffs instruction | 15 | * ffs -- vax ffs instruction |
43 | */ | 16 | */ |
44 | int | 17 | int |
45 | ffs(mask) | 18 | ffs(int mask) |
46 | register int mask; | ||
47 | { | 19 | { |
48 | register int bit; | 20 | int bit; |
21 | unsigned int r = mask; | ||
22 | static const signed char t[16] = { | ||
23 | -28, 1, 2, 1, | ||
24 | 3, 1, 2, 1, | ||
25 | 4, 1, 2, 1, | ||
26 | 3, 1, 2, 1 | ||
27 | }; | ||
28 | |||
29 | bit = 0; | ||
30 | if (!(r & 0xffff)) { | ||
31 | bit += 16; | ||
32 | r >>= 16; | ||
33 | } | ||
34 | if (!(r & 0xff)) { | ||
35 | bit += 8; | ||
36 | r >>= 8; | ||
37 | } | ||
38 | if (!(r & 0xf)) { | ||
39 | bit += 4; | ||
40 | r >>= 4; | ||
41 | } | ||
49 | 42 | ||
50 | if (mask == 0) | 43 | return (bit + t[ r & 0xf ]); |
51 | return(0); | ||
52 | for (bit = 1; !(mask & 1); bit++) | ||
53 | mask >>= 1; | ||
54 | return(bit); | ||
55 | } | 44 | } |