commit 1e5c862d8fa7b76b69b8125d454d8f8d6a9cde25 parent 7f24b27652ee6fa84e38f181cea57f319af0c204 Author: Sebastiano Tronto <sebastiano@tronto.net> Date: Mon, 28 Jul 2025 14:17:42 +0200 Turns checks into assertions in utils/math.h Diffstat:
29 files changed, 21 insertions(+), 195 deletions(-)
diff --git a/src/arch/arch.h b/src/arch/arch.h @@ -12,7 +12,6 @@ typedef __m256i cube_t; #elif defined(NEON) -#include <stdlib.h> #include <arm_neon.h> typedef struct { @@ -28,8 +27,6 @@ typedef struct { #else -#include <stdlib.h> - typedef struct { uint8_t corner[8]; uint8_t edge[12]; diff --git a/src/nissy.c b/src/nissy.c @@ -1,3 +1,4 @@ +#include <stdlib.h> #include <inttypes.h> #include <limits.h> #include <pthread.h> diff --git 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 @@ -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 @@ -1 +0,0 @@ --1 diff --git a/test/010_math_permtoindex/01_toobig.in b/test/010_math_permtoindex/01_toobig.in @@ -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 @@ -1 +0,0 @@ --1 diff --git a/test/010_math_permtoindex/02_offrange.in b/test/010_math_permtoindex/02_offrange.in @@ -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 @@ -1 +0,0 @@ --1 diff --git a/test/011_math_indextoperm/00_noperm.in b/test/011_math_indextoperm/00_noperm.in @@ -1,2 +0,0 @@ -3 --1 diff --git a/test/011_math_indextoperm/00_noperm.out b/test/011_math_indextoperm/00_noperm.out @@ -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 @@ -1,2 +0,0 @@ -15 -0 diff --git a/test/011_math_indextoperm/01_toobig.out b/test/011_math_indextoperm/01_toobig.out @@ -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 @@ -1,2 +0,0 @@ -3 -10 diff --git a/test/011_math_indextoperm/02_offrange.out b/test/011_math_indextoperm/02_offrange.out @@ -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 @@ -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 @@ -1 +0,0 @@ --1 diff --git a/test/013_math_digitstosumzero/01_bad_b12.in b/test/013_math_digitstosumzero/01_bad_b12.in @@ -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 @@ -1 +0,0 @@ --1 diff --git a/test/013_math_digitstosumzero/02_bad_b8.in b/test/013_math_digitstosumzero/02_bad_b8.in @@ -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 @@ -1 +0,0 @@ --1 diff --git a/test/013_math_digitstosumzero/09_sumnonzero.in b/test/013_math_digitstosumzero/09_sumnonzero.in @@ -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 @@ -1 +0,0 @@ --1 diff --git a/test/014_math_sumzerotodigits/00_bad_n.in b/test/014_math_sumzerotodigits/00_bad_n.in @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -1,8 +0,0 @@ -255 -255 -255 -255 -255 -255 -255 -255