From 56048d13b73ec6e6a9c59d62e82f41e69a3994bd Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Tue, 25 Mar 2025 18:35:47 +0100 Subject: More safety with pointers using VLA function parameters --- src/solvers/h48/coordinate.h | 21 +++++++---- src/solvers/h48/coordinate_macros.h | 7 ---- src/solvers/h48/coordinate_types_macros.h | 7 ++++ src/solvers/h48/gendata_cocsep.h | 39 ++++++++++++++------ src/solvers/h48/gendata_eoesep.h | 17 +++++++-- src/solvers/h48/gendata_h48.h | 61 ++++++++++--------------------- src/solvers/h48/gendata_types_macros.h | 2 +- src/solvers/h48/h48.h | 4 +- src/solvers/h48/map.h | 31 +++++++--------- src/solvers/h48/solve.h | 18 ++++----- 10 files changed, 109 insertions(+), 98 deletions(-) delete mode 100644 src/solvers/h48/coordinate_macros.h create mode 100644 src/solvers/h48/coordinate_types_macros.h (limited to 'src/solvers/h48') diff --git a/src/solvers/h48/coordinate.h b/src/solvers/h48/coordinate.h index 6caac2a..eb08fd8 100644 --- a/src/solvers/h48/coordinate.h +++ b/src/solvers/h48/coordinate.h @@ -1,12 +1,15 @@ -/* Macros defined in a separate file for easier testing */ -#include "coordinate_macros.h" - -STATIC_INLINE int64_t coord_h48(cube_t, const uint32_t *, uint8_t); +STATIC_INLINE int64_t coord_h48( + cube_t, const uint32_t [static COCSEP_TABLESIZE], uint8_t); STATIC_INLINE int64_t coord_h48_edges(cube_t, int64_t, uint8_t, uint8_t); -STATIC_INLINE cube_t invcoord_h48(int64_t, const cube_t *, uint8_t); +STATIC_INLINE cube_t invcoord_h48( + int64_t, const cube_t [static COCSEP_CLASSES], uint8_t); STATIC_INLINE int64_t -coord_h48(cube_t c, const uint32_t *cocsepdata, uint8_t h) +coord_h48( + cube_t c, + const uint32_t cocsepdata[static COCSEP_TABLESIZE], + uint8_t h +) { int64_t cocsep, coclass; uint32_t data; @@ -42,7 +45,11 @@ the given value, because it works up to symmetry. This means that the returned cube is a transformed cube of one that gives the correct value. */ STATIC_INLINE cube_t -invcoord_h48(int64_t i, const cube_t *crep, uint8_t h) +invcoord_h48( + int64_t i, + const cube_t crep[static COCSEP_CLASSES], + uint8_t h +) { cube_t ret; int64_t hh, coclass, ee, esep, eo; diff --git a/src/solvers/h48/coordinate_macros.h b/src/solvers/h48/coordinate_macros.h deleted file mode 100644 index 04462d6..0000000 --- a/src/solvers/h48/coordinate_macros.h +++ /dev/null @@ -1,7 +0,0 @@ -#define H48_ESIZE(h) ((COMB_12_4 * COMB_8_4) << (int64_t)(h)) - -#define COCLASS_MASK (UINT32_C(0xFFFF) << UINT32_C(16)) -#define COCLASS(x) (((x) & COCLASS_MASK) >> UINT32_C(16)) -#define ECLASS(x) COCLASS(x) -#define TTREP_MASK (UINT32_C(0xFF) << UINT32_C(8)) -#define TTREP(x) (((x) & TTREP_MASK) >> UINT32_C(8)) diff --git a/src/solvers/h48/coordinate_types_macros.h b/src/solvers/h48/coordinate_types_macros.h new file mode 100644 index 0000000..04462d6 --- /dev/null +++ b/src/solvers/h48/coordinate_types_macros.h @@ -0,0 +1,7 @@ +#define H48_ESIZE(h) ((COMB_12_4 * COMB_8_4) << (int64_t)(h)) + +#define COCLASS_MASK (UINT32_C(0xFFFF) << UINT32_C(16)) +#define COCLASS(x) (((x) & COCLASS_MASK) >> UINT32_C(16)) +#define ECLASS(x) COCLASS(x) +#define TTREP_MASK (UINT32_C(0xFF) << UINT32_C(8)) +#define TTREP(x) (((x) & TTREP_MASK) >> UINT32_C(8)) diff --git a/src/solvers/h48/gendata_cocsep.h b/src/solvers/h48/gendata_cocsep.h index bf67962..cda1608 100644 --- a/src/solvers/h48/gendata_cocsep.h +++ b/src/solvers/h48/gendata_cocsep.h @@ -1,11 +1,15 @@ -STATIC_INLINE bool gendata_cocsep_get_visited(const uint8_t *, int64_t); -STATIC_INLINE void gendata_cocsep_set_visited(uint8_t *, int64_t); - STATIC size_t gendata_cocsep(char *, uint64_t *, cube_t *); -STATIC uint32_t gendata_cocsep_dfs(cocsep_dfs_arg_t *); -STATIC void getdistribution_cocsep(const uint32_t *, uint64_t [static 21]); +STATIC uint32_t gendata_cocsep_dfs(cocsep_dfs_arg_t [static 1]); +STATIC void getdistribution_cocsep( + const uint32_t [static COCSEP_TABLESIZE], uint64_t [static 21]); + +STATIC_INLINE bool gendata_cocsep_get_visited( + const uint8_t [static COCSEP_VISITEDSIZE], int64_t); +STATIC_INLINE void gendata_cocsep_set_visited( + uint8_t [static COCSEP_VISITEDSIZE], int64_t); -STATIC_INLINE int8_t get_h48_cdata(cube_t, const uint32_t *, uint32_t *); +STATIC_INLINE int8_t get_h48_cdata( + cube_t, const uint32_t [static COCSEP_TABLESIZE], uint32_t *); STATIC size_t gendata_cocsep( @@ -78,7 +82,7 @@ gendata_cocsep_return_size: } STATIC uint32_t -gendata_cocsep_dfs(cocsep_dfs_arg_t *arg) +gendata_cocsep_dfs(cocsep_dfs_arg_t arg[static 1]) { uint8_t m; uint32_t cc, class, ttrep, depth, olddepth, tinv; @@ -133,7 +137,10 @@ gendata_cocsep_dfs(cocsep_dfs_arg_t *arg) } STATIC void -getdistribution_cocsep(const uint32_t *table, uint64_t distr[static 21]) +getdistribution_cocsep( + const uint32_t table[static COCSEP_TABLESIZE], + uint64_t distr[static 21] +) { size_t i; @@ -144,19 +151,29 @@ getdistribution_cocsep(const uint32_t *table, uint64_t distr[static 21]) } STATIC_INLINE bool -gendata_cocsep_get_visited(const uint8_t *a, int64_t i) +gendata_cocsep_get_visited( + const uint8_t a[static COCSEP_VISITEDSIZE], + int64_t i +) { return a[VISITED_IND(i)] & VISITED_MASK(i); } STATIC_INLINE void -gendata_cocsep_set_visited(uint8_t *a, int64_t i) +gendata_cocsep_set_visited( + uint8_t a[static COCSEP_VISITEDSIZE], + int64_t i +) { a[VISITED_IND(i)] |= VISITED_MASK(i); } STATIC_INLINE int8_t -get_h48_cdata(cube_t cube, const uint32_t *cocsepdata, uint32_t *cdata) +get_h48_cdata( + cube_t cube, + const uint32_t cocsepdata[static COCSEP_TABLESIZE], + uint32_t *cdata +) { int64_t coord; diff --git a/src/solvers/h48/gendata_eoesep.h b/src/solvers/h48/gendata_eoesep.h index d887209..4bf5a84 100644 --- a/src/solvers/h48/gendata_eoesep.h +++ b/src/solvers/h48/gendata_eoesep.h @@ -12,9 +12,11 @@ STATIC uint32_t gendata_eoesep_marksim(int64_t, uint8_t, uint8_t [static EOESEP_BUF], uint32_t [static ESEP_MAX]); STATIC bool gendata_eoesep_next(cube_t, uint8_t, uint8_t [static EOESEP_BUF], uint32_t [static ESEP_MAX]); -STATIC uint8_t get_eoesep_pval(const uint8_t *, int64_t); +STATIC uint8_t get_eoesep_pval( + const uint8_t [static DIV_ROUND_UP(EOESEP_TABLESIZE, 2)], int64_t); STATIC uint8_t get_eoesep_pval_cube(const void *, cube_t); -STATIC void set_eoesep_pval(uint8_t *, int64_t, uint8_t); +STATIC void set_eoesep_pval( + uint8_t [static DIV_ROUND_UP(EOESEP_TABLESIZE, 2)], int64_t, uint8_t); STATIC int64_t coord_eoesep_sym(cube_t c, const uint32_t esep_classes[static ESEP_MAX]) @@ -247,7 +249,10 @@ gendata_eoesep_next( } STATIC uint8_t -get_eoesep_pval(const uint8_t *table, int64_t i) +get_eoesep_pval( + const uint8_t table[static DIV_ROUND_UP(EOESEP_TABLESIZE, 2)], + int64_t i +) { return (table[EOESEP_INDEX(i)] & EOESEP_MASK(i)) >> EOESEP_SHIFT(i); } @@ -267,7 +272,11 @@ get_eoesep_pval_cube(const void *data, cube_t c) } STATIC void -set_eoesep_pval(uint8_t *table, int64_t i, uint8_t val) +set_eoesep_pval( + uint8_t table[static DIV_ROUND_UP(EOESEP_TABLESIZE, 2)], + int64_t i, + uint8_t val +) { table[EOESEP_INDEX(i)] = (table[EOESEP_INDEX(i)] & (~EOESEP_MASK(i))) | (val << EOESEP_SHIFT(i)); diff --git a/src/solvers/h48/gendata_h48.h b/src/solvers/h48/gendata_h48.h index 2d9a9f7..959264d 100644 --- a/src/solvers/h48/gendata_h48.h +++ b/src/solvers/h48/gendata_h48.h @@ -1,17 +1,17 @@ -STATIC uint64_t gendata_h48short(gendata_h48short_arg_t *); -STATIC int64_t gendata_h48(gendata_h48_arg_t *); -STATIC void gendata_h48h0k4(gendata_h48_arg_t *); -STATIC void gendata_h48k2(gendata_h48_arg_t *); -STATIC void gendata_h48k2_realcoord(gendata_h48_arg_t *); +STATIC uint64_t genddfggta_h48short(gendata_h48short_arg_t [static 1]); +STATIC int64_t gendata_h48(gendata_h48_arg_t [static 1]); +STATIC void gendata_h48h0k4(gendata_h48_arg_t [static 1]); +STATIC void gendata_h48k2(gendata_h48_arg_t [static 1]); STATIC void * gendata_h48h0k4_runthread(void *); STATIC void * gendata_h48k2_runthread(void *); -STATIC_INLINE void gendata_h48_mark_atomic(gendata_h48_mark_t *); -STATIC_INLINE void gendata_h48_mark(gendata_h48_mark_t *); -STATIC_INLINE bool gendata_h48k2_dfs_stop(cube_t, int8_t, h48k2_dfs_arg_t *); -STATIC void gendata_h48k2_dfs(h48k2_dfs_arg_t *arg); -STATIC tableinfo_t makeinfo_h48k2(gendata_h48_arg_t *); +STATIC_INLINE void gendata_h48_mark_atomic(gendata_h48_mark_t [static 1]); +STATIC_INLINE void gendata_h48_mark(gendata_h48_mark_t [static 1]); +STATIC_INLINE bool gendata_h48k2_dfs_stop( + cube_t, int8_t, h48k2_dfs_arg_t [static 1]); +STATIC void gendata_h48k2_dfs(h48k2_dfs_arg_t [static 1]); +STATIC tableinfo_t makeinfo_h48k2(gendata_h48_arg_t [static 1]); STATIC void getdistribution_h48(const uint8_t *, uint64_t [static INFO_DISTRIBUTION_LEN], uint8_t, uint8_t); @@ -28,7 +28,7 @@ STATIC_INLINE void set_h48_pval_atomic( size_t gendata_h48_derive(uint8_t, const void *, void *); STATIC uint64_t -gendata_h48short(gendata_h48short_arg_t *arg) +gendata_h48short(gendata_h48short_arg_t arg[static 1]) { uint8_t i, m; int64_t coord; @@ -62,7 +62,7 @@ gendata_h48short(gendata_h48short_arg_t *arg) } STATIC int64_t -gendata_h48(gendata_h48_arg_t *arg) +gendata_h48(gendata_h48_arg_t arg[static 1]) { uint64_t size, cocsepsize, h48size, fallbacksize, fallback2size, of; long long r; @@ -70,11 +70,6 @@ gendata_h48(gendata_h48_arg_t *arg) tableinfo_t cocsepinfo, h48info, fallbackinfo; gendata_h48_arg_t arg_h0k4; - if (arg == NULL) { - LOG("Error computing H48 data: arg is NULL.\n"); - return NISSY_ERROR_UNKNOWN; - } - cocsepsize = COCSEP_FULLSIZE; h48size = INFOSIZE + H48_TABLESIZE(arg->h, arg->k); fallbacksize = arg->k == 2 ? INFOSIZE + H48_TABLESIZE(0, 4) : 0; @@ -107,8 +102,6 @@ gendata_h48(gendata_h48_arg_t *arg) if (arg->h == 0 && arg->k == 4) { gendata_h48h0k4(arg); - } else if ((arg->h == 0 || arg->h == 11) && arg->k == 2) { - gendata_h48k2_realcoord(arg); } else if (arg->k == 2) { gendata_h48k2(arg); } else { @@ -189,7 +182,7 @@ gendata_h48(gendata_h48_arg_t *arg) } STATIC void -gendata_h48h0k4(gendata_h48_arg_t *arg) +gendata_h48h0k4(gendata_h48_arg_t arg[static 1]) { _Atomic uint8_t *table; uint8_t val; @@ -325,7 +318,7 @@ gendata_h48h0k4_runthread(void *arg) } STATIC void -gendata_h48k2(gendata_h48_arg_t *arg) +gendata_h48k2(gendata_h48_arg_t arg[static 1]) { static const uint8_t shortdepth = 8; static const uint64_t capacity = 10000019; @@ -483,7 +476,7 @@ gendata_h48k2_runthread(void *arg) } STATIC void -gendata_h48k2_dfs(h48k2_dfs_arg_t *arg) +gendata_h48k2_dfs(h48k2_dfs_arg_t arg[static 1]) { int8_t d; uint8_t m[4]; @@ -562,7 +555,7 @@ gendata_h48k2_dfs(h48k2_dfs_arg_t *arg) } STATIC_INLINE void -gendata_h48_mark_atomic(gendata_h48_mark_t *arg) +gendata_h48_mark_atomic(gendata_h48_mark_t arg[static 1]) { uint8_t oldval, newval; int64_t coord, mutex; @@ -582,7 +575,7 @@ gendata_h48_mark_atomic(gendata_h48_mark_t *arg) } STATIC_INLINE void -gendata_h48_mark(gendata_h48_mark_t *arg) +gendata_h48_mark(gendata_h48_mark_t arg[static 1]) { uint8_t oldval, newval; int64_t coord, mutex; @@ -599,7 +592,7 @@ gendata_h48_mark(gendata_h48_mark_t *arg) } STATIC_INLINE bool -gendata_h48k2_dfs_stop(cube_t cube, int8_t depth, h48k2_dfs_arg_t *arg) +gendata_h48k2_dfs_stop(cube_t cube, int8_t d, h48k2_dfs_arg_t arg[static 1]) { uint64_t val; int64_t coord, mutex; @@ -613,7 +606,7 @@ gendata_h48k2_dfs_stop(cube_t cube, int8_t depth, h48k2_dfs_arg_t *arg) pthread_mutex_lock(arg->table_mutex[mutex]); oldval = get_h48_pval(arg->table, coord, arg->k); pthread_mutex_unlock(arg->table_mutex[mutex]); - return oldval <= depth; + return oldval <= d; } else { /* With 0 < k < 11 we do not have a "real coordinate". The best we can do is checking if we backtracked to @@ -624,22 +617,8 @@ gendata_h48k2_dfs_stop(cube_t cube, int8_t depth, h48k2_dfs_arg_t *arg) } } -STATIC void -gendata_h48k2_realcoord(gendata_h48_arg_t *arg) -{ - /* TODO */ - gendata_h48k2(arg); -} - -STATIC void * -gendata_h48k2_realcoord_runthread(void *arg) -{ - /* TODO */ - return NULL; -} - STATIC tableinfo_t -makeinfo_h48k2(gendata_h48_arg_t *arg) +makeinfo_h48k2(gendata_h48_arg_t arg[static 1]) { tableinfo_t info; diff --git a/src/solvers/h48/gendata_types_macros.h b/src/solvers/h48/gendata_types_macros.h index ec6576a..11940d8 100644 --- a/src/solvers/h48/gendata_types_macros.h +++ b/src/solvers/h48/gendata_types_macros.h @@ -33,7 +33,7 @@ /* TODO: This loop over similar h48 coordinates can be improved by only transforming edges, but we need to compose transformations (i.e. conjugate -_t by _ttrep). +VAR_T by VAR_TTREP). */ #define FOREACH_H48SIM(ARG_CUBE, ARG_COCSEPDATA, ARG_SELFSIM, ARG_ACTION) \ int64_t VAR_COCSEP = coord_cocsep(ARG_CUBE); \ diff --git a/src/solvers/h48/h48.h b/src/solvers/h48/h48.h index d5a67ef..0cfa773 100644 --- a/src/solvers/h48/h48.h +++ b/src/solvers/h48/h48.h @@ -1,6 +1,8 @@ +#include "coordinate_types_macros.h" +#include "map_types_macros.h" +#include "gendata_types_macros.h" #include "coordinate.h" #include "map.h" -#include "gendata_types_macros.h" #include "gendata_cocsep.h" #include "gendata_eoesep.h" #include "gendata_h48.h" diff --git a/src/solvers/h48/map.h b/src/solvers/h48/map.h index e6903ce..7718794 100644 --- a/src/solvers/h48/map.h +++ b/src/solvers/h48/map.h @@ -1,16 +1,13 @@ -/* Type definitions and macros are in a separate file for easier testing */ -#include "map_types_macros.h" - -STATIC void h48map_create(h48map_t *, uint64_t, uint64_t); -STATIC void h48map_clear(h48map_t *); -STATIC void h48map_destroy(h48map_t *); -STATIC uint64_t h48map_lookup(h48map_t *, uint64_t); -STATIC void h48map_insertmin(h48map_t *, uint64_t, uint64_t); -STATIC uint64_t h48map_value(h48map_t *, uint64_t); -STATIC kvpair_t h48map_nextkvpair(h48map_t *, uint64_t *); +STATIC void h48map_create(h48map_t [static 1], uint64_t, uint64_t); +STATIC void h48map_clear(h48map_t [static 1]); +STATIC void h48map_destroy(h48map_t [static 1]); +STATIC uint64_t h48map_lookup(h48map_t [static 1], uint64_t); +STATIC void h48map_insertmin(h48map_t [static 1], uint64_t, uint64_t); +STATIC uint64_t h48map_value(h48map_t [static 1], uint64_t); +STATIC kvpair_t h48map_nextkvpair(h48map_t [static 1], uint64_t [static 1]); STATIC void -h48map_create(h48map_t *map, uint64_t capacity, uint64_t randomizer) +h48map_create(h48map_t map[static 1], uint64_t capacity, uint64_t randomizer) { map->capacity = capacity; map->randomizer = randomizer; @@ -20,20 +17,20 @@ h48map_create(h48map_t *map, uint64_t capacity, uint64_t randomizer) } STATIC void -h48map_clear(h48map_t *map) +h48map_clear(h48map_t map[static 1]) { memset(map->table, 0xFF, map->capacity * sizeof(uint64_t)); map->n = 0; } STATIC void -h48map_destroy(h48map_t *map) +h48map_destroy(h48map_t map[static 1]) { free(map->table); } STATIC_INLINE uint64_t -h48map_lookup(h48map_t *map, uint64_t x) +h48map_lookup(h48map_t map[static 1], uint64_t x) { uint64_t hash, i; @@ -47,7 +44,7 @@ h48map_lookup(h48map_t *map, uint64_t x) } STATIC_INLINE void -h48map_insertmin(h48map_t *map, uint64_t key, uint64_t val) +h48map_insertmin(h48map_t map[static 1], uint64_t key, uint64_t val) { uint64_t i, oldval, min; @@ -60,13 +57,13 @@ h48map_insertmin(h48map_t *map, uint64_t key, uint64_t val) } STATIC_INLINE uint64_t -h48map_value(h48map_t *map, uint64_t key) +h48map_value(h48map_t map[static 1], uint64_t key) { return map->table[h48map_lookup(map, key)] >> MAP_KEYSHIFT; } STATIC kvpair_t -h48map_nextkvpair(h48map_t *map, uint64_t *p) +h48map_nextkvpair(h48map_t map[static 1], uint64_t p[static 1]) { kvpair_t kv; uint64_t pair; diff --git a/src/solvers/h48/solve.h b/src/solvers/h48/solve.h index dbd15bb..dcec5a4 100644 --- a/src/solvers/h48/solve.h +++ b/src/solvers/h48/solve.h @@ -46,18 +46,18 @@ typedef struct { int8_t *shortest_sol; } dfsarg_solve_h48_maketasks_t; -STATIC_INLINE bool solve_h48_stop(dfsarg_solve_h48_t *); +STATIC_INLINE bool solve_h48_stop(dfsarg_solve_h48_t [static 1]); STATIC int64_t solve_h48_maketasks( - dfsarg_solve_h48_t *, dfsarg_solve_h48_maketasks_t *, - solve_h48_task_t [static STARTING_CUBES], int *); + dfsarg_solve_h48_t [static 1], dfsarg_solve_h48_maketasks_t [static 1], + solve_h48_task_t [static STARTING_CUBES], int [static 1]); STATIC void *solve_h48_runthread(void *); -STATIC int64_t solve_h48_dfs(dfsarg_solve_h48_t *); +STATIC int64_t solve_h48_dfs(dfsarg_solve_h48_t [static 1]); STATIC int64_t solve_h48(cube_t, int8_t, int8_t, uint64_t, int8_t, int8_t, uint64_t, const void *, size_t n, char [n], long long [static NISSY_SIZE_SOLVE_STATS]); STATIC_INLINE bool -solve_h48_stop(dfsarg_solve_h48_t *arg) +solve_h48_stop(dfsarg_solve_h48_t arg[static 1]) { uint32_t data, data_inv; int64_t coord; @@ -145,7 +145,7 @@ solve_h48_stop(dfsarg_solve_h48_t *arg) } STATIC int64_t -solve_h48_dfs(dfsarg_solve_h48_t *arg) +solve_h48_dfs(dfsarg_solve_h48_t arg[static 1]) { int64_t ret, n; uint8_t m, nm, lbn, lbi; @@ -261,10 +261,10 @@ solve_h48_runthread(void *arg) STATIC int64_t solve_h48_maketasks( - dfsarg_solve_h48_t *solve_arg, - dfsarg_solve_h48_maketasks_t *maketasks_arg, + dfsarg_solve_h48_t solve_arg[static 1], + dfsarg_solve_h48_maketasks_t maketasks_arg[static 1], solve_h48_task_t tasks[static STARTING_CUBES], - int *ntasks + int ntasks[static 1] ) { int r; -- cgit v1.3