blob: 465737e7121fe1549face9c0fe993b1fb45e5de0 [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 Howard411f7322018-09-26 17:21:19 -060024#include <stdbool.h>
25#include <stdlib.h>
26#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070027
Gavin Howard305249a2018-10-15 20:24:47 -060028#include <limits.h>
29
Gavin Howard29493062018-03-20 19:57:37 -060030#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070031#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060032#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070033
Gavin Howard773c86b2018-11-02 14:07:19 -060034void bc_num_setToZero(BcNum *n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060035 assert(n);
Gavin Howard97102b12018-11-01 23:37:31 -060036 n->len = 0;
37 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060038 n->rdx = scale;
39}
40
41void bc_num_zero(BcNum *n) {
42 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060043}
44
45void bc_num_one(BcNum *n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060046 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060047 n->len = 1;
48 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060049}
50
51void bc_num_ten(BcNum *n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060052 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060053 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060054 n->len = 2;
55 n->num[0] = 0;
56 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060057}
58
Gavin Howard84edc792018-11-17 14:22:10 -070059BcStatus bc_num_subArrays(BcDig *restrict a, BcDig *restrict b, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060060 size_t i, j;
Gavin Howard112304b2018-12-04 14:30:01 -070061 for (i = 0; !BC_SIGINT && i < len; ++i) {
62 for (a[i] -= b[i], j = 0; !BC_SIGINT && a[i + j] < 0;) {
Gavin Howard54b946a2018-10-23 12:06:57 -060063 a[i + j++] += 10;
64 a[i + j] -= 1;
Gavin Howard63738202018-09-26 15:34:20 -060065 }
66 }
Gavin Howard112304b2018-12-04 14:30:01 -070067 return BC_SIGINT ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060068}
69
Gavin Howard773c86b2018-11-02 14:07:19 -060070ssize_t bc_num_compare(BcDig *restrict a, BcDig *restrict b, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060071 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -060072 int c = 0;
Gavin Howard112304b2018-12-04 14:30:01 -070073 for (i = len - 1; !BC_SIGINT && i < len && !(c = a[i] - b[i]); --i);
Gavin Howard7fbb4e22018-10-18 11:43:17 -060074 return BC_NUM_NEG(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -060075}
76
Gavin Howard6a804cf2018-05-17 16:54:12 -060077ssize_t bc_num_cmp(BcNum *a, BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070078
Gavin Howard63738202018-09-26 15:34:20 -060079 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -060080 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060081 bool a_max, neg = false;
82 ssize_t cmp;
83
84 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070085
Gavin Howard8a921bd2018-10-11 14:15:32 -060086 if (a == b) return 0;
Gavin Howard53eba8b2018-10-31 15:14:37 -060087 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
88 if (b->len == 0) return BC_NUM_NEG(1, a->neg);
Gavin Howard890d0c02018-10-30 16:34:50 -060089 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -060090 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -060091 else return -1;
92 }
93 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070094
Gavin Howard63738202018-09-26 15:34:20 -060095 a_int = BC_NUM_INT(a);
96 b_int = BC_NUM_INT(b);
Gavin Howard890d0c02018-10-30 16:34:50 -060097 a_int -= b_int;
98 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -070099
Gavin Howard53eba8b2018-10-31 15:14:37 -0600100 if (a_int != 0) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700101
Gavin Howard890d0c02018-10-30 16:34:50 -0600102 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600103 min = b->rdx;
104 diff = a->rdx - b->rdx;
105 max_num = a->num + diff;
106 min_num = b->num;
107 }
108 else {
109 min = a->rdx;
110 diff = b->rdx - a->rdx;
111 max_num = b->num + diff;
112 min_num = a->num;
113 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700114
Gavin Howard890d0c02018-10-30 16:34:50 -0600115 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600116 if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700117
Gavin Howard112304b2018-12-04 14:30:01 -0700118 for (max_num -= diff, i = diff - 1; !BC_SIGINT && i < diff; --i) {
Gavin Howard40974152018-10-18 14:23:01 -0600119 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
Gavin Howard63738202018-09-26 15:34:20 -0600120 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700121
Gavin Howard63738202018-09-26 15:34:20 -0600122 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700123}
124
Gavin Howardd9734e52018-03-29 15:54:41 -0600125void bc_num_truncate(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700126
Gavin Howard53eba8b2018-10-31 15:14:37 -0600127 assert(places <= n->rdx && (n->len == 0 || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700128
Gavin Howard53eba8b2018-10-31 15:14:37 -0600129 if (places == 0) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700130
Gavin Howard63738202018-09-26 15:34:20 -0600131 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700132
Gavin Howard53eba8b2018-10-31 15:14:37 -0600133 if (n->len != 0) {
Gavin Howard955da852018-10-23 11:08:20 -0600134 n->len -= places;
135 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
136 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700137}
138
Gavin Howard773c86b2018-11-02 14:07:19 -0600139void bc_num_extend(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700140
Gavin Howard88c25302018-10-17 13:32:23 -0600141 size_t len = n->len + places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700142
Gavin Howard53eba8b2018-10-31 15:14:37 -0600143 if (places != 0) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700144
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600145 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700146
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600147 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
148 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700149
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600150 n->len += places;
151 n->rdx += places;
152 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700153}
154
Gavin Howard773c86b2018-11-02 14:07:19 -0600155void bc_num_clean(BcNum *n) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600156 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
157 if (n->len == 0) n->neg = false;
Gavin Howard63738202018-09-26 15:34:20 -0600158 else if (n->len < n->rdx) n->len = n->rdx;
Gavin Howarde57525d2018-09-01 16:56:21 -0600159}
160
Gavin Howard773c86b2018-11-02 14:07:19 -0600161void bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600162
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600163 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600164 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600165
Gavin Howard305249a2018-10-15 20:24:47 -0600166 bc_num_clean(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600167 if (n->len != 0) n->neg = !neg1 != !neg2;
Gavin Howard305249a2018-10-15 20:24:47 -0600168}
169
Gavin Howard96664f22018-11-17 14:37:13 -0700170void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
171 BcNum *restrict b)
172{
Gavin Howard305249a2018-10-15 20:24:47 -0600173 if (idx < n->len) {
174
Gavin Howard305249a2018-10-15 20:24:47 -0600175 b->len = n->len - idx;
176 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600177 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600178
179 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
180 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard305249a2018-10-15 20:24:47 -0600181 }
182 else {
183 bc_num_zero(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600184 bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600185 }
186
187 bc_num_clean(a);
188 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600189}
190
Gavin Howard773c86b2018-11-02 14:07:19 -0600191BcStatus bc_num_shift(BcNum *n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600192
Gavin Howard53eba8b2018-10-31 15:14:37 -0600193 if (places == 0 || n->len == 0) return BC_STATUS_SUCCESS;
Gavin Howard7536dcf2018-12-15 19:27:09 -0700194 if (places + n->len > BC_MAX_NUM) return bc_vm_err(BC_ERROR_EXEC_NUM_LEN);
Gavin Howard305249a2018-10-15 20:24:47 -0600195
196 if (n->rdx >= places) n->rdx -= places;
197 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600198 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600199 n->rdx = 0;
200 }
201
202 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600203
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600204 return BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600205}
206
Gavin Howard773c86b2018-11-02 14:07:19 -0600207BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600208
Gavin Howard63738202018-09-26 15:34:20 -0600209 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600210 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600211
Gavin Howardd32d7df2018-10-15 08:42:25 -0600212 one.cap = 2;
213 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600214 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600215
Gavin Howardd32d7df2018-10-15 08:42:25 -0600216 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600217}
218
Gavin Howard773c86b2018-11-02 14:07:19 -0600219BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700220
Gavin Howard8a921bd2018-10-11 14:15:32 -0600221 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600222 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700223 unsigned int carry, in;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700224
Gavin Howard63738202018-09-26 15:34:20 -0600225 // Because this function doesn't need to use scale (per the bc spec),
226 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600227
Gavin Howard53eba8b2018-10-31 15:14:37 -0600228 if (a->len == 0) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600229 bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600230 if (sub && c->len) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600231 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600232 }
Gavin Howard53eba8b2018-10-31 15:14:37 -0600233 else if (b->len == 0) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600234 bc_num_copy(c, a);
235 return BC_STATUS_SUCCESS;
236 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600237
Gavin Howard63738202018-09-26 15:34:20 -0600238 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600239 c->rdx = BC_MAX(a->rdx, b->rdx);
240 min_rdx = BC_MIN(a->rdx, b->rdx);
241 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700242
Gavin Howard63738202018-09-26 15:34:20 -0600243 if (a->rdx > b->rdx) {
244 diff = a->rdx - b->rdx;
245 ptr = a->num;
246 ptr_a = a->num + diff;
247 ptr_b = b->num;
248 }
249 else {
250 diff = b->rdx - a->rdx;
251 ptr = b->num;
252 ptr_a = a->num;
253 ptr_b = b->num + diff;
254 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700255
Gavin Howard63738202018-09-26 15:34:20 -0600256 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700257
Gavin Howard63738202018-09-26 15:34:20 -0600258 ptr_c += diff;
Gavin Howard890d0c02018-10-30 16:34:50 -0600259 a_int = BC_NUM_INT(a);
260 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700261
Gavin Howard890d0c02018-10-30 16:34:50 -0600262 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600263 min_int = b_int;
264 max = a_int;
265 ptr = ptr_a;
266 }
267 else {
268 min_int = a_int;
269 max = b_int;
270 ptr = ptr_b;
271 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700272
Gavin Howard112304b2018-12-04 14:30:01 -0700273 for (carry = 0, i = 0; !BC_SIGINT && i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700274 in = ((unsigned int) ptr_a[i]) + ((unsigned int) ptr_b[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600275 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600276 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600277 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700278
Gavin Howard112304b2018-12-04 14:30:01 -0700279 for (; !BC_SIGINT && i < max + min_rdx; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700280 in = ((unsigned int) ptr[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600281 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600282 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600283 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700284
Gavin Howard53eba8b2018-10-31 15:14:37 -0600285 if (carry != 0) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600286
Gavin Howard112304b2018-12-04 14:30:01 -0700287 return BC_SIGINT ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700288}
289
Gavin Howard773c86b2018-11-02 14:07:19 -0600290BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700291
Gavin Howard63738202018-09-26 15:34:20 -0600292 BcStatus s;
293 ssize_t cmp;
294 BcNum *minuend, *subtrahend;
295 size_t start;
296 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700297
Gavin Howard63738202018-09-26 15:34:20 -0600298 // Because this function doesn't need to use scale (per the bc spec),
299 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700300
Gavin Howard53eba8b2018-10-31 15:14:37 -0600301 if (a->len == 0) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600302 bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600303 if (sub && c->len) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600304 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600305 }
Gavin Howard53eba8b2018-10-31 15:14:37 -0600306 else if (b->len == 0) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600307 bc_num_copy(c, a);
308 return BC_STATUS_SUCCESS;
309 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700310
Gavin Howard63738202018-09-26 15:34:20 -0600311 aneg = a->neg;
312 bneg = b->neg;
313 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700314
Gavin Howard63738202018-09-26 15:34:20 -0600315 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700316
Gavin Howard63738202018-09-26 15:34:20 -0600317 a->neg = aneg;
318 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700319
Gavin Howard53eba8b2018-10-31 15:14:37 -0600320 if (cmp == 0) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600321 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600322 return BC_STATUS_SUCCESS;
323 }
324 else if (cmp > 0) {
325 neg = a->neg;
326 minuend = a;
327 subtrahend = b;
328 }
329 else {
330 neg = b->neg;
331 if (sub) neg = !neg;
332 minuend = b;
333 subtrahend = a;
334 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700335
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600336 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600337 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700338
Gavin Howard63738202018-09-26 15:34:20 -0600339 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600340 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600341 start = 0;
342 }
343 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700344
Gavin Howard63738202018-09-26 15:34:20 -0600345 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700346
Gavin Howard63738202018-09-26 15:34:20 -0600347 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700348
Gavin Howard63738202018-09-26 15:34:20 -0600349 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700350}
351
Gavin Howard773c86b2018-11-02 14:07:19 -0600352BcStatus bc_num_k(BcNum *restrict a, BcNum *restrict b, BcNum *restrict c) {
353
Gavin Howard63738202018-09-26 15:34:20 -0600354 BcStatus s;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700355 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600356 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard63738202018-09-26 15:34:20 -0600357 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600358
Gavin Howard112304b2018-12-04 14:30:01 -0700359 if (BC_SIGINT) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600360 if (a->len == 0 || b->len == 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600361 bc_num_zero(c);
362 return BC_STATUS_SUCCESS;
363 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600364 else if (aone || BC_NUM_ONE(b)) {
365 bc_num_copy(c, aone ? b : a);
366 return BC_STATUS_SUCCESS;
367 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700368
Gavin Howardb5093d72018-10-17 12:13:17 -0600369 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
370 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600371 {
Gavin Howard34c4ba02018-12-11 10:25:01 -0700372 size_t i, j, len;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700373 unsigned int carry;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700374
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600375 bc_num_expand(c, a->len + b->len + 1);
Gavin Howard23d8d252018-10-17 12:03:41 -0600376
Gavin Howard305249a2018-10-15 20:24:47 -0600377 memset(c->num, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700378 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700379
Gavin Howard112304b2018-12-04 14:30:01 -0700380 for (i = 0; !BC_SIGINT && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600381
Gavin Howard34c4ba02018-12-11 10:25:01 -0700382 carry = 0;
383
Gavin Howard112304b2018-12-04 14:30:01 -0700384 for (j = 0; !BC_SIGINT && j < a->len; ++j) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700385 unsigned int in = c->num[i + j];
386 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
387 in += carry;
Gavin Howard305249a2018-10-15 20:24:47 -0600388 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600389 c->num[i + j] = (BcDig) (in % 10);
Gavin Howard305249a2018-10-15 20:24:47 -0600390 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700391
Gavin Howard88c25302018-10-17 13:32:23 -0600392 c->num[i + j] += (BcDig) carry;
393 len = BC_MAX(len, i + j + !!carry);
Gavin Howard63738202018-09-26 15:34:20 -0600394 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700395
Gavin Howard305249a2018-10-15 20:24:47 -0600396 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600397
Gavin Howard112304b2018-12-04 14:30:01 -0700398 return BC_SIGINT ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600399 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700400
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600401 bc_num_init(&l1, max);
402 bc_num_init(&h1, max);
403 bc_num_init(&l2, max);
404 bc_num_init(&h2, max);
405 bc_num_init(&m1, max);
406 bc_num_init(&m2, max);
407 bc_num_init(&z0, max);
408 bc_num_init(&z1, max);
409 bc_num_init(&z2, max);
410 bc_num_init(&temp, max + max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600411
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600412 bc_num_split(a, max2, &l1, &h1);
413 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700414
Gavin Howard53eba8b2018-10-31 15:14:37 -0600415 s = bc_num_add(&h1, &l1, &m1, 0);
416 if (s) goto err;
417 s = bc_num_add(&h2, &l2, &m2, 0);
418 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600419
Gavin Howard53eba8b2018-10-31 15:14:37 -0600420 s = bc_num_k(&h1, &h2, &z0);
421 if (s) goto err;
422 s = bc_num_k(&m1, &m2, &z1);
423 if (s) goto err;
424 s = bc_num_k(&l1, &l2, &z2);
425 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600426
Gavin Howard53eba8b2018-10-31 15:14:37 -0600427 s = bc_num_sub(&z1, &z0, &temp, 0);
428 if (s) goto err;
429 s = bc_num_sub(&temp, &z2, &z1, 0);
430 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600431
Gavin Howard53eba8b2018-10-31 15:14:37 -0600432 s = bc_num_shift(&z0, max2 * 2);
433 if (s) goto err;
434 s = bc_num_shift(&z1, max2);
435 if (s) goto err;
436 s = bc_num_add(&z0, &z1, &temp, 0);
437 if (s) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600438 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600439
440err:
441 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600442 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600443 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600444 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600445 bc_num_free(&m2);
Gavin Howard305249a2018-10-15 20:24:47 -0600446 bc_num_free(&m1);
Gavin Howard305249a2018-10-15 20:24:47 -0600447 bc_num_free(&h2);
Gavin Howard305249a2018-10-15 20:24:47 -0600448 bc_num_free(&l2);
Gavin Howard305249a2018-10-15 20:24:47 -0600449 bc_num_free(&h1);
Gavin Howard305249a2018-10-15 20:24:47 -0600450 bc_num_free(&l1);
451 return s;
452}
453
Gavin Howard773c86b2018-11-02 14:07:19 -0600454BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600455
456 BcStatus s;
457 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600458 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600459
460 scale = BC_MAX(scale, a->rdx);
461 scale = BC_MAX(scale, b->rdx);
462 scale = BC_MIN(a->rdx + b->rdx, scale);
463 maxrdx = BC_MAX(maxrdx, scale);
464
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600465 bc_num_init(&cpa, a->len);
466 bc_num_init(&cpb, b->len);
Gavin Howard305249a2018-10-15 20:24:47 -0600467
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600468 bc_num_copy(&cpa, a);
469 bc_num_copy(&cpb, b);
Gavin Howard23d8d252018-10-17 12:03:41 -0600470 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600471
Gavin Howard53eba8b2018-10-31 15:14:37 -0600472 s = bc_num_shift(&cpa, maxrdx);
473 if (s) goto err;
474 s = bc_num_shift(&cpb, maxrdx);
475 if (s) goto err;
476 s = bc_num_k(&cpa, &cpb, c);
477 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600478
Gavin Howard53eba8b2018-10-31 15:14:37 -0600479 maxrdx += scale;
480 bc_num_expand(c, c->len + maxrdx);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600481
Gavin Howard88c25302018-10-17 13:32:23 -0600482 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600483 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600484 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600485 }
486
Gavin Howard88c25302018-10-17 13:32:23 -0600487 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600488 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600489
490err:
491 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600492 bc_num_free(&cpa);
493 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700494}
495
Gavin Howard773c86b2018-11-02 14:07:19 -0600496BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700497
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600498 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600499 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600500 size_t len, end, i;
501 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600502 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700503
Gavin Howard7536dcf2018-12-15 19:27:09 -0700504 if (b->len == 0) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600505 else if (a->len == 0) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600506 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600507 return BC_STATUS_SUCCESS;
508 }
509 else if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600510 bc_num_copy(c, a);
511 bc_num_retireMul(c, scale, a->neg, b->neg);
512 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600513 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700514
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600515 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
516 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600517 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700518
Gavin Howard890d0c02018-10-30 16:34:50 -0600519 if (len > cp.len) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600520 bc_num_expand(&cp, len + 2);
521 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600522 }
Gavin Howard021150b2018-03-10 15:40:42 -0700523
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600524 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600525 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600526 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700527
Gavin Howard63738202018-09-26 15:34:20 -0600528 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600529 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600530 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600531 len -= i - 1;
532 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700533
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600534 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700535
Gavin Howard63738202018-09-26 15:34:20 -0600536 // We want an extra zero in front to make things simpler.
537 cp.num[cp.len++] = 0;
538 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700539
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600540 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700541
Gavin Howard63738202018-09-26 15:34:20 -0600542 bc_num_zero(c);
Gavin Howarda2514a02018-10-18 14:20:09 -0600543 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600544 c->rdx = cp.rdx;
545 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600546 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700547
Gavin Howard112304b2018-12-04 14:30:01 -0700548 for (i = end - 1; !BC_SIGINT && !s && i < end; --i) {
Gavin Howard63738202018-09-26 15:34:20 -0600549 n = cp.num + i;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600550 for (q = 0; (!s && n[len] != 0) || bc_num_compare(n, p, len) >= 0; ++q)
551 s = bc_num_subArrays(n, p, len);
Gavin Howard63738202018-09-26 15:34:20 -0600552 c->num[i] = q;
553 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700554
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600555 if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600556 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600557
Gavin Howard63738202018-09-26 15:34:20 -0600558 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700559}
560
Gavin Howard773c86b2018-11-02 14:07:19 -0600561BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
562 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600563{
Gavin Howard63738202018-09-26 15:34:20 -0600564 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600565 BcNum temp;
566 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700567
Gavin Howard7536dcf2018-12-15 19:27:09 -0700568 if (b->len == 0) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard8b254872018-03-14 01:13:35 -0600569
Gavin Howard53eba8b2018-10-31 15:14:37 -0600570 if (a->len == 0) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600571 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600572 return BC_STATUS_SUCCESS;
573 }
Gavin Howard8b254872018-03-14 01:13:35 -0600574
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600575 bc_num_init(&temp, d->cap);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600576 bc_num_d(a, b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700577
Gavin Howard53eba8b2018-10-31 15:14:37 -0600578 if (scale != 0) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600579
Gavin Howard53eba8b2018-10-31 15:14:37 -0600580 s = bc_num_m(c, b, &temp, scale);
581 if (s) goto err;
582 s = bc_num_sub(a, &temp, d, scale);
583 if (s) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600584
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600585 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600586
587 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600588 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600589 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700590
591err:
Gavin Howard18441082018-10-22 10:03:30 -0600592 bc_num_free(&temp);
593 return s;
594}
595
Gavin Howard773c86b2018-11-02 14:07:19 -0600596BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
597
Gavin Howard18441082018-10-22 10:03:30 -0600598 BcStatus s;
599 BcNum c1;
600 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
601
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600602 bc_num_init(&c1, len);
Gavin Howard54b946a2018-10-23 12:06:57 -0600603 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600604 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600605
Gavin Howard63738202018-09-26 15:34:20 -0600606 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700607}
608
Gavin Howard773c86b2018-11-02 14:07:19 -0600609BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700610
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600611 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600612 BcNum copy;
613 unsigned long pow;
614 size_t i, powrdx, resrdx;
615 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700616
Gavin Howard7536dcf2018-12-15 19:27:09 -0700617 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700618
Gavin Howard53eba8b2018-10-31 15:14:37 -0600619 if (b->len == 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600620 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600621 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600622 }
Gavin Howard53eba8b2018-10-31 15:14:37 -0600623 else if (a->len == 0) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600624 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600625 return BC_STATUS_SUCCESS;
626 }
627 else if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600628 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600629 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600630 return s;
631 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700632
Gavin Howard63738202018-09-26 15:34:20 -0600633 neg = b->neg;
634 b->neg = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700635
Gavin Howard53eba8b2018-10-31 15:14:37 -0600636 s = bc_num_ulong(b, &pow);
637 if (s) return s;
638
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600639 bc_num_init(&copy, a->len);
640 bc_num_copy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700641
Gavin Howard63738202018-09-26 15:34:20 -0600642 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600643
Gavin Howard63738202018-09-26 15:34:20 -0600644 b->neg = neg;
Gavin Howardb29674f2018-03-22 22:24:58 -0600645
Gavin Howard112304b2018-12-04 14:30:01 -0700646 for (powrdx = a->rdx; !BC_SIGINT && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600647 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600648 s = bc_num_mul(&copy, &copy, &copy, powrdx);
649 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600650 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700651
Gavin Howard112304b2018-12-04 14:30:01 -0700652 if (BC_SIGINT) {
Gavin Howard63738202018-09-26 15:34:20 -0600653 s = BC_STATUS_EXEC_SIGNAL;
654 goto err;
655 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700656
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600657 bc_num_copy(c, &copy);
658
Gavin Howard112304b2018-12-04 14:30:01 -0700659 for (resrdx = powrdx, pow >>= 1; !BC_SIGINT && pow != 0; pow >>= 1) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600660
661 powrdx <<= 1;
662 s = bc_num_mul(&copy, &copy, &copy, powrdx);
663 if (s) goto err;
664
Gavin Howard63738202018-09-26 15:34:20 -0600665 if (pow & 1) {
666 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600667 s = bc_num_mul(c, &copy, c, resrdx);
668 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600669 }
670 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700671
Gavin Howard53eba8b2018-10-31 15:14:37 -0600672 if (neg) {
673 s = bc_num_inv(c, c, scale);
674 if (s) goto err;
675 }
676
Gavin Howard112304b2018-12-04 14:30:01 -0700677 if (BC_SIGINT) {
Gavin Howard63738202018-09-26 15:34:20 -0600678 s = BC_STATUS_EXEC_SIGNAL;
679 goto err;
680 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700681
Gavin Howard63738202018-09-26 15:34:20 -0600682 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700683
Gavin Howard53eba8b2018-10-31 15:14:37 -0600684 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600685 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600686 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700687
Gavin Howard6fbdb292018-02-27 15:44:48 -0700688err:
Gavin Howard63738202018-09-26 15:34:20 -0600689 bc_num_free(&copy);
690 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700691}
692
Gavin Howard773c86b2018-11-02 14:07:19 -0600693BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
694 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700695{
Gavin Howarda1c44392018-09-27 12:41:15 -0600696 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600697 BcNum num2, *ptr_a, *ptr_b;
698 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700699
Gavin Howard63738202018-09-26 15:34:20 -0600700 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700701
Gavin Howard890d0c02018-10-30 16:34:50 -0600702 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600703 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600704 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600705 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600706 }
707 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700708
Gavin Howard63738202018-09-26 15:34:20 -0600709 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600710 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600711 if (c != a) {
712 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600713 init = true;
714 }
715 }
716 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700717
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600718 if (init) bc_num_init(c, req);
719 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700720
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600721 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700722
Gavin Howard3cf769e2018-10-11 13:55:11 -0600723 assert(!c->neg || c->len);
724
Gavin Howardba009802018-09-29 04:41:51 -0600725 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600726
Gavin Howarda1c44392018-09-27 12:41:15 -0600727 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700728}
729
Gavin Howard773c86b2018-11-02 14:07:19 -0600730bool bc_num_strValid(const char *val, size_t base) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700731
Gavin Howard8a921bd2018-10-11 14:15:32 -0600732 BcDig b;
Gavin Howard63738202018-09-26 15:34:20 -0600733 bool small, radix = false;
734 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700735
Gavin Howard63738202018-09-26 15:34:20 -0600736 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700737
Gavin Howard63738202018-09-26 15:34:20 -0600738 small = base <= 10;
Gavin Howard34edd0a2018-10-25 15:51:05 -0600739 b = (BcDig) (small ? base + '0' : base - 10 + 'A');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700740
Gavin Howard63738202018-09-26 15:34:20 -0600741 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700742
Gavin Howard8a921bd2018-10-11 14:15:32 -0600743 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600744
Gavin Howard63738202018-09-26 15:34:20 -0600745 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700746
Gavin Howard63738202018-09-26 15:34:20 -0600747 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700748
Gavin Howard63738202018-09-26 15:34:20 -0600749 radix = true;
750 continue;
751 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700752
Gavin Howard63738202018-09-26 15:34:20 -0600753 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
754 return false;
755 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700756
Gavin Howard63738202018-09-26 15:34:20 -0600757 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700758}
759
Gavin Howard773c86b2018-11-02 14:07:19 -0600760void bc_num_parseDecimal(BcNum *n, const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700761
Gavin Howard63738202018-09-26 15:34:20 -0600762 size_t len, i;
763 const char *ptr;
764 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700765
Gavin Howard63738202018-09-26 15:34:20 -0600766 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700767
Gavin Howard63738202018-09-26 15:34:20 -0600768 val += i;
769 len = strlen(val);
770 bc_num_zero(n);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700771
Gavin Howard53eba8b2018-10-31 15:14:37 -0600772 if (len != 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600773 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600774 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600775 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600776
Gavin Howard63738202018-09-26 15:34:20 -0600777 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700778
Gavin Howard63738202018-09-26 15:34:20 -0600779 // Explicitly test for NULL here to produce either a 0 or 1.
780 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700781
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600782 if (!zero) {
783 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
784 n->num[n->len] = val[i] - '0';
785 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700786}
787
Gavin Howard7536dcf2018-12-15 19:27:09 -0700788BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
Gavin Howardede51f02018-03-02 12:30:00 -0700789
Gavin Howard63738202018-09-26 15:34:20 -0600790 BcStatus s;
791 BcNum temp, mult, result;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600792 BcDig c = '\0';
Gavin Howard11b9afd2018-10-18 14:23:28 -0600793 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600794 unsigned long v;
795 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700796
Gavin Howard63738202018-09-26 15:34:20 -0600797 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700798
Gavin Howard11b9afd2018-10-18 14:23:28 -0600799 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howard7536dcf2018-12-15 19:27:09 -0700800 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700801
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600802 bc_num_init(&temp, BC_NUM_DEF_SIZE);
803 bc_num_init(&mult, BC_NUM_DEF_SIZE);
Gavin Howardede51f02018-03-02 12:30:00 -0700804
Gavin Howard53eba8b2018-10-31 15:14:37 -0600805 for (i = 0; i < len; ++i) {
806
807 c = val[i];
808 if (c == '.') break;
Gavin Howardede51f02018-03-02 12:30:00 -0700809
Gavin Howard63738202018-09-26 15:34:20 -0600810 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700811
Gavin Howard53eba8b2018-10-31 15:14:37 -0600812 s = bc_num_mul(n, base, &mult, 0);
813 if (s) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700814 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600815 s = bc_num_add(&mult, &temp, n, 0);
816 if (s) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600817 }
Gavin Howardede51f02018-03-02 12:30:00 -0700818
Gavin Howard53eba8b2018-10-31 15:14:37 -0600819 if (i == len) {
820 c = val[i];
821 if (c == 0) goto int_err;
822 }
823
Gavin Howard63738202018-09-26 15:34:20 -0600824 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600825 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -0600826 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700827
Gavin Howard53eba8b2018-10-31 15:14:37 -0600828 for (i += 1, digits = 0; i < len; ++i, ++digits) {
829
830 c = val[i];
831 if (c == 0) break;
Gavin Howardede51f02018-03-02 12:30:00 -0700832
Gavin Howard63738202018-09-26 15:34:20 -0600833 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardede51f02018-03-02 12:30:00 -0700834
Gavin Howard53eba8b2018-10-31 15:14:37 -0600835 s = bc_num_mul(&result, base, &result, 0);
836 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700837 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600838 s = bc_num_add(&result, &temp, &result, 0);
839 if (s) goto err;
840 s = bc_num_mul(&mult, base, &mult, 0);
841 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600842 }
Gavin Howardede51f02018-03-02 12:30:00 -0700843
Gavin Howard53eba8b2018-10-31 15:14:37 -0600844 s = bc_num_div(&result, &mult, &result, digits);
845 if (s) goto err;
846 s = bc_num_add(n, &result, n, digits);
847 if (s) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600848
Gavin Howard53eba8b2018-10-31 15:14:37 -0600849 if (n->len != 0) {
850 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600851 }
852 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700853
854err:
Gavin Howard63738202018-09-26 15:34:20 -0600855 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700856int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600857 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -0600858 bc_num_free(&temp);
Gavin Howard7536dcf2018-12-15 19:27:09 -0700859 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700860}
861
Gavin Howard70e3d602018-12-11 11:00:49 -0700862void bc_num_printNewline(size_t *nchars) {
863 if (*nchars == vm->line_len - 1) {
Gavin Howard48af52e2018-10-30 14:47:38 -0600864 bc_vm_putchar('\\');
865 bc_vm_putchar('\n');
Gavin Howard63738202018-09-26 15:34:20 -0600866 *nchars = 0;
867 }
Gavin Howard80977b22018-09-06 14:29:13 -0600868}
869
Gavin Howard40a085f2018-12-03 12:08:59 -0700870#if DC_ENABLED
Gavin Howard70e3d602018-12-11 11:00:49 -0700871void bc_num_printChar(size_t num, size_t width, bool radix, size_t *nchars)
Gavin Howard83eb8392018-10-09 01:21:19 -0600872{
Gavin Howard70e3d602018-12-11 11:00:49 -0700873 (void) radix;
Gavin Howard954f9b62018-12-19 15:22:20 -0700874 bc_vm_putchar((uchar) num);
Gavin Howard83eb8392018-10-09 01:21:19 -0600875 *nchars = *nchars + width;
Gavin Howard83eb8392018-10-09 01:21:19 -0600876}
877#endif // DC_ENABLED
878
Gavin Howard70e3d602018-12-11 11:00:49 -0700879void bc_num_printDigits(size_t num, size_t width, bool radix, size_t *nchars) {
880
Gavin Howarda84ad992018-12-03 19:11:06 -0700881 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -0600882
Gavin Howard70e3d602018-12-11 11:00:49 -0700883 bc_num_printNewline(nchars);
Gavin Howard48af52e2018-10-30 14:47:38 -0600884 bc_vm_putchar(radix ? '.' : ' ');
Gavin Howard63738202018-09-26 15:34:20 -0600885 ++(*nchars);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600886
Gavin Howard70e3d602018-12-11 11:00:49 -0700887 bc_num_printNewline(nchars);
Gavin Howard63738202018-09-26 15:34:20 -0600888 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600889
Gavin Howard63738202018-09-26 15:34:20 -0600890 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -0700891 size_t dig;
Gavin Howard70e3d602018-12-11 11:00:49 -0700892 bc_num_printNewline(nchars);
Gavin Howarda84ad992018-12-03 19:11:06 -0700893 dig = num / pow;
894 num -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -0700895 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -0600896 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700897}
Gavin Howardfe679f02018-02-14 15:50:09 -0700898
Gavin Howard70e3d602018-12-11 11:00:49 -0700899void bc_num_printHex(size_t num, size_t width, bool radix, size_t *nchars) {
900
Gavin Howard63738202018-09-26 15:34:20 -0600901 assert(width == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600902
Gavin Howard63738202018-09-26 15:34:20 -0600903 if (radix) {
Gavin Howard70e3d602018-12-11 11:00:49 -0700904 bc_num_printNewline(nchars);
Gavin Howard48af52e2018-10-30 14:47:38 -0600905 bc_vm_putchar('.');
Gavin Howard63738202018-09-26 15:34:20 -0600906 *nchars += 1;
907 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700908
Gavin Howard70e3d602018-12-11 11:00:49 -0700909 bc_num_printNewline(nchars);
Gavin Howard48af52e2018-10-30 14:47:38 -0600910 bc_vm_putchar(bc_num_hex_digits[num]);
Gavin Howard63738202018-09-26 15:34:20 -0600911 *nchars = *nchars + width;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700912}
913
Gavin Howard70e3d602018-12-11 11:00:49 -0700914void bc_num_printDecimal(BcNum *n, size_t *nchars) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700915
Gavin Howard63738202018-09-26 15:34:20 -0600916 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700917
Gavin Howard48af52e2018-10-30 14:47:38 -0600918 if (n->neg) bc_vm_putchar('-');
Gavin Howard63738202018-09-26 15:34:20 -0600919 (*nchars) += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700920
Gavin Howard48af52e2018-10-30 14:47:38 -0600921 for (i = n->len - 1; i < n->len; --i)
Gavin Howard70e3d602018-12-11 11:00:49 -0700922 bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700923}
924
Gavin Howard70e3d602018-12-11 11:00:49 -0700925BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width,
926 size_t *nchars, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -0600927{
Gavin Howard63738202018-09-26 15:34:20 -0600928 BcStatus s;
929 BcVec stack;
930 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -0600931 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -0600932 size_t i;
933 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700934
Gavin Howard53eba8b2018-10-31 15:14:37 -0600935 if (n->len == 0) {
Gavin Howard70e3d602018-12-11 11:00:49 -0700936 print(0, width, false, nchars);
Gavin Howard48af52e2018-10-30 14:47:38 -0600937 return BC_STATUS_SUCCESS;
938 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -0600939
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600940 bc_vec_init(&stack, sizeof(long), NULL);
941 bc_num_init(&intp, n->len);
942 bc_num_init(&fracp, n->rdx);
943 bc_num_init(&digit, width);
944 bc_num_init(&frac_len, BC_NUM_INT(n));
945 bc_num_copy(&intp, n);
Gavin Howard63738202018-09-26 15:34:20 -0600946 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -0600947
Gavin Howard63738202018-09-26 15:34:20 -0600948 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600949 s = bc_num_sub(n, &intp, &fracp, 0);
950 if (s) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700951
Gavin Howard53eba8b2018-10-31 15:14:37 -0600952 while (intp.len != 0) {
953 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
954 if (s) goto err;
955 s = bc_num_ulong(&digit, &dig);
956 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600957 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -0600958 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700959
Gavin Howard63738202018-09-26 15:34:20 -0600960 for (i = 0; i < stack.len; ++i) {
961 ptr = bc_vec_item_rev(&stack, i);
962 assert(ptr);
Gavin Howard70e3d602018-12-11 11:00:49 -0700963 print(*ptr, width, false, nchars);
Gavin Howard63738202018-09-26 15:34:20 -0600964 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700965
Gavin Howard63738202018-09-26 15:34:20 -0600966 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700967
Gavin Howard63738202018-09-26 15:34:20 -0600968 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600969 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
970 if (s) goto err;
971 s = bc_num_ulong(&fracp, &dig);
972 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700973 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600974 s = bc_num_sub(&fracp, &intp, &fracp, 0);
975 if (s) goto err;
Gavin Howard70e3d602018-12-11 11:00:49 -0700976 print(dig, width, radix, nchars);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600977 s = bc_num_mul(&frac_len, base, &frac_len, 0);
978 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600979 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700980
981err:
Gavin Howard63738202018-09-26 15:34:20 -0600982 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -0600983 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -0600984 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -0600985 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -0600986 bc_vec_free(&stack);
Gavin Howard63738202018-09-26 15:34:20 -0600987 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700988}
989
Gavin Howard70e3d602018-12-11 11:00:49 -0700990BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t, size_t *nchars)
Gavin Howard83eb8392018-10-09 01:21:19 -0600991{
992 BcStatus s;
993 size_t width, i;
994 BcNumDigitOp print;
995 bool neg = n->neg;
996
Gavin Howard48af52e2018-10-30 14:47:38 -0600997 if (neg) bc_vm_putchar('-');
Gavin Howard83eb8392018-10-09 01:21:19 -0600998 (*nchars) += neg;
999
1000 n->neg = false;
1001
1002 if (base_t <= BC_NUM_MAX_IBASE) {
1003 width = 1;
1004 print = bc_num_printHex;
1005 }
1006 else {
1007 for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width);
1008 print = bc_num_printDigits;
1009 }
1010
Gavin Howard70e3d602018-12-11 11:00:49 -07001011 s = bc_num_printNum(n, base, width, nchars, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001012 n->neg = neg;
1013
1014 return s;
1015}
1016
Gavin Howard40a085f2018-12-03 12:08:59 -07001017#if DC_ENABLED
Gavin Howard70e3d602018-12-11 11:00:49 -07001018BcStatus bc_num_stream(BcNum *n, BcNum *base, size_t *nchars) {
1019 return bc_num_printNum(n, base, 1, nchars, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001020}
1021#endif // DC_ENABLED
1022
Gavin Howard53eba8b2018-10-31 15:14:37 -06001023void bc_num_init(BcNum *n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001024 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001025 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001026 n->num = bc_vm_malloc(req);
1027 n->cap = req;
Gavin Howard38ddfc22018-12-11 13:25:18 -07001028 n->rdx = n->len = 0;
1029 n->neg = false;
Gavin Howardb5c77212018-02-14 17:12:34 -07001030}
1031
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001032void bc_num_expand(BcNum *n, size_t req) {
Gavin Howard844e7a52018-10-09 11:46:39 -06001033 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001034 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1035 if (req > n->cap) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001036 n->num = bc_vm_realloc(n->num, req);
1037 n->cap = req;
1038 }
Gavin Howardb5c77212018-02-14 17:12:34 -07001039}
1040
Gavin Howarded392aa2018-02-27 13:09:26 -07001041void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001042 assert(num);
1043 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001044}
1045
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001046void bc_num_copy(BcNum *d, BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -07001047
Gavin Howard63738202018-09-26 15:34:20 -06001048 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -07001049
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001050 if (d != s) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001051 bc_num_expand(d, s->cap);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001052 d->len = s->len;
1053 d->neg = s->neg;
1054 d->rdx = s->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001055 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1056 }
Gavin Howard5a049c42018-02-15 11:24:11 -07001057}
1058
Gavin Howard5cea43b2018-03-02 11:32:11 -07001059BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
Gavin Howard025d04d2018-02-20 13:53:28 -07001060
Gavin Howard7536dcf2018-12-15 19:27:09 -07001061 BcStatus s = BC_STATUS_SUCCESS;
1062
Gavin Howard63738202018-09-26 15:34:20 -06001063 assert(n && val && base);
1064 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_NUM_MAX_IBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001065
Gavin Howard7536dcf2018-12-15 19:27:09 -07001066 if (!bc_num_strValid(val, base_t)) return bc_vm_err(BC_ERROR_MATH_STRING);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001067
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001068 if (base_t == 10) bc_num_parseDecimal(n, val);
Gavin Howard7536dcf2018-12-15 19:27:09 -07001069 else s = bc_num_parseBase(n, val, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001070
Gavin Howard7536dcf2018-12-15 19:27:09 -07001071 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001072}
1073
Gavin Howard70e3d602018-12-11 11:00:49 -07001074BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t,
1075 bool newline, size_t *nchars)
Gavin Howard152f3e82018-03-07 12:33:15 -07001076{
Gavin Howard48af52e2018-10-30 14:47:38 -06001077 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001078
Gavin Howard63738202018-09-26 15:34:20 -06001079 assert(n && base && nchars);
1080 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001081
Gavin Howard70e3d602018-12-11 11:00:49 -07001082 bc_num_printNewline(nchars);
Gavin Howardbc7cae82018-03-14 13:43:04 -06001083
Gavin Howard53eba8b2018-10-31 15:14:37 -06001084 if (n->len == 0) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001085 bc_vm_putchar('0');
Gavin Howarde9f73312018-10-24 17:11:03 -06001086 ++(*nchars);
1087 }
Gavin Howard70e3d602018-12-11 11:00:49 -07001088 else if (base_t == 10) bc_num_printDecimal(n, nchars);
1089 else s = bc_num_printBase(n, base, base_t, nchars);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001090
Gavin Howard63738202018-09-26 15:34:20 -06001091 if (newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001092 bc_vm_putchar('\n');
Gavin Howard63738202018-09-26 15:34:20 -06001093 *nchars = 0;
1094 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001095
Gavin Howard63738202018-09-26 15:34:20 -06001096 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001097}
1098
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001099BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001100
Gavin Howard63738202018-09-26 15:34:20 -06001101 size_t i;
Gavin Howard7b557da2018-12-21 10:25:32 -07001102 unsigned long pow, r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001103
Gavin Howard63738202018-09-26 15:34:20 -06001104 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001105
Gavin Howarded38e942018-12-21 15:09:13 -07001106 // Make sure this is initialized.
1107 *result = 0;
1108
Gavin Howard7536dcf2018-12-15 19:27:09 -07001109 if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001110
Gavin Howard7b557da2018-12-21 10:25:32 -07001111 for (r = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001112
Gavin Howard7b557da2018-12-21 10:25:32 -07001113 unsigned long prev = r, powprev = pow;
Gavin Howard88c25302018-10-17 13:32:23 -06001114
Gavin Howard7b557da2018-12-21 10:25:32 -07001115 r += ((unsigned long) n->num[i]) * pow;
Gavin Howard63738202018-09-26 15:34:20 -06001116 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001117
Gavin Howard7b557da2018-12-21 10:25:32 -07001118 if (r < prev || pow < powprev) return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001119 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001120
Gavin Howard7b557da2018-12-21 10:25:32 -07001121 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001122
Gavin Howard63738202018-09-26 15:34:20 -06001123 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001124}
1125
Gavin Howard705c4bc2018-12-03 15:38:04 -07001126void bc_num_ulong2num(BcNum *n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001127
Gavin Howardec82bdc2018-10-08 23:50:57 -06001128 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001129 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001130 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001131
Gavin Howard63738202018-09-26 15:34:20 -06001132 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001133
Gavin Howard63738202018-09-26 15:34:20 -06001134 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001135
Gavin Howard705c4bc2018-12-03 15:38:04 -07001136 if (val == 0) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001137
Gavin Howardec82bdc2018-10-08 23:50:57 -06001138 for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len)
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001139 bc_num_expand(n, len);
Gavin Howardec82bdc2018-10-08 23:50:57 -06001140 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001141}
1142
Gavin Howard12fe7812018-09-29 03:45:18 -06001143BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001144 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howardf8f232b2018-10-11 17:29:47 -06001145 (void) scale;
Gavin Howard12fe7812018-09-29 03:45:18 -06001146 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001147}
1148
Gavin Howard12fe7812018-09-29 03:45:18 -06001149BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001150 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howardf8f232b2018-10-11 17:29:47 -06001151 (void) scale;
Gavin Howard12fe7812018-09-29 03:45:18 -06001152 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001153}
1154
Gavin Howard12fe7812018-09-29 03:45:18 -06001155BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001156 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001157 return bc_num_binary(a, b, c, scale, bc_num_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001158}
1159
Gavin Howard12fe7812018-09-29 03:45:18 -06001160BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001161 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001162 return bc_num_binary(a, b, c, scale, bc_num_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001163}
1164
Gavin Howard54b946a2018-10-23 12:06:57 -06001165BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001166 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001167 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001168}
1169
Gavin Howard12fe7812018-09-29 03:45:18 -06001170BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001171 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001172}
1173
Gavin Howard34edd0a2018-10-25 15:51:05 -06001174BcStatus bc_num_sqrt(BcNum *a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001175
Gavin Howard63738202018-09-26 15:34:20 -06001176 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001177 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001178 size_t pow, len, digs, digs1, resrdx, req, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001179 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard954276b2018-05-16 01:43:58 -06001180
Gavin Howard34edd0a2018-10-25 15:51:05 -06001181 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001182
Gavin Howardd8d60c42018-10-29 12:13:09 -06001183 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001184 bc_num_expand(b, req);
Gavin Howard954276b2018-05-16 01:43:58 -06001185
Gavin Howard53eba8b2018-10-31 15:14:37 -06001186 if (a->len == 0) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001187 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001188 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001189 }
Gavin Howard7536dcf2018-12-15 19:27:09 -07001190 else if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard63738202018-09-26 15:34:20 -06001191 else if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001192 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001193 bc_num_extend(b, scale);
1194 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001195 }
Gavin Howard954276b2018-05-16 01:43:58 -06001196
Gavin Howard890d0c02018-10-30 16:34:50 -06001197 scale = BC_MAX(scale, a->rdx) + 1;
1198 len = a->len + scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001199
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001200 bc_num_init(&num1, len);
1201 bc_num_init(&num2, len);
1202 bc_num_init(&half, BC_NUM_DEF_SIZE);
Gavin Howard954276b2018-05-16 01:43:58 -06001203
Gavin Howard63738202018-09-26 15:34:20 -06001204 bc_num_one(&half);
1205 half.num[0] = 5;
1206 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001207
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001208 bc_num_init(&f, len);
1209 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001210
Gavin Howard63738202018-09-26 15:34:20 -06001211 x0 = &num1;
1212 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001213
Gavin Howard63738202018-09-26 15:34:20 -06001214 bc_num_one(x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001215 pow = BC_NUM_INT(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001216
Gavin Howard53eba8b2018-10-31 15:14:37 -06001217 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001218
Gavin Howardc39fd492018-10-04 10:07:03 -06001219 if (pow & 1) x0->num[0] = 2;
1220 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001221
Gavin Howardc39fd492018-10-04 10:07:03 -06001222 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001223
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001224 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001225
Gavin Howard63738202018-09-26 15:34:20 -06001226 // Make sure to move the radix back.
1227 x0->rdx -= pow;
1228 }
Gavin Howard954276b2018-05-16 01:43:58 -06001229
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001230 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001231 resrdx = scale + 2;
1232 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001233
Gavin Howard112304b2018-12-04 14:30:01 -07001234 while (!BC_SIGINT && (cmp != 0 || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001235
Gavin Howard53eba8b2018-10-31 15:14:37 -06001236 s = bc_num_div(a, x0, &f, resrdx);
1237 if (s) goto err;
1238 s = bc_num_add(x0, &f, &fprime, resrdx);
1239 if (s) goto err;
1240 s = bc_num_mul(&fprime, &half, x1, resrdx);
1241 if (s) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001242
Gavin Howard53eba8b2018-10-31 15:14:37 -06001243 cmp = bc_num_cmp(x1, x0);
1244 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001245
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001246 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001247 else times = 0;
1248
1249 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001250
1251 cmp2 = cmp1;
1252 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001253 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001254
Gavin Howard63738202018-09-26 15:34:20 -06001255 temp = x0;
1256 x0 = x1;
1257 x1 = temp;
1258 }
Gavin Howard954276b2018-05-16 01:43:58 -06001259
Gavin Howard112304b2018-12-04 14:30:01 -07001260 if (BC_SIGINT) {
Gavin Howard63738202018-09-26 15:34:20 -06001261 s = BC_STATUS_EXEC_SIGNAL;
1262 goto err;
1263 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001264
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001265 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001266 scale -= 1;
1267 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001268
1269err:
Gavin Howard63738202018-09-26 15:34:20 -06001270 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001271 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001272 bc_num_free(&half);
Gavin Howard63738202018-09-26 15:34:20 -06001273 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001274 bc_num_free(&num1);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001275 assert(!b->neg || b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001276 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001277}
Gavin Howardba009802018-09-29 04:41:51 -06001278
Gavin Howard2cb39612018-10-22 08:46:48 -06001279BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1280
1281 BcStatus s;
1282 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001283 bool init = false;
Gavin Howard2cb39612018-10-22 08:46:48 -06001284 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
1285
1286 assert(c != d && a != b && a != d && b != d && b != c);
1287
Gavin Howard890d0c02018-10-30 16:34:50 -06001288 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001289 memcpy(&num2, c, sizeof(BcNum));
1290 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001291 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001292 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001293 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001294 else {
1295 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001296 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001297 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001298
Gavin Howard54b946a2018-10-23 12:06:57 -06001299 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001300
1301 assert(!c->neg || c->len);
1302 assert(!d->neg || d->len);
1303
1304 if (init) bc_num_free(&num2);
1305
1306 return s;
1307}
1308
Gavin Howard40a085f2018-12-03 12:08:59 -07001309#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001310BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001311
1312 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001313 BcNum base, exp, two, temp;
Gavin Howardba009802018-09-29 04:41:51 -06001314
Gavin Howard34edd0a2018-10-25 15:51:05 -06001315 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001316
Gavin Howard7536dcf2018-12-15 19:27:09 -07001317 if (c->len == 0) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1318 if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1319 if (a->rdx || b->rdx || c->rdx)
1320 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001321
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001322 bc_num_expand(d, c->len);
1323 bc_num_init(&base, c->len);
1324 bc_num_init(&exp, b->len);
1325 bc_num_init(&two, BC_NUM_DEF_SIZE);
1326 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001327
1328 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001329 two.num[0] = 2;
1330 bc_num_one(d);
1331
Gavin Howard53eba8b2018-10-31 15:14:37 -06001332 s = bc_num_rem(a, c, &base, 0);
1333 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001334 bc_num_copy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001335
Gavin Howard53eba8b2018-10-31 15:14:37 -06001336 while (exp.len != 0) {
Gavin Howardba009802018-09-29 04:41:51 -06001337
Gavin Howard53eba8b2018-10-31 15:14:37 -06001338 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
1339 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001340
1341 if (BC_NUM_ONE(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001342 s = bc_num_mul(d, &base, &temp, 0);
1343 if (s) goto err;
1344 s = bc_num_rem(&temp, c, d, 0);
1345 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001346 }
1347
Gavin Howard53eba8b2018-10-31 15:14:37 -06001348 s = bc_num_mul(&base, &base, &temp, 0);
1349 if (s) goto err;
1350 s = bc_num_rem(&temp, c, &base, 0);
1351 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001352 }
1353
1354err:
1355 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001356 bc_num_free(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001357 bc_num_free(&exp);
Gavin Howardba009802018-09-29 04:41:51 -06001358 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001359 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001360 return s;
1361}
Gavin Howarde6e84762018-10-03 11:46:34 -06001362#endif // DC_ENABLED