blob: e32fe2ba43787e3e819689b98645cf7d7e03eff5 [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 Howardfcf29012019-04-25 20:12:19 -0600160static unsigned long bc_num_addDigit(BcDig *restrict num, unsigned long d,
161 unsigned long c)
Gavin Howard14c354a2019-04-24 14:28:24 -0600162{
163 d += c;
Gavin Howard0700efc2019-04-24 17:31:23 -0600164 *num = (BcDig) (d % BC_BASE_DIG);
165 assert(*num >= 0 && *num < BC_BASE_DIG);
166 return d / BC_BASE_DIG;
Gavin Howard14c354a2019-04-24 14:28:24 -0600167}
168
169static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
170 size_t len)
171{
172 size_t i;
Gavin Howard971a2672019-04-26 14:32:07 -0600173 unsigned long carry = 0;
Gavin Howard14c354a2019-04-24 14:28:24 -0600174
175 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard971a2672019-04-26 14:32:07 -0600176 unsigned long in = ((unsigned long) a[i]) + ((unsigned long) b[i]);
Gavin Howard14c354a2019-04-24 14:28:24 -0600177 carry = bc_num_addDigit(a + i, in, carry);
178 }
179
180 for (; BC_NO_SIG && carry; ++i)
Gavin Howard971a2672019-04-26 14:32:07 -0600181 carry = bc_num_addDigit(a + i, (unsigned long) a[i], carry);
Gavin Howard14c354a2019-04-24 14:28:24 -0600182
183 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
184}
185
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700186static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
187 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700188{
Gavin Howard63738202018-09-26 15:34:20 -0600189 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600190
Gavin Howard2d188a52019-02-25 14:19:08 -0700191 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600192
Gavin Howard2d188a52019-02-25 14:19:08 -0700193 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600194 assert(a[i + j] >= -BC_BASE_DIG);
195 a[i + j++] += BC_BASE_DIG;
Gavin Howard54b946a2018-10-23 12:06:57 -0600196 a[i + j] -= 1;
Gavin Howard0700efc2019-04-24 17:31:23 -0600197 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_DIG);
Gavin Howard63738202018-09-26 15:34:20 -0600198 }
199 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600200
Gavin Howard2d188a52019-02-25 14:19:08 -0700201 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600202}
203
Gavin Howard2d188a52019-02-25 14:19:08 -0700204static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
205 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700206{
Gavin Howard63738202018-09-26 15:34:20 -0600207 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600208 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700209 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600210 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600211}
212
Gavin Howard3c02ed32018-12-28 18:17:15 -0700213ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700214
Gavin Howard63738202018-09-26 15:34:20 -0600215 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600216 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600217 bool a_max, neg = false;
218 ssize_t cmp;
219
220 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700221
Gavin Howard8a921bd2018-10-11 14:15:32 -0600222 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700223 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700224 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600225 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600226 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600227 else return -1;
228 }
229 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700230
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700231 a_int = bc_num_int(a);
232 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600233 a_int -= b_int;
234 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700235
Gavin Howard1ab22d22019-01-03 13:32:17 -0700236 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700237
Gavin Howard890d0c02018-10-30 16:34:50 -0600238 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600239 min = b->rdx;
240 diff = a->rdx - b->rdx;
241 max_num = a->num + diff;
242 min_num = b->num;
243 }
244 else {
245 min = a->rdx;
246 diff = b->rdx - a->rdx;
247 max_num = b->num + diff;
248 min_num = a->num;
249 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700250
Gavin Howard890d0c02018-10-30 16:34:50 -0600251 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600252 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700253 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700254
Gavin Howard2d188a52019-02-25 14:19:08 -0700255 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700256 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600257 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700258
Gavin Howardf2ee6342019-04-09 17:05:20 -0600259 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700260}
261
Gavin Howard2a366922019-01-16 10:50:20 -0700262static void bc_num_clean(BcNum *restrict n) {
263 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
264 if (BC_NUM_ZERO(n)) n->neg = false;
265 else if (n->len < n->rdx) n->len = n->rdx;
266}
267
Gavin Howard3c02ed32018-12-28 18:17:15 -0700268void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700269
Gavin Howardb1558662019-04-26 14:32:55 -0600270 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700271
Stefan Esser70f11852019-04-27 10:39:00 +0200272 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700273
Gavin Howardb1558662019-04-26 14:32:55 -0600274 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
275 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
276
277 n->scale -= places;
278 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700279
Gavin Howardddce6d42019-01-03 14:07:06 -0700280 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600281
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600282 size_t pow;
283
284 pow = n->scale % BC_BASE_POWER;
285 pow = pow ? BC_BASE_POWER - pow : 0;
286 pow = bc_num_pow10(pow);
Gavin Howardb1558662019-04-26 14:32:55 -0600287
288 n->len -= places_rdx;
Gavin Howard404ece72019-04-26 15:59:15 -0600289 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
Gavin Howardb1558662019-04-26 14:32:55 -0600290
291 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600292 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600293
Gavin Howard2a366922019-01-16 10:50:20 -0700294 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600295 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700296}
297
Stefan Esser70f11852019-04-27 10:39:00 +0200298static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700299
Gavin Howardb1558662019-04-26 14:32:55 -0600300 size_t places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700301
Gavin Howard1ab22d22019-01-03 13:32:17 -0700302 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700303
Gavin Howardb1558662019-04-26 14:32:55 -0600304 bc_num_expand(n, n->len + places_rdx);
Gavin Howard404ece72019-04-26 15:59:15 -0600305 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
306 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
Gavin Howarde6f326e2019-04-26 10:13:45 -0600307 n->rdx += places_rdx;
308 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600309 n->len += places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700310
Gavin Howarde6f326e2019-04-26 10:13:45 -0600311 assert(n->rdx == BC_NUM_RDX(n->scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200312}
313
Gavin Howard2d188a52019-02-25 14:19:08 -0700314static void bc_num_retireMul(BcNum *restrict n, size_t scale,
315 bool neg1, bool neg2)
316{
Gavin Howard971a2672019-04-26 14:32:07 -0600317 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
318 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600319
Gavin Howard305249a2018-10-15 20:24:47 -0600320 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700321 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600322}
323
Gavin Howard2d188a52019-02-25 14:19:08 -0700324static void bc_num_split(const BcNum *restrict n, size_t idx,
325 BcNum *restrict a, BcNum *restrict b)
326{
Gavin Howardfcf29012019-04-25 20:12:19 -0600327 // TODO: Check this function.
Gavin Howard305249a2018-10-15 20:24:47 -0600328 if (idx < n->len) {
329
Gavin Howard305249a2018-10-15 20:24:47 -0600330 b->len = n->len - idx;
331 a->len = idx;
Gavin Howarda3f260c2019-04-26 15:47:25 -0600332 a->scale = a->rdx = b->scale = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600333
Gavin Howard404ece72019-04-26 15:59:15 -0600334 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
335 memcpy(a->num, n->num, BC_NUM_SIZE(idx));
Gavin Howard50c80022019-01-03 15:14:33 -0700336
337 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600338 }
Gavin Howard50c80022019-01-03 15:14:33 -0700339 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600340
341 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600342}
343
Gavin Howard14913fc2019-04-23 14:36:03 -0600344static size_t bc_num_shiftZero(BcNum *restrict n) {
345
346 size_t i;
347
348 assert(!n->rdx || BC_NUM_ZERO(n));
349
350 for (i = 0; i < n->len && !n->num[i]; ++i);
351
352 n->len -= i;
353 n->num += i;
354
355 return i;
356}
357
Gavin Howard2e736de2019-04-27 06:41:58 -0600358static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
359 n->len += places_rdx;
360 n->num -= places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600361}
362
Gavin Howard48a0e472019-04-26 15:39:46 -0600363static BcStatus bc_num_shift(BcNum *restrict n, BcDig *restrict ptr,
364 unsigned long dig, size_t len)
365{
Gavin Howardb1558662019-04-26 14:32:55 -0600366 size_t i;
367 unsigned long carry = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600368
Gavin Howardb1558662019-04-26 14:32:55 -0600369 assert(dig < BC_BASE_POWER);
Gavin Howard305249a2018-10-15 20:24:47 -0600370
Gavin Howardb1558662019-04-26 14:32:55 -0600371 dig = bc_num_pow10(dig);
372
373 for (i = 0; BC_NO_SIG && i < len; ++i) {
374 unsigned long in = ((unsigned long) ptr[i]) * dig;
375 carry = bc_num_addDigit(ptr + i, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600376 }
377
Gavin Howardb1558662019-04-26 14:32:55 -0600378 if (carry) {
379 bc_num_expand(n, n->len + 1);
380 assert(carry < BC_BASE_DIG);
381 n->num[n->len] = (BcDig) carry;
382 n->len += 1;
383 }
384
385 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600386}
387
Gavin Howardb1558662019-04-26 14:32:55 -0600388static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700389
Gavin Howardb1558662019-04-26 14:32:55 -0600390 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600391 unsigned long dig;
392 bool shift;
393 size_t places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600394
Gavin Howardb1558662019-04-26 14:32:55 -0600395 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600396
Gavin Howard48a0e472019-04-26 15:39:46 -0600397 dig = (unsigned long) (places % BC_BASE_POWER);
398 shift = (dig != 0);
399 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx - shift;
400
Gavin Howardb1558662019-04-26 14:32:55 -0600401 if (n->scale >= places) {
402 n->scale -= places;
403 n->rdx = BC_NUM_RDX(n->scale);
404 }
405 else if (BC_NUM_NONZERO(n)) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600406
Stefan Esser6189d952019-04-27 10:40:14 +0200407 if (places_rdx > n->rdx) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600408 bc_num_expand(n, n->len + places_rdx - n->rdx + shift);
Gavin Howard404ece72019-04-26 15:59:15 -0600409 memmove(n->num + places_rdx - n->rdx, n->num, BC_NUM_SIZE(n->len));
410 memset(n->num, 0, BC_NUM_SIZE(places_rdx - n->rdx));
Gavin Howard48a0e472019-04-26 15:39:46 -0600411 n->len += places_rdx;
412 }
413
Gavin Howardb1558662019-04-26 14:32:55 -0600414 n->scale = n->rdx = 0;
415 }
416 else n->scale = 0;
417
418 if (shift) s = bc_num_shift(n, n->num + places_rdx, dig, n->len - places_rdx);
419
420 bc_num_clean(n);
421
422 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
423}
424
425static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
426
427 BcStatus s = BC_STATUS_SUCCESS;
428 unsigned long dig = (unsigned long) (places % BC_BASE_POWER);
429 bool shift = (dig != 0);
430 size_t len, places_rdx;
431
432 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600433 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600434 n->scale += places;
435 bc_num_expand(n, BC_NUM_RDX(n->scale));
436 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600437 }
438
Gavin Howardb1558662019-04-26 14:32:55 -0600439 places_rdx = BC_NUM_RDX(places);
440 len = bc_vm_growSize(n->len, places_rdx);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700441
442 if (len > n->len) {
443
444 if (len > n->cap) bc_num_expand(n, len);
445
Gavin Howard404ece72019-04-26 15:59:15 -0600446 memset(n->num + n->len, 0, BC_NUM_SIZE(len - n->len));
Gavin Howard7ad5a662019-02-19 14:40:46 -0700447 n->len = len;
448 }
449
Gavin Howardb1558662019-04-26 14:32:55 -0600450 n->scale += places;
451 n->rdx += places_rdx;
452
453 if (shift) s = bc_num_shift(n, n->num, BC_BASE_POWER - dig, n->len);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700454
455 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600456 assert(n->rdx == BC_NUM_RDX(n->scale));
457
458 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700459}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700460
Gavin Howard1cbfe242019-01-09 17:13:11 -0700461static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600462
Gavin Howard63738202018-09-26 15:34:20 -0600463 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600464 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600465
Gavin Howard971a2672019-04-26 14:32:07 -0600466 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700467
Gavin Howardd32d7df2018-10-15 08:42:25 -0600468 one.cap = 2;
469 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600470 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600471
Gavin Howardd32d7df2018-10-15 08:42:25 -0600472 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600473}
474
Gavin Howard7bda4782018-12-28 09:53:22 -0700475#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700476static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
477 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700478{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700479 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700480 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700481 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700482}
Gavin Howard7bda4782018-12-28 09:53:22 -0700483#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700484
Gavin Howard1cbfe242019-01-09 17:13:11 -0700485static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700486
Gavin Howard8a921bd2018-10-11 14:15:32 -0600487 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600488 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard6fccd222019-04-26 10:17:02 -0600489 unsigned long carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700490
Gavin Howard63738202018-09-26 15:34:20 -0600491 // Because this function doesn't need to use scale (per the bc spec),
492 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600493
Gavin Howardddce6d42019-01-03 14:07:06 -0700494 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600495 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700496 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600497 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600498 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700499 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600500 bc_num_copy(c, a);
501 return BC_STATUS_SUCCESS;
502 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600503
Gavin Howard63738202018-09-26 15:34:20 -0600504 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600505 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600506 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600507 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700508
Gavin Howard63738202018-09-26 15:34:20 -0600509 if (a->rdx > b->rdx) {
510 diff = a->rdx - b->rdx;
511 ptr = a->num;
512 ptr_a = a->num + diff;
513 ptr_b = b->num;
514 }
515 else {
516 diff = b->rdx - a->rdx;
517 ptr = b->num;
518 ptr_a = a->num;
519 ptr_b = b->num + diff;
520 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700521
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700522 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700523
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700524 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600525 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700526 a_int = bc_num_int(a);
527 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700528
Gavin Howard890d0c02018-10-30 16:34:50 -0600529 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600530 min_int = b_int;
531 max = a_int;
532 ptr = ptr_a;
533 }
534 else {
535 min_int = a_int;
536 max = b_int;
537 ptr = ptr_b;
538 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700539
Gavin Howard2d188a52019-02-25 14:19:08 -0700540 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard6fccd222019-04-26 10:17:02 -0600541 unsigned long in;
542 in = ((unsigned long) ptr_a[i]) + ((unsigned long) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700543 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600544 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700545
Gavin Howard2d188a52019-02-25 14:19:08 -0700546 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard6fccd222019-04-26 10:17:02 -0600547 carry = bc_num_addDigit(ptr_c + i, (unsigned long) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700548
549 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700550
Gavin Howard1ab22d22019-01-03 13:32:17 -0700551 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600552
Gavin Howard2d188a52019-02-25 14:19:08 -0700553 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700554}
555
Gavin Howard1cbfe242019-01-09 17:13:11 -0700556static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700557
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700558 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600559 ssize_t cmp;
560 BcNum *minuend, *subtrahend;
561 size_t start;
562 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700563
Gavin Howard63738202018-09-26 15:34:20 -0600564 // Because this function doesn't need to use scale (per the bc spec),
565 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700566
Gavin Howardddce6d42019-01-03 14:07:06 -0700567 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600568 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700569 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600570 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600571 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700572 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600573 bc_num_copy(c, a);
574 return BC_STATUS_SUCCESS;
575 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700576
Gavin Howard63738202018-09-26 15:34:20 -0600577 aneg = a->neg;
578 bneg = b->neg;
579 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700580
Gavin Howard63738202018-09-26 15:34:20 -0600581 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600582 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700583
Gavin Howard63738202018-09-26 15:34:20 -0600584 a->neg = aneg;
585 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700586
Gavin Howard1ab22d22019-01-03 13:32:17 -0700587 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600588 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600589 return BC_STATUS_SUCCESS;
590 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700591
592 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600593 neg = a->neg;
594 minuend = a;
595 subtrahend = b;
596 }
597 else {
598 neg = b->neg;
599 if (sub) neg = !neg;
600 minuend = b;
601 subtrahend = a;
602 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700603
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600604 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600605 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700606
Gavin Howarde6f326e2019-04-26 10:13:45 -0600607 if (c->scale < subtrahend->scale) {
608 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600609 start = 0;
610 }
611 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700612
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700613 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700614
Gavin Howard63738202018-09-26 15:34:20 -0600615 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700616
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700617 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700618}
619
Gavin Howarddc4e3712019-04-23 14:56:29 -0600620static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
621{
Gavin Howardfcf29012019-04-25 20:12:19 -0600622 // TODO: Check this function.
Gavin Howard971a2672019-04-26 14:32:07 -0600623 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600624 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard971a2672019-04-26 14:32:07 -0600625 unsigned long sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600626
Gavin Howard971a2672019-04-26 14:32:07 -0600627 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600628 assert(!a->rdx && !b->rdx);
629
Gavin Howard5083aec2019-04-23 18:03:46 -0600630 clen = bc_vm_growSize(alen, blen);
631 bc_num_expand(c, clen + 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600632
633 ptr_c = c->num;
Gavin Howard404ece72019-04-26 15:59:15 -0600634 memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600635
Gavin Howard5083aec2019-04-23 18:03:46 -0600636 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600637
Gavin Howard5083aec2019-04-23 18:03:46 -0600638 ssize_t sidx = (ssize_t) (i - blen + 1);
639 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600640
Stefan Essera299ffc2019-04-24 23:28:32 +0200641 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
Stefan Eßer4b111c22019-04-24 17:23:08 -0600642
Gavin Howard971a2672019-04-26 14:32:07 -0600643 sum += ((unsigned long) ptr_a[j]) * ((unsigned long) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600644
Gavin Howard0700efc2019-04-24 17:31:23 -0600645 if (sum >= BC_BASE_DIG) {
646 carry += sum / BC_BASE_DIG;
647 sum %= BC_BASE_DIG;
Stefan Essera299ffc2019-04-24 23:28:32 +0200648 }
649 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600650
Stefan Essera299ffc2019-04-24 23:28:32 +0200651 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600652 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Essera299ffc2019-04-24 23:28:32 +0200653 sum = carry;
654 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600655 }
656
Gavin Howard5083aec2019-04-23 18:03:46 -0600657 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600658 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600659 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600660 clen += 1;
661 }
662
663 c->len = clen;
664
Gavin Howarddc4e3712019-04-23 14:56:29 -0600665 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
666}
667
Gavin Howard14c354a2019-04-24 14:28:24 -0600668static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
669 size_t shift, BcNumShiftAddOp op)
670{
Gavin Howardfcf29012019-04-25 20:12:19 -0600671 // TODO: Check this function.
Gavin Howard14c354a2019-04-24 14:28:24 -0600672 assert(n->len >= shift + a->len);
673 assert(!n->rdx && !a->rdx);
674 return op(n->num + shift, a->num, a->len);
675}
676
677static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600678
Gavin Howardfcf29012019-04-25 20:12:19 -0600679 // TODO: Check this function.
680
Gavin Howard9b801632019-02-16 23:34:06 -0700681 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200682 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600683 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700684 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600685 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700686 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600687
Gavin Howard14913fc2019-04-23 14:36:03 -0600688 assert(BC_NUM_ZERO(c));
689
Gavin Howardddce6d42019-01-03 14:07:06 -0700690 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600691 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700692 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600693 bc_num_zero(c);
694 return BC_STATUS_SUCCESS;
695 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700696 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600697 bc_num_copy(c, aone ? b : a);
698 return BC_STATUS_SUCCESS;
699 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700700
Gavin Howardb5093d72018-10-17 12:13:17 -0600701 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
702 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600703 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600704 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600705 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700706
Gavin Howarde0432502019-02-26 16:23:45 -0700707 max = BC_MAX(a->len, b->len);
Stefan Essera299ffc2019-04-24 23:28:32 +0200708 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700709 max2 = (max + 1) / 2;
710
Stefan Esser5fd43a32019-04-25 07:05:24 +0200711 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
Gavin Howard404ece72019-04-26 15:59:15 -0600712 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total));
Gavin Howard5bede412019-02-23 09:31:28 -0700713
714 bc_num_setup(&l1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200715 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700716 bc_num_setup(&h1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200717 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700718 bc_num_setup(&l2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200719 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700720 bc_num_setup(&h2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200721 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700722 bc_num_setup(&m1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200723 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700724 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600725 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600726 bc_num_init(&z0, max);
727 bc_num_init(&z1, max);
728 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600729 max = bc_vm_growSize(max, max) + 1;
730 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600731
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600732 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600733 bc_num_clean(&l1);
734 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600735 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600736 bc_num_clean(&l2);
737 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700738
Gavin Howard14c354a2019-04-24 14:28:24 -0600739 bc_num_expand(c, max);
740 c->len = max;
Gavin Howard404ece72019-04-26 15:59:15 -0600741 memset(c->num, 0, BC_NUM_SIZE(c->len));
Gavin Howard305249a2018-10-15 20:24:47 -0600742
Gavin Howard14c354a2019-04-24 14:28:24 -0600743 s = bc_num_sub(&h1, &l1, &m1, 0);
744 if (BC_ERR(s)) goto err;
745 s = bc_num_sub(&l2, &h2, &m2, 0);
746 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600747
Gavin Howard971a2672019-04-26 14:32:07 -0600748 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600749
Gavin Howard14c354a2019-04-24 14:28:24 -0600750 s = bc_num_m(&h1, &h2, &z2, 0);
751 if (BC_ERR(s)) goto err;
752 bc_num_clean(&z2);
753
754 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
755 if (BC_ERR(s)) goto err;
756 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
757 if (BC_ERR(s)) goto err;
758 }
759
Gavin Howard971a2672019-04-26 14:32:07 -0600760 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600761
762 s = bc_num_m(&l1, &l2, &z0, 0);
763 if (BC_ERR(s)) goto err;
764 bc_num_clean(&z0);
765
766 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
767 if (BC_ERR(s)) goto err;
768 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
769 if (BC_ERR(s)) goto err;
770 }
771
Gavin Howard971a2672019-04-26 14:32:07 -0600772 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600773
774 s = bc_num_m(&m1, &m2, &z1, 0);
775 if (BC_ERR(s)) goto err;
776 bc_num_clean(&z1);
777
778 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
779 s = bc_num_shiftAddSub(c, &z1, max2, op);
780 if (BC_ERR(s)) goto err;
781 }
Gavin Howard305249a2018-10-15 20:24:47 -0600782
783err:
Gavin Howard5bede412019-02-23 09:31:28 -0700784 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600785 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600786 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600787 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600788 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600789 return s;
790}
791
Gavin Howard1cbfe242019-01-09 17:13:11 -0700792static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600793
Gavin Howardfcf29012019-04-25 20:12:19 -0600794 // TODO: Check this function.
795
Gavin Howard305249a2018-10-15 20:24:47 -0600796 BcStatus s;
797 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600798 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600799
Gavin Howard14913fc2019-04-23 14:36:03 -0600800 bc_num_setToZero(c, 0);
801
Gavin Howardcf9f2462019-04-27 06:43:57 -0600802 ascale = a->scale;
803 bscale = b->scale;
804 scale = BC_MAX(scale, ascale);
805 scale = BC_MAX(scale, b->scale);
Gavin Howard48a0e472019-04-26 15:39:46 -0600806 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600807 scale = BC_MIN(rscale, scale);
Gavin Howard305249a2018-10-15 20:24:47 -0600808
Gavin Howardbaa4f582019-01-24 13:56:35 -0700809 bc_num_createCopy(&cpa, a);
810 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600811
Gavin Howard23d8d252018-10-17 12:03:41 -0600812 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600813
Gavin Howard48a0e472019-04-26 15:39:46 -0600814 ardx = cpa.rdx * BC_BASE_POWER;
815 s = bc_num_shiftLeft(&cpa, ardx);
816 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600817 bc_num_clean(&cpa);
818 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600819
820 brdx = cpb.rdx * BC_BASE_POWER;
821 s = bc_num_shiftLeft(&cpb, brdx);
822 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600823 bzero = bc_num_shiftZero(&cpb);
824 bc_num_clean(&cpb);
825
Gavin Howard14c354a2019-04-24 14:28:24 -0600826 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700827 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600828
Gavin Howard14913fc2019-04-23 14:36:03 -0600829 zero = bc_vm_growSize(azero, bzero);
830 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600831
Gavin Howard14913fc2019-04-23 14:36:03 -0600832 bc_num_expand(c, len);
Gavin Howard48a0e472019-04-26 15:39:46 -0600833 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_POWER);
834 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
835 s = bc_num_shiftRight(c, ardx + brdx);
836 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600837
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600838 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600839
Gavin Howard305249a2018-10-15 20:24:47 -0600840err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600841 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600842 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600843 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600844 bc_num_free(&cpa);
845 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700846}
847
Stefan Esser193f8522019-04-27 01:29:01 +0200848static BcStatus bc_num_invert(BcNum *val, size_t bits, size_t scale) { // --> num.h <se>
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600849
Stefan Esser193f8522019-04-27 01:29:01 +0200850 BcNum xi, two, temp;
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600851 size_t rdx, bitlimit;
Stefan Esser193f8522019-04-27 01:29:01 +0200852 BcStatus s = BC_STATUS_SUCCESS;
Stefan Esser193f8522019-04-27 01:29:01 +0200853
854 bitlimit = ((val->rdx + 1) * 50 * BC_BASE_POWER) / 10;
Stefan Esser193f8522019-04-27 01:29:01 +0200855 bc_num_createCopy(&xi, val);
Stefan Esser193f8522019-04-27 01:29:01 +0200856 bc_num_init(&two, 1);
857 bc_num_ulong2num(&two, 2);
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600858 bc_num_init(&temp, bc_num_mulReq(val, &xi, scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200859 while (bits <= bitlimit) {
860 s = bc_num_mul(val, &xi, &temp, scale + 1);
861 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
862 s = bc_num_sub(&two, &temp, &temp, scale + 1);
863 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
864 s = bc_num_mul(&temp, &xi, &xi, scale + 1);
865 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
866 bits *= 2;
867 }
868 bc_num_copy(val, &xi);
869err:
870 bc_num_free(&xi);
871 bc_num_free(&temp);
872 bc_num_free(&two);
873
874 return s;
875}
876
877static int intlog2(size_t n) {
878 int bits = 0;
879
880 n >>= 1;
881 while (n != 0) {
882 bits++;
883 n >>= 1;
884 }
885 return (bits);
886}
887
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600888static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700889
Gavin Howardfcf29012019-04-25 20:12:19 -0600890 // TODO: Check this function.
891
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600892 BcStatus s = BC_STATUS_SUCCESS;
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600893 ssize_t cmp;
Stefan Esser193f8522019-04-27 01:29:01 +0200894 size_t len, rdx;
895 BcNum b1, f;
896 size_t factor, dividend, divisor;
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600897 size_t i, j, digits, maxdigits, mindivisor;
898 int validbits, shift;
899 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700900
Gavin Howardddce6d42019-01-03 14:07:06 -0700901 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
902 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600903 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600904 return BC_STATUS_SUCCESS;
905 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700906 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600907 bc_num_copy(c, a);
908 bc_num_retireMul(c, scale, a->neg, b->neg);
909 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600910 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700911
Stefan Esser193f8522019-04-27 01:29:01 +0200912 maxdigits = (sizeof(dividend) * 24) / 10;
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600913 maxdigits = (maxdigits / BC_BASE_POWER) * BC_BASE_POWER;
914 mindivisor = (1 << ((((maxdigits - BC_BASE_POWER)/2 + 1) * 3) + 2)) + 1;
Gavin Howard021150b2018-03-10 15:40:42 -0700915
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600916 shift = (a->len - a->rdx) - (b->len - b->rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700917
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600918 bc_num_createCopy(c, a);
919 c->rdx = c->len; // apply e_a at end
Gavin Howardedd9d9d2019-04-27 07:28:45 -0600920
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600921 bc_num_createCopy(&b1, b);
922 b1.rdx = b1.len; // apply e_b at end
923
924 neg = c->neg != b1.neg;
925 c->neg = false;
926 b1.neg = false;
927 cmp = bc_num_cmp(&b1, c);
928 if (cmp == 0) {
929 bc_num_free(c);
930 bc_num_init(c, 1); // identical BcNum arrays
931 bc_num_ulong2num(c, 1);
932 } else {
933 if (cmp > 0) //==> b > a, result will be one exp higher
934 shift--;
935
936 dividend = 1;
937 divisor = 0;
938 digits = 0;
939 j = 0;
940
941 for (i = 0, digits = 0; digits < maxdigits; digits += BC_BASE_POWER, i++) {
942 dividend = dividend * BC_BASE_DIG;
943 if (i < b1.len) {
944 if (divisor < mindivisor)
945 divisor = divisor * BC_BASE_DIG + b1.num[b1.len - 1 - i];
946 else
947 if (b1.num[b1.len - 1 - i] != 0) j = 1;
Stefan Esser193f8522019-04-27 01:29:01 +0200948 }
Gavin Howard5a788392019-03-28 11:01:13 -0600949 }
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600950 for (; i < b1.len; i++)
951 if (b1.num[b1.len - 1 - i] != 0) j = 1;
952
953 divisor += j;
954 factor = dividend / divisor;
955
956 bc_num_init(&f, maxdigits);
957 bc_num_ulong2num(&f, factor);
958
959 bc_num_mul(&f, &b1, &b1, scale);
960
961 if (b1.num[b1.len - 1] == 1) {
962 b1.rdx = b1.len - 1; // only possible if b1 == 1.000000...
963 } else {
964 b1.rdx = b1.len;
965 validbits = factor < divisor ? intlog2(factor) : intlog2(divisor);
966 bc_num_invert(&b1, validbits, (a->len + b1.len) * BC_BASE_POWER);
967 if (b1.len > c->len)
968 bc_num_extend(c, c->len + b1.len); // ???
969 bc_num_mul(&b1, c, c, scale);
970 }
971 bc_num_mul(&f, c, c, scale);
972 c->rdx = c->len - 1;
Gavin Howard63738202018-09-26 15:34:20 -0600973 }
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600974 if (shift > 0) {
975 bc_num_expand(c, shift * BC_BASE_POWER);
976 bc_num_shiftLeft(c, shift * BC_BASE_POWER);
Stefan Esser193f8522019-04-27 01:29:01 +0200977 } else {
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600978 bc_num_extend(c, -shift * BC_BASE_POWER);
979 bc_num_shiftRight(c, -shift * BC_BASE_POWER);
Stefan Esser193f8522019-04-27 01:29:01 +0200980 }
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600981 // bc_num_truncDecimals(c, scale);
Gavin Howard034b5b92019-04-27 07:28:56 -0600982
Stefan Esser193f8522019-04-27 01:29:01 +0200983err:
Gavin Howardf2ee6342019-04-09 17:05:20 -0600984 if (BC_SIG) s = BC_STATUS_SIGNAL;
Stefan Eßerdaaaaa72019-04-27 18:04:02 -0600985 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, neg, false);
986
Gavin Howard63738202018-09-26 15:34:20 -0600987 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700988}
989
Gavin Howard1cbfe242019-01-09 17:13:11 -0700990static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
991 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600992{
Gavin Howardfcf29012019-04-25 20:12:19 -0600993 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -0600994 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600995 BcNum temp;
996 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700997
Gavin Howardddce6d42019-01-03 14:07:06 -0700998 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
999 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -07001000 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001001 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001002 return BC_STATUS_SUCCESS;
1003 }
Gavin Howard8b254872018-03-14 01:13:35 -06001004
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001005 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -07001006 s = bc_num_d(a, b, c, scale);
1007 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001008 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001009
Gavin Howard1ab22d22019-01-03 13:32:17 -07001010 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -06001011
Gavin Howard53eba8b2018-10-31 15:14:37 -06001012 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001013 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001014 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001015 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -06001016
Gavin Howard77445432019-04-26 15:59:33 -06001017 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
Gavin Howard18441082018-10-22 10:03:30 -06001018
1019 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001020 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -06001021 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001022
1023err:
Gavin Howard18441082018-10-22 10:03:30 -06001024 bc_num_free(&temp);
1025 return s;
1026}
1027
Gavin Howard2d188a52019-02-25 14:19:08 -07001028static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1029{
Gavin Howardfcf29012019-04-25 20:12:19 -06001030 // TODO: Check this function.
Gavin Howard18441082018-10-22 10:03:30 -06001031 BcStatus s;
1032 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -07001033 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -06001034
Gavin Howard77445432019-04-26 15:59:33 -06001035 ts = bc_vm_growSize(scale, b->scale);
1036 ts = BC_MAX(ts, a->scale);
Gavin Howard798508b2019-02-21 16:45:32 -07001037
1038 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -06001039 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001040 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -06001041
Gavin Howard63738202018-09-26 15:34:20 -06001042 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001043}
1044
Gavin Howard1cbfe242019-01-09 17:13:11 -07001045static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001046
Gavin Howardfcf29012019-04-25 20:12:19 -06001047 // TODO: Check this function.
1048
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001049 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001050 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -07001051 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001052 size_t i, powrdx, resrdx;
1053 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001054
Gavin Howardecafd4f2019-02-23 09:30:45 -07001055 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001056
Gavin Howardddce6d42019-01-03 14:07:06 -07001057 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -06001058 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -06001059 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001060 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001061 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001062 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001063 return BC_STATUS_SUCCESS;
1064 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001065 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001066 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -06001067 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001068 return s;
1069 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001070
Gavin Howard63738202018-09-26 15:34:20 -06001071 neg = b->neg;
1072 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001073 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -07001074 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001075 if (s) return s;
1076
Gavin Howardbaa4f582019-01-24 13:56:35 -07001077 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001078
Gavin Howard63738202018-09-26 15:34:20 -06001079 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -06001080
Gavin Howard2d188a52019-02-25 14:19:08 -07001081 for (powrdx = a->rdx; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -06001082 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001083 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001084 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001085 }
Gavin Howard6fb635f2018-03-03 12:45:42 -07001086
Gavin Howard2d188a52019-02-25 14:19:08 -07001087 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -07001088
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001089 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -07001090 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001091
Gavin Howard2d188a52019-02-25 14:19:08 -07001092 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001093
Gavin Howard4d5daba2019-01-07 14:42:50 -07001094 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001095 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001096 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001097
Gavin Howard63738202018-09-26 15:34:20 -06001098 if (pow & 1) {
1099 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001100 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001101 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001102 }
1103 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001104
Gavin Howard2d188a52019-02-25 14:19:08 -07001105 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -07001106 if (neg) {
1107 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001108 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -07001109 }
1110
Gavin Howard63738202018-09-26 15:34:20 -06001111 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001112
Gavin Howard53eba8b2018-10-31 15:14:37 -06001113 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -06001114 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001115 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001116
Gavin Howard9b801632019-02-16 23:34:06 -07001117sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001118 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001119err:
Gavin Howard63738202018-09-26 15:34:20 -06001120 bc_num_free(&copy);
1121 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001122}
1123
Gavin Howard7bda4782018-12-28 09:53:22 -07001124#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -07001125static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
1126 size_t scale)
1127{
Gavin Howardac4d9122018-12-27 23:59:12 -07001128 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001129 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001130
1131 BC_UNUSED(scale);
1132
1133 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001134 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001135
Gavin Howardc9bef2d2019-04-26 14:46:52 -06001136 if (val < c->scale) bc_num_truncate(c, c->scale - val);
1137 else if (val > c->scale) bc_num_extend(c, val - c->scale);
Gavin Howardac4d9122018-12-27 23:59:12 -07001138
1139 return s;
1140}
1141
Gavin Howard2d188a52019-02-25 14:19:08 -07001142static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1143{
Gavin Howardac4d9122018-12-27 23:59:12 -07001144 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001145 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001146
1147 BC_UNUSED(scale);
1148
1149 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001150 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001151
Gavin Howardb1558662019-04-26 14:32:55 -06001152 return bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001153}
1154
Gavin Howard2d188a52019-02-25 14:19:08 -07001155static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1156 size_t scale)
1157{
Gavin Howardac4d9122018-12-27 23:59:12 -07001158 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001159 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001160
1161 BC_UNUSED(scale);
1162
1163 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001164 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001165
Gavin Howard2a366922019-01-16 10:50:20 -07001166 if (BC_NUM_ZERO(c)) return s;
1167
Gavin Howardb1558662019-04-26 14:32:55 -06001168 return bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001169}
Gavin Howard7bda4782018-12-28 09:53:22 -07001170#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001171
Gavin Howard1cbfe242019-01-09 17:13:11 -07001172static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1173 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001174{
Gavin Howarda1c44392018-09-27 12:41:15 -06001175 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001176 BcNum num2, *ptr_a, *ptr_b;
1177 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001178
Gavin Howard63738202018-09-26 15:34:20 -06001179 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001180
Gavin Howard890d0c02018-10-30 16:34:50 -06001181 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001182 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001183 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001184 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001185 }
1186 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001187
Gavin Howard63738202018-09-26 15:34:20 -06001188 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001189 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001190 if (c != a) {
1191 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001192 init = true;
1193 }
1194 }
1195 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001196
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001197 if (init) bc_num_init(c, req);
1198 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001199
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001200 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001201
Gavin Howardddce6d42019-01-03 14:07:06 -07001202 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001203 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001204
Gavin Howardba009802018-09-29 04:41:51 -06001205 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001206
Stefan Esser193f8522019-04-27 01:29:01 +02001207// bc_num_truncDecimals(c, scale);
1208
Gavin Howardee9d01e2019-02-16 22:48:11 -07001209 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001210}
1211
Gavin Howardc90ed902019-01-02 13:07:49 -07001212#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001213static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001214
Gavin Howardc7d655b2018-12-28 19:45:13 -07001215 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001216 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001217
Gavin Howard63738202018-09-26 15:34:20 -06001218 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001219
Gavin Howard63738202018-09-26 15:34:20 -06001220 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001221
Gavin Howard8a921bd2018-10-11 14:15:32 -06001222 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001223
Gavin Howard63738202018-09-26 15:34:20 -06001224 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001225
Gavin Howard63738202018-09-26 15:34:20 -06001226 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001227
Gavin Howard63738202018-09-26 15:34:20 -06001228 radix = true;
1229 continue;
1230 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001231
Gavin Howardc7d655b2018-12-28 19:45:13 -07001232 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001233 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001234
Gavin Howard63738202018-09-26 15:34:20 -06001235 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001236}
Gavin Howardc90ed902019-01-02 13:07:49 -07001237#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001238
Gavin Howard1cbfe242019-01-09 17:13:11 -07001239static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001240
Gavin Howardfcf29012019-04-25 20:12:19 -06001241 // TODO: Check this function.
1242
Gavin Howardc7d655b2018-12-28 19:45:13 -07001243 if (isupper(c)) {
1244 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001245 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001246 }
1247 else c -= '0';
1248
1249 return (unsigned long) (uchar) c;
1250}
1251
Gavin Howard1cbfe242019-01-09 17:13:11 -07001252static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001253
Gavin Howard3b60abb2019-04-26 08:49:17 -06001254 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001255 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001256 bool zero = true, rdx;
Stefan Essera299ffc2019-04-24 23:28:32 +02001257
Gavin Howard63738202018-09-26 15:34:20 -06001258 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001259
Gavin Howard63738202018-09-26 15:34:20 -06001260 val += i;
Gavin Howard3b60abb2019-04-26 08:49:17 -06001261 assert(isalnum(val[0]) || val[0] == '.');
Gavin Howard63738202018-09-26 15:34:20 -06001262 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001263
Gavin Howard63738202018-09-26 15:34:20 -06001264 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001265 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001266
Gavin Howardd43ec372019-04-25 21:13:54 -06001267 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001268
Gavin Howardd43ec372019-04-25 21:13:54 -06001269 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001270 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001271
Gavin Howard3b60abb2019-04-26 08:49:17 -06001272 i = len - (ptr == val ? 0 : i) - rdx;
Gavin Howard48a0e472019-04-26 15:39:46 -06001273 temp = BC_NUM_ROUND_POW(i);
Gavin Howard3b60abb2019-04-26 08:49:17 -06001274 mod = n->scale % BC_BASE_POWER;
1275 i = mod ? BC_BASE_POWER - mod : 0;
1276 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001277
1278 bc_num_expand(n, n->len);
Gavin Howard404ece72019-04-26 15:59:15 -06001279 memset(n->num, 0, BC_NUM_SIZE(n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -07001280
Gavin Howard6076dbb2019-04-26 09:47:50 -06001281 if (zero) n->len = n->rdx = 0;
1282 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001283
Gavin Howard971a2672019-04-26 14:32:07 -06001284 unsigned long exp, pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001285
Gavin Howard3b60abb2019-04-26 08:49:17 -06001286 exp = i;
Gavin Howard971a2672019-04-26 14:32:07 -06001287 pow = bc_num_pow10(exp);
Gavin Howardd43ec372019-04-25 21:13:54 -06001288
1289 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001290
Gavin Howardc7d655b2018-12-28 19:45:13 -07001291 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001292
Gavin Howardd43ec372019-04-25 21:13:54 -06001293 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001294 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001295
1296 size_t idx = exp / BC_BASE_POWER;
1297
Gavin Howard8dd307e2019-01-08 23:05:19 -07001298 if (isupper(c)) c = '9';
Gavin Howardf196fbe2019-04-26 08:49:05 -06001299 n->num[idx] += (((unsigned long) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001300
1301 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1302 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001303 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001304 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001305 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001306}
1307
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001308static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1309 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001310{
Gavin Howardfcf29012019-04-25 20:12:19 -06001311 // TODO: Check this function.
Gavin Howard1cbfe242019-01-09 17:13:11 -07001312 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001313 BcNum temp, mult, result;
Gavin Howard77445432019-04-26 15:59:33 -06001314 char c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001315 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001316 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001317 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001318
Gavin Howard11b9afd2018-10-18 14:23:28 -06001319 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001320 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001321
Gavin Howard26d078f2019-01-24 13:58:12 -07001322 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1323 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001324
Gavin Howardc7d655b2018-12-28 19:45:13 -07001325 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001326
Gavin Howardc7d655b2018-12-28 19:45:13 -07001327 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001328
Gavin Howard53eba8b2018-10-31 15:14:37 -06001329 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001330 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001331 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001332 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001333 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001334 }
Gavin Howardede51f02018-03-02 12:30:00 -07001335
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001336 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001337
Gavin Howard63738202018-09-26 15:34:20 -06001338 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001339 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001340 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001341
Gavin Howard2d188a52019-02-25 14:19:08 -07001342 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001343 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001344 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001345
Gavin Howard53eba8b2018-10-31 15:14:37 -06001346 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001347 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001348
Gavin Howard705c4bc2018-12-03 15:38:04 -07001349 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001350 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001351 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001352 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001353 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001354 }
Gavin Howardede51f02018-03-02 12:30:00 -07001355
Gavin Howardf2ee6342019-04-09 17:05:20 -06001356 if (BC_SIG) {
1357 s = BC_STATUS_SIGNAL;
1358 goto err;
1359 }
1360
Gavin Howard1769abc2019-02-21 09:36:14 -07001361 // This one cannot be a divide by 0 because mult starts out at 1, then is
1362 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1363 s = bc_num_div(&result, &mult, &result, digs);
1364 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001365 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001366 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001367 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001368
Gavin Howardddce6d42019-01-03 14:07:06 -07001369 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001370 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001371 }
1372 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001373
Gavin Howardd1417682019-02-15 17:08:42 -07001374err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001375 bc_num_free(&result);
1376int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001377 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001378 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001379 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001380}
1381
Gavin Howard1cbfe242019-01-09 17:13:11 -07001382static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001383 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001384 bc_vm_putchar('\\');
1385 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001386 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001387 }
Gavin Howard80977b22018-09-06 14:29:13 -06001388}
1389
Gavin Howard40a085f2018-12-03 12:08:59 -07001390#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001391static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001392 BC_UNUSED(rdx);
1393 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001394 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001395}
1396#endif // DC_ENABLED
1397
Gavin Howard1cbfe242019-01-09 17:13:11 -07001398static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1399
Gavin Howardfcf29012019-04-25 20:12:19 -06001400 // TODO: Check this function.
1401
Gavin Howarda84ad992018-12-03 19:11:06 -07001402 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001403
Gavin Howardab5e9632019-01-02 13:32:02 -07001404 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001405 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001406 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001407
Gavin Howardab5e9632019-01-02 13:32:02 -07001408 bc_num_printNewline();
Gavin Howard530e3072019-04-23 16:23:36 -06001409 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001410
Gavin Howard530e3072019-04-23 16:23:36 -06001411 for (exp = 0; exp < len; pow /= BC_BASE, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001412 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001413 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001414 dig = n / pow;
1415 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001416 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001417 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001418}
Gavin Howardfe679f02018-02-14 15:50:09 -07001419
Gavin Howard1cbfe242019-01-09 17:13:11 -07001420static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001421
Gavin Howard3c02ed32018-12-28 18:17:15 -07001422 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001423
Gavin Howard3c02ed32018-12-28 18:17:15 -07001424 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001425 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001426 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001427 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001428 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001429
Gavin Howardab5e9632019-01-02 13:32:02 -07001430 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001431 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001432 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001433}
1434
Gavin Howard1cbfe242019-01-09 17:13:11 -07001435static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001436
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001437 size_t i, j, rdx = n->rdx;
1438 bool zero = true;
1439 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001440
Gavin Howard48af52e2018-10-30 14:47:38 -06001441 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001442 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001443
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001444 for (i = n->len - 1; i < n->len; --i) {
1445
1446 BcDig n9 = n->num[i];
1447 size_t temp;
1448 bool irdx = (i == rdx - 1);
1449
1450 zero = (zero & !irdx);
1451 temp = n->scale % BC_BASE_POWER;
1452 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1453
1454 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1455
1456 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Essera299ffc2019-04-24 23:28:32 +02001457 buffer[j] = n9 % BC_BASE;
1458 n9 /= BC_BASE;
1459 }
Stefan Esser0da17752019-04-25 12:25:15 +02001460
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001461 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1462 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1463 zero = (zero && buffer[j] == 0);
1464 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Essera299ffc2019-04-24 23:28:32 +02001465 }
1466 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001467}
1468
Gavin Howard7ad5a662019-02-19 14:40:46 -07001469#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001470static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) {
Gavin Howard7ad5a662019-02-19 14:40:46 -07001471
Gavin Howardfcf29012019-04-25 20:12:19 -06001472 // TODO: Check this function.
1473
Gavin Howardb1558662019-04-26 14:32:55 -06001474 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001475 bool neg = (n->len <= n->rdx);
1476 BcNum temp, exp;
1477 size_t places, mod;
1478 BcDig digs[BC_NUM_LONG_LOG10];
1479
1480 bc_num_createCopy(&temp, n);
1481
1482 if (neg) {
1483 places = n->rdx - bc_num_len(n) + 1;
1484 mod = places % 3;
1485 if (eng && mod != 0) places += 3 - mod;
Gavin Howardb1558662019-04-26 14:32:55 -06001486 s = bc_num_shiftLeft(&temp, places);
1487 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001488 }
1489 else {
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001490 places = bc_num_int(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001491 mod = places % 3;
1492 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howardb1558662019-04-26 14:32:55 -06001493 s = bc_num_shiftRight(&temp, places);
1494 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001495 }
1496
1497 bc_num_printDecimal(&temp);
1498
1499 bc_num_printNewline();
1500 bc_vm_putchar('e');
1501
1502 if (!places) {
1503 bc_num_printHex(0, 1, false);
1504 goto exit;
1505 }
1506
1507 if (neg) {
1508 bc_num_printNewline();
1509 bc_vm_putchar('-');
1510 }
1511
1512 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1513 bc_num_ulong2num(&exp, (unsigned long) places);
1514
1515 bc_num_printDecimal(&exp);
1516
1517exit:
1518 bc_num_free(&temp);
Gavin Howardb1558662019-04-26 14:32:55 -06001519 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001520}
1521#endif // BC_ENABLE_EXTRA_MATH
1522
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001523static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1524 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001525{
Gavin Howardfcf29012019-04-25 20:12:19 -06001526 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001527 BcStatus s;
1528 BcVec stack;
1529 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001530 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001531 size_t i;
1532 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001533
Gavin Howard971a2672019-04-26 14:32:07 -06001534 assert(BC_NUM_NONZERO(base));
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001535
Gavin Howardddce6d42019-01-03 14:07:06 -07001536 if (BC_NUM_ZERO(n)) {
1537 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001538 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001539 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001540
Gavin Howardd3929832018-12-24 15:13:15 -07001541 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001542 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001543 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001544 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001545 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001546 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001547
Gavin Howard63738202018-09-26 15:34:20 -06001548 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001549 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001550 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001551
Gavin Howard2d188a52019-02-25 14:19:08 -07001552 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001553
1554 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001555 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001556 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001557
1558 // Will never fail (except for signals) because digit is
1559 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001560 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001561 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001562
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001563 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001564 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001565
Gavin Howard2d188a52019-02-25 14:19:08 -07001566 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001567
Gavin Howard2d188a52019-02-25 14:19:08 -07001568 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001569 ptr = bc_vec_item_rev(&stack, i);
1570 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001571 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001572 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001573
Gavin Howard2d188a52019-02-25 14:19:08 -07001574 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001575 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001576
Gavin Howard2d188a52019-02-25 14:19:08 -07001577 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001578
Gavin Howard53eba8b2018-10-31 15:14:37 -06001579 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001580 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001581
1582 // Will never fail (except for signals) because fracp is
1583 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001584 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001585 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001586
Gavin Howard705c4bc2018-12-03 15:38:04 -07001587 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001588 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001589 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001590
Gavin Howardddce6d42019-01-03 14:07:06 -07001591 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001592 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001593 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001594 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001595
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001596sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001597 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001598err:
Gavin Howard63738202018-09-26 15:34:20 -06001599 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001600 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001601 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001602 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001603 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001604 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001605}
1606
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001607static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1608 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001609{
Gavin Howardfcf29012019-04-25 20:12:19 -06001610 // TODO: Check this function.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001611 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001612 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001613 BcNumDigitOp print;
1614 bool neg = n->neg;
1615
Gavin Howard48af52e2018-10-30 14:47:38 -06001616 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001617 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001618
1619 n->neg = false;
1620
Gavin Howardbe4e8222019-01-03 13:34:16 -07001621 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001622 width = 1;
1623 print = bc_num_printHex;
1624 }
1625 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001626 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001627 print = bc_num_printDigits;
1628 }
1629
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001630 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001631 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001632
1633 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001634}
1635
Gavin Howard40a085f2018-12-03 12:08:59 -07001636#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001637BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1638 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001639}
1640#endif // DC_ENABLED
1641
Gavin Howard3c02ed32018-12-28 18:17:15 -07001642void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001643 assert(n);
1644 n->num = num;
1645 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001646 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001647 n->neg = false;
1648}
1649
Gavin Howard3c02ed32018-12-28 18:17:15 -07001650void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001651 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001652 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howard404ece72019-04-26 15:59:15 -06001653 bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001654}
1655
Gavin Howarded392aa2018-02-27 13:09:26 -07001656void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001657 assert(num);
1658 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001659}
1660
Gavin Howard3c02ed32018-12-28 18:17:15 -07001661void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001662 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001663 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001664 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001665 d->len = s->len;
1666 d->neg = s->neg;
1667 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001668 d->scale = s->scale;
Gavin Howard404ece72019-04-26 15:59:15 -06001669 memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -07001670}
1671
Gavin Howardbaa4f582019-01-24 13:56:35 -07001672void bc_num_createCopy(BcNum *d, const BcNum *s) {
1673 bc_num_init(d, s->len);
1674 bc_num_copy(d, s);
1675}
1676
1677void bc_num_createFromUlong(BcNum *n, unsigned long val) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001678 bc_num_init(n, (BC_NUM_LONG_LOG10 - 1) / BC_BASE_POWER + 1);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001679 bc_num_ulong2num(n, val);
1680}
1681
Gavin Howard7ad5a662019-02-19 14:40:46 -07001682size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001683 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001684}
1685
1686size_t bc_num_len(const BcNum *restrict n) {
1687
Gavin Howardb41483b2019-04-27 08:30:10 -06001688 size_t i, pow, scale, len = n->len;
Gavin Howardfcf29012019-04-25 20:12:19 -06001689 BcDig dig;
1690
Gavin Howardb41483b2019-04-27 08:30:10 -06001691 if (BC_NUM_ZERO(n)) return 0;
1692
1693 if (n->rdx == len) {
1694 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1695 }
Gavin Howard7ad5a662019-02-19 14:40:46 -07001696
Gavin Howardfcf29012019-04-25 20:12:19 -06001697 dig = n->num[len - 1];
Gavin Howardb41483b2019-04-27 08:30:10 -06001698 pow = BC_BASE_DIG;
1699 i = BC_BASE_POWER + 1;
Gavin Howardfcf29012019-04-25 20:12:19 -06001700
Gavin Howardb41483b2019-04-27 08:30:10 -06001701 while (pow && (dig % (BcDig) pow == dig)) {
1702 i -= 1;
1703 pow /= BC_BASE;
1704 }
Gavin Howardfcf29012019-04-25 20:12:19 -06001705
Gavin Howardb41483b2019-04-27 08:30:10 -06001706 scale = n->scale % BC_BASE_POWER;
1707 scale = scale ? scale : BC_BASE_POWER;
1708
1709 return (len - 1) * BC_BASE_POWER + i - (BC_BASE_POWER - scale);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001710}
1711
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001712BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1713 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001714{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001715 BcStatus s = BC_STATUS_SUCCESS;
1716
Gavin Howard63738202018-09-26 15:34:20 -06001717 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001718 assert(BC_ENABLE_EXTRA_MATH ||
1719 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001720 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001721
Gavin Howardd746e802018-12-28 22:06:00 -07001722 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06001723 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001724 else s = bc_num_parseBase(n, val, base, base_t);
1725
1726 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001727}
1728
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001729BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1730 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001731{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001732 BcStatus s = BC_STATUS_SUCCESS;
1733
Gavin Howardab5e9632019-01-02 13:32:02 -07001734 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001735 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001736
Gavin Howardab5e9632019-01-02 13:32:02 -07001737 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001738
Gavin Howardddce6d42019-01-03 14:07:06 -07001739 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06001740 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001741#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001742 else if (base_t == 0 || base_t == 1)
1743 s = bc_num_printExponent(n, base_t != 0);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001744#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001745 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001746
Gavin Howarde5f11c72019-02-23 09:42:51 -07001747 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001748 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001749 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001750 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001751
1752 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001753}
1754
Gavin Howard3c02ed32018-12-28 18:17:15 -07001755BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001756
Gavin Howard63738202018-09-26 15:34:20 -06001757 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001758 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001759
Gavin Howard63738202018-09-26 15:34:20 -06001760 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001761
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001762 *result = 0;
1763
Gavin Howardecafd4f2019-02-23 09:30:45 -07001764 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001765
Gavin Howard06b6e862019-02-15 11:15:31 -07001766 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001767
Gavin Howard0700efc2019-04-24 17:31:23 -06001768 unsigned long prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07001769
Gavin Howard0700efc2019-04-24 17:31:23 -06001770 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001771 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1772
Gavin Howard77445432019-04-26 15:59:33 -06001773 r = prev + (unsigned long) n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07001774
1775 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001776 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001777 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001778
Gavin Howard7b557da2018-12-21 10:25:32 -07001779 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001780
Gavin Howard63738202018-09-26 15:34:20 -06001781 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001782}
1783
Gavin Howard3c02ed32018-12-28 18:17:15 -07001784void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001785
Gavin Howardc25fd612019-04-26 19:31:31 -06001786 BcDig *ptr;
1787 unsigned long i;
1788
Gavin Howard63738202018-09-26 15:34:20 -06001789 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001790
Gavin Howard63738202018-09-26 15:34:20 -06001791 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001792
Gavin Howard1ab22d22019-01-03 13:32:17 -07001793 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001794
Gavin Howard798508b2019-02-21 16:45:32 -07001795 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howard05feab02019-04-23 16:24:07 -06001796
Gavin Howardc25fd612019-04-26 19:31:31 -06001797 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_DIG)
1798 ptr[i] = val % BC_BASE_DIG;
Gavin Howard025d04d2018-02-20 13:53:28 -07001799}
1800
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001801size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001802
1803 size_t aint, bint, ardx, brdx;
1804
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001805 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001806
1807 ardx = a->rdx;
1808 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001809 aint = bc_num_int(a);
1810 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001811 ardx = BC_MAX(ardx, brdx);
1812 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001813
Gavin Howard2d188a52019-02-25 14:19:08 -07001814 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001815}
1816
1817size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001818 size_t max, rdx;
1819 rdx = bc_vm_growSize(a->rdx, b->rdx);
1820 max = bc_vm_growSize(BC_MAX(scale, rdx), 1);
1821 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1822 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001823}
1824
1825size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1826 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001827 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001828}
1829
1830#if BC_ENABLE_EXTRA_MATH
1831size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1832 BC_UNUSED(b);
1833 BC_UNUSED(scale);
1834 return BC_NUM_SHREQ(a);
1835}
1836#endif // BC_ENABLE_EXTRA_MATH
1837
Gavin Howard12fe7812018-09-29 03:45:18 -06001838BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001839 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001840 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001841 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001842}
1843
Gavin Howard12fe7812018-09-29 03:45:18 -06001844BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001845 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001846 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001847 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001848}
1849
Gavin Howard12fe7812018-09-29 03:45:18 -06001850BcStatus bc_num_mul(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_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001852}
1853
Gavin Howard12fe7812018-09-29 03:45:18 -06001854BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001855 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001856}
1857
Gavin Howard54b946a2018-10-23 12:06:57 -06001858BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001859 size_t req = bc_num_mulReq(a, b, scale);
1860 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001861}
1862
Gavin Howard12fe7812018-09-29 03:45:18 -06001863BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001864 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001865}
1866
Gavin Howard7d8f0382018-12-27 16:15:13 -07001867#if BC_ENABLE_EXTRA_MATH
1868BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001869 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001870}
1871
1872BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001873 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001874}
1875
1876BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001877 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001878}
1879#endif // BC_ENABLE_EXTRA_MATH
1880
Gavin Howard3c02ed32018-12-28 18:17:15 -07001881BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001882
Gavin Howardfcf29012019-04-25 20:12:19 -06001883 // TODO: Check this function.
1884
Gavin Howard1cbfe242019-01-09 17:13:11 -07001885 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001886 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard14913fc2019-04-23 14:36:03 -06001887 size_t pow, len, req, digs, digs1, digs2, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001888 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001889 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001890
Gavin Howard34edd0a2018-10-25 15:51:05 -06001891 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001892
Gavin Howardecafd4f2019-02-23 09:30:45 -07001893 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07001894
Gavin Howard007afdf2019-02-23 09:45:42 -07001895 len = bc_vm_growSize(bc_num_int(a), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001896 req = bc_vm_growSize(BC_MAX(scale, a->rdx), len >> 1);
1897 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001898
Gavin Howardddce6d42019-01-03 14:07:06 -07001899 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001900 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001901 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001902 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001903 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001904 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001905 bc_num_extend(b, scale);
1906 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001907 }
Gavin Howard954276b2018-05-16 01:43:58 -06001908
Gavin Howard890d0c02018-10-30 16:34:50 -06001909 scale = BC_MAX(scale, a->rdx) + 1;
Gavin Howard007afdf2019-02-23 09:45:42 -07001910 len = bc_vm_growSize(a->len, scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001911
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001912 bc_num_init(&num1, len);
1913 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001914 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001915
Gavin Howard63738202018-09-26 15:34:20 -06001916 bc_num_one(&half);
1917 half.num[0] = 5;
1918 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001919
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001920 bc_num_init(&f, len);
1921 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001922
Gavin Howard63738202018-09-26 15:34:20 -06001923 x0 = &num1;
1924 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001925
Gavin Howard63738202018-09-26 15:34:20 -06001926 bc_num_one(x0);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001927 pow = bc_num_int(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001928
Gavin Howard53eba8b2018-10-31 15:14:37 -06001929 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001930
Gavin Howardc39fd492018-10-04 10:07:03 -06001931 if (pow & 1) x0->num[0] = 2;
1932 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001933
Gavin Howardc39fd492018-10-04 10:07:03 -06001934 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001935
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001936 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001937
Gavin Howard63738202018-09-26 15:34:20 -06001938 // Make sure to move the radix back.
1939 x0->rdx -= pow;
1940 }
Gavin Howard954276b2018-05-16 01:43:58 -06001941
Gavin Howard14913fc2019-04-23 14:36:03 -06001942 x0->rdx = digs = digs1 = digs2 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001943 resrdx = scale + 2;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001944 len = bc_num_int(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001945
Gavin Howard2d188a52019-02-25 14:19:08 -07001946 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001947
Gavin Howard971a2672019-04-26 14:32:07 -06001948 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07001949
Gavin Howard53eba8b2018-10-31 15:14:37 -06001950 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001951 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001952 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001953 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001954 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001955 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001956 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001957
Gavin Howard53eba8b2018-10-31 15:14:37 -06001958 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06001959 if (cmp == BC_NUM_SSIZE_MIN) {
1960 s = BC_STATUS_SIGNAL;
1961 break;
1962 }
1963
Gavin Howard53eba8b2018-10-31 15:14:37 -06001964 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001965
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001966 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001967 else times = 0;
1968
1969 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001970
1971 cmp2 = cmp1;
1972 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001973 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001974
Gavin Howard63738202018-09-26 15:34:20 -06001975 temp = x0;
1976 x0 = x1;
1977 x1 = temp;
1978 }
Gavin Howard954276b2018-05-16 01:43:58 -06001979
Gavin Howard2d188a52019-02-25 14:19:08 -07001980 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07001981 s = BC_STATUS_SIGNAL;
1982 goto err;
1983 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001984
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001985 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001986 scale -= 1;
1987 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001988
1989err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06001990 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06001991 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001992 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001993 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001994 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001995 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001996 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001997 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001998}
Gavin Howardba009802018-09-29 04:41:51 -06001999
Gavin Howard2cb39612018-10-22 08:46:48 -06002000BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
2001
Gavin Howardfcf29012019-04-25 20:12:19 -06002002 // TODO: Check this function.
2003
Gavin Howard2cb39612018-10-22 08:46:48 -06002004 BcStatus s;
2005 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06002006 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07002007 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002008
Gavin Howardf2ef2f32019-01-16 11:25:16 -07002009 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06002010
Gavin Howard890d0c02018-10-30 16:34:50 -06002011 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06002012 memcpy(&num2, c, sizeof(BcNum));
2013 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002014 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06002015 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06002016 }
Gavin Howardf679ad82018-10-29 12:26:30 -06002017 else {
2018 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002019 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06002020 }
Gavin Howard2cb39612018-10-22 08:46:48 -06002021
Gavin Howard54b946a2018-10-23 12:06:57 -06002022 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002023
Gavin Howardddce6d42019-01-03 14:07:06 -07002024 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002025 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07002026 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002027 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06002028
2029 if (init) bc_num_free(&num2);
2030
2031 return s;
2032}
2033
Gavin Howard40a085f2018-12-03 12:08:59 -07002034#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06002035BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06002036
Gavin Howardfcf29012019-04-25 20:12:19 -06002037 // TODO: Check this function.
2038
Gavin Howardba009802018-09-29 04:41:51 -06002039 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002040 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07002041 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06002042
Gavin Howard34edd0a2018-10-25 15:51:05 -06002043 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06002044
Gavin Howardecafd4f2019-02-23 09:30:45 -07002045 if (BC_ERR(BC_NUM_ZERO(c)))
2046 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
2047 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
2048 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07002049 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06002050
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002051 bc_num_expand(d, c->len);
2052 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07002053 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002054 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06002055
2056 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06002057 two.num[0] = 2;
2058 bc_num_one(d);
2059
Gavin Howard1769abc2019-02-21 09:36:14 -07002060 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002061 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002062 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002063 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07002064 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06002065
Gavin Howard2d188a52019-02-25 14:19:08 -07002066 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06002067
Gavin Howard1769abc2019-02-21 09:36:14 -07002068 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002069 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002070 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002071
Gavin Howarda83a5e52019-02-21 16:51:06 -07002072 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06002073 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002074 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002075
2076 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002077 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002078 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002079 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002080 }
2081
Gavin Howard53eba8b2018-10-31 15:14:37 -06002082 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002083 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002084
2085 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002086 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002087 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002088 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002089 }
2090
Gavin Howard2d188a52019-02-25 14:19:08 -07002091 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002092
Gavin Howardba009802018-09-29 04:41:51 -06002093err:
Gavin Howardba009802018-09-29 04:41:51 -06002094 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002095rem_err:
2096 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002097 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002098 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002099 return s;
2100}
Gavin Howarde6e84762018-10-03 11:46:34 -06002101#endif // DC_ENABLED