M4RI 20250128
djb.h
Go to the documentation of this file.
1
17#ifndef M4RI_DJB_H
18#define M4RI_DJB_H
19
20#include <m4ri/mzd.h>
21
26typedef enum {
27 source_target, //< add from target matrix
28 source_source //< add from source matrix
29} srctyp_t;
30
44
49#define M4RI_DJB_BASE_SIZE 64
50
58static inline djb_t *djb_init(rci_t nrows, rci_t ncols) {
59 /* we want to use realloc, so we call unaligned malloc */
60 djb_t *m = (djb_t *)malloc(sizeof(djb_t));
61 if (m == NULL) m4ri_die("malloc failed.\n");
62
63 m->nrows = nrows;
64 m->ncols = ncols;
65 m->target = (rci_t *)malloc(sizeof(rci_t) * M4RI_DJB_BASE_SIZE);
66 m->source = (rci_t *)malloc(sizeof(rci_t) * M4RI_DJB_BASE_SIZE);
67 m->srctyp = (srctyp_t *)malloc(sizeof(srctyp_t) * M4RI_DJB_BASE_SIZE);
68 m->length = 0;
70
71 if (m->target == NULL || m->source == NULL || m->srctyp == NULL) m4ri_die("malloc failed.\n");
72 return m;
73}
74
81static inline void djb_free(djb_t *m) {
82 free(m->target);
83 free(m->source);
84 free(m->srctyp);
85 free(m);
86}
87
97static inline void djb_push_back(djb_t *z, rci_t target, rci_t source, srctyp_t srctyp) {
98 assert((target < z->nrows) && ((source < z->ncols) | (srctyp != source_source)) &&
99 ((source < z->nrows) | (srctyp != source_target)));
100 if (z->length >= z->allocated) {
102 z->target = (rci_t *)realloc(z->target, z->allocated * sizeof(rci_t));
103 z->source = (rci_t *)realloc(z->source, z->allocated * sizeof(rci_t));
104 z->srctyp = (srctyp_t *)realloc(z->srctyp, z->allocated * sizeof(srctyp_t));
105 }
106 z->target[z->length] = target;
107 z->source[z->length] = source;
108 z->srctyp[z->length] = srctyp;
109 z->length++;
110}
111
119
130void djb_apply_mzd(djb_t *z, mzd_t *W, const mzd_t *V);
131
136static inline void djb_info(const djb_t *z) {
137 double save = (double)z->length / (double)(z->nrows * z->ncols);
138 printf("%d x %d linear map in %d xors (cost: %.5f)\n", z->nrows, z->ncols, z->length, save);
139}
140
141#endif // M4RI_DJB_H
static void djb_push_back(djb_t *z, rci_t target, rci_t source, srctyp_t srctyp)
Definition djb.h:97
static void djb_free(djb_t *m)
Definition djb.h:81
void djb_apply_mzd(djb_t *z, mzd_t *W, const mzd_t *V)
W = m*V.
Definition djb.c:142
srctyp_t
Specify source type of addition.
Definition djb.h:26
djb_t * djb_compile(mzd_t *A)
Definition djb.c:110
#define M4RI_DJB_BASE_SIZE
Definition djb.h:49
static djb_t * djb_init(rci_t nrows, rci_t ncols)
Definition djb.h:58
static void djb_info(const djb_t *z)
Definition djb.h:136
int rci_t
Type of row and column indexes.
Definition misc.h:72
int64_t wi_t
Type of word indexes.
Definition misc.h:81
Dense matrices over GF(2) represented as a bit field.
DJB's optimized linear maps mod 2.
Definition djb.h:35
rci_t length
Definition djb.h:41
rci_t ncols
Definition djb.h:37
rci_t nrows
Definition djb.h:36
wi_t allocated
Definition djb.h:42
srctyp_t * srctyp
Definition djb.h:40
rci_t * source
Definition djb.h:39
rci_t * target
Definition djb.h:38
Dense matrices over GF(2).
Definition mzd.h:68