blob: f8179037874322861218d6ead355f59457308877 [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) {
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 Howard63738202018-09-26 15:34:20 -0600354 memset(c->num, 0, sizeof(BcDigit) * c->cap);
355 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 Howard3f68df72018-03-22 20:30:27 -0600383BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700384
Gavin Howard63738202018-09-26 15:34:20 -0600385 BcStatus s;
386 BcDigit *n, *bptr, q;
387 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 Howardbe53edd2018-10-04 10:55:08 -0600459BcStatus bc_num_alg_rem(BcNum *a, BcNum *b, BcNum *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 Howard3f68df72018-03-22 20:30:27 -0600490BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *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 Howardba009802018-09-29 04:41:51 -0600598 if (init) bc_num_free(&num2);
Gavin Howarda1c44392018-09-27 12:41:15 -0600599 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700600}
601
Gavin Howard3f68df72018-03-22 20:30:27 -0600602bool bc_num_strValid(const char *val, size_t base) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700603
Gavin Howard63738202018-09-26 15:34:20 -0600604 BcDigit b;
605 bool small, radix = false;
606 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700607
Gavin Howard63738202018-09-26 15:34:20 -0600608 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700609
Gavin Howard63738202018-09-26 15:34:20 -0600610 small = base <= 10;
611 b = (BcDigit) (small ? base + '0' : base - 9 + 'A');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700612
Gavin Howard63738202018-09-26 15:34:20 -0600613 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700614
Gavin Howard63738202018-09-26 15:34:20 -0600615 BcDigit c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600616
Gavin Howard63738202018-09-26 15:34:20 -0600617 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700618
Gavin Howard63738202018-09-26 15:34:20 -0600619 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700620
Gavin Howard63738202018-09-26 15:34:20 -0600621 radix = true;
622 continue;
623 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700624
Gavin Howard63738202018-09-26 15:34:20 -0600625 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
626 return false;
627 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700628
Gavin Howard63738202018-09-26 15:34:20 -0600629 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700630}
631
Gavin Howard3f68df72018-03-22 20:30:27 -0600632BcStatus bc_num_parseDecimal(BcNum *n, const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700633
Gavin Howard63738202018-09-26 15:34:20 -0600634 BcStatus s;
635 size_t len, i;
636 const char *ptr;
637 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700638
Gavin Howard63738202018-09-26 15:34:20 -0600639 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700640
Gavin Howard63738202018-09-26 15:34:20 -0600641 val += i;
642 len = strlen(val);
643 bc_num_zero(n);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700644
Gavin Howard63738202018-09-26 15:34:20 -0600645 if (len) {
646 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
647 if ((s = bc_num_expand(n, len))) return s;
648 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600649
Gavin Howard63738202018-09-26 15:34:20 -0600650 if (zero) {
651 memset(n->num, 0, sizeof(BcDigit) * n->cap);
652 n->neg = false;
653 return BC_STATUS_SUCCESS;
654 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700655
Gavin Howard63738202018-09-26 15:34:20 -0600656 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700657
Gavin Howard63738202018-09-26 15:34:20 -0600658 // Explicitly test for NULL here to produce either a 0 or 1.
659 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700660
Gavin Howard63738202018-09-26 15:34:20 -0600661 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
662 n->num[n->len] = val[i] - '0';
Gavin Howard6fbdb292018-02-27 15:44:48 -0700663
Gavin Howard63738202018-09-26 15:34:20 -0600664 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700665}
666
Gavin Howard3f68df72018-03-22 20:30:27 -0600667BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
Gavin Howardede51f02018-03-02 12:30:00 -0700668
Gavin Howard63738202018-09-26 15:34:20 -0600669 BcStatus s;
670 BcNum temp, mult, result;
671 BcDigit c = '\0';
672 bool zero;
673 unsigned long v;
674 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700675
Gavin Howard63738202018-09-26 15:34:20 -0600676 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700677
Gavin Howard63738202018-09-26 15:34:20 -0600678 for (zero = true, i = 0; zero && i < len; ++i)
679 zero = (val[i] == '.' || val[i] == '0');
680 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700681
Gavin Howard63738202018-09-26 15:34:20 -0600682 if ((s = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return s;
683 if ((s = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700684
Gavin Howard63738202018-09-26 15:34:20 -0600685 for (i = 0; i < len && (c = val[i]) != '.'; ++i) {
Gavin Howardede51f02018-03-02 12:30:00 -0700686
Gavin Howard63738202018-09-26 15:34:20 -0600687 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700688
Gavin Howard63738202018-09-26 15:34:20 -0600689 if ((s = bc_num_mul(n, base, &mult, 0))) goto int_err;
690 if ((s = bc_num_ulong2num(&temp, v))) goto int_err;
691 if ((s = bc_num_add(&mult, &temp, n, 0))) goto int_err;
692 }
Gavin Howardede51f02018-03-02 12:30:00 -0700693
Gavin Howard63738202018-09-26 15:34:20 -0600694 if (i == len && !(c = val[i])) goto int_err;
695 assert(c == '.');
696 if ((s = bc_num_init(&result, base->len))) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700697
Gavin Howard63738202018-09-26 15:34:20 -0600698 bc_num_zero(&result);
699 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700700
Gavin Howard63738202018-09-26 15:34:20 -0600701 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howardede51f02018-03-02 12:30:00 -0700702
Gavin Howard63738202018-09-26 15:34:20 -0600703 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardede51f02018-03-02 12:30:00 -0700704
Gavin Howard63738202018-09-26 15:34:20 -0600705 if ((s = bc_num_mul(&result, base, &result, 0))) goto err;
706 if ((s = bc_num_ulong2num(&temp, v))) goto err;
707 if ((s = bc_num_add(&result, &temp, &result, 0))) goto err;
708 if ((s = bc_num_mul(&mult, base, &mult, 0))) goto err;
709 }
Gavin Howardede51f02018-03-02 12:30:00 -0700710
Gavin Howard63738202018-09-26 15:34:20 -0600711 if ((s = bc_num_div(&result, &mult, &result, digits))) goto err;
712 if ((s = bc_num_add(n, &result, n, digits))) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600713
Gavin Howard63738202018-09-26 15:34:20 -0600714 if (n->len) {
715 if (n->rdx < digits && n->len) s = bc_num_extend(n, digits - n->rdx);
716 }
717 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700718
719err:
Gavin Howard63738202018-09-26 15:34:20 -0600720 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700721int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600722 bc_num_free(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700723mult_err:
Gavin Howard63738202018-09-26 15:34:20 -0600724 bc_num_free(&temp);
725 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700726}
727
Gavin Howard80977b22018-09-06 14:29:13 -0600728BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) {
Gavin Howard63738202018-09-26 15:34:20 -0600729 if (*nchars == line_len - 1) {
730 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
731 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
732 *nchars = 0;
733 }
Gavin Howard63738202018-09-26 15:34:20 -0600734 return BC_STATUS_SUCCESS;
Gavin Howard80977b22018-09-06 14:29:13 -0600735}
736
737BcStatus bc_num_printDigits(size_t num, size_t width, bool radix,
738 size_t *nchars, size_t line_len)
739{
Gavin Howard63738202018-09-26 15:34:20 -0600740 BcStatus s;
Gavin Howardc39fd492018-10-04 10:07:03 -0600741 size_t exp, pow, div;
Gavin Howard80977b22018-09-06 14:29:13 -0600742
Gavin Howard63738202018-09-26 15:34:20 -0600743 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard1a5cac22018-09-25 12:59:21 -0600744
Gavin Howard63738202018-09-26 15:34:20 -0600745 if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR;
746 ++(*nchars);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600747
Gavin Howard63738202018-09-26 15:34:20 -0600748 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard80977b22018-09-06 14:29:13 -0600749
Gavin Howard63738202018-09-26 15:34:20 -0600750 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600751
Gavin Howard63738202018-09-26 15:34:20 -0600752 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600753
Gavin Howard63738202018-09-26 15:34:20 -0600754 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600755
Gavin Howard63738202018-09-26 15:34:20 -0600756 div = num / pow;
757 num -= div * pow;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600758
Gavin Howard63738202018-09-26 15:34:20 -0600759 if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
760 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700761
Gavin Howard63738202018-09-26 15:34:20 -0600762 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700763}
Gavin Howardfe679f02018-02-14 15:50:09 -0700764
Gavin Howard902a16c2018-05-16 01:22:53 -0600765BcStatus bc_num_printHex(size_t num, size_t width, bool radix,
Gavin Howard0011a3a2018-03-29 23:11:32 -0600766 size_t *nchars, size_t line_len)
Gavin Howard2682a1f2018-03-03 09:09:13 -0700767{
Gavin Howard63738202018-09-26 15:34:20 -0600768 BcStatus s;
Gavin Howard80977b22018-09-06 14:29:13 -0600769
Gavin Howard63738202018-09-26 15:34:20 -0600770 assert(width == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600771
Gavin Howard63738202018-09-26 15:34:20 -0600772 if (radix) {
773 if ((s = bc_num_printNewline(nchars, line_len))) return s;
774 if (putchar('.') == EOF) return BC_STATUS_IO_ERR;
775 *nchars += 1;
776 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700777
Gavin Howard63738202018-09-26 15:34:20 -0600778 if ((s = bc_num_printNewline(nchars, line_len))) return s;
779 if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR;
780 *nchars = *nchars + width;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700781
Gavin Howard63738202018-09-26 15:34:20 -0600782 return BC_STATUS_SUCCESS;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700783}
784
Gavin Howard5f46ee32018-09-25 13:02:43 -0600785BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700786
Gavin Howardc39fd492018-10-04 10:07:03 -0600787 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600788 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700789
Gavin Howard63738202018-09-26 15:34:20 -0600790 if (n->neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
791 (*nchars) += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700792
Gavin Howard63738202018-09-26 15:34:20 -0600793 for (i = n->len - 1; !s && i < n->len; --i)
794 s = bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700795
Gavin Howard63738202018-09-26 15:34:20 -0600796 return s;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700797}
798
Gavin Howard0011a3a2018-03-29 23:11:32 -0600799BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
800 size_t *nchars, size_t line_len)
Gavin Howardbc7cae82018-03-14 13:43:04 -0600801{
Gavin Howard63738202018-09-26 15:34:20 -0600802 BcStatus s;
803 BcVec stack;
804 BcNum intp, fracp, digit, frac_len;
805 size_t width, i;
806 BcNumDigitOp print;
807 unsigned long dig, *ptr;
808 bool radix, neg = n->neg;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700809
Gavin Howard63738202018-09-26 15:34:20 -0600810 if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
811 (*nchars) += neg;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700812
Gavin Howard63738202018-09-26 15:34:20 -0600813 n->neg = false;
Gavin Howard7f45f1a2018-09-24 14:50:55 -0600814
Gavin Howard63738202018-09-26 15:34:20 -0600815 if (base_t <= BC_NUM_MAX_IBASE) {
816 width = 1;
817 print = bc_num_printHex;
818 }
819 else {
820 width = (size_t) floor(log10((double) (base_t - 1)) + 1.0);
821 print = bc_num_printDigits;
822 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700823
Gavin Howard63738202018-09-26 15:34:20 -0600824 if ((s = bc_vec_init(&stack, sizeof(long), NULL))) goto stack_err;
825 if ((s = bc_num_init(&intp, n->len))) goto int_err;
826 if ((s = bc_num_init(&fracp, n->rdx))) goto frac_err;
827 if ((s = bc_num_init(&digit, width))) goto digit_err;
828 if ((s = bc_num_init(&frac_len, BC_NUM_INT(n)))) goto frac_len_err;
829 if ((s = bc_num_copy(&intp, n))) goto err;
830 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -0600831
Gavin Howard63738202018-09-26 15:34:20 -0600832 bc_num_truncate(&intp, intp.rdx);
833 if ((s = bc_num_sub(n, &intp, &fracp, 0))) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700834
Gavin Howard63738202018-09-26 15:34:20 -0600835 while (intp.len) {
Gavin Howardbe53edd2018-10-04 10:55:08 -0600836 if ((s = bc_num_rem(&intp, base, &digit, 0))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600837 if ((s = bc_num_ulong(&digit, &dig))) goto err;
Gavin Howard90198862018-09-29 03:37:47 -0600838 if ((s = bc_vec_push(&stack, &dig))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600839 if ((s = bc_num_div(&intp, base, &intp, 0))) goto err;
840 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700841
Gavin Howard63738202018-09-26 15:34:20 -0600842 for (i = 0; i < stack.len; ++i) {
843 ptr = bc_vec_item_rev(&stack, i);
844 assert(ptr);
845 if ((s = print(*ptr, width, false, nchars, line_len))) goto err;
846 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700847
Gavin Howard63738202018-09-26 15:34:20 -0600848 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700849
Gavin Howard63738202018-09-26 15:34:20 -0600850 for (radix = true; frac_len.len <= n->rdx; radix = false) {
851 if ((s = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err;
852 if ((s = bc_num_ulong(&fracp, &dig))) goto err;
853 if ((s = bc_num_ulong2num(&intp, dig))) goto err;
854 if ((s = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err;
855 if ((s = print(dig, width, radix, nchars, line_len))) goto err;
856 if ((s = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err;
857 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700858
859err:
Gavin Howard63738202018-09-26 15:34:20 -0600860 bc_num_free(&frac_len);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700861frac_len_err:
Gavin Howard63738202018-09-26 15:34:20 -0600862 bc_num_free(&digit);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700863digit_err:
Gavin Howard63738202018-09-26 15:34:20 -0600864 bc_num_free(&fracp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700865frac_err:
Gavin Howard63738202018-09-26 15:34:20 -0600866 bc_num_free(&intp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700867int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600868 bc_vec_free(&stack);
Gavin Howard7f45f1a2018-09-24 14:50:55 -0600869stack_err:
Gavin Howard63738202018-09-26 15:34:20 -0600870 n->neg = neg;
871 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700872}
873
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700874BcStatus bc_num_init(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700875
Gavin Howard63738202018-09-26 15:34:20 -0600876 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -0700877
Gavin Howard63738202018-09-26 15:34:20 -0600878 request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE;
Gavin Howardc39fd492018-10-04 10:07:03 -0600879 memset(n, 0, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600880 if (!(n->num = malloc(request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -0700881
Gavin Howard63738202018-09-26 15:34:20 -0600882 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700883
Gavin Howard63738202018-09-26 15:34:20 -0600884 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700885}
886
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700887BcStatus bc_num_expand(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700888
Gavin Howard63738202018-09-26 15:34:20 -0600889 BcDigit *temp;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600890
Gavin Howard63738202018-09-26 15:34:20 -0600891 assert(n && request);
Gavin Howardb5c77212018-02-14 17:12:34 -0700892
Gavin Howard63738202018-09-26 15:34:20 -0600893 if (request <= n->cap) return BC_STATUS_SUCCESS;
894 if (!(temp = realloc(n->num, request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -0700895
Gavin Howard63738202018-09-26 15:34:20 -0600896 memset(temp + n->cap, 0, sizeof(char) * (request - n->cap));
897 n->num = temp;
898 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700899
Gavin Howard63738202018-09-26 15:34:20 -0600900 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700901}
902
Gavin Howarded392aa2018-02-27 13:09:26 -0700903void bc_num_free(void *num) {
Gavin Howard63738202018-09-26 15:34:20 -0600904 BcNum *n = (BcNum*) num;
Gavin Howard8c019982018-10-05 12:35:09 -0600905 assert(n);
906 if (n->num) free(n->num);
Gavin Howardb5c77212018-02-14 17:12:34 -0700907}
908
Gavin Howard9c4358c2018-03-22 20:11:28 -0600909BcStatus bc_num_copy(BcNum *d, BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -0700910
Gavin Howard63738202018-09-26 15:34:20 -0600911 BcStatus status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700912
Gavin Howard63738202018-09-26 15:34:20 -0600913 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -0700914
Gavin Howard63738202018-09-26 15:34:20 -0600915 if (d == s) return BC_STATUS_SUCCESS;
916 if ((status = bc_num_expand(d, s->cap))) return status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700917
Gavin Howard63738202018-09-26 15:34:20 -0600918 d->len = s->len;
919 d->neg = s->neg;
920 d->rdx = s->rdx;
Gavin Howard5a049c42018-02-15 11:24:11 -0700921
Gavin Howard63738202018-09-26 15:34:20 -0600922 memcpy(d->num, s->num, sizeof(BcDigit) * d->len);
923 memset(d->num + d->len, 0, sizeof(BcDigit) * (d->cap - d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -0700924
Gavin Howard63738202018-09-26 15:34:20 -0600925 return BC_STATUS_SUCCESS;
Gavin Howard5a049c42018-02-15 11:24:11 -0700926}
927
Gavin Howard5cea43b2018-03-02 11:32:11 -0700928BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
Gavin Howard025d04d2018-02-20 13:53:28 -0700929
Gavin Howard63738202018-09-26 15:34:20 -0600930 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700931
Gavin Howard63738202018-09-26 15:34:20 -0600932 assert(n && val && base);
933 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_NUM_MAX_IBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700934
Gavin Howard63738202018-09-26 15:34:20 -0600935 if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700936
Gavin Howard63738202018-09-26 15:34:20 -0600937 if (base_t == 10) s = bc_num_parseDecimal(n, val);
938 else s = bc_num_parseBase(n, val, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700939
Gavin Howard63738202018-09-26 15:34:20 -0600940 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700941}
942
Gavin Howard0011a3a2018-03-29 23:11:32 -0600943BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
944 size_t *nchars, size_t line_len)
Gavin Howard152f3e82018-03-07 12:33:15 -0700945{
Gavin Howardc39fd492018-10-04 10:07:03 -0600946 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700947
Gavin Howard63738202018-09-26 15:34:20 -0600948 assert(n && base && nchars);
949 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700950
Gavin Howard63738202018-09-26 15:34:20 -0600951 if (*nchars >= line_len) {
952 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
953 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
954 *nchars = 0;
955 }
Gavin Howardbc7cae82018-03-14 13:43:04 -0600956
Gavin Howard63738202018-09-26 15:34:20 -0600957 if (!n->len) {
958 if (putchar('0') == EOF) return BC_STATUS_IO_ERR;
959 ++(*nchars);
Gavin Howard63738202018-09-26 15:34:20 -0600960 }
961 else if (base_t == 10) s = bc_num_printDecimal(n, nchars, line_len);
962 else s = bc_num_printBase(n, base, base_t, nchars, line_len);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700963
Gavin Howard63738202018-09-26 15:34:20 -0600964 if (s) return s;
Gavin Howard152f3e82018-03-07 12:33:15 -0700965
Gavin Howard63738202018-09-26 15:34:20 -0600966 if (newline) {
967 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
968 *nchars = 0;
969 }
Gavin Howard152f3e82018-03-07 12:33:15 -0700970
Gavin Howard63738202018-09-26 15:34:20 -0600971 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700972}
973
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700974BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700975
Gavin Howard63738202018-09-26 15:34:20 -0600976 size_t i;
977 unsigned long pow;
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700978
Gavin Howard63738202018-09-26 15:34:20 -0600979 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700980
Gavin Howard63738202018-09-26 15:34:20 -0600981 if (n->neg) return BC_STATUS_MATH_NEGATIVE;
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700982
Gavin Howard63738202018-09-26 15:34:20 -0600983 for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700984
Gavin Howard63738202018-09-26 15:34:20 -0600985 unsigned long prev = *result, powprev = pow;
986 *result += ((unsigned long) n->num[i]) * pow;
987 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700988
Gavin Howard63738202018-09-26 15:34:20 -0600989 if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW;
990 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700991
Gavin Howard63738202018-09-26 15:34:20 -0600992 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700993}
994
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700995BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -0700996
Gavin Howard63738202018-09-26 15:34:20 -0600997 BcStatus s;
998 size_t len, i;
999 BcDigit *ptr;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001000
Gavin Howard63738202018-09-26 15:34:20 -06001001 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001002
Gavin Howard63738202018-09-26 15:34:20 -06001003 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001004
Gavin Howard63738202018-09-26 15:34:20 -06001005 if (!val) {
1006 memset(n->num, 0, sizeof(char) * n->cap);
1007 return BC_STATUS_SUCCESS;
1008 }
Gavin Howard8e2cc692018-02-15 17:39:14 -07001009
Gavin Howard63738202018-09-26 15:34:20 -06001010 len = (size_t) ceil(log10(((double) ULONG_MAX) + 1.0));
Gavin Howard8e2cc692018-02-15 17:39:14 -07001011
Gavin Howard63738202018-09-26 15:34:20 -06001012 if ((s = bc_num_expand(n, len))) return s;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001013
Gavin Howardc39fd492018-10-04 10:07:03 -06001014 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10)
Gavin Howard63738202018-09-26 15:34:20 -06001015 ptr[i] = (char) (val % 10);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001016
Gavin Howard63738202018-09-26 15:34:20 -06001017 return BC_STATUS_SUCCESS;
Gavin Howard025d04d2018-02-20 13:53:28 -07001018}
1019
Gavin Howard12fe7812018-09-29 03:45:18 -06001020BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001021 (void) scale;
1022 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s;
Gavin Howard12fe7812018-09-29 03:45:18 -06001023 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001024}
1025
Gavin Howard12fe7812018-09-29 03:45:18 -06001026BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001027 (void) scale;
1028 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a;
Gavin Howard12fe7812018-09-29 03:45:18 -06001029 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001030}
1031
Gavin Howard12fe7812018-09-29 03:45:18 -06001032BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001033 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001034 return bc_num_binary(a, b, c, scale, bc_num_alg_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001035}
1036
Gavin Howard12fe7812018-09-29 03:45:18 -06001037BcStatus bc_num_div(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_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001040}
1041
Gavin Howardbe53edd2018-10-04 10:55:08 -06001042BcStatus bc_num_rem(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 Howardbe53edd2018-10-04 10:55:08 -06001044 return bc_num_binary(a, b, c, scale, bc_num_alg_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001045}
1046
Gavin Howard12fe7812018-09-29 03:45:18 -06001047BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
1048 return bc_num_binary(a, b, c, scale, bc_num_alg_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001049}
1050
Gavin Howard4538c902018-08-29 14:31:40 -06001051BcStatus bc_num_sqrt(BcNum *a, BcNum *res, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001052
Gavin Howard63738202018-09-26 15:34:20 -06001053 BcStatus s;
1054 BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp;
1055 size_t pow, len, digits, resrdx, req;
1056 ssize_t cmp;
Gavin Howard954276b2018-05-16 01:43:58 -06001057
Gavin Howard63738202018-09-26 15:34:20 -06001058 assert(a && res);
Gavin Howard954276b2018-05-16 01:43:58 -06001059
Gavin Howard63738202018-09-26 15:34:20 -06001060 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a)+ 1) >> 1) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001061
Gavin Howard63738202018-09-26 15:34:20 -06001062 if (res == a) {
1063 memcpy(&a2, res, sizeof(BcNum));
1064 ptr_a = &a2;
1065 s = bc_num_init(res, req);
1066 }
1067 else {
1068 ptr_a = a;
1069 s = bc_num_expand(res, req);
1070 }
Gavin Howard954276b2018-05-16 01:43:58 -06001071
Gavin Howard63738202018-09-26 15:34:20 -06001072 if (s) goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001073
Gavin Howard63738202018-09-26 15:34:20 -06001074 if (!ptr_a->len) {
1075 bc_num_zero(res);
1076 goto init_err;
1077 }
1078 else if (ptr_a->neg) {
1079 s = BC_STATUS_MATH_NEG_SQRT;
1080 goto init_err;
1081 }
1082 else if (BC_NUM_ONE(a)) {
1083 bc_num_one(res);
1084 s = bc_num_extend(res, scale);
1085 goto init_err;
1086 }
Gavin Howard954276b2018-05-16 01:43:58 -06001087
Gavin Howard63738202018-09-26 15:34:20 -06001088 memset(res->num, 0, res->cap * sizeof(BcDigit));
1089 len = ptr_a->len;
Gavin Howard954276b2018-05-16 01:43:58 -06001090
Gavin Howard63738202018-09-26 15:34:20 -06001091 scale = BC_MAX(scale, ptr_a->rdx) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001092
Gavin Howard63738202018-09-26 15:34:20 -06001093 if ((s = bc_num_init(&num1, len))) goto init_err;
1094 if ((s = bc_num_init(&num2, num1.len))) goto num2_err;
1095 if ((s = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001096
Gavin Howard63738202018-09-26 15:34:20 -06001097 bc_num_one(&half);
1098 half.num[0] = 5;
1099 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001100
Gavin Howard63738202018-09-26 15:34:20 -06001101 len += scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001102
Gavin Howard63738202018-09-26 15:34:20 -06001103 if ((s = bc_num_init(&f, len))) goto f_err;
1104 if ((s = bc_num_init(&fprime, len + scale))) goto fprime_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001105
Gavin Howard63738202018-09-26 15:34:20 -06001106 x0 = &num1;
1107 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001108
Gavin Howard63738202018-09-26 15:34:20 -06001109 bc_num_one(x0);
Gavin Howard954276b2018-05-16 01:43:58 -06001110
Gavin Howardc39fd492018-10-04 10:07:03 -06001111 if ((pow = BC_NUM_INT(ptr_a))) {
Gavin Howard954276b2018-05-16 01:43:58 -06001112
Gavin Howardc39fd492018-10-04 10:07:03 -06001113 if (pow & 1) x0->num[0] = 2;
1114 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001115
Gavin Howardc39fd492018-10-04 10:07:03 -06001116 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001117
Gavin Howard63738202018-09-26 15:34:20 -06001118 if ((s = bc_num_extend(x0, pow))) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06001119
Gavin Howard63738202018-09-26 15:34:20 -06001120 // Make sure to move the radix back.
1121 x0->rdx -= pow;
1122 }
Gavin Howard954276b2018-05-16 01:43:58 -06001123
Gavin Howard63738202018-09-26 15:34:20 -06001124 cmp = 1;
1125 x0->rdx = digits = 0;
1126 resrdx = scale + 1;
1127 len = BC_NUM_INT(x0) + resrdx;
Gavin Howard954276b2018-05-16 01:43:58 -06001128
Gavin Howard63738202018-09-26 15:34:20 -06001129 while (!bcg.signe && cmp && digits <= len) {
Gavin Howard954276b2018-05-16 01:43:58 -06001130
Gavin Howard63738202018-09-26 15:34:20 -06001131 if ((s = bc_num_div(a, x0, &f, resrdx))) goto err;
1132 if ((s = bc_num_add(x0, &f, &fprime, resrdx))) goto err;
1133 if ((s = bc_num_mul(&fprime, &half, x1, resrdx))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001134
Gavin Howard63738202018-09-26 15:34:20 -06001135 cmp = bc_num_cmp(x1, x0);
1136 digits = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001137
Gavin Howard63738202018-09-26 15:34:20 -06001138 temp = x0;
1139 x0 = x1;
1140 x1 = temp;
1141 }
Gavin Howard954276b2018-05-16 01:43:58 -06001142
Gavin Howard63738202018-09-26 15:34:20 -06001143 if (bcg.signe) {
1144 s = BC_STATUS_EXEC_SIGNAL;
1145 goto err;
1146 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001147
Gavin Howard63738202018-09-26 15:34:20 -06001148 if ((s = bc_num_copy(res, x0))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001149
Gavin Howard63738202018-09-26 15:34:20 -06001150 if (res->rdx > --scale) bc_num_truncate(res, res->rdx - scale);
1151 else if (res->rdx < scale) s = bc_num_extend(res, scale - res->rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06001152
1153err:
Gavin Howard63738202018-09-26 15:34:20 -06001154 bc_num_free(&fprime);
Gavin Howard954276b2018-05-16 01:43:58 -06001155fprime_err:
Gavin Howard63738202018-09-26 15:34:20 -06001156 bc_num_free(&f);
Gavin Howard954276b2018-05-16 01:43:58 -06001157f_err:
Gavin Howard63738202018-09-26 15:34:20 -06001158 bc_num_free(&half);
Gavin Howard954276b2018-05-16 01:43:58 -06001159two_err:
Gavin Howard63738202018-09-26 15:34:20 -06001160 bc_num_free(&num2);
Gavin Howard954276b2018-05-16 01:43:58 -06001161num2_err:
Gavin Howard63738202018-09-26 15:34:20 -06001162 bc_num_free(&num1);
Gavin Howard954276b2018-05-16 01:43:58 -06001163init_err:
Gavin Howard63738202018-09-26 15:34:20 -06001164 if (res == a) bc_num_free(&a2);
1165 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001166}
Gavin Howardba009802018-09-29 04:41:51 -06001167
Gavin Howarde6e84762018-10-03 11:46:34 -06001168#ifdef DC_ENABLED
Gavin Howardba009802018-09-29 04:41:51 -06001169BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1170
1171 BcStatus s;
Gavin Howard2c8959e2018-10-04 18:32:58 -06001172 BcNum num2, *ptr_a, *ptr_b, *ptr_c, base, exp, two, temp;
Gavin Howardba009802018-09-29 04:41:51 -06001173 bool init = false;
1174
1175 assert(a && b && c && d);
1176
1177 scale = 0;
1178
1179 if (d == a) {
1180 memcpy(&num2, d, sizeof(BcNum));
1181 ptr_a = &num2;
1182 init = true;
1183 }
1184 else ptr_a = a;
1185
1186 if (d == b) {
1187
1188 ptr_b = &num2;
1189
1190 if (d != a) {
1191 memcpy(ptr_b, d, sizeof(BcNum));
1192 init = true;
1193 }
1194 }
1195 else ptr_b = b;
1196
1197 if (d == c) {
1198
1199 ptr_c = &num2;
1200
1201 if (d != a && d != b) {
1202 memcpy(ptr_c, d, sizeof(BcNum));
1203 init = true;
1204 }
1205 }
1206 else ptr_c = c;
1207
1208 if (init) s = bc_num_init(d, ptr_c->len);
1209 else s = bc_num_expand(d, ptr_c->len);
1210
1211 if (s) return s;
1212
1213 if (!ptr_c->len) {
1214 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
1215 goto base_err;
1216 }
1217
Gavin Howard5ef610b2018-10-04 16:57:47 -06001218 if (ptr_a->rdx || ptr_b->rdx || ptr_c->rdx) {
Gavin Howardba009802018-09-29 04:41:51 -06001219 s = BC_STATUS_MATH_NON_INTEGER;
1220 goto base_err;
1221 }
1222
1223 if (ptr_b->neg) {
1224 s = BC_STATUS_MATH_NEGATIVE;
1225 goto base_err;
1226 }
1227
Gavin Howardba009802018-09-29 04:41:51 -06001228 if ((s = bc_num_init(&base, ptr_c->len))) goto base_err;
1229 if ((s = bc_num_init(&exp, ptr_b->len))) goto exp_err;
1230 if ((s = bc_num_init(&two, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howardba009802018-09-29 04:41:51 -06001231 if ((s = bc_num_init(&temp, ptr_b->len))) goto temp_err;
1232
1233 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001234 two.num[0] = 2;
1235 bc_num_one(d);
1236
Gavin Howardbe53edd2018-10-04 10:55:08 -06001237 if ((s = bc_num_rem(ptr_a, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001238 if ((s = bc_num_copy(&exp, ptr_b))) goto err;
1239
Gavin Howardaf905602018-09-29 18:56:37 -06001240 while (exp.len) {
Gavin Howardba009802018-09-29 04:41:51 -06001241
Gavin Howardbe53edd2018-10-04 10:55:08 -06001242 if ((s = bc_num_rem(&exp, &two, &temp, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001243
1244 if (BC_NUM_ONE(&temp)) {
1245 if ((s = bc_num_mul(d, &base, &temp, scale))) goto err;
Gavin Howardbe53edd2018-10-04 10:55:08 -06001246 if ((s = bc_num_rem(&temp, ptr_c, d, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001247 }
1248
1249 if ((s = bc_num_div(&exp, &two, &exp, scale))) goto err;
1250 if ((s = bc_num_mul(&base, &base, &temp, scale))) goto err;
Gavin Howardbe53edd2018-10-04 10:55:08 -06001251 if ((s = bc_num_rem(&temp, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001252 }
1253
1254err:
1255 bc_num_free(&temp);
1256temp_err:
Gavin Howardba009802018-09-29 04:41:51 -06001257 bc_num_free(&two);
1258two_err:
1259 bc_num_free(&exp);
1260exp_err:
1261 bc_num_free(&base);
1262base_err:
1263 if (init) bc_num_free(&num2);
1264 return s;
1265}
Gavin Howarde6e84762018-10-03 11:46:34 -06001266#endif // DC_ENABLED