blob: 2f93f25b01f7873569c1193d5664701cc7e6f5ab [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 Howardd8b004b2019-02-16 22:35:08 -0700200static void bc_num_shift(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 Howard1cbfe242019-01-09 17:13:11 -0700213static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600214
Gavin Howard63738202018-09-26 15:34:20 -0600215 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600216 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600217
Gavin Howardd32d7df2018-10-15 08:42:25 -0600218 one.cap = 2;
219 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600220 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600221
Gavin Howardd32d7df2018-10-15 08:42:25 -0600222 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600223}
224
Gavin Howard7bda4782018-12-28 09:53:22 -0700225#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700226static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
227 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700228{
Gavin Howardac4d9122018-12-27 23:59:12 -0700229 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
230 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700231 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700232}
Gavin Howard7bda4782018-12-28 09:53:22 -0700233#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700234
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700235static unsigned int bc_num_addDigit(BcDig *restrict num, unsigned int d, unsigned int c)
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700236{
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700237 d += c;
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700238 *num = (BcDig) (d % 10);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700239 assert(*num >= 0 && *num < 10);
Gavin Howard201cb6f2019-01-22 10:01:29 -0700240 assert((d / 10) < 10);
241 return d / 10;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700242}
243
Gavin Howard1cbfe242019-01-09 17:13:11 -0700244static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700245
Gavin Howard8a921bd2018-10-11 14:15:32 -0600246 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600247 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700248 unsigned int carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700249
Gavin Howard63738202018-09-26 15:34:20 -0600250 // Because this function doesn't need to use scale (per the bc spec),
251 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600252
Gavin Howardddce6d42019-01-03 14:07:06 -0700253 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600254 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700255 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600256 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600257 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700258 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600259 bc_num_copy(c, a);
260 return BC_STATUS_SUCCESS;
261 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600262
Gavin Howard63738202018-09-26 15:34:20 -0600263 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600264 c->rdx = BC_MAX(a->rdx, b->rdx);
265 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700266
Gavin Howard63738202018-09-26 15:34:20 -0600267 if (a->rdx > b->rdx) {
268 diff = a->rdx - b->rdx;
269 ptr = a->num;
270 ptr_a = a->num + diff;
271 ptr_b = b->num;
272 }
273 else {
274 diff = b->rdx - a->rdx;
275 ptr = b->num;
276 ptr_a = a->num;
277 ptr_b = b->num + diff;
278 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700279
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700280 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700281
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700282 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600283 ptr_c += diff;
Gavin Howard890d0c02018-10-30 16:34:50 -0600284 a_int = BC_NUM_INT(a);
285 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700286
Gavin Howard890d0c02018-10-30 16:34:50 -0600287 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600288 min_int = b_int;
289 max = a_int;
290 ptr = ptr_a;
291 }
292 else {
293 min_int = a_int;
294 max = b_int;
295 ptr = ptr_b;
296 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700297
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700298 for (carry = 0, i = 0; !BC_SIGNAL && i < min_rdx + min_int; ++i) {
299 unsigned int in = (unsigned int) (ptr_a[i] + ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700300 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600301 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700302
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700303 for (; !BC_SIGNAL && i < max + min_rdx; ++i)
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700304 carry = bc_num_addDigit(ptr_c + i, (unsigned int) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700305
306 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700307
Gavin Howard1ab22d22019-01-03 13:32:17 -0700308 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600309
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700310 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700311}
312
Gavin Howard1cbfe242019-01-09 17:13:11 -0700313static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700314
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700315 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600316 ssize_t cmp;
317 BcNum *minuend, *subtrahend;
318 size_t start;
319 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700320
Gavin Howard63738202018-09-26 15:34:20 -0600321 // Because this function doesn't need to use scale (per the bc spec),
322 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700323
Gavin Howardddce6d42019-01-03 14:07:06 -0700324 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600325 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700326 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600327 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600328 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700329 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600330 bc_num_copy(c, a);
331 return BC_STATUS_SUCCESS;
332 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700333
Gavin Howard63738202018-09-26 15:34:20 -0600334 aneg = a->neg;
335 bneg = b->neg;
336 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700337
Gavin Howard63738202018-09-26 15:34:20 -0600338 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700339
Gavin Howard63738202018-09-26 15:34:20 -0600340 a->neg = aneg;
341 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700342
Gavin Howard1ab22d22019-01-03 13:32:17 -0700343 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600344 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600345 return BC_STATUS_SUCCESS;
346 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700347
348 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600349 neg = a->neg;
350 minuend = a;
351 subtrahend = b;
352 }
353 else {
354 neg = b->neg;
355 if (sub) neg = !neg;
356 minuend = b;
357 subtrahend = a;
358 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700359
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600360 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600361 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700362
Gavin Howard63738202018-09-26 15:34:20 -0600363 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600364 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600365 start = 0;
366 }
367 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700368
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700369 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700370
Gavin Howard63738202018-09-26 15:34:20 -0600371 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700372
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700373 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700374}
375
Gavin Howard1cbfe242019-01-09 17:13:11 -0700376static BcStatus bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600377
Gavin Howard9b801632019-02-16 23:34:06 -0700378 BcStatus s;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700379 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600380 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard63738202018-09-26 15:34:20 -0600381 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600382
Gavin Howardddce6d42019-01-03 14:07:06 -0700383 // This is here because the function is recursive.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700384 if (BC_SIGNAL) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700385 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600386 bc_num_zero(c);
387 return BC_STATUS_SUCCESS;
388 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700389 if (aone || BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600390 bc_num_copy(c, aone ? b : a);
391 return BC_STATUS_SUCCESS;
392 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700393
Gavin Howardb5093d72018-10-17 12:13:17 -0600394 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
395 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600396 {
Gavin Howard34c4ba02018-12-11 10:25:01 -0700397 size_t i, j, len;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700398 unsigned int carry;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700399 BcDig *ptr_c;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700400
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600401 bc_num_expand(c, a->len + b->len + 1);
Gavin Howard23d8d252018-10-17 12:03:41 -0600402
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700403 ptr_c = c->num;
404 memset(ptr_c, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700405 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700406
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700407 for (i = 0; !BC_SIGNAL && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600408
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700409 BcDig *ptr = ptr_c + i;
410
Gavin Howard34c4ba02018-12-11 10:25:01 -0700411 carry = 0;
412
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700413 for (j = 0; !BC_SIGNAL && j < a->len; ++j) {
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700414 unsigned int in = (uchar) ptr[j];
415 assert(in < 10);
Gavin Howard2aef61b2018-12-18 10:56:20 -0700416 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700417 carry = bc_num_addDigit(ptr + j, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600418 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700419
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700420 ptr[j] += (BcDig) carry;
421 assert(ptr[j] >= 0 && ptr[j] < 10);
422 len = BC_MAX(len, i + j + (carry != 0));
Gavin Howard63738202018-09-26 15:34:20 -0600423 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700424
Gavin Howard305249a2018-10-15 20:24:47 -0600425 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600426
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700427 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600428 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700429
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600430 bc_num_init(&l1, max);
431 bc_num_init(&h1, max);
432 bc_num_init(&l2, max);
433 bc_num_init(&h2, max);
434 bc_num_init(&m1, max);
435 bc_num_init(&m2, max);
436 bc_num_init(&z0, max);
437 bc_num_init(&z1, max);
438 bc_num_init(&z2, max);
439 bc_num_init(&temp, max + max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600440
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600441 bc_num_split(a, max2, &l1, &h1);
442 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700443
Gavin Howard53eba8b2018-10-31 15:14:37 -0600444 s = bc_num_add(&h1, &l1, &m1, 0);
445 if (s) goto err;
446 s = bc_num_add(&h2, &l2, &m2, 0);
447 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600448
Gavin Howard53eba8b2018-10-31 15:14:37 -0600449 s = bc_num_k(&h1, &h2, &z0);
450 if (s) goto err;
451 s = bc_num_k(&m1, &m2, &z1);
452 if (s) goto err;
453 s = bc_num_k(&l1, &l2, &z2);
454 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600455
Gavin Howard53eba8b2018-10-31 15:14:37 -0600456 s = bc_num_sub(&z1, &z0, &temp, 0);
457 if (s) goto err;
458 s = bc_num_sub(&temp, &z2, &z1, 0);
459 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600460
Gavin Howardd8b004b2019-02-16 22:35:08 -0700461 bc_num_shift(&z0, max2 * 2);
462 bc_num_shift(&z1, max2);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600463 s = bc_num_add(&z0, &z1, &temp, 0);
464 if (s) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600465 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600466
467err:
468 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600469 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600470 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600471 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600472 bc_num_free(&m2);
Gavin Howard305249a2018-10-15 20:24:47 -0600473 bc_num_free(&m1);
Gavin Howard305249a2018-10-15 20:24:47 -0600474 bc_num_free(&h2);
Gavin Howard305249a2018-10-15 20:24:47 -0600475 bc_num_free(&l2);
Gavin Howard305249a2018-10-15 20:24:47 -0600476 bc_num_free(&h1);
Gavin Howard305249a2018-10-15 20:24:47 -0600477 bc_num_free(&l1);
478 return s;
479}
480
Gavin Howard1cbfe242019-01-09 17:13:11 -0700481static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600482
483 BcStatus s;
484 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600485 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600486
487 scale = BC_MAX(scale, a->rdx);
488 scale = BC_MAX(scale, b->rdx);
489 scale = BC_MIN(a->rdx + b->rdx, scale);
490 maxrdx = BC_MAX(maxrdx, scale);
491
Gavin Howardbaa4f582019-01-24 13:56:35 -0700492 bc_num_createCopy(&cpa, a);
493 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600494
Gavin Howard23d8d252018-10-17 12:03:41 -0600495 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600496
Gavin Howardd8b004b2019-02-16 22:35:08 -0700497 bc_num_shift(&cpa, maxrdx);
498 bc_num_shift(&cpb, maxrdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600499 s = bc_num_k(&cpa, &cpb, c);
500 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600501
Gavin Howard53eba8b2018-10-31 15:14:37 -0600502 maxrdx += scale;
503 bc_num_expand(c, c->len + maxrdx);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600504
Gavin Howard88c25302018-10-17 13:32:23 -0600505 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600506 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600507 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600508 }
509
Gavin Howard88c25302018-10-17 13:32:23 -0600510 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600511 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600512
513err:
514 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600515 bc_num_free(&cpa);
516 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700517}
518
Gavin Howard1cbfe242019-01-09 17:13:11 -0700519static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700520
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600521 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600522 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600523 size_t len, end, i;
524 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600525 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700526
Gavin Howardddce6d42019-01-03 14:07:06 -0700527 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
528 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600529 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600530 return BC_STATUS_SUCCESS;
531 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700532 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600533 bc_num_copy(c, a);
534 bc_num_retireMul(c, scale, a->neg, b->neg);
535 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600536 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700537
Gavin Howard620a8af2019-01-24 13:54:54 -0700538 bc_num_init(&cp, bc_num_mulReq(a, b, scale));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600539 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600540 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700541
Gavin Howard890d0c02018-10-30 16:34:50 -0600542 if (len > cp.len) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600543 bc_num_expand(&cp, len + 2);
544 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600545 }
Gavin Howard021150b2018-03-10 15:40:42 -0700546
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600547 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600548 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600549 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700550
Gavin Howard63738202018-09-26 15:34:20 -0600551 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600552 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600553 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600554 len -= i - 1;
555 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700556
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600557 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700558
Gavin Howard63738202018-09-26 15:34:20 -0600559 // We want an extra zero in front to make things simpler.
560 cp.num[cp.len++] = 0;
561 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700562
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600563 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700564
Gavin Howarda2514a02018-10-18 14:20:09 -0600565 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600566 c->rdx = cp.rdx;
567 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600568 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700569
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700570 for (i = end - 1; !BC_SIGNAL && !s && i < end; --i) {
Gavin Howard63738202018-09-26 15:34:20 -0600571 n = cp.num + i;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700572 for (q = 0; !s && (n[len] || bc_num_compare(n, p, len) >= 0); ++q)
573 s = bc_num_subArrays(n, p, len);
Gavin Howard63738202018-09-26 15:34:20 -0600574 c->num[i] = q;
575 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700576
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600577 if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600578 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600579
Gavin Howard63738202018-09-26 15:34:20 -0600580 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700581}
582
Gavin Howard1cbfe242019-01-09 17:13:11 -0700583static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
584 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600585{
Gavin Howard63738202018-09-26 15:34:20 -0600586 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600587 BcNum temp;
588 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700589
Gavin Howardddce6d42019-01-03 14:07:06 -0700590 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
591 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700592 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600593 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600594 return BC_STATUS_SUCCESS;
595 }
Gavin Howard8b254872018-03-14 01:13:35 -0600596
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600597 bc_num_init(&temp, d->cap);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600598 bc_num_d(a, b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700599
Gavin Howard1ab22d22019-01-03 13:32:17 -0700600 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600601
Gavin Howard53eba8b2018-10-31 15:14:37 -0600602 s = bc_num_m(c, b, &temp, scale);
603 if (s) goto err;
604 s = bc_num_sub(a, &temp, d, scale);
605 if (s) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600606
Gavin Howardddce6d42019-01-03 14:07:06 -0700607 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600608
609 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600610 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600611 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700612
613err:
Gavin Howard18441082018-10-22 10:03:30 -0600614 bc_num_free(&temp);
615 return s;
616}
617
Gavin Howard1cbfe242019-01-09 17:13:11 -0700618static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600619
Gavin Howard18441082018-10-22 10:03:30 -0600620 BcStatus s;
621 BcNum c1;
Gavin Howard620a8af2019-01-24 13:54:54 -0700622 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard18441082018-10-22 10:03:30 -0600623
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600624 bc_num_init(&c1, len);
Gavin Howard54b946a2018-10-23 12:06:57 -0600625 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600626 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600627
Gavin Howard63738202018-09-26 15:34:20 -0600628 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700629}
630
Gavin Howard1cbfe242019-01-09 17:13:11 -0700631static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700632
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600633 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600634 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700635 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600636 size_t i, powrdx, resrdx;
637 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700638
Gavin Howardac4d9122018-12-27 23:59:12 -0700639 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700640
Gavin Howardddce6d42019-01-03 14:07:06 -0700641 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600642 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600643 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600644 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700645 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600646 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600647 return BC_STATUS_SUCCESS;
648 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700649 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600650 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600651 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600652 return s;
653 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700654
Gavin Howard63738202018-09-26 15:34:20 -0600655 neg = b->neg;
656 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600657 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700658 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600659 if (s) return s;
660
Gavin Howardbaa4f582019-01-24 13:56:35 -0700661 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700662
Gavin Howard63738202018-09-26 15:34:20 -0600663 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600664
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700665 for (powrdx = a->rdx; !BC_SIGNAL && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600666 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600667 s = bc_num_mul(&copy, &copy, &copy, powrdx);
668 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600669 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700670
Gavin Howard9b801632019-02-16 23:34:06 -0700671 if (BC_SIGNAL) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -0700672
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600673 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700674 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600675
Gavin Howard4d5daba2019-01-07 14:42:50 -0700676 while (!BC_SIGNAL && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600677
Gavin Howard4d5daba2019-01-07 14:42:50 -0700678 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600679 s = bc_num_mul(&copy, &copy, &copy, powrdx);
680 if (s) goto err;
681
Gavin Howard63738202018-09-26 15:34:20 -0600682 if (pow & 1) {
683 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600684 s = bc_num_mul(c, &copy, c, resrdx);
685 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600686 }
687 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700688
Gavin Howard9b801632019-02-16 23:34:06 -0700689 if (BC_SIGNAL) goto sig_err;
690 if (neg) {
691 s = bc_num_inv(c, c, scale);
692 if (s) goto err;
693 }
694
Gavin Howard63738202018-09-26 15:34:20 -0600695 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700696
Gavin Howard53eba8b2018-10-31 15:14:37 -0600697 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600698 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600699 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700700
Gavin Howard9b801632019-02-16 23:34:06 -0700701sig_err:
702 if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700703err:
Gavin Howard63738202018-09-26 15:34:20 -0600704 bc_num_free(&copy);
705 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700706}
707
Gavin Howard7bda4782018-12-28 09:53:22 -0700708#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700709static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700710
711 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700712 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700713
714 BC_UNUSED(scale);
715
716 s = bc_num_intop(a, b, c, &val);
717 if (s) return s;
718
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700719 if (val < c->rdx) bc_num_truncate(c, c->rdx - val);
720 else if (val > c->rdx) bc_num_extend(c, val - c->rdx);
Gavin Howardac4d9122018-12-27 23:59:12 -0700721
722 return s;
723}
724
Gavin Howard1cbfe242019-01-09 17:13:11 -0700725static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700726
727 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700728 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700729
730 BC_UNUSED(scale);
731
732 s = bc_num_intop(a, b, c, &val);
733 if (s) return s;
734
735 bc_num_shift(c, val);
736
737 return s;
738}
739
Gavin Howard1cbfe242019-01-09 17:13:11 -0700740static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700741
742 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700743 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700744 size_t len;
745
746 BC_UNUSED(scale);
747
748 s = bc_num_intop(a, b, c, &val);
749 if (s) return s;
750
Gavin Howard2a366922019-01-16 10:50:20 -0700751 if (BC_NUM_ZERO(c)) return s;
752
Gavin Howardac4d9122018-12-27 23:59:12 -0700753 len = c->rdx + val;
754
755 if (len > c->len) {
756
757 if (len > c->cap) bc_num_expand(c, len);
758
759 memset(c->num + c->len, 0, len - c->len);
760 c->len = len;
761 }
762
763 c->rdx += val;
764
765 assert(c->rdx <= c->len && c->len <= c->cap);
766
767 return s;
768}
Gavin Howard7bda4782018-12-28 09:53:22 -0700769#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700770
Gavin Howard1cbfe242019-01-09 17:13:11 -0700771static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
772 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700773{
Gavin Howarda1c44392018-09-27 12:41:15 -0600774 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600775 BcNum num2, *ptr_a, *ptr_b;
776 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700777
Gavin Howard63738202018-09-26 15:34:20 -0600778 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700779
Gavin Howard890d0c02018-10-30 16:34:50 -0600780 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600781 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600782 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600783 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600784 }
785 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700786
Gavin Howard63738202018-09-26 15:34:20 -0600787 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600788 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600789 if (c != a) {
790 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600791 init = true;
792 }
793 }
794 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700795
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600796 if (init) bc_num_init(c, req);
797 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700798
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600799 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700800
Gavin Howardddce6d42019-01-03 14:07:06 -0700801 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700802 assert(c->rdx <= c->len || !c->len);
Gavin Howard3cf769e2018-10-11 13:55:11 -0600803
Gavin Howardba009802018-09-29 04:41:51 -0600804 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600805
Gavin Howardee9d01e2019-02-16 22:48:11 -0700806 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700807}
808
Gavin Howardc90ed902019-01-02 13:07:49 -0700809#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -0700810static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700811
Gavin Howardc7d655b2018-12-28 19:45:13 -0700812 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -0600813 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700814
Gavin Howard63738202018-09-26 15:34:20 -0600815 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700816
Gavin Howard63738202018-09-26 15:34:20 -0600817 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700818
Gavin Howard8a921bd2018-10-11 14:15:32 -0600819 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600820
Gavin Howard63738202018-09-26 15:34:20 -0600821 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700822
Gavin Howard63738202018-09-26 15:34:20 -0600823 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700824
Gavin Howard63738202018-09-26 15:34:20 -0600825 radix = true;
826 continue;
827 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700828
Gavin Howardc7d655b2018-12-28 19:45:13 -0700829 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -0600830 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700831
Gavin Howard63738202018-09-26 15:34:20 -0600832 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700833}
Gavin Howardc90ed902019-01-02 13:07:49 -0700834#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -0700835
Gavin Howard1cbfe242019-01-09 17:13:11 -0700836static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -0700837
838 if (isupper(c)) {
839 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -0700840 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -0700841 }
842 else c -= '0';
843
844 return (unsigned long) (uchar) c;
845}
846
Gavin Howard1cbfe242019-01-09 17:13:11 -0700847static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700848
Gavin Howard63738202018-09-26 15:34:20 -0600849 size_t len, i;
850 const char *ptr;
851 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700852
Gavin Howard63738202018-09-26 15:34:20 -0600853 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700854
Gavin Howard63738202018-09-26 15:34:20 -0600855 val += i;
856 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700857
Gavin Howard1ab22d22019-01-03 13:32:17 -0700858 if (len) {
859 for (i = 0; zero && i < len; ++i) zero = (val[i] == '0') || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600860 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600861 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600862
Gavin Howard63738202018-09-26 15:34:20 -0600863 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700864
Gavin Howard63738202018-09-26 15:34:20 -0600865 // Explicitly test for NULL here to produce either a 0 or 1.
866 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700867
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600868 if (!zero) {
Gavin Howard8dd307e2019-01-08 23:05:19 -0700869 for (i = len - 1; i < len; ++n->len, --i) {
870
Gavin Howardc7d655b2018-12-28 19:45:13 -0700871 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -0700872
873 if (c == '.') n->len -= 1;
874 else {
875 if (isupper(c)) c = '9';
876 n->num[n->len] = c - '0';
877 }
Gavin Howardc7d655b2018-12-28 19:45:13 -0700878 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600879 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700880}
881
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700882static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
883 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -0700884{
885 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600886 BcNum temp, mult, result;
Gavin Howard1cbfe242019-01-09 17:13:11 -0700887 BcDig c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600888 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600889 unsigned long v;
890 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700891
Gavin Howard11b9afd2018-10-18 14:23:28 -0600892 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700893 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700894
Gavin Howard26d078f2019-01-24 13:58:12 -0700895 bc_num_init(&temp, BC_NUM_LONG_LOG10);
896 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -0700897
Gavin Howardc7d655b2018-12-28 19:45:13 -0700898 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -0700899
Gavin Howardc7d655b2018-12-28 19:45:13 -0700900 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -0700901
Gavin Howard53eba8b2018-10-31 15:14:37 -0600902 s = bc_num_mul(n, base, &mult, 0);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700903 if (s) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700904 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600905 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700906 if (s) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600907 }
Gavin Howardede51f02018-03-02 12:30:00 -0700908
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700909 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600910
Gavin Howard63738202018-09-26 15:34:20 -0600911 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600912 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -0600913 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700914
Gavin Howardc7d655b2018-12-28 19:45:13 -0700915 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700916
Gavin Howardc7d655b2018-12-28 19:45:13 -0700917 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -0700918
Gavin Howard53eba8b2018-10-31 15:14:37 -0600919 s = bc_num_mul(&result, base, &result, 0);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700920 if (s) goto err;
921
Gavin Howard705c4bc2018-12-03 15:38:04 -0700922 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600923 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700924 if (s) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600925 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700926 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600927 }
Gavin Howardede51f02018-03-02 12:30:00 -0700928
Gavin Howard53eba8b2018-10-31 15:14:37 -0600929 s = bc_num_div(&result, &mult, &result, digits);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700930 if (s) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600931 s = bc_num_add(n, &result, n, digits);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700932 if (s) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -0700933
Gavin Howardddce6d42019-01-03 14:07:06 -0700934 if (BC_NUM_NONZERO(n)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600935 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600936 }
937 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700938
Gavin Howardd1417682019-02-15 17:08:42 -0700939err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700940 bc_num_free(&result);
941int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600942 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -0600943 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700944 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700945}
946
Gavin Howard1cbfe242019-01-09 17:13:11 -0700947static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -0700948 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -0600949 bc_vm_putchar('\\');
950 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -0700951 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600952 }
Gavin Howard80977b22018-09-06 14:29:13 -0600953}
954
Gavin Howard40a085f2018-12-03 12:08:59 -0700955#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -0700956static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -0700957 BC_UNUSED(rdx);
958 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -0700959 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600960}
961#endif // DC_ENABLED
962
Gavin Howard1cbfe242019-01-09 17:13:11 -0700963static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
964
Gavin Howarda84ad992018-12-03 19:11:06 -0700965 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -0600966
Gavin Howardab5e9632019-01-02 13:32:02 -0700967 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700968 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -0700969 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600970
Gavin Howardab5e9632019-01-02 13:32:02 -0700971 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700972 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600973
Gavin Howardab5e9632019-01-02 13:32:02 -0700974 for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -0700975 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -0700976 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700977 dig = n / pow;
978 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -0700979 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -0600980 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700981}
Gavin Howardfe679f02018-02-14 15:50:09 -0700982
Gavin Howard1cbfe242019-01-09 17:13:11 -0700983static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -0700984
Gavin Howard3c02ed32018-12-28 18:17:15 -0700985 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600986
Gavin Howard3c02ed32018-12-28 18:17:15 -0700987 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -0700988 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -0600989 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -0700990 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -0600991 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700992
Gavin Howardab5e9632019-01-02 13:32:02 -0700993 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700994 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -0700995 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700996}
997
Gavin Howard1cbfe242019-01-09 17:13:11 -0700998static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700999
Gavin Howard63738202018-09-26 15:34:20 -06001000 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001001
Gavin Howard48af52e2018-10-30 14:47:38 -06001002 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001003 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001004
Gavin Howard48af52e2018-10-30 14:47:38 -06001005 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001006 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001007}
1008
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001009static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1010 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001011{
Gavin Howard63738202018-09-26 15:34:20 -06001012 BcStatus s;
1013 BcVec stack;
1014 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001015 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001016 size_t i;
1017 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001018
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001019 assert(!BC_NUM_ZERO(base));
1020
Gavin Howardddce6d42019-01-03 14:07:06 -07001021 if (BC_NUM_ZERO(n)) {
1022 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001023 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001024 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001025
Gavin Howardd3929832018-12-24 15:13:15 -07001026 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001027 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001028 bc_num_init(&digit, len);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001029 bc_num_init(&frac_len, BC_NUM_INT(n));
Gavin Howard63738202018-09-26 15:34:20 -06001030 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001031 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001032
Gavin Howard63738202018-09-26 15:34:20 -06001033 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001034 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001035 if (s) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001036
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001037 while (!BC_SIGNAL && BC_NUM_NONZERO(&intp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001038 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001039 if (s) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001040 s = bc_num_ulong(&digit, &dig);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001041 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001042 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001043 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001044
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001045 if (BC_SIGNAL) goto sig_err;
1046
1047 for (i = 0; !BC_SIGNAL && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001048 ptr = bc_vec_item_rev(&stack, i);
1049 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001050 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001051 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001052
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001053 if (BC_SIGNAL) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001054 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001055
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001056 for (radix = true; !BC_SIGNAL && frac_len.len <= n->rdx; radix = false) {
1057
Gavin Howard53eba8b2018-10-31 15:14:37 -06001058 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001059 if (s) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001060 s = bc_num_ulong(&fracp, &dig);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001061 if (s) goto err;
1062
Gavin Howard705c4bc2018-12-03 15:38:04 -07001063 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001064 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001065 if (s) goto err;
1066
Gavin Howardddce6d42019-01-03 14:07:06 -07001067 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001068 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001069 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001070 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001071
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001072sig_err:
Gavin Howard9b801632019-02-16 23:34:06 -07001073 if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001074err:
Gavin Howard63738202018-09-26 15:34:20 -06001075 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001076 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001077 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001078 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001079 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001080 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001081}
1082
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001083static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1084 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001085{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001086 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001087 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001088 BcNumDigitOp print;
1089 bool neg = n->neg;
1090
Gavin Howard48af52e2018-10-30 14:47:38 -06001091 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001092 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001093
1094 n->neg = false;
1095
Gavin Howardbe4e8222019-01-03 13:34:16 -07001096 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001097 width = 1;
1098 print = bc_num_printHex;
1099 }
1100 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001101 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001102 print = bc_num_printDigits;
1103 }
1104
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001105 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001106 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001107
1108 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001109}
1110
Gavin Howard40a085f2018-12-03 12:08:59 -07001111#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001112BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1113 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001114}
1115#endif // DC_ENABLED
1116
Gavin Howard3c02ed32018-12-28 18:17:15 -07001117void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001118 assert(n);
1119 n->num = num;
1120 n->cap = cap;
1121 n->rdx = n->len = 0;
1122 n->neg = false;
1123}
1124
Gavin Howard3c02ed32018-12-28 18:17:15 -07001125void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001126 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001127 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001128 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001129}
1130
Gavin Howard3c02ed32018-12-28 18:17:15 -07001131void bc_num_expand(BcNum *restrict n, size_t req) {
Gavin Howard844e7a52018-10-09 11:46:39 -06001132 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001133 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1134 if (req > n->cap) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001135 n->num = bc_vm_realloc(n->num, req);
1136 n->cap = req;
1137 }
Gavin Howardb5c77212018-02-14 17:12:34 -07001138}
1139
Gavin Howarded392aa2018-02-27 13:09:26 -07001140void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001141 assert(num);
1142 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001143}
1144
Gavin Howard3c02ed32018-12-28 18:17:15 -07001145void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001146 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001147 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001148 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001149 d->len = s->len;
1150 d->neg = s->neg;
1151 d->rdx = s->rdx;
1152 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001153}
1154
Gavin Howardbaa4f582019-01-24 13:56:35 -07001155void bc_num_createCopy(BcNum *d, const BcNum *s) {
1156 bc_num_init(d, s->len);
1157 bc_num_copy(d, s);
1158}
1159
1160void bc_num_createFromUlong(BcNum *n, unsigned long val) {
1161 bc_num_init(n, BC_NUM_LONG_LOG10);
1162 bc_num_ulong2num(n, val);
1163}
1164
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001165BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1166 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001167{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001168 BcStatus s = BC_STATUS_SUCCESS;
1169
Gavin Howard63738202018-09-26 15:34:20 -06001170 assert(n && val && base);
Gavin Howard96edeb32018-12-31 09:33:22 -07001171 assert(base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase);
Gavin Howardc90ed902019-01-02 13:07:49 -07001172 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001173
Gavin Howardd746e802018-12-28 22:06:00 -07001174 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howardc7d655b2018-12-28 19:45:13 -07001175 else if (base_t == 10) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001176 else s = bc_num_parseBase(n, val, base, base_t);
1177
1178 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001179}
1180
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001181BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1182 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001183{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001184 BcStatus s = BC_STATUS_SUCCESS;
1185
Gavin Howardab5e9632019-01-02 13:32:02 -07001186 assert(n && base);
Gavin Howard1bbcec02019-02-17 00:13:08 -07001187 assert(base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001188
Gavin Howardab5e9632019-01-02 13:32:02 -07001189 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001190
Gavin Howardddce6d42019-01-03 14:07:06 -07001191 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howardab5e9632019-01-02 13:32:02 -07001192 else if (base_t == 10) bc_num_printDecimal(n);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001193 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001194
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001195 if (!s && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001196 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001197 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001198 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001199
1200 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001201}
1202
Gavin Howard3c02ed32018-12-28 18:17:15 -07001203BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001204
Gavin Howard63738202018-09-26 15:34:20 -06001205 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001206 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001207
Gavin Howard63738202018-09-26 15:34:20 -06001208 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001209
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001210 *result = 0;
1211
Gavin Howard7536dcf2018-12-15 19:27:09 -07001212 if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001213
Gavin Howard06b6e862019-02-15 11:15:31 -07001214 for (r = 0, i = n->len; i > n->rdx;) {
1215 unsigned long prev = r;
1216 r = r * 10 + ((uchar) n->num[--i]);
1217 if ((r / 8) < prev) return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001218 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001219
Gavin Howard7b557da2018-12-21 10:25:32 -07001220 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001221
Gavin Howard63738202018-09-26 15:34:20 -06001222 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001223}
1224
Gavin Howard3c02ed32018-12-28 18:17:15 -07001225void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001226
Gavin Howardec82bdc2018-10-08 23:50:57 -06001227 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001228 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001229 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001230
Gavin Howard63738202018-09-26 15:34:20 -06001231 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001232
Gavin Howard63738202018-09-26 15:34:20 -06001233 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001234
Gavin Howard1ab22d22019-01-03 13:32:17 -07001235 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001236
Gavin Howard1ab22d22019-01-03 13:32:17 -07001237 len = bc_num_log10(ULONG_MAX);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001238 bc_num_expand(n, len);
Gavin Howardec82bdc2018-10-08 23:50:57 -06001239 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001240}
1241
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001242size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
1243 BC_UNUSED(scale);
Gavin Howard620a8af2019-01-24 13:54:54 -07001244 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 -07001245}
1246
1247size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard620a8af2019-01-24 13:54:54 -07001248 return BC_NUM_INT(a) + BC_NUM_INT(b) + BC_MAX(scale, a->rdx + b->rdx) + 1;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001249}
1250
1251size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1252 BC_UNUSED(scale);
1253 return BC_NUM_PREQ(a, b);
1254}
1255
1256#if BC_ENABLE_EXTRA_MATH
1257size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1258 BC_UNUSED(b);
1259 BC_UNUSED(scale);
1260 return BC_NUM_SHREQ(a);
1261}
1262#endif // BC_ENABLE_EXTRA_MATH
1263
Gavin Howard12fe7812018-09-29 03:45:18 -06001264BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001265 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001266 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001267 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001268}
1269
Gavin Howard12fe7812018-09-29 03:45:18 -06001270BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001271 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001272 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001273 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001274}
1275
Gavin Howard12fe7812018-09-29 03:45:18 -06001276BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001277 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001278}
1279
Gavin Howard12fe7812018-09-29 03:45:18 -06001280BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001281 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001282}
1283
Gavin Howard54b946a2018-10-23 12:06:57 -06001284BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001285 return bc_num_binary(a, b, c, scale, bc_num_rem, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001286}
1287
Gavin Howard12fe7812018-09-29 03:45:18 -06001288BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001289 return bc_num_binary(a, b, c, scale, bc_num_p, BC_NUM_PREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001290}
1291
Gavin Howard7d8f0382018-12-27 16:15:13 -07001292#if BC_ENABLE_EXTRA_MATH
1293BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001294 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001295}
1296
1297BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001298 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001299}
1300
1301BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001302 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001303}
1304#endif // BC_ENABLE_EXTRA_MATH
1305
Gavin Howard3c02ed32018-12-28 18:17:15 -07001306BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001307
Gavin Howard1cbfe242019-01-09 17:13:11 -07001308 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001309 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard283c4eb2019-01-24 16:36:46 -07001310 size_t pow, len, digs, digs1, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001311 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001312 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001313
Gavin Howard34edd0a2018-10-25 15:51:05 -06001314 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001315
Gavin Howard07fbf012019-02-19 09:25:11 -07001316 if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1317
Gavin Howard283c4eb2019-01-24 16:36:46 -07001318 bc_num_init(b, BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001319
Gavin Howardddce6d42019-01-03 14:07:06 -07001320 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001321 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001322 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001323 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001324 if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001325 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001326 bc_num_extend(b, scale);
1327 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001328 }
Gavin Howard954276b2018-05-16 01:43:58 -06001329
Gavin Howard890d0c02018-10-30 16:34:50 -06001330 scale = BC_MAX(scale, a->rdx) + 1;
1331 len = a->len + scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001332
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001333 bc_num_init(&num1, len);
1334 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001335 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001336
Gavin Howard63738202018-09-26 15:34:20 -06001337 bc_num_one(&half);
1338 half.num[0] = 5;
1339 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001340
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001341 bc_num_init(&f, len);
1342 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001343
Gavin Howard63738202018-09-26 15:34:20 -06001344 x0 = &num1;
1345 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001346
Gavin Howard63738202018-09-26 15:34:20 -06001347 bc_num_one(x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001348 pow = BC_NUM_INT(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001349
Gavin Howard53eba8b2018-10-31 15:14:37 -06001350 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001351
Gavin Howardc39fd492018-10-04 10:07:03 -06001352 if (pow & 1) x0->num[0] = 2;
1353 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001354
Gavin Howardc39fd492018-10-04 10:07:03 -06001355 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001356
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001357 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001358
Gavin Howard63738202018-09-26 15:34:20 -06001359 // Make sure to move the radix back.
1360 x0->rdx -= pow;
1361 }
Gavin Howard954276b2018-05-16 01:43:58 -06001362
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001363 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001364 resrdx = scale + 2;
1365 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001366
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001367 while (!BC_SIGNAL && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001368
Gavin Howard53eba8b2018-10-31 15:14:37 -06001369 s = bc_num_div(a, x0, &f, resrdx);
1370 if (s) goto err;
1371 s = bc_num_add(x0, &f, &fprime, resrdx);
1372 if (s) goto err;
1373 s = bc_num_mul(&fprime, &half, x1, resrdx);
1374 if (s) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001375
Gavin Howard53eba8b2018-10-31 15:14:37 -06001376 cmp = bc_num_cmp(x1, x0);
1377 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001378
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001379 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001380 else times = 0;
1381
1382 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001383
1384 cmp2 = cmp1;
1385 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001386 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001387
Gavin Howard63738202018-09-26 15:34:20 -06001388 temp = x0;
1389 x0 = x1;
1390 x1 = temp;
1391 }
Gavin Howard954276b2018-05-16 01:43:58 -06001392
Gavin Howard176cfe62019-02-16 23:40:13 -07001393 if (BC_SIGNAL) {
1394 s = BC_STATUS_SIGNAL;
1395 goto err;
1396 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001397
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001398 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001399 scale -= 1;
1400 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001401
1402err:
Gavin Howard63738202018-09-26 15:34:20 -06001403 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001404 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001405 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001406 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001407 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001408 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001409 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001410}
Gavin Howardba009802018-09-29 04:41:51 -06001411
Gavin Howard2cb39612018-10-22 08:46:48 -06001412BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1413
1414 BcStatus s;
1415 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001416 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07001417 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001418
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001419 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001420
Gavin Howard890d0c02018-10-30 16:34:50 -06001421 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001422 memcpy(&num2, c, sizeof(BcNum));
1423 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001424 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001425 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001426 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001427 else {
1428 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001429 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001430 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001431
Gavin Howard54b946a2018-10-23 12:06:57 -06001432 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001433
Gavin Howardddce6d42019-01-03 14:07:06 -07001434 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001435 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001436 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001437 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001438
1439 if (init) bc_num_free(&num2);
1440
1441 return s;
1442}
1443
Gavin Howard40a085f2018-12-03 12:08:59 -07001444#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001445BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001446
1447 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001448 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001449 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001450
Gavin Howard34edd0a2018-10-25 15:51:05 -06001451 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001452
Gavin Howardddce6d42019-01-03 14:07:06 -07001453 if (BC_NUM_ZERO(c)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard7536dcf2018-12-15 19:27:09 -07001454 if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1455 if (a->rdx || b->rdx || c->rdx)
1456 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001457
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001458 bc_num_expand(d, c->len);
1459 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001460 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001461 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001462
1463 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001464 two.num[0] = 2;
1465 bc_num_one(d);
1466
Gavin Howard53eba8b2018-10-31 15:14:37 -06001467 s = bc_num_rem(a, c, &base, 0);
1468 if (s) goto err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07001469 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001470
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001471 while (!BC_SIGNAL && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001472
Gavin Howard53eba8b2018-10-31 15:14:37 -06001473 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
1474 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001475
1476 if (BC_NUM_ONE(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001477 s = bc_num_mul(d, &base, &temp, 0);
1478 if (s) goto err;
1479 s = bc_num_rem(&temp, c, d, 0);
1480 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001481 }
1482
Gavin Howard53eba8b2018-10-31 15:14:37 -06001483 s = bc_num_mul(&base, &base, &temp, 0);
1484 if (s) goto err;
1485 s = bc_num_rem(&temp, c, &base, 0);
1486 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001487 }
1488
Gavin Howardfa4983a2019-02-16 23:43:03 -07001489 if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL;
1490
Gavin Howardba009802018-09-29 04:41:51 -06001491err:
1492 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001493 bc_num_free(&exp);
Gavin Howardba009802018-09-29 04:41:51 -06001494 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001495 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001496 return s;
1497}
Gavin Howarde6e84762018-10-03 11:46:34 -06001498#endif // DC_ENABLED