aboutsummaryrefslogtreecommitdiff
path: root/src/utils/math.h
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-03-22 06:43:11 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2025-03-22 18:45:47 +0100
commitce3f1cc0ef9f46d70ab5387b1458e9098b40711d (patch)
tree4949745670b829f2211381bf14b8440dcc6ca7b1 /src/utils/math.h
parent0550a16c1cce868bbc3f3b5ad59f80e35cf2a6cd (diff)
downloadnissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.tar.gz
nissy-core-ce3f1cc0ef9f46d70ab5387b1458e9098b40711d.zip
Some safety with move arrays, small refactor appendchar
Diffstat (limited to 'src/utils/math.h')
-rw-r--r--src/utils/math.h65
1 files changed, 32 insertions, 33 deletions
diff --git a/src/utils/math.h b/src/utils/math.h
index 1ab51e6..21fc60b 100644
--- a/src/utils/math.h
+++ b/src/utils/math.h
@@ -4,12 +4,12 @@
4#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) 4#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
5 5
6STATIC int64_t factorial(int64_t); 6STATIC int64_t factorial(int64_t);
7STATIC bool isperm(uint8_t *, int64_t); 7STATIC bool isperm(size_t n, const uint8_t [n]);
8STATIC int64_t permtoindex(uint8_t *, int64_t); 8STATIC int64_t permtoindex(size_t n, const uint8_t [n]);
9STATIC void indextoperm(int64_t, int64_t, uint8_t *); 9STATIC void indextoperm(int64_t, size_t n, uint8_t [n]);
10STATIC int permsign(uint8_t *, int64_t); 10STATIC int permsign(size_t n, const uint8_t [n]);
11STATIC int64_t digitstosumzero(uint8_t *, uint8_t, uint8_t); 11STATIC int64_t digitstosumzero(size_t n, const uint8_t [n], uint8_t);
12STATIC void sumzerotodigits(int64_t, uint8_t, uint8_t, uint8_t *); 12STATIC void sumzerotodigits(int64_t, size_t n, uint8_t, uint8_t [n]);
13 13
14STATIC int64_t 14STATIC int64_t
15factorial(int64_t n) 15factorial(int64_t n)
@@ -32,13 +32,13 @@ factorial(int64_t n)
32} 32}
33 33
34STATIC bool 34STATIC bool
35isperm(uint8_t *a, int64_t n) 35isperm(size_t n, const uint8_t a[n])
36{ 36{
37 int64_t i; 37 size_t i;
38 bool aux[FACTORIAL_MAX+1]; 38 bool aux[FACTORIAL_MAX+1];
39 39
40 if (n > FACTORIAL_MAX) { 40 if (n > (size_t)FACTORIAL_MAX) {
41 LOG("Error: won't compute 'isperm()' for n=%" PRId64 " because" 41 LOG("Error: won't compute 'isperm()' for n=%zu because"
42 " it is larger than %" PRId64 "\n", n, FACTORIAL_MAX); 42 " it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
43 return false; 43 return false;
44 } 44 }
@@ -60,18 +60,18 @@ isperm(uint8_t *a, int64_t n)
60} 60}
61 61
62STATIC int64_t 62STATIC int64_t
63permtoindex(uint8_t *a, int64_t n) 63permtoindex(size_t n, const uint8_t a[n])
64{ 64{
65 int64_t i, j, c, ret; 65 size_t i, j;
66 int64_t c, ret;
66 67
67 if (n > FACTORIAL_MAX) { 68 if (n > (size_t)FACTORIAL_MAX) {
68 LOG("Error: won't compute 'permtoindex()' for n=%" PRId64 69 LOG("Error: won't compute 'permtoindex()' for n=%zu because "
69 " because it is larger than %" PRId64 "\n", 70 "it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
70 n, FACTORIAL_MAX);
71 return -1; 71 return -1;
72 } 72 }
73 73
74 if (!isperm(a, n)) 74 if (!isperm(n, a))
75 return -1; 75 return -1;
76 76
77 for (i = 0, ret = 0; i < n; i++) { 77 for (i = 0, ret = 0; i < n; i++) {
@@ -84,15 +84,15 @@ permtoindex(uint8_t *a, int64_t n)
84} 84}
85 85
86STATIC void 86STATIC void
87indextoperm(int64_t p, int64_t n, uint8_t *r) 87indextoperm(int64_t p, size_t n, uint8_t r[n])
88{ 88{
89 int64_t i, j, c; 89 int64_t c;
90 size_t i, j;
90 uint8_t a[FACTORIAL_MAX+1]; 91 uint8_t a[FACTORIAL_MAX+1];
91 92
92 if (n > FACTORIAL_MAX) { 93 if (n > FACTORIAL_MAX) {
93 LOG("Error: won't compute 'permtoindex()' for n=%" PRId64 94 LOG("Error: won't compute 'indextoperm()' for n=%zu because "
94 " because it is larger than %" PRId64 "\n", 95 "it is larger than %" PRId64 "\n", n, FACTORIAL_MAX);
95 n, FACTORIAL_MAX);
96 goto indextoperm_error; 96 goto indextoperm_error;
97 } 97 }
98 98
@@ -109,7 +109,7 @@ indextoperm(int64_t p, int64_t n, uint8_t *r)
109 p %= factorial(n-i-1); 109 p %= factorial(n-i-1);
110 } 110 }
111 111
112 if (!isperm(r, n)) 112 if (!isperm(n, r))
113 goto indextoperm_error; 113 goto indextoperm_error;
114 114
115 return; 115 return;
@@ -119,10 +119,9 @@ indextoperm_error:
119} 119}
120 120
121STATIC int 121STATIC int
122permsign(uint8_t *a, int64_t n) 122permsign(size_t n, const uint8_t a[n])
123{ 123{
124 int i, j; 124 size_t i, j, ret;
125 uint8_t ret;
126 125
127 for (i = 0, ret = 0; i < n; i++) 126 for (i = 0, ret = 0; i < n; i++)
128 for (j = i+1; j < n; j++) 127 for (j = i+1; j < n; j++)
@@ -132,13 +131,13 @@ permsign(uint8_t *a, int64_t n)
132} 131}
133 132
134STATIC int64_t 133STATIC int64_t
135digitstosumzero(uint8_t *a, uint8_t n, uint8_t b) 134digitstosumzero(size_t n, const uint8_t a[n], uint8_t b)
136{ 135{
137 int64_t ret, p; 136 int64_t ret, p;
138 uint8_t i, sum; 137 size_t i, sum;
139 138
140 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { 139 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) {
141 LOG("Won't compute 'sumzero' for n=%" PRIu8 "and b=%" PRIu8 140 LOG("Won't compute 'sumzero' for n=%zu and b=%" PRIu8
142 " (use n=8 b=3 or n=12 b=2)\n", n, b); 141 " (use n=8 b=3 or n=12 b=2)\n", n, b);
143 return -1; 142 return -1;
144 } 143 }
@@ -162,15 +161,15 @@ digitstosumzero(uint8_t *a, uint8_t n, uint8_t b)
162} 161}
163 162
164STATIC void 163STATIC void
165sumzerotodigits(int64_t d, uint8_t n, uint8_t b, uint8_t *a) 164sumzerotodigits(int64_t d, size_t n, uint8_t b, uint8_t a[n])
166{ 165{
167 uint8_t sum; 166 uint8_t sum;
168 int64_t i; 167 size_t i;
169 168
170 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) { 169 if (!((n == 8 && b == 3 ) || (n == 12 && b == 2))) {
171 LOG("Won't compute 'digits' for n=%" PRIu8 "and b=%" PRIu8 170 LOG("Won't compute 'digits' for n=%" PRIu8 "and b=%" PRIu8
172 " (use n=8 b=3 or n=12 b=2)\n"); 171 " (use n=8 b=3 or n=12 b=2)\n");
173 goto digitstosumzero_error; 172 goto sumzerotodigits_error;
174 } 173 }
175 174
176 for (i = 1, sum = 0; i < n; i++, d /= (int64_t)b) { 175 for (i = 1, sum = 0; i < n; i++, d /= (int64_t)b) {
@@ -181,6 +180,6 @@ sumzerotodigits(int64_t d, uint8_t n, uint8_t b, uint8_t *a)
181 180
182 return; 181 return;
183 182
184digitstosumzero_error: 183sumzerotodigits_error:
185 memset(a, UINT8_ERROR, n); 184 memset(a, UINT8_ERROR, n);
186} 185}

Generated with cgit - Back to sebastiano.tronto.net