root/Whitix/trunk/include/bitmap.h

Revision 2007, 1.8 KB (checked in by mwhitworth, 3 years ago)

Add _BitFindFirstSet and fls functions.

Line 
1/*  This file is part of Whitix.
2 *
3 *  Whitix is free software; you can redistribute it and/or modify
4 *  it under the terms of the GNU General Public License as published by
5 *  the Free Software Foundation; either version 2 of the License, or
6 *  (at your option) any later version.
7 *
8 *  Whitix is distributed in the hope that it will be useful,
9 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 *  GNU General Public License for more details.
12 *
13 *  You should have received a copy of the GNU General Public License
14 *  along with Whitix; if not, write to the Free Software
15 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
16 *
17 */
18 
19/* General bit functions */
20
21#ifndef BITMAP_H
22#define BITMAP_H
23
24#include <typedefs.h>
25
26/* A good utility function really */
27void BmapSetBit(void* array,DWORD index,int value);
28/* Returns non-zero if set */
29int BmapGetBit(void* array,DWORD index);
30
31/* Adapted from Linux. */
32static inline int BitFindFirstZero(void* array, int size)
33{
34        int d0, d1, d2; /* Temporary variables. */
35        int result;
36       
37        if (!size)
38                return 0;
39               
40        asm volatile(
41        "movl $-1,%%eax\n\t"
42        "xorl %%edx,%%edx\n\t"
43        "repe; scasl\n\t"
44        "je 1f\n\t"
45        "xorl -4(%%edi),%%eax\n\t"
46        "subl $4,%%edi\n\t"
47        "bsfl %%eax,%%edx\n"
48        "1:\tsubl %%ebx,%%edi\n\t"
49        "shll $3,%%edi\n\t"
50        "addl %%edi,%%edx"
51        :"=d" (result), "=&c" (d0), "=&D" (d1), "=&a" (d2)
52        :"1" ((size + 31) >> 5), "2" (array), "b" (array) : "memory");
53
54        return result;
55}
56
57static inline int _BitFindFirstSet(unsigned long word)
58{
59        asm("bsf %1, %0" : "=r"(word) : "rm"(word));
60        return word;
61}
62
63/* Adapted from Linux too. */
64static inline int fls(unsigned long word)
65{
66        if(word == 0)
67                return -1;
68       
69        asm("bsr %1,%0"
70                : "=r" (word)
71                : "rm" (word));
72        return (int) word;
73}
74
75#endif
Note: See TracBrowser for help on using the browser.