Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate ffdata.c and ffdata.h at build time #4723

Merged
merged 2 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions Makefile.rules
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ SOURCES += src/dt.c
SOURCES += src/dteval.c
SOURCES += src/error.c
SOURCES += src/exprs.c
SOURCES += src/ffdata.c
SOURCES += build/ffdata.c
SOURCES += src/finfield.c
SOURCES += src/funcs.c
SOURCES += src/gap.c
Expand Down Expand Up @@ -340,20 +340,22 @@ DEPFLAGS = -MQ $(OBJFILE) -MMD -MP -MF $(DEPFILE)
# DEPFILE and OBJFILE above)
########################################################################

obj_deps = libtool build/config.h build/version.h build/ffdata.h

# Build rule for C++ source files
#
# We disable support for exceptions and RTTI as we don't use them and they
# cause compiler/linker issues in some build configurations; but we are
# careful to not put these into GAP_CXXFLAGS, as kernel extensions may want to
# use GAP_CXXFLAGS but also may need to interface with C++ code in
# libraries that use exceptions.
build/obj/%.cc.lo: %.cc cnf/GAP-CXXFLAGS cnf/GAP-CPPFLAGS libtool build/config.h build/version.h
build/obj/%.cc.lo: %.cc cnf/GAP-CXXFLAGS cnf/GAP-CPPFLAGS $(obj_deps)
@$(MKDIR_P) build/obj/$(*D) build/deps/$(*D)
$(QUIET_CXX)$(LIBTOOL) --mode=compile --tag CXX $(CXX) $(DEPFLAGS) $(GAP_CXXFLAGS) -fno-exceptions -fno-rtti $(WARN_CXXFLAGS) $(GAP_CPPFLAGS) -c $< -o $(OBJFILE)
@echo "$<:" >> $(DEPFILE)

# Build rule for C source files
build/obj/%.c.lo: %.c cnf/GAP-CFLAGS cnf/GAP-CPPFLAGS libtool build/config.h build/version.h
build/obj/%.c.lo: %.c cnf/GAP-CFLAGS cnf/GAP-CPPFLAGS $(obj_deps)
@$(MKDIR_P) build/obj/$(*D) build/deps/$(*D)
$(QUIET_CC)$(LIBTOOL) --mode=compile --tag CC $(CC) $(DEPFLAGS) $(GAP_CFLAGS) $(WARN_CFLAGS) $(GAP_CPPFLAGS) -c $< -o $(OBJFILE)
@echo "$<:" >> $(DEPFILE)
Expand Down Expand Up @@ -436,15 +438,11 @@ build/c_%.c: $(srcdir)/lib/%.g build/gap-nocomp$(EXEEXT)
# The "ffdata" target regenerates src/ffdata.{c,h} using etc/ffgen.c
########################################################################

ffgen: etc/ffgen.c
$(QUIET_CC)$(CC) -Wall -Wextra $< -o $@

ffdata: ffgen
./ffgen -h > $(abs_srcdir)/src/ffdata.h
./ffgen -c > $(abs_srcdir)/src/ffdata.c

.PHONY: ffdata
ffgen: $(srcdir)/etc/ffgen.c src/common.h build/config.h
$(QUIET_CC)$(CC) -Wall -Wextra $(GAP_CPPFLAGS) $< -o $@

build/ffdata.h build/ffdata.c: ffgen
./ffgen -b build/ffdata

########################################################################
# The "tags" target regenerates the tags file using Exuberant Ctags
Expand Down
71 changes: 57 additions & 14 deletions etc/ffgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
finfield.c
*/

#include "common.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -14,6 +16,7 @@ unsigned char is_ff[MAX_FF + 1];
unsigned deg[MAX_FF + 1];
unsigned ch[MAX_FF + 1];
unsigned num_ff;
unsigned max_deg;

void make_primes()
{
Expand All @@ -39,24 +42,41 @@ void make_ff()
ch[j] = i;
num_ff++;
}
if (d > 0 && d - 1 > max_deg)
max_deg = d - 1;
}
}
}

unsigned int needed_bits(unsigned int x)
{
if (x == 0)
return 0;
return 1 + needed_bits(x >> 1);
}

void emit_code(FILE * dest, int header)
{
unsigned i, j;
fprintf(dest, "/* This file is generated by etc/ffgen.c during build do not edit */\n");
if (header) {
fprintf(dest, "#ifndef GAP_FFDATA_H\n");
fprintf(dest, "#define GAP_FFDATA_H\n");
fprintf(dest, "\n");
fprintf(dest, "#include \"common.h\"\n");
fprintf(dest, "\n");
fprintf(dest, "enum {\n");
fprintf(dest, " NUM_SHORT_FINITE_FIELDS = %d\n", num_ff);
fprintf(dest, " NUM_SHORT_FINITE_FIELDS = %d,\n", num_ff);
fprintf(dest, " MAXSIZE_GF_INTERNAL = %d,\n", MAX_FF);
fprintf(dest, " DEGREE_LARGEST_INTERNAL_FF = %d,\n", max_deg);
fprintf(dest, " FIELD_BITS_FFE = %d,\n", needed_bits(num_ff));
fprintf(dest, " VAL_BITS_FFE = %d\n", needed_bits(MAX_FF - 1));
fprintf(dest, "};\n");
fprintf(dest, "\n");
fprintf(dest, "extern const unsigned long SizeFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf(dest, "extern const unsigned char DegrFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf(dest, "extern const unsigned long CharFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf(dest, "extern const UInt4 SizeFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf(dest, "extern const UInt1 DegrFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf(dest, "extern const UInt4 CharFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf(dest, "\n");
fprintf(dest, "\n");
fprintf(dest, "#endif // GAP_FFDATA_H\n");
}
Expand All @@ -67,7 +87,7 @@ void emit_code(FILE * dest, int header)
fprintf(dest, " * to find them. Indices start at 1.\n");
fprintf(dest, " */\n");
fprintf(dest, "\n");
fprintf(dest, "const unsigned char DegrFF[NUM_SHORT_FINITE_FIELDS+1] = {\n");
fprintf(dest, "const UInt1 DegrFF[NUM_SHORT_FINITE_FIELDS+1] = {\n");
fprintf(dest, " %3d,", 0);
for (i = 0, j = 1; i <= MAX_FF; i++) {
if (is_ff[i]) {
Expand All @@ -82,13 +102,13 @@ void emit_code(FILE * dest, int header)
fprintf(dest, "\n");
fprintf(dest, "};\n");
fprintf(dest, "\n");
fprintf(dest, "const unsigned long CharFF[NUM_SHORT_FINITE_FIELDS+1] = {\n");
fprintf(dest, " %6d,", 0);
fprintf(dest, "const UInt4 CharFF[NUM_SHORT_FINITE_FIELDS+1] = {\n");
fprintf(dest, " %9d,", 0);
for (i = 0, j = 1; i <= MAX_FF; i++) {
if (is_ff[i]) {
fprintf(dest, "%6d,", ch[i]);
fprintf(dest, "%9d,", ch[i]);
j++;
j %= 8;
j %= 6;
if (!j)
fprintf(dest, "\n ");
}
Expand All @@ -97,13 +117,13 @@ void emit_code(FILE * dest, int header)
fprintf(dest, "\n");
fprintf(dest, "};\n");
fprintf(dest, "\n");
fprintf(dest, "const unsigned long SizeFF[NUM_SHORT_FINITE_FIELDS+1] = {\n");
fprintf(dest, " %6d,", 0);
fprintf(dest, "const UInt4 SizeFF[NUM_SHORT_FINITE_FIELDS+1] = {\n");
fprintf(dest, " %9d,", 0);
for (i = 0, j = 1; i <= MAX_FF; i++) {
if (is_ff[i]) {
fprintf(dest, "%6d,", i);
fprintf(dest, "%9d,", i);
j++;
j %= 8;
j %= 6;
if (!j)
fprintf(dest, "\n ");
}
Expand All @@ -114,6 +134,22 @@ void emit_code(FILE * dest, int header)
}
}


void emit_code_to_file_by_basename(int header, char * basename)
{
char * filename = malloc(strlen(basename) + 3);
strcpy(filename, basename);
strcat(filename, header ? ".h" : ".c");
FILE * f = fopen(filename, "w");
if (!f) {
perror("opening output file");
exit(EXIT_FAILURE);
}
emit_code(f, header);
fclose(f);
}


int main(int argc, char * argv[])
{
char * opt = argc > 1 ? argv[1] : NULL;
Expand All @@ -125,10 +161,17 @@ int main(int argc, char * argv[])
emit_code(stdout, 1);
else if (!strcmp(opt, "c") || !strcmp(opt, ".c") || !strcmp(opt, "-c"))
emit_code(stdout, 0);
else if (!strcmp(opt, "-b") && argc > 2) {
char * basename = argv[2];
emit_code_to_file_by_basename(0, basename);
emit_code_to_file_by_basename(1, basename);
}
else {
fprintf(stderr, "Usage: ffgen [-h|-c]\n");
fprintf(stderr, "Usage: ffgen [-h|-c|-b basename]\n");
fprintf(stderr, " -h for header file\n");
fprintf(stderr, " -c for C source file\n");
fprintf(stderr,
" -b basename, makes both as basename.c and basename.h\n");
return 1;
}
return 0;
Expand Down
Loading