summaryrefslogtreecommitdiffstats
blob: 045830d90bd6f583f2a60f1a801ff910d728d9c3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __CUTILS_BITOPS_H
#define __CUTILS_BITOPS_H

#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <sys/cdefs.h>

__BEGIN_DECLS

/*
 * Bitmask Operations
 *
 * Note this doesn't provide any locking/exclusion, and isn't atomic.
 * Additionally no bounds checking is done on the bitmask array.
 *
 * Example:
 *
 * int num_resources;
 * unsigned int resource_bits[BITS_TO_WORDS(num_resources)];
 * bitmask_init(resource_bits, num_resources);
 * ...
 * int bit = bitmask_ffz(resource_bits, num_resources);
 * bitmask_set(resource_bits, bit);
 * ...
 * if (bitmask_test(resource_bits, bit)) { ... }
 * ...
 * bitmask_clear(resource_bits, bit);
 *
 */

#define BITS_PER_WORD    (sizeof(unsigned int) * 8)
#define BITS_TO_WORDS(x) (((x) + BITS_PER_WORD - 1) / BITS_PER_WORD)
#define BIT_IN_WORD(x)   ((x) % BITS_PER_WORD)
#define BIT_WORD(x)      ((x) / BITS_PER_WORD)
#define BIT_MASK(x)      (1 << BIT_IN_WORD(x))

static inline void bitmask_init(unsigned int *bitmask, int num_bits)
{
    memset(bitmask, 0, BITS_TO_WORDS(num_bits)*sizeof(unsigned int));
}

static inline int bitmask_ffz(unsigned int *bitmask, int num_bits)
{
    int bit, result;
    size_t i;

    for (i = 0; i < BITS_TO_WORDS(num_bits); i++) {
        bit = ffs(~bitmask[i]);
        if (bit) {
            // ffs is 1-indexed, return 0-indexed result
            bit--;
            result = BITS_PER_WORD * i + bit;
            if (result >= num_bits)
                return -1;
            return result;
        }
    }
    return -1;
}

static inline int bitmask_weight(unsigned int *bitmask, int num_bits)
{
    size_t i;
    int weight = 0;

    for (i = 0; i < BITS_TO_WORDS(num_bits); i++)
        weight += __builtin_popcount(bitmask[i]);
    return weight;
}

static inline void bitmask_set(unsigned int *bitmask, int bit)
{
    bitmask[BIT_WORD(bit)] |= BIT_MASK(bit);
}

static inline void bitmask_clear(unsigned int *bitmask, int bit)
{
    bitmask[BIT_WORD(bit)] &= ~BIT_MASK(bit);
}

static inline bool bitmask_test(unsigned int *bitmask, int bit)
{
    return bitmask[BIT_WORD(bit)] & BIT_MASK(bit);
}

static inline int popcount(unsigned int x)
{
    return __builtin_popcount(x);
}

static inline int popcountl(unsigned long x)
{
    return __builtin_popcountl(x);
}

static inline int popcountll(unsigned long long x)
{
    return __builtin_popcountll(x);
}

__END_DECLS

#endif /* __CUTILS_BITOPS_H */