blob: 45fcf030051a3d8efd40d1412b0fe82cf827d04b [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 Howard7345cb92019-04-08 14:13:43 -06004 * Copyright (c) 2018-2019 Gavin D. Howard and contributors.
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
Gavin Howard7345cb92019-04-08 14:13:43 -06006 * All rights reserved.
Gavin Howard3eb626f2018-02-14 13:54:35 -07007 *
Gavin Howard7345cb92019-04-08 14:13:43 -06008 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
Gavin Howard3eb626f2018-02-14 13:54:35 -070029 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070030 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070031 *
32 * Code for the number type.
33 *
34 */
35
36#include <assert.h>
Gavin Howardc7d655b2018-12-28 19:45:13 -070037#include <ctype.h>
Gavin Howard411f7322018-09-26 17:21:19 -060038#include <stdbool.h>
39#include <stdlib.h>
40#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070041
Gavin Howard305249a2018-10-15 20:24:47 -060042#include <limits.h>
43
Gavin Howard29493062018-03-20 19:57:37 -060044#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070045#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060046#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070047
Gavin Howard55c8f732019-04-24 14:05:27 -060048#if BC_DEBUG_CODE
49static void bc_num_printDecimal(const BcNum *restrict a);
50
Gavin Howard5b40e402019-04-27 07:26:35 -060051static void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline)
52{
Gavin Howard55c8f732019-04-24 14:05:27 -060053 printf("%s: ", name);
54 bc_num_printDecimal(n);
55 printf("\n");
Gavin Howard5b40e402019-04-27 07:26:35 -060056 if (emptyline) printf("\n");
Gavin Howard55c8f732019-04-24 14:05:27 -060057 vm->nchars = 0;
58}
Stefan Eßer4b111c22019-04-24 17:23:08 -060059
Gavin Howard5b40e402019-04-27 07:26:35 -060060static void bc_num_printDigs(const BcNum *n, const char *name, bool emptyline) {
61
62 size_t i;
63
Gavin Howard37118f42019-04-27 07:28:24 -060064 printf("%s len: %zu, rdx: %zu, scale: %zu\n",
65 name, n->len, n->rdx, n->scale);
Gavin Howard5b40e402019-04-27 07:26:35 -060066
67 for (i = n->len - 1; i < n->len; --i)
68 printf(" %0*d", BC_BASE_POWER, n->num[i]);
69
70 printf("\n");
71 if (emptyline) printf("\n");
72 vm->nchars = 0;
73}
74
75static void bc_num_dump(const char *varname, const BcNum *n) {
Stefan Eßer4b111c22019-04-24 17:23:08 -060076
Gavin Howardc4783ac2019-04-26 10:40:02 -060077 unsigned long i;
Stefan Eßer4b111c22019-04-24 17:23:08 -060078
Stefan Eßerdaaaaa72019-04-27 18:04:02 -060079 fprintf(stderr, "\n%s = %s", varname, n->len ? (n->neg ? "-" : "+") : "0 ");
Stefan Eßer4b111c22019-04-24 17:23:08 -060080
Gavin Howardc4783ac2019-04-26 10:40:02 -060081 for (i = n->len -1; i < n->len; i--) {
Gavin Howardc88e5232019-04-27 07:33:16 -060082 if (i + 1 == n->rdx) fprintf(stderr, ". ");
Stefan Esser193f8522019-04-27 01:29:01 +020083 fprintf(stderr, "%0*d ", BC_BASE_POWER, n->num[i]);
Stefan Eßer4b111c22019-04-24 17:23:08 -060084 }
85
Gavin Howard948b6392019-04-27 07:53:23 -060086 fprintf(stderr, "(%p | %zu.%zu/%zu)\n",
87 (void*) n->num, n->len, n->rdx, n->cap);
Stefan Eßer4b111c22019-04-24 17:23:08 -060088}
Gavin Howard55c8f732019-04-24 14:05:27 -060089#endif // BC_DEBUG_CODE
90
Gavin Howard49ea7b62019-04-23 14:46:26 -060091static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
92
Gavin Howard56ebcf72019-02-21 16:59:48 -070093static ssize_t bc_num_neg(size_t n, bool neg) {
94 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
95}
96
Gavin Howardd2cf06a2019-02-21 17:03:24 -070097ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -070098 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -070099}
100
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700101static size_t bc_num_int(const BcNum *n) {
102 return n->len ? n->len - n->rdx : 0;
103}
104
Gavin Howarda83a5e52019-02-21 16:51:06 -0700105static bool bc_num_isOne(const BcNum *n) {
106 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
107}
108
Gavin Howard798508b2019-02-21 16:45:32 -0700109static void bc_num_expand(BcNum *restrict n, size_t req) {
110 assert(n);
111 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
112 if (req > n->cap) {
Gavin Howard404ece72019-04-26 15:59:15 -0600113 n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req));
Gavin Howard798508b2019-02-21 16:45:32 -0700114 n->cap = req;
115 }
116}
117
Gavin Howard1cbfe242019-01-09 17:13:11 -0700118static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600119 assert(n);
Gavin Howardfcf29012019-04-25 20:12:19 -0600120 n->scale = scale;
121 n->len = n->rdx = 0;
Gavin Howard97102b12018-11-01 23:37:31 -0600122 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600123}
124
Gavin Howard1cbfe242019-01-09 17:13:11 -0700125static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600126 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -0600127}
128
Gavin Howard3c02ed32018-12-28 18:17:15 -0700129void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600130 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -0600131 n->len = 1;
132 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -0600133}
134
Gavin Howard3c02ed32018-12-28 18:17:15 -0700135void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600136 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600137 bc_num_setToZero(n, 0);
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600138#if BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600139 n->len = 2;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600140 n->num[0] = 0;
141 n->num[1] = 1;
142#else // BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600143 n->len = 1;
Stefan Eßer4b111c22019-04-24 17:23:08 -0600144 n->num[0] = BC_BASE;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600145#endif // BC_BASE_DIG == 10
Gavin Howard0be26ed2018-08-31 20:21:56 -0600146}
147
Gavin Howard1cbfe242019-01-09 17:13:11 -0700148static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -0700149 size_t len;
Gavin Howard530e3072019-04-23 16:23:36 -0600150 for (len = 1; i; i /= BC_BASE, ++len);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700151 return len;
152}
153
Gavin Howard971a2672019-04-26 14:32:07 -0600154static unsigned long bc_num_pow10(unsigned long i) {
155 unsigned long pow;
156 for (pow = 1; i; --i, pow *= BC_BASE);
157 return pow;
158}
159
Gavin Howard08f78562019-04-29 07:56:04 -0600160static size_t bc_num_nonzeroIdx(const BcNum *restrict n) {
161 size_t i, len = n->len;
162 assert(len == n->rdx);
163 for (i = len - 1; i < n->len && !n->num[i]; --len, --i);
164 return len;
165}
166
Gavin Howardfcf29012019-04-25 20:12:19 -0600167static unsigned long bc_num_addDigit(BcDig *restrict num, unsigned long d,
168 unsigned long c)
Gavin Howard14c354a2019-04-24 14:28:24 -0600169{
170 d += c;
Gavin Howard0700efc2019-04-24 17:31:23 -0600171 *num = (BcDig) (d % BC_BASE_DIG);
172 assert(*num >= 0 && *num < BC_BASE_DIG);
173 return d / BC_BASE_DIG;
Gavin Howard14c354a2019-04-24 14:28:24 -0600174}
175
176static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
177 size_t len)
178{
179 size_t i;
Gavin Howard971a2672019-04-26 14:32:07 -0600180 unsigned long carry = 0;
Gavin Howard14c354a2019-04-24 14:28:24 -0600181
182 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard971a2672019-04-26 14:32:07 -0600183 unsigned long in = ((unsigned long) a[i]) + ((unsigned long) b[i]);
Gavin Howard14c354a2019-04-24 14:28:24 -0600184 carry = bc_num_addDigit(a + i, in, carry);
185 }
186
187 for (; BC_NO_SIG && carry; ++i)
Gavin Howard971a2672019-04-26 14:32:07 -0600188 carry = bc_num_addDigit(a + i, (unsigned long) a[i], carry);
Gavin Howard14c354a2019-04-24 14:28:24 -0600189
190 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
191}
192
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700193static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
194 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700195{
Gavin Howard63738202018-09-26 15:34:20 -0600196 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600197
Gavin Howard2d188a52019-02-25 14:19:08 -0700198 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600199
Gavin Howard2d188a52019-02-25 14:19:08 -0700200 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600201 assert(a[i + j] >= -BC_BASE_DIG);
202 a[i + j++] += BC_BASE_DIG;
Gavin Howard54b946a2018-10-23 12:06:57 -0600203 a[i + j] -= 1;
Gavin Howard0700efc2019-04-24 17:31:23 -0600204 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_DIG);
Gavin Howard63738202018-09-26 15:34:20 -0600205 }
206 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600207
Gavin Howard2d188a52019-02-25 14:19:08 -0700208 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600209}
210
Gavin Howard2d188a52019-02-25 14:19:08 -0700211static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
212 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700213{
Gavin Howard63738202018-09-26 15:34:20 -0600214 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600215 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700216 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600217 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600218}
219
Gavin Howard3c02ed32018-12-28 18:17:15 -0700220ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700221
Gavin Howard63738202018-09-26 15:34:20 -0600222 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600223 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600224 bool a_max, neg = false;
225 ssize_t cmp;
226
227 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700228
Gavin Howard8a921bd2018-10-11 14:15:32 -0600229 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700230 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700231 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600232 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600233 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600234 else return -1;
235 }
236 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700237
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700238 a_int = bc_num_int(a);
239 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600240 a_int -= b_int;
241 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700242
Gavin Howard1ab22d22019-01-03 13:32:17 -0700243 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700244
Gavin Howard890d0c02018-10-30 16:34:50 -0600245 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600246 min = b->rdx;
247 diff = a->rdx - b->rdx;
248 max_num = a->num + diff;
249 min_num = b->num;
250 }
251 else {
252 min = a->rdx;
253 diff = b->rdx - a->rdx;
254 max_num = b->num + diff;
255 min_num = a->num;
256 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700257
Gavin Howard890d0c02018-10-30 16:34:50 -0600258 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600259 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700260 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700261
Gavin Howard2d188a52019-02-25 14:19:08 -0700262 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700263 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600264 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700265
Gavin Howardf2ee6342019-04-09 17:05:20 -0600266 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700267}
268
Gavin Howard2a366922019-01-16 10:50:20 -0700269static void bc_num_clean(BcNum *restrict n) {
270 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
271 if (BC_NUM_ZERO(n)) n->neg = false;
272 else if (n->len < n->rdx) n->len = n->rdx;
273}
274
Gavin Howard3c02ed32018-12-28 18:17:15 -0700275void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700276
Gavin Howardb1558662019-04-26 14:32:55 -0600277 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700278
Stefan Esser70f11852019-04-27 10:39:00 +0200279 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700280
Gavin Howardb1558662019-04-26 14:32:55 -0600281 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
282 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
283
284 n->scale -= places;
285 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700286
Gavin Howardddce6d42019-01-03 14:07:06 -0700287 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600288
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600289 size_t pow;
290
291 pow = n->scale % BC_BASE_POWER;
292 pow = pow ? BC_BASE_POWER - pow : 0;
293 pow = bc_num_pow10(pow);
Gavin Howardb1558662019-04-26 14:32:55 -0600294
295 n->len -= places_rdx;
Gavin Howard404ece72019-04-26 15:59:15 -0600296 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
Gavin Howardb1558662019-04-26 14:32:55 -0600297
298 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600299 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600300
Gavin Howard2a366922019-01-16 10:50:20 -0700301 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600302 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700303}
304
Stefan Esser70f11852019-04-27 10:39:00 +0200305static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700306
Gavin Howardb1558662019-04-26 14:32:55 -0600307 size_t places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700308
Gavin Howard1ab22d22019-01-03 13:32:17 -0700309 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700310
Gavin Howardb1558662019-04-26 14:32:55 -0600311 bc_num_expand(n, n->len + places_rdx);
Gavin Howard404ece72019-04-26 15:59:15 -0600312 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
313 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
Gavin Howarde6f326e2019-04-26 10:13:45 -0600314 n->rdx += places_rdx;
315 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600316 n->len += places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700317
Gavin Howarde6f326e2019-04-26 10:13:45 -0600318 assert(n->rdx == BC_NUM_RDX(n->scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200319}
320
Gavin Howard2d188a52019-02-25 14:19:08 -0700321static void bc_num_retireMul(BcNum *restrict n, size_t scale,
322 bool neg1, bool neg2)
323{
Gavin Howard971a2672019-04-26 14:32:07 -0600324 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
325 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600326
Gavin Howard305249a2018-10-15 20:24:47 -0600327 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700328 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600329}
330
Gavin Howard2d188a52019-02-25 14:19:08 -0700331static void bc_num_split(const BcNum *restrict n, size_t idx,
332 BcNum *restrict a, BcNum *restrict b)
333{
Gavin Howardfcf29012019-04-25 20:12:19 -0600334 // TODO: Check this function.
Gavin Howard305249a2018-10-15 20:24:47 -0600335 if (idx < n->len) {
336
Gavin Howard305249a2018-10-15 20:24:47 -0600337 b->len = n->len - idx;
338 a->len = idx;
Gavin Howarda3f260c2019-04-26 15:47:25 -0600339 a->scale = a->rdx = b->scale = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600340
Gavin Howard404ece72019-04-26 15:59:15 -0600341 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
342 memcpy(a->num, n->num, BC_NUM_SIZE(idx));
Gavin Howard50c80022019-01-03 15:14:33 -0700343
344 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600345 }
Gavin Howard50c80022019-01-03 15:14:33 -0700346 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600347
348 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600349}
350
Gavin Howard14913fc2019-04-23 14:36:03 -0600351static size_t bc_num_shiftZero(BcNum *restrict n) {
352
353 size_t i;
354
355 assert(!n->rdx || BC_NUM_ZERO(n));
356
357 for (i = 0; i < n->len && !n->num[i]; ++i);
358
359 n->len -= i;
360 n->num += i;
361
362 return i;
363}
364
Gavin Howard2e736de2019-04-27 06:41:58 -0600365static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
366 n->len += places_rdx;
367 n->num -= places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600368}
369
Gavin Howard48a0e472019-04-26 15:39:46 -0600370static BcStatus bc_num_shift(BcNum *restrict n, BcDig *restrict ptr,
371 unsigned long dig, size_t len)
372{
Gavin Howardb1558662019-04-26 14:32:55 -0600373 size_t i;
374 unsigned long carry = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600375
Gavin Howardb1558662019-04-26 14:32:55 -0600376 assert(dig < BC_BASE_POWER);
Gavin Howard305249a2018-10-15 20:24:47 -0600377
Gavin Howardb1558662019-04-26 14:32:55 -0600378 dig = bc_num_pow10(dig);
379
380 for (i = 0; BC_NO_SIG && i < len; ++i) {
381 unsigned long in = ((unsigned long) ptr[i]) * dig;
382 carry = bc_num_addDigit(ptr + i, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600383 }
384
Gavin Howardb1558662019-04-26 14:32:55 -0600385 if (carry) {
386 bc_num_expand(n, n->len + 1);
387 assert(carry < BC_BASE_DIG);
388 n->num[n->len] = (BcDig) carry;
389 n->len += 1;
390 }
391
392 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600393}
394
Gavin Howardb1558662019-04-26 14:32:55 -0600395static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700396
Gavin Howardb1558662019-04-26 14:32:55 -0600397 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600398 unsigned long dig;
399 bool shift;
400 size_t places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600401
Gavin Howardb1558662019-04-26 14:32:55 -0600402 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600403
Gavin Howard48a0e472019-04-26 15:39:46 -0600404 dig = (unsigned long) (places % BC_BASE_POWER);
405 shift = (dig != 0);
406 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx - shift;
407
Gavin Howardb1558662019-04-26 14:32:55 -0600408 if (n->scale >= places) {
409 n->scale -= places;
410 n->rdx = BC_NUM_RDX(n->scale);
411 }
412 else if (BC_NUM_NONZERO(n)) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600413
Stefan Esser6189d952019-04-27 10:40:14 +0200414 if (places_rdx > n->rdx) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600415 bc_num_expand(n, n->len + places_rdx - n->rdx + shift);
Gavin Howard404ece72019-04-26 15:59:15 -0600416 memmove(n->num + places_rdx - n->rdx, n->num, BC_NUM_SIZE(n->len));
417 memset(n->num, 0, BC_NUM_SIZE(places_rdx - n->rdx));
Gavin Howard48a0e472019-04-26 15:39:46 -0600418 n->len += places_rdx;
419 }
420
Gavin Howardb1558662019-04-26 14:32:55 -0600421 n->scale = n->rdx = 0;
422 }
423 else n->scale = 0;
424
425 if (shift) s = bc_num_shift(n, n->num + places_rdx, dig, n->len - places_rdx);
426
427 bc_num_clean(n);
428
429 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
430}
431
432static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
433
434 BcStatus s = BC_STATUS_SUCCESS;
435 unsigned long dig = (unsigned long) (places % BC_BASE_POWER);
436 bool shift = (dig != 0);
437 size_t len, places_rdx;
438
439 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600440 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600441 n->scale += places;
442 bc_num_expand(n, BC_NUM_RDX(n->scale));
443 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600444 }
445
Gavin Howardb1558662019-04-26 14:32:55 -0600446 places_rdx = BC_NUM_RDX(places);
Gavin Howard36ec9872019-04-29 10:14:21 -0600447 len = bc_vm_growSize(n->rdx, places_rdx);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700448
449 if (len > n->len) {
450
451 if (len > n->cap) bc_num_expand(n, len);
452
Gavin Howard404ece72019-04-26 15:59:15 -0600453 memset(n->num + n->len, 0, BC_NUM_SIZE(len - n->len));
Gavin Howard7ad5a662019-02-19 14:40:46 -0700454 n->len = len;
455 }
456
Gavin Howardb1558662019-04-26 14:32:55 -0600457 n->scale += places;
458 n->rdx += places_rdx;
459
460 if (shift) s = bc_num_shift(n, n->num, BC_BASE_POWER - dig, n->len);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700461
462 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600463 assert(n->rdx == BC_NUM_RDX(n->scale));
464
465 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700466}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700467
Gavin Howard1cbfe242019-01-09 17:13:11 -0700468static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600469
Gavin Howard63738202018-09-26 15:34:20 -0600470 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600471 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600472
Gavin Howard971a2672019-04-26 14:32:07 -0600473 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700474
Gavin Howardd32d7df2018-10-15 08:42:25 -0600475 one.cap = 2;
476 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600477 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600478
Gavin Howardd32d7df2018-10-15 08:42:25 -0600479 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600480}
481
Gavin Howard7bda4782018-12-28 09:53:22 -0700482#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700483static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
484 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700485{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700486 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700487 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700488 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700489}
Gavin Howard7bda4782018-12-28 09:53:22 -0700490#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700491
Gavin Howard1cbfe242019-01-09 17:13:11 -0700492static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700493
Gavin Howard8a921bd2018-10-11 14:15:32 -0600494 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600495 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard6fccd222019-04-26 10:17:02 -0600496 unsigned long carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700497
Gavin Howard63738202018-09-26 15:34:20 -0600498 // Because this function doesn't need to use scale (per the bc spec),
499 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600500
Gavin Howardddce6d42019-01-03 14:07:06 -0700501 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600502 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700503 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600504 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600505 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700506 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600507 bc_num_copy(c, a);
508 return BC_STATUS_SUCCESS;
509 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600510
Gavin Howard63738202018-09-26 15:34:20 -0600511 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600512 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600513 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600514 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700515
Gavin Howard63738202018-09-26 15:34:20 -0600516 if (a->rdx > b->rdx) {
517 diff = a->rdx - b->rdx;
518 ptr = a->num;
519 ptr_a = a->num + diff;
520 ptr_b = b->num;
521 }
522 else {
523 diff = b->rdx - a->rdx;
524 ptr = b->num;
525 ptr_a = a->num;
526 ptr_b = b->num + diff;
527 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700528
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700529 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700530
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700531 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600532 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700533 a_int = bc_num_int(a);
534 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700535
Gavin Howard890d0c02018-10-30 16:34:50 -0600536 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600537 min_int = b_int;
538 max = a_int;
539 ptr = ptr_a;
540 }
541 else {
542 min_int = a_int;
543 max = b_int;
544 ptr = ptr_b;
545 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700546
Gavin Howard2d188a52019-02-25 14:19:08 -0700547 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard6fccd222019-04-26 10:17:02 -0600548 unsigned long in;
549 in = ((unsigned long) ptr_a[i]) + ((unsigned long) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700550 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600551 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700552
Gavin Howard2d188a52019-02-25 14:19:08 -0700553 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard6fccd222019-04-26 10:17:02 -0600554 carry = bc_num_addDigit(ptr_c + i, (unsigned long) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700555
556 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700557
Gavin Howard1ab22d22019-01-03 13:32:17 -0700558 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600559
Gavin Howard2d188a52019-02-25 14:19:08 -0700560 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700561}
562
Gavin Howard1cbfe242019-01-09 17:13:11 -0700563static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700564
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700565 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600566 ssize_t cmp;
567 BcNum *minuend, *subtrahend;
568 size_t start;
569 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700570
Gavin Howard63738202018-09-26 15:34:20 -0600571 // Because this function doesn't need to use scale (per the bc spec),
572 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700573
Gavin Howardddce6d42019-01-03 14:07:06 -0700574 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600575 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700576 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600577 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600578 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700579 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600580 bc_num_copy(c, a);
581 return BC_STATUS_SUCCESS;
582 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700583
Gavin Howard63738202018-09-26 15:34:20 -0600584 aneg = a->neg;
585 bneg = b->neg;
586 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700587
Gavin Howard63738202018-09-26 15:34:20 -0600588 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600589 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700590
Gavin Howard63738202018-09-26 15:34:20 -0600591 a->neg = aneg;
592 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700593
Gavin Howard1ab22d22019-01-03 13:32:17 -0700594 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600595 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600596 return BC_STATUS_SUCCESS;
597 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700598
599 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600600 neg = a->neg;
601 minuend = a;
602 subtrahend = b;
603 }
604 else {
605 neg = b->neg;
606 if (sub) neg = !neg;
607 minuend = b;
608 subtrahend = a;
609 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700610
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600611 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600612 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700613
Gavin Howarde6f326e2019-04-26 10:13:45 -0600614 if (c->scale < subtrahend->scale) {
615 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600616 start = 0;
617 }
618 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700619
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700620 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700621
Gavin Howard63738202018-09-26 15:34:20 -0600622 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700623
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700624 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700625}
626
Gavin Howarddc4e3712019-04-23 14:56:29 -0600627static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
628{
Gavin Howardfcf29012019-04-25 20:12:19 -0600629 // TODO: Check this function.
Gavin Howard971a2672019-04-26 14:32:07 -0600630 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600631 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard971a2672019-04-26 14:32:07 -0600632 unsigned long sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600633
Gavin Howard971a2672019-04-26 14:32:07 -0600634 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600635 assert(!a->rdx && !b->rdx);
636
Gavin Howard5083aec2019-04-23 18:03:46 -0600637 clen = bc_vm_growSize(alen, blen);
638 bc_num_expand(c, clen + 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600639
640 ptr_c = c->num;
Gavin Howard404ece72019-04-26 15:59:15 -0600641 memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600642
Gavin Howard5083aec2019-04-23 18:03:46 -0600643 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600644
Gavin Howard5083aec2019-04-23 18:03:46 -0600645 ssize_t sidx = (ssize_t) (i - blen + 1);
646 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600647
Stefan Essera299ffc2019-04-24 23:28:32 +0200648 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
Stefan Eßer4b111c22019-04-24 17:23:08 -0600649
Gavin Howard971a2672019-04-26 14:32:07 -0600650 sum += ((unsigned long) ptr_a[j]) * ((unsigned long) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600651
Gavin Howard0700efc2019-04-24 17:31:23 -0600652 if (sum >= BC_BASE_DIG) {
653 carry += sum / BC_BASE_DIG;
654 sum %= BC_BASE_DIG;
Stefan Essera299ffc2019-04-24 23:28:32 +0200655 }
656 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600657
Stefan Essera299ffc2019-04-24 23:28:32 +0200658 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600659 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Essera299ffc2019-04-24 23:28:32 +0200660 sum = carry;
661 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600662 }
663
Gavin Howard5083aec2019-04-23 18:03:46 -0600664 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600665 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600666 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600667 clen += 1;
668 }
669
670 c->len = clen;
671
Gavin Howarddc4e3712019-04-23 14:56:29 -0600672 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
673}
674
Gavin Howard14c354a2019-04-24 14:28:24 -0600675static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
676 size_t shift, BcNumShiftAddOp op)
677{
Gavin Howardfcf29012019-04-25 20:12:19 -0600678 // TODO: Check this function.
Gavin Howard14c354a2019-04-24 14:28:24 -0600679 assert(n->len >= shift + a->len);
680 assert(!n->rdx && !a->rdx);
681 return op(n->num + shift, a->num, a->len);
682}
683
684static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600685
Gavin Howardfcf29012019-04-25 20:12:19 -0600686 // TODO: Check this function.
687
Gavin Howard9b801632019-02-16 23:34:06 -0700688 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200689 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600690 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700691 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600692 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700693 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600694
Gavin Howard14913fc2019-04-23 14:36:03 -0600695 assert(BC_NUM_ZERO(c));
696
Gavin Howardddce6d42019-01-03 14:07:06 -0700697 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600698 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700699 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600700 bc_num_zero(c);
701 return BC_STATUS_SUCCESS;
702 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700703 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600704 bc_num_copy(c, aone ? b : a);
705 return BC_STATUS_SUCCESS;
706 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700707
Gavin Howardb5093d72018-10-17 12:13:17 -0600708 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
709 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600710 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600711 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600712 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700713
Gavin Howarde0432502019-02-26 16:23:45 -0700714 max = BC_MAX(a->len, b->len);
Stefan Essera299ffc2019-04-24 23:28:32 +0200715 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700716 max2 = (max + 1) / 2;
717
Stefan Esser5fd43a32019-04-25 07:05:24 +0200718 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
Gavin Howard404ece72019-04-26 15:59:15 -0600719 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total));
Gavin Howard5bede412019-02-23 09:31:28 -0700720
721 bc_num_setup(&l1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200722 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700723 bc_num_setup(&h1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200724 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700725 bc_num_setup(&l2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200726 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700727 bc_num_setup(&h2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200728 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700729 bc_num_setup(&m1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200730 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700731 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600732 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600733 bc_num_init(&z0, max);
734 bc_num_init(&z1, max);
735 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600736 max = bc_vm_growSize(max, max) + 1;
737 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600738
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600739 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600740 bc_num_clean(&l1);
741 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600742 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600743 bc_num_clean(&l2);
744 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700745
Gavin Howard14c354a2019-04-24 14:28:24 -0600746 bc_num_expand(c, max);
747 c->len = max;
Gavin Howard404ece72019-04-26 15:59:15 -0600748 memset(c->num, 0, BC_NUM_SIZE(c->len));
Gavin Howard305249a2018-10-15 20:24:47 -0600749
Gavin Howard14c354a2019-04-24 14:28:24 -0600750 s = bc_num_sub(&h1, &l1, &m1, 0);
751 if (BC_ERR(s)) goto err;
752 s = bc_num_sub(&l2, &h2, &m2, 0);
753 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600754
Gavin Howard971a2672019-04-26 14:32:07 -0600755 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600756
Gavin Howard14c354a2019-04-24 14:28:24 -0600757 s = bc_num_m(&h1, &h2, &z2, 0);
758 if (BC_ERR(s)) goto err;
759 bc_num_clean(&z2);
760
761 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
762 if (BC_ERR(s)) goto err;
763 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
764 if (BC_ERR(s)) goto err;
765 }
766
Gavin Howard971a2672019-04-26 14:32:07 -0600767 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600768
769 s = bc_num_m(&l1, &l2, &z0, 0);
770 if (BC_ERR(s)) goto err;
771 bc_num_clean(&z0);
772
773 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
774 if (BC_ERR(s)) goto err;
775 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
776 if (BC_ERR(s)) goto err;
777 }
778
Gavin Howard971a2672019-04-26 14:32:07 -0600779 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600780
781 s = bc_num_m(&m1, &m2, &z1, 0);
782 if (BC_ERR(s)) goto err;
783 bc_num_clean(&z1);
784
785 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
786 s = bc_num_shiftAddSub(c, &z1, max2, op);
787 if (BC_ERR(s)) goto err;
788 }
Gavin Howard305249a2018-10-15 20:24:47 -0600789
790err:
Gavin Howard5bede412019-02-23 09:31:28 -0700791 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600792 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600793 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600794 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600795 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600796 return s;
797}
798
Gavin Howard1cbfe242019-01-09 17:13:11 -0700799static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600800
Gavin Howardfcf29012019-04-25 20:12:19 -0600801 // TODO: Check this function.
802
Gavin Howard305249a2018-10-15 20:24:47 -0600803 BcStatus s;
804 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600805 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600806
Gavin Howard14913fc2019-04-23 14:36:03 -0600807 bc_num_setToZero(c, 0);
808
Gavin Howardcf9f2462019-04-27 06:43:57 -0600809 ascale = a->scale;
810 bscale = b->scale;
811 scale = BC_MAX(scale, ascale);
812 scale = BC_MAX(scale, b->scale);
Gavin Howard48a0e472019-04-26 15:39:46 -0600813 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600814 scale = BC_MIN(rscale, scale);
Gavin Howard305249a2018-10-15 20:24:47 -0600815
Gavin Howardbaa4f582019-01-24 13:56:35 -0700816 bc_num_createCopy(&cpa, a);
817 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600818
Gavin Howard23d8d252018-10-17 12:03:41 -0600819 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600820
Gavin Howard48a0e472019-04-26 15:39:46 -0600821 ardx = cpa.rdx * BC_BASE_POWER;
822 s = bc_num_shiftLeft(&cpa, ardx);
823 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600824 bc_num_clean(&cpa);
825 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600826
827 brdx = cpb.rdx * BC_BASE_POWER;
828 s = bc_num_shiftLeft(&cpb, brdx);
829 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600830 bzero = bc_num_shiftZero(&cpb);
831 bc_num_clean(&cpb);
832
Gavin Howard14c354a2019-04-24 14:28:24 -0600833 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700834 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600835
Gavin Howard14913fc2019-04-23 14:36:03 -0600836 zero = bc_vm_growSize(azero, bzero);
837 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600838
Gavin Howard14913fc2019-04-23 14:36:03 -0600839 bc_num_expand(c, len);
Gavin Howard48a0e472019-04-26 15:39:46 -0600840 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_POWER);
841 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
842 s = bc_num_shiftRight(c, ardx + brdx);
843 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600844
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600845 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600846
Gavin Howard305249a2018-10-15 20:24:47 -0600847err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600848 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600849 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600850 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600851 bc_num_free(&cpa);
852 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700853}
854
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600855static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700856
Gavin Howardfcf29012019-04-25 20:12:19 -0600857 // TODO: Check this function.
858
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600859 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard36ec9872019-04-29 10:14:21 -0600860 size_t rdx, rscale, req;
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600861 ssize_t cmp;
Gavin Howard36ec9872019-04-29 10:14:21 -0600862 BcNum cpa, cpb, two, factor, factor2, *fi, *fnext, *temp;
863 BcDig two_digs[2];
Gavin Howardc542d0a2019-04-29 10:28:51 -0600864 bool aneg, bneg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700865
Gavin Howard36ec9872019-04-29 10:14:21 -0600866 aneg = a->neg;
867 bneg = b->neg;
868
Gavin Howardddce6d42019-01-03 14:07:06 -0700869 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
870 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600871 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600872 return BC_STATUS_SUCCESS;
873 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700874 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600875 bc_num_copy(c, a);
Gavin Howardfdbcd062019-04-29 07:36:46 -0600876 goto exit;
Gavin Howard63738202018-09-26 15:34:20 -0600877 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700878
Gavin Howard36ec9872019-04-29 10:14:21 -0600879 a->neg = b->neg = false;
Gavin Howardfdbcd062019-04-29 07:36:46 -0600880 cmp = bc_num_cmp(a, b);
Gavin Howard021150b2018-03-10 15:40:42 -0700881
Gavin Howardfdbcd062019-04-29 07:36:46 -0600882 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
883 if (!cmp) {
884 bc_num_one(c);
885 goto exit;
Gavin Howard63738202018-09-26 15:34:20 -0600886 }
Gavin Howardfdbcd062019-04-29 07:36:46 -0600887
888 bc_num_createCopy(&cpa, a);
889 bc_num_createCopy(&cpb, b);
890
Gavin Howard36ec9872019-04-29 10:14:21 -0600891 req = bc_num_int(a) + BC_NUM_RDX(scale) + 1;
892 bc_num_init(&factor, req);
893 bc_num_init(&factor2, req);
894
895 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
896 bc_num_one(&two);
897 two.num[0] = 2;
898
Gavin Howardc542d0a2019-04-29 10:28:51 -0600899 if (b->rdx == b->len) {
Gavin Howard36ec9872019-04-29 10:14:21 -0600900 rdx = cpb.len - bc_num_nonzeroIdx(&cpb);
901 rscale = rdx * BC_BASE_POWER;
902 s = bc_num_shiftLeft(&cpa, rscale);
903 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
904 s = bc_num_shiftLeft(&cpb, rscale);
Stefan Esser193f8522019-04-27 01:29:01 +0200905 }
Gavin Howardfdbcd062019-04-29 07:36:46 -0600906 else {
907 rdx = b->len - b->rdx;
Gavin Howard36ec9872019-04-29 10:14:21 -0600908 rscale = rdx * BC_BASE_POWER;
909 s = bc_num_shiftRight(&cpa, rscale);
Gavin Howardfdbcd062019-04-29 07:36:46 -0600910 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard36ec9872019-04-29 10:14:21 -0600911 s = bc_num_shiftRight(&cpb, rscale);
Gavin Howardfdbcd062019-04-29 07:36:46 -0600912 }
Gavin Howard034b5b92019-04-27 07:28:56 -0600913
Gavin Howard36ec9872019-04-29 10:14:21 -0600914 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
915
916 req = bc_num_int(&cpa);
917 if (!req) req = cpa.len - bc_num_nonzeroIdx(&cpa);
Gavin Howardcef03972019-04-29 10:28:33 -0600918 req = BC_BASE_POWER * (req + 1);
Gavin Howard36ec9872019-04-29 10:14:21 -0600919 req += b->scale % BC_BASE_POWER == 0 ? BC_BASE_POWER : 0;
Gavin Howardcef03972019-04-29 10:28:33 -0600920 req += BC_NUM_RDX(scale) * BC_BASE_POWER;
Gavin Howard36ec9872019-04-29 10:14:21 -0600921
Gavin Howardcef03972019-04-29 10:28:33 -0600922 rscale += req;
Gavin Howard36ec9872019-04-29 10:14:21 -0600923 bc_num_extend(&cpa, req);
924 bc_num_extend(&cpb, req);
925
Gavin Howard36ec9872019-04-29 10:14:21 -0600926 fi = &factor;
927 fnext = &factor2;
928 cmp = 1;
929
930 while (!bc_num_isOne(fi) && cmp) {
931
932 s = bc_num_sub(&two, &cpb, fi, rscale);
933 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
934 s = bc_num_mul(&cpa, fi, &cpa, rscale);
935 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
936 s = bc_num_mul(&cpb, fi, &cpb, rscale);
937 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
938
Gavin Howard36ec9872019-04-29 10:14:21 -0600939 temp = fi;
940 fi = fnext;
941 fnext = temp;
942
943 cmp = bc_num_cmp(fi, fnext);
944 if (cmp == BC_NUM_SSIZE_MIN) {
945 s = BC_STATUS_SIGNAL;
946 goto err;
947 }
948
949// printf("cmp: %zu\n", cmp);
950 }
951
952 bc_num_copy(c, &cpa);
953
Stefan Esser193f8522019-04-27 01:29:01 +0200954err:
Gavin Howard36ec9872019-04-29 10:14:21 -0600955 bc_num_free(&factor2);
956 bc_num_free(&factor);
Gavin Howardfdbcd062019-04-29 07:36:46 -0600957 bc_num_free(&cpb);
958 bc_num_free(&cpa);
959exit:
Gavin Howard36ec9872019-04-29 10:14:21 -0600960 a->neg = aneg;
961 b->neg = bneg;
Gavin Howardf2ee6342019-04-09 17:05:20 -0600962 if (BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfdbcd062019-04-29 07:36:46 -0600963 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600964 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700965}
966
Gavin Howard1cbfe242019-01-09 17:13:11 -0700967static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
968 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600969{
Gavin Howardfcf29012019-04-25 20:12:19 -0600970 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -0600971 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600972 BcNum temp;
973 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700974
Gavin Howardddce6d42019-01-03 14:07:06 -0700975 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
976 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700977 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600978 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600979 return BC_STATUS_SUCCESS;
980 }
Gavin Howard8b254872018-03-14 01:13:35 -0600981
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600982 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -0700983 s = bc_num_d(a, b, c, scale);
984 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -0700985 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700986
Gavin Howard1ab22d22019-01-03 13:32:17 -0700987 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600988
Gavin Howard53eba8b2018-10-31 15:14:37 -0600989 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700990 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600991 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700992 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600993
Gavin Howard77445432019-04-26 15:59:33 -0600994 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
Gavin Howard18441082018-10-22 10:03:30 -0600995
996 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600997 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600998 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700999
1000err:
Gavin Howard18441082018-10-22 10:03:30 -06001001 bc_num_free(&temp);
1002 return s;
1003}
1004
Gavin Howard2d188a52019-02-25 14:19:08 -07001005static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1006{
Gavin Howardfcf29012019-04-25 20:12:19 -06001007 // TODO: Check this function.
Gavin Howard18441082018-10-22 10:03:30 -06001008 BcStatus s;
1009 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -07001010 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -06001011
Gavin Howard77445432019-04-26 15:59:33 -06001012 ts = bc_vm_growSize(scale, b->scale);
1013 ts = BC_MAX(ts, a->scale);
Gavin Howard798508b2019-02-21 16:45:32 -07001014
1015 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -06001016 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001017 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -06001018
Gavin Howard63738202018-09-26 15:34:20 -06001019 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001020}
1021
Gavin Howard1cbfe242019-01-09 17:13:11 -07001022static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001023
Gavin Howardfcf29012019-04-25 20:12:19 -06001024 // TODO: Check this function.
1025
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001026 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001027 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -07001028 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001029 size_t i, powrdx, resrdx;
1030 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001031
Gavin Howardecafd4f2019-02-23 09:30:45 -07001032 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001033
Gavin Howardddce6d42019-01-03 14:07:06 -07001034 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -06001035 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -06001036 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001037 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001038 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001039 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001040 return BC_STATUS_SUCCESS;
1041 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001042 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001043 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -06001044 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001045 return s;
1046 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001047
Gavin Howard63738202018-09-26 15:34:20 -06001048 neg = b->neg;
1049 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001050 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -07001051 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001052 if (s) return s;
1053
Gavin Howardbaa4f582019-01-24 13:56:35 -07001054 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001055
Stefan Eßerb5318342019-04-29 10:31:26 -06001056 if (!neg) scale = BC_MIN(a->scale * pow, BC_MAX(scale, a->scale));
Gavin Howardb29674f2018-03-22 22:24:58 -06001057
Gavin Howardd497c232019-04-29 10:32:38 -06001058 for (powrdx = a->scale; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -06001059 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001060 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001061 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001062 }
Gavin Howard6fb635f2018-03-03 12:45:42 -07001063
Gavin Howard2d188a52019-02-25 14:19:08 -07001064 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -07001065
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001066 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -07001067 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001068
Gavin Howard2d188a52019-02-25 14:19:08 -07001069 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001070
Gavin Howard4d5daba2019-01-07 14:42:50 -07001071 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001072 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001073 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001074
Gavin Howard63738202018-09-26 15:34:20 -06001075 if (pow & 1) {
1076 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001077 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001078 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001079 }
1080 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001081
Gavin Howard2d188a52019-02-25 14:19:08 -07001082 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -07001083 if (neg) {
1084 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001085 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -07001086 }
1087
Stefan Eßerb5318342019-04-29 10:31:26 -06001088 if (c->scale > scale) bc_num_truncate(c, c->scale - scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001089
Gavin Howard53eba8b2018-10-31 15:14:37 -06001090 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -06001091 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001092 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001093
Gavin Howard9b801632019-02-16 23:34:06 -07001094sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001095 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001096err:
Gavin Howard63738202018-09-26 15:34:20 -06001097 bc_num_free(&copy);
1098 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001099}
1100
Gavin Howard7bda4782018-12-28 09:53:22 -07001101#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -07001102static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
1103 size_t scale)
1104{
Gavin Howardac4d9122018-12-27 23:59:12 -07001105 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001106 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001107
1108 BC_UNUSED(scale);
1109
1110 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001111 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001112
Gavin Howardc9bef2d2019-04-26 14:46:52 -06001113 if (val < c->scale) bc_num_truncate(c, c->scale - val);
1114 else if (val > c->scale) bc_num_extend(c, val - c->scale);
Gavin Howardac4d9122018-12-27 23:59:12 -07001115
1116 return s;
1117}
1118
Gavin Howard2d188a52019-02-25 14:19:08 -07001119static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1120{
Gavin Howardac4d9122018-12-27 23:59:12 -07001121 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001122 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001123
1124 BC_UNUSED(scale);
1125
1126 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001127 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001128
Gavin Howardb1558662019-04-26 14:32:55 -06001129 return bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001130}
1131
Gavin Howard2d188a52019-02-25 14:19:08 -07001132static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1133 size_t scale)
1134{
Gavin Howardac4d9122018-12-27 23:59:12 -07001135 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001136 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001137
1138 BC_UNUSED(scale);
1139
1140 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001141 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001142
Gavin Howard2a366922019-01-16 10:50:20 -07001143 if (BC_NUM_ZERO(c)) return s;
1144
Gavin Howardb1558662019-04-26 14:32:55 -06001145 return bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001146}
Gavin Howard7bda4782018-12-28 09:53:22 -07001147#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001148
Gavin Howard1cbfe242019-01-09 17:13:11 -07001149static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1150 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001151{
Gavin Howarda1c44392018-09-27 12:41:15 -06001152 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001153 BcNum num2, *ptr_a, *ptr_b;
1154 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001155
Gavin Howard63738202018-09-26 15:34:20 -06001156 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001157
Gavin Howard890d0c02018-10-30 16:34:50 -06001158 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001159 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001160 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001161 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001162 }
1163 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001164
Gavin Howard63738202018-09-26 15:34:20 -06001165 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001166 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001167 if (c != a) {
1168 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001169 init = true;
1170 }
1171 }
1172 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001173
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001174 if (init) bc_num_init(c, req);
1175 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001176
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001177 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001178
Gavin Howardddce6d42019-01-03 14:07:06 -07001179 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001180 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001181
Gavin Howardba009802018-09-29 04:41:51 -06001182 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001183
Stefan Esser193f8522019-04-27 01:29:01 +02001184// bc_num_truncDecimals(c, scale);
1185
Gavin Howardee9d01e2019-02-16 22:48:11 -07001186 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001187}
1188
Gavin Howardc90ed902019-01-02 13:07:49 -07001189#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001190static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001191
Gavin Howardc7d655b2018-12-28 19:45:13 -07001192 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001193 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001194
Gavin Howard63738202018-09-26 15:34:20 -06001195 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001196
Gavin Howard63738202018-09-26 15:34:20 -06001197 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001198
Gavin Howard8a921bd2018-10-11 14:15:32 -06001199 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001200
Gavin Howard63738202018-09-26 15:34:20 -06001201 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001202
Gavin Howard63738202018-09-26 15:34:20 -06001203 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001204
Gavin Howard63738202018-09-26 15:34:20 -06001205 radix = true;
1206 continue;
1207 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001208
Gavin Howardc7d655b2018-12-28 19:45:13 -07001209 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001210 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001211
Gavin Howard63738202018-09-26 15:34:20 -06001212 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001213}
Gavin Howardc90ed902019-01-02 13:07:49 -07001214#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001215
Gavin Howard1cbfe242019-01-09 17:13:11 -07001216static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001217
Gavin Howardfcf29012019-04-25 20:12:19 -06001218 // TODO: Check this function.
1219
Gavin Howardc7d655b2018-12-28 19:45:13 -07001220 if (isupper(c)) {
1221 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001222 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001223 }
1224 else c -= '0';
1225
1226 return (unsigned long) (uchar) c;
1227}
1228
Gavin Howard1cbfe242019-01-09 17:13:11 -07001229static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001230
Gavin Howard3b60abb2019-04-26 08:49:17 -06001231 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001232 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001233 bool zero = true, rdx;
Stefan Essera299ffc2019-04-24 23:28:32 +02001234
Gavin Howard63738202018-09-26 15:34:20 -06001235 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001236
Gavin Howard63738202018-09-26 15:34:20 -06001237 val += i;
Gavin Howard3b60abb2019-04-26 08:49:17 -06001238 assert(isalnum(val[0]) || val[0] == '.');
Gavin Howard63738202018-09-26 15:34:20 -06001239 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001240
Gavin Howard63738202018-09-26 15:34:20 -06001241 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001242 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001243
Gavin Howardd43ec372019-04-25 21:13:54 -06001244 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001245
Gavin Howardd43ec372019-04-25 21:13:54 -06001246 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001247 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001248
Gavin Howard3b60abb2019-04-26 08:49:17 -06001249 i = len - (ptr == val ? 0 : i) - rdx;
Gavin Howard48a0e472019-04-26 15:39:46 -06001250 temp = BC_NUM_ROUND_POW(i);
Gavin Howard3b60abb2019-04-26 08:49:17 -06001251 mod = n->scale % BC_BASE_POWER;
1252 i = mod ? BC_BASE_POWER - mod : 0;
1253 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001254
1255 bc_num_expand(n, n->len);
Gavin Howard404ece72019-04-26 15:59:15 -06001256 memset(n->num, 0, BC_NUM_SIZE(n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -07001257
Gavin Howard6076dbb2019-04-26 09:47:50 -06001258 if (zero) n->len = n->rdx = 0;
1259 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001260
Gavin Howard971a2672019-04-26 14:32:07 -06001261 unsigned long exp, pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001262
Gavin Howard3b60abb2019-04-26 08:49:17 -06001263 exp = i;
Gavin Howard971a2672019-04-26 14:32:07 -06001264 pow = bc_num_pow10(exp);
Gavin Howardd43ec372019-04-25 21:13:54 -06001265
1266 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001267
Gavin Howardc7d655b2018-12-28 19:45:13 -07001268 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001269
Gavin Howardd43ec372019-04-25 21:13:54 -06001270 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001271 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001272
1273 size_t idx = exp / BC_BASE_POWER;
1274
Gavin Howard8dd307e2019-01-08 23:05:19 -07001275 if (isupper(c)) c = '9';
Gavin Howardf196fbe2019-04-26 08:49:05 -06001276 n->num[idx] += (((unsigned long) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001277
1278 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1279 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001280 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001281 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001282 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001283}
1284
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001285static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1286 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001287{
Gavin Howardfcf29012019-04-25 20:12:19 -06001288 // TODO: Check this function.
Gavin Howard1cbfe242019-01-09 17:13:11 -07001289 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001290 BcNum temp, mult, result;
Gavin Howard77445432019-04-26 15:59:33 -06001291 char c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001292 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001293 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001294 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001295
Gavin Howard11b9afd2018-10-18 14:23:28 -06001296 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001297 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001298
Gavin Howard26d078f2019-01-24 13:58:12 -07001299 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1300 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001301
Gavin Howardc7d655b2018-12-28 19:45:13 -07001302 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001303
Gavin Howardc7d655b2018-12-28 19:45:13 -07001304 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001305
Gavin Howard53eba8b2018-10-31 15:14:37 -06001306 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001307 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001308 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001309 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001310 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001311 }
Gavin Howardede51f02018-03-02 12:30:00 -07001312
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001313 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001314
Gavin Howard63738202018-09-26 15:34:20 -06001315 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001316 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001317 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001318
Gavin Howard2d188a52019-02-25 14:19:08 -07001319 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001320 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001321 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001322
Gavin Howard53eba8b2018-10-31 15:14:37 -06001323 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001324 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001325
Gavin Howard705c4bc2018-12-03 15:38:04 -07001326 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001327 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001328 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001329 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001330 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001331 }
Gavin Howardede51f02018-03-02 12:30:00 -07001332
Gavin Howardf2ee6342019-04-09 17:05:20 -06001333 if (BC_SIG) {
1334 s = BC_STATUS_SIGNAL;
1335 goto err;
1336 }
1337
Gavin Howard1769abc2019-02-21 09:36:14 -07001338 // This one cannot be a divide by 0 because mult starts out at 1, then is
1339 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1340 s = bc_num_div(&result, &mult, &result, digs);
1341 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001342 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001343 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001344 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001345
Gavin Howardddce6d42019-01-03 14:07:06 -07001346 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001347 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001348 }
1349 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001350
Gavin Howardd1417682019-02-15 17:08:42 -07001351err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001352 bc_num_free(&result);
1353int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001354 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001355 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001356 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001357}
1358
Gavin Howard1cbfe242019-01-09 17:13:11 -07001359static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001360 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001361 bc_vm_putchar('\\');
1362 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001363 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001364 }
Gavin Howard80977b22018-09-06 14:29:13 -06001365}
1366
Gavin Howard40a085f2018-12-03 12:08:59 -07001367#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001368static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001369 BC_UNUSED(rdx);
1370 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001371 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001372}
1373#endif // DC_ENABLED
1374
Gavin Howard1cbfe242019-01-09 17:13:11 -07001375static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1376
Gavin Howardfcf29012019-04-25 20:12:19 -06001377 // TODO: Check this function.
1378
Gavin Howarda84ad992018-12-03 19:11:06 -07001379 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001380
Gavin Howardab5e9632019-01-02 13:32:02 -07001381 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001382 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001383 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001384
Gavin Howardab5e9632019-01-02 13:32:02 -07001385 bc_num_printNewline();
Gavin Howard530e3072019-04-23 16:23:36 -06001386 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001387
Gavin Howard530e3072019-04-23 16:23:36 -06001388 for (exp = 0; exp < len; pow /= BC_BASE, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001389 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001390 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001391 dig = n / pow;
1392 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001393 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001394 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001395}
Gavin Howardfe679f02018-02-14 15:50:09 -07001396
Gavin Howard1cbfe242019-01-09 17:13:11 -07001397static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001398
Gavin Howard3c02ed32018-12-28 18:17:15 -07001399 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001400
Gavin Howard3c02ed32018-12-28 18:17:15 -07001401 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001402 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001403 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001404 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001405 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001406
Gavin Howardab5e9632019-01-02 13:32:02 -07001407 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001408 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001409 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001410}
1411
Gavin Howard1cbfe242019-01-09 17:13:11 -07001412static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001413
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001414 size_t i, j, rdx = n->rdx;
1415 bool zero = true;
1416 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001417
Gavin Howard48af52e2018-10-30 14:47:38 -06001418 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001419 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001420
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001421 for (i = n->len - 1; i < n->len; --i) {
1422
1423 BcDig n9 = n->num[i];
1424 size_t temp;
1425 bool irdx = (i == rdx - 1);
1426
1427 zero = (zero & !irdx);
1428 temp = n->scale % BC_BASE_POWER;
1429 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1430
1431 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1432
1433 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Essera299ffc2019-04-24 23:28:32 +02001434 buffer[j] = n9 % BC_BASE;
1435 n9 /= BC_BASE;
1436 }
Stefan Esser0da17752019-04-25 12:25:15 +02001437
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001438 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1439 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1440 zero = (zero && buffer[j] == 0);
1441 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Essera299ffc2019-04-24 23:28:32 +02001442 }
1443 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001444}
1445
Gavin Howard7ad5a662019-02-19 14:40:46 -07001446#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001447static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) {
Gavin Howard7ad5a662019-02-19 14:40:46 -07001448
Gavin Howardfcf29012019-04-25 20:12:19 -06001449 // TODO: Check this function.
1450
Gavin Howardb1558662019-04-26 14:32:55 -06001451 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001452 bool neg = (n->len <= n->rdx);
1453 BcNum temp, exp;
1454 size_t places, mod;
1455 BcDig digs[BC_NUM_LONG_LOG10];
1456
1457 bc_num_createCopy(&temp, n);
1458
1459 if (neg) {
1460 places = n->rdx - bc_num_len(n) + 1;
1461 mod = places % 3;
1462 if (eng && mod != 0) places += 3 - mod;
Gavin Howardb1558662019-04-26 14:32:55 -06001463 s = bc_num_shiftLeft(&temp, places);
1464 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001465 }
1466 else {
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001467 places = bc_num_int(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001468 mod = places % 3;
1469 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howardb1558662019-04-26 14:32:55 -06001470 s = bc_num_shiftRight(&temp, places);
1471 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001472 }
1473
1474 bc_num_printDecimal(&temp);
1475
1476 bc_num_printNewline();
1477 bc_vm_putchar('e');
1478
1479 if (!places) {
1480 bc_num_printHex(0, 1, false);
1481 goto exit;
1482 }
1483
1484 if (neg) {
1485 bc_num_printNewline();
1486 bc_vm_putchar('-');
1487 }
1488
1489 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1490 bc_num_ulong2num(&exp, (unsigned long) places);
1491
1492 bc_num_printDecimal(&exp);
1493
1494exit:
1495 bc_num_free(&temp);
Gavin Howardb1558662019-04-26 14:32:55 -06001496 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001497}
1498#endif // BC_ENABLE_EXTRA_MATH
1499
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001500static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1501 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001502{
Gavin Howardfcf29012019-04-25 20:12:19 -06001503 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001504 BcStatus s;
1505 BcVec stack;
1506 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001507 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001508 size_t i;
1509 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001510
Gavin Howard971a2672019-04-26 14:32:07 -06001511 assert(BC_NUM_NONZERO(base));
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001512
Gavin Howardddce6d42019-01-03 14:07:06 -07001513 if (BC_NUM_ZERO(n)) {
1514 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001515 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001516 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001517
Gavin Howardd3929832018-12-24 15:13:15 -07001518 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001519 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001520 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001521 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001522 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001523 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001524
Gavin Howard63738202018-09-26 15:34:20 -06001525 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001526 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001527 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001528
Gavin Howard2d188a52019-02-25 14:19:08 -07001529 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001530
1531 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001532 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001533 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001534
1535 // Will never fail (except for signals) because digit is
1536 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001537 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001538 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001539
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001540 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001541 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001542
Gavin Howard2d188a52019-02-25 14:19:08 -07001543 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001544
Gavin Howard2d188a52019-02-25 14:19:08 -07001545 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001546 ptr = bc_vec_item_rev(&stack, i);
1547 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001548 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001549 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001550
Gavin Howard2d188a52019-02-25 14:19:08 -07001551 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001552 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001553
Gavin Howard2d188a52019-02-25 14:19:08 -07001554 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001555
Gavin Howard53eba8b2018-10-31 15:14:37 -06001556 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001557 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001558
1559 // Will never fail (except for signals) because fracp is
1560 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001561 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001562 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001563
Gavin Howard705c4bc2018-12-03 15:38:04 -07001564 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001565 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001566 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001567
Gavin Howardddce6d42019-01-03 14:07:06 -07001568 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001569 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001570 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001571 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001572
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001573sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001574 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001575err:
Gavin Howard63738202018-09-26 15:34:20 -06001576 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001577 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001578 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001579 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001580 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001581 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001582}
1583
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001584static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1585 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001586{
Gavin Howardfcf29012019-04-25 20:12:19 -06001587 // TODO: Check this function.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001588 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001589 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001590 BcNumDigitOp print;
1591 bool neg = n->neg;
1592
Gavin Howard48af52e2018-10-30 14:47:38 -06001593 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001594 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001595
1596 n->neg = false;
1597
Gavin Howardbe4e8222019-01-03 13:34:16 -07001598 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001599 width = 1;
1600 print = bc_num_printHex;
1601 }
1602 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001603 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001604 print = bc_num_printDigits;
1605 }
1606
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001607 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001608 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001609
1610 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001611}
1612
Gavin Howard40a085f2018-12-03 12:08:59 -07001613#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001614BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1615 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001616}
1617#endif // DC_ENABLED
1618
Gavin Howard3c02ed32018-12-28 18:17:15 -07001619void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001620 assert(n);
1621 n->num = num;
1622 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001623 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001624 n->neg = false;
1625}
1626
Gavin Howard3c02ed32018-12-28 18:17:15 -07001627void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001628 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001629 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howard404ece72019-04-26 15:59:15 -06001630 bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001631}
1632
Gavin Howarded392aa2018-02-27 13:09:26 -07001633void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001634 assert(num);
1635 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001636}
1637
Gavin Howard3c02ed32018-12-28 18:17:15 -07001638void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001639 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001640 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001641 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001642 d->len = s->len;
1643 d->neg = s->neg;
1644 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001645 d->scale = s->scale;
Gavin Howard404ece72019-04-26 15:59:15 -06001646 memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -07001647}
1648
Gavin Howardbaa4f582019-01-24 13:56:35 -07001649void bc_num_createCopy(BcNum *d, const BcNum *s) {
1650 bc_num_init(d, s->len);
1651 bc_num_copy(d, s);
1652}
1653
1654void bc_num_createFromUlong(BcNum *n, unsigned long val) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001655 bc_num_init(n, (BC_NUM_LONG_LOG10 - 1) / BC_BASE_POWER + 1);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001656 bc_num_ulong2num(n, val);
1657}
1658
Gavin Howard7ad5a662019-02-19 14:40:46 -07001659size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001660 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001661}
1662
1663size_t bc_num_len(const BcNum *restrict n) {
1664
Gavin Howardb41483b2019-04-27 08:30:10 -06001665 size_t i, pow, scale, len = n->len;
Gavin Howardfcf29012019-04-25 20:12:19 -06001666 BcDig dig;
1667
Gavin Howardb41483b2019-04-27 08:30:10 -06001668 if (BC_NUM_ZERO(n)) return 0;
Gavin Howard08f78562019-04-29 07:56:04 -06001669 if (n->rdx == len) len = bc_num_nonzeroIdx(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001670
Gavin Howardfcf29012019-04-25 20:12:19 -06001671 dig = n->num[len - 1];
Gavin Howardb41483b2019-04-27 08:30:10 -06001672 pow = BC_BASE_DIG;
1673 i = BC_BASE_POWER + 1;
Gavin Howardfcf29012019-04-25 20:12:19 -06001674
Gavin Howardb41483b2019-04-27 08:30:10 -06001675 while (pow && (dig % (BcDig) pow == dig)) {
1676 i -= 1;
1677 pow /= BC_BASE;
1678 }
Gavin Howardfcf29012019-04-25 20:12:19 -06001679
Gavin Howardb41483b2019-04-27 08:30:10 -06001680 scale = n->scale % BC_BASE_POWER;
1681 scale = scale ? scale : BC_BASE_POWER;
1682
1683 return (len - 1) * BC_BASE_POWER + i - (BC_BASE_POWER - scale);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001684}
1685
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001686BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1687 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001688{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001689 BcStatus s = BC_STATUS_SUCCESS;
1690
Gavin Howard63738202018-09-26 15:34:20 -06001691 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001692 assert(BC_ENABLE_EXTRA_MATH ||
1693 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001694 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001695
Gavin Howardd746e802018-12-28 22:06:00 -07001696 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06001697 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001698 else s = bc_num_parseBase(n, val, base, base_t);
1699
1700 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001701}
1702
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001703BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1704 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001705{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001706 BcStatus s = BC_STATUS_SUCCESS;
1707
Gavin Howardab5e9632019-01-02 13:32:02 -07001708 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001709 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001710
Gavin Howardab5e9632019-01-02 13:32:02 -07001711 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001712
Gavin Howardddce6d42019-01-03 14:07:06 -07001713 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06001714 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001715#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001716 else if (base_t == 0 || base_t == 1)
1717 s = bc_num_printExponent(n, base_t != 0);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001718#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001719 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001720
Gavin Howarde5f11c72019-02-23 09:42:51 -07001721 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001722 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001723 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001724 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001725
1726 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001727}
1728
Gavin Howard3c02ed32018-12-28 18:17:15 -07001729BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001730
Gavin Howard63738202018-09-26 15:34:20 -06001731 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001732 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001733
Gavin Howard63738202018-09-26 15:34:20 -06001734 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001735
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001736 *result = 0;
1737
Gavin Howardecafd4f2019-02-23 09:30:45 -07001738 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001739
Gavin Howard06b6e862019-02-15 11:15:31 -07001740 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001741
Gavin Howard0700efc2019-04-24 17:31:23 -06001742 unsigned long prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07001743
Gavin Howard0700efc2019-04-24 17:31:23 -06001744 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001745 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1746
Gavin Howard77445432019-04-26 15:59:33 -06001747 r = prev + (unsigned long) n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07001748
1749 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001750 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001751 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001752
Gavin Howard7b557da2018-12-21 10:25:32 -07001753 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001754
Gavin Howard63738202018-09-26 15:34:20 -06001755 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001756}
1757
Gavin Howard3c02ed32018-12-28 18:17:15 -07001758void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001759
Gavin Howardc25fd612019-04-26 19:31:31 -06001760 BcDig *ptr;
1761 unsigned long i;
1762
Gavin Howard63738202018-09-26 15:34:20 -06001763 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001764
Gavin Howard63738202018-09-26 15:34:20 -06001765 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001766
Gavin Howard1ab22d22019-01-03 13:32:17 -07001767 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001768
Gavin Howard798508b2019-02-21 16:45:32 -07001769 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howard05feab02019-04-23 16:24:07 -06001770
Gavin Howardc25fd612019-04-26 19:31:31 -06001771 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_DIG)
1772 ptr[i] = val % BC_BASE_DIG;
Gavin Howard025d04d2018-02-20 13:53:28 -07001773}
1774
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001775size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001776
1777 size_t aint, bint, ardx, brdx;
1778
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001779 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001780
1781 ardx = a->rdx;
1782 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001783 aint = bc_num_int(a);
1784 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001785 ardx = BC_MAX(ardx, brdx);
1786 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001787
Gavin Howard2d188a52019-02-25 14:19:08 -07001788 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001789}
1790
1791size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001792 size_t max, rdx;
1793 rdx = bc_vm_growSize(a->rdx, b->rdx);
1794 max = bc_vm_growSize(BC_MAX(scale, rdx), 1);
1795 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1796 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001797}
1798
1799size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1800 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001801 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001802}
1803
1804#if BC_ENABLE_EXTRA_MATH
1805size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1806 BC_UNUSED(b);
1807 BC_UNUSED(scale);
1808 return BC_NUM_SHREQ(a);
1809}
1810#endif // BC_ENABLE_EXTRA_MATH
1811
Gavin Howard12fe7812018-09-29 03:45:18 -06001812BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001813 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001814 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001815 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001816}
1817
Gavin Howard12fe7812018-09-29 03:45:18 -06001818BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001819 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001820 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001821 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001822}
1823
Gavin Howard12fe7812018-09-29 03:45:18 -06001824BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001825 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001826}
1827
Gavin Howard12fe7812018-09-29 03:45:18 -06001828BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001829 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001830}
1831
Gavin Howard54b946a2018-10-23 12:06:57 -06001832BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001833 size_t req = bc_num_mulReq(a, b, scale);
1834 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001835}
1836
Gavin Howard12fe7812018-09-29 03:45:18 -06001837BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001838 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001839}
1840
Gavin Howard7d8f0382018-12-27 16:15:13 -07001841#if BC_ENABLE_EXTRA_MATH
1842BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001843 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001844}
1845
1846BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001847 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001848}
1849
1850BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001851 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001852}
1853#endif // BC_ENABLE_EXTRA_MATH
1854
Gavin Howard3c02ed32018-12-28 18:17:15 -07001855BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001856
Gavin Howardfcf29012019-04-25 20:12:19 -06001857 // TODO: Check this function.
1858
Gavin Howard1cbfe242019-01-09 17:13:11 -07001859 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001860 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard14913fc2019-04-23 14:36:03 -06001861 size_t pow, len, req, digs, digs1, digs2, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001862 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001863 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001864
Gavin Howard34edd0a2018-10-25 15:51:05 -06001865 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001866
Gavin Howardecafd4f2019-02-23 09:30:45 -07001867 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07001868
Gavin Howard007afdf2019-02-23 09:45:42 -07001869 len = bc_vm_growSize(bc_num_int(a), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001870 req = bc_vm_growSize(BC_MAX(scale, a->rdx), len >> 1);
1871 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001872
Gavin Howardddce6d42019-01-03 14:07:06 -07001873 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001874 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001875 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001876 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001877 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001878 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001879 bc_num_extend(b, scale);
1880 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001881 }
Gavin Howard954276b2018-05-16 01:43:58 -06001882
Gavin Howard890d0c02018-10-30 16:34:50 -06001883 scale = BC_MAX(scale, a->rdx) + 1;
Gavin Howard007afdf2019-02-23 09:45:42 -07001884 len = bc_vm_growSize(a->len, scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001885
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001886 bc_num_init(&num1, len);
1887 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001888 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001889
Gavin Howard63738202018-09-26 15:34:20 -06001890 bc_num_one(&half);
1891 half.num[0] = 5;
1892 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001893
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001894 bc_num_init(&f, len);
1895 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001896
Gavin Howard63738202018-09-26 15:34:20 -06001897 x0 = &num1;
1898 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001899
Gavin Howard63738202018-09-26 15:34:20 -06001900 bc_num_one(x0);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001901 pow = bc_num_int(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001902
Gavin Howard53eba8b2018-10-31 15:14:37 -06001903 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001904
Gavin Howardc39fd492018-10-04 10:07:03 -06001905 if (pow & 1) x0->num[0] = 2;
1906 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001907
Gavin Howardc39fd492018-10-04 10:07:03 -06001908 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001909
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001910 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001911
Gavin Howard63738202018-09-26 15:34:20 -06001912 // Make sure to move the radix back.
1913 x0->rdx -= pow;
1914 }
Gavin Howard954276b2018-05-16 01:43:58 -06001915
Gavin Howard14913fc2019-04-23 14:36:03 -06001916 x0->rdx = digs = digs1 = digs2 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001917 resrdx = scale + 2;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001918 len = bc_num_int(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001919
Gavin Howard2d188a52019-02-25 14:19:08 -07001920 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001921
Gavin Howard971a2672019-04-26 14:32:07 -06001922 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07001923
Gavin Howard53eba8b2018-10-31 15:14:37 -06001924 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001925 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001926 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001927 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001928 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001929 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001930 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001931
Gavin Howard53eba8b2018-10-31 15:14:37 -06001932 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06001933 if (cmp == BC_NUM_SSIZE_MIN) {
1934 s = BC_STATUS_SIGNAL;
1935 break;
1936 }
1937
Gavin Howard53eba8b2018-10-31 15:14:37 -06001938 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001939
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001940 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001941 else times = 0;
1942
1943 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001944
1945 cmp2 = cmp1;
1946 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001947 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001948
Gavin Howard63738202018-09-26 15:34:20 -06001949 temp = x0;
1950 x0 = x1;
1951 x1 = temp;
1952 }
Gavin Howard954276b2018-05-16 01:43:58 -06001953
Gavin Howard2d188a52019-02-25 14:19:08 -07001954 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07001955 s = BC_STATUS_SIGNAL;
1956 goto err;
1957 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001958
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001959 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001960 scale -= 1;
1961 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001962
1963err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06001964 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06001965 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001966 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001967 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001968 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001969 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001970 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001971 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001972}
Gavin Howardba009802018-09-29 04:41:51 -06001973
Gavin Howard2cb39612018-10-22 08:46:48 -06001974BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1975
Gavin Howardfcf29012019-04-25 20:12:19 -06001976 // TODO: Check this function.
1977
Gavin Howard2cb39612018-10-22 08:46:48 -06001978 BcStatus s;
1979 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001980 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07001981 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001982
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001983 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001984
Gavin Howard890d0c02018-10-30 16:34:50 -06001985 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001986 memcpy(&num2, c, sizeof(BcNum));
1987 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001988 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001989 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001990 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001991 else {
1992 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001993 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001994 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001995
Gavin Howard54b946a2018-10-23 12:06:57 -06001996 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001997
Gavin Howardddce6d42019-01-03 14:07:06 -07001998 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001999 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07002000 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002001 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06002002
2003 if (init) bc_num_free(&num2);
2004
2005 return s;
2006}
2007
Gavin Howard40a085f2018-12-03 12:08:59 -07002008#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06002009BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06002010
Gavin Howardfcf29012019-04-25 20:12:19 -06002011 // TODO: Check this function.
2012
Gavin Howardba009802018-09-29 04:41:51 -06002013 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002014 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07002015 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06002016
Gavin Howard34edd0a2018-10-25 15:51:05 -06002017 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06002018
Gavin Howardecafd4f2019-02-23 09:30:45 -07002019 if (BC_ERR(BC_NUM_ZERO(c)))
2020 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
2021 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
2022 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07002023 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06002024
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002025 bc_num_expand(d, c->len);
2026 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07002027 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002028 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06002029
2030 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06002031 two.num[0] = 2;
2032 bc_num_one(d);
2033
Gavin Howard1769abc2019-02-21 09:36:14 -07002034 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002035 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002036 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002037 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07002038 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06002039
Gavin Howard2d188a52019-02-25 14:19:08 -07002040 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06002041
Gavin Howard1769abc2019-02-21 09:36:14 -07002042 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002043 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002044 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002045
Gavin Howarda83a5e52019-02-21 16:51:06 -07002046 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06002047 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002048 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002049
2050 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002051 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002052 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002053 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002054 }
2055
Gavin Howard53eba8b2018-10-31 15:14:37 -06002056 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002057 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002058
2059 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002060 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002061 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002062 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002063 }
2064
Gavin Howard2d188a52019-02-25 14:19:08 -07002065 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002066
Gavin Howardba009802018-09-29 04:41:51 -06002067err:
Gavin Howardba009802018-09-29 04:41:51 -06002068 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002069rem_err:
2070 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002071 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002072 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002073 return s;
2074}
Gavin Howarde6e84762018-10-03 11:46:34 -06002075#endif // DC_ENABLED