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.
52 #define talloc(type, num) (type *) malloc(sizeof(type)*(num))
54 static void usage(char *s)
56 fprintf(stderr, "usage: jerasure_04 k w - Performs the analogous bit-matrix operations to jerasure_03.\n\n");
57 fprintf(stderr, " It converts the matrix to a kw*kw bit matrix and does the same operations.\n");
58 fprintf(stderr, " k must be < 2^w.\n");
59 fprintf(stderr, "This demonstrates: jerasure_print_bitmatrix()\n");
60 fprintf(stderr, " jerasure_matrix_to_bitmatrix()\n");
61 fprintf(stderr, " jerasure_invertible_bitmatrix()\n");
62 fprintf(stderr, " jerasure_invert_bitmatrix()\n");
63 fprintf(stderr, " jerasure_matrix_multiply().\n");
64 if (s != NULL) fprintf(stderr, "%s\n", s);
68 int main(int argc, char **argv)
70 unsigned int k, w, i, j, n;
77 if (argc != 3) usage(NULL);
78 if (sscanf(argv[1], "%d", &k) == 0 || k <= 0) usage("Bad k");
79 if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 31) usage("Bad w");
80 if (k >= (1 << w)) usage("K too big");
82 matrix = talloc(int, k*k);
83 bitmatrix_copy = talloc(int, k*w*k*w);
84 inverse = talloc(int, k*w*k*w);
86 for (i = 0; i < k; i++) {
87 for (j = 0; j < k; j++) {
88 n = i ^ ((1 << w)-1-j);
89 matrix[i*k+j] = (n == 0) ? 0 : galois_single_divide(1, n, w);
92 bitmatrix = jerasure_matrix_to_bitmatrix(k, k, w, matrix);
94 printf("<HTML><TITLE>jerasure_04");
95 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
97 printf("<h3>jerasure_04");
98 for (i = 1; i < argc; i++) printf(" %s", argv[i]);
102 printf("The Cauchy Bit-Matrix:\n");
103 jerasure_print_bitmatrix(bitmatrix, k*w, k*w, w);
104 memcpy(bitmatrix_copy, bitmatrix, sizeof(int)*k*w*k*w);
105 i = jerasure_invertible_bitmatrix(bitmatrix_copy, k*w);
106 printf("\nInvertible: %s\n", (i == 1) ? "Yes" : "No");
108 printf("\nInverse:\n");
109 memcpy(bitmatrix_copy, bitmatrix, sizeof(int)*k*w*k*w);
110 i = jerasure_invert_bitmatrix(bitmatrix_copy, inverse, k*w);
111 jerasure_print_bitmatrix(inverse, k*w, k*w, w);
112 identity = jerasure_matrix_multiply(inverse, bitmatrix, k*w, k*w, k*w, k*w, 2);
113 printf("\nInverse times matrix (should be identity):\n");
114 jerasure_print_bitmatrix(identity, k*w, k*w, w);