blob: fb70ab4e44f6317dac75febd2d6335c72b367fef [file] [log] [blame]
Gavin Howard3eb626f2018-02-14 13:54:35 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -07003 *
Gavin Howard7345cb92019-04-08 14:13:43 -06004 * Copyright (c) 2018-2019 Gavin D. Howard and contributors.
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
Gavin Howard7345cb92019-04-08 14:13:43 -06006 * All rights reserved.
Gavin Howard3eb626f2018-02-14 13:54:35 -07007 *
Gavin Howard7345cb92019-04-08 14:13:43 -06008 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
Gavin Howard3eb626f2018-02-14 13:54:35 -070029 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070030 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070031 *
32 * Code for the number type.
33 *
34 */
35
36#include <assert.h>
Gavin Howardc7d655b2018-12-28 19:45:13 -070037#include <ctype.h>
Gavin Howard411f7322018-09-26 17:21:19 -060038#include <stdbool.h>
39#include <stdlib.h>
40#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070041
Gavin Howard305249a2018-10-15 20:24:47 -060042#include <limits.h>
43
Gavin Howard29493062018-03-20 19:57:37 -060044#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070045#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060046#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070047
Gavin Howard55c8f732019-04-24 14:05:27 -060048#if BC_DEBUG_CODE
49static void bc_num_printDecimal(const BcNum *restrict a);
50
Gavin Howard5b40e402019-04-27 07:26:35 -060051static void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline)
52{
Gavin Howard55c8f732019-04-24 14:05:27 -060053 printf("%s: ", name);
54 bc_num_printDecimal(n);
55 printf("\n");
Gavin Howard5b40e402019-04-27 07:26:35 -060056 if (emptyline) printf("\n");
Gavin Howard55c8f732019-04-24 14:05:27 -060057 vm->nchars = 0;
58}
Stefan Eßer4b111c22019-04-24 17:23:08 -060059
Gavin Howard5b40e402019-04-27 07:26:35 -060060static void bc_num_printDigs(const BcNum *n, const char *name, bool emptyline) {
61
62 size_t i;
63
Gavin Howard37118f42019-04-27 07:28:24 -060064 printf("%s len: %zu, rdx: %zu, scale: %zu\n",
65 name, n->len, n->rdx, n->scale);
Gavin Howard5b40e402019-04-27 07:26:35 -060066
67 for (i = n->len - 1; i < n->len; --i)
68 printf(" %0*d", BC_BASE_POWER, n->num[i]);
69
70 printf("\n");
71 if (emptyline) printf("\n");
72 vm->nchars = 0;
73}
74
75static void bc_num_dump(const char *varname, const BcNum *n) {
Stefan Eßer4b111c22019-04-24 17:23:08 -060076
Gavin Howardc4783ac2019-04-26 10:40:02 -060077 unsigned long i;
Stefan Eßer4b111c22019-04-24 17:23:08 -060078
Stefan Eßerdaaaaa72019-04-27 18:04:02 -060079 fprintf(stderr, "\n%s = %s", varname, n->len ? (n->neg ? "-" : "+") : "0 ");
Stefan Eßer4b111c22019-04-24 17:23:08 -060080
Gavin Howardc4783ac2019-04-26 10:40:02 -060081 for (i = n->len -1; i < n->len; i--) {
Gavin Howardc88e5232019-04-27 07:33:16 -060082 if (i + 1 == n->rdx) fprintf(stderr, ". ");
Stefan Esser193f8522019-04-27 01:29:01 +020083 fprintf(stderr, "%0*d ", BC_BASE_POWER, n->num[i]);
Stefan Eßer4b111c22019-04-24 17:23:08 -060084 }
85
Gavin Howard948b6392019-04-27 07:53:23 -060086 fprintf(stderr, "(%p | %zu.%zu/%zu)\n",
87 (void*) n->num, n->len, n->rdx, n->cap);
Stefan Eßer4b111c22019-04-24 17:23:08 -060088}
Gavin Howard55c8f732019-04-24 14:05:27 -060089#endif // BC_DEBUG_CODE
90
Gavin Howard49ea7b62019-04-23 14:46:26 -060091static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
92
Gavin Howard56ebcf72019-02-21 16:59:48 -070093static ssize_t bc_num_neg(size_t n, bool neg) {
94 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
95}
96
Gavin Howardd2cf06a2019-02-21 17:03:24 -070097ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -070098 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -070099}
100
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700101static size_t bc_num_int(const BcNum *n) {
102 return n->len ? n->len - n->rdx : 0;
103}
104
Gavin Howarda83a5e52019-02-21 16:51:06 -0700105static bool bc_num_isOne(const BcNum *n) {
106 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
107}
108
Gavin Howard798508b2019-02-21 16:45:32 -0700109static void bc_num_expand(BcNum *restrict n, size_t req) {
110 assert(n);
111 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
112 if (req > n->cap) {
Gavin Howard404ece72019-04-26 15:59:15 -0600113 n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req));
Gavin Howard798508b2019-02-21 16:45:32 -0700114 n->cap = req;
115 }
116}
117
Gavin Howard1cbfe242019-01-09 17:13:11 -0700118static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600119 assert(n);
Gavin Howardfcf29012019-04-25 20:12:19 -0600120 n->scale = scale;
121 n->len = n->rdx = 0;
Gavin Howard97102b12018-11-01 23:37:31 -0600122 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600123}
124
Gavin Howard1cbfe242019-01-09 17:13:11 -0700125static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600126 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -0600127}
128
Gavin Howard3c02ed32018-12-28 18:17:15 -0700129void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600130 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -0600131 n->len = 1;
132 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -0600133}
134
Gavin Howard3c02ed32018-12-28 18:17:15 -0700135void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600136 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600137 bc_num_setToZero(n, 0);
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600138#if BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600139 n->len = 2;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600140 n->num[0] = 0;
141 n->num[1] = 1;
142#else // BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600143 n->len = 1;
Stefan Eßer4b111c22019-04-24 17:23:08 -0600144 n->num[0] = BC_BASE;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600145#endif // BC_BASE_DIG == 10
Gavin Howard0be26ed2018-08-31 20:21:56 -0600146}
147
Gavin Howard1cbfe242019-01-09 17:13:11 -0700148static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -0700149 size_t len;
Gavin Howard530e3072019-04-23 16:23:36 -0600150 for (len = 1; i; i /= BC_BASE, ++len);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700151 return len;
152}
153
Gavin Howard971a2672019-04-26 14:32:07 -0600154static unsigned long bc_num_pow10(unsigned long i) {
155 unsigned long pow;
156 for (pow = 1; i; --i, pow *= BC_BASE);
157 return pow;
158}
159
Gavin Howardfcf29012019-04-25 20:12:19 -0600160static unsigned long bc_num_addDigit(BcDig *restrict num, unsigned long d,
161 unsigned long c)
Gavin Howard14c354a2019-04-24 14:28:24 -0600162{
163 d += c;
Gavin Howard0700efc2019-04-24 17:31:23 -0600164 *num = (BcDig) (d % BC_BASE_DIG);
165 assert(*num >= 0 && *num < BC_BASE_DIG);
166 return d / BC_BASE_DIG;
Gavin Howard14c354a2019-04-24 14:28:24 -0600167}
168
169static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
170 size_t len)
171{
172 size_t i;
Gavin Howard971a2672019-04-26 14:32:07 -0600173 unsigned long carry = 0;
Gavin Howard14c354a2019-04-24 14:28:24 -0600174
175 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard971a2672019-04-26 14:32:07 -0600176 unsigned long in = ((unsigned long) a[i]) + ((unsigned long) b[i]);
Gavin Howard14c354a2019-04-24 14:28:24 -0600177 carry = bc_num_addDigit(a + i, in, carry);
178 }
179
180 for (; BC_NO_SIG && carry; ++i)
Gavin Howard971a2672019-04-26 14:32:07 -0600181 carry = bc_num_addDigit(a + i, (unsigned long) a[i], carry);
Gavin Howard14c354a2019-04-24 14:28:24 -0600182
183 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
184}
185
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700186static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
187 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700188{
Gavin Howard63738202018-09-26 15:34:20 -0600189 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600190
Gavin Howard2d188a52019-02-25 14:19:08 -0700191 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600192
Gavin Howard2d188a52019-02-25 14:19:08 -0700193 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600194 assert(a[i + j] >= -BC_BASE_DIG);
195 a[i + j++] += BC_BASE_DIG;
Gavin Howard54b946a2018-10-23 12:06:57 -0600196 a[i + j] -= 1;
Gavin Howard0700efc2019-04-24 17:31:23 -0600197 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_DIG);
Gavin Howard63738202018-09-26 15:34:20 -0600198 }
199 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600200
Gavin Howard2d188a52019-02-25 14:19:08 -0700201 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600202}
203
Gavin Howard2d188a52019-02-25 14:19:08 -0700204static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
205 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700206{
Gavin Howard63738202018-09-26 15:34:20 -0600207 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600208 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700209 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600210 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600211}
212
Gavin Howard3c02ed32018-12-28 18:17:15 -0700213ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700214
Gavin Howard63738202018-09-26 15:34:20 -0600215 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600216 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600217 bool a_max, neg = false;
218 ssize_t cmp;
219
220 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700221
Gavin Howard8a921bd2018-10-11 14:15:32 -0600222 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700223 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700224 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600225 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600226 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600227 else return -1;
228 }
229 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700230
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700231 a_int = bc_num_int(a);
232 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600233 a_int -= b_int;
234 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700235
Gavin Howard1ab22d22019-01-03 13:32:17 -0700236 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700237
Gavin Howard890d0c02018-10-30 16:34:50 -0600238 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600239 min = b->rdx;
240 diff = a->rdx - b->rdx;
241 max_num = a->num + diff;
242 min_num = b->num;
243 }
244 else {
245 min = a->rdx;
246 diff = b->rdx - a->rdx;
247 max_num = b->num + diff;
248 min_num = a->num;
249 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700250
Gavin Howard890d0c02018-10-30 16:34:50 -0600251 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600252 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700253 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700254
Gavin Howard2d188a52019-02-25 14:19:08 -0700255 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700256 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600257 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700258
Gavin Howardf2ee6342019-04-09 17:05:20 -0600259 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700260}
261
Gavin Howard2a366922019-01-16 10:50:20 -0700262static void bc_num_clean(BcNum *restrict n) {
263 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
264 if (BC_NUM_ZERO(n)) n->neg = false;
265 else if (n->len < n->rdx) n->len = n->rdx;
266}
267
Gavin Howard3c02ed32018-12-28 18:17:15 -0700268void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700269
Gavin Howardb1558662019-04-26 14:32:55 -0600270 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700271
Stefan Esser70f11852019-04-27 10:39:00 +0200272 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700273
Gavin Howardb1558662019-04-26 14:32:55 -0600274 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
275 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
276
277 n->scale -= places;
278 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700279
Gavin Howardddce6d42019-01-03 14:07:06 -0700280 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600281
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600282 size_t pow;
283
284 pow = n->scale % BC_BASE_POWER;
285 pow = pow ? BC_BASE_POWER - pow : 0;
286 pow = bc_num_pow10(pow);
Gavin Howardb1558662019-04-26 14:32:55 -0600287
288 n->len -= places_rdx;
Gavin Howard404ece72019-04-26 15:59:15 -0600289 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
Gavin Howardb1558662019-04-26 14:32:55 -0600290
291 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600292 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600293
Gavin Howard2a366922019-01-16 10:50:20 -0700294 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600295 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700296}
297
Stefan Esser70f11852019-04-27 10:39:00 +0200298static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700299
Gavin Howardb1558662019-04-26 14:32:55 -0600300 size_t places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700301
Gavin Howard1ab22d22019-01-03 13:32:17 -0700302 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700303
Gavin Howardb1558662019-04-26 14:32:55 -0600304 bc_num_expand(n, n->len + places_rdx);
Gavin Howard404ece72019-04-26 15:59:15 -0600305 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
306 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
Gavin Howarde6f326e2019-04-26 10:13:45 -0600307 n->rdx += places_rdx;
308 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600309 n->len += places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700310
Gavin Howarde6f326e2019-04-26 10:13:45 -0600311 assert(n->rdx == BC_NUM_RDX(n->scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200312}
313
Gavin Howard2d188a52019-02-25 14:19:08 -0700314static void bc_num_retireMul(BcNum *restrict n, size_t scale,
315 bool neg1, bool neg2)
316{
Gavin Howard971a2672019-04-26 14:32:07 -0600317 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
318 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600319
Gavin Howard305249a2018-10-15 20:24:47 -0600320 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700321 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600322}
323
Gavin Howard2d188a52019-02-25 14:19:08 -0700324static void bc_num_split(const BcNum *restrict n, size_t idx,
325 BcNum *restrict a, BcNum *restrict b)
326{
Gavin Howardfcf29012019-04-25 20:12:19 -0600327 // TODO: Check this function.
Gavin Howard305249a2018-10-15 20:24:47 -0600328 if (idx < n->len) {
329
Gavin Howard305249a2018-10-15 20:24:47 -0600330 b->len = n->len - idx;
331 a->len = idx;
Gavin Howarda3f260c2019-04-26 15:47:25 -0600332 a->scale = a->rdx = b->scale = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600333
Gavin Howard404ece72019-04-26 15:59:15 -0600334 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
335 memcpy(a->num, n->num, BC_NUM_SIZE(idx));
Gavin Howard50c80022019-01-03 15:14:33 -0700336
337 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600338 }
Gavin Howard50c80022019-01-03 15:14:33 -0700339 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600340
341 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600342}
343
Gavin Howard14913fc2019-04-23 14:36:03 -0600344static size_t bc_num_shiftZero(BcNum *restrict n) {
345
346 size_t i;
347
348 assert(!n->rdx || BC_NUM_ZERO(n));
349
350 for (i = 0; i < n->len && !n->num[i]; ++i);
351
352 n->len -= i;
353 n->num += i;
354
355 return i;
356}
357
Gavin Howard2e736de2019-04-27 06:41:58 -0600358static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
359 n->len += places_rdx;
360 n->num -= places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600361}
362
Gavin Howard48a0e472019-04-26 15:39:46 -0600363static BcStatus bc_num_shift(BcNum *restrict n, BcDig *restrict ptr,
364 unsigned long dig, size_t len)
365{
Gavin Howardb1558662019-04-26 14:32:55 -0600366 size_t i;
367 unsigned long carry = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600368
Gavin Howardb1558662019-04-26 14:32:55 -0600369 assert(dig < BC_BASE_POWER);
Gavin Howard305249a2018-10-15 20:24:47 -0600370
Gavin Howardb1558662019-04-26 14:32:55 -0600371 dig = bc_num_pow10(dig);
372
373 for (i = 0; BC_NO_SIG && i < len; ++i) {
374 unsigned long in = ((unsigned long) ptr[i]) * dig;
375 carry = bc_num_addDigit(ptr + i, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600376 }
377
Gavin Howardb1558662019-04-26 14:32:55 -0600378 if (carry) {
379 bc_num_expand(n, n->len + 1);
380 assert(carry < BC_BASE_DIG);
381 n->num[n->len] = (BcDig) carry;
382 n->len += 1;
383 }
384
385 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600386}
387
Gavin Howardb1558662019-04-26 14:32:55 -0600388static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700389
Gavin Howardb1558662019-04-26 14:32:55 -0600390 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600391 unsigned long dig;
392 bool shift;
393 size_t places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600394
Gavin Howardb1558662019-04-26 14:32:55 -0600395 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600396
Gavin Howard48a0e472019-04-26 15:39:46 -0600397 dig = (unsigned long) (places % BC_BASE_POWER);
398 shift = (dig != 0);
399 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx - shift;
400
Gavin Howardb1558662019-04-26 14:32:55 -0600401 if (n->scale >= places) {
402 n->scale -= places;
403 n->rdx = BC_NUM_RDX(n->scale);
404 }
405 else if (BC_NUM_NONZERO(n)) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600406
Stefan Esser6189d952019-04-27 10:40:14 +0200407 if (places_rdx > n->rdx) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600408 bc_num_expand(n, n->len + places_rdx - n->rdx + shift);
Gavin Howard404ece72019-04-26 15:59:15 -0600409 memmove(n->num + places_rdx - n->rdx, n->num, BC_NUM_SIZE(n->len));
410 memset(n->num, 0, BC_NUM_SIZE(places_rdx - n->rdx));
Gavin Howard48a0e472019-04-26 15:39:46 -0600411 n->len += places_rdx;
412 }
413
Gavin Howardb1558662019-04-26 14:32:55 -0600414 n->scale = n->rdx = 0;
415 }
416 else n->scale = 0;
417
418 if (shift) s = bc_num_shift(n, n->num + places_rdx, dig, n->len - places_rdx);
419
420 bc_num_clean(n);
421
422 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
423}
424
425static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
426
427 BcStatus s = BC_STATUS_SUCCESS;
428 unsigned long dig = (unsigned long) (places % BC_BASE_POWER);
429 bool shift = (dig != 0);
430 size_t len, places_rdx;
431
432 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600433 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600434 n->scale += places;
435 bc_num_expand(n, BC_NUM_RDX(n->scale));
436 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600437 }
438
Gavin Howardb1558662019-04-26 14:32:55 -0600439 places_rdx = BC_NUM_RDX(places);
440 len = bc_vm_growSize(n->len, places_rdx);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700441
442 if (len > n->len) {
443
444 if (len > n->cap) bc_num_expand(n, len);
445
Gavin Howard404ece72019-04-26 15:59:15 -0600446 memset(n->num + n->len, 0, BC_NUM_SIZE(len - n->len));
Gavin Howard7ad5a662019-02-19 14:40:46 -0700447 n->len = len;
448 }
449
Gavin Howardb1558662019-04-26 14:32:55 -0600450 n->scale += places;
451 n->rdx += places_rdx;
452
453 if (shift) s = bc_num_shift(n, n->num, BC_BASE_POWER - dig, n->len);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700454
455 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600456 assert(n->rdx == BC_NUM_RDX(n->scale));
457
458 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700459}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700460
Gavin Howard1cbfe242019-01-09 17:13:11 -0700461static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600462
Gavin Howard63738202018-09-26 15:34:20 -0600463 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600464 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600465
Gavin Howard971a2672019-04-26 14:32:07 -0600466 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700467
Gavin Howardd32d7df2018-10-15 08:42:25 -0600468 one.cap = 2;
469 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600470 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600471
Gavin Howardd32d7df2018-10-15 08:42:25 -0600472 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600473}
474
Gavin Howard7bda4782018-12-28 09:53:22 -0700475#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700476static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
477 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700478{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700479 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700480 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700481 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700482}
Gavin Howard7bda4782018-12-28 09:53:22 -0700483#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700484
Gavin Howard1cbfe242019-01-09 17:13:11 -0700485static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700486
Gavin Howard8a921bd2018-10-11 14:15:32 -0600487 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600488 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard6fccd222019-04-26 10:17:02 -0600489 unsigned long carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700490
Gavin Howard63738202018-09-26 15:34:20 -0600491 // Because this function doesn't need to use scale (per the bc spec),
492 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600493
Gavin Howardddce6d42019-01-03 14:07:06 -0700494 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600495 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700496 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600497 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600498 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700499 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600500 bc_num_copy(c, a);
501 return BC_STATUS_SUCCESS;
502 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600503
Gavin Howard63738202018-09-26 15:34:20 -0600504 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600505 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600506 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600507 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700508
Gavin Howard63738202018-09-26 15:34:20 -0600509 if (a->rdx > b->rdx) {
510 diff = a->rdx - b->rdx;
511 ptr = a->num;
512 ptr_a = a->num + diff;
513 ptr_b = b->num;
514 }
515 else {
516 diff = b->rdx - a->rdx;
517 ptr = b->num;
518 ptr_a = a->num;
519 ptr_b = b->num + diff;
520 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700521
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700522 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700523
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700524 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600525 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700526 a_int = bc_num_int(a);
527 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700528
Gavin Howard890d0c02018-10-30 16:34:50 -0600529 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600530 min_int = b_int;
531 max = a_int;
532 ptr = ptr_a;
533 }
534 else {
535 min_int = a_int;
536 max = b_int;
537 ptr = ptr_b;
538 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700539
Gavin Howard2d188a52019-02-25 14:19:08 -0700540 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard6fccd222019-04-26 10:17:02 -0600541 unsigned long in;
542 in = ((unsigned long) ptr_a[i]) + ((unsigned long) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700543 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600544 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700545
Gavin Howard2d188a52019-02-25 14:19:08 -0700546 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard6fccd222019-04-26 10:17:02 -0600547 carry = bc_num_addDigit(ptr_c + i, (unsigned long) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700548
549 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700550
Gavin Howard1ab22d22019-01-03 13:32:17 -0700551 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600552
Gavin Howard2d188a52019-02-25 14:19:08 -0700553 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700554}
555
Gavin Howard1cbfe242019-01-09 17:13:11 -0700556static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700557
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700558 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600559 ssize_t cmp;
560 BcNum *minuend, *subtrahend;
561 size_t start;
562 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700563
Gavin Howard63738202018-09-26 15:34:20 -0600564 // Because this function doesn't need to use scale (per the bc spec),
565 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700566
Gavin Howardddce6d42019-01-03 14:07:06 -0700567 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600568 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700569 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600570 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600571 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700572 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600573 bc_num_copy(c, a);
574 return BC_STATUS_SUCCESS;
575 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700576
Gavin Howard63738202018-09-26 15:34:20 -0600577 aneg = a->neg;
578 bneg = b->neg;
579 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700580
Gavin Howard63738202018-09-26 15:34:20 -0600581 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600582 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700583
Gavin Howard63738202018-09-26 15:34:20 -0600584 a->neg = aneg;
585 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700586
Gavin Howard1ab22d22019-01-03 13:32:17 -0700587 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600588 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600589 return BC_STATUS_SUCCESS;
590 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700591
592 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600593 neg = a->neg;
594 minuend = a;
595 subtrahend = b;
596 }
597 else {
598 neg = b->neg;
599 if (sub) neg = !neg;
600 minuend = b;
601 subtrahend = a;
602 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700603
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600604 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600605 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700606
Gavin Howarde6f326e2019-04-26 10:13:45 -0600607 if (c->scale < subtrahend->scale) {
608 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600609 start = 0;
610 }
611 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700612
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700613 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700614
Gavin Howard63738202018-09-26 15:34:20 -0600615 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700616
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700617 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700618}
619
Gavin Howarddc4e3712019-04-23 14:56:29 -0600620static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
621{
Gavin Howardfcf29012019-04-25 20:12:19 -0600622 // TODO: Check this function.
Gavin Howard971a2672019-04-26 14:32:07 -0600623 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600624 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard971a2672019-04-26 14:32:07 -0600625 unsigned long sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600626
Gavin Howard971a2672019-04-26 14:32:07 -0600627 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600628 assert(!a->rdx && !b->rdx);
629
Gavin Howard5083aec2019-04-23 18:03:46 -0600630 clen = bc_vm_growSize(alen, blen);
631 bc_num_expand(c, clen + 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600632
633 ptr_c = c->num;
Gavin Howard404ece72019-04-26 15:59:15 -0600634 memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600635
Gavin Howard5083aec2019-04-23 18:03:46 -0600636 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600637
Gavin Howard5083aec2019-04-23 18:03:46 -0600638 ssize_t sidx = (ssize_t) (i - blen + 1);
639 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600640
Stefan Essera299ffc2019-04-24 23:28:32 +0200641 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
Stefan Eßer4b111c22019-04-24 17:23:08 -0600642
Gavin Howard971a2672019-04-26 14:32:07 -0600643 sum += ((unsigned long) ptr_a[j]) * ((unsigned long) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600644
Gavin Howard0700efc2019-04-24 17:31:23 -0600645 if (sum >= BC_BASE_DIG) {
646 carry += sum / BC_BASE_DIG;
647 sum %= BC_BASE_DIG;
Stefan Essera299ffc2019-04-24 23:28:32 +0200648 }
649 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600650
Stefan Essera299ffc2019-04-24 23:28:32 +0200651 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600652 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Essera299ffc2019-04-24 23:28:32 +0200653 sum = carry;
654 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600655 }
656
Gavin Howard5083aec2019-04-23 18:03:46 -0600657 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600658 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600659 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600660 clen += 1;
661 }
662
663 c->len = clen;
664
Gavin Howarddc4e3712019-04-23 14:56:29 -0600665 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
666}
667
Gavin Howard14c354a2019-04-24 14:28:24 -0600668static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
669 size_t shift, BcNumShiftAddOp op)
670{
Gavin Howardfcf29012019-04-25 20:12:19 -0600671 // TODO: Check this function.
Gavin Howard14c354a2019-04-24 14:28:24 -0600672 assert(n->len >= shift + a->len);
673 assert(!n->rdx && !a->rdx);
674 return op(n->num + shift, a->num, a->len);
675}
676
677static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600678
Gavin Howardfcf29012019-04-25 20:12:19 -0600679 // TODO: Check this function.
680
Gavin Howard9b801632019-02-16 23:34:06 -0700681 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200682 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600683 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700684 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600685 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700686 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600687
Gavin Howard14913fc2019-04-23 14:36:03 -0600688 assert(BC_NUM_ZERO(c));
689
Gavin Howardddce6d42019-01-03 14:07:06 -0700690 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600691 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700692 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600693 bc_num_zero(c);
694 return BC_STATUS_SUCCESS;
695 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700696 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600697 bc_num_copy(c, aone ? b : a);
698 return BC_STATUS_SUCCESS;
699 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700700
Gavin Howardb5093d72018-10-17 12:13:17 -0600701 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
702 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600703 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600704 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600705 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700706
Gavin Howarde0432502019-02-26 16:23:45 -0700707 max = BC_MAX(a->len, b->len);
Stefan Essera299ffc2019-04-24 23:28:32 +0200708 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700709 max2 = (max + 1) / 2;
710
Stefan Esser5fd43a32019-04-25 07:05:24 +0200711 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
Gavin Howard404ece72019-04-26 15:59:15 -0600712 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total));
Gavin Howard5bede412019-02-23 09:31:28 -0700713
714 bc_num_setup(&l1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200715 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700716 bc_num_setup(&h1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200717 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700718 bc_num_setup(&l2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200719 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700720 bc_num_setup(&h2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200721 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700722 bc_num_setup(&m1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200723 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700724 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600725 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600726 bc_num_init(&z0, max);
727 bc_num_init(&z1, max);
728 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600729 max = bc_vm_growSize(max, max) + 1;
730 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600731
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600732 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600733 bc_num_clean(&l1);
734 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600735 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600736 bc_num_clean(&l2);
737 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700738
Gavin Howard14c354a2019-04-24 14:28:24 -0600739 bc_num_expand(c, max);
740 c->len = max;
Gavin Howard404ece72019-04-26 15:59:15 -0600741 memset(c->num, 0, BC_NUM_SIZE(c->len));
Gavin Howard305249a2018-10-15 20:24:47 -0600742
Gavin Howard14c354a2019-04-24 14:28:24 -0600743 s = bc_num_sub(&h1, &l1, &m1, 0);
744 if (BC_ERR(s)) goto err;
745 s = bc_num_sub(&l2, &h2, &m2, 0);
746 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600747
Gavin Howard971a2672019-04-26 14:32:07 -0600748 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600749
Gavin Howard14c354a2019-04-24 14:28:24 -0600750 s = bc_num_m(&h1, &h2, &z2, 0);
751 if (BC_ERR(s)) goto err;
752 bc_num_clean(&z2);
753
754 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
755 if (BC_ERR(s)) goto err;
756 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
757 if (BC_ERR(s)) goto err;
758 }
759
Gavin Howard971a2672019-04-26 14:32:07 -0600760 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600761
762 s = bc_num_m(&l1, &l2, &z0, 0);
763 if (BC_ERR(s)) goto err;
764 bc_num_clean(&z0);
765
766 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
767 if (BC_ERR(s)) goto err;
768 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
769 if (BC_ERR(s)) goto err;
770 }
771
Gavin Howard971a2672019-04-26 14:32:07 -0600772 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600773
774 s = bc_num_m(&m1, &m2, &z1, 0);
775 if (BC_ERR(s)) goto err;
776 bc_num_clean(&z1);
777
778 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
779 s = bc_num_shiftAddSub(c, &z1, max2, op);
780 if (BC_ERR(s)) goto err;
781 }
Gavin Howard305249a2018-10-15 20:24:47 -0600782
783err:
Gavin Howard5bede412019-02-23 09:31:28 -0700784 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600785 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600786 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600787 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600788 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600789 return s;
790}
791
Gavin Howard1cbfe242019-01-09 17:13:11 -0700792static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600793
Gavin Howardfcf29012019-04-25 20:12:19 -0600794 // TODO: Check this function.
795
Gavin Howard305249a2018-10-15 20:24:47 -0600796 BcStatus s;
797 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600798 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600799
Gavin Howard14913fc2019-04-23 14:36:03 -0600800 bc_num_setToZero(c, 0);
801
Gavin Howardcf9f2462019-04-27 06:43:57 -0600802 ascale = a->scale;
803 bscale = b->scale;
804 scale = BC_MAX(scale, ascale);
805 scale = BC_MAX(scale, b->scale);
Gavin Howard48a0e472019-04-26 15:39:46 -0600806 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600807 scale = BC_MIN(rscale, scale);
Gavin Howard305249a2018-10-15 20:24:47 -0600808
Gavin Howardbaa4f582019-01-24 13:56:35 -0700809 bc_num_createCopy(&cpa, a);
810 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600811
Gavin Howard23d8d252018-10-17 12:03:41 -0600812 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600813
Gavin Howard48a0e472019-04-26 15:39:46 -0600814 ardx = cpa.rdx * BC_BASE_POWER;
815 s = bc_num_shiftLeft(&cpa, ardx);
816 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600817 bc_num_clean(&cpa);
818 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600819
820 brdx = cpb.rdx * BC_BASE_POWER;
821 s = bc_num_shiftLeft(&cpb, brdx);
822 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600823 bzero = bc_num_shiftZero(&cpb);
824 bc_num_clean(&cpb);
825
Gavin Howard14c354a2019-04-24 14:28:24 -0600826 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700827 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600828
Gavin Howard14913fc2019-04-23 14:36:03 -0600829 zero = bc_vm_growSize(azero, bzero);
830 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600831
Gavin Howard14913fc2019-04-23 14:36:03 -0600832 bc_num_expand(c, len);
Gavin Howard48a0e472019-04-26 15:39:46 -0600833 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_POWER);
834 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
835 s = bc_num_shiftRight(c, ardx + brdx);
836 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600837
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600838 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600839
Gavin Howard305249a2018-10-15 20:24:47 -0600840err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600841 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600842 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600843 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600844 bc_num_free(&cpa);
845 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700846}
847
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600848static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700849
Gavin Howardfcf29012019-04-25 20:12:19 -0600850 // TODO: Check this function.
851
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600852 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howardfdbcd062019-04-29 07:36:46 -0600853 size_t rdx;
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600854 ssize_t cmp;
Gavin Howardfdbcd062019-04-29 07:36:46 -0600855 BcNum cpa, cpb;
856 bool aneg, bneg, shift_left;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700857
Gavin Howardddce6d42019-01-03 14:07:06 -0700858 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
859 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600860 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600861 return BC_STATUS_SUCCESS;
862 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700863 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600864 bc_num_copy(c, a);
Gavin Howardfdbcd062019-04-29 07:36:46 -0600865 goto exit;
Gavin Howard63738202018-09-26 15:34:20 -0600866 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700867
Gavin Howardfdbcd062019-04-29 07:36:46 -0600868 aneg = a->neg;
869 bneg = b->neg;
870 aneg = bneg = false;
871 cmp = bc_num_cmp(a, b);
872 a->neg = aneg;
873 b->neg = bneg;
Gavin Howard021150b2018-03-10 15:40:42 -0700874
Gavin Howardfdbcd062019-04-29 07:36:46 -0600875 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
876 if (!cmp) {
877 bc_num_one(c);
878 goto exit;
Gavin Howard63738202018-09-26 15:34:20 -0600879 }
Gavin Howardfdbcd062019-04-29 07:36:46 -0600880
881 bc_num_createCopy(&cpa, a);
882 bc_num_createCopy(&cpb, b);
883
884 shift_left = (b->rdx == b->len);
885
886 if (shift_left) {
887
Stefan Esser193f8522019-04-27 01:29:01 +0200888 }
Gavin Howardfdbcd062019-04-29 07:36:46 -0600889 else {
890 rdx = b->len - b->rdx;
891 s = bc_num_shiftRight(&cpa, rdx * BC_BASE_POWER);
892 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
893 s = bc_num_shiftRight(&cpb, rdx * BC_BASE_POWER);
894 }
Gavin Howard034b5b92019-04-27 07:28:56 -0600895
Stefan Esser193f8522019-04-27 01:29:01 +0200896err:
Gavin Howardfdbcd062019-04-29 07:36:46 -0600897 bc_num_free(&cpb);
898 bc_num_free(&cpa);
899exit:
Gavin Howardf2ee6342019-04-09 17:05:20 -0600900 if (BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfdbcd062019-04-29 07:36:46 -0600901 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600902 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700903}
904
Gavin Howard1cbfe242019-01-09 17:13:11 -0700905static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
906 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600907{
Gavin Howardfcf29012019-04-25 20:12:19 -0600908 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -0600909 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600910 BcNum temp;
911 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700912
Gavin Howardddce6d42019-01-03 14:07:06 -0700913 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
914 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700915 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600916 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600917 return BC_STATUS_SUCCESS;
918 }
Gavin Howard8b254872018-03-14 01:13:35 -0600919
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600920 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -0700921 s = bc_num_d(a, b, c, scale);
922 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -0700923 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700924
Gavin Howard1ab22d22019-01-03 13:32:17 -0700925 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600926
Gavin Howard53eba8b2018-10-31 15:14:37 -0600927 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700928 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600929 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700930 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600931
Gavin Howard77445432019-04-26 15:59:33 -0600932 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
Gavin Howard18441082018-10-22 10:03:30 -0600933
934 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600935 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600936 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700937
938err:
Gavin Howard18441082018-10-22 10:03:30 -0600939 bc_num_free(&temp);
940 return s;
941}
942
Gavin Howard2d188a52019-02-25 14:19:08 -0700943static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
944{
Gavin Howardfcf29012019-04-25 20:12:19 -0600945 // TODO: Check this function.
Gavin Howard18441082018-10-22 10:03:30 -0600946 BcStatus s;
947 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -0700948 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -0600949
Gavin Howard77445432019-04-26 15:59:33 -0600950 ts = bc_vm_growSize(scale, b->scale);
951 ts = BC_MAX(ts, a->scale);
Gavin Howard798508b2019-02-21 16:45:32 -0700952
953 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -0600954 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600955 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600956
Gavin Howard63738202018-09-26 15:34:20 -0600957 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700958}
959
Gavin Howard1cbfe242019-01-09 17:13:11 -0700960static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700961
Gavin Howardfcf29012019-04-25 20:12:19 -0600962 // TODO: Check this function.
963
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600964 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600965 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700966 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600967 size_t i, powrdx, resrdx;
968 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700969
Gavin Howardecafd4f2019-02-23 09:30:45 -0700970 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700971
Gavin Howardddce6d42019-01-03 14:07:06 -0700972 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600973 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600974 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600975 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700976 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600977 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600978 return BC_STATUS_SUCCESS;
979 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700980 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600981 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600982 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600983 return s;
984 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700985
Gavin Howard63738202018-09-26 15:34:20 -0600986 neg = b->neg;
987 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600988 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700989 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600990 if (s) return s;
991
Gavin Howardbaa4f582019-01-24 13:56:35 -0700992 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700993
Gavin Howard63738202018-09-26 15:34:20 -0600994 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600995
Gavin Howard2d188a52019-02-25 14:19:08 -0700996 for (powrdx = a->rdx; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600997 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600998 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700999 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001000 }
Gavin Howard6fb635f2018-03-03 12:45:42 -07001001
Gavin Howard2d188a52019-02-25 14:19:08 -07001002 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -07001003
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001004 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -07001005 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001006
Gavin Howard2d188a52019-02-25 14:19:08 -07001007 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001008
Gavin Howard4d5daba2019-01-07 14:42:50 -07001009 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001010 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001011 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001012
Gavin Howard63738202018-09-26 15:34:20 -06001013 if (pow & 1) {
1014 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001015 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001016 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001017 }
1018 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001019
Gavin Howard2d188a52019-02-25 14:19:08 -07001020 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -07001021 if (neg) {
1022 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001023 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -07001024 }
1025
Gavin Howard63738202018-09-26 15:34:20 -06001026 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001027
Gavin Howard53eba8b2018-10-31 15:14:37 -06001028 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -06001029 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001030 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001031
Gavin Howard9b801632019-02-16 23:34:06 -07001032sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001033 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001034err:
Gavin Howard63738202018-09-26 15:34:20 -06001035 bc_num_free(&copy);
1036 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001037}
1038
Gavin Howard7bda4782018-12-28 09:53:22 -07001039#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -07001040static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
1041 size_t scale)
1042{
Gavin Howardac4d9122018-12-27 23:59:12 -07001043 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001044 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001045
1046 BC_UNUSED(scale);
1047
1048 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001049 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001050
Gavin Howardc9bef2d2019-04-26 14:46:52 -06001051 if (val < c->scale) bc_num_truncate(c, c->scale - val);
1052 else if (val > c->scale) bc_num_extend(c, val - c->scale);
Gavin Howardac4d9122018-12-27 23:59:12 -07001053
1054 return s;
1055}
1056
Gavin Howard2d188a52019-02-25 14:19:08 -07001057static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1058{
Gavin Howardac4d9122018-12-27 23:59:12 -07001059 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001060 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001061
1062 BC_UNUSED(scale);
1063
1064 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001065 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001066
Gavin Howardb1558662019-04-26 14:32:55 -06001067 return bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001068}
1069
Gavin Howard2d188a52019-02-25 14:19:08 -07001070static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1071 size_t scale)
1072{
Gavin Howardac4d9122018-12-27 23:59:12 -07001073 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001074 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001075
1076 BC_UNUSED(scale);
1077
1078 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001079 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001080
Gavin Howard2a366922019-01-16 10:50:20 -07001081 if (BC_NUM_ZERO(c)) return s;
1082
Gavin Howardb1558662019-04-26 14:32:55 -06001083 return bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001084}
Gavin Howard7bda4782018-12-28 09:53:22 -07001085#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001086
Gavin Howard1cbfe242019-01-09 17:13:11 -07001087static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1088 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001089{
Gavin Howarda1c44392018-09-27 12:41:15 -06001090 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001091 BcNum num2, *ptr_a, *ptr_b;
1092 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001093
Gavin Howard63738202018-09-26 15:34:20 -06001094 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001095
Gavin Howard890d0c02018-10-30 16:34:50 -06001096 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001097 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001098 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001099 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001100 }
1101 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001102
Gavin Howard63738202018-09-26 15:34:20 -06001103 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001104 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001105 if (c != a) {
1106 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001107 init = true;
1108 }
1109 }
1110 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001111
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001112 if (init) bc_num_init(c, req);
1113 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001114
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001115 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001116
Gavin Howardddce6d42019-01-03 14:07:06 -07001117 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001118 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001119
Gavin Howardba009802018-09-29 04:41:51 -06001120 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001121
Stefan Esser193f8522019-04-27 01:29:01 +02001122// bc_num_truncDecimals(c, scale);
1123
Gavin Howardee9d01e2019-02-16 22:48:11 -07001124 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001125}
1126
Gavin Howardc90ed902019-01-02 13:07:49 -07001127#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001128static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001129
Gavin Howardc7d655b2018-12-28 19:45:13 -07001130 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001131 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001132
Gavin Howard63738202018-09-26 15:34:20 -06001133 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001134
Gavin Howard63738202018-09-26 15:34:20 -06001135 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001136
Gavin Howard8a921bd2018-10-11 14:15:32 -06001137 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001138
Gavin Howard63738202018-09-26 15:34:20 -06001139 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001140
Gavin Howard63738202018-09-26 15:34:20 -06001141 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001142
Gavin Howard63738202018-09-26 15:34:20 -06001143 radix = true;
1144 continue;
1145 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001146
Gavin Howardc7d655b2018-12-28 19:45:13 -07001147 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001148 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001149
Gavin Howard63738202018-09-26 15:34:20 -06001150 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001151}
Gavin Howardc90ed902019-01-02 13:07:49 -07001152#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001153
Gavin Howard1cbfe242019-01-09 17:13:11 -07001154static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001155
Gavin Howardfcf29012019-04-25 20:12:19 -06001156 // TODO: Check this function.
1157
Gavin Howardc7d655b2018-12-28 19:45:13 -07001158 if (isupper(c)) {
1159 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001160 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001161 }
1162 else c -= '0';
1163
1164 return (unsigned long) (uchar) c;
1165}
1166
Gavin Howard1cbfe242019-01-09 17:13:11 -07001167static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001168
Gavin Howard3b60abb2019-04-26 08:49:17 -06001169 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001170 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001171 bool zero = true, rdx;
Stefan Essera299ffc2019-04-24 23:28:32 +02001172
Gavin Howard63738202018-09-26 15:34:20 -06001173 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001174
Gavin Howard63738202018-09-26 15:34:20 -06001175 val += i;
Gavin Howard3b60abb2019-04-26 08:49:17 -06001176 assert(isalnum(val[0]) || val[0] == '.');
Gavin Howard63738202018-09-26 15:34:20 -06001177 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001178
Gavin Howard63738202018-09-26 15:34:20 -06001179 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001180 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001181
Gavin Howardd43ec372019-04-25 21:13:54 -06001182 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001183
Gavin Howardd43ec372019-04-25 21:13:54 -06001184 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001185 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001186
Gavin Howard3b60abb2019-04-26 08:49:17 -06001187 i = len - (ptr == val ? 0 : i) - rdx;
Gavin Howard48a0e472019-04-26 15:39:46 -06001188 temp = BC_NUM_ROUND_POW(i);
Gavin Howard3b60abb2019-04-26 08:49:17 -06001189 mod = n->scale % BC_BASE_POWER;
1190 i = mod ? BC_BASE_POWER - mod : 0;
1191 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001192
1193 bc_num_expand(n, n->len);
Gavin Howard404ece72019-04-26 15:59:15 -06001194 memset(n->num, 0, BC_NUM_SIZE(n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -07001195
Gavin Howard6076dbb2019-04-26 09:47:50 -06001196 if (zero) n->len = n->rdx = 0;
1197 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001198
Gavin Howard971a2672019-04-26 14:32:07 -06001199 unsigned long exp, pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001200
Gavin Howard3b60abb2019-04-26 08:49:17 -06001201 exp = i;
Gavin Howard971a2672019-04-26 14:32:07 -06001202 pow = bc_num_pow10(exp);
Gavin Howardd43ec372019-04-25 21:13:54 -06001203
1204 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001205
Gavin Howardc7d655b2018-12-28 19:45:13 -07001206 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001207
Gavin Howardd43ec372019-04-25 21:13:54 -06001208 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001209 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001210
1211 size_t idx = exp / BC_BASE_POWER;
1212
Gavin Howard8dd307e2019-01-08 23:05:19 -07001213 if (isupper(c)) c = '9';
Gavin Howardf196fbe2019-04-26 08:49:05 -06001214 n->num[idx] += (((unsigned long) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001215
1216 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1217 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001218 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001219 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001220 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001221}
1222
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001223static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1224 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001225{
Gavin Howardfcf29012019-04-25 20:12:19 -06001226 // TODO: Check this function.
Gavin Howard1cbfe242019-01-09 17:13:11 -07001227 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001228 BcNum temp, mult, result;
Gavin Howard77445432019-04-26 15:59:33 -06001229 char c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001230 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001231 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001232 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001233
Gavin Howard11b9afd2018-10-18 14:23:28 -06001234 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001235 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001236
Gavin Howard26d078f2019-01-24 13:58:12 -07001237 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1238 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001239
Gavin Howardc7d655b2018-12-28 19:45:13 -07001240 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001241
Gavin Howardc7d655b2018-12-28 19:45:13 -07001242 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001243
Gavin Howard53eba8b2018-10-31 15:14:37 -06001244 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001245 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001246 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001247 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001248 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001249 }
Gavin Howardede51f02018-03-02 12:30:00 -07001250
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001251 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001252
Gavin Howard63738202018-09-26 15:34:20 -06001253 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001254 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001255 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001256
Gavin Howard2d188a52019-02-25 14:19:08 -07001257 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001258 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001259 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001260
Gavin Howard53eba8b2018-10-31 15:14:37 -06001261 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001262 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001263
Gavin Howard705c4bc2018-12-03 15:38:04 -07001264 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001265 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001266 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001267 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001268 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001269 }
Gavin Howardede51f02018-03-02 12:30:00 -07001270
Gavin Howardf2ee6342019-04-09 17:05:20 -06001271 if (BC_SIG) {
1272 s = BC_STATUS_SIGNAL;
1273 goto err;
1274 }
1275
Gavin Howard1769abc2019-02-21 09:36:14 -07001276 // This one cannot be a divide by 0 because mult starts out at 1, then is
1277 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1278 s = bc_num_div(&result, &mult, &result, digs);
1279 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001280 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001281 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001282 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001283
Gavin Howardddce6d42019-01-03 14:07:06 -07001284 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001285 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001286 }
1287 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001288
Gavin Howardd1417682019-02-15 17:08:42 -07001289err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001290 bc_num_free(&result);
1291int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001292 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001293 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001294 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001295}
1296
Gavin Howard1cbfe242019-01-09 17:13:11 -07001297static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001298 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001299 bc_vm_putchar('\\');
1300 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001301 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001302 }
Gavin Howard80977b22018-09-06 14:29:13 -06001303}
1304
Gavin Howard40a085f2018-12-03 12:08:59 -07001305#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001306static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001307 BC_UNUSED(rdx);
1308 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001309 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001310}
1311#endif // DC_ENABLED
1312
Gavin Howard1cbfe242019-01-09 17:13:11 -07001313static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1314
Gavin Howardfcf29012019-04-25 20:12:19 -06001315 // TODO: Check this function.
1316
Gavin Howarda84ad992018-12-03 19:11:06 -07001317 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001318
Gavin Howardab5e9632019-01-02 13:32:02 -07001319 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001320 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001321 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001322
Gavin Howardab5e9632019-01-02 13:32:02 -07001323 bc_num_printNewline();
Gavin Howard530e3072019-04-23 16:23:36 -06001324 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001325
Gavin Howard530e3072019-04-23 16:23:36 -06001326 for (exp = 0; exp < len; pow /= BC_BASE, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001327 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001328 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001329 dig = n / pow;
1330 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001331 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001332 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001333}
Gavin Howardfe679f02018-02-14 15:50:09 -07001334
Gavin Howard1cbfe242019-01-09 17:13:11 -07001335static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001336
Gavin Howard3c02ed32018-12-28 18:17:15 -07001337 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001338
Gavin Howard3c02ed32018-12-28 18:17:15 -07001339 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001340 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001341 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001342 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001343 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001344
Gavin Howardab5e9632019-01-02 13:32:02 -07001345 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001346 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001347 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001348}
1349
Gavin Howard1cbfe242019-01-09 17:13:11 -07001350static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001351
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001352 size_t i, j, rdx = n->rdx;
1353 bool zero = true;
1354 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001355
Gavin Howard48af52e2018-10-30 14:47:38 -06001356 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001357 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001358
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001359 for (i = n->len - 1; i < n->len; --i) {
1360
1361 BcDig n9 = n->num[i];
1362 size_t temp;
1363 bool irdx = (i == rdx - 1);
1364
1365 zero = (zero & !irdx);
1366 temp = n->scale % BC_BASE_POWER;
1367 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1368
1369 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1370
1371 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Essera299ffc2019-04-24 23:28:32 +02001372 buffer[j] = n9 % BC_BASE;
1373 n9 /= BC_BASE;
1374 }
Stefan Esser0da17752019-04-25 12:25:15 +02001375
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001376 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1377 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1378 zero = (zero && buffer[j] == 0);
1379 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Essera299ffc2019-04-24 23:28:32 +02001380 }
1381 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001382}
1383
Gavin Howard7ad5a662019-02-19 14:40:46 -07001384#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001385static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) {
Gavin Howard7ad5a662019-02-19 14:40:46 -07001386
Gavin Howardfcf29012019-04-25 20:12:19 -06001387 // TODO: Check this function.
1388
Gavin Howardb1558662019-04-26 14:32:55 -06001389 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001390 bool neg = (n->len <= n->rdx);
1391 BcNum temp, exp;
1392 size_t places, mod;
1393 BcDig digs[BC_NUM_LONG_LOG10];
1394
1395 bc_num_createCopy(&temp, n);
1396
1397 if (neg) {
1398 places = n->rdx - bc_num_len(n) + 1;
1399 mod = places % 3;
1400 if (eng && mod != 0) places += 3 - mod;
Gavin Howardb1558662019-04-26 14:32:55 -06001401 s = bc_num_shiftLeft(&temp, places);
1402 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001403 }
1404 else {
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001405 places = bc_num_int(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001406 mod = places % 3;
1407 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howardb1558662019-04-26 14:32:55 -06001408 s = bc_num_shiftRight(&temp, places);
1409 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001410 }
1411
1412 bc_num_printDecimal(&temp);
1413
1414 bc_num_printNewline();
1415 bc_vm_putchar('e');
1416
1417 if (!places) {
1418 bc_num_printHex(0, 1, false);
1419 goto exit;
1420 }
1421
1422 if (neg) {
1423 bc_num_printNewline();
1424 bc_vm_putchar('-');
1425 }
1426
1427 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1428 bc_num_ulong2num(&exp, (unsigned long) places);
1429
1430 bc_num_printDecimal(&exp);
1431
1432exit:
1433 bc_num_free(&temp);
Gavin Howardb1558662019-04-26 14:32:55 -06001434 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001435}
1436#endif // BC_ENABLE_EXTRA_MATH
1437
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001438static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1439 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001440{
Gavin Howardfcf29012019-04-25 20:12:19 -06001441 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001442 BcStatus s;
1443 BcVec stack;
1444 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001445 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001446 size_t i;
1447 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001448
Gavin Howard971a2672019-04-26 14:32:07 -06001449 assert(BC_NUM_NONZERO(base));
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001450
Gavin Howardddce6d42019-01-03 14:07:06 -07001451 if (BC_NUM_ZERO(n)) {
1452 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001453 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001454 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001455
Gavin Howardd3929832018-12-24 15:13:15 -07001456 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001457 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001458 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001459 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001460 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001461 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001462
Gavin Howard63738202018-09-26 15:34:20 -06001463 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001464 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001465 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001466
Gavin Howard2d188a52019-02-25 14:19:08 -07001467 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001468
1469 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001470 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001471 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001472
1473 // Will never fail (except for signals) because digit is
1474 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001475 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001476 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001477
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001478 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001479 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001480
Gavin Howard2d188a52019-02-25 14:19:08 -07001481 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001482
Gavin Howard2d188a52019-02-25 14:19:08 -07001483 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001484 ptr = bc_vec_item_rev(&stack, i);
1485 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001486 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001487 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001488
Gavin Howard2d188a52019-02-25 14:19:08 -07001489 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001490 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001491
Gavin Howard2d188a52019-02-25 14:19:08 -07001492 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001493
Gavin Howard53eba8b2018-10-31 15:14:37 -06001494 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001495 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001496
1497 // Will never fail (except for signals) because fracp is
1498 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001499 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001500 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001501
Gavin Howard705c4bc2018-12-03 15:38:04 -07001502 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001503 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001504 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001505
Gavin Howardddce6d42019-01-03 14:07:06 -07001506 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001507 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001508 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001509 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001510
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001511sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001512 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001513err:
Gavin Howard63738202018-09-26 15:34:20 -06001514 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001515 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001516 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001517 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001518 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001519 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001520}
1521
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001522static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1523 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001524{
Gavin Howardfcf29012019-04-25 20:12:19 -06001525 // TODO: Check this function.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001526 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001527 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001528 BcNumDigitOp print;
1529 bool neg = n->neg;
1530
Gavin Howard48af52e2018-10-30 14:47:38 -06001531 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001532 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001533
1534 n->neg = false;
1535
Gavin Howardbe4e8222019-01-03 13:34:16 -07001536 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001537 width = 1;
1538 print = bc_num_printHex;
1539 }
1540 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001541 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001542 print = bc_num_printDigits;
1543 }
1544
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001545 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001546 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001547
1548 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001549}
1550
Gavin Howard40a085f2018-12-03 12:08:59 -07001551#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001552BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1553 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001554}
1555#endif // DC_ENABLED
1556
Gavin Howard3c02ed32018-12-28 18:17:15 -07001557void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001558 assert(n);
1559 n->num = num;
1560 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001561 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001562 n->neg = false;
1563}
1564
Gavin Howard3c02ed32018-12-28 18:17:15 -07001565void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001566 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001567 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howard404ece72019-04-26 15:59:15 -06001568 bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001569}
1570
Gavin Howarded392aa2018-02-27 13:09:26 -07001571void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001572 assert(num);
1573 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001574}
1575
Gavin Howard3c02ed32018-12-28 18:17:15 -07001576void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001577 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001578 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001579 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001580 d->len = s->len;
1581 d->neg = s->neg;
1582 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001583 d->scale = s->scale;
Gavin Howard404ece72019-04-26 15:59:15 -06001584 memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -07001585}
1586
Gavin Howardbaa4f582019-01-24 13:56:35 -07001587void bc_num_createCopy(BcNum *d, const BcNum *s) {
1588 bc_num_init(d, s->len);
1589 bc_num_copy(d, s);
1590}
1591
1592void bc_num_createFromUlong(BcNum *n, unsigned long val) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001593 bc_num_init(n, (BC_NUM_LONG_LOG10 - 1) / BC_BASE_POWER + 1);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001594 bc_num_ulong2num(n, val);
1595}
1596
Gavin Howard7ad5a662019-02-19 14:40:46 -07001597size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001598 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001599}
1600
1601size_t bc_num_len(const BcNum *restrict n) {
1602
Gavin Howardb41483b2019-04-27 08:30:10 -06001603 size_t i, pow, scale, len = n->len;
Gavin Howardfcf29012019-04-25 20:12:19 -06001604 BcDig dig;
1605
Gavin Howardb41483b2019-04-27 08:30:10 -06001606 if (BC_NUM_ZERO(n)) return 0;
1607
1608 if (n->rdx == len) {
1609 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1610 }
Gavin Howard7ad5a662019-02-19 14:40:46 -07001611
Gavin Howardfcf29012019-04-25 20:12:19 -06001612 dig = n->num[len - 1];
Gavin Howardb41483b2019-04-27 08:30:10 -06001613 pow = BC_BASE_DIG;
1614 i = BC_BASE_POWER + 1;
Gavin Howardfcf29012019-04-25 20:12:19 -06001615
Gavin Howardb41483b2019-04-27 08:30:10 -06001616 while (pow && (dig % (BcDig) pow == dig)) {
1617 i -= 1;
1618 pow /= BC_BASE;
1619 }
Gavin Howardfcf29012019-04-25 20:12:19 -06001620
Gavin Howardb41483b2019-04-27 08:30:10 -06001621 scale = n->scale % BC_BASE_POWER;
1622 scale = scale ? scale : BC_BASE_POWER;
1623
1624 return (len - 1) * BC_BASE_POWER + i - (BC_BASE_POWER - scale);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001625}
1626
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001627BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1628 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001629{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001630 BcStatus s = BC_STATUS_SUCCESS;
1631
Gavin Howard63738202018-09-26 15:34:20 -06001632 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001633 assert(BC_ENABLE_EXTRA_MATH ||
1634 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001635 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001636
Gavin Howardd746e802018-12-28 22:06:00 -07001637 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06001638 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001639 else s = bc_num_parseBase(n, val, base, base_t);
1640
1641 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001642}
1643
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001644BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1645 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001646{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001647 BcStatus s = BC_STATUS_SUCCESS;
1648
Gavin Howardab5e9632019-01-02 13:32:02 -07001649 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001650 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001651
Gavin Howardab5e9632019-01-02 13:32:02 -07001652 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001653
Gavin Howardddce6d42019-01-03 14:07:06 -07001654 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06001655 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001656#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001657 else if (base_t == 0 || base_t == 1)
1658 s = bc_num_printExponent(n, base_t != 0);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001659#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001660 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001661
Gavin Howarde5f11c72019-02-23 09:42:51 -07001662 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001663 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001664 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001665 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001666
1667 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001668}
1669
Gavin Howard3c02ed32018-12-28 18:17:15 -07001670BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001671
Gavin Howard63738202018-09-26 15:34:20 -06001672 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001673 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001674
Gavin Howard63738202018-09-26 15:34:20 -06001675 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001676
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001677 *result = 0;
1678
Gavin Howardecafd4f2019-02-23 09:30:45 -07001679 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001680
Gavin Howard06b6e862019-02-15 11:15:31 -07001681 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001682
Gavin Howard0700efc2019-04-24 17:31:23 -06001683 unsigned long prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07001684
Gavin Howard0700efc2019-04-24 17:31:23 -06001685 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001686 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1687
Gavin Howard77445432019-04-26 15:59:33 -06001688 r = prev + (unsigned long) n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07001689
1690 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001691 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001692 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001693
Gavin Howard7b557da2018-12-21 10:25:32 -07001694 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001695
Gavin Howard63738202018-09-26 15:34:20 -06001696 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001697}
1698
Gavin Howard3c02ed32018-12-28 18:17:15 -07001699void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001700
Gavin Howardc25fd612019-04-26 19:31:31 -06001701 BcDig *ptr;
1702 unsigned long i;
1703
Gavin Howard63738202018-09-26 15:34:20 -06001704 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001705
Gavin Howard63738202018-09-26 15:34:20 -06001706 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001707
Gavin Howard1ab22d22019-01-03 13:32:17 -07001708 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001709
Gavin Howard798508b2019-02-21 16:45:32 -07001710 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howard05feab02019-04-23 16:24:07 -06001711
Gavin Howardc25fd612019-04-26 19:31:31 -06001712 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_DIG)
1713 ptr[i] = val % BC_BASE_DIG;
Gavin Howard025d04d2018-02-20 13:53:28 -07001714}
1715
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001716size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001717
1718 size_t aint, bint, ardx, brdx;
1719
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001720 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001721
1722 ardx = a->rdx;
1723 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001724 aint = bc_num_int(a);
1725 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001726 ardx = BC_MAX(ardx, brdx);
1727 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001728
Gavin Howard2d188a52019-02-25 14:19:08 -07001729 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001730}
1731
1732size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001733 size_t max, rdx;
1734 rdx = bc_vm_growSize(a->rdx, b->rdx);
1735 max = bc_vm_growSize(BC_MAX(scale, rdx), 1);
1736 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1737 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001738}
1739
1740size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1741 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001742 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001743}
1744
1745#if BC_ENABLE_EXTRA_MATH
1746size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1747 BC_UNUSED(b);
1748 BC_UNUSED(scale);
1749 return BC_NUM_SHREQ(a);
1750}
1751#endif // BC_ENABLE_EXTRA_MATH
1752
Gavin Howard12fe7812018-09-29 03:45:18 -06001753BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001754 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001755 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001756 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001757}
1758
Gavin Howard12fe7812018-09-29 03:45:18 -06001759BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001760 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001761 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001762 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001763}
1764
Gavin Howard12fe7812018-09-29 03:45:18 -06001765BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001766 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001767}
1768
Gavin Howard12fe7812018-09-29 03:45:18 -06001769BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001770 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001771}
1772
Gavin Howard54b946a2018-10-23 12:06:57 -06001773BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001774 size_t req = bc_num_mulReq(a, b, scale);
1775 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001776}
1777
Gavin Howard12fe7812018-09-29 03:45:18 -06001778BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001779 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001780}
1781
Gavin Howard7d8f0382018-12-27 16:15:13 -07001782#if BC_ENABLE_EXTRA_MATH
1783BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001784 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001785}
1786
1787BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001788 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001789}
1790
1791BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001792 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001793}
1794#endif // BC_ENABLE_EXTRA_MATH
1795
Gavin Howard3c02ed32018-12-28 18:17:15 -07001796BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001797
Gavin Howardfcf29012019-04-25 20:12:19 -06001798 // TODO: Check this function.
1799
Gavin Howard1cbfe242019-01-09 17:13:11 -07001800 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001801 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard14913fc2019-04-23 14:36:03 -06001802 size_t pow, len, req, digs, digs1, digs2, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001803 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001804 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001805
Gavin Howard34edd0a2018-10-25 15:51:05 -06001806 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001807
Gavin Howardecafd4f2019-02-23 09:30:45 -07001808 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07001809
Gavin Howard007afdf2019-02-23 09:45:42 -07001810 len = bc_vm_growSize(bc_num_int(a), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001811 req = bc_vm_growSize(BC_MAX(scale, a->rdx), len >> 1);
1812 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001813
Gavin Howardddce6d42019-01-03 14:07:06 -07001814 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001815 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001816 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001817 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001818 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001819 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001820 bc_num_extend(b, scale);
1821 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001822 }
Gavin Howard954276b2018-05-16 01:43:58 -06001823
Gavin Howard890d0c02018-10-30 16:34:50 -06001824 scale = BC_MAX(scale, a->rdx) + 1;
Gavin Howard007afdf2019-02-23 09:45:42 -07001825 len = bc_vm_growSize(a->len, scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001826
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001827 bc_num_init(&num1, len);
1828 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001829 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001830
Gavin Howard63738202018-09-26 15:34:20 -06001831 bc_num_one(&half);
1832 half.num[0] = 5;
1833 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001834
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001835 bc_num_init(&f, len);
1836 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001837
Gavin Howard63738202018-09-26 15:34:20 -06001838 x0 = &num1;
1839 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001840
Gavin Howard63738202018-09-26 15:34:20 -06001841 bc_num_one(x0);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001842 pow = bc_num_int(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001843
Gavin Howard53eba8b2018-10-31 15:14:37 -06001844 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001845
Gavin Howardc39fd492018-10-04 10:07:03 -06001846 if (pow & 1) x0->num[0] = 2;
1847 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001848
Gavin Howardc39fd492018-10-04 10:07:03 -06001849 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001850
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001851 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001852
Gavin Howard63738202018-09-26 15:34:20 -06001853 // Make sure to move the radix back.
1854 x0->rdx -= pow;
1855 }
Gavin Howard954276b2018-05-16 01:43:58 -06001856
Gavin Howard14913fc2019-04-23 14:36:03 -06001857 x0->rdx = digs = digs1 = digs2 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001858 resrdx = scale + 2;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001859 len = bc_num_int(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001860
Gavin Howard2d188a52019-02-25 14:19:08 -07001861 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001862
Gavin Howard971a2672019-04-26 14:32:07 -06001863 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07001864
Gavin Howard53eba8b2018-10-31 15:14:37 -06001865 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001866 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001867 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001868 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001869 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001870 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001871 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001872
Gavin Howard53eba8b2018-10-31 15:14:37 -06001873 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06001874 if (cmp == BC_NUM_SSIZE_MIN) {
1875 s = BC_STATUS_SIGNAL;
1876 break;
1877 }
1878
Gavin Howard53eba8b2018-10-31 15:14:37 -06001879 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001880
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001881 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001882 else times = 0;
1883
1884 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001885
1886 cmp2 = cmp1;
1887 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001888 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001889
Gavin Howard63738202018-09-26 15:34:20 -06001890 temp = x0;
1891 x0 = x1;
1892 x1 = temp;
1893 }
Gavin Howard954276b2018-05-16 01:43:58 -06001894
Gavin Howard2d188a52019-02-25 14:19:08 -07001895 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07001896 s = BC_STATUS_SIGNAL;
1897 goto err;
1898 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001899
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001900 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001901 scale -= 1;
1902 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001903
1904err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06001905 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06001906 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001907 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001908 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001909 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001910 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001911 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001912 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001913}
Gavin Howardba009802018-09-29 04:41:51 -06001914
Gavin Howard2cb39612018-10-22 08:46:48 -06001915BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1916
Gavin Howardfcf29012019-04-25 20:12:19 -06001917 // TODO: Check this function.
1918
Gavin Howard2cb39612018-10-22 08:46:48 -06001919 BcStatus s;
1920 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001921 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07001922 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001923
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001924 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001925
Gavin Howard890d0c02018-10-30 16:34:50 -06001926 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001927 memcpy(&num2, c, sizeof(BcNum));
1928 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001929 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001930 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001931 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001932 else {
1933 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001934 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001935 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001936
Gavin Howard54b946a2018-10-23 12:06:57 -06001937 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001938
Gavin Howardddce6d42019-01-03 14:07:06 -07001939 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001940 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001941 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001942 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001943
1944 if (init) bc_num_free(&num2);
1945
1946 return s;
1947}
1948
Gavin Howard40a085f2018-12-03 12:08:59 -07001949#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001950BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001951
Gavin Howardfcf29012019-04-25 20:12:19 -06001952 // TODO: Check this function.
1953
Gavin Howardba009802018-09-29 04:41:51 -06001954 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001955 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001956 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001957
Gavin Howard34edd0a2018-10-25 15:51:05 -06001958 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001959
Gavin Howardecafd4f2019-02-23 09:30:45 -07001960 if (BC_ERR(BC_NUM_ZERO(c)))
1961 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1962 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1963 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07001964 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001965
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001966 bc_num_expand(d, c->len);
1967 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001968 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001969 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001970
1971 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001972 two.num[0] = 2;
1973 bc_num_one(d);
1974
Gavin Howard1769abc2019-02-21 09:36:14 -07001975 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001976 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001977 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001978 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07001979 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001980
Gavin Howard2d188a52019-02-25 14:19:08 -07001981 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001982
Gavin Howard1769abc2019-02-21 09:36:14 -07001983 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001984 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001985 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001986
Gavin Howarda83a5e52019-02-21 16:51:06 -07001987 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001988 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001989 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001990
1991 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001992 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001993 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001994 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001995 }
1996
Gavin Howard53eba8b2018-10-31 15:14:37 -06001997 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001998 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001999
2000 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002001 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002002 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002003 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002004 }
2005
Gavin Howard2d188a52019-02-25 14:19:08 -07002006 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002007
Gavin Howardba009802018-09-29 04:41:51 -06002008err:
Gavin Howardba009802018-09-29 04:41:51 -06002009 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002010rem_err:
2011 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002012 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002013 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002014 return s;
2015}
Gavin Howarde6e84762018-10-03 11:46:34 -06002016#endif // DC_ENABLED