blob: a1420a4434c97a205db6534d4ade814f3c42eda9 [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 Howard8e2cc692018-02-15 17:39:14 -070024#include <math.h>
Gavin Howard411f7322018-09-26 17:21:19 -060025#include <stdbool.h>
26#include <stdlib.h>
27#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070028
Gavin Howard29493062018-03-20 19:57:37 -060029#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070030#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060031#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070032
Gavin Howard0be26ed2018-08-31 20:21:56 -060033void bc_num_zero(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -060034 if (!n) return;
35 memset(n->num, 0, n->cap * sizeof(char));
36 n->neg = false;
37 n->len = 0;
38 n->rdx = 0;
Gavin Howard0be26ed2018-08-31 20:21:56 -060039}
40
41void bc_num_one(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -060042 if (!n) return;
43 bc_num_zero(n);
44 n->len = 1;
45 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060046}
47
48void bc_num_ten(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -060049 if (!n) return;
50 bc_num_zero(n);
51 n->len = 2;
52 n->num[0] = 0;
53 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060054}
55
Gavin Howard2ea7dc42018-05-22 14:02:02 -060056BcStatus bc_num_subArrays(BcDigit *n1, BcDigit *n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060057 size_t i, j;
58 for (i = 0; !bcg.signe && i < len; ++i) {
59 for (n1[i] -= n2[i], j = 0; !bcg.signe && n1[i + j] < 0;) {
60 n1[i + j++] += 10;
61 n1[i + j] -= 1;
62 }
63 }
64 return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060065}
66
Gavin Howard6a804cf2018-05-17 16:54:12 -060067ssize_t bc_num_compare(BcDigit *n1, BcDigit *n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060068 size_t i;
69 BcDigit c = 0;
70 for (i = len - 1; !bcg.signe && i < len && !(c = n1[i] - n2[i]); --i);
71 return (c < 0 ? -1 : 1) * (ssize_t) (i + 1);
Gavin Howard08bf5292018-03-20 14:59:33 -060072}
73
Gavin Howard6a804cf2018-05-17 16:54:12 -060074ssize_t bc_num_cmp(BcNum *a, BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070075
Gavin Howard63738202018-09-26 15:34:20 -060076 size_t i, min, a_int, b_int, diff;
77 BcDigit *max_num, *min_num;
78 bool a_max;
79 ssize_t cmp, neg = 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070080
Gavin Howard63738202018-09-26 15:34:20 -060081 if (!a) return !b ? 0 : !b->neg * -2 + 1;
82 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 Howard63738202018-09-26 15:34:20 -0600126 BcDigit *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 Howard63738202018-09-26 15:34:20 -0600136 memmove(n->num, ptr, n->len * sizeof(BcDigit));
137 memset(n->num + n->len, 0, sizeof(BcDigit) * (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;
143 BcDigit *ptr;
144 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;
152 memmove(ptr, n->num, sizeof(BcDigit) * n->len);
153 memset(n->num, 0, sizeof(BcDigit) * 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 Howard24434e12018-09-06 15:10:32 -0600193BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700194
Gavin Howard63738202018-09-26 15:34:20 -0600195 BcDigit *ptr, *ptr_a, *ptr_b, *ptr_c;
196 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
197 BcDigit 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);
204 if (sub) c->neg = !c->neg;
205 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;
210 memset(c->num, 0, c->cap * sizeof(BcDigit));
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 Howard3f68df72018-03-22 20:30:27 -0600265BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *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);
278 if (sub) c->neg = !b->neg;
279 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 Howard3f68df72018-03-22 20:30:27 -0600324BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700325
Gavin Howard63738202018-09-26 15:34:20 -0600326 BcStatus s;
327 BcDigit carry;
328 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) {
343 s = bc_num_copy(c, b);
344 if (a->neg) c->neg = !c->neg;
345 }
346 else {
347 s = bc_num_copy(c, a);
348 if (b->neg) c->neg = !c->neg;
349 }
Gavin Howard337fe602018-09-01 15:10:59 -0600350
Gavin Howard63738202018-09-26 15:34:20 -0600351 if (s) return s;
Gavin Howardbe261e72018-09-05 10:11:41 -0600352
Gavin Howard63738202018-09-26 15:34:20 -0600353 return bc_num_retireMul(c, scale);
354 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700355
Gavin Howard63738202018-09-26 15:34:20 -0600356 memset(c->num, 0, sizeof(BcDigit) * c->cap);
357 c->len = carry = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700358
Gavin Howard63738202018-09-26 15:34:20 -0600359 for (i = 0; !bcg.signe && i < b->len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700360
Gavin Howard63738202018-09-26 15:34:20 -0600361 for (j = 0; !bcg.signe && j < a->len; ++j) {
362 c->num[i + j] += a->num[j] * b->num[i] + carry;
363 carry = c->num[i + j] / 10;
364 c->num[i + j] %= 10;
365 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700366
Gavin Howard63738202018-09-26 15:34:20 -0600367 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600368
Gavin Howard63738202018-09-26 15:34:20 -0600369 if (carry) {
370 c->num[i + j] += carry;
371 carry = 0;
372 len = BC_MAX(len, i + j + 1);
373 }
374 else len = BC_MAX(len, i + j);
375 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700376
Gavin Howard63738202018-09-26 15:34:20 -0600377 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600378
Gavin Howard63738202018-09-26 15:34:20 -0600379 c->len = BC_MAX(len, c->rdx);
380 c->neg = !a->neg != !b->neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700381
Gavin Howard63738202018-09-26 15:34:20 -0600382 return bc_num_retireMul(c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700383}
384
Gavin Howard3f68df72018-03-22 20:30:27 -0600385BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700386
Gavin Howard63738202018-09-26 15:34:20 -0600387 BcStatus s;
388 BcDigit *n, *bptr, q;
389 size_t len, end, i;
390 BcNum cp;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700391
Gavin Howard63738202018-09-26 15:34:20 -0600392 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
393 else if (!a->len) {
394 bc_num_zero(c);
395 return BC_STATUS_SUCCESS;
396 }
397 else if (BC_NUM_ONE(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600398 if ((s = bc_num_copy(c, a))) return s;
399 if (b->neg) c->neg = !c->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600400 return bc_num_retireMul(c, scale);
401 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700402
Gavin Howard63738202018-09-26 15:34:20 -0600403 if ((s = bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)))) return s;
404 if ((s = bc_num_copy(&cp, a))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700405
Gavin Howard63738202018-09-26 15:34:20 -0600406 if ((len = b->len) > cp.len) {
407 if ((s = bc_num_expand(&cp, len + 2))) goto err;
408 if ((s = bc_num_extend(&cp, len - cp.len))) goto err;
409 }
Gavin Howard021150b2018-03-10 15:40:42 -0700410
Gavin Howard50651542018-09-29 02:53:11 -0600411 if (b->rdx > cp.rdx && (s = bc_num_extend(&cp, b->rdx - cp.rdx))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600412 cp.rdx -= b->rdx;
Gavin Howard50651542018-09-29 02:53:11 -0600413 if (scale > cp.rdx && (s = bc_num_extend(&cp, scale - cp.rdx))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700414
Gavin Howard63738202018-09-26 15:34:20 -0600415 if (b->rdx == b->len) {
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600416
Gavin Howard63738202018-09-26 15:34:20 -0600417 bool zero = true;
418 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600419
Gavin Howard63738202018-09-26 15:34:20 -0600420 if (i == len && zero) {
421 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
422 goto err;
423 }
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600424
Gavin Howard63738202018-09-26 15:34:20 -0600425 len -= i - 1;
426 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700427
Gavin Howard63738202018-09-26 15:34:20 -0600428 if (cp.cap == cp.len && (s = bc_num_expand(&cp, cp.len + 1))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700429
Gavin Howard63738202018-09-26 15:34:20 -0600430 // We want an extra zero in front to make things simpler.
431 cp.num[cp.len++] = 0;
432 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700433
Gavin Howard63738202018-09-26 15:34:20 -0600434 if ((s = bc_num_expand(c, cp.len))) goto err;
Gavin Howardac7656d2018-03-01 17:18:40 -0700435
Gavin Howard63738202018-09-26 15:34:20 -0600436 bc_num_zero(c);
437 c->rdx = cp.rdx;
438 c->len = cp.len;
439 bptr = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700440
Gavin Howardc39fd492018-10-04 10:07:03 -0600441 for (i = end - 1; !bcg.signe && !s && i < end; --i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700442
Gavin Howard63738202018-09-26 15:34:20 -0600443 n = cp.num + i;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700444
Gavin Howard63738202018-09-26 15:34:20 -0600445 for (q = 0; (!s && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q)
446 s = bc_num_subArrays(n, bptr, len);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700447
Gavin Howard63738202018-09-26 15:34:20 -0600448 c->num[i] = q;
449 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700450
Gavin Howard63738202018-09-26 15:34:20 -0600451 if (s) goto err;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600452
Gavin Howard63738202018-09-26 15:34:20 -0600453 c->neg = !a->neg != !b->neg;
454 s = bc_num_retireMul(c, scale);
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600455
Gavin Howardb651f1a2018-02-28 17:34:18 -0700456err:
Gavin Howard63738202018-09-26 15:34:20 -0600457 bc_num_free(&cp);
458 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700459}
460
Gavin Howardbe53edd2018-10-04 10:55:08 -0600461BcStatus bc_num_alg_rem(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700462
Gavin Howard63738202018-09-26 15:34:20 -0600463 BcStatus s;
464 BcNum c1, c2;
465 size_t len, tscale = BC_MAX(scale + b->rdx, a->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700466
Gavin Howard63738202018-09-26 15:34:20 -0600467 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
Gavin Howard8b254872018-03-14 01:13:35 -0600468
Gavin Howard63738202018-09-26 15:34:20 -0600469 if (!a->len) {
470 bc_num_zero(c);
471 return BC_STATUS_SUCCESS;
472 }
Gavin Howard8b254872018-03-14 01:13:35 -0600473
Gavin Howard3115c012018-10-04 10:53:56 -0600474 len = BC_NUM_MREQ(a, b, tscale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700475
Gavin Howard63738202018-09-26 15:34:20 -0600476 if ((s = bc_num_init(&c1, len))) return s;
477 if ((s = bc_num_init(&c2, len))) goto c2_err;
478 if ((s = bc_num_div(a, b, &c1, scale))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700479
Gavin Howard3115c012018-10-04 10:53:56 -0600480 if (scale) scale = tscale;
481
Gavin Howard63738202018-09-26 15:34:20 -0600482 if ((s = bc_num_mul(&c1, b, &c2, scale))) goto err;
483 if ((s = bc_num_sub(a, &c2, c, scale))) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600484
Gavin Howard63738202018-09-26 15:34:20 -0600485 if (tscale > c->rdx && c->len) s = bc_num_extend(c, tscale - c->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700486
487err:
Gavin Howard63738202018-09-26 15:34:20 -0600488 bc_num_free(&c2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700489c2_err:
Gavin Howard63738202018-09-26 15:34:20 -0600490 bc_num_free(&c1);
491 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700492}
493
Gavin Howard3f68df72018-03-22 20:30:27 -0600494BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700495
Gavin Howard63738202018-09-26 15:34:20 -0600496 BcStatus s;
497 BcNum copy;
498 unsigned long pow;
499 size_t i, powrdx, resrdx;
500 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700501
Gavin Howard63738202018-09-26 15:34:20 -0600502 if (b->rdx) return BC_STATUS_MATH_NON_INTEGER;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700503
Gavin Howard63738202018-09-26 15:34:20 -0600504 if (!b->len) {
505 bc_num_one(c);
506 return BC_STATUS_SUCCESS;
507 }
508 else if (!a->len) {
509 bc_num_zero(c);
510 return BC_STATUS_SUCCESS;
511 }
512 else if (BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700513
Gavin Howard63738202018-09-26 15:34:20 -0600514 if (!b->neg) s = bc_num_copy(c, a);
515 else s = bc_num_inv(a, c, scale);
Gavin Howard5fc40e72018-03-05 12:11:09 -0700516
Gavin Howard63738202018-09-26 15:34:20 -0600517 return s;
518 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700519
Gavin Howard63738202018-09-26 15:34:20 -0600520 neg = b->neg;
521 b->neg = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700522
Gavin Howard63738202018-09-26 15:34:20 -0600523 if ((s = bc_num_ulong(b, &pow))) return s;
524 if ((s = bc_num_init(&copy, a->len))) return s;
525 if ((s = bc_num_copy(&copy, a))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700526
Gavin Howard63738202018-09-26 15:34:20 -0600527 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600528
Gavin Howard63738202018-09-26 15:34:20 -0600529 b->neg = neg;
Gavin Howardb29674f2018-03-22 22:24:58 -0600530
Gavin Howard63738202018-09-26 15:34:20 -0600531 for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) {
532 powrdx <<= 1;
533 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
534 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700535
Gavin Howard63738202018-09-26 15:34:20 -0600536 if ((s = bc_num_copy(c, &copy))) goto err;
537 if (bcg.signe) {
538 s = BC_STATUS_EXEC_SIGNAL;
539 goto err;
540 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700541
Gavin Howardc39fd492018-10-04 10:07:03 -0600542 for (resrdx = powrdx, pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) {
Gavin Howard1d959152018-03-03 23:33:13 -0700543
Gavin Howard63738202018-09-26 15:34:20 -0600544 powrdx <<= 1;
Gavin Howard1d959152018-03-03 23:33:13 -0700545
Gavin Howard63738202018-09-26 15:34:20 -0600546 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
Gavin Howard1d959152018-03-03 23:33:13 -0700547
Gavin Howard63738202018-09-26 15:34:20 -0600548 if (pow & 1) {
549 resrdx += powrdx;
550 if ((s = bc_num_mul(c, &copy, c, resrdx))) goto err;
551 }
552 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700553
Gavin Howard63738202018-09-26 15:34:20 -0600554 if (neg && (s = bc_num_inv(c, c, scale))) goto err;
555 if (bcg.signe) {
556 s = BC_STATUS_EXEC_SIGNAL;
557 goto err;
558 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700559
Gavin Howard63738202018-09-26 15:34:20 -0600560 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700561
Gavin Howard63738202018-09-26 15:34:20 -0600562 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
563 if (zero) bc_num_zero(c);
Gavin Howard1d959152018-03-03 23:33:13 -0700564
Gavin Howard6fbdb292018-02-27 15:44:48 -0700565err:
Gavin Howard63738202018-09-26 15:34:20 -0600566 bc_num_free(&copy);
567 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700568}
569
Gavin Howard63738202018-09-26 15:34:20 -0600570BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600571 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700572{
Gavin Howarda1c44392018-09-27 12:41:15 -0600573 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600574 BcNum num2, *ptr_a, *ptr_b;
575 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700576
Gavin Howard63738202018-09-26 15:34:20 -0600577 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700578
Gavin Howard63738202018-09-26 15:34:20 -0600579 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600580 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600581 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600582 init = true;
583 }
584 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700585
Gavin Howard63738202018-09-26 15:34:20 -0600586 if (c == b) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700587
Gavin Howardba009802018-09-29 04:41:51 -0600588 ptr_b = &num2;
589
590 if (c != a) {
591 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600592 init = true;
593 }
594 }
595 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700596
Gavin Howarda1c44392018-09-27 12:41:15 -0600597 if (init) s = bc_num_init(c, req);
598 else s = bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700599
Gavin Howardba009802018-09-29 04:41:51 -0600600 if (s) return s;
Gavin Howarda1c44392018-09-27 12:41:15 -0600601 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700602
Gavin Howardba009802018-09-29 04:41:51 -0600603 if (init) bc_num_free(&num2);
Gavin Howarda1c44392018-09-27 12:41:15 -0600604 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700605}
606
Gavin Howard3f68df72018-03-22 20:30:27 -0600607bool bc_num_strValid(const char *val, size_t base) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700608
Gavin Howard63738202018-09-26 15:34:20 -0600609 BcDigit b;
610 bool small, radix = false;
611 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700612
Gavin Howard63738202018-09-26 15:34:20 -0600613 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700614
Gavin Howard63738202018-09-26 15:34:20 -0600615 small = base <= 10;
616 b = (BcDigit) (small ? base + '0' : base - 9 + 'A');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700617
Gavin Howard63738202018-09-26 15:34:20 -0600618 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700619
Gavin Howard63738202018-09-26 15:34:20 -0600620 BcDigit c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600621
Gavin Howard63738202018-09-26 15:34:20 -0600622 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700623
Gavin Howard63738202018-09-26 15:34:20 -0600624 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700625
Gavin Howard63738202018-09-26 15:34:20 -0600626 radix = true;
627 continue;
628 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700629
Gavin Howard63738202018-09-26 15:34:20 -0600630 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
631 return false;
632 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700633
Gavin Howard63738202018-09-26 15:34:20 -0600634 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700635}
636
Gavin Howard3f68df72018-03-22 20:30:27 -0600637BcStatus bc_num_parseDecimal(BcNum *n, const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700638
Gavin Howard63738202018-09-26 15:34:20 -0600639 BcStatus s;
640 size_t len, i;
641 const char *ptr;
642 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700643
Gavin Howard63738202018-09-26 15:34:20 -0600644 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700645
Gavin Howard63738202018-09-26 15:34:20 -0600646 val += i;
647 len = strlen(val);
648 bc_num_zero(n);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700649
Gavin Howard63738202018-09-26 15:34:20 -0600650 if (len) {
651 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
652 if ((s = bc_num_expand(n, len))) return s;
653 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600654
Gavin Howard63738202018-09-26 15:34:20 -0600655 if (zero) {
656 memset(n->num, 0, sizeof(BcDigit) * n->cap);
657 n->neg = false;
658 return BC_STATUS_SUCCESS;
659 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700660
Gavin Howard63738202018-09-26 15:34:20 -0600661 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700662
Gavin Howard63738202018-09-26 15:34:20 -0600663 // Explicitly test for NULL here to produce either a 0 or 1.
664 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700665
Gavin Howard63738202018-09-26 15:34:20 -0600666 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
667 n->num[n->len] = val[i] - '0';
Gavin Howard6fbdb292018-02-27 15:44:48 -0700668
Gavin Howard63738202018-09-26 15:34:20 -0600669 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700670}
671
Gavin Howard3f68df72018-03-22 20:30:27 -0600672BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
Gavin Howardede51f02018-03-02 12:30:00 -0700673
Gavin Howard63738202018-09-26 15:34:20 -0600674 BcStatus s;
675 BcNum temp, mult, result;
676 BcDigit c = '\0';
677 bool zero;
678 unsigned long v;
679 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700680
Gavin Howard63738202018-09-26 15:34:20 -0600681 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700682
Gavin Howard63738202018-09-26 15:34:20 -0600683 for (zero = true, i = 0; zero && i < len; ++i)
684 zero = (val[i] == '.' || val[i] == '0');
685 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700686
Gavin Howard63738202018-09-26 15:34:20 -0600687 if ((s = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return s;
688 if ((s = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700689
Gavin Howard63738202018-09-26 15:34:20 -0600690 for (i = 0; i < len && (c = val[i]) != '.'; ++i) {
Gavin Howardede51f02018-03-02 12:30:00 -0700691
Gavin Howard63738202018-09-26 15:34:20 -0600692 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700693
Gavin Howard63738202018-09-26 15:34:20 -0600694 if ((s = bc_num_mul(n, base, &mult, 0))) goto int_err;
695 if ((s = bc_num_ulong2num(&temp, v))) goto int_err;
696 if ((s = bc_num_add(&mult, &temp, n, 0))) goto int_err;
697 }
Gavin Howardede51f02018-03-02 12:30:00 -0700698
Gavin Howard63738202018-09-26 15:34:20 -0600699 if (i == len && !(c = val[i])) goto int_err;
700 assert(c == '.');
701 if ((s = bc_num_init(&result, base->len))) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700702
Gavin Howard63738202018-09-26 15:34:20 -0600703 bc_num_zero(&result);
704 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700705
Gavin Howard63738202018-09-26 15:34:20 -0600706 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howardede51f02018-03-02 12:30:00 -0700707
Gavin Howard63738202018-09-26 15:34:20 -0600708 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardede51f02018-03-02 12:30:00 -0700709
Gavin Howard63738202018-09-26 15:34:20 -0600710 if ((s = bc_num_mul(&result, base, &result, 0))) goto err;
711 if ((s = bc_num_ulong2num(&temp, v))) goto err;
712 if ((s = bc_num_add(&result, &temp, &result, 0))) goto err;
713 if ((s = bc_num_mul(&mult, base, &mult, 0))) goto err;
714 }
Gavin Howardede51f02018-03-02 12:30:00 -0700715
Gavin Howard63738202018-09-26 15:34:20 -0600716 if ((s = bc_num_div(&result, &mult, &result, digits))) goto err;
717 if ((s = bc_num_add(n, &result, n, digits))) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600718
Gavin Howard63738202018-09-26 15:34:20 -0600719 if (n->len) {
720 if (n->rdx < digits && n->len) s = bc_num_extend(n, digits - n->rdx);
721 }
722 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700723
724err:
Gavin Howard63738202018-09-26 15:34:20 -0600725 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700726int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600727 bc_num_free(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700728mult_err:
Gavin Howard63738202018-09-26 15:34:20 -0600729 bc_num_free(&temp);
730 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700731}
732
Gavin Howard80977b22018-09-06 14:29:13 -0600733BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) {
Gavin Howard63738202018-09-26 15:34:20 -0600734 if (*nchars == line_len - 1) {
735 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
736 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
737 *nchars = 0;
738 }
Gavin Howard63738202018-09-26 15:34:20 -0600739 return BC_STATUS_SUCCESS;
Gavin Howard80977b22018-09-06 14:29:13 -0600740}
741
742BcStatus bc_num_printDigits(size_t num, size_t width, bool radix,
743 size_t *nchars, size_t line_len)
744{
Gavin Howard63738202018-09-26 15:34:20 -0600745 BcStatus s;
Gavin Howardc39fd492018-10-04 10:07:03 -0600746 size_t exp, pow, div;
Gavin Howard80977b22018-09-06 14:29:13 -0600747
Gavin Howard63738202018-09-26 15:34:20 -0600748 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard1a5cac22018-09-25 12:59:21 -0600749
Gavin Howard63738202018-09-26 15:34:20 -0600750 if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR;
751 ++(*nchars);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600752
Gavin Howard63738202018-09-26 15:34:20 -0600753 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard80977b22018-09-06 14:29:13 -0600754
Gavin Howard63738202018-09-26 15:34:20 -0600755 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600756
Gavin Howard63738202018-09-26 15:34:20 -0600757 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600758
Gavin Howard63738202018-09-26 15:34:20 -0600759 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600760
Gavin Howard63738202018-09-26 15:34:20 -0600761 div = num / pow;
762 num -= div * pow;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600763
Gavin Howard63738202018-09-26 15:34:20 -0600764 if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
765 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700766
Gavin Howard63738202018-09-26 15:34:20 -0600767 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700768}
Gavin Howardfe679f02018-02-14 15:50:09 -0700769
Gavin Howard902a16c2018-05-16 01:22:53 -0600770BcStatus bc_num_printHex(size_t num, size_t width, bool radix,
Gavin Howard0011a3a2018-03-29 23:11:32 -0600771 size_t *nchars, size_t line_len)
Gavin Howard2682a1f2018-03-03 09:09:13 -0700772{
Gavin Howard63738202018-09-26 15:34:20 -0600773 BcStatus s;
Gavin Howard80977b22018-09-06 14:29:13 -0600774
Gavin Howard63738202018-09-26 15:34:20 -0600775 assert(width == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600776
Gavin Howard63738202018-09-26 15:34:20 -0600777 if (radix) {
778 if ((s = bc_num_printNewline(nchars, line_len))) return s;
779 if (putchar('.') == EOF) return BC_STATUS_IO_ERR;
780 *nchars += 1;
781 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700782
Gavin Howard63738202018-09-26 15:34:20 -0600783 if ((s = bc_num_printNewline(nchars, line_len))) return s;
784 if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR;
785 *nchars = *nchars + width;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700786
Gavin Howard63738202018-09-26 15:34:20 -0600787 return BC_STATUS_SUCCESS;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700788}
789
Gavin Howard5f46ee32018-09-25 13:02:43 -0600790BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700791
Gavin Howardc39fd492018-10-04 10:07:03 -0600792 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600793 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700794
Gavin Howard63738202018-09-26 15:34:20 -0600795 if (n->neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
796 (*nchars) += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700797
Gavin Howard63738202018-09-26 15:34:20 -0600798 for (i = n->len - 1; !s && i < n->len; --i)
799 s = bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700800
Gavin Howard63738202018-09-26 15:34:20 -0600801 return s;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700802}
803
Gavin Howard0011a3a2018-03-29 23:11:32 -0600804BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
805 size_t *nchars, size_t line_len)
Gavin Howardbc7cae82018-03-14 13:43:04 -0600806{
Gavin Howard63738202018-09-26 15:34:20 -0600807 BcStatus s;
808 BcVec stack;
809 BcNum intp, fracp, digit, frac_len;
810 size_t width, i;
811 BcNumDigitOp print;
812 unsigned long dig, *ptr;
813 bool radix, neg = n->neg;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700814
Gavin Howard63738202018-09-26 15:34:20 -0600815 if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
816 (*nchars) += neg;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700817
Gavin Howard63738202018-09-26 15:34:20 -0600818 n->neg = false;
Gavin Howard7f45f1a2018-09-24 14:50:55 -0600819
Gavin Howard63738202018-09-26 15:34:20 -0600820 if (base_t <= BC_NUM_MAX_IBASE) {
821 width = 1;
822 print = bc_num_printHex;
823 }
824 else {
825 width = (size_t) floor(log10((double) (base_t - 1)) + 1.0);
826 print = bc_num_printDigits;
827 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700828
Gavin Howard63738202018-09-26 15:34:20 -0600829 if ((s = bc_vec_init(&stack, sizeof(long), NULL))) goto stack_err;
830 if ((s = bc_num_init(&intp, n->len))) goto int_err;
831 if ((s = bc_num_init(&fracp, n->rdx))) goto frac_err;
832 if ((s = bc_num_init(&digit, width))) goto digit_err;
833 if ((s = bc_num_init(&frac_len, BC_NUM_INT(n)))) goto frac_len_err;
834 if ((s = bc_num_copy(&intp, n))) goto err;
835 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -0600836
Gavin Howard63738202018-09-26 15:34:20 -0600837 bc_num_truncate(&intp, intp.rdx);
838 if ((s = bc_num_sub(n, &intp, &fracp, 0))) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700839
Gavin Howard63738202018-09-26 15:34:20 -0600840 while (intp.len) {
Gavin Howardbe53edd2018-10-04 10:55:08 -0600841 if ((s = bc_num_rem(&intp, base, &digit, 0))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600842 if ((s = bc_num_ulong(&digit, &dig))) goto err;
Gavin Howard90198862018-09-29 03:37:47 -0600843 if ((s = bc_vec_push(&stack, &dig))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600844 if ((s = bc_num_div(&intp, base, &intp, 0))) goto err;
845 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700846
Gavin Howard63738202018-09-26 15:34:20 -0600847 for (i = 0; i < stack.len; ++i) {
848 ptr = bc_vec_item_rev(&stack, i);
849 assert(ptr);
850 if ((s = print(*ptr, width, false, nchars, line_len))) goto err;
851 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700852
Gavin Howard63738202018-09-26 15:34:20 -0600853 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700854
Gavin Howard63738202018-09-26 15:34:20 -0600855 for (radix = true; frac_len.len <= n->rdx; radix = false) {
856 if ((s = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err;
857 if ((s = bc_num_ulong(&fracp, &dig))) goto err;
858 if ((s = bc_num_ulong2num(&intp, dig))) goto err;
859 if ((s = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err;
860 if ((s = print(dig, width, radix, nchars, line_len))) goto err;
861 if ((s = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err;
862 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700863
864err:
Gavin Howard63738202018-09-26 15:34:20 -0600865 bc_num_free(&frac_len);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700866frac_len_err:
Gavin Howard63738202018-09-26 15:34:20 -0600867 bc_num_free(&digit);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700868digit_err:
Gavin Howard63738202018-09-26 15:34:20 -0600869 bc_num_free(&fracp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700870frac_err:
Gavin Howard63738202018-09-26 15:34:20 -0600871 bc_num_free(&intp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700872int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600873 bc_vec_free(&stack);
Gavin Howard7f45f1a2018-09-24 14:50:55 -0600874stack_err:
Gavin Howard63738202018-09-26 15:34:20 -0600875 n->neg = neg;
876 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700877}
878
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700879BcStatus bc_num_init(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700880
Gavin Howard63738202018-09-26 15:34:20 -0600881 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -0700882
Gavin Howard63738202018-09-26 15:34:20 -0600883 request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE;
Gavin Howardc39fd492018-10-04 10:07:03 -0600884 memset(n, 0, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600885 if (!(n->num = malloc(request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -0700886
Gavin Howard63738202018-09-26 15:34:20 -0600887 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700888
Gavin Howard63738202018-09-26 15:34:20 -0600889 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700890}
891
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700892BcStatus bc_num_expand(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700893
Gavin Howard63738202018-09-26 15:34:20 -0600894 BcDigit *temp;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600895
Gavin Howard63738202018-09-26 15:34:20 -0600896 assert(n && request);
Gavin Howardb5c77212018-02-14 17:12:34 -0700897
Gavin Howard63738202018-09-26 15:34:20 -0600898 if (request <= n->cap) return BC_STATUS_SUCCESS;
899 if (!(temp = realloc(n->num, request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -0700900
Gavin Howard63738202018-09-26 15:34:20 -0600901 memset(temp + n->cap, 0, sizeof(char) * (request - n->cap));
902 n->num = temp;
903 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700904
Gavin Howard63738202018-09-26 15:34:20 -0600905 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700906}
907
Gavin Howarded392aa2018-02-27 13:09:26 -0700908void bc_num_free(void *num) {
Gavin Howard63738202018-09-26 15:34:20 -0600909 BcNum *n = (BcNum*) num;
Gavin Howarde1129772018-10-02 14:50:45 -0600910 assert(n && n->num);
911 free(n->num);
Gavin Howardb5c77212018-02-14 17:12:34 -0700912}
913
Gavin Howard9c4358c2018-03-22 20:11:28 -0600914BcStatus bc_num_copy(BcNum *d, BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -0700915
Gavin Howard63738202018-09-26 15:34:20 -0600916 BcStatus status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700917
Gavin Howard63738202018-09-26 15:34:20 -0600918 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -0700919
Gavin Howard63738202018-09-26 15:34:20 -0600920 if (d == s) return BC_STATUS_SUCCESS;
921 if ((status = bc_num_expand(d, s->cap))) return status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700922
Gavin Howard63738202018-09-26 15:34:20 -0600923 d->len = s->len;
924 d->neg = s->neg;
925 d->rdx = s->rdx;
Gavin Howard5a049c42018-02-15 11:24:11 -0700926
Gavin Howard63738202018-09-26 15:34:20 -0600927 memcpy(d->num, s->num, sizeof(BcDigit) * d->len);
928 memset(d->num + d->len, 0, sizeof(BcDigit) * (d->cap - d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -0700929
Gavin Howard63738202018-09-26 15:34:20 -0600930 return BC_STATUS_SUCCESS;
Gavin Howard5a049c42018-02-15 11:24:11 -0700931}
932
Gavin Howard5cea43b2018-03-02 11:32:11 -0700933BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
Gavin Howard025d04d2018-02-20 13:53:28 -0700934
Gavin Howard63738202018-09-26 15:34:20 -0600935 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700936
Gavin Howard63738202018-09-26 15:34:20 -0600937 assert(n && val && base);
938 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_NUM_MAX_IBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700939
Gavin Howard63738202018-09-26 15:34:20 -0600940 if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700941
Gavin Howard63738202018-09-26 15:34:20 -0600942 if (base_t == 10) s = bc_num_parseDecimal(n, val);
943 else s = bc_num_parseBase(n, val, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700944
Gavin Howard63738202018-09-26 15:34:20 -0600945 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700946}
947
Gavin Howard0011a3a2018-03-29 23:11:32 -0600948BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
949 size_t *nchars, size_t line_len)
Gavin Howard152f3e82018-03-07 12:33:15 -0700950{
Gavin Howardc39fd492018-10-04 10:07:03 -0600951 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700952
Gavin Howard63738202018-09-26 15:34:20 -0600953 assert(n && base && nchars);
954 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700955
Gavin Howard63738202018-09-26 15:34:20 -0600956 if (*nchars >= line_len) {
957 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
958 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
959 *nchars = 0;
960 }
Gavin Howardbc7cae82018-03-14 13:43:04 -0600961
Gavin Howard63738202018-09-26 15:34:20 -0600962 if (!n->len) {
963 if (putchar('0') == EOF) return BC_STATUS_IO_ERR;
964 ++(*nchars);
Gavin Howard63738202018-09-26 15:34:20 -0600965 }
966 else if (base_t == 10) s = bc_num_printDecimal(n, nchars, line_len);
967 else s = bc_num_printBase(n, base, base_t, nchars, line_len);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700968
Gavin Howard63738202018-09-26 15:34:20 -0600969 if (s) return s;
Gavin Howard152f3e82018-03-07 12:33:15 -0700970
Gavin Howard63738202018-09-26 15:34:20 -0600971 if (newline) {
972 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
973 *nchars = 0;
974 }
Gavin Howard152f3e82018-03-07 12:33:15 -0700975
Gavin Howard63738202018-09-26 15:34:20 -0600976 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700977}
978
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700979BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700980
Gavin Howard63738202018-09-26 15:34:20 -0600981 size_t i;
982 unsigned long pow;
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700983
Gavin Howard63738202018-09-26 15:34:20 -0600984 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700985
Gavin Howard63738202018-09-26 15:34:20 -0600986 if (n->neg) return BC_STATUS_MATH_NEGATIVE;
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700987
Gavin Howard63738202018-09-26 15:34:20 -0600988 for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700989
Gavin Howard63738202018-09-26 15:34:20 -0600990 unsigned long prev = *result, powprev = pow;
991 *result += ((unsigned long) n->num[i]) * pow;
992 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700993
Gavin Howard63738202018-09-26 15:34:20 -0600994 if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW;
995 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700996
Gavin Howard63738202018-09-26 15:34:20 -0600997 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700998}
999
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001000BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001001
Gavin Howard63738202018-09-26 15:34:20 -06001002 BcStatus s;
1003 size_t len, i;
1004 BcDigit *ptr;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001005
Gavin Howard63738202018-09-26 15:34:20 -06001006 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001007
Gavin Howard63738202018-09-26 15:34:20 -06001008 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001009
Gavin Howard63738202018-09-26 15:34:20 -06001010 if (!val) {
1011 memset(n->num, 0, sizeof(char) * n->cap);
1012 return BC_STATUS_SUCCESS;
1013 }
Gavin Howard8e2cc692018-02-15 17:39:14 -07001014
Gavin Howard63738202018-09-26 15:34:20 -06001015 len = (size_t) ceil(log10(((double) ULONG_MAX) + 1.0));
Gavin Howard8e2cc692018-02-15 17:39:14 -07001016
Gavin Howard63738202018-09-26 15:34:20 -06001017 if ((s = bc_num_expand(n, len))) return s;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001018
Gavin Howardc39fd492018-10-04 10:07:03 -06001019 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10)
Gavin Howard63738202018-09-26 15:34:20 -06001020 ptr[i] = (char) (val % 10);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001021
Gavin Howard63738202018-09-26 15:34:20 -06001022 return BC_STATUS_SUCCESS;
Gavin Howard025d04d2018-02-20 13:53:28 -07001023}
1024
Gavin Howard12fe7812018-09-29 03:45:18 -06001025BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001026 (void) scale;
1027 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s;
Gavin Howard12fe7812018-09-29 03:45:18 -06001028 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001029}
1030
Gavin Howard12fe7812018-09-29 03:45:18 -06001031BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001032 (void) scale;
1033 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a;
Gavin Howard12fe7812018-09-29 03:45:18 -06001034 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001035}
1036
Gavin Howard12fe7812018-09-29 03:45:18 -06001037BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001038 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001039 return bc_num_binary(a, b, c, scale, bc_num_alg_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001040}
1041
Gavin Howard12fe7812018-09-29 03:45:18 -06001042BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001043 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001044 return bc_num_binary(a, b, c, scale, bc_num_alg_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001045}
1046
Gavin Howardbe53edd2018-10-04 10:55:08 -06001047BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001048 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howardbe53edd2018-10-04 10:55:08 -06001049 return bc_num_binary(a, b, c, scale, bc_num_alg_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001050}
1051
Gavin Howard12fe7812018-09-29 03:45:18 -06001052BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
1053 return bc_num_binary(a, b, c, scale, bc_num_alg_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001054}
1055
Gavin Howard4538c902018-08-29 14:31:40 -06001056BcStatus bc_num_sqrt(BcNum *a, BcNum *res, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001057
Gavin Howard63738202018-09-26 15:34:20 -06001058 BcStatus s;
1059 BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp;
1060 size_t pow, len, digits, resrdx, req;
1061 ssize_t cmp;
Gavin Howard954276b2018-05-16 01:43:58 -06001062
Gavin Howard63738202018-09-26 15:34:20 -06001063 assert(a && res);
Gavin Howard954276b2018-05-16 01:43:58 -06001064
Gavin Howard63738202018-09-26 15:34:20 -06001065 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a)+ 1) >> 1) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001066
Gavin Howard63738202018-09-26 15:34:20 -06001067 if (res == a) {
1068 memcpy(&a2, res, sizeof(BcNum));
1069 ptr_a = &a2;
1070 s = bc_num_init(res, req);
1071 }
1072 else {
1073 ptr_a = a;
1074 s = bc_num_expand(res, req);
1075 }
Gavin Howard954276b2018-05-16 01:43:58 -06001076
Gavin Howard63738202018-09-26 15:34:20 -06001077 if (s) goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001078
Gavin Howard63738202018-09-26 15:34:20 -06001079 if (!ptr_a->len) {
1080 bc_num_zero(res);
1081 goto init_err;
1082 }
1083 else if (ptr_a->neg) {
1084 s = BC_STATUS_MATH_NEG_SQRT;
1085 goto init_err;
1086 }
1087 else if (BC_NUM_ONE(a)) {
1088 bc_num_one(res);
1089 s = bc_num_extend(res, scale);
1090 goto init_err;
1091 }
Gavin Howard954276b2018-05-16 01:43:58 -06001092
Gavin Howard63738202018-09-26 15:34:20 -06001093 memset(res->num, 0, res->cap * sizeof(BcDigit));
1094 len = ptr_a->len;
Gavin Howard954276b2018-05-16 01:43:58 -06001095
Gavin Howard63738202018-09-26 15:34:20 -06001096 scale = BC_MAX(scale, ptr_a->rdx) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001097
Gavin Howard63738202018-09-26 15:34:20 -06001098 if ((s = bc_num_init(&num1, len))) goto init_err;
1099 if ((s = bc_num_init(&num2, num1.len))) goto num2_err;
1100 if ((s = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001101
Gavin Howard63738202018-09-26 15:34:20 -06001102 bc_num_one(&half);
1103 half.num[0] = 5;
1104 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001105
Gavin Howard63738202018-09-26 15:34:20 -06001106 len += scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001107
Gavin Howard63738202018-09-26 15:34:20 -06001108 if ((s = bc_num_init(&f, len))) goto f_err;
1109 if ((s = bc_num_init(&fprime, len + scale))) goto fprime_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001110
Gavin Howard63738202018-09-26 15:34:20 -06001111 x0 = &num1;
1112 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001113
Gavin Howard63738202018-09-26 15:34:20 -06001114 bc_num_one(x0);
Gavin Howard954276b2018-05-16 01:43:58 -06001115
Gavin Howardc39fd492018-10-04 10:07:03 -06001116 if ((pow = BC_NUM_INT(ptr_a))) {
Gavin Howard954276b2018-05-16 01:43:58 -06001117
Gavin Howardc39fd492018-10-04 10:07:03 -06001118 if (pow & 1) x0->num[0] = 2;
1119 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001120
Gavin Howardc39fd492018-10-04 10:07:03 -06001121 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001122
Gavin Howard63738202018-09-26 15:34:20 -06001123 if ((s = bc_num_extend(x0, pow))) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06001124
Gavin Howard63738202018-09-26 15:34:20 -06001125 // Make sure to move the radix back.
1126 x0->rdx -= pow;
1127 }
Gavin Howard954276b2018-05-16 01:43:58 -06001128
Gavin Howard63738202018-09-26 15:34:20 -06001129 cmp = 1;
1130 x0->rdx = digits = 0;
1131 resrdx = scale + 1;
1132 len = BC_NUM_INT(x0) + resrdx;
Gavin Howard954276b2018-05-16 01:43:58 -06001133
Gavin Howard63738202018-09-26 15:34:20 -06001134 while (!bcg.signe && cmp && digits <= len) {
Gavin Howard954276b2018-05-16 01:43:58 -06001135
Gavin Howard63738202018-09-26 15:34:20 -06001136 if ((s = bc_num_div(a, x0, &f, resrdx))) goto err;
1137 if ((s = bc_num_add(x0, &f, &fprime, resrdx))) goto err;
1138 if ((s = bc_num_mul(&fprime, &half, x1, resrdx))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001139
Gavin Howard63738202018-09-26 15:34:20 -06001140 cmp = bc_num_cmp(x1, x0);
1141 digits = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001142
Gavin Howard63738202018-09-26 15:34:20 -06001143 temp = x0;
1144 x0 = x1;
1145 x1 = temp;
1146 }
Gavin Howard954276b2018-05-16 01:43:58 -06001147
Gavin Howard63738202018-09-26 15:34:20 -06001148 if (bcg.signe) {
1149 s = BC_STATUS_EXEC_SIGNAL;
1150 goto err;
1151 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001152
Gavin Howard63738202018-09-26 15:34:20 -06001153 if ((s = bc_num_copy(res, x0))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001154
Gavin Howard63738202018-09-26 15:34:20 -06001155 if (res->rdx > --scale) bc_num_truncate(res, res->rdx - scale);
1156 else if (res->rdx < scale) s = bc_num_extend(res, scale - res->rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06001157
1158err:
Gavin Howard63738202018-09-26 15:34:20 -06001159 bc_num_free(&fprime);
Gavin Howard954276b2018-05-16 01:43:58 -06001160fprime_err:
Gavin Howard63738202018-09-26 15:34:20 -06001161 bc_num_free(&f);
Gavin Howard954276b2018-05-16 01:43:58 -06001162f_err:
Gavin Howard63738202018-09-26 15:34:20 -06001163 bc_num_free(&half);
Gavin Howard954276b2018-05-16 01:43:58 -06001164two_err:
Gavin Howard63738202018-09-26 15:34:20 -06001165 bc_num_free(&num2);
Gavin Howard954276b2018-05-16 01:43:58 -06001166num2_err:
Gavin Howard63738202018-09-26 15:34:20 -06001167 bc_num_free(&num1);
Gavin Howard954276b2018-05-16 01:43:58 -06001168init_err:
Gavin Howard63738202018-09-26 15:34:20 -06001169 if (res == a) bc_num_free(&a2);
1170 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001171}
Gavin Howardba009802018-09-29 04:41:51 -06001172
Gavin Howarde6e84762018-10-03 11:46:34 -06001173#ifdef DC_ENABLED
Gavin Howardba009802018-09-29 04:41:51 -06001174BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1175
1176 BcStatus s;
1177 BcNum num2, *ptr_a, *ptr_b, *ptr_c, base, exp, two, zero, temp;
1178 bool init = false;
1179
1180 assert(a && b && c && d);
1181
1182 scale = 0;
1183
1184 if (d == a) {
1185 memcpy(&num2, d, sizeof(BcNum));
1186 ptr_a = &num2;
1187 init = true;
1188 }
1189 else ptr_a = a;
1190
1191 if (d == b) {
1192
1193 ptr_b = &num2;
1194
1195 if (d != a) {
1196 memcpy(ptr_b, d, sizeof(BcNum));
1197 init = true;
1198 }
1199 }
1200 else ptr_b = b;
1201
1202 if (d == c) {
1203
1204 ptr_c = &num2;
1205
1206 if (d != a && d != b) {
1207 memcpy(ptr_c, d, sizeof(BcNum));
1208 init = true;
1209 }
1210 }
1211 else ptr_c = c;
1212
1213 if (init) s = bc_num_init(d, ptr_c->len);
1214 else s = bc_num_expand(d, ptr_c->len);
1215
1216 if (s) return s;
1217
1218 if (!ptr_c->len) {
1219 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
1220 goto base_err;
1221 }
1222
1223 if (ptr_b->rdx || ptr_c->rdx || ptr_a->rdx) {
1224 s = BC_STATUS_MATH_NON_INTEGER;
1225 goto base_err;
1226 }
1227
1228 if (ptr_b->neg) {
1229 s = BC_STATUS_MATH_NEGATIVE;
1230 goto base_err;
1231 }
1232
1233 if (!ptr_c->len) {
1234 bc_num_zero(d);
1235 goto base_err;
1236 }
1237
1238 if ((s = bc_num_init(&base, ptr_c->len))) goto base_err;
1239 if ((s = bc_num_init(&exp, ptr_b->len))) goto exp_err;
1240 if ((s = bc_num_init(&two, BC_NUM_DEF_SIZE))) goto two_err;
1241 if ((s = bc_num_init(&zero, BC_NUM_DEF_SIZE))) goto zero_err;
1242 if ((s = bc_num_init(&temp, ptr_b->len))) goto temp_err;
1243
1244 bc_num_one(&two);
1245
1246 if ((s = bc_num_sub(ptr_c, &two, &exp, scale))) goto err;
1247 if ((s = bc_num_mul(&exp, &exp, &base, scale))) goto err;
1248 if (bc_num_cmp(&exp, ptr_a) > 0) {
1249 s = BC_STATUS_MATH_BASE_OVERFLOW;
1250 goto err;
1251 }
1252
1253 two.num[0] = 2;
1254 bc_num_one(d);
1255
Gavin Howardbe53edd2018-10-04 10:55:08 -06001256 if ((s = bc_num_rem(ptr_a, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001257 if ((s = bc_num_copy(&exp, ptr_b))) goto err;
1258
Gavin Howardaf905602018-09-29 18:56:37 -06001259 while (exp.len) {
Gavin Howardba009802018-09-29 04:41:51 -06001260
Gavin Howardbe53edd2018-10-04 10:55:08 -06001261 if ((s = bc_num_rem(&exp, &two, &temp, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001262
1263 if (BC_NUM_ONE(&temp)) {
1264 if ((s = bc_num_mul(d, &base, &temp, scale))) goto err;
Gavin Howardbe53edd2018-10-04 10:55:08 -06001265 if ((s = bc_num_rem(&temp, ptr_c, d, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001266 }
1267
1268 if ((s = bc_num_div(&exp, &two, &exp, scale))) goto err;
1269 if ((s = bc_num_mul(&base, &base, &temp, scale))) goto err;
Gavin Howardbe53edd2018-10-04 10:55:08 -06001270 if ((s = bc_num_rem(&temp, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001271 }
1272
1273err:
1274 bc_num_free(&temp);
1275temp_err:
1276 bc_num_free(&zero);
1277zero_err:
1278 bc_num_free(&two);
1279two_err:
1280 bc_num_free(&exp);
1281exp_err:
1282 bc_num_free(&base);
1283base_err:
1284 if (init) bc_num_free(&num2);
1285 return s;
1286}
Gavin Howarde6e84762018-10-03 11:46:34 -06001287#endif // DC_ENABLED