blob: 86c25671addc8f01631884c3b0b771438f896ef7 [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 Howard29493062018-03-20 19:57:37 -060028#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070029#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060030#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070031
Gavin Howard0be26ed2018-08-31 20:21:56 -060032void bc_num_zero(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -060033 if (!n) return;
34 memset(n->num, 0, n->cap * sizeof(char));
35 n->neg = false;
36 n->len = 0;
37 n->rdx = 0;
Gavin Howard0be26ed2018-08-31 20:21:56 -060038}
39
40void bc_num_one(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -060041 if (!n) return;
42 bc_num_zero(n);
43 n->len = 1;
44 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060045}
46
47void bc_num_ten(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -060048 if (!n) return;
49 bc_num_zero(n);
50 n->len = 2;
51 n->num[0] = 0;
52 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060053}
54
Gavin Howard8a921bd2018-10-11 14:15:32 -060055BcStatus bc_num_subArrays(BcDig *restrict n1, BcDig *restrict n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060056 size_t i, j;
57 for (i = 0; !bcg.signe && i < len; ++i) {
58 for (n1[i] -= n2[i], j = 0; !bcg.signe && n1[i + j] < 0;) {
59 n1[i + j++] += 10;
60 n1[i + j] -= 1;
61 }
62 }
63 return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060064}
65
Gavin Howard8a921bd2018-10-11 14:15:32 -060066ssize_t bc_num_compare(BcDig *restrict n1, BcDig *restrict n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060067 size_t i;
Gavin Howard8a921bd2018-10-11 14:15:32 -060068 BcDig c = 0;
Gavin Howard63738202018-09-26 15:34:20 -060069 for (i = len - 1; !bcg.signe && i < len && !(c = n1[i] - n2[i]); --i);
70 return (c < 0 ? -1 : 1) * (ssize_t) (i + 1);
Gavin Howard08bf5292018-03-20 14:59:33 -060071}
72
Gavin Howard6a804cf2018-05-17 16:54:12 -060073ssize_t bc_num_cmp(BcNum *a, BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070074
Gavin Howard63738202018-09-26 15:34:20 -060075 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -060076 BcDig *max_num, *min_num;
Gavin Howard63738202018-09-26 15:34:20 -060077 bool a_max;
78 ssize_t cmp, neg = 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070079
Gavin Howard8a921bd2018-10-11 14:15:32 -060080 if (a == b) return 0;
81 else if (!a) return !b ? 0 : !b->neg * -2 + 1;
Gavin Howard63738202018-09-26 15:34:20 -060082 else if (!b) return a->neg * -2 + 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070083
Gavin Howard63738202018-09-26 15:34:20 -060084 if (!a->len) return (!b->neg * -2 + 1) * !!b->len;
85 else if (!b->len) return a->neg * -2 + 1;
Gavin Howard2444f272018-09-11 12:25:53 -060086
Gavin Howard63738202018-09-26 15:34:20 -060087 if (a->neg) {
88 if (b->neg) neg = -1;
89 else return -1;
90 }
91 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070092
Gavin Howard63738202018-09-26 15:34:20 -060093 a_int = BC_NUM_INT(a);
94 b_int = BC_NUM_INT(b);
95 a_int -= b_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -070096
Gavin Howard63738202018-09-26 15:34:20 -060097 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -070098
Gavin Howard63738202018-09-26 15:34:20 -060099 a_max = a->rdx > b->rdx;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700100
Gavin Howard63738202018-09-26 15:34:20 -0600101 if (a_max) {
102 min = b->rdx;
103 diff = a->rdx - b->rdx;
104 max_num = a->num + diff;
105 min_num = b->num;
106 }
107 else {
108 min = a->rdx;
109 diff = b->rdx - a->rdx;
110 max_num = b->num + diff;
111 min_num = a->num;
112 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700113
Gavin Howard63738202018-09-26 15:34:20 -0600114 cmp = bc_num_compare(max_num, min_num, b_int + min);
115 if (cmp) return cmp * (!a_max * -2 + 1) * neg;
Gavin Howard021150b2018-03-10 15:40:42 -0700116
Gavin Howard63738202018-09-26 15:34:20 -0600117 for (max_num -= diff, i = diff - 1; !bcg.signe && i < diff; --i) {
118 if (max_num[i]) return neg * (!a_max * -2 + 1);
119 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700120
Gavin Howard63738202018-09-26 15:34:20 -0600121 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700122}
123
Gavin Howardd9734e52018-03-29 15:54:41 -0600124void bc_num_truncate(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700125
Gavin Howard8a921bd2018-10-11 14:15:32 -0600126 BcDig *ptr;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700127
Gavin Howard63738202018-09-26 15:34:20 -0600128 assert(places <= n->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700129
Gavin Howard63738202018-09-26 15:34:20 -0600130 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700131
Gavin Howard63738202018-09-26 15:34:20 -0600132 ptr = n->num + places;
133 n->len -= places;
134 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700135
Gavin Howard8a921bd2018-10-11 14:15:32 -0600136 memmove(n->num, ptr, n->len * sizeof(BcDig));
137 memset(n->num + n->len, 0, sizeof(BcDig) * (n->cap - n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700138}
139
Gavin Howard3f68df72018-03-22 20:30:27 -0600140BcStatus bc_num_extend(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700141
Gavin Howard63738202018-09-26 15:34:20 -0600142 BcStatus s;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600143 BcDig *ptr;
Gavin Howard63738202018-09-26 15:34:20 -0600144 size_t len;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700145
Gavin Howard63738202018-09-26 15:34:20 -0600146 if (!places) return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700147
Gavin Howard63738202018-09-26 15:34:20 -0600148 len = n->len + places;
149 if (n->cap < len && (s = bc_num_expand(n, len))) return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700150
Gavin Howard63738202018-09-26 15:34:20 -0600151 ptr = n->num + places;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600152 memmove(ptr, n->num, sizeof(BcDig) * n->len);
153 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700154
Gavin Howard63738202018-09-26 15:34:20 -0600155 n->len += places;
156 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700157
Gavin Howard63738202018-09-26 15:34:20 -0600158 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700159}
160
Gavin Howardc0f65d72018-09-01 16:59:04 -0600161void bc_num_clean(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -0600162 while (n->len > 0 && !n->num[n->len - 1]) --n->len;
163 if (n->len == 0) n->rdx = n->neg = 0;
164 else if (n->len < n->rdx) n->len = n->rdx;
Gavin Howarde57525d2018-09-01 16:56:21 -0600165}
166
Gavin Howard337fe602018-09-01 15:10:59 -0600167BcStatus bc_num_retireMul(BcNum *n, size_t scale) {
168
Gavin Howard63738202018-09-26 15:34:20 -0600169 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600170
Gavin Howard63738202018-09-26 15:34:20 -0600171 if (n->rdx < scale) s = bc_num_extend(n, scale - n->rdx);
172 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600173
Gavin Howard63738202018-09-26 15:34:20 -0600174 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600175
Gavin Howard63738202018-09-26 15:34:20 -0600176 return s;
Gavin Howard337fe602018-09-01 15:10:59 -0600177}
178
Gavin Howard9c4358c2018-03-22 20:11:28 -0600179BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
180
Gavin Howard63738202018-09-26 15:34:20 -0600181 BcStatus s;
182 BcNum one;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600183
Gavin Howard63738202018-09-26 15:34:20 -0600184 if ((s = bc_num_init(&one, BC_NUM_DEF_SIZE))) return s;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600185
Gavin Howard63738202018-09-26 15:34:20 -0600186 bc_num_one(&one);
187 s = bc_num_div(&one, a, b, scale);
188 bc_num_free(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600189
Gavin Howard63738202018-09-26 15:34:20 -0600190 return s;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600191}
192
Gavin Howard8a921bd2018-10-11 14:15:32 -0600193BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700194
Gavin Howard8a921bd2018-10-11 14:15:32 -0600195 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600196 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600197 BcDig carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700198
Gavin Howard63738202018-09-26 15:34:20 -0600199 // Because this function doesn't need to use scale (per the bc spec),
200 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600201
Gavin Howard63738202018-09-26 15:34:20 -0600202 if (!a->len) {
203 BcStatus s = bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600204 if (sub && c->len) c->neg = !c->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600205 return s;
206 }
207 else if (!b->len) return bc_num_copy(c, a);
Gavin Howardf6964a12018-03-14 10:52:31 -0600208
Gavin Howard63738202018-09-26 15:34:20 -0600209 c->neg = a->neg;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600210 memset(c->num, 0, c->cap * sizeof(BcDig));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700211
Gavin Howard63738202018-09-26 15:34:20 -0600212 c->rdx = BC_MAX(a->rdx, b->rdx);
213 min_rdx = BC_MIN(a->rdx, b->rdx);
214 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700215
Gavin Howard63738202018-09-26 15:34:20 -0600216 if (a->rdx > b->rdx) {
217 diff = a->rdx - b->rdx;
218 ptr = a->num;
219 ptr_a = a->num + diff;
220 ptr_b = b->num;
221 }
222 else {
223 diff = b->rdx - a->rdx;
224 ptr = b->num;
225 ptr_a = a->num;
226 ptr_b = b->num + diff;
227 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700228
Gavin Howard63738202018-09-26 15:34:20 -0600229 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700230
Gavin Howard63738202018-09-26 15:34:20 -0600231 ptr_c += diff;
232 a_int = BC_NUM_INT(a);
233 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700234
Gavin Howard63738202018-09-26 15:34:20 -0600235 if (a_int > b_int) {
236 min_int = b_int;
237 max = a_int;
238 ptr = ptr_a;
239 }
240 else {
241 min_int = a_int;
242 max = b_int;
243 ptr = ptr_b;
244 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700245
Gavin Howard63738202018-09-26 15:34:20 -0600246 for (carry = 0, i = 0; !bcg.signe && i < min_rdx + min_int; ++i, ++c->len) {
247 ptr_c[i] = ptr_a[i] + ptr_b[i] + carry;
248 carry = ptr_c[i] / 10;
249 ptr_c[i] %= 10;
250 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700251
Gavin Howard63738202018-09-26 15:34:20 -0600252 for (; !bcg.signe && i < max + min_rdx; ++i, ++c->len) {
253 ptr_c[i] += ptr[i] + carry;
254 carry = ptr_c[i] / 10;
255 ptr_c[i] %= 10;
256 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700257
Gavin Howard63738202018-09-26 15:34:20 -0600258 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600259
Gavin Howard63738202018-09-26 15:34:20 -0600260 if (carry) c->num[c->len++] = carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700261
Gavin Howard63738202018-09-26 15:34:20 -0600262 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700263}
264
Gavin Howard8a921bd2018-10-11 14:15:32 -0600265BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700266
Gavin Howard63738202018-09-26 15:34:20 -0600267 BcStatus s;
268 ssize_t cmp;
269 BcNum *minuend, *subtrahend;
270 size_t start;
271 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700272
Gavin Howard63738202018-09-26 15:34:20 -0600273 // Because this function doesn't need to use scale (per the bc spec),
274 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700275
Gavin Howard63738202018-09-26 15:34:20 -0600276 if (!a->len) {
277 s = bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600278 if (sub && c->len) c->neg = !c->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600279 return s;
280 }
281 else if (!b->len) return bc_num_copy(c, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700282
Gavin Howard63738202018-09-26 15:34:20 -0600283 aneg = a->neg;
284 bneg = b->neg;
285 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700286
Gavin Howard63738202018-09-26 15:34:20 -0600287 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700288
Gavin Howard63738202018-09-26 15:34:20 -0600289 a->neg = aneg;
290 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700291
Gavin Howard63738202018-09-26 15:34:20 -0600292 if (!cmp) {
293 bc_num_zero(c);
294 return BC_STATUS_SUCCESS;
295 }
296 else if (cmp > 0) {
297 neg = a->neg;
298 minuend = a;
299 subtrahend = b;
300 }
301 else {
302 neg = b->neg;
303 if (sub) neg = !neg;
304 minuend = b;
305 subtrahend = a;
306 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700307
Gavin Howard63738202018-09-26 15:34:20 -0600308 if ((s = bc_num_copy(c, minuend))) return s;
309 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700310
Gavin Howard63738202018-09-26 15:34:20 -0600311 if (c->rdx < subtrahend->rdx) {
312 if ((s = bc_num_extend(c, subtrahend->rdx - c->rdx))) return s;
313 start = 0;
314 }
315 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700316
Gavin Howard63738202018-09-26 15:34:20 -0600317 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700318
Gavin Howard63738202018-09-26 15:34:20 -0600319 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700320
Gavin Howard63738202018-09-26 15:34:20 -0600321 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700322}
323
Gavin Howard8a921bd2018-10-11 14:15:32 -0600324BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700325
Gavin Howard63738202018-09-26 15:34:20 -0600326 BcStatus s;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600327 BcDig carry;
Gavin Howard63738202018-09-26 15:34:20 -0600328 size_t i, j, len;
329 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600330
Gavin Howard63738202018-09-26 15:34:20 -0600331 c->rdx = a->rdx + b->rdx;
332 scale = BC_MAX(scale, a->rdx);
333 scale = BC_MAX(scale, b->rdx);
334 scale = BC_MIN(scale, c->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700335
Gavin Howard63738202018-09-26 15:34:20 -0600336 if (!a->len || !b->len) {
337 bc_num_zero(c);
338 return BC_STATUS_SUCCESS;
339 }
340 else if (aone || BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700341
Gavin Howard63738202018-09-26 15:34:20 -0600342 if (aone) {
Gavin Howard9ab772a2018-10-05 11:38:40 -0600343 if ((s = bc_num_copy(c, b))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600344 if (a->neg) c->neg = !c->neg;
345 }
346 else {
Gavin Howard9ab772a2018-10-05 11:38:40 -0600347 if ((s = bc_num_copy(c, a))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600348 if (b->neg) c->neg = !c->neg;
349 }
Gavin Howard337fe602018-09-01 15:10:59 -0600350
Gavin Howard63738202018-09-26 15:34:20 -0600351 return bc_num_retireMul(c, scale);
352 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700353
Gavin Howard8a921bd2018-10-11 14:15:32 -0600354 memset(c->num, 0, sizeof(BcDig) * c->cap);
Gavin Howard63738202018-09-26 15:34:20 -0600355 c->len = carry = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700356
Gavin Howard63738202018-09-26 15:34:20 -0600357 for (i = 0; !bcg.signe && i < b->len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700358
Gavin Howard63738202018-09-26 15:34:20 -0600359 for (j = 0; !bcg.signe && j < a->len; ++j) {
360 c->num[i + j] += a->num[j] * b->num[i] + carry;
361 carry = c->num[i + j] / 10;
362 c->num[i + j] %= 10;
363 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700364
Gavin Howard63738202018-09-26 15:34:20 -0600365 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600366
Gavin Howard63738202018-09-26 15:34:20 -0600367 if (carry) {
368 c->num[i + j] += carry;
369 carry = 0;
370 len = BC_MAX(len, i + j + 1);
371 }
372 else len = BC_MAX(len, i + j);
373 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700374
Gavin Howard63738202018-09-26 15:34:20 -0600375 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600376
Gavin Howard63738202018-09-26 15:34:20 -0600377 c->len = BC_MAX(len, c->rdx);
378 c->neg = !a->neg != !b->neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700379
Gavin Howard63738202018-09-26 15:34:20 -0600380 return bc_num_retireMul(c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700381}
382
Gavin Howard8a921bd2018-10-11 14:15:32 -0600383BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700384
Gavin Howard63738202018-09-26 15:34:20 -0600385 BcStatus s;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600386 BcDig *n, *bptr, q;
Gavin Howard63738202018-09-26 15:34:20 -0600387 size_t len, end, i;
388 BcNum cp;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700389
Gavin Howard63738202018-09-26 15:34:20 -0600390 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
391 else if (!a->len) {
392 bc_num_zero(c);
393 return BC_STATUS_SUCCESS;
394 }
395 else if (BC_NUM_ONE(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600396 if ((s = bc_num_copy(c, a))) return s;
397 if (b->neg) c->neg = !c->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600398 return bc_num_retireMul(c, scale);
399 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700400
Gavin Howard63738202018-09-26 15:34:20 -0600401 if ((s = bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)))) return s;
402 if ((s = bc_num_copy(&cp, a))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700403
Gavin Howard63738202018-09-26 15:34:20 -0600404 if ((len = b->len) > cp.len) {
405 if ((s = bc_num_expand(&cp, len + 2))) goto err;
406 if ((s = bc_num_extend(&cp, len - cp.len))) goto err;
407 }
Gavin Howard021150b2018-03-10 15:40:42 -0700408
Gavin Howard50651542018-09-29 02:53:11 -0600409 if (b->rdx > cp.rdx && (s = bc_num_extend(&cp, b->rdx - cp.rdx))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600410 cp.rdx -= b->rdx;
Gavin Howard50651542018-09-29 02:53:11 -0600411 if (scale > cp.rdx && (s = bc_num_extend(&cp, scale - cp.rdx))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700412
Gavin Howard63738202018-09-26 15:34:20 -0600413 if (b->rdx == b->len) {
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600414
Gavin Howard63738202018-09-26 15:34:20 -0600415 bool zero = true;
416 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600417
Gavin Howard63738202018-09-26 15:34:20 -0600418 if (i == len && zero) {
419 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
420 goto err;
421 }
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600422
Gavin Howard63738202018-09-26 15:34:20 -0600423 len -= i - 1;
424 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700425
Gavin Howard63738202018-09-26 15:34:20 -0600426 if (cp.cap == cp.len && (s = bc_num_expand(&cp, cp.len + 1))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700427
Gavin Howard63738202018-09-26 15:34:20 -0600428 // We want an extra zero in front to make things simpler.
429 cp.num[cp.len++] = 0;
430 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700431
Gavin Howard63738202018-09-26 15:34:20 -0600432 if ((s = bc_num_expand(c, cp.len))) goto err;
Gavin Howardac7656d2018-03-01 17:18:40 -0700433
Gavin Howard63738202018-09-26 15:34:20 -0600434 bc_num_zero(c);
435 c->rdx = cp.rdx;
436 c->len = cp.len;
437 bptr = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700438
Gavin Howardc39fd492018-10-04 10:07:03 -0600439 for (i = end - 1; !bcg.signe && !s && i < end; --i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700440
Gavin Howard63738202018-09-26 15:34:20 -0600441 n = cp.num + i;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700442
Gavin Howard63738202018-09-26 15:34:20 -0600443 for (q = 0; (!s && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q)
444 s = bc_num_subArrays(n, bptr, len);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700445
Gavin Howard63738202018-09-26 15:34:20 -0600446 c->num[i] = q;
447 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700448
Gavin Howard63738202018-09-26 15:34:20 -0600449 if (s) goto err;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600450
Gavin Howard63738202018-09-26 15:34:20 -0600451 c->neg = !a->neg != !b->neg;
452 s = bc_num_retireMul(c, scale);
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600453
Gavin Howardb651f1a2018-02-28 17:34:18 -0700454err:
Gavin Howard63738202018-09-26 15:34:20 -0600455 bc_num_free(&cp);
456 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700457}
458
Gavin Howard8a921bd2018-10-11 14:15:32 -0600459BcStatus bc_num_alg_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700460
Gavin Howard63738202018-09-26 15:34:20 -0600461 BcStatus s;
462 BcNum c1, c2;
Gavin Howard841a3882018-10-04 11:28:14 -0600463 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700464
Gavin Howard63738202018-09-26 15:34:20 -0600465 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
Gavin Howard8b254872018-03-14 01:13:35 -0600466
Gavin Howard63738202018-09-26 15:34:20 -0600467 if (!a->len) {
468 bc_num_zero(c);
469 return BC_STATUS_SUCCESS;
470 }
Gavin Howard8b254872018-03-14 01:13:35 -0600471
Gavin Howard63738202018-09-26 15:34:20 -0600472 if ((s = bc_num_init(&c1, len))) return s;
473 if ((s = bc_num_init(&c2, len))) goto c2_err;
474 if ((s = bc_num_div(a, b, &c1, scale))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700475
Gavin Howard841a3882018-10-04 11:28:14 -0600476 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600477
Gavin Howard63738202018-09-26 15:34:20 -0600478 if ((s = bc_num_mul(&c1, b, &c2, scale))) goto err;
479 if ((s = bc_num_sub(a, &c2, c, scale))) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600480
Gavin Howard841a3882018-10-04 11:28:14 -0600481 if (ts > c->rdx && c->len) s = bc_num_extend(c, ts - c->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700482
483err:
Gavin Howard63738202018-09-26 15:34:20 -0600484 bc_num_free(&c2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700485c2_err:
Gavin Howard63738202018-09-26 15:34:20 -0600486 bc_num_free(&c1);
487 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700488}
489
Gavin Howard8a921bd2018-10-11 14:15:32 -0600490BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700491
Gavin Howard63738202018-09-26 15:34:20 -0600492 BcStatus s;
493 BcNum copy;
494 unsigned long pow;
495 size_t i, powrdx, resrdx;
496 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700497
Gavin Howard63738202018-09-26 15:34:20 -0600498 if (b->rdx) return BC_STATUS_MATH_NON_INTEGER;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700499
Gavin Howard63738202018-09-26 15:34:20 -0600500 if (!b->len) {
501 bc_num_one(c);
502 return BC_STATUS_SUCCESS;
503 }
504 else if (!a->len) {
505 bc_num_zero(c);
506 return BC_STATUS_SUCCESS;
507 }
508 else if (BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700509
Gavin Howard63738202018-09-26 15:34:20 -0600510 if (!b->neg) s = bc_num_copy(c, a);
511 else s = bc_num_inv(a, c, scale);
Gavin Howard5fc40e72018-03-05 12:11:09 -0700512
Gavin Howard63738202018-09-26 15:34:20 -0600513 return s;
514 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700515
Gavin Howard63738202018-09-26 15:34:20 -0600516 neg = b->neg;
517 b->neg = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700518
Gavin Howard63738202018-09-26 15:34:20 -0600519 if ((s = bc_num_ulong(b, &pow))) return s;
520 if ((s = bc_num_init(&copy, a->len))) return s;
521 if ((s = bc_num_copy(&copy, a))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700522
Gavin Howard63738202018-09-26 15:34:20 -0600523 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600524
Gavin Howard63738202018-09-26 15:34:20 -0600525 b->neg = neg;
Gavin Howardb29674f2018-03-22 22:24:58 -0600526
Gavin Howard63738202018-09-26 15:34:20 -0600527 for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) {
528 powrdx <<= 1;
529 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
530 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700531
Gavin Howard63738202018-09-26 15:34:20 -0600532 if ((s = bc_num_copy(c, &copy))) goto err;
533 if (bcg.signe) {
534 s = BC_STATUS_EXEC_SIGNAL;
535 goto err;
536 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700537
Gavin Howardc39fd492018-10-04 10:07:03 -0600538 for (resrdx = powrdx, pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) {
Gavin Howard1d959152018-03-03 23:33:13 -0700539
Gavin Howard63738202018-09-26 15:34:20 -0600540 powrdx <<= 1;
Gavin Howard1d959152018-03-03 23:33:13 -0700541
Gavin Howard63738202018-09-26 15:34:20 -0600542 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
Gavin Howard1d959152018-03-03 23:33:13 -0700543
Gavin Howard63738202018-09-26 15:34:20 -0600544 if (pow & 1) {
545 resrdx += powrdx;
546 if ((s = bc_num_mul(c, &copy, c, resrdx))) goto err;
547 }
548 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700549
Gavin Howard63738202018-09-26 15:34:20 -0600550 if (neg && (s = bc_num_inv(c, c, scale))) goto err;
551 if (bcg.signe) {
552 s = BC_STATUS_EXEC_SIGNAL;
553 goto err;
554 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700555
Gavin Howard63738202018-09-26 15:34:20 -0600556 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700557
Gavin Howard63738202018-09-26 15:34:20 -0600558 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
559 if (zero) bc_num_zero(c);
Gavin Howard1d959152018-03-03 23:33:13 -0700560
Gavin Howard6fbdb292018-02-27 15:44:48 -0700561err:
Gavin Howard63738202018-09-26 15:34:20 -0600562 bc_num_free(&copy);
563 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700564}
565
Gavin Howard63738202018-09-26 15:34:20 -0600566BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600567 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700568{
Gavin Howarda1c44392018-09-27 12:41:15 -0600569 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600570 BcNum num2, *ptr_a, *ptr_b;
571 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700572
Gavin Howard63738202018-09-26 15:34:20 -0600573 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700574
Gavin Howard63738202018-09-26 15:34:20 -0600575 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600576 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600577 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600578 init = true;
579 }
580 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700581
Gavin Howard63738202018-09-26 15:34:20 -0600582 if (c == b) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700583
Gavin Howardba009802018-09-29 04:41:51 -0600584 ptr_b = &num2;
585
586 if (c != a) {
587 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600588 init = true;
589 }
590 }
591 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700592
Gavin Howarda1c44392018-09-27 12:41:15 -0600593 if (init) s = bc_num_init(c, req);
594 else s = bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700595
Gavin Howard9ab772a2018-10-05 11:38:40 -0600596 if (!s) s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700597
Gavin Howard3cf769e2018-10-11 13:55:11 -0600598 assert(!c->neg || c->len);
599
Gavin Howardba009802018-09-29 04:41:51 -0600600 if (init) bc_num_free(&num2);
Gavin Howarda1c44392018-09-27 12:41:15 -0600601 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700602}
603
Gavin Howard3f68df72018-03-22 20:30:27 -0600604bool bc_num_strValid(const char *val, size_t base) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700605
Gavin Howard8a921bd2018-10-11 14:15:32 -0600606 BcDig b;
Gavin Howard63738202018-09-26 15:34:20 -0600607 bool small, radix = false;
608 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700609
Gavin Howard63738202018-09-26 15:34:20 -0600610 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700611
Gavin Howard63738202018-09-26 15:34:20 -0600612 small = base <= 10;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600613 b = (BcDig) (small ? base + '0' : base - 9 + 'A');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700614
Gavin Howard63738202018-09-26 15:34:20 -0600615 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700616
Gavin Howard8a921bd2018-10-11 14:15:32 -0600617 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600618
Gavin Howard63738202018-09-26 15:34:20 -0600619 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700620
Gavin Howard63738202018-09-26 15:34:20 -0600621 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700622
Gavin Howard63738202018-09-26 15:34:20 -0600623 radix = true;
624 continue;
625 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700626
Gavin Howard63738202018-09-26 15:34:20 -0600627 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
628 return false;
629 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700630
Gavin Howard63738202018-09-26 15:34:20 -0600631 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700632}
633
Gavin Howard3f68df72018-03-22 20:30:27 -0600634BcStatus bc_num_parseDecimal(BcNum *n, const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700635
Gavin Howard63738202018-09-26 15:34:20 -0600636 BcStatus s;
637 size_t len, i;
638 const char *ptr;
639 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700640
Gavin Howard63738202018-09-26 15:34:20 -0600641 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700642
Gavin Howard63738202018-09-26 15:34:20 -0600643 val += i;
644 len = strlen(val);
645 bc_num_zero(n);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700646
Gavin Howard63738202018-09-26 15:34:20 -0600647 if (len) {
648 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
649 if ((s = bc_num_expand(n, len))) return s;
650 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600651
Gavin Howard63738202018-09-26 15:34:20 -0600652 if (zero) {
Gavin Howard8a921bd2018-10-11 14:15:32 -0600653 memset(n->num, 0, sizeof(BcDig) * n->cap);
Gavin Howard63738202018-09-26 15:34:20 -0600654 n->neg = false;
655 return BC_STATUS_SUCCESS;
656 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700657
Gavin Howard63738202018-09-26 15:34:20 -0600658 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700659
Gavin Howard63738202018-09-26 15:34:20 -0600660 // Explicitly test for NULL here to produce either a 0 or 1.
661 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700662
Gavin Howard63738202018-09-26 15:34:20 -0600663 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
664 n->num[n->len] = val[i] - '0';
Gavin Howard6fbdb292018-02-27 15:44:48 -0700665
Gavin Howard63738202018-09-26 15:34:20 -0600666 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700667}
668
Gavin Howard3f68df72018-03-22 20:30:27 -0600669BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
Gavin Howardede51f02018-03-02 12:30:00 -0700670
Gavin Howard63738202018-09-26 15:34:20 -0600671 BcStatus s;
672 BcNum temp, mult, result;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600673 BcDig c = '\0';
Gavin Howard63738202018-09-26 15:34:20 -0600674 bool zero;
675 unsigned long v;
676 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700677
Gavin Howard63738202018-09-26 15:34:20 -0600678 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700679
Gavin Howard63738202018-09-26 15:34:20 -0600680 for (zero = true, i = 0; zero && i < len; ++i)
681 zero = (val[i] == '.' || val[i] == '0');
682 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700683
Gavin Howard63738202018-09-26 15:34:20 -0600684 if ((s = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return s;
685 if ((s = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700686
Gavin Howard63738202018-09-26 15:34:20 -0600687 for (i = 0; i < len && (c = val[i]) != '.'; ++i) {
Gavin Howardede51f02018-03-02 12:30:00 -0700688
Gavin Howard63738202018-09-26 15:34:20 -0600689 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700690
Gavin Howard63738202018-09-26 15:34:20 -0600691 if ((s = bc_num_mul(n, base, &mult, 0))) goto int_err;
692 if ((s = bc_num_ulong2num(&temp, v))) goto int_err;
693 if ((s = bc_num_add(&mult, &temp, n, 0))) goto int_err;
694 }
Gavin Howardede51f02018-03-02 12:30:00 -0700695
Gavin Howard63738202018-09-26 15:34:20 -0600696 if (i == len && !(c = val[i])) goto int_err;
697 assert(c == '.');
698 if ((s = bc_num_init(&result, base->len))) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700699
Gavin Howard63738202018-09-26 15:34:20 -0600700 bc_num_zero(&result);
701 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700702
Gavin Howard63738202018-09-26 15:34:20 -0600703 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howardede51f02018-03-02 12:30:00 -0700704
Gavin Howard63738202018-09-26 15:34:20 -0600705 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardede51f02018-03-02 12:30:00 -0700706
Gavin Howard63738202018-09-26 15:34:20 -0600707 if ((s = bc_num_mul(&result, base, &result, 0))) goto err;
708 if ((s = bc_num_ulong2num(&temp, v))) goto err;
709 if ((s = bc_num_add(&result, &temp, &result, 0))) goto err;
710 if ((s = bc_num_mul(&mult, base, &mult, 0))) goto err;
711 }
Gavin Howardede51f02018-03-02 12:30:00 -0700712
Gavin Howard63738202018-09-26 15:34:20 -0600713 if ((s = bc_num_div(&result, &mult, &result, digits))) goto err;
714 if ((s = bc_num_add(n, &result, n, digits))) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600715
Gavin Howard63738202018-09-26 15:34:20 -0600716 if (n->len) {
717 if (n->rdx < digits && n->len) s = bc_num_extend(n, digits - n->rdx);
718 }
719 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700720
721err:
Gavin Howard63738202018-09-26 15:34:20 -0600722 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700723int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600724 bc_num_free(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700725mult_err:
Gavin Howard63738202018-09-26 15:34:20 -0600726 bc_num_free(&temp);
727 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700728}
729
Gavin Howard80977b22018-09-06 14:29:13 -0600730BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) {
Gavin Howard63738202018-09-26 15:34:20 -0600731 if (*nchars == line_len - 1) {
732 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
733 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
734 *nchars = 0;
735 }
Gavin Howard63738202018-09-26 15:34:20 -0600736 return BC_STATUS_SUCCESS;
Gavin Howard80977b22018-09-06 14:29:13 -0600737}
738
Gavin Howard83eb8392018-10-09 01:21:19 -0600739#ifdef DC_ENABLED
740BcStatus bc_num_printChar(size_t num, size_t width, bool radix,
741 size_t *nchars, size_t line_len)
742{
Gavin Howard4c091542018-10-09 03:33:08 -0600743 (void) radix, (void) line_len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600744
Gavin Howard4c091542018-10-09 03:33:08 -0600745 if (putc((char) num, stdout) == EOF) return BC_STATUS_IO_ERR;
Gavin Howard83eb8392018-10-09 01:21:19 -0600746 *nchars = *nchars + width;
747
748 return BC_STATUS_SUCCESS;
749}
750#endif // DC_ENABLED
751
Gavin Howard80977b22018-09-06 14:29:13 -0600752BcStatus bc_num_printDigits(size_t num, size_t width, bool radix,
753 size_t *nchars, size_t line_len)
754{
Gavin Howard63738202018-09-26 15:34:20 -0600755 BcStatus s;
Gavin Howardc39fd492018-10-04 10:07:03 -0600756 size_t exp, pow, div;
Gavin Howard80977b22018-09-06 14:29:13 -0600757
Gavin Howard63738202018-09-26 15:34:20 -0600758 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600759 if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR;
760 ++(*nchars);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600761
Gavin Howard63738202018-09-26 15:34:20 -0600762 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600763 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600764
Gavin Howard63738202018-09-26 15:34:20 -0600765 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600766
Gavin Howard63738202018-09-26 15:34:20 -0600767 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600768
Gavin Howard63738202018-09-26 15:34:20 -0600769 div = num / pow;
770 num -= div * pow;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600771
Gavin Howard63738202018-09-26 15:34:20 -0600772 if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
773 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700774
Gavin Howard63738202018-09-26 15:34:20 -0600775 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700776}
Gavin Howardfe679f02018-02-14 15:50:09 -0700777
Gavin Howard902a16c2018-05-16 01:22:53 -0600778BcStatus bc_num_printHex(size_t num, size_t width, bool radix,
Gavin Howard0011a3a2018-03-29 23:11:32 -0600779 size_t *nchars, size_t line_len)
Gavin Howard2682a1f2018-03-03 09:09:13 -0700780{
Gavin Howard63738202018-09-26 15:34:20 -0600781 BcStatus s;
Gavin Howard80977b22018-09-06 14:29:13 -0600782
Gavin Howard63738202018-09-26 15:34:20 -0600783 assert(width == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600784
Gavin Howard63738202018-09-26 15:34:20 -0600785 if (radix) {
786 if ((s = bc_num_printNewline(nchars, line_len))) return s;
787 if (putchar('.') == EOF) return BC_STATUS_IO_ERR;
788 *nchars += 1;
789 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700790
Gavin Howard63738202018-09-26 15:34:20 -0600791 if ((s = bc_num_printNewline(nchars, line_len))) return s;
792 if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR;
793 *nchars = *nchars + width;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700794
Gavin Howard63738202018-09-26 15:34:20 -0600795 return BC_STATUS_SUCCESS;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700796}
797
Gavin Howard5f46ee32018-09-25 13:02:43 -0600798BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700799
Gavin Howardc39fd492018-10-04 10:07:03 -0600800 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600801 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700802
Gavin Howard63738202018-09-26 15:34:20 -0600803 if (n->neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
804 (*nchars) += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700805
Gavin Howard63738202018-09-26 15:34:20 -0600806 for (i = n->len - 1; !s && i < n->len; --i)
807 s = bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700808
Gavin Howard63738202018-09-26 15:34:20 -0600809 return s;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700810}
811
Gavin Howard83eb8392018-10-09 01:21:19 -0600812BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, size_t *nchars,
813 size_t line_len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -0600814{
Gavin Howard63738202018-09-26 15:34:20 -0600815 BcStatus s;
816 BcVec stack;
817 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -0600818 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -0600819 size_t i;
820 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700821
Gavin Howard83eb8392018-10-09 01:21:19 -0600822 if ((s = bc_vec_init(&stack, sizeof(long), NULL))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600823 if ((s = bc_num_init(&intp, n->len))) goto int_err;
824 if ((s = bc_num_init(&fracp, n->rdx))) goto frac_err;
825 if ((s = bc_num_init(&digit, width))) goto digit_err;
826 if ((s = bc_num_init(&frac_len, BC_NUM_INT(n)))) goto frac_len_err;
827 if ((s = bc_num_copy(&intp, n))) goto err;
828 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -0600829
Gavin Howard63738202018-09-26 15:34:20 -0600830 bc_num_truncate(&intp, intp.rdx);
831 if ((s = bc_num_sub(n, &intp, &fracp, 0))) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700832
Gavin Howard4c091542018-10-09 03:33:08 -0600833 if (!n->len) {
834 s = print(0, width, false, nchars, line_len);
835 goto err;
836 }
837
Gavin Howard63738202018-09-26 15:34:20 -0600838 while (intp.len) {
Gavin Howardbe53edd2018-10-04 10:55:08 -0600839 if ((s = bc_num_rem(&intp, base, &digit, 0))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600840 if ((s = bc_num_ulong(&digit, &dig))) goto err;
Gavin Howard90198862018-09-29 03:37:47 -0600841 if ((s = bc_vec_push(&stack, &dig))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600842 if ((s = bc_num_div(&intp, base, &intp, 0))) goto err;
843 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700844
Gavin Howard63738202018-09-26 15:34:20 -0600845 for (i = 0; i < stack.len; ++i) {
846 ptr = bc_vec_item_rev(&stack, i);
847 assert(ptr);
848 if ((s = print(*ptr, width, false, nchars, line_len))) goto err;
849 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700850
Gavin Howard63738202018-09-26 15:34:20 -0600851 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700852
Gavin Howard63738202018-09-26 15:34:20 -0600853 for (radix = true; frac_len.len <= n->rdx; radix = false) {
854 if ((s = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err;
855 if ((s = bc_num_ulong(&fracp, &dig))) goto err;
856 if ((s = bc_num_ulong2num(&intp, dig))) goto err;
857 if ((s = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err;
858 if ((s = print(dig, width, radix, nchars, line_len))) goto err;
859 if ((s = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err;
860 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700861
862err:
Gavin Howard63738202018-09-26 15:34:20 -0600863 bc_num_free(&frac_len);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700864frac_len_err:
Gavin Howard63738202018-09-26 15:34:20 -0600865 bc_num_free(&digit);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700866digit_err:
Gavin Howard63738202018-09-26 15:34:20 -0600867 bc_num_free(&fracp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700868frac_err:
Gavin Howard63738202018-09-26 15:34:20 -0600869 bc_num_free(&intp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700870int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600871 bc_vec_free(&stack);
Gavin Howard63738202018-09-26 15:34:20 -0600872 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700873}
874
Gavin Howard83eb8392018-10-09 01:21:19 -0600875BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
876 size_t *nchars, size_t line_len)
877{
878 BcStatus s;
879 size_t width, i;
880 BcNumDigitOp print;
881 bool neg = n->neg;
882
883 if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
884 (*nchars) += neg;
885
886 n->neg = false;
887
888 if (base_t <= BC_NUM_MAX_IBASE) {
889 width = 1;
890 print = bc_num_printHex;
891 }
892 else {
893 for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width);
894 print = bc_num_printDigits;
895 }
896
897 s = bc_num_printNum(n, base, width, nchars, line_len, print);
898 n->neg = neg;
899
900 return s;
901}
902
903#ifdef DC_ENABLED
Gavin Howard844e7a52018-10-09 11:46:39 -0600904BcStatus bc_num_stream(BcNum *n, BcNum *base, size_t *nchars, size_t len) {
Gavin Howard83eb8392018-10-09 01:21:19 -0600905 return bc_num_printNum(n, base, 1, nchars, len, bc_num_printChar);
906}
907#endif // DC_ENABLED
908
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700909BcStatus bc_num_init(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700910
Gavin Howard63738202018-09-26 15:34:20 -0600911 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -0700912
Gavin Howard63738202018-09-26 15:34:20 -0600913 request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE;
Gavin Howardc39fd492018-10-04 10:07:03 -0600914 memset(n, 0, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600915 if (!(n->num = malloc(request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -0700916
Gavin Howard63738202018-09-26 15:34:20 -0600917 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700918
Gavin Howard63738202018-09-26 15:34:20 -0600919 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700920}
921
Gavin Howard844e7a52018-10-09 11:46:39 -0600922BcStatus bc_num_expand(BcNum *n, size_t req) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700923
Gavin Howard8a921bd2018-10-11 14:15:32 -0600924 BcDig *temp;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600925
Gavin Howard844e7a52018-10-09 11:46:39 -0600926 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -0700927
Gavin Howard844e7a52018-10-09 11:46:39 -0600928 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb5c77212018-02-14 17:12:34 -0700929
Gavin Howard844e7a52018-10-09 11:46:39 -0600930 if (req <= n->cap) return BC_STATUS_SUCCESS;
931 if (!(temp = realloc(n->num, req))) return BC_STATUS_ALLOC_ERR;
932
933 memset(temp + n->cap, 0, sizeof(char) * (req - n->cap));
Gavin Howard63738202018-09-26 15:34:20 -0600934 n->num = temp;
Gavin Howard844e7a52018-10-09 11:46:39 -0600935 n->cap = req;
Gavin Howardb5c77212018-02-14 17:12:34 -0700936
Gavin Howard63738202018-09-26 15:34:20 -0600937 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700938}
939
Gavin Howarded392aa2018-02-27 13:09:26 -0700940void bc_num_free(void *num) {
Gavin Howard63738202018-09-26 15:34:20 -0600941 BcNum *n = (BcNum*) num;
Gavin Howard8c019982018-10-05 12:35:09 -0600942 assert(n);
Gavin Howard2bc54182018-10-06 17:03:46 -0600943 free(n->num);
Gavin Howardb5c77212018-02-14 17:12:34 -0700944}
945
Gavin Howard9c4358c2018-03-22 20:11:28 -0600946BcStatus bc_num_copy(BcNum *d, BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -0700947
Gavin Howardb20c2db2018-10-08 10:31:23 -0600948 BcStatus status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700949
Gavin Howard63738202018-09-26 15:34:20 -0600950 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -0700951
Gavin Howard63738202018-09-26 15:34:20 -0600952 if (d == s) return BC_STATUS_SUCCESS;
Gavin Howardb20c2db2018-10-08 10:31:23 -0600953 if ((status = bc_num_expand(d, s->cap))) return status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700954
Gavin Howard63738202018-09-26 15:34:20 -0600955 d->len = s->len;
956 d->neg = s->neg;
957 d->rdx = s->rdx;
Gavin Howard5a049c42018-02-15 11:24:11 -0700958
Gavin Howard8a921bd2018-10-11 14:15:32 -0600959 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
960 memset(d->num + d->len, 0, sizeof(BcDig) * (d->cap - d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -0700961
Gavin Howardb20c2db2018-10-08 10:31:23 -0600962 return status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700963}
964
Gavin Howard5cea43b2018-03-02 11:32:11 -0700965BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
Gavin Howard025d04d2018-02-20 13:53:28 -0700966
Gavin Howard63738202018-09-26 15:34:20 -0600967 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700968
Gavin Howard63738202018-09-26 15:34:20 -0600969 assert(n && val && base);
970 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_NUM_MAX_IBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700971
Gavin Howard63738202018-09-26 15:34:20 -0600972 if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700973
Gavin Howard63738202018-09-26 15:34:20 -0600974 if (base_t == 10) s = bc_num_parseDecimal(n, val);
975 else s = bc_num_parseBase(n, val, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700976
Gavin Howard63738202018-09-26 15:34:20 -0600977 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700978}
979
Gavin Howard0011a3a2018-03-29 23:11:32 -0600980BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
981 size_t *nchars, size_t line_len)
Gavin Howard152f3e82018-03-07 12:33:15 -0700982{
Gavin Howardc39fd492018-10-04 10:07:03 -0600983 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700984
Gavin Howard63738202018-09-26 15:34:20 -0600985 assert(n && base && nchars);
986 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700987
Gavin Howard63738202018-09-26 15:34:20 -0600988 if (*nchars >= line_len) {
989 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
990 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
991 *nchars = 0;
992 }
Gavin Howardbc7cae82018-03-14 13:43:04 -0600993
Gavin Howard63738202018-09-26 15:34:20 -0600994 if (!n->len) {
995 if (putchar('0') == EOF) return BC_STATUS_IO_ERR;
996 ++(*nchars);
Gavin Howard63738202018-09-26 15:34:20 -0600997 }
998 else if (base_t == 10) s = bc_num_printDecimal(n, nchars, line_len);
999 else s = bc_num_printBase(n, base, base_t, nchars, line_len);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001000
Gavin Howard63738202018-09-26 15:34:20 -06001001 if (s) return s;
Gavin Howard152f3e82018-03-07 12:33:15 -07001002
Gavin Howard63738202018-09-26 15:34:20 -06001003 if (newline) {
1004 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
1005 *nchars = 0;
1006 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001007
Gavin Howard63738202018-09-26 15:34:20 -06001008 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001009}
1010
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001011BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001012
Gavin Howard63738202018-09-26 15:34:20 -06001013 size_t i;
1014 unsigned long pow;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001015
Gavin Howard63738202018-09-26 15:34:20 -06001016 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001017
Gavin Howard63738202018-09-26 15:34:20 -06001018 if (n->neg) return BC_STATUS_MATH_NEGATIVE;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001019
Gavin Howard63738202018-09-26 15:34:20 -06001020 for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001021
Gavin Howard63738202018-09-26 15:34:20 -06001022 unsigned long prev = *result, powprev = pow;
1023 *result += ((unsigned long) n->num[i]) * pow;
1024 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001025
Gavin Howard63738202018-09-26 15:34:20 -06001026 if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW;
1027 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001028
Gavin Howard63738202018-09-26 15:34:20 -06001029 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001030}
1031
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001032BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001033
Gavin Howard63738202018-09-26 15:34:20 -06001034 BcStatus s;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001035 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001036 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001037 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001038
Gavin Howard63738202018-09-26 15:34:20 -06001039 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001040
Gavin Howard63738202018-09-26 15:34:20 -06001041 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001042
Gavin Howard63738202018-09-26 15:34:20 -06001043 if (!val) {
1044 memset(n->num, 0, sizeof(char) * n->cap);
1045 return BC_STATUS_SUCCESS;
1046 }
Gavin Howard8e2cc692018-02-15 17:39:14 -07001047
Gavin Howardec82bdc2018-10-08 23:50:57 -06001048 for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len)
Gavin Howard63738202018-09-26 15:34:20 -06001049 if ((s = bc_num_expand(n, len))) return s;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001050 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001051
Gavin Howard63738202018-09-26 15:34:20 -06001052 return BC_STATUS_SUCCESS;
Gavin Howard025d04d2018-02-20 13:53:28 -07001053}
1054
Gavin Howard12fe7812018-09-29 03:45:18 -06001055BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001056 (void) scale;
1057 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s;
Gavin Howard12fe7812018-09-29 03:45:18 -06001058 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001059}
1060
Gavin Howard12fe7812018-09-29 03:45:18 -06001061BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001062 (void) scale;
1063 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a;
Gavin Howard12fe7812018-09-29 03:45:18 -06001064 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001065}
1066
Gavin Howard12fe7812018-09-29 03:45:18 -06001067BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001068 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001069 return bc_num_binary(a, b, c, scale, bc_num_alg_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001070}
1071
Gavin Howard12fe7812018-09-29 03:45:18 -06001072BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001073 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001074 return bc_num_binary(a, b, c, scale, bc_num_alg_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001075}
1076
Gavin Howardbe53edd2018-10-04 10:55:08 -06001077BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001078 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howardbe53edd2018-10-04 10:55:08 -06001079 return bc_num_binary(a, b, c, scale, bc_num_alg_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001080}
1081
Gavin Howard12fe7812018-09-29 03:45:18 -06001082BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
1083 return bc_num_binary(a, b, c, scale, bc_num_alg_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001084}
1085
Gavin Howard757b66a2018-10-06 04:13:46 -06001086BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001087
Gavin Howard63738202018-09-26 15:34:20 -06001088 BcStatus s;
1089 BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001090 size_t pow, len, digits, digits1, resrdx, req, times = 0;
1091 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard954276b2018-05-16 01:43:58 -06001092
Gavin Howard757b66a2018-10-06 04:13:46 -06001093 assert(a && b);
Gavin Howard954276b2018-05-16 01:43:58 -06001094
Gavin Howard63738202018-09-26 15:34:20 -06001095 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a)+ 1) >> 1) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001096
Gavin Howard757b66a2018-10-06 04:13:46 -06001097 if (b == a) {
1098 memcpy(&a2, b, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001099 ptr_a = &a2;
Gavin Howard757b66a2018-10-06 04:13:46 -06001100 s = bc_num_init(b, req);
Gavin Howard63738202018-09-26 15:34:20 -06001101 }
1102 else {
1103 ptr_a = a;
Gavin Howard757b66a2018-10-06 04:13:46 -06001104 s = bc_num_expand(b, req);
Gavin Howard63738202018-09-26 15:34:20 -06001105 }
Gavin Howard954276b2018-05-16 01:43:58 -06001106
Gavin Howard63738202018-09-26 15:34:20 -06001107 if (s) goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001108
Gavin Howard63738202018-09-26 15:34:20 -06001109 if (!ptr_a->len) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001110 bc_num_zero(b);
Gavin Howard63738202018-09-26 15:34:20 -06001111 goto init_err;
1112 }
1113 else if (ptr_a->neg) {
Gavin Howard4c4f5f02018-10-11 11:52:11 -06001114 s = BC_STATUS_MATH_NEGATIVE;
Gavin Howard63738202018-09-26 15:34:20 -06001115 goto init_err;
1116 }
1117 else if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001118 bc_num_one(b);
1119 s = bc_num_extend(b, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001120 goto init_err;
1121 }
Gavin Howard954276b2018-05-16 01:43:58 -06001122
Gavin Howard8a921bd2018-10-11 14:15:32 -06001123 memset(b->num, 0, b->cap * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -06001124 len = ptr_a->len;
Gavin Howard954276b2018-05-16 01:43:58 -06001125
Gavin Howard63738202018-09-26 15:34:20 -06001126 scale = BC_MAX(scale, ptr_a->rdx) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001127
Gavin Howard63738202018-09-26 15:34:20 -06001128 if ((s = bc_num_init(&num1, len))) goto init_err;
1129 if ((s = bc_num_init(&num2, num1.len))) goto num2_err;
1130 if ((s = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001131
Gavin Howard63738202018-09-26 15:34:20 -06001132 bc_num_one(&half);
1133 half.num[0] = 5;
1134 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001135
Gavin Howard63738202018-09-26 15:34:20 -06001136 len += scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001137
Gavin Howard63738202018-09-26 15:34:20 -06001138 if ((s = bc_num_init(&f, len))) goto f_err;
1139 if ((s = bc_num_init(&fprime, len + scale))) goto fprime_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001140
Gavin Howard63738202018-09-26 15:34:20 -06001141 x0 = &num1;
1142 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001143
Gavin Howard63738202018-09-26 15:34:20 -06001144 bc_num_one(x0);
Gavin Howard954276b2018-05-16 01:43:58 -06001145
Gavin Howardc39fd492018-10-04 10:07:03 -06001146 if ((pow = BC_NUM_INT(ptr_a))) {
Gavin Howard954276b2018-05-16 01:43:58 -06001147
Gavin Howardc39fd492018-10-04 10:07:03 -06001148 if (pow & 1) x0->num[0] = 2;
1149 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001150
Gavin Howardc39fd492018-10-04 10:07:03 -06001151 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001152
Gavin Howard63738202018-09-26 15:34:20 -06001153 if ((s = bc_num_extend(x0, pow))) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06001154
Gavin Howard63738202018-09-26 15:34:20 -06001155 // Make sure to move the radix back.
1156 x0->rdx -= pow;
1157 }
Gavin Howard954276b2018-05-16 01:43:58 -06001158
Gavin Howard9f2395b2018-10-06 05:28:58 -06001159 x0->rdx = digits = digits1 = 0;
1160 resrdx = scale + 2;
1161 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001162
Gavin Howard0bfcf7a2018-10-11 09:37:09 -06001163 while (!bcg.signe && (cmp || digits < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001164
Gavin Howard63738202018-09-26 15:34:20 -06001165 if ((s = bc_num_div(a, x0, &f, resrdx))) goto err;
1166 if ((s = bc_num_add(x0, &f, &fprime, resrdx))) goto err;
1167 if ((s = bc_num_mul(&fprime, &half, x1, resrdx))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001168
Gavin Howard63738202018-09-26 15:34:20 -06001169 cmp = bc_num_cmp(x1, x0);
1170 digits = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001171
Gavin Howardb11e9e22018-10-06 17:26:02 -06001172 if (cmp == cmp2 && digits == digits1) times += 1;
1173 else times = 0;
1174
1175 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001176
1177 cmp2 = cmp1;
1178 cmp1 = cmp;
1179 digits1 = digits;
1180
Gavin Howard63738202018-09-26 15:34:20 -06001181 temp = x0;
1182 x0 = x1;
1183 x1 = temp;
1184 }
Gavin Howard954276b2018-05-16 01:43:58 -06001185
Gavin Howard63738202018-09-26 15:34:20 -06001186 if (bcg.signe) {
1187 s = BC_STATUS_EXEC_SIGNAL;
1188 goto err;
1189 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001190
Gavin Howard757b66a2018-10-06 04:13:46 -06001191 if ((s = bc_num_copy(b, x0))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001192
Gavin Howard757b66a2018-10-06 04:13:46 -06001193 if (b->rdx > --scale) bc_num_truncate(b, b->rdx - scale);
1194 else if (b->rdx < scale) s = bc_num_extend(b, scale - b->rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06001195
1196err:
Gavin Howard63738202018-09-26 15:34:20 -06001197 bc_num_free(&fprime);
Gavin Howard954276b2018-05-16 01:43:58 -06001198fprime_err:
Gavin Howard63738202018-09-26 15:34:20 -06001199 bc_num_free(&f);
Gavin Howard954276b2018-05-16 01:43:58 -06001200f_err:
Gavin Howard63738202018-09-26 15:34:20 -06001201 bc_num_free(&half);
Gavin Howard954276b2018-05-16 01:43:58 -06001202two_err:
Gavin Howard63738202018-09-26 15:34:20 -06001203 bc_num_free(&num2);
Gavin Howard954276b2018-05-16 01:43:58 -06001204num2_err:
Gavin Howard63738202018-09-26 15:34:20 -06001205 bc_num_free(&num1);
Gavin Howard954276b2018-05-16 01:43:58 -06001206init_err:
Gavin Howard757b66a2018-10-06 04:13:46 -06001207 if (b == a) bc_num_free(&a2);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001208 assert(!b->neg || b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001209 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001210}
Gavin Howardba009802018-09-29 04:41:51 -06001211
Gavin Howarde6e84762018-10-03 11:46:34 -06001212#ifdef DC_ENABLED
Gavin Howardba009802018-09-29 04:41:51 -06001213BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1214
1215 BcStatus s;
Gavin Howard2c8959e2018-10-04 18:32:58 -06001216 BcNum num2, *ptr_a, *ptr_b, *ptr_c, base, exp, two, temp;
Gavin Howardba009802018-09-29 04:41:51 -06001217 bool init = false;
1218
1219 assert(a && b && c && d);
1220
1221 scale = 0;
1222
1223 if (d == a) {
1224 memcpy(&num2, d, sizeof(BcNum));
1225 ptr_a = &num2;
1226 init = true;
1227 }
1228 else ptr_a = a;
1229
1230 if (d == b) {
1231
1232 ptr_b = &num2;
1233
1234 if (d != a) {
1235 memcpy(ptr_b, d, sizeof(BcNum));
1236 init = true;
1237 }
1238 }
1239 else ptr_b = b;
1240
1241 if (d == c) {
1242
1243 ptr_c = &num2;
1244
1245 if (d != a && d != b) {
1246 memcpy(ptr_c, d, sizeof(BcNum));
1247 init = true;
1248 }
1249 }
1250 else ptr_c = c;
1251
1252 if (init) s = bc_num_init(d, ptr_c->len);
1253 else s = bc_num_expand(d, ptr_c->len);
1254
1255 if (s) return s;
1256
1257 if (!ptr_c->len) {
1258 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
1259 goto base_err;
1260 }
1261
Gavin Howard5ef610b2018-10-04 16:57:47 -06001262 if (ptr_a->rdx || ptr_b->rdx || ptr_c->rdx) {
Gavin Howardba009802018-09-29 04:41:51 -06001263 s = BC_STATUS_MATH_NON_INTEGER;
1264 goto base_err;
1265 }
1266
1267 if (ptr_b->neg) {
1268 s = BC_STATUS_MATH_NEGATIVE;
1269 goto base_err;
1270 }
1271
Gavin Howardba009802018-09-29 04:41:51 -06001272 if ((s = bc_num_init(&base, ptr_c->len))) goto base_err;
1273 if ((s = bc_num_init(&exp, ptr_b->len))) goto exp_err;
1274 if ((s = bc_num_init(&two, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howardba009802018-09-29 04:41:51 -06001275 if ((s = bc_num_init(&temp, ptr_b->len))) goto temp_err;
1276
1277 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001278 two.num[0] = 2;
1279 bc_num_one(d);
1280
Gavin Howardbe53edd2018-10-04 10:55:08 -06001281 if ((s = bc_num_rem(ptr_a, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001282 if ((s = bc_num_copy(&exp, ptr_b))) goto err;
1283
Gavin Howardaf905602018-09-29 18:56:37 -06001284 while (exp.len) {
Gavin Howardba009802018-09-29 04:41:51 -06001285
Gavin Howardbe53edd2018-10-04 10:55:08 -06001286 if ((s = bc_num_rem(&exp, &two, &temp, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001287
1288 if (BC_NUM_ONE(&temp)) {
1289 if ((s = bc_num_mul(d, &base, &temp, scale))) goto err;
Gavin Howardbe53edd2018-10-04 10:55:08 -06001290 if ((s = bc_num_rem(&temp, ptr_c, d, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001291 }
1292
1293 if ((s = bc_num_div(&exp, &two, &exp, scale))) goto err;
1294 if ((s = bc_num_mul(&base, &base, &temp, scale))) goto err;
Gavin Howardbe53edd2018-10-04 10:55:08 -06001295 if ((s = bc_num_rem(&temp, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001296 }
1297
1298err:
1299 bc_num_free(&temp);
1300temp_err:
Gavin Howardba009802018-09-29 04:41:51 -06001301 bc_num_free(&two);
1302two_err:
1303 bc_num_free(&exp);
1304exp_err:
1305 bc_num_free(&base);
1306base_err:
1307 if (init) bc_num_free(&num2);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001308 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001309 return s;
1310}
Gavin Howarde6e84762018-10-03 11:46:34 -06001311#endif // DC_ENABLED