liberasurecode 1.8.0
Erasure Code API library
Loading...
Searching...
No Matches
rs_galois.c
Go to the documentation of this file.
1/*
2 * Copyright 2015 Kevin M Greenan
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY
13 * THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16 * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
21 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * vi: set noai tw=79 ts=4 sw=4:
25 */
26
27// DISCLAIMER: This is a totally basic implementation of RS used if a user does not
28// want to install one of the supported backends, such as Jerasure and ISA-L.
29// This is not expected to perform as well as the other supported backends,
30// but does not make any assumptions about the host system. Using a library
31// like Jerasure with GF-Complete will give users the ability to tune to their
32// architecture (Intel or ARM), CPU and memory (lots of options).
33
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37
38// We are only implementing w=16 here. If you want to use something
39// else, then use Jerasure with GF-Complete or ISA-L.
40#define PRIM_POLY 0x1100b
41#define FIELD_SIZE (1 << 16)
42#define GROUP_SIZE (FIELD_SIZE - 1)
43
44static int *log_table = NULL;
45static int *ilog_table = NULL;
46static int *ilog_table_begin = NULL;
47static int init_counter = 0;
48
49__attribute__ ((visibility ("internal")))
50void rs_galois_init_tables(void)
51{
52 if (init_counter++ > 0) {
53 /* already initialized */
54 return;
55 }
56 log_table = (int*)malloc(sizeof(int)*FIELD_SIZE);
57 ilog_table_begin = (int*)malloc(sizeof(int)*FIELD_SIZE*3);
58 int i = 0;
59 int x = 1;
60
61 for (i = 0; i < GROUP_SIZE; i++) {
62 log_table[x] = i;
63 ilog_table_begin[i] = x;
65 ilog_table_begin[i + (GROUP_SIZE*2)] = x;
66 x = x << 1;
67 if (x & FIELD_SIZE) {
68 x ^= PRIM_POLY;
69 }
70 }
72}
73
74__attribute__ ((visibility ("internal")))
75void rs_galois_deinit_tables(void)
76{
78 if (init_counter < 0) {
79 /* deinit when not initialized?? */
80 init_counter = 0;
81 } else if (init_counter > 0) {
82 /* still at least one desc using it */
83 return;
84 } else {
85 free(log_table);
86 log_table = NULL;
87 free(ilog_table_begin);
88 ilog_table_begin = NULL;
89 }
90}
91
92__attribute__ ((visibility ("internal")))
93int rs_galois_mult(int x, int y)
94{
95 int sum;
96 if (x == 0 || y == 0) return 0;
97 // This can 'overflow' beyond 255. This is
98 // handled by positive overflow of ilog_table
99 sum = log_table[x] + log_table[y];
100
101 return ilog_table[sum];
102}
103
104static int rs_galois_div(int x, int y)
105{
106 int diff;
107 if (x == 0) return 0;
108 if (y == 0) return -1;
109
110 // This can 'underflow'. This is handled
111 // by negative overflow of ilog_table
112 diff = log_table[x] - log_table[y];
113
114 return ilog_table[diff];
115}
116
117__attribute__ ((visibility ("internal")))
118int rs_galois_inverse(int x)
119{
120 return rs_galois_div(1, x);
121}
static int * log_table
Definition: rs_galois.c:44
#define FIELD_SIZE
Definition: rs_galois.c:41
#define GROUP_SIZE
Definition: rs_galois.c:42
#define PRIM_POLY
Definition: rs_galois.c:40
static int * ilog_table
Definition: rs_galois.c:45
static int * ilog_table_begin
Definition: rs_galois.c:46
__attribute__((visibility("internal")))
Definition: rs_galois.c:49
static int rs_galois_div(int x, int y)
Definition: rs_galois.c:104
static int init_counter
Definition: rs_galois.c:47