blob: af0ea7b4914d38ff3dbd470f102d83a0de9f6451 [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 Howard49ea7b62019-04-23 14:46:26 -060048static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
49
Gavin Howard56ebcf72019-02-21 16:59:48 -070050static ssize_t bc_num_neg(size_t n, bool neg) {
51 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
52}
53
Gavin Howardd2cf06a2019-02-21 17:03:24 -070054ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -070055 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -070056}
57
Gavin Howard7b7d2f32019-02-21 16:46:47 -070058static size_t bc_num_int(const BcNum *n) {
59 return n->len ? n->len - n->rdx : 0;
60}
61
Gavin Howarda83a5e52019-02-21 16:51:06 -070062static bool bc_num_isOne(const BcNum *n) {
63 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
64}
65
Gavin Howard798508b2019-02-21 16:45:32 -070066static void bc_num_expand(BcNum *restrict n, size_t req) {
67 assert(n);
68 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
69 if (req > n->cap) {
70 n->num = bc_vm_realloc(n->num, req);
71 n->cap = req;
72 }
73}
74
Gavin Howard1cbfe242019-01-09 17:13:11 -070075static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060076 assert(n);
Gavin Howard97102b12018-11-01 23:37:31 -060077 n->len = 0;
78 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060079 n->rdx = scale;
80}
81
Gavin Howard1cbfe242019-01-09 17:13:11 -070082static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060083 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060084}
85
Gavin Howard3c02ed32018-12-28 18:17:15 -070086void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060087 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060088 n->len = 1;
89 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060090}
91
Gavin Howard3c02ed32018-12-28 18:17:15 -070092void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060093 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060094 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060095 n->len = 2;
96 n->num[0] = 0;
97 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060098}
99
Gavin Howard1cbfe242019-01-09 17:13:11 -0700100static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -0700101 size_t len;
Gavin Howard530e3072019-04-23 16:23:36 -0600102 for (len = 1; i; i /= BC_BASE, ++len);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700103 return len;
104}
105
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700106static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
107 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700108{
Gavin Howard63738202018-09-26 15:34:20 -0600109 size_t i, j;
Gavin Howard2d188a52019-02-25 14:19:08 -0700110 for (i = 0; BC_NO_SIG && i < len; ++i) {
111 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard530e3072019-04-23 16:23:36 -0600112 a[i + j++] += BC_BASE;
Gavin Howard54b946a2018-10-23 12:06:57 -0600113 a[i + j] -= 1;
Gavin Howard530e3072019-04-23 16:23:36 -0600114 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE);
Gavin Howard63738202018-09-26 15:34:20 -0600115 }
116 }
Gavin Howard2d188a52019-02-25 14:19:08 -0700117 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600118}
119
Gavin Howard2d188a52019-02-25 14:19:08 -0700120static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
121 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700122{
Gavin Howard63738202018-09-26 15:34:20 -0600123 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -0600124 int c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700125 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600126 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600127}
128
Gavin Howard3c02ed32018-12-28 18:17:15 -0700129ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700130
Gavin Howard63738202018-09-26 15:34:20 -0600131 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600132 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600133 bool a_max, neg = false;
134 ssize_t cmp;
135
136 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700137
Gavin Howard8a921bd2018-10-11 14:15:32 -0600138 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700139 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700140 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600141 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600142 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600143 else return -1;
144 }
145 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700146
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700147 a_int = bc_num_int(a);
148 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600149 a_int -= b_int;
150 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700151
Gavin Howard1ab22d22019-01-03 13:32:17 -0700152 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700153
Gavin Howard890d0c02018-10-30 16:34:50 -0600154 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600155 min = b->rdx;
156 diff = a->rdx - b->rdx;
157 max_num = a->num + diff;
158 min_num = b->num;
159 }
160 else {
161 min = a->rdx;
162 diff = b->rdx - a->rdx;
163 max_num = b->num + diff;
164 min_num = a->num;
165 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700166
Gavin Howard890d0c02018-10-30 16:34:50 -0600167 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600168 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700169 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700170
Gavin Howard2d188a52019-02-25 14:19:08 -0700171 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700172 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600173 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700174
Gavin Howardf2ee6342019-04-09 17:05:20 -0600175 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700176}
177
Gavin Howard2a366922019-01-16 10:50:20 -0700178static void bc_num_clean(BcNum *restrict n) {
179 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
180 if (BC_NUM_ZERO(n)) n->neg = false;
181 else if (n->len < n->rdx) n->len = n->rdx;
182}
183
Gavin Howard3c02ed32018-12-28 18:17:15 -0700184void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700185
Gavin Howardddce6d42019-01-03 14:07:06 -0700186 assert(places <= n->rdx && (BC_NUM_ZERO(n) || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700187
Gavin Howard1ab22d22019-01-03 13:32:17 -0700188 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700189
Gavin Howard63738202018-09-26 15:34:20 -0600190 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700191
Gavin Howardddce6d42019-01-03 14:07:06 -0700192 if (BC_NUM_NONZERO(n)) {
Gavin Howard955da852018-10-23 11:08:20 -0600193 n->len -= places;
194 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
Gavin Howard2a366922019-01-16 10:50:20 -0700195 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600196 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700197}
198
Gavin Howard1cbfe242019-01-09 17:13:11 -0700199static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700200
Gavin Howard007afdf2019-02-23 09:45:42 -0700201 size_t len = bc_vm_growSize(n->len, places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700202
Gavin Howard1ab22d22019-01-03 13:32:17 -0700203 if (!places) return;
Gavin Howard1ab22d22019-01-03 13:32:17 -0700204 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700205
Gavin Howard1ab22d22019-01-03 13:32:17 -0700206 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
207 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700208
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700209 if (n->len) n->len += places;
210
Gavin Howard1ab22d22019-01-03 13:32:17 -0700211 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700212}
213
Gavin Howard2d188a52019-02-25 14:19:08 -0700214static void bc_num_retireMul(BcNum *restrict n, size_t scale,
215 bool neg1, bool neg2)
216{
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600217 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600218 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600219
Gavin Howard305249a2018-10-15 20:24:47 -0600220 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700221 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600222}
223
Gavin Howard2d188a52019-02-25 14:19:08 -0700224static void bc_num_split(const BcNum *restrict n, size_t idx,
225 BcNum *restrict a, BcNum *restrict b)
226{
Gavin Howard305249a2018-10-15 20:24:47 -0600227 if (idx < n->len) {
228
Gavin Howard305249a2018-10-15 20:24:47 -0600229 b->len = n->len - idx;
230 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600231 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600232
233 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
234 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard50c80022019-01-03 15:14:33 -0700235
236 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600237 }
Gavin Howard50c80022019-01-03 15:14:33 -0700238 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600239
240 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600241}
242
Gavin Howard14913fc2019-04-23 14:36:03 -0600243static size_t bc_num_shiftZero(BcNum *restrict n) {
244
245 size_t i;
246
247 assert(!n->rdx || BC_NUM_ZERO(n));
248
249 for (i = 0; i < n->len && !n->num[i]; ++i);
250
251 n->len -= i;
252 n->num += i;
253
254 return i;
255}
256
257static void bc_num_unshiftZero(BcNum *restrict n, size_t places) {
258 n->len += places;
259 n->num -= places;
260}
261
Gavin Howarddbbada82019-02-22 08:51:19 -0700262static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600263
Gavin Howardd8b004b2019-02-16 22:35:08 -0700264 if (!places || BC_NUM_ZERO(n)) return;
Gavin Howard305249a2018-10-15 20:24:47 -0600265
266 if (n->rdx >= places) n->rdx -= places;
267 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600268 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600269 n->rdx = 0;
270 }
271
272 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600273}
274
Gavin Howarddbbada82019-02-22 08:51:19 -0700275static void bc_num_shiftRight(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700276
Gavin Howard14913fc2019-04-23 14:36:03 -0600277 size_t len;
278
279 if (!places) return;
280 if (BC_NUM_ZERO(n)) {
281 n->rdx += places;
282 bc_num_expand(n, n->rdx);
283 return;
284 }
285
286 len = bc_vm_growSize(n->rdx, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700287
288 if (len > n->len) {
289
290 if (len > n->cap) bc_num_expand(n, len);
291
292 memset(n->num + n->len, 0, len - n->len);
293 n->len = len;
294 }
295
296 n->rdx += places;
297
298 assert(n->rdx <= n->len && n->len <= n->cap);
299}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700300
Gavin Howard1cbfe242019-01-09 17:13:11 -0700301static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600302
Gavin Howard63738202018-09-26 15:34:20 -0600303 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600304 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600305
Gavin Howard1769abc2019-02-21 09:36:14 -0700306 assert(!BC_NUM_ZERO(a));
307
Gavin Howardd32d7df2018-10-15 08:42:25 -0600308 one.cap = 2;
309 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600310 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600311
Gavin Howardd32d7df2018-10-15 08:42:25 -0600312 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600313}
314
Gavin Howard7bda4782018-12-28 09:53:22 -0700315#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700316static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
317 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700318{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700319 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700320 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700321 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700322}
Gavin Howard7bda4782018-12-28 09:53:22 -0700323#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700324
Gavin Howard2d188a52019-02-25 14:19:08 -0700325static unsigned int bc_num_addDigit(BcDig *restrict num, unsigned int d,
326 unsigned int c)
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700327{
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700328 d += c;
Gavin Howard530e3072019-04-23 16:23:36 -0600329 *num = (BcDig) (d % BC_BASE);
330 assert(*num >= 0 && *num < BC_BASE);
331 assert((d / BC_BASE) < BC_BASE);
332 return d / BC_BASE;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700333}
334
Gavin Howard1cbfe242019-01-09 17:13:11 -0700335static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700336
Gavin Howard8a921bd2018-10-11 14:15:32 -0600337 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600338 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700339 unsigned int carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700340
Gavin Howard63738202018-09-26 15:34:20 -0600341 // Because this function doesn't need to use scale (per the bc spec),
342 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600343
Gavin Howardddce6d42019-01-03 14:07:06 -0700344 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600345 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700346 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600347 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600348 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700349 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600350 bc_num_copy(c, a);
351 return BC_STATUS_SUCCESS;
352 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600353
Gavin Howard63738202018-09-26 15:34:20 -0600354 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600355 c->rdx = BC_MAX(a->rdx, b->rdx);
356 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700357
Gavin Howard63738202018-09-26 15:34:20 -0600358 if (a->rdx > b->rdx) {
359 diff = a->rdx - b->rdx;
360 ptr = a->num;
361 ptr_a = a->num + diff;
362 ptr_b = b->num;
363 }
364 else {
365 diff = b->rdx - a->rdx;
366 ptr = b->num;
367 ptr_a = a->num;
368 ptr_b = b->num + diff;
369 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700370
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700371 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700372
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700373 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600374 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700375 a_int = bc_num_int(a);
376 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700377
Gavin Howard890d0c02018-10-30 16:34:50 -0600378 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600379 min_int = b_int;
380 max = a_int;
381 ptr = ptr_a;
382 }
383 else {
384 min_int = a_int;
385 max = b_int;
386 ptr = ptr_b;
387 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700388
Gavin Howard2d188a52019-02-25 14:19:08 -0700389 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700390 unsigned int in = (unsigned int) (ptr_a[i] + ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700391 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600392 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700393
Gavin Howard2d188a52019-02-25 14:19:08 -0700394 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700395 carry = bc_num_addDigit(ptr_c + i, (unsigned int) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700396
397 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700398
Gavin Howard1ab22d22019-01-03 13:32:17 -0700399 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600400
Gavin Howard2d188a52019-02-25 14:19:08 -0700401 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700402}
403
Gavin Howard1cbfe242019-01-09 17:13:11 -0700404static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700405
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700406 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600407 ssize_t cmp;
408 BcNum *minuend, *subtrahend;
409 size_t start;
410 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700411
Gavin Howard63738202018-09-26 15:34:20 -0600412 // Because this function doesn't need to use scale (per the bc spec),
413 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700414
Gavin Howardddce6d42019-01-03 14:07:06 -0700415 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600416 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700417 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600418 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600419 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700420 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600421 bc_num_copy(c, a);
422 return BC_STATUS_SUCCESS;
423 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700424
Gavin Howard63738202018-09-26 15:34:20 -0600425 aneg = a->neg;
426 bneg = b->neg;
427 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700428
Gavin Howard63738202018-09-26 15:34:20 -0600429 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600430 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700431
Gavin Howard63738202018-09-26 15:34:20 -0600432 a->neg = aneg;
433 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700434
Gavin Howard1ab22d22019-01-03 13:32:17 -0700435 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600436 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600437 return BC_STATUS_SUCCESS;
438 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700439
440 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600441 neg = a->neg;
442 minuend = a;
443 subtrahend = b;
444 }
445 else {
446 neg = b->neg;
447 if (sub) neg = !neg;
448 minuend = b;
449 subtrahend = a;
450 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700451
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600452 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600453 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700454
Gavin Howard63738202018-09-26 15:34:20 -0600455 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600456 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600457 start = 0;
458 }
459 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700460
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700461 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700462
Gavin Howard63738202018-09-26 15:34:20 -0600463 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700464
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700465 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700466}
467
Gavin Howarddc4e3712019-04-23 14:56:29 -0600468static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
469{
Gavin Howard5083aec2019-04-23 18:03:46 -0600470 size_t i, sum = 0, alen = a->len, blen = b->len, clen;
471 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600472
Gavin Howardb538d802019-04-23 16:50:55 -0600473 assert(!a->rdx && !b->rdx);
474
Gavin Howard5083aec2019-04-23 18:03:46 -0600475 clen = bc_vm_growSize(alen, blen);
476 bc_num_expand(c, clen + 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600477
478 ptr_c = c->num;
Gavin Howard5083aec2019-04-23 18:03:46 -0600479 memset(ptr_c, 0, sizeof(BcDig) * c->cap);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600480
Gavin Howard5083aec2019-04-23 18:03:46 -0600481 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600482
Gavin Howard5083aec2019-04-23 18:03:46 -0600483 ssize_t sidx = (ssize_t) (i - blen + 1);
484 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600485
Gavin Howard5083aec2019-04-23 18:03:46 -0600486 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k)
487 sum += ((size_t) ptr_a[j]) * ((size_t) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600488
Gavin Howard5083aec2019-04-23 18:03:46 -0600489 ptr_c[i] = (BcDig) (sum % BC_BASE);
490 assert(ptr_c[i] < BC_BASE);
491 sum /= BC_BASE;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600492 }
493
Gavin Howard5083aec2019-04-23 18:03:46 -0600494 if (sum) {
495 assert(sum < BC_BASE);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600496 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600497 clen += 1;
498 }
499
500 c->len = clen;
501
Gavin Howarddc4e3712019-04-23 14:56:29 -0600502 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
503}
504
Gavin Howard14913fc2019-04-23 14:36:03 -0600505static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600506
Gavin Howard9b801632019-02-16 23:34:06 -0700507 BcStatus s;
Gavin Howarde0432502019-02-26 16:23:45 -0700508 size_t max, max2, total, size;
Gavin Howard305249a2018-10-15 20:24:47 -0600509 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700510 BcDig *digs, *dig_ptr;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700511 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600512
Gavin Howard14913fc2019-04-23 14:36:03 -0600513 assert(BC_NUM_ZERO(c));
514
Gavin Howardddce6d42019-01-03 14:07:06 -0700515 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600516 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700517 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600518 bc_num_zero(c);
519 return BC_STATUS_SUCCESS;
520 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700521 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600522 bc_num_copy(c, aone ? b : a);
523 return BC_STATUS_SUCCESS;
524 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700525
Gavin Howardb5093d72018-10-17 12:13:17 -0600526 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
527 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600528 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600529 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600530 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700531
Gavin Howarde0432502019-02-26 16:23:45 -0700532 max = BC_MAX(a->len, b->len);
533 max = max < BC_NUM_DEF_SIZE ? BC_NUM_DEF_SIZE : max;
534 max2 = (max + 1) / 2;
535
Gavin Howard5bede412019-02-23 09:31:28 -0700536 size = BC_NUM_KARATSUBA_ALLOCS * sizeof(BcDig);
537 total = bc_vm_arraySize(size, max);
538 digs = dig_ptr = bc_vm_malloc(total);
539 size = max * sizeof(BcDig);
540
541 bc_num_setup(&l1, dig_ptr, max);
542 dig_ptr += size;
543 bc_num_setup(&h1, dig_ptr, max);
544 dig_ptr += size;
545 bc_num_setup(&l2, dig_ptr, max);
546 dig_ptr += size;
547 bc_num_setup(&h2, dig_ptr, max);
548 dig_ptr += size;
549 bc_num_setup(&m1, dig_ptr, max);
550 dig_ptr += size;
551 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600552 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600553 bc_num_init(&z0, max);
554 bc_num_init(&z1, max);
555 bc_num_init(&z2, max);
Gavin Howard57223022019-04-24 08:37:14 -0600556 bc_num_init(&temp, bc_vm_growSize(max, max) + 1);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600557
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600558 bc_num_split(a, max2, &l1, &h1);
559 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700560
Gavin Howard5bede412019-02-23 09:31:28 -0700561 assert(bc_num_addReq(&h1, &l1, 0) <= m1.cap);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600562 s = bc_num_add(&h1, &l1, &m1, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700563 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5bede412019-02-23 09:31:28 -0700564 assert(bc_num_addReq(&h2, &l2, 0) <= m2.cap);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600565 s = bc_num_add(&h2, &l2, &m2, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700566 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600567
Gavin Howard489cd162019-04-22 15:03:29 -0600568 s = bc_num_m(&h1, &h2, &z0, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700569 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard489cd162019-04-22 15:03:29 -0600570 s = bc_num_m(&m1, &m2, &z1, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700571 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard489cd162019-04-22 15:03:29 -0600572 s = bc_num_m(&l1, &l2, &z2, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700573 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600574
Gavin Howard53eba8b2018-10-31 15:14:37 -0600575 s = bc_num_sub(&z1, &z0, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700576 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600577 s = bc_num_sub(&temp, &z2, &z1, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700578 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600579
Gavin Howarddbbada82019-02-22 08:51:19 -0700580 bc_num_shiftLeft(&z0, max2 * 2);
581 bc_num_shiftLeft(&z1, max2);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600582 s = bc_num_add(&z0, &z1, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -0700583 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600584 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600585
586err:
Gavin Howard5bede412019-02-23 09:31:28 -0700587 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600588 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600589 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600590 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600591 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600592 return s;
593}
594
Gavin Howard1cbfe242019-01-09 17:13:11 -0700595static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600596
597 BcStatus s;
598 BcNum cpa, cpb;
Gavin Howard14913fc2019-04-23 14:36:03 -0600599 size_t ardx = a->rdx, brdx = b->rdx, azero, bzero, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600600
Gavin Howard14913fc2019-04-23 14:36:03 -0600601 bc_num_setToZero(c, 0);
602
603 scale = BC_MAX(scale, ardx);
604 scale = BC_MAX(scale, brdx);
605 rscale = ardx + brdx;
606 scale = BC_MIN(rscale, scale);
Gavin Howard305249a2018-10-15 20:24:47 -0600607
Gavin Howardbaa4f582019-01-24 13:56:35 -0700608 bc_num_createCopy(&cpa, a);
609 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600610
Gavin Howard23d8d252018-10-17 12:03:41 -0600611 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600612
Gavin Howard14913fc2019-04-23 14:36:03 -0600613 bc_num_shiftLeft(&cpa, ardx);
614 bc_num_clean(&cpa);
615 azero = bc_num_shiftZero(&cpa);
616 bc_num_shiftLeft(&cpb, brdx);
617 bzero = bc_num_shiftZero(&cpb);
618 bc_num_clean(&cpb);
619
Gavin Howard489cd162019-04-22 15:03:29 -0600620 s = bc_num_k(&cpa, &cpb, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700621 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600622
Gavin Howard14913fc2019-04-23 14:36:03 -0600623 zero = bc_vm_growSize(azero, bzero);
624 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600625
Gavin Howard14913fc2019-04-23 14:36:03 -0600626 bc_num_expand(c, len);
627 bc_num_shiftLeft(c, len - c->len);
628 bc_num_shiftRight(c, rscale);
Gavin Howard305249a2018-10-15 20:24:47 -0600629
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600630 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600631
632err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600633 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600634 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600635 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600636 bc_num_free(&cpa);
637 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700638}
639
Gavin Howard1cbfe242019-01-09 17:13:11 -0700640static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700641
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600642 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600643 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600644 size_t len, end, i;
645 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600646 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700647
Gavin Howardddce6d42019-01-03 14:07:06 -0700648 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
649 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600650 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600651 return BC_STATUS_SUCCESS;
652 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700653 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600654 bc_num_copy(c, a);
655 bc_num_retireMul(c, scale, a->neg, b->neg);
656 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600657 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700658
Gavin Howard620a8af2019-01-24 13:54:54 -0700659 bc_num_init(&cp, bc_num_mulReq(a, b, scale));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600660 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600661 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700662
Gavin Howard890d0c02018-10-30 16:34:50 -0600663 if (len > cp.len) {
Gavin Howard007afdf2019-02-23 09:45:42 -0700664 bc_num_expand(&cp, bc_vm_growSize(len, 2));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600665 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600666 }
Gavin Howard021150b2018-03-10 15:40:42 -0700667
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600668 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600669 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600670 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700671
Gavin Howard63738202018-09-26 15:34:20 -0600672 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600673 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600674 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600675 len -= i - 1;
676 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700677
Gavin Howard007afdf2019-02-23 09:45:42 -0700678 if (cp.cap == cp.len) bc_num_expand(&cp, bc_vm_growSize(cp.len, 1));
Gavin Howardb651f1a2018-02-28 17:34:18 -0700679
Gavin Howard63738202018-09-26 15:34:20 -0600680 // We want an extra zero in front to make things simpler.
681 cp.num[cp.len++] = 0;
682 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700683
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600684 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700685
Gavin Howarda2514a02018-10-18 14:20:09 -0600686 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600687 c->rdx = cp.rdx;
688 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600689 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700690
Gavin Howard2d188a52019-02-25 14:19:08 -0700691 for (i = end - 1; BC_NO_SIG && BC_NO_ERR(!s) && i < end; --i) {
Gavin Howard5a788392019-03-28 11:01:13 -0600692
Gavin Howard63738202018-09-26 15:34:20 -0600693 n = cp.num + i;
Gavin Howardecafd4f2019-02-23 09:30:45 -0700694 q = 0;
Gavin Howard5a788392019-03-28 11:01:13 -0600695
696 while (BC_NO_SIG && BC_NO_ERR(!s) &&
697 (n[len] || bc_num_compare(n, p, len) >= 0))
698 {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700699 s = bc_num_subArrays(n, p, len);
Gavin Howard5a788392019-03-28 11:01:13 -0600700 q += 1;
701 }
702
Gavin Howard63738202018-09-26 15:34:20 -0600703 c->num[i] = q;
704 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700705
Gavin Howardf2ee6342019-04-09 17:05:20 -0600706 if (BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howarde5f11c72019-02-23 09:42:51 -0700707 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600708 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600709
Gavin Howard63738202018-09-26 15:34:20 -0600710 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700711}
712
Gavin Howard1cbfe242019-01-09 17:13:11 -0700713static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
714 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600715{
Gavin Howard63738202018-09-26 15:34:20 -0600716 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600717 BcNum temp;
718 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700719
Gavin Howardddce6d42019-01-03 14:07:06 -0700720 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
721 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700722 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600723 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600724 return BC_STATUS_SUCCESS;
725 }
Gavin Howard8b254872018-03-14 01:13:35 -0600726
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600727 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -0700728 s = bc_num_d(a, b, c, scale);
729 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -0700730 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700731
Gavin Howard1ab22d22019-01-03 13:32:17 -0700732 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600733
Gavin Howard53eba8b2018-10-31 15:14:37 -0600734 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700735 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600736 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700737 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600738
Gavin Howardddce6d42019-01-03 14:07:06 -0700739 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600740
741 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600742 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600743 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700744
745err:
Gavin Howard18441082018-10-22 10:03:30 -0600746 bc_num_free(&temp);
747 return s;
748}
749
Gavin Howard2d188a52019-02-25 14:19:08 -0700750static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
751{
Gavin Howard18441082018-10-22 10:03:30 -0600752 BcStatus s;
753 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -0700754 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -0600755
Gavin Howard007afdf2019-02-23 09:45:42 -0700756 ts = bc_vm_growSize(scale, b->rdx);
Gavin Howard798508b2019-02-21 16:45:32 -0700757 ts = BC_MAX(ts, a->rdx);
758
759 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -0600760 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600761 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600762
Gavin Howard63738202018-09-26 15:34:20 -0600763 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700764}
765
Gavin Howard1cbfe242019-01-09 17:13:11 -0700766static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700767
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600768 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600769 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700770 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600771 size_t i, powrdx, resrdx;
772 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700773
Gavin Howardecafd4f2019-02-23 09:30:45 -0700774 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700775
Gavin Howardddce6d42019-01-03 14:07:06 -0700776 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600777 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600778 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600779 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700780 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600781 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600782 return BC_STATUS_SUCCESS;
783 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700784 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600785 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600786 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600787 return s;
788 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700789
Gavin Howard63738202018-09-26 15:34:20 -0600790 neg = b->neg;
791 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600792 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700793 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600794 if (s) return s;
795
Gavin Howardbaa4f582019-01-24 13:56:35 -0700796 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700797
Gavin Howard63738202018-09-26 15:34:20 -0600798 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600799
Gavin Howard2d188a52019-02-25 14:19:08 -0700800 for (powrdx = a->rdx; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600801 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600802 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700803 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600804 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700805
Gavin Howard2d188a52019-02-25 14:19:08 -0700806 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -0700807
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600808 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700809 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600810
Gavin Howard2d188a52019-02-25 14:19:08 -0700811 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600812
Gavin Howard4d5daba2019-01-07 14:42:50 -0700813 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600814 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700815 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600816
Gavin Howard63738202018-09-26 15:34:20 -0600817 if (pow & 1) {
818 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600819 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700820 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600821 }
822 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700823
Gavin Howard2d188a52019-02-25 14:19:08 -0700824 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -0700825 if (neg) {
826 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700827 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -0700828 }
829
Gavin Howard63738202018-09-26 15:34:20 -0600830 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700831
Gavin Howard53eba8b2018-10-31 15:14:37 -0600832 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600833 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600834 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700835
Gavin Howard9b801632019-02-16 23:34:06 -0700836sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -0700837 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700838err:
Gavin Howard63738202018-09-26 15:34:20 -0600839 bc_num_free(&copy);
840 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700841}
842
Gavin Howard7bda4782018-12-28 09:53:22 -0700843#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -0700844static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
845 size_t scale)
846{
Gavin Howardac4d9122018-12-27 23:59:12 -0700847 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700848 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700849
850 BC_UNUSED(scale);
851
852 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -0700853 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -0700854
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700855 if (val < c->rdx) bc_num_truncate(c, c->rdx - val);
856 else if (val > c->rdx) bc_num_extend(c, val - c->rdx);
Gavin Howardac4d9122018-12-27 23:59:12 -0700857
858 return s;
859}
860
Gavin Howard2d188a52019-02-25 14:19:08 -0700861static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
862{
Gavin Howardac4d9122018-12-27 23:59:12 -0700863 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700864 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700865
866 BC_UNUSED(scale);
867
868 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -0700869 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -0700870
Gavin Howarddbbada82019-02-22 08:51:19 -0700871 bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -0700872
873 return s;
874}
875
Gavin Howard2d188a52019-02-25 14:19:08 -0700876static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
877 size_t scale)
878{
Gavin Howardac4d9122018-12-27 23:59:12 -0700879 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700880 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700881
882 BC_UNUSED(scale);
883
884 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -0700885 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -0700886
Gavin Howard2a366922019-01-16 10:50:20 -0700887 if (BC_NUM_ZERO(c)) return s;
888
Gavin Howarddbbada82019-02-22 08:51:19 -0700889 bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -0700890
891 return s;
892}
Gavin Howard7bda4782018-12-28 09:53:22 -0700893#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700894
Gavin Howard1cbfe242019-01-09 17:13:11 -0700895static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
896 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700897{
Gavin Howarda1c44392018-09-27 12:41:15 -0600898 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600899 BcNum num2, *ptr_a, *ptr_b;
900 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700901
Gavin Howard63738202018-09-26 15:34:20 -0600902 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700903
Gavin Howard890d0c02018-10-30 16:34:50 -0600904 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600905 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600906 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600907 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600908 }
909 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700910
Gavin Howard63738202018-09-26 15:34:20 -0600911 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600912 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600913 if (c != a) {
914 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600915 init = true;
916 }
917 }
918 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700919
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600920 if (init) bc_num_init(c, req);
921 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700922
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600923 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700924
Gavin Howardddce6d42019-01-03 14:07:06 -0700925 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -0600926 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -0600927
Gavin Howardba009802018-09-29 04:41:51 -0600928 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600929
Gavin Howardee9d01e2019-02-16 22:48:11 -0700930 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700931}
932
Gavin Howardc90ed902019-01-02 13:07:49 -0700933#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -0700934static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700935
Gavin Howardc7d655b2018-12-28 19:45:13 -0700936 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -0600937 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700938
Gavin Howard63738202018-09-26 15:34:20 -0600939 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700940
Gavin Howard63738202018-09-26 15:34:20 -0600941 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700942
Gavin Howard8a921bd2018-10-11 14:15:32 -0600943 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600944
Gavin Howard63738202018-09-26 15:34:20 -0600945 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700946
Gavin Howard63738202018-09-26 15:34:20 -0600947 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700948
Gavin Howard63738202018-09-26 15:34:20 -0600949 radix = true;
950 continue;
951 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700952
Gavin Howardc7d655b2018-12-28 19:45:13 -0700953 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -0600954 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700955
Gavin Howard63738202018-09-26 15:34:20 -0600956 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700957}
Gavin Howardc90ed902019-01-02 13:07:49 -0700958#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -0700959
Gavin Howard1cbfe242019-01-09 17:13:11 -0700960static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -0700961
962 if (isupper(c)) {
963 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -0700964 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -0700965 }
966 else c -= '0';
967
968 return (unsigned long) (uchar) c;
969}
970
Gavin Howard1cbfe242019-01-09 17:13:11 -0700971static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700972
Gavin Howard63738202018-09-26 15:34:20 -0600973 size_t len, i;
974 const char *ptr;
975 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700976
Gavin Howard63738202018-09-26 15:34:20 -0600977 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700978
Gavin Howard63738202018-09-26 15:34:20 -0600979 val += i;
980 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700981
Gavin Howard1ab22d22019-01-03 13:32:17 -0700982 if (len) {
Gavin Howard798508b2019-02-21 16:45:32 -0700983 for (i = 0; zero && i < len; ++i)
984 zero = (val[i] == '0') || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600985 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600986 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600987
Gavin Howard63738202018-09-26 15:34:20 -0600988 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700989
Gavin Howard63738202018-09-26 15:34:20 -0600990 // Explicitly test for NULL here to produce either a 0 or 1.
991 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700992
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600993 if (!zero) {
Gavin Howard8dd307e2019-01-08 23:05:19 -0700994 for (i = len - 1; i < len; ++n->len, --i) {
995
Gavin Howardc7d655b2018-12-28 19:45:13 -0700996 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -0700997
998 if (c == '.') n->len -= 1;
999 else {
1000 if (isupper(c)) c = '9';
1001 n->num[n->len] = c - '0';
1002 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001003 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001004 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001005}
1006
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001007static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1008 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001009{
1010 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001011 BcNum temp, mult, result;
Gavin Howard1cbfe242019-01-09 17:13:11 -07001012 BcDig c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001013 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001014 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001015 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001016
Gavin Howard11b9afd2018-10-18 14:23:28 -06001017 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001018 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001019
Gavin Howard26d078f2019-01-24 13:58:12 -07001020 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1021 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001022
Gavin Howardc7d655b2018-12-28 19:45:13 -07001023 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001024
Gavin Howardc7d655b2018-12-28 19:45:13 -07001025 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001026
Gavin Howard53eba8b2018-10-31 15:14:37 -06001027 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001028 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001029 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001030 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001031 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001032 }
Gavin Howardede51f02018-03-02 12:30:00 -07001033
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001034 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001035
Gavin Howard63738202018-09-26 15:34:20 -06001036 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001037 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001038 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001039
Gavin Howard2d188a52019-02-25 14:19:08 -07001040 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001041 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001042 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001043
Gavin Howard53eba8b2018-10-31 15:14:37 -06001044 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001045 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001046
Gavin Howard705c4bc2018-12-03 15:38:04 -07001047 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001048 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001049 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001050 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001051 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001052 }
Gavin Howardede51f02018-03-02 12:30:00 -07001053
Gavin Howardf2ee6342019-04-09 17:05:20 -06001054 if (BC_SIG) {
1055 s = BC_STATUS_SIGNAL;
1056 goto err;
1057 }
1058
Gavin Howard1769abc2019-02-21 09:36:14 -07001059 // This one cannot be a divide by 0 because mult starts out at 1, then is
1060 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1061 s = bc_num_div(&result, &mult, &result, digs);
1062 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001063 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001064 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001065 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001066
Gavin Howardddce6d42019-01-03 14:07:06 -07001067 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001068 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001069 }
1070 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001071
Gavin Howardd1417682019-02-15 17:08:42 -07001072err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001073 bc_num_free(&result);
1074int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001075 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001076 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001077 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001078}
1079
Gavin Howard1cbfe242019-01-09 17:13:11 -07001080static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001081 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001082 bc_vm_putchar('\\');
1083 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001084 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001085 }
Gavin Howard80977b22018-09-06 14:29:13 -06001086}
1087
Gavin Howard40a085f2018-12-03 12:08:59 -07001088#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001089static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001090 BC_UNUSED(rdx);
1091 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001092 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001093}
1094#endif // DC_ENABLED
1095
Gavin Howard1cbfe242019-01-09 17:13:11 -07001096static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1097
Gavin Howarda84ad992018-12-03 19:11:06 -07001098 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001099
Gavin Howardab5e9632019-01-02 13:32:02 -07001100 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001101 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001102 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001103
Gavin Howardab5e9632019-01-02 13:32:02 -07001104 bc_num_printNewline();
Gavin Howard530e3072019-04-23 16:23:36 -06001105 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001106
Gavin Howard530e3072019-04-23 16:23:36 -06001107 for (exp = 0; exp < len; pow /= BC_BASE, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001108 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001109 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001110 dig = n / pow;
1111 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001112 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001113 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001114}
Gavin Howardfe679f02018-02-14 15:50:09 -07001115
Gavin Howard1cbfe242019-01-09 17:13:11 -07001116static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001117
Gavin Howard3c02ed32018-12-28 18:17:15 -07001118 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001119
Gavin Howard3c02ed32018-12-28 18:17:15 -07001120 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001121 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001122 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001123 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001124 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001125
Gavin Howardab5e9632019-01-02 13:32:02 -07001126 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001127 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001128 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001129}
1130
Gavin Howard1cbfe242019-01-09 17:13:11 -07001131static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001132
Gavin Howard63738202018-09-26 15:34:20 -06001133 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001134
Gavin Howard48af52e2018-10-30 14:47:38 -06001135 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001136 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001137
Gavin Howard48af52e2018-10-30 14:47:38 -06001138 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001139 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001140}
1141
Gavin Howard7ad5a662019-02-19 14:40:46 -07001142#if BC_ENABLE_EXTRA_MATH
1143static void bc_num_printExponent(const BcNum *restrict n, bool eng) {
1144
1145 bool neg = (n->len <= n->rdx);
1146 BcNum temp, exp;
1147 size_t places, mod;
1148 BcDig digs[BC_NUM_LONG_LOG10];
1149
1150 bc_num_createCopy(&temp, n);
1151
1152 if (neg) {
1153 places = n->rdx - bc_num_len(n) + 1;
1154 mod = places % 3;
1155 if (eng && mod != 0) places += 3 - mod;
Gavin Howarddbbada82019-02-22 08:51:19 -07001156 bc_num_shiftLeft(&temp, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001157 }
1158 else {
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001159 places = bc_num_int(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001160 mod = places % 3;
1161 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howarddbbada82019-02-22 08:51:19 -07001162 bc_num_shiftRight(&temp, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001163 }
1164
1165 bc_num_printDecimal(&temp);
1166
1167 bc_num_printNewline();
1168 bc_vm_putchar('e');
1169
1170 if (!places) {
1171 bc_num_printHex(0, 1, false);
1172 goto exit;
1173 }
1174
1175 if (neg) {
1176 bc_num_printNewline();
1177 bc_vm_putchar('-');
1178 }
1179
1180 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1181 bc_num_ulong2num(&exp, (unsigned long) places);
1182
1183 bc_num_printDecimal(&exp);
1184
1185exit:
1186 bc_num_free(&temp);
1187}
1188#endif // BC_ENABLE_EXTRA_MATH
1189
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001190static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1191 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001192{
Gavin Howard63738202018-09-26 15:34:20 -06001193 BcStatus s;
1194 BcVec stack;
1195 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001196 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001197 size_t i;
1198 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001199
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001200 assert(!BC_NUM_ZERO(base));
1201
Gavin Howardddce6d42019-01-03 14:07:06 -07001202 if (BC_NUM_ZERO(n)) {
1203 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001204 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001205 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001206
Gavin Howardd3929832018-12-24 15:13:15 -07001207 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001208 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001209 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001210 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001211 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001212 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001213
Gavin Howard63738202018-09-26 15:34:20 -06001214 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001215 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001216 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001217
Gavin Howard2d188a52019-02-25 14:19:08 -07001218 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001219
1220 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001221 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001222 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001223
1224 // Will never fail (except for signals) because digit is
1225 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001226 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001227 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001228
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001229 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001230 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001231
Gavin Howard2d188a52019-02-25 14:19:08 -07001232 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001233
Gavin Howard2d188a52019-02-25 14:19:08 -07001234 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001235 ptr = bc_vec_item_rev(&stack, i);
1236 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001237 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001238 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001239
Gavin Howard2d188a52019-02-25 14:19:08 -07001240 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001241 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001242
Gavin Howard2d188a52019-02-25 14:19:08 -07001243 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001244
Gavin Howard53eba8b2018-10-31 15:14:37 -06001245 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001246 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001247
1248 // Will never fail (except for signals) because fracp is
1249 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001250 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001251 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001252
Gavin Howard705c4bc2018-12-03 15:38:04 -07001253 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001254 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001255 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001256
Gavin Howardddce6d42019-01-03 14:07:06 -07001257 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001258 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001259 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001260 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001261
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001262sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001263 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001264err:
Gavin Howard63738202018-09-26 15:34:20 -06001265 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001266 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001267 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001268 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001269 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001270 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001271}
1272
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001273static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1274 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001275{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001276 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001277 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001278 BcNumDigitOp print;
1279 bool neg = n->neg;
1280
Gavin Howard48af52e2018-10-30 14:47:38 -06001281 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001282 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001283
1284 n->neg = false;
1285
Gavin Howardbe4e8222019-01-03 13:34:16 -07001286 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001287 width = 1;
1288 print = bc_num_printHex;
1289 }
1290 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001291 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001292 print = bc_num_printDigits;
1293 }
1294
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001295 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001296 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001297
1298 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001299}
1300
Gavin Howard40a085f2018-12-03 12:08:59 -07001301#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001302BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1303 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001304}
1305#endif // DC_ENABLED
1306
Gavin Howard3c02ed32018-12-28 18:17:15 -07001307void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001308 assert(n);
1309 n->num = num;
1310 n->cap = cap;
1311 n->rdx = n->len = 0;
1312 n->neg = false;
1313}
1314
Gavin Howard3c02ed32018-12-28 18:17:15 -07001315void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard57223022019-04-24 08:37:14 -06001316 assert(n && req);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001317 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001318 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001319}
1320
Gavin Howarded392aa2018-02-27 13:09:26 -07001321void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001322 assert(num);
1323 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001324}
1325
Gavin Howard3c02ed32018-12-28 18:17:15 -07001326void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001327 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001328 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001329 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001330 d->len = s->len;
1331 d->neg = s->neg;
1332 d->rdx = s->rdx;
1333 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001334}
1335
Gavin Howardbaa4f582019-01-24 13:56:35 -07001336void bc_num_createCopy(BcNum *d, const BcNum *s) {
1337 bc_num_init(d, s->len);
1338 bc_num_copy(d, s);
1339}
1340
1341void bc_num_createFromUlong(BcNum *n, unsigned long val) {
1342 bc_num_init(n, BC_NUM_LONG_LOG10);
1343 bc_num_ulong2num(n, val);
1344}
1345
Gavin Howard7ad5a662019-02-19 14:40:46 -07001346size_t bc_num_scale(const BcNum *restrict n) {
1347 return n->rdx;
1348}
1349
1350size_t bc_num_len(const BcNum *restrict n) {
1351
1352 size_t i, len = n->len;
1353
1354 if (n->rdx != n->len) return len;
1355 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1356
1357 return len;
1358}
1359
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001360BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1361 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001362{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001363 BcStatus s = BC_STATUS_SUCCESS;
1364
Gavin Howard63738202018-09-26 15:34:20 -06001365 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001366 assert(BC_ENABLE_EXTRA_MATH ||
1367 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001368 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001369
Gavin Howardd746e802018-12-28 22:06:00 -07001370 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06001371 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001372 else s = bc_num_parseBase(n, val, base, base_t);
1373
1374 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001375}
1376
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001377BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1378 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001379{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001380 BcStatus s = BC_STATUS_SUCCESS;
1381
Gavin Howardab5e9632019-01-02 13:32:02 -07001382 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001383 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001384
Gavin Howardab5e9632019-01-02 13:32:02 -07001385 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001386
Gavin Howardddce6d42019-01-03 14:07:06 -07001387 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06001388 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001389#if BC_ENABLE_EXTRA_MATH
1390 else if (base_t == 0 || base_t == 1) bc_num_printExponent(n, base_t != 0);
1391#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001392 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001393
Gavin Howarde5f11c72019-02-23 09:42:51 -07001394 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001395 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001396 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001397 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001398
1399 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001400}
1401
Gavin Howard3c02ed32018-12-28 18:17:15 -07001402BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001403
Gavin Howard63738202018-09-26 15:34:20 -06001404 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001405 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001406
Gavin Howard63738202018-09-26 15:34:20 -06001407 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001408
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001409 *result = 0;
1410
Gavin Howardecafd4f2019-02-23 09:30:45 -07001411 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001412
Gavin Howard06b6e862019-02-15 11:15:31 -07001413 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001414
Gavin Howard530e3072019-04-23 16:23:36 -06001415 unsigned long prev = r * BC_BASE;
Gavin Howard5bede412019-02-23 09:31:28 -07001416
Gavin Howard530e3072019-04-23 16:23:36 -06001417 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001418 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1419
1420 r = prev + ((uchar) n->num[--i]);
1421
1422 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001423 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001424 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001425
Gavin Howard7b557da2018-12-21 10:25:32 -07001426 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001427
Gavin Howard63738202018-09-26 15:34:20 -06001428 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001429}
1430
Gavin Howard3c02ed32018-12-28 18:17:15 -07001431void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001432
Gavin Howard8a921bd2018-10-11 14:15:32 -06001433 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001434 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001435
Gavin Howard63738202018-09-26 15:34:20 -06001436 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001437
Gavin Howard63738202018-09-26 15:34:20 -06001438 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001439
Gavin Howard1ab22d22019-01-03 13:32:17 -07001440 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001441
Gavin Howard798508b2019-02-21 16:45:32 -07001442 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howard05feab02019-04-23 16:24:07 -06001443
Gavin Howard530e3072019-04-23 16:23:36 -06001444 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE)
1445 ptr[i] = val % BC_BASE;
Gavin Howard025d04d2018-02-20 13:53:28 -07001446}
1447
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001448size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001449
1450 size_t aint, bint, ardx, brdx;
1451
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001452 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001453
1454 ardx = a->rdx;
1455 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001456 aint = bc_num_int(a);
1457 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001458 ardx = BC_MAX(ardx, brdx);
1459 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001460
Gavin Howard2d188a52019-02-25 14:19:08 -07001461 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001462}
1463
1464size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001465 size_t max, rdx;
1466 rdx = bc_vm_growSize(a->rdx, b->rdx);
1467 max = bc_vm_growSize(BC_MAX(scale, rdx), 1);
1468 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1469 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001470}
1471
1472size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1473 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001474 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001475}
1476
1477#if BC_ENABLE_EXTRA_MATH
1478size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1479 BC_UNUSED(b);
1480 BC_UNUSED(scale);
1481 return BC_NUM_SHREQ(a);
1482}
1483#endif // BC_ENABLE_EXTRA_MATH
1484
Gavin Howard12fe7812018-09-29 03:45:18 -06001485BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001486 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001487 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001488 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001489}
1490
Gavin Howard12fe7812018-09-29 03:45:18 -06001491BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001492 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001493 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001494 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001495}
1496
Gavin Howard12fe7812018-09-29 03:45:18 -06001497BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001498 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001499}
1500
Gavin Howard12fe7812018-09-29 03:45:18 -06001501BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001502 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001503}
1504
Gavin Howard54b946a2018-10-23 12:06:57 -06001505BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001506 size_t req = bc_num_mulReq(a, b, scale);
1507 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001508}
1509
Gavin Howard12fe7812018-09-29 03:45:18 -06001510BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001511 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001512}
1513
Gavin Howard7d8f0382018-12-27 16:15:13 -07001514#if BC_ENABLE_EXTRA_MATH
1515BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001516 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001517}
1518
1519BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001520 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001521}
1522
1523BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001524 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001525}
1526#endif // BC_ENABLE_EXTRA_MATH
1527
Gavin Howard3c02ed32018-12-28 18:17:15 -07001528BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001529
Gavin Howard1cbfe242019-01-09 17:13:11 -07001530 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001531 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard14913fc2019-04-23 14:36:03 -06001532 size_t pow, len, req, digs, digs1, digs2, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001533 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001534 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001535
Gavin Howard34edd0a2018-10-25 15:51:05 -06001536 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001537
Gavin Howardecafd4f2019-02-23 09:30:45 -07001538 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07001539
Gavin Howard007afdf2019-02-23 09:45:42 -07001540 len = bc_vm_growSize(bc_num_int(a), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001541 req = bc_vm_growSize(BC_MAX(scale, a->rdx), len >> 1);
1542 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001543
Gavin Howardddce6d42019-01-03 14:07:06 -07001544 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001545 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001546 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001547 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001548 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001549 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001550 bc_num_extend(b, scale);
1551 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001552 }
Gavin Howard954276b2018-05-16 01:43:58 -06001553
Gavin Howard890d0c02018-10-30 16:34:50 -06001554 scale = BC_MAX(scale, a->rdx) + 1;
Gavin Howard007afdf2019-02-23 09:45:42 -07001555 len = bc_vm_growSize(a->len, scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001556
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001557 bc_num_init(&num1, len);
1558 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001559 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001560
Gavin Howard63738202018-09-26 15:34:20 -06001561 bc_num_one(&half);
1562 half.num[0] = 5;
1563 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001564
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001565 bc_num_init(&f, len);
1566 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001567
Gavin Howard63738202018-09-26 15:34:20 -06001568 x0 = &num1;
1569 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001570
Gavin Howard63738202018-09-26 15:34:20 -06001571 bc_num_one(x0);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001572 pow = bc_num_int(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001573
Gavin Howard53eba8b2018-10-31 15:14:37 -06001574 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001575
Gavin Howardc39fd492018-10-04 10:07:03 -06001576 if (pow & 1) x0->num[0] = 2;
1577 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001578
Gavin Howardc39fd492018-10-04 10:07:03 -06001579 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001580
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001581 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001582
Gavin Howard63738202018-09-26 15:34:20 -06001583 // Make sure to move the radix back.
1584 x0->rdx -= pow;
1585 }
Gavin Howard954276b2018-05-16 01:43:58 -06001586
Gavin Howard14913fc2019-04-23 14:36:03 -06001587 x0->rdx = digs = digs1 = digs2 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001588 resrdx = scale + 2;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001589 len = bc_num_int(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001590
Gavin Howard2d188a52019-02-25 14:19:08 -07001591 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001592
Gavin Howard1769abc2019-02-21 09:36:14 -07001593 assert(!BC_NUM_ZERO(x0));
1594
Gavin Howard53eba8b2018-10-31 15:14:37 -06001595 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001596 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001597 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001598 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001599 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001600 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001601 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001602
Gavin Howard53eba8b2018-10-31 15:14:37 -06001603 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06001604 if (cmp == BC_NUM_SSIZE_MIN) {
1605 s = BC_STATUS_SIGNAL;
1606 break;
1607 }
1608
Gavin Howard53eba8b2018-10-31 15:14:37 -06001609 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001610
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001611 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001612 else times = 0;
1613
1614 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001615
1616 cmp2 = cmp1;
1617 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001618 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001619
Gavin Howard63738202018-09-26 15:34:20 -06001620 temp = x0;
1621 x0 = x1;
1622 x1 = temp;
1623 }
Gavin Howard954276b2018-05-16 01:43:58 -06001624
Gavin Howard2d188a52019-02-25 14:19:08 -07001625 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07001626 s = BC_STATUS_SIGNAL;
1627 goto err;
1628 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001629
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001630 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001631 scale -= 1;
1632 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001633
1634err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06001635 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06001636 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001637 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001638 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001639 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001640 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001641 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001642 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001643}
Gavin Howardba009802018-09-29 04:41:51 -06001644
Gavin Howard2cb39612018-10-22 08:46:48 -06001645BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1646
1647 BcStatus s;
1648 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001649 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07001650 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001651
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001652 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001653
Gavin Howard890d0c02018-10-30 16:34:50 -06001654 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001655 memcpy(&num2, c, sizeof(BcNum));
1656 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001657 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001658 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001659 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001660 else {
1661 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001662 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001663 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001664
Gavin Howard54b946a2018-10-23 12:06:57 -06001665 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001666
Gavin Howardddce6d42019-01-03 14:07:06 -07001667 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001668 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001669 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001670 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001671
1672 if (init) bc_num_free(&num2);
1673
1674 return s;
1675}
1676
Gavin Howard40a085f2018-12-03 12:08:59 -07001677#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001678BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001679
1680 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001681 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001682 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001683
Gavin Howard34edd0a2018-10-25 15:51:05 -06001684 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001685
Gavin Howardecafd4f2019-02-23 09:30:45 -07001686 if (BC_ERR(BC_NUM_ZERO(c)))
1687 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1688 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1689 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07001690 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001691
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001692 bc_num_expand(d, c->len);
1693 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001694 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001695 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001696
1697 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001698 two.num[0] = 2;
1699 bc_num_one(d);
1700
Gavin Howard1769abc2019-02-21 09:36:14 -07001701 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001702 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001703 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001704 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07001705 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001706
Gavin Howard2d188a52019-02-25 14:19:08 -07001707 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001708
Gavin Howard1769abc2019-02-21 09:36:14 -07001709 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001710 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001711 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001712
Gavin Howarda83a5e52019-02-21 16:51:06 -07001713 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001714 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001715 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001716
1717 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001718 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001719 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001720 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001721 }
1722
Gavin Howard53eba8b2018-10-31 15:14:37 -06001723 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001724 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001725
1726 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001727 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001728 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001729 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001730 }
1731
Gavin Howard2d188a52019-02-25 14:19:08 -07001732 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07001733
Gavin Howardba009802018-09-29 04:41:51 -06001734err:
Gavin Howardba009802018-09-29 04:41:51 -06001735 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07001736rem_err:
1737 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001738 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001739 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001740 return s;
1741}
Gavin Howarde6e84762018-10-03 11:46:34 -06001742#endif // DC_ENABLED