summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authormickey <>2000-07-02 01:00:01 +0000
committermickey <>2000-07-02 01:00:01 +0000
commite04c90bdf0ba56a88e9aea97e38ebc803f68310a (patch)
tree60b84ab3abe2ddd870d051e7d6158a3033e501b0 /src
parent08c90fefec0e6e20f6b74875e411ab9e628e0c20 (diff)
downloadopenbsd-e04c90bdf0ba56a88e9aea97e38ebc803f68310a.tar.gz
openbsd-e04c90bdf0ba56a88e9aea97e38ebc803f68310a.tar.bz2
openbsd-e04c90bdf0ba56a88e9aea97e38ebc803f68310a.zip
new version of ffs from drahn@
combines binary search and table lookup. tested on i386, sparc, ppc, hppa
Diffstat (limited to 'src')
-rw-r--r--src/lib/libc/string/ffs.c66
1 files changed, 28 insertions, 38 deletions
diff --git a/src/lib/libc/string/ffs.c b/src/lib/libc/string/ffs.c
index a0767e50ad..de4c205cd0 100644
--- a/src/lib/libc/string/ffs.c
+++ b/src/lib/libc/string/ffs.c
@@ -1,41 +1,15 @@
1/*- 1/* $OpenBSD: ffs.c,v 1.4 2000/07/02 01:00:01 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)
35static char *rcsid = "$OpenBSD: ffs.c,v 1.3 1996/08/19 08:34:01 tholo Exp $"; 9static char *rcsid = "$OpenBSD: ffs.c,v 1.4 2000/07/02 01:00:01 mickey Exp $";
36#endif /* LIBC_SCCS and not lint */ 10#endif /* LIBC_SCCS and not lint */
37 11
38#ifndef _KERNEL 12#if !defined(_KERNEL) && !defined(_STANDALONE)
39#include <string.h> 13#include <string.h>
40#else 14#else
41#include <lib/libkern/libkern.h> 15#include <lib/libkern/libkern.h>
@@ -49,10 +23,26 @@ ffs(mask)
49 register int mask; 23 register int mask;
50{ 24{
51 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 bit = 0;
34 if (0 == (r & 0xffff)) {
35 bit += 16;
36 r >>= 16;
37 }
38 if (0 == (r & 0xff)) {
39 bit += 8;
40 r >>= 8;
41 }
42 if (0 == (r & 0xf)) {
43 bit += 4;
44 r >>= 4;
45 }
52 46
53 if (mask == 0) 47 return (bit + t[ r & 0xf ]);
54 return(0);
55 for (bit = 1; !(mask & 1); bit++)
56 mask >>= 1;
57 return(bit);
58} 48}