blob: b2296914ed570109ea0577d63b1b0c07d7125722 [file] [log] [blame]
Gavin Howard3eb626f2018-02-14 13:54:35 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -07003 *
Gavin Howardb5904bf2018-02-20 13:28:18 -07004 * Copyright 2018 Gavin D. Howard
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070017 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070018 *
19 * Code for the number type.
20 *
21 */
22
23#include <assert.h>
Gavin Howardc7d655b2018-12-28 19:45:13 -070024#include <ctype.h>
Gavin Howard411f7322018-09-26 17:21:19 -060025#include <stdbool.h>
26#include <stdlib.h>
27#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070028
Gavin Howard305249a2018-10-15 20:24:47 -060029#include <limits.h>
30
Gavin Howard29493062018-03-20 19:57:37 -060031#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070032#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060033#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070034
Gavin Howard56ebcf72019-02-21 16:59:48 -070035static ssize_t bc_num_neg(size_t n, bool neg) {
36 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
37}
38
Gavin Howardd2cf06a2019-02-21 17:03:24 -070039ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -070040 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -070041}
42
Gavin Howard7b7d2f32019-02-21 16:46:47 -070043static size_t bc_num_int(const BcNum *n) {
44 return n->len ? n->len - n->rdx : 0;
45}
46
Gavin Howarda83a5e52019-02-21 16:51:06 -070047static bool bc_num_isOne(const BcNum *n) {
48 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
49}
50
Gavin Howard798508b2019-02-21 16:45:32 -070051static void bc_num_expand(BcNum *restrict n, size_t req) {
52 assert(n);
53 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
54 if (req > n->cap) {
55 n->num = bc_vm_realloc(n->num, req);
56 n->cap = req;
57 }
58}
59
Gavin Howard1cbfe242019-01-09 17:13:11 -070060static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060061 assert(n);
Gavin Howard97102b12018-11-01 23:37:31 -060062 n->len = 0;
63 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060064 n->rdx = scale;
65}
66
Gavin Howard1cbfe242019-01-09 17:13:11 -070067static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060068 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060069}
70
Gavin Howard3c02ed32018-12-28 18:17:15 -070071void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060072 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060073 n->len = 1;
74 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060075}
76
Gavin Howard3c02ed32018-12-28 18:17:15 -070077void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060078 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060079 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060080 n->len = 2;
81 n->num[0] = 0;
82 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060083}
84
Gavin Howard1cbfe242019-01-09 17:13:11 -070085static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -070086 size_t len;
87 for (len = 1; i; i /= 10, ++len);
88 return len;
89}
90
Gavin Howarddc0ee9e2019-02-16 23:27:08 -070091static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
92 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -070093{
Gavin Howard63738202018-09-26 15:34:20 -060094 size_t i, j;
Gavin Howard5dbb48b2019-01-04 11:29:29 -070095 for (i = 0; !BC_SIGNAL && i < len; ++i) {
96 for (a[i] -= b[i], j = 0; !BC_SIGNAL && a[i + j] < 0;) {
Gavin Howard54b946a2018-10-23 12:06:57 -060097 a[i + j++] += 10;
98 a[i + j] -= 1;
Gavin Howard231e66c2019-01-08 23:02:19 -070099 assert(a[i + j - 1] >= 0 && a[i + j - 1] < 10);
Gavin Howard63738202018-09-26 15:34:20 -0600100 }
101 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700102 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600103}
104
Gavin Howard1cbfe242019-01-09 17:13:11 -0700105static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b, size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700106{
Gavin Howard63738202018-09-26 15:34:20 -0600107 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -0600108 int c = 0;
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700109 for (i = len - 1; !BC_SIGNAL && i < len && !(c = a[i] - b[i]); --i);
Gavin Howard56ebcf72019-02-21 16:59:48 -0700110 return bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600111}
112
Gavin Howard3c02ed32018-12-28 18:17:15 -0700113ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700114
Gavin Howard63738202018-09-26 15:34:20 -0600115 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600116 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600117 bool a_max, neg = false;
118 ssize_t cmp;
119
120 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700121
Gavin Howard8a921bd2018-10-11 14:15:32 -0600122 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700123 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700124 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600125 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600126 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600127 else return -1;
128 }
129 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700130
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700131 a_int = bc_num_int(a);
132 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600133 a_int -= b_int;
134 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700135
Gavin Howard1ab22d22019-01-03 13:32:17 -0700136 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700137
Gavin Howard890d0c02018-10-30 16:34:50 -0600138 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600139 min = b->rdx;
140 diff = a->rdx - b->rdx;
141 max_num = a->num + diff;
142 min_num = b->num;
143 }
144 else {
145 min = a->rdx;
146 diff = b->rdx - a->rdx;
147 max_num = b->num + diff;
148 min_num = a->num;
149 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700150
Gavin Howard890d0c02018-10-30 16:34:50 -0600151 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard56ebcf72019-02-21 16:59:48 -0700152 if (cmp) return bc_num_neg((size_t) cmp, (!a_max) != neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700153
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700154 for (max_num -= diff, i = diff - 1; !BC_SIGNAL && i < diff; --i) {
Gavin Howard56ebcf72019-02-21 16:59:48 -0700155 if (max_num[i]) return bc_num_neg(1, (!a_max) != neg);
Gavin Howard63738202018-09-26 15:34:20 -0600156 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700157
Gavin Howard63738202018-09-26 15:34:20 -0600158 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700159}
160
Gavin Howard2a366922019-01-16 10:50:20 -0700161static void bc_num_clean(BcNum *restrict n) {
162 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
163 if (BC_NUM_ZERO(n)) n->neg = false;
164 else if (n->len < n->rdx) n->len = n->rdx;
165}
166
Gavin Howard3c02ed32018-12-28 18:17:15 -0700167void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700168
Gavin Howardddce6d42019-01-03 14:07:06 -0700169 assert(places <= n->rdx && (BC_NUM_ZERO(n) || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700170
Gavin Howard1ab22d22019-01-03 13:32:17 -0700171 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700172
Gavin Howard63738202018-09-26 15:34:20 -0600173 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700174
Gavin Howardddce6d42019-01-03 14:07:06 -0700175 if (BC_NUM_NONZERO(n)) {
Gavin Howard955da852018-10-23 11:08:20 -0600176 n->len -= places;
177 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
Gavin Howard2a366922019-01-16 10:50:20 -0700178 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600179 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700180}
181
Gavin Howard1cbfe242019-01-09 17:13:11 -0700182static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700183
Gavin Howard798508b2019-02-21 16:45:32 -0700184 size_t len = bc_vm_checkSize(n->len, places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700185
Gavin Howard1ab22d22019-01-03 13:32:17 -0700186 if (!places) return;
Gavin Howard1ab22d22019-01-03 13:32:17 -0700187 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700188
Gavin Howard1ab22d22019-01-03 13:32:17 -0700189 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
190 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700191
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700192 if (n->len) n->len += places;
193
Gavin Howard1ab22d22019-01-03 13:32:17 -0700194 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700195}
196
Gavin Howard1cbfe242019-01-09 17:13:11 -0700197static void bc_num_retireMul(BcNum *restrict n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600198
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600199 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600200 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600201
Gavin Howard305249a2018-10-15 20:24:47 -0600202 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700203 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600204}
205
Gavin Howard1cbfe242019-01-09 17:13:11 -0700206static void bc_num_split(const BcNum *restrict n, size_t idx, BcNum *restrict a, BcNum *restrict b) {
Gavin Howard50c80022019-01-03 15:14:33 -0700207
Gavin Howard305249a2018-10-15 20:24:47 -0600208 if (idx < n->len) {
209
Gavin Howard305249a2018-10-15 20:24:47 -0600210 b->len = n->len - idx;
211 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600212 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600213
214 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
215 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard50c80022019-01-03 15:14:33 -0700216
217 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600218 }
Gavin Howard50c80022019-01-03 15:14:33 -0700219 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600220
221 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600222}
223
Gavin Howard7ad5a662019-02-19 14:40:46 -0700224static void bc_num_shift_l(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600225
Gavin Howardd8b004b2019-02-16 22:35:08 -0700226 if (!places || BC_NUM_ZERO(n)) return;
Gavin Howard305249a2018-10-15 20:24:47 -0600227
228 if (n->rdx >= places) n->rdx -= places;
229 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600230 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600231 n->rdx = 0;
232 }
233
234 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600235}
236
Gavin Howard7ad5a662019-02-19 14:40:46 -0700237#if BC_ENABLE_EXTRA_MATH
238static void bc_num_shift_r(BcNum *restrict n, size_t places) {
239
Gavin Howard798508b2019-02-21 16:45:32 -0700240 size_t len = bc_vm_checkSize(n->rdx, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700241
242 if (len > n->len) {
243
244 if (len > n->cap) bc_num_expand(n, len);
245
246 memset(n->num + n->len, 0, len - n->len);
247 n->len = len;
248 }
249
250 n->rdx += places;
251
252 assert(n->rdx <= n->len && n->len <= n->cap);
253}
254#endif // BC_ENABLE_EXTRA_MATH
255
Gavin Howard1cbfe242019-01-09 17:13:11 -0700256static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600257
Gavin Howard63738202018-09-26 15:34:20 -0600258 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600259 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600260
Gavin Howard1769abc2019-02-21 09:36:14 -0700261 assert(!BC_NUM_ZERO(a));
262
Gavin Howardd32d7df2018-10-15 08:42:25 -0600263 one.cap = 2;
264 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600265 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600266
Gavin Howardd32d7df2018-10-15 08:42:25 -0600267 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600268}
269
Gavin Howard7bda4782018-12-28 09:53:22 -0700270#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700271static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
272 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700273{
Gavin Howardac4d9122018-12-27 23:59:12 -0700274 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
275 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700276 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700277}
Gavin Howard7bda4782018-12-28 09:53:22 -0700278#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700279
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700280static unsigned int bc_num_addDigit(BcDig *restrict num, unsigned int d, unsigned int c)
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700281{
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700282 d += c;
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700283 *num = (BcDig) (d % 10);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700284 assert(*num >= 0 && *num < 10);
Gavin Howard201cb6f2019-01-22 10:01:29 -0700285 assert((d / 10) < 10);
286 return d / 10;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700287}
288
Gavin Howard1cbfe242019-01-09 17:13:11 -0700289static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700290
Gavin Howard8a921bd2018-10-11 14:15:32 -0600291 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600292 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700293 unsigned int carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700294
Gavin Howard63738202018-09-26 15:34:20 -0600295 // Because this function doesn't need to use scale (per the bc spec),
296 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600297
Gavin Howardddce6d42019-01-03 14:07:06 -0700298 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600299 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700300 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600301 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600302 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700303 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600304 bc_num_copy(c, a);
305 return BC_STATUS_SUCCESS;
306 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600307
Gavin Howard63738202018-09-26 15:34:20 -0600308 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600309 c->rdx = BC_MAX(a->rdx, b->rdx);
310 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700311
Gavin Howard63738202018-09-26 15:34:20 -0600312 if (a->rdx > b->rdx) {
313 diff = a->rdx - b->rdx;
314 ptr = a->num;
315 ptr_a = a->num + diff;
316 ptr_b = b->num;
317 }
318 else {
319 diff = b->rdx - a->rdx;
320 ptr = b->num;
321 ptr_a = a->num;
322 ptr_b = b->num + diff;
323 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700324
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700325 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700326
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700327 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600328 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700329 a_int = bc_num_int(a);
330 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700331
Gavin Howard890d0c02018-10-30 16:34:50 -0600332 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600333 min_int = b_int;
334 max = a_int;
335 ptr = ptr_a;
336 }
337 else {
338 min_int = a_int;
339 max = b_int;
340 ptr = ptr_b;
341 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700342
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700343 for (carry = 0, i = 0; !BC_SIGNAL && i < min_rdx + min_int; ++i) {
344 unsigned int in = (unsigned int) (ptr_a[i] + ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700345 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600346 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700347
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700348 for (; !BC_SIGNAL && i < max + min_rdx; ++i)
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700349 carry = bc_num_addDigit(ptr_c + i, (unsigned int) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700350
351 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700352
Gavin Howard1ab22d22019-01-03 13:32:17 -0700353 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600354
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700355 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700356}
357
Gavin Howard1cbfe242019-01-09 17:13:11 -0700358static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700359
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700360 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600361 ssize_t cmp;
362 BcNum *minuend, *subtrahend;
363 size_t start;
364 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700365
Gavin Howard63738202018-09-26 15:34:20 -0600366 // Because this function doesn't need to use scale (per the bc spec),
367 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700368
Gavin Howardddce6d42019-01-03 14:07:06 -0700369 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600370 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700371 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600372 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600373 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700374 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600375 bc_num_copy(c, a);
376 return BC_STATUS_SUCCESS;
377 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700378
Gavin Howard63738202018-09-26 15:34:20 -0600379 aneg = a->neg;
380 bneg = b->neg;
381 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700382
Gavin Howard63738202018-09-26 15:34:20 -0600383 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700384
Gavin Howard63738202018-09-26 15:34:20 -0600385 a->neg = aneg;
386 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700387
Gavin Howard1ab22d22019-01-03 13:32:17 -0700388 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600389 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600390 return BC_STATUS_SUCCESS;
391 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700392
393 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600394 neg = a->neg;
395 minuend = a;
396 subtrahend = b;
397 }
398 else {
399 neg = b->neg;
400 if (sub) neg = !neg;
401 minuend = b;
402 subtrahend = a;
403 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700404
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600405 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600406 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700407
Gavin Howard63738202018-09-26 15:34:20 -0600408 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600409 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600410 start = 0;
411 }
412 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700413
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700414 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700415
Gavin Howard63738202018-09-26 15:34:20 -0600416 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700417
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700418 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700419}
420
Gavin Howard1cbfe242019-01-09 17:13:11 -0700421static BcStatus bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600422
Gavin Howard9b801632019-02-16 23:34:06 -0700423 BcStatus s;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700424 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600425 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700426 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600427
Gavin Howardddce6d42019-01-03 14:07:06 -0700428 // This is here because the function is recursive.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700429 if (BC_SIGNAL) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700430 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600431 bc_num_zero(c);
432 return BC_STATUS_SUCCESS;
433 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700434 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600435 bc_num_copy(c, aone ? b : a);
436 return BC_STATUS_SUCCESS;
437 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700438
Gavin Howardb5093d72018-10-17 12:13:17 -0600439 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
440 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600441 {
Gavin Howard34c4ba02018-12-11 10:25:01 -0700442 size_t i, j, len;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700443 unsigned int carry;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700444 BcDig *ptr_c;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700445
Gavin Howard798508b2019-02-21 16:45:32 -0700446 bc_num_expand(c, bc_vm_checkSize3(a->len, b->len, 1));
Gavin Howard23d8d252018-10-17 12:03:41 -0600447
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700448 ptr_c = c->num;
449 memset(ptr_c, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700450 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700451
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700452 for (i = 0; !BC_SIGNAL && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600453
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700454 BcDig *ptr = ptr_c + i;
455
Gavin Howard34c4ba02018-12-11 10:25:01 -0700456 carry = 0;
457
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700458 for (j = 0; !BC_SIGNAL && j < a->len; ++j) {
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700459 unsigned int in = (uchar) ptr[j];
460 assert(in < 10);
Gavin Howard2aef61b2018-12-18 10:56:20 -0700461 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700462 carry = bc_num_addDigit(ptr + j, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600463 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700464
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700465 ptr[j] += (BcDig) carry;
466 assert(ptr[j] >= 0 && ptr[j] < 10);
467 len = BC_MAX(len, i + j + (carry != 0));
Gavin Howard63738202018-09-26 15:34:20 -0600468 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700469
Gavin Howard305249a2018-10-15 20:24:47 -0600470 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600471
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700472 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600473 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700474
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600475 bc_num_init(&l1, max);
476 bc_num_init(&h1, max);
477 bc_num_init(&l2, max);
478 bc_num_init(&h2, max);
479 bc_num_init(&m1, max);
480 bc_num_init(&m2, max);
481 bc_num_init(&z0, max);
482 bc_num_init(&z1, max);
483 bc_num_init(&z2, max);
Gavin Howard798508b2019-02-21 16:45:32 -0700484 bc_num_init(&temp, bc_vm_checkSize(max, max));
Gavin Howard0dfe2922018-05-22 13:57:02 -0600485
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600486 bc_num_split(a, max2, &l1, &h1);
487 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700488
Gavin Howard53eba8b2018-10-31 15:14:37 -0600489 s = bc_num_add(&h1, &l1, &m1, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700490 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600491 s = bc_num_add(&h2, &l2, &m2, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700492 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600493
Gavin Howard53eba8b2018-10-31 15:14:37 -0600494 s = bc_num_k(&h1, &h2, &z0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700495 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600496 s = bc_num_k(&m1, &m2, &z1);
Gavin Howard1769abc2019-02-21 09:36:14 -0700497 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600498 s = bc_num_k(&l1, &l2, &z2);
Gavin Howard1769abc2019-02-21 09:36:14 -0700499 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600500
Gavin Howard53eba8b2018-10-31 15:14:37 -0600501 s = bc_num_sub(&z1, &z0, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700502 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600503 s = bc_num_sub(&temp, &z2, &z1, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700504 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600505
Gavin Howard7ad5a662019-02-19 14:40:46 -0700506 bc_num_shift_l(&z0, max2 * 2);
507 bc_num_shift_l(&z1, max2);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600508 s = bc_num_add(&z0, &z1, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700509 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600510 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600511
512err:
513 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600514 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600515 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600516 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600517 bc_num_free(&m2);
Gavin Howard305249a2018-10-15 20:24:47 -0600518 bc_num_free(&m1);
Gavin Howard305249a2018-10-15 20:24:47 -0600519 bc_num_free(&h2);
Gavin Howard305249a2018-10-15 20:24:47 -0600520 bc_num_free(&l2);
Gavin Howard305249a2018-10-15 20:24:47 -0600521 bc_num_free(&h1);
Gavin Howard305249a2018-10-15 20:24:47 -0600522 bc_num_free(&l1);
523 return s;
524}
525
Gavin Howard1cbfe242019-01-09 17:13:11 -0700526static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600527
528 BcStatus s;
529 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600530 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600531
532 scale = BC_MAX(scale, a->rdx);
533 scale = BC_MAX(scale, b->rdx);
534 scale = BC_MIN(a->rdx + b->rdx, scale);
535 maxrdx = BC_MAX(maxrdx, scale);
536
Gavin Howardbaa4f582019-01-24 13:56:35 -0700537 bc_num_createCopy(&cpa, a);
538 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600539
Gavin Howard23d8d252018-10-17 12:03:41 -0600540 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600541
Gavin Howard7ad5a662019-02-19 14:40:46 -0700542 bc_num_shift_l(&cpa, maxrdx);
543 bc_num_shift_l(&cpb, maxrdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600544 s = bc_num_k(&cpa, &cpb, c);
Gavin Howard1769abc2019-02-21 09:36:14 -0700545 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600546
Gavin Howard53eba8b2018-10-31 15:14:37 -0600547 maxrdx += scale;
Gavin Howard798508b2019-02-21 16:45:32 -0700548 bc_num_expand(c, bc_vm_checkSize(c->len, maxrdx));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600549
Gavin Howard88c25302018-10-17 13:32:23 -0600550 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600551 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600552 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600553 }
554
Gavin Howard88c25302018-10-17 13:32:23 -0600555 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600556 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600557
558err:
559 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600560 bc_num_free(&cpa);
561 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700562}
563
Gavin Howard1cbfe242019-01-09 17:13:11 -0700564static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700565
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600566 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600567 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600568 size_t len, end, i;
569 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600570 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700571
Gavin Howardddce6d42019-01-03 14:07:06 -0700572 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
573 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600574 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600575 return BC_STATUS_SUCCESS;
576 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700577 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600578 bc_num_copy(c, a);
579 bc_num_retireMul(c, scale, a->neg, b->neg);
580 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600581 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700582
Gavin Howard620a8af2019-01-24 13:54:54 -0700583 bc_num_init(&cp, bc_num_mulReq(a, b, scale));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600584 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600585 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700586
Gavin Howard890d0c02018-10-30 16:34:50 -0600587 if (len > cp.len) {
Gavin Howard798508b2019-02-21 16:45:32 -0700588 bc_num_expand(&cp, bc_vm_checkSize(len, 2));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600589 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600590 }
Gavin Howard021150b2018-03-10 15:40:42 -0700591
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600592 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600593 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600594 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700595
Gavin Howard63738202018-09-26 15:34:20 -0600596 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600597 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600598 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600599 len -= i - 1;
600 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700601
Gavin Howard798508b2019-02-21 16:45:32 -0700602 if (cp.cap == cp.len) bc_num_expand(&cp, bc_vm_checkSize(cp.len, 1));
Gavin Howardb651f1a2018-02-28 17:34:18 -0700603
Gavin Howard63738202018-09-26 15:34:20 -0600604 // We want an extra zero in front to make things simpler.
605 cp.num[cp.len++] = 0;
606 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700607
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600608 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700609
Gavin Howarda2514a02018-10-18 14:20:09 -0600610 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600611 c->rdx = cp.rdx;
612 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600613 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700614
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700615 for (i = end - 1; !BC_SIGNAL && !s && i < end; --i) {
Gavin Howard63738202018-09-26 15:34:20 -0600616 n = cp.num + i;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700617 for (q = 0; !s && (n[len] || bc_num_compare(n, p, len) >= 0); ++q)
618 s = bc_num_subArrays(n, p, len);
Gavin Howard63738202018-09-26 15:34:20 -0600619 c->num[i] = q;
620 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700621
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600622 if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600623 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600624
Gavin Howard63738202018-09-26 15:34:20 -0600625 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700626}
627
Gavin Howard1cbfe242019-01-09 17:13:11 -0700628static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
629 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600630{
Gavin Howard63738202018-09-26 15:34:20 -0600631 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600632 BcNum temp;
633 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700634
Gavin Howardddce6d42019-01-03 14:07:06 -0700635 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
636 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700637 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600638 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600639 return BC_STATUS_SUCCESS;
640 }
Gavin Howard8b254872018-03-14 01:13:35 -0600641
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600642 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -0700643 s = bc_num_d(a, b, c, scale);
644 assert(!s || s == BC_STATUS_SIGNAL);
645 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700646
Gavin Howard1ab22d22019-01-03 13:32:17 -0700647 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600648
Gavin Howard53eba8b2018-10-31 15:14:37 -0600649 s = bc_num_m(c, b, &temp, scale);
Gavin Howard1769abc2019-02-21 09:36:14 -0700650 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600651 s = bc_num_sub(a, &temp, d, scale);
Gavin Howard1769abc2019-02-21 09:36:14 -0700652 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600653
Gavin Howardddce6d42019-01-03 14:07:06 -0700654 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600655
656 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600657 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600658 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700659
660err:
Gavin Howard18441082018-10-22 10:03:30 -0600661 bc_num_free(&temp);
662 return s;
663}
664
Gavin Howard1cbfe242019-01-09 17:13:11 -0700665static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600666
Gavin Howard18441082018-10-22 10:03:30 -0600667 BcStatus s;
668 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -0700669 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -0600670
Gavin Howard798508b2019-02-21 16:45:32 -0700671 ts = bc_vm_checkSize(scale, b->rdx);
672 ts = BC_MAX(ts, a->rdx);
673
674 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -0600675 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600676 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600677
Gavin Howard63738202018-09-26 15:34:20 -0600678 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700679}
680
Gavin Howard1cbfe242019-01-09 17:13:11 -0700681static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700682
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600683 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600684 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700685 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600686 size_t i, powrdx, resrdx;
687 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700688
Gavin Howardac4d9122018-12-27 23:59:12 -0700689 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700690
Gavin Howardddce6d42019-01-03 14:07:06 -0700691 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600692 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600693 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600694 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700695 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600696 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600697 return BC_STATUS_SUCCESS;
698 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700699 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600700 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600701 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600702 return s;
703 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700704
Gavin Howard63738202018-09-26 15:34:20 -0600705 neg = b->neg;
706 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600707 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700708 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600709 if (s) return s;
710
Gavin Howardbaa4f582019-01-24 13:56:35 -0700711 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700712
Gavin Howard63738202018-09-26 15:34:20 -0600713 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600714
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700715 for (powrdx = a->rdx; !BC_SIGNAL && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600716 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600717 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -0700718 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600719 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700720
Gavin Howard9b801632019-02-16 23:34:06 -0700721 if (BC_SIGNAL) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -0700722
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600723 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700724 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600725
Gavin Howard4d5daba2019-01-07 14:42:50 -0700726 while (!BC_SIGNAL && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600727
Gavin Howard4d5daba2019-01-07 14:42:50 -0700728 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600729 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -0700730 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600731
Gavin Howard63738202018-09-26 15:34:20 -0600732 if (pow & 1) {
733 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600734 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -0700735 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600736 }
737 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700738
Gavin Howard9b801632019-02-16 23:34:06 -0700739 if (BC_SIGNAL) goto sig_err;
740 if (neg) {
741 s = bc_num_inv(c, c, scale);
Gavin Howard1769abc2019-02-21 09:36:14 -0700742 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -0700743 }
744
Gavin Howard63738202018-09-26 15:34:20 -0600745 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700746
Gavin Howard53eba8b2018-10-31 15:14:37 -0600747 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600748 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600749 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700750
Gavin Howard9b801632019-02-16 23:34:06 -0700751sig_err:
752 if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700753err:
Gavin Howard63738202018-09-26 15:34:20 -0600754 bc_num_free(&copy);
755 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700756}
757
Gavin Howard7bda4782018-12-28 09:53:22 -0700758#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700759static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700760
761 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700762 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700763
764 BC_UNUSED(scale);
765
766 s = bc_num_intop(a, b, c, &val);
767 if (s) return s;
768
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700769 if (val < c->rdx) bc_num_truncate(c, c->rdx - val);
770 else if (val > c->rdx) bc_num_extend(c, val - c->rdx);
Gavin Howardac4d9122018-12-27 23:59:12 -0700771
772 return s;
773}
774
Gavin Howard1cbfe242019-01-09 17:13:11 -0700775static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700776
777 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700778 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700779
780 BC_UNUSED(scale);
781
782 s = bc_num_intop(a, b, c, &val);
783 if (s) return s;
784
Gavin Howard7ad5a662019-02-19 14:40:46 -0700785 bc_num_shift_l(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -0700786
787 return s;
788}
789
Gavin Howard1cbfe242019-01-09 17:13:11 -0700790static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -0700791
792 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700793 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700794
795 BC_UNUSED(scale);
796
797 s = bc_num_intop(a, b, c, &val);
798 if (s) return s;
799
Gavin Howard2a366922019-01-16 10:50:20 -0700800 if (BC_NUM_ZERO(c)) return s;
801
Gavin Howard7ad5a662019-02-19 14:40:46 -0700802 bc_num_shift_r(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -0700803
804 return s;
805}
Gavin Howard7bda4782018-12-28 09:53:22 -0700806#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700807
Gavin Howard1cbfe242019-01-09 17:13:11 -0700808static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
809 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700810{
Gavin Howarda1c44392018-09-27 12:41:15 -0600811 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600812 BcNum num2, *ptr_a, *ptr_b;
813 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700814
Gavin Howard63738202018-09-26 15:34:20 -0600815 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700816
Gavin Howard890d0c02018-10-30 16:34:50 -0600817 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600818 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600819 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600820 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600821 }
822 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700823
Gavin Howard63738202018-09-26 15:34:20 -0600824 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600825 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600826 if (c != a) {
827 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600828 init = true;
829 }
830 }
831 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700832
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600833 if (init) bc_num_init(c, req);
834 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700835
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600836 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700837
Gavin Howardddce6d42019-01-03 14:07:06 -0700838 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700839 assert(c->rdx <= c->len || !c->len);
Gavin Howard3cf769e2018-10-11 13:55:11 -0600840
Gavin Howardba009802018-09-29 04:41:51 -0600841 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600842
Gavin Howardee9d01e2019-02-16 22:48:11 -0700843 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700844}
845
Gavin Howardc90ed902019-01-02 13:07:49 -0700846#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -0700847static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700848
Gavin Howardc7d655b2018-12-28 19:45:13 -0700849 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -0600850 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700851
Gavin Howard63738202018-09-26 15:34:20 -0600852 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700853
Gavin Howard63738202018-09-26 15:34:20 -0600854 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700855
Gavin Howard8a921bd2018-10-11 14:15:32 -0600856 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600857
Gavin Howard63738202018-09-26 15:34:20 -0600858 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700859
Gavin Howard63738202018-09-26 15:34:20 -0600860 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700861
Gavin Howard63738202018-09-26 15:34:20 -0600862 radix = true;
863 continue;
864 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700865
Gavin Howardc7d655b2018-12-28 19:45:13 -0700866 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -0600867 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700868
Gavin Howard63738202018-09-26 15:34:20 -0600869 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700870}
Gavin Howardc90ed902019-01-02 13:07:49 -0700871#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -0700872
Gavin Howard1cbfe242019-01-09 17:13:11 -0700873static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -0700874
875 if (isupper(c)) {
876 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -0700877 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -0700878 }
879 else c -= '0';
880
881 return (unsigned long) (uchar) c;
882}
883
Gavin Howard1cbfe242019-01-09 17:13:11 -0700884static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700885
Gavin Howard63738202018-09-26 15:34:20 -0600886 size_t len, i;
887 const char *ptr;
888 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700889
Gavin Howard63738202018-09-26 15:34:20 -0600890 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700891
Gavin Howard63738202018-09-26 15:34:20 -0600892 val += i;
893 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700894
Gavin Howard1ab22d22019-01-03 13:32:17 -0700895 if (len) {
Gavin Howard798508b2019-02-21 16:45:32 -0700896 for (i = 0; zero && i < len; ++i)
897 zero = (val[i] == '0') || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600898 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600899 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600900
Gavin Howard63738202018-09-26 15:34:20 -0600901 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700902
Gavin Howard63738202018-09-26 15:34:20 -0600903 // Explicitly test for NULL here to produce either a 0 or 1.
904 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700905
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600906 if (!zero) {
Gavin Howard8dd307e2019-01-08 23:05:19 -0700907 for (i = len - 1; i < len; ++n->len, --i) {
908
Gavin Howardc7d655b2018-12-28 19:45:13 -0700909 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -0700910
911 if (c == '.') n->len -= 1;
912 else {
913 if (isupper(c)) c = '9';
914 n->num[n->len] = c - '0';
915 }
Gavin Howardc7d655b2018-12-28 19:45:13 -0700916 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600917 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700918}
919
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700920static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
921 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -0700922{
923 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600924 BcNum temp, mult, result;
Gavin Howard1cbfe242019-01-09 17:13:11 -0700925 BcDig c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600926 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600927 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -0700928 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700929
Gavin Howard11b9afd2018-10-18 14:23:28 -0600930 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700931 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700932
Gavin Howard26d078f2019-01-24 13:58:12 -0700933 bc_num_init(&temp, BC_NUM_LONG_LOG10);
934 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -0700935
Gavin Howardc7d655b2018-12-28 19:45:13 -0700936 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -0700937
Gavin Howardc7d655b2018-12-28 19:45:13 -0700938 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -0700939
Gavin Howard53eba8b2018-10-31 15:14:37 -0600940 s = bc_num_mul(n, base, &mult, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700941 if (BC_STATUS_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700942 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600943 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700944 if (BC_STATUS_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600945 }
Gavin Howardede51f02018-03-02 12:30:00 -0700946
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700947 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600948
Gavin Howard63738202018-09-26 15:34:20 -0600949 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600950 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -0600951 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700952
Gavin Howard1769abc2019-02-21 09:36:14 -0700953 for (i += 1, digs = 0; !BC_SIGNAL && i < len && (c = val[i]); ++i, ++digs) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700954
Gavin Howardc7d655b2018-12-28 19:45:13 -0700955 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -0700956
Gavin Howard53eba8b2018-10-31 15:14:37 -0600957 s = bc_num_mul(&result, base, &result, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700958 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700959
Gavin Howard705c4bc2018-12-03 15:38:04 -0700960 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600961 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700962 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600963 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -0700964 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600965 }
Gavin Howardede51f02018-03-02 12:30:00 -0700966
Gavin Howard1769abc2019-02-21 09:36:14 -0700967 // This one cannot be a divide by 0 because mult starts out at 1, then is
968 // multiplied by base, and base cannot be 0, so mult cannot be 0.
969 s = bc_num_div(&result, &mult, &result, digs);
970 assert(!s || s == BC_STATUS_SIGNAL);
971 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
972 s = bc_num_add(n, &result, n, digs);
973 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -0700974
Gavin Howardddce6d42019-01-03 14:07:06 -0700975 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -0700976 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600977 }
978 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700979
Gavin Howardd1417682019-02-15 17:08:42 -0700980err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700981 bc_num_free(&result);
982int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600983 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -0600984 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700985 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700986}
987
Gavin Howard1cbfe242019-01-09 17:13:11 -0700988static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -0700989 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -0600990 bc_vm_putchar('\\');
991 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -0700992 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600993 }
Gavin Howard80977b22018-09-06 14:29:13 -0600994}
995
Gavin Howard40a085f2018-12-03 12:08:59 -0700996#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -0700997static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -0700998 BC_UNUSED(rdx);
999 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001000 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001001}
1002#endif // DC_ENABLED
1003
Gavin Howard1cbfe242019-01-09 17:13:11 -07001004static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1005
Gavin Howarda84ad992018-12-03 19:11:06 -07001006 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001007
Gavin Howardab5e9632019-01-02 13:32:02 -07001008 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001009 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001010 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001011
Gavin Howardab5e9632019-01-02 13:32:02 -07001012 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001013 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001014
Gavin Howardab5e9632019-01-02 13:32:02 -07001015 for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001016 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001017 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001018 dig = n / pow;
1019 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001020 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001021 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001022}
Gavin Howardfe679f02018-02-14 15:50:09 -07001023
Gavin Howard1cbfe242019-01-09 17:13:11 -07001024static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001025
Gavin Howard3c02ed32018-12-28 18:17:15 -07001026 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001027
Gavin Howard3c02ed32018-12-28 18:17:15 -07001028 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001029 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001030 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001031 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001032 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001033
Gavin Howardab5e9632019-01-02 13:32:02 -07001034 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001035 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001036 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001037}
1038
Gavin Howard1cbfe242019-01-09 17:13:11 -07001039static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001040
Gavin Howard63738202018-09-26 15:34:20 -06001041 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001042
Gavin Howard48af52e2018-10-30 14:47:38 -06001043 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001044 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001045
Gavin Howard48af52e2018-10-30 14:47:38 -06001046 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001047 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001048}
1049
Gavin Howard7ad5a662019-02-19 14:40:46 -07001050#if BC_ENABLE_EXTRA_MATH
1051static void bc_num_printExponent(const BcNum *restrict n, bool eng) {
1052
1053 bool neg = (n->len <= n->rdx);
1054 BcNum temp, exp;
1055 size_t places, mod;
1056 BcDig digs[BC_NUM_LONG_LOG10];
1057
1058 bc_num_createCopy(&temp, n);
1059
1060 if (neg) {
1061 places = n->rdx - bc_num_len(n) + 1;
1062 mod = places % 3;
1063 if (eng && mod != 0) places += 3 - mod;
1064 bc_num_shift_l(&temp, places);
1065 }
1066 else {
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001067 places = bc_num_int(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001068 mod = places % 3;
1069 if (eng && mod != 0) places -= 3 - (3 - mod);
1070 bc_num_shift_r(&temp, places);
1071 }
1072
1073 bc_num_printDecimal(&temp);
1074
1075 bc_num_printNewline();
1076 bc_vm_putchar('e');
1077
1078 if (!places) {
1079 bc_num_printHex(0, 1, false);
1080 goto exit;
1081 }
1082
1083 if (neg) {
1084 bc_num_printNewline();
1085 bc_vm_putchar('-');
1086 }
1087
1088 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1089 bc_num_ulong2num(&exp, (unsigned long) places);
1090
1091 bc_num_printDecimal(&exp);
1092
1093exit:
1094 bc_num_free(&temp);
1095}
1096#endif // BC_ENABLE_EXTRA_MATH
1097
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001098static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1099 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001100{
Gavin Howard63738202018-09-26 15:34:20 -06001101 BcStatus s;
1102 BcVec stack;
1103 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001104 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001105 size_t i;
1106 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001107
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001108 assert(!BC_NUM_ZERO(base));
1109
Gavin Howardddce6d42019-01-03 14:07:06 -07001110 if (BC_NUM_ZERO(n)) {
1111 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001112 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001113 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001114
Gavin Howardd3929832018-12-24 15:13:15 -07001115 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001116 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001117 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001118 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001119 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001120 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001121
Gavin Howard63738202018-09-26 15:34:20 -06001122 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001123 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001124 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001125
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001126 while (!BC_SIGNAL && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001127
1128 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001129 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001130 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1131
1132 // Will never fail (except for signals) because digit is
1133 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001134 s = bc_num_ulong(&digit, &dig);
Gavin Howard1769abc2019-02-21 09:36:14 -07001135 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1136
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001137 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001138 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001139
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001140 if (BC_SIGNAL) goto sig_err;
1141
1142 for (i = 0; !BC_SIGNAL && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001143 ptr = bc_vec_item_rev(&stack, i);
1144 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001145 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001146 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001147
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001148 if (BC_SIGNAL) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001149 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001150
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001151 for (radix = true; !BC_SIGNAL && frac_len.len <= n->rdx; radix = false) {
1152
Gavin Howard53eba8b2018-10-31 15:14:37 -06001153 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001154 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1155
1156 // Will never fail (except for signals) because fracp is
1157 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001158 s = bc_num_ulong(&fracp, &dig);
Gavin Howard1769abc2019-02-21 09:36:14 -07001159 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001160
Gavin Howard705c4bc2018-12-03 15:38:04 -07001161 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001162 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001163 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001164
Gavin Howardddce6d42019-01-03 14:07:06 -07001165 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001166 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001167 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001168 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001169
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001170sig_err:
Gavin Howard9b801632019-02-16 23:34:06 -07001171 if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001172err:
Gavin Howard63738202018-09-26 15:34:20 -06001173 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001174 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001175 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001176 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001177 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001178 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001179}
1180
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001181static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1182 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001183{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001184 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001185 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001186 BcNumDigitOp print;
1187 bool neg = n->neg;
1188
Gavin Howard48af52e2018-10-30 14:47:38 -06001189 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001190 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001191
1192 n->neg = false;
1193
Gavin Howardbe4e8222019-01-03 13:34:16 -07001194 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001195 width = 1;
1196 print = bc_num_printHex;
1197 }
1198 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001199 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001200 print = bc_num_printDigits;
1201 }
1202
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001203 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001204 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001205
1206 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001207}
1208
Gavin Howard40a085f2018-12-03 12:08:59 -07001209#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001210BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1211 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001212}
1213#endif // DC_ENABLED
1214
Gavin Howard3c02ed32018-12-28 18:17:15 -07001215void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001216 assert(n);
1217 n->num = num;
1218 n->cap = cap;
1219 n->rdx = n->len = 0;
1220 n->neg = false;
1221}
1222
Gavin Howard3c02ed32018-12-28 18:17:15 -07001223void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001224 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001225 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001226 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001227}
1228
Gavin Howarded392aa2018-02-27 13:09:26 -07001229void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001230 assert(num);
1231 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001232}
1233
Gavin Howard3c02ed32018-12-28 18:17:15 -07001234void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001235 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001236 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001237 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001238 d->len = s->len;
1239 d->neg = s->neg;
1240 d->rdx = s->rdx;
1241 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001242}
1243
Gavin Howardbaa4f582019-01-24 13:56:35 -07001244void bc_num_createCopy(BcNum *d, const BcNum *s) {
1245 bc_num_init(d, s->len);
1246 bc_num_copy(d, s);
1247}
1248
1249void bc_num_createFromUlong(BcNum *n, unsigned long val) {
1250 bc_num_init(n, BC_NUM_LONG_LOG10);
1251 bc_num_ulong2num(n, val);
1252}
1253
Gavin Howard7ad5a662019-02-19 14:40:46 -07001254size_t bc_num_scale(const BcNum *restrict n) {
1255 return n->rdx;
1256}
1257
1258size_t bc_num_len(const BcNum *restrict n) {
1259
1260 size_t i, len = n->len;
1261
1262 if (n->rdx != n->len) return len;
1263 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1264
1265 return len;
1266}
1267
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001268BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1269 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001270{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001271 BcStatus s = BC_STATUS_SUCCESS;
1272
Gavin Howard63738202018-09-26 15:34:20 -06001273 assert(n && val && base);
Gavin Howard96edeb32018-12-31 09:33:22 -07001274 assert(base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase);
Gavin Howardc90ed902019-01-02 13:07:49 -07001275 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001276
Gavin Howardd746e802018-12-28 22:06:00 -07001277 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howardc7d655b2018-12-28 19:45:13 -07001278 else if (base_t == 10) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001279 else s = bc_num_parseBase(n, val, base, base_t);
1280
1281 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001282}
1283
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001284BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1285 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001286{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001287 BcStatus s = BC_STATUS_SUCCESS;
1288
Gavin Howardab5e9632019-01-02 13:32:02 -07001289 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001290 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001291
Gavin Howardab5e9632019-01-02 13:32:02 -07001292 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001293
Gavin Howardddce6d42019-01-03 14:07:06 -07001294 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howardab5e9632019-01-02 13:32:02 -07001295 else if (base_t == 10) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001296#if BC_ENABLE_EXTRA_MATH
1297 else if (base_t == 0 || base_t == 1) bc_num_printExponent(n, base_t != 0);
1298#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001299 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001300
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001301 if (!s && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001302 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001303 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001304 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001305
1306 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001307}
1308
Gavin Howard3c02ed32018-12-28 18:17:15 -07001309BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001310
Gavin Howard63738202018-09-26 15:34:20 -06001311 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001312 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001313
Gavin Howard63738202018-09-26 15:34:20 -06001314 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001315
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001316 *result = 0;
1317
Gavin Howard7536dcf2018-12-15 19:27:09 -07001318 if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001319
Gavin Howard06b6e862019-02-15 11:15:31 -07001320 for (r = 0, i = n->len; i > n->rdx;) {
1321 unsigned long prev = r;
1322 r = r * 10 + ((uchar) n->num[--i]);
Gavin Howard5edf8492019-02-21 14:06:58 -07001323 if (r == SIZE_MAX || (r / 8) < prev)
1324 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001325 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001326
Gavin Howard7b557da2018-12-21 10:25:32 -07001327 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001328
Gavin Howard63738202018-09-26 15:34:20 -06001329 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001330}
1331
Gavin Howard3c02ed32018-12-28 18:17:15 -07001332void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001333
Gavin Howard8a921bd2018-10-11 14:15:32 -06001334 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001335 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001336
Gavin Howard63738202018-09-26 15:34:20 -06001337 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001338
Gavin Howard63738202018-09-26 15:34:20 -06001339 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001340
Gavin Howard1ab22d22019-01-03 13:32:17 -07001341 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001342
Gavin Howard798508b2019-02-21 16:45:32 -07001343 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howardec82bdc2018-10-08 23:50:57 -06001344 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001345}
1346
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001347size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001348
1349 size_t aint, bint, ardx, brdx;
1350
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001351 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001352
1353 ardx = a->rdx;
1354 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001355 aint = bc_num_int(a);
1356 bint = bc_num_int(b);
Gavin Howard1973d612019-02-21 15:37:32 -07001357
Gavin Howard798508b2019-02-21 16:45:32 -07001358 return bc_vm_checkSize3(BC_MAX(ardx, brdx), BC_MAX(aint, bint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001359}
1360
1361size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard798508b2019-02-21 16:45:32 -07001362 size_t rdx = bc_vm_checkSize(a->rdx, b->rdx);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001363 rdx = bc_vm_checkSize3(bc_num_int(a), bc_num_int(b), BC_MAX(scale, rdx));
Gavin Howard798508b2019-02-21 16:45:32 -07001364 return rdx + 1;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001365}
1366
1367size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1368 BC_UNUSED(scale);
1369 return BC_NUM_PREQ(a, b);
1370}
1371
1372#if BC_ENABLE_EXTRA_MATH
1373size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1374 BC_UNUSED(b);
1375 BC_UNUSED(scale);
1376 return BC_NUM_SHREQ(a);
1377}
1378#endif // BC_ENABLE_EXTRA_MATH
1379
Gavin Howard12fe7812018-09-29 03:45:18 -06001380BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001381 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001382 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001383 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001384}
1385
Gavin Howard12fe7812018-09-29 03:45:18 -06001386BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001387 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001388 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001389 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001390}
1391
Gavin Howard12fe7812018-09-29 03:45:18 -06001392BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001393 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001394}
1395
Gavin Howard12fe7812018-09-29 03:45:18 -06001396BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001397 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001398}
1399
Gavin Howard54b946a2018-10-23 12:06:57 -06001400BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001401 return bc_num_binary(a, b, c, scale, bc_num_rem, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001402}
1403
Gavin Howard12fe7812018-09-29 03:45:18 -06001404BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001405 return bc_num_binary(a, b, c, scale, bc_num_p, BC_NUM_PREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001406}
1407
Gavin Howard7d8f0382018-12-27 16:15:13 -07001408#if BC_ENABLE_EXTRA_MATH
1409BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001410 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001411}
1412
1413BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001414 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001415}
1416
1417BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001418 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001419}
1420#endif // BC_ENABLE_EXTRA_MATH
1421
Gavin Howard3c02ed32018-12-28 18:17:15 -07001422BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001423
Gavin Howard1cbfe242019-01-09 17:13:11 -07001424 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001425 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard283c4eb2019-01-24 16:36:46 -07001426 size_t pow, len, digs, digs1, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001427 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001428 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001429
Gavin Howard34edd0a2018-10-25 15:51:05 -06001430 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001431
Gavin Howard07fbf012019-02-19 09:25:11 -07001432 if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1433
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001434 len = bc_vm_checkSize(bc_num_int(a), 1);
Gavin Howard798508b2019-02-21 16:45:32 -07001435 bc_num_init(b, bc_vm_checkSize3(BC_MAX(scale, a->rdx), len >> 1, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001436
Gavin Howardddce6d42019-01-03 14:07:06 -07001437 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001438 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001439 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001440 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001441 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001442 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001443 bc_num_extend(b, scale);
1444 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001445 }
Gavin Howard954276b2018-05-16 01:43:58 -06001446
Gavin Howard890d0c02018-10-30 16:34:50 -06001447 scale = BC_MAX(scale, a->rdx) + 1;
Gavin Howard798508b2019-02-21 16:45:32 -07001448 len = bc_vm_checkSize(a->len, scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001449
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001450 bc_num_init(&num1, len);
1451 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001452 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001453
Gavin Howard63738202018-09-26 15:34:20 -06001454 bc_num_one(&half);
1455 half.num[0] = 5;
1456 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001457
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001458 bc_num_init(&f, len);
1459 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001460
Gavin Howard63738202018-09-26 15:34:20 -06001461 x0 = &num1;
1462 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001463
Gavin Howard63738202018-09-26 15:34:20 -06001464 bc_num_one(x0);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001465 pow = bc_num_int(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001466
Gavin Howard53eba8b2018-10-31 15:14:37 -06001467 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001468
Gavin Howardc39fd492018-10-04 10:07:03 -06001469 if (pow & 1) x0->num[0] = 2;
1470 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001471
Gavin Howardc39fd492018-10-04 10:07:03 -06001472 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001473
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001474 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001475
Gavin Howard63738202018-09-26 15:34:20 -06001476 // Make sure to move the radix back.
1477 x0->rdx -= pow;
1478 }
Gavin Howard954276b2018-05-16 01:43:58 -06001479
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001480 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001481 resrdx = scale + 2;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001482 len = bc_num_int(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001483
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001484 while (!BC_SIGNAL && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001485
Gavin Howard1769abc2019-02-21 09:36:14 -07001486 assert(!BC_NUM_ZERO(x0));
1487
Gavin Howard53eba8b2018-10-31 15:14:37 -06001488 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001489 assert(!s || s == BC_STATUS_SIGNAL);
1490 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001491 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001492 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001493 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001494 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001495
Gavin Howard53eba8b2018-10-31 15:14:37 -06001496 cmp = bc_num_cmp(x1, x0);
1497 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001498
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001499 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001500 else times = 0;
1501
1502 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001503
1504 cmp2 = cmp1;
1505 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001506 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001507
Gavin Howard63738202018-09-26 15:34:20 -06001508 temp = x0;
1509 x0 = x1;
1510 x1 = temp;
1511 }
Gavin Howard954276b2018-05-16 01:43:58 -06001512
Gavin Howard176cfe62019-02-16 23:40:13 -07001513 if (BC_SIGNAL) {
1514 s = BC_STATUS_SIGNAL;
1515 goto err;
1516 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001517
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001518 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001519 scale -= 1;
1520 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001521
1522err:
Gavin Howard63738202018-09-26 15:34:20 -06001523 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001524 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001525 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001526 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001527 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001528 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001529 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001530}
Gavin Howardba009802018-09-29 04:41:51 -06001531
Gavin Howard2cb39612018-10-22 08:46:48 -06001532BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1533
1534 BcStatus s;
1535 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001536 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07001537 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001538
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001539 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001540
Gavin Howard890d0c02018-10-30 16:34:50 -06001541 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001542 memcpy(&num2, c, sizeof(BcNum));
1543 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001544 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001545 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001546 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001547 else {
1548 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001549 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001550 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001551
Gavin Howard54b946a2018-10-23 12:06:57 -06001552 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001553
Gavin Howardddce6d42019-01-03 14:07:06 -07001554 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001555 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001556 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001557 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001558
1559 if (init) bc_num_free(&num2);
1560
1561 return s;
1562}
1563
Gavin Howard40a085f2018-12-03 12:08:59 -07001564#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001565BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001566
1567 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001568 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001569 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001570
Gavin Howard34edd0a2018-10-25 15:51:05 -06001571 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001572
Gavin Howardddce6d42019-01-03 14:07:06 -07001573 if (BC_NUM_ZERO(c)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard7536dcf2018-12-15 19:27:09 -07001574 if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1575 if (a->rdx || b->rdx || c->rdx)
1576 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001577
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001578 bc_num_expand(d, c->len);
1579 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001580 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001581 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001582
1583 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001584 two.num[0] = 2;
1585 bc_num_one(d);
1586
Gavin Howard1769abc2019-02-21 09:36:14 -07001587 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001588 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001589 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardd88164d2019-02-21 09:57:31 -07001590 if (BC_STATUS_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07001591 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001592
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001593 while (!BC_SIGNAL && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001594
Gavin Howard1769abc2019-02-21 09:36:14 -07001595 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001596 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001597 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001598
Gavin Howarda83a5e52019-02-21 16:51:06 -07001599 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001600 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001601 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1602
1603 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001604 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001605 assert(!s || s == BC_STATUS_SIGNAL);
1606 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001607 }
1608
Gavin Howard53eba8b2018-10-31 15:14:37 -06001609 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001610 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
1611
1612 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001613 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001614 assert(!s || s == BC_STATUS_SIGNAL);
1615 if (BC_STATUS_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001616 }
1617
Gavin Howardfa4983a2019-02-16 23:43:03 -07001618 if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL;
1619
Gavin Howardba009802018-09-29 04:41:51 -06001620err:
Gavin Howardba009802018-09-29 04:41:51 -06001621 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07001622rem_err:
1623 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001624 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001625 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001626 return s;
1627}
Gavin Howarde6e84762018-10-03 11:46:34 -06001628#endif // DC_ENABLED