M4RI  20140914
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 
26 typedef enum {
27  source_target, //< add from target matrix
28  source_source //< add from source matrix
29 } srctyp_t;
30 
35 typedef struct {
43 } djb_t;
44 
49 #define M4RI_DJB_BASE_SIZE 64
50 
58 static 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)
62  m4ri_die("malloc failed.\n");
63 
64  m->nrows = nrows;
65  m->ncols = ncols;
66  m->target = (rci_t*)malloc(sizeof(rci_t) * M4RI_DJB_BASE_SIZE);
67  m->source = (rci_t*)malloc(sizeof(rci_t) * M4RI_DJB_BASE_SIZE);
68  m->srctyp = (srctyp_t*)malloc(sizeof(srctyp_t) * M4RI_DJB_BASE_SIZE);
69  m->length = 0;
71 
72  if (m->target == NULL || m->source == NULL || m->srctyp == NULL)
73  m4ri_die("malloc failed.\n");
74  return m;
75 }
76 
83 static inline void djb_free(djb_t *m) {
84  free(m->target);
85  free(m->source);
86  free(m->srctyp);
87  free(m);
88 }
89 
99 static inline void djb_push_back(djb_t *z, rci_t target, rci_t source, srctyp_t srctyp) {
100  assert((target < z->nrows) &&
101  ((source < z->ncols) | (srctyp != source_source)) &&
102  ((source < z->nrows) | (srctyp != source_target)));
103  if (z->length >= z->allocated) {
105  z->target = (rci_t*)realloc(z->target, z->allocated*sizeof(rci_t));
106  z->source = (rci_t*)realloc(z->source, z->allocated*sizeof(rci_t));
107  z->srctyp = (srctyp_t*)realloc(z->srctyp, z->allocated*sizeof(srctyp_t));
108  }
109  z->target[z->length] = target;
110  z->source[z->length] = source;
111  z->srctyp[z->length] = srctyp;
112  z->length++;
113 }
114 
121 djb_t *djb_compile(mzd_t *A);
122 
133 void djb_apply_mzd(djb_t *z, mzd_t *W, const mzd_t *V);
134 
135 
140 static inline void djb_info(djb_t *z) {
141  double save = (double)z->length / (double)(z->nrows * z->ncols);
142  printf("%d x %d linear map in %d xors (cost: %.5f)\n", z->nrows, z->ncols, z->length, save);
143 }
144 
145 
146 #endif //M4RI_DJB_H
srctyp_t
Specify source type of addition.
Definition: djb.h:26
rci_t * target
Definition: djb.h:38
DJB's optimized linear maps mod 2.
Definition: djb.h:35
Dense matrices over GF(2).
Definition: mzd.h:86
rci_t nrows
Definition: djb.h:36
int rci_t
Type of row and column indexes.
Definition: misc.h:72
wi_t allocated
Definition: djb.h:42
void m4ri_die(const char *errormessage,...)
Print error message and abort().
Definition: misc.c:36
rci_t ncols
Definition: djb.h:37
static void djb_info(djb_t *z)
Definition: djb.h:140
#define M4RI_DJB_BASE_SIZE
Definition: djb.h:49
static void djb_free(djb_t *m)
Definition: djb.h:83
djb_t * djb_compile(mzd_t *A)
Definition: djb.c:112
static void djb_push_back(djb_t *z, rci_t target, rci_t source, srctyp_t srctyp)
Definition: djb.h:99
rci_t * source
Definition: djb.h:39
srctyp_t * srctyp
Definition: djb.h:40
int wi_t
Type of word indexes.
Definition: misc.h:80
rci_t length
Definition: djb.h:41
void djb_apply_mzd(djb_t *z, mzd_t *W, const mzd_t *V)
W = m*V.
Definition: djb.c:145
static djb_t * djb_init(rci_t nrows, rci_t ncols)
Definition: djb.h:58