~ubuntu-branches/ubuntu/hardy/haproxy/hardy

« back to all changes in this revision

Viewing changes to include/import/bitops.h

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Cornet
  • Date: 2007-08-17 09:33:41 UTC
  • Revision ID: james.westby@ubuntu.com-20070817093341-h0t6aeeoyzo25z3r
Tags: upstream-1.3.12.dfsg
ImportĀ upstreamĀ versionĀ 1.3.12.dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * bitops.h : macros and functions for bit operations.
 
3
 * (C) 2002 - Willy Tarreau - willy@ant-computing.com
 
4
 *
 
5
 */
 
6
 
 
7
#ifndef __BITOPS_H__
 
8
#define __BITOPS_H__
 
9
 
 
10
/* how many bits are needed to code the size of an int (eg: 32bits -> 5) */
 
11
#define LONGSHIFT       5
 
12
#define LLONGSHIFT      6
 
13
#define LONGBITS        32
 
14
#define LLONGBITS       64
 
15
 
 
16
/* very fast FFS function : returns the position of the lowest 1 */
 
17
#define __ffs_fast32(___a) ({ \
 
18
    register int ___x, ___bits = 32; \
 
19
    if (___a) { \
 
20
        ___x = (___a); \
 
21
        ___bits--; \
 
22
        if (___x & 0x0000ffff) { ___x &= 0x0000ffff; ___bits -= 16;} \
 
23
        if (___x & 0x00ff00ff) { ___x &= 0x00ff00ff; ___bits -=  8;} \
 
24
        if (___x & 0x0f0f0f0f) { ___x &= 0x0f0f0f0f; ___bits -=  4;} \
 
25
        if (___x & 0x33333333) { ___x &= 0x33333333; ___bits -=  2;} \
 
26
        if (___x & 0x55555555) { ___x &= 0x55555555; ___bits -=  1;} \
 
27
    }\
 
28
    ___bits; \
 
29
    })
 
30
 
 
31
/* very fast FLS function : returns the position of the highest 1  */
 
32
#define __fls_fast32(___a) ({ \
 
33
    register int ___x, ___bits = 0; \
 
34
    if (___a) { \
 
35
        ___x = (___a); \
 
36
        if (___x & 0xffff0000) { ___x &= 0xffff0000; ___bits += 16;} \
 
37
        if (___x & 0xff00ff00) { ___x &= 0xff00ff00; ___bits +=  8;} \
 
38
        if (___x & 0xf0f0f0f0) { ___x &= 0xf0f0f0f0; ___bits +=  4;} \
 
39
        if (___x & 0xcccccccc) { ___x &= 0xcccccccc; ___bits +=  2;} \
 
40
        if (___x & 0xaaaaaaaa) { ___x &= 0xaaaaaaaa; ___bits +=  1;} \
 
41
    } else { \
 
42
        ___bits = 32; \
 
43
    } \
 
44
    ___bits; \
 
45
    })
 
46
 
 
47
/* very fast FFS function working on 64 bits */
 
48
#define __ffs_fast64(___a) ({ \
 
49
    register int ___bits = 64; \
 
50
    register unsigned long  ___x = ((___a) >> 32); \
 
51
    if ((___a) & 0xffffffffUL) { \
 
52
        ___x = (___a) & 0xffffffffUL; \
 
53
        ___bits -= 32; \
 
54
    } \
 
55
    if (___x) { \
 
56
        ___bits--; \
 
57
        if (___x & 0x0000ffff) { ___x &= 0x0000ffff; ___bits -= 16;} \
 
58
        if (___x & 0x00ff00ff) { ___x &= 0x00ff00ff; ___bits -=  8;} \
 
59
        if (___x & 0x0f0f0f0f) { ___x &= 0x0f0f0f0f; ___bits -=  4;} \
 
60
        if (___x & 0x33333333) { ___x &= 0x33333333; ___bits -=  2;} \
 
61
        if (___x & 0x55555555) { ___x &= 0x55555555; ___bits -=  1;} \
 
62
    }\
 
63
    ___bits; \
 
64
    })
 
65
 
 
66
 
 
67
/* very fast FLS function working on 64 bits */
 
68
#define __fls_fast64(___a) ({ \
 
69
    register int ___bits = 0; \
 
70
    register unsigned long ___x = (___a); \
 
71
    if (((unsigned long long)(___a)) >> 32) { \
 
72
        ___x = ((unsigned long long)(___a)) >> 32; \
 
73
        ___bits += 32; \
 
74
    } \
 
75
    if (___x) { \
 
76
        if (___x & 0xffff0000) { ___x &= 0xffff0000; ___bits += 16;} \
 
77
        if (___x & 0xff00ff00) { ___x &= 0xff00ff00; ___bits +=  8;} \
 
78
        if (___x & 0xf0f0f0f0) { ___x &= 0xf0f0f0f0; ___bits +=  4;} \
 
79
        if (___x & 0xcccccccc) { ___x &= 0xcccccccc; ___bits +=  2;} \
 
80
        if (___x & 0xaaaaaaaa) { ___x &= 0xaaaaaaaa; ___bits +=  1;} \
 
81
    } else { \
 
82
         ___bits += 32; \
 
83
    } \
 
84
    ___bits; \
 
85
    })
 
86
 
 
87
static int ffs_fast32(register unsigned long a) {
 
88
    return __ffs_fast32(a);
 
89
}
 
90
 
 
91
static int fls_fast32(unsigned long a) {
 
92
    return __fls_fast32(a);
 
93
}
 
94
 
 
95
static int ffs_fast64(unsigned long long a) {
 
96
    return __ffs_fast64(a);
 
97
}
 
98
 
 
99
static int fls_fast64(unsigned long long a) {
 
100
    return __fls_fast64(a);
 
101
}
 
102
 
 
103
#endif /* __BITOPS_H__ */