Blame view

src/gf_w4.c 50.9 KB
70b6d55a   plank   Big checkin after...
1
/*
b0e2ae07   Jim Plank   Put headers on th...
2
3
4
5
 * GF-Complete: A Comprehensive Open Source Library for Galois Field Arithmetic
 * James S. Plank, Ethan L. Miller, Kevin M. Greenan,
 * Benjamin A. Arnold, John A. Burnum, Adam W. Disney, Allen C. McBride.
 *
70b6d55a   plank   Big checkin after...
6
7
8
9
10
11
12
13
 * gf_w4.c
 *
 * Routines for 4-bit Galois fields
 */

#include "gf_int.h"
#include <stdio.h>
#include <stdlib.h>
1311a44f   Janne Grunau   arm: NEON optimis...
14
#include "gf_w4.h"
4339569f   Bassam Tabbara   Support for runti...
15

70b6d55a   plank   Big checkin after...
16
17
18
19
20
21
22
#define AB2(ip, am1 ,am2, b, t1, t2) {\
  t1 = (b << 1) & am1;\
  t2 = b & am2; \
  t2 = ((t2 << 1) - (t2 >> (GF_FIELD_WIDTH-1))); \
  b = (t1 ^ (t2 & ip));}

// ToDo(KMG/JSP): Why is 0x88 hard-coded?
b5ac2580   Loic Dachary   TODO reminder for...
23
#define SSE_AB2(pp, m1, va, t1, t2) {\
cfcc1881   Loic Dachary   remove unused arg...
24
          t1 = _mm_and_si128(_mm_slli_epi64(va, 1), m1); \
70b6d55a   plank   Big checkin after...
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
          t2 = _mm_and_si128(va, _mm_set1_epi8(0x88)); \
          t2 = _mm_sub_epi64 (_mm_slli_epi64(t2, 1), _mm_srli_epi64(t2, (GF_FIELD_WIDTH-1))); \
          va = _mm_xor_si128(t1, _mm_and_si128(t2, pp)); }

/* ------------------------------------------------------------
   JSP: These are basic and work from multiple implementations.
 */

static
inline
gf_val_32_t gf_w4_inverse_from_divide (gf_t *gf, gf_val_32_t a)
{
  return gf->divide.w32(gf, 1, a);
}

static
inline
gf_val_32_t gf_w4_divide_from_inverse (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  b = gf->inverse.w32(gf, b);
  return gf->multiply.w32(gf, a, b);
}

static
inline
gf_val_32_t gf_w4_euclid (gf_t *gf, gf_val_32_t b)
{
  gf_val_32_t e_i, e_im1, e_ip1;
  gf_val_32_t d_i, d_im1, d_ip1;
  gf_val_32_t y_i, y_im1, y_ip1;
  gf_val_32_t c_i;

  if (b == 0) return -1;
  e_im1 = ((gf_internal_t *) (gf->scratch))->prim_poly;
  e_i = b;
  d_im1 = 4;
  for (d_i = d_im1; ((1 << d_i) & e_i) == 0; d_i--) ;
  y_i = 1;
  y_im1 = 0;

  while (e_i != 1) {
    e_ip1 = e_im1;
70b6d55a   plank   Big checkin after...
67
68
69
70
71
72
73
    d_ip1 = d_im1;
    c_i = 0;

    while (d_ip1 >= d_i) {
      c_i ^= (1 << (d_ip1 - d_i));
      e_ip1 ^= (e_i << (d_ip1 - d_i));
      if (e_ip1 == 0) return 0;
110523d6   Jim Plank   GF-Complete Relea...
74
      while ((e_ip1 & (1 << d_ip1)) == 0) d_ip1--;
70b6d55a   plank   Big checkin after...
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
    }

    y_ip1 = y_im1 ^ gf->multiply.w32(gf, c_i, y_i);
    y_im1 = y_i;
    y_i = y_ip1;

    e_im1 = e_i;
    d_im1 = d_i;
    e_i = e_ip1;
    d_i = d_ip1;
  }

  return y_i;
}

static 
gf_val_32_t gf_w4_extract_word(gf_t *gf, void *start, int bytes, int index)
{
  uint8_t *r8, v;

  r8 = (uint8_t *) start;
  v = r8[index/2];
  if (index%2) {
    return v >> 4;
  } else {
    return v&0xf;
  }
}


static
inline
gf_val_32_t gf_w4_matrix (gf_t *gf, gf_val_32_t b)
{
  return gf_bitmatrix_inverse(b, 4, ((gf_internal_t *) (gf->scratch))->prim_poly);
}


110523d6   Jim Plank   GF-Complete Relea...
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
static
inline
gf_val_32_t
gf_w4_shift_multiply (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  uint8_t product, i, pp;
  gf_internal_t *h;
  
  h = (gf_internal_t *) gf->scratch;
  pp = h->prim_poly;

  product = 0;

  for (i = 0; i < GF_FIELD_WIDTH; i++) { 
    if (a & (1 << i)) product ^= (b << i);
  }
  for (i = (GF_FIELD_WIDTH*2-2); i >= GF_FIELD_WIDTH; i--) {
    if (product & (1 << i)) product ^= (pp << (i-GF_FIELD_WIDTH)); 
  }
  return product;
}

/* Ben: This function works, but it is 33% slower than the normal shift mult */

static
4339569f   Bassam Tabbara   Support for runti...
138
inline
110523d6   Jim Plank   GF-Complete Relea...
139
140
141
142
143
144
145
gf_val_32_t
gf_w4_clm_multiply (gf_t *gf, gf_val_32_t a4, gf_val_32_t b4)
{
  gf_val_32_t rv = 0;

#if defined(INTEL_SSE4_PCLMUL)

110523d6   Jim Plank   GF-Complete Relea...
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  __m128i         a, b;
  __m128i         result;
  __m128i         prim_poly;
  __m128i         w;
  gf_internal_t * h = gf->scratch;

  a = _mm_insert_epi32 (_mm_setzero_si128(), a4, 0);
  b = _mm_insert_epi32 (a, b4, 0);

  prim_poly = _mm_set_epi32(0, 0, 0, (uint32_t)(h->prim_poly & 0x1fULL));

  /* Do the initial multiply */

  result = _mm_clmulepi64_si128 (a, b, 0);

  /* Ben/JSP: Do prim_poly reduction once. We are guaranteed that we will only
     have to do the reduction only once, because (w-2)/z == 1. Where
     z is equal to the number of zeros after the leading 1.

     _mm_clmulepi64_si128 is the carryless multiply operation. Here
     _mm_srli_epi64 shifts the result to the right by 4 bits. This allows
     us to multiply the prim_poly by the leading bits of the result. We
     then xor the result of that operation back with the result. */

  w = _mm_clmulepi64_si128 (prim_poly, _mm_srli_epi64 (result, 4), 0);
  result = _mm_xor_si128 (result, w);

  /* Extracts 32 bit value from result. */

  rv = ((gf_val_32_t)_mm_extract_epi32(result, 0));
110523d6   Jim Plank   GF-Complete Relea...
176
177
#endif
  return rv;
4339569f   Bassam Tabbara   Support for runti...
178
}
110523d6   Jim Plank   GF-Complete Relea...
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

static
void
gf_w4_multiply_region_from_single(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int 
    xor)
{
  gf_region_data rd;
  uint8_t *s8;
  uint8_t *d8;

  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }

  gf_set_region_data(&rd, gf, src, dest, bytes, val, xor, 1);
  gf_do_initial_region_alignment(&rd);

  s8 = (uint8_t *) rd.s_start;
  d8 = (uint8_t *) rd.d_start;

  if (xor) {
    while (d8 < ((uint8_t *) rd.d_top)) {
      *d8 ^= (gf->multiply.w32(gf, val, (*s8 & 0xf)) | 
             ((gf->multiply.w32(gf, val, (*s8 >> 4))) << 4));
      d8++;
      s8++;
    }
  } else {
    while (d8 < ((uint8_t *) rd.d_top)) {
      *d8 = (gf->multiply.w32(gf, val, (*s8 & 0xf)) | 
             ((gf->multiply.w32(gf, val, (*s8 >> 4))) << 4));
      d8++;
      s8++;
    }
  }
  gf_do_final_region_alignment(&rd);
}

70b6d55a   plank   Big checkin after...
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/* ------------------------------------------------------------
  IMPLEMENTATION: LOG_TABLE: 

  JSP: This is a basic log-antilog implementation.  
       I'm not going to spend any time optimizing it because the
       other techniques are faster for both single and region
       operations. 
 */

static
inline
gf_val_32_t
gf_w4_log_multiply (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  struct gf_logtable_data *ltd;
    
  ltd = (struct gf_logtable_data *) ((gf_internal_t *) (gf->scratch))->private;
  return (a == 0 || b == 0) ? 0 : ltd->antilog_tbl[(unsigned)(ltd->log_tbl[a] + ltd->log_tbl[b])];
}

static
inline
gf_val_32_t
gf_w4_log_divide (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  int log_sum = 0;
  struct gf_logtable_data *ltd;
    
  if (a == 0 || b == 0) return 0;
  ltd = (struct gf_logtable_data *) ((gf_internal_t *) (gf->scratch))->private;

  log_sum = ltd->log_tbl[a] - ltd->log_tbl[b];
  return (ltd->antilog_tbl_div[log_sum]);
}

static
void 
gf_w4_log_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
{
  int i;
  uint8_t lv, b, c;
  uint8_t *s8, *d8;
  
  struct gf_logtable_data *ltd;

  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
e96c0e28   plank   Renaming gf.h to ...
262
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }
70b6d55a   plank   Big checkin after...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289

  ltd = (struct gf_logtable_data *) ((gf_internal_t *) (gf->scratch))->private;
  s8 = (uint8_t *) src;
  d8 = (uint8_t *) dest;

  lv = ltd->log_tbl[val];

  for (i = 0; i < bytes; i++) {
    c = (xor) ? d8[i] : 0;
    b = (s8[i] >> GF_FIELD_WIDTH);
    c ^= (b == 0) ? 0 : (ltd->antilog_tbl[lv + ltd->log_tbl[b]] << GF_FIELD_WIDTH);
    b = (s8[i] & 0xf);
    c ^= (b == 0) ? 0 : ltd->antilog_tbl[lv + ltd->log_tbl[b]];
    d8[i] = c;
  }
}

static 
int gf_w4_log_init(gf_t *gf)
{
  gf_internal_t *h;
  struct gf_logtable_data *ltd;
  int i, b;

  h = (gf_internal_t *) gf->scratch;
  ltd = h->private;

110523d6   Jim Plank   GF-Complete Relea...
290
291
  for (i = 0; i < GF_FIELD_SIZE; i++)
    ltd->log_tbl[i]=0;
70b6d55a   plank   Big checkin after...
292
293
294

  ltd->antilog_tbl_div = ltd->antilog_tbl + (GF_FIELD_SIZE-1);
  b = 1;
110523d6   Jim Plank   GF-Complete Relea...
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
  i = 0;
  do {
    if (ltd->log_tbl[b] != 0 && i != 0) {
      fprintf(stderr, "Cannot construct log table: Polynomial is not primitive.\n\n");
      return 0;
    }
    ltd->log_tbl[b] = i;
    ltd->antilog_tbl[i] = b;
    ltd->antilog_tbl[i+GF_FIELD_SIZE-1] = b;
    b <<= 1;
    i++;
    if (b & GF_FIELD_SIZE) b = b ^ h->prim_poly;
  } while (b != 1);

  if (i != GF_FIELD_SIZE - 1) {
    _gf_errno = GF_E_LOGPOLY;
    return 0;
70b6d55a   plank   Big checkin after...
312
313
  }
    
87f0d439   Bassam Tabbara   Add support for p...
314
315
316
317
  gf->inverse.w32 = gf_w4_inverse_from_divide;
  gf->divide.w32 = gf_w4_log_divide;
  gf->multiply.w32 = gf_w4_log_multiply;
  gf->multiply_region.w32 = gf_w4_log_multiply_region;
70b6d55a   plank   Big checkin after...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  return 1;
}

/* ------------------------------------------------------------
  IMPLEMENTATION: SINGLE TABLE: JSP. 
 */

static
inline
gf_val_32_t
gf_w4_single_table_multiply (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  struct gf_single_table_data *std;
    
  std = (struct gf_single_table_data *) ((gf_internal_t *) (gf->scratch))->private;
  return std->mult[a][b];
}

static
inline
gf_val_32_t
gf_w4_single_table_divide (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  struct gf_single_table_data *std;
    
  std = (struct gf_single_table_data *) ((gf_internal_t *) (gf->scratch))->private;
  return std->div[a][b];
}

static
void 
gf_w4_single_table_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
{
  int i;
  uint8_t b, c;
  uint8_t *s8, *d8;
  
  struct gf_single_table_data *std;

  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
e96c0e28   plank   Renaming gf.h to ...
358
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }
70b6d55a   plank   Big checkin after...
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375

  std = (struct gf_single_table_data *) ((gf_internal_t *) (gf->scratch))->private;
  s8 = (uint8_t *) src;
  d8 = (uint8_t *) dest;

  for (i = 0; i < bytes; i++) {
    c = (xor) ? d8[i] : 0;
    b = (s8[i] >> GF_FIELD_WIDTH);
    c ^= (std->mult[val][b] << GF_FIELD_WIDTH);
    b = (s8[i] & 0xf);
    c ^= (std->mult[val][b]);
    d8[i] = c;
  }
}

#define MM_PRINT(s, r) { uint8_t blah[16]; printf("%-12s", s); _mm_storeu_si128((__m128i *)blah, r); for (i = 0; i < 16; i++) printf(" %02x", blah[i]); printf("\n"); }

29899ad4   Loic Dachary   move #if to avoid...
376
#ifdef INTEL_SSSE3
70b6d55a   plank   Big checkin after...
377
378
379
380
static
void 
gf_w4_single_table_sse_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
{
70b6d55a   plank   Big checkin after...
381
382
  gf_region_data rd;
  uint8_t *base, *sptr, *dptr, *top;
191b86b5   Loic Dachary   remove unused var...
383
  __m128i  tl, loset, r, va, th;
70b6d55a   plank   Big checkin after...
384
385
386
387
  
  struct gf_single_table_data *std;
    
  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
e96c0e28   plank   Renaming gf.h to ...
388
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }
70b6d55a   plank   Big checkin after...
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421

  gf_set_region_data(&rd, gf, src, dest, bytes, val, xor, 16);

  std = (struct gf_single_table_data *) ((gf_internal_t *) (gf->scratch))->private;
  base = (uint8_t *) std->mult;
  base += (val << GF_FIELD_WIDTH);

  gf_do_initial_region_alignment(&rd);

  tl = _mm_loadu_si128((__m128i *)base);
  th = _mm_slli_epi64(tl, 4);
  loset = _mm_set1_epi8 (0x0f);

  sptr = rd.s_start;
  dptr = rd.d_start;
  top = rd.s_top;

  while (sptr < (uint8_t *) top) {
    va = _mm_load_si128 ((__m128i *)(sptr));
    r = _mm_and_si128 (loset, va);
    r = _mm_shuffle_epi8 (tl, r);
    va = _mm_srli_epi64 (va, 4);
    va = _mm_and_si128 (loset, va);
    va = _mm_shuffle_epi8 (th, va);
    r = _mm_xor_si128 (r, va);
    va = (xor) ? _mm_load_si128 ((__m128i *)(dptr)) : _mm_setzero_si128(); 
    r = _mm_xor_si128 (r, va);
    _mm_store_si128 ((__m128i *)(dptr), r);
    dptr += 16;
    sptr += 16;
  }
  gf_do_final_region_alignment(&rd);

70b6d55a   plank   Big checkin after...
422
}
29899ad4   Loic Dachary   move #if to avoid...
423
#endif
70b6d55a   plank   Big checkin after...
424
425
426
427
428
429

static 
int gf_w4_single_table_init(gf_t *gf)
{
  gf_internal_t *h;
  struct gf_single_table_data *std;
f043479e   Loic Dachary   remove unused var...
430
  int a, b, prod;
61d75c53   plank   Bugs with SSE4. ...
431

70b6d55a   plank   Big checkin after...
432
433
434
435

  h = (gf_internal_t *) gf->scratch;
  std = (struct gf_single_table_data *)h->private;

70b6d55a   plank   Big checkin after...
436
437
438
439
  bzero(std->mult, sizeof(uint8_t) * GF_FIELD_SIZE * GF_FIELD_SIZE);
  bzero(std->div, sizeof(uint8_t) * GF_FIELD_SIZE * GF_FIELD_SIZE);

  for (a = 1; a < GF_FIELD_SIZE; a++) {
70b6d55a   plank   Big checkin after...
440
    for (b = 1; b < GF_FIELD_SIZE; b++) {
110523d6   Jim Plank   GF-Complete Relea...
441
      prod = gf_w4_shift_multiply(gf, a, b);
70b6d55a   plank   Big checkin after...
442
443
444
445
446
      std->mult[a][b] = prod;
      std->div[prod][b] = a;
    }
  }

87f0d439   Bassam Tabbara   Add support for p...
447
448
449
  gf->inverse.w32 = NULL;
  gf->divide.w32 = gf_w4_single_table_divide;
  gf->multiply.w32 = gf_w4_single_table_multiply;
4339569f   Bassam Tabbara   Support for runti...
450
451
  #if defined(INTEL_SSSE3) || defined(ARM_NEON)
    if(h->region_type & (GF_REGION_NOSIMD | GF_REGION_CAUCHY))
87f0d439   Bassam Tabbara   Add support for p...
452
      gf->multiply_region.w32 = gf_w4_single_table_multiply_region;
4339569f   Bassam Tabbara   Support for runti...
453
454
455
    else
    #if defined(INTEL_SSSE3)
      gf->multiply_region.w32 = gf_w4_single_table_sse_multiply_region;
1311a44f   Janne Grunau   arm: NEON optimis...
456
    #elif defined(ARM_NEON)
4339569f   Bassam Tabbara   Support for runti...
457
458
459
460
461
462
      gf_w4_neon_single_table_init(gf);
    #endif
  #else
    gf->multiply_region.w32 = gf_w4_single_table_multiply_region;
    if (h->region_type & GF_REGION_SIMD) return 0;
  #endif
110523d6   Jim Plank   GF-Complete Relea...
463
464

  return 1;
70b6d55a   plank   Big checkin after...
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
}

/* ------------------------------------------------------------
  IMPLEMENTATION: DOUBLE TABLE: JSP. 
 */

static
inline
gf_val_32_t
gf_w4_double_table_multiply (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  struct gf_double_table_data *std;
    
  std = (struct gf_double_table_data *) ((gf_internal_t *) (gf->scratch))->private;
  return std->mult[a][b];
}

static
inline
gf_val_32_t
gf_w4_double_table_divide (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  struct gf_double_table_data *std;
    
  std = (struct gf_double_table_data *) ((gf_internal_t *) (gf->scratch))->private;
  return std->div[a][b];
}

static
void 
gf_w4_double_table_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
{
  int i;
70b6d55a   plank   Big checkin after...
498
499
500
501
502
503
  uint8_t *s8, *d8, *base;
  gf_region_data rd;
  struct gf_double_table_data *std;
    
  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }
e96c0e28   plank   Renaming gf.h to ...
504

70b6d55a   plank   Big checkin after...
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
  gf_set_region_data(&rd, gf, src, dest, bytes, val, xor, 8);

  std = (struct gf_double_table_data *) ((gf_internal_t *) (gf->scratch))->private;
  s8 = (uint8_t *) src;
  d8 = (uint8_t *) dest;
  base = (uint8_t *) std->mult;
  base += (val << GF_DOUBLE_WIDTH);

  if (xor) {
    for (i = 0; i < bytes; i++) d8[i] ^= base[s8[i]];
  } else {
    for (i = 0; i < bytes; i++) d8[i] = base[s8[i]];
  }
}

static 
int gf_w4_double_table_init(gf_t *gf)
{
  gf_internal_t *h;
  struct gf_double_table_data *std;
  int a, b, c, prod, ab;
f043479e   Loic Dachary   remove unused var...
526
  uint8_t mult[GF_FIELD_SIZE][GF_FIELD_SIZE];
70b6d55a   plank   Big checkin after...
527
528
529
530
531

  h = (gf_internal_t *) gf->scratch;
  std = (struct gf_double_table_data *)h->private;

  bzero(mult, sizeof(uint8_t) * GF_FIELD_SIZE * GF_FIELD_SIZE);
70b6d55a   plank   Big checkin after...
532
533
534
535
  bzero(std->div, sizeof(uint8_t) * GF_FIELD_SIZE * GF_FIELD_SIZE);

  for (a = 1; a < GF_FIELD_SIZE; a++) {
    for (b = 1; b < GF_FIELD_SIZE; b++) {
70b6d55a   plank   Big checkin after...
536
      prod = gf_w4_shift_multiply(gf, a, b);
110523d6   Jim Plank   GF-Complete Relea...
537
      mult[a][b] = prod;
70b6d55a   plank   Big checkin after...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
      std->div[prod][b] = a;
    }
  }
  bzero(std->mult, sizeof(uint8_t) * GF_FIELD_SIZE * GF_FIELD_SIZE * GF_FIELD_SIZE);
  for (a = 0; a < GF_FIELD_SIZE; a++) {
    for (b = 0; b < GF_FIELD_SIZE; b++) {
      ab = mult[a][b];
      for (c = 0; c < GF_FIELD_SIZE; c++) {
        std->mult[a][(b << 4) | c] = ((ab << 4) | mult[a][c]);
      }
    }
  }

  gf->inverse.w32 = NULL;
87f0d439   Bassam Tabbara   Add support for p...
552
553
554
555
  gf->divide.w32 = gf_w4_double_table_divide;
  gf->multiply.w32 = gf_w4_double_table_multiply;
  gf->multiply_region.w32 = gf_w4_double_table_multiply_region;
  return 1;
70b6d55a   plank   Big checkin after...
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
}


static
inline
gf_val_32_t
gf_w4_quad_table_lazy_divide (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  struct gf_quad_table_lazy_data *std;
    
  std = (struct gf_quad_table_lazy_data *) ((gf_internal_t *) (gf->scratch))->private;
  return std->div[a][b];
}

static
inline
gf_val_32_t
gf_w4_quad_table_lazy_multiply (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  struct gf_quad_table_lazy_data *std;
    
  std = (struct gf_quad_table_lazy_data *) ((gf_internal_t *) (gf->scratch))->private;
  return std->smult[a][b];
}

static
inline
gf_val_32_t
gf_w4_quad_table_divide (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  struct gf_quad_table_data *std;
    
  std = (struct gf_quad_table_data *) ((gf_internal_t *) (gf->scratch))->private;
  return std->div[a][b];
}

static
inline
gf_val_32_t
gf_w4_quad_table_multiply (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  struct gf_quad_table_data *std;
  uint16_t v;
    
  std = (struct gf_quad_table_data *) ((gf_internal_t *) (gf->scratch))->private;
  v = std->mult[a][b];
  return v;
}

static
void 
gf_w4_quad_table_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
{
  uint16_t *base;
  gf_region_data rd;
  struct gf_quad_table_data *std;
  struct gf_quad_table_lazy_data *ltd;
  gf_internal_t *h;
  int a, b, c, d, va, vb, vc, vd;
    
  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }
e96c0e28   plank   Renaming gf.h to ...
618

70b6d55a   plank   Big checkin after...
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
  h = (gf_internal_t *) (gf->scratch);
  if (h->region_type & GF_REGION_LAZY) {
    ltd = (struct gf_quad_table_lazy_data *) ((gf_internal_t *) (gf->scratch))->private;
    base = ltd->mult;
    for (a = 0; a < 16; a++) {
      va = (ltd->smult[val][a] << 12);
      for (b = 0; b < 16; b++) {
        vb = (ltd->smult[val][b] << 8);
        for (c = 0; c < 16; c++) {
          vc = (ltd->smult[val][c] << 4);
          for (d = 0; d < 16; d++) {
            vd = ltd->smult[val][d];
            base[(a << 12) | (b << 8) | (c << 4) | d ] = (va | vb | vc | vd);
          }
        }
      }
    }
  } else {
    std = (struct gf_quad_table_data *) ((gf_internal_t *) (gf->scratch))->private;
    base = &(std->mult[val][0]);
  }

  gf_set_region_data(&rd, gf, src, dest, bytes, val, xor, 8);
  gf_do_initial_region_alignment(&rd);
  gf_two_byte_region_table_multiply(&rd, base);
  gf_do_final_region_alignment(&rd);
}

static 
int gf_w4_quad_table_init(gf_t *gf)
{
  gf_internal_t *h;
  struct gf_quad_table_data *std;
  int prod, val, a, b, c, d, va, vb, vc, vd;
f043479e   Loic Dachary   remove unused var...
653
  uint8_t mult[GF_FIELD_SIZE][GF_FIELD_SIZE];
70b6d55a   plank   Big checkin after...
654
655
656
657
658

  h = (gf_internal_t *) gf->scratch;
  std = (struct gf_quad_table_data *)h->private;

  bzero(mult, sizeof(uint8_t) * GF_FIELD_SIZE * GF_FIELD_SIZE);
70b6d55a   plank   Big checkin after...
659
660
661
662
  bzero(std->div, sizeof(uint8_t) * GF_FIELD_SIZE * GF_FIELD_SIZE);

  for (a = 1; a < GF_FIELD_SIZE; a++) {
    for (b = 1; b < GF_FIELD_SIZE; b++) {
70b6d55a   plank   Big checkin after...
663
      prod = gf_w4_shift_multiply(gf, a, b);
110523d6   Jim Plank   GF-Complete Relea...
664
      mult[a][b] = prod;
70b6d55a   plank   Big checkin after...
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
      std->div[prod][b] = a;
    }
  }

  for (val = 0; val < 16; val++) {
    for (a = 0; a < 16; a++) {
      va = (mult[val][a] << 12);
      for (b = 0; b < 16; b++) {
        vb = (mult[val][b] << 8);
        for (c = 0; c < 16; c++) {
          vc = (mult[val][c] << 4);
          for (d = 0; d < 16; d++) {
            vd = mult[val][d];
            std->mult[val][(a << 12) | (b << 8) | (c << 4) | d ] = (va | vb | vc | vd);
          }
        }
      }
    }
  }

  gf->inverse.w32 = NULL;
87f0d439   Bassam Tabbara   Add support for p...
686
687
688
689
  gf->divide.w32 = gf_w4_quad_table_divide;
  gf->multiply.w32 = gf_w4_quad_table_multiply;
  gf->multiply_region.w32 = gf_w4_quad_table_multiply_region;
  return 1;
70b6d55a   plank   Big checkin after...
690
691
692
693
694
695
696
}
static 
int gf_w4_quad_table_lazy_init(gf_t *gf)
{
  gf_internal_t *h;
  struct gf_quad_table_lazy_data *std;
  int a, b, prod, loga, logb;
f043479e   Loic Dachary   remove unused var...
697
  uint8_t log_tbl[GF_FIELD_SIZE];
70b6d55a   plank   Big checkin after...
698
699
  uint8_t antilog_tbl[GF_FIELD_SIZE*2];

70b6d55a   plank   Big checkin after...
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
  h = (gf_internal_t *) gf->scratch;
  std = (struct gf_quad_table_lazy_data *)h->private;

  b = 1;
  for (a = 0; a < GF_MULT_GROUP_SIZE; a++) {
      log_tbl[b] = a;
      antilog_tbl[a] = b;
      antilog_tbl[a+GF_MULT_GROUP_SIZE] = b;
      b <<= 1;
      if (b & GF_FIELD_SIZE) {
          b = b ^ h->prim_poly;
      }
  }

  bzero(std->smult, sizeof(uint8_t) * GF_FIELD_SIZE * GF_FIELD_SIZE);
  bzero(std->div, sizeof(uint8_t) * GF_FIELD_SIZE * GF_FIELD_SIZE);

  for (a = 1; a < GF_FIELD_SIZE; a++) {
    loga = log_tbl[a];
    for (b = 1; b < GF_FIELD_SIZE; b++) {
      logb = log_tbl[b];
      prod = antilog_tbl[loga+logb];
      std->smult[a][b] = prod;
      std->div[prod][b] = a;
    }
  }

  gf->inverse.w32 = NULL;
87f0d439   Bassam Tabbara   Add support for p...
728
729
730
731
  gf->divide.w32 = gf_w4_quad_table_lazy_divide;
  gf->multiply.w32 = gf_w4_quad_table_lazy_multiply;
  gf->multiply_region.w32 = gf_w4_quad_table_multiply_region;
  return 1;
70b6d55a   plank   Big checkin after...
732
733
734
735
736
737
738
739
}

static 
int gf_w4_table_init(gf_t *gf)
{
  int rt;
  gf_internal_t *h;
  int simd = 0;
70b6d55a   plank   Big checkin after...
740
741
742

#if defined(INTEL_SSSE3) || defined(ARM_NEON)
  simd = 1;
110523d6   Jim Plank   GF-Complete Relea...
743
#endif
4339569f   Bassam Tabbara   Support for runti...
744
745
746

  h = (gf_internal_t *) gf->scratch;
  rt = (h->region_type);
110523d6   Jim Plank   GF-Complete Relea...
747
748

  if (h->mult_type == GF_MULT_DEFAULT && !simd) rt |= GF_REGION_DOUBLE_TABLE;
70b6d55a   plank   Big checkin after...
749
750
751
752
753
754
755

  if (rt & GF_REGION_DOUBLE_TABLE) {
    return gf_w4_double_table_init(gf);
  } else if (rt & GF_REGION_QUAD_TABLE) {
    if (rt & GF_REGION_LAZY) {
      return gf_w4_quad_table_lazy_init(gf);
    } else {
110523d6   Jim Plank   GF-Complete Relea...
756
757
758
      return gf_w4_quad_table_init(gf);
    }
  } else {
70b6d55a   plank   Big checkin after...
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
    return gf_w4_single_table_init(gf);
  }
  return 0;
}

/* ------------------------------------------------------------
   JSP: GF_MULT_BYTWO_p and _b: See the paper.
*/

static
inline
gf_val_32_t
gf_w4_bytwo_p_multiply (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  uint32_t prod, pp, pmask, amask;
  gf_internal_t *h;
  
  h = (gf_internal_t *) gf->scratch;
  pp = h->prim_poly;

  
  prod = 0;
  pmask = 0x8;
  amask = 0x8;

  while (amask != 0) {
    if (prod & pmask) {
      prod = ((prod << 1) ^ pp);
    } else {
      prod <<= 1;
    }
    if (a & amask) prod ^= b;
    amask >>= 1;
  }
  return prod;
}

static
inline
gf_val_32_t
gf_w4_bytwo_b_multiply (gf_t *gf, gf_val_32_t a, gf_val_32_t b)
{
  uint32_t prod, pp, bmask;
  gf_internal_t *h;
  
  h = (gf_internal_t *) gf->scratch;
  pp = h->prim_poly;

  prod = 0;
  bmask = 0x8;

  while (1) {
    if (a & 1) prod ^= b;
    a >>= 1;
    if (a == 0) return prod;
    if (b & bmask) {
      b = ((b << 1) ^ pp);
    } else {
      b <<= 1;
    }
  }
}

static
void 
gf_w4_bytwo_p_nosse_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
{
  uint64_t *s64, *d64, t1, t2, ta, prod, amask;
  gf_region_data rd;
  struct gf_bytwo_data *btd;
e96c0e28   plank   Renaming gf.h to ...
829
    
70b6d55a   plank   Big checkin after...
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }

  btd = (struct gf_bytwo_data *) ((gf_internal_t *) (gf->scratch))->private;

  gf_set_region_data(&rd, gf, src, dest, bytes, val, xor, 8);
  gf_do_initial_region_alignment(&rd);

  s64 = (uint64_t *) rd.s_start;
  d64 = (uint64_t *) rd.d_start;

  if (xor) {
    while (s64 < (uint64_t *) rd.s_top) {
      prod = 0;
      amask = 0x8;
      ta = *s64;
      while (amask != 0) {
        AB2(btd->prim_poly, btd->mask1, btd->mask2, prod, t1, t2);
        if (val & amask) prod ^= ta;
        amask >>= 1;
      }
      *d64 ^= prod;
      d64++;
      s64++;
    }
  } else { 
    while (s64 < (uint64_t *) rd.s_top) {
      prod = 0;
      amask = 0x8;
      ta = *s64;
      while (amask != 0) {
        AB2(btd->prim_poly, btd->mask1, btd->mask2, prod, t1, t2);
        if (val & amask) prod ^= ta;
        amask >>= 1;
      }
      *d64 = prod;
      d64++;
      s64++;
    }
  }
  gf_do_final_region_alignment(&rd);
}
cfcc1881   Loic Dachary   remove unused arg...
872

70b6d55a   plank   Big checkin after...
873
874
875
876
877
878
#define BYTWO_P_ONESTEP {\
      SSE_AB2(pp, m1, prod, t1, t2); \
      t1 = _mm_and_si128(v, one); \
      t1 = _mm_sub_epi8(t1, one); \
      t1 = _mm_and_si128(t1, ta); \
      prod = _mm_xor_si128(prod, t1); \
29899ad4   Loic Dachary   move #if to avoid...
879
      v = _mm_srli_epi64(v, 1); }
70b6d55a   plank   Big checkin after...
880
881
882
883

#ifdef INTEL_SSE2
static
void
70b6d55a   plank   Big checkin after...
884
885
886
gf_w4_bytwo_p_sse_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
{
  int i;
191b86b5   Loic Dachary   remove unused var...
887
  uint8_t *s8, *d8;
70b6d55a   plank   Big checkin after...
888
889
890
891
  uint8_t vrev;
  __m128i pp, m1, ta, prod, t1, t2, tp, one, v;
  struct gf_bytwo_data *btd;
  gf_region_data rd;
e96c0e28   plank   Renaming gf.h to ...
892

70b6d55a   plank   Big checkin after...
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }

  btd = (struct gf_bytwo_data *) ((gf_internal_t *) (gf->scratch))->private;

  gf_set_region_data(&rd, gf, src, dest, bytes, val, xor, 16);
  gf_do_initial_region_alignment(&rd);

  vrev = 0;
  for (i = 0; i < 4; i++) {
    vrev <<= 1;
    if (!(val & (1 << i))) vrev |= 1;
  }

  s8 = (uint8_t *) rd.s_start;
  d8 = (uint8_t *) rd.d_start;

70b6d55a   plank   Big checkin after...
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);
  one = _mm_set1_epi8(1);

  while (d8 < (uint8_t *) rd.d_top) {
    prod = _mm_setzero_si128();
    v = _mm_set1_epi8(vrev);
    ta = _mm_load_si128((__m128i *) s8);
    tp = (!xor) ? _mm_setzero_si128() : _mm_load_si128((__m128i *) d8);
    BYTWO_P_ONESTEP;
    BYTWO_P_ONESTEP;
    BYTWO_P_ONESTEP;
    BYTWO_P_ONESTEP;
    _mm_store_si128((__m128i *) d8, _mm_xor_si128(prod, tp));
    d8 += 16;
    s8 += 16;
70b6d55a   plank   Big checkin after...
926
  }
29899ad4   Loic Dachary   move #if to avoid...
927
  gf_do_final_region_alignment(&rd);
70b6d55a   plank   Big checkin after...
928
929
}
#endif
4339569f   Bassam Tabbara   Support for runti...
930

70b6d55a   plank   Big checkin after...
931
932
933
934
/*
static
void 
gf_w4_bytwo_b_sse_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
70b6d55a   plank   Big checkin after...
935
936
937
938
939
940
{
#ifdef INTEL_SSE2
  uint8_t *d8, *s8, tb;
  __m128i pp, m1, m2, t1, t2, va, vb;
  struct gf_bytwo_data *btd;
  gf_region_data rd;
e96c0e28   plank   Renaming gf.h to ...
941
    
70b6d55a   plank   Big checkin after...
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }

  gf_set_region_data(&rd, gf, src, dest, bytes, val, xor, 16);
  gf_do_initial_region_alignment(&rd);

  s8 = (uint8_t *) rd.s_start;
  d8 = (uint8_t *) rd.d_start;

  btd = (struct gf_bytwo_data *) ((gf_internal_t *) (gf->scratch))->private;

  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);
  m2 = _mm_set1_epi8((btd->mask2)&0xff);

  if (xor) {
    while (d8 < (uint8_t *) rd.d_top) {
      va = _mm_load_si128 ((__m128i *)(s8));
      vb = _mm_load_si128 ((__m128i *)(d8));
      tb = val;
      while (1) {
        if (tb & 1) vb = _mm_xor_si128(vb, va);
        tb >>= 1;
        if (tb == 0) break;
        SSE_AB2(pp, m1, m2, va, t1, t2);
      }
      _mm_store_si128((__m128i *)d8, vb);
      d8 += 16;
      s8 += 16;
    }
  } else {
    while (d8 < (uint8_t *) rd.d_top) {
      va = _mm_load_si128 ((__m128i *)(s8));
      vb = _mm_setzero_si128 ();
      tb = val;
      while (1) {
        if (tb & 1) vb = _mm_xor_si128(vb, va);
        tb >>= 1;
        if (tb == 0) break;
        t1 = _mm_and_si128(_mm_slli_epi64(va, 1), m1);
        t2 = _mm_and_si128(va, m2);
        t2 = _mm_sub_epi64 (
          _mm_slli_epi64(t2, 1), _mm_srli_epi64(t2, (GF_FIELD_WIDTH-1)));
        va = _mm_xor_si128(t1, _mm_and_si128(t2, pp));
      }
      _mm_store_si128((__m128i *)d8, vb);
      d8 += 16;
      s8 += 16;
    }
70b6d55a   plank   Big checkin after...
991
  }
4339569f   Bassam Tabbara   Support for runti...
992
  gf_do_final_region_alignment(&rd);
70b6d55a   plank   Big checkin after...
993
994
#endif
}
29899ad4   Loic Dachary   move #if to avoid...
995
*/
70b6d55a   plank   Big checkin after...
996
997
998
999

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1000
1001
gf_w4_bytwo_b_sse_region_2_noxor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1002
1003
1004
1005
1006
1007
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1008
1009
1010
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

cfcc1881   Loic Dachary   remove unused arg...
1011
  while (d8 < (uint8_t *) rd->d_top) {
70b6d55a   plank   Big checkin after...
1012
1013
1014
1015
    va = _mm_load_si128 ((__m128i *)(s8));
    SSE_AB2(pp, m1, va, t1, t2);
    _mm_store_si128((__m128i *)d8, va);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1016
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1017
  }
70b6d55a   plank   Big checkin after...
1018
}
29899ad4   Loic Dachary   move #if to avoid...
1019
#endif
70b6d55a   plank   Big checkin after...
1020
1021
1022
1023

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1024
1025
gf_w4_bytwo_b_sse_region_2_xor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1026
1027
1028
1029
1030
1031
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1032
1033
1034
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

cfcc1881   Loic Dachary   remove unused arg...
1035
  while (d8 < (uint8_t *) rd->d_top) {
70b6d55a   plank   Big checkin after...
1036
1037
1038
1039
1040
1041
    va = _mm_load_si128 ((__m128i *)(s8));
    SSE_AB2(pp, m1, va, t1, t2);
    vb = _mm_load_si128 ((__m128i *)(d8));
    vb = _mm_xor_si128(vb, va);
    _mm_store_si128((__m128i *)d8, vb);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1042
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1043
  }
70b6d55a   plank   Big checkin after...
1044
}
29899ad4   Loic Dachary   move #if to avoid...
1045
#endif
70b6d55a   plank   Big checkin after...
1046
1047
1048
1049

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1050
1051
gf_w4_bytwo_b_sse_region_4_noxor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1052
1053
1054
1055
1056
1057
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1058
1059
1060
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

cfcc1881   Loic Dachary   remove unused arg...
1061
1062
  while (d8 < (uint8_t *) rd->d_top) {
    va = _mm_load_si128 ((__m128i *)(s8));
70b6d55a   plank   Big checkin after...
1063
1064
1065
1066
    SSE_AB2(pp, m1, va, t1, t2);
    SSE_AB2(pp, m1, va, t1, t2);
    _mm_store_si128((__m128i *)d8, va);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1067
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1068
  }
70b6d55a   plank   Big checkin after...
1069
}
29899ad4   Loic Dachary   move #if to avoid...
1070
#endif
70b6d55a   plank   Big checkin after...
1071
1072
1073
1074

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1075
1076
gf_w4_bytwo_b_sse_region_4_xor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1077
1078
1079
1080
1081
1082
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1083
1084
1085
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

cfcc1881   Loic Dachary   remove unused arg...
1086
1087
  while (d8 < (uint8_t *) rd->d_top) {
    va = _mm_load_si128 ((__m128i *)(s8));
70b6d55a   plank   Big checkin after...
1088
1089
1090
1091
1092
1093
    SSE_AB2(pp, m1, va, t1, t2);
    SSE_AB2(pp, m1, va, t1, t2);
    vb = _mm_load_si128 ((__m128i *)(d8));
    vb = _mm_xor_si128(vb, va);
    _mm_store_si128((__m128i *)d8, vb);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1094
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1095
  }
70b6d55a   plank   Big checkin after...
1096
1097
}
#endif
29899ad4   Loic Dachary   move #if to avoid...
1098

70b6d55a   plank   Big checkin after...
1099
1100
1101
1102

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1103
1104
gf_w4_bytwo_b_sse_region_3_noxor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1105
1106
1107
1108
1109
1110
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1111
1112
1113
1114
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

  while (d8 < (uint8_t *) rd->d_top) {
cfcc1881   Loic Dachary   remove unused arg...
1115
    va = _mm_load_si128 ((__m128i *)(s8));
70b6d55a   plank   Big checkin after...
1116
1117
1118
1119
1120
    vb = va;
    SSE_AB2(pp, m1, va, t1, t2);
    va = _mm_xor_si128(va, vb);
    _mm_store_si128((__m128i *)d8, va);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1121
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1122
  }
70b6d55a   plank   Big checkin after...
1123
}
29899ad4   Loic Dachary   move #if to avoid...
1124
#endif
70b6d55a   plank   Big checkin after...
1125
1126
1127
1128

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1129
1130
gf_w4_bytwo_b_sse_region_3_xor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1131
1132
1133
1134
1135
1136
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1137
1138
1139
1140
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

  while (d8 < (uint8_t *) rd->d_top) {
cfcc1881   Loic Dachary   remove unused arg...
1141
    va = _mm_load_si128 ((__m128i *)(s8));
70b6d55a   plank   Big checkin after...
1142
1143
1144
1145
1146
    vb = _mm_xor_si128(_mm_load_si128 ((__m128i *)(d8)), va);
    SSE_AB2(pp, m1, va, t1, t2);
    vb = _mm_xor_si128(vb, va);
    _mm_store_si128((__m128i *)d8, vb);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1147
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1148
  }
70b6d55a   plank   Big checkin after...
1149
}
29899ad4   Loic Dachary   move #if to avoid...
1150
#endif
70b6d55a   plank   Big checkin after...
1151
1152
1153
1154

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1155
1156
gf_w4_bytwo_b_sse_region_5_noxor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1157
1158
1159
1160
1161
1162
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1163
1164
1165
1166
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

  while (d8 < (uint8_t *) rd->d_top) {
cfcc1881   Loic Dachary   remove unused arg...
1167
1168
    va = _mm_load_si128 ((__m128i *)(s8));
    vb = va;
70b6d55a   plank   Big checkin after...
1169
1170
1171
1172
1173
    SSE_AB2(pp, m1, va, t1, t2);
    SSE_AB2(pp, m1, va, t1, t2);
    va = _mm_xor_si128(va, vb);
    _mm_store_si128((__m128i *)d8, va);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1174
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1175
  }
70b6d55a   plank   Big checkin after...
1176
}
29899ad4   Loic Dachary   move #if to avoid...
1177
#endif
70b6d55a   plank   Big checkin after...
1178
1179
1180
1181

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1182
1183
gf_w4_bytwo_b_sse_region_5_xor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1184
1185
1186
1187
1188
1189
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1190
1191
1192
1193
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

  while (d8 < (uint8_t *) rd->d_top) {
cfcc1881   Loic Dachary   remove unused arg...
1194
1195
    va = _mm_load_si128 ((__m128i *)(s8));
    vb = _mm_xor_si128(_mm_load_si128 ((__m128i *)(d8)), va);
70b6d55a   plank   Big checkin after...
1196
1197
1198
1199
1200
    SSE_AB2(pp, m1, va, t1, t2);
    SSE_AB2(pp, m1, va, t1, t2);
    vb = _mm_xor_si128(vb, va);
    _mm_store_si128((__m128i *)d8, vb);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1201
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1202
  }
70b6d55a   plank   Big checkin after...
1203
}
29899ad4   Loic Dachary   move #if to avoid...
1204
#endif
70b6d55a   plank   Big checkin after...
1205
1206
1207
1208

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1209
1210
gf_w4_bytwo_b_sse_region_7_noxor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1211
1212
1213
1214
1215
1216
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1217
1218
1219
1220
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

  while (d8 < (uint8_t *) rd->d_top) {
cfcc1881   Loic Dachary   remove unused arg...
1221
    va = _mm_load_si128 ((__m128i *)(s8));
70b6d55a   plank   Big checkin after...
1222
    vb = va;
cfcc1881   Loic Dachary   remove unused arg...
1223
    SSE_AB2(pp, m1, va, t1, t2);
70b6d55a   plank   Big checkin after...
1224
1225
1226
1227
1228
    vb = _mm_xor_si128(va, vb);
    SSE_AB2(pp, m1, va, t1, t2);
    va = _mm_xor_si128(va, vb);
    _mm_store_si128((__m128i *)d8, va);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1229
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1230
  }
70b6d55a   plank   Big checkin after...
1231
}
29899ad4   Loic Dachary   move #if to avoid...
1232
#endif
70b6d55a   plank   Big checkin after...
1233
1234
1235
1236

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1237
1238
gf_w4_bytwo_b_sse_region_7_xor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1239
1240
1241
1242
1243
1244
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1245
1246
1247
1248
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

  while (d8 < (uint8_t *) rd->d_top) {
cfcc1881   Loic Dachary   remove unused arg...
1249
    va = _mm_load_si128 ((__m128i *)(s8));
70b6d55a   plank   Big checkin after...
1250
    vb = _mm_xor_si128(_mm_load_si128 ((__m128i *)(d8)), va);
cfcc1881   Loic Dachary   remove unused arg...
1251
    SSE_AB2(pp, m1, va, t1, t2);
70b6d55a   plank   Big checkin after...
1252
1253
1254
1255
1256
    vb = _mm_xor_si128(vb, va);
    SSE_AB2(pp, m1, va, t1, t2);
    vb = _mm_xor_si128(vb, va);
    _mm_store_si128((__m128i *)d8, vb);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1257
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1258
  }
70b6d55a   plank   Big checkin after...
1259
}
29899ad4   Loic Dachary   move #if to avoid...
1260
#endif
70b6d55a   plank   Big checkin after...
1261
1262
1263
1264

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1265
1266
gf_w4_bytwo_b_sse_region_6_noxor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1267
1268
1269
1270
1271
1272
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1273
1274
1275
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

cfcc1881   Loic Dachary   remove unused arg...
1276
  while (d8 < (uint8_t *) rd->d_top) {
70b6d55a   plank   Big checkin after...
1277
    va = _mm_load_si128 ((__m128i *)(s8));
cfcc1881   Loic Dachary   remove unused arg...
1278
    SSE_AB2(pp, m1, va, t1, t2);
70b6d55a   plank   Big checkin after...
1279
1280
1281
1282
1283
    vb = va;
    SSE_AB2(pp, m1, va, t1, t2);
    va = _mm_xor_si128(va, vb);
    _mm_store_si128((__m128i *)d8, va);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1284
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1285
  }
70b6d55a   plank   Big checkin after...
1286
}
29899ad4   Loic Dachary   move #if to avoid...
1287
#endif
70b6d55a   plank   Big checkin after...
1288
1289
1290
1291

#ifdef INTEL_SSE2
static 
void
191b86b5   Loic Dachary   remove unused var...
1292
1293
gf_w4_bytwo_b_sse_region_6_xor(gf_region_data *rd, struct gf_bytwo_data *btd)
{
70b6d55a   plank   Big checkin after...
1294
1295
1296
1297
1298
1299
  uint8_t *d8, *s8;
  __m128i pp, m1, t1, t2, va, vb;

  s8 = (uint8_t *) rd->s_start;
  d8 = (uint8_t *) rd->d_start;

70b6d55a   plank   Big checkin after...
1300
1301
1302
  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);

cfcc1881   Loic Dachary   remove unused arg...
1303
  while (d8 < (uint8_t *) rd->d_top) {
70b6d55a   plank   Big checkin after...
1304
    va = _mm_load_si128 ((__m128i *)(s8));
cfcc1881   Loic Dachary   remove unused arg...
1305
    SSE_AB2(pp, m1, va, t1, t2);
70b6d55a   plank   Big checkin after...
1306
1307
1308
1309
1310
    vb = _mm_xor_si128(_mm_load_si128 ((__m128i *)(d8)), va);
    SSE_AB2(pp, m1, va, t1, t2);
    vb = _mm_xor_si128(vb, va);
    _mm_store_si128((__m128i *)d8, vb);
    d8 += 16;
70b6d55a   plank   Big checkin after...
1311
    s8 += 16;
29899ad4   Loic Dachary   move #if to avoid...
1312
  }
70b6d55a   plank   Big checkin after...
1313
}
29899ad4   Loic Dachary   move #if to avoid...
1314
#endif
70b6d55a   plank   Big checkin after...
1315
1316
1317
1318

#ifdef INTEL_SSE2
static
void 
70b6d55a   plank   Big checkin after...
1319
1320
1321
1322
1323
1324
gf_w4_bytwo_b_sse_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
{
  uint8_t *d8, *s8, tb;
  __m128i pp, m1, m2, t1, t2, va, vb;
  struct gf_bytwo_data *btd;
  gf_region_data rd;
e96c0e28   plank   Renaming gf.h to ...
1325
    
70b6d55a   plank   Big checkin after...
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }

  gf_set_region_data(&rd, gf, src, dest, bytes, val, xor, 16);
  gf_do_initial_region_alignment(&rd);

  s8 = (uint8_t *) rd.s_start;
  d8 = (uint8_t *) rd.d_start;

  btd = (struct gf_bytwo_data *) ((gf_internal_t *) (gf->scratch))->private;

  switch (val) {
    case 2:
      if (!xor) {
        gf_w4_bytwo_b_sse_region_2_noxor(&rd, btd);
      } else {
        gf_w4_bytwo_b_sse_region_2_xor(&rd, btd);
      }
      gf_do_final_region_alignment(&rd);
      return;
    case 3:
      if (!xor) {
        gf_w4_bytwo_b_sse_region_3_noxor(&rd, btd);
      } else {
        gf_w4_bytwo_b_sse_region_3_xor(&rd, btd);
      }
      gf_do_final_region_alignment(&rd);
      return;
    case 4:
      if (!xor) {
        gf_w4_bytwo_b_sse_region_4_noxor(&rd, btd);
      } else {
        gf_w4_bytwo_b_sse_region_4_xor(&rd, btd);
      }
      gf_do_final_region_alignment(&rd);
      return;
    case 5:
      if (!xor) {
        gf_w4_bytwo_b_sse_region_5_noxor(&rd, btd);
      } else {
        gf_w4_bytwo_b_sse_region_5_xor(&rd, btd);
      }
      gf_do_final_region_alignment(&rd);
      return;
    case 6:
      if (!xor) {
        gf_w4_bytwo_b_sse_region_6_noxor(&rd, btd);
      } else {
        gf_w4_bytwo_b_sse_region_6_xor(&rd, btd);
      }
      gf_do_final_region_alignment(&rd);
      return;
    case 7:
      if (!xor) {
        gf_w4_bytwo_b_sse_region_7_noxor(&rd, btd);
      } else {
        gf_w4_bytwo_b_sse_region_7_xor(&rd, btd);
      }
      gf_do_final_region_alignment(&rd);
      return;
  }

  pp = _mm_set1_epi8(btd->prim_poly&0xff);
  m1 = _mm_set1_epi8((btd->mask1)&0xff);
  m2 = _mm_set1_epi8((btd->mask2)&0xff);

  if (xor) {
    while (d8 < (uint8_t *) rd.d_top) {
      va = _mm_load_si128 ((__m128i *)(s8));
      vb = _mm_load_si128 ((__m128i *)(d8));
      tb = val;
      while (1) {
        if (tb & 1) vb = _mm_xor_si128(vb, va);
cfcc1881   Loic Dachary   remove unused arg...
1399
        tb >>= 1;
70b6d55a   plank   Big checkin after...
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
        if (tb == 0) break;
        SSE_AB2(pp, m1, va, t1, t2);
      }
      _mm_store_si128((__m128i *)d8, vb);
      d8 += 16;
      s8 += 16;
    }
  } else {
    while (d8 < (uint8_t *) rd.d_top) {
      va = _mm_load_si128 ((__m128i *)(s8));
      vb = _mm_setzero_si128 ();
      tb = val;
      while (1) {
        if (tb & 1) vb = _mm_xor_si128(vb, va);
        tb >>= 1;
        if (tb == 0) break;
        t1 = _mm_and_si128(_mm_slli_epi64(va, 1), m1);
        t2 = _mm_and_si128(va, m2);
        t2 = _mm_sub_epi64 (
          _mm_slli_epi64(t2, 1), _mm_srli_epi64(t2, (GF_FIELD_WIDTH-1)));
        va = _mm_xor_si128(t1, _mm_and_si128(t2, pp));
      }
      _mm_store_si128((__m128i *)d8, vb);
      d8 += 16;
      s8 += 16;
    }
70b6d55a   plank   Big checkin after...
1426
  }
29899ad4   Loic Dachary   move #if to avoid...
1427
  gf_do_final_region_alignment(&rd);
70b6d55a   plank   Big checkin after...
1428
1429
1430
1431
1432
}
#endif

static
void 
70b6d55a   plank   Big checkin after...
1433
1434
1435
1436
1437
gf_w4_bytwo_b_nosse_multiply_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor)
{
  uint64_t *s64, *d64, t1, t2, ta, tb, prod;
  struct gf_bytwo_data *btd;
  gf_region_data rd;
e96c0e28   plank   Renaming gf.h to ...
1438

70b6d55a   plank   Big checkin after...
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
  if (val == 0) { gf_multby_zero(dest, bytes, xor); return; }
  if (val == 1) { gf_multby_one(src, dest, bytes, xor); return; }

  gf_set_region_data(&rd, gf, src, dest, bytes, val, xor, 16);
  gf_do_initial_region_alignment(&rd);

  btd = (struct gf_bytwo_data *) ((gf_internal_t *) (gf->scratch))->private;
  s64 = (uint64_t *) rd.s_start;
  d64 = (uint64_t *) rd.d_start;

  switch (val) {
  case 1:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        *d64 ^= *s64;
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        *d64 = *s64;
        d64++;
        s64++;
      }
    }
    break;
  case 2:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= ta;
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = ta;
        d64++;
        s64++;
      }
    }
    break; 
  case 3:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = (ta ^ prod);
        d64++;
        s64++;
      }
    }
    break; 
  case 4:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= ta;
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = ta;
        d64++;
        s64++;
      }
    }
    break; 
  case 5:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = ta ^ prod;
        d64++;
        s64++;
3b6364e5   Danny Al-Gaaf   gf_w4.c: add miss...
1546
      }
70b6d55a   plank   Big checkin after...
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
    }
    break;
  case 6:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = ta ^ prod;
        d64++;
        s64++;
3b6364e5   Danny Al-Gaaf   gf_w4.c: add miss...
1569
      }
70b6d55a   plank   Big checkin after...
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
    }
    break;
  case 7:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = ta ^ prod;
        d64++;
        s64++;
      }
    }
    break; 
  case 8:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= ta;
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = ta;
        d64++;
        s64++;
      }
    }
    break; 
  case 9:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = (ta ^ prod);
        d64++;
        s64++;
      }
    }
    break; 
  case 10:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = (ta ^ prod);
        d64++;
        s64++;
      }
    }
    break; 
  case 11:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = (ta ^ prod);
        d64++;
        s64++;
      }
    }
    break; 
  case 12:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = (ta ^ prod);
        d64++;
        s64++;
      }
    }
    break; 
  case 13:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = (ta ^ prod);
        d64++;
        s64++;
      }
    }
    break; 
  case 14:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = (ta ^ prod);
        d64++;
        s64++;
      }
    }
    break; 
  case 15:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 ^= (ta ^ prod);
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        ta = *s64;
        prod = ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        prod ^= ta;
        AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        *d64 = (ta ^ prod);
        d64++;
        s64++;
      }
    }
    break; 
  default:
    if (xor) {
      while (d64 < (uint64_t *) rd.d_top) {
        prod = *d64 ;
        ta = *s64;
        tb = val;
        while (1) {
          if (tb & 1) prod ^= ta;
          tb >>= 1;
          if (tb == 0) break;
          AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        }
        *d64 = prod;
        d64++;
        s64++;
      }
    } else {
      while (d64 < (uint64_t *) rd.d_top) {
        prod = 0 ;
        ta = *s64;
        tb = val;
        while (1) {
          if (tb & 1) prod ^= ta;
          tb >>= 1;
          if (tb == 0) break;
          AB2(btd->prim_poly, btd->mask1, btd->mask2, ta, t1, t2);
        }
        *d64 = prod;
        d64++;
        s64++;
      }
    }
    break;
  }
  gf_do_final_region_alignment(&rd);
}

static 
int gf_w4_bytwo_init(gf_t *gf)
{
  gf_internal_t *h;
  uint64_t ip, m1, m2;
  struct gf_bytwo_data *btd;

  h = (gf_internal_t *) gf->scratch;
  btd = (struct gf_bytwo_data *) (h->private);
  ip = h->prim_poly & 0xf;
  m1 = 0xe;
  m2 = 0x8;
  btd->prim_poly = 0;
  btd->mask1 = 0;
  btd->mask2 = 0;

  while (ip != 0) {
    btd->prim_poly |= ip;
    btd->mask1 |= m1;
    btd->mask2 |= m2;
    ip <<= GF_FIELD_WIDTH;
    m1 <<= GF_FIELD_WIDTH;
    m2 <<= GF_FIELD_WIDTH;
  }
87f0d439   Bassam Tabbara   Add support for p...
1866

110523d6   Jim Plank   GF-Complete Relea...
1867
  if (h->mult_type == GF_MULT_BYTWO_p) {
4339569f   Bassam Tabbara   Support for runti...
1868
    gf->multiply.w32 = gf_w4_bytwo_p_multiply;
87f0d439   Bassam Tabbara   Add support for p...
1869
    #ifdef INTEL_SSE2
4339569f   Bassam Tabbara   Support for runti...
1870
1871
1872
1873
1874
1875
1876
      if (h->region_type & GF_REGION_NOSIMD)
        gf->multiply_region.w32 = gf_w4_bytwo_p_nosse_multiply_region;
      else
        gf->multiply_region.w32 = gf_w4_bytwo_p_sse_multiply_region;
    #else
      gf->multiply_region.w32 = gf_w4_bytwo_p_nosse_multiply_region;
      if (h->region_type & GF_REGION_SIMD)
110523d6   Jim Plank   GF-Complete Relea...
1877
        return 0;
70b6d55a   plank   Big checkin after...
1878
    #endif
87f0d439   Bassam Tabbara   Add support for p...
1879
  } else {
110523d6   Jim Plank   GF-Complete Relea...
1880
    gf->multiply.w32 = gf_w4_bytwo_b_multiply;
4339569f   Bassam Tabbara   Support for runti...
1881
    #ifdef INTEL_SSE2
87f0d439   Bassam Tabbara   Add support for p...
1882
      if (h->region_type & GF_REGION_NOSIMD)
4339569f   Bassam Tabbara   Support for runti...
1883
1884
1885
1886
1887
1888
1889
        gf->multiply_region.w32 = gf_w4_bytwo_b_nosse_multiply_region;
      else
        gf->multiply_region.w32 = gf_w4_bytwo_b_sse_multiply_region;
    #else
      gf->multiply_region.w32 = gf_w4_bytwo_b_nosse_multiply_region;
      if (h->region_type & GF_REGION_SIMD)
        return 0;
110523d6   Jim Plank   GF-Complete Relea...
1890
    #endif
70b6d55a   plank   Big checkin after...
1891
  }
70b6d55a   plank   Big checkin after...
1892
1893
1894
1895
  return 1;
}


110523d6   Jim Plank   GF-Complete Relea...
1896
1897
static 
int gf_w4_cfm_init(gf_t *gf)
70b6d55a   plank   Big checkin after...
1898
{
fb0bbdcf   Jim Plank   Fixed the problem...
1899
#if defined(INTEL_SSE4_PCLMUL)
4339569f   Bassam Tabbara   Support for runti...
1900
1901
1902
1903
  gf->multiply.w32 = gf_w4_clm_multiply;
  return 1;
#elif defined(ARM_NEON)
  return gf_w4_neon_cfm_init(gf);
1311a44f   Janne Grunau   arm: NEON optimis...
1904
#endif
4339569f   Bassam Tabbara   Support for runti...
1905
1906
1907
  return 0;
}

110523d6   Jim Plank   GF-Complete Relea...
1908
1909
static 
int gf_w4_shift_init(gf_t *gf)
70b6d55a   plank   Big checkin after...
1910
1911
1912
1913
1914
{
  gf->multiply.w32 = gf_w4_shift_multiply;
  return 1;
}

87f0d439   Bassam Tabbara   Add support for p...
1915
/* JSP: I'm putting all error-checking into gf_error_check(), so you don't 
70b6d55a   plank   Big checkin after...
1916
1917
1918
   have to do error checking in scratch_size or in init */

int gf_w4_scratch_size(int mult_type, int region_type, int divide_type, int arg1, int arg2)
110523d6   Jim Plank   GF-Complete Relea...
1919
1920
1921
{
  int issse3 = 0, isneon = 0;

70b6d55a   plank   Big checkin after...
1922
1923
#ifdef INTEL_SSSE3
  issse3 = 1;
70b6d55a   plank   Big checkin after...
1924
1925
1926
1927
#endif
#ifdef ARM_NEON
  isneon = 1;
#endif
70b6d55a   plank   Big checkin after...
1928
1929

  switch(mult_type)
c9750778   plank   Changed the defau...
1930
  {
70b6d55a   plank   Big checkin after...
1931
    case GF_MULT_BYTWO_p:
110523d6   Jim Plank   GF-Complete Relea...
1932
    case GF_MULT_BYTWO_b:
70b6d55a   plank   Big checkin after...
1933
1934
      return sizeof(gf_internal_t) + sizeof(struct gf_bytwo_data);
      break;
110523d6   Jim Plank   GF-Complete Relea...
1935
    case GF_MULT_DEFAULT:
4339569f   Bassam Tabbara   Support for runti...
1936
1937
    case GF_MULT_TABLE:
      if (region_type == GF_REGION_CAUCHY) {
1311a44f   Janne Grunau   arm: NEON optimis...
1938
        return sizeof(gf_internal_t) + sizeof(struct gf_single_table_data) + 64;
110523d6   Jim Plank   GF-Complete Relea...
1939
1940
      }

70b6d55a   plank   Big checkin after...
1941
1942
      if (mult_type == GF_MULT_DEFAULT && !(issse3 || isneon))
          region_type = GF_REGION_DOUBLE_TABLE;
70b6d55a   plank   Big checkin after...
1943
1944
1945
1946
1947

      if (region_type & GF_REGION_DOUBLE_TABLE) {
        return sizeof(gf_internal_t) + sizeof(struct gf_double_table_data) + 64;
      } else if (region_type & GF_REGION_QUAD_TABLE) {
        if ((region_type & GF_REGION_LAZY) == 0) {
110523d6   Jim Plank   GF-Complete Relea...
1948
1949
          return sizeof(gf_internal_t) + sizeof(struct gf_quad_table_data) + 64;
        } else {
70b6d55a   plank   Big checkin after...
1950
          return sizeof(gf_internal_t) + sizeof(struct gf_quad_table_lazy_data) + 64;
70b6d55a   plank   Big checkin after...
1951
        }
110523d6   Jim Plank   GF-Complete Relea...
1952
      } else {
70b6d55a   plank   Big checkin after...
1953
        return sizeof(gf_internal_t) + sizeof(struct gf_single_table_data) + 64;
70b6d55a   plank   Big checkin after...
1954
1955
      }
      break;
110523d6   Jim Plank   GF-Complete Relea...
1956
1957
1958

    case GF_MULT_LOG_TABLE:
      return sizeof(gf_internal_t) + sizeof(struct gf_logtable_data) + 64;
70b6d55a   plank   Big checkin after...
1959
      break;
70b6d55a   plank   Big checkin after...
1960
1961
1962
    case GF_MULT_CARRY_FREE:
      return sizeof(gf_internal_t);
      break;
110523d6   Jim Plank   GF-Complete Relea...
1963
    case GF_MULT_SHIFT:
70b6d55a   plank   Big checkin after...
1964
      return sizeof(gf_internal_t);
110523d6   Jim Plank   GF-Complete Relea...
1965
      break;
70b6d55a   plank   Big checkin after...
1966
1967
1968
1969
1970
1971
1972
1973
1974
    default:
      return 0;
   }
  return 0;
}

int
gf_w4_init (gf_t *gf)
{
110523d6   Jim Plank   GF-Complete Relea...
1975
  gf_internal_t *h;
87f0d439   Bassam Tabbara   Add support for p...
1976
1977
1978
1979
1980

  h = (gf_internal_t *) gf->scratch;
  if (h->prim_poly == 0) h->prim_poly = 0x13;
  h->prim_poly |= 0x10;
  gf->multiply.w32 = NULL;
70b6d55a   plank   Big checkin after...
1981
1982
  gf->divide.w32 = NULL;
  gf->inverse.w32 = NULL;
110523d6   Jim Plank   GF-Complete Relea...
1983
1984
  gf->multiply_region.w32 = NULL;
  gf->extract_word.w32 = gf_w4_extract_word;
70b6d55a   plank   Big checkin after...
1985

110523d6   Jim Plank   GF-Complete Relea...
1986
1987
  switch(h->mult_type) {
    case GF_MULT_CARRY_FREE: if (gf_w4_cfm_init(gf) == 0) return 0; break;
c9750778   plank   Changed the defau...
1988
    case GF_MULT_SHIFT:      if (gf_w4_shift_init(gf) == 0) return 0; break;
110523d6   Jim Plank   GF-Complete Relea...
1989
    case GF_MULT_BYTWO_p:   
70b6d55a   plank   Big checkin after...
1990
1991
1992
1993
    case GF_MULT_BYTWO_b:    if (gf_w4_bytwo_init(gf) == 0) return 0; break;
    case GF_MULT_LOG_TABLE:  if (gf_w4_log_init(gf) == 0) return 0; break;
    case GF_MULT_DEFAULT:   
    case GF_MULT_TABLE:      if (gf_w4_table_init(gf) == 0) return 0; break;
87f0d439   Bassam Tabbara   Add support for p...
1994
1995
    default: return 0;
  }
70b6d55a   plank   Big checkin after...
1996

87f0d439   Bassam Tabbara   Add support for p...
1997
1998
  if (h->divide_type == GF_DIVIDE_EUCLID) {
    gf->divide.w32 = gf_w4_divide_from_inverse;
70b6d55a   plank   Big checkin after...
1999
2000
    gf->inverse.w32 = gf_w4_euclid;
  } else if (h->divide_type == GF_DIVIDE_MATRIX) {
110523d6   Jim Plank   GF-Complete Relea...
2001
    gf->divide.w32 = gf_w4_divide_from_inverse;
87f0d439   Bassam Tabbara   Add support for p...
2002
2003
    gf->inverse.w32 = gf_w4_matrix;
  }
70b6d55a   plank   Big checkin after...
2004

110523d6   Jim Plank   GF-Complete Relea...
2005
  if (gf->divide.w32 == NULL) {
87f0d439   Bassam Tabbara   Add support for p...
2006
    gf->divide.w32 = gf_w4_divide_from_inverse;
70b6d55a   plank   Big checkin after...
2007
2008
    if (gf->inverse.w32 == NULL) gf->inverse.w32 = gf_w4_euclid;
  }
87f0d439   Bassam Tabbara   Add support for p...
2009
2010

  if (gf->inverse.w32 == NULL)  gf->inverse.w32 = gf_w4_inverse_from_divide;
70b6d55a   plank   Big checkin after...
2011

110523d6   Jim Plank   GF-Complete Relea...
2012
2013
  if (h->region_type == GF_REGION_CAUCHY) {
    gf->multiply_region.w32 = gf_wgen_cauchy_region;
87f0d439   Bassam Tabbara   Add support for p...
2014
    gf->extract_word.w32 = gf_wgen_extract_word;
110523d6   Jim Plank   GF-Complete Relea...
2015
2016
  }

70b6d55a   plank   Big checkin after...
2017
2018
  if (gf->multiply_region.w32 == NULL) {
    gf->multiply_region.w32 = gf_w4_multiply_region_from_single;
345a4de1   Jim Plank   See the last comm...
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
  }

  return 1;
}

/* Inline setup functions */

uint8_t *gf_w4_get_mult_table(gf_t *gf)
{
  gf_internal_t *h;
  struct gf_single_table_data *std;
  
  h = (gf_internal_t *) gf->scratch;
  if (gf->multiply.w32 == gf_w4_single_table_multiply) {
    std = (struct gf_single_table_data *) h->private;
    return (uint8_t *) std->mult;
  } 
  return NULL;
}
    
uint8_t *gf_w4_get_div_table(gf_t *gf)
{
  gf_internal_t *h;
  struct gf_single_table_data *std;
  
  h = (gf_internal_t *) gf->scratch;
  if (gf->multiply.w32 == gf_w4_single_table_multiply) {
    std = (struct gf_single_table_data *) h->private;
    return (uint8_t *) std->div;