blob: 1dc8ae3935623216c69082dbead201c091a47b73 [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 Howard305249a2018-10-15 20:24:47 -060028#include <limits.h>
29
Gavin Howard29493062018-03-20 19:57:37 -060030#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070031#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060032#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070033
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060034static void bc_num_setToZero(BcNum *n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060035 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060036 n->neg = (n->len = 0);
37 n->rdx = scale;
38}
39
40void bc_num_zero(BcNum *n) {
41 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060042}
43
44void bc_num_one(BcNum *n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060045 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060046 n->len = 1;
47 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060048}
49
50void bc_num_ten(BcNum *n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060051 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060052 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060053 n->len = 2;
54 n->num[0] = 0;
55 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060056}
57
Gavin Howard8a921bd2018-10-11 14:15:32 -060058BcStatus bc_num_subArrays(BcDig *restrict n1, BcDig *restrict n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060059 size_t i, j;
60 for (i = 0; !bcg.signe && i < len; ++i) {
61 for (n1[i] -= n2[i], j = 0; !bcg.signe && n1[i + j] < 0;) {
62 n1[i + j++] += 10;
63 n1[i + j] -= 1;
64 }
65 }
66 return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060067}
68
Gavin Howard8a921bd2018-10-11 14:15:32 -060069ssize_t bc_num_compare(BcDig *restrict n1, BcDig *restrict n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060070 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -060071 int c = 0;
Gavin Howard63738202018-09-26 15:34:20 -060072 for (i = len - 1; !bcg.signe && i < len && !(c = n1[i] - n2[i]); --i);
Gavin Howard7fbb4e22018-10-18 11:43:17 -060073 return BC_NUM_NEG(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -060074}
75
Gavin Howard6a804cf2018-05-17 16:54:12 -060076ssize_t bc_num_cmp(BcNum *a, BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070077
Gavin Howard63738202018-09-26 15:34:20 -060078 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -060079 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060080 bool a_max, neg = false;
81 ssize_t cmp;
82
83 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070084
Gavin Howard8a921bd2018-10-11 14:15:32 -060085 if (a == b) return 0;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060086 else if (!a->len) return BC_NUM_NEG(!!b->len, !b->neg);
87 else if (!b->len) return BC_NUM_NEG(1, a->neg);
88 else if (a->neg) {
89 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -060090 else return -1;
91 }
92 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070093
Gavin Howard63738202018-09-26 15:34:20 -060094 a_int = BC_NUM_INT(a);
95 b_int = BC_NUM_INT(b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070096
Gavin Howard7fbb4e22018-10-18 11:43:17 -060097 if ((a_int -= b_int)) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -070098
Gavin Howard7fbb4e22018-10-18 11:43:17 -060099 if ((a_max = a->rdx > b->rdx)) {
Gavin Howard63738202018-09-26 15:34:20 -0600100 min = b->rdx;
101 diff = a->rdx - b->rdx;
102 max_num = a->num + diff;
103 min_num = b->num;
104 }
105 else {
106 min = a->rdx;
107 diff = b->rdx - a->rdx;
108 max_num = b->num + diff;
109 min_num = a->num;
110 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700111
Gavin Howard63738202018-09-26 15:34:20 -0600112 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard40974152018-10-18 14:23:01 -0600113 if (cmp) return BC_NUM_NEG(cmp, (!a_max) != neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700114
Gavin Howard63738202018-09-26 15:34:20 -0600115 for (max_num -= diff, i = diff - 1; !bcg.signe && i < diff; --i) {
Gavin Howard40974152018-10-18 14:23:01 -0600116 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
Gavin Howard63738202018-09-26 15:34:20 -0600117 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700118
Gavin Howard63738202018-09-26 15:34:20 -0600119 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700120}
121
Gavin Howardd9734e52018-03-29 15:54:41 -0600122void bc_num_truncate(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700123
Gavin Howard305249a2018-10-15 20:24:47 -0600124 assert(places <= n->rdx && places <= n->len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700125
Gavin Howard63738202018-09-26 15:34:20 -0600126 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700127
Gavin Howard63738202018-09-26 15:34:20 -0600128 n->len -= places;
129 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700130
Gavin Howardd32d7df2018-10-15 08:42:25 -0600131 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700132}
133
Gavin Howard3f68df72018-03-22 20:30:27 -0600134BcStatus bc_num_extend(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700135
Gavin Howard63738202018-09-26 15:34:20 -0600136 BcStatus s;
Gavin Howard88c25302018-10-17 13:32:23 -0600137 size_t len = n->len + places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700138
Gavin Howard63738202018-09-26 15:34:20 -0600139 if (!places) return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600140 if (n->cap < len && (s = bc_num_expand(n, len))) return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700141
Gavin Howardd32d7df2018-10-15 08:42:25 -0600142 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
Gavin Howard8a921bd2018-10-11 14:15:32 -0600143 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700144
Gavin Howard63738202018-09-26 15:34:20 -0600145 n->len += places;
146 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700147
Gavin Howard63738202018-09-26 15:34:20 -0600148 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700149}
150
Gavin Howardc0f65d72018-09-01 16:59:04 -0600151void bc_num_clean(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -0600152 while (n->len > 0 && !n->num[n->len - 1]) --n->len;
Gavin Howard549d2072018-10-22 08:38:09 -0600153 if (!n->len) n->neg = false;
Gavin Howard63738202018-09-26 15:34:20 -0600154 else if (n->len < n->rdx) n->len = n->rdx;
Gavin Howarde57525d2018-09-01 16:56:21 -0600155}
156
Gavin Howardd32d7df2018-10-15 08:42:25 -0600157BcStatus bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600158
Gavin Howard63738202018-09-26 15:34:20 -0600159 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600160
Gavin Howard63738202018-09-26 15:34:20 -0600161 if (n->rdx < scale) s = bc_num_extend(n, scale - n->rdx);
162 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600163
Gavin Howard305249a2018-10-15 20:24:47 -0600164 bc_num_clean(n);
Gavin Howard549d2072018-10-22 08:38:09 -0600165 if (n->len) n->neg = !neg1 != !neg2;
Gavin Howard305249a2018-10-15 20:24:47 -0600166
167 return s;
168}
169
170BcStatus bc_num_splitAt(BcNum *restrict n, size_t idx, BcNum *restrict a,
Gavin Howardb5093d72018-10-17 12:13:17 -0600171 BcNum *restrict b)
Gavin Howard305249a2018-10-15 20:24:47 -0600172{
173 BcStatus s = BC_STATUS_SUCCESS;
174
175 if (idx < n->len) {
176
Gavin Howard305249a2018-10-15 20:24:47 -0600177 b->len = n->len - idx;
178 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600179 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600180
181 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
182 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard305249a2018-10-15 20:24:47 -0600183 }
184 else {
185 bc_num_zero(b);
186 s = bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600187 }
188
189 bc_num_clean(a);
190 bc_num_clean(b);
191
192 return s;
193}
194
195BcStatus bc_num_shift(BcNum *n, size_t places) {
196
197 BcStatus s = BC_STATUS_SUCCESS;
198
199 if (!places) return BC_STATUS_SUCCESS;
200 if (places + n->len > BC_MAX_NUM) return BC_STATUS_EXEC_NUM_LEN;
201
202 if (n->rdx >= places) n->rdx -= places;
203 else {
204 s = bc_num_extend(n, places - n->rdx);
205 n->rdx = 0;
206 }
207
208 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600209
Gavin Howard63738202018-09-26 15:34:20 -0600210 return s;
Gavin Howard337fe602018-09-01 15:10:59 -0600211}
212
Gavin Howard9c4358c2018-03-22 20:11:28 -0600213BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
214
Gavin Howard63738202018-09-26 15:34:20 -0600215 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600216 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600217
Gavin Howardd32d7df2018-10-15 08:42:25 -0600218 one.cap = 2;
219 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600220 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600221
Gavin Howardd32d7df2018-10-15 08:42:25 -0600222 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600223}
224
Gavin Howard8a921bd2018-10-11 14:15:32 -0600225BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700226
Gavin Howard8a921bd2018-10-11 14:15:32 -0600227 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600228 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard60136d72018-10-15 14:28:19 -0600229 int carry, in;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700230
Gavin Howard63738202018-09-26 15:34:20 -0600231 // Because this function doesn't need to use scale (per the bc spec),
232 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600233
Gavin Howard63738202018-09-26 15:34:20 -0600234 if (!a->len) {
235 BcStatus s = bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600236 if (sub && c->len) c->neg = !c->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600237 return s;
238 }
239 else if (!b->len) return bc_num_copy(c, a);
Gavin Howardf6964a12018-03-14 10:52:31 -0600240
Gavin Howard63738202018-09-26 15:34:20 -0600241 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600242 c->rdx = BC_MAX(a->rdx, b->rdx);
243 min_rdx = BC_MIN(a->rdx, b->rdx);
244 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700245
Gavin Howard63738202018-09-26 15:34:20 -0600246 if (a->rdx > b->rdx) {
247 diff = a->rdx - b->rdx;
248 ptr = a->num;
249 ptr_a = a->num + diff;
250 ptr_b = b->num;
251 }
252 else {
253 diff = b->rdx - a->rdx;
254 ptr = b->num;
255 ptr_a = a->num;
256 ptr_b = b->num + diff;
257 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700258
Gavin Howard63738202018-09-26 15:34:20 -0600259 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700260
Gavin Howard63738202018-09-26 15:34:20 -0600261 ptr_c += diff;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700262
Gavin Howard8ae3bf52018-10-19 13:30:17 -0600263 if ((a_int = BC_NUM_INT(a)) > (b_int = BC_NUM_INT(b))) {
Gavin Howard63738202018-09-26 15:34:20 -0600264 min_int = b_int;
265 max = a_int;
266 ptr = ptr_a;
267 }
268 else {
269 min_int = a_int;
270 max = b_int;
271 ptr = ptr_b;
272 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700273
Gavin Howard63738202018-09-26 15:34:20 -0600274 for (carry = 0, i = 0; !bcg.signe && i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard60136d72018-10-15 14:28:19 -0600275 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
276 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600277 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600278 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700279
Gavin Howard63738202018-09-26 15:34:20 -0600280 for (; !bcg.signe && i < max + min_rdx; ++i, ++c->len) {
Gavin Howard11b9afd2018-10-18 14:23:28 -0600281 in = ((int) ptr[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600282 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600283 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600284 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700285
Gavin Howard63738202018-09-26 15:34:20 -0600286 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600287
Gavin Howard60136d72018-10-15 14:28:19 -0600288 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700289
Gavin Howard63738202018-09-26 15:34:20 -0600290 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700291}
292
Gavin Howard8a921bd2018-10-11 14:15:32 -0600293BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700294
Gavin Howard63738202018-09-26 15:34:20 -0600295 BcStatus s;
296 ssize_t cmp;
297 BcNum *minuend, *subtrahend;
298 size_t start;
299 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700300
Gavin Howard63738202018-09-26 15:34:20 -0600301 // Because this function doesn't need to use scale (per the bc spec),
302 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700303
Gavin Howard63738202018-09-26 15:34:20 -0600304 if (!a->len) {
305 s = bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600306 if (sub && c->len) c->neg = !c->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600307 return s;
308 }
309 else if (!b->len) return bc_num_copy(c, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700310
Gavin Howard63738202018-09-26 15:34:20 -0600311 aneg = a->neg;
312 bneg = b->neg;
313 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700314
Gavin Howard63738202018-09-26 15:34:20 -0600315 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700316
Gavin Howard63738202018-09-26 15:34:20 -0600317 a->neg = aneg;
318 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700319
Gavin Howard63738202018-09-26 15:34:20 -0600320 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600321 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600322 return BC_STATUS_SUCCESS;
323 }
324 else if (cmp > 0) {
325 neg = a->neg;
326 minuend = a;
327 subtrahend = b;
328 }
329 else {
330 neg = b->neg;
331 if (sub) neg = !neg;
332 minuend = b;
333 subtrahend = a;
334 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700335
Gavin Howard63738202018-09-26 15:34:20 -0600336 if ((s = bc_num_copy(c, minuend))) return s;
337 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700338
Gavin Howard63738202018-09-26 15:34:20 -0600339 if (c->rdx < subtrahend->rdx) {
340 if ((s = bc_num_extend(c, subtrahend->rdx - c->rdx))) return s;
341 start = 0;
342 }
343 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700344
Gavin Howard63738202018-09-26 15:34:20 -0600345 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700346
Gavin Howard63738202018-09-26 15:34:20 -0600347 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700348
Gavin Howard63738202018-09-26 15:34:20 -0600349 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700350}
351
Gavin Howardb5093d72018-10-17 12:13:17 -0600352BcStatus bc_num_alg_k(BcNum *restrict a, BcNum *restrict b, BcNum *restrict c) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700353
Gavin Howard63738202018-09-26 15:34:20 -0600354 BcStatus s;
Gavin Howard60136d72018-10-15 14:28:19 -0600355 int carry;
Gavin Howard305249a2018-10-15 20:24:47 -0600356 size_t i, j, len, max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600357 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard63738202018-09-26 15:34:20 -0600358 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600359
Gavin Howardb5093d72018-10-17 12:13:17 -0600360 if (!a->len || !b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600361 bc_num_zero(c);
362 return BC_STATUS_SUCCESS;
363 }
Gavin Howard23d8d252018-10-17 12:03:41 -0600364 else if (aone || BC_NUM_ONE(b)) return bc_num_copy(c, aone ? b : a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700365
Gavin Howardb5093d72018-10-17 12:13:17 -0600366 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
367 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600368 {
Gavin Howard23d8d252018-10-17 12:03:41 -0600369 if ((s = bc_num_expand(c, a->len + b->len + 1))) return s;
370
Gavin Howard305249a2018-10-15 20:24:47 -0600371 memset(c->num, 0, sizeof(BcDig) * c->cap);
372 c->len = carry = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700373
Gavin Howard305249a2018-10-15 20:24:47 -0600374 for (i = 0; !bcg.signe && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600375
Gavin Howard305249a2018-10-15 20:24:47 -0600376 for (j = 0; !bcg.signe && j < a->len; ++j) {
Gavin Howard23d8d252018-10-17 12:03:41 -0600377 int in = (int) c->num[i + j];
Gavin Howard305249a2018-10-15 20:24:47 -0600378 in += ((int) a->num[j]) * ((int) b->num[i]) + carry;
379 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600380 c->num[i + j] = (BcDig) (in % 10);
Gavin Howard305249a2018-10-15 20:24:47 -0600381 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700382
Gavin Howard305249a2018-10-15 20:24:47 -0600383 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
384
Gavin Howard88c25302018-10-17 13:32:23 -0600385 c->num[i + j] += (BcDig) carry;
386 len = BC_MAX(len, i + j + !!carry);
387 carry = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600388 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700389
Gavin Howard305249a2018-10-15 20:24:47 -0600390 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600391
Gavin Howardd1a78362018-10-18 09:52:06 -0600392 return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600393 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700394
Gavin Howard305249a2018-10-15 20:24:47 -0600395 if ((s = bc_num_init(&l1, max))) return s;
396 if ((s = bc_num_init(&h1, max))) goto high1_err;
397 if ((s = bc_num_init(&l2, max))) goto low2_err;
398 if ((s = bc_num_init(&h2, max))) goto high2_err;
399 if ((s = bc_num_init(&m1, max))) goto mix1_err;
400 if ((s = bc_num_init(&m2, max))) goto mix2_err;
401 if ((s = bc_num_init(&z0, max))) goto z0_err;
402 if ((s = bc_num_init(&z1, max))) goto z1_err;
403 if ((s = bc_num_init(&z2, max))) goto z2_err;
404 if ((s = bc_num_init(&temp, max + max))) goto temp_err;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600405
Gavin Howardb5093d72018-10-17 12:13:17 -0600406 if ((s = bc_num_splitAt(a, max2, &l1, &h1))) goto err;
407 if ((s = bc_num_splitAt(b, max2, &l2, &h2))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700408
Gavin Howardb5093d72018-10-17 12:13:17 -0600409 if ((s = bc_num_add(&h1, &l1, &m1, 0))) goto err;
410 if ((s = bc_num_add(&h2, &l2, &m2, 0))) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600411
Gavin Howardb5093d72018-10-17 12:13:17 -0600412 if ((s = bc_num_alg_k(&h1, &h2, &z0))) goto err;
413 if ((s = bc_num_alg_k(&m1, &m2, &z1))) goto err;
414 if ((s = bc_num_alg_k(&l1, &l2, &z2))) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600415
Gavin Howardb5093d72018-10-17 12:13:17 -0600416 if ((s = bc_num_sub(&z1, &z0, &temp, 0))) goto err;
417 if ((s = bc_num_sub(&temp, &z2, &z1, 0))) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600418
419 if ((s = bc_num_shift(&z0, max2 * 2))) goto err;
420 if ((s = bc_num_shift(&z1, max2))) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600421 if ((s = bc_num_add(&z0, &z1, &temp, 0))) goto err;
422 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600423
424err:
425 bc_num_free(&temp);
426temp_err:
427 bc_num_free(&z2);
428z2_err:
429 bc_num_free(&z1);
430z1_err:
431 bc_num_free(&z0);
432z0_err:
433 bc_num_free(&m2);
434mix2_err:
435 bc_num_free(&m1);
436mix1_err:
437 bc_num_free(&h2);
438high2_err:
439 bc_num_free(&l2);
440low2_err:
441 bc_num_free(&h1);
442high1_err:
443 bc_num_free(&l1);
444 return s;
445}
446
447BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
448
449 BcStatus s;
450 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600451 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600452
453 scale = BC_MAX(scale, a->rdx);
454 scale = BC_MAX(scale, b->rdx);
455 scale = BC_MIN(a->rdx + b->rdx, scale);
456 maxrdx = BC_MAX(maxrdx, scale);
457
458 if ((s = bc_num_init(&cpa, a->len))) return s;
459 if ((s = bc_num_init(&cpb, b->len))) goto b_err;
460
461 if ((s = bc_num_copy(&cpa, a))) goto err;
462 if ((s = bc_num_copy(&cpb, b))) goto err;
Gavin Howard23d8d252018-10-17 12:03:41 -0600463 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600464 if ((s = bc_num_shift(&cpa, maxrdx))) goto err;
465 if ((s = bc_num_shift(&cpb, maxrdx))) goto err;
466
Gavin Howardb5093d72018-10-17 12:13:17 -0600467 if ((s = bc_num_alg_k(&cpa, &cpb, c))) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600468
Gavin Howard88c25302018-10-17 13:32:23 -0600469 if ((s = bc_num_expand(c, c->len + (maxrdx += scale)))) goto err;
470 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600471 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600472 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600473 }
474
Gavin Howard88c25302018-10-17 13:32:23 -0600475 c->rdx = maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600476 s = bc_num_retireMul(c, scale, a->neg, b->neg);
477
478err:
479 bc_num_free(&cpb);
480b_err:
481 bc_num_free(&cpa);
482 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700483}
484
Gavin Howard8a921bd2018-10-11 14:15:32 -0600485BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700486
Gavin Howard63738202018-09-26 15:34:20 -0600487 BcStatus s;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600488 BcDig *n, *bptr, q;
Gavin Howard63738202018-09-26 15:34:20 -0600489 size_t len, end, i;
490 BcNum cp;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700491
Gavin Howard63738202018-09-26 15:34:20 -0600492 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
493 else if (!a->len) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600494 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600495 return BC_STATUS_SUCCESS;
496 }
497 else if (BC_NUM_ONE(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600498 if ((s = bc_num_copy(c, a))) return s;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600499 return bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600500 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700501
Gavin Howard63738202018-09-26 15:34:20 -0600502 if ((s = bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)))) return s;
503 if ((s = bc_num_copy(&cp, a))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700504
Gavin Howard63738202018-09-26 15:34:20 -0600505 if ((len = b->len) > cp.len) {
506 if ((s = bc_num_expand(&cp, len + 2))) goto err;
507 if ((s = bc_num_extend(&cp, len - cp.len))) goto err;
508 }
Gavin Howard021150b2018-03-10 15:40:42 -0700509
Gavin Howard50651542018-09-29 02:53:11 -0600510 if (b->rdx > cp.rdx && (s = bc_num_extend(&cp, b->rdx - cp.rdx))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600511 cp.rdx -= b->rdx;
Gavin Howard50651542018-09-29 02:53:11 -0600512 if (scale > cp.rdx && (s = bc_num_extend(&cp, scale - cp.rdx))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700513
Gavin Howard63738202018-09-26 15:34:20 -0600514 if (b->rdx == b->len) {
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600515
Gavin Howard63738202018-09-26 15:34:20 -0600516 bool zero = true;
517 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600518
Gavin Howard63738202018-09-26 15:34:20 -0600519 if (i == len && zero) {
520 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
521 goto err;
522 }
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600523
Gavin Howard63738202018-09-26 15:34:20 -0600524 len -= i - 1;
525 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700526
Gavin Howard63738202018-09-26 15:34:20 -0600527 if (cp.cap == cp.len && (s = bc_num_expand(&cp, cp.len + 1))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700528
Gavin Howard63738202018-09-26 15:34:20 -0600529 // We want an extra zero in front to make things simpler.
530 cp.num[cp.len++] = 0;
531 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700532
Gavin Howard63738202018-09-26 15:34:20 -0600533 if ((s = bc_num_expand(c, cp.len))) goto err;
Gavin Howardac7656d2018-03-01 17:18:40 -0700534
Gavin Howard63738202018-09-26 15:34:20 -0600535 bc_num_zero(c);
Gavin Howarda2514a02018-10-18 14:20:09 -0600536 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600537 c->rdx = cp.rdx;
538 c->len = cp.len;
539 bptr = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700540
Gavin Howardc39fd492018-10-04 10:07:03 -0600541 for (i = end - 1; !bcg.signe && !s && i < end; --i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700542
Gavin Howard63738202018-09-26 15:34:20 -0600543 n = cp.num + i;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700544
Gavin Howard63738202018-09-26 15:34:20 -0600545 for (q = 0; (!s && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q)
546 s = bc_num_subArrays(n, bptr, len);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700547
Gavin Howard63738202018-09-26 15:34:20 -0600548 c->num[i] = q;
549 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700550
Gavin Howardd32d7df2018-10-15 08:42:25 -0600551 if (!s) s = bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600552
Gavin Howardb651f1a2018-02-28 17:34:18 -0700553err:
Gavin Howard63738202018-09-26 15:34:20 -0600554 bc_num_free(&cp);
555 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700556}
557
Gavin Howard18441082018-10-22 10:03:30 -0600558BcStatus bc_num_alg_r(BcNum *a, BcNum *b, BcNum *restrict c, BcNum *restrict d,
559 size_t scale, size_t ts)
560{
Gavin Howard63738202018-09-26 15:34:20 -0600561 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600562 BcNum temp;
563 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700564
Gavin Howard63738202018-09-26 15:34:20 -0600565 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
Gavin Howard8b254872018-03-14 01:13:35 -0600566
Gavin Howard63738202018-09-26 15:34:20 -0600567 if (!a->len) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600568 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600569 return BC_STATUS_SUCCESS;
570 }
Gavin Howard8b254872018-03-14 01:13:35 -0600571
Gavin Howard18441082018-10-22 10:03:30 -0600572 if ((s = bc_num_init(&temp, d->cap))) return s;
573
574 if ((s = bc_num_alg_d(a, b, c, scale))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700575
Gavin Howard841a3882018-10-04 11:28:14 -0600576 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600577
Gavin Howard18441082018-10-22 10:03:30 -0600578 if ((s = bc_num_alg_m(c, b, &temp, scale))) goto err;
579 if ((s = bc_num_sub(a, &temp, d, scale))) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600580
Gavin Howard18441082018-10-22 10:03:30 -0600581 if (ts > d->rdx && d->len && (s = bc_num_extend(d, ts - d->rdx))) goto err;
582
583 neg = d->neg;
584 s = bc_num_retireMul(d, ts, a->neg, b->neg);
585 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700586
587err:
Gavin Howard18441082018-10-22 10:03:30 -0600588 bc_num_free(&temp);
589 return s;
590}
591
592BcStatus bc_num_alg_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
593
594 BcStatus s;
595 BcNum c1;
596 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
597
598 if ((s = bc_num_init(&c1, len))) return s;
599
600 s = bc_num_alg_r(a, b, &c1, c, scale, ts);
601
Gavin Howard63738202018-09-26 15:34:20 -0600602 bc_num_free(&c1);
603 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700604}
605
Gavin Howard8a921bd2018-10-11 14:15:32 -0600606BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700607
Gavin Howard63738202018-09-26 15:34:20 -0600608 BcStatus s;
609 BcNum copy;
610 unsigned long pow;
611 size_t i, powrdx, resrdx;
612 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700613
Gavin Howard63738202018-09-26 15:34:20 -0600614 if (b->rdx) return BC_STATUS_MATH_NON_INTEGER;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700615
Gavin Howard63738202018-09-26 15:34:20 -0600616 if (!b->len) {
617 bc_num_one(c);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600618 return bc_num_extend(c, a->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600619 }
620 else if (!a->len) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600621 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600622 return BC_STATUS_SUCCESS;
623 }
624 else if (BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700625
Gavin Howard63738202018-09-26 15:34:20 -0600626 if (!b->neg) s = bc_num_copy(c, a);
627 else s = bc_num_inv(a, c, scale);
Gavin Howard5fc40e72018-03-05 12:11:09 -0700628
Gavin Howard63738202018-09-26 15:34:20 -0600629 return s;
630 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700631
Gavin Howard63738202018-09-26 15:34:20 -0600632 neg = b->neg;
633 b->neg = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700634
Gavin Howard63738202018-09-26 15:34:20 -0600635 if ((s = bc_num_ulong(b, &pow))) return s;
636 if ((s = bc_num_init(&copy, a->len))) return s;
637 if ((s = bc_num_copy(&copy, a))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700638
Gavin Howard63738202018-09-26 15:34:20 -0600639 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600640
Gavin Howard63738202018-09-26 15:34:20 -0600641 b->neg = neg;
Gavin Howardb29674f2018-03-22 22:24:58 -0600642
Gavin Howard63738202018-09-26 15:34:20 -0600643 for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) {
644 powrdx <<= 1;
645 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
646 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700647
Gavin Howard63738202018-09-26 15:34:20 -0600648 if ((s = bc_num_copy(c, &copy))) goto err;
649 if (bcg.signe) {
650 s = BC_STATUS_EXEC_SIGNAL;
651 goto err;
652 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700653
Gavin Howardc39fd492018-10-04 10:07:03 -0600654 for (resrdx = powrdx, pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) {
Gavin Howard88c25302018-10-17 13:32:23 -0600655 if ((s = bc_num_mul(&copy, &copy, &copy, (powrdx <<= 1)))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600656 if (pow & 1) {
657 resrdx += powrdx;
658 if ((s = bc_num_mul(c, &copy, c, resrdx))) goto err;
659 }
660 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700661
Gavin Howard63738202018-09-26 15:34:20 -0600662 if (neg && (s = bc_num_inv(c, c, scale))) goto err;
663 if (bcg.signe) {
664 s = BC_STATUS_EXEC_SIGNAL;
665 goto err;
666 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700667
Gavin Howard63738202018-09-26 15:34:20 -0600668 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700669
Gavin Howard63738202018-09-26 15:34:20 -0600670 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600671 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700672
Gavin Howard6fbdb292018-02-27 15:44:48 -0700673err:
Gavin Howard63738202018-09-26 15:34:20 -0600674 bc_num_free(&copy);
675 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700676}
677
Gavin Howard63738202018-09-26 15:34:20 -0600678BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600679 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700680{
Gavin Howarda1c44392018-09-27 12:41:15 -0600681 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600682 BcNum num2, *ptr_a, *ptr_b;
683 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700684
Gavin Howard63738202018-09-26 15:34:20 -0600685 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700686
Gavin Howard11b9afd2018-10-18 14:23:28 -0600687 if ((init = (c == a))) {
Gavin Howard63738202018-09-26 15:34:20 -0600688 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600689 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600690 }
691 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700692
Gavin Howard63738202018-09-26 15:34:20 -0600693 if (c == b) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700694
Gavin Howardba009802018-09-29 04:41:51 -0600695 ptr_b = &num2;
696
697 if (c != a) {
698 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600699 init = true;
700 }
701 }
702 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700703
Gavin Howarda1c44392018-09-27 12:41:15 -0600704 if (init) s = bc_num_init(c, req);
705 else s = bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700706
Gavin Howard9ab772a2018-10-05 11:38:40 -0600707 if (!s) s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700708
Gavin Howard3cf769e2018-10-11 13:55:11 -0600709 assert(!c->neg || c->len);
710
Gavin Howardba009802018-09-29 04:41:51 -0600711 if (init) bc_num_free(&num2);
Gavin Howarda1c44392018-09-27 12:41:15 -0600712 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700713}
714
Gavin Howard3f68df72018-03-22 20:30:27 -0600715bool bc_num_strValid(const char *val, size_t base) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700716
Gavin Howard8a921bd2018-10-11 14:15:32 -0600717 BcDig b;
Gavin Howard63738202018-09-26 15:34:20 -0600718 bool small, radix = false;
719 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700720
Gavin Howard63738202018-09-26 15:34:20 -0600721 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700722
Gavin Howard63738202018-09-26 15:34:20 -0600723 small = base <= 10;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600724 b = (BcDig) (small ? base + '0' : base - 9 + 'A');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700725
Gavin Howard63738202018-09-26 15:34:20 -0600726 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700727
Gavin Howard8a921bd2018-10-11 14:15:32 -0600728 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600729
Gavin Howard63738202018-09-26 15:34:20 -0600730 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700731
Gavin Howard63738202018-09-26 15:34:20 -0600732 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700733
Gavin Howard63738202018-09-26 15:34:20 -0600734 radix = true;
735 continue;
736 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700737
Gavin Howard63738202018-09-26 15:34:20 -0600738 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
739 return false;
740 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700741
Gavin Howard63738202018-09-26 15:34:20 -0600742 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700743}
744
Gavin Howard3f68df72018-03-22 20:30:27 -0600745BcStatus bc_num_parseDecimal(BcNum *n, const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700746
Gavin Howard63738202018-09-26 15:34:20 -0600747 BcStatus s;
748 size_t len, i;
749 const char *ptr;
750 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700751
Gavin Howard63738202018-09-26 15:34:20 -0600752 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700753
Gavin Howard63738202018-09-26 15:34:20 -0600754 val += i;
755 len = strlen(val);
756 bc_num_zero(n);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700757
Gavin Howard63738202018-09-26 15:34:20 -0600758 if (len) {
759 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
760 if ((s = bc_num_expand(n, len))) return s;
761 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600762
Gavin Howarda2514a02018-10-18 14:20:09 -0600763 if (zero) return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700764
Gavin Howard63738202018-09-26 15:34:20 -0600765 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700766
Gavin Howard63738202018-09-26 15:34:20 -0600767 // Explicitly test for NULL here to produce either a 0 or 1.
768 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700769
Gavin Howard63738202018-09-26 15:34:20 -0600770 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
771 n->num[n->len] = val[i] - '0';
Gavin Howard6fbdb292018-02-27 15:44:48 -0700772
Gavin Howard63738202018-09-26 15:34:20 -0600773 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700774}
775
Gavin Howard3f68df72018-03-22 20:30:27 -0600776BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
Gavin Howardede51f02018-03-02 12:30:00 -0700777
Gavin Howard63738202018-09-26 15:34:20 -0600778 BcStatus s;
779 BcNum temp, mult, result;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600780 BcDig c = '\0';
Gavin Howard11b9afd2018-10-18 14:23:28 -0600781 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600782 unsigned long v;
783 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700784
Gavin Howard63738202018-09-26 15:34:20 -0600785 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700786
Gavin Howard11b9afd2018-10-18 14:23:28 -0600787 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howard63738202018-09-26 15:34:20 -0600788 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700789
Gavin Howard63738202018-09-26 15:34:20 -0600790 if ((s = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return s;
791 if ((s = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700792
Gavin Howard63738202018-09-26 15:34:20 -0600793 for (i = 0; i < len && (c = val[i]) != '.'; ++i) {
Gavin Howardede51f02018-03-02 12:30:00 -0700794
Gavin Howard63738202018-09-26 15:34:20 -0600795 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700796
Gavin Howard31060d92018-10-18 15:07:44 -0600797 if ((s = bc_num_alg_m(n, base, &mult, 0))) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600798 if ((s = bc_num_ulong2num(&temp, v))) goto int_err;
799 if ((s = bc_num_add(&mult, &temp, n, 0))) goto int_err;
800 }
Gavin Howardede51f02018-03-02 12:30:00 -0700801
Gavin Howard63738202018-09-26 15:34:20 -0600802 if (i == len && !(c = val[i])) goto int_err;
803 assert(c == '.');
804 if ((s = bc_num_init(&result, base->len))) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700805
Gavin Howard63738202018-09-26 15:34:20 -0600806 bc_num_zero(&result);
807 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700808
Gavin Howard63738202018-09-26 15:34:20 -0600809 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howardede51f02018-03-02 12:30:00 -0700810
Gavin Howard63738202018-09-26 15:34:20 -0600811 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardede51f02018-03-02 12:30:00 -0700812
Gavin Howard63738202018-09-26 15:34:20 -0600813 if ((s = bc_num_mul(&result, base, &result, 0))) goto err;
814 if ((s = bc_num_ulong2num(&temp, v))) goto err;
815 if ((s = bc_num_add(&result, &temp, &result, 0))) goto err;
816 if ((s = bc_num_mul(&mult, base, &mult, 0))) goto err;
817 }
Gavin Howardede51f02018-03-02 12:30:00 -0700818
Gavin Howard63738202018-09-26 15:34:20 -0600819 if ((s = bc_num_div(&result, &mult, &result, digits))) goto err;
820 if ((s = bc_num_add(n, &result, n, digits))) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600821
Gavin Howard63738202018-09-26 15:34:20 -0600822 if (n->len) {
823 if (n->rdx < digits && n->len) s = bc_num_extend(n, digits - n->rdx);
824 }
825 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700826
827err:
Gavin Howard63738202018-09-26 15:34:20 -0600828 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700829int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600830 bc_num_free(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700831mult_err:
Gavin Howard63738202018-09-26 15:34:20 -0600832 bc_num_free(&temp);
833 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700834}
835
Gavin Howard80977b22018-09-06 14:29:13 -0600836BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) {
Gavin Howard63738202018-09-26 15:34:20 -0600837 if (*nchars == line_len - 1) {
838 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
839 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
840 *nchars = 0;
841 }
Gavin Howard63738202018-09-26 15:34:20 -0600842 return BC_STATUS_SUCCESS;
Gavin Howard80977b22018-09-06 14:29:13 -0600843}
844
Gavin Howard83eb8392018-10-09 01:21:19 -0600845#ifdef DC_ENABLED
846BcStatus bc_num_printChar(size_t num, size_t width, bool radix,
847 size_t *nchars, size_t line_len)
848{
Gavin Howard4c091542018-10-09 03:33:08 -0600849 (void) radix, (void) line_len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600850
Gavin Howard11b9afd2018-10-18 14:23:28 -0600851 if (putchar((char) num) == EOF) return BC_STATUS_IO_ERR;
Gavin Howard83eb8392018-10-09 01:21:19 -0600852 *nchars = *nchars + width;
853
854 return BC_STATUS_SUCCESS;
855}
856#endif // DC_ENABLED
857
Gavin Howard80977b22018-09-06 14:29:13 -0600858BcStatus bc_num_printDigits(size_t num, size_t width, bool radix,
859 size_t *nchars, size_t line_len)
860{
Gavin Howard63738202018-09-26 15:34:20 -0600861 BcStatus s;
Gavin Howardc39fd492018-10-04 10:07:03 -0600862 size_t exp, pow, div;
Gavin Howard80977b22018-09-06 14:29:13 -0600863
Gavin Howard63738202018-09-26 15:34:20 -0600864 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600865 if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR;
866 ++(*nchars);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600867
Gavin Howard63738202018-09-26 15:34:20 -0600868 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600869 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600870
Gavin Howard63738202018-09-26 15:34:20 -0600871 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
Gavin Howard63738202018-09-26 15:34:20 -0600872 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600873 div = num / pow;
874 num -= div * pow;
Gavin Howard63738202018-09-26 15:34:20 -0600875 if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
876 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700877
Gavin Howard63738202018-09-26 15:34:20 -0600878 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700879}
Gavin Howardfe679f02018-02-14 15:50:09 -0700880
Gavin Howard902a16c2018-05-16 01:22:53 -0600881BcStatus bc_num_printHex(size_t num, size_t width, bool radix,
Gavin Howard0011a3a2018-03-29 23:11:32 -0600882 size_t *nchars, size_t line_len)
Gavin Howard2682a1f2018-03-03 09:09:13 -0700883{
Gavin Howard63738202018-09-26 15:34:20 -0600884 BcStatus s;
Gavin Howard80977b22018-09-06 14:29:13 -0600885
Gavin Howard63738202018-09-26 15:34:20 -0600886 assert(width == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600887
Gavin Howard63738202018-09-26 15:34:20 -0600888 if (radix) {
889 if ((s = bc_num_printNewline(nchars, line_len))) return s;
890 if (putchar('.') == EOF) return BC_STATUS_IO_ERR;
891 *nchars += 1;
892 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700893
Gavin Howard63738202018-09-26 15:34:20 -0600894 if ((s = bc_num_printNewline(nchars, line_len))) return s;
895 if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR;
896 *nchars = *nchars + width;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700897
Gavin Howard63738202018-09-26 15:34:20 -0600898 return BC_STATUS_SUCCESS;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700899}
900
Gavin Howard5f46ee32018-09-25 13:02:43 -0600901BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700902
Gavin Howardc39fd492018-10-04 10:07:03 -0600903 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600904 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700905
Gavin Howard63738202018-09-26 15:34:20 -0600906 if (n->neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
907 (*nchars) += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700908
Gavin Howard63738202018-09-26 15:34:20 -0600909 for (i = n->len - 1; !s && i < n->len; --i)
910 s = bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700911
Gavin Howard63738202018-09-26 15:34:20 -0600912 return s;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700913}
914
Gavin Howard83eb8392018-10-09 01:21:19 -0600915BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, size_t *nchars,
916 size_t line_len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -0600917{
Gavin Howard63738202018-09-26 15:34:20 -0600918 BcStatus s;
919 BcVec stack;
920 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -0600921 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -0600922 size_t i;
923 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700924
Gavin Howard83eb8392018-10-09 01:21:19 -0600925 if ((s = bc_vec_init(&stack, sizeof(long), NULL))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600926 if ((s = bc_num_init(&intp, n->len))) goto int_err;
927 if ((s = bc_num_init(&fracp, n->rdx))) goto frac_err;
928 if ((s = bc_num_init(&digit, width))) goto digit_err;
929 if ((s = bc_num_init(&frac_len, BC_NUM_INT(n)))) goto frac_len_err;
930 if ((s = bc_num_copy(&intp, n))) goto err;
931 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -0600932
Gavin Howard63738202018-09-26 15:34:20 -0600933 bc_num_truncate(&intp, intp.rdx);
934 if ((s = bc_num_sub(n, &intp, &fracp, 0))) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700935
Gavin Howard4c091542018-10-09 03:33:08 -0600936 if (!n->len) {
937 s = print(0, width, false, nchars, line_len);
938 goto err;
939 }
940
Gavin Howard63738202018-09-26 15:34:20 -0600941 while (intp.len) {
Gavin Howard18441082018-10-22 10:03:30 -0600942 if ((s = bc_num_divmod(&intp, base, &intp, &digit, 0))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600943 if ((s = bc_num_ulong(&digit, &dig))) goto err;
Gavin Howard90198862018-09-29 03:37:47 -0600944 if ((s = bc_vec_push(&stack, &dig))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600945 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700946
Gavin Howard63738202018-09-26 15:34:20 -0600947 for (i = 0; i < stack.len; ++i) {
948 ptr = bc_vec_item_rev(&stack, i);
949 assert(ptr);
950 if ((s = print(*ptr, width, false, nchars, line_len))) goto err;
951 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700952
Gavin Howard63738202018-09-26 15:34:20 -0600953 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700954
Gavin Howard63738202018-09-26 15:34:20 -0600955 for (radix = true; frac_len.len <= n->rdx; radix = false) {
956 if ((s = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err;
957 if ((s = bc_num_ulong(&fracp, &dig))) goto err;
958 if ((s = bc_num_ulong2num(&intp, dig))) goto err;
959 if ((s = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err;
960 if ((s = print(dig, width, radix, nchars, line_len))) goto err;
961 if ((s = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err;
962 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700963
964err:
Gavin Howard63738202018-09-26 15:34:20 -0600965 bc_num_free(&frac_len);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700966frac_len_err:
Gavin Howard63738202018-09-26 15:34:20 -0600967 bc_num_free(&digit);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700968digit_err:
Gavin Howard63738202018-09-26 15:34:20 -0600969 bc_num_free(&fracp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700970frac_err:
Gavin Howard63738202018-09-26 15:34:20 -0600971 bc_num_free(&intp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700972int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600973 bc_vec_free(&stack);
Gavin Howard63738202018-09-26 15:34:20 -0600974 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700975}
976
Gavin Howard83eb8392018-10-09 01:21:19 -0600977BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
978 size_t *nchars, size_t line_len)
979{
980 BcStatus s;
981 size_t width, i;
982 BcNumDigitOp print;
983 bool neg = n->neg;
984
985 if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
986 (*nchars) += neg;
987
988 n->neg = false;
989
990 if (base_t <= BC_NUM_MAX_IBASE) {
991 width = 1;
992 print = bc_num_printHex;
993 }
994 else {
995 for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width);
996 print = bc_num_printDigits;
997 }
998
999 s = bc_num_printNum(n, base, width, nchars, line_len, print);
1000 n->neg = neg;
1001
1002 return s;
1003}
1004
1005#ifdef DC_ENABLED
Gavin Howard844e7a52018-10-09 11:46:39 -06001006BcStatus bc_num_stream(BcNum *n, BcNum *base, size_t *nchars, size_t len) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001007 return bc_num_printNum(n, base, 1, nchars, len, bc_num_printChar);
1008}
1009#endif // DC_ENABLED
1010
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001011BcStatus bc_num_init(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -07001012
Gavin Howard63738202018-09-26 15:34:20 -06001013 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -07001014
Gavin Howard63738202018-09-26 15:34:20 -06001015 request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE;
Gavin Howardc39fd492018-10-04 10:07:03 -06001016 memset(n, 0, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001017 if (!(n->num = malloc(request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -07001018
Gavin Howard63738202018-09-26 15:34:20 -06001019 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -07001020
Gavin Howard63738202018-09-26 15:34:20 -06001021 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -07001022}
1023
Gavin Howard844e7a52018-10-09 11:46:39 -06001024BcStatus bc_num_expand(BcNum *n, size_t req) {
Gavin Howardb5c77212018-02-14 17:12:34 -07001025
Gavin Howard8a921bd2018-10-11 14:15:32 -06001026 BcDig *temp;
Gavin Howard9c4358c2018-03-22 20:11:28 -06001027
Gavin Howard844e7a52018-10-09 11:46:39 -06001028 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -07001029
Gavin Howard844e7a52018-10-09 11:46:39 -06001030 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb5c77212018-02-14 17:12:34 -07001031
Gavin Howard844e7a52018-10-09 11:46:39 -06001032 if (req <= n->cap) return BC_STATUS_SUCCESS;
1033 if (!(temp = realloc(n->num, req))) return BC_STATUS_ALLOC_ERR;
1034
Gavin Howard63738202018-09-26 15:34:20 -06001035 n->num = temp;
Gavin Howard844e7a52018-10-09 11:46:39 -06001036 n->cap = req;
Gavin Howardb5c77212018-02-14 17:12:34 -07001037
Gavin Howard63738202018-09-26 15:34:20 -06001038 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -07001039}
1040
Gavin Howarded392aa2018-02-27 13:09:26 -07001041void bc_num_free(void *num) {
Gavin Howard63738202018-09-26 15:34:20 -06001042 BcNum *n = (BcNum*) num;
Gavin Howard8c019982018-10-05 12:35:09 -06001043 assert(n);
Gavin Howard2bc54182018-10-06 17:03:46 -06001044 free(n->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001045}
1046
Gavin Howard9c4358c2018-03-22 20:11:28 -06001047BcStatus bc_num_copy(BcNum *d, BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -07001048
Gavin Howardb20c2db2018-10-08 10:31:23 -06001049 BcStatus status;
Gavin Howard5a049c42018-02-15 11:24:11 -07001050
Gavin Howard63738202018-09-26 15:34:20 -06001051 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -07001052
Gavin Howard63738202018-09-26 15:34:20 -06001053 if (d == s) return BC_STATUS_SUCCESS;
Gavin Howardb20c2db2018-10-08 10:31:23 -06001054 if ((status = bc_num_expand(d, s->cap))) return status;
Gavin Howard5a049c42018-02-15 11:24:11 -07001055
Gavin Howard63738202018-09-26 15:34:20 -06001056 d->len = s->len;
1057 d->neg = s->neg;
1058 d->rdx = s->rdx;
Gavin Howard5a049c42018-02-15 11:24:11 -07001059
Gavin Howard8a921bd2018-10-11 14:15:32 -06001060 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001061
Gavin Howardb20c2db2018-10-08 10:31:23 -06001062 return status;
Gavin Howard5a049c42018-02-15 11:24:11 -07001063}
1064
Gavin Howard5cea43b2018-03-02 11:32:11 -07001065BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
Gavin Howard025d04d2018-02-20 13:53:28 -07001066
Gavin Howard63738202018-09-26 15:34:20 -06001067 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001068
Gavin Howard63738202018-09-26 15:34:20 -06001069 assert(n && val && base);
1070 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_NUM_MAX_IBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001071
Gavin Howard63738202018-09-26 15:34:20 -06001072 if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001073
Gavin Howard63738202018-09-26 15:34:20 -06001074 if (base_t == 10) s = bc_num_parseDecimal(n, val);
1075 else s = bc_num_parseBase(n, val, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001076
Gavin Howard63738202018-09-26 15:34:20 -06001077 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001078}
1079
Gavin Howard0011a3a2018-03-29 23:11:32 -06001080BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
1081 size_t *nchars, size_t line_len)
Gavin Howard152f3e82018-03-07 12:33:15 -07001082{
Gavin Howard2cb39612018-10-22 08:46:48 -06001083 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001084
Gavin Howard63738202018-09-26 15:34:20 -06001085 assert(n && base && nchars);
1086 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001087
Gavin Howard2cb39612018-10-22 08:46:48 -06001088 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howardbc7cae82018-03-14 13:43:04 -06001089
Gavin Howard63738202018-09-26 15:34:20 -06001090 if (!n->len) {
1091 if (putchar('0') == EOF) return BC_STATUS_IO_ERR;
1092 ++(*nchars);
Gavin Howard63738202018-09-26 15:34:20 -06001093 }
1094 else if (base_t == 10) s = bc_num_printDecimal(n, nchars, line_len);
1095 else s = bc_num_printBase(n, base, base_t, nchars, line_len);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001096
Gavin Howard63738202018-09-26 15:34:20 -06001097 if (s) return s;
Gavin Howard152f3e82018-03-07 12:33:15 -07001098
Gavin Howard63738202018-09-26 15:34:20 -06001099 if (newline) {
1100 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
1101 *nchars = 0;
1102 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001103
Gavin Howard63738202018-09-26 15:34:20 -06001104 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001105}
1106
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001107BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001108
Gavin Howard63738202018-09-26 15:34:20 -06001109 size_t i;
1110 unsigned long pow;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001111
Gavin Howard63738202018-09-26 15:34:20 -06001112 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001113
Gavin Howard63738202018-09-26 15:34:20 -06001114 if (n->neg) return BC_STATUS_MATH_NEGATIVE;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001115
Gavin Howard63738202018-09-26 15:34:20 -06001116 for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001117
Gavin Howard63738202018-09-26 15:34:20 -06001118 unsigned long prev = *result, powprev = pow;
Gavin Howard88c25302018-10-17 13:32:23 -06001119
Gavin Howard63738202018-09-26 15:34:20 -06001120 *result += ((unsigned long) n->num[i]) * pow;
1121 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001122
Gavin Howard63738202018-09-26 15:34:20 -06001123 if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW;
1124 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001125
Gavin Howard63738202018-09-26 15:34:20 -06001126 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001127}
1128
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001129BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001130
Gavin Howard63738202018-09-26 15:34:20 -06001131 BcStatus s;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001132 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001133 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001134 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001135
Gavin Howard63738202018-09-26 15:34:20 -06001136 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001137
Gavin Howard63738202018-09-26 15:34:20 -06001138 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001139
Gavin Howarda2514a02018-10-18 14:20:09 -06001140 if (!val) return BC_STATUS_SUCCESS;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001141
Gavin Howardec82bdc2018-10-08 23:50:57 -06001142 for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len)
Gavin Howard63738202018-09-26 15:34:20 -06001143 if ((s = bc_num_expand(n, len))) return s;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001144 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001145
Gavin Howard63738202018-09-26 15:34:20 -06001146 return BC_STATUS_SUCCESS;
Gavin Howard025d04d2018-02-20 13:53:28 -07001147}
1148
Gavin Howard12fe7812018-09-29 03:45:18 -06001149BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001150 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s;
Gavin Howardf8f232b2018-10-11 17:29:47 -06001151 (void) scale;
Gavin Howard12fe7812018-09-29 03:45:18 -06001152 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001153}
1154
Gavin Howard12fe7812018-09-29 03:45:18 -06001155BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001156 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a;
Gavin Howardf8f232b2018-10-11 17:29:47 -06001157 (void) scale;
Gavin Howard12fe7812018-09-29 03:45:18 -06001158 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001159}
1160
Gavin Howard12fe7812018-09-29 03:45:18 -06001161BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001162 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001163 return bc_num_binary(a, b, c, scale, bc_num_alg_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001164}
1165
Gavin Howard12fe7812018-09-29 03:45:18 -06001166BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001167 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001168 return bc_num_binary(a, b, c, scale, bc_num_alg_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001169}
1170
Gavin Howardbe53edd2018-10-04 10:55:08 -06001171BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001172 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howardbe53edd2018-10-04 10:55:08 -06001173 return bc_num_binary(a, b, c, scale, bc_num_alg_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001174}
1175
Gavin Howard12fe7812018-09-29 03:45:18 -06001176BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
1177 return bc_num_binary(a, b, c, scale, bc_num_alg_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001178}
1179
Gavin Howard757b66a2018-10-06 04:13:46 -06001180BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001181
Gavin Howard63738202018-09-26 15:34:20 -06001182 BcStatus s;
1183 BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001184 size_t pow, len, digits, digits1, resrdx, req, times = 0;
1185 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard954276b2018-05-16 01:43:58 -06001186
Gavin Howard757b66a2018-10-06 04:13:46 -06001187 assert(a && b);
Gavin Howard954276b2018-05-16 01:43:58 -06001188
Gavin Howard63738202018-09-26 15:34:20 -06001189 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a)+ 1) >> 1) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001190
Gavin Howard757b66a2018-10-06 04:13:46 -06001191 if (b == a) {
1192 memcpy(&a2, b, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001193 ptr_a = &a2;
Gavin Howard757b66a2018-10-06 04:13:46 -06001194 s = bc_num_init(b, req);
Gavin Howard63738202018-09-26 15:34:20 -06001195 }
1196 else {
1197 ptr_a = a;
Gavin Howard757b66a2018-10-06 04:13:46 -06001198 s = bc_num_expand(b, req);
Gavin Howard63738202018-09-26 15:34:20 -06001199 }
Gavin Howard954276b2018-05-16 01:43:58 -06001200
Gavin Howard63738202018-09-26 15:34:20 -06001201 if (s) goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001202
Gavin Howard63738202018-09-26 15:34:20 -06001203 if (!ptr_a->len) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001204 bc_num_setToZero(b, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001205 goto init_err;
1206 }
1207 else if (ptr_a->neg) {
Gavin Howard4c4f5f02018-10-11 11:52:11 -06001208 s = BC_STATUS_MATH_NEGATIVE;
Gavin Howard63738202018-09-26 15:34:20 -06001209 goto init_err;
1210 }
1211 else if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001212 bc_num_one(b);
1213 s = bc_num_extend(b, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001214 goto init_err;
1215 }
Gavin Howard954276b2018-05-16 01:43:58 -06001216
Gavin Howard63738202018-09-26 15:34:20 -06001217 len = ptr_a->len;
Gavin Howard954276b2018-05-16 01:43:58 -06001218
Gavin Howard63738202018-09-26 15:34:20 -06001219 scale = BC_MAX(scale, ptr_a->rdx) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001220
Gavin Howard63738202018-09-26 15:34:20 -06001221 if ((s = bc_num_init(&num1, len))) goto init_err;
1222 if ((s = bc_num_init(&num2, num1.len))) goto num2_err;
1223 if ((s = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001224
Gavin Howard63738202018-09-26 15:34:20 -06001225 bc_num_one(&half);
1226 half.num[0] = 5;
1227 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001228
Gavin Howard63738202018-09-26 15:34:20 -06001229 len += scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001230
Gavin Howard63738202018-09-26 15:34:20 -06001231 if ((s = bc_num_init(&f, len))) goto f_err;
1232 if ((s = bc_num_init(&fprime, len + scale))) goto fprime_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001233
Gavin Howard63738202018-09-26 15:34:20 -06001234 x0 = &num1;
1235 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001236
Gavin Howard63738202018-09-26 15:34:20 -06001237 bc_num_one(x0);
Gavin Howard954276b2018-05-16 01:43:58 -06001238
Gavin Howardc39fd492018-10-04 10:07:03 -06001239 if ((pow = BC_NUM_INT(ptr_a))) {
Gavin Howard954276b2018-05-16 01:43:58 -06001240
Gavin Howardc39fd492018-10-04 10:07:03 -06001241 if (pow & 1) x0->num[0] = 2;
1242 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001243
Gavin Howardc39fd492018-10-04 10:07:03 -06001244 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001245
Gavin Howard63738202018-09-26 15:34:20 -06001246 if ((s = bc_num_extend(x0, pow))) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06001247
Gavin Howard63738202018-09-26 15:34:20 -06001248 // Make sure to move the radix back.
1249 x0->rdx -= pow;
1250 }
Gavin Howard954276b2018-05-16 01:43:58 -06001251
Gavin Howard9f2395b2018-10-06 05:28:58 -06001252 x0->rdx = digits = digits1 = 0;
1253 resrdx = scale + 2;
1254 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001255
Gavin Howard0bfcf7a2018-10-11 09:37:09 -06001256 while (!bcg.signe && (cmp || digits < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001257
Gavin Howard31060d92018-10-18 15:07:44 -06001258 if ((s = bc_num_alg_d(a, x0, &f, resrdx))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001259 if ((s = bc_num_add(x0, &f, &fprime, resrdx))) goto err;
Gavin Howard31060d92018-10-18 15:07:44 -06001260 if ((s = bc_num_alg_m(&fprime, &half, x1, resrdx))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001261
Gavin Howard63738202018-09-26 15:34:20 -06001262 cmp = bc_num_cmp(x1, x0);
1263 digits = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001264
Gavin Howardb11e9e22018-10-06 17:26:02 -06001265 if (cmp == cmp2 && digits == digits1) times += 1;
1266 else times = 0;
1267
1268 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001269
1270 cmp2 = cmp1;
1271 cmp1 = cmp;
1272 digits1 = digits;
1273
Gavin Howard63738202018-09-26 15:34:20 -06001274 temp = x0;
1275 x0 = x1;
1276 x1 = temp;
1277 }
Gavin Howard954276b2018-05-16 01:43:58 -06001278
Gavin Howard63738202018-09-26 15:34:20 -06001279 if (bcg.signe) {
1280 s = BC_STATUS_EXEC_SIGNAL;
1281 goto err;
1282 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001283
Gavin Howard757b66a2018-10-06 04:13:46 -06001284 if ((s = bc_num_copy(b, x0))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001285
Gavin Howard757b66a2018-10-06 04:13:46 -06001286 if (b->rdx > --scale) bc_num_truncate(b, b->rdx - scale);
1287 else if (b->rdx < scale) s = bc_num_extend(b, scale - b->rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06001288
1289err:
Gavin Howard63738202018-09-26 15:34:20 -06001290 bc_num_free(&fprime);
Gavin Howard954276b2018-05-16 01:43:58 -06001291fprime_err:
Gavin Howard63738202018-09-26 15:34:20 -06001292 bc_num_free(&f);
Gavin Howard954276b2018-05-16 01:43:58 -06001293f_err:
Gavin Howard63738202018-09-26 15:34:20 -06001294 bc_num_free(&half);
Gavin Howard954276b2018-05-16 01:43:58 -06001295two_err:
Gavin Howard63738202018-09-26 15:34:20 -06001296 bc_num_free(&num2);
Gavin Howard954276b2018-05-16 01:43:58 -06001297num2_err:
Gavin Howard63738202018-09-26 15:34:20 -06001298 bc_num_free(&num1);
Gavin Howard954276b2018-05-16 01:43:58 -06001299init_err:
Gavin Howard757b66a2018-10-06 04:13:46 -06001300 if (b == a) bc_num_free(&a2);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001301 assert(!b->neg || b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001302 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001303}
Gavin Howardba009802018-09-29 04:41:51 -06001304
Gavin Howard2cb39612018-10-22 08:46:48 -06001305BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1306
1307 BcStatus s;
1308 BcNum num2, *ptr_a;
1309 bool init;
1310 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
1311
1312 assert(c != d && a != b && a != d && b != d && b != c);
1313
1314 if ((init = (c == a))) {
1315 memcpy(&num2, c, sizeof(BcNum));
1316 ptr_a = &num2;
1317 }
1318 else ptr_a = a;
1319
1320 if (init) s = bc_num_init(c, len);
1321 else s = bc_num_expand(d, ptr_a->len);
1322
1323 if (s) return s;
1324
Gavin Howard18441082018-10-22 10:03:30 -06001325 s = bc_num_alg_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001326
1327 assert(!c->neg || c->len);
1328 assert(!d->neg || d->len);
1329
1330 if (init) bc_num_free(&num2);
1331
1332 return s;
1333}
1334
Gavin Howarde6e84762018-10-03 11:46:34 -06001335#ifdef DC_ENABLED
Gavin Howardba009802018-09-29 04:41:51 -06001336BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1337
1338 BcStatus s;
Gavin Howard2c8959e2018-10-04 18:32:58 -06001339 BcNum num2, *ptr_a, *ptr_b, *ptr_c, base, exp, two, temp;
Gavin Howard2cb39612018-10-22 08:46:48 -06001340 bool init;
Gavin Howardba009802018-09-29 04:41:51 -06001341
1342 assert(a && b && c && d);
1343
1344 scale = 0;
1345
Gavin Howard11b9afd2018-10-18 14:23:28 -06001346 if ((init = (d == a))) {
Gavin Howardba009802018-09-29 04:41:51 -06001347 memcpy(&num2, d, sizeof(BcNum));
1348 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001349 }
1350 else ptr_a = a;
1351
1352 if (d == b) {
1353
1354 ptr_b = &num2;
1355
1356 if (d != a) {
1357 memcpy(ptr_b, d, sizeof(BcNum));
1358 init = true;
1359 }
1360 }
1361 else ptr_b = b;
1362
1363 if (d == c) {
1364
1365 ptr_c = &num2;
1366
1367 if (d != a && d != b) {
1368 memcpy(ptr_c, d, sizeof(BcNum));
1369 init = true;
1370 }
1371 }
1372 else ptr_c = c;
1373
1374 if (init) s = bc_num_init(d, ptr_c->len);
1375 else s = bc_num_expand(d, ptr_c->len);
1376
1377 if (s) return s;
1378
1379 if (!ptr_c->len) {
1380 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
1381 goto base_err;
1382 }
1383
Gavin Howard5ef610b2018-10-04 16:57:47 -06001384 if (ptr_a->rdx || ptr_b->rdx || ptr_c->rdx) {
Gavin Howardba009802018-09-29 04:41:51 -06001385 s = BC_STATUS_MATH_NON_INTEGER;
1386 goto base_err;
1387 }
1388
1389 if (ptr_b->neg) {
1390 s = BC_STATUS_MATH_NEGATIVE;
1391 goto base_err;
1392 }
1393
Gavin Howardba009802018-09-29 04:41:51 -06001394 if ((s = bc_num_init(&base, ptr_c->len))) goto base_err;
1395 if ((s = bc_num_init(&exp, ptr_b->len))) goto exp_err;
1396 if ((s = bc_num_init(&two, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howardba009802018-09-29 04:41:51 -06001397 if ((s = bc_num_init(&temp, ptr_b->len))) goto temp_err;
1398
1399 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001400 two.num[0] = 2;
1401 bc_num_one(d);
1402
Gavin Howard31060d92018-10-18 15:07:44 -06001403 if ((s = bc_num_alg_rem(ptr_a, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001404 if ((s = bc_num_copy(&exp, ptr_b))) goto err;
1405
Gavin Howardaf905602018-09-29 18:56:37 -06001406 while (exp.len) {
Gavin Howardba009802018-09-29 04:41:51 -06001407
Gavin Howard18441082018-10-22 10:03:30 -06001408 if ((s = bc_num_divmod(&exp, &two, &exp, &temp, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001409
1410 if (BC_NUM_ONE(&temp)) {
Gavin Howard31060d92018-10-18 15:07:44 -06001411 if ((s = bc_num_alg_m(d, &base, &temp, scale))) goto err;
1412 if ((s = bc_num_alg_rem(&temp, ptr_c, d, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001413 }
1414
Gavin Howard31060d92018-10-18 15:07:44 -06001415 if ((s = bc_num_alg_m(&base, &base, &temp, scale))) goto err;
1416 if ((s = bc_num_alg_rem(&temp, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001417 }
1418
1419err:
1420 bc_num_free(&temp);
1421temp_err:
Gavin Howardba009802018-09-29 04:41:51 -06001422 bc_num_free(&two);
1423two_err:
1424 bc_num_free(&exp);
1425exp_err:
1426 bc_num_free(&base);
1427base_err:
1428 if (init) bc_num_free(&num2);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001429 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001430 return s;
1431}
Gavin Howarde6e84762018-10-03 11:46:34 -06001432#endif // DC_ENABLED