spinn_common 7.1.1
Support code for SpiNNaker applications.
Loading...
Searching...
No Matches
bit_field.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 The University of Manchester
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
64#ifndef __BIT_FIELD_H__
65#define __BIT_FIELD_H__
66
67#include <stdint.h>
68#include <stdbool.h>
69
73typedef uint32_t* bit_field_t;
74
75#ifndef __SIZE_T__
77typedef uint32_t size_t;
78#define __SIZE_T__
79#endif /*__SIZE_T__*/
80
81#ifndef __INDEX_T__
83typedef uint32_t index_t;
84#define __INDEX_T__
85#endif /*__INDEX_T__*/
86
87#ifndef __COUNTER_T__
89typedef uint32_t counter_t;
90#define __COUNTER_T__
91#endif /*__COUNTER_T__*/
92
97static inline bool bit_field_test(
99 index_t n)
100{
101 return (b[n >> 5] & (1 << (n & 0x1F))) != 0;
102}
103
107static inline void bit_field_clear(
108 bit_field_t restrict b,
109 index_t n)
110{
111 b[n >> 5] &= ~(1 << (n & 0x1F));
112}
113
117static inline void bit_field_set(
118 bit_field_t restrict b,
119 index_t n)
120{
121 b[n >> 5] |= 1 << (n & 0x1F);
122}
123
127static inline void not_bit_field(
128 bit_field_t restrict b,
129 size_t s)
130{
131 for ( ; s > 0; s--) {
132 b[s-1] = ~ b[s-1];
133 }
134}
135
141static inline void and_bit_fields(
142 bit_field_t restrict b1,
143 const bit_field_t restrict b2,
144 size_t s)
145{
146 for ( ; s > 0; s--) {
147 b1[s-1] &= b2[s-1];
148 }
149}
150
156static inline void or_bit_fields(
157 bit_field_t restrict b1,
158 const bit_field_t restrict b2,
159 size_t s)
160{
161 for ( ; s > 0; s--) {
162 b1[s-1] |= b2[s-1];
163 }
164}
165
169static inline void clear_bit_field(
170 bit_field_t restrict b,
171 size_t s)
172{
173 for ( ; s > 0; s--) {
174 b[s-1] = 0;
175 }
176}
177
181static inline void set_bit_field(
182 bit_field_t restrict b,
183 size_t s)
184{
185 for ( ; s > 0; s--) {
186 b[s-1] = 0xFFFFFFFF;
187 }
188}
189
194static inline bool empty_bit_field(
195 const bit_field_t restrict b,
196 size_t s)
197{
198 bool empty = true;
199
200 for ( ; s > 0; s--) {
201 empty = empty && (b[s-1] == 0);
202 }
203 return empty;
204}
205
211static inline bool nonempty_bit_field(
212 const bit_field_t restrict b,
213 size_t s)
214{
215 return !empty_bit_field(b, s);
216}
217
222static inline size_t get_bit_field_size(
223 size_t bits)
224{
225 // **NOTE** in floating point terms this is ceil(num_neurons / 32)
226 const uint32_t bits_to_words_shift = 5;
227 const uint32_t bits_to_words_remainder = (1 << bits_to_words_shift) - 1;
228
229 // Down shift number of bits to words
230 uint32_t words = bits >> bits_to_words_shift;
231
232 // If there was a remainder, add an extra word
233 if ((bits & bits_to_words_remainder) != 0) {
234 words++;
235 }
236 return words;
237}
238
243static inline int count_bit_field(
244 const bit_field_t restrict b,
245 size_t s)
246{
247 int sum = 0;
248
249 for ( ; s > 0; s--) {
250 sum += __builtin_popcount(b[s - 1]);
251 }
252 return sum;
253}
254
259void print_bit_field_bits(const bit_field_t restrict b, size_t s);
260
265void print_bit_field(const bit_field_t restrict b, size_t s);
266
270void random_bit_field(bit_field_t restrict b, size_t s);
271
275bit_field_t bit_field_alloc(uint32_t n_atoms);
276
277#endif /*__BIT_FIELD_H__*/
static void or_bit_fields(bit_field_t restrict b1, const bit_field_t restrict b2, size_t s)
This function ors two bit_fields together.
Definition bit_field.h:156
static bool empty_bit_field(const bit_field_t restrict b, size_t s)
This function tests whether a bit_field is all zeros.
Definition bit_field.h:194
uint32_t size_t
An unsigned integer used for the size of objects.
Definition bit_field.h:77
static void set_bit_field(bit_field_t restrict b, size_t s)
This function sets an entire bit_field.
Definition bit_field.h:181
static void not_bit_field(bit_field_t restrict b, size_t s)
This function negates the bits of an entire bit_field.
Definition bit_field.h:127
static size_t get_bit_field_size(size_t bits)
A function that calculates the size of a bit_field to hold 'bits' bits.
Definition bit_field.h:222
static bool bit_field_test(bit_field_t b, index_t n)
This function tests a particular bit of a bit_field.
Definition bit_field.h:97
uint32_t * bit_field_t
bit_field_t is an arbitrary length bit field (vector of bits) which is used to compactly represent a ...
Definition bit_field.h:73
static void bit_field_clear(bit_field_t restrict b, index_t n)
This function clears a particular bit of a bit_field.
Definition bit_field.h:107
static void bit_field_set(bit_field_t restrict b, index_t n)
This function sets a particular bit of a bit_field.
Definition bit_field.h:117
static void clear_bit_field(bit_field_t restrict b, size_t s)
This function clears an entire bit_field.
Definition bit_field.h:169
static void and_bit_fields(bit_field_t restrict b1, const bit_field_t restrict b2, size_t s)
This function ands two bit_fields together.
Definition bit_field.h:141
static int count_bit_field(const bit_field_t restrict b, size_t s)
Computes the number of set bits in a bit_field.
Definition bit_field.h:243
void random_bit_field(bit_field_t restrict b, size_t s)
Generates a random bit_field for testing purposes.
Definition bit_field.c:116
uint32_t index_t
An unsigned integer used as an index.
Definition bit_field.h:83
void print_bit_field(const bit_field_t restrict b, size_t s)
This function prints out an entire bit_field, as a sequence of hexadecimal numbers,...
Definition bit_field.c:103
void print_bit_field_bits(const bit_field_t restrict b, size_t s)
This function prints out an entire bit_field, as a sequence of ones and zeros.
Definition bit_field.c:90
bit_field_t bit_field_alloc(uint32_t n_atoms)
allocates a bit_field_t object
Definition bit_field.c:129
static bool nonempty_bit_field(const bit_field_t restrict b, size_t s)
Testing whether a bit_field is non-empty, i.e. if there is at least one bit set.
Definition bit_field.h:211
uint32_t counter_t
An unsigned integer used as a counter or iterator.
Definition bit_field.h:89