Commit c28c67fab5ae4950ec1e2ef3514947b70f34ffdf

Authored by KMG
1 parent ee163abb
Exists in master and in 1 other branch v2

First (crude) pass at hooking GF-Complete into Jerasure. Just enough to do some testing before

doing the actual wiring.
Showing 3 changed files with 81 additions and 704 deletions   Show diff stats
galois.c
... ... @@ -52,377 +52,82 @@ POSSIBILITY OF SUCH DAMAGE.
52 52  
53 53 #include "galois.h"
54 54  
55   -#define NONE (10)
56   -#define TABLE (11)
57   -#define SHIFT (12)
58   -#define LOGS (13)
59   -#define SPLITW8 (14)
  55 +#define MAX_GF_INSTANCES 128
  56 +gf_t *gfp_array[MAX_GF_INSTANCES] = { 0 };
60 57  
61   -static int prim_poly[33] =
62   -{ 0,
63   -/* 1 */ 1,
64   -/* 2 */ 07,
65   -/* 3 */ 013,
66   -/* 4 */ 023,
67   -/* 5 */ 045,
68   -/* 6 */ 0103,
69   -/* 7 */ 0211,
70   -/* 8 */ 0435,
71   -/* 9 */ 01021,
72   -/* 10 */ 02011,
73   -/* 11 */ 04005,
74   -/* 12 */ 010123,
75   -/* 13 */ 020033,
76   -/* 14 */ 042103,
77   -/* 15 */ 0100003,
78   -/* 16 */ 0210013,
79   -/* 17 */ 0400011,
80   -/* 18 */ 01000201,
81   -/* 19 */ 02000047,
82   -/* 20 */ 04000011,
83   -/* 21 */ 010000005,
84   -/* 22 */ 020000003,
85   -/* 23 */ 040000041,
86   -/* 24 */ 0100000207,
87   -/* 25 */ 0200000011,
88   -/* 26 */ 0400000107,
89   -/* 27 */ 01000000047,
90   -/* 28 */ 02000000011,
91   -/* 29 */ 04000000005,
92   -/* 30 */ 010040000007,
93   -/* 31 */ 020000000011,
94   -/* 32 */ 00020000007 }; /* Really 40020000007, but we're omitting the high order bit */
95   -
96   -static int mult_type[33] =
97   -{ NONE,
98   -/* 1 */ TABLE,
99   -/* 2 */ TABLE,
100   -/* 3 */ TABLE,
101   -/* 4 */ TABLE,
102   -/* 5 */ TABLE,
103   -/* 6 */ TABLE,
104   -/* 7 */ TABLE,
105   -/* 8 */ TABLE,
106   -/* 9 */ TABLE,
107   -/* 10 */ LOGS,
108   -/* 11 */ LOGS,
109   -/* 12 */ LOGS,
110   -/* 13 */ LOGS,
111   -/* 14 */ LOGS,
112   -/* 15 */ LOGS,
113   -/* 16 */ LOGS,
114   -/* 17 */ LOGS,
115   -/* 18 */ LOGS,
116   -/* 19 */ LOGS,
117   -/* 20 */ LOGS,
118   -/* 21 */ LOGS,
119   -/* 22 */ LOGS,
120   -/* 23 */ SHIFT,
121   -/* 24 */ SHIFT,
122   -/* 25 */ SHIFT,
123   -/* 26 */ SHIFT,
124   -/* 27 */ SHIFT,
125   -/* 28 */ SHIFT,
126   -/* 29 */ SHIFT,
127   -/* 30 */ SHIFT,
128   -/* 31 */ SHIFT,
129   -/* 32 */ SPLITW8 };
130   -
131   -static int nw[33] = { 0, (1 << 1), (1 << 2), (1 << 3), (1 << 4),
132   -(1 << 5), (1 << 6), (1 << 7), (1 << 8), (1 << 9), (1 << 10),
133   -(1 << 11), (1 << 12), (1 << 13), (1 << 14), (1 << 15), (1 << 16),
134   -(1 << 17), (1 << 18), (1 << 19), (1 << 20), (1 << 21), (1 << 22),
135   -(1 << 23), (1 << 24), (1 << 25), (1 << 26), (1 << 27), (1 << 28),
136   -(1 << 29), (1 << 30), (1 << 31), -1 };
137   -
138   -static int nwm1[33] = { 0, (1 << 1)-1, (1 << 2)-1, (1 << 3)-1, (1 << 4)-1,
139   -(1 << 5)-1, (1 << 6)-1, (1 << 7)-1, (1 << 8)-1, (1 << 9)-1, (1 << 10)-1,
140   -(1 << 11)-1, (1 << 12)-1, (1 << 13)-1, (1 << 14)-1, (1 << 15)-1, (1 << 16)-1,
141   -(1 << 17)-1, (1 << 18)-1, (1 << 19)-1, (1 << 20)-1, (1 << 21)-1, (1 << 22)-1,
142   -(1 << 23)-1, (1 << 24)-1, (1 << 25)-1, (1 << 26)-1, (1 << 27)-1, (1 << 28)-1,
143   -(1 << 29)-1, (1 << 30)-1, 0x7fffffff, 0xffffffff };
144   -
145   -static int *galois_log_tables[33] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL,
146   -NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
147   -NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
148   -
149   -static int *galois_ilog_tables[33] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL,
150   -NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
151   -NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
152   -
153   -static int *galois_mult_tables[33] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL,
154   -NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
155   -NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
156   -
157   -static int *galois_div_tables[33] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL,
158   -NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
159   -NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
160   -
161   -/* Special case for w = 32 */
162   -
163   -static int *galois_split_w8[7] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL };
164   -
165   -int galois_create_log_tables(int w)
  58 +static void
  59 +galois_init_field(int w)
166 60 {
167   - int j, b;
  61 + if (w <= 0 || w > 128) {
  62 + fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
  63 + exit(1);
  64 + }
168 65  
169   - if (w > 30) return -1;
170   - if (galois_log_tables[w] != NULL) return 0;
171   - galois_log_tables[w] = (int *) malloc(sizeof(int)*nw[w]);
172   - if (galois_log_tables[w] == NULL) return -1;
173   -
174   - galois_ilog_tables[w] = (int *) malloc(sizeof(int)*nw[w]*3);
175   - if (galois_ilog_tables[w] == NULL) {
176   - free(galois_log_tables[w]);
177   - galois_log_tables[w] = NULL;
178   - return -1;
  66 + if (w > 32 && (w != 64 || w != 128)) {
  67 + fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
  68 + exit(1);
179 69 }
180   -
181   - for (j = 0; j < nw[w]; j++) {
182   - galois_log_tables[w][j] = nwm1[w];
183   - galois_ilog_tables[w][j] = 0;
184   - }
185   -
186   - b = 1;
187   - for (j = 0; j < nwm1[w]; j++) {
188   - if (galois_log_tables[w][b] != nwm1[w]) {
189   - fprintf(stderr, "Galois_create_log_tables Error: j=%d, b=%d, B->J[b]=%d, J->B[j]=%d (0%o)\n",
190   - j, b, galois_log_tables[w][b], galois_ilog_tables[w][j], (b << 1) ^ prim_poly[w]);
  70 + if (gfp_array[w] == NULL) {
  71 + gfp_array[w] = (gf_t*)malloc(sizeof(gf_t));
  72 + if (gfp_array[w] == NULL) {
  73 + fprintf(stderr, "ERROR -- cannot allocate memory for Galois field w=%d\n", w);
191 74 exit(1);
192 75 }
193   - galois_log_tables[w][b] = j;
194   - galois_ilog_tables[w][j] = b;
195   - b = b << 1;
196   - if (b & nw[w]) b = (b ^ prim_poly[w]) & nwm1[w];
197 76 }
198   - for (j = 0; j < nwm1[w]; j++) {
199   - galois_ilog_tables[w][j+nwm1[w]] = galois_ilog_tables[w][j];
200   - galois_ilog_tables[w][j+nwm1[w]*2] = galois_ilog_tables[w][j];
201   - }
202   - galois_ilog_tables[w] += nwm1[w];
203   - return 0;
204   -}
205   -
206   -int galois_logtable_multiply(int x, int y, int w)
207   -{
208   - int sum_j;
209   -
210   - if (x == 0 || y == 0) return 0;
211   -
212   - sum_j = galois_log_tables[w][x] + galois_log_tables[w][y];
213   - /* if (sum_j >= nwm1[w]) sum_j -= nwm1[w]; Don't need to do this,
214   - because we replicate the ilog table twice. */
215   - return galois_ilog_tables[w][sum_j];
216   -}
217   -
218   -int galois_logtable_divide(int x, int y, int w)
219   -{
220   - int sum_j;
221   - int z;
222 77  
223   - if (y == 0) return -1;
224   - if (x == 0) return 0;
225   - sum_j = galois_log_tables[w][x] - galois_log_tables[w][y];
226   - /* if (sum_j < 0) sum_j += nwm1[w]; Don't need to do this, because we replicate the ilog table twice. */
227   - z = galois_ilog_tables[w][sum_j];
228   - return z;
229   -}
230   -
231   -int galois_create_mult_tables(int w)
232   -{
233   - int j, x, y, logx;
234   -
235   - if (w >= 14) return -1;
236   -
237   - if (galois_mult_tables[w] != NULL) return 0;
238   - galois_mult_tables[w] = (int *) malloc(sizeof(int) * nw[w] * nw[w]);
239   - if (galois_mult_tables[w] == NULL) return -1;
240   -
241   - galois_div_tables[w] = (int *) malloc(sizeof(int) * nw[w] * nw[w]);
242   - if (galois_div_tables[w] == NULL) {
243   - free(galois_mult_tables[w]);
244   - galois_mult_tables[w] = NULL;
245   - return -1;
246   - }
247   - if (galois_log_tables[w] == NULL) {
248   - if (galois_create_log_tables(w) < 0) {
249   - free(galois_mult_tables[w]);
250   - free(galois_div_tables[w]);
251   - galois_mult_tables[w] = NULL;
252   - galois_div_tables[w] = NULL;
253   - return -1;
  78 + if (w == 16) {
  79 + if (!gf_init_hard(gfp_array[w], w, GF_MULT_SPLIT_TABLE, GF_REGION_ALTMAP, GF_DIVIDE_DEFAULT,
  80 + 0, 16, 4, NULL, NULL)) {
  81 + fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
  82 + exit(1);
254 83 }
255 84 }
256   -
257   - /* Set mult/div tables for x = 0 */
258   - j = 0;
259   - galois_mult_tables[w][j] = 0; /* y = 0 */
260   - galois_div_tables[w][j] = -1;
261   - j++;
262   - for (y = 1; y < nw[w]; y++) { /* y > 0 */
263   - galois_mult_tables[w][j] = 0;
264   - galois_div_tables[w][j] = 0;
265   - j++;
  85 + if (!gf_init_easy(gfp_array[w], w)) {
  86 + fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
  87 + exit(1);
266 88 }
267   -
268   - for (x = 1; x < nw[w]; x++) { /* x > 0 */
269   - galois_mult_tables[w][j] = 0; /* y = 0 */
270   - galois_div_tables[w][j] = -1;
271   - j++;
272   - logx = galois_log_tables[w][x];
273   - for (y = 1; y < nw[w]; y++) { /* y > 0 */
274   - galois_mult_tables[w][j] = galois_ilog_tables[w][logx+galois_log_tables[w][y]];
275   - galois_div_tables[w][j] = galois_ilog_tables[w][logx-galois_log_tables[w][y]];
276   - j++;
277   - }
278   - }
279   - return 0;
280 89 }
281 90  
282   -int galois_ilog(int value, int w)
  91 +void galois_change_technique(gf_t *gf, int w)
283 92 {
284   - if (galois_ilog_tables[w] == NULL) {
285   - if (galois_create_log_tables(w) < 0) {
286   - fprintf(stderr, "Error: galois_ilog - w is too big. Sorry\n");
287   - exit(1);
288   - }
  93 + if (w <= 0 || w > 128) {
  94 + fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
  95 + exit(1);
289 96 }
290   - return galois_ilog_tables[w][value];
291   -}
292 97  
293   -int galois_log(int value, int w)
294   -{
295   - if (galois_log_tables[w] == NULL) {
296   - if (galois_create_log_tables(w) < 0) {
297   - fprintf(stderr, "Error: galois_log - w is too big. Sorry\n");
298   - exit(1);
299   - }
  98 + if (w > 32 && (w != 64 || w != 128)) {
  99 + fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
  100 + exit(1);
300 101 }
301   - return galois_log_tables[w][value];
302   -}
303   -
304 102  
305   -int galois_shift_multiply(int x, int y, int w)
306   -{
307   - int prod;
308   - int i, j, ind;
309   - int k;
310   - int scratch[33];
311   -
312   - prod = 0;
313   - for (i = 0; i < w; i++) {
314   - scratch[i] = y;
315   - if (y & (1 << (w-1))) {
316   - y = y << 1;
317   - y = (y ^ prim_poly[w]) & nwm1[w];
318   - } else {
319   - y = y << 1;
320   - }
321   - }
322   - for (i = 0; i < w; i++) {
323   - ind = (1 << i);
324   - if (ind & x) {
325   - j = 1;
326   - for (k = 0; k < w; k++) {
327   - prod = prod ^ (j & scratch[i]);
328   - j = (j << 1);
329   - }
330   - }
331   - }
332   - return prod;
  103 + /*
  104 + * TODO: Sanity check on gf?
  105 + */
  106 + gfp_array[w] = gf;
333 107 }
334 108  
335 109 int galois_single_multiply(int x, int y, int w)
336 110 {
337   - int sum_j;
338   - int z;
339 111  
340 112 if (x == 0 || y == 0) return 0;
341 113  
342   - if (mult_type[w] == TABLE) {
343   - if (galois_mult_tables[w] == NULL) {
344   - if (galois_create_mult_tables(w) < 0) {
345   - fprintf(stderr, "ERROR -- cannot make multiplication tables for w=%d\n", w);
346   - exit(1);
347   - }
348   - }
349   - return galois_mult_tables[w][(x<<w)|y];
350   - } else if (mult_type[w] == LOGS) {
351   - if (galois_log_tables[w] == NULL) {
352   - if (galois_create_log_tables(w) < 0) {
353   - fprintf(stderr, "ERROR -- cannot make log tables for w=%d\n", w);
354   - exit(1);
355   - }
356   - }
357   - sum_j = galois_log_tables[w][x] + galois_log_tables[w][y];
358   - z = galois_ilog_tables[w][sum_j];
359   - return z;
360   - } else if (mult_type[w] == SPLITW8) {
361   - if (galois_split_w8[0] == NULL) {
362   - if (galois_create_split_w8_tables() < 0) {
363   - fprintf(stderr, "ERROR -- cannot make log split_w8_tables for w=%d\n", w);
364   - exit(1);
365   - }
366   - }
367   - return galois_split_w8_multiply(x, y);
368   - } else if (mult_type[w] == SHIFT) {
369   - return galois_shift_multiply(x, y, w);
  114 + if (gfp_array[w] == NULL) {
  115 + galois_init_field(w);
370 116 }
371   - fprintf(stderr, "Galois_single_multiply - no implementation for w=%d\n", w);
372   - exit(1);
373   -}
374 117  
375   -int galois_multtable_multiply(int x, int y, int w)
376   -{
377   - return galois_mult_tables[w][(x<<w)|y];
  118 + return gfp_array[w]->multiply.w32(gfp_array[w], x, y);
378 119 }
379 120  
380   -int galois_single_divide(int a, int b, int w)
  121 +int galois_single_divide(int x, int y, int w)
381 122 {
382   - int sum_j;
  123 + if (x == 0) return 0;
  124 + if (y == 0) return -1;
383 125  
384   - if (mult_type[w] == TABLE) {
385   - if (galois_div_tables[w] == NULL) {
386   - if (galois_create_mult_tables(w) < 0) {
387   - fprintf(stderr, "ERROR -- cannot make multiplication tables for w=%d\n", w);
388   - exit(1);
389   - }
390   - }
391   - return galois_div_tables[w][(a<<w)|b];
392   - } else if (mult_type[w] == LOGS) {
393   - if (b == 0) return -1;
394   - if (a == 0) return 0;
395   - if (galois_log_tables[w] == NULL) {
396   - if (galois_create_log_tables(w) < 0) {
397   - fprintf(stderr, "ERROR -- cannot make log tables for w=%d\n", w);
398   - exit(1);
399   - }
400   - }
401   - sum_j = galois_log_tables[w][a] - galois_log_tables[w][b];
402   - return galois_ilog_tables[w][sum_j];
403   - } else {
404   - if (b == 0) return -1;
405   - if (a == 0) return 0;
406   - sum_j = galois_inverse(b, w);
407   - return galois_single_multiply(a, sum_j, w);
  126 + if (gfp_array[w] == NULL) {
  127 + galois_init_field(w);
408 128 }
409   - fprintf(stderr, "Galois_single_divide - no implementation for w=%d\n", w);
410   - exit(1);
411   -}
412   -
413   -int galois_shift_divide(int a, int b, int w)
414   -{
415   - int inverse;
416 129  
417   - if (b == 0) return -1;
418   - if (a == 0) return 0;
419   - inverse = galois_shift_inverse(b, w);
420   - return galois_shift_multiply(a, inverse, w);
421   -}
422   -
423   -int galois_multtable_divide(int x, int y, int w)
424   -{
425   - return galois_div_tables[w][(x<<w)|y];
  130 + return gfp_array[w]->divide.w32(gfp_array[w], x, y);
426 131 }
427 132  
428 133 void galois_w08_region_multiply(char *region, /* Region to multiply */
... ... @@ -431,53 +136,10 @@ void galois_w08_region_multiply(char *region, /* Region to multiply */
431 136 char *r2, /* If r2 != NULL, products go here */
432 137 int add)
433 138 {
434   - unsigned char *ur1, *ur2, *cp;
435   - unsigned char prod;
436   - int i, srow, j;
437   - unsigned long l, *lp2;
438   - unsigned char *lp;
439   - int sol;
440   -
441   - ur1 = (unsigned char *) region;
442   - ur2 = (r2 == NULL) ? ur1 : (unsigned char *) r2;
443   -
444   -/* This is used to test its performance with respect to just calling galois_single_multiply
445   - if (r2 == NULL || !add) {
446   - for (i = 0; i < nbytes; i++) ur2[i] = galois_single_multiply(ur1[i], multby, 8);
447   - } else {
448   - for (i = 0; i < nbytes; i++) {
449   - ur2[i] = (ur2[i]^galois_single_multiply(ur1[i], multby, 8));
450   - }
  139 + if (gfp_array[8] == NULL) {
  140 + galois_init_field(8);
451 141 }
452   - */
453   -
454   - if (galois_mult_tables[8] == NULL) {
455   - if (galois_create_mult_tables(8) < 0) {
456   - fprintf(stderr, "galois_08_region_multiply -- couldn't make multiplication tables\n");
457   - exit(1);
458   - }
459   - }
460   - srow = multby * nw[8];
461   - if (r2 == NULL || !add) {
462   - for (i = 0; i < nbytes; i++) {
463   - prod = galois_mult_tables[8][srow+ur1[i]];
464   - ur2[i] = prod;
465   - }
466   - } else {
467   - sol = sizeof(long);
468   - lp2 = &l;
469   - lp = (unsigned char *) lp2;
470   - for (i = 0; i < nbytes; i += sol) {
471   - cp = ur2+i;
472   - lp2 = (unsigned long *) cp;
473   - for (j = 0; j < sol; j++) {
474   - prod = galois_mult_tables[8][srow+ur1[i+j]];
475   - lp[j] = prod;
476   - }
477   - *lp2 = (*lp2) ^ l;
478   - }
479   - }
480   - return;
  142 + gfp_array[8]->multiply_region.w32(gfp_array[8], region, r2, multby, nbytes, add);
481 143 }
482 144  
483 145 void galois_w16_region_multiply(char *region, /* Region to multiply */
... ... @@ -486,197 +148,12 @@ void galois_w16_region_multiply(char *region, /* Region to multiply */
486 148 char *r2, /* If r2 != NULL, products go here */
487 149 int add)
488 150 {
489   - unsigned short *ur1, *ur2, *cp;
490   - int prod;
491   - int i, log1, j, log2;
492   - unsigned long l, *lp2, *lptop;
493   - unsigned short *lp;
494   - int sol;
495   -
496   - ur1 = (unsigned short *) region;
497   - ur2 = (r2 == NULL) ? ur1 : (unsigned short *) r2;
498   - nbytes /= 2;
499   -
500   -
501   -/* This is used to test its performance with respect to just calling galois_single_multiply */
502   -/*
503   - if (r2 == NULL || !add) {
504   - for (i = 0; i < nbytes; i++) ur2[i] = galois_single_multiply(ur1[i], multby, 16);
505   - } else {
506   - for (i = 0; i < nbytes; i++) {
507   - ur2[i] = (ur2[i]^galois_single_multiply(ur1[i], multby, 16));
508   - }
509   - }
510   - return;
511   - */
512   -
513   - if (multby == 0) {
514   - if (!add) {
515   - lp2 = (unsigned long *) ur2;
516   - ur2 += nbytes;
517   - lptop = (unsigned long *) ur2;
518   - while (lp2 < lptop) { *lp2 = 0; lp2++; }
519   - }
520   - return;
  151 + if (gfp_array[16] == NULL) {
  152 + galois_init_field(16);
521 153 }
522   -
523   - if (galois_log_tables[16] == NULL) {
524   - if (galois_create_log_tables(16) < 0) {
525   - fprintf(stderr, "galois_16_region_multiply -- couldn't make log tables\n");
526   - exit(1);
527   - }
528   - }
529   - log1 = galois_log_tables[16][multby];
530   -
531   - if (r2 == NULL || !add) {
532   - for (i = 0; i < nbytes; i++) {
533   - if (ur1[i] == 0) {
534   - ur2[i] = 0;
535   - } else {
536   - prod = galois_log_tables[16][ur1[i]] + log1;
537   - ur2[i] = galois_ilog_tables[16][prod];
538   - }
539   - }
540   - } else {
541   - sol = sizeof(long)/2;
542   - lp2 = &l;
543   - lp = (unsigned short *) lp2;
544   - for (i = 0; i < nbytes; i += sol) {
545   - cp = ur2+i;
546   - lp2 = (unsigned long *) cp;
547   - for (j = 0; j < sol; j++) {
548   - if (ur1[i+j] == 0) {
549   - lp[j] = 0;
550   - } else {
551   - log2 = galois_log_tables[16][ur1[i+j]];
552   - prod = log2 + log1;
553   - lp[j] = galois_ilog_tables[16][prod];
554   - }
555   - }
556   - *lp2 = (*lp2) ^ l;
557   - }
558   - }
559   - return;
560   -}
561   -
562   -/* This will destroy mat, by the way */
563   -
564   -void galois_invert_binary_matrix(int *mat, int *inv, int rows)
565   -{
566   - int cols, i, j, k;
567   - int tmp;
568   -
569   - cols = rows;
570   -
571   - for (i = 0; i < rows; i++) inv[i] = (1 << i);
572   -
573   - /* First -- convert into upper triangular */
574   -
575   - for (i = 0; i < cols; i++) {
576   -
577   - /* Swap rows if we ave a zero i,i element. If we can't swap, then the
578   - matrix was not invertible */
579   -
580   - if ((mat[i] & (1 << i)) == 0) {
581   - for (j = i+1; j < rows && (mat[j] & (1 << i)) == 0; j++) ;
582   - if (j == rows) {
583   - fprintf(stderr, "galois_invert_matrix: Matrix not invertible!!\n");
584   - exit(1);
585   - }
586   - tmp = mat[i]; mat[i] = mat[j]; mat[j] = tmp;
587   - tmp = inv[i]; inv[i] = inv[j]; inv[j] = tmp;
588   - }
589   -
590   - /* Now for each j>i, add A_ji*Ai to Aj */
591   - for (j = i+1; j != rows; j++) {
592   - if ((mat[j] & (1 << i)) != 0) {
593   - mat[j] ^= mat[i];
594   - inv[j] ^= inv[i];
595   - }
596   - }
597   - }
598   -
599   - /* Now the matrix is upper triangular. Start at the top and multiply down */
600   -
601   - for (i = rows-1; i >= 0; i--) {
602   - for (j = 0; j < i; j++) {
603   - if (mat[j] & (1 << i)) {
604   -/* mat[j] ^= mat[i]; */
605   - inv[j] ^= inv[i];
606   - }
607   - }
608   - }
609   -}
610   -
611   -int galois_inverse(int y, int w)
612   -{
613   -
614   - if (y == 0) return -1;
615   - if (mult_type[w] == SHIFT || mult_type[w] == SPLITW8) return galois_shift_inverse(y, w);
616   - return galois_single_divide(1, y, w);
617   -}
618   -
619   -int galois_shift_inverse(int y, int w)
620   -{
621   - int mat[1024], mat2[32];
622   - int inv[1024], inv2[32];
623   - int ind, i, j, k, prod;
624   -
625   - for (i = 0; i < w; i++) {
626   - mat2[i] = y;
627   -
628   - if (y & nw[w-1]) {
629   - y = y << 1;
630   - y = (y ^ prim_poly[w]) & nwm1[w];
631   - } else {
632   - y = y << 1;
633   - }
634   - }
635   -
636   - galois_invert_binary_matrix(mat2, inv2, w);
637   -
638   - return inv2[0];
639   -}
640   -
641   -int *galois_get_mult_table(int w)
642   -{
643   - if (galois_mult_tables[w] == NULL) {
644   - if (galois_create_mult_tables(w)) {
645   - return NULL;
646   - }
647   - }
648   - return galois_mult_tables[w];
649   -}
650   -
651   -int *galois_get_div_table(int w)
652   -{
653   - if (galois_mult_tables[w] == NULL) {
654   - if (galois_create_mult_tables(w)) {
655   - return NULL;
656   - }
657   - }
658   - return galois_div_tables[w];
  154 + gfp_array[16]->multiply_region.w32(gfp_array[16], region, r2, multby, nbytes, add);
659 155 }
660 156  
661   -int *galois_get_log_table(int w)
662   -{
663   - if (galois_log_tables[w] == NULL) {
664   - if (galois_create_log_tables(w)) {
665   - return NULL;
666   - }
667   - }
668   - return galois_log_tables[w];
669   -}
670   -
671   -int *galois_get_ilog_table(int w)
672   -{
673   - if (galois_ilog_tables[w] == NULL) {
674   - if (galois_create_log_tables(w)) {
675   - return NULL;
676   - }
677   - }
678   - return galois_ilog_tables[w];
679   -}
680 157  
681 158 void galois_w32_region_multiply(char *region, /* Region to multiply */
682 159 int multby, /* Number to multiply by */
... ... @@ -684,62 +161,10 @@ void galois_w32_region_multiply(char *region, /* Region to multiply */
684 161 char *r2, /* If r2 != NULL, products go here */
685 162 int add)
686 163 {
687   - unsigned int *ur1, *ur2, *cp, *ur2top;
688   - unsigned long *lp2, *lptop;
689   - int i, j, a, b, accumulator, i8, j8, k;
690   - int acache[4];
691   -
692   - ur1 = (unsigned int *) region;
693   - ur2 = (r2 == NULL) ? ur1 : (unsigned int *) r2;
694   - nbytes /= sizeof(int);
695   - ur2top = ur2 + nbytes;
696   -
697   - if (galois_split_w8[0]== NULL) {
698   - if (galois_create_split_w8_tables(8) < 0) {
699   - fprintf(stderr, "galois_32_region_multiply -- couldn't make split multiplication tables\n");
700   - exit(1);
701   - }
702   - }
703   -
704   - /* If we're overwriting r2, then we can't do better than just calling split_multiply.
705   - We'll inline it here to save on the procedure call overhead */
706   -
707   - i8 = 0;
708   - for (i = 0; i < 4; i++) {
709   - acache[i] = (((multby >> i8) & 255) << 8);
710   - i8 += 8;
711   - }
712   - if (!add) {
713   - for (k = 0; k < nbytes; k++) {
714   - accumulator = 0;
715   - for (i = 0; i < 4; i++) {
716   - a = acache[i];
717   - j8 = 0;
718   - for (j = 0; j < 4; j++) {
719   - b = ((ur1[k] >> j8) & 255);
720   - accumulator ^= galois_split_w8[i+j][a|b];
721   - j8 += 8;
722   - }
723   - }
724   - ur2[k] = accumulator;
725   - }
726   - } else {
727   - for (k = 0; k < nbytes; k++) {
728   - accumulator = 0;
729   - for (i = 0; i < 4; i++) {
730   - a = acache[i];
731   - j8 = 0;
732   - for (j = 0; j < 4; j++) {
733   - b = ((ur1[k] >> j8) & 255);
734   - accumulator ^= galois_split_w8[i+j][a|b];
735   - j8 += 8;
736   - }
737   - }
738   - ur2[k] = (ur2[k] ^ accumulator);
739   - }
  164 + if (gfp_array[32] == NULL) {
  165 + galois_init_field(32);
740 166 }
741   - return;
742   -
  167 + gfp_array[32]->multiply_region.w32(gfp_array[32], region, r2, multby, nbytes, add);
743 168 }
744 169  
745 170 void galois_region_xor( char *r1, /* Region 1 */
... ... @@ -767,57 +192,8 @@ void galois_region_xor( char *r1, /* Region 1 */
767 192 }
768 193 }
769 194  
770   -int galois_create_split_w8_tables()
771   -{
772   - int p1, p2, i, j, p1elt, p2elt, index, ishift, jshift, *table;
773   -
774   - if (galois_split_w8[0] != NULL) return 0;
775   -
776   - if (galois_create_mult_tables(8) < 0) return -1;
777   -
778   - for (i = 0; i < 7; i++) {
779   - galois_split_w8[i] = (int *) malloc(sizeof(int) * (1 << 16));
780   - if (galois_split_w8[i] == NULL) {
781   - for (i--; i >= 0; i--) free(galois_split_w8[i]);
782   - return -1;
783   - }
784   - }
785   -
786   - for (i = 0; i < 4; i += 3) {
787   - ishift = i * 8;
788   - for (j = ((i == 0) ? 0 : 1) ; j < 4; j++) {
789   - jshift = j * 8;
790   - table = galois_split_w8[i+j];
791   - index = 0;
792   - for (p1 = 0; p1 < 256; p1++) {
793   - p1elt = (p1 << ishift);
794   - for (p2 = 0; p2 < 256; p2++) {
795   - p2elt = (p2 << jshift);
796   - table[index] = galois_shift_multiply(p1elt, p2elt, 32);
797   - index++;
798   - }
799   - }
800   - }
801   - }
802   - return 0;
803   -}
804   -
805   -int galois_split_w8_multiply(int x, int y)
  195 +int galois_inverse(int y, int w)
806 196 {
807   - int i, j, a, b, accumulator, i8, j8;
808   -
809   - accumulator = 0;
810   -
811   - i8 = 0;
812   - for (i = 0; i < 4; i++) {
813   - a = (((x >> i8) & 255) << 8);
814   - j8 = 0;
815   - for (j = 0; j < 4; j++) {
816   - b = ((y >> j8) & 255);
817   - accumulator ^= galois_split_w8[i+j][a|b];
818   - j8 += 8;
819   - }
820   - i8 += 8;
821   - }
822   - return accumulator;
  197 + if (y == 0) return -1;
  198 + return galois_single_divide(1, y, w);
823 199 }
... ...
galois.h
... ... @@ -51,33 +51,13 @@ POSSIBILITY OF SUCH DAMAGE.
51 51  
52 52 #include <stdio.h>
53 53 #include <stdlib.h>
  54 +#include <gf_complete.h>
  55 +
  56 +extern void galois_change_technique(gf_t *gf, int w);
54 57  
55 58 extern int galois_single_multiply(int a, int b, int w);
56 59 extern int galois_single_divide(int a, int b, int w);
57   -extern int galois_log(int value, int w);
58   -extern int galois_ilog(int value, int w);
59   -
60   -extern int galois_create_log_tables(int w); /* Returns 0 on success, -1 on failure */
61   -extern int galois_logtable_multiply(int x, int y, int w);
62   -extern int galois_logtable_divide(int x, int y, int w);
63   -
64   -extern int galois_create_mult_tables(int w); /* Returns 0 on success, -1 on failure */
65   -extern int galois_multtable_multiply(int x, int y, int w);
66   -extern int galois_multtable_divide(int x, int y, int w);
67   -
68   -extern int galois_shift_multiply(int x, int y, int w);
69   -extern int galois_shift_divide(int x, int y, int w);
70   -
71   -extern int galois_create_split_w8_tables();
72   -extern int galois_split_w8_multiply(int x, int y);
73   -
74 60 extern int galois_inverse(int x, int w);
75   -extern int galois_shift_inverse(int y, int w);
76   -
77   -extern int *galois_get_mult_table(int w);
78   -extern int *galois_get_div_table(int w);
79   -extern int *galois_get_log_table(int w);
80   -extern int *galois_get_ilog_table(int w);
81 61  
82 62 void galois_region_xor( char *r1, /* Region 1 */
83 63 char *r2, /* Region 2 */
... ...
makefile
... ... @@ -27,16 +27,25 @@
27 27 # $Revision: 1.0 $
28 28 # $Date: 2007/09/25 15:12:20 $
29 29  
  30 +PREFIX=/usr/local
  31 +BINDIR=${PREFIX}/bin
  32 +LIBDIR=${PREFIX}/lib
  33 +INCDIR=${PREFIX}/include
  34 +
30 35 CC = gcc
31   -CFLAGS = -O3 -I$(HOME)/include
  36 +CFLAGS = -O3 -I${INCDIR} -L${LIBDIR}
32 37  
33   -ALL = galois.o jerasure.o reed_sol.o cauchy.o liberation.o
  38 +ALL = galois.o jerasure.o reed_sol.o cauchy.o liberation.o lib/libJerasure.so
  39 +OBJS = galois.o jerasure.o reed_sol.o cauchy.o liberation.o
34 40  
35 41 all: $(ALL)
36 42  
37 43 clean:
38 44 rm -f core *.o $(ALL) a.out
39 45  
  46 +lib:
  47 + mkdir -p lib
  48 +
40 49 .SUFFIXES: .c .o
41 50 .c.o:
42 51 $(CC) $(CFLAGS) -c $*.c
... ... @@ -47,3 +56,15 @@ reed_sol.o: jerasure.h galois.h reed_sol.h
47 56 cauchy.o: jerasure.h galois.h cauchy.h
48 57 liberation.o: jerasure.h galois.h liberation.h
49 58  
  59 +lib/libJerasure.so: lib/libJerasure.so.0
  60 + ln -sf libJerasure.so.0 lib/libJerasure.so
  61 +
  62 +lib/libJerasure.so.0: lib $(OBJS)
  63 + $(CC) -shared -Wl,-soname,libJerasure.so.0 \
  64 + -o lib/libJerasure.so.0 $(OBJS) ${LIBDIR}/gf_complete.a
  65 +
  66 +install: lib/libJerasure.so
  67 + cp -P lib/libJerasure.so* ${LIBDIR}
  68 + mkdir -p ${INCDIR}/jerasure
  69 + cp *.h ${INCDIR}/jerasure
  70 +
... ...