2 * GF-Complete: A Comprehensive Open Source Library for Galois Field Arithmetic
3 * James S. Plank, Ethan L. Miller, Kevin M. Greenan,
4 * Benjamin A. Arnold, John A. Burnum, Adam W. Disney, Allen C. McBride.
8 * The main include file for gf_complete.
11 #ifndef _GF_COMPLETE_H_
12 #define _GF_COMPLETE_H_
17 #include <nmmintrin.h>
20 #include <smmintrin.h>
25 #include <tmmintrin.h>
29 #include <emmintrin.h>
32 #ifdef INTEL_SSE4_PCLMUL
33 #include <wmmintrin.h>
41 /* These are the different ways to perform multiplication.
42 Not all are implemented for all values of w.
43 See the paper for an explanation of how they work. */
45 typedef enum {GF_MULT_DEFAULT,
48 GF_MULT_CARRY_FREE_GK,
57 GF_MULT_COMPOSITE } gf_mult_type_t;
59 /* These are the different ways to optimize region
60 operations. They are bits because you can compose them.
61 Certain optimizations only apply to certain gf_mult_type_t's.
62 Again, please see documentation for how to use these */
64 #define GF_REGION_DEFAULT (0x0)
65 #define GF_REGION_DOUBLE_TABLE (0x1)
66 #define GF_REGION_QUAD_TABLE (0x2)
67 #define GF_REGION_LAZY (0x4)
68 #define GF_REGION_SIMD (0x8)
69 #define GF_REGION_SSE (0x8)
70 #define GF_REGION_NOSIMD (0x10)
71 #define GF_REGION_NOSSE (0x10)
72 #define GF_REGION_ALTMAP (0x20)
73 #define GF_REGION_CAUCHY (0x40)
75 typedef uint32_t gf_region_type_t;
77 /* These are different ways to implement division.
78 Once again, it's best to use "DEFAULT". However,
79 there are times when you may want to experiment
82 typedef enum { GF_DIVIDE_DEFAULT,
84 GF_DIVIDE_EUCLID } gf_division_type_t;
86 /* We support w=4,8,16,32,64 and 128 with their own data types and
87 operations for multiplication, division, etc. We also support
88 a "gen" type so that you can do general gf arithmetic for any
89 value of w from 1 to 32. You can perform a "region" operation
90 on these if you use "CAUCHY" as the mapping.
93 typedef uint32_t gf_val_32_t;
94 typedef uint64_t gf_val_64_t;
95 typedef uint64_t *gf_val_128_t;
98 extern void gf_error();
100 typedef struct gf *GFP;
102 typedef union gf_func_a_b {
103 gf_val_32_t (*w32) (GFP gf, gf_val_32_t a, gf_val_32_t b);
104 gf_val_64_t (*w64) (GFP gf, gf_val_64_t a, gf_val_64_t b);
105 void (*w128)(GFP gf, gf_val_128_t a, gf_val_128_t b, gf_val_128_t c);
109 gf_val_32_t (*w32) (GFP gf, gf_val_32_t a);
110 gf_val_64_t (*w64) (GFP gf, gf_val_64_t a);
111 void (*w128)(GFP gf, gf_val_128_t a, gf_val_128_t b);
115 void (*w32) (GFP gf, void *src, void *dest, gf_val_32_t val, int bytes, int add);
116 void (*w64) (GFP gf, void *src, void *dest, gf_val_64_t val, int bytes, int add);
117 void (*w128)(GFP gf, void *src, void *dest, gf_val_128_t val, int bytes, int add);
121 gf_val_32_t (*w32) (GFP gf, void *start, int bytes, int index);
122 gf_val_64_t (*w64) (GFP gf, void *start, int bytes, int index);
123 void (*w128)(GFP gf, void *start, int bytes, int index, gf_val_128_t rv);
127 gf_func_a_b multiply;
130 gf_region multiply_region;
131 gf_extract extract_word;
135 /* Initializes the GF to defaults. Pass it a pointer to a gf_t.
136 Returns 0 on failure, 1 on success. */
138 extern int gf_init_easy(GFP gf, int w);
140 /* Initializes the GF changing the defaults.
141 Returns 0 on failure, 1 on success.
142 Pass it a pointer to a gf_t.
143 For mult_type and divide_type, use one of gf_mult_type_t gf_divide_type_t .
144 For region_type, OR together the GF_REGION_xxx's defined above.
145 Use 0 as prim_poly for defaults. Otherwise, the leading 1 is optional.
146 Use NULL for scratch_memory to have init_hard allocate memory. Otherwise,
147 use gf_scratch_size() to determine how big scratch_memory has to be.
150 extern int gf_init_hard(GFP gf,
159 void *scratch_memory);
161 /* Determines the size for scratch_memory.
162 Returns 0 on failure and non-zero on success. */
164 extern int gf_scratch_size(int w,
171 /* This reports the gf_scratch_size of a gf_t that has already been created */
173 extern int gf_size(GFP gf);
175 /* Frees scratch memory if gf_init_easy/gf_init_hard called malloc.
176 If recursive = 1, then it calls itself recursively on base_gf. */
178 extern int gf_free(GFP gf, int recursive);
180 /* This is support for inline single multiplications and divisions.
181 I know it's yucky, but if you've got to be fast, you've got to be fast.
182 We support inlining for w=4, w=8 and w=16.
184 To use inline multiplication and division with w=4 or 8, you should use the
185 default gf_t, or one with a single table. Otherwise, gf_w4/8_get_mult_table()
186 will return NULL. Similarly, with w=16, the gf_t must be LOG */
188 uint8_t *gf_w4_get_mult_table(GFP gf);
189 uint8_t *gf_w4_get_div_table(GFP gf);
191 #define GF_W4_INLINE_MULTDIV(table, a, b) (table[((a)<<4)|(b)])
193 uint8_t *gf_w8_get_mult_table(GFP gf);
194 uint8_t *gf_w8_get_div_table(GFP gf);
196 #define GF_W8_INLINE_MULTDIV(table, a, b) (table[(((uint32_t) (a))<<8)|(b)])
198 uint16_t *gf_w16_get_log_table(GFP gf);
199 uint16_t *gf_w16_get_mult_alog_table(GFP gf);
200 uint16_t *gf_w16_get_div_alog_table(GFP gf);
202 #define GF_W16_INLINE_MULT(log, alog, a, b) ((a) == 0 || (b) == 0) ? 0 : (alog[(uint32_t)log[a]+(uint32_t)log[b]])
203 #define GF_W16_INLINE_DIV(log, alog, a, b) ((a) == 0 || (b) == 0) ? 0 : (alog[(int)log[a]-(int)log[b]])