blob: 7ead0e150989ed91c6d2e39973a6b848c3ee82dd [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 Howardb5904bf2018-02-20 13:28:18 -07004 * Copyright 2018 Gavin D. Howard
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070017 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070018 *
19 * Code for the number type.
20 *
21 */
22
23#include <assert.h>
Gavin Howardc7d655b2018-12-28 19:45:13 -070024#include <ctype.h>
Gavin Howard411f7322018-09-26 17:21:19 -060025#include <stdbool.h>
26#include <stdlib.h>
27#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070028
Gavin Howard305249a2018-10-15 20:24:47 -060029#include <limits.h>
30
Gavin Howard29493062018-03-20 19:57:37 -060031#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070032#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060033#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070034
Gavin Howard1cbfe242019-01-09 17:13:11 -070035static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060036 assert(n);
Gavin Howard97102b12018-11-01 23:37:31 -060037 n->len = 0;
38 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060039 n->rdx = scale;
40}
41
Gavin Howard1cbfe242019-01-09 17:13:11 -070042static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060043 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060044}
45
Gavin Howard3c02ed32018-12-28 18:17:15 -070046void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060047 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060048 n->len = 1;
49 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060050}
51
Gavin Howard3c02ed32018-12-28 18:17:15 -070052void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060053 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060054 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060055 n->len = 2;
56 n->num[0] = 0;
57 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060058}
59
Gavin Howard1cbfe242019-01-09 17:13:11 -070060static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -070061 size_t len;
62 for (len = 1; i; i /= 10, ++len);
63 return len;
64}
65
Gavin Howarddc0ee9e2019-02-16 23:27:08 -070066static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
67 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -070068{
Gavin Howard63738202018-09-26 15:34:20 -060069 size_t i, j;
Gavin Howard5dbb48b2019-01-04 11:29:29 -070070 for (i = 0; !BC_SIGNAL && i < len; ++i) {
71 for (a[i] -= b[i], j = 0; !BC_SIGNAL && a[i + j] < 0;) {
Gavin Howard54b946a2018-10-23 12:06:57 -060072 a[i + j++] += 10;
73 a[i + j] -= 1;
Gavin Howard231e66c2019-01-08 23:02:19 -070074 assert(a[i + j - 1] >= 0 && a[i + j - 1] < 10);
Gavin Howard63738202018-09-26 15:34:20 -060075 }
76 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -070077 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060078}
79
Gavin Howard1cbfe242019-01-09 17:13:11 -070080static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b, size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -070081{
Gavin Howard63738202018-09-26 15:34:20 -060082 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -060083 int c = 0;
Gavin Howard5dbb48b2019-01-04 11:29:29 -070084 for (i = len - 1; !BC_SIGNAL && i < len && !(c = a[i] - b[i]); --i);
Gavin Howard7fbb4e22018-10-18 11:43:17 -060085 return BC_NUM_NEG(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -060086}
87
Gavin Howard3c02ed32018-12-28 18:17:15 -070088ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070089
Gavin Howard63738202018-09-26 15:34:20 -060090 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -060091 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060092 bool a_max, neg = false;
93 ssize_t cmp;
94
95 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070096
Gavin Howard8a921bd2018-10-11 14:15:32 -060097 if (a == b) return 0;
Gavin Howard6d38ef42019-01-22 09:54:05 -070098 if (BC_NUM_ZERO(a)) return BC_NUM_NEG(b->len != 0, !b->neg);
Gavin Howardddce6d42019-01-03 14:07:06 -070099 if (BC_NUM_ZERO(b)) return BC_NUM_CMP_ZERO(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600100 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600101 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600102 else return -1;
103 }
104 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700105
Gavin Howard63738202018-09-26 15:34:20 -0600106 a_int = BC_NUM_INT(a);
107 b_int = BC_NUM_INT(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600108 a_int -= b_int;
109 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700110
Gavin Howard1ab22d22019-01-03 13:32:17 -0700111 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700112
Gavin Howard890d0c02018-10-30 16:34:50 -0600113 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600114 min = b->rdx;
115 diff = a->rdx - b->rdx;
116 max_num = a->num + diff;
117 min_num = b->num;
118 }
119 else {
120 min = a->rdx;
121 diff = b->rdx - a->rdx;
122 max_num = b->num + diff;
123 min_num = a->num;
124 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700125
Gavin Howard890d0c02018-10-30 16:34:50 -0600126 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700127 if (cmp) return BC_NUM_NEG(cmp, (!a_max) != neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700128
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700129 for (max_num -= diff, i = diff - 1; !BC_SIGNAL && i < diff; --i) {
Gavin Howard40974152018-10-18 14:23:01 -0600130 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
Gavin Howard63738202018-09-26 15:34:20 -0600131 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700132
Gavin Howard63738202018-09-26 15:34:20 -0600133 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700134}
135
Gavin Howard2a366922019-01-16 10:50:20 -0700136static void bc_num_clean(BcNum *restrict n) {
137 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
138 if (BC_NUM_ZERO(n)) n->neg = false;
139 else if (n->len < n->rdx) n->len = n->rdx;
140}
141
Gavin Howard3c02ed32018-12-28 18:17:15 -0700142void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700143
Gavin Howardddce6d42019-01-03 14:07:06 -0700144 assert(places <= n->rdx && (BC_NUM_ZERO(n) || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700145
Gavin Howard1ab22d22019-01-03 13:32:17 -0700146 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700147
Gavin Howard63738202018-09-26 15:34:20 -0600148 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700149
Gavin Howardddce6d42019-01-03 14:07:06 -0700150 if (BC_NUM_NONZERO(n)) {
Gavin Howard955da852018-10-23 11:08:20 -0600151 n->len -= places;
152 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
Gavin Howard2a366922019-01-16 10:50:20 -0700153 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600154 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700155}
156
Gavin Howard1cbfe242019-01-09 17:13:11 -0700157static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700158
Gavin Howard88c25302018-10-17 13:32:23 -0600159 size_t len = n->len + places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700160
Gavin Howard1ab22d22019-01-03 13:32:17 -0700161 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700162
Gavin Howard1ab22d22019-01-03 13:32:17 -0700163 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700164
Gavin Howard1ab22d22019-01-03 13:32:17 -0700165 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
166 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700167
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700168 if (n->len) n->len += places;
169
Gavin Howard1ab22d22019-01-03 13:32:17 -0700170 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700171}
172
Gavin Howard1cbfe242019-01-09 17:13:11 -0700173static void bc_num_retireMul(BcNum *restrict n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600174
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600175 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600176 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600177
Gavin Howard305249a2018-10-15 20:24:47 -0600178 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700179 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600180}
181
Gavin Howard1cbfe242019-01-09 17:13:11 -0700182static void bc_num_split(const BcNum *restrict n, size_t idx, BcNum *restrict a, BcNum *restrict b) {
Gavin Howard50c80022019-01-03 15:14:33 -0700183
Gavin Howard305249a2018-10-15 20:24:47 -0600184 if (idx < n->len) {
185
Gavin Howard305249a2018-10-15 20:24:47 -0600186 b->len = n->len - idx;
187 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600188 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600189
190 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
191 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard50c80022019-01-03 15:14:33 -0700192
193 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600194 }
Gavin Howard50c80022019-01-03 15:14:33 -0700195 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600196
197 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600198}
199
Gavin Howard7ad5a662019-02-19 14:40:46 -0700200static void bc_num_shift_l(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600201
Gavin Howardd8b004b2019-02-16 22:35:08 -0700202 if (!places || BC_NUM_ZERO(n)) return;
Gavin Howard305249a2018-10-15 20:24:47 -0600203
204 if (n->rdx >= places) n->rdx -= places;
205 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600206 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600207 n->rdx = 0;
208 }
209
210 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600211}
212
Gavin Howard7ad5a662019-02-19 14:40:46 -0700213#if BC_ENABLE_EXTRA_MATH
214static void bc_num_shift_r(BcNum *restrict n, size_t places) {
215
216 size_t len;
217
218 len = n->rdx + places;
219
220 if (len > n->len) {
221
222 if (len > n->cap) bc_num_expand(n, len);
223
224 memset(n->num + n->len, 0, len - n->len);
225 n->len = len;
226 }
227
228 n->rdx += places;
229
230 assert(n->rdx <= n->len && n->len <= n->cap);
231}
232#endif // BC_ENABLE_EXTRA_MATH
233
Gavin Howard1cbfe242019-01-09 17:13:11 -0700234static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600235
Gavin Howard63738202018-09-26 15:34:20 -0600236 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600237 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600238
Gavin Howard1769abc2019-02-21 09:36:14 -0700239 assert(!BC_NUM_ZERO(a));
240
Gavin Howardd32d7df2018-10-15 08:42:25 -0600241 one.cap = 2;
242 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600243 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600244
Gavin Howardd32d7df2018-10-15 08:42:25 -0600245 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600246}
247
Gavin Howard7bda4782018-12-28 09:53:22 -0700248#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700249static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
250 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700251{
Gavin Howardac4d9122018-12-27 23:59:12 -0700252 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
253 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700254 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700255}
Gavin Howard7bda4782018-12-28 09:53:22 -0700256#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700257
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700258static unsigned int bc_num_addDigit(BcDig *restrict num, unsigned int d, unsigned int c)
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700259{
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700260 d += c;
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700261 *num = (BcDig) (d % 10);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700262 assert(*num >= 0 && *num < 10);
Gavin Howard201cb6f2019-01-22 10:01:29 -0700263 assert((d / 10) < 10);
264 return d / 10;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700265}
266
Gavin Howard1cbfe242019-01-09 17:13:11 -0700267static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700268
Gavin Howard8a921bd2018-10-11 14:15:32 -0600269 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600270 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700271 unsigned int carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700272
Gavin Howard63738202018-09-26 15:34:20 -0600273 // Because this function doesn't need to use scale (per the bc spec),
274 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600275
Gavin Howardddce6d42019-01-03 14:07:06 -0700276 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600277 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700278 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600279 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600280 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700281 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600282 bc_num_copy(c, a);
283 return BC_STATUS_SUCCESS;
284 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600285
Gavin Howard63738202018-09-26 15:34:20 -0600286 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600287 c->rdx = BC_MAX(a->rdx, b->rdx);
288 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700289
Gavin Howard63738202018-09-26 15:34:20 -0600290 if (a->rdx > b->rdx) {
291 diff = a->rdx - b->rdx;
292 ptr = a->num;
293 ptr_a = a->num + diff;
294 ptr_b = b->num;
295 }
296 else {
297 diff = b->rdx - a->rdx;
298 ptr = b->num;
299 ptr_a = a->num;
300 ptr_b = b->num + diff;
301 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700302
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700303 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700304
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700305 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600306 ptr_c += diff;
Gavin Howard890d0c02018-10-30 16:34:50 -0600307 a_int = BC_NUM_INT(a);
308 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700309
Gavin Howard890d0c02018-10-30 16:34:50 -0600310 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600311 min_int = b_int;
312 max = a_int;
313 ptr = ptr_a;
314 }
315 else {
316 min_int = a_int;
317 max = b_int;
318 ptr = ptr_b;
319 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700320
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700321 for (carry = 0, i = 0; !BC_SIGNAL && i < min_rdx + min_int; ++i) {
322 unsigned int in = (unsigned int) (ptr_a[i] + ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700323 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600324 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700325
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700326 for (; !BC_SIGNAL && i < max + min_rdx; ++i)
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700327 carry = bc_num_addDigit(ptr_c + i, (unsigned int) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700328
329 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700330
Gavin Howard1ab22d22019-01-03 13:32:17 -0700331 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600332
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700333 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700334}
335
Gavin Howard1cbfe242019-01-09 17:13:11 -0700336static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700337
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700338 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600339 ssize_t cmp;
340 BcNum *minuend, *subtrahend;
341 size_t start;
342 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700343
Gavin Howard63738202018-09-26 15:34:20 -0600344 // Because this function doesn't need to use scale (per the bc spec),
345 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700346
Gavin Howardddce6d42019-01-03 14:07:06 -0700347 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600348 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700349 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600350 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600351 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700352 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600353 bc_num_copy(c, a);
354 return BC_STATUS_SUCCESS;
355 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700356
Gavin Howard63738202018-09-26 15:34:20 -0600357 aneg = a->neg;
358 bneg = b->neg;
359 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700360
Gavin Howard63738202018-09-26 15:34:20 -0600361 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700362
Gavin Howard63738202018-09-26 15:34:20 -0600363 a->neg = aneg;
364 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700365
Gavin Howard1ab22d22019-01-03 13:32:17 -0700366 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600367 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600368 return BC_STATUS_SUCCESS;
369 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700370
371 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600372 neg = a->neg;
373 minuend = a;
374 subtrahend = b;
375 }
376 else {
377 neg = b->neg;
378 if (sub) neg = !neg;
379 minuend = b;
380 subtrahend = a;
381 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700382
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600383 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600384 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700385
Gavin Howard63738202018-09-26 15:34:20 -0600386 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600387 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600388 start = 0;
389 }
390 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700391
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700392 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700393
Gavin Howard63738202018-09-26 15:34:20 -0600394 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700395
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700396 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700397}
398
Gavin Howard1cbfe242019-01-09 17:13:11 -0700399static BcStatus bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600400
Gavin Howard9b801632019-02-16 23:34:06 -0700401 BcStatus s;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700402 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600403 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard63738202018-09-26 15:34:20 -0600404 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600405
Gavin Howardddce6d42019-01-03 14:07:06 -0700406 // This is here because the function is recursive.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700407 if (BC_SIGNAL) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700408 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600409 bc_num_zero(c);
410 return BC_STATUS_SUCCESS;
411 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700412 if (aone || BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600413 bc_num_copy(c, aone ? b : a);
414 return BC_STATUS_SUCCESS;
415 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700416
Gavin Howardb5093d72018-10-17 12:13:17 -0600417 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
418 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600419 {
Gavin Howard34c4ba02018-12-11 10:25:01 -0700420 size_t i, j, len;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700421 unsigned int carry;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700422 BcDig *ptr_c;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700423
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600424 bc_num_expand(c, a->len + b->len + 1);
Gavin Howard23d8d252018-10-17 12:03:41 -0600425
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700426 ptr_c = c->num;
427 memset(ptr_c, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700428 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700429
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700430 for (i = 0; !BC_SIGNAL && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600431
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700432 BcDig *ptr = ptr_c + i;
433
Gavin Howard34c4ba02018-12-11 10:25:01 -0700434 carry = 0;
435
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700436 for (j = 0; !BC_SIGNAL && j < a->len; ++j) {
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700437 unsigned int in = (uchar) ptr[j];
438 assert(in < 10);
Gavin Howard2aef61b2018-12-18 10:56:20 -0700439 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700440 carry = bc_num_addDigit(ptr + j, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600441 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700442
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700443 ptr[j] += (BcDig) carry;
444 assert(ptr[j] >= 0 && ptr[j] < 10);
445 len = BC_MAX(len, i + j + (carry != 0));
Gavin Howard63738202018-09-26 15:34:20 -0600446 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700447
Gavin Howard305249a2018-10-15 20:24:47 -0600448 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600449
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700450 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600451 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700452
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600453 bc_num_init(&l1, max);
454 bc_num_init(&h1, max);
455 bc_num_init(&l2, max);
456 bc_num_init(&h2, max);
457 bc_num_init(&m1, max);
458 bc_num_init(&m2, max);
459 bc_num_init(&z0, max);
460 bc_num_init(&z1, max);
461 bc_num_init(&z2, max);
462 bc_num_init(&temp, max + max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600463
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600464 bc_num_split(a, max2, &l1, &h1);
465 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700466
Gavin Howard53eba8b2018-10-31 15:14:37 -0600467 s = bc_num_add(&h1, &l1, &m1, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700468 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600469 s = bc_num_add(&h2, &l2, &m2, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700470 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600471
Gavin Howard53eba8b2018-10-31 15:14:37 -0600472 s = bc_num_k(&h1, &h2, &z0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700473 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600474 s = bc_num_k(&m1, &m2, &z1);
Gavin Howard1769abc2019-02-21 09:36:14 -0700475 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600476 s = bc_num_k(&l1, &l2, &z2);
Gavin Howard1769abc2019-02-21 09:36:14 -0700477 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600478
Gavin Howard53eba8b2018-10-31 15:14:37 -0600479 s = bc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700480 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600481 s = bc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700482 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600483
Gavin Howard7ad5a662019-02-19 14:40:46 -0700484 bc_num_shift_l(&z0, max2 * 2);
485 bc_num_shift_l(&z1, max2);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600486 s = bc_num_add(&z0, &z1, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700487 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600488 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600489
490err:
491 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600492 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600493 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600494 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600495 bc_num_free(&m2);
Gavin Howard305249a2018-10-15 20:24:47 -0600496 bc_num_free(&m1);
Gavin Howard305249a2018-10-15 20:24:47 -0600497 bc_num_free(&h2);
Gavin Howard305249a2018-10-15 20:24:47 -0600498 bc_num_free(&l2);
Gavin Howard305249a2018-10-15 20:24:47 -0600499 bc_num_free(&h1);
Gavin Howard305249a2018-10-15 20:24:47 -0600500 bc_num_free(&l1);
501 return s;
502}
503
Gavin Howard1cbfe242019-01-09 17:13:11 -0700504static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600505
506 BcStatus s;
507 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600508 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600509
510 scale = BC_MAX(scale, a->rdx);
511 scale = BC_MAX(scale, b->rdx);
512 scale = BC_MIN(a->rdx + b->rdx, scale);
513 maxrdx = BC_MAX(maxrdx, scale);
514
Gavin Howardbaa4f582019-01-24 13:56:35 -0700515 bc_num_createCopy(&cpa, a);
516 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600517
Gavin Howard23d8d252018-10-17 12:03:41 -0600518 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600519
Gavin Howard7ad5a662019-02-19 14:40:46 -0700520 bc_num_shift_l(&cpa, maxrdx);
521 bc_num_shift_l(&cpb, maxrdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600522 s = bc_num_k(&cpa, &cpb, c);
Gavin Howard1769abc2019-02-21 09:36:14 -0700523 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600524
Gavin Howard53eba8b2018-10-31 15:14:37 -0600525 maxrdx += scale;
526 bc_num_expand(c, c->len + maxrdx);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600527
Gavin Howard88c25302018-10-17 13:32:23 -0600528 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600529 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600530 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600531 }
532
Gavin Howard88c25302018-10-17 13:32:23 -0600533 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600534 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600535
536err:
537 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600538 bc_num_free(&cpa);
539 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700540}
541
Gavin Howard1cbfe242019-01-09 17:13:11 -0700542static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700543
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600544 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600545 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600546 size_t len, end, i;
547 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600548 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700549
Gavin Howardddce6d42019-01-03 14:07:06 -0700550 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
551 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600552 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600553 return BC_STATUS_SUCCESS;
554 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700555 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600556 bc_num_copy(c, a);
557 bc_num_retireMul(c, scale, a->neg, b->neg);
558 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600559 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700560
Gavin Howard620a8af2019-01-24 13:54:54 -0700561 bc_num_init(&cp, bc_num_mulReq(a, b, scale));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600562 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600563 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700564
Gavin Howard890d0c02018-10-30 16:34:50 -0600565 if (len > cp.len) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600566 bc_num_expand(&cp, len + 2);
567 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600568 }
Gavin Howard021150b2018-03-10 15:40:42 -0700569
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600570 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600571 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600572 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700573
Gavin Howard63738202018-09-26 15:34:20 -0600574 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600575 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600576 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600577 len -= i - 1;
578 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700579
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600580 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700581
Gavin Howard63738202018-09-26 15:34:20 -0600582 // We want an extra zero in front to make things simpler.
583 cp.num[cp.len++] = 0;
584 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700585
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600586 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700587
Gavin Howarda2514a02018-10-18 14:20:09 -0600588 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600589 c->rdx = cp.rdx;
590 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600591 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700592
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700593 for (i = end - 1; !BC_SIGNAL && !s && i < end; --i) {
Gavin Howard63738202018-09-26 15:34:20 -0600594 n = cp.num + i;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700595 for (q = 0; !s && (n[len] || bc_num_compare(n, p, len) >= 0); ++q)
596 s = bc_num_subArrays(n, p, len);
Gavin Howard63738202018-09-26 15:34:20 -0600597 c->num[i] = q;
598 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700599
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600600 if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600601 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600602
Gavin Howard63738202018-09-26 15:34:20 -0600603 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700604}
605
Gavin Howard1cbfe242019-01-09 17:13:11 -0700606static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
607 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600608{
Gavin Howard63738202018-09-26 15:34:20 -0600609 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600610 BcNum temp;
611 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700612
Gavin Howardddce6d42019-01-03 14:07:06 -0700613 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
614 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700615 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600616 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600617 return BC_STATUS_SUCCESS;
618 }
Gavin Howard8b254872018-03-14 01:13:35 -0600619
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600620 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -0700621 s = bc_num_d(a, b, c, scale);
622 assert(!s || s == BC_STATUS_SIGNAL);
623 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700624
Gavin Howard1ab22d22019-01-03 13:32:17 -0700625 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600626
Gavin Howard53eba8b2018-10-31 15:14:37 -0600627 s = bc_num_m(c, b, &temp, scale);
Gavin Howard1769abc2019-02-21 09:36:14 -0700628 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600629 s = bc_num_sub(a, &temp, d, scale);
Gavin Howard1769abc2019-02-21 09:36:14 -0700630 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600631
Gavin Howardddce6d42019-01-03 14:07:06 -0700632 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600633
634 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600635 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600636 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700637
638err:
Gavin Howard18441082018-10-22 10:03:30 -0600639 bc_num_free(&temp);
640 return s;
641}
642
Gavin Howard1cbfe242019-01-09 17:13:11 -0700643static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600644
Gavin Howard18441082018-10-22 10:03:30 -0600645 BcStatus s;
646 BcNum c1;
Gavin Howard620a8af2019-01-24 13:54:54 -0700647 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard18441082018-10-22 10:03:30 -0600648
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600649 bc_num_init(&c1, len);
Gavin Howard54b946a2018-10-23 12:06:57 -0600650 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600651 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600652
Gavin Howard63738202018-09-26 15:34:20 -0600653 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700654}
655
Gavin Howard1cbfe242019-01-09 17:13:11 -0700656static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700657
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600658 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600659 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700660 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600661 size_t i, powrdx, resrdx;
662 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700663
Gavin Howardac4d9122018-12-27 23:59:12 -0700664 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700665
Gavin Howardddce6d42019-01-03 14:07:06 -0700666 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600667 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600668 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600669 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700670 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600671 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600672 return BC_STATUS_SUCCESS;
673 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700674 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600675 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600676 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600677 return s;
678 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700679
Gavin Howard63738202018-09-26 15:34:20 -0600680 neg = b->neg;
681 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600682 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700683 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600684 if (s) return s;
685
Gavin Howardbaa4f582019-01-24 13:56:35 -0700686 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700687
Gavin Howard63738202018-09-26 15:34:20 -0600688 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600689
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700690 for (powrdx = a->rdx; !BC_SIGNAL && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600691 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600692 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -0700693 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600694 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700695
Gavin Howard9b801632019-02-16 23:34:06 -0700696 if (BC_SIGNAL) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -0700697
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600698 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700699 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600700
Gavin Howard4d5daba2019-01-07 14:42:50 -0700701 while (!BC_SIGNAL && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600702
Gavin Howard4d5daba2019-01-07 14:42:50 -0700703 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600704 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -0700705 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600706
Gavin Howard63738202018-09-26 15:34:20 -0600707 if (pow & 1) {
708 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600709 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -0700710 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600711 }
712 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700713
Gavin Howard9b801632019-02-16 23:34:06 -0700714 if (BC_SIGNAL) goto sig_err;
715 if (neg) {
716 s = bc_num_inv(c, c, scale);
Gavin Howard1769abc2019-02-21 09:36:14 -0700717 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -0700718 }
719
Gavin Howard63738202018-09-26 15:34:20 -0600720 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700721
Gavin Howard53eba8b2018-10-31 15:14:37 -0600722 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600723 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600724 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700725
Gavin Howard9b801632019-02-16 23:34:06 -0700726sig_err:
727 if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700728err:
Gavin Howard63738202018-09-26 15:34:20 -0600729 bc_num_free(&copy);
730 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700731}
732
Gavin Howard7bda4782018-12-28 09:53:22 -0700733#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700734static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700735
736 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700737 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700738
739 BC_UNUSED(scale);
740
741 s = bc_num_intop(a, b, c, &val);
742 if (s) return s;
743
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700744 if (val < c->rdx) bc_num_truncate(c, c->rdx - val);
745 else if (val > c->rdx) bc_num_extend(c, val - c->rdx);
Gavin Howardac4d9122018-12-27 23:59:12 -0700746
747 return s;
748}
749
Gavin Howard1cbfe242019-01-09 17:13:11 -0700750static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700751
752 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700753 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700754
755 BC_UNUSED(scale);
756
757 s = bc_num_intop(a, b, c, &val);
758 if (s) return s;
759
Gavin Howard7ad5a662019-02-19 14:40:46 -0700760 bc_num_shift_l(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -0700761
762 return s;
763}
764
Gavin Howard1cbfe242019-01-09 17:13:11 -0700765static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700766
767 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700768 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700769
770 BC_UNUSED(scale);
771
772 s = bc_num_intop(a, b, c, &val);
773 if (s) return s;
774
Gavin Howard2a366922019-01-16 10:50:20 -0700775 if (BC_NUM_ZERO(c)) return s;
776
Gavin Howard7ad5a662019-02-19 14:40:46 -0700777 bc_num_shift_r(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -0700778
779 return s;
780}
Gavin Howard7bda4782018-12-28 09:53:22 -0700781#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700782
Gavin Howard1cbfe242019-01-09 17:13:11 -0700783static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
784 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700785{
Gavin Howarda1c44392018-09-27 12:41:15 -0600786 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600787 BcNum num2, *ptr_a, *ptr_b;
788 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700789
Gavin Howard63738202018-09-26 15:34:20 -0600790 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700791
Gavin Howard890d0c02018-10-30 16:34:50 -0600792 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600793 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600794 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600795 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600796 }
797 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700798
Gavin Howard63738202018-09-26 15:34:20 -0600799 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600800 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600801 if (c != a) {
802 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600803 init = true;
804 }
805 }
806 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700807
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600808 if (init) bc_num_init(c, req);
809 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700810
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600811 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700812
Gavin Howardddce6d42019-01-03 14:07:06 -0700813 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700814 assert(c->rdx <= c->len || !c->len);
Gavin Howard3cf769e2018-10-11 13:55:11 -0600815
Gavin Howardba009802018-09-29 04:41:51 -0600816 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600817
Gavin Howardee9d01e2019-02-16 22:48:11 -0700818 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700819}
820
Gavin Howardc90ed902019-01-02 13:07:49 -0700821#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -0700822static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700823
Gavin Howardc7d655b2018-12-28 19:45:13 -0700824 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -0600825 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700826
Gavin Howard63738202018-09-26 15:34:20 -0600827 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700828
Gavin Howard63738202018-09-26 15:34:20 -0600829 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700830
Gavin Howard8a921bd2018-10-11 14:15:32 -0600831 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600832
Gavin Howard63738202018-09-26 15:34:20 -0600833 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700834
Gavin Howard63738202018-09-26 15:34:20 -0600835 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700836
Gavin Howard63738202018-09-26 15:34:20 -0600837 radix = true;
838 continue;
839 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700840
Gavin Howardc7d655b2018-12-28 19:45:13 -0700841 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -0600842 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700843
Gavin Howard63738202018-09-26 15:34:20 -0600844 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700845}
Gavin Howardc90ed902019-01-02 13:07:49 -0700846#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -0700847
Gavin Howard1cbfe242019-01-09 17:13:11 -0700848static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -0700849
850 if (isupper(c)) {
851 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -0700852 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -0700853 }
854 else c -= '0';
855
856 return (unsigned long) (uchar) c;
857}
858
Gavin Howard1cbfe242019-01-09 17:13:11 -0700859static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700860
Gavin Howard63738202018-09-26 15:34:20 -0600861 size_t len, i;
862 const char *ptr;
863 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700864
Gavin Howard63738202018-09-26 15:34:20 -0600865 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700866
Gavin Howard63738202018-09-26 15:34:20 -0600867 val += i;
868 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700869
Gavin Howard1ab22d22019-01-03 13:32:17 -0700870 if (len) {
871 for (i = 0; zero && i < len; ++i) zero = (val[i] == '0') || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600872 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600873 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600874
Gavin Howard63738202018-09-26 15:34:20 -0600875 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700876
Gavin Howard63738202018-09-26 15:34:20 -0600877 // Explicitly test for NULL here to produce either a 0 or 1.
878 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700879
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600880 if (!zero) {
Gavin Howard8dd307e2019-01-08 23:05:19 -0700881 for (i = len - 1; i < len; ++n->len, --i) {
882
Gavin Howardc7d655b2018-12-28 19:45:13 -0700883 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -0700884
885 if (c == '.') n->len -= 1;
886 else {
887 if (isupper(c)) c = '9';
888 n->num[n->len] = c - '0';
889 }
Gavin Howardc7d655b2018-12-28 19:45:13 -0700890 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600891 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700892}
893
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700894static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
895 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -0700896{
897 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600898 BcNum temp, mult, result;
Gavin Howard1cbfe242019-01-09 17:13:11 -0700899 BcDig c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600900 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600901 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -0700902 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700903
Gavin Howard11b9afd2018-10-18 14:23:28 -0600904 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700905 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700906
Gavin Howard26d078f2019-01-24 13:58:12 -0700907 bc_num_init(&temp, BC_NUM_LONG_LOG10);
908 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -0700909
Gavin Howardc7d655b2018-12-28 19:45:13 -0700910 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -0700911
Gavin Howardc7d655b2018-12-28 19:45:13 -0700912 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -0700913
Gavin Howard53eba8b2018-10-31 15:14:37 -0600914 s = bc_num_mul(n, base, &mult, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700915 if (BC_STATUS_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700916 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600917 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700918 if (BC_STATUS_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600919 }
Gavin Howardede51f02018-03-02 12:30:00 -0700920
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700921 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600922
Gavin Howard63738202018-09-26 15:34:20 -0600923 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600924 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -0600925 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700926
Gavin Howard1769abc2019-02-21 09:36:14 -0700927 for (i += 1, digs = 0; !BC_SIGNAL && i < len && (c = val[i]); ++i, ++digs) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700928
Gavin Howardc7d655b2018-12-28 19:45:13 -0700929 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -0700930
Gavin Howard53eba8b2018-10-31 15:14:37 -0600931 s = bc_num_mul(&result, base, &result, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700932 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700933
Gavin Howard705c4bc2018-12-03 15:38:04 -0700934 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600935 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700936 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600937 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700938 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600939 }
Gavin Howardede51f02018-03-02 12:30:00 -0700940
Gavin Howard1769abc2019-02-21 09:36:14 -0700941 // This one cannot be a divide by 0 because mult starts out at 1, then is
942 // multiplied by base, and base cannot be 0, so mult cannot be 0.
943 s = bc_num_div(&result, &mult, &result, digs);
944 assert(!s || s == BC_STATUS_SIGNAL);
945 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
946 s = bc_num_add(n, &result, n, digs);
947 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -0700948
Gavin Howardddce6d42019-01-03 14:07:06 -0700949 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -0700950 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600951 }
952 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700953
Gavin Howardd1417682019-02-15 17:08:42 -0700954err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700955 bc_num_free(&result);
956int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600957 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -0600958 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700959 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700960}
961
Gavin Howard1cbfe242019-01-09 17:13:11 -0700962static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -0700963 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -0600964 bc_vm_putchar('\\');
965 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -0700966 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600967 }
Gavin Howard80977b22018-09-06 14:29:13 -0600968}
969
Gavin Howard40a085f2018-12-03 12:08:59 -0700970#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -0700971static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -0700972 BC_UNUSED(rdx);
973 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -0700974 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600975}
976#endif // DC_ENABLED
977
Gavin Howard1cbfe242019-01-09 17:13:11 -0700978static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
979
Gavin Howarda84ad992018-12-03 19:11:06 -0700980 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -0600981
Gavin Howardab5e9632019-01-02 13:32:02 -0700982 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700983 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -0700984 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600985
Gavin Howardab5e9632019-01-02 13:32:02 -0700986 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700987 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600988
Gavin Howardab5e9632019-01-02 13:32:02 -0700989 for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -0700990 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -0700991 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700992 dig = n / pow;
993 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -0700994 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -0600995 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700996}
Gavin Howardfe679f02018-02-14 15:50:09 -0700997
Gavin Howard1cbfe242019-01-09 17:13:11 -0700998static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -0700999
Gavin Howard3c02ed32018-12-28 18:17:15 -07001000 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001001
Gavin Howard3c02ed32018-12-28 18:17:15 -07001002 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001003 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001004 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001005 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001006 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001007
Gavin Howardab5e9632019-01-02 13:32:02 -07001008 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001009 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001010 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001011}
1012
Gavin Howard1cbfe242019-01-09 17:13:11 -07001013static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001014
Gavin Howard63738202018-09-26 15:34:20 -06001015 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001016
Gavin Howard48af52e2018-10-30 14:47:38 -06001017 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001018 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001019
Gavin Howard48af52e2018-10-30 14:47:38 -06001020 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001021 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001022}
1023
Gavin Howard7ad5a662019-02-19 14:40:46 -07001024#if BC_ENABLE_EXTRA_MATH
1025static void bc_num_printExponent(const BcNum *restrict n, bool eng) {
1026
1027 bool neg = (n->len <= n->rdx);
1028 BcNum temp, exp;
1029 size_t places, mod;
1030 BcDig digs[BC_NUM_LONG_LOG10];
1031
1032 bc_num_createCopy(&temp, n);
1033
1034 if (neg) {
1035 places = n->rdx - bc_num_len(n) + 1;
1036 mod = places % 3;
1037 if (eng && mod != 0) places += 3 - mod;
1038 bc_num_shift_l(&temp, places);
1039 }
1040 else {
1041 places = BC_NUM_INT(n) - 1;
1042 mod = places % 3;
1043 if (eng && mod != 0) places -= 3 - (3 - mod);
1044 bc_num_shift_r(&temp, places);
1045 }
1046
1047 bc_num_printDecimal(&temp);
1048
1049 bc_num_printNewline();
1050 bc_vm_putchar('e');
1051
1052 if (!places) {
1053 bc_num_printHex(0, 1, false);
1054 goto exit;
1055 }
1056
1057 if (neg) {
1058 bc_num_printNewline();
1059 bc_vm_putchar('-');
1060 }
1061
1062 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1063 bc_num_ulong2num(&exp, (unsigned long) places);
1064
1065 bc_num_printDecimal(&exp);
1066
1067exit:
1068 bc_num_free(&temp);
1069}
1070#endif // BC_ENABLE_EXTRA_MATH
1071
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001072static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1073 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001074{
Gavin Howard63738202018-09-26 15:34:20 -06001075 BcStatus s;
1076 BcVec stack;
1077 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001078 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001079 size_t i;
1080 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001081
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001082 assert(!BC_NUM_ZERO(base));
1083
Gavin Howardddce6d42019-01-03 14:07:06 -07001084 if (BC_NUM_ZERO(n)) {
1085 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001086 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001087 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001088
Gavin Howardd3929832018-12-24 15:13:15 -07001089 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001090 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001091 bc_num_init(&digit, len);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001092 bc_num_init(&frac_len, BC_NUM_INT(n));
Gavin Howard63738202018-09-26 15:34:20 -06001093 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001094 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001095
Gavin Howard63738202018-09-26 15:34:20 -06001096 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001097 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001098 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001099
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001100 while (!BC_SIGNAL && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001101
1102 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001103 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001104 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1105
1106 // Will never fail (except for signals) because digit is
1107 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001108 s = bc_num_ulong(&digit, &dig);
Gavin Howard1769abc2019-02-21 09:36:14 -07001109 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1110
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001111 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001112 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001113
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001114 if (BC_SIGNAL) goto sig_err;
1115
1116 for (i = 0; !BC_SIGNAL && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001117 ptr = bc_vec_item_rev(&stack, i);
1118 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001119 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001120 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001121
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001122 if (BC_SIGNAL) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001123 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001124
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001125 for (radix = true; !BC_SIGNAL && frac_len.len <= n->rdx; radix = false) {
1126
Gavin Howard53eba8b2018-10-31 15:14:37 -06001127 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001128 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1129
1130 // Will never fail (except for signals) because fracp is
1131 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001132 s = bc_num_ulong(&fracp, &dig);
Gavin Howard1769abc2019-02-21 09:36:14 -07001133 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001134
Gavin Howard705c4bc2018-12-03 15:38:04 -07001135 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001136 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001137 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001138
Gavin Howardddce6d42019-01-03 14:07:06 -07001139 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001140 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001141 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001142 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001143
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001144sig_err:
Gavin Howard9b801632019-02-16 23:34:06 -07001145 if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001146err:
Gavin Howard63738202018-09-26 15:34:20 -06001147 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001148 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001149 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001150 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001151 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001152 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001153}
1154
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001155static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1156 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001157{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001158 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001159 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001160 BcNumDigitOp print;
1161 bool neg = n->neg;
1162
Gavin Howard48af52e2018-10-30 14:47:38 -06001163 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001164 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001165
1166 n->neg = false;
1167
Gavin Howardbe4e8222019-01-03 13:34:16 -07001168 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001169 width = 1;
1170 print = bc_num_printHex;
1171 }
1172 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001173 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001174 print = bc_num_printDigits;
1175 }
1176
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001177 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001178 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001179
1180 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001181}
1182
Gavin Howard40a085f2018-12-03 12:08:59 -07001183#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001184BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1185 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001186}
1187#endif // DC_ENABLED
1188
Gavin Howard3c02ed32018-12-28 18:17:15 -07001189void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001190 assert(n);
1191 n->num = num;
1192 n->cap = cap;
1193 n->rdx = n->len = 0;
1194 n->neg = false;
1195}
1196
Gavin Howard3c02ed32018-12-28 18:17:15 -07001197void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001198 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001199 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001200 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001201}
1202
Gavin Howard3c02ed32018-12-28 18:17:15 -07001203void bc_num_expand(BcNum *restrict n, size_t req) {
Gavin Howard844e7a52018-10-09 11:46:39 -06001204 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001205 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1206 if (req > n->cap) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001207 n->num = bc_vm_realloc(n->num, req);
1208 n->cap = req;
1209 }
Gavin Howardb5c77212018-02-14 17:12:34 -07001210}
1211
Gavin Howarded392aa2018-02-27 13:09:26 -07001212void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001213 assert(num);
1214 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001215}
1216
Gavin Howard3c02ed32018-12-28 18:17:15 -07001217void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001218 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001219 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001220 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001221 d->len = s->len;
1222 d->neg = s->neg;
1223 d->rdx = s->rdx;
1224 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001225}
1226
Gavin Howardbaa4f582019-01-24 13:56:35 -07001227void bc_num_createCopy(BcNum *d, const BcNum *s) {
1228 bc_num_init(d, s->len);
1229 bc_num_copy(d, s);
1230}
1231
1232void bc_num_createFromUlong(BcNum *n, unsigned long val) {
1233 bc_num_init(n, BC_NUM_LONG_LOG10);
1234 bc_num_ulong2num(n, val);
1235}
1236
Gavin Howard7ad5a662019-02-19 14:40:46 -07001237size_t bc_num_scale(const BcNum *restrict n) {
1238 return n->rdx;
1239}
1240
1241size_t bc_num_len(const BcNum *restrict n) {
1242
1243 size_t i, len = n->len;
1244
1245 if (n->rdx != n->len) return len;
1246 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1247
1248 return len;
1249}
1250
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001251BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1252 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001253{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001254 BcStatus s = BC_STATUS_SUCCESS;
1255
Gavin Howard63738202018-09-26 15:34:20 -06001256 assert(n && val && base);
Gavin Howard96edeb32018-12-31 09:33:22 -07001257 assert(base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase);
Gavin Howardc90ed902019-01-02 13:07:49 -07001258 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001259
Gavin Howardd746e802018-12-28 22:06:00 -07001260 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howardc7d655b2018-12-28 19:45:13 -07001261 else if (base_t == 10) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001262 else s = bc_num_parseBase(n, val, base, base_t);
1263
1264 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001265}
1266
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001267BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1268 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001269{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001270 BcStatus s = BC_STATUS_SUCCESS;
1271
Gavin Howardab5e9632019-01-02 13:32:02 -07001272 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001273 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001274
Gavin Howardab5e9632019-01-02 13:32:02 -07001275 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001276
Gavin Howardddce6d42019-01-03 14:07:06 -07001277 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howardab5e9632019-01-02 13:32:02 -07001278 else if (base_t == 10) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001279#if BC_ENABLE_EXTRA_MATH
1280 else if (base_t == 0 || base_t == 1) bc_num_printExponent(n, base_t != 0);
1281#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001282 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001283
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001284 if (!s && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001285 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001286 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001287 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001288
1289 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001290}
1291
Gavin Howard3c02ed32018-12-28 18:17:15 -07001292BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001293
Gavin Howard63738202018-09-26 15:34:20 -06001294 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001295 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001296
Gavin Howard63738202018-09-26 15:34:20 -06001297 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001298
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001299 *result = 0;
1300
Gavin Howard7536dcf2018-12-15 19:27:09 -07001301 if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001302
Gavin Howard06b6e862019-02-15 11:15:31 -07001303 for (r = 0, i = n->len; i > n->rdx;) {
1304 unsigned long prev = r;
1305 r = r * 10 + ((uchar) n->num[--i]);
1306 if ((r / 8) < prev) return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001307 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001308
Gavin Howard7b557da2018-12-21 10:25:32 -07001309 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001310
Gavin Howard63738202018-09-26 15:34:20 -06001311 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001312}
1313
Gavin Howard3c02ed32018-12-28 18:17:15 -07001314void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001315
Gavin Howardec82bdc2018-10-08 23:50:57 -06001316 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001317 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001318 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001319
Gavin Howard63738202018-09-26 15:34:20 -06001320 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001321
Gavin Howard63738202018-09-26 15:34:20 -06001322 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001323
Gavin Howard1ab22d22019-01-03 13:32:17 -07001324 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001325
Gavin Howard1ab22d22019-01-03 13:32:17 -07001326 len = bc_num_log10(ULONG_MAX);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001327 bc_num_expand(n, len);
Gavin Howardec82bdc2018-10-08 23:50:57 -06001328 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001329}
1330
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001331size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
1332 BC_UNUSED(scale);
Gavin Howard620a8af2019-01-24 13:54:54 -07001333 return BC_MAX(a->rdx, b->rdx) + BC_MAX(BC_NUM_INT(a), BC_NUM_INT(b)) + 1;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001334}
1335
1336size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard620a8af2019-01-24 13:54:54 -07001337 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001338}
1339
1340size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1341 BC_UNUSED(scale);
1342 return BC_NUM_PREQ(a, b);
1343}
1344
1345#if BC_ENABLE_EXTRA_MATH
1346size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1347 BC_UNUSED(b);
1348 BC_UNUSED(scale);
1349 return BC_NUM_SHREQ(a);
1350}
1351#endif // BC_ENABLE_EXTRA_MATH
1352
Gavin Howard12fe7812018-09-29 03:45:18 -06001353BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001354 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001355 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001356 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001357}
1358
Gavin Howard12fe7812018-09-29 03:45:18 -06001359BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001360 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001361 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001362 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001363}
1364
Gavin Howard12fe7812018-09-29 03:45:18 -06001365BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001366 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001367}
1368
Gavin Howard12fe7812018-09-29 03:45:18 -06001369BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001370 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001371}
1372
Gavin Howard54b946a2018-10-23 12:06:57 -06001373BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001374 return bc_num_binary(a, b, c, scale, bc_num_rem, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001375}
1376
Gavin Howard12fe7812018-09-29 03:45:18 -06001377BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001378 return bc_num_binary(a, b, c, scale, bc_num_p, BC_NUM_PREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001379}
1380
Gavin Howard7d8f0382018-12-27 16:15:13 -07001381#if BC_ENABLE_EXTRA_MATH
1382BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001383 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001384}
1385
1386BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001387 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001388}
1389
1390BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001391 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001392}
1393#endif // BC_ENABLE_EXTRA_MATH
1394
Gavin Howard3c02ed32018-12-28 18:17:15 -07001395BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001396
Gavin Howard1cbfe242019-01-09 17:13:11 -07001397 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001398 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard283c4eb2019-01-24 16:36:46 -07001399 size_t pow, len, digs, digs1, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001400 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001401 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001402
Gavin Howard34edd0a2018-10-25 15:51:05 -06001403 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001404
Gavin Howard07fbf012019-02-19 09:25:11 -07001405 if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1406
Gavin Howard283c4eb2019-01-24 16:36:46 -07001407 bc_num_init(b, BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001408
Gavin Howardddce6d42019-01-03 14:07:06 -07001409 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001410 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001411 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001412 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001413 if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001414 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001415 bc_num_extend(b, scale);
1416 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001417 }
Gavin Howard954276b2018-05-16 01:43:58 -06001418
Gavin Howard890d0c02018-10-30 16:34:50 -06001419 scale = BC_MAX(scale, a->rdx) + 1;
1420 len = a->len + scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001421
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001422 bc_num_init(&num1, len);
1423 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001424 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001425
Gavin Howard63738202018-09-26 15:34:20 -06001426 bc_num_one(&half);
1427 half.num[0] = 5;
1428 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001429
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001430 bc_num_init(&f, len);
1431 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001432
Gavin Howard63738202018-09-26 15:34:20 -06001433 x0 = &num1;
1434 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001435
Gavin Howard63738202018-09-26 15:34:20 -06001436 bc_num_one(x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001437 pow = BC_NUM_INT(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001438
Gavin Howard53eba8b2018-10-31 15:14:37 -06001439 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001440
Gavin Howardc39fd492018-10-04 10:07:03 -06001441 if (pow & 1) x0->num[0] = 2;
1442 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001443
Gavin Howardc39fd492018-10-04 10:07:03 -06001444 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001445
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001446 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001447
Gavin Howard63738202018-09-26 15:34:20 -06001448 // Make sure to move the radix back.
1449 x0->rdx -= pow;
1450 }
Gavin Howard954276b2018-05-16 01:43:58 -06001451
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001452 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001453 resrdx = scale + 2;
1454 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001455
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001456 while (!BC_SIGNAL && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001457
Gavin Howard1769abc2019-02-21 09:36:14 -07001458 assert(!BC_NUM_ZERO(x0));
1459
Gavin Howard53eba8b2018-10-31 15:14:37 -06001460 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001461 assert(!s || s == BC_STATUS_SIGNAL);
1462 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001463 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001464 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001465 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001466 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001467
Gavin Howard53eba8b2018-10-31 15:14:37 -06001468 cmp = bc_num_cmp(x1, x0);
1469 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001470
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001471 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001472 else times = 0;
1473
1474 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001475
1476 cmp2 = cmp1;
1477 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001478 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001479
Gavin Howard63738202018-09-26 15:34:20 -06001480 temp = x0;
1481 x0 = x1;
1482 x1 = temp;
1483 }
Gavin Howard954276b2018-05-16 01:43:58 -06001484
Gavin Howard176cfe62019-02-16 23:40:13 -07001485 if (BC_SIGNAL) {
1486 s = BC_STATUS_SIGNAL;
1487 goto err;
1488 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001489
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001490 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001491 scale -= 1;
1492 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001493
1494err:
Gavin Howard63738202018-09-26 15:34:20 -06001495 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001496 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001497 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001498 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001499 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001500 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001501 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001502}
Gavin Howardba009802018-09-29 04:41:51 -06001503
Gavin Howard2cb39612018-10-22 08:46:48 -06001504BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1505
1506 BcStatus s;
1507 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001508 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07001509 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001510
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001511 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001512
Gavin Howard890d0c02018-10-30 16:34:50 -06001513 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001514 memcpy(&num2, c, sizeof(BcNum));
1515 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001516 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001517 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001518 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001519 else {
1520 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001521 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001522 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001523
Gavin Howard54b946a2018-10-23 12:06:57 -06001524 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001525
Gavin Howardddce6d42019-01-03 14:07:06 -07001526 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001527 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001528 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001529 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001530
1531 if (init) bc_num_free(&num2);
1532
1533 return s;
1534}
1535
Gavin Howard40a085f2018-12-03 12:08:59 -07001536#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001537BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001538
1539 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001540 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001541 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001542
Gavin Howard34edd0a2018-10-25 15:51:05 -06001543 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001544
Gavin Howardddce6d42019-01-03 14:07:06 -07001545 if (BC_NUM_ZERO(c)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard7536dcf2018-12-15 19:27:09 -07001546 if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1547 if (a->rdx || b->rdx || c->rdx)
1548 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001549
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001550 bc_num_expand(d, c->len);
1551 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001552 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001553 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001554
1555 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001556 two.num[0] = 2;
1557 bc_num_one(d);
1558
Gavin Howard1769abc2019-02-21 09:36:14 -07001559 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001560 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001561 assert(!s || s == BC_STATUS_SIGNAL);
1562 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07001563 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001564
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001565 while (!BC_SIGNAL && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001566
Gavin Howard1769abc2019-02-21 09:36:14 -07001567 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001568 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001569 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001570
1571 if (BC_NUM_ONE(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001572 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001573 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1574
1575 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001576 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001577 assert(!s || s == BC_STATUS_SIGNAL);
1578 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001579 }
1580
Gavin Howard53eba8b2018-10-31 15:14:37 -06001581 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001582 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1583
1584 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001585 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001586 assert(!s || s == BC_STATUS_SIGNAL);
1587 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001588 }
1589
Gavin Howardfa4983a2019-02-16 23:43:03 -07001590 if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL;
1591
Gavin Howardba009802018-09-29 04:41:51 -06001592err:
1593 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001594 bc_num_free(&exp);
Gavin Howardba009802018-09-29 04:41:51 -06001595 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001596 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001597 return s;
1598}
Gavin Howarde6e84762018-10-03 11:46:34 -06001599#endif // DC_ENABLED