blob: 8fd42749976f897647ac3a12998bc48688fb8800 [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 Howardae6e7d52018-03-14 10:13:43 -0600398
Gavin Howard63738202018-09-26 15:34:20 -0600399 if ((s = bc_num_copy(c, a))) return s;
400 if (b->neg) c->neg = !c->neg;
Gavin Howardae6e7d52018-03-14 10:13:43 -0600401
Gavin Howard63738202018-09-26 15:34:20 -0600402 return bc_num_retireMul(c, scale);
403 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700404
Gavin Howard63738202018-09-26 15:34:20 -0600405 if ((s = bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)))) return s;
406 if ((s = bc_num_copy(&cp, a))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700407
Gavin Howard63738202018-09-26 15:34:20 -0600408 if ((len = b->len) > cp.len) {
409 if ((s = bc_num_expand(&cp, len + 2))) goto err;
410 if ((s = bc_num_extend(&cp, len - cp.len))) goto err;
411 }
Gavin Howard021150b2018-03-10 15:40:42 -0700412
Gavin Howard63738202018-09-26 15:34:20 -0600413 if (b->rdx > cp.rdx && (s = bc_num_extend(&cp, b->rdx - cp.rdx)))
414 goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700415
Gavin Howard63738202018-09-26 15:34:20 -0600416 cp.rdx -= b->rdx;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700417
Gavin Howard63738202018-09-26 15:34:20 -0600418 if (scale > cp.rdx && (s = bc_num_extend(&cp, scale - cp.rdx)))
419 goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700420
Gavin Howard63738202018-09-26 15:34:20 -0600421 if (b->rdx == b->len) {
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600422
Gavin Howard63738202018-09-26 15:34:20 -0600423 bool zero = true;
424 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600425
Gavin Howard63738202018-09-26 15:34:20 -0600426 if (i == len && zero) {
427 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
428 goto err;
429 }
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600430
Gavin Howard63738202018-09-26 15:34:20 -0600431 len -= i - 1;
432 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700433
Gavin Howard63738202018-09-26 15:34:20 -0600434 if (cp.cap == cp.len && (s = bc_num_expand(&cp, cp.len + 1))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700435
Gavin Howard63738202018-09-26 15:34:20 -0600436 // We want an extra zero in front to make things simpler.
437 cp.num[cp.len++] = 0;
438 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700439
Gavin Howard63738202018-09-26 15:34:20 -0600440 if ((s = bc_num_expand(c, cp.len))) goto err;
Gavin Howardac7656d2018-03-01 17:18:40 -0700441
Gavin Howard63738202018-09-26 15:34:20 -0600442 bc_num_zero(c);
443 c->rdx = cp.rdx;
444 c->len = cp.len;
445 bptr = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700446
Gavin Howard63738202018-09-26 15:34:20 -0600447 for (i = end - 1; !bcg.signe && i < end; --i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700448
Gavin Howard63738202018-09-26 15:34:20 -0600449 n = cp.num + i;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700450
Gavin Howard63738202018-09-26 15:34:20 -0600451 for (q = 0; (!s && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q)
452 s = bc_num_subArrays(n, bptr, len);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700453
Gavin Howard63738202018-09-26 15:34:20 -0600454 c->num[i] = q;
455 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700456
Gavin Howard63738202018-09-26 15:34:20 -0600457 if (s) goto err;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600458
Gavin Howard63738202018-09-26 15:34:20 -0600459 c->neg = !a->neg != !b->neg;
460 s = bc_num_retireMul(c, scale);
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600461
Gavin Howardb651f1a2018-02-28 17:34:18 -0700462err:
Gavin Howard63738202018-09-26 15:34:20 -0600463 bc_num_free(&cp);
464 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700465}
466
Gavin Howard3f68df72018-03-22 20:30:27 -0600467BcStatus bc_num_alg_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700468
Gavin Howard63738202018-09-26 15:34:20 -0600469 BcStatus s;
470 BcNum c1, c2;
471 size_t len, tscale = BC_MAX(scale + b->rdx, a->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700472
Gavin Howard63738202018-09-26 15:34:20 -0600473 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
Gavin Howard8b254872018-03-14 01:13:35 -0600474
Gavin Howard63738202018-09-26 15:34:20 -0600475 if (!a->len) {
476 bc_num_zero(c);
477 return BC_STATUS_SUCCESS;
478 }
Gavin Howard8b254872018-03-14 01:13:35 -0600479
Gavin Howard63738202018-09-26 15:34:20 -0600480 if (scale) scale = tscale;
481 len = BC_NUM_MREQ(a, b, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700482
Gavin Howard63738202018-09-26 15:34:20 -0600483 if ((s = bc_num_init(&c1, len))) return s;
484 if ((s = bc_num_init(&c2, len))) goto c2_err;
485 if ((s = bc_num_div(a, b, &c1, scale))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700486
Gavin Howard63738202018-09-26 15:34:20 -0600487 if ((s = bc_num_mul(&c1, b, &c2, scale))) goto err;
488 if ((s = bc_num_sub(a, &c2, c, scale))) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600489
Gavin Howard63738202018-09-26 15:34:20 -0600490 if (tscale > c->rdx && c->len) s = bc_num_extend(c, tscale - c->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700491
492err:
Gavin Howard63738202018-09-26 15:34:20 -0600493 bc_num_free(&c2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700494c2_err:
Gavin Howard63738202018-09-26 15:34:20 -0600495 bc_num_free(&c1);
496 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700497}
498
Gavin Howard3f68df72018-03-22 20:30:27 -0600499BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700500
Gavin Howard63738202018-09-26 15:34:20 -0600501 BcStatus s;
502 BcNum copy;
503 unsigned long pow;
504 size_t i, powrdx, resrdx;
505 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700506
Gavin Howard63738202018-09-26 15:34:20 -0600507 if (b->rdx) return BC_STATUS_MATH_NON_INTEGER;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700508
Gavin Howard63738202018-09-26 15:34:20 -0600509 if (!b->len) {
510 bc_num_one(c);
511 return BC_STATUS_SUCCESS;
512 }
513 else if (!a->len) {
514 bc_num_zero(c);
515 return BC_STATUS_SUCCESS;
516 }
517 else if (BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700518
Gavin Howard63738202018-09-26 15:34:20 -0600519 if (!b->neg) s = bc_num_copy(c, a);
520 else s = bc_num_inv(a, c, scale);
Gavin Howard5fc40e72018-03-05 12:11:09 -0700521
Gavin Howard63738202018-09-26 15:34:20 -0600522 return s;
523 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700524
Gavin Howard63738202018-09-26 15:34:20 -0600525 neg = b->neg;
526 b->neg = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700527
Gavin Howard63738202018-09-26 15:34:20 -0600528 if ((s = bc_num_ulong(b, &pow))) return s;
529 if ((s = bc_num_init(&copy, a->len))) return s;
530 if ((s = bc_num_copy(&copy, a))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700531
Gavin Howard63738202018-09-26 15:34:20 -0600532 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600533
Gavin Howard63738202018-09-26 15:34:20 -0600534 b->neg = neg;
Gavin Howardb29674f2018-03-22 22:24:58 -0600535
Gavin Howard63738202018-09-26 15:34:20 -0600536 for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) {
537 powrdx <<= 1;
538 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
539 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700540
Gavin Howard63738202018-09-26 15:34:20 -0600541 if ((s = bc_num_copy(c, &copy))) goto err;
542 if (bcg.signe) {
543 s = BC_STATUS_EXEC_SIGNAL;
544 goto err;
545 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700546
Gavin Howard63738202018-09-26 15:34:20 -0600547 resrdx = powrdx;
Gavin Howard6fb635f2018-03-03 12:45:42 -0700548
Gavin Howard63738202018-09-26 15:34:20 -0600549 for (pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) {
Gavin Howard1d959152018-03-03 23:33:13 -0700550
Gavin Howard63738202018-09-26 15:34:20 -0600551 powrdx <<= 1;
Gavin Howard1d959152018-03-03 23:33:13 -0700552
Gavin Howard63738202018-09-26 15:34:20 -0600553 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
Gavin Howard1d959152018-03-03 23:33:13 -0700554
Gavin Howard63738202018-09-26 15:34:20 -0600555 if (pow & 1) {
556 resrdx += powrdx;
557 if ((s = bc_num_mul(c, &copy, c, resrdx))) goto err;
558 }
559 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700560
Gavin Howard63738202018-09-26 15:34:20 -0600561 if (neg && (s = bc_num_inv(c, c, scale))) goto err;
562 if (bcg.signe) {
563 s = BC_STATUS_EXEC_SIGNAL;
564 goto err;
565 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700566
Gavin Howard63738202018-09-26 15:34:20 -0600567 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700568
Gavin Howard63738202018-09-26 15:34:20 -0600569 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
570 if (zero) bc_num_zero(c);
Gavin Howard1d959152018-03-03 23:33:13 -0700571
Gavin Howard6fbdb292018-02-27 15:44:48 -0700572err:
Gavin Howard63738202018-09-26 15:34:20 -0600573 bc_num_free(&copy);
574 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700575}
576
Gavin Howard63738202018-09-26 15:34:20 -0600577BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600578 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700579{
Gavin Howarda1c44392018-09-27 12:41:15 -0600580 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600581 BcNum num2, *ptr_a, *ptr_b;
582 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700583
Gavin Howard63738202018-09-26 15:34:20 -0600584 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700585
Gavin Howard63738202018-09-26 15:34:20 -0600586 if (c == a) {
587 memcpy(&num2, c, sizeof(BcNum));
588 ptr_a = &num2;
589 init = true;
590 }
591 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700592
Gavin Howard63738202018-09-26 15:34:20 -0600593 if (c == b) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700594
Gavin Howard63738202018-09-26 15:34:20 -0600595 if (c == a) {
596 ptr_b = ptr_a;
597 }
598 else {
599 memcpy(&num2, c, sizeof(BcNum));
600 ptr_b = &num2;
601 init = true;
602 }
603 }
604 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700605
Gavin Howarda1c44392018-09-27 12:41:15 -0600606 if (init) s = bc_num_init(c, req);
607 else s = bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700608
Gavin Howarda1c44392018-09-27 12:41:15 -0600609 if (s) goto err;
610 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700611
Gavin Howard9c4358c2018-03-22 20:11:28 -0600612err:
Gavin Howard63738202018-09-26 15:34:20 -0600613 if (c == a || c == b) bc_num_free(&num2);
Gavin Howarda1c44392018-09-27 12:41:15 -0600614 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700615}
616
Gavin Howard3f68df72018-03-22 20:30:27 -0600617bool bc_num_strValid(const char *val, size_t base) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700618
Gavin Howard63738202018-09-26 15:34:20 -0600619 BcDigit b;
620 bool small, radix = false;
621 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700622
Gavin Howard63738202018-09-26 15:34:20 -0600623 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700624
Gavin Howard63738202018-09-26 15:34:20 -0600625 small = base <= 10;
626 b = (BcDigit) (small ? base + '0' : base - 9 + 'A');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700627
Gavin Howard63738202018-09-26 15:34:20 -0600628 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700629
Gavin Howard63738202018-09-26 15:34:20 -0600630 BcDigit c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600631
Gavin Howard63738202018-09-26 15:34:20 -0600632 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700633
Gavin Howard63738202018-09-26 15:34:20 -0600634 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700635
Gavin Howard63738202018-09-26 15:34:20 -0600636 radix = true;
637 continue;
638 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700639
Gavin Howard63738202018-09-26 15:34:20 -0600640 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
641 return false;
642 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700643
Gavin Howard63738202018-09-26 15:34:20 -0600644 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700645}
646
Gavin Howard3f68df72018-03-22 20:30:27 -0600647BcStatus bc_num_parseDecimal(BcNum *n, const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700648
Gavin Howard63738202018-09-26 15:34:20 -0600649 BcStatus s;
650 size_t len, i;
651 const char *ptr;
652 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700653
Gavin Howard63738202018-09-26 15:34:20 -0600654 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700655
Gavin Howard63738202018-09-26 15:34:20 -0600656 val += i;
657 len = strlen(val);
658 bc_num_zero(n);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700659
Gavin Howard63738202018-09-26 15:34:20 -0600660 if (len) {
661 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
662 if ((s = bc_num_expand(n, len))) return s;
663 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600664
Gavin Howard63738202018-09-26 15:34:20 -0600665 if (zero) {
666 memset(n->num, 0, sizeof(BcDigit) * n->cap);
667 n->neg = false;
668 return BC_STATUS_SUCCESS;
669 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700670
Gavin Howard63738202018-09-26 15:34:20 -0600671 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700672
Gavin Howard63738202018-09-26 15:34:20 -0600673 // Explicitly test for NULL here to produce either a 0 or 1.
674 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700675
Gavin Howard63738202018-09-26 15:34:20 -0600676 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
677 n->num[n->len] = val[i] - '0';
Gavin Howard6fbdb292018-02-27 15:44:48 -0700678
Gavin Howard63738202018-09-26 15:34:20 -0600679 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700680}
681
Gavin Howard3f68df72018-03-22 20:30:27 -0600682BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
Gavin Howardede51f02018-03-02 12:30:00 -0700683
Gavin Howard63738202018-09-26 15:34:20 -0600684 BcStatus s;
685 BcNum temp, mult, result;
686 BcDigit c = '\0';
687 bool zero;
688 unsigned long v;
689 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700690
Gavin Howard63738202018-09-26 15:34:20 -0600691 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700692
Gavin Howard63738202018-09-26 15:34:20 -0600693 for (zero = true, i = 0; zero && i < len; ++i)
694 zero = (val[i] == '.' || val[i] == '0');
695 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700696
Gavin Howard63738202018-09-26 15:34:20 -0600697 if ((s = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return s;
698 if ((s = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700699
Gavin Howard63738202018-09-26 15:34:20 -0600700 for (i = 0; i < len && (c = val[i]) != '.'; ++i) {
Gavin Howardede51f02018-03-02 12:30:00 -0700701
Gavin Howard63738202018-09-26 15:34:20 -0600702 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700703
Gavin Howard63738202018-09-26 15:34:20 -0600704 if ((s = bc_num_mul(n, base, &mult, 0))) goto int_err;
705 if ((s = bc_num_ulong2num(&temp, v))) goto int_err;
706 if ((s = bc_num_add(&mult, &temp, n, 0))) goto int_err;
707 }
Gavin Howardede51f02018-03-02 12:30:00 -0700708
Gavin Howard63738202018-09-26 15:34:20 -0600709 if (i == len && !(c = val[i])) goto int_err;
710 assert(c == '.');
711 if ((s = bc_num_init(&result, base->len))) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700712
Gavin Howard63738202018-09-26 15:34:20 -0600713 bc_num_zero(&result);
714 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700715
Gavin Howard63738202018-09-26 15:34:20 -0600716 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howardede51f02018-03-02 12:30:00 -0700717
Gavin Howard63738202018-09-26 15:34:20 -0600718 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardede51f02018-03-02 12:30:00 -0700719
Gavin Howard63738202018-09-26 15:34:20 -0600720 if ((s = bc_num_mul(&result, base, &result, 0))) goto err;
721 if ((s = bc_num_ulong2num(&temp, v))) goto err;
722 if ((s = bc_num_add(&result, &temp, &result, 0))) goto err;
723 if ((s = bc_num_mul(&mult, base, &mult, 0))) goto err;
724 }
Gavin Howardede51f02018-03-02 12:30:00 -0700725
Gavin Howard63738202018-09-26 15:34:20 -0600726 if ((s = bc_num_div(&result, &mult, &result, digits))) goto err;
727 if ((s = bc_num_add(n, &result, n, digits))) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600728
Gavin Howard63738202018-09-26 15:34:20 -0600729 if (n->len) {
730 if (n->rdx < digits && n->len) s = bc_num_extend(n, digits - n->rdx);
731 }
732 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700733
734err:
Gavin Howard63738202018-09-26 15:34:20 -0600735 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700736int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600737 bc_num_free(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700738mult_err:
Gavin Howard63738202018-09-26 15:34:20 -0600739 bc_num_free(&temp);
740 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700741}
742
Gavin Howard80977b22018-09-06 14:29:13 -0600743BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) {
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600744
Gavin Howard63738202018-09-26 15:34:20 -0600745 if (*nchars == line_len - 1) {
746 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
747 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
748 *nchars = 0;
749 }
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600750
Gavin Howard63738202018-09-26 15:34:20 -0600751 return BC_STATUS_SUCCESS;
Gavin Howard80977b22018-09-06 14:29:13 -0600752}
753
754BcStatus bc_num_printDigits(size_t num, size_t width, bool radix,
755 size_t *nchars, size_t line_len)
756{
Gavin Howard63738202018-09-26 15:34:20 -0600757 BcStatus s;
758 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -0600759
Gavin Howard63738202018-09-26 15:34:20 -0600760 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard1a5cac22018-09-25 12:59:21 -0600761
Gavin Howard63738202018-09-26 15:34:20 -0600762 if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR;
763 ++(*nchars);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600764
Gavin Howard63738202018-09-26 15:34:20 -0600765 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard80977b22018-09-06 14:29:13 -0600766
Gavin Howard63738202018-09-26 15:34:20 -0600767 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600768
Gavin Howard63738202018-09-26 15:34:20 -0600769 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600770
Gavin Howard63738202018-09-26 15:34:20 -0600771 size_t div;
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600772
Gavin Howard63738202018-09-26 15:34:20 -0600773 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600774
Gavin Howard63738202018-09-26 15:34:20 -0600775 div = num / pow;
776 num -= div * pow;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600777
Gavin Howard63738202018-09-26 15:34:20 -0600778 if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
779 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700780
Gavin Howard63738202018-09-26 15:34:20 -0600781 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700782}
Gavin Howardfe679f02018-02-14 15:50:09 -0700783
Gavin Howard902a16c2018-05-16 01:22:53 -0600784BcStatus bc_num_printHex(size_t num, size_t width, bool radix,
Gavin Howard0011a3a2018-03-29 23:11:32 -0600785 size_t *nchars, size_t line_len)
Gavin Howard2682a1f2018-03-03 09:09:13 -0700786{
Gavin Howard63738202018-09-26 15:34:20 -0600787 BcStatus s;
Gavin Howard80977b22018-09-06 14:29:13 -0600788
Gavin Howard63738202018-09-26 15:34:20 -0600789 assert(width == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600790
Gavin Howard63738202018-09-26 15:34:20 -0600791 if (radix) {
792 if ((s = bc_num_printNewline(nchars, line_len))) return s;
793 if (putchar('.') == EOF) return BC_STATUS_IO_ERR;
794 *nchars += 1;
795 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700796
Gavin Howard63738202018-09-26 15:34:20 -0600797 if ((s = bc_num_printNewline(nchars, line_len))) return s;
798 if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR;
799 *nchars = *nchars + width;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700800
Gavin Howard63738202018-09-26 15:34:20 -0600801 return BC_STATUS_SUCCESS;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700802}
803
Gavin Howard5f46ee32018-09-25 13:02:43 -0600804BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700805
Gavin Howard63738202018-09-26 15:34:20 -0600806 BcStatus s;
807 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700808
Gavin Howard63738202018-09-26 15:34:20 -0600809 if (n->neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
810 (*nchars) += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700811
Gavin Howard63738202018-09-26 15:34:20 -0600812 s = BC_STATUS_SUCCESS;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700813
Gavin Howard63738202018-09-26 15:34:20 -0600814 for (i = n->len - 1; !s && i < n->len; --i)
815 s = bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700816
Gavin Howard63738202018-09-26 15:34:20 -0600817 return s;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700818}
819
Gavin Howard0011a3a2018-03-29 23:11:32 -0600820BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
821 size_t *nchars, size_t line_len)
Gavin Howardbc7cae82018-03-14 13:43:04 -0600822{
Gavin Howard63738202018-09-26 15:34:20 -0600823 BcStatus s;
824 BcVec stack;
825 BcNum intp, fracp, digit, frac_len;
826 size_t width, i;
827 BcNumDigitOp print;
828 unsigned long dig, *ptr;
829 bool radix, neg = n->neg;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700830
Gavin Howard63738202018-09-26 15:34:20 -0600831 if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
832 (*nchars) += neg;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700833
Gavin Howard63738202018-09-26 15:34:20 -0600834 n->neg = false;
Gavin Howard7f45f1a2018-09-24 14:50:55 -0600835
Gavin Howard63738202018-09-26 15:34:20 -0600836 if (base_t <= BC_NUM_MAX_IBASE) {
837 width = 1;
838 print = bc_num_printHex;
839 }
840 else {
841 width = (size_t) floor(log10((double) (base_t - 1)) + 1.0);
842 print = bc_num_printDigits;
843 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700844
Gavin Howard63738202018-09-26 15:34:20 -0600845 if ((s = bc_vec_init(&stack, sizeof(long), NULL))) goto stack_err;
846 if ((s = bc_num_init(&intp, n->len))) goto int_err;
847 if ((s = bc_num_init(&fracp, n->rdx))) goto frac_err;
848 if ((s = bc_num_init(&digit, width))) goto digit_err;
849 if ((s = bc_num_init(&frac_len, BC_NUM_INT(n)))) goto frac_len_err;
850 if ((s = bc_num_copy(&intp, n))) goto err;
851 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -0600852
Gavin Howard63738202018-09-26 15:34:20 -0600853 bc_num_truncate(&intp, intp.rdx);
854 if ((s = bc_num_sub(n, &intp, &fracp, 0))) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700855
Gavin Howard63738202018-09-26 15:34:20 -0600856 while (intp.len) {
857 if ((s = bc_num_mod(&intp, base, &digit, 0))) goto err;
858 if ((s = bc_num_ulong(&digit, &dig))) goto err;
859 if ((s = bc_vec_push(&stack, 1, &dig))) goto err;
860 if ((s = bc_num_div(&intp, base, &intp, 0))) goto err;
861 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700862
Gavin Howard63738202018-09-26 15:34:20 -0600863 for (i = 0; i < stack.len; ++i) {
864 ptr = bc_vec_item_rev(&stack, i);
865 assert(ptr);
866 if ((s = print(*ptr, width, false, nchars, line_len))) goto err;
867 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700868
Gavin Howard63738202018-09-26 15:34:20 -0600869 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700870
Gavin Howard63738202018-09-26 15:34:20 -0600871 for (radix = true; frac_len.len <= n->rdx; radix = false) {
872 if ((s = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err;
873 if ((s = bc_num_ulong(&fracp, &dig))) goto err;
874 if ((s = bc_num_ulong2num(&intp, dig))) goto err;
875 if ((s = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err;
876 if ((s = print(dig, width, radix, nchars, line_len))) goto err;
877 if ((s = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err;
878 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700879
880err:
Gavin Howard63738202018-09-26 15:34:20 -0600881 bc_num_free(&frac_len);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700882frac_len_err:
Gavin Howard63738202018-09-26 15:34:20 -0600883 bc_num_free(&digit);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700884digit_err:
Gavin Howard63738202018-09-26 15:34:20 -0600885 bc_num_free(&fracp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700886frac_err:
Gavin Howard63738202018-09-26 15:34:20 -0600887 bc_num_free(&intp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700888int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600889 bc_vec_free(&stack);
Gavin Howard7f45f1a2018-09-24 14:50:55 -0600890stack_err:
Gavin Howard63738202018-09-26 15:34:20 -0600891 n->neg = neg;
892 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700893}
894
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700895BcStatus bc_num_init(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700896
Gavin Howard63738202018-09-26 15:34:20 -0600897 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -0700898
Gavin Howard63738202018-09-26 15:34:20 -0600899 memset(n, 0, sizeof(BcNum));
Gavin Howardb5c77212018-02-14 17:12:34 -0700900
Gavin Howard63738202018-09-26 15:34:20 -0600901 request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE;
902 if (!(n->num = malloc(request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -0700903
Gavin Howard63738202018-09-26 15:34:20 -0600904 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700905
Gavin Howard63738202018-09-26 15:34:20 -0600906 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700907}
908
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700909BcStatus bc_num_expand(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700910
Gavin Howard63738202018-09-26 15:34:20 -0600911 BcDigit *temp;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600912
Gavin Howard63738202018-09-26 15:34:20 -0600913 assert(n && request);
Gavin Howardb5c77212018-02-14 17:12:34 -0700914
Gavin Howard63738202018-09-26 15:34:20 -0600915 if (request <= n->cap) return BC_STATUS_SUCCESS;
916 if (!(temp = realloc(n->num, request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -0700917
Gavin Howard63738202018-09-26 15:34:20 -0600918 memset(temp + n->cap, 0, sizeof(char) * (request - n->cap));
919 n->num = temp;
920 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700921
Gavin Howard63738202018-09-26 15:34:20 -0600922 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700923}
924
Gavin Howarded392aa2018-02-27 13:09:26 -0700925void bc_num_free(void *num) {
Gavin Howard63738202018-09-26 15:34:20 -0600926 BcNum *n = (BcNum*) num;
927 assert(n && n->num);
928 free(n->num);
Gavin Howardb5c77212018-02-14 17:12:34 -0700929}
930
Gavin Howard9c4358c2018-03-22 20:11:28 -0600931BcStatus bc_num_copy(BcNum *d, BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -0700932
Gavin Howard63738202018-09-26 15:34:20 -0600933 BcStatus status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700934
Gavin Howard63738202018-09-26 15:34:20 -0600935 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -0700936
Gavin Howard63738202018-09-26 15:34:20 -0600937 if (d == s) return BC_STATUS_SUCCESS;
938 if ((status = bc_num_expand(d, s->cap))) return status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700939
Gavin Howard63738202018-09-26 15:34:20 -0600940 d->len = s->len;
941 d->neg = s->neg;
942 d->rdx = s->rdx;
Gavin Howard5a049c42018-02-15 11:24:11 -0700943
Gavin Howard63738202018-09-26 15:34:20 -0600944 memcpy(d->num, s->num, sizeof(BcDigit) * d->len);
945 memset(d->num + d->len, 0, sizeof(BcDigit) * (d->cap - d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -0700946
Gavin Howard63738202018-09-26 15:34:20 -0600947 return BC_STATUS_SUCCESS;
Gavin Howard5a049c42018-02-15 11:24:11 -0700948}
949
Gavin Howard5cea43b2018-03-02 11:32:11 -0700950BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
Gavin Howard025d04d2018-02-20 13:53:28 -0700951
Gavin Howard63738202018-09-26 15:34:20 -0600952 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700953
Gavin Howard63738202018-09-26 15:34:20 -0600954 assert(n && val && base);
955 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_NUM_MAX_IBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700956
Gavin Howard63738202018-09-26 15:34:20 -0600957 if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700958
Gavin Howard63738202018-09-26 15:34:20 -0600959 if (base_t == 10) s = bc_num_parseDecimal(n, val);
960 else s = bc_num_parseBase(n, val, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700961
Gavin Howard63738202018-09-26 15:34:20 -0600962 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700963}
964
Gavin Howard0011a3a2018-03-29 23:11:32 -0600965BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
966 size_t *nchars, size_t line_len)
Gavin Howard152f3e82018-03-07 12:33:15 -0700967{
Gavin Howard63738202018-09-26 15:34:20 -0600968 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700969
Gavin Howard63738202018-09-26 15:34:20 -0600970 assert(n && base && nchars);
971 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700972
Gavin Howard63738202018-09-26 15:34:20 -0600973 if (*nchars >= line_len) {
974 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
975 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
976 *nchars = 0;
977 }
Gavin Howardbc7cae82018-03-14 13:43:04 -0600978
Gavin Howard63738202018-09-26 15:34:20 -0600979 if (!n->len) {
980 if (putchar('0') == EOF) return BC_STATUS_IO_ERR;
981 ++(*nchars);
982 s = BC_STATUS_SUCCESS;
983 }
984 else if (base_t == 10) s = bc_num_printDecimal(n, nchars, line_len);
985 else s = bc_num_printBase(n, base, base_t, nchars, line_len);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700986
Gavin Howard63738202018-09-26 15:34:20 -0600987 if (s) return s;
Gavin Howard152f3e82018-03-07 12:33:15 -0700988
Gavin Howard63738202018-09-26 15:34:20 -0600989 if (newline) {
990 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
991 *nchars = 0;
992 }
Gavin Howard152f3e82018-03-07 12:33:15 -0700993
Gavin Howard63738202018-09-26 15:34:20 -0600994 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700995}
996
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700997BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -0700998
Gavin Howard63738202018-09-26 15:34:20 -0600999 size_t i;
1000 unsigned long pow;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001001
Gavin Howard63738202018-09-26 15:34:20 -06001002 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001003
Gavin Howard63738202018-09-26 15:34:20 -06001004 if (n->neg) return BC_STATUS_MATH_NEGATIVE;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001005
Gavin Howard63738202018-09-26 15:34:20 -06001006 for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001007
Gavin Howard63738202018-09-26 15:34:20 -06001008 unsigned long prev = *result, powprev = pow;
1009 *result += ((unsigned long) n->num[i]) * pow;
1010 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001011
Gavin Howard63738202018-09-26 15:34:20 -06001012 if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW;
1013 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001014
Gavin Howard63738202018-09-26 15:34:20 -06001015 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001016}
1017
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001018BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001019
Gavin Howard63738202018-09-26 15:34:20 -06001020 BcStatus s;
1021 size_t len, i;
1022 BcDigit *ptr;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001023
Gavin Howard63738202018-09-26 15:34:20 -06001024 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001025
Gavin Howard63738202018-09-26 15:34:20 -06001026 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001027
Gavin Howard63738202018-09-26 15:34:20 -06001028 if (!val) {
1029 memset(n->num, 0, sizeof(char) * n->cap);
1030 return BC_STATUS_SUCCESS;
1031 }
Gavin Howard8e2cc692018-02-15 17:39:14 -07001032
Gavin Howard63738202018-09-26 15:34:20 -06001033 len = (size_t) ceil(log10(((double) ULONG_MAX) + 1.0));
Gavin Howard8e2cc692018-02-15 17:39:14 -07001034
Gavin Howard63738202018-09-26 15:34:20 -06001035 if ((s = bc_num_expand(n, len))) return s;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001036
Gavin Howard63738202018-09-26 15:34:20 -06001037 for (ptr = n->num, i = 0; val; ++i, ++n->len) {
1038 ptr[i] = (char) (val % 10);
1039 val /= 10;
1040 }
Gavin Howard8e2cc692018-02-15 17:39:14 -07001041
Gavin Howard63738202018-09-26 15:34:20 -06001042 return BC_STATUS_SUCCESS;
Gavin Howard025d04d2018-02-20 13:53:28 -07001043}
1044
Gavin Howard4538c902018-08-29 14:31:40 -06001045BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001046 (void) scale;
1047 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s;
1048 return bc_num_binary(a, b, res, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001049}
1050
Gavin Howard4538c902018-08-29 14:31:40 -06001051BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001052 (void) scale;
1053 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a;
1054 return bc_num_binary(a, b, res, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001055}
1056
Gavin Howard4538c902018-08-29 14:31:40 -06001057BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001058 size_t req = BC_NUM_MREQ(a, b, scale);
1059 return bc_num_binary(a, b, res, scale, bc_num_alg_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001060}
1061
Gavin Howard4538c902018-08-29 14:31:40 -06001062BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001063 size_t req = BC_NUM_MREQ(a, b, scale);
1064 return bc_num_binary(a, b, res, scale, bc_num_alg_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001065}
1066
Gavin Howard4538c902018-08-29 14:31:40 -06001067BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001068 size_t req = BC_NUM_MREQ(a, b, scale);
1069 return bc_num_binary(a, b, res, scale, bc_num_alg_mod, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001070}
1071
Gavin Howard4538c902018-08-29 14:31:40 -06001072BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001073 return bc_num_binary(a, b, res, scale, bc_num_alg_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001074}
1075
Gavin Howard4538c902018-08-29 14:31:40 -06001076BcStatus bc_num_sqrt(BcNum *a, BcNum *res, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001077
Gavin Howard63738202018-09-26 15:34:20 -06001078 BcStatus s;
1079 BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp;
1080 size_t pow, len, digits, resrdx, req;
1081 ssize_t cmp;
Gavin Howard954276b2018-05-16 01:43:58 -06001082
Gavin Howard63738202018-09-26 15:34:20 -06001083 assert(a && res);
Gavin Howard954276b2018-05-16 01:43:58 -06001084
Gavin Howard63738202018-09-26 15:34:20 -06001085 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a)+ 1) >> 1) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001086
Gavin Howard63738202018-09-26 15:34:20 -06001087 if (res == a) {
1088 memcpy(&a2, res, sizeof(BcNum));
1089 ptr_a = &a2;
1090 s = bc_num_init(res, req);
1091 }
1092 else {
1093 ptr_a = a;
1094 s = bc_num_expand(res, req);
1095 }
Gavin Howard954276b2018-05-16 01:43:58 -06001096
Gavin Howard63738202018-09-26 15:34:20 -06001097 if (s) goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001098
Gavin Howard63738202018-09-26 15:34:20 -06001099 if (!ptr_a->len) {
1100 bc_num_zero(res);
1101 goto init_err;
1102 }
1103 else if (ptr_a->neg) {
1104 s = BC_STATUS_MATH_NEG_SQRT;
1105 goto init_err;
1106 }
1107 else if (BC_NUM_ONE(a)) {
1108 bc_num_one(res);
1109 s = bc_num_extend(res, scale);
1110 goto init_err;
1111 }
Gavin Howard954276b2018-05-16 01:43:58 -06001112
Gavin Howard63738202018-09-26 15:34:20 -06001113 memset(res->num, 0, res->cap * sizeof(BcDigit));
1114 len = ptr_a->len;
Gavin Howard954276b2018-05-16 01:43:58 -06001115
Gavin Howard63738202018-09-26 15:34:20 -06001116 scale = BC_MAX(scale, ptr_a->rdx) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001117
Gavin Howard63738202018-09-26 15:34:20 -06001118 if ((s = bc_num_init(&num1, len))) goto init_err;
1119 if ((s = bc_num_init(&num2, num1.len))) goto num2_err;
1120 if ((s = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001121
Gavin Howard63738202018-09-26 15:34:20 -06001122 bc_num_one(&half);
1123 half.num[0] = 5;
1124 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001125
Gavin Howard63738202018-09-26 15:34:20 -06001126 len += scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001127
Gavin Howard63738202018-09-26 15:34:20 -06001128 if ((s = bc_num_init(&f, len))) goto f_err;
1129 if ((s = bc_num_init(&fprime, len + scale))) goto fprime_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001130
Gavin Howard63738202018-09-26 15:34:20 -06001131 x0 = &num1;
1132 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001133
Gavin Howard63738202018-09-26 15:34:20 -06001134 bc_num_one(x0);
Gavin Howard954276b2018-05-16 01:43:58 -06001135
Gavin Howard63738202018-09-26 15:34:20 -06001136 pow = BC_NUM_INT(ptr_a);
Gavin Howard954276b2018-05-16 01:43:58 -06001137
Gavin Howard63738202018-09-26 15:34:20 -06001138 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001139
Gavin Howard63738202018-09-26 15:34:20 -06001140 if (pow & 1) {
1141 x0->num[0] = 2;
1142 pow -= 1;
1143 }
1144 else {
1145 x0->num[0] = 6;
1146 pow -= 2;
1147 }
Gavin Howard954276b2018-05-16 01:43:58 -06001148
Gavin Howard63738202018-09-26 15:34:20 -06001149 if ((s = bc_num_extend(x0, pow))) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06001150
Gavin Howard63738202018-09-26 15:34:20 -06001151 // Make sure to move the radix back.
1152 x0->rdx -= pow;
1153 }
Gavin Howard954276b2018-05-16 01:43:58 -06001154
Gavin Howard63738202018-09-26 15:34:20 -06001155 cmp = 1;
1156 x0->rdx = digits = 0;
1157 resrdx = scale + 1;
1158 len = BC_NUM_INT(x0) + resrdx;
Gavin Howard954276b2018-05-16 01:43:58 -06001159
Gavin Howard63738202018-09-26 15:34:20 -06001160 while (!bcg.signe && cmp && digits <= len) {
Gavin Howard954276b2018-05-16 01:43:58 -06001161
Gavin Howard63738202018-09-26 15:34:20 -06001162 if ((s = bc_num_div(a, x0, &f, resrdx))) goto err;
1163 if ((s = bc_num_add(x0, &f, &fprime, resrdx))) goto err;
1164 if ((s = bc_num_mul(&fprime, &half, x1, resrdx))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001165
Gavin Howard63738202018-09-26 15:34:20 -06001166 cmp = bc_num_cmp(x1, x0);
1167 digits = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001168
Gavin Howard63738202018-09-26 15:34:20 -06001169 temp = x0;
1170 x0 = x1;
1171 x1 = temp;
1172 }
Gavin Howard954276b2018-05-16 01:43:58 -06001173
Gavin Howard63738202018-09-26 15:34:20 -06001174 if (bcg.signe) {
1175 s = BC_STATUS_EXEC_SIGNAL;
1176 goto err;
1177 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001178
Gavin Howard63738202018-09-26 15:34:20 -06001179 if ((s = bc_num_copy(res, x0))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001180
Gavin Howard63738202018-09-26 15:34:20 -06001181 if (res->rdx > --scale) bc_num_truncate(res, res->rdx - scale);
1182 else if (res->rdx < scale) s = bc_num_extend(res, scale - res->rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06001183
1184err:
Gavin Howard63738202018-09-26 15:34:20 -06001185 bc_num_free(&fprime);
Gavin Howard954276b2018-05-16 01:43:58 -06001186fprime_err:
Gavin Howard63738202018-09-26 15:34:20 -06001187 bc_num_free(&f);
Gavin Howard954276b2018-05-16 01:43:58 -06001188f_err:
Gavin Howard63738202018-09-26 15:34:20 -06001189 bc_num_free(&half);
Gavin Howard954276b2018-05-16 01:43:58 -06001190two_err:
Gavin Howard63738202018-09-26 15:34:20 -06001191 bc_num_free(&num2);
Gavin Howard954276b2018-05-16 01:43:58 -06001192num2_err:
Gavin Howard63738202018-09-26 15:34:20 -06001193 bc_num_free(&num1);
Gavin Howard954276b2018-05-16 01:43:58 -06001194init_err:
Gavin Howard63738202018-09-26 15:34:20 -06001195 if (res == a) bc_num_free(&a2);
1196 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001197}