SpiNNFrontEndCommon 7.3.1
Common support code for user-facing front end systems.
Loading...
Searching...
No Matches
bit_set.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 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
19#ifndef __BIT_SET_H__
20#define __BIT_SET_H__
21
22// NOTE: THIS LOOKS AND ACTS LIKE A WRAPPER OVER WHAT SPINN_COMMON HAS ON
23// BIT_FIELD.h. IS LEFT DUE TO THE AMOUNT THAT IS USED IN THE COMPRESSOR.
24// DAMN WELL LOOKS LIKE A FILTER_INFO.h
25
26#include <bit_field.h>
27#include <stdint.h>
28#include <malloc_extras.h>
29#include "../common/constants.h"
30#include <debug.h>
31
33typedef struct _bit_set_t {
35 unsigned int count;
36
38 unsigned int n_words;
39
41 unsigned int n_elements;
42
44 uint32_t *_data;
45} bit_set_t;
46
51 // Clear the data
52 for (unsigned int i = 0; i < b->n_words; i++) {
53 b->_data[i] = 0x0;
54 }
55
56 // Reset the count
57 b->count = 0;
58 return true;
59}
60
65static inline bool bit_set_init(bit_set_t *b, unsigned int length) {
66 // Malloc space for the data
67 unsigned int n_words = length / BITS_IN_A_WORD;
68 if (length % BITS_IN_A_WORD) {
69 n_words++;
70 }
71
72 uint32_t *data = (uint32_t *) MALLOC(n_words * sizeof(uint32_t));
73 if (data == NULL) {
74 b->_data = NULL;
75 b->n_elements = 0;
76 b->n_words = 0;
77 return false;
78 }
79
80 b->_data = data;
81 b->n_words = n_words;
82 b->n_elements = length;
84 return true;
85}
86
89static inline void bit_set_delete(bit_set_t *b) {
90 // Free the storage
91 FREE(b->_data);
92 b->_data = NULL;
93 b->n_elements = 0;
94}
95
100static inline bool bit_set_add(bit_set_t* b, unsigned int i) {
101 if (b->n_elements <= i) {
102 return false;
103 }
104
105 // Determine the word and bit
106 unsigned int word = i / BITS_IN_A_WORD;
107 unsigned int bit = 1 << (i & 31);
108
109 // Set the word and bit
110 b->_data[word] |= bit;
111
112 // Increment the count of set elements
113 b->count++;
114 return true;
115}
116
121static inline bool bit_set_contains(bit_set_t *b, unsigned int i) {
122 if (b->n_elements <= i) {
123 return false;
124 }
125
126 // Determine the word and bit
127 unsigned int word = i / BITS_IN_A_WORD;
128 uint32_t bit = 1 << (i & 31);
129 return (bool) (b->_data[word] & bit);
130}
131
136static inline bool bit_set_remove(bit_set_t *b, unsigned int i) {
137 if (!bit_set_contains(b, i)) {
138 return false;
139 }
140 // Determine the word and bit
141 unsigned int word = i >> 5;
142 unsigned int bit = 1 << (i & 0x1f);
143
144 // Decrement the count of set elements
145 b->count--;
146
147 // Unset the bit of the appropriate word
148 b->_data[word] &= ~bit;
149 return true;
150}
151
156static inline void print_bit_field_entry_v2(uint32_t e, int offset) {
157 for (counter_t i = 32 ; i > 0; i--) {
158 log_debug("%d,%c", offset + i, ((e & 0x1) == 0) ? ' ' : '1');
159 e >>= 1;
160 }
161}
162
168 for (int i = s; i > 0; i--) {
169 print_bit_field_entry_v2(b[i], ((i - 1) * 32));
170 }
171}
172
178 for (int i = s; i > 0; i--) {
179 print_bit_field_entry_v2(b[i], ((i - 1) * BITS_IN_A_WORD));
180 }
181}
182
186 print_bit_set_bits(b._data, b.n_words);
187}
188
189#endif // __BIT_SET_H__
uint32_t * bit_field_t
uint32_t counter_t
bool bit_set_clear(bit_set_t *b)
Empty a bitset entirely.
Definition bit_set.h:50
static bool bit_set_add(bit_set_t *b, unsigned int i)
Add an element to a bitset.
Definition bit_set.h:100
unsigned int count
Keep track of members.
Definition bit_set.h:35
static void print_bit_field_entry_v2(uint32_t e, int offset)
This function prints out an individual word of a bit_field, as a sequence of ones and zeros.
Definition bit_set.h:156
unsigned int n_elements
Number of elements which may be in the set.
Definition bit_set.h:41
static bool bit_set_contains(bit_set_t *b, unsigned int i)
Test if an element is in a bitset.
Definition bit_set.h:121
void print_bit_set(bit_set_t b)
prints a bit set
Definition bit_set.h:185
void print_bit_field_bits_v2(bit_field_t b, size_t s)
This function prints out an entire bit_field, as a sequence of ones and zeros.
Definition bit_set.h:167
static bool bit_set_remove(bit_set_t *b, unsigned int i)
Remove an element from a bitset.
Definition bit_set.h:136
void print_bit_set_bits(bit_field_t b, int s)
This function prints out an entire bit_field, as a sequence of ones and zeros.
Definition bit_set.h:177
static bool bit_set_init(bit_set_t *b, unsigned int length)
Create a new bitset.
Definition bit_set.h:65
static void bit_set_delete(bit_set_t *b)
Destroy a bitset.
Definition bit_set.h:89
unsigned int n_words
Number of words in _data.
Definition bit_set.h:38
wrapper over bitfield
Definition bit_set.h:33
#define BITS_IN_A_WORD
bits in a word
Definition constants.h:43
static uint32_t data[ITEMS_PER_DATA_PACKET]
SpiNNaker debug header file.
void log_debug(const char *message,...)
This function logs debugging messages. This level of message is normally not printed except when the ...
Support for adding debugging information to dynamic allocation.
#define MALLOC
An easily-insertable name for the memory allocator.
#define FREE
An easily-insertable name for the memory free.
#define NULL