blob: 02f38eece2c0d1c9fbd9e0fb42a5918e83ebb4ff [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 Howard0be26ed2018-08-31 20:21:56 -060034void bc_num_zero(BcNum *n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060035 assert(n);
36 n->neg = (n->rdx = n->len = 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060037}
38
39void bc_num_one(BcNum *n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060040 assert(n);
Gavin Howard63738202018-09-26 15:34:20 -060041 bc_num_zero(n);
42 n->len = 1;
43 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060044}
45
46void bc_num_ten(BcNum *n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060047 assert(n);
Gavin Howard63738202018-09-26 15:34:20 -060048 bc_num_zero(n);
49 n->len = 2;
50 n->num[0] = 0;
51 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060052}
53
Gavin Howard8a921bd2018-10-11 14:15:32 -060054BcStatus bc_num_subArrays(BcDig *restrict n1, BcDig *restrict n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060055 size_t i, j;
56 for (i = 0; !bcg.signe && i < len; ++i) {
57 for (n1[i] -= n2[i], j = 0; !bcg.signe && n1[i + j] < 0;) {
58 n1[i + j++] += 10;
59 n1[i + j] -= 1;
60 }
61 }
62 return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060063}
64
Gavin Howard8a921bd2018-10-11 14:15:32 -060065ssize_t bc_num_compare(BcDig *restrict n1, BcDig *restrict n2, size_t len) {
Gavin Howard63738202018-09-26 15:34:20 -060066 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -060067 int c = 0;
Gavin Howard63738202018-09-26 15:34:20 -060068 for (i = len - 1; !bcg.signe && i < len && !(c = n1[i] - n2[i]); --i);
Gavin Howard7fbb4e22018-10-18 11:43:17 -060069 return BC_NUM_NEG(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -060070}
71
Gavin Howard6a804cf2018-05-17 16:54:12 -060072ssize_t bc_num_cmp(BcNum *a, BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070073
Gavin Howard63738202018-09-26 15:34:20 -060074 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -060075 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060076 bool a_max, neg = false;
77 ssize_t cmp;
78
79 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070080
Gavin Howard8a921bd2018-10-11 14:15:32 -060081 if (a == b) return 0;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060082 else if (!a->len) return BC_NUM_NEG(!!b->len, !b->neg);
83 else if (!b->len) return BC_NUM_NEG(1, a->neg);
84 else if (a->neg) {
85 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -060086 else return -1;
87 }
88 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070089
Gavin Howard63738202018-09-26 15:34:20 -060090 a_int = BC_NUM_INT(a);
91 b_int = BC_NUM_INT(b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070092
Gavin Howard7fbb4e22018-10-18 11:43:17 -060093 if ((a_int -= b_int)) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -070094
Gavin Howard7fbb4e22018-10-18 11:43:17 -060095 if ((a_max = a->rdx > b->rdx)) {
Gavin Howard63738202018-09-26 15:34:20 -060096 min = b->rdx;
97 diff = a->rdx - b->rdx;
98 max_num = a->num + diff;
99 min_num = b->num;
100 }
101 else {
102 min = a->rdx;
103 diff = b->rdx - a->rdx;
104 max_num = b->num + diff;
105 min_num = a->num;
106 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700107
Gavin Howard63738202018-09-26 15:34:20 -0600108 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard40974152018-10-18 14:23:01 -0600109 if (cmp) return BC_NUM_NEG(cmp, (!a_max) != neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700110
Gavin Howard63738202018-09-26 15:34:20 -0600111 for (max_num -= diff, i = diff - 1; !bcg.signe && i < diff; --i) {
Gavin Howard40974152018-10-18 14:23:01 -0600112 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
Gavin Howard63738202018-09-26 15:34:20 -0600113 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700114
Gavin Howard63738202018-09-26 15:34:20 -0600115 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700116}
117
Gavin Howardd9734e52018-03-29 15:54:41 -0600118void bc_num_truncate(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700119
Gavin Howard305249a2018-10-15 20:24:47 -0600120 assert(places <= n->rdx && places <= n->len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700121
Gavin Howard63738202018-09-26 15:34:20 -0600122 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700123
Gavin Howard63738202018-09-26 15:34:20 -0600124 n->len -= places;
125 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700126
Gavin Howardd32d7df2018-10-15 08:42:25 -0600127 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700128}
129
Gavin Howard3f68df72018-03-22 20:30:27 -0600130BcStatus bc_num_extend(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700131
Gavin Howard63738202018-09-26 15:34:20 -0600132 BcStatus s;
Gavin Howard88c25302018-10-17 13:32:23 -0600133 size_t len = n->len + places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700134
Gavin Howard63738202018-09-26 15:34:20 -0600135 if (!places) return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600136 if (n->cap < len && (s = bc_num_expand(n, len))) return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700137
Gavin Howardd32d7df2018-10-15 08:42:25 -0600138 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
Gavin Howard8a921bd2018-10-11 14:15:32 -0600139 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700140
Gavin Howard63738202018-09-26 15:34:20 -0600141 n->len += places;
142 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700143
Gavin Howard63738202018-09-26 15:34:20 -0600144 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700145}
146
Gavin Howardc0f65d72018-09-01 16:59:04 -0600147void bc_num_clean(BcNum *n) {
Gavin Howard63738202018-09-26 15:34:20 -0600148 while (n->len > 0 && !n->num[n->len - 1]) --n->len;
Gavin Howard549d2072018-10-22 08:38:09 -0600149 if (!n->len) n->neg = false;
Gavin Howard63738202018-09-26 15:34:20 -0600150 else if (n->len < n->rdx) n->len = n->rdx;
Gavin Howarde57525d2018-09-01 16:56:21 -0600151}
152
Gavin Howardd32d7df2018-10-15 08:42:25 -0600153BcStatus bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600154
Gavin Howard63738202018-09-26 15:34:20 -0600155 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600156
Gavin Howard63738202018-09-26 15:34:20 -0600157 if (n->rdx < scale) s = bc_num_extend(n, scale - n->rdx);
158 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600159
Gavin Howard305249a2018-10-15 20:24:47 -0600160 bc_num_clean(n);
Gavin Howard549d2072018-10-22 08:38:09 -0600161 if (n->len) n->neg = !neg1 != !neg2;
Gavin Howard305249a2018-10-15 20:24:47 -0600162
163 return s;
164}
165
166BcStatus bc_num_splitAt(BcNum *restrict n, size_t idx, BcNum *restrict a,
Gavin Howardb5093d72018-10-17 12:13:17 -0600167 BcNum *restrict b)
Gavin Howard305249a2018-10-15 20:24:47 -0600168{
169 BcStatus s = BC_STATUS_SUCCESS;
170
171 if (idx < n->len) {
172
Gavin Howard305249a2018-10-15 20:24:47 -0600173 b->len = n->len - idx;
174 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600175 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600176
177 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
178 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard305249a2018-10-15 20:24:47 -0600179 }
180 else {
181 bc_num_zero(b);
182 s = bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600183 }
184
185 bc_num_clean(a);
186 bc_num_clean(b);
187
188 return s;
189}
190
191BcStatus bc_num_shift(BcNum *n, size_t places) {
192
193 BcStatus s = BC_STATUS_SUCCESS;
194
195 if (!places) return BC_STATUS_SUCCESS;
196 if (places + n->len > BC_MAX_NUM) return BC_STATUS_EXEC_NUM_LEN;
197
198 if (n->rdx >= places) n->rdx -= places;
199 else {
200 s = bc_num_extend(n, places - n->rdx);
201 n->rdx = 0;
202 }
203
204 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600205
Gavin Howard63738202018-09-26 15:34:20 -0600206 return s;
Gavin Howard337fe602018-09-01 15:10:59 -0600207}
208
Gavin Howard9c4358c2018-03-22 20:11:28 -0600209BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
210
Gavin Howard63738202018-09-26 15:34:20 -0600211 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600212 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600213
Gavin Howardd32d7df2018-10-15 08:42:25 -0600214 one.cap = 2;
215 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600216 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600217
Gavin Howardd32d7df2018-10-15 08:42:25 -0600218 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600219}
220
Gavin Howard8a921bd2018-10-11 14:15:32 -0600221BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700222
Gavin Howard8a921bd2018-10-11 14:15:32 -0600223 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600224 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard60136d72018-10-15 14:28:19 -0600225 int carry, in;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700226
Gavin Howard63738202018-09-26 15:34:20 -0600227 // Because this function doesn't need to use scale (per the bc spec),
228 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600229
Gavin Howard63738202018-09-26 15:34:20 -0600230 if (!a->len) {
231 BcStatus s = bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600232 if (sub && c->len) c->neg = !c->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600233 return s;
234 }
235 else if (!b->len) return bc_num_copy(c, a);
Gavin Howardf6964a12018-03-14 10:52:31 -0600236
Gavin Howard63738202018-09-26 15:34:20 -0600237 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600238 c->rdx = BC_MAX(a->rdx, b->rdx);
239 min_rdx = BC_MIN(a->rdx, b->rdx);
240 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700241
Gavin Howard63738202018-09-26 15:34:20 -0600242 if (a->rdx > b->rdx) {
243 diff = a->rdx - b->rdx;
244 ptr = a->num;
245 ptr_a = a->num + diff;
246 ptr_b = b->num;
247 }
248 else {
249 diff = b->rdx - a->rdx;
250 ptr = b->num;
251 ptr_a = a->num;
252 ptr_b = b->num + diff;
253 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700254
Gavin Howard63738202018-09-26 15:34:20 -0600255 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700256
Gavin Howard63738202018-09-26 15:34:20 -0600257 ptr_c += diff;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700258
Gavin Howard8ae3bf52018-10-19 13:30:17 -0600259 if ((a_int = BC_NUM_INT(a)) > (b_int = BC_NUM_INT(b))) {
Gavin Howard63738202018-09-26 15:34:20 -0600260 min_int = b_int;
261 max = a_int;
262 ptr = ptr_a;
263 }
264 else {
265 min_int = a_int;
266 max = b_int;
267 ptr = ptr_b;
268 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700269
Gavin Howard63738202018-09-26 15:34:20 -0600270 for (carry = 0, i = 0; !bcg.signe && i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard60136d72018-10-15 14:28:19 -0600271 in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry;
272 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600273 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600274 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700275
Gavin Howard63738202018-09-26 15:34:20 -0600276 for (; !bcg.signe && i < max + min_rdx; ++i, ++c->len) {
Gavin Howard11b9afd2018-10-18 14:23:28 -0600277 in = ((int) ptr[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600278 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600279 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600280 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700281
Gavin Howard63738202018-09-26 15:34:20 -0600282 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600283
Gavin Howard60136d72018-10-15 14:28:19 -0600284 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700285
Gavin Howard63738202018-09-26 15:34:20 -0600286 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700287}
288
Gavin Howard8a921bd2018-10-11 14:15:32 -0600289BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700290
Gavin Howard63738202018-09-26 15:34:20 -0600291 BcStatus s;
292 ssize_t cmp;
293 BcNum *minuend, *subtrahend;
294 size_t start;
295 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700296
Gavin Howard63738202018-09-26 15:34:20 -0600297 // Because this function doesn't need to use scale (per the bc spec),
298 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700299
Gavin Howard63738202018-09-26 15:34:20 -0600300 if (!a->len) {
301 s = bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600302 if (sub && c->len) c->neg = !c->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600303 return s;
304 }
305 else if (!b->len) return bc_num_copy(c, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700306
Gavin Howard63738202018-09-26 15:34:20 -0600307 aneg = a->neg;
308 bneg = b->neg;
309 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700310
Gavin Howard63738202018-09-26 15:34:20 -0600311 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700312
Gavin Howard63738202018-09-26 15:34:20 -0600313 a->neg = aneg;
314 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700315
Gavin Howard63738202018-09-26 15:34:20 -0600316 if (!cmp) {
317 bc_num_zero(c);
318 return BC_STATUS_SUCCESS;
319 }
320 else if (cmp > 0) {
321 neg = a->neg;
322 minuend = a;
323 subtrahend = b;
324 }
325 else {
326 neg = b->neg;
327 if (sub) neg = !neg;
328 minuend = b;
329 subtrahend = a;
330 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700331
Gavin Howard63738202018-09-26 15:34:20 -0600332 if ((s = bc_num_copy(c, minuend))) return s;
333 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700334
Gavin Howard63738202018-09-26 15:34:20 -0600335 if (c->rdx < subtrahend->rdx) {
336 if ((s = bc_num_extend(c, subtrahend->rdx - c->rdx))) return s;
337 start = 0;
338 }
339 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700340
Gavin Howard63738202018-09-26 15:34:20 -0600341 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700342
Gavin Howard63738202018-09-26 15:34:20 -0600343 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700344
Gavin Howard63738202018-09-26 15:34:20 -0600345 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700346}
347
Gavin Howardb5093d72018-10-17 12:13:17 -0600348BcStatus bc_num_alg_k(BcNum *restrict a, BcNum *restrict b, BcNum *restrict c) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700349
Gavin Howard63738202018-09-26 15:34:20 -0600350 BcStatus s;
Gavin Howard60136d72018-10-15 14:28:19 -0600351 int carry;
Gavin Howard305249a2018-10-15 20:24:47 -0600352 size_t i, j, len, max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600353 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard63738202018-09-26 15:34:20 -0600354 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600355
Gavin Howardb5093d72018-10-17 12:13:17 -0600356 if (!a->len || !b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600357 bc_num_zero(c);
358 return BC_STATUS_SUCCESS;
359 }
Gavin Howard23d8d252018-10-17 12:03:41 -0600360 else if (aone || BC_NUM_ONE(b)) return bc_num_copy(c, aone ? b : a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700361
Gavin Howardb5093d72018-10-17 12:13:17 -0600362 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
363 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600364 {
Gavin Howard23d8d252018-10-17 12:03:41 -0600365 if ((s = bc_num_expand(c, a->len + b->len + 1))) return s;
366
Gavin Howard305249a2018-10-15 20:24:47 -0600367 memset(c->num, 0, sizeof(BcDig) * c->cap);
368 c->len = carry = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700369
Gavin Howard305249a2018-10-15 20:24:47 -0600370 for (i = 0; !bcg.signe && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600371
Gavin Howard305249a2018-10-15 20:24:47 -0600372 for (j = 0; !bcg.signe && j < a->len; ++j) {
Gavin Howard23d8d252018-10-17 12:03:41 -0600373 int in = (int) c->num[i + j];
Gavin Howard305249a2018-10-15 20:24:47 -0600374 in += ((int) a->num[j]) * ((int) b->num[i]) + carry;
375 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600376 c->num[i + j] = (BcDig) (in % 10);
Gavin Howard305249a2018-10-15 20:24:47 -0600377 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700378
Gavin Howard305249a2018-10-15 20:24:47 -0600379 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
380
Gavin Howard88c25302018-10-17 13:32:23 -0600381 c->num[i + j] += (BcDig) carry;
382 len = BC_MAX(len, i + j + !!carry);
383 carry = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600384 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700385
Gavin Howard305249a2018-10-15 20:24:47 -0600386 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600387
Gavin Howardd1a78362018-10-18 09:52:06 -0600388 return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600389 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700390
Gavin Howard305249a2018-10-15 20:24:47 -0600391 if ((s = bc_num_init(&l1, max))) return s;
392 if ((s = bc_num_init(&h1, max))) goto high1_err;
393 if ((s = bc_num_init(&l2, max))) goto low2_err;
394 if ((s = bc_num_init(&h2, max))) goto high2_err;
395 if ((s = bc_num_init(&m1, max))) goto mix1_err;
396 if ((s = bc_num_init(&m2, max))) goto mix2_err;
397 if ((s = bc_num_init(&z0, max))) goto z0_err;
398 if ((s = bc_num_init(&z1, max))) goto z1_err;
399 if ((s = bc_num_init(&z2, max))) goto z2_err;
400 if ((s = bc_num_init(&temp, max + max))) goto temp_err;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600401
Gavin Howardb5093d72018-10-17 12:13:17 -0600402 if ((s = bc_num_splitAt(a, max2, &l1, &h1))) goto err;
403 if ((s = bc_num_splitAt(b, max2, &l2, &h2))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700404
Gavin Howardb5093d72018-10-17 12:13:17 -0600405 if ((s = bc_num_add(&h1, &l1, &m1, 0))) goto err;
406 if ((s = bc_num_add(&h2, &l2, &m2, 0))) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600407
Gavin Howardb5093d72018-10-17 12:13:17 -0600408 if ((s = bc_num_alg_k(&h1, &h2, &z0))) goto err;
409 if ((s = bc_num_alg_k(&m1, &m2, &z1))) goto err;
410 if ((s = bc_num_alg_k(&l1, &l2, &z2))) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600411
Gavin Howardb5093d72018-10-17 12:13:17 -0600412 if ((s = bc_num_sub(&z1, &z0, &temp, 0))) goto err;
413 if ((s = bc_num_sub(&temp, &z2, &z1, 0))) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600414
415 if ((s = bc_num_shift(&z0, max2 * 2))) goto err;
416 if ((s = bc_num_shift(&z1, max2))) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600417 if ((s = bc_num_add(&z0, &z1, &temp, 0))) goto err;
418 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600419
420err:
421 bc_num_free(&temp);
422temp_err:
423 bc_num_free(&z2);
424z2_err:
425 bc_num_free(&z1);
426z1_err:
427 bc_num_free(&z0);
428z0_err:
429 bc_num_free(&m2);
430mix2_err:
431 bc_num_free(&m1);
432mix1_err:
433 bc_num_free(&h2);
434high2_err:
435 bc_num_free(&l2);
436low2_err:
437 bc_num_free(&h1);
438high1_err:
439 bc_num_free(&l1);
440 return s;
441}
442
443BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
444
445 BcStatus s;
446 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600447 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600448
449 scale = BC_MAX(scale, a->rdx);
450 scale = BC_MAX(scale, b->rdx);
451 scale = BC_MIN(a->rdx + b->rdx, scale);
452 maxrdx = BC_MAX(maxrdx, scale);
453
454 if ((s = bc_num_init(&cpa, a->len))) return s;
455 if ((s = bc_num_init(&cpb, b->len))) goto b_err;
456
457 if ((s = bc_num_copy(&cpa, a))) goto err;
458 if ((s = bc_num_copy(&cpb, b))) goto err;
Gavin Howard23d8d252018-10-17 12:03:41 -0600459 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600460 if ((s = bc_num_shift(&cpa, maxrdx))) goto err;
461 if ((s = bc_num_shift(&cpb, maxrdx))) goto err;
462
Gavin Howardb5093d72018-10-17 12:13:17 -0600463 if ((s = bc_num_alg_k(&cpa, &cpb, c))) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600464
Gavin Howard88c25302018-10-17 13:32:23 -0600465 if ((s = bc_num_expand(c, c->len + (maxrdx += scale)))) goto err;
466 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600467 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600468 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600469 }
470
Gavin Howard88c25302018-10-17 13:32:23 -0600471 c->rdx = maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600472 s = bc_num_retireMul(c, scale, a->neg, b->neg);
473
474err:
475 bc_num_free(&cpb);
476b_err:
477 bc_num_free(&cpa);
478 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700479}
480
Gavin Howard8a921bd2018-10-11 14:15:32 -0600481BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700482
Gavin Howard63738202018-09-26 15:34:20 -0600483 BcStatus s;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600484 BcDig *n, *bptr, q;
Gavin Howard63738202018-09-26 15:34:20 -0600485 size_t len, end, i;
486 BcNum cp;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700487
Gavin Howard63738202018-09-26 15:34:20 -0600488 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
489 else if (!a->len) {
490 bc_num_zero(c);
491 return BC_STATUS_SUCCESS;
492 }
493 else if (BC_NUM_ONE(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600494 if ((s = bc_num_copy(c, a))) return s;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600495 return bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600496 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700497
Gavin Howard63738202018-09-26 15:34:20 -0600498 if ((s = bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)))) return s;
499 if ((s = bc_num_copy(&cp, a))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700500
Gavin Howard63738202018-09-26 15:34:20 -0600501 if ((len = b->len) > cp.len) {
502 if ((s = bc_num_expand(&cp, len + 2))) goto err;
503 if ((s = bc_num_extend(&cp, len - cp.len))) goto err;
504 }
Gavin Howard021150b2018-03-10 15:40:42 -0700505
Gavin Howard50651542018-09-29 02:53:11 -0600506 if (b->rdx > cp.rdx && (s = bc_num_extend(&cp, b->rdx - cp.rdx))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600507 cp.rdx -= b->rdx;
Gavin Howard50651542018-09-29 02:53:11 -0600508 if (scale > cp.rdx && (s = bc_num_extend(&cp, scale - cp.rdx))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700509
Gavin Howard63738202018-09-26 15:34:20 -0600510 if (b->rdx == b->len) {
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600511
Gavin Howard63738202018-09-26 15:34:20 -0600512 bool zero = true;
513 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600514
Gavin Howard63738202018-09-26 15:34:20 -0600515 if (i == len && zero) {
516 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
517 goto err;
518 }
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600519
Gavin Howard63738202018-09-26 15:34:20 -0600520 len -= i - 1;
521 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700522
Gavin Howard63738202018-09-26 15:34:20 -0600523 if (cp.cap == cp.len && (s = bc_num_expand(&cp, cp.len + 1))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700524
Gavin Howard63738202018-09-26 15:34:20 -0600525 // We want an extra zero in front to make things simpler.
526 cp.num[cp.len++] = 0;
527 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700528
Gavin Howard63738202018-09-26 15:34:20 -0600529 if ((s = bc_num_expand(c, cp.len))) goto err;
Gavin Howardac7656d2018-03-01 17:18:40 -0700530
Gavin Howard63738202018-09-26 15:34:20 -0600531 bc_num_zero(c);
Gavin Howarda2514a02018-10-18 14:20:09 -0600532 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600533 c->rdx = cp.rdx;
534 c->len = cp.len;
535 bptr = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700536
Gavin Howardc39fd492018-10-04 10:07:03 -0600537 for (i = end - 1; !bcg.signe && !s && i < end; --i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700538
Gavin Howard63738202018-09-26 15:34:20 -0600539 n = cp.num + i;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700540
Gavin Howard63738202018-09-26 15:34:20 -0600541 for (q = 0; (!s && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q)
542 s = bc_num_subArrays(n, bptr, len);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700543
Gavin Howard63738202018-09-26 15:34:20 -0600544 c->num[i] = q;
545 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700546
Gavin Howardd32d7df2018-10-15 08:42:25 -0600547 if (!s) s = bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600548
Gavin Howardb651f1a2018-02-28 17:34:18 -0700549err:
Gavin Howard63738202018-09-26 15:34:20 -0600550 bc_num_free(&cp);
551 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700552}
553
Gavin Howard18441082018-10-22 10:03:30 -0600554BcStatus bc_num_alg_r(BcNum *a, BcNum *b, BcNum *restrict c, BcNum *restrict d,
555 size_t scale, size_t ts)
556{
Gavin Howard63738202018-09-26 15:34:20 -0600557 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600558 BcNum temp;
559 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700560
Gavin Howard63738202018-09-26 15:34:20 -0600561 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
Gavin Howard8b254872018-03-14 01:13:35 -0600562
Gavin Howard63738202018-09-26 15:34:20 -0600563 if (!a->len) {
Gavin Howard18441082018-10-22 10:03:30 -0600564 bc_num_zero(d);
Gavin Howard63738202018-09-26 15:34:20 -0600565 return BC_STATUS_SUCCESS;
566 }
Gavin Howard8b254872018-03-14 01:13:35 -0600567
Gavin Howard18441082018-10-22 10:03:30 -0600568 if ((s = bc_num_init(&temp, d->cap))) return s;
569
570 if ((s = bc_num_alg_d(a, b, c, scale))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700571
Gavin Howard841a3882018-10-04 11:28:14 -0600572 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600573
Gavin Howard18441082018-10-22 10:03:30 -0600574 if ((s = bc_num_alg_m(c, b, &temp, scale))) goto err;
575 if ((s = bc_num_sub(a, &temp, d, scale))) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600576
Gavin Howard18441082018-10-22 10:03:30 -0600577 if (ts > d->rdx && d->len && (s = bc_num_extend(d, ts - d->rdx))) goto err;
578
579 neg = d->neg;
580 s = bc_num_retireMul(d, ts, a->neg, b->neg);
581 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700582
583err:
Gavin Howard18441082018-10-22 10:03:30 -0600584 bc_num_free(&temp);
585 return s;
586}
587
588BcStatus bc_num_alg_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
589
590 BcStatus s;
591 BcNum c1;
592 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
593
594 if ((s = bc_num_init(&c1, len))) return s;
595
596 s = bc_num_alg_r(a, b, &c1, c, scale, ts);
597
Gavin Howard63738202018-09-26 15:34:20 -0600598 bc_num_free(&c1);
599 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700600}
601
Gavin Howard8a921bd2018-10-11 14:15:32 -0600602BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700603
Gavin Howard63738202018-09-26 15:34:20 -0600604 BcStatus s;
605 BcNum copy;
606 unsigned long pow;
607 size_t i, powrdx, resrdx;
608 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700609
Gavin Howard63738202018-09-26 15:34:20 -0600610 if (b->rdx) return BC_STATUS_MATH_NON_INTEGER;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700611
Gavin Howard63738202018-09-26 15:34:20 -0600612 if (!b->len) {
613 bc_num_one(c);
614 return BC_STATUS_SUCCESS;
615 }
616 else if (!a->len) {
617 bc_num_zero(c);
618 return BC_STATUS_SUCCESS;
619 }
620 else if (BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700621
Gavin Howard63738202018-09-26 15:34:20 -0600622 if (!b->neg) s = bc_num_copy(c, a);
623 else s = bc_num_inv(a, c, scale);
Gavin Howard5fc40e72018-03-05 12:11:09 -0700624
Gavin Howard63738202018-09-26 15:34:20 -0600625 return s;
626 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700627
Gavin Howard63738202018-09-26 15:34:20 -0600628 neg = b->neg;
629 b->neg = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700630
Gavin Howard63738202018-09-26 15:34:20 -0600631 if ((s = bc_num_ulong(b, &pow))) return s;
632 if ((s = bc_num_init(&copy, a->len))) return s;
633 if ((s = bc_num_copy(&copy, a))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700634
Gavin Howard63738202018-09-26 15:34:20 -0600635 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600636
Gavin Howard63738202018-09-26 15:34:20 -0600637 b->neg = neg;
Gavin Howardb29674f2018-03-22 22:24:58 -0600638
Gavin Howard63738202018-09-26 15:34:20 -0600639 for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) {
640 powrdx <<= 1;
641 if ((s = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
642 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700643
Gavin Howard63738202018-09-26 15:34:20 -0600644 if ((s = bc_num_copy(c, &copy))) goto err;
645 if (bcg.signe) {
646 s = BC_STATUS_EXEC_SIGNAL;
647 goto err;
648 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700649
Gavin Howardc39fd492018-10-04 10:07:03 -0600650 for (resrdx = powrdx, pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) {
Gavin Howard88c25302018-10-17 13:32:23 -0600651 if ((s = bc_num_mul(&copy, &copy, &copy, (powrdx <<= 1)))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600652 if (pow & 1) {
653 resrdx += powrdx;
654 if ((s = bc_num_mul(c, &copy, c, resrdx))) goto err;
655 }
656 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700657
Gavin Howard63738202018-09-26 15:34:20 -0600658 if (neg && (s = bc_num_inv(c, c, scale))) goto err;
659 if (bcg.signe) {
660 s = BC_STATUS_EXEC_SIGNAL;
661 goto err;
662 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700663
Gavin Howard63738202018-09-26 15:34:20 -0600664 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700665
Gavin Howard63738202018-09-26 15:34:20 -0600666 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
667 if (zero) bc_num_zero(c);
Gavin Howard1d959152018-03-03 23:33:13 -0700668
Gavin Howard6fbdb292018-02-27 15:44:48 -0700669err:
Gavin Howard63738202018-09-26 15:34:20 -0600670 bc_num_free(&copy);
671 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700672}
673
Gavin Howard63738202018-09-26 15:34:20 -0600674BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600675 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700676{
Gavin Howarda1c44392018-09-27 12:41:15 -0600677 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600678 BcNum num2, *ptr_a, *ptr_b;
679 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700680
Gavin Howard63738202018-09-26 15:34:20 -0600681 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700682
Gavin Howard11b9afd2018-10-18 14:23:28 -0600683 if ((init = (c == a))) {
Gavin Howard63738202018-09-26 15:34:20 -0600684 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600685 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600686 }
687 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700688
Gavin Howard63738202018-09-26 15:34:20 -0600689 if (c == b) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700690
Gavin Howardba009802018-09-29 04:41:51 -0600691 ptr_b = &num2;
692
693 if (c != a) {
694 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600695 init = true;
696 }
697 }
698 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700699
Gavin Howarda1c44392018-09-27 12:41:15 -0600700 if (init) s = bc_num_init(c, req);
701 else s = bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700702
Gavin Howard9ab772a2018-10-05 11:38:40 -0600703 if (!s) s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700704
Gavin Howard3cf769e2018-10-11 13:55:11 -0600705 assert(!c->neg || c->len);
706
Gavin Howardba009802018-09-29 04:41:51 -0600707 if (init) bc_num_free(&num2);
Gavin Howarda1c44392018-09-27 12:41:15 -0600708 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700709}
710
Gavin Howard3f68df72018-03-22 20:30:27 -0600711bool bc_num_strValid(const char *val, size_t base) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700712
Gavin Howard8a921bd2018-10-11 14:15:32 -0600713 BcDig b;
Gavin Howard63738202018-09-26 15:34:20 -0600714 bool small, radix = false;
715 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700716
Gavin Howard63738202018-09-26 15:34:20 -0600717 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700718
Gavin Howard63738202018-09-26 15:34:20 -0600719 small = base <= 10;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600720 b = (BcDig) (small ? base + '0' : base - 9 + 'A');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700721
Gavin Howard63738202018-09-26 15:34:20 -0600722 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700723
Gavin Howard8a921bd2018-10-11 14:15:32 -0600724 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600725
Gavin Howard63738202018-09-26 15:34:20 -0600726 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700727
Gavin Howard63738202018-09-26 15:34:20 -0600728 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700729
Gavin Howard63738202018-09-26 15:34:20 -0600730 radix = true;
731 continue;
732 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700733
Gavin Howard63738202018-09-26 15:34:20 -0600734 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
735 return false;
736 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700737
Gavin Howard63738202018-09-26 15:34:20 -0600738 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700739}
740
Gavin Howard3f68df72018-03-22 20:30:27 -0600741BcStatus bc_num_parseDecimal(BcNum *n, const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700742
Gavin Howard63738202018-09-26 15:34:20 -0600743 BcStatus s;
744 size_t len, i;
745 const char *ptr;
746 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700747
Gavin Howard63738202018-09-26 15:34:20 -0600748 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700749
Gavin Howard63738202018-09-26 15:34:20 -0600750 val += i;
751 len = strlen(val);
752 bc_num_zero(n);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700753
Gavin Howard63738202018-09-26 15:34:20 -0600754 if (len) {
755 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
756 if ((s = bc_num_expand(n, len))) return s;
757 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600758
Gavin Howarda2514a02018-10-18 14:20:09 -0600759 if (zero) return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700760
Gavin Howard63738202018-09-26 15:34:20 -0600761 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700762
Gavin Howard63738202018-09-26 15:34:20 -0600763 // Explicitly test for NULL here to produce either a 0 or 1.
764 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700765
Gavin Howard63738202018-09-26 15:34:20 -0600766 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
767 n->num[n->len] = val[i] - '0';
Gavin Howard6fbdb292018-02-27 15:44:48 -0700768
Gavin Howard63738202018-09-26 15:34:20 -0600769 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700770}
771
Gavin Howard3f68df72018-03-22 20:30:27 -0600772BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
Gavin Howardede51f02018-03-02 12:30:00 -0700773
Gavin Howard63738202018-09-26 15:34:20 -0600774 BcStatus s;
775 BcNum temp, mult, result;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600776 BcDig c = '\0';
Gavin Howard11b9afd2018-10-18 14:23:28 -0600777 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600778 unsigned long v;
779 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700780
Gavin Howard63738202018-09-26 15:34:20 -0600781 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700782
Gavin Howard11b9afd2018-10-18 14:23:28 -0600783 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howard63738202018-09-26 15:34:20 -0600784 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700785
Gavin Howard63738202018-09-26 15:34:20 -0600786 if ((s = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return s;
787 if ((s = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700788
Gavin Howard63738202018-09-26 15:34:20 -0600789 for (i = 0; i < len && (c = val[i]) != '.'; ++i) {
Gavin Howardede51f02018-03-02 12:30:00 -0700790
Gavin Howard63738202018-09-26 15:34:20 -0600791 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700792
Gavin Howard31060d92018-10-18 15:07:44 -0600793 if ((s = bc_num_alg_m(n, base, &mult, 0))) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600794 if ((s = bc_num_ulong2num(&temp, v))) goto int_err;
795 if ((s = bc_num_add(&mult, &temp, n, 0))) goto int_err;
796 }
Gavin Howardede51f02018-03-02 12:30:00 -0700797
Gavin Howard63738202018-09-26 15:34:20 -0600798 if (i == len && !(c = val[i])) goto int_err;
799 assert(c == '.');
800 if ((s = bc_num_init(&result, base->len))) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700801
Gavin Howard63738202018-09-26 15:34:20 -0600802 bc_num_zero(&result);
803 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700804
Gavin Howard63738202018-09-26 15:34:20 -0600805 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howardede51f02018-03-02 12:30:00 -0700806
Gavin Howard63738202018-09-26 15:34:20 -0600807 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardede51f02018-03-02 12:30:00 -0700808
Gavin Howard63738202018-09-26 15:34:20 -0600809 if ((s = bc_num_mul(&result, base, &result, 0))) goto err;
810 if ((s = bc_num_ulong2num(&temp, v))) goto err;
811 if ((s = bc_num_add(&result, &temp, &result, 0))) goto err;
812 if ((s = bc_num_mul(&mult, base, &mult, 0))) goto err;
813 }
Gavin Howardede51f02018-03-02 12:30:00 -0700814
Gavin Howard63738202018-09-26 15:34:20 -0600815 if ((s = bc_num_div(&result, &mult, &result, digits))) goto err;
816 if ((s = bc_num_add(n, &result, n, digits))) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600817
Gavin Howard63738202018-09-26 15:34:20 -0600818 if (n->len) {
819 if (n->rdx < digits && n->len) s = bc_num_extend(n, digits - n->rdx);
820 }
821 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700822
823err:
Gavin Howard63738202018-09-26 15:34:20 -0600824 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700825int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600826 bc_num_free(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700827mult_err:
Gavin Howard63738202018-09-26 15:34:20 -0600828 bc_num_free(&temp);
829 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700830}
831
Gavin Howard80977b22018-09-06 14:29:13 -0600832BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) {
Gavin Howard63738202018-09-26 15:34:20 -0600833 if (*nchars == line_len - 1) {
834 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
835 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
836 *nchars = 0;
837 }
Gavin Howard63738202018-09-26 15:34:20 -0600838 return BC_STATUS_SUCCESS;
Gavin Howard80977b22018-09-06 14:29:13 -0600839}
840
Gavin Howard83eb8392018-10-09 01:21:19 -0600841#ifdef DC_ENABLED
842BcStatus bc_num_printChar(size_t num, size_t width, bool radix,
843 size_t *nchars, size_t line_len)
844{
Gavin Howard4c091542018-10-09 03:33:08 -0600845 (void) radix, (void) line_len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600846
Gavin Howard11b9afd2018-10-18 14:23:28 -0600847 if (putchar((char) num) == EOF) return BC_STATUS_IO_ERR;
Gavin Howard83eb8392018-10-09 01:21:19 -0600848 *nchars = *nchars + width;
849
850 return BC_STATUS_SUCCESS;
851}
852#endif // DC_ENABLED
853
Gavin Howard80977b22018-09-06 14:29:13 -0600854BcStatus bc_num_printDigits(size_t num, size_t width, bool radix,
855 size_t *nchars, size_t line_len)
856{
Gavin Howard63738202018-09-26 15:34:20 -0600857 BcStatus s;
Gavin Howardc39fd492018-10-04 10:07:03 -0600858 size_t exp, pow, div;
Gavin Howard80977b22018-09-06 14:29:13 -0600859
Gavin Howard63738202018-09-26 15:34:20 -0600860 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600861 if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR;
862 ++(*nchars);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -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 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600866
Gavin Howard63738202018-09-26 15:34:20 -0600867 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
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 div = num / pow;
870 num -= div * pow;
Gavin Howard63738202018-09-26 15:34:20 -0600871 if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
872 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700873
Gavin Howard63738202018-09-26 15:34:20 -0600874 return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700875}
Gavin Howardfe679f02018-02-14 15:50:09 -0700876
Gavin Howard902a16c2018-05-16 01:22:53 -0600877BcStatus bc_num_printHex(size_t num, size_t width, bool radix,
Gavin Howard0011a3a2018-03-29 23:11:32 -0600878 size_t *nchars, size_t line_len)
Gavin Howard2682a1f2018-03-03 09:09:13 -0700879{
Gavin Howard63738202018-09-26 15:34:20 -0600880 BcStatus s;
Gavin Howard80977b22018-09-06 14:29:13 -0600881
Gavin Howard63738202018-09-26 15:34:20 -0600882 assert(width == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600883
Gavin Howard63738202018-09-26 15:34:20 -0600884 if (radix) {
885 if ((s = bc_num_printNewline(nchars, line_len))) return s;
886 if (putchar('.') == EOF) return BC_STATUS_IO_ERR;
887 *nchars += 1;
888 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700889
Gavin Howard63738202018-09-26 15:34:20 -0600890 if ((s = bc_num_printNewline(nchars, line_len))) return s;
891 if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR;
892 *nchars = *nchars + width;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700893
Gavin Howard63738202018-09-26 15:34:20 -0600894 return BC_STATUS_SUCCESS;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700895}
896
Gavin Howard5f46ee32018-09-25 13:02:43 -0600897BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700898
Gavin Howardc39fd492018-10-04 10:07:03 -0600899 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600900 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700901
Gavin Howard63738202018-09-26 15:34:20 -0600902 if (n->neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
903 (*nchars) += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700904
Gavin Howard63738202018-09-26 15:34:20 -0600905 for (i = n->len - 1; !s && i < n->len; --i)
906 s = bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700907
Gavin Howard63738202018-09-26 15:34:20 -0600908 return s;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700909}
910
Gavin Howard83eb8392018-10-09 01:21:19 -0600911BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, size_t *nchars,
912 size_t line_len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -0600913{
Gavin Howard63738202018-09-26 15:34:20 -0600914 BcStatus s;
915 BcVec stack;
916 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -0600917 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -0600918 size_t i;
919 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700920
Gavin Howard83eb8392018-10-09 01:21:19 -0600921 if ((s = bc_vec_init(&stack, sizeof(long), NULL))) return s;
Gavin Howard63738202018-09-26 15:34:20 -0600922 if ((s = bc_num_init(&intp, n->len))) goto int_err;
923 if ((s = bc_num_init(&fracp, n->rdx))) goto frac_err;
924 if ((s = bc_num_init(&digit, width))) goto digit_err;
925 if ((s = bc_num_init(&frac_len, BC_NUM_INT(n)))) goto frac_len_err;
926 if ((s = bc_num_copy(&intp, n))) goto err;
927 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -0600928
Gavin Howard63738202018-09-26 15:34:20 -0600929 bc_num_truncate(&intp, intp.rdx);
930 if ((s = bc_num_sub(n, &intp, &fracp, 0))) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700931
Gavin Howard4c091542018-10-09 03:33:08 -0600932 if (!n->len) {
933 s = print(0, width, false, nchars, line_len);
934 goto err;
935 }
936
Gavin Howard63738202018-09-26 15:34:20 -0600937 while (intp.len) {
Gavin Howard18441082018-10-22 10:03:30 -0600938 if ((s = bc_num_divmod(&intp, base, &intp, &digit, 0))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600939 if ((s = bc_num_ulong(&digit, &dig))) goto err;
Gavin Howard90198862018-09-29 03:37:47 -0600940 if ((s = bc_vec_push(&stack, &dig))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600941 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700942
Gavin Howard63738202018-09-26 15:34:20 -0600943 for (i = 0; i < stack.len; ++i) {
944 ptr = bc_vec_item_rev(&stack, i);
945 assert(ptr);
946 if ((s = print(*ptr, width, false, nchars, line_len))) goto err;
947 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700948
Gavin Howard63738202018-09-26 15:34:20 -0600949 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700950
Gavin Howard63738202018-09-26 15:34:20 -0600951 for (radix = true; frac_len.len <= n->rdx; radix = false) {
952 if ((s = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err;
953 if ((s = bc_num_ulong(&fracp, &dig))) goto err;
954 if ((s = bc_num_ulong2num(&intp, dig))) goto err;
955 if ((s = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err;
956 if ((s = print(dig, width, radix, nchars, line_len))) goto err;
957 if ((s = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err;
958 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700959
960err:
Gavin Howard63738202018-09-26 15:34:20 -0600961 bc_num_free(&frac_len);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700962frac_len_err:
Gavin Howard63738202018-09-26 15:34:20 -0600963 bc_num_free(&digit);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700964digit_err:
Gavin Howard63738202018-09-26 15:34:20 -0600965 bc_num_free(&fracp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700966frac_err:
Gavin Howard63738202018-09-26 15:34:20 -0600967 bc_num_free(&intp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700968int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600969 bc_vec_free(&stack);
Gavin Howard63738202018-09-26 15:34:20 -0600970 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700971}
972
Gavin Howard83eb8392018-10-09 01:21:19 -0600973BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
974 size_t *nchars, size_t line_len)
975{
976 BcStatus s;
977 size_t width, i;
978 BcNumDigitOp print;
979 bool neg = n->neg;
980
981 if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
982 (*nchars) += neg;
983
984 n->neg = false;
985
986 if (base_t <= BC_NUM_MAX_IBASE) {
987 width = 1;
988 print = bc_num_printHex;
989 }
990 else {
991 for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width);
992 print = bc_num_printDigits;
993 }
994
995 s = bc_num_printNum(n, base, width, nchars, line_len, print);
996 n->neg = neg;
997
998 return s;
999}
1000
1001#ifdef DC_ENABLED
Gavin Howard844e7a52018-10-09 11:46:39 -06001002BcStatus bc_num_stream(BcNum *n, BcNum *base, size_t *nchars, size_t len) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001003 return bc_num_printNum(n, base, 1, nchars, len, bc_num_printChar);
1004}
1005#endif // DC_ENABLED
1006
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001007BcStatus bc_num_init(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -07001008
Gavin Howard63738202018-09-26 15:34:20 -06001009 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -07001010
Gavin Howard63738202018-09-26 15:34:20 -06001011 request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE;
Gavin Howardc39fd492018-10-04 10:07:03 -06001012 memset(n, 0, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001013 if (!(n->num = malloc(request))) return BC_STATUS_ALLOC_ERR;
Gavin Howardb5c77212018-02-14 17:12:34 -07001014
Gavin Howard63738202018-09-26 15:34:20 -06001015 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -07001016
Gavin Howard63738202018-09-26 15:34:20 -06001017 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -07001018}
1019
Gavin Howard844e7a52018-10-09 11:46:39 -06001020BcStatus bc_num_expand(BcNum *n, size_t req) {
Gavin Howardb5c77212018-02-14 17:12:34 -07001021
Gavin Howard8a921bd2018-10-11 14:15:32 -06001022 BcDig *temp;
Gavin Howard9c4358c2018-03-22 20:11:28 -06001023
Gavin Howard844e7a52018-10-09 11:46:39 -06001024 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -07001025
Gavin Howard844e7a52018-10-09 11:46:39 -06001026 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb5c77212018-02-14 17:12:34 -07001027
Gavin Howard844e7a52018-10-09 11:46:39 -06001028 if (req <= n->cap) return BC_STATUS_SUCCESS;
1029 if (!(temp = realloc(n->num, req))) return BC_STATUS_ALLOC_ERR;
1030
Gavin Howard63738202018-09-26 15:34:20 -06001031 n->num = temp;
Gavin Howard844e7a52018-10-09 11:46:39 -06001032 n->cap = req;
Gavin Howardb5c77212018-02-14 17:12:34 -07001033
Gavin Howard63738202018-09-26 15:34:20 -06001034 return BC_STATUS_SUCCESS;
Gavin Howardb5c77212018-02-14 17:12:34 -07001035}
1036
Gavin Howarded392aa2018-02-27 13:09:26 -07001037void bc_num_free(void *num) {
Gavin Howard63738202018-09-26 15:34:20 -06001038 BcNum *n = (BcNum*) num;
Gavin Howard8c019982018-10-05 12:35:09 -06001039 assert(n);
Gavin Howard2bc54182018-10-06 17:03:46 -06001040 free(n->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001041}
1042
Gavin Howard9c4358c2018-03-22 20:11:28 -06001043BcStatus bc_num_copy(BcNum *d, BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -07001044
Gavin Howardb20c2db2018-10-08 10:31:23 -06001045 BcStatus status;
Gavin Howard5a049c42018-02-15 11:24:11 -07001046
Gavin Howard63738202018-09-26 15:34:20 -06001047 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -07001048
Gavin Howard63738202018-09-26 15:34:20 -06001049 if (d == s) return BC_STATUS_SUCCESS;
Gavin Howardb20c2db2018-10-08 10:31:23 -06001050 if ((status = bc_num_expand(d, s->cap))) return status;
Gavin Howard5a049c42018-02-15 11:24:11 -07001051
Gavin Howard63738202018-09-26 15:34:20 -06001052 d->len = s->len;
1053 d->neg = s->neg;
1054 d->rdx = s->rdx;
Gavin Howard5a049c42018-02-15 11:24:11 -07001055
Gavin Howard8a921bd2018-10-11 14:15:32 -06001056 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001057
Gavin Howardb20c2db2018-10-08 10:31:23 -06001058 return status;
Gavin Howard5a049c42018-02-15 11:24:11 -07001059}
1060
Gavin Howard5cea43b2018-03-02 11:32:11 -07001061BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
Gavin Howard025d04d2018-02-20 13:53:28 -07001062
Gavin Howard63738202018-09-26 15:34:20 -06001063 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001064
Gavin Howard63738202018-09-26 15:34:20 -06001065 assert(n && val && base);
1066 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_NUM_MAX_IBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001067
Gavin Howard63738202018-09-26 15:34:20 -06001068 if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001069
Gavin Howard63738202018-09-26 15:34:20 -06001070 if (base_t == 10) s = bc_num_parseDecimal(n, val);
1071 else s = bc_num_parseBase(n, val, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001072
Gavin Howard63738202018-09-26 15:34:20 -06001073 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001074}
1075
Gavin Howard0011a3a2018-03-29 23:11:32 -06001076BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
1077 size_t *nchars, size_t line_len)
Gavin Howard152f3e82018-03-07 12:33:15 -07001078{
Gavin Howard2cb39612018-10-22 08:46:48 -06001079 BcStatus s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001080
Gavin Howard63738202018-09-26 15:34:20 -06001081 assert(n && base && nchars);
1082 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001083
Gavin Howard2cb39612018-10-22 08:46:48 -06001084 if ((s = bc_num_printNewline(nchars, line_len))) return s;
Gavin Howardbc7cae82018-03-14 13:43:04 -06001085
Gavin Howard63738202018-09-26 15:34:20 -06001086 if (!n->len) {
1087 if (putchar('0') == EOF) return BC_STATUS_IO_ERR;
1088 ++(*nchars);
Gavin Howard63738202018-09-26 15:34:20 -06001089 }
1090 else if (base_t == 10) s = bc_num_printDecimal(n, nchars, line_len);
1091 else s = bc_num_printBase(n, base, base_t, nchars, line_len);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001092
Gavin Howard63738202018-09-26 15:34:20 -06001093 if (s) return s;
Gavin Howard152f3e82018-03-07 12:33:15 -07001094
Gavin Howard63738202018-09-26 15:34:20 -06001095 if (newline) {
1096 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
1097 *nchars = 0;
1098 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001099
Gavin Howard63738202018-09-26 15:34:20 -06001100 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001101}
1102
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001103BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001104
Gavin Howard63738202018-09-26 15:34:20 -06001105 size_t i;
1106 unsigned long pow;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001107
Gavin Howard63738202018-09-26 15:34:20 -06001108 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001109
Gavin Howard63738202018-09-26 15:34:20 -06001110 if (n->neg) return BC_STATUS_MATH_NEGATIVE;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001111
Gavin Howard63738202018-09-26 15:34:20 -06001112 for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001113
Gavin Howard63738202018-09-26 15:34:20 -06001114 unsigned long prev = *result, powprev = pow;
Gavin Howard88c25302018-10-17 13:32:23 -06001115
Gavin Howard63738202018-09-26 15:34:20 -06001116 *result += ((unsigned long) n->num[i]) * pow;
1117 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001118
Gavin Howard63738202018-09-26 15:34:20 -06001119 if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW;
1120 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001121
Gavin Howard63738202018-09-26 15:34:20 -06001122 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001123}
1124
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001125BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001126
Gavin Howard63738202018-09-26 15:34:20 -06001127 BcStatus s;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001128 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001129 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001130 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001131
Gavin Howard63738202018-09-26 15:34:20 -06001132 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001133
Gavin Howard63738202018-09-26 15:34:20 -06001134 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001135
Gavin Howarda2514a02018-10-18 14:20:09 -06001136 if (!val) return BC_STATUS_SUCCESS;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001137
Gavin Howardec82bdc2018-10-08 23:50:57 -06001138 for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len)
Gavin Howard63738202018-09-26 15:34:20 -06001139 if ((s = bc_num_expand(n, len))) return s;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001140 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001141
Gavin Howard63738202018-09-26 15:34:20 -06001142 return BC_STATUS_SUCCESS;
Gavin Howard025d04d2018-02-20 13:53:28 -07001143}
1144
Gavin Howard12fe7812018-09-29 03:45:18 -06001145BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001146 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s;
Gavin Howardf8f232b2018-10-11 17:29:47 -06001147 (void) scale;
Gavin Howard12fe7812018-09-29 03:45:18 -06001148 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001149}
1150
Gavin Howard12fe7812018-09-29 03:45:18 -06001151BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001152 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a;
Gavin Howardf8f232b2018-10-11 17:29:47 -06001153 (void) scale;
Gavin Howard12fe7812018-09-29 03:45:18 -06001154 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001155}
1156
Gavin Howard12fe7812018-09-29 03:45:18 -06001157BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001158 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001159 return bc_num_binary(a, b, c, scale, bc_num_alg_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001160}
1161
Gavin Howard12fe7812018-09-29 03:45:18 -06001162BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001163 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001164 return bc_num_binary(a, b, c, scale, bc_num_alg_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001165}
1166
Gavin Howardbe53edd2018-10-04 10:55:08 -06001167BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001168 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howardbe53edd2018-10-04 10:55:08 -06001169 return bc_num_binary(a, b, c, scale, bc_num_alg_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001170}
1171
Gavin Howard12fe7812018-09-29 03:45:18 -06001172BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
1173 return bc_num_binary(a, b, c, scale, bc_num_alg_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001174}
1175
Gavin Howard757b66a2018-10-06 04:13:46 -06001176BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001177
Gavin Howard63738202018-09-26 15:34:20 -06001178 BcStatus s;
1179 BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001180 size_t pow, len, digits, digits1, resrdx, req, times = 0;
1181 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard954276b2018-05-16 01:43:58 -06001182
Gavin Howard757b66a2018-10-06 04:13:46 -06001183 assert(a && b);
Gavin Howard954276b2018-05-16 01:43:58 -06001184
Gavin Howard63738202018-09-26 15:34:20 -06001185 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a)+ 1) >> 1) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001186
Gavin Howard757b66a2018-10-06 04:13:46 -06001187 if (b == a) {
1188 memcpy(&a2, b, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001189 ptr_a = &a2;
Gavin Howard757b66a2018-10-06 04:13:46 -06001190 s = bc_num_init(b, req);
Gavin Howard63738202018-09-26 15:34:20 -06001191 }
1192 else {
1193 ptr_a = a;
Gavin Howard757b66a2018-10-06 04:13:46 -06001194 s = bc_num_expand(b, req);
Gavin Howard63738202018-09-26 15:34:20 -06001195 }
Gavin Howard954276b2018-05-16 01:43:58 -06001196
Gavin Howard63738202018-09-26 15:34:20 -06001197 if (s) goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001198
Gavin Howard63738202018-09-26 15:34:20 -06001199 if (!ptr_a->len) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001200 bc_num_zero(b);
Gavin Howard63738202018-09-26 15:34:20 -06001201 goto init_err;
1202 }
1203 else if (ptr_a->neg) {
Gavin Howard4c4f5f02018-10-11 11:52:11 -06001204 s = BC_STATUS_MATH_NEGATIVE;
Gavin Howard63738202018-09-26 15:34:20 -06001205 goto init_err;
1206 }
1207 else if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001208 bc_num_one(b);
1209 s = bc_num_extend(b, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001210 goto init_err;
1211 }
Gavin Howard954276b2018-05-16 01:43:58 -06001212
Gavin Howard63738202018-09-26 15:34:20 -06001213 len = ptr_a->len;
Gavin Howard954276b2018-05-16 01:43:58 -06001214
Gavin Howard63738202018-09-26 15:34:20 -06001215 scale = BC_MAX(scale, ptr_a->rdx) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001216
Gavin Howard63738202018-09-26 15:34:20 -06001217 if ((s = bc_num_init(&num1, len))) goto init_err;
1218 if ((s = bc_num_init(&num2, num1.len))) goto num2_err;
1219 if ((s = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001220
Gavin Howard63738202018-09-26 15:34:20 -06001221 bc_num_one(&half);
1222 half.num[0] = 5;
1223 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001224
Gavin Howard63738202018-09-26 15:34:20 -06001225 len += scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001226
Gavin Howard63738202018-09-26 15:34:20 -06001227 if ((s = bc_num_init(&f, len))) goto f_err;
1228 if ((s = bc_num_init(&fprime, len + scale))) goto fprime_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001229
Gavin Howard63738202018-09-26 15:34:20 -06001230 x0 = &num1;
1231 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001232
Gavin Howard63738202018-09-26 15:34:20 -06001233 bc_num_one(x0);
Gavin Howard954276b2018-05-16 01:43:58 -06001234
Gavin Howardc39fd492018-10-04 10:07:03 -06001235 if ((pow = BC_NUM_INT(ptr_a))) {
Gavin Howard954276b2018-05-16 01:43:58 -06001236
Gavin Howardc39fd492018-10-04 10:07:03 -06001237 if (pow & 1) x0->num[0] = 2;
1238 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001239
Gavin Howardc39fd492018-10-04 10:07:03 -06001240 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001241
Gavin Howard63738202018-09-26 15:34:20 -06001242 if ((s = bc_num_extend(x0, pow))) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06001243
Gavin Howard63738202018-09-26 15:34:20 -06001244 // Make sure to move the radix back.
1245 x0->rdx -= pow;
1246 }
Gavin Howard954276b2018-05-16 01:43:58 -06001247
Gavin Howard9f2395b2018-10-06 05:28:58 -06001248 x0->rdx = digits = digits1 = 0;
1249 resrdx = scale + 2;
1250 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001251
Gavin Howard0bfcf7a2018-10-11 09:37:09 -06001252 while (!bcg.signe && (cmp || digits < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001253
Gavin Howard31060d92018-10-18 15:07:44 -06001254 if ((s = bc_num_alg_d(a, x0, &f, resrdx))) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001255 if ((s = bc_num_add(x0, &f, &fprime, resrdx))) goto err;
Gavin Howard31060d92018-10-18 15:07:44 -06001256 if ((s = bc_num_alg_m(&fprime, &half, x1, resrdx))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001257
Gavin Howard63738202018-09-26 15:34:20 -06001258 cmp = bc_num_cmp(x1, x0);
1259 digits = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001260
Gavin Howardb11e9e22018-10-06 17:26:02 -06001261 if (cmp == cmp2 && digits == digits1) times += 1;
1262 else times = 0;
1263
1264 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001265
1266 cmp2 = cmp1;
1267 cmp1 = cmp;
1268 digits1 = digits;
1269
Gavin Howard63738202018-09-26 15:34:20 -06001270 temp = x0;
1271 x0 = x1;
1272 x1 = temp;
1273 }
Gavin Howard954276b2018-05-16 01:43:58 -06001274
Gavin Howard63738202018-09-26 15:34:20 -06001275 if (bcg.signe) {
1276 s = BC_STATUS_EXEC_SIGNAL;
1277 goto err;
1278 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001279
Gavin Howard757b66a2018-10-06 04:13:46 -06001280 if ((s = bc_num_copy(b, x0))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001281
Gavin Howard757b66a2018-10-06 04:13:46 -06001282 if (b->rdx > --scale) bc_num_truncate(b, b->rdx - scale);
1283 else if (b->rdx < scale) s = bc_num_extend(b, scale - b->rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06001284
1285err:
Gavin Howard63738202018-09-26 15:34:20 -06001286 bc_num_free(&fprime);
Gavin Howard954276b2018-05-16 01:43:58 -06001287fprime_err:
Gavin Howard63738202018-09-26 15:34:20 -06001288 bc_num_free(&f);
Gavin Howard954276b2018-05-16 01:43:58 -06001289f_err:
Gavin Howard63738202018-09-26 15:34:20 -06001290 bc_num_free(&half);
Gavin Howard954276b2018-05-16 01:43:58 -06001291two_err:
Gavin Howard63738202018-09-26 15:34:20 -06001292 bc_num_free(&num2);
Gavin Howard954276b2018-05-16 01:43:58 -06001293num2_err:
Gavin Howard63738202018-09-26 15:34:20 -06001294 bc_num_free(&num1);
Gavin Howard954276b2018-05-16 01:43:58 -06001295init_err:
Gavin Howard757b66a2018-10-06 04:13:46 -06001296 if (b == a) bc_num_free(&a2);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001297 assert(!b->neg || b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001298 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001299}
Gavin Howardba009802018-09-29 04:41:51 -06001300
Gavin Howard2cb39612018-10-22 08:46:48 -06001301BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1302
1303 BcStatus s;
1304 BcNum num2, *ptr_a;
1305 bool init;
1306 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
1307
1308 assert(c != d && a != b && a != d && b != d && b != c);
1309
1310 if ((init = (c == a))) {
1311 memcpy(&num2, c, sizeof(BcNum));
1312 ptr_a = &num2;
1313 }
1314 else ptr_a = a;
1315
1316 if (init) s = bc_num_init(c, len);
1317 else s = bc_num_expand(d, ptr_a->len);
1318
1319 if (s) return s;
1320
Gavin Howard18441082018-10-22 10:03:30 -06001321 s = bc_num_alg_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001322
1323 assert(!c->neg || c->len);
1324 assert(!d->neg || d->len);
1325
1326 if (init) bc_num_free(&num2);
1327
1328 return s;
1329}
1330
Gavin Howarde6e84762018-10-03 11:46:34 -06001331#ifdef DC_ENABLED
Gavin Howardba009802018-09-29 04:41:51 -06001332BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1333
1334 BcStatus s;
Gavin Howard2c8959e2018-10-04 18:32:58 -06001335 BcNum num2, *ptr_a, *ptr_b, *ptr_c, base, exp, two, temp;
Gavin Howard2cb39612018-10-22 08:46:48 -06001336 bool init;
Gavin Howardba009802018-09-29 04:41:51 -06001337
1338 assert(a && b && c && d);
1339
1340 scale = 0;
1341
Gavin Howard11b9afd2018-10-18 14:23:28 -06001342 if ((init = (d == a))) {
Gavin Howardba009802018-09-29 04:41:51 -06001343 memcpy(&num2, d, sizeof(BcNum));
1344 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001345 }
1346 else ptr_a = a;
1347
1348 if (d == b) {
1349
1350 ptr_b = &num2;
1351
1352 if (d != a) {
1353 memcpy(ptr_b, d, sizeof(BcNum));
1354 init = true;
1355 }
1356 }
1357 else ptr_b = b;
1358
1359 if (d == c) {
1360
1361 ptr_c = &num2;
1362
1363 if (d != a && d != b) {
1364 memcpy(ptr_c, d, sizeof(BcNum));
1365 init = true;
1366 }
1367 }
1368 else ptr_c = c;
1369
1370 if (init) s = bc_num_init(d, ptr_c->len);
1371 else s = bc_num_expand(d, ptr_c->len);
1372
1373 if (s) return s;
1374
1375 if (!ptr_c->len) {
1376 s = BC_STATUS_MATH_DIVIDE_BY_ZERO;
1377 goto base_err;
1378 }
1379
Gavin Howard5ef610b2018-10-04 16:57:47 -06001380 if (ptr_a->rdx || ptr_b->rdx || ptr_c->rdx) {
Gavin Howardba009802018-09-29 04:41:51 -06001381 s = BC_STATUS_MATH_NON_INTEGER;
1382 goto base_err;
1383 }
1384
1385 if (ptr_b->neg) {
1386 s = BC_STATUS_MATH_NEGATIVE;
1387 goto base_err;
1388 }
1389
Gavin Howardba009802018-09-29 04:41:51 -06001390 if ((s = bc_num_init(&base, ptr_c->len))) goto base_err;
1391 if ((s = bc_num_init(&exp, ptr_b->len))) goto exp_err;
1392 if ((s = bc_num_init(&two, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howardba009802018-09-29 04:41:51 -06001393 if ((s = bc_num_init(&temp, ptr_b->len))) goto temp_err;
1394
1395 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001396 two.num[0] = 2;
1397 bc_num_one(d);
1398
Gavin Howard31060d92018-10-18 15:07:44 -06001399 if ((s = bc_num_alg_rem(ptr_a, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001400 if ((s = bc_num_copy(&exp, ptr_b))) goto err;
1401
Gavin Howardaf905602018-09-29 18:56:37 -06001402 while (exp.len) {
Gavin Howardba009802018-09-29 04:41:51 -06001403
Gavin Howard18441082018-10-22 10:03:30 -06001404 if ((s = bc_num_divmod(&exp, &two, &exp, &temp, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001405
1406 if (BC_NUM_ONE(&temp)) {
Gavin Howard31060d92018-10-18 15:07:44 -06001407 if ((s = bc_num_alg_m(d, &base, &temp, scale))) goto err;
1408 if ((s = bc_num_alg_rem(&temp, ptr_c, d, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001409 }
1410
Gavin Howard31060d92018-10-18 15:07:44 -06001411 if ((s = bc_num_alg_m(&base, &base, &temp, scale))) goto err;
1412 if ((s = bc_num_alg_rem(&temp, ptr_c, &base, scale))) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001413 }
1414
1415err:
1416 bc_num_free(&temp);
1417temp_err:
Gavin Howardba009802018-09-29 04:41:51 -06001418 bc_num_free(&two);
1419two_err:
1420 bc_num_free(&exp);
1421exp_err:
1422 bc_num_free(&base);
1423base_err:
1424 if (init) bc_num_free(&num2);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001425 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001426 return s;
1427}
Gavin Howarde6e84762018-10-03 11:46:34 -06001428#endif // DC_ENABLED