summaryrefslogtreecommitdiff
path: root/firmware/include/bitarray.h
blob: a1e7a3fd5874523fc82ffa1c6227d813c2fc882e (plain)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2014 by Michael Sevakis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#ifndef BITARRAY_H
#define BITARRAY_H

/* Type-checked bit array definitions */

/* All this stuff gets optimized into very simple object code */

#define BITARRAY_WORD_BITS  \
     (sizeof (unsigned int) * 8)
#define BITARRAY_NWORDS(bits) \
    (((bits) + BITARRAY_WORD_BITS - 1) / BITARRAY_WORD_BITS)
#define BITARRAY_BITWORD(bitnum) \
    ((bitnum) / BITARRAY_WORD_BITS)
#define BITARRAY_WORDBIT(bitnum) \
    ((bitnum) % BITARRAY_WORD_BITS)
#define BITARRAY_NBIT(word, bit) \
    ((word)*BITARRAY_WORD_BITS + (bit))
#define BITARRAY_BITS(bits) \
    (BITARRAY_NWORDS(bits)*BITARRAY_WORD_BITS)
#define BITARRAY_BITN(bitnum) \
    (1u << BITARRAY_WORDBIT(bitnum))


/** Iterators **/
#include "config.h"
#include <stdint.h>
#include <limits.h>

#if (defined(CPU_ARM) && ARM_ARCH >= 5) || UINT32_MAX < UINT_MAX
#define __BITARRAY_CTZ(wval)    __builtin_ctz(wval)
#else
#include "system.h"
#define __BITARRAY_CTZ(wval)    find_first_set_bit(wval)
#endif
#define __BITARRAY_POPCNT(wval) __builtin_popcount(wval)

#ifndef BIT_N
#define BIT_N(n) (1u << (n))
#endif

/* Enumerate each word index */
#define FOR_EACH_BITARRAY_WORD_INDEX(nwords, index) \
    for (unsigned int index = 0, _nwords = (nwords); \
         index < _nwords; index++)

/* Enumerate each word value */
#define FOR_EACH_BITARRAY_WORD(a, wval) \
    FOR_EACH_BITARRAY_WORD_INDEX(ARRAYLEN((a)->words), _w) \
        for (unsigned int wval = (a)->words[_w], _ = 1; _; _--)

/* Enumerate the bit number of each set bit of a word in sequence */
#define FOR_EACH_BITARRAY_SET_WORD_BIT(wval, bit) \
    for (unsigned int _wval = (wval), bit;                 \
         _wval ? (((bit) = __BITARRAY_CTZ(_wval)), 1) : 0; \
         _wval &= ~BIT_N(bit))

/* Enumerate the bit number of each set bit in the bit array in sequence */
#define FOR_EACH_BITARRAY_SET_BIT_ARR(nwords, words, nbit) \
    FOR_EACH_BITARRAY_WORD_INDEX(nwords, _w)               \
        FOR_EACH_BITARRAY_SET_WORD_BIT(words[_w], _bit)    \
            for (unsigned int nbit = BITARRAY_NBIT(_w, _bit), _ = 1; _; _--)

/* As above but takes an array type for an argument */
#define FOR_EACH_BITARRAY_SET_BIT(a, nbit) \
    FOR_EACH_BITARRAY_SET_BIT_ARR(ARRAYLEN((a)->words), (a)->words, nbit)


/** Base functions (called by typed functions) **/

/* Return the word associated with the bit */
static inline unsigned int
__bitarray_get_word(unsigned int words[], unsigned int bitnum)
{
    return words[BITARRAY_BITWORD(bitnum)];
}

/* Set the word associated with the bit */
static inline void
__bitarray_set_word(unsigned int words[], unsigned int bitnum,
                    unsigned int wordval)
{
    words[BITARRAY_BITWORD(bitnum)] = wordval;
}

/* Set the bit at index 'bitnum' to '1' */
static inline void
__bitarray_set_bit(unsigned int words[], unsigned int bitnum)
{
    unsigned int word = BITARRAY_BITWORD(bitnum);
    unsigned int bit  = BITARRAY_BITN(bitnum);
    words[word] |= bit;
}

/* Set the bit at index 'bitnum' to '0' */
static inline void
__bitarray_clear_bit(unsigned int words[], unsigned int bitnum)
{
    unsigned int word = BITARRAY_BITWORD(bitnum);
    unsigned int bit  = BITARRAY_BITN(bitnum);
    words[word] &= ~bit;
}

/* Return the value of the specified bit ('0' or '1') */
static inline unsigned int
__bitarray_test_bit(const unsigned int words[], unsigned int bitnum)
{
    unsigned int word = BITARRAY_BITWORD(bitnum);
    unsigned int nbit = BITARRAY_WORDBIT(bitnum);
    return (words[word] >> nbit) & 1u;
}

/* Check if all bits in the bit array are '0' */
static inline bool
__bitarray_is_clear(const unsigned int words[], unsigned int nbits)
{
    FOR_EACH_BITARRAY_WORD_INDEX(BITARRAY_NWORDS(nbits), word)
    {
        if (words[word] != 0)
            return false;
    }

    return true;
}

/* Set every bit in the array to '0' */
static inline void
__bitarray_clear(unsigned int words[], unsigned int nbits)
{
    FOR_EACH_BITARRAY_WORD_INDEX(BITARRAY_NWORDS(nbits), word)
        words[word] = 0;
}

/* Set every bit in the array to '1' */
static inline void
__bitarray_set(unsigned int words[], unsigned int nbits)
{
    FOR_EACH_BITARRAY_WORD_INDEX(BITARRAY_NWORDS(nbits), word)
        words[word] = ~0u;
}

/* Find the lowest-indexed '1' bit in the bit array, returning the size of
   the array if none are set */
static inline unsigned int
__bitarray_ffs(const unsigned int words[], unsigned int nbits)
{
    FOR_EACH_BITARRAY_SET_BIT_ARR(BITARRAY_NWORDS(nbits), words, nbit)
        return nbit;

    return BITARRAY_BITS(nbits);
}

/* Return the number of bits currently set to '1' in the bit array */
static inline unsigned int
__bitarray_popcount(const unsigned int words[], unsigned int nbits)
{
    unsigned int count = 0;

    FOR_EACH_BITARRAY_WORD_INDEX(BITARRAY_NWORDS(nbits), word)
        count += __BITARRAY_POPCNT(words[word]);

    return count;
}

/**
 * Giant macro to define all the typed functions
 *   typename: The name of the type (e.g. myarr_t myarr;)
 *   fnprefix: The prefix all functions get (e.g. myarr_set_bit)
 *   nbits   : The minimum number of bits the array is meant to hold
 *             (the implementation rounds this up to the word size
 *              and all words may be fully utilized)
 *
 * uses 'typedef' to freely change from, e.g., struct to union without
 * changing source code
 */
#define BITARRAY_TYPE_DECLARE(typename, fnprefix, nbits) \
typedef struct                                                  \
{                                                               \
    unsigned int words[BITARRAY_NWORDS(nbits)];                 \
} typename;                                                     \
static inline unsigned int                                      \
fnprefix##_get_word(typename *array, unsigned int bitnum)       \
    { return __bitarray_get_word(array->words, bitnum); }       \
static inline void                                              \
fnprefix##_set_word(typename *array, unsigned int bitnum,       \
                    unsigned int wordval)                       \
    { __bitarray_set_word(array->words, bitnum, wordval); }     \
static inline void                                              \
fnprefix##_set_bit(typename *array, unsigned int bitnum)        \
    { __bitarray_set_bit(array->words, bitnum); }               \
static inline void                                              \
fnprefix##_clear_bit(typename *array, unsigned int bitnum)      \
    { __bitarray_clear_bit(array->words, bitnum); }             \
static inline unsigned int                                      \
fnprefix##_test_bit(const typename *array, unsigned int bitnum) \
    { return __bitarray_test_bit(array->words, bitnum); }       \
static inline bool                                              \
fnprefix##_is_clear(const typename *array)                      \
    { return __bitarray_is_clear(array->words, nbits); }        \
static inline void                                              \
fnprefix##_clear(typename *array)                               \
    { __bitarray_clear(array->words, nbits); }                  \
static inline void                                              \
fnprefix##_set(typename *array)                                 \
    { __bitarray_set(array->words, nbits); }                    \
static inline unsigned int                                      \
fnprefix##_ffs(const typename *array)                           \
    { return __bitarray_ffs(array->words, nbits); }             \
static inline unsigned int                                      \
fnprefix##_popcount(const typename *array)                      \
    { return __bitarray_popcount(array->words, nbits); }

#endif /* BITARRAY_H */