src$ gcc golay23.c -o golay23 -Wall -DGOLAY23_UNITTEST
src$ ./golay23
+ To generate tables:
+ src$ gcc golay23.c -o golay23 -Wall -DGOLAY23_MAKETABLES -DRUN_TIME_TABLES
+
\*---------------------------------------------------------------------------*/
/* File: golay23.c
static int inited = 0;
+#ifdef RUN_TIME_TABLES
static int encoding_table[4096], decoding_table[2048];
+#else
+#include "golayenctable.h"
+#include "golaydectable.h"
+#endif
+
#ifdef GOLAY23_UNITTEST
static int position[23] = { 0x00000001, 0x00000002, 0x00000004, 0x00000008,
0x00000010, 0x00000020, 0x00000040, 0x00000080,
0x00010000, 0x00020000, 0x00040000, 0x00080000,
0x00100000, 0x00200000, 0x00400000 };
#endif
+
+#ifdef RUN_TIME_TABLES
static int arr2int(int a[], int r)
/*
* Convert a binary vector of Hamming weight r, and nonzero positions in
}
return(result);
}
+#endif
void nextcomb(int n, int r, int a[])
/*
\*---------------------------------------------------------------------------*/
-void golay23_init(void ) {
+void golay23_init(void) {
+#ifdef RUN_TIME_TABLES
int i;
long temp;
int a[4];
temp = arr2int(a,3);
decoding_table[get_syndrome(temp)] = temp;
}
-
+#endif
inited = 1;
}
int main(void)
{
int i;
- long temp;
- long pattern;
int tests;
int a[4];
int error_pattern;
- /*
- * ---------------------------------------------------------------------
- * Generate ENCODING TABLE
- *
- * An entry to the table is an information vector, a 32-bit integer,
- * whose 12 least significant positions are the information bits. The
- * resulting value is a codeword in the (23,12,7) Golay code: A 32-bit
- * integer whose 23 least significant bits are coded bits: Of these, the
- * 12 most significant bits are information bits and the 11 least
- * significant bits are redundant bits (systematic encoding).
- * ---------------------------------------------------------------------
- */
- for (pattern = 0; pattern < 4096; pattern++) {
- temp = pattern << 11; /* multiply information by X^{11} */
- encoding_table[pattern] = temp + get_syndrome(temp);/* add redundancy */
- }
-
- /*
- * ---------------------------------------------------------------------
- * Generate DECODING TABLE
- *
- * An entry to the decoding table is a syndrome and the resulting value
- * is the most likely error pattern. First an error pattern is generated.
- * Then its syndrome is calculated and used as a pointer to the table
- * where the error pattern value is stored.
- * ---------------------------------------------------------------------
- *
- * (1) Error patterns of WEIGHT 1 (SINGLE ERRORS)
- */
- decoding_table[0] = 0;
- decoding_table[1] = 1;
- temp = 1;
- for (i=2; i<= 23; i++) {
- temp *= 2;
- decoding_table[get_syndrome(temp)] = temp;
- }
- /*
- * (2) Error patterns of WEIGHT 2 (DOUBLE ERRORS)
- */
- a[1] = 1; a[2] = 2;
- temp = arr2int(a,2);
- decoding_table[get_syndrome(temp)] = temp;
- for (i=1; i<253; i++) {
- nextcomb(23,2,a);
- temp = arr2int(a,2);
- decoding_table[get_syndrome(temp)] = temp;
- }
- /*
- * (3) Error patterns of WEIGHT 3 (TRIPLE ERRORS)
- */
- a[1] = 1; a[2] = 2; a[3] = 3;
- temp = arr2int(a,3);
- decoding_table[get_syndrome(temp)] = temp;
- for (i=1; i<1771; i++) {
- nextcomb(23,3,a);
- temp = arr2int(a,3);
- decoding_table[get_syndrome(temp)] = temp;
- }
+ golay23_init();
/* ---------------------------------------------------------------------
* Generate DATA
return 0;
}
#endif
+
+#ifdef GOLAY23_MAKETABLES
+int main(int argc, char *argv[]) {
+ FILE *f;
+ int i;
+
+ golay23_init();
+
+ f=fopen("golayenctable.h","wt");
+ assert(f != NULL);
+
+ fprintf(f,"/* Generated by golay23.c -DGOLAY23_MAKETABLE */\n\n");
+ fprintf(f,"const int static encoding_table[]={\n");
+
+ for (i=0; i<4095; i++)
+ fprintf(f," 0x%x,\n", encoding_table[i]);
+ fprintf(f, " 0x%x\n};\n", encoding_table[i]);
+ fclose(f);
+
+ f=fopen("golaydectable.h","wt");
+ assert(f != NULL);
+
+ fprintf(f,"/* Generated by golay23.c -DGOLAY23_MAKETABLE */\n\n");
+ fprintf(f,"const int static decoding_table[]={\n");
+
+ for (i=0; i<2047; i++)
+ fprintf(f," 0x%x,\n", decoding_table[i]);
+ fprintf(f, " 0x%x\n};\n", decoding_table[i]);
+ fclose(f);
+
+ return 0;
+}
+
+#endif
+
+