blob: d2de1eb186b198190c4db609f0de8802aa5d5252 [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 Esser193f8522019-04-27 01:29:01 +020079 fprintf(stderr, "\n%s= %c", varname, n->len ? (n->neg ? '-' : '+') : ' ');
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 Howard2e736de2019-04-27 06:41:58 -0600213static ssize_t bc_num_compare2(const BcDig *restrict a, size_t len_a,
Stefan Esser193f8522019-04-27 01:29:01 +0200214 const BcDig *restrict b, size_t len_b)
215{
216 int i;
217 int c = 0;
218 size_t len;
219
220 len = BC_MIN(len_a, len_b);
221 for (i = len; c == 0 && i > 0 && BC_NO_SIG; --i) {
222 c = a[--len_a] - b[--len_b];
223 fprintf(stderr, "Compare [%zu,%zu] %09d-%09d => %d\n", len_a, len_b, a[len_a], b[len_b], c);
224 }
225 for (i = len_a; c == 0 && i > 0 && BC_NO_SIG; --i) {
226 c = a[--len_a];
227 fprintf(stderr, "Compare [%zu,%d] %09d-%09d => %d\n", len_a, -1, a[len_a], 0, c);
228 }
229 for (i = len_b; c == 0 && i > 0 && BC_NO_SIG; --i) {
230 c = -b[--len_b];
231 fprintf(stderr, "Compare [%d,%zu] %09d-%09d => %d\n", -1, len_b, 0, b[len_b], c);
232 }
233 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
234}
235
Gavin Howard3c02ed32018-12-28 18:17:15 -0700236ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700237
Gavin Howard63738202018-09-26 15:34:20 -0600238 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600239 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600240 bool a_max, neg = false;
241 ssize_t cmp;
242
243 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700244
Gavin Howard8a921bd2018-10-11 14:15:32 -0600245 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700246 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700247 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600248 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600249 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600250 else return -1;
251 }
252 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700253
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700254 a_int = bc_num_int(a);
255 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600256 a_int -= b_int;
257 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700258
Gavin Howard1ab22d22019-01-03 13:32:17 -0700259 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700260
Gavin Howard890d0c02018-10-30 16:34:50 -0600261 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600262 min = b->rdx;
263 diff = a->rdx - b->rdx;
264 max_num = a->num + diff;
265 min_num = b->num;
266 }
267 else {
268 min = a->rdx;
269 diff = b->rdx - a->rdx;
270 max_num = b->num + diff;
271 min_num = a->num;
272 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700273
Gavin Howard890d0c02018-10-30 16:34:50 -0600274 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600275 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700276 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700277
Gavin Howard2d188a52019-02-25 14:19:08 -0700278 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700279 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600280 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700281
Gavin Howardf2ee6342019-04-09 17:05:20 -0600282 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700283}
284
Gavin Howard2a366922019-01-16 10:50:20 -0700285static void bc_num_clean(BcNum *restrict n) {
286 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
287 if (BC_NUM_ZERO(n)) n->neg = false;
288 else if (n->len < n->rdx) n->len = n->rdx;
289}
290
Gavin Howard3c02ed32018-12-28 18:17:15 -0700291void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700292
Gavin Howardb1558662019-04-26 14:32:55 -0600293 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700294
Stefan Esser70f11852019-04-27 10:39:00 +0200295 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700296
Gavin Howardb1558662019-04-26 14:32:55 -0600297 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
298 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
299
300 n->scale -= places;
301 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700302
Gavin Howardddce6d42019-01-03 14:07:06 -0700303 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600304
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600305 size_t pow;
306
307 pow = n->scale % BC_BASE_POWER;
308 pow = pow ? BC_BASE_POWER - pow : 0;
309 pow = bc_num_pow10(pow);
Gavin Howardb1558662019-04-26 14:32:55 -0600310
311 n->len -= places_rdx;
Gavin Howard404ece72019-04-26 15:59:15 -0600312 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
Gavin Howardb1558662019-04-26 14:32:55 -0600313
314 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600315 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600316
Gavin Howard2a366922019-01-16 10:50:20 -0700317 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600318 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700319}
320
Stefan Esser70f11852019-04-27 10:39:00 +0200321static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700322
Gavin Howardb1558662019-04-26 14:32:55 -0600323 size_t places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700324
Gavin Howard1ab22d22019-01-03 13:32:17 -0700325 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700326
Gavin Howardb1558662019-04-26 14:32:55 -0600327 bc_num_expand(n, n->len + places_rdx);
Gavin Howard404ece72019-04-26 15:59:15 -0600328 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
329 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
Gavin Howarde6f326e2019-04-26 10:13:45 -0600330 n->rdx += places_rdx;
331 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600332 n->len += places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700333
Gavin Howarde6f326e2019-04-26 10:13:45 -0600334 assert(n->rdx == BC_NUM_RDX(n->scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200335}
336
Stefan Esser87c27b52019-04-27 09:16:26 +0200337#if 0 // <se>
Stefan Esser193f8522019-04-27 01:29:01 +0200338static void bc_num_truncDecimals(BcNum *restrict n, int digits) { // --> num.h <se>
339 int i, d;
340
341 if (n->rdx * BC_BASE_POWER > digits) {
342 bc_num_truncate(n, n->rdx - (digits + BC_BASE_POWER - 1) / BC_BASE_POWER);
343 if (n->rdx * BC_BASE_POWER > digits) {
344 d = 10;
345 for (i = digits % BC_BASE_POWER + 1; i < BC_BASE_POWER; i++)
346 d = d * 10;
347 n->num[0] -= n->num[0] % d;
348 }
349 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700350}
Stefan Esser87c27b52019-04-27 09:16:26 +0200351#endif
Gavin Howard6fbdb292018-02-27 15:44:48 -0700352
Gavin Howard2d188a52019-02-25 14:19:08 -0700353static void bc_num_retireMul(BcNum *restrict n, size_t scale,
354 bool neg1, bool neg2)
355{
Gavin Howard971a2672019-04-26 14:32:07 -0600356 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
357 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600358
Gavin Howard305249a2018-10-15 20:24:47 -0600359 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700360 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600361}
362
Gavin Howard2d188a52019-02-25 14:19:08 -0700363static void bc_num_split(const BcNum *restrict n, size_t idx,
364 BcNum *restrict a, BcNum *restrict b)
365{
Gavin Howardfcf29012019-04-25 20:12:19 -0600366 // TODO: Check this function.
Gavin Howard305249a2018-10-15 20:24:47 -0600367 if (idx < n->len) {
368
Gavin Howard305249a2018-10-15 20:24:47 -0600369 b->len = n->len - idx;
370 a->len = idx;
Gavin Howarda3f260c2019-04-26 15:47:25 -0600371 a->scale = a->rdx = b->scale = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600372
Gavin Howard404ece72019-04-26 15:59:15 -0600373 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
374 memcpy(a->num, n->num, BC_NUM_SIZE(idx));
Gavin Howard50c80022019-01-03 15:14:33 -0700375
376 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600377 }
Gavin Howard50c80022019-01-03 15:14:33 -0700378 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600379
380 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600381}
382
Gavin Howard14913fc2019-04-23 14:36:03 -0600383static size_t bc_num_shiftZero(BcNum *restrict n) {
384
385 size_t i;
386
387 assert(!n->rdx || BC_NUM_ZERO(n));
388
389 for (i = 0; i < n->len && !n->num[i]; ++i);
390
391 n->len -= i;
392 n->num += i;
393
394 return i;
395}
396
Gavin Howard2e736de2019-04-27 06:41:58 -0600397static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
398 n->len += places_rdx;
399 n->num -= places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600400}
401
Gavin Howard48a0e472019-04-26 15:39:46 -0600402static BcStatus bc_num_shift(BcNum *restrict n, BcDig *restrict ptr,
403 unsigned long dig, size_t len)
404{
Gavin Howardb1558662019-04-26 14:32:55 -0600405 size_t i;
406 unsigned long carry = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600407
Gavin Howardb1558662019-04-26 14:32:55 -0600408 assert(dig < BC_BASE_POWER);
Gavin Howard305249a2018-10-15 20:24:47 -0600409
Gavin Howardb1558662019-04-26 14:32:55 -0600410 dig = bc_num_pow10(dig);
411
412 for (i = 0; BC_NO_SIG && i < len; ++i) {
413 unsigned long in = ((unsigned long) ptr[i]) * dig;
414 carry = bc_num_addDigit(ptr + i, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600415 }
416
Gavin Howardb1558662019-04-26 14:32:55 -0600417 if (carry) {
418 bc_num_expand(n, n->len + 1);
419 assert(carry < BC_BASE_DIG);
420 n->num[n->len] = (BcDig) carry;
421 n->len += 1;
422 }
423
424 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600425}
426
Gavin Howardb1558662019-04-26 14:32:55 -0600427static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700428
Gavin Howardb1558662019-04-26 14:32:55 -0600429 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600430 unsigned long dig;
431 bool shift;
432 size_t places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600433
Gavin Howardb1558662019-04-26 14:32:55 -0600434 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600435
Gavin Howard48a0e472019-04-26 15:39:46 -0600436 dig = (unsigned long) (places % BC_BASE_POWER);
437 shift = (dig != 0);
438 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx - shift;
439
Gavin Howardb1558662019-04-26 14:32:55 -0600440 if (n->scale >= places) {
441 n->scale -= places;
442 n->rdx = BC_NUM_RDX(n->scale);
443 }
444 else if (BC_NUM_NONZERO(n)) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600445
Stefan Esser6189d952019-04-27 10:40:14 +0200446 if (places_rdx > n->rdx) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600447 bc_num_expand(n, n->len + places_rdx - n->rdx + shift);
Gavin Howard404ece72019-04-26 15:59:15 -0600448 memmove(n->num + places_rdx - n->rdx, n->num, BC_NUM_SIZE(n->len));
449 memset(n->num, 0, BC_NUM_SIZE(places_rdx - n->rdx));
Gavin Howard48a0e472019-04-26 15:39:46 -0600450 n->len += places_rdx;
451 }
452
Gavin Howardb1558662019-04-26 14:32:55 -0600453 n->scale = n->rdx = 0;
454 }
455 else n->scale = 0;
456
457 if (shift) s = bc_num_shift(n, n->num + places_rdx, dig, n->len - places_rdx);
458
459 bc_num_clean(n);
460
461 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
462}
463
464static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
465
466 BcStatus s = BC_STATUS_SUCCESS;
467 unsigned long dig = (unsigned long) (places % BC_BASE_POWER);
468 bool shift = (dig != 0);
469 size_t len, places_rdx;
470
471 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600472 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600473 n->scale += places;
474 bc_num_expand(n, BC_NUM_RDX(n->scale));
475 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600476 }
477
Gavin Howardb1558662019-04-26 14:32:55 -0600478 places_rdx = BC_NUM_RDX(places);
479 len = bc_vm_growSize(n->len, places_rdx);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700480
481 if (len > n->len) {
482
483 if (len > n->cap) bc_num_expand(n, len);
484
Gavin Howard404ece72019-04-26 15:59:15 -0600485 memset(n->num + n->len, 0, BC_NUM_SIZE(len - n->len));
Gavin Howard7ad5a662019-02-19 14:40:46 -0700486 n->len = len;
487 }
488
Gavin Howardb1558662019-04-26 14:32:55 -0600489 n->scale += places;
490 n->rdx += places_rdx;
491
492 if (shift) s = bc_num_shift(n, n->num, BC_BASE_POWER - dig, n->len);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700493
494 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600495 assert(n->rdx == BC_NUM_RDX(n->scale));
496
497 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700498}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700499
Gavin Howard1cbfe242019-01-09 17:13:11 -0700500static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600501
Gavin Howard63738202018-09-26 15:34:20 -0600502 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600503 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600504
Gavin Howard971a2672019-04-26 14:32:07 -0600505 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700506
Gavin Howardd32d7df2018-10-15 08:42:25 -0600507 one.cap = 2;
508 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600509 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600510
Gavin Howardd32d7df2018-10-15 08:42:25 -0600511 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600512}
513
Gavin Howard7bda4782018-12-28 09:53:22 -0700514#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700515static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
516 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700517{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700518 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700519 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700520 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700521}
Gavin Howard7bda4782018-12-28 09:53:22 -0700522#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700523
Gavin Howard1cbfe242019-01-09 17:13:11 -0700524static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700525
Gavin Howard8a921bd2018-10-11 14:15:32 -0600526 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600527 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard6fccd222019-04-26 10:17:02 -0600528 unsigned long carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700529
Gavin Howard63738202018-09-26 15:34:20 -0600530 // Because this function doesn't need to use scale (per the bc spec),
531 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600532
Gavin Howardddce6d42019-01-03 14:07:06 -0700533 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600534 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700535 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600536 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600537 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700538 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600539 bc_num_copy(c, a);
540 return BC_STATUS_SUCCESS;
541 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600542
Gavin Howard63738202018-09-26 15:34:20 -0600543 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600544 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600545 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600546 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700547
Gavin Howard63738202018-09-26 15:34:20 -0600548 if (a->rdx > b->rdx) {
549 diff = a->rdx - b->rdx;
550 ptr = a->num;
551 ptr_a = a->num + diff;
552 ptr_b = b->num;
553 }
554 else {
555 diff = b->rdx - a->rdx;
556 ptr = b->num;
557 ptr_a = a->num;
558 ptr_b = b->num + diff;
559 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700560
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700561 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700562
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700563 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600564 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700565 a_int = bc_num_int(a);
566 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700567
Gavin Howard890d0c02018-10-30 16:34:50 -0600568 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600569 min_int = b_int;
570 max = a_int;
571 ptr = ptr_a;
572 }
573 else {
574 min_int = a_int;
575 max = b_int;
576 ptr = ptr_b;
577 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700578
Gavin Howard2d188a52019-02-25 14:19:08 -0700579 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard6fccd222019-04-26 10:17:02 -0600580 unsigned long in;
581 in = ((unsigned long) ptr_a[i]) + ((unsigned long) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700582 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600583 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700584
Gavin Howard2d188a52019-02-25 14:19:08 -0700585 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard6fccd222019-04-26 10:17:02 -0600586 carry = bc_num_addDigit(ptr_c + i, (unsigned long) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700587
588 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700589
Gavin Howard1ab22d22019-01-03 13:32:17 -0700590 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600591
Gavin Howard2d188a52019-02-25 14:19:08 -0700592 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700593}
594
Gavin Howard1cbfe242019-01-09 17:13:11 -0700595static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700596
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700597 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600598 ssize_t cmp;
599 BcNum *minuend, *subtrahend;
600 size_t start;
601 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700602
Gavin Howard63738202018-09-26 15:34:20 -0600603 // Because this function doesn't need to use scale (per the bc spec),
604 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700605
Gavin Howardddce6d42019-01-03 14:07:06 -0700606 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600607 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700608 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600609 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600610 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700611 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600612 bc_num_copy(c, a);
613 return BC_STATUS_SUCCESS;
614 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700615
Gavin Howard63738202018-09-26 15:34:20 -0600616 aneg = a->neg;
617 bneg = b->neg;
618 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700619
Gavin Howard63738202018-09-26 15:34:20 -0600620 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600621 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700622
Gavin Howard63738202018-09-26 15:34:20 -0600623 a->neg = aneg;
624 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700625
Gavin Howard1ab22d22019-01-03 13:32:17 -0700626 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600627 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600628 return BC_STATUS_SUCCESS;
629 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700630
631 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600632 neg = a->neg;
633 minuend = a;
634 subtrahend = b;
635 }
636 else {
637 neg = b->neg;
638 if (sub) neg = !neg;
639 minuend = b;
640 subtrahend = a;
641 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700642
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600643 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600644 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700645
Gavin Howarde6f326e2019-04-26 10:13:45 -0600646 if (c->scale < subtrahend->scale) {
647 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600648 start = 0;
649 }
650 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700651
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700652 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700653
Gavin Howard63738202018-09-26 15:34:20 -0600654 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700655
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700656 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700657}
658
Gavin Howarddc4e3712019-04-23 14:56:29 -0600659static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
660{
Gavin Howardfcf29012019-04-25 20:12:19 -0600661 // TODO: Check this function.
Gavin Howard971a2672019-04-26 14:32:07 -0600662 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600663 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard971a2672019-04-26 14:32:07 -0600664 unsigned long sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600665
Gavin Howard971a2672019-04-26 14:32:07 -0600666 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600667 assert(!a->rdx && !b->rdx);
668
Gavin Howard5083aec2019-04-23 18:03:46 -0600669 clen = bc_vm_growSize(alen, blen);
670 bc_num_expand(c, clen + 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600671
672 ptr_c = c->num;
Gavin Howard404ece72019-04-26 15:59:15 -0600673 memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600674
Gavin Howard5083aec2019-04-23 18:03:46 -0600675 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600676
Gavin Howard5083aec2019-04-23 18:03:46 -0600677 ssize_t sidx = (ssize_t) (i - blen + 1);
678 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600679
Stefan Essera299ffc2019-04-24 23:28:32 +0200680 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
Stefan Eßer4b111c22019-04-24 17:23:08 -0600681
Gavin Howard971a2672019-04-26 14:32:07 -0600682 sum += ((unsigned long) ptr_a[j]) * ((unsigned long) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600683
Gavin Howard0700efc2019-04-24 17:31:23 -0600684 if (sum >= BC_BASE_DIG) {
685 carry += sum / BC_BASE_DIG;
686 sum %= BC_BASE_DIG;
Stefan Essera299ffc2019-04-24 23:28:32 +0200687 }
688 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600689
Stefan Essera299ffc2019-04-24 23:28:32 +0200690 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600691 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Essera299ffc2019-04-24 23:28:32 +0200692 sum = carry;
693 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600694 }
695
Gavin Howard5083aec2019-04-23 18:03:46 -0600696 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600697 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600698 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600699 clen += 1;
700 }
701
702 c->len = clen;
703
Gavin Howarddc4e3712019-04-23 14:56:29 -0600704 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
705}
706
Gavin Howard14c354a2019-04-24 14:28:24 -0600707static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
708 size_t shift, BcNumShiftAddOp op)
709{
Gavin Howardfcf29012019-04-25 20:12:19 -0600710 // TODO: Check this function.
Gavin Howard14c354a2019-04-24 14:28:24 -0600711 assert(n->len >= shift + a->len);
712 assert(!n->rdx && !a->rdx);
713 return op(n->num + shift, a->num, a->len);
714}
715
716static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600717
Gavin Howardfcf29012019-04-25 20:12:19 -0600718 // TODO: Check this function.
719
Gavin Howard9b801632019-02-16 23:34:06 -0700720 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200721 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600722 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700723 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600724 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700725 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600726
Gavin Howard14913fc2019-04-23 14:36:03 -0600727 assert(BC_NUM_ZERO(c));
728
Gavin Howardddce6d42019-01-03 14:07:06 -0700729 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600730 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700731 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600732 bc_num_zero(c);
733 return BC_STATUS_SUCCESS;
734 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700735 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600736 bc_num_copy(c, aone ? b : a);
737 return BC_STATUS_SUCCESS;
738 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700739
Gavin Howardb5093d72018-10-17 12:13:17 -0600740 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
741 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600742 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600743 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600744 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700745
Gavin Howarde0432502019-02-26 16:23:45 -0700746 max = BC_MAX(a->len, b->len);
Stefan Essera299ffc2019-04-24 23:28:32 +0200747 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700748 max2 = (max + 1) / 2;
749
Stefan Esser5fd43a32019-04-25 07:05:24 +0200750 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
Gavin Howard404ece72019-04-26 15:59:15 -0600751 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total));
Gavin Howard5bede412019-02-23 09:31:28 -0700752
753 bc_num_setup(&l1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200754 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700755 bc_num_setup(&h1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200756 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700757 bc_num_setup(&l2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200758 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700759 bc_num_setup(&h2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200760 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700761 bc_num_setup(&m1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200762 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700763 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600764 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600765 bc_num_init(&z0, max);
766 bc_num_init(&z1, max);
767 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600768 max = bc_vm_growSize(max, max) + 1;
769 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600770
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600771 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600772 bc_num_clean(&l1);
773 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600774 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600775 bc_num_clean(&l2);
776 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700777
Gavin Howard14c354a2019-04-24 14:28:24 -0600778 bc_num_expand(c, max);
779 c->len = max;
Gavin Howard404ece72019-04-26 15:59:15 -0600780 memset(c->num, 0, BC_NUM_SIZE(c->len));
Gavin Howard305249a2018-10-15 20:24:47 -0600781
Gavin Howard14c354a2019-04-24 14:28:24 -0600782 s = bc_num_sub(&h1, &l1, &m1, 0);
783 if (BC_ERR(s)) goto err;
784 s = bc_num_sub(&l2, &h2, &m2, 0);
785 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600786
Gavin Howard971a2672019-04-26 14:32:07 -0600787 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600788
Gavin Howard14c354a2019-04-24 14:28:24 -0600789 s = bc_num_m(&h1, &h2, &z2, 0);
790 if (BC_ERR(s)) goto err;
791 bc_num_clean(&z2);
792
793 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
794 if (BC_ERR(s)) goto err;
795 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
796 if (BC_ERR(s)) goto err;
797 }
798
Gavin Howard971a2672019-04-26 14:32:07 -0600799 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600800
801 s = bc_num_m(&l1, &l2, &z0, 0);
802 if (BC_ERR(s)) goto err;
803 bc_num_clean(&z0);
804
805 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
806 if (BC_ERR(s)) goto err;
807 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
808 if (BC_ERR(s)) goto err;
809 }
810
Gavin Howard971a2672019-04-26 14:32:07 -0600811 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600812
813 s = bc_num_m(&m1, &m2, &z1, 0);
814 if (BC_ERR(s)) goto err;
815 bc_num_clean(&z1);
816
817 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
818 s = bc_num_shiftAddSub(c, &z1, max2, op);
819 if (BC_ERR(s)) goto err;
820 }
Gavin Howard305249a2018-10-15 20:24:47 -0600821
822err:
Gavin Howard5bede412019-02-23 09:31:28 -0700823 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600824 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600825 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600826 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600827 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600828 return s;
829}
830
Gavin Howard1cbfe242019-01-09 17:13:11 -0700831static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600832
Gavin Howardfcf29012019-04-25 20:12:19 -0600833 // TODO: Check this function.
834
Gavin Howard305249a2018-10-15 20:24:47 -0600835 BcStatus s;
836 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600837 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600838
Gavin Howard14913fc2019-04-23 14:36:03 -0600839 bc_num_setToZero(c, 0);
840
Gavin Howardcf9f2462019-04-27 06:43:57 -0600841 ascale = a->scale;
842 bscale = b->scale;
843 scale = BC_MAX(scale, ascale);
844 scale = BC_MAX(scale, b->scale);
Gavin Howard48a0e472019-04-26 15:39:46 -0600845 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600846 scale = BC_MIN(rscale, scale);
Gavin Howard305249a2018-10-15 20:24:47 -0600847
Gavin Howardbaa4f582019-01-24 13:56:35 -0700848 bc_num_createCopy(&cpa, a);
849 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600850
Gavin Howard23d8d252018-10-17 12:03:41 -0600851 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600852
Gavin Howard48a0e472019-04-26 15:39:46 -0600853 ardx = cpa.rdx * BC_BASE_POWER;
854 s = bc_num_shiftLeft(&cpa, ardx);
855 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600856 bc_num_clean(&cpa);
857 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600858
859 brdx = cpb.rdx * BC_BASE_POWER;
860 s = bc_num_shiftLeft(&cpb, brdx);
861 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600862 bzero = bc_num_shiftZero(&cpb);
863 bc_num_clean(&cpb);
864
Gavin Howard14c354a2019-04-24 14:28:24 -0600865 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700866 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600867
Gavin Howard14913fc2019-04-23 14:36:03 -0600868 zero = bc_vm_growSize(azero, bzero);
869 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600870
Gavin Howard14913fc2019-04-23 14:36:03 -0600871 bc_num_expand(c, len);
Gavin Howard48a0e472019-04-26 15:39:46 -0600872 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_POWER);
873 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
874 s = bc_num_shiftRight(c, ardx + brdx);
875 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600876
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600877 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600878
Gavin Howard305249a2018-10-15 20:24:47 -0600879err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600880 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600881 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600882 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600883 bc_num_free(&cpa);
884 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700885}
886
Stefan Esser193f8522019-04-27 01:29:01 +0200887static BcStatus bc_num_invert(BcNum *val, size_t bits, size_t scale) { // --> num.h <se>
888 int i;
889 BcNum xi, two, temp;
890 BcStatus s = BC_STATUS_SUCCESS;
891 size_t len, rdx, bitlimit;
892
893 bitlimit = ((val->rdx + 1) * 50 * BC_BASE_POWER) / 10;
894
895 bc_num_createCopy(&xi, val);
896 bc_num_init(&temp, xi.len + 1);
897 bc_num_init(&two, 1);
898 bc_num_ulong2num(&two, 2);
899
900 while (bits <= bitlimit) {
901 s = bc_num_mul(val, &xi, &temp, scale + 1);
902 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
903 s = bc_num_sub(&two, &temp, &temp, scale + 1);
904 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
905 s = bc_num_mul(&temp, &xi, &xi, scale + 1);
906 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
907 bits *= 2;
908 }
909 bc_num_copy(val, &xi);
910err:
911 bc_num_free(&xi);
912 bc_num_free(&temp);
913 bc_num_free(&two);
914
915 return s;
916}
917
918static int intlog2(size_t n) {
919 int bits = 0;
920
921 n >>= 1;
922 while (n != 0) {
923 bits++;
924 n >>= 1;
925 }
926 return (bits);
927}
928
Gavin Howard1cbfe242019-01-09 17:13:11 -0700929static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700930
Gavin Howardfcf29012019-04-25 20:12:19 -0600931 // TODO: Check this function.
932
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600933 BcStatus s = BC_STATUS_SUCCESS;
Stefan Esser193f8522019-04-27 01:29:01 +0200934 size_t len, rdx;
935 BcNum b1, f;
936 size_t factor, dividend, divisor;
937 size_t i, j, digits, maxdigits, maxdivisor;
938 int validbits;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700939
Gavin Howardddce6d42019-01-03 14:07:06 -0700940 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
941 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600942 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600943 return BC_STATUS_SUCCESS;
944 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700945 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600946 bc_num_copy(c, a);
947 bc_num_retireMul(c, scale, a->neg, b->neg);
948 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600949 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700950
Stefan Esser193f8522019-04-27 01:29:01 +0200951 bc_num_init(&b1, b->len +1);
952 bc_num_copy(&b1, b);
953 len = b1.len;
954 rdx = b1.rdx;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700955
Stefan Esser193f8522019-04-27 01:29:01 +0200956 maxdigits = (sizeof(dividend) * 24) / 10;
957 maxdigits = ((maxdigits / BC_BASE_POWER) * BC_BASE_POWER);
958 maxdivisor = (1 << (((maxdigits - BC_BASE_POWER - 7) * 8) / 3)) + 1;
Gavin Howard021150b2018-03-10 15:40:42 -0700959
Stefan Esser193f8522019-04-27 01:29:01 +0200960 dividend = 1;
961 divisor = 0;
962 digits = 0;
963 j = 0;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700964
Gavin Howardedd9d9d2019-04-27 07:28:45 -0600965 for (i = 1; i < b1.len && b1.num[b1.len - i] == 0; ++i);
966
967 for (; i < b1.len && digits < maxdigits; ++i) {
Stefan Esser193f8522019-04-27 01:29:01 +0200968 dividend = dividend * BC_BASE_DIG;
969 digits += BC_BASE_POWER;
970 rdx++;
971 if (i <= b1.len) {
972 if (divisor <= maxdivisor) {
973 rdx--;
974 divisor = divisor * BC_BASE_DIG + b1.num[b1.len - i];
975 } else {
976 j = 1;
977 }
Gavin Howard5a788392019-03-28 11:01:13 -0600978 }
Gavin Howard63738202018-09-26 15:34:20 -0600979 }
Stefan Esser193f8522019-04-27 01:29:01 +0200980 divisor += j;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700981
Stefan Esser193f8522019-04-27 01:29:01 +0200982 factor = dividend / divisor;
983 validbits = factor < divisor ? intlog2(factor) : intlog2(divisor);
984
985 bc_num_init(&f, maxdigits);
986 bc_num_ulong2num(&f, factor);
987 bc_num_mul(&b1, &f, &b1, scale);
988 i = b1.len - b1.rdx;
989 b1.rdx = b1.len;
990
991 if (b1.num[b1.len - 1] == 1) {
992 b1.rdx -= 2;
993 } else {
994 bc_num_invert(&b1, validbits, a->len + b1.len + 1);
995 }
996 bc_num_init(c, bc_num_mulReq(a, &b1, scale));
997 bc_num_mul(&f, &b1, c, scale);
998 bc_num_mul(c, a, c, scale);
999 bc_num_shiftRight(c, i);
1000// bc_num_truncDecimals(c, scale);
Gavin Howard034b5b92019-04-27 07:28:56 -06001001
Stefan Esser193f8522019-04-27 01:29:01 +02001002err:
Gavin Howardf2ee6342019-04-09 17:05:20 -06001003 if (BC_SIG) s = BC_STATUS_SIGNAL;
Stefan Esser193f8522019-04-27 01:29:01 +02001004 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b1.neg);
Gavin Howard63738202018-09-26 15:34:20 -06001005 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001006}
1007
Gavin Howard1cbfe242019-01-09 17:13:11 -07001008static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
1009 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -06001010{
Gavin Howardfcf29012019-04-25 20:12:19 -06001011 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001012 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -06001013 BcNum temp;
1014 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001015
Gavin Howardddce6d42019-01-03 14:07:06 -07001016 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1017 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -07001018 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001019 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001020 return BC_STATUS_SUCCESS;
1021 }
Gavin Howard8b254872018-03-14 01:13:35 -06001022
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001023 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -07001024 s = bc_num_d(a, b, c, scale);
1025 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001026 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001027
Gavin Howard1ab22d22019-01-03 13:32:17 -07001028 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -06001029
Gavin Howard53eba8b2018-10-31 15:14:37 -06001030 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001031 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001032 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001033 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -06001034
Gavin Howard77445432019-04-26 15:59:33 -06001035 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
Gavin Howard18441082018-10-22 10:03:30 -06001036
1037 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001038 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -06001039 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001040
1041err:
Gavin Howard18441082018-10-22 10:03:30 -06001042 bc_num_free(&temp);
1043 return s;
1044}
1045
Gavin Howard2d188a52019-02-25 14:19:08 -07001046static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1047{
Gavin Howardfcf29012019-04-25 20:12:19 -06001048 // TODO: Check this function.
Gavin Howard18441082018-10-22 10:03:30 -06001049 BcStatus s;
1050 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -07001051 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -06001052
Gavin Howard77445432019-04-26 15:59:33 -06001053 ts = bc_vm_growSize(scale, b->scale);
1054 ts = BC_MAX(ts, a->scale);
Gavin Howard798508b2019-02-21 16:45:32 -07001055
1056 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -06001057 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001058 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -06001059
Gavin Howard63738202018-09-26 15:34:20 -06001060 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001061}
1062
Gavin Howard1cbfe242019-01-09 17:13:11 -07001063static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001064
Gavin Howardfcf29012019-04-25 20:12:19 -06001065 // TODO: Check this function.
1066
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001067 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001068 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -07001069 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001070 size_t i, powrdx, resrdx;
1071 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001072
Gavin Howardecafd4f2019-02-23 09:30:45 -07001073 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001074
Gavin Howardddce6d42019-01-03 14:07:06 -07001075 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -06001076 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -06001077 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001078 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001079 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001080 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001081 return BC_STATUS_SUCCESS;
1082 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001083 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001084 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -06001085 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001086 return s;
1087 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001088
Gavin Howard63738202018-09-26 15:34:20 -06001089 neg = b->neg;
1090 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001091 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -07001092 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001093 if (s) return s;
1094
Gavin Howardbaa4f582019-01-24 13:56:35 -07001095 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001096
Gavin Howard63738202018-09-26 15:34:20 -06001097 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -06001098
Gavin Howard2d188a52019-02-25 14:19:08 -07001099 for (powrdx = a->rdx; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -06001100 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001101 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001102 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001103 }
Gavin Howard6fb635f2018-03-03 12:45:42 -07001104
Gavin Howard2d188a52019-02-25 14:19:08 -07001105 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -07001106
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001107 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -07001108 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001109
Gavin Howard2d188a52019-02-25 14:19:08 -07001110 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001111
Gavin Howard4d5daba2019-01-07 14:42:50 -07001112 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001113 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001114 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001115
Gavin Howard63738202018-09-26 15:34:20 -06001116 if (pow & 1) {
1117 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001118 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001119 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001120 }
1121 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001122
Gavin Howard2d188a52019-02-25 14:19:08 -07001123 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -07001124 if (neg) {
1125 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001126 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -07001127 }
1128
Gavin Howard63738202018-09-26 15:34:20 -06001129 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001130
Gavin Howard53eba8b2018-10-31 15:14:37 -06001131 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -06001132 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001133 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001134
Gavin Howard9b801632019-02-16 23:34:06 -07001135sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001136 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001137err:
Gavin Howard63738202018-09-26 15:34:20 -06001138 bc_num_free(&copy);
1139 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001140}
1141
Gavin Howard7bda4782018-12-28 09:53:22 -07001142#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -07001143static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
1144 size_t scale)
1145{
Gavin Howardac4d9122018-12-27 23:59:12 -07001146 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001147 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001148
1149 BC_UNUSED(scale);
1150
1151 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001152 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001153
Gavin Howardc9bef2d2019-04-26 14:46:52 -06001154 if (val < c->scale) bc_num_truncate(c, c->scale - val);
1155 else if (val > c->scale) bc_num_extend(c, val - c->scale);
Gavin Howardac4d9122018-12-27 23:59:12 -07001156
1157 return s;
1158}
1159
Gavin Howard2d188a52019-02-25 14:19:08 -07001160static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1161{
Gavin Howardac4d9122018-12-27 23:59:12 -07001162 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001163 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001164
1165 BC_UNUSED(scale);
1166
1167 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001168 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001169
Gavin Howardb1558662019-04-26 14:32:55 -06001170 return bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001171}
1172
Gavin Howard2d188a52019-02-25 14:19:08 -07001173static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1174 size_t scale)
1175{
Gavin Howardac4d9122018-12-27 23:59:12 -07001176 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001177 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001178
1179 BC_UNUSED(scale);
1180
1181 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001182 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001183
Gavin Howard2a366922019-01-16 10:50:20 -07001184 if (BC_NUM_ZERO(c)) return s;
1185
Gavin Howardb1558662019-04-26 14:32:55 -06001186 return bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001187}
Gavin Howard7bda4782018-12-28 09:53:22 -07001188#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001189
Gavin Howard1cbfe242019-01-09 17:13:11 -07001190static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1191 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001192{
Gavin Howarda1c44392018-09-27 12:41:15 -06001193 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001194 BcNum num2, *ptr_a, *ptr_b;
1195 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001196
Gavin Howard63738202018-09-26 15:34:20 -06001197 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001198
Gavin Howard890d0c02018-10-30 16:34:50 -06001199 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001200 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001201 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001202 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001203 }
1204 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001205
Gavin Howard63738202018-09-26 15:34:20 -06001206 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001207 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001208 if (c != a) {
1209 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001210 init = true;
1211 }
1212 }
1213 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001214
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001215 if (init) bc_num_init(c, req);
1216 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001217
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001218 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001219
Gavin Howardddce6d42019-01-03 14:07:06 -07001220 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001221 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001222
Gavin Howardba009802018-09-29 04:41:51 -06001223 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001224
Stefan Esser193f8522019-04-27 01:29:01 +02001225// bc_num_truncDecimals(c, scale);
1226
Gavin Howardee9d01e2019-02-16 22:48:11 -07001227 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001228}
1229
Gavin Howardc90ed902019-01-02 13:07:49 -07001230#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001231static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001232
Gavin Howardc7d655b2018-12-28 19:45:13 -07001233 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001234 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001235
Gavin Howard63738202018-09-26 15:34:20 -06001236 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001237
Gavin Howard63738202018-09-26 15:34:20 -06001238 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001239
Gavin Howard8a921bd2018-10-11 14:15:32 -06001240 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001241
Gavin Howard63738202018-09-26 15:34:20 -06001242 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001243
Gavin Howard63738202018-09-26 15:34:20 -06001244 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001245
Gavin Howard63738202018-09-26 15:34:20 -06001246 radix = true;
1247 continue;
1248 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001249
Gavin Howardc7d655b2018-12-28 19:45:13 -07001250 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001251 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001252
Gavin Howard63738202018-09-26 15:34:20 -06001253 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001254}
Gavin Howardc90ed902019-01-02 13:07:49 -07001255#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001256
Gavin Howard1cbfe242019-01-09 17:13:11 -07001257static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001258
Gavin Howardfcf29012019-04-25 20:12:19 -06001259 // TODO: Check this function.
1260
Gavin Howardc7d655b2018-12-28 19:45:13 -07001261 if (isupper(c)) {
1262 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001263 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001264 }
1265 else c -= '0';
1266
1267 return (unsigned long) (uchar) c;
1268}
1269
Gavin Howard1cbfe242019-01-09 17:13:11 -07001270static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001271
Gavin Howard3b60abb2019-04-26 08:49:17 -06001272 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001273 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001274 bool zero = true, rdx;
Stefan Essera299ffc2019-04-24 23:28:32 +02001275
Gavin Howard63738202018-09-26 15:34:20 -06001276 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001277
Gavin Howard63738202018-09-26 15:34:20 -06001278 val += i;
Gavin Howard3b60abb2019-04-26 08:49:17 -06001279 assert(isalnum(val[0]) || val[0] == '.');
Gavin Howard63738202018-09-26 15:34:20 -06001280 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001281
Gavin Howard63738202018-09-26 15:34:20 -06001282 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001283 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001284
Gavin Howardd43ec372019-04-25 21:13:54 -06001285 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001286
Gavin Howardd43ec372019-04-25 21:13:54 -06001287 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001288 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001289
Gavin Howard3b60abb2019-04-26 08:49:17 -06001290 i = len - (ptr == val ? 0 : i) - rdx;
Gavin Howard48a0e472019-04-26 15:39:46 -06001291 temp = BC_NUM_ROUND_POW(i);
Gavin Howard3b60abb2019-04-26 08:49:17 -06001292 mod = n->scale % BC_BASE_POWER;
1293 i = mod ? BC_BASE_POWER - mod : 0;
1294 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001295
1296 bc_num_expand(n, n->len);
Gavin Howard404ece72019-04-26 15:59:15 -06001297 memset(n->num, 0, BC_NUM_SIZE(n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -07001298
Gavin Howard6076dbb2019-04-26 09:47:50 -06001299 if (zero) n->len = n->rdx = 0;
1300 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001301
Gavin Howard971a2672019-04-26 14:32:07 -06001302 unsigned long exp, pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001303
Gavin Howard3b60abb2019-04-26 08:49:17 -06001304 exp = i;
Gavin Howard971a2672019-04-26 14:32:07 -06001305 pow = bc_num_pow10(exp);
Gavin Howardd43ec372019-04-25 21:13:54 -06001306
1307 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001308
Gavin Howardc7d655b2018-12-28 19:45:13 -07001309 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001310
Gavin Howardd43ec372019-04-25 21:13:54 -06001311 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001312 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001313
1314 size_t idx = exp / BC_BASE_POWER;
1315
Gavin Howard8dd307e2019-01-08 23:05:19 -07001316 if (isupper(c)) c = '9';
Gavin Howardf196fbe2019-04-26 08:49:05 -06001317 n->num[idx] += (((unsigned long) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001318
1319 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1320 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001321 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001322 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001323 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001324}
1325
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001326static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1327 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001328{
Gavin Howardfcf29012019-04-25 20:12:19 -06001329 // TODO: Check this function.
Gavin Howard1cbfe242019-01-09 17:13:11 -07001330 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001331 BcNum temp, mult, result;
Gavin Howard77445432019-04-26 15:59:33 -06001332 char c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001333 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001334 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001335 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001336
Gavin Howard11b9afd2018-10-18 14:23:28 -06001337 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001338 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001339
Gavin Howard26d078f2019-01-24 13:58:12 -07001340 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1341 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001342
Gavin Howardc7d655b2018-12-28 19:45:13 -07001343 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001344
Gavin Howardc7d655b2018-12-28 19:45:13 -07001345 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001346
Gavin Howard53eba8b2018-10-31 15:14:37 -06001347 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001348 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
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(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001351 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001352 }
Gavin Howardede51f02018-03-02 12:30:00 -07001353
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001354 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001355
Gavin Howard63738202018-09-26 15:34:20 -06001356 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001357 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001358 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001359
Gavin Howard2d188a52019-02-25 14:19:08 -07001360 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001361 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001362 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001363
Gavin Howard53eba8b2018-10-31 15:14:37 -06001364 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001365 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001366
Gavin Howard705c4bc2018-12-03 15:38:04 -07001367 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001368 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001369 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001370 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001371 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001372 }
Gavin Howardede51f02018-03-02 12:30:00 -07001373
Gavin Howardf2ee6342019-04-09 17:05:20 -06001374 if (BC_SIG) {
1375 s = BC_STATUS_SIGNAL;
1376 goto err;
1377 }
1378
Gavin Howard1769abc2019-02-21 09:36:14 -07001379 // This one cannot be a divide by 0 because mult starts out at 1, then is
1380 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1381 s = bc_num_div(&result, &mult, &result, digs);
1382 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001383 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001384 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001385 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001386
Gavin Howardddce6d42019-01-03 14:07:06 -07001387 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001388 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001389 }
1390 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001391
Gavin Howardd1417682019-02-15 17:08:42 -07001392err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001393 bc_num_free(&result);
1394int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001395 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001396 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001397 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001398}
1399
Gavin Howard1cbfe242019-01-09 17:13:11 -07001400static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001401 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001402 bc_vm_putchar('\\');
1403 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001404 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001405 }
Gavin Howard80977b22018-09-06 14:29:13 -06001406}
1407
Gavin Howard40a085f2018-12-03 12:08:59 -07001408#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001409static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001410 BC_UNUSED(rdx);
1411 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001412 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001413}
1414#endif // DC_ENABLED
1415
Gavin Howard1cbfe242019-01-09 17:13:11 -07001416static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1417
Gavin Howardfcf29012019-04-25 20:12:19 -06001418 // TODO: Check this function.
1419
Gavin Howarda84ad992018-12-03 19:11:06 -07001420 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001421
Gavin Howardab5e9632019-01-02 13:32:02 -07001422 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001423 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001424 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001425
Gavin Howardab5e9632019-01-02 13:32:02 -07001426 bc_num_printNewline();
Gavin Howard530e3072019-04-23 16:23:36 -06001427 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001428
Gavin Howard530e3072019-04-23 16:23:36 -06001429 for (exp = 0; exp < len; pow /= BC_BASE, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001430 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001431 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001432 dig = n / pow;
1433 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001434 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001435 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001436}
Gavin Howardfe679f02018-02-14 15:50:09 -07001437
Gavin Howard1cbfe242019-01-09 17:13:11 -07001438static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001439
Gavin Howard3c02ed32018-12-28 18:17:15 -07001440 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001441
Gavin Howard3c02ed32018-12-28 18:17:15 -07001442 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001443 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001444 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001445 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001446 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001447
Gavin Howardab5e9632019-01-02 13:32:02 -07001448 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001449 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001450 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001451}
1452
Gavin Howard1cbfe242019-01-09 17:13:11 -07001453static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001454
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001455 size_t i, j, rdx = n->rdx;
1456 bool zero = true;
1457 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001458
Gavin Howard48af52e2018-10-30 14:47:38 -06001459 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001460 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001461
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001462 for (i = n->len - 1; i < n->len; --i) {
1463
1464 BcDig n9 = n->num[i];
1465 size_t temp;
1466 bool irdx = (i == rdx - 1);
1467
1468 zero = (zero & !irdx);
1469 temp = n->scale % BC_BASE_POWER;
1470 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1471
1472 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1473
1474 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Essera299ffc2019-04-24 23:28:32 +02001475 buffer[j] = n9 % BC_BASE;
1476 n9 /= BC_BASE;
1477 }
Stefan Esser0da17752019-04-25 12:25:15 +02001478
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001479 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1480 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1481 zero = (zero && buffer[j] == 0);
1482 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Essera299ffc2019-04-24 23:28:32 +02001483 }
1484 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001485}
1486
Gavin Howard7ad5a662019-02-19 14:40:46 -07001487#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001488static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) {
Gavin Howard7ad5a662019-02-19 14:40:46 -07001489
Gavin Howardfcf29012019-04-25 20:12:19 -06001490 // TODO: Check this function.
1491
Gavin Howardb1558662019-04-26 14:32:55 -06001492 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001493 bool neg = (n->len <= n->rdx);
1494 BcNum temp, exp;
1495 size_t places, mod;
1496 BcDig digs[BC_NUM_LONG_LOG10];
1497
1498 bc_num_createCopy(&temp, n);
1499
1500 if (neg) {
1501 places = n->rdx - bc_num_len(n) + 1;
1502 mod = places % 3;
1503 if (eng && mod != 0) places += 3 - mod;
Gavin Howardb1558662019-04-26 14:32:55 -06001504 s = bc_num_shiftLeft(&temp, places);
1505 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001506 }
1507 else {
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001508 places = bc_num_int(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001509 mod = places % 3;
1510 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howardb1558662019-04-26 14:32:55 -06001511 s = bc_num_shiftRight(&temp, places);
1512 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001513 }
1514
1515 bc_num_printDecimal(&temp);
1516
1517 bc_num_printNewline();
1518 bc_vm_putchar('e');
1519
1520 if (!places) {
1521 bc_num_printHex(0, 1, false);
1522 goto exit;
1523 }
1524
1525 if (neg) {
1526 bc_num_printNewline();
1527 bc_vm_putchar('-');
1528 }
1529
1530 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1531 bc_num_ulong2num(&exp, (unsigned long) places);
1532
1533 bc_num_printDecimal(&exp);
1534
1535exit:
1536 bc_num_free(&temp);
Gavin Howardb1558662019-04-26 14:32:55 -06001537 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001538}
1539#endif // BC_ENABLE_EXTRA_MATH
1540
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001541static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1542 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001543{
Gavin Howardfcf29012019-04-25 20:12:19 -06001544 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001545 BcStatus s;
1546 BcVec stack;
1547 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001548 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001549 size_t i;
1550 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001551
Gavin Howard971a2672019-04-26 14:32:07 -06001552 assert(BC_NUM_NONZERO(base));
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001553
Gavin Howardddce6d42019-01-03 14:07:06 -07001554 if (BC_NUM_ZERO(n)) {
1555 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001556 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001557 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001558
Gavin Howardd3929832018-12-24 15:13:15 -07001559 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001560 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001561 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001562 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001563 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001564 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001565
Gavin Howard63738202018-09-26 15:34:20 -06001566 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001567 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001568 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001569
Gavin Howard2d188a52019-02-25 14:19:08 -07001570 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001571
1572 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001573 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001574 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001575
1576 // Will never fail (except for signals) because digit is
1577 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001578 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001579 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001580
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001581 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001582 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001583
Gavin Howard2d188a52019-02-25 14:19:08 -07001584 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001585
Gavin Howard2d188a52019-02-25 14:19:08 -07001586 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001587 ptr = bc_vec_item_rev(&stack, i);
1588 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001589 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001590 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001591
Gavin Howard2d188a52019-02-25 14:19:08 -07001592 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001593 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001594
Gavin Howard2d188a52019-02-25 14:19:08 -07001595 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001596
Gavin Howard53eba8b2018-10-31 15:14:37 -06001597 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001598 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001599
1600 // Will never fail (except for signals) because fracp is
1601 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001602 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001603 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001604
Gavin Howard705c4bc2018-12-03 15:38:04 -07001605 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001606 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001607 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001608
Gavin Howardddce6d42019-01-03 14:07:06 -07001609 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001610 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001611 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001612 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001613
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001614sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001615 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001616err:
Gavin Howard63738202018-09-26 15:34:20 -06001617 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001618 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001619 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001620 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001621 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001622 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001623}
1624
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001625static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1626 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001627{
Gavin Howardfcf29012019-04-25 20:12:19 -06001628 // TODO: Check this function.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001629 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001630 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001631 BcNumDigitOp print;
1632 bool neg = n->neg;
1633
Gavin Howard48af52e2018-10-30 14:47:38 -06001634 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001635 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001636
1637 n->neg = false;
1638
Gavin Howardbe4e8222019-01-03 13:34:16 -07001639 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001640 width = 1;
1641 print = bc_num_printHex;
1642 }
1643 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001644 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001645 print = bc_num_printDigits;
1646 }
1647
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001648 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001649 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001650
1651 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001652}
1653
Gavin Howard40a085f2018-12-03 12:08:59 -07001654#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001655BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1656 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001657}
1658#endif // DC_ENABLED
1659
Gavin Howard3c02ed32018-12-28 18:17:15 -07001660void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001661 assert(n);
1662 n->num = num;
1663 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001664 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001665 n->neg = false;
1666}
1667
Gavin Howard3c02ed32018-12-28 18:17:15 -07001668void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001669 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001670 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howard404ece72019-04-26 15:59:15 -06001671 bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001672}
1673
Gavin Howarded392aa2018-02-27 13:09:26 -07001674void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001675 assert(num);
1676 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001677}
1678
Gavin Howard3c02ed32018-12-28 18:17:15 -07001679void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001680 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001681 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001682 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001683 d->len = s->len;
1684 d->neg = s->neg;
1685 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001686 d->scale = s->scale;
Gavin Howard404ece72019-04-26 15:59:15 -06001687 memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -07001688}
1689
Gavin Howardbaa4f582019-01-24 13:56:35 -07001690void bc_num_createCopy(BcNum *d, const BcNum *s) {
1691 bc_num_init(d, s->len);
1692 bc_num_copy(d, s);
1693}
1694
1695void bc_num_createFromUlong(BcNum *n, unsigned long val) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001696 bc_num_init(n, (BC_NUM_LONG_LOG10 - 1) / BC_BASE_POWER + 1);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001697 bc_num_ulong2num(n, val);
1698}
1699
Gavin Howard7ad5a662019-02-19 14:40:46 -07001700size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001701 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001702}
1703
1704size_t bc_num_len(const BcNum *restrict n) {
1705
Gavin Howardfcf29012019-04-25 20:12:19 -06001706 // TODO: Check this function.
Gavin Howard7ad5a662019-02-19 14:40:46 -07001707
Gavin Howardfcf29012019-04-25 20:12:19 -06001708 size_t i, len = n->len;
1709 BcDig dig;
1710
1711 if (n->rdx != len) return len;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001712 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1713
Gavin Howardfcf29012019-04-25 20:12:19 -06001714 dig = n->num[len - 1];
1715
1716 for (i = BC_BASE_DIG; i && (dig % (BcDig) i == dig); i /= BC_BASE);
1717
1718 return (len - 1) * BC_BASE_POWER + i;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001719}
1720
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001721BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1722 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001723{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001724 BcStatus s = BC_STATUS_SUCCESS;
1725
Gavin Howard63738202018-09-26 15:34:20 -06001726 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001727 assert(BC_ENABLE_EXTRA_MATH ||
1728 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001729 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001730
Gavin Howardd746e802018-12-28 22:06:00 -07001731 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06001732 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001733 else s = bc_num_parseBase(n, val, base, base_t);
1734
1735 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001736}
1737
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001738BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1739 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001740{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001741 BcStatus s = BC_STATUS_SUCCESS;
1742
Gavin Howardab5e9632019-01-02 13:32:02 -07001743 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001744 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001745
Gavin Howardab5e9632019-01-02 13:32:02 -07001746 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001747
Gavin Howardddce6d42019-01-03 14:07:06 -07001748 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06001749 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001750#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001751 else if (base_t == 0 || base_t == 1)
1752 s = bc_num_printExponent(n, base_t != 0);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001753#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001754 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001755
Gavin Howarde5f11c72019-02-23 09:42:51 -07001756 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001757 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001758 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001759 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001760
1761 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001762}
1763
Gavin Howard3c02ed32018-12-28 18:17:15 -07001764BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001765
Gavin Howard63738202018-09-26 15:34:20 -06001766 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001767 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001768
Gavin Howard63738202018-09-26 15:34:20 -06001769 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001770
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001771 *result = 0;
1772
Gavin Howardecafd4f2019-02-23 09:30:45 -07001773 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001774
Gavin Howard06b6e862019-02-15 11:15:31 -07001775 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001776
Gavin Howard0700efc2019-04-24 17:31:23 -06001777 unsigned long prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07001778
Gavin Howard0700efc2019-04-24 17:31:23 -06001779 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001780 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1781
Gavin Howard77445432019-04-26 15:59:33 -06001782 r = prev + (unsigned long) n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07001783
1784 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001785 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001786 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001787
Gavin Howard7b557da2018-12-21 10:25:32 -07001788 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001789
Gavin Howard63738202018-09-26 15:34:20 -06001790 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001791}
1792
Gavin Howard3c02ed32018-12-28 18:17:15 -07001793void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001794
Gavin Howardc25fd612019-04-26 19:31:31 -06001795 BcDig *ptr;
1796 unsigned long i;
1797
Gavin Howard63738202018-09-26 15:34:20 -06001798 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001799
Gavin Howard63738202018-09-26 15:34:20 -06001800 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001801
Gavin Howard1ab22d22019-01-03 13:32:17 -07001802 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001803
Gavin Howard798508b2019-02-21 16:45:32 -07001804 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howard05feab02019-04-23 16:24:07 -06001805
Gavin Howardc25fd612019-04-26 19:31:31 -06001806 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_DIG)
1807 ptr[i] = val % BC_BASE_DIG;
Gavin Howard025d04d2018-02-20 13:53:28 -07001808}
1809
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001810size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001811
1812 size_t aint, bint, ardx, brdx;
1813
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001814 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001815
1816 ardx = a->rdx;
1817 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001818 aint = bc_num_int(a);
1819 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001820 ardx = BC_MAX(ardx, brdx);
1821 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001822
Gavin Howard2d188a52019-02-25 14:19:08 -07001823 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001824}
1825
1826size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001827 size_t max, rdx;
1828 rdx = bc_vm_growSize(a->rdx, b->rdx);
1829 max = bc_vm_growSize(BC_MAX(scale, rdx), 1);
1830 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1831 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001832}
1833
1834size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1835 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001836 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001837}
1838
1839#if BC_ENABLE_EXTRA_MATH
1840size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1841 BC_UNUSED(b);
1842 BC_UNUSED(scale);
1843 return BC_NUM_SHREQ(a);
1844}
1845#endif // BC_ENABLE_EXTRA_MATH
1846
Gavin Howard12fe7812018-09-29 03:45:18 -06001847BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001848 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001849 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001850 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001851}
1852
Gavin Howard12fe7812018-09-29 03:45:18 -06001853BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001854 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001855 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001856 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001857}
1858
Gavin Howard12fe7812018-09-29 03:45:18 -06001859BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001860 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001861}
1862
Gavin Howard12fe7812018-09-29 03:45:18 -06001863BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001864 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001865}
1866
Gavin Howard54b946a2018-10-23 12:06:57 -06001867BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001868 size_t req = bc_num_mulReq(a, b, scale);
1869 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001870}
1871
Gavin Howard12fe7812018-09-29 03:45:18 -06001872BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001873 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001874}
1875
Gavin Howard7d8f0382018-12-27 16:15:13 -07001876#if BC_ENABLE_EXTRA_MATH
1877BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001878 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001879}
1880
1881BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001882 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001883}
1884
1885BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001886 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001887}
1888#endif // BC_ENABLE_EXTRA_MATH
1889
Gavin Howard3c02ed32018-12-28 18:17:15 -07001890BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001891
Gavin Howardfcf29012019-04-25 20:12:19 -06001892 // TODO: Check this function.
1893
Gavin Howard1cbfe242019-01-09 17:13:11 -07001894 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001895 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard14913fc2019-04-23 14:36:03 -06001896 size_t pow, len, req, digs, digs1, digs2, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001897 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001898 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001899
Gavin Howard34edd0a2018-10-25 15:51:05 -06001900 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001901
Gavin Howardecafd4f2019-02-23 09:30:45 -07001902 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07001903
Gavin Howard007afdf2019-02-23 09:45:42 -07001904 len = bc_vm_growSize(bc_num_int(a), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001905 req = bc_vm_growSize(BC_MAX(scale, a->rdx), len >> 1);
1906 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001907
Gavin Howardddce6d42019-01-03 14:07:06 -07001908 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001909 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001910 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001911 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001912 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001913 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001914 bc_num_extend(b, scale);
1915 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001916 }
Gavin Howard954276b2018-05-16 01:43:58 -06001917
Gavin Howard890d0c02018-10-30 16:34:50 -06001918 scale = BC_MAX(scale, a->rdx) + 1;
Gavin Howard007afdf2019-02-23 09:45:42 -07001919 len = bc_vm_growSize(a->len, scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001920
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001921 bc_num_init(&num1, len);
1922 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001923 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001924
Gavin Howard63738202018-09-26 15:34:20 -06001925 bc_num_one(&half);
1926 half.num[0] = 5;
1927 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001928
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001929 bc_num_init(&f, len);
1930 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001931
Gavin Howard63738202018-09-26 15:34:20 -06001932 x0 = &num1;
1933 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001934
Gavin Howard63738202018-09-26 15:34:20 -06001935 bc_num_one(x0);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001936 pow = bc_num_int(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001937
Gavin Howard53eba8b2018-10-31 15:14:37 -06001938 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001939
Gavin Howardc39fd492018-10-04 10:07:03 -06001940 if (pow & 1) x0->num[0] = 2;
1941 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001942
Gavin Howardc39fd492018-10-04 10:07:03 -06001943 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001944
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001945 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001946
Gavin Howard63738202018-09-26 15:34:20 -06001947 // Make sure to move the radix back.
1948 x0->rdx -= pow;
1949 }
Gavin Howard954276b2018-05-16 01:43:58 -06001950
Gavin Howard14913fc2019-04-23 14:36:03 -06001951 x0->rdx = digs = digs1 = digs2 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001952 resrdx = scale + 2;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001953 len = bc_num_int(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001954
Gavin Howard2d188a52019-02-25 14:19:08 -07001955 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001956
Gavin Howard971a2672019-04-26 14:32:07 -06001957 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07001958
Gavin Howard53eba8b2018-10-31 15:14:37 -06001959 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001960 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001961 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001962 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001963 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001964 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001965 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001966
Gavin Howard53eba8b2018-10-31 15:14:37 -06001967 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06001968 if (cmp == BC_NUM_SSIZE_MIN) {
1969 s = BC_STATUS_SIGNAL;
1970 break;
1971 }
1972
Gavin Howard53eba8b2018-10-31 15:14:37 -06001973 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001974
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001975 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001976 else times = 0;
1977
1978 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001979
1980 cmp2 = cmp1;
1981 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001982 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001983
Gavin Howard63738202018-09-26 15:34:20 -06001984 temp = x0;
1985 x0 = x1;
1986 x1 = temp;
1987 }
Gavin Howard954276b2018-05-16 01:43:58 -06001988
Gavin Howard2d188a52019-02-25 14:19:08 -07001989 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07001990 s = BC_STATUS_SIGNAL;
1991 goto err;
1992 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001993
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001994 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001995 scale -= 1;
1996 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001997
1998err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06001999 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06002000 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06002001 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06002002 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06002003 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07002004 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002005 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06002006 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07002007}
Gavin Howardba009802018-09-29 04:41:51 -06002008
Gavin Howard2cb39612018-10-22 08:46:48 -06002009BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
2010
Gavin Howardfcf29012019-04-25 20:12:19 -06002011 // TODO: Check this function.
2012
Gavin Howard2cb39612018-10-22 08:46:48 -06002013 BcStatus s;
2014 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06002015 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07002016 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002017
Gavin Howardf2ef2f32019-01-16 11:25:16 -07002018 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06002019
Gavin Howard890d0c02018-10-30 16:34:50 -06002020 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06002021 memcpy(&num2, c, sizeof(BcNum));
2022 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002023 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06002024 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06002025 }
Gavin Howardf679ad82018-10-29 12:26:30 -06002026 else {
2027 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002028 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06002029 }
Gavin Howard2cb39612018-10-22 08:46:48 -06002030
Gavin Howard54b946a2018-10-23 12:06:57 -06002031 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002032
Gavin Howardddce6d42019-01-03 14:07:06 -07002033 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002034 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07002035 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002036 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06002037
2038 if (init) bc_num_free(&num2);
2039
2040 return s;
2041}
2042
Gavin Howard40a085f2018-12-03 12:08:59 -07002043#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06002044BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06002045
Gavin Howardfcf29012019-04-25 20:12:19 -06002046 // TODO: Check this function.
2047
Gavin Howardba009802018-09-29 04:41:51 -06002048 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002049 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07002050 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06002051
Gavin Howard34edd0a2018-10-25 15:51:05 -06002052 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06002053
Gavin Howardecafd4f2019-02-23 09:30:45 -07002054 if (BC_ERR(BC_NUM_ZERO(c)))
2055 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
2056 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
2057 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07002058 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06002059
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002060 bc_num_expand(d, c->len);
2061 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07002062 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002063 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06002064
2065 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06002066 two.num[0] = 2;
2067 bc_num_one(d);
2068
Gavin Howard1769abc2019-02-21 09:36:14 -07002069 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002070 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002071 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002072 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07002073 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06002074
Gavin Howard2d188a52019-02-25 14:19:08 -07002075 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06002076
Gavin Howard1769abc2019-02-21 09:36:14 -07002077 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002078 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002079 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002080
Gavin Howarda83a5e52019-02-21 16:51:06 -07002081 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06002082 s = bc_num_mul(d, &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, d, 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 Howard53eba8b2018-10-31 15:14:37 -06002091 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002092 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002093
2094 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002095 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002096 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002097 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002098 }
2099
Gavin Howard2d188a52019-02-25 14:19:08 -07002100 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002101
Gavin Howardba009802018-09-29 04:41:51 -06002102err:
Gavin Howardba009802018-09-29 04:41:51 -06002103 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002104rem_err:
2105 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002106 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002107 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002108 return s;
2109}
Gavin Howarde6e84762018-10-03 11:46:34 -06002110#endif // DC_ENABLED