blob: 450f452fa31cd1208886d9a85c5c1afc2147331b [file] [log] [blame]
Gavin Howard3eb626f2018-02-14 13:54:35 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -07003 *
Gavin Howardb5904bf2018-02-20 13:28:18 -07004 * Copyright 2018 Gavin D. Howard
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070017 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070018 *
19 * Code for the number type.
20 *
21 */
22
23#include <assert.h>
Gavin Howard411f7322018-09-26 17:21:19 -060024#include <stdbool.h>
25#include <stdlib.h>
26#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070027
Gavin Howard29493062018-03-20 19:57:37 -060028#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070029#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060030#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070031
Gavin Howard0be26ed2018-08-31 20:21:56 -060032void bc_num_zero(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -060033 if (!n) return;
34 memset(n->num, 0, n->cap * sizeof(char));
35 n->neg = false;
36 n->len = 0;
37 n->rdx = 0;
Gavin Howard0be26ed2018-08-31 20:21:56 -060038}
39
40void bc_num_one(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -060041 if (!n) return;
42 bc_num_zero(n);
43 n->len = 1;
44 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060045}
46
47void bc_num_ten(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -060048 if (!n) return;
49 bc_num_zero(n);
50 n->len = 2;
51 n->num[0] = 0;
52 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060053}
54
Gavin Howard2ea7dc42018-05-22 14:02:02 -060055BcStatus bc_num_subArrays(BcDigit *n1, BcDigit *n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060056 size_t i, j;
57 for (i = 0; !bcg.signe && i < len; ++i) {
58 for (n1[i] -= n2[i], j = 0; !bcg.signe && n1[i + j] < 0;) {
59 n1[i + j++] += 10;
60 n1[i + j] -= 1;
61 }
62 }
63 return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060064}
65
Gavin Howard6a804cf2018-05-17 16:54:12 -060066ssize_t bc_num_compare(BcDigit *n1, BcDigit *n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060067 size_t i;
68 BcDigit c = 0;
69 for (i = len - 1; !bcg.signe && i < len && !(c = n1[i] - n2[i]); --i);
70 return (c < 0 ? -1 : 1) * (ssize_t) (i + 1);
Gavin Howard08bf5292018-03-20 14:59:33 -060071}
72
Gavin Howard6a804cf2018-05-17 16:54:12 -060073ssize_t bc_num_cmp(BcNum *a, BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070074
Gavin Howard63738202018-09-26 15:34:20 -060075 size_t i, min, a_int, b_int, diff;
76 BcDigit *max_num, *min_num;
77 bool a_max;
78 ssize_t cmp, neg = 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070079
Gavin Howard63738202018-09-26 15:34:20 -060080 if (!a) return !b ? 0 : !b->neg * -2 + 1;
81 else if (!b) return a->neg * -2 + 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070082
Gavin Howard63738202018-09-26 15:34:20 -060083 if (!a->len) return (!b->neg * -2 + 1) * !!b->len;
84 else if (!b->len) return a->neg * -2 + 1;
Gavin Howard2444f272018-09-11 12:25:53 -060085
Gavin Howard63738202018-09-26 15:34:20 -060086 if (a->neg) {
87 if (b->neg) neg = -1;
88 else return -1;
89 }
90 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070091
Gavin Howard63738202018-09-26 15:34:20 -060092 a_int = BC_NUM_INT(a);
93 b_int = BC_NUM_INT(b);
94 a_int -= b_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -070095
Gavin Howard63738202018-09-26 15:34:20 -060096 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -070097
Gavin Howard63738202018-09-26 15:34:20 -060098 a_max = a->rdx > b->rdx;
Gavin Howard4681d1b2018-03-05 19:49:33 -070099
Gavin Howard63738202018-09-26 15:34:20 -0600100 if (a_max) {
101 min = b->rdx;
102 diff = a->rdx - b->rdx;
103 max_num = a->num + diff;
104 min_num = b->num;
105 }
106 else {
107 min = a->rdx;
108 diff = b->rdx - a->rdx;
109 max_num = b->num + diff;
110 min_num = a->num;
111 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700112
Gavin Howard63738202018-09-26 15:34:20 -0600113 cmp = bc_num_compare(max_num, min_num, b_int + min);
114 if (cmp) return cmp * (!a_max * -2 + 1) * neg;
Gavin Howard021150b2018-03-10 15:40:42 -0700115
Gavin Howard63738202018-09-26 15:34:20 -0600116 for (max_num -= diff, i = diff - 1; !bcg.signe && i < diff; --i) {
117 if (max_num[i]) return neg * (!a_max * -2 + 1);
118 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700119
Gavin Howard63738202018-09-26 15:34:20 -0600120 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700121}
122
Gavin Howardd9734e52018-03-29 15:54:41 -0600123void bc_num_truncate(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700124
Gavin Howard63738202018-09-26 15:34:20 -0600125 BcDigit *ptr;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700126
Gavin Howard63738202018-09-26 15:34:20 -0600127 assert(places <= n->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700128
Gavin Howard63738202018-09-26 15:34:20 -0600129 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700130
Gavin Howard63738202018-09-26 15:34:20 -0600131 ptr = n->num + places;
132 n->len -= places;
133 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700134
Gavin Howard63738202018-09-26 15:34:20 -0600135 memmove(n->num, ptr, n->len * sizeof(BcDigit));
136 memset(n->num + n->len, 0, sizeof(BcDigit) * (n->cap - n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700137}
138
Gavin Howard3f68df72018-03-22 20:30:27 -0600139BcStatus bc_num_extend(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700140
Gavin Howard63738202018-09-26 15:34:20 -0600141 BcStatus s;
142 BcDigit *ptr;
143 size_t len;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700144
Gavin Howard63738202018-09-26 15:34:20 -0600145 if (!places) return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700146
Gavin Howard63738202018-09-26 15:34:20 -0600147 len = n->len + places;
148 if (n->cap < len && (s = bc_num_expand(n, len))) return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700149
Gavin Howard63738202018-09-26 15:34:20 -0600150 ptr = n->num + places;
151 memmove(ptr, n->num, sizeof(BcDigit) * n->len);
152 memset(n->num, 0, sizeof(BcDigit) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700153
Gavin Howard63738202018-09-26 15:34:20 -0600154 n->len += places;
155 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700156
Gavin Howard63738202018-09-26 15:34:20 -0600157 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700158}
159
Gavin Howardc0f65d72018-09-01 16:59:04 -0600160void bc_num_clean(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -0600161 while (n->len > 0 && !n->num[n->len - 1]) --n->len;
162 if (n->len == 0) n->rdx = n->neg = 0;
163 else if (n->len < n->rdx) n->len = n->rdx;
Gavin Howarde57525d2018-09-01 16:56:21 -0600164}
165
Gavin Howard337fe602018-09-01 15:10:59 -0600166BcStatus bc_num_retireMul(BcNum *n, size_t scale) {
167
Gavin Howard63738202018-09-26 15:34:20 -0600168 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600169
Gavin Howard63738202018-09-26 15:34:20 -0600170 if (n->rdx < scale) s = bc_num_extend(n, scale - n->rdx);
171 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600172
Gavin Howard63738202018-09-26 15:34:20 -0600173 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600174
Gavin Howard63738202018-09-26 15:34:20 -0600175 return s;
Gavin Howard337fe602018-09-01 15:10:59 -0600176}
177
Gavin Howard9c4358c2018-03-22 20:11:28 -0600178BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
179
Gavin Howard63738202018-09-26 15:34:20 -0600180 BcStatus s;
181 BcNum one;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600182
Gavin Howard63738202018-09-26 15:34:20 -0600183 if ((s = bc_num_init(&one, BC_NUM_DEF_SIZE))) return s;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600184
Gavin Howard63738202018-09-26 15:34:20 -0600185 bc_num_one(&one);
186 s = bc_num_div(&one, a, b, scale);
187 bc_num_free(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600188
Gavin Howard63738202018-09-26 15:34:20 -0600189 return s;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600190}
191
Gavin Howard24434e12018-09-06 15:10:32 -0600192BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700193
Gavin Howard63738202018-09-26 15:34:20 -0600194 BcDigit *ptr, *ptr_a, *ptr_b, *ptr_c;
195 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
196 BcDigit carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700197
Gavin Howard63738202018-09-26 15:34:20 -0600198 // Because this function doesn't need to use scale (per the bc spec),
199 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600200
Gavin Howard63738202018-09-26 15:34:20 -0600201 if (!a->len) {
202 BcStatus s = bc_num_copy(c, b);
203 if (sub) c->neg = !c->neg;
204 return s;
205 }
206 else if (!b->len) return bc_num_copy(c, a);
Gavin Howardf6964a12018-03-14 10:52:31 -0600207
Gavin Howard63738202018-09-26 15:34:20 -0600208 c->neg = a->neg;
209 memset(c->num, 0, c->cap * sizeof(BcDigit));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700210
Gavin Howard63738202018-09-26 15:34:20 -0600211 c->rdx = BC_MAX(a->rdx, b->rdx);
212 min_rdx = BC_MIN(a->rdx, b->rdx);
213 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700214
Gavin Howard63738202018-09-26 15:34:20 -0600215 if (a->rdx > b->rdx) {
216 diff = a->rdx - b->rdx;
217 ptr = a->num;
218 ptr_a = a->num + diff;
219 ptr_b = b->num;
220 }
221 else {
222 diff = b->rdx - a->rdx;
223 ptr = b->num;
224 ptr_a = a->num;
225 ptr_b = b->num + diff;
226 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700227
Gavin Howard63738202018-09-26 15:34:20 -0600228 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700229
Gavin Howard63738202018-09-26 15:34:20 -0600230 ptr_c += diff;
231 a_int = BC_NUM_INT(a);
232 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700233
Gavin Howard63738202018-09-26 15:34:20 -0600234 if (a_int > b_int) {
235 min_int = b_int;
236 max = a_int;
237 ptr = ptr_a;
238 }
239 else {
240 min_int = a_int;
241 max = b_int;
242 ptr = ptr_b;
243 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700244
Gavin Howard63738202018-09-26 15:34:20 -0600245 for (carry = 0, i = 0; !bcg.signe && i < min_rdx + min_int; ++i, ++c->len) {
246 ptr_c[i] = ptr_a[i] + ptr_b[i] + carry;
247 carry = ptr_c[i] / 10;
248 ptr_c[i] %= 10;
249 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700250
Gavin Howard63738202018-09-26 15:34:20 -0600251 for (; !bcg.signe && i < max + min_rdx; ++i, ++c->len) {
252 ptr_c[i] += ptr[i] + carry;
253 carry = ptr_c[i] / 10;
254 ptr_c[i] %= 10;
255 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700256
Gavin Howard63738202018-09-26 15:34:20 -0600257 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600258
Gavin Howard63738202018-09-26 15:34:20 -0600259 if (carry) c->num[c->len++] = carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700260
Gavin Howard63738202018-09-26 15:34:20 -0600261 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700262}
263
Gavin Howard3f68df72018-03-22 20:30:27 -0600264BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700265
Gavin Howard63738202018-09-26 15:34:20 -0600266 BcStatus s;
267 ssize_t cmp;
268 BcNum *minuend, *subtrahend;
269 size_t start;
270 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700271
Gavin Howard63738202018-09-26 15:34:20 -0600272 // Because this function doesn't need to use scale (per the bc spec),
273 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700274
Gavin Howard63738202018-09-26 15:34:20 -0600275 if (!a->len) {
276 s = bc_num_copy(c, b);
277 if (sub) c->neg = !b->neg;
278 return s;
279 }
280 else if (!b->len) return bc_num_copy(c, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700281
Gavin Howard63738202018-09-26 15:34:20 -0600282 aneg = a->neg;
283 bneg = b->neg;
284 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700285
Gavin Howard63738202018-09-26 15:34:20 -0600286 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700287
Gavin Howard63738202018-09-26 15:34:20 -0600288 a->neg = aneg;
289 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700290
Gavin Howard63738202018-09-26 15:34:20 -0600291 if (!cmp) {
292 bc_num_zero(c);
293 return BC_STATUS_SUCCESS;
294 }
295 else if (cmp > 0) {
296 neg = a->neg;
297 minuend = a;
298 subtrahend = b;
299 }
300 else {
301 neg = b->neg;
302 if (sub) neg = !neg;
303 minuend = b;
304 subtrahend = a;
305 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700306
Gavin Howard63738202018-09-26 15:34:20 -0600307 if ((s = bc_num_copy(c, minuend))) return s;
308 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700309
Gavin Howard63738202018-09-26 15:34:20 -0600310 if (c->rdx < subtrahend->rdx) {
311 if ((s = bc_num_extend(c, subtrahend->rdx - c->rdx))) return s;
312 start = 0;
313 }
314 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700315
Gavin Howard63738202018-09-26 15:34:20 -0600316 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700317
Gavin Howard63738202018-09-26 15:34:20 -0600318 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700319
Gavin Howard63738202018-09-26 15:34:20 -0600320 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700321}
322
Gavin Howard3f68df72018-03-22 20:30:27 -0600323BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700324
Gavin Howard63738202018-09-26 15:34:20 -0600325 BcStatus s;
326 BcDigit carry;
327 size_t i, j, len;
328 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600329
Gavin Howard63738202018-09-26 15:34:20 -0600330 c->rdx = a->rdx + b->rdx;
331 scale = BC_MAX(scale, a->rdx);
332 scale = BC_MAX(scale, b->rdx);
333 scale = BC_MIN(scale, c->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700334
Gavin Howard63738202018-09-26 15:34:20 -0600335 if (!a->len || !b->len) {
336 bc_num_zero(c);
337 return BC_STATUS_SUCCESS;
338 }
339 else if (aone || BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700340
Gavin Howard63738202018-09-26 15:34:20 -0600341 if (aone) {
Gavin Howard9ab772a2018-10-05 11:38:40 -0600342 if ((s = bc_num_copy(c, b))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600343 if (a->neg) c->neg = !c->neg;
344 }
345 else {
Gavin Howard9ab772a2018-10-05 11:38:40 -0600346 if ((s = bc_num_copy(c, a))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600347 if (b->neg) c->neg = !c->neg;
348 }
Gavin Howard337fe602018-09-01 15:10:59 -0600349
Gavin Howard63738202018-09-26 15:34:20 -0600350 return bc_num_retireMul(c, scale);
351 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700352
Gavin Howard63738202018-09-26 15:34:20 -0600353 memset(c->num, 0, sizeof(BcDigit) * c->cap);
354 c->len = carry = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700355
Gavin Howard63738202018-09-26 15:34:20 -0600356 for (i = 0; !bcg.signe && i < b->len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700357
Gavin Howard63738202018-09-26 15:34:20 -0600358 for (j = 0; !bcg.signe && j < a->len; ++j) {
359 c->num[i + j] += a->num[j] * b->num[i] + carry;
360 carry = c->num[i + j] / 10;
361 c->num[i + j] %= 10;
362 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700363
Gavin Howard63738202018-09-26 15:34:20 -0600364 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600365
Gavin Howard63738202018-09-26 15:34:20 -0600366 if (carry) {
367 c->num[i + j] += carry;
368 carry = 0;
369 len = BC_MAX(len, i + j + 1);
370 }
371 else len = BC_MAX(len, i + j);
372 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700373
Gavin Howard63738202018-09-26 15:34:20 -0600374 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600375
Gavin Howard63738202018-09-26 15:34:20 -0600376 c->len = BC_MAX(len, c->rdx);
377 c->neg = !a->neg != !b->neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700378
Gavin Howard63738202018-09-26 15:34:20 -0600379 return bc_num_retireMul(c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700380}
381
Gavin Howard3f68df72018-03-22 20:30:27 -0600382BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700383
Gavin Howard63738202018-09-26 15:34:20 -0600384 BcStatus s;
385 BcDigit *n, *bptr, q;
386 size_t len, end, i;
387 BcNum cp;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700388
Gavin Howard63738202018-09-26 15:34:20 -0600389 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
390 else if (!a->len) {
391 bc_num_zero(c);
392 return BC_STATUS_SUCCESS;
393 }
394 else if (BC_NUM_ONE(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600395 if ((s = bc_num_copy(c, a))) return s;
396 if (b->neg) c->neg = !c->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600397 return bc_num_retireMul(c, scale);
398 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700399
Gavin Howard63738202018-09-26 15:34:20 -0600400 if ((s = bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)))) return s;
401 if ((s = bc_num_copy(&cp, a))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700402
Gavin Howard63738202018-09-26 15:34:20 -0600403 if ((len = b->len) > cp.len) {
404 if ((s = bc_num_expand(&cp, len + 2))) goto err;
405 if ((s = bc_num_extend(&cp, len - cp.len))) goto err;
406 }
Gavin Howard021150b2018-03-10 15:40:42 -0700407
Gavin Howard50651542018-09-29 02:53:11 -0600408 if (b->rdx > cp.rdx && (s = bc_num_extend(&cp, b->rdx - cp.rdx))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600409 cp.rdx -= b->rdx;
Gavin Howard50651542018-09-29 02:53:11 -0600410 if (scale > cp.rdx && (s = bc_num_extend(&cp, scale - cp.rdx))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700411
Gavin Howard63738202018-09-26 15:34:20 -0600412 if (b->rdx == b->len) {
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600413
Gavin Howard63738202018-09-26 15:34:20 -0600414 bool zero = true;
415 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600416
Gavin Howard63738202018-09-26 15:34:20 -0600417 if (i == len && zero) {
418 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
419 goto err;
420 }
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600421
Gavin Howard63738202018-09-26 15:34:20 -0600422 len -= i - 1;
423 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700424
Gavin Howard63738202018-09-26 15:34:20 -0600425 if (cp.cap == cp.len && (s = bc_num_expand(&cp, cp.len + 1))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700426
Gavin Howard63738202018-09-26 15:34:20 -0600427 // We want an extra zero in front to make things simpler.
428 cp.num[cp.len++] = 0;
429 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700430
Gavin Howard63738202018-09-26 15:34:20 -0600431 if ((s = bc_num_expand(c, cp.len))) goto err;
Gavin Howardac7656d2018-03-01 17:18:40 -0700432
Gavin Howard63738202018-09-26 15:34:20 -0600433 bc_num_zero(c);
434 c->rdx = cp.rdx;
435 c->len = cp.len;
436 bptr = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700437
Gavin Howardc39fd492018-10-04 10:07:03 -0600438 for (i = end - 1; !bcg.signe && !s && i < end; --i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700439
Gavin Howard63738202018-09-26 15:34:20 -0600440 n = cp.num + i;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700441
Gavin Howard63738202018-09-26 15:34:20 -0600442 for (q = 0; (!s && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q)
443 s = bc_num_subArrays(n, bptr, len);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700444
Gavin Howard63738202018-09-26 15:34:20 -0600445 c->num[i] = q;
446 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700447
Gavin Howard63738202018-09-26 15:34:20 -0600448 if (s) goto err;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600449
Gavin Howard63738202018-09-26 15:34:20 -0600450 c->neg = !a->neg != !b->neg;
451 s = bc_num_retireMul(c, scale);
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600452
Gavin Howardb651f1a2018-02-28 17:34:18 -0700453err:
Gavin Howard63738202018-09-26 15:34:20 -0600454 bc_num_free(&cp);
455 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700456}
457
Gavin Howardbe53edd2018-10-04 10:55:08 -0600458BcStatus bc_num_alg_rem(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700459
Gavin Howard63738202018-09-26 15:34:20 -0600460 BcStatus s;
461 BcNum c1, c2;
Gavin Howard841a3882018-10-04 11:28:14 -0600462 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700463
Gavin Howard63738202018-09-26 15:34:20 -0600464 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
Gavin Howard8b254872018-03-14 01:13:35 -0600465
Gavin Howard63738202018-09-26 15:34:20 -0600466 if (!a->len) {
467 bc_num_zero(c);
468 return BC_STATUS_SUCCESS;
469 }
Gavin Howard8b254872018-03-14 01:13:35 -0600470
Gavin Howard63738202018-09-26 15:34:20 -0600471 if ((s = bc_num_init(&c1, len))) return s;
472 if ((s = bc_num_init(&c2, len))) goto c2_err;
473 if ((s = bc_num_div(a, b, &c1, scale))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700474
Gavin Howard841a3882018-10-04 11:28:14 -0600475 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600476
Gavin Howard63738202018-09-26 15:34:20 -0600477 if ((s = bc_num_mul(&c1, b, &c2, scale))) goto err;
478 if ((s = bc_num_sub(a, &c2, c, scale))) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600479
Gavin Howard841a3882018-10-04 11:28:14 -0600480 if (ts > c->rdx && c->len) s = bc_num_extend(c, ts - c->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700481
482err:
Gavin Howard63738202018-09-26 15:34:20 -0600483 bc_num_free(&c2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700484c2_err:
Gavin Howard63738202018-09-26 15:34:20 -0600485 bc_num_free(&c1);
486 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700487}
488
Gavin Howard3f68df72018-03-22 20:30:27 -0600489BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700490
Gavin Howard63738202018-09-26 15:34:20 -0600491 BcStatus s;
492 BcNum copy;
493 unsigned long pow;
494 size_t i, powrdx, resrdx;
495 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700496
Gavin Howard63738202018-09-26 15:34:20 -0600497 if (b->rdx) return BC_STATUS_MATH_NON_INTEGER;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700498
Gavin Howard63738202018-09-26 15:34:20 -0600499 if (!b->len) {
500 bc_num_one(c);
501 return BC_STATUS_SUCCESS;
502 }
503 else if (!a->len) {
504 bc_num_zero(c);
505 return BC_STATUS_SUCCESS;
506 }
507 else if (BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700508
Gavin Howard63738202018-09-26 15:34:20 -0600509 if (!b->neg) s = bc_num_copy(c, a);
510 else s = bc_num_inv(a, c, scale);
Gavin Howard5fc40e72018-03-05 12:11:09 -0700511
Gavin Howard63738202018-09-26 15:34:20 -0600512 return s;
513 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700514
Gavin Howard63738202018-09-26 15:34:20 -0600515 neg = b->neg;
516 b->neg = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700517
Gavin Howard63738202018-09-26 15:34:20 -0600518 if ((s = bc_num_ulong(b, &pow))) return s;
519 if ((s = bc_num_init(&copy, a->len))) return s;
520 if ((s = bc_num_copy(&copy, a))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700521
Gavin Howard63738202018-09-26 15:34:20 -0600522 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600523
Gavin Howard63738202018-09-26 15:34:20 -0600524 b->neg = neg;
Gavin Howardb29674f2018-03-22 22:24:58 -0600525
Gavin Howard63738202018-09-26 15:34:20 -0600526 for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) {
527 powrdx <<= 1;
528 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
529 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700530
Gavin Howard63738202018-09-26 15:34:20 -0600531 if ((s = bc_num_copy(c, &copy))) goto err;
532 if (bcg.signe) {
533 s = BC_STATUS_EXEC_SIGNAL;
534 goto err;
535 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700536
Gavin Howardc39fd492018-10-04 10:07:03 -0600537 for (resrdx = powrdx, pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) {
Gavin Howard1d959152018-03-03 23:33:13 -0700538
Gavin Howard63738202018-09-26 15:34:20 -0600539 powrdx <<= 1;
Gavin Howard1d959152018-03-03 23:33:13 -0700540
Gavin Howard63738202018-09-26 15:34:20 -0600541 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
Gavin Howard1d959152018-03-03 23:33:13 -0700542
Gavin Howard63738202018-09-26 15:34:20 -0600543 if (pow & 1) {
544 resrdx += powrdx;
545 if ((s = bc_num_mul(c, &copy, c, resrdx))) goto err;
546 }
547 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700548
Gavin Howard63738202018-09-26 15:34:20 -0600549 if (neg && (s = bc_num_inv(c, c, scale))) goto err;
550 if (bcg.signe) {
551 s = BC_STATUS_EXEC_SIGNAL;
552 goto err;
553 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700554
Gavin Howard63738202018-09-26 15:34:20 -0600555 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700556
Gavin Howard63738202018-09-26 15:34:20 -0600557 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
558 if (zero) bc_num_zero(c);
Gavin Howard1d959152018-03-03 23:33:13 -0700559
Gavin Howard6fbdb292018-02-27 15:44:48 -0700560err:
Gavin Howard63738202018-09-26 15:34:20 -0600561 bc_num_free(&copy);
562 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700563}
564
Gavin Howard63738202018-09-26 15:34:20 -0600565BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600566 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700567{
Gavin Howarda1c44392018-09-27 12:41:15 -0600568 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600569 BcNum num2, *ptr_a, *ptr_b;
570 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700571
Gavin Howard63738202018-09-26 15:34:20 -0600572 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700573
Gavin Howard63738202018-09-26 15:34:20 -0600574 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600575 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600576 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600577 init = true;
578 }
579 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700580
Gavin Howard63738202018-09-26 15:34:20 -0600581 if (c == b) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700582
Gavin Howardba009802018-09-29 04:41:51 -0600583 ptr_b = &num2;
584
585 if (c != a) {
586 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600587 init = true;
588 }
589 }
590 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700591
Gavin Howarda1c44392018-09-27 12:41:15 -0600592 if (init) s = bc_num_init(c, req);
593 else s = bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700594
Gavin Howard9ab772a2018-10-05 11:38:40 -0600595 if (!s) s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700596
Gavin Howardba009802018-09-29 04:41:51 -0600597 if (init) bc_num_free(&num2);
Gavin Howarda1c44392018-09-27 12:41:15 -0600598 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700599}
600
Gavin Howard3f68df72018-03-22 20:30:27 -0600601bool bc_num_strValid(const char *val, size_t base) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700602
Gavin Howard63738202018-09-26 15:34:20 -0600603 BcDigit b;
604 bool small, radix = false;
605 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700606
Gavin Howard63738202018-09-26 15:34:20 -0600607 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700608
Gavin Howard63738202018-09-26 15:34:20 -0600609 small = base <= 10;
610 b = (BcDigit) (small ? base + '0' : base - 9 + 'A');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700611
Gavin Howard63738202018-09-26 15:34:20 -0600612 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700613
Gavin Howard63738202018-09-26 15:34:20 -0600614 BcDigit c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600615
Gavin Howard63738202018-09-26 15:34:20 -0600616 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700617
Gavin Howard63738202018-09-26 15:34:20 -0600618 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700619
Gavin Howard63738202018-09-26 15:34:20 -0600620 radix = true;
621 continue;
622 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700623
Gavin Howard63738202018-09-26 15:34:20 -0600624 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
625 return false;
626 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700627
Gavin Howard63738202018-09-26 15:34:20 -0600628 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700629}
630
Gavin Howard3f68df72018-03-22 20:30:27 -0600631BcStatus bc_num_parseDecimal(BcNum *n, const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700632
Gavin Howard63738202018-09-26 15:34:20 -0600633 BcStatus s;
634 size_t len, i;
635 const char *ptr;
636 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700637
Gavin Howard63738202018-09-26 15:34:20 -0600638 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700639
Gavin Howard63738202018-09-26 15:34:20 -0600640 val += i;
641 len = strlen(val);
642 bc_num_zero(n);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700643
Gavin Howard63738202018-09-26 15:34:20 -0600644 if (len) {
645 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
646 if ((s = bc_num_expand(n, len))) return s;
647 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600648
Gavin Howard63738202018-09-26 15:34:20 -0600649 if (zero) {
650 memset(n->num, 0, sizeof(BcDigit) * n->cap);
651 n->neg = false;
652 return BC_STATUS_SUCCESS;
653 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700654
Gavin Howard63738202018-09-26 15:34:20 -0600655 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700656
Gavin Howard63738202018-09-26 15:34:20 -0600657 // Explicitly test for NULL here to produce either a 0 or 1.
658 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700659
Gavin Howard63738202018-09-26 15:34:20 -0600660 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
661 n->num[n->len] = val[i] - '0';
Gavin Howard6fbdb292018-02-27 15:44:48 -0700662
Gavin Howard63738202018-09-26 15:34:20 -0600663 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700664}
665
Gavin Howard3f68df72018-03-22 20:30:27 -0600666BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
Gavin Howardede51f02018-03-02 12:30:00 -0700667
Gavin Howard63738202018-09-26 15:34:20 -0600668 BcStatus s;
669 BcNum temp, mult, result;
670 BcDigit c = '\0';
671 bool zero;
672 unsigned long v;
673 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700674
Gavin Howard63738202018-09-26 15:34:20 -0600675 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700676
Gavin Howard63738202018-09-26 15:34:20 -0600677 for (zero = true, i = 0; zero && i < len; ++i)
678 zero = (val[i] == '.' || val[i] == '0');
679 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700680
Gavin Howard63738202018-09-26 15:34:20 -0600681 if ((s = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return s;
682 if ((s = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700683
Gavin Howard63738202018-09-26 15:34:20 -0600684 for (i = 0; i < len && (c = val[i]) != '.'; ++i) {
Gavin Howardede51f02018-03-02 12:30:00 -0700685
Gavin Howard63738202018-09-26 15:34:20 -0600686 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700687
Gavin Howard63738202018-09-26 15:34:20 -0600688 if ((s = bc_num_mul(n, base, &mult, 0))) goto int_err;
689 if ((s = bc_num_ulong2num(&temp, v))) goto int_err;
690 if ((s = bc_num_add(&mult, &temp, n, 0))) goto int_err;
691 }
Gavin Howardede51f02018-03-02 12:30:00 -0700692
Gavin Howard63738202018-09-26 15:34:20 -0600693 if (i == len && !(c = val[i])) goto int_err;
694 assert(c == '.');
695 if ((s = bc_num_init(&result, base->len))) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700696
Gavin Howard63738202018-09-26 15:34:20 -0600697 bc_num_zero(&result);
698 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700699
Gavin Howard63738202018-09-26 15:34:20 -0600700 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
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 Howardede51f02018-03-02 12:30:00 -0700703
Gavin Howard63738202018-09-26 15:34:20 -0600704 if ((s = bc_num_mul(&result, base, &result, 0))) goto err;
705 if ((s = bc_num_ulong2num(&temp, v))) goto err;
706 if ((s = bc_num_add(&result, &temp, &result, 0))) goto err;
707 if ((s = bc_num_mul(&mult, base, &mult, 0))) goto err;
708 }
Gavin Howardede51f02018-03-02 12:30:00 -0700709
Gavin Howard63738202018-09-26 15:34:20 -0600710 if ((s = bc_num_div(&result, &mult, &result, digits))) goto err;
711 if ((s = bc_num_add(n, &result, n, digits))) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600712
Gavin Howard63738202018-09-26 15:34:20 -0600713 if (n->len) {
714 if (n->rdx < digits && n->len) s = bc_num_extend(n, digits - n->rdx);
715 }
716 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700717
718err:
Gavin Howard63738202018-09-26 15:34:20 -0600719 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700720int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600721 bc_num_free(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700722mult_err:
Gavin Howard63738202018-09-26 15:34:20 -0600723 bc_num_free(&temp);
724 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700725}
726
Gavin Howard80977b22018-09-06 14:29:13 -0600727BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) {
Gavin Howard63738202018-09-26 15:34:20 -0600728 if (*nchars == line_len - 1) {
729 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
730 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
731 *nchars = 0;
732 }
Gavin Howard63738202018-09-26 15:34:20 -0600733 return BC_STATUS_SUCCESS;
Gavin Howard80977b22018-09-06 14:29:13 -0600734}
735
Gavin Howard83eb8392018-10-09 01:21:19 -0600736#ifdef DC_ENABLED
737BcStatus bc_num_printChar(size_t num, size_t width, bool radix,
738 size_t *nchars, size_t line_len)
739{
Gavin Howard4c091542018-10-09 03:33:08 -0600740 (void) radix, (void) line_len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600741
Gavin Howard4c091542018-10-09 03:33:08 -0600742 if (putc((char) num, stdout) == EOF) return BC_STATUS_IO_ERR;
Gavin Howard83eb8392018-10-09 01:21:19 -0600743 *nchars = *nchars + width;
744
745 return BC_STATUS_SUCCESS;
746}
747#endif // DC_ENABLED
748
Gavin Howard80977b22018-09-06 14:29:13 -0600749BcStatus bc_num_printDigits(size_t num, size_t width, bool radix,
750 size_t *nchars, size_t line_len)
751{
Gavin Howard63738202018-09-26 15:34:20 -0600752 BcStatus s;
Gavin Howardc39fd492018-10-04 10:07:03 -0600753 size_t exp, pow, div;
Gavin Howard80977b22018-09-06 14:29:13 -0600754
Gavin Howard63738202018-09-26 15:34:20 -0600755 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600756 if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR;
757 ++(*nchars);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600758
Gavin Howard63738202018-09-26 15:34:20 -0600759 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600760 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600761
Gavin Howard63738202018-09-26 15:34:20 -0600762 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600763
Gavin Howard63738202018-09-26 15:34:20 -0600764 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600765
Gavin Howard63738202018-09-26 15:34:20 -0600766 div = num / pow;
767 num -= div * pow;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600768
Gavin Howard63738202018-09-26 15:34:20 -0600769 if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
770 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700771
Gavin Howard63738202018-09-26 15:34:20 -0600772 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700773}
Gavin Howardfe679f02018-02-14 15:50:09 -0700774
Gavin Howard902a16c2018-05-16 01:22:53 -0600775BcStatus bc_num_printHex(size_t num, size_t width, bool radix,
Gavin Howard0011a3a2018-03-29 23:11:32 -0600776 size_t *nchars, size_t line_len)
Gavin Howard2682a1f2018-03-03 09:09:13 -0700777{
Gavin Howard63738202018-09-26 15:34:20 -0600778 BcStatus s;
Gavin Howard80977b22018-09-06 14:29:13 -0600779
Gavin Howard63738202018-09-26 15:34:20 -0600780 assert(width == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600781
Gavin Howard63738202018-09-26 15:34:20 -0600782 if (radix) {
783 if ((s = bc_num_printNewline(nchars, line_len))) return s;
784 if (putchar('.') == EOF) return BC_STATUS_IO_ERR;
785 *nchars += 1;
786 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700787
Gavin Howard63738202018-09-26 15:34:20 -0600788 if ((s = bc_num_printNewline(nchars, line_len))) return s;
789 if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR;
790 *nchars = *nchars + width;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700791
Gavin Howard63738202018-09-26 15:34:20 -0600792 return BC_STATUS_SUCCESS;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700793}
794
Gavin Howard5f46ee32018-09-25 13:02:43 -0600795BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700796
Gavin Howardc39fd492018-10-04 10:07:03 -0600797 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600798 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700799
Gavin Howard63738202018-09-26 15:34:20 -0600800 if (n->neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
801 (*nchars) += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700802
Gavin Howard63738202018-09-26 15:34:20 -0600803 for (i = n->len - 1; !s && i < n->len; --i)
804 s = bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700805
Gavin Howard63738202018-09-26 15:34:20 -0600806 return s;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700807}
808
Gavin Howard83eb8392018-10-09 01:21:19 -0600809BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, size_t *nchars,
810 size_t line_len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -0600811{
Gavin Howard63738202018-09-26 15:34:20 -0600812 BcStatus s;
813 BcVec stack;
814 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -0600815 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -0600816 size_t i;
817 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700818
Gavin Howard83eb8392018-10-09 01:21:19 -0600819 if ((s = bc_vec_init(&stack, sizeof(long), NULL))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600820 if ((s = bc_num_init(&intp, n->len))) goto int_err;
821 if ((s = bc_num_init(&fracp, n->rdx))) goto frac_err;
822 if ((s = bc_num_init(&digit, width))) goto digit_err;
823 if ((s = bc_num_init(&frac_len, BC_NUM_INT(n)))) goto frac_len_err;
824 if ((s = bc_num_copy(&intp, n))) goto err;
825 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -0600826
Gavin Howard63738202018-09-26 15:34:20 -0600827 bc_num_truncate(&intp, intp.rdx);
828 if ((s = bc_num_sub(n, &intp, &fracp, 0))) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700829
Gavin Howard4c091542018-10-09 03:33:08 -0600830 if (!n->len) {
831 s = print(0, width, false, nchars, line_len);
832 goto err;
833 }
834
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 Howard63738202018-09-26 15:34:20 -0600869 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700870}
871
Gavin Howard83eb8392018-10-09 01:21:19 -0600872BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
873 size_t *nchars, size_t line_len)
874{
875 BcStatus s;
876 size_t width, i;
877 BcNumDigitOp print;
878 bool neg = n->neg;
879
880 if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
881 (*nchars) += neg;
882
883 n->neg = false;
884
885 if (base_t <= BC_NUM_MAX_IBASE) {
886 width = 1;
887 print = bc_num_printHex;
888 }
889 else {
890 for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width);
891 print = bc_num_printDigits;
892 }
893
894 s = bc_num_printNum(n, base, width, nchars, line_len, print);
895 n->neg = neg;
896
897 return s;
898}
899
900#ifdef DC_ENABLED
Gavin Howard844e7a52018-10-09 11:46:39 -0600901BcStatus bc_num_stream(BcNum *n, BcNum *base, size_t *nchars, size_t len) {
Gavin Howard83eb8392018-10-09 01:21:19 -0600902 return bc_num_printNum(n, base, 1, nchars, len, bc_num_printChar);
903}
904#endif // DC_ENABLED
905
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700906BcStatus bc_num_init(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700907
Gavin Howard63738202018-09-26 15:34:20 -0600908 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -0700909
Gavin Howard63738202018-09-26 15:34:20 -0600910 request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE;
Gavin Howardc39fd492018-10-04 10:07:03 -0600911 memset(n, 0, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600912 if (!(n->num = malloc(request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -0700913
Gavin Howard63738202018-09-26 15:34:20 -0600914 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700915
Gavin Howard63738202018-09-26 15:34:20 -0600916 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700917}
918
Gavin Howard844e7a52018-10-09 11:46:39 -0600919BcStatus bc_num_expand(BcNum *n, size_t req) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700920
Gavin Howard63738202018-09-26 15:34:20 -0600921 BcDigit *temp;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600922
Gavin Howard844e7a52018-10-09 11:46:39 -0600923 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -0700924
Gavin Howard844e7a52018-10-09 11:46:39 -0600925 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb5c77212018-02-14 17:12:34 -0700926
Gavin Howard844e7a52018-10-09 11:46:39 -0600927 if (req <= n->cap) return BC_STATUS_SUCCESS;
928 if (!(temp = realloc(n->num, req))) return BC_STATUS_ALLOC_ERR;
929
930 memset(temp + n->cap, 0, sizeof(char) * (req - n->cap));
Gavin Howard63738202018-09-26 15:34:20 -0600931 n->num = temp;
Gavin Howard844e7a52018-10-09 11:46:39 -0600932 n->cap = req;
Gavin Howardb5c77212018-02-14 17:12:34 -0700933
Gavin Howard63738202018-09-26 15:34:20 -0600934 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -0700935}
936
Gavin Howarded392aa2018-02-27 13:09:26 -0700937void bc_num_free(void *num) {
Gavin Howard63738202018-09-26 15:34:20 -0600938 BcNum *n = (BcNum*) num;
Gavin Howard8c019982018-10-05 12:35:09 -0600939 assert(n);
Gavin Howard2bc54182018-10-06 17:03:46 -0600940 free(n->num);
Gavin Howardb5c77212018-02-14 17:12:34 -0700941}
942
Gavin Howard9c4358c2018-03-22 20:11:28 -0600943BcStatus bc_num_copy(BcNum *d, BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -0700944
Gavin Howardb20c2db2018-10-08 10:31:23 -0600945 BcStatus status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700946
Gavin Howard63738202018-09-26 15:34:20 -0600947 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -0700948
Gavin Howard63738202018-09-26 15:34:20 -0600949 if (d == s) return BC_STATUS_SUCCESS;
Gavin Howardb20c2db2018-10-08 10:31:23 -0600950 if ((status = bc_num_expand(d, s->cap))) return status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700951
Gavin Howard63738202018-09-26 15:34:20 -0600952 d->len = s->len;
953 d->neg = s->neg;
954 d->rdx = s->rdx;
Gavin Howard5a049c42018-02-15 11:24:11 -0700955
Gavin Howard63738202018-09-26 15:34:20 -0600956 memcpy(d->num, s->num, sizeof(BcDigit) * d->len);
957 memset(d->num + d->len, 0, sizeof(BcDigit) * (d->cap - d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -0700958
Gavin Howardb20c2db2018-10-08 10:31:23 -0600959 return status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700960}
961
Gavin Howard5cea43b2018-03-02 11:32:11 -0700962BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
Gavin Howard025d04d2018-02-20 13:53:28 -0700963
Gavin Howard63738202018-09-26 15:34:20 -0600964 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700965
Gavin Howard63738202018-09-26 15:34:20 -0600966 assert(n && val && base);
967 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_NUM_MAX_IBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700968
Gavin Howard63738202018-09-26 15:34:20 -0600969 if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700970
Gavin Howard63738202018-09-26 15:34:20 -0600971 if (base_t == 10) s = bc_num_parseDecimal(n, val);
972 else s = bc_num_parseBase(n, val, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700973
Gavin Howard63738202018-09-26 15:34:20 -0600974 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700975}
976
Gavin Howard0011a3a2018-03-29 23:11:32 -0600977BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
978 size_t *nchars, size_t line_len)
Gavin Howard152f3e82018-03-07 12:33:15 -0700979{
Gavin Howardc39fd492018-10-04 10:07:03 -0600980 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700981
Gavin Howard63738202018-09-26 15:34:20 -0600982 assert(n && base && nchars);
983 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700984
Gavin Howard63738202018-09-26 15:34:20 -0600985 if (*nchars >= line_len) {
986 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
987 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
988 *nchars = 0;
989 }
Gavin Howardbc7cae82018-03-14 13:43:04 -0600990
Gavin Howard63738202018-09-26 15:34:20 -0600991 if (!n->len) {
992 if (putchar('0') == EOF) return BC_STATUS_IO_ERR;
993 ++(*nchars);
Gavin Howard63738202018-09-26 15:34:20 -0600994 }
995 else if (base_t == 10) s = bc_num_printDecimal(n, nchars, line_len);
996 else s = bc_num_printBase(n, base, base_t, nchars, line_len);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700997
Gavin Howard63738202018-09-26 15:34:20 -0600998 if (s) return s;
Gavin Howard152f3e82018-03-07 12:33:15 -0700999
Gavin Howard63738202018-09-26 15:34:20 -06001000 if (newline) {
1001 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
1002 *nchars = 0;
1003 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001004
Gavin Howard63738202018-09-26 15:34:20 -06001005 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001006}
1007
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001008BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001009
Gavin Howard63738202018-09-26 15:34:20 -06001010 size_t i;
1011 unsigned long pow;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001012
Gavin Howard63738202018-09-26 15:34:20 -06001013 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001014
Gavin Howard63738202018-09-26 15:34:20 -06001015 if (n->neg) return BC_STATUS_MATH_NEGATIVE;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001016
Gavin Howard63738202018-09-26 15:34:20 -06001017 for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001018
Gavin Howard63738202018-09-26 15:34:20 -06001019 unsigned long prev = *result, powprev = pow;
1020 *result += ((unsigned long) n->num[i]) * pow;
1021 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001022
Gavin Howard63738202018-09-26 15:34:20 -06001023 if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW;
1024 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001025
Gavin Howard63738202018-09-26 15:34:20 -06001026 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001027}
1028
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001029BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001030
Gavin Howard63738202018-09-26 15:34:20 -06001031 BcStatus s;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001032 size_t len;
Gavin Howard63738202018-09-26 15:34:20 -06001033 BcDigit *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001034 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001035
Gavin Howard63738202018-09-26 15:34:20 -06001036 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001037
Gavin Howard63738202018-09-26 15:34:20 -06001038 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001039
Gavin Howard63738202018-09-26 15:34:20 -06001040 if (!val) {
1041 memset(n->num, 0, sizeof(char) * n->cap);
1042 return BC_STATUS_SUCCESS;
1043 }
Gavin Howard8e2cc692018-02-15 17:39:14 -07001044
Gavin Howardec82bdc2018-10-08 23:50:57 -06001045 for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len)
Gavin Howard63738202018-09-26 15:34:20 -06001046 if ((s = bc_num_expand(n, len))) return s;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001047 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001048
Gavin Howard63738202018-09-26 15:34:20 -06001049 return BC_STATUS_SUCCESS;
Gavin Howard025d04d2018-02-20 13:53:28 -07001050}
1051
Gavin Howard12fe7812018-09-29 03:45:18 -06001052BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001053 (void) scale;
1054 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s;
Gavin Howard12fe7812018-09-29 03:45:18 -06001055 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001056}
1057
Gavin Howard12fe7812018-09-29 03:45:18 -06001058BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001059 (void) scale;
1060 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a;
Gavin Howard12fe7812018-09-29 03:45:18 -06001061 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001062}
1063
Gavin Howard12fe7812018-09-29 03:45:18 -06001064BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001065 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001066 return bc_num_binary(a, b, c, scale, bc_num_alg_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001067}
1068
Gavin Howard12fe7812018-09-29 03:45:18 -06001069BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001070 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001071 return bc_num_binary(a, b, c, scale, bc_num_alg_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001072}
1073
Gavin Howardbe53edd2018-10-04 10:55:08 -06001074BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001075 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howardbe53edd2018-10-04 10:55:08 -06001076 return bc_num_binary(a, b, c, scale, bc_num_alg_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001077}
1078
Gavin Howard12fe7812018-09-29 03:45:18 -06001079BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
1080 return bc_num_binary(a, b, c, scale, bc_num_alg_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001081}
1082
Gavin Howard757b66a2018-10-06 04:13:46 -06001083BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001084
Gavin Howard63738202018-09-26 15:34:20 -06001085 BcStatus s;
1086 BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001087 size_t pow, len, digits, digits1, resrdx, req, times = 0;
1088 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard954276b2018-05-16 01:43:58 -06001089
Gavin Howard757b66a2018-10-06 04:13:46 -06001090 assert(a && b);
Gavin Howard954276b2018-05-16 01:43:58 -06001091
Gavin Howard63738202018-09-26 15:34:20 -06001092 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a)+ 1) >> 1) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001093
Gavin Howard757b66a2018-10-06 04:13:46 -06001094 if (b == a) {
1095 memcpy(&a2, b, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001096 ptr_a = &a2;
Gavin Howard757b66a2018-10-06 04:13:46 -06001097 s = bc_num_init(b, req);
Gavin Howard63738202018-09-26 15:34:20 -06001098 }
1099 else {
1100 ptr_a = a;
Gavin Howard757b66a2018-10-06 04:13:46 -06001101 s = bc_num_expand(b, req);
Gavin Howard63738202018-09-26 15:34:20 -06001102 }
Gavin Howard954276b2018-05-16 01:43:58 -06001103
Gavin Howard63738202018-09-26 15:34:20 -06001104 if (s) goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001105
Gavin Howard63738202018-09-26 15:34:20 -06001106 if (!ptr_a->len) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001107 bc_num_zero(b);
Gavin Howard63738202018-09-26 15:34:20 -06001108 goto init_err;
1109 }
1110 else if (ptr_a->neg) {
1111 s = BC_STATUS_MATH_NEG_SQRT;
1112 goto init_err;
1113 }
1114 else if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001115 bc_num_one(b);
1116 s = bc_num_extend(b, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001117 goto init_err;
1118 }
Gavin Howard954276b2018-05-16 01:43:58 -06001119
Gavin Howard757b66a2018-10-06 04:13:46 -06001120 memset(b->num, 0, b->cap * sizeof(BcDigit));
Gavin Howard63738202018-09-26 15:34:20 -06001121 len = ptr_a->len;
Gavin Howard954276b2018-05-16 01:43:58 -06001122
Gavin Howard63738202018-09-26 15:34:20 -06001123 scale = BC_MAX(scale, ptr_a->rdx) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001124
Gavin Howard63738202018-09-26 15:34:20 -06001125 if ((s = bc_num_init(&num1, len))) goto init_err;
1126 if ((s = bc_num_init(&num2, num1.len))) goto num2_err;
1127 if ((s = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001128
Gavin Howard63738202018-09-26 15:34:20 -06001129 bc_num_one(&half);
1130 half.num[0] = 5;
1131 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001132
Gavin Howard63738202018-09-26 15:34:20 -06001133 len += scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001134
Gavin Howard63738202018-09-26 15:34:20 -06001135 if ((s = bc_num_init(&f, len))) goto f_err;
1136 if ((s = bc_num_init(&fprime, len + scale))) goto fprime_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001137
Gavin Howard63738202018-09-26 15:34:20 -06001138 x0 = &num1;
1139 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001140
Gavin Howard63738202018-09-26 15:34:20 -06001141 bc_num_one(x0);
Gavin Howard954276b2018-05-16 01:43:58 -06001142
Gavin Howardc39fd492018-10-04 10:07:03 -06001143 if ((pow = BC_NUM_INT(ptr_a))) {
Gavin Howard954276b2018-05-16 01:43:58 -06001144
Gavin Howardc39fd492018-10-04 10:07:03 -06001145 if (pow & 1) x0->num[0] = 2;
1146 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001147
Gavin Howardc39fd492018-10-04 10:07:03 -06001148 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001149
Gavin Howard63738202018-09-26 15:34:20 -06001150 if ((s = bc_num_extend(x0, pow))) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06001151
Gavin Howard63738202018-09-26 15:34:20 -06001152 // Make sure to move the radix back.
1153 x0->rdx -= pow;
1154 }
Gavin Howard954276b2018-05-16 01:43:58 -06001155
Gavin Howard9f2395b2018-10-06 05:28:58 -06001156 x0->rdx = digits = digits1 = 0;
1157 resrdx = scale + 2;
1158 len = BC_NUM_INT(x0) + resrdx - 1;
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 Howardb11e9e22018-10-06 17:26:02 -06001169 if (cmp == cmp2 && digits == digits1) times += 1;
1170 else times = 0;
1171
1172 resrdx += times > 4;
1173 len += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001174
1175 cmp2 = cmp1;
1176 cmp1 = cmp;
1177 digits1 = digits;
1178
Gavin Howard63738202018-09-26 15:34:20 -06001179 temp = x0;
1180 x0 = x1;
1181 x1 = temp;
1182 }
Gavin Howard954276b2018-05-16 01:43:58 -06001183
Gavin Howard63738202018-09-26 15:34:20 -06001184 if (bcg.signe) {
1185 s = BC_STATUS_EXEC_SIGNAL;
1186 goto err;
1187 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001188
Gavin Howard757b66a2018-10-06 04:13:46 -06001189 if ((s = bc_num_copy(b, x0))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001190
Gavin Howard757b66a2018-10-06 04:13:46 -06001191 if (b->rdx > --scale) bc_num_truncate(b, b->rdx - scale);
1192 else if (b->rdx < scale) s = bc_num_extend(b, scale - b->rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06001193
1194err:
Gavin Howard63738202018-09-26 15:34:20 -06001195 bc_num_free(&fprime);
Gavin Howard954276b2018-05-16 01:43:58 -06001196fprime_err:
Gavin Howard63738202018-09-26 15:34:20 -06001197 bc_num_free(&f);
Gavin Howard954276b2018-05-16 01:43:58 -06001198f_err:
Gavin Howard63738202018-09-26 15:34:20 -06001199 bc_num_free(&half);
Gavin Howard954276b2018-05-16 01:43:58 -06001200two_err:
Gavin Howard63738202018-09-26 15:34:20 -06001201 bc_num_free(&num2);
Gavin Howard954276b2018-05-16 01:43:58 -06001202num2_err:
Gavin Howard63738202018-09-26 15:34:20 -06001203 bc_num_free(&num1);
Gavin Howard954276b2018-05-16 01:43:58 -06001204init_err:
Gavin Howard757b66a2018-10-06 04:13:46 -06001205 if (b == a) bc_num_free(&a2);
Gavin Howard63738202018-09-26 15:34:20 -06001206 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001207}
Gavin Howardba009802018-09-29 04:41:51 -06001208
Gavin Howarde6e84762018-10-03 11:46:34 -06001209#ifdef DC_ENABLED
Gavin Howardba009802018-09-29 04:41:51 -06001210BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1211
1212 BcStatus s;
Gavin Howard2c8959e2018-10-04 18:32:58 -06001213 BcNum num2, *ptr_a, *ptr_b, *ptr_c, base, exp, two, temp;
Gavin Howardba009802018-09-29 04:41:51 -06001214 bool init = false;
1215
1216 assert(a && b && c && d);
1217
1218 scale = 0;
1219
1220 if (d == a) {
1221 memcpy(&num2, d, sizeof(BcNum));
1222 ptr_a = &num2;
1223 init = true;
1224 }
1225 else ptr_a = a;
1226
1227 if (d == b) {
1228
1229 ptr_b = &num2;
1230
1231 if (d != a) {
1232 memcpy(ptr_b, d, sizeof(BcNum));
1233 init = true;
1234 }
1235 }
1236 else ptr_b = b;
1237
1238 if (d == c) {
1239
1240 ptr_c = &num2;
1241
1242 if (d != a && d != b) {
1243 memcpy(ptr_c, d, sizeof(BcNum));
1244 init = true;
1245 }
1246 }
1247 else ptr_c = c;
1248
1249 if (init) s = bc_num_init(d, ptr_c->len);
1250 else s = bc_num_expand(d, ptr_c->len);
1251
1252 if (s) return s;
1253
1254 if (!ptr_c->len) {
1255 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
1256 goto base_err;
1257 }
1258
Gavin Howard5ef610b2018-10-04 16:57:47 -06001259 if (ptr_a->rdx || ptr_b->rdx || ptr_c->rdx) {
Gavin Howardba009802018-09-29 04:41:51 -06001260 s = BC_STATUS_MATH_NON_INTEGER;
1261 goto base_err;
1262 }
1263
1264 if (ptr_b->neg) {
1265 s = BC_STATUS_MATH_NEGATIVE;
1266 goto base_err;
1267 }
1268
Gavin Howardba009802018-09-29 04:41:51 -06001269 if ((s = bc_num_init(&base, ptr_c->len))) goto base_err;
1270 if ((s = bc_num_init(&exp, ptr_b->len))) goto exp_err;
1271 if ((s = bc_num_init(&two, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howardba009802018-09-29 04:41:51 -06001272 if ((s = bc_num_init(&temp, ptr_b->len))) goto temp_err;
1273
1274 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001275 two.num[0] = 2;
1276 bc_num_one(d);
1277
Gavin Howardbe53edd2018-10-04 10:55:08 -06001278 if ((s = bc_num_rem(ptr_a, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001279 if ((s = bc_num_copy(&exp, ptr_b))) goto err;
1280
Gavin Howardaf905602018-09-29 18:56:37 -06001281 while (exp.len) {
Gavin Howardba009802018-09-29 04:41:51 -06001282
Gavin Howardbe53edd2018-10-04 10:55:08 -06001283 if ((s = bc_num_rem(&exp, &two, &temp, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001284
1285 if (BC_NUM_ONE(&temp)) {
1286 if ((s = bc_num_mul(d, &base, &temp, scale))) goto err;
Gavin Howardbe53edd2018-10-04 10:55:08 -06001287 if ((s = bc_num_rem(&temp, ptr_c, d, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001288 }
1289
1290 if ((s = bc_num_div(&exp, &two, &exp, scale))) goto err;
1291 if ((s = bc_num_mul(&base, &base, &temp, scale))) goto err;
Gavin Howardbe53edd2018-10-04 10:55:08 -06001292 if ((s = bc_num_rem(&temp, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001293 }
1294
1295err:
1296 bc_num_free(&temp);
1297temp_err:
Gavin Howardba009802018-09-29 04:41:51 -06001298 bc_num_free(&two);
1299two_err:
1300 bc_num_free(&exp);
1301exp_err:
1302 bc_num_free(&base);
1303base_err:
1304 if (init) bc_num_free(&num2);
1305 return s;
1306}
Gavin Howarde6e84762018-10-03 11:46:34 -06001307#endif // DC_ENABLED