Use LUT for group indexes instead of calculating on-the-fly
This commit is contained in:
84
genlut.c
Normal file
84
genlut.c
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* Copyright (C) 2022 Camden Dixie O'Brien
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public
|
||||
* License along with this program. If not, see
|
||||
* <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "sud.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define Q(x) #x
|
||||
#define PRLUT(x) prlut(Q(x), x)
|
||||
|
||||
static void prlut(const char *name, unsigned lut[NCELLS][NDIGITS - 1])
|
||||
{
|
||||
printf("const unsigned %s[NCELLS][NDIGITS - 1] = {\n", name);
|
||||
for (unsigned i = 0; i < NCELLS; ++i) {
|
||||
printf("\t[%2u] = { ", i);
|
||||
for (unsigned j = 0; j < NDIGITS - 1; ++j) {
|
||||
printf("%2u", lut[i][j]);
|
||||
if (j != NDIGITS - 2)
|
||||
putchar(',');
|
||||
putchar(' ');
|
||||
}
|
||||
fputs("},\n", stdout);
|
||||
}
|
||||
fputs("};\n", stdout);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
unsigned rowidx_lut[NCELLS][NDIGITS - 1];
|
||||
unsigned colidx_lut[NCELLS][NDIGITS - 1];
|
||||
unsigned segidx_lut[NCELLS][NDIGITS - 1];
|
||||
|
||||
/* Populate tables. */
|
||||
unsigned rowi, coli, segi;
|
||||
for (unsigned r = 0; r < NDIGITS; ++r) {
|
||||
for (unsigned c = 0; c < NDIGITS; ++c) {
|
||||
/* Calculate row and column indices. */
|
||||
rowi = coli = 0;
|
||||
for (unsigned i = 0; i < NDIGITS; ++i) {
|
||||
if (i != c)
|
||||
rowidx_lut[r * NDIGITS + c][rowi++] = r * NDIGITS + i;
|
||||
if (i != r)
|
||||
colidx_lut[r * NDIGITS + c][coli++] = i * NDIGITS + c;
|
||||
}
|
||||
|
||||
/* Calculate segment indices. */
|
||||
segi = 0;
|
||||
const unsigned sr = SEGLEN * (r / SEGLEN);
|
||||
const unsigned sc = SEGLEN * (c / SEGLEN);
|
||||
for (unsigned i = sr; i < sr + SEGLEN; ++i) {
|
||||
for (unsigned j = sc; j < sc + SEGLEN; ++j) {
|
||||
if (i == r && j == c)
|
||||
continue;
|
||||
segidx_lut[r * NDIGITS + c][segi++] = i * NDIGITS + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("/*\n * This file is auto-generated; do not edit.\n */\n\n");
|
||||
printf("#include \"lut.h\"\n\n");
|
||||
PRLUT(rowidx_lut);
|
||||
putchar('\n');
|
||||
PRLUT(colidx_lut);
|
||||
putchar('\n');
|
||||
PRLUT(segidx_lut);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user