diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-29 12:12:43 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-07-29 12:12:43 +0200 |
| commit | ea0387796a349c91032fbcb10f50c6ad8607b0f6 (patch) | |
| tree | aed484690d24c0c28c7695d4b5389f2e3c341b96 /src/utils/math.h | |
| parent | 52c21640508c3fc668107778ae027ff4428ebd89 (diff) | |
| download | nissy-core-ea0387796a349c91032fbcb10f50c6ad8607b0f6.tar.gz nissy-core-ea0387796a349c91032fbcb10f50c6ad8607b0f6.zip | |
All coordinates unsigned
Diffstat (limited to 'src/utils/math.h')
| -rw-r--r-- | src/utils/math.h | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/src/utils/math.h b/src/utils/math.h index ebbd63e..f2176b6 100644 --- a/src/utils/math.h +++ b/src/utils/math.h | |||
| @@ -2,12 +2,13 @@ | |||
| 2 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) | 2 | #define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 3 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) | 3 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) |
| 4 | #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) | 4 | #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
| 5 | #define POSITIVE_MOD(x, y) (((x) % (y) + (y)) % (y)) | ||
| 5 | 6 | ||
| 6 | STATIC int64_t permtoindex(size_t, const uint8_t *); | 7 | STATIC uint64_t permtoindex(size_t, const uint8_t *); |
| 7 | STATIC void indextoperm(int64_t, size_t, uint8_t *); | 8 | STATIC void indextoperm(uint64_t, size_t, uint8_t *); |
| 8 | STATIC int permsign(size_t, const uint8_t *); | 9 | STATIC int permsign(size_t, const uint8_t *); |
| 9 | STATIC int64_t digitstosumzero(size_t, const uint8_t *, uint8_t); | 10 | STATIC uint64_t digitstosumzero(size_t, const uint8_t *, uint8_t); |
| 10 | STATIC void sumzerotodigits(int64_t, size_t, uint8_t, uint8_t *); | 11 | STATIC void sumzerotodigits(uint64_t, size_t, uint8_t, uint8_t *); |
| 11 | STATIC double intpow(double, uint64_t); | 12 | STATIC double intpow(double, uint64_t); |
| 12 | 13 | ||
| 13 | /* This code is only used for assertions in debug mode */ | 14 | /* This code is only used for assertions in debug mode */ |
| @@ -36,14 +37,14 @@ isperm(size_t n, const uint8_t *a) | |||
| 36 | } | 37 | } |
| 37 | #endif | 38 | #endif |
| 38 | 39 | ||
| 39 | STATIC int64_t | 40 | STATIC uint64_t |
| 40 | permtoindex(size_t n, const uint8_t *a) | 41 | permtoindex(size_t n, const uint8_t *a) |
| 41 | { | 42 | { |
| 42 | size_t i, j; | 43 | size_t i, j; |
| 43 | int64_t c, ret; | 44 | uint64_t c, ret; |
| 44 | 45 | ||
| 45 | DBG_ASSERT(n <= FACTORIAL_MAX, "Error: cannot compute permtoindex() " | 46 | DBG_ASSERT(n <= FACTORIAL_MAX, "Error: cannot compute permtoindex() " |
| 46 | "for set of size %zu > %" PRId64 "\n", n, FACTORIAL_MAX); | 47 | "for set of size %zu > %" PRIu64 "\n", n, FACTORIAL_MAX); |
| 47 | DBG_ASSERT(isperm(n, a), "Error: cannot compute permtoindex() for " | 48 | DBG_ASSERT(isperm(n, a), "Error: cannot compute permtoindex() for " |
| 48 | "invalid permutation\n"); | 49 | "invalid permutation\n"); |
| 49 | 50 | ||
| @@ -57,15 +58,15 @@ permtoindex(size_t n, const uint8_t *a) | |||
| 57 | } | 58 | } |
| 58 | 59 | ||
| 59 | STATIC void | 60 | STATIC void |
| 60 | indextoperm(int64_t p, size_t n, uint8_t *r) | 61 | indextoperm(uint64_t p, size_t n, uint8_t *r) |
| 61 | { | 62 | { |
| 62 | int64_t c, k; | 63 | uint64_t c, k; |
| 63 | size_t i, j, used; | 64 | size_t i, j, used; |
| 64 | 65 | ||
| 65 | DBG_ASSERT(n <= FACTORIAL_MAX, "Error: cannot compute indextoperm() " | 66 | DBG_ASSERT(n <= FACTORIAL_MAX, "Error: cannot compute indextoperm() " |
| 66 | "for set of size %zu > %" PRId64 "\n", n, FACTORIAL_MAX); | 67 | "for set of size %zu > %" PRIu64 "\n", n, FACTORIAL_MAX); |
| 67 | DBG_ASSERT(p >= 0 && p < factorial[n], "Error: invalid permutation " | 68 | DBG_ASSERT(p < factorial[n], "Error: invalid permutation index %" |
| 68 | "index %" PRId64 " for set of size %zu\n", p, n); | 69 | PRIu64 " for set of size %zu\n", p, n); |
| 69 | 70 | ||
| 70 | for (i = 0, used = 0; i < n; i++) { | 71 | for (i = 0, used = 0; i < n; i++) { |
| 71 | k = p / factorial[n-i-1]; | 72 | k = p / factorial[n-i-1]; |
| @@ -94,28 +95,28 @@ permsign(size_t n, const uint8_t *a) | |||
| 94 | return ret % 2; | 95 | return ret % 2; |
| 95 | } | 96 | } |
| 96 | 97 | ||
| 97 | STATIC int64_t | 98 | STATIC uint64_t |
| 98 | digitstosumzero(size_t n, const uint8_t *a, uint8_t b) | 99 | digitstosumzero(size_t n, const uint8_t *a, uint8_t b) |
| 99 | { | 100 | { |
| 100 | int64_t ret, p; | 101 | uint64_t ret, p; |
| 101 | size_t i, sum; | 102 | size_t i, sum; |
| 102 | 103 | ||
| 103 | DBG_ASSERT((n == 8 && b == 3 ) || (n == 12 && b == 2), | 104 | DBG_ASSERT((n == 8 && b == 3 ) || (n == 12 && b == 2), |
| 104 | "Error: digitstosumzero() called with n=%zu and b=%" PRIu8 | 105 | "Error: digitstosumzero() called with n=%zu and b=%" PRIu8 |
| 105 | " (use n=8 b=3 or n=12 b=2)\n", n, b); | 106 | " (use n=8 b=3 or n=12 b=2)\n", n, b); |
| 106 | 107 | ||
| 107 | for (i = 1, ret = 0, p = 1, sum = 0; i < n; i++, p *= (int64_t)b) { | 108 | for (i = 1, ret = 0, p = 1, sum = 0; i < n; i++, p *= (uint64_t)b) { |
| 108 | DBG_ASSERT(a[i] < b, "Error: digit %" PRIu8 | 109 | DBG_ASSERT(a[i] < b, "Error: digit %" PRIu8 |
| 109 | " > %" PRIu8 "in digitstosumzero()\n", a[i], b); | 110 | " > %" PRIu8 "in digitstosumzero()\n", a[i], b); |
| 110 | sum += a[i]; | 111 | sum += a[i]; |
| 111 | ret += p * (int64_t)a[i]; | 112 | ret += p * (uint64_t)a[i]; |
| 112 | } | 113 | } |
| 113 | 114 | ||
| 114 | return ret; | 115 | return ret; |
| 115 | } | 116 | } |
| 116 | 117 | ||
| 117 | STATIC void | 118 | STATIC void |
| 118 | sumzerotodigits(int64_t d, size_t n, uint8_t b, uint8_t *a) | 119 | sumzerotodigits(uint64_t d, size_t n, uint8_t b, uint8_t *a) |
| 119 | { | 120 | { |
| 120 | uint8_t sum; | 121 | uint8_t sum; |
| 121 | size_t i; | 122 | size_t i; |
| @@ -124,8 +125,8 @@ sumzerotodigits(int64_t d, size_t n, uint8_t b, uint8_t *a) | |||
| 124 | "Error: sumzerotodigits() called with n=%zu and b=%" PRIu8 | 125 | "Error: sumzerotodigits() called with n=%zu and b=%" PRIu8 |
| 125 | " (use n=8 b=3 or n=12 b=2)\n", n, b); | 126 | " (use n=8 b=3 or n=12 b=2)\n", n, b); |
| 126 | 127 | ||
| 127 | for (i = 1, sum = 0; i < n; i++, d /= (int64_t)b) { | 128 | for (i = 1, sum = 0; i < n; i++, d /= (uint64_t)b) { |
| 128 | a[i] = (uint8_t)(d % (int64_t)b); | 129 | a[i] = (uint8_t)(d % (uint64_t)b); |
| 129 | sum += a[i]; | 130 | sum += a[i]; |
| 130 | } | 131 | } |
| 131 | a[0] = (b - (sum % b)) % b; | 132 | a[0] = (b - (sum % b)) % b; |
