blob: 57435e37a77a2369c337de78b0cb0b860f925b6a [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 Howard63738202018-09-26 15:34:20 -060073 }
74 }
Gavin Howard5dbb48b2019-01-04 11:29:29 -070075 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060076}
77
Gavin Howard3c02ed32018-12-28 18:17:15 -070078ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
79 size_t len)
80{
Gavin Howard63738202018-09-26 15:34:20 -060081 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -060082 int c = 0;
Gavin Howard5dbb48b2019-01-04 11:29:29 -070083 for (i = len - 1; !BC_SIGNAL && i < len && !(c = a[i] - b[i]); --i);
Gavin Howard7fbb4e22018-10-18 11:43:17 -060084 return BC_NUM_NEG(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -060085}
86
Gavin Howard3c02ed32018-12-28 18:17:15 -070087ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070088
Gavin Howard63738202018-09-26 15:34:20 -060089 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -060090 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060091 bool a_max, neg = false;
92 ssize_t cmp;
93
94 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070095
Gavin Howard8a921bd2018-10-11 14:15:32 -060096 if (a == b) return 0;
Gavin Howardddce6d42019-01-03 14:07:06 -070097 if (BC_NUM_ZERO(a)) return BC_NUM_NEG(!!b->len, !b->neg);
98 if (BC_NUM_ZERO(b)) return BC_NUM_CMP_ZERO(a);
Gavin Howard890d0c02018-10-30 16:34:50 -060099 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600100 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600101 else return -1;
102 }
103 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700104
Gavin Howard63738202018-09-26 15:34:20 -0600105 a_int = BC_NUM_INT(a);
106 b_int = BC_NUM_INT(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600107 a_int -= b_int;
108 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700109
Gavin Howard1ab22d22019-01-03 13:32:17 -0700110 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700111
Gavin Howard890d0c02018-10-30 16:34:50 -0600112 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600113 min = b->rdx;
114 diff = a->rdx - b->rdx;
115 max_num = a->num + diff;
116 min_num = b->num;
117 }
118 else {
119 min = a->rdx;
120 diff = b->rdx - a->rdx;
121 max_num = b->num + diff;
122 min_num = a->num;
123 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700124
Gavin Howard890d0c02018-10-30 16:34:50 -0600125 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700126 if (cmp) return BC_NUM_NEG(cmp, (!a_max) != neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700127
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700128 for (max_num -= diff, i = diff - 1; !BC_SIGNAL && i < diff; --i) {
Gavin Howard40974152018-10-18 14:23:01 -0600129 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
Gavin Howard63738202018-09-26 15:34:20 -0600130 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700131
Gavin Howard63738202018-09-26 15:34:20 -0600132 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700133}
134
Gavin Howard3c02ed32018-12-28 18:17:15 -0700135void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700136
Gavin Howardddce6d42019-01-03 14:07:06 -0700137 assert(places <= n->rdx && (BC_NUM_ZERO(n) || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700138
Gavin Howard1ab22d22019-01-03 13:32:17 -0700139 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700140
Gavin Howard63738202018-09-26 15:34:20 -0600141 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700142
Gavin Howardddce6d42019-01-03 14:07:06 -0700143 if (BC_NUM_NONZERO(n)) {
Gavin Howard955da852018-10-23 11:08:20 -0600144 n->len -= places;
145 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
146 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700147}
148
Gavin Howard3c02ed32018-12-28 18:17:15 -0700149void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700150
Gavin Howard88c25302018-10-17 13:32:23 -0600151 size_t len = n->len + places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700152
Gavin Howard1ab22d22019-01-03 13:32:17 -0700153 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700154
Gavin Howard1ab22d22019-01-03 13:32:17 -0700155 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700156
Gavin Howard1ab22d22019-01-03 13:32:17 -0700157 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
158 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700159
Gavin Howard1ab22d22019-01-03 13:32:17 -0700160 n->len += places;
161 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700162}
163
Gavin Howard3c02ed32018-12-28 18:17:15 -0700164void bc_num_clean(BcNum *restrict n) {
Gavin Howardddce6d42019-01-03 14:07:06 -0700165 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
166 if (BC_NUM_ZERO(n)) n->neg = false;
Gavin Howard63738202018-09-26 15:34:20 -0600167 else if (n->len < n->rdx) n->len = n->rdx;
Gavin Howarde57525d2018-09-01 16:56:21 -0600168}
169
Gavin Howard3c02ed32018-12-28 18:17:15 -0700170void bc_num_retireMul(BcNum *restrict n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600171
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600172 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600173 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600174
Gavin Howard305249a2018-10-15 20:24:47 -0600175 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700176 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600177}
178
Gavin Howard50c80022019-01-03 15:14:33 -0700179void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a, BcNum *restrict b) {
180
Gavin Howard305249a2018-10-15 20:24:47 -0600181 if (idx < n->len) {
182
Gavin Howard305249a2018-10-15 20:24:47 -0600183 b->len = n->len - idx;
184 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600185 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600186
187 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
188 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard50c80022019-01-03 15:14:33 -0700189
190 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600191 }
Gavin Howard50c80022019-01-03 15:14:33 -0700192 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600193
194 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600195}
196
Gavin Howard3c02ed32018-12-28 18:17:15 -0700197BcStatus bc_num_shift(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600198
Gavin Howardddce6d42019-01-03 14:07:06 -0700199 if (!places || BC_NUM_ZERO(n)) return BC_STATUS_SUCCESS;
Gavin Howardecc7fc22018-12-31 13:21:47 -0700200 if (places + n->len > BC_MAX_NUM)
Gavin Howard92037352019-01-02 14:58:35 -0700201 return bc_vm_verr(BC_ERROR_MATH_OVERFLOW, "shifted left too far");
Gavin Howard305249a2018-10-15 20:24:47 -0600202
203 if (n->rdx >= places) n->rdx -= places;
204 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600205 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600206 n->rdx = 0;
207 }
208
209 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600210
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600211 return BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600212}
213
Gavin Howard773c86b2018-11-02 14:07:19 -0600214BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600215
Gavin Howard63738202018-09-26 15:34:20 -0600216 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600217 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600218
Gavin Howardd32d7df2018-10-15 08:42:25 -0600219 one.cap = 2;
220 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600221 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600222
Gavin Howardd32d7df2018-10-15 08:42:25 -0600223 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600224}
225
Gavin Howard7bda4782018-12-28 09:53:22 -0700226#if BC_ENABLE_EXTRA_MATH
Gavin Howard3c02ed32018-12-28 18:17:15 -0700227BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
228 unsigned long *v)
229{
Gavin Howardac4d9122018-12-27 23:59:12 -0700230 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
231 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700232 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700233}
Gavin Howard7bda4782018-12-28 09:53:22 -0700234#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700235
Gavin Howard773c86b2018-11-02 14:07:19 -0600236BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700237
Gavin Howard8a921bd2018-10-11 14:15:32 -0600238 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600239 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700240 unsigned int carry, in;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700241
Gavin Howard63738202018-09-26 15:34:20 -0600242 // Because this function doesn't need to use scale (per the bc spec),
243 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600244
Gavin Howardddce6d42019-01-03 14:07:06 -0700245 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600246 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700247 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600248 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600249 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700250 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600251 bc_num_copy(c, a);
252 return BC_STATUS_SUCCESS;
253 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600254
Gavin Howard63738202018-09-26 15:34:20 -0600255 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600256 c->rdx = BC_MAX(a->rdx, b->rdx);
257 min_rdx = BC_MIN(a->rdx, b->rdx);
258 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700259
Gavin Howard63738202018-09-26 15:34:20 -0600260 if (a->rdx > b->rdx) {
261 diff = a->rdx - b->rdx;
262 ptr = a->num;
263 ptr_a = a->num + diff;
264 ptr_b = b->num;
265 }
266 else {
267 diff = b->rdx - a->rdx;
268 ptr = b->num;
269 ptr_a = a->num;
270 ptr_b = b->num + diff;
271 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700272
Gavin Howard63738202018-09-26 15:34:20 -0600273 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700274
Gavin Howard63738202018-09-26 15:34:20 -0600275 ptr_c += diff;
Gavin Howard890d0c02018-10-30 16:34:50 -0600276 a_int = BC_NUM_INT(a);
277 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700278
Gavin Howard890d0c02018-10-30 16:34:50 -0600279 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600280 min_int = b_int;
281 max = a_int;
282 ptr = ptr_a;
283 }
284 else {
285 min_int = a_int;
286 max = b_int;
287 ptr = ptr_b;
288 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700289
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700290 for (carry = 0, i = 0; !BC_SIGNAL && i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700291 in = ((unsigned int) ptr_a[i]) + ((unsigned int) ptr_b[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600292 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600293 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600294 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700295
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700296 for (; !BC_SIGNAL && i < max + min_rdx; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700297 in = ((unsigned int) ptr[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600298 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600299 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600300 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700301
Gavin Howard1ab22d22019-01-03 13:32:17 -0700302 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600303
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700304 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700305}
306
Gavin Howard773c86b2018-11-02 14:07:19 -0600307BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700308
Gavin Howard63738202018-09-26 15:34:20 -0600309 BcStatus s;
310 ssize_t cmp;
311 BcNum *minuend, *subtrahend;
312 size_t start;
313 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700314
Gavin Howard63738202018-09-26 15:34:20 -0600315 // Because this function doesn't need to use scale (per the bc spec),
316 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700317
Gavin Howardddce6d42019-01-03 14:07:06 -0700318 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600319 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700320 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600321 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600322 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700323 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600324 bc_num_copy(c, a);
325 return BC_STATUS_SUCCESS;
326 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700327
Gavin Howard63738202018-09-26 15:34:20 -0600328 aneg = a->neg;
329 bneg = b->neg;
330 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700331
Gavin Howard63738202018-09-26 15:34:20 -0600332 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700333
Gavin Howard63738202018-09-26 15:34:20 -0600334 a->neg = aneg;
335 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700336
Gavin Howard1ab22d22019-01-03 13:32:17 -0700337 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600338 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600339 return BC_STATUS_SUCCESS;
340 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700341
342 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600343 neg = a->neg;
344 minuend = a;
345 subtrahend = b;
346 }
347 else {
348 neg = b->neg;
349 if (sub) neg = !neg;
350 minuend = b;
351 subtrahend = a;
352 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700353
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600354 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600355 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700356
Gavin Howard63738202018-09-26 15:34:20 -0600357 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600358 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600359 start = 0;
360 }
361 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700362
Gavin Howard63738202018-09-26 15:34:20 -0600363 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700364
Gavin Howard63738202018-09-26 15:34:20 -0600365 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700366
Gavin Howard63738202018-09-26 15:34:20 -0600367 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700368}
369
Gavin Howard3c02ed32018-12-28 18:17:15 -0700370BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600371
Gavin Howard63738202018-09-26 15:34:20 -0600372 BcStatus s;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700373 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600374 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard63738202018-09-26 15:34:20 -0600375 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600376
Gavin Howardddce6d42019-01-03 14:07:06 -0700377 // This is here because the function is recursive.
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700378 if (BC_SIGNAL) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700379 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600380 bc_num_zero(c);
381 return BC_STATUS_SUCCESS;
382 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700383 if (aone || BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600384 bc_num_copy(c, aone ? b : a);
385 return BC_STATUS_SUCCESS;
386 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700387
Gavin Howardb5093d72018-10-17 12:13:17 -0600388 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
389 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600390 {
Gavin Howard34c4ba02018-12-11 10:25:01 -0700391 size_t i, j, len;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700392 unsigned int carry;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700393
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600394 bc_num_expand(c, a->len + b->len + 1);
Gavin Howard23d8d252018-10-17 12:03:41 -0600395
Gavin Howard305249a2018-10-15 20:24:47 -0600396 memset(c->num, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700397 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700398
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700399 for (i = 0; !BC_SIGNAL && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600400
Gavin Howard34c4ba02018-12-11 10:25:01 -0700401 carry = 0;
402
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700403 for (j = 0; !BC_SIGNAL && j < a->len; ++j) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700404 unsigned int in = c->num[i + j];
405 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
406 in += carry;
Gavin Howard305249a2018-10-15 20:24:47 -0600407 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600408 c->num[i + j] = (BcDig) (in % 10);
Gavin Howard305249a2018-10-15 20:24:47 -0600409 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700410
Gavin Howard88c25302018-10-17 13:32:23 -0600411 c->num[i + j] += (BcDig) carry;
412 len = BC_MAX(len, i + j + !!carry);
Gavin Howard63738202018-09-26 15:34:20 -0600413 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700414
Gavin Howard305249a2018-10-15 20:24:47 -0600415 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600416
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700417 return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600418 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700419
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600420 bc_num_init(&l1, max);
421 bc_num_init(&h1, max);
422 bc_num_init(&l2, max);
423 bc_num_init(&h2, max);
424 bc_num_init(&m1, max);
425 bc_num_init(&m2, max);
426 bc_num_init(&z0, max);
427 bc_num_init(&z1, max);
428 bc_num_init(&z2, max);
429 bc_num_init(&temp, max + max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600430
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600431 bc_num_split(a, max2, &l1, &h1);
432 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700433
Gavin Howard53eba8b2018-10-31 15:14:37 -0600434 s = bc_num_add(&h1, &l1, &m1, 0);
435 if (s) goto err;
436 s = bc_num_add(&h2, &l2, &m2, 0);
437 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600438
Gavin Howard53eba8b2018-10-31 15:14:37 -0600439 s = bc_num_k(&h1, &h2, &z0);
440 if (s) goto err;
441 s = bc_num_k(&m1, &m2, &z1);
442 if (s) goto err;
443 s = bc_num_k(&l1, &l2, &z2);
444 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600445
Gavin Howard53eba8b2018-10-31 15:14:37 -0600446 s = bc_num_sub(&z1, &z0, &temp, 0);
447 if (s) goto err;
448 s = bc_num_sub(&temp, &z2, &z1, 0);
449 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600450
Gavin Howard53eba8b2018-10-31 15:14:37 -0600451 s = bc_num_shift(&z0, max2 * 2);
452 if (s) goto err;
453 s = bc_num_shift(&z1, max2);
454 if (s) goto err;
455 s = bc_num_add(&z0, &z1, &temp, 0);
456 if (s) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600457 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600458
459err:
460 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600461 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600462 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600463 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600464 bc_num_free(&m2);
Gavin Howard305249a2018-10-15 20:24:47 -0600465 bc_num_free(&m1);
Gavin Howard305249a2018-10-15 20:24:47 -0600466 bc_num_free(&h2);
Gavin Howard305249a2018-10-15 20:24:47 -0600467 bc_num_free(&l2);
Gavin Howard305249a2018-10-15 20:24:47 -0600468 bc_num_free(&h1);
Gavin Howard305249a2018-10-15 20:24:47 -0600469 bc_num_free(&l1);
470 return s;
471}
472
Gavin Howard773c86b2018-11-02 14:07:19 -0600473BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600474
475 BcStatus s;
476 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600477 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600478
479 scale = BC_MAX(scale, a->rdx);
480 scale = BC_MAX(scale, b->rdx);
481 scale = BC_MIN(a->rdx + b->rdx, scale);
482 maxrdx = BC_MAX(maxrdx, scale);
483
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600484 bc_num_init(&cpa, a->len);
485 bc_num_init(&cpb, b->len);
Gavin Howard305249a2018-10-15 20:24:47 -0600486
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600487 bc_num_copy(&cpa, a);
488 bc_num_copy(&cpb, b);
Gavin Howard23d8d252018-10-17 12:03:41 -0600489 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600490
Gavin Howard53eba8b2018-10-31 15:14:37 -0600491 s = bc_num_shift(&cpa, maxrdx);
492 if (s) goto err;
493 s = bc_num_shift(&cpb, maxrdx);
494 if (s) goto err;
495 s = bc_num_k(&cpa, &cpb, c);
496 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600497
Gavin Howard53eba8b2018-10-31 15:14:37 -0600498 maxrdx += scale;
499 bc_num_expand(c, c->len + maxrdx);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600500
Gavin Howard88c25302018-10-17 13:32:23 -0600501 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600502 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600503 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600504 }
505
Gavin Howard88c25302018-10-17 13:32:23 -0600506 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600507 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600508
509err:
510 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600511 bc_num_free(&cpa);
512 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700513}
514
Gavin Howard773c86b2018-11-02 14:07:19 -0600515BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700516
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600517 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600518 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600519 size_t len, end, i;
520 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600521 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700522
Gavin Howardddce6d42019-01-03 14:07:06 -0700523 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
524 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600525 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600526 return BC_STATUS_SUCCESS;
527 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700528 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600529 bc_num_copy(c, a);
530 bc_num_retireMul(c, scale, a->neg, b->neg);
531 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600532 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700533
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600534 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
535 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600536 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700537
Gavin Howard890d0c02018-10-30 16:34:50 -0600538 if (len > cp.len) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600539 bc_num_expand(&cp, len + 2);
540 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600541 }
Gavin Howard021150b2018-03-10 15:40:42 -0700542
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600543 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600544 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600545 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700546
Gavin Howard63738202018-09-26 15:34:20 -0600547 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600548 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600549 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600550 len -= i - 1;
551 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700552
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600553 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700554
Gavin Howard63738202018-09-26 15:34:20 -0600555 // We want an extra zero in front to make things simpler.
556 cp.num[cp.len++] = 0;
557 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700558
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600559 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700560
Gavin Howarda2514a02018-10-18 14:20:09 -0600561 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600562 c->rdx = cp.rdx;
563 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600564 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700565
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700566 for (i = end - 1; !BC_SIGNAL && !s && i < end; --i) {
Gavin Howard63738202018-09-26 15:34:20 -0600567 n = cp.num + i;
Gavin Howard1ab22d22019-01-03 13:32:17 -0700568 for (q = 0; !s && (n[len] || bc_num_compare(n, p, len) >= 0); ++q)
Gavin Howard53eba8b2018-10-31 15:14:37 -0600569 s = bc_num_subArrays(n, p, len);
Gavin Howard63738202018-09-26 15:34:20 -0600570 c->num[i] = q;
571 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700572
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600573 if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600574 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600575
Gavin Howard63738202018-09-26 15:34:20 -0600576 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700577}
578
Gavin Howard773c86b2018-11-02 14:07:19 -0600579BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
580 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600581{
Gavin Howard63738202018-09-26 15:34:20 -0600582 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600583 BcNum temp;
584 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700585
Gavin Howardddce6d42019-01-03 14:07:06 -0700586 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
587 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700588 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600589 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600590 return BC_STATUS_SUCCESS;
591 }
Gavin Howard8b254872018-03-14 01:13:35 -0600592
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600593 bc_num_init(&temp, d->cap);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600594 bc_num_d(a, b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700595
Gavin Howard1ab22d22019-01-03 13:32:17 -0700596 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600597
Gavin Howard53eba8b2018-10-31 15:14:37 -0600598 s = bc_num_m(c, b, &temp, scale);
599 if (s) goto err;
600 s = bc_num_sub(a, &temp, d, scale);
601 if (s) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600602
Gavin Howardddce6d42019-01-03 14:07:06 -0700603 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600604
605 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600606 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600607 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700608
609err:
Gavin Howard18441082018-10-22 10:03:30 -0600610 bc_num_free(&temp);
611 return s;
612}
613
Gavin Howard773c86b2018-11-02 14:07:19 -0600614BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
615
Gavin Howard18441082018-10-22 10:03:30 -0600616 BcStatus s;
617 BcNum c1;
618 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
619
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600620 bc_num_init(&c1, len);
Gavin Howard54b946a2018-10-23 12:06:57 -0600621 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600622 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600623
Gavin Howard63738202018-09-26 15:34:20 -0600624 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700625}
626
Gavin Howard773c86b2018-11-02 14:07:19 -0600627BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700628
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600629 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600630 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700631 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600632 size_t i, powrdx, resrdx;
633 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700634
Gavin Howardac4d9122018-12-27 23:59:12 -0700635 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700636
Gavin Howardddce6d42019-01-03 14:07:06 -0700637 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600638 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600639 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600640 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700641 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600642 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600643 return BC_STATUS_SUCCESS;
644 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700645 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600646 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600647 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600648 return s;
649 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700650
Gavin Howard63738202018-09-26 15:34:20 -0600651 neg = b->neg;
652 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600653 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700654 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600655 if (s) return s;
656
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600657 bc_num_init(&copy, a->len);
658 bc_num_copy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700659
Gavin Howard63738202018-09-26 15:34:20 -0600660 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600661
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700662 for (powrdx = a->rdx; !BC_SIGNAL && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600663 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600664 s = bc_num_mul(&copy, &copy, &copy, powrdx);
665 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600666 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700667
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700668 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700669 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600670 goto err;
671 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700672
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600673 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700674 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600675
Gavin Howard4d5daba2019-01-07 14:42:50 -0700676 while (!BC_SIGNAL && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600677
Gavin Howard4d5daba2019-01-07 14:42:50 -0700678 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600679 s = bc_num_mul(&copy, &copy, &copy, powrdx);
680 if (s) goto err;
681
Gavin Howard63738202018-09-26 15:34:20 -0600682 if (pow & 1) {
683 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600684 s = bc_num_mul(c, &copy, c, resrdx);
685 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600686 }
687 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700688
Gavin Howard53eba8b2018-10-31 15:14:37 -0600689 if (neg) {
690 s = bc_num_inv(c, c, scale);
691 if (s) goto err;
692 }
693
Gavin Howard5dbb48b2019-01-04 11:29:29 -0700694 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700695 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600696 goto err;
697 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700698
Gavin Howard63738202018-09-26 15:34:20 -0600699 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700700
Gavin Howard53eba8b2018-10-31 15:14:37 -0600701 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600702 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600703 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700704
Gavin Howard6fbdb292018-02-27 15:44:48 -0700705err:
Gavin Howard63738202018-09-26 15:34:20 -0600706 bc_num_free(&copy);
707 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700708}
709
Gavin Howard7bda4782018-12-28 09:53:22 -0700710#if BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700711BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
712
713 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700714 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700715
716 BC_UNUSED(scale);
717
718 s = bc_num_intop(a, b, c, &val);
719 if (s) return s;
720
721 if (val < a->rdx) bc_num_truncate(c, a->rdx - val);
722 else if (val > a->rdx) bc_num_extend(c, val - a->rdx);
723
724 return s;
725}
726
727BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
728
729 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700730 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700731
732 BC_UNUSED(scale);
733
734 s = bc_num_intop(a, b, c, &val);
735 if (s) return s;
736
737 bc_num_shift(c, val);
738
739 return s;
740}
741
742BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
743
744 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700745 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700746 size_t len;
747
748 BC_UNUSED(scale);
749
750 s = bc_num_intop(a, b, c, &val);
751 if (s) return s;
752
753 len = c->rdx + val;
754
755 if (len > c->len) {
756
Gavin Howard92037352019-01-02 14:58:35 -0700757 if (len > BC_MAX_NUM) return bc_vm_err(BC_ERROR_MATH_UNDERFLOW);
Gavin Howardac4d9122018-12-27 23:59:12 -0700758 if (len > c->cap) bc_num_expand(c, len);
759
760 memset(c->num + c->len, 0, len - c->len);
761 c->len = len;
762 }
763
764 c->rdx += val;
765
766 assert(c->rdx <= c->len && c->len <= c->cap);
767
768 return s;
769}
Gavin Howard7bda4782018-12-28 09:53:22 -0700770#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700771
Gavin Howard773c86b2018-11-02 14:07:19 -0600772BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
773 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700774{
Gavin Howarda1c44392018-09-27 12:41:15 -0600775 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600776 BcNum num2, *ptr_a, *ptr_b;
777 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700778
Gavin Howard63738202018-09-26 15:34:20 -0600779 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700780
Gavin Howard890d0c02018-10-30 16:34:50 -0600781 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600782 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600783 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600784 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600785 }
786 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700787
Gavin Howard63738202018-09-26 15:34:20 -0600788 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600789 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600790 if (c != a) {
791 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600792 init = true;
793 }
794 }
795 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700796
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600797 if (init) bc_num_init(c, req);
798 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700799
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600800 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700801
Gavin Howardddce6d42019-01-03 14:07:06 -0700802 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard3cf769e2018-10-11 13:55:11 -0600803
Gavin Howardba009802018-09-29 04:41:51 -0600804 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600805
Gavin Howarda1c44392018-09-27 12:41:15 -0600806 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700807}
808
Gavin Howardc90ed902019-01-02 13:07:49 -0700809#ifndef NDEBUG
Gavin Howardc7d655b2018-12-28 19:45:13 -0700810bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700811
Gavin Howardc7d655b2018-12-28 19:45:13 -0700812 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -0600813 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700814
Gavin Howard63738202018-09-26 15:34:20 -0600815 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700816
Gavin Howard63738202018-09-26 15:34:20 -0600817 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700818
Gavin Howard8a921bd2018-10-11 14:15:32 -0600819 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600820
Gavin Howard63738202018-09-26 15:34:20 -0600821 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700822
Gavin Howard63738202018-09-26 15:34:20 -0600823 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700824
Gavin Howard63738202018-09-26 15:34:20 -0600825 radix = true;
826 continue;
827 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700828
Gavin Howardc7d655b2018-12-28 19:45:13 -0700829 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -0600830 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700831
Gavin Howard63738202018-09-26 15:34:20 -0600832 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700833}
Gavin Howardc90ed902019-01-02 13:07:49 -0700834#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -0700835
Gavin Howardc7d655b2018-12-28 19:45:13 -0700836unsigned long bc_num_parseChar(char c, size_t base_t) {
837
838 if (isupper(c)) {
839 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -0700840 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -0700841 }
842 else c -= '0';
843
844 return (unsigned long) (uchar) c;
845}
846
Gavin Howard3c02ed32018-12-28 18:17:15 -0700847void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700848
Gavin Howard63738202018-09-26 15:34:20 -0600849 size_t len, i;
850 const char *ptr;
851 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700852
Gavin Howard63738202018-09-26 15:34:20 -0600853 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700854
Gavin Howard63738202018-09-26 15:34:20 -0600855 val += i;
856 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700857
Gavin Howard1ab22d22019-01-03 13:32:17 -0700858 if (len) {
859 for (i = 0; zero && i < len; ++i) zero = (val[i] == '0') || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600860 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600861 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600862
Gavin Howard63738202018-09-26 15:34:20 -0600863 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700864
Gavin Howard63738202018-09-26 15:34:20 -0600865 // Explicitly test for NULL here to produce either a 0 or 1.
866 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700867
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600868 if (!zero) {
Gavin Howardc7d655b2018-12-28 19:45:13 -0700869 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.')) {
870 char c = val[i];
871 if (isupper(c)) c = '9';
872 n->num[n->len] = c - '0';
873 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600874 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700875}
876
Gavin Howardddce6d42019-01-03 14:07:06 -0700877BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val, BcNum *restrict base, size_t base_t) {
878
Gavin Howard63738202018-09-26 15:34:20 -0600879 BcStatus s;
880 BcNum temp, mult, result;
Gavin Howard223fa6d2018-12-31 12:24:25 -0700881 BcDig c;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600882 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600883 unsigned long v;
884 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700885
Gavin Howard11b9afd2018-10-18 14:23:28 -0600886 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howard7536dcf2018-12-15 19:27:09 -0700887 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700888
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600889 bc_num_init(&temp, BC_NUM_DEF_SIZE);
890 bc_num_init(&mult, BC_NUM_DEF_SIZE);
Gavin Howardede51f02018-03-02 12:30:00 -0700891
Gavin Howardc7d655b2018-12-28 19:45:13 -0700892 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600893
Gavin Howardc7d655b2018-12-28 19:45:13 -0700894 v = bc_num_parseChar(c, base_t);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700895
Gavin Howard53eba8b2018-10-31 15:14:37 -0600896 s = bc_num_mul(n, base, &mult, 0);
897 if (s) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700898 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600899 s = bc_num_add(&mult, &temp, n, 0);
900 if (s) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600901 }
Gavin Howardede51f02018-03-02 12:30:00 -0700902
Gavin Howard1ab22d22019-01-03 13:32:17 -0700903 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600904
Gavin Howard63738202018-09-26 15:34:20 -0600905 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600906 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -0600907 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700908
Gavin Howardc7d655b2018-12-28 19:45:13 -0700909 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600910
Gavin Howardc7d655b2018-12-28 19:45:13 -0700911 v = bc_num_parseChar(c, base_t);
Gavin Howardede51f02018-03-02 12:30:00 -0700912
Gavin Howard53eba8b2018-10-31 15:14:37 -0600913 s = bc_num_mul(&result, base, &result, 0);
914 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700915 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600916 s = bc_num_add(&result, &temp, &result, 0);
917 if (s) goto err;
918 s = bc_num_mul(&mult, base, &mult, 0);
919 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600920 }
Gavin Howardede51f02018-03-02 12:30:00 -0700921
Gavin Howard53eba8b2018-10-31 15:14:37 -0600922 s = bc_num_div(&result, &mult, &result, digits);
923 if (s) goto err;
924 s = bc_num_add(n, &result, n, digits);
925 if (s) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600926
Gavin Howardddce6d42019-01-03 14:07:06 -0700927 if (BC_NUM_NONZERO(n)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600928 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600929 }
930 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700931
932err:
Gavin Howard63738202018-09-26 15:34:20 -0600933 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700934int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600935 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -0600936 bc_num_free(&temp);
Gavin Howard7536dcf2018-12-15 19:27:09 -0700937 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700938}
939
Gavin Howardab5e9632019-01-02 13:32:02 -0700940void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -0700941 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -0600942 bc_vm_putchar('\\');
943 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -0700944 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600945 }
Gavin Howard80977b22018-09-06 14:29:13 -0600946}
947
Gavin Howard40a085f2018-12-03 12:08:59 -0700948#if DC_ENABLED
Gavin Howardab5e9632019-01-02 13:32:02 -0700949void bc_num_printChar(size_t n, size_t len, bool rdx)
Gavin Howard83eb8392018-10-09 01:21:19 -0600950{
Gavin Howard3c02ed32018-12-28 18:17:15 -0700951 BC_UNUSED(rdx);
952 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -0700953 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600954}
955#endif // DC_ENABLED
956
Gavin Howardab5e9632019-01-02 13:32:02 -0700957void bc_num_printDigits(size_t n, size_t len, bool rdx)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700958{
Gavin Howarda84ad992018-12-03 19:11:06 -0700959 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -0600960
Gavin Howardab5e9632019-01-02 13:32:02 -0700961 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700962 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -0700963 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600964
Gavin Howardab5e9632019-01-02 13:32:02 -0700965 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700966 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600967
Gavin Howardab5e9632019-01-02 13:32:02 -0700968 for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -0700969 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -0700970 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700971 dig = n / pow;
972 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -0700973 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -0600974 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700975}
Gavin Howardfe679f02018-02-14 15:50:09 -0700976
Gavin Howardab5e9632019-01-02 13:32:02 -0700977void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -0700978
Gavin Howard3c02ed32018-12-28 18:17:15 -0700979 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600980
Gavin Howard3c02ed32018-12-28 18:17:15 -0700981 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -0700982 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -0600983 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -0700984 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -0600985 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700986
Gavin Howardab5e9632019-01-02 13:32:02 -0700987 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700988 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -0700989 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700990}
991
Gavin Howardab5e9632019-01-02 13:32:02 -0700992void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700993
Gavin Howard63738202018-09-26 15:34:20 -0600994 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700995
Gavin Howard48af52e2018-10-30 14:47:38 -0600996 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -0700997 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700998
Gavin Howard48af52e2018-10-30 14:47:38 -0600999 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001000 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001001}
1002
Gavin Howardddce6d42019-01-03 14:07:06 -07001003BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base, size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001004{
Gavin Howard63738202018-09-26 15:34:20 -06001005 BcStatus s;
1006 BcVec stack;
1007 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001008 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001009 size_t i;
1010 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001011
Gavin Howardddce6d42019-01-03 14:07:06 -07001012 if (BC_NUM_ZERO(n)) {
1013 print(0, len, false);
Gavin Howard48af52e2018-10-30 14:47:38 -06001014 return BC_STATUS_SUCCESS;
1015 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001016
Gavin Howardd3929832018-12-24 15:13:15 -07001017 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001018 bc_num_init(&intp, n->len);
1019 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001020 bc_num_init(&digit, len);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001021 bc_num_init(&frac_len, BC_NUM_INT(n));
1022 bc_num_copy(&intp, n);
Gavin Howard63738202018-09-26 15:34:20 -06001023 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -06001024
Gavin Howard63738202018-09-26 15:34:20 -06001025 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001026 s = bc_num_sub(n, &intp, &fracp, 0);
1027 if (s) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001028
Gavin Howardddce6d42019-01-03 14:07:06 -07001029 while (BC_NUM_NONZERO(&intp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001030 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
1031 if (s) goto err;
1032 s = bc_num_ulong(&digit, &dig);
1033 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001034 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001035 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001036
Gavin Howard63738202018-09-26 15:34:20 -06001037 for (i = 0; i < stack.len; ++i) {
1038 ptr = bc_vec_item_rev(&stack, i);
1039 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001040 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001041 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001042
Gavin Howard63738202018-09-26 15:34:20 -06001043 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001044
Gavin Howard63738202018-09-26 15:34:20 -06001045 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001046 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
1047 if (s) goto err;
1048 s = bc_num_ulong(&fracp, &dig);
1049 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001050 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001051 s = bc_num_sub(&fracp, &intp, &fracp, 0);
1052 if (s) goto err;
Gavin Howardddce6d42019-01-03 14:07:06 -07001053 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001054 s = bc_num_mul(&frac_len, base, &frac_len, 0);
1055 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001056 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001057
1058err:
Gavin Howard63738202018-09-26 15:34:20 -06001059 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001060 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001061 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001062 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001063 bc_vec_free(&stack);
Gavin Howard63738202018-09-26 15:34:20 -06001064 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001065}
1066
Gavin Howardbe4e8222019-01-03 13:34:16 -07001067BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base, size_t base_t) {
Gavin Howard5a0616e2019-01-03 13:33:50 -07001068
Gavin Howard83eb8392018-10-09 01:21:19 -06001069 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001070 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001071 BcNumDigitOp print;
1072 bool neg = n->neg;
1073
Gavin Howard48af52e2018-10-30 14:47:38 -06001074 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001075 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001076
1077 n->neg = false;
1078
Gavin Howardbe4e8222019-01-03 13:34:16 -07001079 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001080 width = 1;
1081 print = bc_num_printHex;
1082 }
1083 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001084 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001085 print = bc_num_printDigits;
1086 }
1087
Gavin Howardab5e9632019-01-02 13:32:02 -07001088 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001089 n->neg = neg;
1090
1091 return s;
1092}
1093
Gavin Howard40a085f2018-12-03 12:08:59 -07001094#if DC_ENABLED
Gavin Howardab5e9632019-01-02 13:32:02 -07001095BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1096 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001097}
1098#endif // DC_ENABLED
1099
Gavin Howard3c02ed32018-12-28 18:17:15 -07001100void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001101 assert(n);
1102 n->num = num;
1103 n->cap = cap;
1104 n->rdx = n->len = 0;
1105 n->neg = false;
1106}
1107
Gavin Howard3c02ed32018-12-28 18:17:15 -07001108void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001109 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001110 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001111 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001112}
1113
Gavin Howard3c02ed32018-12-28 18:17:15 -07001114void bc_num_expand(BcNum *restrict n, size_t req) {
Gavin Howard844e7a52018-10-09 11:46:39 -06001115 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001116 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1117 if (req > n->cap) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001118 n->num = bc_vm_realloc(n->num, req);
1119 n->cap = req;
1120 }
Gavin Howardb5c77212018-02-14 17:12:34 -07001121}
1122
Gavin Howarded392aa2018-02-27 13:09:26 -07001123void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001124 assert(num);
1125 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001126}
1127
Gavin Howard3c02ed32018-12-28 18:17:15 -07001128void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001129 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001130 if (d == s) return;
1131 bc_num_expand(d, s->cap);
1132 d->len = s->len;
1133 d->neg = s->neg;
1134 d->rdx = s->rdx;
1135 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001136}
1137
Gavin Howard3c02ed32018-12-28 18:17:15 -07001138BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
Gavin Howardd746e802018-12-28 22:06:00 -07001139 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001140{
Gavin Howard7536dcf2018-12-15 19:27:09 -07001141 BcStatus s = BC_STATUS_SUCCESS;
1142
Gavin Howard63738202018-09-26 15:34:20 -06001143 assert(n && val && base);
Gavin Howard96edeb32018-12-31 09:33:22 -07001144 assert(base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase);
Gavin Howardc90ed902019-01-02 13:07:49 -07001145 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001146
Gavin Howardd746e802018-12-28 22:06:00 -07001147 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howardc7d655b2018-12-28 19:45:13 -07001148 else if (base_t == 10) bc_num_parseDecimal(n, val);
1149 else s = bc_num_parseBase(n, val, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001150
Gavin Howard7536dcf2018-12-15 19:27:09 -07001151 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001152}
1153
Gavin Howardab5e9632019-01-02 13:32:02 -07001154BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1155 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001156{
Gavin Howard48af52e2018-10-30 14:47:38 -06001157 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001158
Gavin Howardab5e9632019-01-02 13:32:02 -07001159 assert(n && base);
Gavin Howard63738202018-09-26 15:34:20 -06001160 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001161
Gavin Howardab5e9632019-01-02 13:32:02 -07001162 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001163
Gavin Howardddce6d42019-01-03 14:07:06 -07001164 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howardab5e9632019-01-02 13:32:02 -07001165 else if (base_t == 10) bc_num_printDecimal(n);
1166 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001167
Gavin Howardddce6d42019-01-03 14:07:06 -07001168 if (!s && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001169 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001170 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001171 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001172
Gavin Howard63738202018-09-26 15:34:20 -06001173 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001174}
1175
Gavin Howard3c02ed32018-12-28 18:17:15 -07001176BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001177
Gavin Howard63738202018-09-26 15:34:20 -06001178 size_t i;
Gavin Howard7b557da2018-12-21 10:25:32 -07001179 unsigned long pow, r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001180
Gavin Howard63738202018-09-26 15:34:20 -06001181 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001182
Gavin Howard7536dcf2018-12-15 19:27:09 -07001183 if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001184
Gavin Howard7b557da2018-12-21 10:25:32 -07001185 for (r = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001186
Gavin Howard7b557da2018-12-21 10:25:32 -07001187 unsigned long prev = r, powprev = pow;
Gavin Howard88c25302018-10-17 13:32:23 -06001188
Gavin Howard7b557da2018-12-21 10:25:32 -07001189 r += ((unsigned long) n->num[i]) * pow;
Gavin Howard63738202018-09-26 15:34:20 -06001190 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001191
Gavin Howard92037352019-01-02 14:58:35 -07001192 if (r < prev || pow < powprev)
1193 return bc_vm_verr(BC_ERROR_MATH_OVERFLOW, "number cannot fit");
Gavin Howard63738202018-09-26 15:34:20 -06001194 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001195
Gavin Howard7b557da2018-12-21 10:25:32 -07001196 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001197
Gavin Howard63738202018-09-26 15:34:20 -06001198 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001199}
1200
Gavin Howard3c02ed32018-12-28 18:17:15 -07001201void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001202
Gavin Howardec82bdc2018-10-08 23:50:57 -06001203 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001204 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001205 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001206
Gavin Howard63738202018-09-26 15:34:20 -06001207 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001208
Gavin Howard63738202018-09-26 15:34:20 -06001209 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001210
Gavin Howard1ab22d22019-01-03 13:32:17 -07001211 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001212
Gavin Howard1ab22d22019-01-03 13:32:17 -07001213 len = bc_num_log10(ULONG_MAX);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001214 bc_num_expand(n, len);
Gavin Howardec82bdc2018-10-08 23:50:57 -06001215 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001216}
1217
Gavin Howard12fe7812018-09-29 03:45:18 -06001218BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001219 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001220 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001221 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001222}
1223
Gavin Howard12fe7812018-09-29 03:45:18 -06001224BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001225 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001226 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001227 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001228}
1229
Gavin Howard12fe7812018-09-29 03:45:18 -06001230BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001231 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001232 return bc_num_binary(a, b, c, scale, bc_num_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001233}
1234
Gavin Howard12fe7812018-09-29 03:45:18 -06001235BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001236 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001237 return bc_num_binary(a, b, c, scale, bc_num_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001238}
1239
Gavin Howard54b946a2018-10-23 12:06:57 -06001240BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001241 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001242 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001243}
1244
Gavin Howard12fe7812018-09-29 03:45:18 -06001245BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001246 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001247}
1248
Gavin Howard7d8f0382018-12-27 16:15:13 -07001249#if BC_ENABLE_EXTRA_MATH
1250BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001251 return bc_num_binary(a, b, c, scale, bc_num_place, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001252}
1253
1254BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001255 return bc_num_binary(a, b, c, scale, bc_num_left, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001256}
1257
1258BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001259 return bc_num_binary(a, b, c, scale, bc_num_right, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001260}
1261#endif // BC_ENABLE_EXTRA_MATH
1262
Gavin Howard3c02ed32018-12-28 18:17:15 -07001263BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001264
Gavin Howard63738202018-09-26 15:34:20 -06001265 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001266 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001267 size_t pow, len, digs, digs1, resrdx, req, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001268 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001269 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001270
Gavin Howard34edd0a2018-10-25 15:51:05 -06001271 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001272
Gavin Howardd8d60c42018-10-29 12:13:09 -06001273 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001274 bc_num_expand(b, req);
Gavin Howard954276b2018-05-16 01:43:58 -06001275
Gavin Howardddce6d42019-01-03 14:07:06 -07001276 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001277 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001278 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001279 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001280 if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1281 if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001282 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001283 bc_num_extend(b, scale);
1284 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001285 }
Gavin Howard954276b2018-05-16 01:43:58 -06001286
Gavin Howard890d0c02018-10-30 16:34:50 -06001287 scale = BC_MAX(scale, a->rdx) + 1;
1288 len = a->len + scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001289
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001290 bc_num_init(&num1, len);
1291 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001292 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001293
Gavin Howard63738202018-09-26 15:34:20 -06001294 bc_num_one(&half);
1295 half.num[0] = 5;
1296 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001297
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001298 bc_num_init(&f, len);
1299 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001300
Gavin Howard63738202018-09-26 15:34:20 -06001301 x0 = &num1;
1302 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001303
Gavin Howard63738202018-09-26 15:34:20 -06001304 bc_num_one(x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001305 pow = BC_NUM_INT(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001306
Gavin Howard53eba8b2018-10-31 15:14:37 -06001307 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001308
Gavin Howardc39fd492018-10-04 10:07:03 -06001309 if (pow & 1) x0->num[0] = 2;
1310 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001311
Gavin Howardc39fd492018-10-04 10:07:03 -06001312 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001313
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001314 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001315
Gavin Howard63738202018-09-26 15:34:20 -06001316 // Make sure to move the radix back.
1317 x0->rdx -= pow;
1318 }
Gavin Howard954276b2018-05-16 01:43:58 -06001319
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001320 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001321 resrdx = scale + 2;
1322 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001323
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001324 while (!BC_SIGNAL && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001325
Gavin Howard53eba8b2018-10-31 15:14:37 -06001326 s = bc_num_div(a, x0, &f, resrdx);
1327 if (s) goto err;
1328 s = bc_num_add(x0, &f, &fprime, resrdx);
1329 if (s) goto err;
1330 s = bc_num_mul(&fprime, &half, x1, resrdx);
1331 if (s) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001332
Gavin Howard53eba8b2018-10-31 15:14:37 -06001333 cmp = bc_num_cmp(x1, x0);
1334 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001335
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001336 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001337 else times = 0;
1338
1339 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001340
1341 cmp2 = cmp1;
1342 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001343 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001344
Gavin Howard63738202018-09-26 15:34:20 -06001345 temp = x0;
1346 x0 = x1;
1347 x1 = temp;
1348 }
Gavin Howard954276b2018-05-16 01:43:58 -06001349
Gavin Howard5dbb48b2019-01-04 11:29:29 -07001350 if (BC_SIGNAL) {
Gavin Howard43df94a2018-12-22 10:29:41 -07001351 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -06001352 goto err;
1353 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001354
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001355 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001356 scale -= 1;
1357 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001358
1359err:
Gavin Howard63738202018-09-26 15:34:20 -06001360 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001361 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001362 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001363 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001364 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howard63738202018-09-26 15:34:20 -06001365 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001366}
Gavin Howardba009802018-09-29 04:41:51 -06001367
Gavin Howard2cb39612018-10-22 08:46:48 -06001368BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1369
1370 BcStatus s;
1371 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001372 bool init = false;
Gavin Howard2cb39612018-10-22 08:46:48 -06001373 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
1374
1375 assert(c != d && a != b && a != d && b != d && b != c);
1376
Gavin Howard890d0c02018-10-30 16:34:50 -06001377 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001378 memcpy(&num2, c, sizeof(BcNum));
1379 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001380 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001381 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001382 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001383 else {
1384 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001385 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001386 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001387
Gavin Howard54b946a2018-10-23 12:06:57 -06001388 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001389
Gavin Howardddce6d42019-01-03 14:07:06 -07001390 assert(!c->neg || BC_NUM_NONZERO(c));
1391 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howard2cb39612018-10-22 08:46:48 -06001392
1393 if (init) bc_num_free(&num2);
1394
1395 return s;
1396}
1397
Gavin Howard40a085f2018-12-03 12:08:59 -07001398#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001399BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001400
1401 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001402 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001403 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001404
Gavin Howard34edd0a2018-10-25 15:51:05 -06001405 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001406
Gavin Howardddce6d42019-01-03 14:07:06 -07001407 if (BC_NUM_ZERO(c)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard7536dcf2018-12-15 19:27:09 -07001408 if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1409 if (a->rdx || b->rdx || c->rdx)
1410 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001411
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001412 bc_num_expand(d, c->len);
1413 bc_num_init(&base, c->len);
1414 bc_num_init(&exp, b->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001415 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001416 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001417
1418 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001419 two.num[0] = 2;
1420 bc_num_one(d);
1421
Gavin Howard53eba8b2018-10-31 15:14:37 -06001422 s = bc_num_rem(a, c, &base, 0);
1423 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001424 bc_num_copy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001425
Gavin Howardddce6d42019-01-03 14:07:06 -07001426 while (BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001427
Gavin Howard53eba8b2018-10-31 15:14:37 -06001428 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
1429 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001430
1431 if (BC_NUM_ONE(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001432 s = bc_num_mul(d, &base, &temp, 0);
1433 if (s) goto err;
1434 s = bc_num_rem(&temp, c, d, 0);
1435 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001436 }
1437
Gavin Howard53eba8b2018-10-31 15:14:37 -06001438 s = bc_num_mul(&base, &base, &temp, 0);
1439 if (s) goto err;
1440 s = bc_num_rem(&temp, c, &base, 0);
1441 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001442 }
1443
1444err:
1445 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001446 bc_num_free(&exp);
Gavin Howardba009802018-09-29 04:41:51 -06001447 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001448 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001449 return s;
1450}
Gavin Howarde6e84762018-10-03 11:46:34 -06001451#endif // DC_ENABLED