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 * Internal code for Galois field routines. This is not meant for
9 * users to include, but for the internal GF files to use.
14 #include "gf_complete.h"
18 extern void timer_start (double *t);
19 extern double timer_split (const double *t);
20 extern void galois_fill_random (void *buf, int len, unsigned int seed);
33 #ifdef DEBUG_FUNCTIONS
37 const char *multiply_region;
38 const char *extract_word;
42 #ifdef DEBUG_FUNCTIONS
43 #define SET_FUNCTION(gf,method,size,func) \
44 { (gf)->method.size = (func); \
45 ((gf_internal_t*)(gf)->scratch)->method = #func; }
47 #define SET_FUNCTION(gf,method,size,func) \
48 (gf)->method.size = (func);
51 extern int gf_w4_init (gf_t *gf);
52 extern int gf_w4_scratch_size(int mult_type, int region_type, int divide_type, int arg1, int arg2);
54 extern int gf_w8_init (gf_t *gf);
55 extern int gf_w8_scratch_size(int mult_type, int region_type, int divide_type, int arg1, int arg2);
57 extern int gf_w16_init (gf_t *gf);
58 extern int gf_w16_scratch_size(int mult_type, int region_type, int divide_type, int arg1, int arg2);
60 extern int gf_w32_init (gf_t *gf);
61 extern int gf_w32_scratch_size(int mult_type, int region_type, int divide_type, int arg1, int arg2);
63 extern int gf_w64_init (gf_t *gf);
64 extern int gf_w64_scratch_size(int mult_type, int region_type, int divide_type, int arg1, int arg2);
66 extern int gf_w128_init (gf_t *gf);
67 extern int gf_w128_scratch_size(int mult_type, int region_type, int divide_type, int arg1, int arg2);
69 extern int gf_wgen_init (gf_t *gf);
70 extern int gf_wgen_scratch_size(int w, int mult_type, int region_type, int divide_type, int arg1, int arg2);
72 void gf_wgen_cauchy_region(gf_t *gf, void *src, void *dest, gf_val_32_t val, int bytes, int xor);
73 gf_val_32_t gf_wgen_extract_word(gf_t *gf, void *start, int bytes, int index);
75 extern void gf_alignment_error(char *s, int a);
77 extern uint32_t gf_bitmatrix_inverse(uint32_t y, int w, uint32_t pp);
79 /* This returns the correct default for prim_poly when base is used as the base
80 field for COMPOSITE. It returns 0 if we don't have a default prim_poly. */
82 extern uint64_t gf_composite_get_default_poly(gf_t *base);
84 /* This structure lets you define a region multiply. It helps because you can handle
85 unaligned portions of the data with the procedures below, which really cleans
95 int align; /* The number of bytes to which to align. */
96 void *s_start; /* The start and the top of the aligned region. */
102 /* This lets you set up one of these in one call. It also sets the start/top pointers. */
104 void gf_set_region_data(gf_region_data *rd,
113 /* This performs gf->multiply.32() on all of the unaligned bytes in the beginning of the region */
115 extern void gf_do_initial_region_alignment(gf_region_data *rd);
117 /* This performs gf->multiply.32() on all of the unaligned bytes in the end of the region */
119 extern void gf_do_final_region_alignment(gf_region_data *rd);
121 extern void gf_two_byte_region_table_multiply(gf_region_data *rd, uint16_t *base);
123 extern void gf_multby_zero(void *dest, int bytes, int xor);
124 extern void gf_multby_one(void *src, void *dest, int bytes, int xor);
126 typedef enum {GF_E_MDEFDIV, /* Dev != Default && Mult == Default */
127 GF_E_MDEFREG, /* Reg != Default && Mult == Default */
128 GF_E_MDEFARG, /* Args != Default && Mult == Default */
129 GF_E_DIVCOMP, /* Mult == Composite && Div != Default */
130 GF_E_CAUCOMP, /* Mult == Composite && Reg == CAUCHY */
131 GF_E_DOUQUAD, /* Reg == DOUBLE && Reg == QUAD */
132 GF_E_SIMD_NO, /* Reg == SIMD && Reg == NOSIMD */
133 GF_E_CAUCHYB, /* Reg == CAUCHY && Other Reg */
134 GF_E_CAUGT32, /* Reg == CAUCHY && w > 32*/
135 GF_E_ARG1SET, /* Arg1 != 0 && Mult \notin COMPOSITE/SPLIT/GROUP */
136 GF_E_ARG2SET, /* Arg2 != 0 && Mult \notin SPLIT/GROUP */
137 GF_E_MATRIXW, /* Div == MATRIX && w > 32 */
138 GF_E_BAD___W, /* Illegal w */
139 GF_E_DOUBLET, /* Reg == DOUBLE && Mult != TABLE */
140 GF_E_DOUBLEW, /* Reg == DOUBLE && w \notin {4,8} */
141 GF_E_DOUBLEJ, /* Reg == DOUBLE && other Reg */
142 GF_E_DOUBLEL, /* Reg == DOUBLE & LAZY but w = 4 */
143 GF_E_QUAD__T, /* Reg == QUAD && Mult != TABLE */
144 GF_E_QUAD__W, /* Reg == QUAD && w != 4 */
145 GF_E_QUAD__J, /* Reg == QUAD && other Reg */
146 GF_E_LAZY__X, /* Reg == LAZY && not DOUBLE or QUAD*/
147 GF_E_ALTSHIF, /* Mult == Shift && Reg == ALTMAP */
148 GF_E_SSESHIF, /* Mult == Shift && Reg == SIMD|NOSIMD */
149 GF_E_ALT_CFM, /* Mult == CARRY_FREE && Reg == ALTMAP */
150 GF_E_SSE_CFM, /* Mult == CARRY_FREE && Reg == SIMD|NOSIMD */
151 GF_E_PCLMULX, /* Mult == Carry_Free && No PCLMUL */
152 GF_E_ALT_BY2, /* Mult == Bytwo_x && Reg == ALTMAP */
153 GF_E_BY2_SSE, /* Mult == Bytwo_x && Reg == SSE && No SSE2 */
154 GF_E_LOGBADW, /* Mult == LOGx, w too big*/
155 GF_E_LOG___J, /* Mult == LOGx, && Reg == SSE|ALTMAP|NOSSE */
156 GF_E_ZERBADW, /* Mult == LOG_ZERO, w \notin {8,16} */
157 GF_E_ZEXBADW, /* Mult == LOG_ZERO_EXT, w != 8 */
158 GF_E_LOGPOLY, /* Mult == LOG & poly not primitive */
159 GF_E_GR_ARGX, /* Mult == GROUP, Bad arg1/2 */
160 GF_E_GR_W_48, /* Mult == GROUP, w \in { 4, 8 } */
161 GF_E_GR_W_16, /* Mult == GROUP, w == 16, arg1 != 4 || arg2 != 4 */
162 GF_E_GR_128A, /* Mult == GROUP, w == 128, bad args */
163 GF_E_GR_A_27, /* Mult == GROUP, either arg > 27 */
164 GF_E_GR_AR_W, /* Mult == GROUP, either arg > w */
165 GF_E_GR____J, /* Mult == GROUP, Reg == SSE|ALTMAP|NOSSE */
166 GF_E_TABLE_W, /* Mult == TABLE, w too big */
167 GF_E_TAB_SSE, /* Mult == TABLE, SIMD|NOSIMD only apply to w == 4 */
168 GF_E_TABSSE3, /* Mult == TABLE, Need SSSE3 for SSE */
169 GF_E_TAB_ALT, /* Mult == TABLE, Reg == ALTMAP */
170 GF_E_SP128AR, /* Mult == SPLIT, w=128, Bad arg1/arg2 */
171 GF_E_SP128AL, /* Mult == SPLIT, w=128, SSE requires ALTMAP */
172 GF_E_SP128AS, /* Mult == SPLIT, w=128, ALTMAP requires SSE */
173 GF_E_SP128_A, /* Mult == SPLIT, w=128, ALTMAP only with 4/128 */
174 GF_E_SP128_S, /* Mult == SPLIT, w=128, SSE only with 4/128 */
175 GF_E_SPLIT_W, /* Mult == SPLIT, Bad w (8, 16, 32, 64, 128) */
176 GF_E_SP_16AR, /* Mult == SPLIT, w=16, Bad arg1/arg2 */
177 GF_E_SP_16_A, /* Mult == SPLIT, w=16, ALTMAP only with 4/16 */
178 GF_E_SP_16_S, /* Mult == SPLIT, w=16, SSE only with 4/16 */
179 GF_E_SP_32AR, /* Mult == SPLIT, w=32, Bad arg1/arg2 */
180 GF_E_SP_32AS, /* Mult == SPLIT, w=32, ALTMAP requires SSE */
181 GF_E_SP_32_A, /* Mult == SPLIT, w=32, ALTMAP only with 4/32 */
182 GF_E_SP_32_S, /* Mult == SPLIT, w=32, SSE only with 4/32 */
183 GF_E_SP_64AR, /* Mult == SPLIT, w=64, Bad arg1/arg2 */
184 GF_E_SP_64AS, /* Mult == SPLIT, w=64, ALTMAP requires SSE */
185 GF_E_SP_64_A, /* Mult == SPLIT, w=64, ALTMAP only with 4/64 */
186 GF_E_SP_64_S, /* Mult == SPLIT, w=64, SSE only with 4/64 */
187 GF_E_SP_8_AR, /* Mult == SPLIT, w=8, Bad arg1/arg2 */
188 GF_E_SP_8__A, /* Mult == SPLIT, w=8, no ALTMAP */
189 GF_E_SP_SSE3, /* Mult == SPLIT, Need SSSE3 for SSE */
190 GF_E_COMP_A2, /* Mult == COMP, arg1 must be = 2 */
191 GF_E_COMP_SS, /* Mult == COMP, SIMD|NOSIMD */
192 GF_E_COMP__W, /* Mult == COMP, Bad w. */
193 GF_E_UNKFLAG, /* Unknown flag in create_from.... */
194 GF_E_UNKNOWN, /* Unknown mult_type. */
195 GF_E_UNK_REG, /* Unknown region_type. */
196 GF_E_UNK_DIV, /* Unknown divide_type. */
197 GF_E_CFM___W, /* Mult == CFM, Bad w. */
198 GF_E_CFM4POL, /* Mult == CFM & Prim Poly has high bits set. */
199 GF_E_CFM8POL, /* Mult == CFM & Prim Poly has high bits set. */
200 GF_E_CF16POL, /* Mult == CFM & Prim Poly has high bits set. */
201 GF_E_CF32POL, /* Mult == CFM & Prim Poly has high bits set. */
202 GF_E_CF64POL, /* Mult == CFM & Prim Poly has high bits set. */
203 GF_E_FEWARGS, /* Too few args in argc/argv. */
204 GF_E_BADPOLY, /* Bad primitive polynomial -- too many bits set. */
205 GF_E_COMP_PP, /* Bad primitive polynomial -- bigger than sub-field. */
206 GF_E_COMPXPP, /* Can't derive a default pp for composite field. */
207 GF_E_BASE__W, /* Composite -- Base field is the wrong size. */
208 GF_E_TWOMULT, /* In create_from... two -m's. */
209 GF_E_TWO_DIV, /* In create_from... two -d's. */
210 GF_E_POLYSPC, /* Bad numbera after -p. */
211 GF_E_SPLITAR, /* Ran out of arguments in SPLIT */
212 GF_E_SPLITNU, /* Arguments not integers in SPLIT. */
213 GF_E_GROUPAR, /* Ran out of arguments in GROUP */
214 GF_E_GROUPNU, /* Arguments not integers in GROUP. */
215 GF_E_DEFAULT } gf_error_type_t;