blob: 3fee2c54675b1caf4dd4a43dc2927dcbebde3ed9 [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
51static void bc_num_printDebug(const BcNum *n, const char* name, bool emptynl) {
52 printf("%s: ", name);
53 bc_num_printDecimal(n);
54 printf("\n");
55 if (emptynl) printf("\n");
56 vm->nchars = 0;
57}
Stefan Eßer4b111c22019-04-24 17:23:08 -060058
Gavin Howardaf107d92019-04-25 19:35:41 -060059static void bc_num_dump(const BcNum *n, const char *c) {
Stefan Eßer4b111c22019-04-24 17:23:08 -060060
Gavin Howardc4783ac2019-04-26 10:40:02 -060061 unsigned long i;
Stefan Eßer4b111c22019-04-24 17:23:08 -060062
63 fprintf(stderr, "\n%s=", c);
64
Gavin Howardc4783ac2019-04-26 10:40:02 -060065 for (i = n->len -1; i < n->len; i--) {
66 if (i + 1 == n->rdx)
Stefan Eßer4b111c22019-04-24 17:23:08 -060067 fprintf(stderr, ".");
68 fprintf(stderr, "%09d ", n->num[i]);
69 }
70
Gavin Howardc4783ac2019-04-26 10:40:02 -060071 fprintf(stderr, "(%p|%zu/%zu)\n", (void*) n->num, n->len, n->cap);
Stefan Eßer4b111c22019-04-24 17:23:08 -060072}
Gavin Howard55c8f732019-04-24 14:05:27 -060073#endif // BC_DEBUG_CODE
74
Gavin Howard49ea7b62019-04-23 14:46:26 -060075static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
76
Gavin Howard56ebcf72019-02-21 16:59:48 -070077static ssize_t bc_num_neg(size_t n, bool neg) {
78 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
79}
80
Gavin Howardd2cf06a2019-02-21 17:03:24 -070081ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -070082 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -070083}
84
Gavin Howard7b7d2f32019-02-21 16:46:47 -070085static size_t bc_num_int(const BcNum *n) {
86 return n->len ? n->len - n->rdx : 0;
87}
88
Gavin Howarda83a5e52019-02-21 16:51:06 -070089static bool bc_num_isOne(const BcNum *n) {
90 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
91}
92
Gavin Howard798508b2019-02-21 16:45:32 -070093static void bc_num_expand(BcNum *restrict n, size_t req) {
94 assert(n);
95 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
96 if (req > n->cap) {
Stefan Esser5fd43a32019-04-25 07:05:24 +020097 n->num = bc_vm_realloc_digs(n->num, req);
Gavin Howard798508b2019-02-21 16:45:32 -070098 n->cap = req;
99 }
100}
101
Gavin Howard1cbfe242019-01-09 17:13:11 -0700102static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600103 assert(n);
Gavin Howardfcf29012019-04-25 20:12:19 -0600104 n->scale = scale;
105 n->len = n->rdx = 0;
Gavin Howard97102b12018-11-01 23:37:31 -0600106 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600107}
108
Gavin Howard1cbfe242019-01-09 17:13:11 -0700109static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600110 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -0600111}
112
Gavin Howard3c02ed32018-12-28 18:17:15 -0700113void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600114 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -0600115 n->len = 1;
116 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -0600117}
118
Gavin Howard3c02ed32018-12-28 18:17:15 -0700119void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600120 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600121 bc_num_setToZero(n, 0);
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600122#if BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600123 n->len = 2;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600124 n->num[0] = 0;
125 n->num[1] = 1;
126#else // BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600127 n->len = 1;
Stefan Eßer4b111c22019-04-24 17:23:08 -0600128 n->num[0] = BC_BASE;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600129#endif // BC_BASE_DIG == 10
Gavin Howard0be26ed2018-08-31 20:21:56 -0600130}
131
Stefan Esser394a5162019-04-25 06:33:10 +0200132static void bc_num_set(BcDig *restrict dst, const size_t val,
133 const size_t n)
134{
Gavin Howardf49fdaa2019-04-25 19:43:49 -0600135 memset(dst, val, n * sizeof(BcDig));
Stefan Esser394a5162019-04-25 06:33:10 +0200136}
137
138static void bc_num_cpy(BcDig *restrict dst, const BcDig *restrict src,
139 const size_t n)
140{
Gavin Howardf49fdaa2019-04-25 19:43:49 -0600141 memcpy(dst, src, n * sizeof(BcDig));
Stefan Esser394a5162019-04-25 06:33:10 +0200142}
143
144static void bc_num_move(BcDig *restrict dst, const BcDig *restrict src,
145 const size_t n)
146{
Gavin Howardf49fdaa2019-04-25 19:43:49 -0600147 memmove(dst, src, n * sizeof(BcDig));
Stefan Esser394a5162019-04-25 06:33:10 +0200148}
149
Gavin Howard1cbfe242019-01-09 17:13:11 -0700150static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -0700151 size_t len;
Gavin Howard530e3072019-04-23 16:23:36 -0600152 for (len = 1; i; i /= BC_BASE, ++len);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700153 return len;
154}
155
Gavin Howard971a2672019-04-26 14:32:07 -0600156static unsigned long bc_num_pow10(unsigned long i) {
157 unsigned long pow;
158 for (pow = 1; i; --i, pow *= BC_BASE);
159 return pow;
160}
161
Gavin Howardfcf29012019-04-25 20:12:19 -0600162static unsigned long bc_num_addDigit(BcDig *restrict num, unsigned long d,
163 unsigned long c)
Gavin Howard14c354a2019-04-24 14:28:24 -0600164{
165 d += c;
Gavin Howard0700efc2019-04-24 17:31:23 -0600166 *num = (BcDig) (d % BC_BASE_DIG);
167 assert(*num >= 0 && *num < BC_BASE_DIG);
168 return d / BC_BASE_DIG;
Gavin Howard14c354a2019-04-24 14:28:24 -0600169}
170
171static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
172 size_t len)
173{
Gavin Howardfcf29012019-04-25 20:12:19 -0600174 // TODO: Check this function.
Gavin Howard14c354a2019-04-24 14:28:24 -0600175 size_t i;
Gavin Howard971a2672019-04-26 14:32:07 -0600176 unsigned long carry = 0;
Gavin Howard14c354a2019-04-24 14:28:24 -0600177
178 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard971a2672019-04-26 14:32:07 -0600179 unsigned long in = ((unsigned long) a[i]) + ((unsigned long) b[i]);
Gavin Howard14c354a2019-04-24 14:28:24 -0600180 carry = bc_num_addDigit(a + i, in, carry);
181 }
182
183 for (; BC_NO_SIG && carry; ++i)
Gavin Howard971a2672019-04-26 14:32:07 -0600184 carry = bc_num_addDigit(a + i, (unsigned long) a[i], carry);
Gavin Howard14c354a2019-04-24 14:28:24 -0600185
186 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
187}
188
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700189static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
190 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700191{
Gavin Howardfcf29012019-04-25 20:12:19 -0600192 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -0600193 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600194
Gavin Howard2d188a52019-02-25 14:19:08 -0700195 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600196
Gavin Howard2d188a52019-02-25 14:19:08 -0700197 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600198 assert(a[i + j] >= -BC_BASE_DIG);
199 a[i + j++] += BC_BASE_DIG;
Gavin Howard54b946a2018-10-23 12:06:57 -0600200 a[i + j] -= 1;
Gavin Howard0700efc2019-04-24 17:31:23 -0600201 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_DIG);
Gavin Howard63738202018-09-26 15:34:20 -0600202 }
203 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600204
Gavin Howard2d188a52019-02-25 14:19:08 -0700205 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600206}
207
Gavin Howard2d188a52019-02-25 14:19:08 -0700208static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
209 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700210{
Gavin Howard63738202018-09-26 15:34:20 -0600211 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600212 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700213 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600214 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600215}
216
Gavin Howard3c02ed32018-12-28 18:17:15 -0700217ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700218
Gavin Howard63738202018-09-26 15:34:20 -0600219 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600220 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600221 bool a_max, neg = false;
222 ssize_t cmp;
223
224 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700225
Gavin Howard8a921bd2018-10-11 14:15:32 -0600226 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700227 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700228 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600229 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600230 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600231 else return -1;
232 }
233 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700234
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700235 a_int = bc_num_int(a);
236 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600237 a_int -= b_int;
238 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700239
Gavin Howard1ab22d22019-01-03 13:32:17 -0700240 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700241
Gavin Howard890d0c02018-10-30 16:34:50 -0600242 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600243 min = b->rdx;
244 diff = a->rdx - b->rdx;
245 max_num = a->num + diff;
246 min_num = b->num;
247 }
248 else {
249 min = a->rdx;
250 diff = b->rdx - a->rdx;
251 max_num = b->num + diff;
252 min_num = a->num;
253 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700254
Gavin Howard890d0c02018-10-30 16:34:50 -0600255 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600256 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700257 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700258
Gavin Howard2d188a52019-02-25 14:19:08 -0700259 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700260 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600261 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700262
Gavin Howardf2ee6342019-04-09 17:05:20 -0600263 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700264}
265
Gavin Howard2a366922019-01-16 10:50:20 -0700266static void bc_num_clean(BcNum *restrict n) {
267 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
268 if (BC_NUM_ZERO(n)) n->neg = false;
269 else if (n->len < n->rdx) n->len = n->rdx;
270}
271
Gavin Howard3c02ed32018-12-28 18:17:15 -0700272void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700273
Gavin Howardb1558662019-04-26 14:32:55 -0600274 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700275
Gavin Howard1ab22d22019-01-03 13:32:17 -0700276 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700277
Gavin Howardb1558662019-04-26 14:32:55 -0600278 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
279 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
280
281 n->scale -= places;
282 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700283
Gavin Howardddce6d42019-01-03 14:07:06 -0700284 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600285
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600286 size_t pow;
287
288 pow = n->scale % BC_BASE_POWER;
289 pow = pow ? BC_BASE_POWER - pow : 0;
290 pow = bc_num_pow10(pow);
Gavin Howardb1558662019-04-26 14:32:55 -0600291
292 n->len -= places_rdx;
293 bc_num_move(n->num, n->num + places_rdx, n->len);
294
295 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600296 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600297
Gavin Howard2a366922019-01-16 10:50:20 -0700298 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600299 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700300}
301
Gavin Howard1cbfe242019-01-09 17:13:11 -0700302static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700303
Gavin Howardb1558662019-04-26 14:32:55 -0600304 size_t places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700305
Gavin Howard1ab22d22019-01-03 13:32:17 -0700306 if (!places) return;
Gavin Howarde6f326e2019-04-26 10:13:45 -0600307
Gavin Howardb1558662019-04-26 14:32:55 -0600308 bc_num_expand(n, n->len + places_rdx);
Gavin Howarde6f326e2019-04-26 10:13:45 -0600309 bc_num_move(n->num + places_rdx, n->num, n->len);
310 bc_num_set(n->num, 0, places_rdx);
Gavin Howarde6f326e2019-04-26 10:13:45 -0600311 n->rdx += places_rdx;
312 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600313 n->len += places_rdx;
Gavin Howarde6f326e2019-04-26 10:13:45 -0600314
315 assert(n->rdx == BC_NUM_RDX(n->scale));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700316}
317
Gavin Howard2d188a52019-02-25 14:19:08 -0700318static void bc_num_retireMul(BcNum *restrict n, size_t scale,
319 bool neg1, bool neg2)
320{
Gavin Howardfcf29012019-04-25 20:12:19 -0600321 // TODO: Check this function.
Gavin Howard971a2672019-04-26 14:32:07 -0600322 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
323 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600324
Gavin Howard305249a2018-10-15 20:24:47 -0600325 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700326 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600327}
328
Gavin Howard2d188a52019-02-25 14:19:08 -0700329static void bc_num_split(const BcNum *restrict n, size_t idx,
330 BcNum *restrict a, BcNum *restrict b)
331{
Gavin Howardfcf29012019-04-25 20:12:19 -0600332 // TODO: Check this function.
Gavin Howard305249a2018-10-15 20:24:47 -0600333 if (idx < n->len) {
334
Gavin Howard305249a2018-10-15 20:24:47 -0600335 b->len = n->len - idx;
336 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600337 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600338
Stefan Esser394a5162019-04-25 06:33:10 +0200339 bc_num_cpy(b->num, n->num + idx, b->len);
340 bc_num_cpy(a->num, n->num, idx);
Gavin Howard50c80022019-01-03 15:14:33 -0700341
342 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600343 }
Gavin Howard50c80022019-01-03 15:14:33 -0700344 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600345
346 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600347}
348
Gavin Howard14913fc2019-04-23 14:36:03 -0600349static size_t bc_num_shiftZero(BcNum *restrict n) {
350
351 size_t i;
352
353 assert(!n->rdx || BC_NUM_ZERO(n));
354
355 for (i = 0; i < n->len && !n->num[i]; ++i);
356
357 n->len -= i;
358 n->num += i;
359
360 return i;
361}
362
363static void bc_num_unshiftZero(BcNum *restrict n, size_t places) {
364 n->len += places;
365 n->num -= places;
366}
367
Gavin Howard48a0e472019-04-26 15:39:46 -0600368static BcStatus bc_num_shift(BcNum *restrict n, BcDig *restrict ptr,
369 unsigned long dig, size_t len)
370{
Gavin Howardb1558662019-04-26 14:32:55 -0600371 size_t i;
372 unsigned long carry = 0;
Gavin Howardfcf29012019-04-25 20:12:19 -0600373
Gavin Howardb1558662019-04-26 14:32:55 -0600374 assert(dig < BC_BASE_POWER);
Gavin Howard305249a2018-10-15 20:24:47 -0600375
Gavin Howardb1558662019-04-26 14:32:55 -0600376 dig = bc_num_pow10(dig);
377
378 for (i = 0; BC_NO_SIG && i < len; ++i) {
379 unsigned long in = ((unsigned long) ptr[i]) * dig;
380 carry = bc_num_addDigit(ptr + i, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600381 }
382
Gavin Howardb1558662019-04-26 14:32:55 -0600383 if (carry) {
384 bc_num_expand(n, n->len + 1);
385 assert(carry < BC_BASE_DIG);
386 n->num[n->len] = (BcDig) carry;
387 n->len += 1;
388 }
389
390 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600391}
392
Gavin Howardb1558662019-04-26 14:32:55 -0600393static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700394
Gavin Howardb1558662019-04-26 14:32:55 -0600395 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600396 unsigned long dig;
397 bool shift;
398 size_t places_rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -0600399
Gavin Howardb1558662019-04-26 14:32:55 -0600400 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600401
Gavin Howard48a0e472019-04-26 15:39:46 -0600402 dig = (unsigned long) (places % BC_BASE_POWER);
403 shift = (dig != 0);
404 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx - shift;
405
Gavin Howardb1558662019-04-26 14:32:55 -0600406 if (n->scale >= places) {
407 n->scale -= places;
408 n->rdx = BC_NUM_RDX(n->scale);
409 }
410 else if (BC_NUM_NONZERO(n)) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600411
412 if (places_rdx != n->rdx) {
413 bc_num_expand(n, n->len + places_rdx - n->rdx + shift);
414 bc_num_move(n->num + places_rdx - n->rdx, n->num, n->len);
415 bc_num_set(n->num, 0, places_rdx - n->rdx);
416 n->len += places_rdx;
417 }
418
Gavin Howardb1558662019-04-26 14:32:55 -0600419 n->scale = n->rdx = 0;
420 }
421 else n->scale = 0;
422
423 if (shift) s = bc_num_shift(n, n->num + places_rdx, dig, n->len - places_rdx);
424
425 bc_num_clean(n);
426
427 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
428}
429
430static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
431
432 BcStatus s = BC_STATUS_SUCCESS;
433 unsigned long dig = (unsigned long) (places % BC_BASE_POWER);
434 bool shift = (dig != 0);
435 size_t len, places_rdx;
436
437 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600438 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600439 n->scale += places;
440 bc_num_expand(n, BC_NUM_RDX(n->scale));
441 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600442 }
443
Gavin Howardb1558662019-04-26 14:32:55 -0600444 places_rdx = BC_NUM_RDX(places);
445 len = bc_vm_growSize(n->len, places_rdx);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700446
447 if (len > n->len) {
448
449 if (len > n->cap) bc_num_expand(n, len);
450
Stefan Esser394a5162019-04-25 06:33:10 +0200451 bc_num_set(n->num + n->len, 0, len - n->len);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700452 n->len = len;
453 }
454
Gavin Howardb1558662019-04-26 14:32:55 -0600455 n->scale += places;
456 n->rdx += places_rdx;
457
458 if (shift) s = bc_num_shift(n, n->num, BC_BASE_POWER - dig, n->len);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700459
460 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600461 assert(n->rdx == BC_NUM_RDX(n->scale));
462
463 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700464}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700465
Gavin Howard1cbfe242019-01-09 17:13:11 -0700466static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600467
Gavin Howard63738202018-09-26 15:34:20 -0600468 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600469 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600470
Gavin Howard971a2672019-04-26 14:32:07 -0600471 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700472
Gavin Howardd32d7df2018-10-15 08:42:25 -0600473 one.cap = 2;
474 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600475 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600476
Gavin Howardd32d7df2018-10-15 08:42:25 -0600477 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600478}
479
Gavin Howard7bda4782018-12-28 09:53:22 -0700480#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700481static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
482 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700483{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700484 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700485 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700486 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700487}
Gavin Howard7bda4782018-12-28 09:53:22 -0700488#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700489
Gavin Howard1cbfe242019-01-09 17:13:11 -0700490static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700491
Gavin Howard8a921bd2018-10-11 14:15:32 -0600492 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600493 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard6fccd222019-04-26 10:17:02 -0600494 unsigned long carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700495
Gavin Howard63738202018-09-26 15:34:20 -0600496 // Because this function doesn't need to use scale (per the bc spec),
497 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600498
Gavin Howardddce6d42019-01-03 14:07:06 -0700499 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600500 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700501 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600502 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600503 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700504 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600505 bc_num_copy(c, a);
506 return BC_STATUS_SUCCESS;
507 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600508
Gavin Howard63738202018-09-26 15:34:20 -0600509 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600510 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600511 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600512 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700513
Gavin Howard63738202018-09-26 15:34:20 -0600514 if (a->rdx > b->rdx) {
515 diff = a->rdx - b->rdx;
516 ptr = a->num;
517 ptr_a = a->num + diff;
518 ptr_b = b->num;
519 }
520 else {
521 diff = b->rdx - a->rdx;
522 ptr = b->num;
523 ptr_a = a->num;
524 ptr_b = b->num + diff;
525 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700526
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700527 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700528
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700529 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600530 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700531 a_int = bc_num_int(a);
532 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700533
Gavin Howard890d0c02018-10-30 16:34:50 -0600534 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600535 min_int = b_int;
536 max = a_int;
537 ptr = ptr_a;
538 }
539 else {
540 min_int = a_int;
541 max = b_int;
542 ptr = ptr_b;
543 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700544
Gavin Howard2d188a52019-02-25 14:19:08 -0700545 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard6fccd222019-04-26 10:17:02 -0600546 unsigned long in;
547 in = ((unsigned long) ptr_a[i]) + ((unsigned long) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700548 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600549 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700550
Gavin Howard2d188a52019-02-25 14:19:08 -0700551 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard6fccd222019-04-26 10:17:02 -0600552 carry = bc_num_addDigit(ptr_c + i, (unsigned long) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700553
554 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700555
Gavin Howard1ab22d22019-01-03 13:32:17 -0700556 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600557
Gavin Howard2d188a52019-02-25 14:19:08 -0700558 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700559}
560
Gavin Howard1cbfe242019-01-09 17:13:11 -0700561static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700562
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700563 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600564 ssize_t cmp;
565 BcNum *minuend, *subtrahend;
566 size_t start;
567 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700568
Gavin Howard63738202018-09-26 15:34:20 -0600569 // Because this function doesn't need to use scale (per the bc spec),
570 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700571
Gavin Howardddce6d42019-01-03 14:07:06 -0700572 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600573 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700574 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600575 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600576 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700577 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600578 bc_num_copy(c, a);
579 return BC_STATUS_SUCCESS;
580 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700581
Gavin Howard63738202018-09-26 15:34:20 -0600582 aneg = a->neg;
583 bneg = b->neg;
584 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700585
Gavin Howard63738202018-09-26 15:34:20 -0600586 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600587 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700588
Gavin Howard63738202018-09-26 15:34:20 -0600589 a->neg = aneg;
590 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700591
Gavin Howard1ab22d22019-01-03 13:32:17 -0700592 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600593 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600594 return BC_STATUS_SUCCESS;
595 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700596
597 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600598 neg = a->neg;
599 minuend = a;
600 subtrahend = b;
601 }
602 else {
603 neg = b->neg;
604 if (sub) neg = !neg;
605 minuend = b;
606 subtrahend = a;
607 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700608
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600609 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600610 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700611
Gavin Howarde6f326e2019-04-26 10:13:45 -0600612 if (c->scale < subtrahend->scale) {
613 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600614 start = 0;
615 }
616 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700617
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700618 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700619
Gavin Howard63738202018-09-26 15:34:20 -0600620 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700621
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700622 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700623}
624
Gavin Howarddc4e3712019-04-23 14:56:29 -0600625static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
626{
Gavin Howardfcf29012019-04-25 20:12:19 -0600627 // TODO: Check this function.
Gavin Howard971a2672019-04-26 14:32:07 -0600628 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600629 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard971a2672019-04-26 14:32:07 -0600630 unsigned long sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600631
Gavin Howard971a2672019-04-26 14:32:07 -0600632 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600633 assert(!a->rdx && !b->rdx);
634
Gavin Howard5083aec2019-04-23 18:03:46 -0600635 clen = bc_vm_growSize(alen, blen);
636 bc_num_expand(c, clen + 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600637
638 ptr_c = c->num;
Stefan Esser394a5162019-04-25 06:33:10 +0200639 bc_num_set(ptr_c, 0, c->cap);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600640
Gavin Howard5083aec2019-04-23 18:03:46 -0600641 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600642
Gavin Howard5083aec2019-04-23 18:03:46 -0600643 ssize_t sidx = (ssize_t) (i - blen + 1);
644 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600645
Stefan Eßer4b111c22019-04-24 17:23:08 -0600646 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
647
Gavin Howard971a2672019-04-26 14:32:07 -0600648 sum += ((unsigned long) ptr_a[j]) * ((unsigned long) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600649
Gavin Howard0700efc2019-04-24 17:31:23 -0600650 if (sum >= BC_BASE_DIG) {
651 carry += sum / BC_BASE_DIG;
652 sum %= BC_BASE_DIG;
Stefan Eßer4b111c22019-04-24 17:23:08 -0600653 }
654 }
655
656 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600657 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600658 sum = carry;
659 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600660 }
661
Gavin Howard5083aec2019-04-23 18:03:46 -0600662 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600663 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600664 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600665 clen += 1;
666 }
667
668 c->len = clen;
669
Gavin Howarddc4e3712019-04-23 14:56:29 -0600670 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
671}
672
Gavin Howard14c354a2019-04-24 14:28:24 -0600673static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
674 size_t shift, BcNumShiftAddOp op)
675{
Gavin Howardfcf29012019-04-25 20:12:19 -0600676 // TODO: Check this function.
Gavin Howard14c354a2019-04-24 14:28:24 -0600677 assert(n->len >= shift + a->len);
678 assert(!n->rdx && !a->rdx);
679 return op(n->num + shift, a->num, a->len);
680}
681
682static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600683
Gavin Howardfcf29012019-04-25 20:12:19 -0600684 // TODO: Check this function.
685
Gavin Howard9b801632019-02-16 23:34:06 -0700686 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200687 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600688 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700689 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600690 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700691 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600692
Gavin Howard14913fc2019-04-23 14:36:03 -0600693 assert(BC_NUM_ZERO(c));
694
Gavin Howardddce6d42019-01-03 14:07:06 -0700695 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600696 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700697 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600698 bc_num_zero(c);
699 return BC_STATUS_SUCCESS;
700 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700701 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600702 bc_num_copy(c, aone ? b : a);
703 return BC_STATUS_SUCCESS;
704 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700705
Gavin Howardb5093d72018-10-17 12:13:17 -0600706 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
707 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600708 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600709 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600710 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700711
Gavin Howarde0432502019-02-26 16:23:45 -0700712 max = BC_MAX(a->len, b->len);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600713 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700714 max2 = (max + 1) / 2;
715
Stefan Esser5fd43a32019-04-25 07:05:24 +0200716 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
717 digs = dig_ptr = bc_vm_malloc_digs(total);
Gavin Howard5bede412019-02-23 09:31:28 -0700718
719 bc_num_setup(&l1, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600720 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700721 bc_num_setup(&h1, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600722 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700723 bc_num_setup(&l2, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600724 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700725 bc_num_setup(&h2, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600726 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700727 bc_num_setup(&m1, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600728 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700729 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600730 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600731 bc_num_init(&z0, max);
732 bc_num_init(&z1, max);
733 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600734 max = bc_vm_growSize(max, max) + 1;
735 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600736
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600737 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600738 bc_num_clean(&l1);
739 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600740 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600741 bc_num_clean(&l2);
742 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700743
Gavin Howard14c354a2019-04-24 14:28:24 -0600744 bc_num_expand(c, max);
745 c->len = max;
Stefan Esser887d6b92019-04-25 07:56:10 +0200746 bc_num_set(c->num, 0, c->len);
Gavin Howard305249a2018-10-15 20:24:47 -0600747
Gavin Howard14c354a2019-04-24 14:28:24 -0600748 s = bc_num_sub(&h1, &l1, &m1, 0);
749 if (BC_ERR(s)) goto err;
750 s = bc_num_sub(&l2, &h2, &m2, 0);
751 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600752
Gavin Howard971a2672019-04-26 14:32:07 -0600753 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600754
Gavin Howard14c354a2019-04-24 14:28:24 -0600755 s = bc_num_m(&h1, &h2, &z2, 0);
756 if (BC_ERR(s)) goto err;
757 bc_num_clean(&z2);
758
759 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
760 if (BC_ERR(s)) goto err;
761 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
762 if (BC_ERR(s)) goto err;
763 }
764
Gavin Howard971a2672019-04-26 14:32:07 -0600765 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600766
767 s = bc_num_m(&l1, &l2, &z0, 0);
768 if (BC_ERR(s)) goto err;
769 bc_num_clean(&z0);
770
771 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
772 if (BC_ERR(s)) goto err;
773 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
774 if (BC_ERR(s)) goto err;
775 }
776
Gavin Howard971a2672019-04-26 14:32:07 -0600777 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600778
779 s = bc_num_m(&m1, &m2, &z1, 0);
780 if (BC_ERR(s)) goto err;
781 bc_num_clean(&z1);
782
783 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
784 s = bc_num_shiftAddSub(c, &z1, max2, op);
785 if (BC_ERR(s)) goto err;
786 }
Gavin Howard305249a2018-10-15 20:24:47 -0600787
788err:
Gavin Howard5bede412019-02-23 09:31:28 -0700789 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600790 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600791 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600792 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600793 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600794 return s;
795}
796
Gavin Howard1cbfe242019-01-09 17:13:11 -0700797static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600798
Gavin Howardfcf29012019-04-25 20:12:19 -0600799 // TODO: Check this function.
800
Gavin Howard305249a2018-10-15 20:24:47 -0600801 BcStatus s;
802 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600803 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600804
Gavin Howard14913fc2019-04-23 14:36:03 -0600805 bc_num_setToZero(c, 0);
806
Gavin Howard48a0e472019-04-26 15:39:46 -0600807 ascale = a->scale;
808 bscale = b->scale;
809 scale = BC_MAX(scale, ascale);
810 scale = BC_MAX(scale, bscale);
811 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600812 scale = BC_MIN(rscale, scale);
Gavin Howard305249a2018-10-15 20:24:47 -0600813
Gavin Howardbaa4f582019-01-24 13:56:35 -0700814 bc_num_createCopy(&cpa, a);
815 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600816
Gavin Howard23d8d252018-10-17 12:03:41 -0600817 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600818
Gavin Howard48a0e472019-04-26 15:39:46 -0600819 ardx = cpa.rdx * BC_BASE_POWER;
820 s = bc_num_shiftLeft(&cpa, ardx);
821 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600822 bc_num_clean(&cpa);
823 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600824
825 brdx = cpb.rdx * BC_BASE_POWER;
826 s = bc_num_shiftLeft(&cpb, brdx);
827 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600828 bzero = bc_num_shiftZero(&cpb);
829 bc_num_clean(&cpb);
830
Gavin Howard14c354a2019-04-24 14:28:24 -0600831 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700832 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600833
Gavin Howard14913fc2019-04-23 14:36:03 -0600834 zero = bc_vm_growSize(azero, bzero);
835 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600836
Gavin Howard14913fc2019-04-23 14:36:03 -0600837 bc_num_expand(c, len);
Gavin Howard48a0e472019-04-26 15:39:46 -0600838 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_POWER);
839 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
840 s = bc_num_shiftRight(c, ardx + brdx);
841 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600842
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600843 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600844
845err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600846 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600847 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600848 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600849 bc_num_free(&cpa);
850 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700851}
852
Gavin Howard1cbfe242019-01-09 17:13:11 -0700853static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700854
Gavin Howardfcf29012019-04-25 20:12:19 -0600855 // TODO: Check this function.
856
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600857 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600858 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600859 size_t len, end, i;
860 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600861 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700862
Gavin Howardddce6d42019-01-03 14:07:06 -0700863 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
864 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600865 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600866 return BC_STATUS_SUCCESS;
867 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700868 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600869 bc_num_copy(c, a);
870 bc_num_retireMul(c, scale, a->neg, b->neg);
871 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600872 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700873
Gavin Howard620a8af2019-01-24 13:54:54 -0700874 bc_num_init(&cp, bc_num_mulReq(a, b, scale));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600875 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600876 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700877
Gavin Howard890d0c02018-10-30 16:34:50 -0600878 if (len > cp.len) {
Gavin Howard007afdf2019-02-23 09:45:42 -0700879 bc_num_expand(&cp, bc_vm_growSize(len, 2));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600880 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600881 }
Gavin Howard021150b2018-03-10 15:40:42 -0700882
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600883 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600884 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600885 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700886
Gavin Howard63738202018-09-26 15:34:20 -0600887 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600888 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600889 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600890 len -= i - 1;
891 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700892
Gavin Howard007afdf2019-02-23 09:45:42 -0700893 if (cp.cap == cp.len) bc_num_expand(&cp, bc_vm_growSize(cp.len, 1));
Gavin Howardb651f1a2018-02-28 17:34:18 -0700894
Gavin Howard63738202018-09-26 15:34:20 -0600895 // We want an extra zero in front to make things simpler.
896 cp.num[cp.len++] = 0;
897 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700898
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600899 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700900
Stefan Esser394a5162019-04-25 06:33:10 +0200901 bc_num_set(c->num + end, 0, c->cap - end);
Gavin Howard63738202018-09-26 15:34:20 -0600902 c->rdx = cp.rdx;
903 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600904 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700905
Gavin Howard2d188a52019-02-25 14:19:08 -0700906 for (i = end - 1; BC_NO_SIG && BC_NO_ERR(!s) && i < end; --i) {
Gavin Howard5a788392019-03-28 11:01:13 -0600907
Gavin Howard63738202018-09-26 15:34:20 -0600908 n = cp.num + i;
Gavin Howardecafd4f2019-02-23 09:30:45 -0700909 q = 0;
Gavin Howard5a788392019-03-28 11:01:13 -0600910
911 while (BC_NO_SIG && BC_NO_ERR(!s) &&
912 (n[len] || bc_num_compare(n, p, len) >= 0))
913 {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700914 s = bc_num_subArrays(n, p, len);
Gavin Howard5a788392019-03-28 11:01:13 -0600915 q += 1;
916 }
917
Gavin Howard63738202018-09-26 15:34:20 -0600918 c->num[i] = q;
919 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700920
Gavin Howardf2ee6342019-04-09 17:05:20 -0600921 if (BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howarde5f11c72019-02-23 09:42:51 -0700922 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600923 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600924
Gavin Howard63738202018-09-26 15:34:20 -0600925 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700926}
927
Gavin Howard1cbfe242019-01-09 17:13:11 -0700928static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
929 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600930{
Gavin Howardfcf29012019-04-25 20:12:19 -0600931 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -0600932 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600933 BcNum temp;
934 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700935
Gavin Howardddce6d42019-01-03 14:07:06 -0700936 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
937 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700938 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600939 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600940 return BC_STATUS_SUCCESS;
941 }
Gavin Howard8b254872018-03-14 01:13:35 -0600942
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600943 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -0700944 s = bc_num_d(a, b, c, scale);
945 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -0700946 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700947
Gavin Howard1ab22d22019-01-03 13:32:17 -0700948 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600949
Gavin Howard53eba8b2018-10-31 15:14:37 -0600950 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700951 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600952 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700953 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600954
Gavin Howardddce6d42019-01-03 14:07:06 -0700955 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600956
957 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600958 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600959 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700960
961err:
Gavin Howard18441082018-10-22 10:03:30 -0600962 bc_num_free(&temp);
963 return s;
964}
965
Gavin Howard2d188a52019-02-25 14:19:08 -0700966static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
967{
Gavin Howardfcf29012019-04-25 20:12:19 -0600968 // TODO: Check this function.
Gavin Howard18441082018-10-22 10:03:30 -0600969 BcStatus s;
970 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -0700971 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -0600972
Gavin Howard007afdf2019-02-23 09:45:42 -0700973 ts = bc_vm_growSize(scale, b->rdx);
Gavin Howard798508b2019-02-21 16:45:32 -0700974 ts = BC_MAX(ts, a->rdx);
975
976 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -0600977 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600978 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600979
Gavin Howard63738202018-09-26 15:34:20 -0600980 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700981}
982
Gavin Howard1cbfe242019-01-09 17:13:11 -0700983static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700984
Gavin Howardfcf29012019-04-25 20:12:19 -0600985 // TODO: Check this function.
986
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600987 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600988 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700989 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600990 size_t i, powrdx, resrdx;
991 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700992
Gavin Howardecafd4f2019-02-23 09:30:45 -0700993 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700994
Gavin Howardddce6d42019-01-03 14:07:06 -0700995 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600996 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600997 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600998 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700999 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001000 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001001 return BC_STATUS_SUCCESS;
1002 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001003 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001004 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -06001005 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001006 return s;
1007 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001008
Gavin Howard63738202018-09-26 15:34:20 -06001009 neg = b->neg;
1010 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001011 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -07001012 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001013 if (s) return s;
1014
Gavin Howardbaa4f582019-01-24 13:56:35 -07001015 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001016
Gavin Howard63738202018-09-26 15:34:20 -06001017 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -06001018
Gavin Howard2d188a52019-02-25 14:19:08 -07001019 for (powrdx = a->rdx; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -06001020 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001021 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001022 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001023 }
Gavin Howard6fb635f2018-03-03 12:45:42 -07001024
Gavin Howard2d188a52019-02-25 14:19:08 -07001025 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -07001026
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001027 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -07001028 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001029
Gavin Howard2d188a52019-02-25 14:19:08 -07001030 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001031
Gavin Howard4d5daba2019-01-07 14:42:50 -07001032 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001033 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001034 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001035
Gavin Howard63738202018-09-26 15:34:20 -06001036 if (pow & 1) {
1037 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001038 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001039 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001040 }
1041 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001042
Gavin Howard2d188a52019-02-25 14:19:08 -07001043 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -07001044 if (neg) {
1045 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001046 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -07001047 }
1048
Gavin Howard63738202018-09-26 15:34:20 -06001049 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001050
Gavin Howard53eba8b2018-10-31 15:14:37 -06001051 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -06001052 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001053 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001054
Gavin Howard9b801632019-02-16 23:34:06 -07001055sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001056 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001057err:
Gavin Howard63738202018-09-26 15:34:20 -06001058 bc_num_free(&copy);
1059 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001060}
1061
Gavin Howard7bda4782018-12-28 09:53:22 -07001062#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -07001063static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
1064 size_t scale)
1065{
Gavin Howardfcf29012019-04-25 20:12:19 -06001066 // TODO: Check this function.
Gavin Howardac4d9122018-12-27 23:59:12 -07001067 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001068 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001069
1070 BC_UNUSED(scale);
1071
1072 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001073 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001074
Gavin Howardc9bef2d2019-04-26 14:46:52 -06001075 if (val < c->scale) bc_num_truncate(c, c->scale - val);
1076 else if (val > c->scale) bc_num_extend(c, val - c->scale);
Gavin Howardac4d9122018-12-27 23:59:12 -07001077
1078 return s;
1079}
1080
Gavin Howard2d188a52019-02-25 14:19:08 -07001081static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1082{
Gavin Howardfcf29012019-04-25 20:12:19 -06001083 // TODO: Check this function.
Gavin Howardac4d9122018-12-27 23:59:12 -07001084 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001085 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001086
1087 BC_UNUSED(scale);
1088
1089 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001090 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001091
Gavin Howardb1558662019-04-26 14:32:55 -06001092 return bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001093}
1094
Gavin Howard2d188a52019-02-25 14:19:08 -07001095static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1096 size_t scale)
1097{
Gavin Howardfcf29012019-04-25 20:12:19 -06001098 // TODO: Check this function.
Gavin Howardac4d9122018-12-27 23:59:12 -07001099 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001100 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001101
1102 BC_UNUSED(scale);
1103
1104 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001105 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001106
Gavin Howard2a366922019-01-16 10:50:20 -07001107 if (BC_NUM_ZERO(c)) return s;
1108
Gavin Howardb1558662019-04-26 14:32:55 -06001109 return bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001110}
Gavin Howard7bda4782018-12-28 09:53:22 -07001111#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001112
Gavin Howard1cbfe242019-01-09 17:13:11 -07001113static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1114 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001115{
Gavin Howardfcf29012019-04-25 20:12:19 -06001116 // TODO: Check this function.
Gavin Howarda1c44392018-09-27 12:41:15 -06001117 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001118 BcNum num2, *ptr_a, *ptr_b;
1119 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001120
Gavin Howard63738202018-09-26 15:34:20 -06001121 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001122
Gavin Howard890d0c02018-10-30 16:34:50 -06001123 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001124 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001125 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001126 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001127 }
1128 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001129
Gavin Howard63738202018-09-26 15:34:20 -06001130 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001131 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001132 if (c != a) {
1133 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001134 init = true;
1135 }
1136 }
1137 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001138
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001139 if (init) bc_num_init(c, req);
1140 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001141
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001142 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001143
Gavin Howardddce6d42019-01-03 14:07:06 -07001144 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001145 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001146
Gavin Howardba009802018-09-29 04:41:51 -06001147 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001148
Gavin Howardee9d01e2019-02-16 22:48:11 -07001149 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001150}
1151
Gavin Howardc90ed902019-01-02 13:07:49 -07001152#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001153static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001154
Gavin Howardc7d655b2018-12-28 19:45:13 -07001155 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001156 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001157
Gavin Howard63738202018-09-26 15:34:20 -06001158 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001159
Gavin Howard63738202018-09-26 15:34:20 -06001160 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001161
Gavin Howard8a921bd2018-10-11 14:15:32 -06001162 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001163
Gavin Howard63738202018-09-26 15:34:20 -06001164 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001165
Gavin Howard63738202018-09-26 15:34:20 -06001166 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001167
Gavin Howard63738202018-09-26 15:34:20 -06001168 radix = true;
1169 continue;
1170 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001171
Gavin Howardc7d655b2018-12-28 19:45:13 -07001172 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001173 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001174
Gavin Howard63738202018-09-26 15:34:20 -06001175 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001176}
Gavin Howardc90ed902019-01-02 13:07:49 -07001177#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001178
Gavin Howard1cbfe242019-01-09 17:13:11 -07001179static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001180
Gavin Howardfcf29012019-04-25 20:12:19 -06001181 // TODO: Check this function.
1182
Gavin Howardc7d655b2018-12-28 19:45:13 -07001183 if (isupper(c)) {
1184 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001185 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001186 }
1187 else c -= '0';
1188
1189 return (unsigned long) (uchar) c;
1190}
1191
Gavin Howard1cbfe242019-01-09 17:13:11 -07001192static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001193
Gavin Howard3b60abb2019-04-26 08:49:17 -06001194 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001195 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001196 bool zero = true, rdx;
Stefan Eßer4b111c22019-04-24 17:23:08 -06001197
Gavin Howard63738202018-09-26 15:34:20 -06001198 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001199
Gavin Howard63738202018-09-26 15:34:20 -06001200 val += i;
Gavin Howard3b60abb2019-04-26 08:49:17 -06001201 assert(isalnum(val[0]) || val[0] == '.');
Gavin Howard63738202018-09-26 15:34:20 -06001202 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001203
Gavin Howard63738202018-09-26 15:34:20 -06001204 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001205 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001206
Gavin Howardd43ec372019-04-25 21:13:54 -06001207 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
1208
1209 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001210 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001211
Gavin Howard3b60abb2019-04-26 08:49:17 -06001212 i = len - (ptr == val ? 0 : i) - rdx;
Gavin Howard48a0e472019-04-26 15:39:46 -06001213 temp = BC_NUM_ROUND_POW(i);
Gavin Howard3b60abb2019-04-26 08:49:17 -06001214 mod = n->scale % BC_BASE_POWER;
1215 i = mod ? BC_BASE_POWER - mod : 0;
1216 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001217
1218 bc_num_expand(n, n->len);
1219 bc_num_set(n->num, 0, n->len);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001220
Gavin Howard6076dbb2019-04-26 09:47:50 -06001221 if (zero) n->len = n->rdx = 0;
1222 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001223
Gavin Howard971a2672019-04-26 14:32:07 -06001224 unsigned long exp, pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001225
Gavin Howard3b60abb2019-04-26 08:49:17 -06001226 exp = i;
Gavin Howard971a2672019-04-26 14:32:07 -06001227 pow = bc_num_pow10(exp);
Gavin Howardd43ec372019-04-25 21:13:54 -06001228
1229 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001230
Gavin Howardc7d655b2018-12-28 19:45:13 -07001231 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001232
Gavin Howardd43ec372019-04-25 21:13:54 -06001233 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001234 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001235
1236 size_t idx = exp / BC_BASE_POWER;
1237
Gavin Howard8dd307e2019-01-08 23:05:19 -07001238 if (isupper(c)) c = '9';
Gavin Howardf196fbe2019-04-26 08:49:05 -06001239 n->num[idx] += (((unsigned long) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001240
1241 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1242 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001243 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001244 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001245 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001246}
1247
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001248static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1249 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001250{
Gavin Howardfcf29012019-04-25 20:12:19 -06001251 // TODO: Check this function.
Gavin Howard1cbfe242019-01-09 17:13:11 -07001252 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001253 BcNum temp, mult, result;
Gavin Howard1cbfe242019-01-09 17:13:11 -07001254 BcDig c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001255 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001256 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001257 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001258
Gavin Howard11b9afd2018-10-18 14:23:28 -06001259 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001260 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001261
Gavin Howard26d078f2019-01-24 13:58:12 -07001262 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1263 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001264
Gavin Howardc7d655b2018-12-28 19:45:13 -07001265 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001266
Gavin Howardc7d655b2018-12-28 19:45:13 -07001267 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001268
Gavin Howard53eba8b2018-10-31 15:14:37 -06001269 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001270 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001271 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001272 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001273 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001274 }
Gavin Howardede51f02018-03-02 12:30:00 -07001275
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001276 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001277
Gavin Howard63738202018-09-26 15:34:20 -06001278 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001279 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001280 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001281
Gavin Howard2d188a52019-02-25 14:19:08 -07001282 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001283 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001284 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001285
Gavin Howard53eba8b2018-10-31 15:14:37 -06001286 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001287 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001288
Gavin Howard705c4bc2018-12-03 15:38:04 -07001289 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001290 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001291 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001292 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001293 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001294 }
Gavin Howardede51f02018-03-02 12:30:00 -07001295
Gavin Howardf2ee6342019-04-09 17:05:20 -06001296 if (BC_SIG) {
1297 s = BC_STATUS_SIGNAL;
1298 goto err;
1299 }
1300
Gavin Howard1769abc2019-02-21 09:36:14 -07001301 // This one cannot be a divide by 0 because mult starts out at 1, then is
1302 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1303 s = bc_num_div(&result, &mult, &result, digs);
1304 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001305 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001306 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001307 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001308
Gavin Howardddce6d42019-01-03 14:07:06 -07001309 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001310 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001311 }
1312 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001313
Gavin Howardd1417682019-02-15 17:08:42 -07001314err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001315 bc_num_free(&result);
1316int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001317 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001318 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001319 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001320}
1321
Gavin Howard1cbfe242019-01-09 17:13:11 -07001322static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001323 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001324 bc_vm_putchar('\\');
1325 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001326 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001327 }
Gavin Howard80977b22018-09-06 14:29:13 -06001328}
1329
Gavin Howard40a085f2018-12-03 12:08:59 -07001330#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001331static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001332 // TODO: Check this function.
Gavin Howard3c02ed32018-12-28 18:17:15 -07001333 BC_UNUSED(rdx);
1334 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001335 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001336}
1337#endif // DC_ENABLED
1338
Gavin Howard1cbfe242019-01-09 17:13:11 -07001339static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1340
Gavin Howardfcf29012019-04-25 20:12:19 -06001341 // TODO: Check this function.
1342
Gavin Howarda84ad992018-12-03 19:11:06 -07001343 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001344
Gavin Howardab5e9632019-01-02 13:32:02 -07001345 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001346 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001347 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001348
Gavin Howardab5e9632019-01-02 13:32:02 -07001349 bc_num_printNewline();
Gavin Howard530e3072019-04-23 16:23:36 -06001350 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001351
Gavin Howard530e3072019-04-23 16:23:36 -06001352 for (exp = 0; exp < len; pow /= BC_BASE, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001353 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001354 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001355 dig = n / pow;
1356 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001357 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001358 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001359}
Gavin Howardfe679f02018-02-14 15:50:09 -07001360
Gavin Howard1cbfe242019-01-09 17:13:11 -07001361static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001362
Gavin Howard3c02ed32018-12-28 18:17:15 -07001363 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001364
Gavin Howard3c02ed32018-12-28 18:17:15 -07001365 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001366 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001367 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001368 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001369 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001370
Gavin Howardab5e9632019-01-02 13:32:02 -07001371 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001372 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001373 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001374}
1375
Gavin Howard1cbfe242019-01-09 17:13:11 -07001376static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001377
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001378 size_t i, j, rdx = n->rdx;
1379 bool zero = true;
1380 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001381
Gavin Howard48af52e2018-10-30 14:47:38 -06001382 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001383 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001384
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001385 for (i = n->len - 1; i < n->len; --i) {
1386
1387 BcDig n9 = n->num[i];
1388 size_t temp;
1389 bool irdx = (i == rdx - 1);
1390
1391 zero = (zero & !irdx);
1392 temp = n->scale % BC_BASE_POWER;
1393 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1394
1395 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1396
1397 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001398 buffer[j] = n9 % BC_BASE;
1399 n9 /= BC_BASE;
1400 }
Stefan Esser0da17752019-04-25 12:25:15 +02001401
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001402 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1403 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1404 zero = (zero && buffer[j] == 0);
1405 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Eßer4b111c22019-04-24 17:23:08 -06001406 }
1407 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001408}
1409
Gavin Howard7ad5a662019-02-19 14:40:46 -07001410#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001411static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) {
Gavin Howard7ad5a662019-02-19 14:40:46 -07001412
Gavin Howardfcf29012019-04-25 20:12:19 -06001413 // TODO: Check this function.
1414
Gavin Howardb1558662019-04-26 14:32:55 -06001415 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001416 bool neg = (n->len <= n->rdx);
1417 BcNum temp, exp;
1418 size_t places, mod;
1419 BcDig digs[BC_NUM_LONG_LOG10];
1420
1421 bc_num_createCopy(&temp, n);
1422
1423 if (neg) {
1424 places = n->rdx - bc_num_len(n) + 1;
1425 mod = places % 3;
1426 if (eng && mod != 0) places += 3 - mod;
Gavin Howardb1558662019-04-26 14:32:55 -06001427 s = bc_num_shiftLeft(&temp, places);
1428 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001429 }
1430 else {
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001431 places = bc_num_int(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001432 mod = places % 3;
1433 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howardb1558662019-04-26 14:32:55 -06001434 s = bc_num_shiftRight(&temp, places);
1435 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001436 }
1437
1438 bc_num_printDecimal(&temp);
1439
1440 bc_num_printNewline();
1441 bc_vm_putchar('e');
1442
1443 if (!places) {
1444 bc_num_printHex(0, 1, false);
1445 goto exit;
1446 }
1447
1448 if (neg) {
1449 bc_num_printNewline();
1450 bc_vm_putchar('-');
1451 }
1452
1453 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1454 bc_num_ulong2num(&exp, (unsigned long) places);
1455
1456 bc_num_printDecimal(&exp);
1457
1458exit:
1459 bc_num_free(&temp);
Gavin Howardb1558662019-04-26 14:32:55 -06001460 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001461}
1462#endif // BC_ENABLE_EXTRA_MATH
1463
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001464static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1465 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001466{
Gavin Howardfcf29012019-04-25 20:12:19 -06001467 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001468 BcStatus s;
1469 BcVec stack;
1470 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001471 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001472 size_t i;
1473 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001474
Gavin Howard971a2672019-04-26 14:32:07 -06001475 assert(BC_NUM_NONZERO(base));
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001476
Gavin Howardddce6d42019-01-03 14:07:06 -07001477 if (BC_NUM_ZERO(n)) {
1478 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001479 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001480 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001481
Gavin Howardd3929832018-12-24 15:13:15 -07001482 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001483 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001484 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001485 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001486 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001487 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001488
Gavin Howard63738202018-09-26 15:34:20 -06001489 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001490 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001491 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001492
Gavin Howard2d188a52019-02-25 14:19:08 -07001493 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001494
1495 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001496 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001497 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001498
1499 // Will never fail (except for signals) because digit is
1500 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001501 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001502 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001503
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001504 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001505 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001506
Gavin Howard2d188a52019-02-25 14:19:08 -07001507 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001508
Gavin Howard2d188a52019-02-25 14:19:08 -07001509 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001510 ptr = bc_vec_item_rev(&stack, i);
1511 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001512 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001513 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001514
Gavin Howard2d188a52019-02-25 14:19:08 -07001515 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001516 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001517
Gavin Howard2d188a52019-02-25 14:19:08 -07001518 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001519
Gavin Howard53eba8b2018-10-31 15:14:37 -06001520 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001521 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001522
1523 // Will never fail (except for signals) because fracp is
1524 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001525 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001526 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001527
Gavin Howard705c4bc2018-12-03 15:38:04 -07001528 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001529 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001530 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001531
Gavin Howardddce6d42019-01-03 14:07:06 -07001532 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001533 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001534 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001535 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001536
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001537sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001538 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001539err:
Gavin Howard63738202018-09-26 15:34:20 -06001540 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001541 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001542 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001543 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001544 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001545 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001546}
1547
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001548static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1549 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001550{
Gavin Howardfcf29012019-04-25 20:12:19 -06001551 // TODO: Check this function.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001552 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001553 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001554 BcNumDigitOp print;
1555 bool neg = n->neg;
1556
Gavin Howard48af52e2018-10-30 14:47:38 -06001557 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001558 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001559
1560 n->neg = false;
1561
Gavin Howardbe4e8222019-01-03 13:34:16 -07001562 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001563 width = 1;
1564 print = bc_num_printHex;
1565 }
1566 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001567 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001568 print = bc_num_printDigits;
1569 }
1570
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001571 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001572 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001573
1574 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001575}
1576
Gavin Howard40a085f2018-12-03 12:08:59 -07001577#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001578BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1579 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001580}
1581#endif // DC_ENABLED
1582
Gavin Howard3c02ed32018-12-28 18:17:15 -07001583void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001584 assert(n);
1585 n->num = num;
1586 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001587 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001588 n->neg = false;
1589}
1590
Gavin Howard3c02ed32018-12-28 18:17:15 -07001591void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard88e2e632019-04-24 14:01:20 -06001592 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001593 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Stefan Esser5fd43a32019-04-25 07:05:24 +02001594 bc_num_setup(n, bc_vm_malloc_digs(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001595}
1596
Gavin Howarded392aa2018-02-27 13:09:26 -07001597void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001598 assert(num);
1599 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001600}
1601
Gavin Howard3c02ed32018-12-28 18:17:15 -07001602void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001603 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001604 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001605 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001606 d->len = s->len;
1607 d->neg = s->neg;
1608 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001609 d->scale = s->scale;
Stefan Esser394a5162019-04-25 06:33:10 +02001610 bc_num_cpy(d->num, s->num, d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001611}
1612
Gavin Howardbaa4f582019-01-24 13:56:35 -07001613void bc_num_createCopy(BcNum *d, const BcNum *s) {
1614 bc_num_init(d, s->len);
1615 bc_num_copy(d, s);
1616}
1617
1618void bc_num_createFromUlong(BcNum *n, unsigned long val) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001619 bc_num_init(n, (BC_NUM_LONG_LOG10 - 1) / BC_BASE_POWER + 1);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001620 bc_num_ulong2num(n, val);
1621}
1622
Gavin Howard7ad5a662019-02-19 14:40:46 -07001623size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001624 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001625}
1626
1627size_t bc_num_len(const BcNum *restrict n) {
1628
Gavin Howardfcf29012019-04-25 20:12:19 -06001629 // TODO: Check this function.
Gavin Howard7ad5a662019-02-19 14:40:46 -07001630
Gavin Howardfcf29012019-04-25 20:12:19 -06001631 size_t i, len = n->len;
1632 BcDig dig;
1633
1634 if (n->rdx != len) return len;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001635 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1636
Gavin Howardfcf29012019-04-25 20:12:19 -06001637 dig = n->num[len - 1];
1638
1639 for (i = BC_BASE_DIG; i && (dig % (BcDig) i == dig); i /= BC_BASE);
1640
1641 return (len - 1) * BC_BASE_POWER + i;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001642}
1643
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001644BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1645 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001646{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001647 BcStatus s = BC_STATUS_SUCCESS;
1648
Gavin Howard63738202018-09-26 15:34:20 -06001649 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001650 assert(BC_ENABLE_EXTRA_MATH ||
1651 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001652 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001653
Gavin Howardd746e802018-12-28 22:06:00 -07001654 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06001655 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001656 else s = bc_num_parseBase(n, val, base, base_t);
1657
1658 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001659}
1660
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001661BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1662 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001663{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001664 BcStatus s = BC_STATUS_SUCCESS;
1665
Gavin Howardab5e9632019-01-02 13:32:02 -07001666 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001667 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001668
Gavin Howardab5e9632019-01-02 13:32:02 -07001669 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001670
Gavin Howardddce6d42019-01-03 14:07:06 -07001671 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06001672 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001673#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001674 else if (base_t == 0 || base_t == 1)
1675 s = bc_num_printExponent(n, base_t != 0);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001676#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001677 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001678
Gavin Howarde5f11c72019-02-23 09:42:51 -07001679 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001680 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001681 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001682 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001683
1684 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001685}
1686
Gavin Howard3c02ed32018-12-28 18:17:15 -07001687BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001688
Gavin Howardfcf29012019-04-25 20:12:19 -06001689 // TODO: Check this function.
1690
Gavin Howard63738202018-09-26 15:34:20 -06001691 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001692 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001693
Gavin Howard63738202018-09-26 15:34:20 -06001694 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001695
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001696 *result = 0;
1697
Gavin Howardecafd4f2019-02-23 09:30:45 -07001698 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001699
Gavin Howard06b6e862019-02-15 11:15:31 -07001700 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001701
Gavin Howard0700efc2019-04-24 17:31:23 -06001702 unsigned long prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07001703
Gavin Howard0700efc2019-04-24 17:31:23 -06001704 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001705 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1706
Stefan Eßer4b111c22019-04-24 17:23:08 -06001707 r = prev + n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07001708
1709 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001710 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001711 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001712
Gavin Howard7b557da2018-12-21 10:25:32 -07001713 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001714
Gavin Howard63738202018-09-26 15:34:20 -06001715 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001716}
1717
Gavin Howard3c02ed32018-12-28 18:17:15 -07001718void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001719
Gavin Howardfcf29012019-04-25 20:12:19 -06001720 // TODO: Check this function.
1721
Gavin Howard63738202018-09-26 15:34:20 -06001722 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001723
Gavin Howard63738202018-09-26 15:34:20 -06001724 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001725
Gavin Howard1ab22d22019-01-03 13:32:17 -07001726 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001727
Gavin Howard798508b2019-02-21 16:45:32 -07001728 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howard05feab02019-04-23 16:24:07 -06001729
Stefan Eßer4b111c22019-04-24 17:23:08 -06001730 while (val) {
Gavin Howard0700efc2019-04-24 17:31:23 -06001731 n->num[n->len++] = val % BC_BASE_DIG;
1732 val /= BC_BASE_DIG;
Stefan Eßer4b111c22019-04-24 17:23:08 -06001733 }
Gavin Howard025d04d2018-02-20 13:53:28 -07001734}
1735
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001736size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001737
1738 size_t aint, bint, ardx, brdx;
1739
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001740 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001741
1742 ardx = a->rdx;
1743 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001744 aint = bc_num_int(a);
1745 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001746 ardx = BC_MAX(ardx, brdx);
1747 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001748
Gavin Howard2d188a52019-02-25 14:19:08 -07001749 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001750}
1751
1752size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001753 size_t max, rdx;
1754 rdx = bc_vm_growSize(a->rdx, b->rdx);
1755 max = bc_vm_growSize(BC_MAX(scale, rdx), 1);
1756 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1757 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001758}
1759
1760size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1761 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001762 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001763}
1764
1765#if BC_ENABLE_EXTRA_MATH
1766size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1767 BC_UNUSED(b);
1768 BC_UNUSED(scale);
1769 return BC_NUM_SHREQ(a);
1770}
1771#endif // BC_ENABLE_EXTRA_MATH
1772
Gavin Howard12fe7812018-09-29 03:45:18 -06001773BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001774 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001775 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001776 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001777}
1778
Gavin Howard12fe7812018-09-29 03:45:18 -06001779BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001780 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001781 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001782 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001783}
1784
Gavin Howard12fe7812018-09-29 03:45:18 -06001785BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001786 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001787}
1788
Gavin Howard12fe7812018-09-29 03:45:18 -06001789BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001790 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001791}
1792
Gavin Howard54b946a2018-10-23 12:06:57 -06001793BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001794 size_t req = bc_num_mulReq(a, b, scale);
1795 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001796}
1797
Gavin Howard12fe7812018-09-29 03:45:18 -06001798BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001799 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001800}
1801
Gavin Howard7d8f0382018-12-27 16:15:13 -07001802#if BC_ENABLE_EXTRA_MATH
1803BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001804 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001805}
1806
1807BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001808 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001809}
1810
1811BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001812 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001813}
1814#endif // BC_ENABLE_EXTRA_MATH
1815
Gavin Howard3c02ed32018-12-28 18:17:15 -07001816BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001817
Gavin Howardfcf29012019-04-25 20:12:19 -06001818 // TODO: Check this function.
1819
Gavin Howard1cbfe242019-01-09 17:13:11 -07001820 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001821 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard14913fc2019-04-23 14:36:03 -06001822 size_t pow, len, req, digs, digs1, digs2, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001823 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001824 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001825
Gavin Howard34edd0a2018-10-25 15:51:05 -06001826 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001827
Gavin Howardecafd4f2019-02-23 09:30:45 -07001828 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07001829
Gavin Howard007afdf2019-02-23 09:45:42 -07001830 len = bc_vm_growSize(bc_num_int(a), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001831 req = bc_vm_growSize(BC_MAX(scale, a->rdx), len >> 1);
1832 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001833
Gavin Howardddce6d42019-01-03 14:07:06 -07001834 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001835 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001836 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001837 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001838 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001839 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001840 bc_num_extend(b, scale);
1841 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001842 }
Gavin Howard954276b2018-05-16 01:43:58 -06001843
Gavin Howard890d0c02018-10-30 16:34:50 -06001844 scale = BC_MAX(scale, a->rdx) + 1;
Gavin Howard007afdf2019-02-23 09:45:42 -07001845 len = bc_vm_growSize(a->len, scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001846
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001847 bc_num_init(&num1, len);
1848 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001849 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001850
Gavin Howard63738202018-09-26 15:34:20 -06001851 bc_num_one(&half);
1852 half.num[0] = 5;
1853 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001854
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001855 bc_num_init(&f, len);
1856 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001857
Gavin Howard63738202018-09-26 15:34:20 -06001858 x0 = &num1;
1859 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001860
Gavin Howard63738202018-09-26 15:34:20 -06001861 bc_num_one(x0);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001862 pow = bc_num_int(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001863
Gavin Howard53eba8b2018-10-31 15:14:37 -06001864 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001865
Gavin Howardc39fd492018-10-04 10:07:03 -06001866 if (pow & 1) x0->num[0] = 2;
1867 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001868
Gavin Howardc39fd492018-10-04 10:07:03 -06001869 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001870
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001871 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001872
Gavin Howard63738202018-09-26 15:34:20 -06001873 // Make sure to move the radix back.
1874 x0->rdx -= pow;
1875 }
Gavin Howard954276b2018-05-16 01:43:58 -06001876
Gavin Howard14913fc2019-04-23 14:36:03 -06001877 x0->rdx = digs = digs1 = digs2 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001878 resrdx = scale + 2;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001879 len = bc_num_int(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001880
Gavin Howard2d188a52019-02-25 14:19:08 -07001881 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001882
Gavin Howard971a2672019-04-26 14:32:07 -06001883 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07001884
Gavin Howard53eba8b2018-10-31 15:14:37 -06001885 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001886 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001887 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001888 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001889 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001890 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001891 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001892
Gavin Howard53eba8b2018-10-31 15:14:37 -06001893 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06001894 if (cmp == BC_NUM_SSIZE_MIN) {
1895 s = BC_STATUS_SIGNAL;
1896 break;
1897 }
1898
Gavin Howard53eba8b2018-10-31 15:14:37 -06001899 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001900
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001901 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001902 else times = 0;
1903
1904 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001905
1906 cmp2 = cmp1;
1907 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001908 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001909
Gavin Howard63738202018-09-26 15:34:20 -06001910 temp = x0;
1911 x0 = x1;
1912 x1 = temp;
1913 }
Gavin Howard954276b2018-05-16 01:43:58 -06001914
Gavin Howard2d188a52019-02-25 14:19:08 -07001915 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07001916 s = BC_STATUS_SIGNAL;
1917 goto err;
1918 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001919
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001920 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001921 scale -= 1;
1922 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001923
1924err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06001925 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06001926 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001927 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001928 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001929 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001930 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001931 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001932 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001933}
Gavin Howardba009802018-09-29 04:41:51 -06001934
Gavin Howard2cb39612018-10-22 08:46:48 -06001935BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1936
Gavin Howardfcf29012019-04-25 20:12:19 -06001937 // TODO: Check this function.
1938
Gavin Howard2cb39612018-10-22 08:46:48 -06001939 BcStatus s;
1940 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001941 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07001942 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001943
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001944 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001945
Gavin Howard890d0c02018-10-30 16:34:50 -06001946 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001947 memcpy(&num2, c, sizeof(BcNum));
1948 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001949 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001950 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001951 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001952 else {
1953 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001954 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001955 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001956
Gavin Howard54b946a2018-10-23 12:06:57 -06001957 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001958
Gavin Howardddce6d42019-01-03 14:07:06 -07001959 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001960 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001961 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001962 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001963
1964 if (init) bc_num_free(&num2);
1965
1966 return s;
1967}
1968
Gavin Howard40a085f2018-12-03 12:08:59 -07001969#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001970BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001971
Gavin Howardfcf29012019-04-25 20:12:19 -06001972 // TODO: Check this function.
1973
Gavin Howardba009802018-09-29 04:41:51 -06001974 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001975 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001976 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001977
Gavin Howard34edd0a2018-10-25 15:51:05 -06001978 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001979
Gavin Howardecafd4f2019-02-23 09:30:45 -07001980 if (BC_ERR(BC_NUM_ZERO(c)))
1981 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1982 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1983 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07001984 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001985
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001986 bc_num_expand(d, c->len);
1987 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001988 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001989 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001990
1991 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001992 two.num[0] = 2;
1993 bc_num_one(d);
1994
Gavin Howard1769abc2019-02-21 09:36:14 -07001995 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001996 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001997 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001998 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07001999 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06002000
Gavin Howard2d188a52019-02-25 14:19:08 -07002001 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06002002
Gavin Howard1769abc2019-02-21 09:36:14 -07002003 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002004 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002005 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002006
Gavin Howarda83a5e52019-02-21 16:51:06 -07002007 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06002008 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002009 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002010
2011 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002012 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002013 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002014 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002015 }
2016
Gavin Howard53eba8b2018-10-31 15:14:37 -06002017 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002018 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002019
2020 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002021 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002022 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002023 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002024 }
2025
Gavin Howard2d188a52019-02-25 14:19:08 -07002026 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002027
Gavin Howardba009802018-09-29 04:41:51 -06002028err:
Gavin Howardba009802018-09-29 04:41:51 -06002029 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002030rem_err:
2031 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002032 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002033 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002034 return s;
2035}
Gavin Howarde6e84762018-10-03 11:46:34 -06002036#endif // DC_ENABLED