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 talloc(type, num) (type *) malloc(sizeof(type)*(num))
57 static void usage(char *s)
59 fprintf(stderr, "usage: cauchy_01 n w - Converts the value n to a bitmatrix using GF(2^w).\n");
60 fprintf(stderr, " \n");
61 fprintf(stderr, " It prints the bitmatrix, and reports on the numberof ones.\n");
62 fprintf(stderr, " Use 0x to input n in hexadecimal.\n");
63 fprintf(stderr, " W must be <= 32.\n");
64 fprintf(stderr, " \n");
65 fprintf(stderr, "This demonstrates: cauchy_n_ones()\n");
66 fprintf(stderr, " jerasure_matrix_to_bitmatrix()\n");
67 fprintf(stderr, " jerasure_print_bitmatrix()\n");
68 if (s != NULL) fprintf(stderr, "\n%s\n", s);
72 int main(int argc, char **argv)
78 if (argc != 3) usage(NULL);
79 if (sscanf(argv[1], "0x%x", &n) == 0) {
80 if (sscanf(argv[1], "%d", &n) == 0) usage("Bad n");
82 if (sscanf(argv[2], "%d", &w) == 0 || w <= 0 || w > 32) usage("Bad w");
84 if (n & 0x80000000L) usage("Bad n/w combination (n not between 0 and 2^w-1)\n");
86 if (n >= (1 << w)) usage("Bad n/w combination (n not between 0 and 2^w-1)\n");
89 bitmatrix = jerasure_matrix_to_bitmatrix(1, 1, w, &n);
90 printf("<HTML><title>cauchy_01 %u %d</title>\n", w, n);
91 printf("<HTML><h3>cauchy_01 %u %d</h3>\n", w, n);
94 printf("Converted the value 0x%x to the following bitmatrix:\n\n", n);
96 printf("Converted the value %d (0x%x) to the following bitmatrix:\n\n", n, n);
98 jerasure_print_bitmatrix(bitmatrix, w, w, w);
102 for (i = 0; i < w*w; i++) no += bitmatrix[i];
103 if (no != cauchy_n_ones(n, w)) {
104 fprintf(stderr, "Jerasure error: # ones in the bitmatrix (%d) doesn't match cauchy_n_ones() (%d).\n",
105 no, cauchy_n_ones(n, w));
109 printf("# Ones: %d\n", cauchy_n_ones(n, w));