Skip to content

Commit

Permalink
clang-format and further minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
stevelinton committed Dec 17, 2018
1 parent c044ee3 commit 0f24523
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 212 deletions.
132 changes: 71 additions & 61 deletions etc/ffgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <stdlib.h>
#include "gen/config.h"

/* These next lines control the size of internal finite field elements
in GAP. Changes here will propagate as needed */
#if (SIZEOF_VOID_P == 8)
#define MAX_FF (1 << 24)
#else
Expand Down Expand Up @@ -44,117 +46,124 @@ void make_ff()
ch[j] = i;
num_ff++;
}
if (d > 0 && d-1 > max_deg)
max_deg = d-1;
if (d > 0 && d - 1 > max_deg)
max_deg = d - 1;
}
}
}

unsigned int needed_bits(unsigned int x) {
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)
void emit_code(FILE * dest, int header)
{
unsigned i, j;
if (header) {
fprintf( dest,"#ifndef GAP_FFDATA_H\n");
fprintf( dest,"#define GAP_FFDATA_H\n");
fprintf( dest,"\n");
fprintf( dest,"enum {\n");
fprintf( dest," NUM_SHORT_FINITE_FIELDS = %d,\n", num_ff);
fprintf( dest," SIZE_LARGEST_INTERNAL_FF = %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 UInt4 SizeFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf( dest,"extern UInt1 DegrFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf( dest,"extern UInt4 CharFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf( dest,"\n");
fprintf(dest, "/* This file is generated by etc/ffgen.c during build"
" do not edit */\n");
fprintf(dest, "#ifndef GAP_FFDATA_H\n");
fprintf(dest, "#define GAP_FFDATA_H\n");
fprintf(dest, "\n");
fprintf(dest, "enum {\n");
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 UInt4 SizeFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf(dest, "extern UInt1 DegrFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf(dest, "extern UInt4 CharFF[NUM_SHORT_FINITE_FIELDS+1];\n");
fprintf(dest, "\n");
if (num_ff < 65536)
fprintf( dest,"typedef UInt2 FF;\n");
fprintf(dest, "typedef UInt2 FF;\n");
else
fprintf( dest,"typedef UInt4 FF;\n");
fprintf(dest, "typedef UInt4 FF;\n");
if (MAX_FF <= 65536)
fprintf( dest,"typedef UInt2 FFV;\n");
fprintf(dest, "typedef UInt2 FFV;\n");
else
fprintf( dest,"typedef UInt4 FFV;\n");
fprintf( dest,"\n");
fprintf( dest,"#endif // GAP_FFDATA_H\n");
fprintf(dest, "typedef UInt4 FFV;\n");
fprintf(dest, "\n");
fprintf(dest, "#endif // GAP_FFDATA_H\n");
}
else {
fprintf( dest,"#include \"system.h\"\n");
fprintf( dest,"#include \"ffdata.h\"\n");
fprintf( dest,"\n");
fprintf( dest,"/* Entries are ordered by value of p^d; can use binary search\n");
fprintf( dest," * to find them. Indices start at 1.\n");
fprintf( dest," */\n");
fprintf( dest,"\n");
fprintf( dest,"UInt1 DegrFF[NUM_SHORT_FINITE_FIELDS+1] = {\n");
fprintf( dest," %3d,", 0);
fprintf(dest, "/* This file is generated by etc/ffgen.c during build"
" do not edit */\n");
fprintf(dest, "#include \"system.h\"\n");
fprintf(dest, "#include \"ffdata.h\"\n");
fprintf(dest, "\n");
fprintf(dest, "/* Entries are ordered by value of p^d; can use "
"binary search\n");
fprintf(dest, " * to find them. Indices start at 1.\n");
fprintf(dest, " */\n");
fprintf(dest, "\n");
fprintf(dest, "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]) {
fprintf( dest,"%3d,", deg[i]);
fprintf(dest, "%3d,", deg[i]);
j++;
j %= 16;
if (!j)
fprintf( dest,"\n ");
fprintf(dest, "\n ");
}
}
if (j)
fprintf( dest,"\n");
fprintf( dest,"};\n");
fprintf( dest,"\n");
fprintf( dest,"UInt4 CharFF[NUM_SHORT_FINITE_FIELDS+1] = {\n");
fprintf( dest," %9d,", 0);
fprintf(dest, "\n");
fprintf(dest, "};\n");
fprintf(dest, "\n");
fprintf(dest, "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,"%9d,", ch[i]);
fprintf(dest, "%9d,", ch[i]);
j++;
j %= 6;
if (!j)
fprintf( dest,"\n ");
fprintf(dest, "\n ");
}
}
if (j)
fprintf( dest,"\n");
fprintf( dest,"};\n");
fprintf( dest,"\n");
fprintf( dest,"UInt4 SizeFF[NUM_SHORT_FINITE_FIELDS+1] = {\n");
fprintf( dest," %9d,", 0);
fprintf(dest, "\n");
fprintf(dest, "};\n");
fprintf(dest, "\n");
fprintf(dest, "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,"%9d,", i);
fprintf(dest, "%9d,", i);
j++;
j %= 6;
if (!j)
fprintf( dest,"\n ");
fprintf(dest, "\n ");
}
}
if (j)
fprintf( dest,"\n");
fprintf( dest,"};\n");
fprintf(dest, "\n");
fprintf(dest, "};\n");
}
}


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");
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[])
{
Expand All @@ -168,15 +177,16 @@ int main(int argc, char * argv[])
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];
char * basename = argv[2];
emit_code_to_file_by_basename(0, basename);
emit_code_to_file_by_basename(1, basename);
}
else {
else {
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");
fprintf(stderr,
" -b basename, makes both as basename.c and basename.h\n");
return 1;
}
return 0;
Expand Down
3 changes: 1 addition & 2 deletions lib/ffe.g
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
##
#V MAXSIZE_GF_INTERNAL . . . . . . . . . . . . maximal size of internal ffes
##
## Set by the kernel, we just need to tidy up
## Now set by the kernel.

MakeReadOnlyGVar("MAXSIZE_GF_INTERNAL");


#############################################################################
Expand Down
Loading

0 comments on commit 0f24523

Please sign in to comment.