blob: b33b31897ff443ad0d12f80cc662cf02a8efb1f0 [file] [log] [blame]
Gavin Howard3eb626f2018-02-14 13:54:35 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -07003 *
Gavin Howard7345cb92019-04-08 14:13:43 -06004 * Copyright (c) 2018-2019 Gavin D. Howard and contributors.
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
Gavin Howard7345cb92019-04-08 14:13:43 -06006 * All rights reserved.
Gavin Howard3eb626f2018-02-14 13:54:35 -07007 *
Gavin Howard7345cb92019-04-08 14:13:43 -06008 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
Gavin Howard3eb626f2018-02-14 13:54:35 -070029 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070030 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070031 *
32 * Code for the number type.
33 *
34 */
35
36#include <assert.h>
Gavin Howardc7d655b2018-12-28 19:45:13 -070037#include <ctype.h>
Gavin Howard411f7322018-09-26 17:21:19 -060038#include <stdbool.h>
39#include <stdlib.h>
40#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070041
Gavin Howard305249a2018-10-15 20:24:47 -060042#include <limits.h>
43
Gavin Howard29493062018-03-20 19:57:37 -060044#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070045#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060046#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070047
Gavin Howard55c8f732019-04-24 14:05:27 -060048#if BC_DEBUG_CODE
49static void bc_num_printDecimal(const BcNum *restrict a);
50
Gavin Howard5b40e402019-04-27 07:26:35 -060051static void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline)
52{
Gavin Howard55c8f732019-04-24 14:05:27 -060053 printf("%s: ", name);
54 bc_num_printDecimal(n);
55 printf("\n");
Gavin Howard5b40e402019-04-27 07:26:35 -060056 if (emptyline) printf("\n");
Gavin Howard55c8f732019-04-24 14:05:27 -060057 vm->nchars = 0;
58}
Stefan Eßer4b111c22019-04-24 17:23:08 -060059
Gavin Howard5b40e402019-04-27 07:26:35 -060060static void bc_num_printDigs(const BcNum *n, const char *name, bool emptyline) {
61
62 size_t i;
63
Gavin Howard37118f42019-04-27 07:28:24 -060064 printf("%s len: %zu, rdx: %zu, scale: %zu\n",
65 name, n->len, n->rdx, n->scale);
Gavin Howard5b40e402019-04-27 07:26:35 -060066
67 for (i = n->len - 1; i < n->len; --i)
68 printf(" %0*d", BC_BASE_POWER, n->num[i]);
69
70 printf("\n");
71 if (emptyline) printf("\n");
72 vm->nchars = 0;
73}
74
75static void bc_num_dump(const char *varname, const BcNum *n) {
Stefan Eßer4b111c22019-04-24 17:23:08 -060076
Stefan Eßerfc689bb2019-04-29 13:40:18 -060077 unsigned long i, scale = n->scale;
Stefan Eßer4b111c22019-04-24 17:23:08 -060078
Stefan Eßerdaaaaa72019-04-27 18:04:02 -060079 fprintf(stderr, "\n%s = %s", varname, n->len ? (n->neg ? "-" : "+") : "0 ");
Stefan Eßer4b111c22019-04-24 17:23:08 -060080
Gavin Howardc4783ac2019-04-26 10:40:02 -060081 for (i = n->len -1; i < n->len; i--) {
Stefan Eßerfc689bb2019-04-29 13:40:18 -060082
Gavin Howardc88e5232019-04-27 07:33:16 -060083 if (i + 1 == n->rdx) fprintf(stderr, ". ");
Stefan Eßerfc689bb2019-04-29 13:40:18 -060084
85 if (scale / BC_BASE_POWER != n->rdx -i -1)
86 fprintf(stderr, "%0*d ", BC_BASE_POWER, n->num[i]);
87 else {
88
89 size_t mod = scale % BC_BASE_POWER;
90 BcDig div;
91
92 if (mod != 0) {
Gavin Howard12a6f462019-04-29 13:57:21 -060093 div = n->num[i] / (BcDig) bc_num_pow10[BC_BASE_POWER - mod];
Stefan Eßerfc689bb2019-04-29 13:40:18 -060094 fprintf(stderr, "%0*d", (int) mod, div);
95 }
96
Gavin Howard12a6f462019-04-29 13:57:21 -060097 div = n->num[i] / (BcDig) bc_num_pow10[mod];
Stefan Eßerfc689bb2019-04-29 13:40:18 -060098 fprintf(stderr, " ' %0*d ", BC_BASE_POWER - (int) mod, div);
99 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600100 }
101
Stefan Eßerfc689bb2019-04-29 13:40:18 -0600102 fprintf(stderr, "(%zu | %zu.%zu/%zu) %p\n",
103 n->scale, n->len, n->rdx, n->cap, (void*) n->num);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600104}
Gavin Howard55c8f732019-04-24 14:05:27 -0600105#endif // BC_DEBUG_CODE
106
Gavin Howard49ea7b62019-04-23 14:46:26 -0600107static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
108
Gavin Howard56ebcf72019-02-21 16:59:48 -0700109static ssize_t bc_num_neg(size_t n, bool neg) {
110 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
111}
112
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700113ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -0700114 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -0700115}
116
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700117static size_t bc_num_int(const BcNum *n) {
118 return n->len ? n->len - n->rdx : 0;
119}
120
Gavin Howarda83a5e52019-02-21 16:51:06 -0700121static bool bc_num_isOne(const BcNum *n) {
122 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
123}
124
Gavin Howard798508b2019-02-21 16:45:32 -0700125static void bc_num_expand(BcNum *restrict n, size_t req) {
126 assert(n);
127 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
128 if (req > n->cap) {
Gavin Howard404ece72019-04-26 15:59:15 -0600129 n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req));
Gavin Howard798508b2019-02-21 16:45:32 -0700130 n->cap = req;
131 }
132}
133
Gavin Howard1cbfe242019-01-09 17:13:11 -0700134static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600135 assert(n);
Gavin Howardfcf29012019-04-25 20:12:19 -0600136 n->scale = scale;
137 n->len = n->rdx = 0;
Gavin Howard97102b12018-11-01 23:37:31 -0600138 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600139}
140
Gavin Howard1cbfe242019-01-09 17:13:11 -0700141static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600142 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -0600143}
144
Gavin Howard3c02ed32018-12-28 18:17:15 -0700145void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600146 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -0600147 n->len = 1;
148 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -0600149}
150
Gavin Howard3c02ed32018-12-28 18:17:15 -0700151void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600152 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600153 bc_num_setToZero(n, 0);
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600154#if BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600155 n->len = 2;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600156 n->num[0] = 0;
157 n->num[1] = 1;
158#else // BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600159 n->len = 1;
Stefan Eßer4b111c22019-04-24 17:23:08 -0600160 n->num[0] = BC_BASE;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600161#endif // BC_BASE_DIG == 10
Gavin Howard0be26ed2018-08-31 20:21:56 -0600162}
163
Gavin Howard1cbfe242019-01-09 17:13:11 -0700164static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -0700165 size_t len;
Gavin Howard530e3072019-04-23 16:23:36 -0600166 for (len = 1; i; i /= BC_BASE, ++len);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700167 return len;
168}
169
Gavin Howard445c1892019-04-29 13:50:24 -0600170static size_t bc_num_int_digits(const BcNum *n) {
171
172 size_t digits;
173
174 digits = bc_num_int(n) * BC_BASE_POWER;
175
176 if (digits > 0)
177 digits -= BC_BASE_POWER - bc_num_log10((size_t) n->num[n->len -1]);
178
179 return digits;
180}
181
Gavin Howard08f78562019-04-29 07:56:04 -0600182static size_t bc_num_nonzeroIdx(const BcNum *restrict n) {
183 size_t i, len = n->len;
184 assert(len == n->rdx);
185 for (i = len - 1; i < n->len && !n->num[i]; --len, --i);
186 return len;
187}
188
Gavin Howardfcf29012019-04-25 20:12:19 -0600189static unsigned long bc_num_addDigit(BcDig *restrict num, unsigned long d,
190 unsigned long c)
Gavin Howard14c354a2019-04-24 14:28:24 -0600191{
192 d += c;
Gavin Howard0700efc2019-04-24 17:31:23 -0600193 *num = (BcDig) (d % BC_BASE_DIG);
194 assert(*num >= 0 && *num < BC_BASE_DIG);
195 return d / BC_BASE_DIG;
Gavin Howard14c354a2019-04-24 14:28:24 -0600196}
197
198static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
199 size_t len)
200{
201 size_t i;
Gavin Howard971a2672019-04-26 14:32:07 -0600202 unsigned long carry = 0;
Gavin Howard14c354a2019-04-24 14:28:24 -0600203
204 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard971a2672019-04-26 14:32:07 -0600205 unsigned long in = ((unsigned long) a[i]) + ((unsigned long) b[i]);
Gavin Howard14c354a2019-04-24 14:28:24 -0600206 carry = bc_num_addDigit(a + i, in, carry);
207 }
208
209 for (; BC_NO_SIG && carry; ++i)
Gavin Howard971a2672019-04-26 14:32:07 -0600210 carry = bc_num_addDigit(a + i, (unsigned long) a[i], carry);
Gavin Howard14c354a2019-04-24 14:28:24 -0600211
212 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
213}
214
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700215static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
216 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700217{
Gavin Howard63738202018-09-26 15:34:20 -0600218 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600219
Gavin Howard2d188a52019-02-25 14:19:08 -0700220 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600221
Gavin Howard2d188a52019-02-25 14:19:08 -0700222 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600223 assert(a[i + j] >= -BC_BASE_DIG);
224 a[i + j++] += BC_BASE_DIG;
Gavin Howard54b946a2018-10-23 12:06:57 -0600225 a[i + j] -= 1;
Gavin Howard0700efc2019-04-24 17:31:23 -0600226 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_DIG);
Gavin Howard63738202018-09-26 15:34:20 -0600227 }
228 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600229
Gavin Howard2d188a52019-02-25 14:19:08 -0700230 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600231}
232
Gavin Howard2d188a52019-02-25 14:19:08 -0700233static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
234 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700235{
Gavin Howard63738202018-09-26 15:34:20 -0600236 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600237 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700238 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600239 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600240}
241
Gavin Howard3c02ed32018-12-28 18:17:15 -0700242ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700243
Gavin Howard63738202018-09-26 15:34:20 -0600244 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600245 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600246 bool a_max, neg = false;
247 ssize_t cmp;
248
249 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700250
Gavin Howard8a921bd2018-10-11 14:15:32 -0600251 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700252 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700253 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600254 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600255 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600256 else return -1;
257 }
258 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700259
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700260 a_int = bc_num_int(a);
261 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600262 a_int -= b_int;
263 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700264
Gavin Howard1ab22d22019-01-03 13:32:17 -0700265 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700266
Gavin Howard890d0c02018-10-30 16:34:50 -0600267 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600268 min = b->rdx;
269 diff = a->rdx - b->rdx;
270 max_num = a->num + diff;
271 min_num = b->num;
272 }
273 else {
274 min = a->rdx;
275 diff = b->rdx - a->rdx;
276 max_num = b->num + diff;
277 min_num = a->num;
278 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700279
Gavin Howard890d0c02018-10-30 16:34:50 -0600280 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600281 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700282 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700283
Gavin Howard2d188a52019-02-25 14:19:08 -0700284 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700285 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600286 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700287
Gavin Howardf2ee6342019-04-09 17:05:20 -0600288 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700289}
290
Gavin Howard2a366922019-01-16 10:50:20 -0700291static void bc_num_clean(BcNum *restrict n) {
292 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
293 if (BC_NUM_ZERO(n)) n->neg = false;
294 else if (n->len < n->rdx) n->len = n->rdx;
295}
296
Gavin Howard3c02ed32018-12-28 18:17:15 -0700297void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700298
Gavin Howardb1558662019-04-26 14:32:55 -0600299 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700300
Stefan Esser70f11852019-04-27 10:39:00 +0200301 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700302
Gavin Howardb1558662019-04-26 14:32:55 -0600303 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
304 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
305
306 n->scale -= places;
307 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700308
Gavin Howardddce6d42019-01-03 14:07:06 -0700309 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600310
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600311 size_t pow;
312
313 pow = n->scale % BC_BASE_POWER;
314 pow = pow ? BC_BASE_POWER - pow : 0;
Gavin Howard12a6f462019-04-29 13:57:21 -0600315 pow = bc_num_pow10[pow];
Gavin Howardb1558662019-04-26 14:32:55 -0600316
317 n->len -= places_rdx;
Gavin Howard404ece72019-04-26 15:59:15 -0600318 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
Gavin Howardb1558662019-04-26 14:32:55 -0600319
320 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600321 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600322
Gavin Howard2a366922019-01-16 10:50:20 -0700323 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600324 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700325}
326
Stefan Esser70f11852019-04-27 10:39:00 +0200327static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700328
Gavin Howardb1558662019-04-26 14:32:55 -0600329 size_t places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700330
Gavin Howard1ab22d22019-01-03 13:32:17 -0700331 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700332
Gavin Howardb1558662019-04-26 14:32:55 -0600333 bc_num_expand(n, n->len + places_rdx);
Gavin Howard404ece72019-04-26 15:59:15 -0600334 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
335 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
Gavin Howarde6f326e2019-04-26 10:13:45 -0600336 n->rdx += places_rdx;
337 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600338 n->len += places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700339
Gavin Howarde6f326e2019-04-26 10:13:45 -0600340 assert(n->rdx == BC_NUM_RDX(n->scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200341}
342
Gavin Howardb2d733b2019-04-29 13:20:45 -0600343static void bc_num_roundPlaces(BcNum *restrict n, size_t places) {
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600344
345 size_t rdx, place, i;
346 BcDig p10, sum;
347
348 if (places >= n->scale || places >= n->rdx * BC_BASE_POWER) {
Gavin Howardb2d733b2019-04-29 13:20:45 -0600349 bc_num_extend(n, places - n->scale);
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600350 return;
351 }
Gavin Howardb2d733b2019-04-29 13:20:45 -0600352
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600353 rdx = n->rdx - BC_NUM_RDX(places + 1);
354 place = BC_BASE_POWER - (places % BC_BASE_POWER + 1);
355
Gavin Howardb2d733b2019-04-29 13:20:45 -0600356 for (i = 0; i < rdx; i++) n->num[i] = 0;
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600357
Gavin Howard12a6f462019-04-29 13:57:21 -0600358 p10 = (BcDig) bc_num_pow10[place];
Gavin Howardb2d733b2019-04-29 13:20:45 -0600359 sum = n->num[rdx] + (BC_BASE / 2) * p10;
360 sum = sum - sum % (BC_BASE * p10);
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600361
Gavin Howardb2d733b2019-04-29 13:20:45 -0600362 if (sum < BC_BASE_DIG) n->num[rdx] = sum;
363 else {
364
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600365 sum -= BC_BASE_DIG;
366 n->num[rdx] = sum;
367
368 do {
Gavin Howardb2d733b2019-04-29 13:20:45 -0600369 rdx += 1;
370
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600371 if (n->len <= rdx) {
372 bc_num_expand(n, rdx + 1);
373 n->num[rdx] = 0;
374 n->len = rdx +1;
375 }
Gavin Howardb2d733b2019-04-29 13:20:45 -0600376
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600377 sum = n->num[rdx] + 1;
378 n->num[rdx] = sum < BC_BASE_DIG ? sum : sum - BC_BASE_DIG;
379 } while (sum >= BC_BASE_DIG);
380 }
Gavin Howardb2d733b2019-04-29 13:20:45 -0600381
382 bc_num_truncate(n, n->scale - places);
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600383}
384
Gavin Howard2d188a52019-02-25 14:19:08 -0700385static void bc_num_retireMul(BcNum *restrict n, size_t scale,
386 bool neg1, bool neg2)
387{
Gavin Howard971a2672019-04-26 14:32:07 -0600388 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
389 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600390
Gavin Howard305249a2018-10-15 20:24:47 -0600391 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700392 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600393}
394
Gavin Howard2d188a52019-02-25 14:19:08 -0700395static void bc_num_split(const BcNum *restrict n, size_t idx,
396 BcNum *restrict a, BcNum *restrict b)
397{
Gavin Howard305249a2018-10-15 20:24:47 -0600398 if (idx < n->len) {
399
Gavin Howard305249a2018-10-15 20:24:47 -0600400 b->len = n->len - idx;
401 a->len = idx;
Gavin Howarda3f260c2019-04-26 15:47:25 -0600402 a->scale = a->rdx = b->scale = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600403
Gavin Howard404ece72019-04-26 15:59:15 -0600404 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
405 memcpy(a->num, n->num, BC_NUM_SIZE(idx));
Gavin Howard50c80022019-01-03 15:14:33 -0700406
407 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600408 }
Gavin Howard50c80022019-01-03 15:14:33 -0700409 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600410
411 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600412}
413
Gavin Howard14913fc2019-04-23 14:36:03 -0600414static size_t bc_num_shiftZero(BcNum *restrict n) {
415
416 size_t i;
417
418 assert(!n->rdx || BC_NUM_ZERO(n));
419
420 for (i = 0; i < n->len && !n->num[i]; ++i);
421
422 n->len -= i;
423 n->num += i;
424
425 return i;
426}
427
Gavin Howard2e736de2019-04-27 06:41:58 -0600428static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
429 n->len += places_rdx;
430 n->num -= places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600431}
432
Gavin Howard48a0e472019-04-26 15:39:46 -0600433static BcStatus bc_num_shift(BcNum *restrict n, BcDig *restrict ptr,
434 unsigned long dig, size_t len)
435{
Gavin Howardb1558662019-04-26 14:32:55 -0600436 size_t i;
437 unsigned long carry = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600438
Gavin Howardb1558662019-04-26 14:32:55 -0600439 assert(dig < BC_BASE_POWER);
Gavin Howard305249a2018-10-15 20:24:47 -0600440
Gavin Howard12a6f462019-04-29 13:57:21 -0600441 dig = bc_num_pow10[dig];
Gavin Howardb1558662019-04-26 14:32:55 -0600442
443 for (i = 0; BC_NO_SIG && i < len; ++i) {
444 unsigned long in = ((unsigned long) ptr[i]) * dig;
445 carry = bc_num_addDigit(ptr + i, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600446 }
447
Gavin Howardb1558662019-04-26 14:32:55 -0600448 if (carry) {
449 bc_num_expand(n, n->len + 1);
450 assert(carry < BC_BASE_DIG);
451 n->num[n->len] = (BcDig) carry;
452 n->len += 1;
453 }
454
455 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600456}
457
Gavin Howardb1558662019-04-26 14:32:55 -0600458static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700459
Gavin Howardb1558662019-04-26 14:32:55 -0600460 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600461 unsigned long dig;
462 bool shift;
463 size_t places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600464
Gavin Howardb1558662019-04-26 14:32:55 -0600465 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600466
Gavin Howard48a0e472019-04-26 15:39:46 -0600467 dig = (unsigned long) (places % BC_BASE_POWER);
468 shift = (dig != 0);
469 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx - shift;
470
Gavin Howardb1558662019-04-26 14:32:55 -0600471 if (n->scale >= places) {
472 n->scale -= places;
473 n->rdx = BC_NUM_RDX(n->scale);
474 }
475 else if (BC_NUM_NONZERO(n)) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600476
Stefan Esser6189d952019-04-27 10:40:14 +0200477 if (places_rdx > n->rdx) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600478 bc_num_expand(n, n->len + places_rdx - n->rdx + shift);
Gavin Howard404ece72019-04-26 15:59:15 -0600479 memmove(n->num + places_rdx - n->rdx, n->num, BC_NUM_SIZE(n->len));
480 memset(n->num, 0, BC_NUM_SIZE(places_rdx - n->rdx));
Gavin Howard48a0e472019-04-26 15:39:46 -0600481 n->len += places_rdx;
482 }
483
Gavin Howardb1558662019-04-26 14:32:55 -0600484 n->scale = n->rdx = 0;
485 }
486 else n->scale = 0;
487
488 if (shift) s = bc_num_shift(n, n->num + places_rdx, dig, n->len - places_rdx);
489
490 bc_num_clean(n);
491
492 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
493}
494
495static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
496
497 BcStatus s = BC_STATUS_SUCCESS;
498 unsigned long dig = (unsigned long) (places % BC_BASE_POWER);
499 bool shift = (dig != 0);
500 size_t len, places_rdx;
501
502 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600503 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600504 n->scale += places;
505 bc_num_expand(n, BC_NUM_RDX(n->scale));
506 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600507 }
508
Gavin Howardb1558662019-04-26 14:32:55 -0600509 places_rdx = BC_NUM_RDX(places);
Gavin Howard36ec9872019-04-29 10:14:21 -0600510 len = bc_vm_growSize(n->rdx, places_rdx);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700511
512 if (len > n->len) {
513
514 if (len > n->cap) bc_num_expand(n, len);
515
Gavin Howard404ece72019-04-26 15:59:15 -0600516 memset(n->num + n->len, 0, BC_NUM_SIZE(len - n->len));
Gavin Howard7ad5a662019-02-19 14:40:46 -0700517 n->len = len;
518 }
519
Gavin Howardb1558662019-04-26 14:32:55 -0600520 n->scale += places;
521 n->rdx += places_rdx;
522
523 if (shift) s = bc_num_shift(n, n->num, BC_BASE_POWER - dig, n->len);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700524
525 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600526 assert(n->rdx == BC_NUM_RDX(n->scale));
527
528 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700529}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700530
Gavin Howard1cbfe242019-01-09 17:13:11 -0700531static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600532
Gavin Howard63738202018-09-26 15:34:20 -0600533 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600534 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600535
Gavin Howard971a2672019-04-26 14:32:07 -0600536 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700537
Gavin Howardd32d7df2018-10-15 08:42:25 -0600538 one.cap = 2;
539 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600540 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600541
Gavin Howardd32d7df2018-10-15 08:42:25 -0600542 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600543}
544
Gavin Howard7bda4782018-12-28 09:53:22 -0700545#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700546static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
547 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700548{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700549 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700550 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700551 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700552}
Gavin Howard7bda4782018-12-28 09:53:22 -0700553#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700554
Gavin Howard1cbfe242019-01-09 17:13:11 -0700555static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700556
Gavin Howard8a921bd2018-10-11 14:15:32 -0600557 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600558 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard6fccd222019-04-26 10:17:02 -0600559 unsigned long carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700560
Gavin Howard63738202018-09-26 15:34:20 -0600561 // Because this function doesn't need to use scale (per the bc spec),
562 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600563
Gavin Howardddce6d42019-01-03 14:07:06 -0700564 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600565 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700566 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600567 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600568 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700569 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600570 bc_num_copy(c, a);
571 return BC_STATUS_SUCCESS;
572 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600573
Gavin Howard63738202018-09-26 15:34:20 -0600574 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600575 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600576 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600577 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700578
Gavin Howard63738202018-09-26 15:34:20 -0600579 if (a->rdx > b->rdx) {
580 diff = a->rdx - b->rdx;
581 ptr = a->num;
582 ptr_a = a->num + diff;
583 ptr_b = b->num;
584 }
585 else {
586 diff = b->rdx - a->rdx;
587 ptr = b->num;
588 ptr_a = a->num;
589 ptr_b = b->num + diff;
590 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700591
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700592 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700593
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700594 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600595 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700596 a_int = bc_num_int(a);
597 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700598
Gavin Howard890d0c02018-10-30 16:34:50 -0600599 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600600 min_int = b_int;
601 max = a_int;
602 ptr = ptr_a;
603 }
604 else {
605 min_int = a_int;
606 max = b_int;
607 ptr = ptr_b;
608 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700609
Gavin Howard2d188a52019-02-25 14:19:08 -0700610 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard6fccd222019-04-26 10:17:02 -0600611 unsigned long in;
612 in = ((unsigned long) ptr_a[i]) + ((unsigned long) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700613 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600614 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700615
Gavin Howard2d188a52019-02-25 14:19:08 -0700616 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard6fccd222019-04-26 10:17:02 -0600617 carry = bc_num_addDigit(ptr_c + i, (unsigned long) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700618
619 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700620
Gavin Howard1ab22d22019-01-03 13:32:17 -0700621 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600622
Gavin Howard2d188a52019-02-25 14:19:08 -0700623 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700624}
625
Gavin Howard1cbfe242019-01-09 17:13:11 -0700626static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700627
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700628 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600629 ssize_t cmp;
630 BcNum *minuend, *subtrahend;
631 size_t start;
632 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700633
Gavin Howard63738202018-09-26 15:34:20 -0600634 // Because this function doesn't need to use scale (per the bc spec),
635 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700636
Gavin Howardddce6d42019-01-03 14:07:06 -0700637 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600638 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700639 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600640 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600641 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700642 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600643 bc_num_copy(c, a);
644 return BC_STATUS_SUCCESS;
645 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700646
Gavin Howard63738202018-09-26 15:34:20 -0600647 aneg = a->neg;
648 bneg = b->neg;
649 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700650
Gavin Howard63738202018-09-26 15:34:20 -0600651 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600652 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700653
Gavin Howard63738202018-09-26 15:34:20 -0600654 a->neg = aneg;
655 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700656
Gavin Howard1ab22d22019-01-03 13:32:17 -0700657 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600658 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600659 return BC_STATUS_SUCCESS;
660 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700661
662 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600663 neg = a->neg;
664 minuend = a;
665 subtrahend = b;
666 }
667 else {
668 neg = b->neg;
669 if (sub) neg = !neg;
670 minuend = b;
671 subtrahend = a;
672 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700673
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600674 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600675 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700676
Gavin Howarde6f326e2019-04-26 10:13:45 -0600677 if (c->scale < subtrahend->scale) {
678 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600679 start = 0;
680 }
681 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700682
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700683 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700684
Gavin Howard63738202018-09-26 15:34:20 -0600685 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700686
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700687 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700688}
689
Gavin Howarddc4e3712019-04-23 14:56:29 -0600690static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
691{
Gavin Howard971a2672019-04-26 14:32:07 -0600692 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600693 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard971a2672019-04-26 14:32:07 -0600694 unsigned long sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600695
Gavin Howard971a2672019-04-26 14:32:07 -0600696 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600697 assert(!a->rdx && !b->rdx);
698
Gavin Howard5083aec2019-04-23 18:03:46 -0600699 clen = bc_vm_growSize(alen, blen);
700 bc_num_expand(c, clen + 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600701
702 ptr_c = c->num;
Gavin Howard404ece72019-04-26 15:59:15 -0600703 memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600704
Gavin Howard5083aec2019-04-23 18:03:46 -0600705 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600706
Gavin Howard5083aec2019-04-23 18:03:46 -0600707 ssize_t sidx = (ssize_t) (i - blen + 1);
708 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600709
Stefan Essera299ffc2019-04-24 23:28:32 +0200710 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
Stefan Eßer4b111c22019-04-24 17:23:08 -0600711
Gavin Howard971a2672019-04-26 14:32:07 -0600712 sum += ((unsigned long) ptr_a[j]) * ((unsigned long) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600713
Gavin Howard0700efc2019-04-24 17:31:23 -0600714 if (sum >= BC_BASE_DIG) {
715 carry += sum / BC_BASE_DIG;
716 sum %= BC_BASE_DIG;
Stefan Essera299ffc2019-04-24 23:28:32 +0200717 }
718 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600719
Stefan Essera299ffc2019-04-24 23:28:32 +0200720 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600721 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Essera299ffc2019-04-24 23:28:32 +0200722 sum = carry;
723 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600724 }
725
Gavin Howard5083aec2019-04-23 18:03:46 -0600726 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600727 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600728 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600729 clen += 1;
730 }
731
732 c->len = clen;
733
Gavin Howarddc4e3712019-04-23 14:56:29 -0600734 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
735}
736
Gavin Howard14c354a2019-04-24 14:28:24 -0600737static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
738 size_t shift, BcNumShiftAddOp op)
739{
740 assert(n->len >= shift + a->len);
741 assert(!n->rdx && !a->rdx);
742 return op(n->num + shift, a->num, a->len);
743}
744
745static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600746
Gavin Howard9b801632019-02-16 23:34:06 -0700747 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200748 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600749 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700750 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600751 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700752 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600753
Gavin Howard14913fc2019-04-23 14:36:03 -0600754 assert(BC_NUM_ZERO(c));
755
Gavin Howardddce6d42019-01-03 14:07:06 -0700756 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600757 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700758 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600759 bc_num_zero(c);
760 return BC_STATUS_SUCCESS;
761 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700762 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600763 bc_num_copy(c, aone ? b : a);
764 return BC_STATUS_SUCCESS;
765 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700766
Gavin Howardb5093d72018-10-17 12:13:17 -0600767 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
768 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600769 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600770 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600771 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700772
Gavin Howarde0432502019-02-26 16:23:45 -0700773 max = BC_MAX(a->len, b->len);
Stefan Essera299ffc2019-04-24 23:28:32 +0200774 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700775 max2 = (max + 1) / 2;
776
Stefan Esser5fd43a32019-04-25 07:05:24 +0200777 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
Gavin Howard404ece72019-04-26 15:59:15 -0600778 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total));
Gavin Howard5bede412019-02-23 09:31:28 -0700779
780 bc_num_setup(&l1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200781 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700782 bc_num_setup(&h1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200783 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700784 bc_num_setup(&l2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200785 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700786 bc_num_setup(&h2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200787 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700788 bc_num_setup(&m1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200789 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700790 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600791 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600792 bc_num_init(&z0, max);
793 bc_num_init(&z1, max);
794 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600795 max = bc_vm_growSize(max, max) + 1;
796 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600797
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600798 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600799 bc_num_clean(&l1);
800 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600801 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600802 bc_num_clean(&l2);
803 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700804
Gavin Howard14c354a2019-04-24 14:28:24 -0600805 bc_num_expand(c, max);
806 c->len = max;
Gavin Howard404ece72019-04-26 15:59:15 -0600807 memset(c->num, 0, BC_NUM_SIZE(c->len));
Gavin Howard305249a2018-10-15 20:24:47 -0600808
Gavin Howard14c354a2019-04-24 14:28:24 -0600809 s = bc_num_sub(&h1, &l1, &m1, 0);
810 if (BC_ERR(s)) goto err;
811 s = bc_num_sub(&l2, &h2, &m2, 0);
812 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600813
Gavin Howard971a2672019-04-26 14:32:07 -0600814 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600815
Gavin Howard14c354a2019-04-24 14:28:24 -0600816 s = bc_num_m(&h1, &h2, &z2, 0);
817 if (BC_ERR(s)) goto err;
818 bc_num_clean(&z2);
819
820 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
821 if (BC_ERR(s)) goto err;
822 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
823 if (BC_ERR(s)) goto err;
824 }
825
Gavin Howard971a2672019-04-26 14:32:07 -0600826 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600827
828 s = bc_num_m(&l1, &l2, &z0, 0);
829 if (BC_ERR(s)) goto err;
830 bc_num_clean(&z0);
831
832 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
833 if (BC_ERR(s)) goto err;
834 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
835 if (BC_ERR(s)) goto err;
836 }
837
Gavin Howard971a2672019-04-26 14:32:07 -0600838 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600839
840 s = bc_num_m(&m1, &m2, &z1, 0);
841 if (BC_ERR(s)) goto err;
842 bc_num_clean(&z1);
843
844 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
845 s = bc_num_shiftAddSub(c, &z1, max2, op);
846 if (BC_ERR(s)) goto err;
847 }
Gavin Howard305249a2018-10-15 20:24:47 -0600848
849err:
Gavin Howard5bede412019-02-23 09:31:28 -0700850 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600851 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600852 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600853 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600854 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600855 return s;
856}
857
Gavin Howard1cbfe242019-01-09 17:13:11 -0700858static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600859
860 BcStatus s;
861 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600862 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600863
Gavin Howard14913fc2019-04-23 14:36:03 -0600864 bc_num_setToZero(c, 0);
865
Gavin Howardcf9f2462019-04-27 06:43:57 -0600866 ascale = a->scale;
867 bscale = b->scale;
868 scale = BC_MAX(scale, ascale);
Gavin Howarda9a887a2019-04-29 14:00:56 -0600869 scale = BC_MAX(scale, bscale);
Gavin Howard7cb5f6d2019-04-29 14:01:44 -0600870
Gavin Howard48a0e472019-04-26 15:39:46 -0600871 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600872 scale = BC_MIN(rscale, scale);
Gavin Howard305249a2018-10-15 20:24:47 -0600873
Gavin Howardbaa4f582019-01-24 13:56:35 -0700874 bc_num_createCopy(&cpa, a);
875 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600876
Gavin Howard23d8d252018-10-17 12:03:41 -0600877 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600878
Gavin Howard48a0e472019-04-26 15:39:46 -0600879 ardx = cpa.rdx * BC_BASE_POWER;
880 s = bc_num_shiftLeft(&cpa, ardx);
881 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600882 bc_num_clean(&cpa);
883 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600884
885 brdx = cpb.rdx * BC_BASE_POWER;
886 s = bc_num_shiftLeft(&cpb, brdx);
887 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600888 bzero = bc_num_shiftZero(&cpb);
889 bc_num_clean(&cpb);
890
Gavin Howard14c354a2019-04-24 14:28:24 -0600891 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700892 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600893
Gavin Howard14913fc2019-04-23 14:36:03 -0600894 zero = bc_vm_growSize(azero, bzero);
895 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600896
Gavin Howard14913fc2019-04-23 14:36:03 -0600897 bc_num_expand(c, len);
Gavin Howard48a0e472019-04-26 15:39:46 -0600898 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_POWER);
899 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
900 s = bc_num_shiftRight(c, ardx + brdx);
901 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600902
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600903 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600904
Gavin Howard305249a2018-10-15 20:24:47 -0600905err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600906 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600907 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600908 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600909 bc_num_free(&cpa);
910 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700911}
912
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600913static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700914
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600915 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard36ec9872019-04-29 10:14:21 -0600916 size_t rdx, rscale, req;
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600917 ssize_t cmp;
Gavin Howard36ec9872019-04-29 10:14:21 -0600918 BcNum cpa, cpb, two, factor, factor2, *fi, *fnext, *temp;
919 BcDig two_digs[2];
Gavin Howardc542d0a2019-04-29 10:28:51 -0600920 bool aneg, bneg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700921
Gavin Howard36ec9872019-04-29 10:14:21 -0600922 aneg = a->neg;
923 bneg = b->neg;
924
Gavin Howardddce6d42019-01-03 14:07:06 -0700925 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
926 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600927 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600928 return BC_STATUS_SUCCESS;
929 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700930 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600931 bc_num_copy(c, a);
Gavin Howardfdbcd062019-04-29 07:36:46 -0600932 goto exit;
Gavin Howard63738202018-09-26 15:34:20 -0600933 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700934
Gavin Howard36ec9872019-04-29 10:14:21 -0600935 a->neg = b->neg = false;
Gavin Howardfdbcd062019-04-29 07:36:46 -0600936 cmp = bc_num_cmp(a, b);
Gavin Howard021150b2018-03-10 15:40:42 -0700937
Gavin Howardfdbcd062019-04-29 07:36:46 -0600938 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
939 if (!cmp) {
940 bc_num_one(c);
941 goto exit;
Gavin Howard63738202018-09-26 15:34:20 -0600942 }
Gavin Howardfdbcd062019-04-29 07:36:46 -0600943
944 bc_num_createCopy(&cpa, a);
945 bc_num_createCopy(&cpb, b);
946
Gavin Howard36ec9872019-04-29 10:14:21 -0600947 req = bc_num_int(a) + BC_NUM_RDX(scale) + 1;
948 bc_num_init(&factor, req);
949 bc_num_init(&factor2, req);
950
951 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
952 bc_num_one(&two);
953 two.num[0] = 2;
954
Gavin Howardc542d0a2019-04-29 10:28:51 -0600955 if (b->rdx == b->len) {
Gavin Howard36ec9872019-04-29 10:14:21 -0600956 rdx = cpb.len - bc_num_nonzeroIdx(&cpb);
957 rscale = rdx * BC_BASE_POWER;
958 s = bc_num_shiftLeft(&cpa, rscale);
959 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
960 s = bc_num_shiftLeft(&cpb, rscale);
Stefan Esser193f8522019-04-27 01:29:01 +0200961 }
Gavin Howardfdbcd062019-04-29 07:36:46 -0600962 else {
963 rdx = b->len - b->rdx;
Gavin Howard36ec9872019-04-29 10:14:21 -0600964 rscale = rdx * BC_BASE_POWER;
965 s = bc_num_shiftRight(&cpa, rscale);
Gavin Howardfdbcd062019-04-29 07:36:46 -0600966 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard36ec9872019-04-29 10:14:21 -0600967 s = bc_num_shiftRight(&cpb, rscale);
Gavin Howardfdbcd062019-04-29 07:36:46 -0600968 }
Gavin Howard034b5b92019-04-27 07:28:56 -0600969
Gavin Howard36ec9872019-04-29 10:14:21 -0600970 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
971
972 req = bc_num_int(&cpa);
973 if (!req) req = cpa.len - bc_num_nonzeroIdx(&cpa);
Gavin Howardcef03972019-04-29 10:28:33 -0600974 req = BC_BASE_POWER * (req + 1);
Gavin Howard36ec9872019-04-29 10:14:21 -0600975 req += b->scale % BC_BASE_POWER == 0 ? BC_BASE_POWER : 0;
Gavin Howardfc290382019-04-29 13:22:26 -0600976 req += BC_NUM_RDX(scale) * BC_BASE_POWER + 2;
Gavin Howard36ec9872019-04-29 10:14:21 -0600977
Gavin Howardcef03972019-04-29 10:28:33 -0600978 rscale += req;
Gavin Howard36ec9872019-04-29 10:14:21 -0600979 bc_num_extend(&cpa, req);
980 bc_num_extend(&cpb, req);
981
Gavin Howard36ec9872019-04-29 10:14:21 -0600982 fi = &factor;
983 fnext = &factor2;
984 cmp = 1;
985
986 while (!bc_num_isOne(fi) && cmp) {
987
988 s = bc_num_sub(&two, &cpb, fi, rscale);
989 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
990 s = bc_num_mul(&cpa, fi, &cpa, rscale);
991 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
992 s = bc_num_mul(&cpb, fi, &cpb, rscale);
993 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
994
Gavin Howard36ec9872019-04-29 10:14:21 -0600995 temp = fi;
996 fi = fnext;
997 fnext = temp;
998
999 cmp = bc_num_cmp(fi, fnext);
1000 if (cmp == BC_NUM_SSIZE_MIN) {
1001 s = BC_STATUS_SIGNAL;
1002 goto err;
1003 }
Gavin Howard36ec9872019-04-29 10:14:21 -06001004 }
1005
Gavin Howardfc290382019-04-29 13:22:26 -06001006 bc_num_roundPlaces(&cpa, scale + 2);
Gavin Howard36ec9872019-04-29 10:14:21 -06001007 bc_num_copy(c, &cpa);
1008
Stefan Esser193f8522019-04-27 01:29:01 +02001009err:
Gavin Howard36ec9872019-04-29 10:14:21 -06001010 bc_num_free(&factor2);
1011 bc_num_free(&factor);
Gavin Howardfdbcd062019-04-29 07:36:46 -06001012 bc_num_free(&cpb);
1013 bc_num_free(&cpa);
1014exit:
Gavin Howard36ec9872019-04-29 10:14:21 -06001015 a->neg = aneg;
1016 b->neg = bneg;
Gavin Howardf2ee6342019-04-09 17:05:20 -06001017 if (BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfdbcd062019-04-29 07:36:46 -06001018 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -06001019 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001020}
1021
Gavin Howard1cbfe242019-01-09 17:13:11 -07001022static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
1023 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -06001024{
Gavin Howard63738202018-09-26 15:34:20 -06001025 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -06001026 BcNum temp;
1027 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001028
Gavin Howardddce6d42019-01-03 14:07:06 -07001029 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1030 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -07001031 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001032 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001033 return BC_STATUS_SUCCESS;
1034 }
Gavin Howard8b254872018-03-14 01:13:35 -06001035
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001036 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -07001037 s = bc_num_d(a, b, c, scale);
1038 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001039 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001040
Gavin Howard996fc222019-04-29 13:22:38 -06001041 if (scale) scale = ts + 1;
Gavin Howard3115c012018-10-04 10:53:56 -06001042
Gavin Howard53eba8b2018-10-31 15:14:37 -06001043 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001044 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001045 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001046 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -06001047
Gavin Howard77445432019-04-26 15:59:33 -06001048 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
Gavin Howard18441082018-10-22 10:03:30 -06001049
1050 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001051 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard996fc222019-04-29 13:22:38 -06001052 d->neg = BC_NUM_NONZERO(d) ? neg : false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001053
1054err:
Gavin Howard18441082018-10-22 10:03:30 -06001055 bc_num_free(&temp);
1056 return s;
1057}
1058
Gavin Howard2d188a52019-02-25 14:19:08 -07001059static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1060{
Gavin Howard18441082018-10-22 10:03:30 -06001061 BcStatus s;
1062 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -07001063 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -06001064
Gavin Howard77445432019-04-26 15:59:33 -06001065 ts = bc_vm_growSize(scale, b->scale);
1066 ts = BC_MAX(ts, a->scale);
Gavin Howard798508b2019-02-21 16:45:32 -07001067
1068 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -06001069 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001070 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -06001071
Gavin Howard63738202018-09-26 15:34:20 -06001072 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001073}
1074
Gavin Howard1cbfe242019-01-09 17:13:11 -07001075static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001076
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001077 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001078 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -07001079 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001080 size_t i, powrdx, resrdx;
1081 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001082
Gavin Howardecafd4f2019-02-23 09:30:45 -07001083 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001084
Gavin Howardddce6d42019-01-03 14:07:06 -07001085 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -06001086 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -06001087 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001088 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001089 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001090 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001091 return BC_STATUS_SUCCESS;
1092 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001093 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001094 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -06001095 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001096 return s;
1097 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001098
Gavin Howard63738202018-09-26 15:34:20 -06001099 neg = b->neg;
1100 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001101 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -07001102 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001103 if (s) return s;
1104
Gavin Howardbaa4f582019-01-24 13:56:35 -07001105 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001106
Stefan Eßerb5318342019-04-29 10:31:26 -06001107 if (!neg) scale = BC_MIN(a->scale * pow, BC_MAX(scale, a->scale));
Gavin Howardb29674f2018-03-22 22:24:58 -06001108
Gavin Howardd497c232019-04-29 10:32:38 -06001109 for (powrdx = a->scale; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -06001110 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001111 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001112 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001113 }
Gavin Howard6fb635f2018-03-03 12:45:42 -07001114
Gavin Howard2d188a52019-02-25 14:19:08 -07001115 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -07001116
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001117 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -07001118 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001119
Gavin Howard2d188a52019-02-25 14:19:08 -07001120 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001121
Gavin Howard4d5daba2019-01-07 14:42:50 -07001122 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001123 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001124 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001125
Gavin Howard63738202018-09-26 15:34:20 -06001126 if (pow & 1) {
1127 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001128 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001129 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001130 }
1131 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001132
Gavin Howard2d188a52019-02-25 14:19:08 -07001133 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -07001134 if (neg) {
1135 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001136 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -07001137 }
1138
Stefan Eßerb5318342019-04-29 10:31:26 -06001139 if (c->scale > scale) bc_num_truncate(c, c->scale - scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001140
Gavin Howard53eba8b2018-10-31 15:14:37 -06001141 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -06001142 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001143 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001144
Gavin Howard9b801632019-02-16 23:34:06 -07001145sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001146 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001147err:
Gavin Howard63738202018-09-26 15:34:20 -06001148 bc_num_free(&copy);
1149 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001150}
1151
Gavin Howard7bda4782018-12-28 09:53:22 -07001152#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -07001153static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
1154 size_t scale)
1155{
Gavin Howardac4d9122018-12-27 23:59:12 -07001156 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001157 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001158
1159 BC_UNUSED(scale);
1160
1161 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001162 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001163
Gavin Howardc9bef2d2019-04-26 14:46:52 -06001164 if (val < c->scale) bc_num_truncate(c, c->scale - val);
1165 else if (val > c->scale) bc_num_extend(c, val - c->scale);
Gavin Howardac4d9122018-12-27 23:59:12 -07001166
1167 return s;
1168}
1169
Gavin Howard2d188a52019-02-25 14:19:08 -07001170static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1171{
Gavin Howardac4d9122018-12-27 23:59:12 -07001172 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001173 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001174
1175 BC_UNUSED(scale);
1176
1177 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001178 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001179
Gavin Howardb1558662019-04-26 14:32:55 -06001180 return bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001181}
1182
Gavin Howard2d188a52019-02-25 14:19:08 -07001183static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1184 size_t scale)
1185{
Gavin Howardac4d9122018-12-27 23:59:12 -07001186 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001187 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001188
1189 BC_UNUSED(scale);
1190
1191 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001192 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001193
Gavin Howard2a366922019-01-16 10:50:20 -07001194 if (BC_NUM_ZERO(c)) return s;
1195
Gavin Howardb1558662019-04-26 14:32:55 -06001196 return bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001197}
Gavin Howard7bda4782018-12-28 09:53:22 -07001198#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001199
Gavin Howard1cbfe242019-01-09 17:13:11 -07001200static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1201 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001202{
Gavin Howarda1c44392018-09-27 12:41:15 -06001203 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001204 BcNum num2, *ptr_a, *ptr_b;
1205 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001206
Gavin Howard63738202018-09-26 15:34:20 -06001207 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001208
Gavin Howard890d0c02018-10-30 16:34:50 -06001209 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001210 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001211 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001212 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001213 }
1214 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001215
Gavin Howard63738202018-09-26 15:34:20 -06001216 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001217 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001218 if (c != a) {
1219 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001220 init = true;
1221 }
1222 }
1223 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001224
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001225 if (init) bc_num_init(c, req);
1226 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001227
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001228 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001229
Gavin Howardddce6d42019-01-03 14:07:06 -07001230 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001231 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001232
Gavin Howardba009802018-09-29 04:41:51 -06001233 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001234
Stefan Esser193f8522019-04-27 01:29:01 +02001235// bc_num_truncDecimals(c, scale);
1236
Gavin Howardee9d01e2019-02-16 22:48:11 -07001237 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001238}
1239
Gavin Howardc90ed902019-01-02 13:07:49 -07001240#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001241static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001242
Gavin Howardc7d655b2018-12-28 19:45:13 -07001243 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001244 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001245
Gavin Howard63738202018-09-26 15:34:20 -06001246 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001247
Gavin Howard63738202018-09-26 15:34:20 -06001248 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001249
Gavin Howard8a921bd2018-10-11 14:15:32 -06001250 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001251
Gavin Howard63738202018-09-26 15:34:20 -06001252 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001253
Gavin Howard63738202018-09-26 15:34:20 -06001254 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001255
Gavin Howard63738202018-09-26 15:34:20 -06001256 radix = true;
1257 continue;
1258 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001259
Gavin Howardc7d655b2018-12-28 19:45:13 -07001260 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001261 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001262
Gavin Howard63738202018-09-26 15:34:20 -06001263 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001264}
Gavin Howardc90ed902019-01-02 13:07:49 -07001265#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001266
Gavin Howard1cbfe242019-01-09 17:13:11 -07001267static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001268
Gavin Howardfcf29012019-04-25 20:12:19 -06001269 // TODO: Check this function.
1270
Gavin Howardc7d655b2018-12-28 19:45:13 -07001271 if (isupper(c)) {
1272 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001273 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001274 }
1275 else c -= '0';
1276
1277 return (unsigned long) (uchar) c;
1278}
1279
Gavin Howard1cbfe242019-01-09 17:13:11 -07001280static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001281
Gavin Howard3b60abb2019-04-26 08:49:17 -06001282 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001283 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001284 bool zero = true, rdx;
Stefan Essera299ffc2019-04-24 23:28:32 +02001285
Gavin Howard63738202018-09-26 15:34:20 -06001286 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001287
Gavin Howard63738202018-09-26 15:34:20 -06001288 val += i;
Gavin Howard3b60abb2019-04-26 08:49:17 -06001289 assert(isalnum(val[0]) || val[0] == '.');
Gavin Howard63738202018-09-26 15:34:20 -06001290 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001291
Gavin Howard63738202018-09-26 15:34:20 -06001292 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001293 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001294
Gavin Howardd43ec372019-04-25 21:13:54 -06001295 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001296
Gavin Howardd43ec372019-04-25 21:13:54 -06001297 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001298 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001299
Gavin Howard3b60abb2019-04-26 08:49:17 -06001300 i = len - (ptr == val ? 0 : i) - rdx;
Gavin Howard48a0e472019-04-26 15:39:46 -06001301 temp = BC_NUM_ROUND_POW(i);
Gavin Howard3b60abb2019-04-26 08:49:17 -06001302 mod = n->scale % BC_BASE_POWER;
1303 i = mod ? BC_BASE_POWER - mod : 0;
1304 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001305
1306 bc_num_expand(n, n->len);
Gavin Howard404ece72019-04-26 15:59:15 -06001307 memset(n->num, 0, BC_NUM_SIZE(n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -07001308
Gavin Howard6076dbb2019-04-26 09:47:50 -06001309 if (zero) n->len = n->rdx = 0;
1310 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001311
Gavin Howard971a2672019-04-26 14:32:07 -06001312 unsigned long exp, pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001313
Gavin Howard3b60abb2019-04-26 08:49:17 -06001314 exp = i;
Gavin Howard12a6f462019-04-29 13:57:21 -06001315 pow = bc_num_pow10[exp];
Gavin Howardd43ec372019-04-25 21:13:54 -06001316
1317 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001318
Gavin Howardc7d655b2018-12-28 19:45:13 -07001319 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001320
Gavin Howardd43ec372019-04-25 21:13:54 -06001321 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001322 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001323
1324 size_t idx = exp / BC_BASE_POWER;
1325
Gavin Howard8dd307e2019-01-08 23:05:19 -07001326 if (isupper(c)) c = '9';
Gavin Howardf196fbe2019-04-26 08:49:05 -06001327 n->num[idx] += (((unsigned long) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001328
1329 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1330 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001331 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001332 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001333 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001334}
1335
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001336static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1337 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001338{
Gavin Howardfcf29012019-04-25 20:12:19 -06001339 // TODO: Check this function.
Gavin Howard1cbfe242019-01-09 17:13:11 -07001340 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001341 BcNum temp, mult, result;
Gavin Howard77445432019-04-26 15:59:33 -06001342 char c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001343 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001344 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001345 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001346
Gavin Howard11b9afd2018-10-18 14:23:28 -06001347 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001348 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001349
Gavin Howard26d078f2019-01-24 13:58:12 -07001350 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1351 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001352
Gavin Howardc7d655b2018-12-28 19:45:13 -07001353 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001354
Gavin Howardc7d655b2018-12-28 19:45:13 -07001355 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001356
Gavin Howard53eba8b2018-10-31 15:14:37 -06001357 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001358 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001359 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001360 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001361 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001362 }
Gavin Howardede51f02018-03-02 12:30:00 -07001363
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001364 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001365
Gavin Howard63738202018-09-26 15:34:20 -06001366 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001367 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001368 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001369
Gavin Howard2d188a52019-02-25 14:19:08 -07001370 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001371 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001372 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001373
Gavin Howard53eba8b2018-10-31 15:14:37 -06001374 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001375 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001376
Gavin Howard705c4bc2018-12-03 15:38:04 -07001377 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001378 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001379 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001380 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001381 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001382 }
Gavin Howardede51f02018-03-02 12:30:00 -07001383
Gavin Howardf2ee6342019-04-09 17:05:20 -06001384 if (BC_SIG) {
1385 s = BC_STATUS_SIGNAL;
1386 goto err;
1387 }
1388
Gavin Howard1769abc2019-02-21 09:36:14 -07001389 // This one cannot be a divide by 0 because mult starts out at 1, then is
1390 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1391 s = bc_num_div(&result, &mult, &result, digs);
1392 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001393 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001394 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001395 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001396
Gavin Howardddce6d42019-01-03 14:07:06 -07001397 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001398 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001399 }
1400 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001401
Gavin Howardd1417682019-02-15 17:08:42 -07001402err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001403 bc_num_free(&result);
1404int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001405 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001406 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001407 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001408}
1409
Gavin Howard1cbfe242019-01-09 17:13:11 -07001410static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001411 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001412 bc_vm_putchar('\\');
1413 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001414 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001415 }
Gavin Howard80977b22018-09-06 14:29:13 -06001416}
1417
Gavin Howard40a085f2018-12-03 12:08:59 -07001418#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001419static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001420 BC_UNUSED(rdx);
1421 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001422 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001423}
1424#endif // DC_ENABLED
1425
Gavin Howard1cbfe242019-01-09 17:13:11 -07001426static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1427
Gavin Howardfcf29012019-04-25 20:12:19 -06001428 // TODO: Check this function.
1429
Gavin Howarda84ad992018-12-03 19:11:06 -07001430 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001431
Gavin Howardab5e9632019-01-02 13:32:02 -07001432 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001433 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001434 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001435
Gavin Howardab5e9632019-01-02 13:32:02 -07001436 bc_num_printNewline();
Gavin Howard530e3072019-04-23 16:23:36 -06001437 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001438
Gavin Howard530e3072019-04-23 16:23:36 -06001439 for (exp = 0; exp < len; pow /= BC_BASE, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001440 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001441 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001442 dig = n / pow;
1443 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001444 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001445 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001446}
Gavin Howardfe679f02018-02-14 15:50:09 -07001447
Gavin Howard1cbfe242019-01-09 17:13:11 -07001448static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001449
Gavin Howard3c02ed32018-12-28 18:17:15 -07001450 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001451
Gavin Howard3c02ed32018-12-28 18:17:15 -07001452 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001453 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001454 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001455 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001456 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001457
Gavin Howardab5e9632019-01-02 13:32:02 -07001458 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001459 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001460 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001461}
1462
Gavin Howard1cbfe242019-01-09 17:13:11 -07001463static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001464
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001465 size_t i, j, rdx = n->rdx;
1466 bool zero = true;
1467 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001468
Gavin Howard48af52e2018-10-30 14:47:38 -06001469 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001470 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001471
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001472 for (i = n->len - 1; i < n->len; --i) {
1473
1474 BcDig n9 = n->num[i];
1475 size_t temp;
1476 bool irdx = (i == rdx - 1);
1477
1478 zero = (zero & !irdx);
1479 temp = n->scale % BC_BASE_POWER;
1480 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1481
1482 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1483
1484 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Essera299ffc2019-04-24 23:28:32 +02001485 buffer[j] = n9 % BC_BASE;
1486 n9 /= BC_BASE;
1487 }
Stefan Esser0da17752019-04-25 12:25:15 +02001488
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001489 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1490 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1491 zero = (zero && buffer[j] == 0);
1492 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Essera299ffc2019-04-24 23:28:32 +02001493 }
1494 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001495}
1496
Gavin Howard7ad5a662019-02-19 14:40:46 -07001497#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001498static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) {
Gavin Howard7ad5a662019-02-19 14:40:46 -07001499
Gavin Howardfcf29012019-04-25 20:12:19 -06001500 // TODO: Check this function.
1501
Gavin Howardb1558662019-04-26 14:32:55 -06001502 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001503 bool neg = (n->len <= n->rdx);
1504 BcNum temp, exp;
1505 size_t places, mod;
1506 BcDig digs[BC_NUM_LONG_LOG10];
1507
1508 bc_num_createCopy(&temp, n);
1509
1510 if (neg) {
1511 places = n->rdx - bc_num_len(n) + 1;
1512 mod = places % 3;
1513 if (eng && mod != 0) places += 3 - mod;
Gavin Howardb1558662019-04-26 14:32:55 -06001514 s = bc_num_shiftLeft(&temp, places);
1515 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001516 }
1517 else {
Stefan Eßerc2e08932019-04-29 14:11:31 -06001518 places = bc_num_int_digits(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001519 mod = places % 3;
1520 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howardb1558662019-04-26 14:32:55 -06001521 s = bc_num_shiftRight(&temp, places);
1522 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001523 }
1524
1525 bc_num_printDecimal(&temp);
1526
1527 bc_num_printNewline();
1528 bc_vm_putchar('e');
1529
1530 if (!places) {
1531 bc_num_printHex(0, 1, false);
1532 goto exit;
1533 }
1534
1535 if (neg) {
1536 bc_num_printNewline();
1537 bc_vm_putchar('-');
1538 }
1539
1540 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1541 bc_num_ulong2num(&exp, (unsigned long) places);
1542
1543 bc_num_printDecimal(&exp);
1544
1545exit:
1546 bc_num_free(&temp);
Gavin Howardb1558662019-04-26 14:32:55 -06001547 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001548}
1549#endif // BC_ENABLE_EXTRA_MATH
1550
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001551static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1552 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001553{
Gavin Howardfcf29012019-04-25 20:12:19 -06001554 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001555 BcStatus s;
1556 BcVec stack;
1557 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001558 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001559 size_t i;
1560 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001561
Gavin Howard971a2672019-04-26 14:32:07 -06001562 assert(BC_NUM_NONZERO(base));
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001563
Gavin Howardddce6d42019-01-03 14:07:06 -07001564 if (BC_NUM_ZERO(n)) {
1565 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001566 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001567 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001568
Gavin Howardd3929832018-12-24 15:13:15 -07001569 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001570 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001571 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001572 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001573 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001574 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001575
Gavin Howard63738202018-09-26 15:34:20 -06001576 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001577 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001578 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001579
Gavin Howard2d188a52019-02-25 14:19:08 -07001580 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001581
1582 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001583 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001584 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001585
1586 // Will never fail (except for signals) because digit is
1587 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001588 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001589 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001590
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001591 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001592 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001593
Gavin Howard2d188a52019-02-25 14:19:08 -07001594 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001595
Gavin Howard2d188a52019-02-25 14:19:08 -07001596 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001597 ptr = bc_vec_item_rev(&stack, i);
1598 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001599 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001600 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001601
Gavin Howard2d188a52019-02-25 14:19:08 -07001602 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001603 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001604
Gavin Howard2d188a52019-02-25 14:19:08 -07001605 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001606
Gavin Howard53eba8b2018-10-31 15:14:37 -06001607 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001608 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001609
1610 // Will never fail (except for signals) because fracp is
1611 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001612 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001613 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001614
Gavin Howard705c4bc2018-12-03 15:38:04 -07001615 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001616 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001617 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001618
Gavin Howardddce6d42019-01-03 14:07:06 -07001619 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001620 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001621 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001622 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001623
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001624sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001625 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001626err:
Gavin Howard63738202018-09-26 15:34:20 -06001627 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001628 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001629 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001630 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001631 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001632 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001633}
1634
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001635static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1636 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001637{
Gavin Howardfcf29012019-04-25 20:12:19 -06001638 // TODO: Check this function.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001639 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001640 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001641 BcNumDigitOp print;
1642 bool neg = n->neg;
1643
Gavin Howard48af52e2018-10-30 14:47:38 -06001644 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001645 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001646
1647 n->neg = false;
1648
Gavin Howardbe4e8222019-01-03 13:34:16 -07001649 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001650 width = 1;
1651 print = bc_num_printHex;
1652 }
1653 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001654 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001655 print = bc_num_printDigits;
1656 }
1657
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001658 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001659 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001660
1661 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001662}
1663
Gavin Howard40a085f2018-12-03 12:08:59 -07001664#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001665BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1666 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001667}
1668#endif // DC_ENABLED
1669
Gavin Howard3c02ed32018-12-28 18:17:15 -07001670void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001671 assert(n);
1672 n->num = num;
1673 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001674 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001675 n->neg = false;
1676}
1677
Gavin Howard3c02ed32018-12-28 18:17:15 -07001678void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001679 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001680 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howard404ece72019-04-26 15:59:15 -06001681 bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001682}
1683
Gavin Howarded392aa2018-02-27 13:09:26 -07001684void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001685 assert(num);
1686 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001687}
1688
Gavin Howard3c02ed32018-12-28 18:17:15 -07001689void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001690 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001691 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001692 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001693 d->len = s->len;
1694 d->neg = s->neg;
1695 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001696 d->scale = s->scale;
Gavin Howard404ece72019-04-26 15:59:15 -06001697 memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -07001698}
1699
Gavin Howardbaa4f582019-01-24 13:56:35 -07001700void bc_num_createCopy(BcNum *d, const BcNum *s) {
1701 bc_num_init(d, s->len);
1702 bc_num_copy(d, s);
1703}
1704
1705void bc_num_createFromUlong(BcNum *n, unsigned long val) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001706 bc_num_init(n, (BC_NUM_LONG_LOG10 - 1) / BC_BASE_POWER + 1);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001707 bc_num_ulong2num(n, val);
1708}
1709
Gavin Howard7ad5a662019-02-19 14:40:46 -07001710size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001711 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001712}
1713
1714size_t bc_num_len(const BcNum *restrict n) {
1715
Gavin Howardb41483b2019-04-27 08:30:10 -06001716 size_t i, pow, scale, len = n->len;
Gavin Howardfcf29012019-04-25 20:12:19 -06001717 BcDig dig;
1718
Gavin Howardb41483b2019-04-27 08:30:10 -06001719 if (BC_NUM_ZERO(n)) return 0;
Gavin Howard08f78562019-04-29 07:56:04 -06001720 if (n->rdx == len) len = bc_num_nonzeroIdx(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001721
Gavin Howardfcf29012019-04-25 20:12:19 -06001722 dig = n->num[len - 1];
Gavin Howardb41483b2019-04-27 08:30:10 -06001723 pow = BC_BASE_DIG;
1724 i = BC_BASE_POWER + 1;
Gavin Howardfcf29012019-04-25 20:12:19 -06001725
Gavin Howardb41483b2019-04-27 08:30:10 -06001726 while (pow && (dig % (BcDig) pow == dig)) {
1727 i -= 1;
1728 pow /= BC_BASE;
1729 }
Gavin Howardfcf29012019-04-25 20:12:19 -06001730
Gavin Howardb41483b2019-04-27 08:30:10 -06001731 scale = n->scale % BC_BASE_POWER;
1732 scale = scale ? scale : BC_BASE_POWER;
1733
1734 return (len - 1) * BC_BASE_POWER + i - (BC_BASE_POWER - scale);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001735}
1736
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001737BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1738 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001739{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001740 BcStatus s = BC_STATUS_SUCCESS;
1741
Gavin Howard63738202018-09-26 15:34:20 -06001742 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001743 assert(BC_ENABLE_EXTRA_MATH ||
1744 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001745 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001746
Gavin Howardd746e802018-12-28 22:06:00 -07001747 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06001748 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001749 else s = bc_num_parseBase(n, val, base, base_t);
1750
1751 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001752}
1753
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001754BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1755 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001756{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001757 BcStatus s = BC_STATUS_SUCCESS;
1758
Gavin Howardab5e9632019-01-02 13:32:02 -07001759 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001760 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001761
Gavin Howardab5e9632019-01-02 13:32:02 -07001762 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001763
Gavin Howardddce6d42019-01-03 14:07:06 -07001764 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06001765 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001766#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001767 else if (base_t == 0 || base_t == 1)
1768 s = bc_num_printExponent(n, base_t != 0);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001769#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001770 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001771
Gavin Howarde5f11c72019-02-23 09:42:51 -07001772 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001773 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001774 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001775 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001776
1777 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001778}
1779
Gavin Howard3c02ed32018-12-28 18:17:15 -07001780BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001781
Gavin Howard63738202018-09-26 15:34:20 -06001782 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001783 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001784
Gavin Howard63738202018-09-26 15:34:20 -06001785 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001786
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001787 *result = 0;
1788
Gavin Howardecafd4f2019-02-23 09:30:45 -07001789 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001790
Gavin Howard06b6e862019-02-15 11:15:31 -07001791 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001792
Gavin Howard0700efc2019-04-24 17:31:23 -06001793 unsigned long prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07001794
Gavin Howard0700efc2019-04-24 17:31:23 -06001795 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001796 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1797
Gavin Howard77445432019-04-26 15:59:33 -06001798 r = prev + (unsigned long) n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07001799
1800 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001801 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001802 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001803
Gavin Howard7b557da2018-12-21 10:25:32 -07001804 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001805
Gavin Howard63738202018-09-26 15:34:20 -06001806 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001807}
1808
Gavin Howard3c02ed32018-12-28 18:17:15 -07001809void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001810
Gavin Howardc25fd612019-04-26 19:31:31 -06001811 BcDig *ptr;
1812 unsigned long i;
1813
Gavin Howard63738202018-09-26 15:34:20 -06001814 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001815
Gavin Howard63738202018-09-26 15:34:20 -06001816 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001817
Gavin Howard1ab22d22019-01-03 13:32:17 -07001818 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001819
Gavin Howard798508b2019-02-21 16:45:32 -07001820 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howard05feab02019-04-23 16:24:07 -06001821
Gavin Howardc25fd612019-04-26 19:31:31 -06001822 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_DIG)
1823 ptr[i] = val % BC_BASE_DIG;
Gavin Howard025d04d2018-02-20 13:53:28 -07001824}
1825
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001826size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001827
1828 size_t aint, bint, ardx, brdx;
1829
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001830 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001831
1832 ardx = a->rdx;
1833 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001834 aint = bc_num_int(a);
1835 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001836 ardx = BC_MAX(ardx, brdx);
1837 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001838
Gavin Howard2d188a52019-02-25 14:19:08 -07001839 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001840}
1841
1842size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001843 size_t max, rdx;
1844 rdx = bc_vm_growSize(a->rdx, b->rdx);
Stefan Eßer0dc37b62019-04-29 14:13:11 -06001845 max = bc_vm_growSize(BC_MAX(BC_NUM_RDX(scale), rdx), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001846 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1847 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001848}
1849
1850size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1851 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001852 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001853}
1854
1855#if BC_ENABLE_EXTRA_MATH
1856size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1857 BC_UNUSED(b);
1858 BC_UNUSED(scale);
1859 return BC_NUM_SHREQ(a);
1860}
1861#endif // BC_ENABLE_EXTRA_MATH
1862
Gavin Howard12fe7812018-09-29 03:45:18 -06001863BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001864 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001865 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001866 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001867}
1868
Gavin Howard12fe7812018-09-29 03:45:18 -06001869BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001870 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001871 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001872 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001873}
1874
Gavin Howard12fe7812018-09-29 03:45:18 -06001875BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001876 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001877}
1878
Gavin Howard12fe7812018-09-29 03:45:18 -06001879BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001880 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001881}
1882
Gavin Howard54b946a2018-10-23 12:06:57 -06001883BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001884 size_t req = bc_num_mulReq(a, b, scale);
1885 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001886}
1887
Gavin Howard12fe7812018-09-29 03:45:18 -06001888BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001889 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001890}
1891
Gavin Howard7d8f0382018-12-27 16:15:13 -07001892#if BC_ENABLE_EXTRA_MATH
1893BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001894 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001895}
1896
1897BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001898 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001899}
1900
1901BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001902 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001903}
1904#endif // BC_ENABLE_EXTRA_MATH
1905
Gavin Howard3c02ed32018-12-28 18:17:15 -07001906BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001907
Gavin Howardfcf29012019-04-25 20:12:19 -06001908 // TODO: Check this function.
1909
Gavin Howard1cbfe242019-01-09 17:13:11 -07001910 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001911 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Stefan Eßer4276af42019-04-29 14:04:57 -06001912 size_t pow, len, rdx, req, digs, digs1, digs2, resscale, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001913 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Stefan Eßer4276af42019-04-29 14:04:57 -06001914 BcDig half_digs[1];
Gavin Howard954276b2018-05-16 01:43:58 -06001915
Gavin Howard34edd0a2018-10-25 15:51:05 -06001916 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001917
Gavin Howardecafd4f2019-02-23 09:30:45 -07001918 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07001919
Stefan Eßer4276af42019-04-29 14:04:57 -06001920 if (a->scale > scale) scale = a->scale;
1921 len = bc_vm_growSize(bc_num_int_digits(a), 1);
1922 req = bc_vm_growSize(BC_MAX(BC_NUM_RDX(scale), a->rdx), len >> 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001923 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001924
Gavin Howardddce6d42019-01-03 14:07:06 -07001925 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001926 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001927 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001928 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001929 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001930 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001931 bc_num_extend(b, scale);
1932 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001933 }
Gavin Howard954276b2018-05-16 01:43:58 -06001934
Stefan Eßer4276af42019-04-29 14:04:57 -06001935 rdx = BC_MAX(BC_NUM_RDX(scale), a->rdx);
1936 len = bc_vm_growSize(a->len, rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06001937
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001938 bc_num_init(&num1, len);
1939 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001940 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001941
Gavin Howard63738202018-09-26 15:34:20 -06001942 bc_num_one(&half);
Stefan Eßer4276af42019-04-29 14:04:57 -06001943 half.num[0] = BC_BASE_DIG / 2;
1944 half.len = 1;
Gavin Howard63738202018-09-26 15:34:20 -06001945 half.rdx = 1;
Stefan Eßer4276af42019-04-29 14:04:57 -06001946 half.scale = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001947
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001948 bc_num_init(&f, len);
1949 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001950
Gavin Howard63738202018-09-26 15:34:20 -06001951 x0 = &num1;
1952 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001953
Gavin Howard63738202018-09-26 15:34:20 -06001954 bc_num_one(x0);
Stefan Eßer4276af42019-04-29 14:04:57 -06001955 pow = bc_num_int_digits(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001956
Gavin Howard53eba8b2018-10-31 15:14:37 -06001957 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001958
Gavin Howardc39fd492018-10-04 10:07:03 -06001959 if (pow & 1) x0->num[0] = 2;
1960 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001961
Gavin Howardc39fd492018-10-04 10:07:03 -06001962 pow -= 2 - (pow & 1);
Stefan Eßer4276af42019-04-29 14:04:57 -06001963 bc_num_shiftLeft(x0, pow / 2);
Gavin Howarded742ce2018-08-29 14:23:12 -06001964
Gavin Howard63738202018-09-26 15:34:20 -06001965 // Make sure to move the radix back.
Stefan Eßer4276af42019-04-29 14:04:57 -06001966 x0->scale -= pow;
1967 x0->rdx = BC_NUM_RDX(x0->scale);
Gavin Howard63738202018-09-26 15:34:20 -06001968 }
Gavin Howard954276b2018-05-16 01:43:58 -06001969
Gavin Howard14913fc2019-04-23 14:36:03 -06001970 x0->rdx = digs = digs1 = digs2 = 0;
Stefan Eßer4276af42019-04-29 14:04:57 -06001971 x0->scale = 0;
1972 resscale = (scale + BC_BASE_POWER) * 2;
1973
1974 len = BC_NUM_RDX(bc_num_int_digits(x0) + resscale - 1); // <se> apply -1 after BC_NUM_RDX???
Gavin Howard954276b2018-05-16 01:43:58 -06001975
Gavin Howard2d188a52019-02-25 14:19:08 -07001976 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001977
Gavin Howard971a2672019-04-26 14:32:07 -06001978 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07001979
Stefan Eßer4276af42019-04-29 14:04:57 -06001980 s = bc_num_div(a, x0, &f, resscale);
Gavin Howard1769abc2019-02-21 09:36:14 -07001981 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001982 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Eßer4276af42019-04-29 14:04:57 -06001983 s = bc_num_add(x0, &f, &fprime, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001984 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Eßer4276af42019-04-29 14:04:57 -06001985 s = bc_num_mul(&fprime, &half, x1, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001986 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001987
Gavin Howard53eba8b2018-10-31 15:14:37 -06001988 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06001989 if (cmp == BC_NUM_SSIZE_MIN) {
1990 s = BC_STATUS_SIGNAL;
1991 break;
1992 }
1993
Gavin Howard53eba8b2018-10-31 15:14:37 -06001994 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001995
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001996 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001997 else times = 0;
1998
Stefan Eßer4276af42019-04-29 14:04:57 -06001999 resscale += times > 2;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002000
2001 cmp2 = cmp1;
2002 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06002003 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002004
Gavin Howard63738202018-09-26 15:34:20 -06002005 temp = x0;
2006 x0 = x1;
2007 x1 = temp;
2008 }
Gavin Howard954276b2018-05-16 01:43:58 -06002009
Gavin Howard2d188a52019-02-25 14:19:08 -07002010 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07002011 s = BC_STATUS_SIGNAL;
2012 goto err;
2013 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06002014
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002015 bc_num_copy(b, x0);
Stefan Eßer4276af42019-04-29 14:04:57 -06002016 if (b->scale > scale) bc_num_truncate(b, b->scale - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06002017
2018err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06002019 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06002020 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06002021 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06002022 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06002023 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07002024 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002025 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06002026 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07002027}
Gavin Howardba009802018-09-29 04:41:51 -06002028
Gavin Howard2cb39612018-10-22 08:46:48 -06002029BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
2030
Gavin Howardfcf29012019-04-25 20:12:19 -06002031 // TODO: Check this function.
2032
Gavin Howard2cb39612018-10-22 08:46:48 -06002033 BcStatus s;
2034 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06002035 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07002036 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002037
Gavin Howardf2ef2f32019-01-16 11:25:16 -07002038 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06002039
Gavin Howard890d0c02018-10-30 16:34:50 -06002040 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06002041 memcpy(&num2, c, sizeof(BcNum));
2042 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002043 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06002044 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06002045 }
Gavin Howardf679ad82018-10-29 12:26:30 -06002046 else {
2047 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002048 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06002049 }
Gavin Howard2cb39612018-10-22 08:46:48 -06002050
Gavin Howard54b946a2018-10-23 12:06:57 -06002051 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002052
Gavin Howardddce6d42019-01-03 14:07:06 -07002053 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002054 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07002055 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002056 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06002057
2058 if (init) bc_num_free(&num2);
2059
2060 return s;
2061}
2062
Gavin Howard40a085f2018-12-03 12:08:59 -07002063#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06002064BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06002065
Gavin Howardfcf29012019-04-25 20:12:19 -06002066 // TODO: Check this function.
2067
Gavin Howardba009802018-09-29 04:41:51 -06002068 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002069 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07002070 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06002071
Gavin Howard34edd0a2018-10-25 15:51:05 -06002072 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06002073
Gavin Howardecafd4f2019-02-23 09:30:45 -07002074 if (BC_ERR(BC_NUM_ZERO(c)))
2075 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
2076 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
2077 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07002078 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06002079
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002080 bc_num_expand(d, c->len);
2081 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07002082 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002083 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06002084
2085 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06002086 two.num[0] = 2;
2087 bc_num_one(d);
2088
Gavin Howard1769abc2019-02-21 09:36:14 -07002089 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002090 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002091 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002092 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07002093 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06002094
Gavin Howard2d188a52019-02-25 14:19:08 -07002095 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06002096
Gavin Howard1769abc2019-02-21 09:36:14 -07002097 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002098 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002099 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002100
Gavin Howarda83a5e52019-02-21 16:51:06 -07002101 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06002102 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002103 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002104
2105 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002106 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002107 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002108 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002109 }
2110
Gavin Howard53eba8b2018-10-31 15:14:37 -06002111 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002112 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002113
2114 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002115 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002116 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002117 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002118 }
2119
Gavin Howard2d188a52019-02-25 14:19:08 -07002120 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002121
Gavin Howardba009802018-09-29 04:41:51 -06002122err:
Gavin Howardba009802018-09-29 04:41:51 -06002123 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002124rem_err:
2125 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002126 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002127 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002128 return s;
2129}
Gavin Howarde6e84762018-10-03 11:46:34 -06002130#endif // DC_ENABLED