blob: 89a39db063ed0e1c20ce83cf12c1f5d9383e3b70 [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 Howard1cbfe242019-01-09 17:13:11 -070066static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b, size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -070067{
Gavin Howard63738202018-09-26 15:34:20 -060068 size_t i, j;
Gavin Howard5dbb48b2019-01-04 11:29:29 -070069 for (i = 0; !BC_SIGNAL && i < len; ++i) {
70 for (a[i] -= b[i], j = 0; !BC_SIGNAL && a[i + j] < 0;) {
Gavin Howard54b946a2018-10-23 12:06:57 -060071 a[i + j++] += 10;
72 a[i + j] -= 1;
Gavin Howard231e66c2019-01-08 23:02:19 -070073 assert(a[i + j - 1] >= 0 && a[i + j - 1] < 10);
Gavin Howard63738202018-09-26 15:34:20 -060074 }
75 }
Gavin Howard5dbb48b2019-01-04 11:29:29 -070076 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060077}
78
Gavin Howard1cbfe242019-01-09 17:13:11 -070079static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b, size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -070080{
Gavin Howard63738202018-09-26 15:34:20 -060081 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -060082 int c = 0;
Gavin Howard5dbb48b2019-01-04 11:29:29 -070083 for (i = len - 1; !BC_SIGNAL && i < len && !(c = a[i] - b[i]); --i);
Gavin Howard7fbb4e22018-10-18 11:43:17 -060084 return BC_NUM_NEG(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -060085}
86
Gavin Howard3c02ed32018-12-28 18:17:15 -070087ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070088
Gavin Howard63738202018-09-26 15:34:20 -060089 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -060090 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060091 bool a_max, neg = false;
92 ssize_t cmp;
93
94 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070095
Gavin Howard8a921bd2018-10-11 14:15:32 -060096 if (a == b) return 0;
Gavin Howardddce6d42019-01-03 14:07:06 -070097 if (BC_NUM_ZERO(a)) return BC_NUM_NEG(!!b->len, !b->neg);
98 if (BC_NUM_ZERO(b)) return BC_NUM_CMP_ZERO(a);
Gavin Howard890d0c02018-10-30 16:34:50 -060099 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600100 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600101 else return -1;
102 }
103 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700104
Gavin Howard63738202018-09-26 15:34:20 -0600105 a_int = BC_NUM_INT(a);
106 b_int = BC_NUM_INT(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600107 a_int -= b_int;
108 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700109
Gavin Howard1ab22d22019-01-03 13:32:17 -0700110 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700111
Gavin Howard890d0c02018-10-30 16:34:50 -0600112 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600113 min = b->rdx;
114 diff = a->rdx - b->rdx;
115 max_num = a->num + diff;
116 min_num = b->num;
117 }
118 else {
119 min = a->rdx;
120 diff = b->rdx - a->rdx;
121 max_num = b->num + diff;
122 min_num = a->num;
123 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700124
Gavin Howard890d0c02018-10-30 16:34:50 -0600125 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700126 if (cmp) return BC_NUM_NEG(cmp, (!a_max) != neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700127
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700128 for (max_num -= diff, i = diff - 1; !BC_SIGNAL && i < diff; --i) {
Gavin Howard40974152018-10-18 14:23:01 -0600129 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
Gavin Howard63738202018-09-26 15:34:20 -0600130 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700131
Gavin Howard63738202018-09-26 15:34:20 -0600132 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700133}
134
Gavin Howard2a366922019-01-16 10:50:20 -0700135static void bc_num_clean(BcNum *restrict n) {
136 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
137 if (BC_NUM_ZERO(n)) n->neg = false;
138 else if (n->len < n->rdx) n->len = n->rdx;
139}
140
Gavin Howard3c02ed32018-12-28 18:17:15 -0700141void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700142
Gavin Howardddce6d42019-01-03 14:07:06 -0700143 assert(places <= n->rdx && (BC_NUM_ZERO(n) || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700144
Gavin Howard1ab22d22019-01-03 13:32:17 -0700145 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700146
Gavin Howard63738202018-09-26 15:34:20 -0600147 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700148
Gavin Howardddce6d42019-01-03 14:07:06 -0700149 if (BC_NUM_NONZERO(n)) {
Gavin Howard955da852018-10-23 11:08:20 -0600150 n->len -= places;
151 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
Gavin Howard2a366922019-01-16 10:50:20 -0700152 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600153 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700154}
155
Gavin Howard1cbfe242019-01-09 17:13:11 -0700156static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700157
Gavin Howard88c25302018-10-17 13:32:23 -0600158 size_t len = n->len + places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700159
Gavin Howard1ab22d22019-01-03 13:32:17 -0700160 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700161
Gavin Howard1ab22d22019-01-03 13:32:17 -0700162 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700163
Gavin Howard1ab22d22019-01-03 13:32:17 -0700164 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
165 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700166
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700167 if (n->len) n->len += places;
168
Gavin Howard1ab22d22019-01-03 13:32:17 -0700169 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700170}
171
Gavin Howard1cbfe242019-01-09 17:13:11 -0700172static void bc_num_retireMul(BcNum *restrict n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600173
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600174 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600175 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600176
Gavin Howard305249a2018-10-15 20:24:47 -0600177 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700178 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600179}
180
Gavin Howard1cbfe242019-01-09 17:13:11 -0700181static void bc_num_split(const BcNum *restrict n, size_t idx, BcNum *restrict a, BcNum *restrict b) {
Gavin Howard50c80022019-01-03 15:14:33 -0700182
Gavin Howard305249a2018-10-15 20:24:47 -0600183 if (idx < n->len) {
184
Gavin Howard305249a2018-10-15 20:24:47 -0600185 b->len = n->len - idx;
186 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600187 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600188
189 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
190 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard50c80022019-01-03 15:14:33 -0700191
192 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600193 }
Gavin Howard50c80022019-01-03 15:14:33 -0700194 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600195
196 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600197}
198
Gavin Howard1cbfe242019-01-09 17:13:11 -0700199static BcStatus bc_num_shift(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600200
Gavin Howardddce6d42019-01-03 14:07:06 -0700201 if (!places || BC_NUM_ZERO(n)) return BC_STATUS_SUCCESS;
Gavin Howardecc7fc22018-12-31 13:21:47 -0700202 if (places + n->len > BC_MAX_NUM)
Gavin Howard92037352019-01-02 14:58:35 -0700203 return bc_vm_verr(BC_ERROR_MATH_OVERFLOW, "shifted left too far");
Gavin Howard305249a2018-10-15 20:24:47 -0600204
205 if (n->rdx >= places) n->rdx -= places;
206 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600207 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600208 n->rdx = 0;
209 }
210
211 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600212
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600213 return BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600214}
215
Gavin Howard1cbfe242019-01-09 17:13:11 -0700216static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600217
Gavin Howard63738202018-09-26 15:34:20 -0600218 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600219 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600220
Gavin Howardd32d7df2018-10-15 08:42:25 -0600221 one.cap = 2;
222 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600223 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600224
Gavin Howardd32d7df2018-10-15 08:42:25 -0600225 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600226}
227
Gavin Howard7bda4782018-12-28 09:53:22 -0700228#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700229static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
230 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700231{
Gavin Howardac4d9122018-12-27 23:59:12 -0700232 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
233 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700234 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700235}
Gavin Howard7bda4782018-12-28 09:53:22 -0700236#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700237
Gavin Howard1cbfe242019-01-09 17:13:11 -0700238static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700239
Gavin Howard8a921bd2018-10-11 14:15:32 -0600240 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600241 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700242 unsigned int carry, in;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700243
Gavin Howard63738202018-09-26 15:34:20 -0600244 // Because this function doesn't need to use scale (per the bc spec),
245 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600246
Gavin Howardddce6d42019-01-03 14:07:06 -0700247 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600248 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700249 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600250 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600251 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700252 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600253 bc_num_copy(c, a);
254 return BC_STATUS_SUCCESS;
255 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600256
Gavin Howard63738202018-09-26 15:34:20 -0600257 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600258 c->rdx = BC_MAX(a->rdx, b->rdx);
259 min_rdx = BC_MIN(a->rdx, b->rdx);
260 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700261
Gavin Howard63738202018-09-26 15:34:20 -0600262 if (a->rdx > b->rdx) {
263 diff = a->rdx - b->rdx;
264 ptr = a->num;
265 ptr_a = a->num + diff;
266 ptr_b = b->num;
267 }
268 else {
269 diff = b->rdx - a->rdx;
270 ptr = b->num;
271 ptr_a = a->num;
272 ptr_b = b->num + diff;
273 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700274
Gavin Howard63738202018-09-26 15:34:20 -0600275 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700276
Gavin Howard63738202018-09-26 15:34:20 -0600277 ptr_c += diff;
Gavin Howard890d0c02018-10-30 16:34:50 -0600278 a_int = BC_NUM_INT(a);
279 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700280
Gavin Howard890d0c02018-10-30 16:34:50 -0600281 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600282 min_int = b_int;
283 max = a_int;
284 ptr = ptr_a;
285 }
286 else {
287 min_int = a_int;
288 max = b_int;
289 ptr = ptr_b;
290 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700291
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700292 for (carry = 0, i = 0; !BC_SIGNAL && i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700293 in = ((unsigned int) ptr_a[i]) + ((unsigned int) ptr_b[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600294 carry = in / 10;
Gavin Howard231e66c2019-01-08 23:02:19 -0700295 assert(carry >= 0 && carry < 10);
Gavin Howard11b9afd2018-10-18 14:23:28 -0600296 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard231e66c2019-01-08 23:02:19 -0700297 assert(ptr_c[i] >= 0 && ptr_c[i] < 10);
Gavin Howard63738202018-09-26 15:34:20 -0600298 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700299
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700300 for (; !BC_SIGNAL && i < max + min_rdx; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700301 in = ((unsigned int) ptr[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600302 carry = in / 10;
Gavin Howard231e66c2019-01-08 23:02:19 -0700303 assert(carry >= 0 && carry < 10);
Gavin Howard11b9afd2018-10-18 14:23:28 -0600304 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard231e66c2019-01-08 23:02:19 -0700305 assert(ptr_c[i] >= 0 && ptr_c[i] < 10);
Gavin Howard63738202018-09-26 15:34:20 -0600306 }
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 Howard5dbb48b2019-01-04 11:29:29 -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 Howard63738202018-09-26 15:34:20 -0600315 BcStatus s;
316 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 Howard63738202018-09-26 15:34:20 -0600369 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700370
Gavin Howard63738202018-09-26 15:34:20 -0600371 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700372
Gavin Howard63738202018-09-26 15:34:20 -0600373 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 Howard63738202018-09-26 15:34:20 -0600378 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 Howard5dbb48b2019-01-04 11:29:29 -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 Howard34c4ba02018-12-11 10:25:01 -0700399
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600400 bc_num_expand(c, a->len + b->len + 1);
Gavin Howard23d8d252018-10-17 12:03:41 -0600401
Gavin Howard305249a2018-10-15 20:24:47 -0600402 memset(c->num, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700403 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700404
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700405 for (i = 0; !BC_SIGNAL && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600406
Gavin Howard34c4ba02018-12-11 10:25:01 -0700407 carry = 0;
408
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700409 for (j = 0; !BC_SIGNAL && j < a->len; ++j) {
Gavin Howard1cbfe242019-01-09 17:13:11 -0700410 unsigned int in = (uchar) c->num[i + j];
Gavin Howard2aef61b2018-12-18 10:56:20 -0700411 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
412 in += carry;
Gavin Howard305249a2018-10-15 20:24:47 -0600413 carry = in / 10;
Gavin Howard231e66c2019-01-08 23:02:19 -0700414 assert(carry >= 0 && carry < 10);
Gavin Howard11b9afd2018-10-18 14:23:28 -0600415 c->num[i + j] = (BcDig) (in % 10);
Gavin Howard231e66c2019-01-08 23:02:19 -0700416 assert(c->num[i + j] >= 0 && c->num[i + j] < 10);
Gavin Howard305249a2018-10-15 20:24:47 -0600417 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700418
Gavin Howard88c25302018-10-17 13:32:23 -0600419 c->num[i + j] += (BcDig) carry;
Gavin Howard231e66c2019-01-08 23:02:19 -0700420 assert(c->num[i + j] >= 0 && c->num[i + j] < 10);
Gavin Howard88c25302018-10-17 13:32:23 -0600421 len = BC_MAX(len, i + j + !!carry);
Gavin Howard63738202018-09-26 15:34:20 -0600422 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700423
Gavin Howard305249a2018-10-15 20:24:47 -0600424 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600425
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700426 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600427 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700428
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600429 bc_num_init(&l1, max);
430 bc_num_init(&h1, max);
431 bc_num_init(&l2, max);
432 bc_num_init(&h2, max);
433 bc_num_init(&m1, max);
434 bc_num_init(&m2, max);
435 bc_num_init(&z0, max);
436 bc_num_init(&z1, max);
437 bc_num_init(&z2, max);
438 bc_num_init(&temp, max + max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600439
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600440 bc_num_split(a, max2, &l1, &h1);
441 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700442
Gavin Howard53eba8b2018-10-31 15:14:37 -0600443 s = bc_num_add(&h1, &l1, &m1, 0);
444 if (s) goto err;
445 s = bc_num_add(&h2, &l2, &m2, 0);
446 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600447
Gavin Howard53eba8b2018-10-31 15:14:37 -0600448 s = bc_num_k(&h1, &h2, &z0);
449 if (s) goto err;
450 s = bc_num_k(&m1, &m2, &z1);
451 if (s) goto err;
452 s = bc_num_k(&l1, &l2, &z2);
453 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600454
Gavin Howard53eba8b2018-10-31 15:14:37 -0600455 s = bc_num_sub(&z1, &z0, &temp, 0);
456 if (s) goto err;
457 s = bc_num_sub(&temp, &z2, &z1, 0);
458 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600459
Gavin Howard53eba8b2018-10-31 15:14:37 -0600460 s = bc_num_shift(&z0, max2 * 2);
461 if (s) goto err;
462 s = bc_num_shift(&z1, max2);
463 if (s) goto err;
464 s = bc_num_add(&z0, &z1, &temp, 0);
465 if (s) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600466 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600467
468err:
469 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600470 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600471 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600472 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600473 bc_num_free(&m2);
Gavin Howard305249a2018-10-15 20:24:47 -0600474 bc_num_free(&m1);
Gavin Howard305249a2018-10-15 20:24:47 -0600475 bc_num_free(&h2);
Gavin Howard305249a2018-10-15 20:24:47 -0600476 bc_num_free(&l2);
Gavin Howard305249a2018-10-15 20:24:47 -0600477 bc_num_free(&h1);
Gavin Howard305249a2018-10-15 20:24:47 -0600478 bc_num_free(&l1);
479 return s;
480}
481
Gavin Howard1cbfe242019-01-09 17:13:11 -0700482static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600483
484 BcStatus s;
485 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600486 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600487
488 scale = BC_MAX(scale, a->rdx);
489 scale = BC_MAX(scale, b->rdx);
490 scale = BC_MIN(a->rdx + b->rdx, scale);
491 maxrdx = BC_MAX(maxrdx, scale);
492
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600493 bc_num_init(&cpa, a->len);
494 bc_num_init(&cpb, b->len);
Gavin Howard305249a2018-10-15 20:24:47 -0600495
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600496 bc_num_copy(&cpa, a);
497 bc_num_copy(&cpb, b);
Gavin Howard23d8d252018-10-17 12:03:41 -0600498 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600499
Gavin Howard53eba8b2018-10-31 15:14:37 -0600500 s = bc_num_shift(&cpa, maxrdx);
501 if (s) goto err;
502 s = bc_num_shift(&cpb, maxrdx);
503 if (s) goto err;
504 s = bc_num_k(&cpa, &cpb, c);
505 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600506
Gavin Howard53eba8b2018-10-31 15:14:37 -0600507 maxrdx += scale;
508 bc_num_expand(c, c->len + maxrdx);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600509
Gavin Howard88c25302018-10-17 13:32:23 -0600510 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600511 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600512 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600513 }
514
Gavin Howard88c25302018-10-17 13:32:23 -0600515 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600516 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600517
518err:
519 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600520 bc_num_free(&cpa);
521 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700522}
523
Gavin Howard1cbfe242019-01-09 17:13:11 -0700524static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700525
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600526 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600527 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600528 size_t len, end, i;
529 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600530 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700531
Gavin Howardddce6d42019-01-03 14:07:06 -0700532 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
533 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600534 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600535 return BC_STATUS_SUCCESS;
536 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700537 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600538 bc_num_copy(c, a);
539 bc_num_retireMul(c, scale, a->neg, b->neg);
540 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600541 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700542
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600543 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
544 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600545 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700546
Gavin Howard890d0c02018-10-30 16:34:50 -0600547 if (len > cp.len) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600548 bc_num_expand(&cp, len + 2);
549 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600550 }
Gavin Howard021150b2018-03-10 15:40:42 -0700551
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600552 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600553 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600554 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700555
Gavin Howard63738202018-09-26 15:34:20 -0600556 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600557 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600558 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600559 len -= i - 1;
560 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700561
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600562 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700563
Gavin Howard63738202018-09-26 15:34:20 -0600564 // We want an extra zero in front to make things simpler.
565 cp.num[cp.len++] = 0;
566 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700567
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600568 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700569
Gavin Howarda2514a02018-10-18 14:20:09 -0600570 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600571 c->rdx = cp.rdx;
572 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600573 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700574
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700575 for (i = end - 1; !BC_SIGNAL && !s && i < end; --i) {
Gavin Howard63738202018-09-26 15:34:20 -0600576 n = cp.num + i;
Gavin Howard1ab22d22019-01-03 13:32:17 -0700577 for (q = 0; !s && (n[len] || bc_num_compare(n, p, len) >= 0); ++q)
Gavin Howard53eba8b2018-10-31 15:14:37 -0600578 s = bc_num_subArrays(n, p, len);
Gavin Howard63738202018-09-26 15:34:20 -0600579 c->num[i] = q;
580 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700581
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600582 if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600583 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600584
Gavin Howard63738202018-09-26 15:34:20 -0600585 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700586}
587
Gavin Howard1cbfe242019-01-09 17:13:11 -0700588static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
589 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600590{
Gavin Howard63738202018-09-26 15:34:20 -0600591 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600592 BcNum temp;
593 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700594
Gavin Howardddce6d42019-01-03 14:07:06 -0700595 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
596 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700597 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600598 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600599 return BC_STATUS_SUCCESS;
600 }
Gavin Howard8b254872018-03-14 01:13:35 -0600601
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600602 bc_num_init(&temp, d->cap);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600603 bc_num_d(a, b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700604
Gavin Howard1ab22d22019-01-03 13:32:17 -0700605 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600606
Gavin Howard53eba8b2018-10-31 15:14:37 -0600607 s = bc_num_m(c, b, &temp, scale);
608 if (s) goto err;
609 s = bc_num_sub(a, &temp, d, scale);
610 if (s) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600611
Gavin Howardddce6d42019-01-03 14:07:06 -0700612 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600613
614 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600615 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600616 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700617
618err:
Gavin Howard18441082018-10-22 10:03:30 -0600619 bc_num_free(&temp);
620 return s;
621}
622
Gavin Howard1cbfe242019-01-09 17:13:11 -0700623static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600624
Gavin Howard18441082018-10-22 10:03:30 -0600625 BcStatus s;
626 BcNum c1;
627 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
628
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600629 bc_num_init(&c1, len);
Gavin Howard54b946a2018-10-23 12:06:57 -0600630 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600631 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600632
Gavin Howard63738202018-09-26 15:34:20 -0600633 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700634}
635
Gavin Howard1cbfe242019-01-09 17:13:11 -0700636static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700637
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600638 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600639 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700640 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600641 size_t i, powrdx, resrdx;
642 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700643
Gavin Howardac4d9122018-12-27 23:59:12 -0700644 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700645
Gavin Howardddce6d42019-01-03 14:07:06 -0700646 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600647 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600648 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600649 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700650 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600651 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600652 return BC_STATUS_SUCCESS;
653 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700654 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600655 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600656 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600657 return s;
658 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700659
Gavin Howard63738202018-09-26 15:34:20 -0600660 neg = b->neg;
661 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600662 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700663 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600664 if (s) return s;
665
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600666 bc_num_init(&copy, a->len);
667 bc_num_copy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700668
Gavin Howard63738202018-09-26 15:34:20 -0600669 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600670
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700671 for (powrdx = a->rdx; !BC_SIGNAL && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600672 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600673 s = bc_num_mul(&copy, &copy, &copy, powrdx);
674 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600675 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700676
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700677 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700678 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600679 goto err;
680 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700681
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600682 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700683 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600684
Gavin Howard4d5daba2019-01-07 14:42:50 -0700685 while (!BC_SIGNAL && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600686
Gavin Howard4d5daba2019-01-07 14:42:50 -0700687 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600688 s = bc_num_mul(&copy, &copy, &copy, powrdx);
689 if (s) goto err;
690
Gavin Howard63738202018-09-26 15:34:20 -0600691 if (pow & 1) {
692 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600693 s = bc_num_mul(c, &copy, c, resrdx);
694 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600695 }
696 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700697
Gavin Howard53eba8b2018-10-31 15:14:37 -0600698 if (neg) {
699 s = bc_num_inv(c, c, scale);
700 if (s) goto err;
701 }
702
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700703 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700704 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600705 goto err;
706 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700707
Gavin Howard63738202018-09-26 15:34:20 -0600708 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700709
Gavin Howard53eba8b2018-10-31 15:14:37 -0600710 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600711 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600712 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700713
Gavin Howard6fbdb292018-02-27 15:44:48 -0700714err:
Gavin Howard63738202018-09-26 15:34:20 -0600715 bc_num_free(&copy);
716 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700717}
718
Gavin Howard7bda4782018-12-28 09:53:22 -0700719#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700720static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700721
722 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700723 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700724
725 BC_UNUSED(scale);
726
727 s = bc_num_intop(a, b, c, &val);
728 if (s) return s;
729
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700730 if (val < c->rdx) bc_num_truncate(c, c->rdx - val);
731 else if (val > c->rdx) bc_num_extend(c, val - c->rdx);
Gavin Howardac4d9122018-12-27 23:59:12 -0700732
733 return s;
734}
735
Gavin Howard1cbfe242019-01-09 17:13:11 -0700736static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700737
738 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700739 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700740
741 BC_UNUSED(scale);
742
743 s = bc_num_intop(a, b, c, &val);
744 if (s) return s;
745
746 bc_num_shift(c, val);
747
748 return s;
749}
750
Gavin Howard1cbfe242019-01-09 17:13:11 -0700751static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700752
753 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700754 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700755 size_t len;
756
757 BC_UNUSED(scale);
758
759 s = bc_num_intop(a, b, c, &val);
760 if (s) return s;
761
Gavin Howard2a366922019-01-16 10:50:20 -0700762 if (BC_NUM_ZERO(c)) return s;
763
Gavin Howardac4d9122018-12-27 23:59:12 -0700764 len = c->rdx + val;
765
766 if (len > c->len) {
767
Gavin Howard92037352019-01-02 14:58:35 -0700768 if (len > BC_MAX_NUM) return bc_vm_err(BC_ERROR_MATH_UNDERFLOW);
Gavin Howardac4d9122018-12-27 23:59:12 -0700769 if (len > c->cap) bc_num_expand(c, len);
770
771 memset(c->num + c->len, 0, len - c->len);
772 c->len = len;
773 }
774
775 c->rdx += val;
776
777 assert(c->rdx <= c->len && c->len <= c->cap);
778
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 Howarda1c44392018-09-27 12:41:15 -0600818 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 Howard1cbfe242019-01-09 17:13:11 -0700894static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
895 BcNum *restrict base, size_t base_t)
896{
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;
902 size_t i, digits, 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 Howard7536dcf2018-12-15 19:27:09 -0700905 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700906
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600907 bc_num_init(&temp, BC_NUM_DEF_SIZE);
908 bc_num_init(&mult, BC_NUM_DEF_SIZE);
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 Howard53eba8b2018-10-31 15:14:37 -0600911
Gavin Howardc7d655b2018-12-28 19:45:13 -0700912 v = bc_num_parseChar(c, base_t);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700913
Gavin Howard53eba8b2018-10-31 15:14:37 -0600914 s = bc_num_mul(n, base, &mult, 0);
915 if (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);
918 if (s) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600919 }
Gavin Howardede51f02018-03-02 12:30:00 -0700920
Gavin Howard1ab22d22019-01-03 13:32:17 -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 Howardc7d655b2018-12-28 19:45:13 -0700927 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600928
Gavin Howardc7d655b2018-12-28 19:45:13 -0700929 v = bc_num_parseChar(c, base_t);
Gavin Howardede51f02018-03-02 12:30:00 -0700930
Gavin Howard53eba8b2018-10-31 15:14:37 -0600931 s = bc_num_mul(&result, base, &result, 0);
932 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700933 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600934 s = bc_num_add(&result, &temp, &result, 0);
935 if (s) goto err;
936 s = bc_num_mul(&mult, base, &mult, 0);
937 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600938 }
Gavin Howardede51f02018-03-02 12:30:00 -0700939
Gavin Howard53eba8b2018-10-31 15:14:37 -0600940 s = bc_num_div(&result, &mult, &result, digits);
941 if (s) goto err;
942 s = bc_num_add(n, &result, n, digits);
943 if (s) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600944
Gavin Howardddce6d42019-01-03 14:07:06 -0700945 if (BC_NUM_NONZERO(n)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600946 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600947 }
948 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700949
950err:
Gavin Howard63738202018-09-26 15:34:20 -0600951 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700952int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600953 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -0600954 bc_num_free(&temp);
Gavin Howard7536dcf2018-12-15 19:27:09 -0700955 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700956}
957
Gavin Howard1cbfe242019-01-09 17:13:11 -0700958static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -0700959 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -0600960 bc_vm_putchar('\\');
961 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -0700962 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600963 }
Gavin Howard80977b22018-09-06 14:29:13 -0600964}
965
Gavin Howard40a085f2018-12-03 12:08:59 -0700966#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -0700967static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -0700968 BC_UNUSED(rdx);
969 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -0700970 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600971}
972#endif // DC_ENABLED
973
Gavin Howard1cbfe242019-01-09 17:13:11 -0700974static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
975
Gavin Howarda84ad992018-12-03 19:11:06 -0700976 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -0600977
Gavin Howardab5e9632019-01-02 13:32:02 -0700978 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700979 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -0700980 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600981
Gavin Howardab5e9632019-01-02 13:32:02 -0700982 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700983 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600984
Gavin Howardab5e9632019-01-02 13:32:02 -0700985 for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -0700986 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -0700987 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700988 dig = n / pow;
989 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -0700990 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -0600991 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700992}
Gavin Howardfe679f02018-02-14 15:50:09 -0700993
Gavin Howard1cbfe242019-01-09 17:13:11 -0700994static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -0700995
Gavin Howard3c02ed32018-12-28 18:17:15 -0700996 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600997
Gavin Howard3c02ed32018-12-28 18:17:15 -0700998 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -0700999 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001000 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001001 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001002 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001003
Gavin Howardab5e9632019-01-02 13:32:02 -07001004 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001005 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001006 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001007}
1008
Gavin Howard1cbfe242019-01-09 17:13:11 -07001009static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001010
Gavin Howard63738202018-09-26 15:34:20 -06001011 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001012
Gavin Howard48af52e2018-10-30 14:47:38 -06001013 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001014 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001015
Gavin Howard48af52e2018-10-30 14:47:38 -06001016 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001017 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001018}
1019
Gavin Howard1cbfe242019-01-09 17:13:11 -07001020static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1021 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001022{
Gavin Howard63738202018-09-26 15:34:20 -06001023 BcStatus s;
1024 BcVec stack;
1025 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001026 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001027 size_t i;
1028 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001029
Gavin Howardddce6d42019-01-03 14:07:06 -07001030 if (BC_NUM_ZERO(n)) {
1031 print(0, len, false);
Gavin Howard48af52e2018-10-30 14:47:38 -06001032 return BC_STATUS_SUCCESS;
1033 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001034
Gavin Howardd3929832018-12-24 15:13:15 -07001035 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001036 bc_num_init(&intp, n->len);
1037 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001038 bc_num_init(&digit, len);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001039 bc_num_init(&frac_len, BC_NUM_INT(n));
1040 bc_num_copy(&intp, n);
Gavin Howard63738202018-09-26 15:34:20 -06001041 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -06001042
Gavin Howard63738202018-09-26 15:34:20 -06001043 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001044 s = bc_num_sub(n, &intp, &fracp, 0);
1045 if (s) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001046
Gavin Howardddce6d42019-01-03 14:07:06 -07001047 while (BC_NUM_NONZERO(&intp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001048 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
1049 if (s) goto err;
1050 s = bc_num_ulong(&digit, &dig);
1051 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001052 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001053 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001054
Gavin Howard63738202018-09-26 15:34:20 -06001055 for (i = 0; i < stack.len; ++i) {
1056 ptr = bc_vec_item_rev(&stack, i);
1057 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001058 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001059 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001060
Gavin Howard63738202018-09-26 15:34:20 -06001061 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001062
Gavin Howard63738202018-09-26 15:34:20 -06001063 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001064 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
1065 if (s) goto err;
1066 s = bc_num_ulong(&fracp, &dig);
1067 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001068 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001069 s = bc_num_sub(&fracp, &intp, &fracp, 0);
1070 if (s) goto err;
Gavin Howardddce6d42019-01-03 14:07:06 -07001071 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001072 s = bc_num_mul(&frac_len, base, &frac_len, 0);
1073 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001074 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001075
1076err:
Gavin Howard63738202018-09-26 15:34:20 -06001077 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001078 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001079 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001080 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001081 bc_vec_free(&stack);
Gavin Howard63738202018-09-26 15:34:20 -06001082 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001083}
1084
Gavin Howard1cbfe242019-01-09 17:13:11 -07001085static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base, size_t base_t) {
Gavin Howard5a0616e2019-01-03 13:33:50 -07001086
Gavin Howard83eb8392018-10-09 01:21:19 -06001087 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001088 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001089 BcNumDigitOp print;
1090 bool neg = n->neg;
1091
Gavin Howard48af52e2018-10-30 14:47:38 -06001092 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001093 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001094
1095 n->neg = false;
1096
Gavin Howardbe4e8222019-01-03 13:34:16 -07001097 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001098 width = 1;
1099 print = bc_num_printHex;
1100 }
1101 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001102 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001103 print = bc_num_printDigits;
1104 }
1105
Gavin Howardab5e9632019-01-02 13:32:02 -07001106 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001107 n->neg = neg;
1108
1109 return s;
1110}
1111
Gavin Howard40a085f2018-12-03 12:08:59 -07001112#if DC_ENABLED
Gavin Howardab5e9632019-01-02 13:32:02 -07001113BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1114 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001115}
1116#endif // DC_ENABLED
1117
Gavin Howard3c02ed32018-12-28 18:17:15 -07001118void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001119 assert(n);
1120 n->num = num;
1121 n->cap = cap;
1122 n->rdx = n->len = 0;
1123 n->neg = false;
1124}
1125
Gavin Howard3c02ed32018-12-28 18:17:15 -07001126void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001127 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001128 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001129 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001130}
1131
Gavin Howard3c02ed32018-12-28 18:17:15 -07001132void bc_num_expand(BcNum *restrict n, size_t req) {
Gavin Howard844e7a52018-10-09 11:46:39 -06001133 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001134 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1135 if (req > n->cap) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001136 n->num = bc_vm_realloc(n->num, req);
1137 n->cap = req;
1138 }
Gavin Howardb5c77212018-02-14 17:12:34 -07001139}
1140
Gavin Howarded392aa2018-02-27 13:09:26 -07001141void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001142 assert(num);
1143 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001144}
1145
Gavin Howard3c02ed32018-12-28 18:17:15 -07001146void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001147 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001148 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001149 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001150 d->len = s->len;
1151 d->neg = s->neg;
1152 d->rdx = s->rdx;
1153 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001154}
1155
Gavin Howard3c02ed32018-12-28 18:17:15 -07001156BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
Gavin Howardd746e802018-12-28 22:06:00 -07001157 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001158{
Gavin Howard7536dcf2018-12-15 19:27:09 -07001159 BcStatus s = BC_STATUS_SUCCESS;
1160
Gavin Howard63738202018-09-26 15:34:20 -06001161 assert(n && val && base);
Gavin Howard96edeb32018-12-31 09:33:22 -07001162 assert(base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase);
Gavin Howardc90ed902019-01-02 13:07:49 -07001163 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001164
Gavin Howardd746e802018-12-28 22:06:00 -07001165 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howardc7d655b2018-12-28 19:45:13 -07001166 else if (base_t == 10) bc_num_parseDecimal(n, val);
1167 else s = bc_num_parseBase(n, val, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001168
Gavin Howard7536dcf2018-12-15 19:27:09 -07001169 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001170}
1171
Gavin Howardab5e9632019-01-02 13:32:02 -07001172BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1173 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001174{
Gavin Howard48af52e2018-10-30 14:47:38 -06001175 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001176
Gavin Howardab5e9632019-01-02 13:32:02 -07001177 assert(n && base);
Gavin Howard63738202018-09-26 15:34:20 -06001178 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001179
Gavin Howardab5e9632019-01-02 13:32:02 -07001180 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001181
Gavin Howardddce6d42019-01-03 14:07:06 -07001182 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howardab5e9632019-01-02 13:32:02 -07001183 else if (base_t == 10) bc_num_printDecimal(n);
1184 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001185
Gavin Howardddce6d42019-01-03 14:07:06 -07001186 if (!s && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001187 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001188 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001189 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001190
Gavin Howard63738202018-09-26 15:34:20 -06001191 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001192}
1193
Gavin Howard3c02ed32018-12-28 18:17:15 -07001194BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001195
Gavin Howard63738202018-09-26 15:34:20 -06001196 size_t i;
Gavin Howard7b557da2018-12-21 10:25:32 -07001197 unsigned long pow, r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001198
Gavin Howard63738202018-09-26 15:34:20 -06001199 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001200
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001201 *result = 0;
1202
Gavin Howard7536dcf2018-12-15 19:27:09 -07001203 if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001204
Gavin Howard7b557da2018-12-21 10:25:32 -07001205 for (r = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001206
Gavin Howard7b557da2018-12-21 10:25:32 -07001207 unsigned long prev = r, powprev = pow;
Gavin Howard88c25302018-10-17 13:32:23 -06001208
Gavin Howard7b557da2018-12-21 10:25:32 -07001209 r += ((unsigned long) n->num[i]) * pow;
Gavin Howard63738202018-09-26 15:34:20 -06001210 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001211
Gavin Howard92037352019-01-02 14:58:35 -07001212 if (r < prev || pow < powprev)
1213 return bc_vm_verr(BC_ERROR_MATH_OVERFLOW, "number cannot fit");
Gavin Howard63738202018-09-26 15:34:20 -06001214 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001215
Gavin Howard7b557da2018-12-21 10:25:32 -07001216 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001217
Gavin Howard63738202018-09-26 15:34:20 -06001218 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001219}
1220
Gavin Howard3c02ed32018-12-28 18:17:15 -07001221void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001222
Gavin Howardec82bdc2018-10-08 23:50:57 -06001223 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001224 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001225 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001226
Gavin Howard63738202018-09-26 15:34:20 -06001227 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001228
Gavin Howard63738202018-09-26 15:34:20 -06001229 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001230
Gavin Howard1ab22d22019-01-03 13:32:17 -07001231 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001232
Gavin Howard1ab22d22019-01-03 13:32:17 -07001233 len = bc_num_log10(ULONG_MAX);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001234 bc_num_expand(n, len);
Gavin Howardec82bdc2018-10-08 23:50:57 -06001235 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001236}
1237
Gavin Howard12fe7812018-09-29 03:45:18 -06001238BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001239 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001240 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001241 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001242}
1243
Gavin Howard12fe7812018-09-29 03:45:18 -06001244BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001245 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001246 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001247 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001248}
1249
Gavin Howard12fe7812018-09-29 03:45:18 -06001250BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001251 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001252 return bc_num_binary(a, b, c, scale, bc_num_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001253}
1254
Gavin Howard12fe7812018-09-29 03:45:18 -06001255BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001256 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001257 return bc_num_binary(a, b, c, scale, bc_num_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001258}
1259
Gavin Howard54b946a2018-10-23 12:06:57 -06001260BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001261 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001262 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001263}
1264
Gavin Howard12fe7812018-09-29 03:45:18 -06001265BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001266 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001267}
1268
Gavin Howard7d8f0382018-12-27 16:15:13 -07001269#if BC_ENABLE_EXTRA_MATH
1270BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001271 return bc_num_binary(a, b, c, scale, bc_num_place, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001272}
1273
1274BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001275 return bc_num_binary(a, b, c, scale, bc_num_left, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001276}
1277
1278BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001279 return bc_num_binary(a, b, c, scale, bc_num_right, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001280}
1281#endif // BC_ENABLE_EXTRA_MATH
1282
Gavin Howard3c02ed32018-12-28 18:17:15 -07001283BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001284
Gavin Howard1cbfe242019-01-09 17:13:11 -07001285 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001286 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001287 size_t pow, len, digs, digs1, resrdx, req, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001288 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001289 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001290
Gavin Howard34edd0a2018-10-25 15:51:05 -06001291 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001292
Gavin Howardd8d60c42018-10-29 12:13:09 -06001293 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001294 bc_num_expand(b, req);
Gavin Howard954276b2018-05-16 01:43:58 -06001295
Gavin Howardddce6d42019-01-03 14:07:06 -07001296 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001297 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001298 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001299 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001300 if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1301 if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001302 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001303 bc_num_extend(b, scale);
1304 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001305 }
Gavin Howard954276b2018-05-16 01:43:58 -06001306
Gavin Howard890d0c02018-10-30 16:34:50 -06001307 scale = BC_MAX(scale, a->rdx) + 1;
1308 len = a->len + scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001309
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001310 bc_num_init(&num1, len);
1311 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001312 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001313
Gavin Howard63738202018-09-26 15:34:20 -06001314 bc_num_one(&half);
1315 half.num[0] = 5;
1316 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001317
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001318 bc_num_init(&f, len);
1319 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001320
Gavin Howard63738202018-09-26 15:34:20 -06001321 x0 = &num1;
1322 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001323
Gavin Howard63738202018-09-26 15:34:20 -06001324 bc_num_one(x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001325 pow = BC_NUM_INT(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001326
Gavin Howard53eba8b2018-10-31 15:14:37 -06001327 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001328
Gavin Howardc39fd492018-10-04 10:07:03 -06001329 if (pow & 1) x0->num[0] = 2;
1330 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001331
Gavin Howardc39fd492018-10-04 10:07:03 -06001332 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001333
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001334 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001335
Gavin Howard63738202018-09-26 15:34:20 -06001336 // Make sure to move the radix back.
1337 x0->rdx -= pow;
1338 }
Gavin Howard954276b2018-05-16 01:43:58 -06001339
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001340 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001341 resrdx = scale + 2;
1342 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001343
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001344 while (!BC_SIGNAL && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001345
Gavin Howard53eba8b2018-10-31 15:14:37 -06001346 s = bc_num_div(a, x0, &f, resrdx);
1347 if (s) goto err;
1348 s = bc_num_add(x0, &f, &fprime, resrdx);
1349 if (s) goto err;
1350 s = bc_num_mul(&fprime, &half, x1, resrdx);
1351 if (s) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001352
Gavin Howard53eba8b2018-10-31 15:14:37 -06001353 cmp = bc_num_cmp(x1, x0);
1354 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001355
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001356 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001357 else times = 0;
1358
1359 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001360
1361 cmp2 = cmp1;
1362 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001363 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001364
Gavin Howard63738202018-09-26 15:34:20 -06001365 temp = x0;
1366 x0 = x1;
1367 x1 = temp;
1368 }
Gavin Howard954276b2018-05-16 01:43:58 -06001369
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001370 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -07001371 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -06001372 goto err;
1373 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001374
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001375 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001376 scale -= 1;
1377 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001378
1379err:
Gavin Howard63738202018-09-26 15:34:20 -06001380 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001381 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001382 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001383 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001384 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001385 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001386 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001387}
Gavin Howardba009802018-09-29 04:41:51 -06001388
Gavin Howard2cb39612018-10-22 08:46:48 -06001389BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1390
1391 BcStatus s;
1392 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001393 bool init = false;
Gavin Howard2cb39612018-10-22 08:46:48 -06001394 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
1395
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001396 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001397
Gavin Howard890d0c02018-10-30 16:34:50 -06001398 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001399 memcpy(&num2, c, sizeof(BcNum));
1400 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001401 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001402 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001403 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001404 else {
1405 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001406 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001407 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001408
Gavin Howard54b946a2018-10-23 12:06:57 -06001409 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001410
Gavin Howardddce6d42019-01-03 14:07:06 -07001411 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001412 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001413 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001414 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001415
1416 if (init) bc_num_free(&num2);
1417
1418 return s;
1419}
1420
Gavin Howard40a085f2018-12-03 12:08:59 -07001421#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001422BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001423
1424 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001425 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001426 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001427
Gavin Howard34edd0a2018-10-25 15:51:05 -06001428 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001429
Gavin Howardddce6d42019-01-03 14:07:06 -07001430 if (BC_NUM_ZERO(c)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard7536dcf2018-12-15 19:27:09 -07001431 if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1432 if (a->rdx || b->rdx || c->rdx)
1433 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001434
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001435 bc_num_expand(d, c->len);
1436 bc_num_init(&base, c->len);
1437 bc_num_init(&exp, b->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001438 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001439 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001440
1441 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001442 two.num[0] = 2;
1443 bc_num_one(d);
1444
Gavin Howard53eba8b2018-10-31 15:14:37 -06001445 s = bc_num_rem(a, c, &base, 0);
1446 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001447 bc_num_copy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001448
Gavin Howardddce6d42019-01-03 14:07:06 -07001449 while (BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001450
Gavin Howard53eba8b2018-10-31 15:14:37 -06001451 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
1452 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001453
1454 if (BC_NUM_ONE(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001455 s = bc_num_mul(d, &base, &temp, 0);
1456 if (s) goto err;
1457 s = bc_num_rem(&temp, c, d, 0);
1458 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001459 }
1460
Gavin Howard53eba8b2018-10-31 15:14:37 -06001461 s = bc_num_mul(&base, &base, &temp, 0);
1462 if (s) goto err;
1463 s = bc_num_rem(&temp, c, &base, 0);
1464 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001465 }
1466
1467err:
1468 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001469 bc_num_free(&exp);
Gavin Howardba009802018-09-29 04:41:51 -06001470 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001471 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001472 return s;
1473}
Gavin Howarde6e84762018-10-03 11:46:34 -06001474#endif // DC_ENABLED