-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit_array_interleave.c
134 lines (113 loc) · 2.88 KB
/
bit_array_interleave.c
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
#include <stdlib.h>
#include <assert.h>
#include "bit_array.h"
#include "bit_array_interleave.h"
#define LOOKUP_TABLE_SIZE 256
#define BIT_COUNT 8
static void
fill_table(bit_array **table, uint8_t dim, uint8_t shift)
{
for (uint16_t i = 0; i < LOOKUP_TABLE_SIZE; i++) {
uint8_t num = i;
for (uint8_t j = 0; j < BIT_COUNT; j++) {
if (num & 1U) {
bit_array_set(table[i], j * dim + shift);
}
num >>= 1ULL;
}
}
}
static void
bit_array_interleave_free_dim_lookup_table(bit_array **table)
{
if (table == NULL) {
return;
}
for (uint16_t i = 0; i < LOOKUP_TABLE_SIZE; i++) {
if (table[i] != NULL) {
bit_array_free(table[i]);
table[i] = NULL;
}
}
free(table);
}
struct bit_array_interleave_lookup_table *
bit_array_interleave_new_lookup_tables(uint8_t dim)
{
assert(dim > 0);
/* Allocate memory for structure */
struct bit_array_interleave_lookup_table *table = calloc(1,
sizeof(struct bit_array_interleave_lookup_table));
if (table == NULL) {
return NULL;
}
table->dim = dim;
/* Allocate memory for buffer */
table->buffer = bit_array_create(dim);
if (table->buffer == NULL) {
free(table);
return NULL;
}
/* Allocate memory for lookup tables for each dimension */
table->tables = calloc(dim, sizeof(bit_array***));
if (table->tables == NULL) {
free(table->buffer);
free(table);
return NULL;
}
for(uint8_t i = 0; i < dim; i++) {
table->tables[i] = calloc(LOOKUP_TABLE_SIZE, sizeof(bit_array**));
if (table->tables[i] == NULL) {
bit_array_interleave_free_lookup_tables(table);
return NULL;
}
for (uint16_t j = 0; j < LOOKUP_TABLE_SIZE; j++) {
table->tables[i][j] = bit_array_create(dim);
if (table->tables[i] == NULL) {
bit_array_interleave_free_lookup_tables(table);
return NULL;
}
}
fill_table(table->tables[i], dim, i);
}
return table;
}
void
bit_array_interleave_free_lookup_tables(
struct bit_array_interleave_lookup_table *table)
{
if (table->tables != NULL) {
for (uint8_t i = 0; i < table->dim; i++) {
bit_array_interleave_free_dim_lookup_table(table->tables[i]);
table->tables[i] = NULL;
}
free(table->tables);
table->tables = NULL;
}
if (table->buffer != NULL) {
free(table->buffer);
table->buffer = NULL;
}
table->dim = 0;
free(table);
}
int
bit_array_interleave(struct bit_array_interleave_lookup_table *table,
const uint64_t *in, bit_array *out)
{
const uint8_t octets_count_in_word = 8;
const uint8_t octet_size_in_bits = 8;
bit_array_clear_all(table->buffer);
for (uint8_t i = 0; i < octets_count_in_word; i++) {
uint16_t shift = octet_size_in_bits * i;
for (uint8_t j = 0; j < table->dim; j++) {
uint8_t octet = (in[j] >> shift);
const bit_array *value = table->tables[j][octet];
bit_array_or(table->buffer, value);
}
bit_array_shift_left(table->buffer, table->dim * shift);
bit_array_or(out, table->buffer);
bit_array_clear_all(table->buffer);
}
return 0;
}