blob: 130e920720dea497ecbbdeab63ed63f37a6dc804 [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 Howardc4bf4c42019-01-09 10:30:15 -0700161 if (n->len) n->len += places;
162
Gavin Howard1ab22d22019-01-03 13:32:17 -0700163 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700164}
165
Gavin Howard3c02ed32018-12-28 18:17:15 -0700166void bc_num_clean(BcNum *restrict n) {
Gavin Howardddce6d42019-01-03 14:07:06 -0700167 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
168 if (BC_NUM_ZERO(n)) n->neg = false;
Gavin Howard63738202018-09-26 15:34:20 -0600169 else if (n->len < n->rdx) n->len = n->rdx;
Gavin Howarde57525d2018-09-01 16:56:21 -0600170}
171
Gavin Howard3c02ed32018-12-28 18:17:15 -0700172void bc_num_retireMul(BcNum *restrict n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600173
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600174 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600175 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600176
Gavin Howard305249a2018-10-15 20:24:47 -0600177 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700178 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600179}
180
Gavin Howard83c850d2019-01-08 23:04:51 -0700181void bc_num_split(const BcNum *restrict n, size_t idx, BcNum *restrict a, BcNum *restrict b) {
Gavin Howard50c80022019-01-03 15:14:33 -0700182
Gavin Howard305249a2018-10-15 20:24:47 -0600183 if (idx < n->len) {
184
Gavin Howard305249a2018-10-15 20:24:47 -0600185 b->len = n->len - idx;
186 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600187 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600188
189 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
190 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard50c80022019-01-03 15:14:33 -0700191
192 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600193 }
Gavin Howard50c80022019-01-03 15:14:33 -0700194 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600195
196 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600197}
198
Gavin Howard3c02ed32018-12-28 18:17:15 -0700199BcStatus bc_num_shift(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600200
Gavin Howardddce6d42019-01-03 14:07:06 -0700201 if (!places || BC_NUM_ZERO(n)) return BC_STATUS_SUCCESS;
Gavin Howardecc7fc22018-12-31 13:21:47 -0700202 if (places + n->len > BC_MAX_NUM)
Gavin Howard92037352019-01-02 14:58:35 -0700203 return bc_vm_verr(BC_ERROR_MATH_OVERFLOW, "shifted left too far");
Gavin Howard305249a2018-10-15 20:24:47 -0600204
205 if (n->rdx >= places) n->rdx -= places;
206 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600207 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600208 n->rdx = 0;
209 }
210
211 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600212
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600213 return BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600214}
215
Gavin Howard773c86b2018-11-02 14:07:19 -0600216BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600217
Gavin Howard63738202018-09-26 15:34:20 -0600218 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600219 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600220
Gavin Howardd32d7df2018-10-15 08:42:25 -0600221 one.cap = 2;
222 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600223 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600224
Gavin Howardd32d7df2018-10-15 08:42:25 -0600225 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600226}
227
Gavin Howard7bda4782018-12-28 09:53:22 -0700228#if BC_ENABLE_EXTRA_MATH
Gavin Howard3c02ed32018-12-28 18:17:15 -0700229BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
230 unsigned long *v)
231{
Gavin Howardac4d9122018-12-27 23:59:12 -0700232 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
233 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700234 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700235}
Gavin Howard7bda4782018-12-28 09:53:22 -0700236#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700237
Gavin Howard773c86b2018-11-02 14:07:19 -0600238BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700239
Gavin Howard8a921bd2018-10-11 14:15:32 -0600240 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600241 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700242 unsigned int carry, in;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700243
Gavin Howard63738202018-09-26 15:34:20 -0600244 // Because this function doesn't need to use scale (per the bc spec),
245 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600246
Gavin Howardddce6d42019-01-03 14:07:06 -0700247 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600248 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700249 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600250 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600251 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700252 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600253 bc_num_copy(c, a);
254 return BC_STATUS_SUCCESS;
255 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600256
Gavin Howard63738202018-09-26 15:34:20 -0600257 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600258 c->rdx = BC_MAX(a->rdx, b->rdx);
259 min_rdx = BC_MIN(a->rdx, b->rdx);
260 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700261
Gavin Howard63738202018-09-26 15:34:20 -0600262 if (a->rdx > b->rdx) {
263 diff = a->rdx - b->rdx;
264 ptr = a->num;
265 ptr_a = a->num + diff;
266 ptr_b = b->num;
267 }
268 else {
269 diff = b->rdx - a->rdx;
270 ptr = b->num;
271 ptr_a = a->num;
272 ptr_b = b->num + diff;
273 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700274
Gavin Howard63738202018-09-26 15:34:20 -0600275 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700276
Gavin Howard63738202018-09-26 15:34:20 -0600277 ptr_c += diff;
Gavin Howard890d0c02018-10-30 16:34:50 -0600278 a_int = BC_NUM_INT(a);
279 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700280
Gavin Howard890d0c02018-10-30 16:34:50 -0600281 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600282 min_int = b_int;
283 max = a_int;
284 ptr = ptr_a;
285 }
286 else {
287 min_int = a_int;
288 max = b_int;
289 ptr = ptr_b;
290 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700291
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700292 for (carry = 0, i = 0; !BC_SIGNAL && i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700293 in = ((unsigned int) ptr_a[i]) + ((unsigned int) ptr_b[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600294 carry = in / 10;
Gavin Howard231e66c2019-01-08 23:02:19 -0700295 assert(carry >= 0 && carry < 10);
Gavin Howard11b9afd2018-10-18 14:23:28 -0600296 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard231e66c2019-01-08 23:02:19 -0700297 assert(ptr_c[i] >= 0 && ptr_c[i] < 10);
Gavin Howard63738202018-09-26 15:34:20 -0600298 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700299
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700300 for (; !BC_SIGNAL && i < max + min_rdx; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700301 in = ((unsigned int) ptr[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600302 carry = in / 10;
Gavin Howard231e66c2019-01-08 23:02:19 -0700303 assert(carry >= 0 && carry < 10);
Gavin Howard11b9afd2018-10-18 14:23:28 -0600304 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard231e66c2019-01-08 23:02:19 -0700305 assert(ptr_c[i] >= 0 && ptr_c[i] < 10);
Gavin Howard63738202018-09-26 15:34:20 -0600306 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700307
Gavin Howard1ab22d22019-01-03 13:32:17 -0700308 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600309
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700310 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700311}
312
Gavin Howard773c86b2018-11-02 14:07:19 -0600313BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700314
Gavin Howard63738202018-09-26 15:34:20 -0600315 BcStatus s;
316 ssize_t cmp;
317 BcNum *minuend, *subtrahend;
318 size_t start;
319 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700320
Gavin Howard63738202018-09-26 15:34:20 -0600321 // Because this function doesn't need to use scale (per the bc spec),
322 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700323
Gavin Howardddce6d42019-01-03 14:07:06 -0700324 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600325 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700326 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600327 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600328 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700329 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600330 bc_num_copy(c, a);
331 return BC_STATUS_SUCCESS;
332 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700333
Gavin Howard63738202018-09-26 15:34:20 -0600334 aneg = a->neg;
335 bneg = b->neg;
336 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700337
Gavin Howard63738202018-09-26 15:34:20 -0600338 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700339
Gavin Howard63738202018-09-26 15:34:20 -0600340 a->neg = aneg;
341 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700342
Gavin Howard1ab22d22019-01-03 13:32:17 -0700343 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600344 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600345 return BC_STATUS_SUCCESS;
346 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700347
348 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600349 neg = a->neg;
350 minuend = a;
351 subtrahend = b;
352 }
353 else {
354 neg = b->neg;
355 if (sub) neg = !neg;
356 minuend = b;
357 subtrahend = a;
358 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700359
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600360 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600361 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700362
Gavin Howard63738202018-09-26 15:34:20 -0600363 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600364 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600365 start = 0;
366 }
367 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700368
Gavin Howard63738202018-09-26 15:34:20 -0600369 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700370
Gavin Howard63738202018-09-26 15:34:20 -0600371 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700372
Gavin Howard63738202018-09-26 15:34:20 -0600373 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700374}
375
Gavin Howard83c850d2019-01-08 23:04:51 -0700376BcStatus bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600377
Gavin Howard63738202018-09-26 15:34:20 -0600378 BcStatus s;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700379 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600380 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard63738202018-09-26 15:34:20 -0600381 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600382
Gavin Howardddce6d42019-01-03 14:07:06 -0700383 // This is here because the function is recursive.
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700384 if (BC_SIGNAL) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700385 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600386 bc_num_zero(c);
387 return BC_STATUS_SUCCESS;
388 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700389 if (aone || BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600390 bc_num_copy(c, aone ? b : a);
391 return BC_STATUS_SUCCESS;
392 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700393
Gavin Howardb5093d72018-10-17 12:13:17 -0600394 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
395 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600396 {
Gavin Howard34c4ba02018-12-11 10:25:01 -0700397 size_t i, j, len;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700398 unsigned int carry;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700399
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600400 bc_num_expand(c, a->len + b->len + 1);
Gavin Howard23d8d252018-10-17 12:03:41 -0600401
Gavin Howard305249a2018-10-15 20:24:47 -0600402 memset(c->num, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700403 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700404
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700405 for (i = 0; !BC_SIGNAL && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600406
Gavin Howard34c4ba02018-12-11 10:25:01 -0700407 carry = 0;
408
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700409 for (j = 0; !BC_SIGNAL && j < a->len; ++j) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700410 unsigned int in = c->num[i + j];
411 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
412 in += carry;
Gavin Howard305249a2018-10-15 20:24:47 -0600413 carry = in / 10;
Gavin Howard231e66c2019-01-08 23:02:19 -0700414 assert(carry >= 0 && carry < 10);
Gavin Howard11b9afd2018-10-18 14:23:28 -0600415 c->num[i + j] = (BcDig) (in % 10);
Gavin Howard231e66c2019-01-08 23:02:19 -0700416 assert(c->num[i + j] >= 0 && c->num[i + j] < 10);
Gavin Howard305249a2018-10-15 20:24:47 -0600417 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700418
Gavin Howard88c25302018-10-17 13:32:23 -0600419 c->num[i + j] += (BcDig) carry;
Gavin Howard231e66c2019-01-08 23:02:19 -0700420 assert(c->num[i + j] >= 0 && c->num[i + j] < 10);
Gavin Howard88c25302018-10-17 13:32:23 -0600421 len = BC_MAX(len, i + j + !!carry);
Gavin Howard63738202018-09-26 15:34:20 -0600422 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700423
Gavin Howard305249a2018-10-15 20:24:47 -0600424 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600425
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700426 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600427 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700428
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600429 bc_num_init(&l1, max);
430 bc_num_init(&h1, max);
431 bc_num_init(&l2, max);
432 bc_num_init(&h2, max);
433 bc_num_init(&m1, max);
434 bc_num_init(&m2, max);
435 bc_num_init(&z0, max);
436 bc_num_init(&z1, max);
437 bc_num_init(&z2, max);
438 bc_num_init(&temp, max + max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600439
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600440 bc_num_split(a, max2, &l1, &h1);
441 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700442
Gavin Howard53eba8b2018-10-31 15:14:37 -0600443 s = bc_num_add(&h1, &l1, &m1, 0);
444 if (s) goto err;
445 s = bc_num_add(&h2, &l2, &m2, 0);
446 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600447
Gavin Howard53eba8b2018-10-31 15:14:37 -0600448 s = bc_num_k(&h1, &h2, &z0);
449 if (s) goto err;
450 s = bc_num_k(&m1, &m2, &z1);
451 if (s) goto err;
452 s = bc_num_k(&l1, &l2, &z2);
453 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600454
Gavin Howard53eba8b2018-10-31 15:14:37 -0600455 s = bc_num_sub(&z1, &z0, &temp, 0);
456 if (s) goto err;
457 s = bc_num_sub(&temp, &z2, &z1, 0);
458 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600459
Gavin Howard53eba8b2018-10-31 15:14:37 -0600460 s = bc_num_shift(&z0, max2 * 2);
461 if (s) goto err;
462 s = bc_num_shift(&z1, max2);
463 if (s) goto err;
464 s = bc_num_add(&z0, &z1, &temp, 0);
465 if (s) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600466 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600467
468err:
469 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600470 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600471 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600472 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600473 bc_num_free(&m2);
Gavin Howard305249a2018-10-15 20:24:47 -0600474 bc_num_free(&m1);
Gavin Howard305249a2018-10-15 20:24:47 -0600475 bc_num_free(&h2);
Gavin Howard305249a2018-10-15 20:24:47 -0600476 bc_num_free(&l2);
Gavin Howard305249a2018-10-15 20:24:47 -0600477 bc_num_free(&h1);
Gavin Howard305249a2018-10-15 20:24:47 -0600478 bc_num_free(&l1);
479 return s;
480}
481
Gavin Howard773c86b2018-11-02 14:07:19 -0600482BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600483
484 BcStatus s;
485 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600486 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600487
488 scale = BC_MAX(scale, a->rdx);
489 scale = BC_MAX(scale, b->rdx);
490 scale = BC_MIN(a->rdx + b->rdx, scale);
491 maxrdx = BC_MAX(maxrdx, scale);
492
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600493 bc_num_init(&cpa, a->len);
494 bc_num_init(&cpb, b->len);
Gavin Howard305249a2018-10-15 20:24:47 -0600495
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600496 bc_num_copy(&cpa, a);
497 bc_num_copy(&cpb, b);
Gavin Howard23d8d252018-10-17 12:03:41 -0600498 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600499
Gavin Howard53eba8b2018-10-31 15:14:37 -0600500 s = bc_num_shift(&cpa, maxrdx);
501 if (s) goto err;
502 s = bc_num_shift(&cpb, maxrdx);
503 if (s) goto err;
504 s = bc_num_k(&cpa, &cpb, c);
505 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600506
Gavin Howard53eba8b2018-10-31 15:14:37 -0600507 maxrdx += scale;
508 bc_num_expand(c, c->len + maxrdx);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600509
Gavin Howard88c25302018-10-17 13:32:23 -0600510 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600511 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600512 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600513 }
514
Gavin Howard88c25302018-10-17 13:32:23 -0600515 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600516 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600517
518err:
519 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600520 bc_num_free(&cpa);
521 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700522}
523
Gavin Howard773c86b2018-11-02 14:07:19 -0600524BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700525
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600526 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600527 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600528 size_t len, end, i;
529 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600530 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700531
Gavin Howardddce6d42019-01-03 14:07:06 -0700532 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
533 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600534 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600535 return BC_STATUS_SUCCESS;
536 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700537 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600538 bc_num_copy(c, a);
539 bc_num_retireMul(c, scale, a->neg, b->neg);
540 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600541 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700542
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600543 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
544 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600545 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700546
Gavin Howard890d0c02018-10-30 16:34:50 -0600547 if (len > cp.len) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600548 bc_num_expand(&cp, len + 2);
549 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600550 }
Gavin Howard021150b2018-03-10 15:40:42 -0700551
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600552 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600553 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600554 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700555
Gavin Howard63738202018-09-26 15:34:20 -0600556 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600557 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600558 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600559 len -= i - 1;
560 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700561
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600562 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700563
Gavin Howard63738202018-09-26 15:34:20 -0600564 // We want an extra zero in front to make things simpler.
565 cp.num[cp.len++] = 0;
566 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700567
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600568 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700569
Gavin Howarda2514a02018-10-18 14:20:09 -0600570 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600571 c->rdx = cp.rdx;
572 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600573 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700574
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700575 for (i = end - 1; !BC_SIGNAL && !s && i < end; --i) {
Gavin Howard63738202018-09-26 15:34:20 -0600576 n = cp.num + i;
Gavin Howard1ab22d22019-01-03 13:32:17 -0700577 for (q = 0; !s && (n[len] || bc_num_compare(n, p, len) >= 0); ++q)
Gavin Howard53eba8b2018-10-31 15:14:37 -0600578 s = bc_num_subArrays(n, p, len);
Gavin Howard63738202018-09-26 15:34:20 -0600579 c->num[i] = q;
580 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700581
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600582 if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600583 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600584
Gavin Howard63738202018-09-26 15:34:20 -0600585 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700586}
587
Gavin Howard773c86b2018-11-02 14:07:19 -0600588BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
589 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600590{
Gavin Howard63738202018-09-26 15:34:20 -0600591 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600592 BcNum temp;
593 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700594
Gavin Howardddce6d42019-01-03 14:07:06 -0700595 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
596 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700597 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600598 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600599 return BC_STATUS_SUCCESS;
600 }
Gavin Howard8b254872018-03-14 01:13:35 -0600601
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600602 bc_num_init(&temp, d->cap);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600603 bc_num_d(a, b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700604
Gavin Howard1ab22d22019-01-03 13:32:17 -0700605 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600606
Gavin Howard53eba8b2018-10-31 15:14:37 -0600607 s = bc_num_m(c, b, &temp, scale);
608 if (s) goto err;
609 s = bc_num_sub(a, &temp, d, scale);
610 if (s) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600611
Gavin Howardddce6d42019-01-03 14:07:06 -0700612 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600613
614 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600615 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600616 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700617
618err:
Gavin Howard18441082018-10-22 10:03:30 -0600619 bc_num_free(&temp);
620 return s;
621}
622
Gavin Howard773c86b2018-11-02 14:07:19 -0600623BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
624
Gavin Howard18441082018-10-22 10:03:30 -0600625 BcStatus s;
626 BcNum c1;
627 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
628
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600629 bc_num_init(&c1, len);
Gavin Howard54b946a2018-10-23 12:06:57 -0600630 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600631 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600632
Gavin Howard63738202018-09-26 15:34:20 -0600633 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700634}
635
Gavin Howard773c86b2018-11-02 14:07:19 -0600636BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700637
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600638 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600639 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700640 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600641 size_t i, powrdx, resrdx;
642 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700643
Gavin Howardac4d9122018-12-27 23:59:12 -0700644 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700645
Gavin Howardddce6d42019-01-03 14:07:06 -0700646 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600647 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600648 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600649 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700650 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600651 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600652 return BC_STATUS_SUCCESS;
653 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700654 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600655 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600656 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600657 return s;
658 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700659
Gavin Howard63738202018-09-26 15:34:20 -0600660 neg = b->neg;
661 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600662 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700663 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600664 if (s) return s;
665
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600666 bc_num_init(&copy, a->len);
667 bc_num_copy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700668
Gavin Howard63738202018-09-26 15:34:20 -0600669 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600670
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700671 for (powrdx = a->rdx; !BC_SIGNAL && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600672 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600673 s = bc_num_mul(&copy, &copy, &copy, powrdx);
674 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600675 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700676
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700677 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700678 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600679 goto err;
680 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700681
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600682 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700683 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600684
Gavin Howard4d5daba2019-01-07 14:42:50 -0700685 while (!BC_SIGNAL && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600686
Gavin Howard4d5daba2019-01-07 14:42:50 -0700687 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600688 s = bc_num_mul(&copy, &copy, &copy, powrdx);
689 if (s) goto err;
690
Gavin Howard63738202018-09-26 15:34:20 -0600691 if (pow & 1) {
692 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600693 s = bc_num_mul(c, &copy, c, resrdx);
694 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600695 }
696 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700697
Gavin Howard53eba8b2018-10-31 15:14:37 -0600698 if (neg) {
699 s = bc_num_inv(c, c, scale);
700 if (s) goto err;
701 }
702
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700703 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700704 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600705 goto err;
706 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700707
Gavin Howard63738202018-09-26 15:34:20 -0600708 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700709
Gavin Howard53eba8b2018-10-31 15:14:37 -0600710 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600711 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600712 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700713
Gavin Howard6fbdb292018-02-27 15:44:48 -0700714err:
Gavin Howard63738202018-09-26 15:34:20 -0600715 bc_num_free(&copy);
716 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700717}
718
Gavin Howard7bda4782018-12-28 09:53:22 -0700719#if BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700720BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
721
722 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700723 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700724
725 BC_UNUSED(scale);
726
727 s = bc_num_intop(a, b, c, &val);
728 if (s) return s;
729
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700730 if (val < c->rdx) bc_num_truncate(c, c->rdx - val);
731 else if (val > c->rdx) bc_num_extend(c, val - c->rdx);
Gavin Howardac4d9122018-12-27 23:59:12 -0700732
733 return s;
734}
735
736BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
737
738 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700739 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700740
741 BC_UNUSED(scale);
742
743 s = bc_num_intop(a, b, c, &val);
744 if (s) return s;
745
746 bc_num_shift(c, val);
747
748 return s;
749}
750
751BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
752
753 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700754 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700755 size_t len;
756
757 BC_UNUSED(scale);
758
759 s = bc_num_intop(a, b, c, &val);
760 if (s) return s;
761
762 len = c->rdx + val;
763
764 if (len > c->len) {
765
Gavin Howard92037352019-01-02 14:58:35 -0700766 if (len > BC_MAX_NUM) return bc_vm_err(BC_ERROR_MATH_UNDERFLOW);
Gavin Howardac4d9122018-12-27 23:59:12 -0700767 if (len > c->cap) bc_num_expand(c, len);
768
769 memset(c->num + c->len, 0, len - c->len);
770 c->len = len;
771 }
772
773 c->rdx += val;
774
775 assert(c->rdx <= c->len && c->len <= c->cap);
776
777 return s;
778}
Gavin Howard7bda4782018-12-28 09:53:22 -0700779#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700780
Gavin Howard773c86b2018-11-02 14:07:19 -0600781BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
782 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700783{
Gavin Howarda1c44392018-09-27 12:41:15 -0600784 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600785 BcNum num2, *ptr_a, *ptr_b;
786 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700787
Gavin Howard63738202018-09-26 15:34:20 -0600788 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700789
Gavin Howard890d0c02018-10-30 16:34:50 -0600790 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600791 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600792 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600793 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600794 }
795 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700796
Gavin Howard63738202018-09-26 15:34:20 -0600797 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600798 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600799 if (c != a) {
800 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600801 init = true;
802 }
803 }
804 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700805
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600806 if (init) bc_num_init(c, req);
807 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700808
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600809 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700810
Gavin Howardddce6d42019-01-03 14:07:06 -0700811 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700812 assert(c->rdx <= c->len || !c->len);
Gavin Howard3cf769e2018-10-11 13:55:11 -0600813
Gavin Howardba009802018-09-29 04:41:51 -0600814 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600815
Gavin Howarda1c44392018-09-27 12:41:15 -0600816 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700817}
818
Gavin Howardc90ed902019-01-02 13:07:49 -0700819#ifndef NDEBUG
Gavin Howardc7d655b2018-12-28 19:45:13 -0700820bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700821
Gavin Howardc7d655b2018-12-28 19:45:13 -0700822 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -0600823 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700824
Gavin Howard63738202018-09-26 15:34:20 -0600825 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700826
Gavin Howard63738202018-09-26 15:34:20 -0600827 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700828
Gavin Howard8a921bd2018-10-11 14:15:32 -0600829 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600830
Gavin Howard63738202018-09-26 15:34:20 -0600831 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700832
Gavin Howard63738202018-09-26 15:34:20 -0600833 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700834
Gavin Howard63738202018-09-26 15:34:20 -0600835 radix = true;
836 continue;
837 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700838
Gavin Howardc7d655b2018-12-28 19:45:13 -0700839 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -0600840 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700841
Gavin Howard63738202018-09-26 15:34:20 -0600842 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700843}
Gavin Howardc90ed902019-01-02 13:07:49 -0700844#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -0700845
Gavin Howardc7d655b2018-12-28 19:45:13 -0700846unsigned long bc_num_parseChar(char c, size_t base_t) {
847
848 if (isupper(c)) {
849 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -0700850 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -0700851 }
852 else c -= '0';
853
854 return (unsigned long) (uchar) c;
855}
856
Gavin Howard3c02ed32018-12-28 18:17:15 -0700857void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700858
Gavin Howard63738202018-09-26 15:34:20 -0600859 size_t len, i;
860 const char *ptr;
861 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700862
Gavin Howard63738202018-09-26 15:34:20 -0600863 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700864
Gavin Howard63738202018-09-26 15:34:20 -0600865 val += i;
866 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700867
Gavin Howard1ab22d22019-01-03 13:32:17 -0700868 if (len) {
869 for (i = 0; zero && i < len; ++i) zero = (val[i] == '0') || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600870 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600871 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600872
Gavin Howard63738202018-09-26 15:34:20 -0600873 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700874
Gavin Howard63738202018-09-26 15:34:20 -0600875 // Explicitly test for NULL here to produce either a 0 or 1.
876 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700877
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600878 if (!zero) {
Gavin Howard8dd307e2019-01-08 23:05:19 -0700879 for (i = len - 1; i < len; ++n->len, --i) {
880
Gavin Howardc7d655b2018-12-28 19:45:13 -0700881 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -0700882
883 if (c == '.') n->len -= 1;
884 else {
885 if (isupper(c)) c = '9';
886 n->num[n->len] = c - '0';
887 }
Gavin Howardc7d655b2018-12-28 19:45:13 -0700888 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600889 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700890}
891
Gavin Howardddce6d42019-01-03 14:07:06 -0700892BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val, BcNum *restrict base, size_t base_t) {
893
Gavin Howard63738202018-09-26 15:34:20 -0600894 BcStatus s;
895 BcNum temp, mult, result;
Gavin Howard223fa6d2018-12-31 12:24:25 -0700896 BcDig c;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600897 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600898 unsigned long v;
899 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700900
Gavin Howard11b9afd2018-10-18 14:23:28 -0600901 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howard7536dcf2018-12-15 19:27:09 -0700902 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700903
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600904 bc_num_init(&temp, BC_NUM_DEF_SIZE);
905 bc_num_init(&mult, BC_NUM_DEF_SIZE);
Gavin Howardede51f02018-03-02 12:30:00 -0700906
Gavin Howardc7d655b2018-12-28 19:45:13 -0700907 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600908
Gavin Howardc7d655b2018-12-28 19:45:13 -0700909 v = bc_num_parseChar(c, base_t);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700910
Gavin Howard53eba8b2018-10-31 15:14:37 -0600911 s = bc_num_mul(n, base, &mult, 0);
912 if (s) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700913 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600914 s = bc_num_add(&mult, &temp, n, 0);
915 if (s) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600916 }
Gavin Howardede51f02018-03-02 12:30:00 -0700917
Gavin Howard1ab22d22019-01-03 13:32:17 -0700918 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600919
Gavin Howard63738202018-09-26 15:34:20 -0600920 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600921 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -0600922 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700923
Gavin Howardc7d655b2018-12-28 19:45:13 -0700924 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600925
Gavin Howardc7d655b2018-12-28 19:45:13 -0700926 v = bc_num_parseChar(c, base_t);
Gavin Howardede51f02018-03-02 12:30:00 -0700927
Gavin Howard53eba8b2018-10-31 15:14:37 -0600928 s = bc_num_mul(&result, base, &result, 0);
929 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700930 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600931 s = bc_num_add(&result, &temp, &result, 0);
932 if (s) goto err;
933 s = bc_num_mul(&mult, base, &mult, 0);
934 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600935 }
Gavin Howardede51f02018-03-02 12:30:00 -0700936
Gavin Howard53eba8b2018-10-31 15:14:37 -0600937 s = bc_num_div(&result, &mult, &result, digits);
938 if (s) goto err;
939 s = bc_num_add(n, &result, n, digits);
940 if (s) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600941
Gavin Howardddce6d42019-01-03 14:07:06 -0700942 if (BC_NUM_NONZERO(n)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600943 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600944 }
945 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700946
947err:
Gavin Howard63738202018-09-26 15:34:20 -0600948 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700949int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600950 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -0600951 bc_num_free(&temp);
Gavin Howard7536dcf2018-12-15 19:27:09 -0700952 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700953}
954
Gavin Howardab5e9632019-01-02 13:32:02 -0700955void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -0700956 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -0600957 bc_vm_putchar('\\');
958 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -0700959 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600960 }
Gavin Howard80977b22018-09-06 14:29:13 -0600961}
962
Gavin Howard40a085f2018-12-03 12:08:59 -0700963#if DC_ENABLED
Gavin Howardab5e9632019-01-02 13:32:02 -0700964void bc_num_printChar(size_t n, size_t len, bool rdx)
Gavin Howard83eb8392018-10-09 01:21:19 -0600965{
Gavin Howard3c02ed32018-12-28 18:17:15 -0700966 BC_UNUSED(rdx);
967 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -0700968 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600969}
970#endif // DC_ENABLED
971
Gavin Howardab5e9632019-01-02 13:32:02 -0700972void bc_num_printDigits(size_t n, size_t len, bool rdx)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700973{
Gavin Howarda84ad992018-12-03 19:11:06 -0700974 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -0600975
Gavin Howardab5e9632019-01-02 13:32:02 -0700976 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700977 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -0700978 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600979
Gavin Howardab5e9632019-01-02 13:32:02 -0700980 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700981 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600982
Gavin Howardab5e9632019-01-02 13:32:02 -0700983 for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -0700984 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -0700985 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700986 dig = n / pow;
987 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -0700988 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -0600989 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700990}
Gavin Howardfe679f02018-02-14 15:50:09 -0700991
Gavin Howardab5e9632019-01-02 13:32:02 -0700992void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -0700993
Gavin Howard3c02ed32018-12-28 18:17:15 -0700994 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600995
Gavin Howard3c02ed32018-12-28 18:17:15 -0700996 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -0700997 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -0600998 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -0700999 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001000 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001001
Gavin Howardab5e9632019-01-02 13:32:02 -07001002 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001003 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001004 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001005}
1006
Gavin Howardab5e9632019-01-02 13:32:02 -07001007void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001008
Gavin Howard63738202018-09-26 15:34:20 -06001009 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001010
Gavin Howard48af52e2018-10-30 14:47:38 -06001011 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001012 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001013
Gavin Howard48af52e2018-10-30 14:47:38 -06001014 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001015 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001016}
1017
Gavin Howardddce6d42019-01-03 14:07:06 -07001018BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base, size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001019{
Gavin Howard63738202018-09-26 15:34:20 -06001020 BcStatus s;
1021 BcVec stack;
1022 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001023 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001024 size_t i;
1025 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001026
Gavin Howardddce6d42019-01-03 14:07:06 -07001027 if (BC_NUM_ZERO(n)) {
1028 print(0, len, false);
Gavin Howard48af52e2018-10-30 14:47:38 -06001029 return BC_STATUS_SUCCESS;
1030 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001031
Gavin Howardd3929832018-12-24 15:13:15 -07001032 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001033 bc_num_init(&intp, n->len);
1034 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001035 bc_num_init(&digit, len);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001036 bc_num_init(&frac_len, BC_NUM_INT(n));
1037 bc_num_copy(&intp, n);
Gavin Howard63738202018-09-26 15:34:20 -06001038 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -06001039
Gavin Howard63738202018-09-26 15:34:20 -06001040 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001041 s = bc_num_sub(n, &intp, &fracp, 0);
1042 if (s) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001043
Gavin Howardddce6d42019-01-03 14:07:06 -07001044 while (BC_NUM_NONZERO(&intp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001045 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
1046 if (s) goto err;
1047 s = bc_num_ulong(&digit, &dig);
1048 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001049 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001050 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001051
Gavin Howard63738202018-09-26 15:34:20 -06001052 for (i = 0; i < stack.len; ++i) {
1053 ptr = bc_vec_item_rev(&stack, i);
1054 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001055 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001056 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001057
Gavin Howard63738202018-09-26 15:34:20 -06001058 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001059
Gavin Howard63738202018-09-26 15:34:20 -06001060 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001061 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
1062 if (s) goto err;
1063 s = bc_num_ulong(&fracp, &dig);
1064 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001065 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001066 s = bc_num_sub(&fracp, &intp, &fracp, 0);
1067 if (s) goto err;
Gavin Howardddce6d42019-01-03 14:07:06 -07001068 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001069 s = bc_num_mul(&frac_len, base, &frac_len, 0);
1070 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001071 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001072
1073err:
Gavin Howard63738202018-09-26 15:34:20 -06001074 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001075 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001076 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001077 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001078 bc_vec_free(&stack);
Gavin Howard63738202018-09-26 15:34:20 -06001079 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001080}
1081
Gavin Howardbe4e8222019-01-03 13:34:16 -07001082BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base, size_t base_t) {
Gavin Howard5a0616e2019-01-03 13:33:50 -07001083
Gavin Howard83eb8392018-10-09 01:21:19 -06001084 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001085 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001086 BcNumDigitOp print;
1087 bool neg = n->neg;
1088
Gavin Howard48af52e2018-10-30 14:47:38 -06001089 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001090 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001091
1092 n->neg = false;
1093
Gavin Howardbe4e8222019-01-03 13:34:16 -07001094 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001095 width = 1;
1096 print = bc_num_printHex;
1097 }
1098 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001099 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001100 print = bc_num_printDigits;
1101 }
1102
Gavin Howardab5e9632019-01-02 13:32:02 -07001103 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001104 n->neg = neg;
1105
1106 return s;
1107}
1108
Gavin Howard40a085f2018-12-03 12:08:59 -07001109#if DC_ENABLED
Gavin Howardab5e9632019-01-02 13:32:02 -07001110BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1111 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001112}
1113#endif // DC_ENABLED
1114
Gavin Howard3c02ed32018-12-28 18:17:15 -07001115void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001116 assert(n);
1117 n->num = num;
1118 n->cap = cap;
1119 n->rdx = n->len = 0;
1120 n->neg = false;
1121}
1122
Gavin Howard3c02ed32018-12-28 18:17:15 -07001123void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001124 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001125 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001126 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001127}
1128
Gavin Howard3c02ed32018-12-28 18:17:15 -07001129void bc_num_expand(BcNum *restrict n, size_t req) {
Gavin Howard844e7a52018-10-09 11:46:39 -06001130 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001131 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1132 if (req > n->cap) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001133 n->num = bc_vm_realloc(n->num, req);
1134 n->cap = req;
1135 }
Gavin Howardb5c77212018-02-14 17:12:34 -07001136}
1137
Gavin Howarded392aa2018-02-27 13:09:26 -07001138void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001139 assert(num);
1140 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001141}
1142
Gavin Howard3c02ed32018-12-28 18:17:15 -07001143void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001144 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001145 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001146 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001147 d->len = s->len;
1148 d->neg = s->neg;
1149 d->rdx = s->rdx;
1150 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001151}
1152
Gavin Howard3c02ed32018-12-28 18:17:15 -07001153BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
Gavin Howardd746e802018-12-28 22:06:00 -07001154 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001155{
Gavin Howard7536dcf2018-12-15 19:27:09 -07001156 BcStatus s = BC_STATUS_SUCCESS;
1157
Gavin Howard63738202018-09-26 15:34:20 -06001158 assert(n && val && base);
Gavin Howard96edeb32018-12-31 09:33:22 -07001159 assert(base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase);
Gavin Howardc90ed902019-01-02 13:07:49 -07001160 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001161
Gavin Howardd746e802018-12-28 22:06:00 -07001162 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howardc7d655b2018-12-28 19:45:13 -07001163 else if (base_t == 10) bc_num_parseDecimal(n, val);
1164 else s = bc_num_parseBase(n, val, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001165
Gavin Howard7536dcf2018-12-15 19:27:09 -07001166 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001167}
1168
Gavin Howardab5e9632019-01-02 13:32:02 -07001169BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1170 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001171{
Gavin Howard48af52e2018-10-30 14:47:38 -06001172 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001173
Gavin Howardab5e9632019-01-02 13:32:02 -07001174 assert(n && base);
Gavin Howard63738202018-09-26 15:34:20 -06001175 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001176
Gavin Howardab5e9632019-01-02 13:32:02 -07001177 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001178
Gavin Howardddce6d42019-01-03 14:07:06 -07001179 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howardab5e9632019-01-02 13:32:02 -07001180 else if (base_t == 10) bc_num_printDecimal(n);
1181 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001182
Gavin Howardddce6d42019-01-03 14:07:06 -07001183 if (!s && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001184 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001185 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001186 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001187
Gavin Howard63738202018-09-26 15:34:20 -06001188 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001189}
1190
Gavin Howard3c02ed32018-12-28 18:17:15 -07001191BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001192
Gavin Howard63738202018-09-26 15:34:20 -06001193 size_t i;
Gavin Howard7b557da2018-12-21 10:25:32 -07001194 unsigned long pow, r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001195
Gavin Howard63738202018-09-26 15:34:20 -06001196 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001197
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001198 *result = 0;
1199
Gavin Howard7536dcf2018-12-15 19:27:09 -07001200 if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001201
Gavin Howard7b557da2018-12-21 10:25:32 -07001202 for (r = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001203
Gavin Howard7b557da2018-12-21 10:25:32 -07001204 unsigned long prev = r, powprev = pow;
Gavin Howard88c25302018-10-17 13:32:23 -06001205
Gavin Howard7b557da2018-12-21 10:25:32 -07001206 r += ((unsigned long) n->num[i]) * pow;
Gavin Howard63738202018-09-26 15:34:20 -06001207 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001208
Gavin Howard92037352019-01-02 14:58:35 -07001209 if (r < prev || pow < powprev)
1210 return bc_vm_verr(BC_ERROR_MATH_OVERFLOW, "number cannot fit");
Gavin Howard63738202018-09-26 15:34:20 -06001211 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001212
Gavin Howard7b557da2018-12-21 10:25:32 -07001213 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001214
Gavin Howard63738202018-09-26 15:34:20 -06001215 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001216}
1217
Gavin Howard3c02ed32018-12-28 18:17:15 -07001218void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001219
Gavin Howardec82bdc2018-10-08 23:50:57 -06001220 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001221 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001222 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001223
Gavin Howard63738202018-09-26 15:34:20 -06001224 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001225
Gavin Howard63738202018-09-26 15:34:20 -06001226 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001227
Gavin Howard1ab22d22019-01-03 13:32:17 -07001228 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001229
Gavin Howard1ab22d22019-01-03 13:32:17 -07001230 len = bc_num_log10(ULONG_MAX);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001231 bc_num_expand(n, len);
Gavin Howardec82bdc2018-10-08 23:50:57 -06001232 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001233}
1234
Gavin Howard12fe7812018-09-29 03:45:18 -06001235BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001236 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001237 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001238 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001239}
1240
Gavin Howard12fe7812018-09-29 03:45:18 -06001241BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001242 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001243 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001244 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001245}
1246
Gavin Howard12fe7812018-09-29 03:45:18 -06001247BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001248 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001249 return bc_num_binary(a, b, c, scale, bc_num_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001250}
1251
Gavin Howard12fe7812018-09-29 03:45:18 -06001252BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001253 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001254 return bc_num_binary(a, b, c, scale, bc_num_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001255}
1256
Gavin Howard54b946a2018-10-23 12:06:57 -06001257BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001258 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001259 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001260}
1261
Gavin Howard12fe7812018-09-29 03:45:18 -06001262BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001263 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001264}
1265
Gavin Howard7d8f0382018-12-27 16:15:13 -07001266#if BC_ENABLE_EXTRA_MATH
1267BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001268 return bc_num_binary(a, b, c, scale, bc_num_place, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001269}
1270
1271BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001272 return bc_num_binary(a, b, c, scale, bc_num_left, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001273}
1274
1275BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001276 return bc_num_binary(a, b, c, scale, bc_num_right, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001277}
1278#endif // BC_ENABLE_EXTRA_MATH
1279
Gavin Howard3c02ed32018-12-28 18:17:15 -07001280BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001281
Gavin Howard63738202018-09-26 15:34:20 -06001282 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001283 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001284 size_t pow, len, digs, digs1, resrdx, req, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001285 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001286 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001287
Gavin Howard34edd0a2018-10-25 15:51:05 -06001288 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001289
Gavin Howardd8d60c42018-10-29 12:13:09 -06001290 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001291 bc_num_expand(b, req);
Gavin Howard954276b2018-05-16 01:43:58 -06001292
Gavin Howardddce6d42019-01-03 14:07:06 -07001293 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001294 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001295 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001296 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001297 if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1298 if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001299 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001300 bc_num_extend(b, scale);
1301 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001302 }
Gavin Howard954276b2018-05-16 01:43:58 -06001303
Gavin Howard890d0c02018-10-30 16:34:50 -06001304 scale = BC_MAX(scale, a->rdx) + 1;
1305 len = a->len + scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001306
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001307 bc_num_init(&num1, len);
1308 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001309 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001310
Gavin Howard63738202018-09-26 15:34:20 -06001311 bc_num_one(&half);
1312 half.num[0] = 5;
1313 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001314
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001315 bc_num_init(&f, len);
1316 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001317
Gavin Howard63738202018-09-26 15:34:20 -06001318 x0 = &num1;
1319 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001320
Gavin Howard63738202018-09-26 15:34:20 -06001321 bc_num_one(x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001322 pow = BC_NUM_INT(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001323
Gavin Howard53eba8b2018-10-31 15:14:37 -06001324 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001325
Gavin Howardc39fd492018-10-04 10:07:03 -06001326 if (pow & 1) x0->num[0] = 2;
1327 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001328
Gavin Howardc39fd492018-10-04 10:07:03 -06001329 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001330
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001331 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001332
Gavin Howard63738202018-09-26 15:34:20 -06001333 // Make sure to move the radix back.
1334 x0->rdx -= pow;
1335 }
Gavin Howard954276b2018-05-16 01:43:58 -06001336
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001337 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001338 resrdx = scale + 2;
1339 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001340
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001341 while (!BC_SIGNAL && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001342
Gavin Howard53eba8b2018-10-31 15:14:37 -06001343 s = bc_num_div(a, x0, &f, resrdx);
1344 if (s) goto err;
1345 s = bc_num_add(x0, &f, &fprime, resrdx);
1346 if (s) goto err;
1347 s = bc_num_mul(&fprime, &half, x1, resrdx);
1348 if (s) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001349
Gavin Howard53eba8b2018-10-31 15:14:37 -06001350 cmp = bc_num_cmp(x1, x0);
1351 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001352
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001353 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001354 else times = 0;
1355
1356 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001357
1358 cmp2 = cmp1;
1359 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001360 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001361
Gavin Howard63738202018-09-26 15:34:20 -06001362 temp = x0;
1363 x0 = x1;
1364 x1 = temp;
1365 }
Gavin Howard954276b2018-05-16 01:43:58 -06001366
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001367 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -07001368 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -06001369 goto err;
1370 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001371
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001372 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001373 scale -= 1;
1374 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001375
1376err:
Gavin Howard63738202018-09-26 15:34:20 -06001377 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001378 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001379 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001380 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001381 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001382 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001383 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001384}
Gavin Howardba009802018-09-29 04:41:51 -06001385
Gavin Howard2cb39612018-10-22 08:46:48 -06001386BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1387
1388 BcStatus s;
1389 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001390 bool init = false;
Gavin Howard2cb39612018-10-22 08:46:48 -06001391 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
1392
1393 assert(c != d && a != b && a != d && b != d && b != c);
1394
Gavin Howard890d0c02018-10-30 16:34:50 -06001395 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001396 memcpy(&num2, c, sizeof(BcNum));
1397 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001398 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001399 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001400 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001401 else {
1402 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001403 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001404 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001405
Gavin Howard54b946a2018-10-23 12:06:57 -06001406 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001407
Gavin Howardddce6d42019-01-03 14:07:06 -07001408 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001409 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001410 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001411 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001412
1413 if (init) bc_num_free(&num2);
1414
1415 return s;
1416}
1417
Gavin Howard40a085f2018-12-03 12:08:59 -07001418#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001419BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001420
1421 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001422 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001423 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001424
Gavin Howard34edd0a2018-10-25 15:51:05 -06001425 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001426
Gavin Howardddce6d42019-01-03 14:07:06 -07001427 if (BC_NUM_ZERO(c)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard7536dcf2018-12-15 19:27:09 -07001428 if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1429 if (a->rdx || b->rdx || c->rdx)
1430 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001431
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001432 bc_num_expand(d, c->len);
1433 bc_num_init(&base, c->len);
1434 bc_num_init(&exp, b->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001435 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001436 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001437
1438 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001439 two.num[0] = 2;
1440 bc_num_one(d);
1441
Gavin Howard53eba8b2018-10-31 15:14:37 -06001442 s = bc_num_rem(a, c, &base, 0);
1443 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001444 bc_num_copy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001445
Gavin Howardddce6d42019-01-03 14:07:06 -07001446 while (BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001447
Gavin Howard53eba8b2018-10-31 15:14:37 -06001448 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
1449 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001450
1451 if (BC_NUM_ONE(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001452 s = bc_num_mul(d, &base, &temp, 0);
1453 if (s) goto err;
1454 s = bc_num_rem(&temp, c, d, 0);
1455 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001456 }
1457
Gavin Howard53eba8b2018-10-31 15:14:37 -06001458 s = bc_num_mul(&base, &base, &temp, 0);
1459 if (s) goto err;
1460 s = bc_num_rem(&temp, c, &base, 0);
1461 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001462 }
1463
1464err:
1465 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001466 bc_num_free(&exp);
Gavin Howardba009802018-09-29 04:41:51 -06001467 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001468 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001469 return s;
1470}
Gavin Howarde6e84762018-10-03 11:46:34 -06001471#endif // DC_ENABLED