| 1 |
/* |
| 2 |
* Copyright (C) 2021 Icecream95 |
| 3 |
* SPDX-License-Identifier: MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
/* A nodearray is an array type that is either sparse or dense, depending on |
| 7 |
* the number of elements. |
| 8 |
* |
| 9 |
* When the number of elements is over a threshold (max_sparse), the dense mode |
| 10 |
* is used, and the nodearray is simply a container for an array. |
| 11 |
* |
| 12 |
* In sparse mode, the array has elements with a 24-bit node index and a value. |
| 13 |
* The nodes are always sorted, so that a binary search can be used to find |
| 14 |
* elements. Nonexistent elements are treated as zero. |
| 15 |
* |
| 16 |
* Function names follow ARM instruction names: orr does *elem |= value. |
| 17 |
* |
| 18 |
* Although it's probably already fast enough, the datastructure could be sped |
| 19 |
* up a lot, especially when NEON is available, by making the sparse mode store |
| 20 |
* sixteen adjacent values, so that adding new keys also allocates nearby keys, |
| 21 |
* and to allow for vectorising iteration, as can be done when in the dense |
| 22 |
* mode. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifndef __BIFROST_NODEARRAY_H |
| 26 |
#define __BIFROST_NODEARRAY_H |
| 27 |
|
| 28 |
#include <stdint.h> |
| 29 |
|
| 30 |
#ifdef __cplusplus |
| 31 |
extern "C" { |
| 32 |
#endif |
| 33 |
|
| 34 |
/* A value that may be stored in a nodearray element, used directly for dense |
| 35 |
* elements and included into sparse elements. |
| 36 |
*/ |
| 37 |
typedef uint16_t nodearray_value; |
| 38 |
|
| 39 |
#define NODEARRAY_MAX_VALUE 0xffff |
| 40 |
|
| 41 |
/* Type storing sparse nodearray elements, consisting of a nodearray_value at |
| 42 |
* the bottom and a nodearray_key at the top. |
| 43 |
*/ |
| 44 |
typedef uint64_t nodearray_sparse; |
| 45 |
|
| 46 |
typedef struct { |
| 47 |
union { |
| 48 |
nodearray_sparse *sparse; |
| 49 |
nodearray_value *dense; |
| 50 |
}; |
| 51 |
unsigned size; |
| 52 |
unsigned sparse_capacity; |
| 53 |
} nodearray; |
| 54 |
|
| 55 |
/* Align sizes to 16-bytes for SIMD purposes */ |
| 56 |
#define NODEARRAY_DENSE_ALIGN(x) ALIGN_POT(x, 16) |
| 57 |
|
| 58 |
#define nodearray_sparse_foreach(buf, elem) \ |
| 59 |
for (nodearray_sparse *elem = (buf)->sparse; \ |
| 60 |
elem < (buf)->sparse + (buf)->size; elem++) |
| 61 |
|
| 62 |
#define nodearray_dense_foreach(buf, elem) \ |
| 63 |
for (nodearray_value *elem = (buf)->dense; \ |
| 64 |
elem < (buf)->dense + (buf)->size; elem++) |
| 65 |
|
| 66 |
#define nodearray_dense_foreach_64(buf, elem) \ |
| 67 |
for (uint64_t *elem = (uint64_t *)(buf)->dense; \ |
| 68 |
(nodearray_value *)elem < (buf)->dense + (buf)->size; elem++) |
| 69 |
|
| 70 |
static inline bool |
| 71 |
nodearray_is_sparse(const nodearray *a) |
| 72 |
{ |
| 73 |
return a->sparse_capacity != ~0U; |
| 74 |
} |
| 75 |
|
| 76 |
static inline void |
| 77 |
nodearray_init(nodearray *a) |
| 78 |
{ |
| 79 |
memset(a, 0, sizeof(nodearray)); |
| 80 |
} |
| 81 |
|
| 82 |
static inline void |
| 83 |
nodearray_reset(nodearray *a) |
| 84 |
{ |
| 85 |
free(a->sparse); |
| 86 |
nodearray_init(a); |
| 87 |
} |
| 88 |
|
| 89 |
static inline nodearray_sparse |
| 90 |
nodearray_encode(unsigned key, nodearray_value value) |
| 91 |
{ |
| 92 |
static_assert(sizeof(nodearray_value) == sizeof(uint16_t), "sizes mismatch"); |
| 93 |
return ((nodearray_sparse)key << 16) | value; |
| 94 |
} |
| 95 |
|
| 96 |
static inline unsigned |
| 97 |
nodearray_sparse_key(const nodearray_sparse *elem) |
| 98 |
{ |
| 99 |
static_assert(sizeof(nodearray_value) == sizeof(uint16_t), "sizes mismatch"); |
| 100 |
return *elem >> 16; |
| 101 |
} |
| 102 |
|
| 103 |
static inline nodearray_value |
| 104 |
nodearray_sparse_value(const nodearray_sparse *elem) |
| 105 |
{ |
| 106 |
return *elem & NODEARRAY_MAX_VALUE; |
| 107 |
} |
| 108 |
|
| 109 |
static inline unsigned |
| 110 |
nodearray_sparse_search(const nodearray *a, nodearray_sparse key, |
| 111 |
nodearray_sparse **elem) |
| 112 |
{ |
| 113 |
assert(nodearray_is_sparse(a) && a->size); |
| 114 |
|
| 115 |
nodearray_sparse *data = a->sparse; |
| 116 |
|
| 117 |
/* Encode the key using the highest possible value, so that the |
| 118 |
* matching node must be encoded lower than this |
| 119 |
*/ |
| 120 |
nodearray_sparse skey = nodearray_encode(key, NODEARRAY_MAX_VALUE); |
| 121 |
|
| 122 |
unsigned left = 0; |
| 123 |
unsigned right = a->size - 1; |
| 124 |
|
| 125 |
if (data[right] <= skey) |
| 126 |
left = right; |
| 127 |
|
| 128 |
while (left != right) { |
| 129 |
/* No need to worry about overflow, we couldn't have more than |
| 130 |
* 2^24 elements */ |
| 131 |
unsigned probe = (left + right + 1) / 2; |
| 132 |
|
| 133 |
if (data[probe] > skey) |
| 134 |
right = probe - 1; |
| 135 |
else |
| 136 |
left = probe; |
| 137 |
} |
| 138 |
|
| 139 |
*elem = data + left; |
| 140 |
return left; |
| 141 |
} |
| 142 |
|
| 143 |
static inline void |
| 144 |
nodearray_orr(nodearray *a, unsigned key, nodearray_value value, |
| 145 |
unsigned max_sparse, unsigned max) |
| 146 |
{ |
| 147 |
assert(key < (1 << 24)); |
| 148 |
assert(key < max); |
| 149 |
|
| 150 |
if (!value) |
| 151 |
return; |
| 152 |
|
| 153 |
if (nodearray_is_sparse(a)) { |
| 154 |
unsigned size = a->size; |
| 155 |
unsigned left = 0; |
| 156 |
|
| 157 |
if (size) { |
| 158 |
/* First, binary search for key */ |
| 159 |
nodearray_sparse *elem; |
| 160 |
left = nodearray_sparse_search(a, key, &elem); |
| 161 |
|
| 162 |
if (nodearray_sparse_key(elem) == key) { |
| 163 |
*elem |= value; |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
/* We insert before `left`, so increment it if it's |
| 168 |
* out of order */ |
| 169 |
if (nodearray_sparse_key(elem) < key) |
| 170 |
++left; |
| 171 |
} |
| 172 |
|
| 173 |
if (size < max_sparse && (size + 1) < max / 4) { |
| 174 |
/* We didn't find it, but we know where to insert it. */ |
| 175 |
|
| 176 |
nodearray_sparse *data = a->sparse; |
| 177 |
nodearray_sparse *data_move = data + left; |
| 178 |
|
| 179 |
bool realloc = (++a->size) > a->sparse_capacity; |
| 180 |
|
| 181 |
if (realloc) { |
| 182 |
a->sparse_capacity = |
| 183 |
MIN2(MAX2(a->sparse_capacity * 2, 64), max / 4); |
| 184 |
|
| 185 |
a->sparse = (nodearray_sparse *)malloc(a->sparse_capacity * |
| 186 |
sizeof(nodearray_sparse)); |
| 187 |
|
| 188 |
if (left) |
| 189 |
memcpy(a->sparse, data, left * sizeof(nodearray_sparse)); |
| 190 |
} |
| 191 |
|
| 192 |
nodearray_sparse *elem = a->sparse + left; |
| 193 |
|
| 194 |
if (left != size) |
| 195 |
memmove(elem + 1, data_move, |
| 196 |
(size - left) * sizeof(nodearray_sparse)); |
| 197 |
|
| 198 |
*elem = nodearray_encode(key, value); |
| 199 |
|
| 200 |
if (realloc) |
| 201 |
free(data); |
| 202 |
|
| 203 |
return; |
| 204 |
} |
| 205 |
|
| 206 |
/* There are too many elements, so convert to a dense array */ |
| 207 |
nodearray old = *a; |
| 208 |
|
| 209 |
a->dense = (nodearray_value *)calloc(NODEARRAY_DENSE_ALIGN(max), |
| 210 |
sizeof(nodearray_value)); |
| 211 |
a->size = max; |
| 212 |
a->sparse_capacity = ~0U; |
| 213 |
|
| 214 |
nodearray_value *data = a->dense; |
| 215 |
|
| 216 |
nodearray_sparse_foreach(&old, x) { |
| 217 |
unsigned key = nodearray_sparse_key(x); |
| 218 |
nodearray_value value = nodearray_sparse_value(x); |
| 219 |
|
| 220 |
assert(key < max); |
| 221 |
data[key] = value; |
| 222 |
} |
| 223 |
|
| 224 |
free(old.sparse); |
| 225 |
} |
| 226 |
|
| 227 |
a->dense[key] |= value; |
| 228 |
} |
| 229 |
|
| 230 |
#ifdef __cplusplus |
| 231 |
} /* extern C */ |
| 232 |
#endif |
| 233 |
|
| 234 |
#endif |