blob: 71dfb8f7b06927f96e618fb4f8f47951f24eb51e [file] [log] [blame]
Gavin Howard3eb626f2018-02-14 13:54:35 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -07003 *
Gavin Howard7345cb92019-04-08 14:13:43 -06004 * Copyright (c) 2018-2019 Gavin D. Howard and contributors.
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
Gavin Howard7345cb92019-04-08 14:13:43 -06006 * All rights reserved.
Gavin Howard3eb626f2018-02-14 13:54:35 -07007 *
Gavin Howard7345cb92019-04-08 14:13:43 -06008 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
Gavin Howard3eb626f2018-02-14 13:54:35 -070029 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070030 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070031 *
32 * Code for the number type.
33 *
34 */
35
36#include <assert.h>
Gavin Howardc7d655b2018-12-28 19:45:13 -070037#include <ctype.h>
Gavin Howard411f7322018-09-26 17:21:19 -060038#include <stdbool.h>
39#include <stdlib.h>
40#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070041
Gavin Howard305249a2018-10-15 20:24:47 -060042#include <limits.h>
43
Gavin Howard29493062018-03-20 19:57:37 -060044#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070045#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060046#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070047
Gavin Howard56ebcf72019-02-21 16:59:48 -070048static ssize_t bc_num_neg(size_t n, bool neg) {
49 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
50}
51
Gavin Howardd2cf06a2019-02-21 17:03:24 -070052ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -070053 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -070054}
55
Gavin Howard7b7d2f32019-02-21 16:46:47 -070056static size_t bc_num_int(const BcNum *n) {
57 return n->len ? n->len - n->rdx : 0;
58}
59
Gavin Howarda83a5e52019-02-21 16:51:06 -070060static bool bc_num_isOne(const BcNum *n) {
61 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
62}
63
Gavin Howard798508b2019-02-21 16:45:32 -070064static void bc_num_expand(BcNum *restrict n, size_t req) {
65 assert(n);
66 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
67 if (req > n->cap) {
68 n->num = bc_vm_realloc(n->num, req);
69 n->cap = req;
70 }
71}
72
Gavin Howard1cbfe242019-01-09 17:13:11 -070073static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060074 assert(n);
Gavin Howard97102b12018-11-01 23:37:31 -060075 n->len = 0;
76 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060077 n->rdx = scale;
78}
79
Gavin Howard1cbfe242019-01-09 17:13:11 -070080static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060081 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060082}
83
Gavin Howard3c02ed32018-12-28 18:17:15 -070084void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060085 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060086 n->len = 1;
87 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060088}
89
Gavin Howard3c02ed32018-12-28 18:17:15 -070090void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060091 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060092 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060093 n->len = 2;
94 n->num[0] = 0;
95 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060096}
97
Gavin Howard1cbfe242019-01-09 17:13:11 -070098static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -070099 size_t len;
100 for (len = 1; i; i /= 10, ++len);
101 return len;
102}
103
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700104static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
105 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700106{
Gavin Howard63738202018-09-26 15:34:20 -0600107 size_t i, j;
Gavin Howard2d188a52019-02-25 14:19:08 -0700108 for (i = 0; BC_NO_SIG && i < len; ++i) {
109 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard54b946a2018-10-23 12:06:57 -0600110 a[i + j++] += 10;
111 a[i + j] -= 1;
Gavin Howard231e66c2019-01-08 23:02:19 -0700112 assert(a[i + j - 1] >= 0 && a[i + j - 1] < 10);
Gavin Howard63738202018-09-26 15:34:20 -0600113 }
114 }
Gavin Howard2d188a52019-02-25 14:19:08 -0700115 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600116}
117
Gavin Howard2d188a52019-02-25 14:19:08 -0700118static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
119 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700120{
Gavin Howard63738202018-09-26 15:34:20 -0600121 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -0600122 int c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700123 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600124 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600125}
126
Gavin Howard3c02ed32018-12-28 18:17:15 -0700127ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700128
Gavin Howard63738202018-09-26 15:34:20 -0600129 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600130 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600131 bool a_max, neg = false;
132 ssize_t cmp;
133
134 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700135
Gavin Howard8a921bd2018-10-11 14:15:32 -0600136 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700137 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700138 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600139 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600140 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600141 else return -1;
142 }
143 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700144
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700145 a_int = bc_num_int(a);
146 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600147 a_int -= b_int;
148 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700149
Gavin Howard1ab22d22019-01-03 13:32:17 -0700150 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700151
Gavin Howard890d0c02018-10-30 16:34:50 -0600152 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600153 min = b->rdx;
154 diff = a->rdx - b->rdx;
155 max_num = a->num + diff;
156 min_num = b->num;
157 }
158 else {
159 min = a->rdx;
160 diff = b->rdx - a->rdx;
161 max_num = b->num + diff;
162 min_num = a->num;
163 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700164
Gavin Howard890d0c02018-10-30 16:34:50 -0600165 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600166 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700167 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700168
Gavin Howard2d188a52019-02-25 14:19:08 -0700169 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700170 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600171 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700172
Gavin Howardf2ee6342019-04-09 17:05:20 -0600173 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700174}
175
Gavin Howard2a366922019-01-16 10:50:20 -0700176static void bc_num_clean(BcNum *restrict n) {
177 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
178 if (BC_NUM_ZERO(n)) n->neg = false;
179 else if (n->len < n->rdx) n->len = n->rdx;
180}
181
Gavin Howard3c02ed32018-12-28 18:17:15 -0700182void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700183
Gavin Howardddce6d42019-01-03 14:07:06 -0700184 assert(places <= n->rdx && (BC_NUM_ZERO(n) || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700185
Gavin Howard1ab22d22019-01-03 13:32:17 -0700186 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700187
Gavin Howard63738202018-09-26 15:34:20 -0600188 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700189
Gavin Howardddce6d42019-01-03 14:07:06 -0700190 if (BC_NUM_NONZERO(n)) {
Gavin Howard955da852018-10-23 11:08:20 -0600191 n->len -= places;
192 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
Gavin Howard2a366922019-01-16 10:50:20 -0700193 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600194 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700195}
196
Gavin Howard1cbfe242019-01-09 17:13:11 -0700197static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700198
Gavin Howard007afdf2019-02-23 09:45:42 -0700199 size_t len = bc_vm_growSize(n->len, places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700200
Gavin Howard1ab22d22019-01-03 13:32:17 -0700201 if (!places) return;
Gavin Howard1ab22d22019-01-03 13:32:17 -0700202 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700203
Gavin Howard1ab22d22019-01-03 13:32:17 -0700204 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
205 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700206
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700207 if (n->len) n->len += places;
208
Gavin Howard1ab22d22019-01-03 13:32:17 -0700209 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700210}
211
Gavin Howard2d188a52019-02-25 14:19:08 -0700212static void bc_num_retireMul(BcNum *restrict n, size_t scale,
213 bool neg1, bool neg2)
214{
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600215 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600216 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600217
Gavin Howard305249a2018-10-15 20:24:47 -0600218 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700219 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600220}
221
Gavin Howard2d188a52019-02-25 14:19:08 -0700222static void bc_num_split(const BcNum *restrict n, size_t idx,
223 BcNum *restrict a, BcNum *restrict b)
224{
Gavin Howard305249a2018-10-15 20:24:47 -0600225 if (idx < n->len) {
226
Gavin Howard305249a2018-10-15 20:24:47 -0600227 b->len = n->len - idx;
228 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600229 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600230
231 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
232 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard50c80022019-01-03 15:14:33 -0700233
234 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600235 }
Gavin Howard50c80022019-01-03 15:14:33 -0700236 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600237
238 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600239}
240
Gavin Howarddbbada82019-02-22 08:51:19 -0700241static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600242
Gavin Howardd8b004b2019-02-16 22:35:08 -0700243 if (!places || BC_NUM_ZERO(n)) return;
Gavin Howard305249a2018-10-15 20:24:47 -0600244
245 if (n->rdx >= places) n->rdx -= places;
246 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600247 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600248 n->rdx = 0;
249 }
250
251 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600252}
253
Gavin Howard7ad5a662019-02-19 14:40:46 -0700254#if BC_ENABLE_EXTRA_MATH
Gavin Howarddbbada82019-02-22 08:51:19 -0700255static void bc_num_shiftRight(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700256
Gavin Howard007afdf2019-02-23 09:45:42 -0700257 size_t len = bc_vm_growSize(n->rdx, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700258
259 if (len > n->len) {
260
261 if (len > n->cap) bc_num_expand(n, len);
262
263 memset(n->num + n->len, 0, len - n->len);
264 n->len = len;
265 }
266
267 n->rdx += places;
268
269 assert(n->rdx <= n->len && n->len <= n->cap);
270}
271#endif // BC_ENABLE_EXTRA_MATH
272
Gavin Howard1cbfe242019-01-09 17:13:11 -0700273static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600274
Gavin Howard63738202018-09-26 15:34:20 -0600275 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600276 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600277
Gavin Howard1769abc2019-02-21 09:36:14 -0700278 assert(!BC_NUM_ZERO(a));
279
Gavin Howardd32d7df2018-10-15 08:42:25 -0600280 one.cap = 2;
281 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600282 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600283
Gavin Howardd32d7df2018-10-15 08:42:25 -0600284 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600285}
286
Gavin Howard7bda4782018-12-28 09:53:22 -0700287#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700288static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
289 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700290{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700291 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700292 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700293 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700294}
Gavin Howard7bda4782018-12-28 09:53:22 -0700295#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700296
Gavin Howard2d188a52019-02-25 14:19:08 -0700297static unsigned int bc_num_addDigit(BcDig *restrict num, unsigned int d,
298 unsigned int c)
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700299{
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700300 d += c;
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700301 *num = (BcDig) (d % 10);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700302 assert(*num >= 0 && *num < 10);
Gavin Howard201cb6f2019-01-22 10:01:29 -0700303 assert((d / 10) < 10);
304 return d / 10;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700305}
306
Gavin Howard1cbfe242019-01-09 17:13:11 -0700307static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700308
Gavin Howard8a921bd2018-10-11 14:15:32 -0600309 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600310 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700311 unsigned int carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700312
Gavin Howard63738202018-09-26 15:34:20 -0600313 // Because this function doesn't need to use scale (per the bc spec),
314 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600315
Gavin Howardddce6d42019-01-03 14:07:06 -0700316 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600317 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700318 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600319 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600320 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700321 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600322 bc_num_copy(c, a);
323 return BC_STATUS_SUCCESS;
324 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600325
Gavin Howard63738202018-09-26 15:34:20 -0600326 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600327 c->rdx = BC_MAX(a->rdx, b->rdx);
328 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700329
Gavin Howard63738202018-09-26 15:34:20 -0600330 if (a->rdx > b->rdx) {
331 diff = a->rdx - b->rdx;
332 ptr = a->num;
333 ptr_a = a->num + diff;
334 ptr_b = b->num;
335 }
336 else {
337 diff = b->rdx - a->rdx;
338 ptr = b->num;
339 ptr_a = a->num;
340 ptr_b = b->num + diff;
341 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700342
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700343 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700344
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700345 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600346 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700347 a_int = bc_num_int(a);
348 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700349
Gavin Howard890d0c02018-10-30 16:34:50 -0600350 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600351 min_int = b_int;
352 max = a_int;
353 ptr = ptr_a;
354 }
355 else {
356 min_int = a_int;
357 max = b_int;
358 ptr = ptr_b;
359 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700360
Gavin Howard2d188a52019-02-25 14:19:08 -0700361 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700362 unsigned int in = (unsigned int) (ptr_a[i] + ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700363 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600364 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700365
Gavin Howard2d188a52019-02-25 14:19:08 -0700366 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700367 carry = bc_num_addDigit(ptr_c + i, (unsigned int) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700368
369 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700370
Gavin Howard1ab22d22019-01-03 13:32:17 -0700371 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600372
Gavin Howard2d188a52019-02-25 14:19:08 -0700373 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700374}
375
Gavin Howard1cbfe242019-01-09 17:13:11 -0700376static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700377
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700378 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600379 ssize_t cmp;
380 BcNum *minuend, *subtrahend;
381 size_t start;
382 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700383
Gavin Howard63738202018-09-26 15:34:20 -0600384 // Because this function doesn't need to use scale (per the bc spec),
385 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700386
Gavin Howardddce6d42019-01-03 14:07:06 -0700387 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600388 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700389 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600390 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600391 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700392 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600393 bc_num_copy(c, a);
394 return BC_STATUS_SUCCESS;
395 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700396
Gavin Howard63738202018-09-26 15:34:20 -0600397 aneg = a->neg;
398 bneg = b->neg;
399 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700400
Gavin Howard63738202018-09-26 15:34:20 -0600401 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600402 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700403
Gavin Howard63738202018-09-26 15:34:20 -0600404 a->neg = aneg;
405 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700406
Gavin Howard1ab22d22019-01-03 13:32:17 -0700407 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600408 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600409 return BC_STATUS_SUCCESS;
410 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700411
412 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600413 neg = a->neg;
414 minuend = a;
415 subtrahend = b;
416 }
417 else {
418 neg = b->neg;
419 if (sub) neg = !neg;
420 minuend = b;
421 subtrahend = a;
422 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700423
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600424 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600425 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700426
Gavin Howard63738202018-09-26 15:34:20 -0600427 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600428 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600429 start = 0;
430 }
431 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700432
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700433 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700434
Gavin Howard63738202018-09-26 15:34:20 -0600435 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700436
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700437 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700438}
439
Gavin Howard1cbfe242019-01-09 17:13:11 -0700440static BcStatus bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600441
Gavin Howard9b801632019-02-16 23:34:06 -0700442 BcStatus s;
Gavin Howarde0432502019-02-26 16:23:45 -0700443 size_t max, max2, total, size;
Gavin Howard305249a2018-10-15 20:24:47 -0600444 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700445 BcDig *digs, *dig_ptr;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700446 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600447
Gavin Howardddce6d42019-01-03 14:07:06 -0700448 // This is here because the function is recursive.
Gavin Howard2d188a52019-02-25 14:19:08 -0700449 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700450 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600451 bc_num_zero(c);
452 return BC_STATUS_SUCCESS;
453 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700454 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600455 bc_num_copy(c, aone ? b : a);
456 return BC_STATUS_SUCCESS;
457 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700458
Gavin Howardb5093d72018-10-17 12:13:17 -0600459 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
460 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600461 {
Gavin Howard34c4ba02018-12-11 10:25:01 -0700462 size_t i, j, len;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700463 unsigned int carry;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700464 BcDig *ptr_c;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700465
Gavin Howard007afdf2019-02-23 09:45:42 -0700466 bc_num_expand(c, bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1));
Gavin Howard23d8d252018-10-17 12:03:41 -0600467
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700468 ptr_c = c->num;
469 memset(ptr_c, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700470 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700471
Gavin Howard2d188a52019-02-25 14:19:08 -0700472 for (i = 0; BC_NO_SIG && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600473
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700474 BcDig *ptr = ptr_c + i;
475
Gavin Howard34c4ba02018-12-11 10:25:01 -0700476 carry = 0;
477
Gavin Howard2d188a52019-02-25 14:19:08 -0700478 for (j = 0; BC_NO_SIG && j < a->len; ++j) {
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700479 unsigned int in = (uchar) ptr[j];
480 assert(in < 10);
Gavin Howard2aef61b2018-12-18 10:56:20 -0700481 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700482 carry = bc_num_addDigit(ptr + j, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600483 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700484
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700485 ptr[j] += (BcDig) carry;
486 assert(ptr[j] >= 0 && ptr[j] < 10);
487 len = BC_MAX(len, i + j + (carry != 0));
Gavin Howard63738202018-09-26 15:34:20 -0600488 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700489
Gavin Howard305249a2018-10-15 20:24:47 -0600490 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600491
Gavin Howard2d188a52019-02-25 14:19:08 -0700492 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600493 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700494
Gavin Howarde0432502019-02-26 16:23:45 -0700495 max = BC_MAX(a->len, b->len);
496 max = max < BC_NUM_DEF_SIZE ? BC_NUM_DEF_SIZE : max;
497 max2 = (max + 1) / 2;
498
Gavin Howard5bede412019-02-23 09:31:28 -0700499 size = BC_NUM_KARATSUBA_ALLOCS * sizeof(BcDig);
500 total = bc_vm_arraySize(size, max);
501 digs = dig_ptr = bc_vm_malloc(total);
502 size = max * sizeof(BcDig);
503
504 bc_num_setup(&l1, dig_ptr, max);
505 dig_ptr += size;
506 bc_num_setup(&h1, dig_ptr, max);
507 dig_ptr += size;
508 bc_num_setup(&l2, dig_ptr, max);
509 dig_ptr += size;
510 bc_num_setup(&h2, dig_ptr, max);
511 dig_ptr += size;
512 bc_num_setup(&m1, dig_ptr, max);
513 dig_ptr += size;
514 bc_num_setup(&m2, dig_ptr, max);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600515 bc_num_init(&z0, max);
516 bc_num_init(&z1, max);
517 bc_num_init(&z2, max);
Gavin Howard007afdf2019-02-23 09:45:42 -0700518 bc_num_init(&temp, bc_vm_growSize(max, max));
Gavin Howard0dfe2922018-05-22 13:57:02 -0600519
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600520 bc_num_split(a, max2, &l1, &h1);
521 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700522
Gavin Howard5bede412019-02-23 09:31:28 -0700523 assert(bc_num_addReq(&h1, &l1, 0) <= m1.cap);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600524 s = bc_num_add(&h1, &l1, &m1, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700525 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5bede412019-02-23 09:31:28 -0700526 assert(bc_num_addReq(&h2, &l2, 0) <= m2.cap);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600527 s = bc_num_add(&h2, &l2, &m2, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700528 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600529
Gavin Howard53eba8b2018-10-31 15:14:37 -0600530 s = bc_num_k(&h1, &h2, &z0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700531 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600532 s = bc_num_k(&m1, &m2, &z1);
Gavin Howardc78e7522019-02-22 13:24:25 -0700533 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600534 s = bc_num_k(&l1, &l2, &z2);
Gavin Howardc78e7522019-02-22 13:24:25 -0700535 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600536
Gavin Howard53eba8b2018-10-31 15:14:37 -0600537 s = bc_num_sub(&z1, &z0, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700538 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600539 s = bc_num_sub(&temp, &z2, &z1, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700540 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600541
Gavin Howarddbbada82019-02-22 08:51:19 -0700542 bc_num_shiftLeft(&z0, max2 * 2);
543 bc_num_shiftLeft(&z1, max2);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600544 s = bc_num_add(&z0, &z1, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700545 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600546 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600547
548err:
Gavin Howard5bede412019-02-23 09:31:28 -0700549 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600550 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600551 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600552 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600553 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600554 return s;
555}
556
Gavin Howard1cbfe242019-01-09 17:13:11 -0700557static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600558
559 BcStatus s;
560 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600561 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600562
563 scale = BC_MAX(scale, a->rdx);
564 scale = BC_MAX(scale, b->rdx);
565 scale = BC_MIN(a->rdx + b->rdx, scale);
566 maxrdx = BC_MAX(maxrdx, scale);
567
Gavin Howardbaa4f582019-01-24 13:56:35 -0700568 bc_num_createCopy(&cpa, a);
569 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600570
Gavin Howard23d8d252018-10-17 12:03:41 -0600571 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600572
Gavin Howarddbbada82019-02-22 08:51:19 -0700573 bc_num_shiftLeft(&cpa, maxrdx);
574 bc_num_shiftLeft(&cpb, maxrdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600575 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700576 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600577
Gavin Howard53eba8b2018-10-31 15:14:37 -0600578 maxrdx += scale;
Gavin Howard007afdf2019-02-23 09:45:42 -0700579 bc_num_expand(c, bc_vm_growSize(c->len, maxrdx));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600580
Gavin Howard88c25302018-10-17 13:32:23 -0600581 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600582 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600583 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600584 }
585
Gavin Howard88c25302018-10-17 13:32:23 -0600586 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600587 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600588
589err:
590 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600591 bc_num_free(&cpa);
592 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700593}
594
Gavin Howard1cbfe242019-01-09 17:13:11 -0700595static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700596
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600597 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600598 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600599 size_t len, end, i;
600 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600601 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700602
Gavin Howardddce6d42019-01-03 14:07:06 -0700603 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
604 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600605 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600606 return BC_STATUS_SUCCESS;
607 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700608 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600609 bc_num_copy(c, a);
610 bc_num_retireMul(c, scale, a->neg, b->neg);
611 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600612 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700613
Gavin Howard620a8af2019-01-24 13:54:54 -0700614 bc_num_init(&cp, bc_num_mulReq(a, b, scale));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600615 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600616 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700617
Gavin Howard890d0c02018-10-30 16:34:50 -0600618 if (len > cp.len) {
Gavin Howard007afdf2019-02-23 09:45:42 -0700619 bc_num_expand(&cp, bc_vm_growSize(len, 2));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600620 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600621 }
Gavin Howard021150b2018-03-10 15:40:42 -0700622
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600623 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600624 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600625 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700626
Gavin Howard63738202018-09-26 15:34:20 -0600627 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600628 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600629 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600630 len -= i - 1;
631 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700632
Gavin Howard007afdf2019-02-23 09:45:42 -0700633 if (cp.cap == cp.len) bc_num_expand(&cp, bc_vm_growSize(cp.len, 1));
Gavin Howardb651f1a2018-02-28 17:34:18 -0700634
Gavin Howard63738202018-09-26 15:34:20 -0600635 // We want an extra zero in front to make things simpler.
636 cp.num[cp.len++] = 0;
637 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700638
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600639 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700640
Gavin Howarda2514a02018-10-18 14:20:09 -0600641 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600642 c->rdx = cp.rdx;
643 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600644 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700645
Gavin Howard2d188a52019-02-25 14:19:08 -0700646 for (i = end - 1; BC_NO_SIG && BC_NO_ERR(!s) && i < end; --i) {
Gavin Howard5a788392019-03-28 11:01:13 -0600647
Gavin Howard63738202018-09-26 15:34:20 -0600648 n = cp.num + i;
Gavin Howardecafd4f2019-02-23 09:30:45 -0700649 q = 0;
Gavin Howard5a788392019-03-28 11:01:13 -0600650
651 while (BC_NO_SIG && BC_NO_ERR(!s) &&
652 (n[len] || bc_num_compare(n, p, len) >= 0))
653 {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700654 s = bc_num_subArrays(n, p, len);
Gavin Howard5a788392019-03-28 11:01:13 -0600655 q += 1;
656 }
657
Gavin Howard63738202018-09-26 15:34:20 -0600658 c->num[i] = q;
659 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700660
Gavin Howardf2ee6342019-04-09 17:05:20 -0600661 if (BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howarde5f11c72019-02-23 09:42:51 -0700662 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600663 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600664
Gavin Howard63738202018-09-26 15:34:20 -0600665 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700666}
667
Gavin Howard1cbfe242019-01-09 17:13:11 -0700668static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
669 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600670{
Gavin Howard63738202018-09-26 15:34:20 -0600671 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600672 BcNum temp;
673 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700674
Gavin Howardddce6d42019-01-03 14:07:06 -0700675 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
676 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700677 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600678 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600679 return BC_STATUS_SUCCESS;
680 }
Gavin Howard8b254872018-03-14 01:13:35 -0600681
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600682 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -0700683 s = bc_num_d(a, b, c, scale);
684 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -0700685 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700686
Gavin Howard1ab22d22019-01-03 13:32:17 -0700687 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600688
Gavin Howard53eba8b2018-10-31 15:14:37 -0600689 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700690 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600691 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700692 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600693
Gavin Howardddce6d42019-01-03 14:07:06 -0700694 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600695
696 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600697 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600698 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700699
700err:
Gavin Howard18441082018-10-22 10:03:30 -0600701 bc_num_free(&temp);
702 return s;
703}
704
Gavin Howard2d188a52019-02-25 14:19:08 -0700705static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
706{
Gavin Howard18441082018-10-22 10:03:30 -0600707 BcStatus s;
708 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -0700709 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -0600710
Gavin Howard007afdf2019-02-23 09:45:42 -0700711 ts = bc_vm_growSize(scale, b->rdx);
Gavin Howard798508b2019-02-21 16:45:32 -0700712 ts = BC_MAX(ts, a->rdx);
713
714 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -0600715 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600716 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600717
Gavin Howard63738202018-09-26 15:34:20 -0600718 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700719}
720
Gavin Howard1cbfe242019-01-09 17:13:11 -0700721static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700722
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600723 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600724 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700725 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600726 size_t i, powrdx, resrdx;
727 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700728
Gavin Howardecafd4f2019-02-23 09:30:45 -0700729 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700730
Gavin Howardddce6d42019-01-03 14:07:06 -0700731 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600732 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600733 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600734 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700735 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600736 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600737 return BC_STATUS_SUCCESS;
738 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700739 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600740 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600741 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600742 return s;
743 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700744
Gavin Howard63738202018-09-26 15:34:20 -0600745 neg = b->neg;
746 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600747 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700748 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600749 if (s) return s;
750
Gavin Howardbaa4f582019-01-24 13:56:35 -0700751 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700752
Gavin Howard63738202018-09-26 15:34:20 -0600753 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600754
Gavin Howard2d188a52019-02-25 14:19:08 -0700755 for (powrdx = a->rdx; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600756 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600757 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700758 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600759 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700760
Gavin Howard2d188a52019-02-25 14:19:08 -0700761 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -0700762
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600763 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700764 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600765
Gavin Howard2d188a52019-02-25 14:19:08 -0700766 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600767
Gavin Howard4d5daba2019-01-07 14:42:50 -0700768 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600769 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700770 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600771
Gavin Howard63738202018-09-26 15:34:20 -0600772 if (pow & 1) {
773 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600774 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700775 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600776 }
777 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700778
Gavin Howard2d188a52019-02-25 14:19:08 -0700779 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -0700780 if (neg) {
781 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700782 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -0700783 }
784
Gavin Howard63738202018-09-26 15:34:20 -0600785 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700786
Gavin Howard53eba8b2018-10-31 15:14:37 -0600787 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600788 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600789 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700790
Gavin Howard9b801632019-02-16 23:34:06 -0700791sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -0700792 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700793err:
Gavin Howard63738202018-09-26 15:34:20 -0600794 bc_num_free(&copy);
795 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700796}
797
Gavin Howard7bda4782018-12-28 09:53:22 -0700798#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -0700799static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
800 size_t scale)
801{
Gavin Howardac4d9122018-12-27 23:59:12 -0700802 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700803 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700804
805 BC_UNUSED(scale);
806
807 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -0700808 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -0700809
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700810 if (val < c->rdx) bc_num_truncate(c, c->rdx - val);
811 else if (val > c->rdx) bc_num_extend(c, val - c->rdx);
Gavin Howardac4d9122018-12-27 23:59:12 -0700812
813 return s;
814}
815
Gavin Howard2d188a52019-02-25 14:19:08 -0700816static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
817{
Gavin Howardac4d9122018-12-27 23:59:12 -0700818 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700819 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700820
821 BC_UNUSED(scale);
822
823 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -0700824 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -0700825
Gavin Howarddbbada82019-02-22 08:51:19 -0700826 bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -0700827
828 return s;
829}
830
Gavin Howard2d188a52019-02-25 14:19:08 -0700831static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
832 size_t scale)
833{
Gavin Howardac4d9122018-12-27 23:59:12 -0700834 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700835 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700836
837 BC_UNUSED(scale);
838
839 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -0700840 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -0700841
Gavin Howard2a366922019-01-16 10:50:20 -0700842 if (BC_NUM_ZERO(c)) return s;
843
Gavin Howarddbbada82019-02-22 08:51:19 -0700844 bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -0700845
846 return s;
847}
Gavin Howard7bda4782018-12-28 09:53:22 -0700848#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700849
Gavin Howard1cbfe242019-01-09 17:13:11 -0700850static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
851 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700852{
Gavin Howarda1c44392018-09-27 12:41:15 -0600853 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600854 BcNum num2, *ptr_a, *ptr_b;
855 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700856
Gavin Howard63738202018-09-26 15:34:20 -0600857 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700858
Gavin Howard890d0c02018-10-30 16:34:50 -0600859 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600860 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600861 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600862 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600863 }
864 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700865
Gavin Howard63738202018-09-26 15:34:20 -0600866 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600867 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600868 if (c != a) {
869 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600870 init = true;
871 }
872 }
873 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700874
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600875 if (init) bc_num_init(c, req);
876 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700877
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600878 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700879
Gavin Howardddce6d42019-01-03 14:07:06 -0700880 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -0600881 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -0600882
Gavin Howardba009802018-09-29 04:41:51 -0600883 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600884
Gavin Howardee9d01e2019-02-16 22:48:11 -0700885 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700886}
887
Gavin Howardc90ed902019-01-02 13:07:49 -0700888#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -0700889static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700890
Gavin Howardc7d655b2018-12-28 19:45:13 -0700891 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -0600892 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700893
Gavin Howard63738202018-09-26 15:34:20 -0600894 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700895
Gavin Howard63738202018-09-26 15:34:20 -0600896 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700897
Gavin Howard8a921bd2018-10-11 14:15:32 -0600898 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600899
Gavin Howard63738202018-09-26 15:34:20 -0600900 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700901
Gavin Howard63738202018-09-26 15:34:20 -0600902 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700903
Gavin Howard63738202018-09-26 15:34:20 -0600904 radix = true;
905 continue;
906 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700907
Gavin Howardc7d655b2018-12-28 19:45:13 -0700908 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -0600909 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700910
Gavin Howard63738202018-09-26 15:34:20 -0600911 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700912}
Gavin Howardc90ed902019-01-02 13:07:49 -0700913#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -0700914
Gavin Howard1cbfe242019-01-09 17:13:11 -0700915static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -0700916
917 if (isupper(c)) {
918 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -0700919 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -0700920 }
921 else c -= '0';
922
923 return (unsigned long) (uchar) c;
924}
925
Gavin Howard1cbfe242019-01-09 17:13:11 -0700926static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700927
Gavin Howard63738202018-09-26 15:34:20 -0600928 size_t len, i;
929 const char *ptr;
930 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700931
Gavin Howard63738202018-09-26 15:34:20 -0600932 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700933
Gavin Howard63738202018-09-26 15:34:20 -0600934 val += i;
935 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700936
Gavin Howard1ab22d22019-01-03 13:32:17 -0700937 if (len) {
Gavin Howard798508b2019-02-21 16:45:32 -0700938 for (i = 0; zero && i < len; ++i)
939 zero = (val[i] == '0') || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600940 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600941 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600942
Gavin Howard63738202018-09-26 15:34:20 -0600943 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700944
Gavin Howard63738202018-09-26 15:34:20 -0600945 // Explicitly test for NULL here to produce either a 0 or 1.
946 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700947
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600948 if (!zero) {
Gavin Howard8dd307e2019-01-08 23:05:19 -0700949 for (i = len - 1; i < len; ++n->len, --i) {
950
Gavin Howardc7d655b2018-12-28 19:45:13 -0700951 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -0700952
953 if (c == '.') n->len -= 1;
954 else {
955 if (isupper(c)) c = '9';
956 n->num[n->len] = c - '0';
957 }
Gavin Howardc7d655b2018-12-28 19:45:13 -0700958 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600959 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700960}
961
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700962static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
963 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -0700964{
965 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600966 BcNum temp, mult, result;
Gavin Howard1cbfe242019-01-09 17:13:11 -0700967 BcDig c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600968 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600969 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -0700970 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700971
Gavin Howard11b9afd2018-10-18 14:23:28 -0600972 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700973 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700974
Gavin Howard26d078f2019-01-24 13:58:12 -0700975 bc_num_init(&temp, BC_NUM_LONG_LOG10);
976 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -0700977
Gavin Howardc7d655b2018-12-28 19:45:13 -0700978 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -0700979
Gavin Howardc7d655b2018-12-28 19:45:13 -0700980 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -0700981
Gavin Howard53eba8b2018-10-31 15:14:37 -0600982 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700983 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700984 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600985 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700986 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600987 }
Gavin Howardede51f02018-03-02 12:30:00 -0700988
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700989 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600990
Gavin Howard63738202018-09-26 15:34:20 -0600991 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600992 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -0600993 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700994
Gavin Howard2d188a52019-02-25 14:19:08 -0700995 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -0700996 {
Gavin Howardc7d655b2018-12-28 19:45:13 -0700997 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -0700998
Gavin Howard53eba8b2018-10-31 15:14:37 -0600999 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001000 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001001
Gavin Howard705c4bc2018-12-03 15:38:04 -07001002 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001003 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001004 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001005 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001006 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001007 }
Gavin Howardede51f02018-03-02 12:30:00 -07001008
Gavin Howardf2ee6342019-04-09 17:05:20 -06001009 if (BC_SIG) {
1010 s = BC_STATUS_SIGNAL;
1011 goto err;
1012 }
1013
Gavin Howard1769abc2019-02-21 09:36:14 -07001014 // This one cannot be a divide by 0 because mult starts out at 1, then is
1015 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1016 s = bc_num_div(&result, &mult, &result, digs);
1017 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001018 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001019 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001020 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001021
Gavin Howardddce6d42019-01-03 14:07:06 -07001022 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001023 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001024 }
1025 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001026
Gavin Howardd1417682019-02-15 17:08:42 -07001027err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001028 bc_num_free(&result);
1029int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001030 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001031 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001032 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001033}
1034
Gavin Howard1cbfe242019-01-09 17:13:11 -07001035static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001036 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001037 bc_vm_putchar('\\');
1038 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001039 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001040 }
Gavin Howard80977b22018-09-06 14:29:13 -06001041}
1042
Gavin Howard40a085f2018-12-03 12:08:59 -07001043#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001044static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001045 BC_UNUSED(rdx);
1046 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001047 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001048}
1049#endif // DC_ENABLED
1050
Gavin Howard1cbfe242019-01-09 17:13:11 -07001051static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1052
Gavin Howarda84ad992018-12-03 19:11:06 -07001053 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001054
Gavin Howardab5e9632019-01-02 13:32:02 -07001055 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001056 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001057 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001058
Gavin Howardab5e9632019-01-02 13:32:02 -07001059 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001060 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001061
Gavin Howardab5e9632019-01-02 13:32:02 -07001062 for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001063 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001064 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001065 dig = n / pow;
1066 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001067 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001068 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001069}
Gavin Howardfe679f02018-02-14 15:50:09 -07001070
Gavin Howard1cbfe242019-01-09 17:13:11 -07001071static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001072
Gavin Howard3c02ed32018-12-28 18:17:15 -07001073 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001074
Gavin Howard3c02ed32018-12-28 18:17:15 -07001075 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001076 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001077 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001078 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001079 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001080
Gavin Howardab5e9632019-01-02 13:32:02 -07001081 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001082 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001083 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001084}
1085
Gavin Howard1cbfe242019-01-09 17:13:11 -07001086static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001087
Gavin Howard63738202018-09-26 15:34:20 -06001088 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001089
Gavin Howard48af52e2018-10-30 14:47:38 -06001090 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001091 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001092
Gavin Howard48af52e2018-10-30 14:47:38 -06001093 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001094 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001095}
1096
Gavin Howard7ad5a662019-02-19 14:40:46 -07001097#if BC_ENABLE_EXTRA_MATH
1098static void bc_num_printExponent(const BcNum *restrict n, bool eng) {
1099
1100 bool neg = (n->len <= n->rdx);
1101 BcNum temp, exp;
1102 size_t places, mod;
1103 BcDig digs[BC_NUM_LONG_LOG10];
1104
1105 bc_num_createCopy(&temp, n);
1106
1107 if (neg) {
1108 places = n->rdx - bc_num_len(n) + 1;
1109 mod = places % 3;
1110 if (eng && mod != 0) places += 3 - mod;
Gavin Howarddbbada82019-02-22 08:51:19 -07001111 bc_num_shiftLeft(&temp, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001112 }
1113 else {
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001114 places = bc_num_int(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001115 mod = places % 3;
1116 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howarddbbada82019-02-22 08:51:19 -07001117 bc_num_shiftRight(&temp, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001118 }
1119
1120 bc_num_printDecimal(&temp);
1121
1122 bc_num_printNewline();
1123 bc_vm_putchar('e');
1124
1125 if (!places) {
1126 bc_num_printHex(0, 1, false);
1127 goto exit;
1128 }
1129
1130 if (neg) {
1131 bc_num_printNewline();
1132 bc_vm_putchar('-');
1133 }
1134
1135 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1136 bc_num_ulong2num(&exp, (unsigned long) places);
1137
1138 bc_num_printDecimal(&exp);
1139
1140exit:
1141 bc_num_free(&temp);
1142}
1143#endif // BC_ENABLE_EXTRA_MATH
1144
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001145static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1146 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001147{
Gavin Howard63738202018-09-26 15:34:20 -06001148 BcStatus s;
1149 BcVec stack;
1150 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001151 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001152 size_t i;
1153 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001154
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001155 assert(!BC_NUM_ZERO(base));
1156
Gavin Howardddce6d42019-01-03 14:07:06 -07001157 if (BC_NUM_ZERO(n)) {
1158 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001159 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001160 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001161
Gavin Howardd3929832018-12-24 15:13:15 -07001162 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001163 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001164 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001165 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001166 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001167 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001168
Gavin Howard63738202018-09-26 15:34:20 -06001169 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001170 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001171 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001172
Gavin Howard2d188a52019-02-25 14:19:08 -07001173 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001174
1175 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001176 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001177 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001178
1179 // Will never fail (except for signals) because digit is
1180 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001181 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001182 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001183
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001184 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001185 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001186
Gavin Howard2d188a52019-02-25 14:19:08 -07001187 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001188
Gavin Howard2d188a52019-02-25 14:19:08 -07001189 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001190 ptr = bc_vec_item_rev(&stack, i);
1191 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001192 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001193 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001194
Gavin Howard2d188a52019-02-25 14:19:08 -07001195 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001196 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001197
Gavin Howard2d188a52019-02-25 14:19:08 -07001198 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001199
Gavin Howard53eba8b2018-10-31 15:14:37 -06001200 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001201 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001202
1203 // Will never fail (except for signals) because fracp is
1204 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001205 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001206 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001207
Gavin Howard705c4bc2018-12-03 15:38:04 -07001208 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001209 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001210 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001211
Gavin Howardddce6d42019-01-03 14:07:06 -07001212 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001213 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001214 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001215 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001216
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001217sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001218 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001219err:
Gavin Howard63738202018-09-26 15:34:20 -06001220 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001221 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001222 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001223 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001224 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001225 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001226}
1227
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001228static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1229 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001230{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001231 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001232 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001233 BcNumDigitOp print;
1234 bool neg = n->neg;
1235
Gavin Howard48af52e2018-10-30 14:47:38 -06001236 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001237 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001238
1239 n->neg = false;
1240
Gavin Howardbe4e8222019-01-03 13:34:16 -07001241 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001242 width = 1;
1243 print = bc_num_printHex;
1244 }
1245 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001246 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001247 print = bc_num_printDigits;
1248 }
1249
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001250 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001251 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001252
1253 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001254}
1255
Gavin Howard40a085f2018-12-03 12:08:59 -07001256#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001257BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1258 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001259}
1260#endif // DC_ENABLED
1261
Gavin Howard3c02ed32018-12-28 18:17:15 -07001262void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001263 assert(n);
1264 n->num = num;
1265 n->cap = cap;
1266 n->rdx = n->len = 0;
1267 n->neg = false;
1268}
1269
Gavin Howard3c02ed32018-12-28 18:17:15 -07001270void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001271 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001272 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001273 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001274}
1275
Gavin Howarded392aa2018-02-27 13:09:26 -07001276void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001277 assert(num);
1278 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001279}
1280
Gavin Howard3c02ed32018-12-28 18:17:15 -07001281void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001282 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001283 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001284 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001285 d->len = s->len;
1286 d->neg = s->neg;
1287 d->rdx = s->rdx;
1288 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001289}
1290
Gavin Howardbaa4f582019-01-24 13:56:35 -07001291void bc_num_createCopy(BcNum *d, const BcNum *s) {
1292 bc_num_init(d, s->len);
1293 bc_num_copy(d, s);
1294}
1295
1296void bc_num_createFromUlong(BcNum *n, unsigned long val) {
1297 bc_num_init(n, BC_NUM_LONG_LOG10);
1298 bc_num_ulong2num(n, val);
1299}
1300
Gavin Howard7ad5a662019-02-19 14:40:46 -07001301size_t bc_num_scale(const BcNum *restrict n) {
1302 return n->rdx;
1303}
1304
1305size_t bc_num_len(const BcNum *restrict n) {
1306
1307 size_t i, len = n->len;
1308
1309 if (n->rdx != n->len) return len;
1310 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1311
1312 return len;
1313}
1314
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001315BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1316 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001317{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001318 BcStatus s = BC_STATUS_SUCCESS;
1319
Gavin Howard63738202018-09-26 15:34:20 -06001320 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001321 assert(BC_ENABLE_EXTRA_MATH ||
1322 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001323 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001324
Gavin Howardd746e802018-12-28 22:06:00 -07001325 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howardc7d655b2018-12-28 19:45:13 -07001326 else if (base_t == 10) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001327 else s = bc_num_parseBase(n, val, base, base_t);
1328
1329 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001330}
1331
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001332BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1333 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001334{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001335 BcStatus s = BC_STATUS_SUCCESS;
1336
Gavin Howardab5e9632019-01-02 13:32:02 -07001337 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001338 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001339
Gavin Howardab5e9632019-01-02 13:32:02 -07001340 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001341
Gavin Howardddce6d42019-01-03 14:07:06 -07001342 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howardab5e9632019-01-02 13:32:02 -07001343 else if (base_t == 10) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001344#if BC_ENABLE_EXTRA_MATH
1345 else if (base_t == 0 || base_t == 1) bc_num_printExponent(n, base_t != 0);
1346#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001347 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001348
Gavin Howarde5f11c72019-02-23 09:42:51 -07001349 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001350 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001351 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001352 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001353
1354 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001355}
1356
Gavin Howard3c02ed32018-12-28 18:17:15 -07001357BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001358
Gavin Howard63738202018-09-26 15:34:20 -06001359 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001360 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001361
Gavin Howard63738202018-09-26 15:34:20 -06001362 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001363
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001364 *result = 0;
1365
Gavin Howardecafd4f2019-02-23 09:30:45 -07001366 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001367
Gavin Howard06b6e862019-02-15 11:15:31 -07001368 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001369
1370 unsigned long prev = r * 10;
1371
1372 if (BC_ERR(prev == SIZE_MAX || prev / 10 != r))
1373 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1374
1375 r = prev + ((uchar) n->num[--i]);
1376
1377 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001378 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001379 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001380
Gavin Howard7b557da2018-12-21 10:25:32 -07001381 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001382
Gavin Howard63738202018-09-26 15:34:20 -06001383 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001384}
1385
Gavin Howard3c02ed32018-12-28 18:17:15 -07001386void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001387
Gavin Howard8a921bd2018-10-11 14:15:32 -06001388 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001389 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001390
Gavin Howard63738202018-09-26 15:34:20 -06001391 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001392
Gavin Howard63738202018-09-26 15:34:20 -06001393 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001394
Gavin Howard1ab22d22019-01-03 13:32:17 -07001395 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001396
Gavin Howard798508b2019-02-21 16:45:32 -07001397 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howardec82bdc2018-10-08 23:50:57 -06001398 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001399}
1400
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001401size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001402
1403 size_t aint, bint, ardx, brdx;
1404
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001405 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001406
1407 ardx = a->rdx;
1408 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001409 aint = bc_num_int(a);
1410 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001411 ardx = BC_MAX(ardx, brdx);
1412 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001413
Gavin Howard2d188a52019-02-25 14:19:08 -07001414 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001415}
1416
1417size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001418 size_t max, rdx;
1419 rdx = bc_vm_growSize(a->rdx, b->rdx);
1420 max = bc_vm_growSize(BC_MAX(scale, rdx), 1);
1421 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1422 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001423}
1424
1425size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1426 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001427 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001428}
1429
1430#if BC_ENABLE_EXTRA_MATH
1431size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1432 BC_UNUSED(b);
1433 BC_UNUSED(scale);
1434 return BC_NUM_SHREQ(a);
1435}
1436#endif // BC_ENABLE_EXTRA_MATH
1437
Gavin Howard12fe7812018-09-29 03:45:18 -06001438BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001439 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001440 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001441 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001442}
1443
Gavin Howard12fe7812018-09-29 03:45:18 -06001444BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001445 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001446 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001447 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001448}
1449
Gavin Howard12fe7812018-09-29 03:45:18 -06001450BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001451 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001452}
1453
Gavin Howard12fe7812018-09-29 03:45:18 -06001454BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001455 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001456}
1457
Gavin Howard54b946a2018-10-23 12:06:57 -06001458BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001459 size_t req = bc_num_mulReq(a, b, scale);
1460 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001461}
1462
Gavin Howard12fe7812018-09-29 03:45:18 -06001463BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001464 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001465}
1466
Gavin Howard7d8f0382018-12-27 16:15:13 -07001467#if BC_ENABLE_EXTRA_MATH
1468BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001469 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001470}
1471
1472BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001473 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001474}
1475
1476BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001477 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001478}
1479#endif // BC_ENABLE_EXTRA_MATH
1480
Gavin Howard3c02ed32018-12-28 18:17:15 -07001481BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001482
Gavin Howard1cbfe242019-01-09 17:13:11 -07001483 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001484 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard2d188a52019-02-25 14:19:08 -07001485 size_t pow, len, req, digs, digs1, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001486 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001487 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001488
Gavin Howard34edd0a2018-10-25 15:51:05 -06001489 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001490
Gavin Howardecafd4f2019-02-23 09:30:45 -07001491 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07001492
Gavin Howard007afdf2019-02-23 09:45:42 -07001493 len = bc_vm_growSize(bc_num_int(a), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001494 req = bc_vm_growSize(BC_MAX(scale, a->rdx), len >> 1);
1495 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001496
Gavin Howardddce6d42019-01-03 14:07:06 -07001497 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001498 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001499 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001500 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001501 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001502 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001503 bc_num_extend(b, scale);
1504 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001505 }
Gavin Howard954276b2018-05-16 01:43:58 -06001506
Gavin Howard890d0c02018-10-30 16:34:50 -06001507 scale = BC_MAX(scale, a->rdx) + 1;
Gavin Howard007afdf2019-02-23 09:45:42 -07001508 len = bc_vm_growSize(a->len, scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001509
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001510 bc_num_init(&num1, len);
1511 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001512 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001513
Gavin Howard63738202018-09-26 15:34:20 -06001514 bc_num_one(&half);
1515 half.num[0] = 5;
1516 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001517
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001518 bc_num_init(&f, len);
1519 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001520
Gavin Howard63738202018-09-26 15:34:20 -06001521 x0 = &num1;
1522 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001523
Gavin Howard63738202018-09-26 15:34:20 -06001524 bc_num_one(x0);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001525 pow = bc_num_int(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001526
Gavin Howard53eba8b2018-10-31 15:14:37 -06001527 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001528
Gavin Howardc39fd492018-10-04 10:07:03 -06001529 if (pow & 1) x0->num[0] = 2;
1530 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001531
Gavin Howardc39fd492018-10-04 10:07:03 -06001532 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001533
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001534 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001535
Gavin Howard63738202018-09-26 15:34:20 -06001536 // Make sure to move the radix back.
1537 x0->rdx -= pow;
1538 }
Gavin Howard954276b2018-05-16 01:43:58 -06001539
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001540 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001541 resrdx = scale + 2;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001542 len = bc_num_int(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001543
Gavin Howard2d188a52019-02-25 14:19:08 -07001544 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001545
Gavin Howard1769abc2019-02-21 09:36:14 -07001546 assert(!BC_NUM_ZERO(x0));
1547
Gavin Howard53eba8b2018-10-31 15:14:37 -06001548 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001549 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001550 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001551 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001552 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001553 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001554 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001555
Gavin Howard53eba8b2018-10-31 15:14:37 -06001556 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06001557 if (cmp == BC_NUM_SSIZE_MIN) {
1558 s = BC_STATUS_SIGNAL;
1559 break;
1560 }
1561
Gavin Howard53eba8b2018-10-31 15:14:37 -06001562 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001563
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001564 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001565 else times = 0;
1566
1567 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001568
1569 cmp2 = cmp1;
1570 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001571 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001572
Gavin Howard63738202018-09-26 15:34:20 -06001573 temp = x0;
1574 x0 = x1;
1575 x1 = temp;
1576 }
Gavin Howard954276b2018-05-16 01:43:58 -06001577
Gavin Howard2d188a52019-02-25 14:19:08 -07001578 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07001579 s = BC_STATUS_SIGNAL;
1580 goto err;
1581 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001582
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001583 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001584 scale -= 1;
1585 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001586
1587err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06001588 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06001589 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001590 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001591 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001592 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001593 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001594 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001595 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001596}
Gavin Howardba009802018-09-29 04:41:51 -06001597
Gavin Howard2cb39612018-10-22 08:46:48 -06001598BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1599
1600 BcStatus s;
1601 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001602 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07001603 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001604
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001605 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001606
Gavin Howard890d0c02018-10-30 16:34:50 -06001607 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001608 memcpy(&num2, c, sizeof(BcNum));
1609 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001610 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001611 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001612 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001613 else {
1614 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001615 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001616 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001617
Gavin Howard54b946a2018-10-23 12:06:57 -06001618 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001619
Gavin Howardddce6d42019-01-03 14:07:06 -07001620 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001621 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001622 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001623 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001624
1625 if (init) bc_num_free(&num2);
1626
1627 return s;
1628}
1629
Gavin Howard40a085f2018-12-03 12:08:59 -07001630#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001631BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001632
1633 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001634 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001635 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001636
Gavin Howard34edd0a2018-10-25 15:51:05 -06001637 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001638
Gavin Howardecafd4f2019-02-23 09:30:45 -07001639 if (BC_ERR(BC_NUM_ZERO(c)))
1640 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1641 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1642 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07001643 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001644
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001645 bc_num_expand(d, c->len);
1646 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001647 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001648 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001649
1650 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001651 two.num[0] = 2;
1652 bc_num_one(d);
1653
Gavin Howard1769abc2019-02-21 09:36:14 -07001654 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001655 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001656 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001657 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07001658 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001659
Gavin Howard2d188a52019-02-25 14:19:08 -07001660 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001661
Gavin Howard1769abc2019-02-21 09:36:14 -07001662 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001663 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001664 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001665
Gavin Howarda83a5e52019-02-21 16:51:06 -07001666 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001667 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001668 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001669
1670 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001671 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001672 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001673 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001674 }
1675
Gavin Howard53eba8b2018-10-31 15:14:37 -06001676 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001677 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001678
1679 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001680 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001681 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001682 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001683 }
1684
Gavin Howard2d188a52019-02-25 14:19:08 -07001685 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07001686
Gavin Howardba009802018-09-29 04:41:51 -06001687err:
Gavin Howardba009802018-09-29 04:41:51 -06001688 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07001689rem_err:
1690 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001691 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001692 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001693 return s;
1694}
Gavin Howarde6e84762018-10-03 11:46:34 -06001695#endif // DC_ENABLED