2 * Copyright (c) 2014, James S. Plank and Kevin Greenan
5 * Jerasure - A C/C++ Library for a Variety of Reed-Solomon and RAID-6 Erasure
8 * Revision 2.0: Galois Field backend now links to GF-Complete
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * - Neither the name of the University of Tennessee nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
32 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
33 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
34 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
36 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
40 /* Jerasure's authors:
42 Revision 2.x - 2014: James S. Plank and Kevin M. Greenan
43 Revision 1.2 - 2008: James S. Plank, Scott Simmerman and Catherine D. Schuman.
44 Revision 1.0 - 2007: James S. Plank
55 #define MAX_GF_INSTANCES 64
56 gf_t *gfp_array[MAX_GF_INSTANCES] = { 0 };
57 int gfp_is_composite[MAX_GF_INSTANCES] = { 0 };
59 gf_t *galois_get_field_ptr(int w)
61 if (gfp_array[w] != NULL) {
68 gf_t* galois_init_field(int w,
80 if (w <= 0 || w > 32) {
81 fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
85 gfp = (gf_t *) malloc(sizeof(gf_t));
87 fprintf(stderr, "ERROR -- cannot allocate memory for Galois field w=%d\n", w);
91 scratch_size = gf_scratch_size(w, mult_type, region_type, divide_type, arg1, arg2);
93 fprintf(stderr, "ERROR -- cannot get scratch size for base field w=%d\n", w);
97 scratch_memory = malloc(scratch_size);
98 if (!scratch_memory) {
99 fprintf(stderr, "ERROR -- cannot get scratch memory for base field w=%d\n", w);
103 if(!gf_init_hard(gfp,
114 fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
118 gfp_is_composite[w] = 0;
122 gf_t* galois_init_composite_field(int w,
129 void *scratch_memory;
132 if (w <= 0 || w > 32) {
133 fprintf(stderr, "ERROR -- cannot init composite field for w=%d\n", w);
137 gfp = (gf_t *) malloc(sizeof(gf_t));
139 fprintf(stderr, "ERROR -- cannot allocate memory for Galois field w=%d\n", w);
143 scratch_size = gf_scratch_size(w, GF_MULT_COMPOSITE, region_type, divide_type, degree, 0);
145 fprintf(stderr, "ERROR -- cannot get scratch size for composite field w=%d\n", w);
149 scratch_memory = malloc(scratch_size);
150 if (!scratch_memory) {
151 fprintf(stderr, "ERROR -- cannot get scratch memory for composite field w=%d\n", w);
155 if(!gf_init_hard(gfp,
166 fprintf(stderr, "ERROR -- cannot init default composite field for w=%d\n", w);
169 gfp_is_composite[w] = 1;
173 int galois_init_default_field(int w)
175 if (gfp_array[w] == NULL) {
176 gfp_array[w] = (gf_t*)malloc(sizeof(gf_t));
177 if(gfp_array[w] == NULL)
179 if (!gf_init_easy(gfp_array[w], w))
185 int galois_uninit_field(int w)
188 if (gfp_array[w] != NULL) {
190 ret = gf_free(gfp_array[w], recursive);
197 static void galois_init(int w)
199 if (w <= 0 || w > 32) {
200 fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
204 switch (galois_init_default_field(w)) {
206 fprintf(stderr, "ERROR -- cannot allocate memory for Galois field w=%d\n", w);
210 fprintf(stderr, "ERROR -- cannot init default Galois field for w=%d\n", w);
217 static int is_valid_gf(gf_t *gf, int w)
219 // TODO: I assume we may eventually
220 // want to do w=64 and 128, so w
221 // will be needed to perform this check
227 if (gf->multiply.w32 == NULL) {
230 if (gf->multiply_region.w32 == NULL) {
233 if (gf->divide.w32 == NULL) {
236 if (gf->inverse.w32 == NULL) {
239 if (gf->extract_word.w32 == NULL) {
246 void galois_change_technique(gf_t *gf, int w)
248 if (w <= 0 || w > 32) {
249 fprintf(stderr, "ERROR -- cannot support Galois field for w=%d\n", w);
253 if (!is_valid_gf(gf, w)) {
254 fprintf(stderr, "ERROR -- overriding with invalid Galois field for w=%d\n", w);
258 if (gfp_array[w] != NULL) {
259 gf_free(gfp_array[w], gfp_is_composite[w]);
265 int galois_single_multiply(int x, int y, int w)
267 if (x == 0 || y == 0) return 0;
269 if (gfp_array[w] == NULL) {
274 return gfp_array[w]->multiply.w32(gfp_array[w], x, y);
276 fprintf(stderr, "ERROR -- Galois field not implemented for w=%d\n", w);
281 int galois_single_divide(int x, int y, int w)
283 if (x == 0) return 0;
284 if (y == 0) return -1;
286 if (gfp_array[w] == NULL) {
291 return gfp_array[w]->divide.w32(gfp_array[w], x, y);
293 fprintf(stderr, "ERROR -- Galois field not implemented for w=%d\n", w);
298 void galois_w08_region_multiply(char *region, /* Region to multiply */
299 int multby, /* Number to multiply by */
300 int nbytes, /* Number of bytes in region */
301 char *r2, /* If r2 != NULL, products go here */
304 if (gfp_array[8] == NULL) {
307 gfp_array[8]->multiply_region.w32(gfp_array[8], region, r2, multby, nbytes, add);
310 void galois_w16_region_multiply(char *region, /* Region to multiply */
311 int multby, /* Number to multiply by */
312 int nbytes, /* Number of bytes in region */
313 char *r2, /* If r2 != NULL, products go here */
316 if (gfp_array[16] == NULL) {
319 gfp_array[16]->multiply_region.w32(gfp_array[16], region, r2, multby, nbytes, add);
323 void galois_w32_region_multiply(char *region, /* Region to multiply */
324 int multby, /* Number to multiply by */
325 int nbytes, /* Number of bytes in region */
326 char *r2, /* If r2 != NULL, products go here */
329 if (gfp_array[32] == NULL) {
332 gfp_array[32]->multiply_region.w32(gfp_array[32], region, r2, multby, nbytes, add);
335 void galois_w8_region_xor(void *src, void *dest, int nbytes)
337 if (gfp_array[8] == NULL) {
340 gfp_array[8]->multiply_region.w32(gfp_array[32], src, dest, 1, nbytes, 1);
343 void galois_w16_region_xor(void *src, void *dest, int nbytes)
345 if (gfp_array[16] == NULL) {
348 gfp_array[16]->multiply_region.w32(gfp_array[16], src, dest, 1, nbytes, 1);
351 void galois_w32_region_xor(void *src, void *dest, int nbytes)
353 if (gfp_array[32] == NULL) {
356 gfp_array[32]->multiply_region.w32(gfp_array[32], src, dest, 1, nbytes, 1);
359 void galois_region_xor(char *src, char *dest, int nbytes)
362 galois_w32_region_xor(src, dest, nbytes);
365 for (i = 0; i < nbytes; i++) {
373 int galois_inverse(int y, int w)
375 if (y == 0) return -1;
376 return galois_single_divide(1, y, w);