diff options
Diffstat (limited to 'src/lib/libc/string/ffs.c')
-rw-r--r-- | src/lib/libc/string/ffs.c | 70 |
1 files changed, 32 insertions, 38 deletions
diff --git a/src/lib/libc/string/ffs.c b/src/lib/libc/string/ffs.c index 42bc87ddea..887ce437d4 100644 --- a/src/lib/libc/string/ffs.c +++ b/src/lib/libc/string/ffs.c | |||
@@ -1,42 +1,19 @@ | |||
1 | /*- | 1 | /* $OpenBSD: ffs.c,v 1.5 2000/07/02 03:10:38 mickey Exp $ */ |
2 | * Copyright (c) 1990 The Regents of the University of California. | 2 | |
3 | * All rights reserved. | 3 | /* |
4 | * | 4 | * Public domain. |
5 | * Redistribution and use in source and binary forms, with or without | 5 | * Written by Dale Rahn. |
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 | */ | 6 | */ |
33 | 7 | ||
34 | #if defined(LIBC_SCCS) && !defined(lint) | 8 | #if defined(LIBC_SCCS) && !defined(lint) |
35 | /*static char *sccsid = "from: @(#)ffs.c 5.4 (Berkeley) 5/17/90";*/ | 9 | static char *rcsid = "$OpenBSD: ffs.c,v 1.5 2000/07/02 03:10:38 mickey Exp $"; |
36 | static char *rcsid = "$Id: ffs.c,v 1.1.1.1 1995/10/18 08:42:21 deraadt Exp $"; | ||
37 | #endif /* LIBC_SCCS and not lint */ | 10 | #endif /* LIBC_SCCS and not lint */ |
38 | 11 | ||
12 | #if !defined(_KERNEL) && !defined(_STANDALONE) | ||
39 | #include <string.h> | 13 | #include <string.h> |
14 | #else | ||
15 | #include <lib/libkern/libkern.h> | ||
16 | #endif | ||
40 | 17 | ||
41 | /* | 18 | /* |
42 | * ffs -- vax ffs instruction | 19 | * ffs -- vax ffs instruction |
@@ -46,10 +23,27 @@ ffs(mask) | |||
46 | register int mask; | 23 | register int mask; |
47 | { | 24 | { |
48 | register int bit; | 25 | register int bit; |
26 | register unsigned int r = mask; | ||
27 | static const signed char t[16] = { | ||
28 | -28, 1, 2, 1, | ||
29 | 3, 1, 2, 1, | ||
30 | 4, 1, 2, 1, | ||
31 | 3, 1, 2, 1 | ||
32 | }; | ||
33 | |||
34 | bit = 0; | ||
35 | if (!(r & 0xffff)) { | ||
36 | bit += 16; | ||
37 | r >>= 16; | ||
38 | } | ||
39 | if (!(r & 0xff)) { | ||
40 | bit += 8; | ||
41 | r >>= 8; | ||
42 | } | ||
43 | if (!(r & 0xf)) { | ||
44 | bit += 4; | ||
45 | r >>= 4; | ||
46 | } | ||
49 | 47 | ||
50 | if (mask == 0) | 48 | return (bit + t[ r & 0xf ]); |
51 | return(0); | ||
52 | for (bit = 1; !(mask & 1); bit++) | ||
53 | mask >>= 1; | ||
54 | return(bit); | ||
55 | } | 49 | } |