From 1e5c862d8fa7b76b69b8125d454d8f8d6a9cde25 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 28 Jul 2025 14:17:42 +0200 Subject: Turns checks into assertions in utils/math.h --- src/arch/arch.h | 3 - src/nissy.c | 1 + src/utils/math.h | 73 +++++++------------------ test/010_math_permtoindex/00_noperm.in | 4 -- test/010_math_permtoindex/00_noperm.out | 1 - test/010_math_permtoindex/01_toobig.in | 16 ------ test/010_math_permtoindex/01_toobig.out | 1 - test/010_math_permtoindex/02_offrange.in | 4 -- test/010_math_permtoindex/02_offrange.out | 1 - test/011_math_indextoperm/00_noperm.in | 2 - test/011_math_indextoperm/00_noperm.out | 3 - test/011_math_indextoperm/01_toobig.in | 2 - test/011_math_indextoperm/01_toobig.out | 15 ----- test/011_math_indextoperm/02_offrange.in | 2 - test/011_math_indextoperm/02_offrange.out | 3 - test/013_math_digitstosumzero/00_bad_n.in | 5 -- test/013_math_digitstosumzero/00_bad_n.out | 1 - test/013_math_digitstosumzero/01_bad_b12.in | 14 ----- test/013_math_digitstosumzero/01_bad_b12.out | 1 - test/013_math_digitstosumzero/02_bad_b8.in | 10 ---- test/013_math_digitstosumzero/02_bad_b8.out | 1 - test/013_math_digitstosumzero/09_sumnonzero.in | 10 ---- test/013_math_digitstosumzero/09_sumnonzero.out | 1 - test/014_math_sumzerotodigits/00_bad_n.in | 3 - test/014_math_sumzerotodigits/00_bad_n.out | 13 ----- test/014_math_sumzerotodigits/01_bad_b12.in | 3 - test/014_math_sumzerotodigits/01_bad_b12.out | 12 ---- test/014_math_sumzerotodigits/02_bad_b8.in | 3 - test/014_math_sumzerotodigits/02_bad_b8.out | 8 --- 29 files changed, 21 insertions(+), 195 deletions(-) delete mode 100644 test/010_math_permtoindex/00_noperm.in delete mode 100644 test/010_math_permtoindex/00_noperm.out delete mode 100644 test/010_math_permtoindex/01_toobig.in delete mode 100644 test/010_math_permtoindex/01_toobig.out delete mode 100644 test/010_math_permtoindex/02_offrange.in delete mode 100644 test/010_math_permtoindex/02_offrange.out delete mode 100644 test/011_math_indextoperm/00_noperm.in delete mode 100644 test/011_math_indextoperm/00_noperm.out delete mode 100644 test/011_math_indextoperm/01_toobig.in delete mode 100644 test/011_math_indextoperm/01_toobig.out delete mode 100644 test/011_math_indextoperm/02_offrange.in delete mode 100644 test/011_math_indextoperm/02_offrange.out delete mode 100644 test/013_math_digitstosumzero/00_bad_n.in delete mode 100644 test/013_math_digitstosumzero/00_bad_n.out delete mode 100644 test/013_math_digitstosumzero/01_bad_b12.in delete mode 100644 test/013_math_digitstosumzero/01_bad_b12.out delete mode 100644 test/013_math_digitstosumzero/02_bad_b8.in delete mode 100644 test/013_math_digitstosumzero/02_bad_b8.out delete mode 100644 test/013_math_digitstosumzero/09_sumnonzero.in delete mode 100644 test/013_math_digitstosumzero/09_sumnonzero.out delete mode 100644 test/014_math_sumzerotodigits/00_bad_n.in delete mode 100644 test/014_math_sumzerotodigits/00_bad_n.out delete mode 100644 test/014_math_sumzerotodigits/01_bad_b12.in delete mode 100644 test/014_math_sumzerotodigits/01_bad_b12.out delete mode 100644 test/014_math_sumzerotodigits/02_bad_b8.in delete mode 100644 test/014_math_sumzerotodigits/02_bad_b8.out diff --git a/src/arch/arch.h b/src/arch/arch.h index 2c418a6..d0ef20c 100644 --- a/src/arch/arch.h +++ b/src/arch/arch.h @@ -12,7 +12,6 @@ typedef __m256i cube_t; #elif defined(NEON) -#include #include typedef struct { @@ -28,8 +27,6 @@ typedef struct { #else -#include - typedef struct { uint8_t corner[8]; uint8_t edge[12]; diff --git a/src/nissy.c b/src/nissy.c index 3190e55..93a4841 100644 --- a/src/nissy.c +++ b/src/nissy.c @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/src/utils/math.h b/src/utils/math.h index 4b1da12..95ec0aa 100644 --- a/src/utils/math.h +++ b/src/utils/math.h @@ -3,7 +3,6 @@ #define MAX(x, y) ((x) > (y) ? (x) : (y)) #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) -STATIC bool isperm(size_t, const uint8_t *); STATIC int64_t permtoindex(size_t, const uint8_t *); STATIC void indextoperm(int64_t, size_t, uint8_t *); STATIC int permsign(size_t, const uint8_t *); @@ -11,18 +10,15 @@ STATIC int64_t digitstosumzero(size_t, const uint8_t *, uint8_t); STATIC void sumzerotodigits(int64_t, size_t, uint8_t, uint8_t *); STATIC double intpow(double, uint64_t); +/* This code is only used for assertions in debug mode */ +#ifdef DEBUG +STATIC bool isperm(size_t, const uint8_t *); STATIC bool isperm(size_t n, const uint8_t *a) { size_t i; bool aux[FACTORIAL_MAX+1]; - if (n > (size_t)FACTORIAL_MAX) { - LOG("Error: won't compute 'isperm()' for n=%zu because" - " it is larger than %" PRId64 "\n", n, FACTORIAL_MAX); - return false; - } - memset(aux, false, n); for (i = 0; i < n; i++) { @@ -38,6 +34,7 @@ isperm(size_t n, const uint8_t *a) return true; } +#endif STATIC int64_t permtoindex(size_t n, const uint8_t *a) @@ -45,14 +42,10 @@ permtoindex(size_t n, const uint8_t *a) size_t i, j; int64_t c, ret; - if (n > (size_t)FACTORIAL_MAX) { - LOG("Error: won't compute 'permtoindex()' for n=%zu because " - "it is larger than %" PRId64 "\n", n, FACTORIAL_MAX); - return -1; - } - - if (!isperm(n, a)) - return -1; + DBG_ASSERT(n <= FACTORIAL_MAX, "Error: cannot compute permtoindex() " + "for set of size %zu > %" PRId64 "\n", n, FACTORIAL_MAX); + DBG_ASSERT(isperm(n, a), "Error: cannot compute permtoindex() for " + "invalid permutation\n"); for (i = 0, ret = 0; i < n; i++) { for (j = i+1, c = 0; j < n; j++) @@ -70,17 +63,12 @@ indextoperm(int64_t p, size_t n, uint8_t *r) size_t i, j; uint8_t a[FACTORIAL_MAX+1]; - if (n > FACTORIAL_MAX) { - LOG("Error: won't compute 'indextoperm()' for n=%zu because " - "it is larger than %" PRId64 "\n", n, FACTORIAL_MAX); - goto indextoperm_error; - } + DBG_ASSERT(n <= FACTORIAL_MAX, "Error: cannot compute indextoperm() " + "for set of size %zu > %" PRId64 "\n", n, FACTORIAL_MAX); + DBG_ASSERT(p >= 0 && p < factorial[n], "Error: invalid permutation " + "index %" PRId64 " for set of size %zu\n", p, n); memset(a, 0, n); - - if (p < 0 || p >= factorial[n]) - goto indextoperm_error; - for (i = 0; i < n; i++) { for (j = 0, c = 0; c <= p / factorial[n-i-1]; j++) c += a[j] ? 0 : 1; @@ -89,13 +77,7 @@ indextoperm(int64_t p, size_t n, uint8_t *r) p %= factorial[n-i-1]; } - if (!isperm(n, r)) - goto indextoperm_error; - return; - -indextoperm_error: - memset(r, UINT8_ERROR, n); } STATIC int @@ -116,27 +98,17 @@ digitstosumzero(size_t n, const uint8_t *a, uint8_t b) int64_t ret, p; size_t i, sum; - if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { - LOG("Error: won't compute 'sumzero' for n=%zu and b=%" PRIu8 - " (use n=8 b=3 or n=12 b=2)\n", n, b); - return -1; - } + DBG_ASSERT((n == 8 && b == 3 ) || (n == 12 && b == 2), + "Error: digitstosumzero() called with n=%zu and b=%" PRIu8 + " (use n=8 b=3 or n=12 b=2)\n", n, b); for (i = 1, ret = 0, p = 1, sum = 0; i < n; i++, p *= (int64_t)b) { - if (a[i] >= b) { - LOG("Error: digit %" PRIu8 " larger than maximum" - " (b=%" PRIu8 "\n", a[i], b); - return -1; - } + DBG_ASSERT(a[i] < b, "Error: digit %" PRIu8 + " > %" PRIu8 "in digitstosumzero()\n", a[i], b); sum += a[i]; ret += p * (int64_t)a[i]; } - if ((sum + a[0]) % b != 0) { - LOG("Error: digits do not have sum zero modulo b\n"); - return -1; - } - return ret; } @@ -146,11 +118,9 @@ sumzerotodigits(int64_t d, size_t n, uint8_t b, uint8_t *a) uint8_t sum; size_t i; - if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { - LOG("Error: won't compute 'digits' for n=%zu and b=%" PRIu8 - " (use n=8 b=3 or n=12 b=2)\n", n, b); - goto sumzerotodigits_error; - } + DBG_ASSERT((n == 8 && b == 3 ) || (n == 12 && b == 2), + "Error: sumzerotodigits() called with n=%zu and b=%" PRIu8 + " (use n=8 b=3 or n=12 b=2)\n", n, b); for (i = 1, sum = 0; i < n; i++, d /= (int64_t)b) { a[i] = (uint8_t)(d % (int64_t)b); @@ -159,9 +129,6 @@ sumzerotodigits(int64_t d, size_t n, uint8_t b, uint8_t *a) a[0] = (b - (sum % b)) % b; return; - -sumzerotodigits_error: - memset(a, UINT8_ERROR, n); } STATIC double diff --git a/test/010_math_permtoindex/00_noperm.in b/test/010_math_permtoindex/00_noperm.in deleted file mode 100644 index f838f21..0000000 --- a/test/010_math_permtoindex/00_noperm.in +++ /dev/null @@ -1,4 +0,0 @@ -3 -0 -1 -1 diff --git a/test/010_math_permtoindex/00_noperm.out b/test/010_math_permtoindex/00_noperm.out deleted file mode 100644 index 3a2e3f4..0000000 --- a/test/010_math_permtoindex/00_noperm.out +++ /dev/null @@ -1 +0,0 @@ --1 diff --git a/test/010_math_permtoindex/01_toobig.in b/test/010_math_permtoindex/01_toobig.in deleted file mode 100644 index a0fb28c..0000000 --- a/test/010_math_permtoindex/01_toobig.in +++ /dev/null @@ -1,16 +0,0 @@ -15 -0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 diff --git a/test/010_math_permtoindex/01_toobig.out b/test/010_math_permtoindex/01_toobig.out deleted file mode 100644 index 3a2e3f4..0000000 --- a/test/010_math_permtoindex/01_toobig.out +++ /dev/null @@ -1 +0,0 @@ --1 diff --git a/test/010_math_permtoindex/02_offrange.in b/test/010_math_permtoindex/02_offrange.in deleted file mode 100644 index 66ec716..0000000 --- a/test/010_math_permtoindex/02_offrange.in +++ /dev/null @@ -1,4 +0,0 @@ -3 -0 -4 -1 diff --git a/test/010_math_permtoindex/02_offrange.out b/test/010_math_permtoindex/02_offrange.out deleted file mode 100644 index 3a2e3f4..0000000 --- a/test/010_math_permtoindex/02_offrange.out +++ /dev/null @@ -1 +0,0 @@ --1 diff --git a/test/011_math_indextoperm/00_noperm.in b/test/011_math_indextoperm/00_noperm.in deleted file mode 100644 index 81d7aba..0000000 --- a/test/011_math_indextoperm/00_noperm.in +++ /dev/null @@ -1,2 +0,0 @@ -3 --1 diff --git a/test/011_math_indextoperm/00_noperm.out b/test/011_math_indextoperm/00_noperm.out deleted file mode 100644 index c656952..0000000 --- a/test/011_math_indextoperm/00_noperm.out +++ /dev/null @@ -1,3 +0,0 @@ -255 -255 -255 diff --git a/test/011_math_indextoperm/01_toobig.in b/test/011_math_indextoperm/01_toobig.in deleted file mode 100644 index 227eae2..0000000 --- a/test/011_math_indextoperm/01_toobig.in +++ /dev/null @@ -1,2 +0,0 @@ -15 -0 diff --git a/test/011_math_indextoperm/01_toobig.out b/test/011_math_indextoperm/01_toobig.out deleted file mode 100644 index a4cc325..0000000 --- a/test/011_math_indextoperm/01_toobig.out +++ /dev/null @@ -1,15 +0,0 @@ -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 diff --git a/test/011_math_indextoperm/02_offrange.in b/test/011_math_indextoperm/02_offrange.in deleted file mode 100644 index 47b5fdd..0000000 --- a/test/011_math_indextoperm/02_offrange.in +++ /dev/null @@ -1,2 +0,0 @@ -3 -10 diff --git a/test/011_math_indextoperm/02_offrange.out b/test/011_math_indextoperm/02_offrange.out deleted file mode 100644 index c656952..0000000 --- a/test/011_math_indextoperm/02_offrange.out +++ /dev/null @@ -1,3 +0,0 @@ -255 -255 -255 diff --git a/test/013_math_digitstosumzero/00_bad_n.in b/test/013_math_digitstosumzero/00_bad_n.in deleted file mode 100644 index edcdc1b..0000000 --- a/test/013_math_digitstosumzero/00_bad_n.in +++ /dev/null @@ -1,5 +0,0 @@ -3 -4 -0 -1 -0 diff --git a/test/013_math_digitstosumzero/00_bad_n.out b/test/013_math_digitstosumzero/00_bad_n.out deleted file mode 100644 index 3a2e3f4..0000000 --- a/test/013_math_digitstosumzero/00_bad_n.out +++ /dev/null @@ -1 +0,0 @@ --1 diff --git a/test/013_math_digitstosumzero/01_bad_b12.in b/test/013_math_digitstosumzero/01_bad_b12.in deleted file mode 100644 index 26bb819..0000000 --- a/test/013_math_digitstosumzero/01_bad_b12.in +++ /dev/null @@ -1,14 +0,0 @@ -12 -3 -0 -1 -2 -0 -1 -2 -2 -1 -0 -0 -0 -1 diff --git a/test/013_math_digitstosumzero/01_bad_b12.out b/test/013_math_digitstosumzero/01_bad_b12.out deleted file mode 100644 index 3a2e3f4..0000000 --- a/test/013_math_digitstosumzero/01_bad_b12.out +++ /dev/null @@ -1 +0,0 @@ --1 diff --git a/test/013_math_digitstosumzero/02_bad_b8.in b/test/013_math_digitstosumzero/02_bad_b8.in deleted file mode 100644 index 146d291..0000000 --- a/test/013_math_digitstosumzero/02_bad_b8.in +++ /dev/null @@ -1,10 +0,0 @@ -8 -2 -0 -0 -0 -0 -0 -0 -0 -1 diff --git a/test/013_math_digitstosumzero/02_bad_b8.out b/test/013_math_digitstosumzero/02_bad_b8.out deleted file mode 100644 index 3a2e3f4..0000000 --- a/test/013_math_digitstosumzero/02_bad_b8.out +++ /dev/null @@ -1 +0,0 @@ --1 diff --git a/test/013_math_digitstosumzero/09_sumnonzero.in b/test/013_math_digitstosumzero/09_sumnonzero.in deleted file mode 100644 index 9466f9a..0000000 --- a/test/013_math_digitstosumzero/09_sumnonzero.in +++ /dev/null @@ -1,10 +0,0 @@ -8 -3 -1 -2 -1 -1 -1 -1 -2 -2 diff --git a/test/013_math_digitstosumzero/09_sumnonzero.out b/test/013_math_digitstosumzero/09_sumnonzero.out deleted file mode 100644 index 3a2e3f4..0000000 --- a/test/013_math_digitstosumzero/09_sumnonzero.out +++ /dev/null @@ -1 +0,0 @@ --1 diff --git a/test/014_math_sumzerotodigits/00_bad_n.in b/test/014_math_sumzerotodigits/00_bad_n.in deleted file mode 100644 index 5e34db5..0000000 --- a/test/014_math_sumzerotodigits/00_bad_n.in +++ /dev/null @@ -1,3 +0,0 @@ -13 -4 -2 diff --git a/test/014_math_sumzerotodigits/00_bad_n.out b/test/014_math_sumzerotodigits/00_bad_n.out deleted file mode 100644 index f9c0ae0..0000000 --- a/test/014_math_sumzerotodigits/00_bad_n.out +++ /dev/null @@ -1,13 +0,0 @@ -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 diff --git a/test/014_math_sumzerotodigits/01_bad_b12.in b/test/014_math_sumzerotodigits/01_bad_b12.in deleted file mode 100644 index 8a6d31c..0000000 --- a/test/014_math_sumzerotodigits/01_bad_b12.in +++ /dev/null @@ -1,3 +0,0 @@ -12 -3 -2 diff --git a/test/014_math_sumzerotodigits/01_bad_b12.out b/test/014_math_sumzerotodigits/01_bad_b12.out deleted file mode 100644 index 32b57e3..0000000 --- a/test/014_math_sumzerotodigits/01_bad_b12.out +++ /dev/null @@ -1,12 +0,0 @@ -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 -255 diff --git a/test/014_math_sumzerotodigits/02_bad_b8.in b/test/014_math_sumzerotodigits/02_bad_b8.in deleted file mode 100644 index b70bee9..0000000 --- a/test/014_math_sumzerotodigits/02_bad_b8.in +++ /dev/null @@ -1,3 +0,0 @@ -8 -7 -0 diff --git a/test/014_math_sumzerotodigits/02_bad_b8.out b/test/014_math_sumzerotodigits/02_bad_b8.out deleted file mode 100644 index 22d5f76..0000000 --- a/test/014_math_sumzerotodigits/02_bad_b8.out +++ /dev/null @@ -1,8 +0,0 @@ -255 -255 -255 -255 -255 -255 -255 -255 -- cgit v1.3