blob: 528fee9bd1d4193ee4803704736b401a15cafa24 [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 Howard3c02ed32018-12-28 18:17:15 -070035void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060036 assert(n);
Gavin Howard97102b12018-11-01 23:37:31 -060037 n->len = 0;
38 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060039 n->rdx = scale;
40}
41
Gavin Howard3c02ed32018-12-28 18:17:15 -070042void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060043 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060044}
45
Gavin Howard3c02ed32018-12-28 18:17:15 -070046void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060047 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060048 n->len = 1;
49 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060050}
51
Gavin Howard3c02ed32018-12-28 18:17:15 -070052void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060053 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060054 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060055 n->len = 2;
56 n->num[0] = 0;
57 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060058}
59
Gavin Howard1ab22d22019-01-03 13:32:17 -070060size_t bc_num_log10(size_t i) {
61 size_t len;
62 for (len = 1; i; i /= 10, ++len);
63 return len;
64}
65
Gavin Howard3c02ed32018-12-28 18:17:15 -070066BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b, size_t len)
67{
Gavin Howard63738202018-09-26 15:34:20 -060068 size_t i, j;
Gavin Howard5dbb48b2019-01-04 11:29:29 -070069 for (i = 0; !BC_SIGNAL && i < len; ++i) {
70 for (a[i] -= b[i], j = 0; !BC_SIGNAL && a[i + j] < 0;) {
Gavin Howard54b946a2018-10-23 12:06:57 -060071 a[i + j++] += 10;
72 a[i + j] -= 1;
Gavin Howard231e66c2019-01-08 23:02:19 -070073 assert(a[i + j - 1] >= 0 && a[i + j - 1] < 10);
Gavin Howard63738202018-09-26 15:34:20 -060074 }
75 }
Gavin Howard5dbb48b2019-01-04 11:29:29 -070076 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060077}
78
Gavin Howard3c02ed32018-12-28 18:17:15 -070079ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
80 size_t len)
81{
Gavin Howard63738202018-09-26 15:34:20 -060082 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -060083 int c = 0;
Gavin Howard5dbb48b2019-01-04 11:29:29 -070084 for (i = len - 1; !BC_SIGNAL && i < len && !(c = a[i] - b[i]); --i);
Gavin Howard7fbb4e22018-10-18 11:43:17 -060085 return BC_NUM_NEG(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -060086}
87
Gavin Howard3c02ed32018-12-28 18:17:15 -070088ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070089
Gavin Howard63738202018-09-26 15:34:20 -060090 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -060091 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060092 bool a_max, neg = false;
93 ssize_t cmp;
94
95 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070096
Gavin Howard8a921bd2018-10-11 14:15:32 -060097 if (a == b) return 0;
Gavin Howardddce6d42019-01-03 14:07:06 -070098 if (BC_NUM_ZERO(a)) return BC_NUM_NEG(!!b->len, !b->neg);
99 if (BC_NUM_ZERO(b)) return BC_NUM_CMP_ZERO(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600100 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600101 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600102 else return -1;
103 }
104 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700105
Gavin Howard63738202018-09-26 15:34:20 -0600106 a_int = BC_NUM_INT(a);
107 b_int = BC_NUM_INT(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600108 a_int -= b_int;
109 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700110
Gavin Howard1ab22d22019-01-03 13:32:17 -0700111 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700112
Gavin Howard890d0c02018-10-30 16:34:50 -0600113 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600114 min = b->rdx;
115 diff = a->rdx - b->rdx;
116 max_num = a->num + diff;
117 min_num = b->num;
118 }
119 else {
120 min = a->rdx;
121 diff = b->rdx - a->rdx;
122 max_num = b->num + diff;
123 min_num = a->num;
124 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700125
Gavin Howard890d0c02018-10-30 16:34:50 -0600126 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700127 if (cmp) return BC_NUM_NEG(cmp, (!a_max) != neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700128
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700129 for (max_num -= diff, i = diff - 1; !BC_SIGNAL && i < diff; --i) {
Gavin Howard40974152018-10-18 14:23:01 -0600130 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
Gavin Howard63738202018-09-26 15:34:20 -0600131 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700132
Gavin Howard63738202018-09-26 15:34:20 -0600133 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700134}
135
Gavin Howard3c02ed32018-12-28 18:17:15 -0700136void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700137
Gavin Howardddce6d42019-01-03 14:07:06 -0700138 assert(places <= n->rdx && (BC_NUM_ZERO(n) || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700139
Gavin Howard1ab22d22019-01-03 13:32:17 -0700140 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700141
Gavin Howard63738202018-09-26 15:34:20 -0600142 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700143
Gavin Howardddce6d42019-01-03 14:07:06 -0700144 if (BC_NUM_NONZERO(n)) {
Gavin Howard955da852018-10-23 11:08:20 -0600145 n->len -= places;
146 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
147 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700148}
149
Gavin Howard3c02ed32018-12-28 18:17:15 -0700150void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700151
Gavin Howard88c25302018-10-17 13:32:23 -0600152 size_t len = n->len + places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700153
Gavin Howard1ab22d22019-01-03 13:32:17 -0700154 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700155
Gavin Howard1ab22d22019-01-03 13:32:17 -0700156 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700157
Gavin Howard1ab22d22019-01-03 13:32:17 -0700158 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
159 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700160
Gavin Howard1ab22d22019-01-03 13:32:17 -0700161 n->len += places;
162 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700163}
164
Gavin Howard3c02ed32018-12-28 18:17:15 -0700165void bc_num_clean(BcNum *restrict n) {
Gavin Howardddce6d42019-01-03 14:07:06 -0700166 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
167 if (BC_NUM_ZERO(n)) n->neg = false;
Gavin Howard63738202018-09-26 15:34:20 -0600168 else if (n->len < n->rdx) n->len = n->rdx;
Gavin Howarde57525d2018-09-01 16:56:21 -0600169}
170
Gavin Howard3c02ed32018-12-28 18:17:15 -0700171void bc_num_retireMul(BcNum *restrict n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600172
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600173 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600174 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600175
Gavin Howard305249a2018-10-15 20:24:47 -0600176 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700177 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600178}
179
Gavin Howard83c850d2019-01-08 23:04:51 -0700180void bc_num_split(const BcNum *restrict n, size_t idx, BcNum *restrict a, BcNum *restrict b) {
Gavin Howard50c80022019-01-03 15:14:33 -0700181
Gavin Howard305249a2018-10-15 20:24:47 -0600182 if (idx < n->len) {
183
Gavin Howard305249a2018-10-15 20:24:47 -0600184 b->len = n->len - idx;
185 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600186 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600187
188 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
189 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard50c80022019-01-03 15:14:33 -0700190
191 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600192 }
Gavin Howard50c80022019-01-03 15:14:33 -0700193 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600194
195 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600196}
197
Gavin Howard3c02ed32018-12-28 18:17:15 -0700198BcStatus bc_num_shift(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600199
Gavin Howardddce6d42019-01-03 14:07:06 -0700200 if (!places || BC_NUM_ZERO(n)) return BC_STATUS_SUCCESS;
Gavin Howardecc7fc22018-12-31 13:21:47 -0700201 if (places + n->len > BC_MAX_NUM)
Gavin Howard92037352019-01-02 14:58:35 -0700202 return bc_vm_verr(BC_ERROR_MATH_OVERFLOW, "shifted left too far");
Gavin Howard305249a2018-10-15 20:24:47 -0600203
204 if (n->rdx >= places) n->rdx -= places;
205 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600206 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600207 n->rdx = 0;
208 }
209
210 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600211
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600212 return BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600213}
214
Gavin Howard773c86b2018-11-02 14:07:19 -0600215BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600216
Gavin Howard63738202018-09-26 15:34:20 -0600217 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600218 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600219
Gavin Howardd32d7df2018-10-15 08:42:25 -0600220 one.cap = 2;
221 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600222 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600223
Gavin Howardd32d7df2018-10-15 08:42:25 -0600224 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600225}
226
Gavin Howard7bda4782018-12-28 09:53:22 -0700227#if BC_ENABLE_EXTRA_MATH
Gavin Howard3c02ed32018-12-28 18:17:15 -0700228BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
229 unsigned long *v)
230{
Gavin Howardac4d9122018-12-27 23:59:12 -0700231 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
232 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700233 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700234}
Gavin Howard7bda4782018-12-28 09:53:22 -0700235#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700236
Gavin Howard773c86b2018-11-02 14:07:19 -0600237BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700238
Gavin Howard8a921bd2018-10-11 14:15:32 -0600239 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600240 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700241 unsigned int carry, in;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700242
Gavin Howard63738202018-09-26 15:34:20 -0600243 // Because this function doesn't need to use scale (per the bc spec),
244 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600245
Gavin Howardddce6d42019-01-03 14:07:06 -0700246 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600247 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700248 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600249 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600250 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700251 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600252 bc_num_copy(c, a);
253 return BC_STATUS_SUCCESS;
254 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600255
Gavin Howard63738202018-09-26 15:34:20 -0600256 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600257 c->rdx = BC_MAX(a->rdx, b->rdx);
258 min_rdx = BC_MIN(a->rdx, b->rdx);
259 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700260
Gavin Howard63738202018-09-26 15:34:20 -0600261 if (a->rdx > b->rdx) {
262 diff = a->rdx - b->rdx;
263 ptr = a->num;
264 ptr_a = a->num + diff;
265 ptr_b = b->num;
266 }
267 else {
268 diff = b->rdx - a->rdx;
269 ptr = b->num;
270 ptr_a = a->num;
271 ptr_b = b->num + diff;
272 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700273
Gavin Howard63738202018-09-26 15:34:20 -0600274 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700275
Gavin Howard63738202018-09-26 15:34:20 -0600276 ptr_c += diff;
Gavin Howard890d0c02018-10-30 16:34:50 -0600277 a_int = BC_NUM_INT(a);
278 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700279
Gavin Howard890d0c02018-10-30 16:34:50 -0600280 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600281 min_int = b_int;
282 max = a_int;
283 ptr = ptr_a;
284 }
285 else {
286 min_int = a_int;
287 max = b_int;
288 ptr = ptr_b;
289 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700290
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700291 for (carry = 0, i = 0; !BC_SIGNAL && i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700292 in = ((unsigned int) ptr_a[i]) + ((unsigned int) ptr_b[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600293 carry = in / 10;
Gavin Howard231e66c2019-01-08 23:02:19 -0700294 assert(carry >= 0 && carry < 10);
Gavin Howard11b9afd2018-10-18 14:23:28 -0600295 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard231e66c2019-01-08 23:02:19 -0700296 assert(ptr_c[i] >= 0 && ptr_c[i] < 10);
Gavin Howard63738202018-09-26 15:34:20 -0600297 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700298
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700299 for (; !BC_SIGNAL && i < max + min_rdx; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700300 in = ((unsigned int) ptr[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600301 carry = in / 10;
Gavin Howard231e66c2019-01-08 23:02:19 -0700302 assert(carry >= 0 && carry < 10);
Gavin Howard11b9afd2018-10-18 14:23:28 -0600303 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard231e66c2019-01-08 23:02:19 -0700304 assert(ptr_c[i] >= 0 && ptr_c[i] < 10);
Gavin Howard63738202018-09-26 15:34:20 -0600305 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700306
Gavin Howard1ab22d22019-01-03 13:32:17 -0700307 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600308
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700309 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700310}
311
Gavin Howard773c86b2018-11-02 14:07:19 -0600312BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700313
Gavin Howard63738202018-09-26 15:34:20 -0600314 BcStatus s;
315 ssize_t cmp;
316 BcNum *minuend, *subtrahend;
317 size_t start;
318 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700319
Gavin Howard63738202018-09-26 15:34:20 -0600320 // Because this function doesn't need to use scale (per the bc spec),
321 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700322
Gavin Howardddce6d42019-01-03 14:07:06 -0700323 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600324 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700325 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600326 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600327 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700328 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600329 bc_num_copy(c, a);
330 return BC_STATUS_SUCCESS;
331 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700332
Gavin Howard63738202018-09-26 15:34:20 -0600333 aneg = a->neg;
334 bneg = b->neg;
335 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700336
Gavin Howard63738202018-09-26 15:34:20 -0600337 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700338
Gavin Howard63738202018-09-26 15:34:20 -0600339 a->neg = aneg;
340 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700341
Gavin Howard1ab22d22019-01-03 13:32:17 -0700342 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600343 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600344 return BC_STATUS_SUCCESS;
345 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700346
347 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600348 neg = a->neg;
349 minuend = a;
350 subtrahend = b;
351 }
352 else {
353 neg = b->neg;
354 if (sub) neg = !neg;
355 minuend = b;
356 subtrahend = a;
357 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700358
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600359 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600360 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700361
Gavin Howard63738202018-09-26 15:34:20 -0600362 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600363 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600364 start = 0;
365 }
366 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700367
Gavin Howard63738202018-09-26 15:34:20 -0600368 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700369
Gavin Howard63738202018-09-26 15:34:20 -0600370 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700371
Gavin Howard63738202018-09-26 15:34:20 -0600372 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700373}
374
Gavin Howard83c850d2019-01-08 23:04:51 -0700375BcStatus bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600376
Gavin Howard63738202018-09-26 15:34:20 -0600377 BcStatus s;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700378 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600379 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard63738202018-09-26 15:34:20 -0600380 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600381
Gavin Howardddce6d42019-01-03 14:07:06 -0700382 // This is here because the function is recursive.
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700383 if (BC_SIGNAL) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700384 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600385 bc_num_zero(c);
386 return BC_STATUS_SUCCESS;
387 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700388 if (aone || BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600389 bc_num_copy(c, aone ? b : a);
390 return BC_STATUS_SUCCESS;
391 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700392
Gavin Howardb5093d72018-10-17 12:13:17 -0600393 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
394 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600395 {
Gavin Howard34c4ba02018-12-11 10:25:01 -0700396 size_t i, j, len;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700397 unsigned int carry;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700398
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600399 bc_num_expand(c, a->len + b->len + 1);
Gavin Howard23d8d252018-10-17 12:03:41 -0600400
Gavin Howard305249a2018-10-15 20:24:47 -0600401 memset(c->num, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700402 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700403
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700404 for (i = 0; !BC_SIGNAL && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600405
Gavin Howard34c4ba02018-12-11 10:25:01 -0700406 carry = 0;
407
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700408 for (j = 0; !BC_SIGNAL && j < a->len; ++j) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700409 unsigned int in = c->num[i + j];
410 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
411 in += carry;
Gavin Howard305249a2018-10-15 20:24:47 -0600412 carry = in / 10;
Gavin Howard231e66c2019-01-08 23:02:19 -0700413 assert(carry >= 0 && carry < 10);
Gavin Howard11b9afd2018-10-18 14:23:28 -0600414 c->num[i + j] = (BcDig) (in % 10);
Gavin Howard231e66c2019-01-08 23:02:19 -0700415 assert(c->num[i + j] >= 0 && c->num[i + j] < 10);
Gavin Howard305249a2018-10-15 20:24:47 -0600416 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700417
Gavin Howard88c25302018-10-17 13:32:23 -0600418 c->num[i + j] += (BcDig) carry;
Gavin Howard231e66c2019-01-08 23:02:19 -0700419 assert(c->num[i + j] >= 0 && c->num[i + j] < 10);
Gavin Howard88c25302018-10-17 13:32:23 -0600420 len = BC_MAX(len, i + j + !!carry);
Gavin Howard63738202018-09-26 15:34:20 -0600421 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700422
Gavin Howard305249a2018-10-15 20:24:47 -0600423 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600424
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700425 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600426 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700427
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600428 bc_num_init(&l1, max);
429 bc_num_init(&h1, max);
430 bc_num_init(&l2, max);
431 bc_num_init(&h2, max);
432 bc_num_init(&m1, max);
433 bc_num_init(&m2, max);
434 bc_num_init(&z0, max);
435 bc_num_init(&z1, max);
436 bc_num_init(&z2, max);
437 bc_num_init(&temp, max + max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600438
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600439 bc_num_split(a, max2, &l1, &h1);
440 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700441
Gavin Howard53eba8b2018-10-31 15:14:37 -0600442 s = bc_num_add(&h1, &l1, &m1, 0);
443 if (s) goto err;
444 s = bc_num_add(&h2, &l2, &m2, 0);
445 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600446
Gavin Howard53eba8b2018-10-31 15:14:37 -0600447 s = bc_num_k(&h1, &h2, &z0);
448 if (s) goto err;
449 s = bc_num_k(&m1, &m2, &z1);
450 if (s) goto err;
451 s = bc_num_k(&l1, &l2, &z2);
452 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600453
Gavin Howard53eba8b2018-10-31 15:14:37 -0600454 s = bc_num_sub(&z1, &z0, &temp, 0);
455 if (s) goto err;
456 s = bc_num_sub(&temp, &z2, &z1, 0);
457 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600458
Gavin Howard53eba8b2018-10-31 15:14:37 -0600459 s = bc_num_shift(&z0, max2 * 2);
460 if (s) goto err;
461 s = bc_num_shift(&z1, max2);
462 if (s) goto err;
463 s = bc_num_add(&z0, &z1, &temp, 0);
464 if (s) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600465 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600466
467err:
468 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600469 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600470 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600471 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600472 bc_num_free(&m2);
Gavin Howard305249a2018-10-15 20:24:47 -0600473 bc_num_free(&m1);
Gavin Howard305249a2018-10-15 20:24:47 -0600474 bc_num_free(&h2);
Gavin Howard305249a2018-10-15 20:24:47 -0600475 bc_num_free(&l2);
Gavin Howard305249a2018-10-15 20:24:47 -0600476 bc_num_free(&h1);
Gavin Howard305249a2018-10-15 20:24:47 -0600477 bc_num_free(&l1);
478 return s;
479}
480
Gavin Howard773c86b2018-11-02 14:07:19 -0600481BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600482
483 BcStatus s;
484 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600485 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600486
487 scale = BC_MAX(scale, a->rdx);
488 scale = BC_MAX(scale, b->rdx);
489 scale = BC_MIN(a->rdx + b->rdx, scale);
490 maxrdx = BC_MAX(maxrdx, scale);
491
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600492 bc_num_init(&cpa, a->len);
493 bc_num_init(&cpb, b->len);
Gavin Howard305249a2018-10-15 20:24:47 -0600494
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600495 bc_num_copy(&cpa, a);
496 bc_num_copy(&cpb, b);
Gavin Howard23d8d252018-10-17 12:03:41 -0600497 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600498
Gavin Howard53eba8b2018-10-31 15:14:37 -0600499 s = bc_num_shift(&cpa, maxrdx);
500 if (s) goto err;
501 s = bc_num_shift(&cpb, maxrdx);
502 if (s) goto err;
503 s = bc_num_k(&cpa, &cpb, c);
504 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600505
Gavin Howard53eba8b2018-10-31 15:14:37 -0600506 maxrdx += scale;
507 bc_num_expand(c, c->len + maxrdx);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600508
Gavin Howard88c25302018-10-17 13:32:23 -0600509 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600510 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600511 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600512 }
513
Gavin Howard88c25302018-10-17 13:32:23 -0600514 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600515 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600516
517err:
518 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600519 bc_num_free(&cpa);
520 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700521}
522
Gavin Howard773c86b2018-11-02 14:07:19 -0600523BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700524
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600525 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600526 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600527 size_t len, end, i;
528 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600529 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700530
Gavin Howardddce6d42019-01-03 14:07:06 -0700531 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
532 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600533 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600534 return BC_STATUS_SUCCESS;
535 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700536 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600537 bc_num_copy(c, a);
538 bc_num_retireMul(c, scale, a->neg, b->neg);
539 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600540 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700541
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600542 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
543 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600544 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700545
Gavin Howard890d0c02018-10-30 16:34:50 -0600546 if (len > cp.len) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600547 bc_num_expand(&cp, len + 2);
548 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600549 }
Gavin Howard021150b2018-03-10 15:40:42 -0700550
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600551 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600552 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600553 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700554
Gavin Howard63738202018-09-26 15:34:20 -0600555 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600556 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600557 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600558 len -= i - 1;
559 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700560
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600561 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700562
Gavin Howard63738202018-09-26 15:34:20 -0600563 // We want an extra zero in front to make things simpler.
564 cp.num[cp.len++] = 0;
565 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700566
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600567 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700568
Gavin Howarda2514a02018-10-18 14:20:09 -0600569 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600570 c->rdx = cp.rdx;
571 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600572 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700573
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700574 for (i = end - 1; !BC_SIGNAL && !s && i < end; --i) {
Gavin Howard63738202018-09-26 15:34:20 -0600575 n = cp.num + i;
Gavin Howard1ab22d22019-01-03 13:32:17 -0700576 for (q = 0; !s && (n[len] || bc_num_compare(n, p, len) >= 0); ++q)
Gavin Howard53eba8b2018-10-31 15:14:37 -0600577 s = bc_num_subArrays(n, p, len);
Gavin Howard63738202018-09-26 15:34:20 -0600578 c->num[i] = q;
579 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700580
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600581 if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600582 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600583
Gavin Howard63738202018-09-26 15:34:20 -0600584 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700585}
586
Gavin Howard773c86b2018-11-02 14:07:19 -0600587BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
588 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600589{
Gavin Howard63738202018-09-26 15:34:20 -0600590 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600591 BcNum temp;
592 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700593
Gavin Howardddce6d42019-01-03 14:07:06 -0700594 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
595 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700596 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600597 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600598 return BC_STATUS_SUCCESS;
599 }
Gavin Howard8b254872018-03-14 01:13:35 -0600600
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600601 bc_num_init(&temp, d->cap);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600602 bc_num_d(a, b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700603
Gavin Howard1ab22d22019-01-03 13:32:17 -0700604 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600605
Gavin Howard53eba8b2018-10-31 15:14:37 -0600606 s = bc_num_m(c, b, &temp, scale);
607 if (s) goto err;
608 s = bc_num_sub(a, &temp, d, scale);
609 if (s) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600610
Gavin Howardddce6d42019-01-03 14:07:06 -0700611 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600612
613 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600614 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600615 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700616
617err:
Gavin Howard18441082018-10-22 10:03:30 -0600618 bc_num_free(&temp);
619 return s;
620}
621
Gavin Howard773c86b2018-11-02 14:07:19 -0600622BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
623
Gavin Howard18441082018-10-22 10:03:30 -0600624 BcStatus s;
625 BcNum c1;
626 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
627
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600628 bc_num_init(&c1, len);
Gavin Howard54b946a2018-10-23 12:06:57 -0600629 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600630 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600631
Gavin Howard63738202018-09-26 15:34:20 -0600632 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700633}
634
Gavin Howard773c86b2018-11-02 14:07:19 -0600635BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700636
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600637 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600638 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700639 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600640 size_t i, powrdx, resrdx;
641 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700642
Gavin Howardac4d9122018-12-27 23:59:12 -0700643 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700644
Gavin Howardddce6d42019-01-03 14:07:06 -0700645 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600646 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600647 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600648 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700649 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 Howardddce6d42019-01-03 14:07:06 -0700653 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600654 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600655 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600656 return s;
657 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700658
Gavin Howard63738202018-09-26 15:34:20 -0600659 neg = b->neg;
660 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600661 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700662 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600663 if (s) return s;
664
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600665 bc_num_init(&copy, a->len);
666 bc_num_copy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700667
Gavin Howard63738202018-09-26 15:34:20 -0600668 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600669
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700670 for (powrdx = a->rdx; !BC_SIGNAL && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600671 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600672 s = bc_num_mul(&copy, &copy, &copy, powrdx);
673 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600674 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700675
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700676 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700677 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600678 goto err;
679 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700680
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600681 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700682 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600683
Gavin Howard4d5daba2019-01-07 14:42:50 -0700684 while (!BC_SIGNAL && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600685
Gavin Howard4d5daba2019-01-07 14:42:50 -0700686 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600687 s = bc_num_mul(&copy, &copy, &copy, powrdx);
688 if (s) goto err;
689
Gavin Howard63738202018-09-26 15:34:20 -0600690 if (pow & 1) {
691 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600692 s = bc_num_mul(c, &copy, c, resrdx);
693 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600694 }
695 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700696
Gavin Howard53eba8b2018-10-31 15:14:37 -0600697 if (neg) {
698 s = bc_num_inv(c, c, scale);
699 if (s) goto err;
700 }
701
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700702 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700703 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600704 goto err;
705 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700706
Gavin Howard63738202018-09-26 15:34:20 -0600707 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700708
Gavin Howard53eba8b2018-10-31 15:14:37 -0600709 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600710 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600711 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700712
Gavin Howard6fbdb292018-02-27 15:44:48 -0700713err:
Gavin Howard63738202018-09-26 15:34:20 -0600714 bc_num_free(&copy);
715 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700716}
717
Gavin Howard7bda4782018-12-28 09:53:22 -0700718#if BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700719BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
720
721 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700722 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700723
724 BC_UNUSED(scale);
725
726 s = bc_num_intop(a, b, c, &val);
727 if (s) return s;
728
729 if (val < a->rdx) bc_num_truncate(c, a->rdx - val);
730 else if (val > a->rdx) bc_num_extend(c, val - a->rdx);
731
732 return s;
733}
734
735BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
736
737 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700738 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700739
740 BC_UNUSED(scale);
741
742 s = bc_num_intop(a, b, c, &val);
743 if (s) return s;
744
745 bc_num_shift(c, val);
746
747 return s;
748}
749
750BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
751
752 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700753 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700754 size_t len;
755
756 BC_UNUSED(scale);
757
758 s = bc_num_intop(a, b, c, &val);
759 if (s) return s;
760
761 len = c->rdx + val;
762
763 if (len > c->len) {
764
Gavin Howard92037352019-01-02 14:58:35 -0700765 if (len > BC_MAX_NUM) return bc_vm_err(BC_ERROR_MATH_UNDERFLOW);
Gavin Howardac4d9122018-12-27 23:59:12 -0700766 if (len > c->cap) bc_num_expand(c, len);
767
768 memset(c->num + c->len, 0, len - c->len);
769 c->len = len;
770 }
771
772 c->rdx += val;
773
774 assert(c->rdx <= c->len && c->len <= c->cap);
775
776 return s;
777}
Gavin Howard7bda4782018-12-28 09:53:22 -0700778#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700779
Gavin Howard773c86b2018-11-02 14:07:19 -0600780BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
781 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700782{
Gavin Howarda1c44392018-09-27 12:41:15 -0600783 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600784 BcNum num2, *ptr_a, *ptr_b;
785 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700786
Gavin Howard63738202018-09-26 15:34:20 -0600787 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700788
Gavin Howard890d0c02018-10-30 16:34:50 -0600789 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600790 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600791 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600792 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600793 }
794 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700795
Gavin Howard63738202018-09-26 15:34:20 -0600796 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600797 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600798 if (c != a) {
799 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600800 init = true;
801 }
802 }
803 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700804
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600805 if (init) bc_num_init(c, req);
806 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700807
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600808 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700809
Gavin Howardddce6d42019-01-03 14:07:06 -0700810 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard3cf769e2018-10-11 13:55:11 -0600811
Gavin Howardba009802018-09-29 04:41:51 -0600812 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600813
Gavin Howarda1c44392018-09-27 12:41:15 -0600814 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700815}
816
Gavin Howardc90ed902019-01-02 13:07:49 -0700817#ifndef NDEBUG
Gavin Howardc7d655b2018-12-28 19:45:13 -0700818bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700819
Gavin Howardc7d655b2018-12-28 19:45:13 -0700820 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -0600821 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700822
Gavin Howard63738202018-09-26 15:34:20 -0600823 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700824
Gavin Howard63738202018-09-26 15:34:20 -0600825 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700826
Gavin Howard8a921bd2018-10-11 14:15:32 -0600827 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600828
Gavin Howard63738202018-09-26 15:34:20 -0600829 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700830
Gavin Howard63738202018-09-26 15:34:20 -0600831 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700832
Gavin Howard63738202018-09-26 15:34:20 -0600833 radix = true;
834 continue;
835 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700836
Gavin Howardc7d655b2018-12-28 19:45:13 -0700837 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -0600838 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700839
Gavin Howard63738202018-09-26 15:34:20 -0600840 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700841}
Gavin Howardc90ed902019-01-02 13:07:49 -0700842#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -0700843
Gavin Howardc7d655b2018-12-28 19:45:13 -0700844unsigned long bc_num_parseChar(char c, size_t base_t) {
845
846 if (isupper(c)) {
847 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -0700848 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -0700849 }
850 else c -= '0';
851
852 return (unsigned long) (uchar) c;
853}
854
Gavin Howard3c02ed32018-12-28 18:17:15 -0700855void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700856
Gavin Howard63738202018-09-26 15:34:20 -0600857 size_t len, i;
858 const char *ptr;
859 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700860
Gavin Howard63738202018-09-26 15:34:20 -0600861 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700862
Gavin Howard63738202018-09-26 15:34:20 -0600863 val += i;
864 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700865
Gavin Howard1ab22d22019-01-03 13:32:17 -0700866 if (len) {
867 for (i = 0; zero && i < len; ++i) zero = (val[i] == '0') || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600868 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600869 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600870
Gavin Howard63738202018-09-26 15:34:20 -0600871 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700872
Gavin Howard63738202018-09-26 15:34:20 -0600873 // Explicitly test for NULL here to produce either a 0 or 1.
874 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700875
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600876 if (!zero) {
Gavin Howard8dd307e2019-01-08 23:05:19 -0700877 for (i = len - 1; i < len; ++n->len, --i) {
878
Gavin Howardc7d655b2018-12-28 19:45:13 -0700879 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -0700880
881 if (c == '.') n->len -= 1;
882 else {
883 if (isupper(c)) c = '9';
884 n->num[n->len] = c - '0';
885 }
Gavin Howardc7d655b2018-12-28 19:45:13 -0700886 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600887 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700888}
889
Gavin Howardddce6d42019-01-03 14:07:06 -0700890BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val, BcNum *restrict base, size_t base_t) {
891
Gavin Howard63738202018-09-26 15:34:20 -0600892 BcStatus s;
893 BcNum temp, mult, result;
Gavin Howard223fa6d2018-12-31 12:24:25 -0700894 BcDig c;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600895 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600896 unsigned long v;
897 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700898
Gavin Howard11b9afd2018-10-18 14:23:28 -0600899 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howard7536dcf2018-12-15 19:27:09 -0700900 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700901
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600902 bc_num_init(&temp, BC_NUM_DEF_SIZE);
903 bc_num_init(&mult, BC_NUM_DEF_SIZE);
Gavin Howardede51f02018-03-02 12:30:00 -0700904
Gavin Howardc7d655b2018-12-28 19:45:13 -0700905 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600906
Gavin Howardc7d655b2018-12-28 19:45:13 -0700907 v = bc_num_parseChar(c, base_t);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700908
Gavin Howard53eba8b2018-10-31 15:14:37 -0600909 s = bc_num_mul(n, base, &mult, 0);
910 if (s) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700911 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600912 s = bc_num_add(&mult, &temp, n, 0);
913 if (s) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600914 }
Gavin Howardede51f02018-03-02 12:30:00 -0700915
Gavin Howard1ab22d22019-01-03 13:32:17 -0700916 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600917
Gavin Howard63738202018-09-26 15:34:20 -0600918 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600919 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -0600920 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700921
Gavin Howardc7d655b2018-12-28 19:45:13 -0700922 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600923
Gavin Howardc7d655b2018-12-28 19:45:13 -0700924 v = bc_num_parseChar(c, base_t);
Gavin Howardede51f02018-03-02 12:30:00 -0700925
Gavin Howard53eba8b2018-10-31 15:14:37 -0600926 s = bc_num_mul(&result, base, &result, 0);
927 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700928 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600929 s = bc_num_add(&result, &temp, &result, 0);
930 if (s) goto err;
931 s = bc_num_mul(&mult, base, &mult, 0);
932 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600933 }
Gavin Howardede51f02018-03-02 12:30:00 -0700934
Gavin Howard53eba8b2018-10-31 15:14:37 -0600935 s = bc_num_div(&result, &mult, &result, digits);
936 if (s) goto err;
937 s = bc_num_add(n, &result, n, digits);
938 if (s) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600939
Gavin Howardddce6d42019-01-03 14:07:06 -0700940 if (BC_NUM_NONZERO(n)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600941 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600942 }
943 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700944
945err:
Gavin Howard63738202018-09-26 15:34:20 -0600946 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700947int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600948 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -0600949 bc_num_free(&temp);
Gavin Howard7536dcf2018-12-15 19:27:09 -0700950 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700951}
952
Gavin Howardab5e9632019-01-02 13:32:02 -0700953void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -0700954 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -0600955 bc_vm_putchar('\\');
956 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -0700957 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600958 }
Gavin Howard80977b22018-09-06 14:29:13 -0600959}
960
Gavin Howard40a085f2018-12-03 12:08:59 -0700961#if DC_ENABLED
Gavin Howardab5e9632019-01-02 13:32:02 -0700962void bc_num_printChar(size_t n, size_t len, bool rdx)
Gavin Howard83eb8392018-10-09 01:21:19 -0600963{
Gavin Howard3c02ed32018-12-28 18:17:15 -0700964 BC_UNUSED(rdx);
965 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -0700966 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600967}
968#endif // DC_ENABLED
969
Gavin Howardab5e9632019-01-02 13:32:02 -0700970void bc_num_printDigits(size_t n, size_t len, bool rdx)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700971{
Gavin Howarda84ad992018-12-03 19:11:06 -0700972 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -0600973
Gavin Howardab5e9632019-01-02 13:32:02 -0700974 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700975 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -0700976 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600977
Gavin Howardab5e9632019-01-02 13:32:02 -0700978 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700979 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600980
Gavin Howardab5e9632019-01-02 13:32:02 -0700981 for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -0700982 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -0700983 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700984 dig = n / pow;
985 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -0700986 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -0600987 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700988}
Gavin Howardfe679f02018-02-14 15:50:09 -0700989
Gavin Howardab5e9632019-01-02 13:32:02 -0700990void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -0700991
Gavin Howard3c02ed32018-12-28 18:17:15 -0700992 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600993
Gavin Howard3c02ed32018-12-28 18:17:15 -0700994 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -0700995 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -0600996 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -0700997 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -0600998 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700999
Gavin Howardab5e9632019-01-02 13:32:02 -07001000 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001001 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001002 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001003}
1004
Gavin Howardab5e9632019-01-02 13:32:02 -07001005void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001006
Gavin Howard63738202018-09-26 15:34:20 -06001007 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001008
Gavin Howard48af52e2018-10-30 14:47:38 -06001009 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001010 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001011
Gavin Howard48af52e2018-10-30 14:47:38 -06001012 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001013 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001014}
1015
Gavin Howardddce6d42019-01-03 14:07:06 -07001016BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base, size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001017{
Gavin Howard63738202018-09-26 15:34:20 -06001018 BcStatus s;
1019 BcVec stack;
1020 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001021 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001022 size_t i;
1023 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001024
Gavin Howardddce6d42019-01-03 14:07:06 -07001025 if (BC_NUM_ZERO(n)) {
1026 print(0, len, false);
Gavin Howard48af52e2018-10-30 14:47:38 -06001027 return BC_STATUS_SUCCESS;
1028 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001029
Gavin Howardd3929832018-12-24 15:13:15 -07001030 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001031 bc_num_init(&intp, n->len);
1032 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001033 bc_num_init(&digit, len);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001034 bc_num_init(&frac_len, BC_NUM_INT(n));
1035 bc_num_copy(&intp, n);
Gavin Howard63738202018-09-26 15:34:20 -06001036 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -06001037
Gavin Howard63738202018-09-26 15:34:20 -06001038 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001039 s = bc_num_sub(n, &intp, &fracp, 0);
1040 if (s) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001041
Gavin Howardddce6d42019-01-03 14:07:06 -07001042 while (BC_NUM_NONZERO(&intp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001043 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
1044 if (s) goto err;
1045 s = bc_num_ulong(&digit, &dig);
1046 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001047 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001048 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001049
Gavin Howard63738202018-09-26 15:34:20 -06001050 for (i = 0; i < stack.len; ++i) {
1051 ptr = bc_vec_item_rev(&stack, i);
1052 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001053 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001054 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001055
Gavin Howard63738202018-09-26 15:34:20 -06001056 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001057
Gavin Howard63738202018-09-26 15:34:20 -06001058 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001059 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
1060 if (s) goto err;
1061 s = bc_num_ulong(&fracp, &dig);
1062 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001063 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001064 s = bc_num_sub(&fracp, &intp, &fracp, 0);
1065 if (s) goto err;
Gavin Howardddce6d42019-01-03 14:07:06 -07001066 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001067 s = bc_num_mul(&frac_len, base, &frac_len, 0);
1068 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001069 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001070
1071err:
Gavin Howard63738202018-09-26 15:34:20 -06001072 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001073 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001074 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001075 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001076 bc_vec_free(&stack);
Gavin Howard63738202018-09-26 15:34:20 -06001077 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001078}
1079
Gavin Howardbe4e8222019-01-03 13:34:16 -07001080BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base, size_t base_t) {
Gavin Howard5a0616e2019-01-03 13:33:50 -07001081
Gavin Howard83eb8392018-10-09 01:21:19 -06001082 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001083 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001084 BcNumDigitOp print;
1085 bool neg = n->neg;
1086
Gavin Howard48af52e2018-10-30 14:47:38 -06001087 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001088 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001089
1090 n->neg = false;
1091
Gavin Howardbe4e8222019-01-03 13:34:16 -07001092 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001093 width = 1;
1094 print = bc_num_printHex;
1095 }
1096 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001097 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001098 print = bc_num_printDigits;
1099 }
1100
Gavin Howardab5e9632019-01-02 13:32:02 -07001101 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001102 n->neg = neg;
1103
1104 return s;
1105}
1106
Gavin Howard40a085f2018-12-03 12:08:59 -07001107#if DC_ENABLED
Gavin Howardab5e9632019-01-02 13:32:02 -07001108BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1109 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001110}
1111#endif // DC_ENABLED
1112
Gavin Howard3c02ed32018-12-28 18:17:15 -07001113void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001114 assert(n);
1115 n->num = num;
1116 n->cap = cap;
1117 n->rdx = n->len = 0;
1118 n->neg = false;
1119}
1120
Gavin Howard3c02ed32018-12-28 18:17:15 -07001121void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001122 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001123 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001124 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001125}
1126
Gavin Howard3c02ed32018-12-28 18:17:15 -07001127void bc_num_expand(BcNum *restrict n, size_t req) {
Gavin Howard844e7a52018-10-09 11:46:39 -06001128 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001129 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1130 if (req > n->cap) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001131 n->num = bc_vm_realloc(n->num, req);
1132 n->cap = req;
1133 }
Gavin Howardb5c77212018-02-14 17:12:34 -07001134}
1135
Gavin Howarded392aa2018-02-27 13:09:26 -07001136void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001137 assert(num);
1138 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001139}
1140
Gavin Howard3c02ed32018-12-28 18:17:15 -07001141void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001142 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001143 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001144 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001145 d->len = s->len;
1146 d->neg = s->neg;
1147 d->rdx = s->rdx;
1148 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001149}
1150
Gavin Howard3c02ed32018-12-28 18:17:15 -07001151BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
Gavin Howardd746e802018-12-28 22:06:00 -07001152 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001153{
Gavin Howard7536dcf2018-12-15 19:27:09 -07001154 BcStatus s = BC_STATUS_SUCCESS;
1155
Gavin Howard63738202018-09-26 15:34:20 -06001156 assert(n && val && base);
Gavin Howard96edeb32018-12-31 09:33:22 -07001157 assert(base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase);
Gavin Howardc90ed902019-01-02 13:07:49 -07001158 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001159
Gavin Howardd746e802018-12-28 22:06:00 -07001160 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howardc7d655b2018-12-28 19:45:13 -07001161 else if (base_t == 10) bc_num_parseDecimal(n, val);
1162 else s = bc_num_parseBase(n, val, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001163
Gavin Howard7536dcf2018-12-15 19:27:09 -07001164 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001165}
1166
Gavin Howardab5e9632019-01-02 13:32:02 -07001167BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1168 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001169{
Gavin Howard48af52e2018-10-30 14:47:38 -06001170 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001171
Gavin Howardab5e9632019-01-02 13:32:02 -07001172 assert(n && base);
Gavin Howard63738202018-09-26 15:34:20 -06001173 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001174
Gavin Howardab5e9632019-01-02 13:32:02 -07001175 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001176
Gavin Howardddce6d42019-01-03 14:07:06 -07001177 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howardab5e9632019-01-02 13:32:02 -07001178 else if (base_t == 10) bc_num_printDecimal(n);
1179 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001180
Gavin Howardddce6d42019-01-03 14:07:06 -07001181 if (!s && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001182 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001183 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001184 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001185
Gavin Howard63738202018-09-26 15:34:20 -06001186 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001187}
1188
Gavin Howard3c02ed32018-12-28 18:17:15 -07001189BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001190
Gavin Howard63738202018-09-26 15:34:20 -06001191 size_t i;
Gavin Howard7b557da2018-12-21 10:25:32 -07001192 unsigned long pow, r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001193
Gavin Howard63738202018-09-26 15:34:20 -06001194 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001195
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001196 *result = 0;
1197
Gavin Howard7536dcf2018-12-15 19:27:09 -07001198 if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001199
Gavin Howard7b557da2018-12-21 10:25:32 -07001200 for (r = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001201
Gavin Howard7b557da2018-12-21 10:25:32 -07001202 unsigned long prev = r, powprev = pow;
Gavin Howard88c25302018-10-17 13:32:23 -06001203
Gavin Howard7b557da2018-12-21 10:25:32 -07001204 r += ((unsigned long) n->num[i]) * pow;
Gavin Howard63738202018-09-26 15:34:20 -06001205 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001206
Gavin Howard92037352019-01-02 14:58:35 -07001207 if (r < prev || pow < powprev)
1208 return bc_vm_verr(BC_ERROR_MATH_OVERFLOW, "number cannot fit");
Gavin Howard63738202018-09-26 15:34:20 -06001209 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001210
Gavin Howard7b557da2018-12-21 10:25:32 -07001211 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001212
Gavin Howard63738202018-09-26 15:34:20 -06001213 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001214}
1215
Gavin Howard3c02ed32018-12-28 18:17:15 -07001216void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001217
Gavin Howardec82bdc2018-10-08 23:50:57 -06001218 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001219 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001220 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001221
Gavin Howard63738202018-09-26 15:34:20 -06001222 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001223
Gavin Howard63738202018-09-26 15:34:20 -06001224 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001225
Gavin Howard1ab22d22019-01-03 13:32:17 -07001226 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001227
Gavin Howard1ab22d22019-01-03 13:32:17 -07001228 len = bc_num_log10(ULONG_MAX);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001229 bc_num_expand(n, len);
Gavin Howardec82bdc2018-10-08 23:50:57 -06001230 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001231}
1232
Gavin Howard12fe7812018-09-29 03:45:18 -06001233BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001234 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001235 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001236 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001237}
1238
Gavin Howard12fe7812018-09-29 03:45:18 -06001239BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001240 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001241 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001242 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001243}
1244
Gavin Howard12fe7812018-09-29 03:45:18 -06001245BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001246 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001247 return bc_num_binary(a, b, c, scale, bc_num_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001248}
1249
Gavin Howard12fe7812018-09-29 03:45:18 -06001250BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001251 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001252 return bc_num_binary(a, b, c, scale, bc_num_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001253}
1254
Gavin Howard54b946a2018-10-23 12:06:57 -06001255BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001256 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001257 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001258}
1259
Gavin Howard12fe7812018-09-29 03:45:18 -06001260BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001261 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001262}
1263
Gavin Howard7d8f0382018-12-27 16:15:13 -07001264#if BC_ENABLE_EXTRA_MATH
1265BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001266 return bc_num_binary(a, b, c, scale, bc_num_place, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001267}
1268
1269BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001270 return bc_num_binary(a, b, c, scale, bc_num_left, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001271}
1272
1273BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001274 return bc_num_binary(a, b, c, scale, bc_num_right, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001275}
1276#endif // BC_ENABLE_EXTRA_MATH
1277
Gavin Howard3c02ed32018-12-28 18:17:15 -07001278BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001279
Gavin Howard63738202018-09-26 15:34:20 -06001280 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001281 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001282 size_t pow, len, digs, digs1, resrdx, req, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001283 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001284 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001285
Gavin Howard34edd0a2018-10-25 15:51:05 -06001286 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001287
Gavin Howardd8d60c42018-10-29 12:13:09 -06001288 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001289 bc_num_expand(b, req);
Gavin Howard954276b2018-05-16 01:43:58 -06001290
Gavin Howardddce6d42019-01-03 14:07:06 -07001291 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001292 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001293 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001294 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001295 if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1296 if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001297 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001298 bc_num_extend(b, scale);
1299 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001300 }
Gavin Howard954276b2018-05-16 01:43:58 -06001301
Gavin Howard890d0c02018-10-30 16:34:50 -06001302 scale = BC_MAX(scale, a->rdx) + 1;
1303 len = a->len + scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001304
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001305 bc_num_init(&num1, len);
1306 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001307 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001308
Gavin Howard63738202018-09-26 15:34:20 -06001309 bc_num_one(&half);
1310 half.num[0] = 5;
1311 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001312
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001313 bc_num_init(&f, len);
1314 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001315
Gavin Howard63738202018-09-26 15:34:20 -06001316 x0 = &num1;
1317 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001318
Gavin Howard63738202018-09-26 15:34:20 -06001319 bc_num_one(x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001320 pow = BC_NUM_INT(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001321
Gavin Howard53eba8b2018-10-31 15:14:37 -06001322 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001323
Gavin Howardc39fd492018-10-04 10:07:03 -06001324 if (pow & 1) x0->num[0] = 2;
1325 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001326
Gavin Howardc39fd492018-10-04 10:07:03 -06001327 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001328
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001329 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001330
Gavin Howard63738202018-09-26 15:34:20 -06001331 // Make sure to move the radix back.
1332 x0->rdx -= pow;
1333 }
Gavin Howard954276b2018-05-16 01:43:58 -06001334
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001335 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001336 resrdx = scale + 2;
1337 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001338
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001339 while (!BC_SIGNAL && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001340
Gavin Howard53eba8b2018-10-31 15:14:37 -06001341 s = bc_num_div(a, x0, &f, resrdx);
1342 if (s) goto err;
1343 s = bc_num_add(x0, &f, &fprime, resrdx);
1344 if (s) goto err;
1345 s = bc_num_mul(&fprime, &half, x1, resrdx);
1346 if (s) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001347
Gavin Howard53eba8b2018-10-31 15:14:37 -06001348 cmp = bc_num_cmp(x1, x0);
1349 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001350
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001351 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001352 else times = 0;
1353
1354 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001355
1356 cmp2 = cmp1;
1357 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001358 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001359
Gavin Howard63738202018-09-26 15:34:20 -06001360 temp = x0;
1361 x0 = x1;
1362 x1 = temp;
1363 }
Gavin Howard954276b2018-05-16 01:43:58 -06001364
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001365 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -07001366 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -06001367 goto err;
1368 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001369
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001370 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001371 scale -= 1;
1372 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001373
1374err:
Gavin Howard63738202018-09-26 15:34:20 -06001375 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001376 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001377 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001378 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001379 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howard63738202018-09-26 15:34:20 -06001380 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001381}
Gavin Howardba009802018-09-29 04:41:51 -06001382
Gavin Howard2cb39612018-10-22 08:46:48 -06001383BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1384
1385 BcStatus s;
1386 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001387 bool init = false;
Gavin Howard2cb39612018-10-22 08:46:48 -06001388 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
1389
1390 assert(c != d && a != b && a != d && b != d && b != c);
1391
Gavin Howard890d0c02018-10-30 16:34:50 -06001392 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001393 memcpy(&num2, c, sizeof(BcNum));
1394 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001395 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001396 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001397 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001398 else {
1399 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001400 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001401 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001402
Gavin Howard54b946a2018-10-23 12:06:57 -06001403 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001404
Gavin Howardddce6d42019-01-03 14:07:06 -07001405 assert(!c->neg || BC_NUM_NONZERO(c));
1406 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howard2cb39612018-10-22 08:46:48 -06001407
1408 if (init) bc_num_free(&num2);
1409
1410 return s;
1411}
1412
Gavin Howard40a085f2018-12-03 12:08:59 -07001413#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001414BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001415
1416 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001417 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001418 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001419
Gavin Howard34edd0a2018-10-25 15:51:05 -06001420 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001421
Gavin Howardddce6d42019-01-03 14:07:06 -07001422 if (BC_NUM_ZERO(c)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard7536dcf2018-12-15 19:27:09 -07001423 if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1424 if (a->rdx || b->rdx || c->rdx)
1425 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001426
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001427 bc_num_expand(d, c->len);
1428 bc_num_init(&base, c->len);
1429 bc_num_init(&exp, b->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001430 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001431 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001432
1433 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001434 two.num[0] = 2;
1435 bc_num_one(d);
1436
Gavin Howard53eba8b2018-10-31 15:14:37 -06001437 s = bc_num_rem(a, c, &base, 0);
1438 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001439 bc_num_copy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001440
Gavin Howardddce6d42019-01-03 14:07:06 -07001441 while (BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001442
Gavin Howard53eba8b2018-10-31 15:14:37 -06001443 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
1444 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001445
1446 if (BC_NUM_ONE(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001447 s = bc_num_mul(d, &base, &temp, 0);
1448 if (s) goto err;
1449 s = bc_num_rem(&temp, c, d, 0);
1450 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001451 }
1452
Gavin Howard53eba8b2018-10-31 15:14:37 -06001453 s = bc_num_mul(&base, &base, &temp, 0);
1454 if (s) goto err;
1455 s = bc_num_rem(&temp, c, &base, 0);
1456 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001457 }
1458
1459err:
1460 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001461 bc_num_free(&exp);
Gavin Howardba009802018-09-29 04:41:51 -06001462 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001463 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001464 return s;
1465}
Gavin Howarde6e84762018-10-03 11:46:34 -06001466#endif // DC_ENABLED