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
53 #include "liberation.h"
55 #define talloc(type, num) (type *) malloc(sizeof(type)*(num))
57 int *liberation_coding_bitmatrix(int k, int w)
59 int *matrix, i, j, index;
61 if (k > w) return NULL;
62 matrix = talloc(int, 2*k*w*w);
63 if (matrix == NULL) return NULL;
64 bzero(matrix, sizeof(int)*2*k*w*w);
66 /* Set up identity matrices */
68 for(i = 0; i < w; i++) {
70 for (j = 0; j < k; j++) {
76 /* Set up liberation matrices */
78 for (j = 0; j < k; j++) {
80 for (i = 0; i < w; i++) {
81 matrix[index+(j+i)%w] = 1;
86 matrix[k*w*w+j*w+i*k*w+(i+j-1)%w] = 1;
93 int *liber8tion_coding_bitmatrix(int k)
95 int *matrix, i, j, index;
99 if (k > w) return NULL;
100 matrix = talloc(int, 2*k*w*w);
101 if (matrix == NULL) return NULL;
102 bzero(matrix, sizeof(int)*2*k*w*w);
104 /* Set up identity matrices */
106 for(i = 0; i < w; i++) {
108 for (j = 0; j < k; j++) {
114 /* Set up liber8tion matrices */
118 if (k == 0) return matrix;
119 matrix[index+0*k*w+0*w+0] = 1;
120 matrix[index+1*k*w+0*w+1] = 1;
121 matrix[index+2*k*w+0*w+2] = 1;
122 matrix[index+3*k*w+0*w+3] = 1;
123 matrix[index+4*k*w+0*w+4] = 1;
124 matrix[index+5*k*w+0*w+5] = 1;
125 matrix[index+6*k*w+0*w+6] = 1;
126 matrix[index+7*k*w+0*w+7] = 1;
128 if (k == 1) return matrix;
129 matrix[index+0*k*w+1*w+7] = 1;
130 matrix[index+1*k*w+1*w+3] = 1;
131 matrix[index+2*k*w+1*w+0] = 1;
132 matrix[index+3*k*w+1*w+2] = 1;
133 matrix[index+4*k*w+1*w+6] = 1;
134 matrix[index+5*k*w+1*w+1] = 1;
135 matrix[index+6*k*w+1*w+5] = 1;
136 matrix[index+7*k*w+1*w+4] = 1;
137 matrix[index+4*k*w+1*w+7] = 1;
139 if (k == 2) return matrix;
140 matrix[index+0*k*w+2*w+6] = 1;
141 matrix[index+1*k*w+2*w+2] = 1;
142 matrix[index+2*k*w+2*w+4] = 1;
143 matrix[index+3*k*w+2*w+0] = 1;
144 matrix[index+4*k*w+2*w+7] = 1;
145 matrix[index+5*k*w+2*w+3] = 1;
146 matrix[index+6*k*w+2*w+1] = 1;
147 matrix[index+7*k*w+2*w+5] = 1;
148 matrix[index+1*k*w+2*w+3] = 1;
150 if (k == 3) return matrix;
151 matrix[index+0*k*w+3*w+2] = 1;
152 matrix[index+1*k*w+3*w+5] = 1;
153 matrix[index+2*k*w+3*w+7] = 1;
154 matrix[index+3*k*w+3*w+6] = 1;
155 matrix[index+4*k*w+3*w+0] = 1;
156 matrix[index+5*k*w+3*w+3] = 1;
157 matrix[index+6*k*w+3*w+4] = 1;
158 matrix[index+7*k*w+3*w+1] = 1;
159 matrix[index+5*k*w+3*w+4] = 1;
161 if (k == 4) return matrix;
162 matrix[index+0*k*w+4*w+5] = 1;
163 matrix[index+1*k*w+4*w+6] = 1;
164 matrix[index+2*k*w+4*w+1] = 1;
165 matrix[index+3*k*w+4*w+7] = 1;
166 matrix[index+4*k*w+4*w+2] = 1;
167 matrix[index+5*k*w+4*w+4] = 1;
168 matrix[index+6*k*w+4*w+3] = 1;
169 matrix[index+7*k*w+4*w+0] = 1;
170 matrix[index+2*k*w+4*w+0] = 1;
172 if (k == 5) return matrix;
173 matrix[index+0*k*w+5*w+1] = 1;
174 matrix[index+1*k*w+5*w+2] = 1;
175 matrix[index+2*k*w+5*w+3] = 1;
176 matrix[index+3*k*w+5*w+4] = 1;
177 matrix[index+4*k*w+5*w+5] = 1;
178 matrix[index+5*k*w+5*w+6] = 1;
179 matrix[index+6*k*w+5*w+7] = 1;
180 matrix[index+7*k*w+5*w+0] = 1;
181 matrix[index+7*k*w+5*w+2] = 1;
183 if (k == 6) return matrix;
184 matrix[index+0*k*w+6*w+3] = 1;
185 matrix[index+1*k*w+6*w+0] = 1;
186 matrix[index+2*k*w+6*w+6] = 1;
187 matrix[index+3*k*w+6*w+5] = 1;
188 matrix[index+4*k*w+6*w+1] = 1;
189 matrix[index+5*k*w+6*w+7] = 1;
190 matrix[index+6*k*w+6*w+4] = 1;
191 matrix[index+7*k*w+6*w+2] = 1;
192 matrix[index+6*k*w+6*w+5] = 1;
194 if (k == 7) return matrix;
195 matrix[index+0*k*w+7*w+4] = 1;
196 matrix[index+1*k*w+7*w+7] = 1;
197 matrix[index+2*k*w+7*w+1] = 1;
198 matrix[index+3*k*w+7*w+5] = 1;
199 matrix[index+4*k*w+7*w+3] = 1;
200 matrix[index+5*k*w+7*w+2] = 1;
201 matrix[index+6*k*w+7*w+0] = 1;
202 matrix[index+7*k*w+7*w+6] = 1;
203 matrix[index+3*k*w+7*w+1] = 1;
208 int *blaum_roth_coding_bitmatrix(int k, int w)
210 int *matrix, i, j, index, l, m, p;
212 if (k > w) return NULL ;
214 matrix = talloc(int, 2*k*w*w);
215 if (matrix == NULL) return NULL;
216 bzero(matrix, sizeof(int)*2*k*w*w);
218 /* Set up identity matrices */
220 for(i = 0; i < w; i++) {
222 for (j = 0; j < k; j++) {
228 /* Set up blaum_roth matrices -- Ignore identity */
231 for (j = 0; j < k; j++) {
234 for (l = 0; l < w; l++) {
240 for (l = 1; l <= w; l++) {
247 matrix[index+i-1] = 1;
251 m = (p/2) + 1 + (i/2);