blob: 01932ce37df6ffc4015276c77b41e59daec98732 [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
Stefan Esser33a1c682019-04-29 13:00:23 +020075#define P(x) fprintf(stderr, "%s = %ld\n", #x, (long)x)
76
77static unsigned long bc_num_pow10(unsigned long i);
78
79#define DUMP_NUM bc_num_dump
Gavin Howard5b40e402019-04-27 07:26:35 -060080static void bc_num_dump(const char *varname, const BcNum *n) {
Stefan Eßer4b111c22019-04-24 17:23:08 -060081
Stefan Esser33a1c682019-04-29 13:00:23 +020082 unsigned long i, scale = n->scale;
Stefan Eßer4b111c22019-04-24 17:23:08 -060083
Stefan Esserb5aeb442019-04-27 23:33:08 +020084 fprintf(stderr, "\n%s = %s", varname, n->len ? (n->neg ? "-" : "+") : "0 ");
Stefan Eßer4b111c22019-04-24 17:23:08 -060085
Gavin Howardc4783ac2019-04-26 10:40:02 -060086 for (i = n->len -1; i < n->len; i--) {
Gavin Howardc88e5232019-04-27 07:33:16 -060087 if (i + 1 == n->rdx) fprintf(stderr, ". ");
Stefan Esserf76f0362019-04-29 21:28:52 +020088 if (scale / BC_BASE_POWER != n->rdx -i -1)
Stefan Esser33a1c682019-04-29 13:00:23 +020089 fprintf(stderr, "%0*d ", BC_BASE_POWER, n->num[i]);
Stefan Esserf76f0362019-04-29 21:28:52 +020090 else {
Stefan Esser33a1c682019-04-29 13:00:23 +020091 if (scale % BC_BASE_POWER != 0)
92 fprintf(stderr, "%0*lu", (int)scale % BC_BASE_POWER, n->num[i] / bc_num_pow10(BC_BASE_POWER - scale % BC_BASE_POWER));
Stefan Esserf76f0362019-04-29 21:28:52 +020093 fprintf(stderr, " ' %0*lu ", BC_BASE_POWER - (int)scale % BC_BASE_POWER, n->num[i] % bc_num_pow10(scale % BC_BASE_POWER));
Stefan Esser33a1c682019-04-29 13:00:23 +020094 }
Stefan Eßer4b111c22019-04-24 17:23:08 -060095 }
96
Stefan Esser33a1c682019-04-29 13:00:23 +020097 fprintf(stderr, "(%zu | %zu.%zu/%zu) %p\n",
98 n->scale, n->len, n->rdx, n->cap, (void*) n->num);
Stefan Eßer4b111c22019-04-24 17:23:08 -060099}
Stefan Esserf76f0362019-04-29 21:28:52 +0200100#else // BC_DEBUG_CODE
101
102#undef DUMP_NUM
103#define DUMP_NUM(x,y)
104#P(x)
105
Gavin Howard55c8f732019-04-24 14:05:27 -0600106#endif // BC_DEBUG_CODE
107
Gavin Howard49ea7b62019-04-23 14:46:26 -0600108static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
109
Gavin Howard56ebcf72019-02-21 16:59:48 -0700110static ssize_t bc_num_neg(size_t n, bool neg) {
111 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
112}
113
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700114ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -0700115 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -0700116}
117
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700118static size_t bc_num_int(const BcNum *n) {
119 return n->len ? n->len - n->rdx : 0;
120}
121
Stefan Essercc7cd932019-04-29 17:10:43 +0200122static size_t bc_num_log10(size_t i); // --> num.h
123
124static size_t bc_num_int_digits(const BcNum *n) {
125 size_t digits;
126
127 digits = bc_num_int(n) * BC_BASE_POWER;
128
129 if (digits > 0) {
130 digits -= BC_BASE_POWER - bc_num_log10(n->num[n->len -1]);
131 }
132
133 return digits;
134}
135
Gavin Howarda83a5e52019-02-21 16:51:06 -0700136static bool bc_num_isOne(const BcNum *n) {
137 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
138}
139
Gavin Howard798508b2019-02-21 16:45:32 -0700140static void bc_num_expand(BcNum *restrict n, size_t req) {
141 assert(n);
142 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
143 if (req > n->cap) {
Gavin Howard404ece72019-04-26 15:59:15 -0600144 n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req));
Gavin Howard798508b2019-02-21 16:45:32 -0700145 n->cap = req;
146 }
147}
148
Gavin Howard1cbfe242019-01-09 17:13:11 -0700149static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600150 assert(n);
Gavin Howardfcf29012019-04-25 20:12:19 -0600151 n->scale = scale;
152 n->len = n->rdx = 0;
Gavin Howard97102b12018-11-01 23:37:31 -0600153 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600154}
155
Gavin Howard1cbfe242019-01-09 17:13:11 -0700156static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600157 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -0600158}
159
Gavin Howard3c02ed32018-12-28 18:17:15 -0700160void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600161 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -0600162 n->len = 1;
163 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -0600164}
165
Gavin Howard3c02ed32018-12-28 18:17:15 -0700166void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600167 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600168 bc_num_setToZero(n, 0);
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600169#if BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600170 n->len = 2;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600171 n->num[0] = 0;
172 n->num[1] = 1;
173#else // BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600174 n->len = 1;
Stefan Eßer4b111c22019-04-24 17:23:08 -0600175 n->num[0] = BC_BASE;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600176#endif // BC_BASE_DIG == 10
Gavin Howard0be26ed2018-08-31 20:21:56 -0600177}
178
Gavin Howard1cbfe242019-01-09 17:13:11 -0700179static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -0700180 size_t len;
Gavin Howard530e3072019-04-23 16:23:36 -0600181 for (len = 1; i; i /= BC_BASE, ++len);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700182 return len;
183}
184
Stefan Esser72109f42019-04-28 21:32:13 +0200185#define POW10N 10
186
187static unsigned long pow10[POW10N] = {
188 1,
189 10,
190 100,
191 1000,
192 10000,
193 100000,
194 1000000,
195 10000000,
196 100000000,
197 1000000000,
198};
199
Gavin Howard971a2672019-04-26 14:32:07 -0600200static unsigned long bc_num_pow10(unsigned long i) {
Stefan Esser72109f42019-04-28 21:32:13 +0200201
Gavin Howard971a2672019-04-26 14:32:07 -0600202 unsigned long pow;
Stefan Esser72109f42019-04-28 21:32:13 +0200203
Stefan Esser33a1c682019-04-29 13:00:23 +0200204 if (i == 0) return 1;
Stefan Esser72109f42019-04-28 21:32:13 +0200205 if (i < POW10N) return pow10[i];
206
Stefan Esser33a1c682019-04-29 13:00:23 +0200207 i -= POW10N;
208 if (i < POW10N - 1) return pow10[POW10N - 1] * pow10[i + 1];
Stefan Esser72109f42019-04-28 21:32:13 +0200209
Stefan Esser72109f42019-04-28 21:32:13 +0200210 return -1;
Gavin Howard971a2672019-04-26 14:32:07 -0600211}
212
Gavin Howardfcf29012019-04-25 20:12:19 -0600213static unsigned long bc_num_addDigit(BcDig *restrict num, unsigned long d,
214 unsigned long c)
Gavin Howard14c354a2019-04-24 14:28:24 -0600215{
216 d += c;
Gavin Howard0700efc2019-04-24 17:31:23 -0600217 *num = (BcDig) (d % BC_BASE_DIG);
218 assert(*num >= 0 && *num < BC_BASE_DIG);
219 return d / BC_BASE_DIG;
Gavin Howard14c354a2019-04-24 14:28:24 -0600220}
221
222static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
223 size_t len)
224{
225 size_t i;
Gavin Howard971a2672019-04-26 14:32:07 -0600226 unsigned long carry = 0;
Gavin Howard14c354a2019-04-24 14:28:24 -0600227
228 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard971a2672019-04-26 14:32:07 -0600229 unsigned long in = ((unsigned long) a[i]) + ((unsigned long) b[i]);
Gavin Howard14c354a2019-04-24 14:28:24 -0600230 carry = bc_num_addDigit(a + i, in, carry);
231 }
232
233 for (; BC_NO_SIG && carry; ++i)
Gavin Howard971a2672019-04-26 14:32:07 -0600234 carry = bc_num_addDigit(a + i, (unsigned long) a[i], carry);
Gavin Howard14c354a2019-04-24 14:28:24 -0600235
236 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
237}
238
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700239static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
240 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700241{
Gavin Howard63738202018-09-26 15:34:20 -0600242 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600243
Gavin Howard2d188a52019-02-25 14:19:08 -0700244 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600245
Gavin Howard2d188a52019-02-25 14:19:08 -0700246 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600247 assert(a[i + j] >= -BC_BASE_DIG);
248 a[i + j++] += BC_BASE_DIG;
Gavin Howard54b946a2018-10-23 12:06:57 -0600249 a[i + j] -= 1;
Gavin Howard0700efc2019-04-24 17:31:23 -0600250 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_DIG);
Gavin Howard63738202018-09-26 15:34:20 -0600251 }
252 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600253
Gavin Howard2d188a52019-02-25 14:19:08 -0700254 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600255}
256
Gavin Howard2d188a52019-02-25 14:19:08 -0700257static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
258 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700259{
Gavin Howard63738202018-09-26 15:34:20 -0600260 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600261 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700262 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600263 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600264}
265
Gavin Howard3c02ed32018-12-28 18:17:15 -0700266ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700267
Gavin Howard63738202018-09-26 15:34:20 -0600268 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600269 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600270 bool a_max, neg = false;
271 ssize_t cmp;
272
273 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700274
Gavin Howard8a921bd2018-10-11 14:15:32 -0600275 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700276 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700277 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600278 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600279 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600280 else return -1;
281 }
282 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700283
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700284 a_int = bc_num_int(a);
285 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600286 a_int -= b_int;
287 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700288
Gavin Howard1ab22d22019-01-03 13:32:17 -0700289 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700290
Gavin Howard890d0c02018-10-30 16:34:50 -0600291 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600292 min = b->rdx;
293 diff = a->rdx - b->rdx;
294 max_num = a->num + diff;
295 min_num = b->num;
296 }
297 else {
298 min = a->rdx;
299 diff = b->rdx - a->rdx;
300 max_num = b->num + diff;
301 min_num = a->num;
302 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700303
Gavin Howard890d0c02018-10-30 16:34:50 -0600304 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600305 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700306 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700307
Gavin Howard2d188a52019-02-25 14:19:08 -0700308 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700309 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600310 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700311
Gavin Howardf2ee6342019-04-09 17:05:20 -0600312 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700313}
314
Gavin Howard2a366922019-01-16 10:50:20 -0700315static void bc_num_clean(BcNum *restrict n) {
316 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
317 if (BC_NUM_ZERO(n)) n->neg = false;
318 else if (n->len < n->rdx) n->len = n->rdx;
319}
320
Gavin Howard3c02ed32018-12-28 18:17:15 -0700321void bc_num_truncate(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;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700324
Stefan Esser70f11852019-04-27 10:39:00 +0200325 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700326
Gavin Howardb1558662019-04-26 14:32:55 -0600327 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
328 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
329
330 n->scale -= places;
331 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700332
Gavin Howardddce6d42019-01-03 14:07:06 -0700333 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600334
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600335 size_t pow;
336
337 pow = n->scale % BC_BASE_POWER;
338 pow = pow ? BC_BASE_POWER - pow : 0;
339 pow = bc_num_pow10(pow);
Gavin Howardb1558662019-04-26 14:32:55 -0600340
341 n->len -= places_rdx;
Gavin Howard404ece72019-04-26 15:59:15 -0600342 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
Gavin Howardb1558662019-04-26 14:32:55 -0600343
344 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600345 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600346
Gavin Howard2a366922019-01-16 10:50:20 -0700347 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600348 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700349}
350
Stefan Esser70f11852019-04-27 10:39:00 +0200351static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700352
Gavin Howardb1558662019-04-26 14:32:55 -0600353 size_t places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700354
Gavin Howard1ab22d22019-01-03 13:32:17 -0700355 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700356
Gavin Howardb1558662019-04-26 14:32:55 -0600357 bc_num_expand(n, n->len + places_rdx);
Gavin Howard404ece72019-04-26 15:59:15 -0600358 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
359 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
Gavin Howarde6f326e2019-04-26 10:13:45 -0600360 n->rdx += places_rdx;
361 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600362 n->len += places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700363
Gavin Howarde6f326e2019-04-26 10:13:45 -0600364 assert(n->rdx == BC_NUM_RDX(n->scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200365}
366
Stefan Esser40b53d22019-04-29 12:56:32 +0200367static void bc_num_roundPlaces(BcNum *restrict n, size_t places) { // --> num.h
Stefan Esserdb970632019-04-28 14:58:58 +0200368
369 size_t rdx, place, i;
370 BcDig p10, sum;
371
Stefan Esser40b53d22019-04-29 12:56:32 +0200372 if (places >= n->scale || places >= n->rdx * BC_BASE_POWER)
Stefan Esserdb970632019-04-28 14:58:58 +0200373 return;
Stefan Esser40b53d22019-04-29 12:56:32 +0200374
Stefan Esserdb970632019-04-28 14:58:58 +0200375 rdx = n->rdx - BC_NUM_RDX(places + 1);
376 place = BC_BASE_POWER - (places % BC_BASE_POWER + 1);
377
378 for (i = 0; i < rdx; i++)
379 n->num[i] = 0;
380
381 p10 = bc_num_pow10(place);
382 sum = n->num[rdx] + 5 * p10;
383 sum = sum - sum % (10 * p10);
384
385 if (sum < BC_BASE_DIG) {
386 n->num[rdx] = sum;
Stefan Esserf76f0362019-04-29 21:28:52 +0200387 }
388 else {
Stefan Esserdb970632019-04-28 14:58:58 +0200389 sum -= BC_BASE_DIG;
390 n->num[rdx] = sum;
391
392 do {
393 rdx++;
394 if (n->len <= rdx) {
395 bc_num_expand(n, rdx + 1);
396 n->num[rdx] = 0;
397 n->len = rdx +1;
398 }
399 sum = n->num[rdx] + 1;
400 n->num[rdx] = sum < BC_BASE_DIG ? sum : sum - BC_BASE_DIG;
401 } while (sum >= BC_BASE_DIG);
402 }
Stefan Esser40b53d22019-04-29 12:56:32 +0200403 if (n->scale > places)
404 bc_num_truncate(n, n->scale - places);
Stefan Esserdb970632019-04-28 14:58:58 +0200405}
406
Gavin Howard2d188a52019-02-25 14:19:08 -0700407static void bc_num_retireMul(BcNum *restrict n, size_t scale,
408 bool neg1, bool neg2)
409{
Gavin Howard971a2672019-04-26 14:32:07 -0600410 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
411 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600412
Gavin Howard305249a2018-10-15 20:24:47 -0600413 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700414 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600415}
416
Gavin Howard2d188a52019-02-25 14:19:08 -0700417static void bc_num_split(const BcNum *restrict n, size_t idx,
418 BcNum *restrict a, BcNum *restrict b)
419{
Gavin Howardfcf29012019-04-25 20:12:19 -0600420 // TODO: Check this function.
Gavin Howard305249a2018-10-15 20:24:47 -0600421 if (idx < n->len) {
422
Gavin Howard305249a2018-10-15 20:24:47 -0600423 b->len = n->len - idx;
424 a->len = idx;
Gavin Howarda3f260c2019-04-26 15:47:25 -0600425 a->scale = a->rdx = b->scale = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600426
Gavin Howard404ece72019-04-26 15:59:15 -0600427 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
428 memcpy(a->num, n->num, BC_NUM_SIZE(idx));
Gavin Howard50c80022019-01-03 15:14:33 -0700429
430 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600431 }
Gavin Howard50c80022019-01-03 15:14:33 -0700432 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600433
434 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600435}
436
Gavin Howard14913fc2019-04-23 14:36:03 -0600437static size_t bc_num_shiftZero(BcNum *restrict n) {
438
439 size_t i;
440
441 assert(!n->rdx || BC_NUM_ZERO(n));
442
443 for (i = 0; i < n->len && !n->num[i]; ++i);
444
445 n->len -= i;
446 n->num += i;
447
448 return i;
449}
450
Gavin Howard2e736de2019-04-27 06:41:58 -0600451static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
452 n->len += places_rdx;
453 n->num -= places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600454}
455
Gavin Howard48a0e472019-04-26 15:39:46 -0600456static BcStatus bc_num_shift(BcNum *restrict n, BcDig *restrict ptr,
457 unsigned long dig, size_t len)
458{
Gavin Howardb1558662019-04-26 14:32:55 -0600459 size_t i;
460 unsigned long carry = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600461
Gavin Howardb1558662019-04-26 14:32:55 -0600462 assert(dig < BC_BASE_POWER);
Gavin Howard305249a2018-10-15 20:24:47 -0600463
Gavin Howardb1558662019-04-26 14:32:55 -0600464 dig = bc_num_pow10(dig);
465
466 for (i = 0; BC_NO_SIG && i < len; ++i) {
467 unsigned long in = ((unsigned long) ptr[i]) * dig;
468 carry = bc_num_addDigit(ptr + i, in, carry);
Gavin Howard305249a2018-10-15 20:24:47 -0600469 }
470
Gavin Howardb1558662019-04-26 14:32:55 -0600471 if (carry) {
472 bc_num_expand(n, n->len + 1);
473 assert(carry < BC_BASE_DIG);
474 n->num[n->len] = (BcDig) carry;
475 n->len += 1;
476 }
477
478 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600479}
480
Gavin Howardb1558662019-04-26 14:32:55 -0600481static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700482
Gavin Howardb1558662019-04-26 14:32:55 -0600483 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600484 unsigned long dig;
485 bool shift;
486 size_t places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600487
Gavin Howardb1558662019-04-26 14:32:55 -0600488 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600489
Gavin Howard48a0e472019-04-26 15:39:46 -0600490 dig = (unsigned long) (places % BC_BASE_POWER);
491 shift = (dig != 0);
492 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx - shift;
493
Gavin Howardb1558662019-04-26 14:32:55 -0600494 if (n->scale >= places) {
495 n->scale -= places;
496 n->rdx = BC_NUM_RDX(n->scale);
497 }
Stefan Esserf76f0362019-04-29 21:28:52 +0200498 else
499 if (BC_NUM_NONZERO(n)) {
Gavin Howard48a0e472019-04-26 15:39:46 -0600500
Stefan Esserf76f0362019-04-29 21:28:52 +0200501 if (places_rdx > n->rdx) {
502 bc_num_expand(n, n->len + places_rdx - n->rdx + shift);
503 memmove(n->num + places_rdx - n->rdx, n->num, BC_NUM_SIZE(n->len));
504 memset(n->num, 0, BC_NUM_SIZE(places_rdx - n->rdx));
505 n->len += places_rdx;
506 }
507
508 n->scale = n->rdx = 0;
Gavin Howard48a0e472019-04-26 15:39:46 -0600509 }
Stefan Esserf76f0362019-04-29 21:28:52 +0200510 else n->scale = 0;
Gavin Howardb1558662019-04-26 14:32:55 -0600511
512 if (shift) s = bc_num_shift(n, n->num + places_rdx, dig, n->len - places_rdx);
513
514 bc_num_clean(n);
515
516 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
517}
518
519static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
520
521 BcStatus s = BC_STATUS_SUCCESS;
522 unsigned long dig = (unsigned long) (places % BC_BASE_POWER);
523 bool shift = (dig != 0);
524 size_t len, places_rdx;
525
526 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600527 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600528 n->scale += places;
529 bc_num_expand(n, BC_NUM_RDX(n->scale));
530 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600531 }
532
Gavin Howardb1558662019-04-26 14:32:55 -0600533 places_rdx = BC_NUM_RDX(places);
534 len = bc_vm_growSize(n->len, places_rdx);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700535
536 if (len > n->len) {
537
538 if (len > n->cap) bc_num_expand(n, len);
539
Gavin Howard404ece72019-04-26 15:59:15 -0600540 memset(n->num + n->len, 0, BC_NUM_SIZE(len - n->len));
Gavin Howard7ad5a662019-02-19 14:40:46 -0700541 n->len = len;
542 }
543
Gavin Howardb1558662019-04-26 14:32:55 -0600544 n->scale += places;
545 n->rdx += places_rdx;
546
547 if (shift) s = bc_num_shift(n, n->num, BC_BASE_POWER - dig, n->len);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700548
549 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600550 assert(n->rdx == BC_NUM_RDX(n->scale));
551
552 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700553}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700554
Gavin Howard1cbfe242019-01-09 17:13:11 -0700555static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600556
Gavin Howard63738202018-09-26 15:34:20 -0600557 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600558 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600559
Gavin Howard971a2672019-04-26 14:32:07 -0600560 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700561
Gavin Howardd32d7df2018-10-15 08:42:25 -0600562 one.cap = 2;
563 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600564 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600565
Gavin Howardd32d7df2018-10-15 08:42:25 -0600566 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600567}
568
Gavin Howard7bda4782018-12-28 09:53:22 -0700569#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700570static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
571 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700572{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700573 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700574 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700575 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700576}
Gavin Howard7bda4782018-12-28 09:53:22 -0700577#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700578
Gavin Howard1cbfe242019-01-09 17:13:11 -0700579static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700580
Gavin Howard8a921bd2018-10-11 14:15:32 -0600581 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600582 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard6fccd222019-04-26 10:17:02 -0600583 unsigned long carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700584
Gavin Howard63738202018-09-26 15:34:20 -0600585 // Because this function doesn't need to use scale (per the bc spec),
586 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600587
Gavin Howardddce6d42019-01-03 14:07:06 -0700588 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600589 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700590 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600591 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600592 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700593 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600594 bc_num_copy(c, a);
595 return BC_STATUS_SUCCESS;
596 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600597
Gavin Howard63738202018-09-26 15:34:20 -0600598 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600599 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600600 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600601 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700602
Gavin Howard63738202018-09-26 15:34:20 -0600603 if (a->rdx > b->rdx) {
604 diff = a->rdx - b->rdx;
605 ptr = a->num;
606 ptr_a = a->num + diff;
607 ptr_b = b->num;
608 }
609 else {
610 diff = b->rdx - a->rdx;
611 ptr = b->num;
612 ptr_a = a->num;
613 ptr_b = b->num + diff;
614 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700615
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700616 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700617
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700618 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600619 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700620 a_int = bc_num_int(a);
621 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700622
Gavin Howard890d0c02018-10-30 16:34:50 -0600623 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600624 min_int = b_int;
625 max = a_int;
626 ptr = ptr_a;
627 }
628 else {
629 min_int = a_int;
630 max = b_int;
631 ptr = ptr_b;
632 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700633
Gavin Howard2d188a52019-02-25 14:19:08 -0700634 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard6fccd222019-04-26 10:17:02 -0600635 unsigned long in;
636 in = ((unsigned long) ptr_a[i]) + ((unsigned long) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700637 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600638 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700639
Gavin Howard2d188a52019-02-25 14:19:08 -0700640 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard6fccd222019-04-26 10:17:02 -0600641 carry = bc_num_addDigit(ptr_c + i, (unsigned long) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700642
643 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700644
Gavin Howard1ab22d22019-01-03 13:32:17 -0700645 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600646
Gavin Howard2d188a52019-02-25 14:19:08 -0700647 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700648}
649
Gavin Howard1cbfe242019-01-09 17:13:11 -0700650static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700651
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700652 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600653 ssize_t cmp;
654 BcNum *minuend, *subtrahend;
655 size_t start;
656 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700657
Gavin Howard63738202018-09-26 15:34:20 -0600658 // Because this function doesn't need to use scale (per the bc spec),
659 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700660
Gavin Howardddce6d42019-01-03 14:07:06 -0700661 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600662 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700663 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600664 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600665 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700666 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600667 bc_num_copy(c, a);
668 return BC_STATUS_SUCCESS;
669 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700670
Gavin Howard63738202018-09-26 15:34:20 -0600671 aneg = a->neg;
672 bneg = b->neg;
673 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700674
Gavin Howard63738202018-09-26 15:34:20 -0600675 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600676 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700677
Gavin Howard63738202018-09-26 15:34:20 -0600678 a->neg = aneg;
679 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700680
Gavin Howard1ab22d22019-01-03 13:32:17 -0700681 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600682 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600683 return BC_STATUS_SUCCESS;
684 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700685
686 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600687 neg = a->neg;
688 minuend = a;
689 subtrahend = b;
690 }
691 else {
692 neg = b->neg;
693 if (sub) neg = !neg;
694 minuend = b;
695 subtrahend = a;
696 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700697
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600698 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600699 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700700
Gavin Howarde6f326e2019-04-26 10:13:45 -0600701 if (c->scale < subtrahend->scale) {
702 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600703 start = 0;
704 }
705 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700706
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700707 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700708
Gavin Howard63738202018-09-26 15:34:20 -0600709 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700710
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700711 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700712}
713
Gavin Howarddc4e3712019-04-23 14:56:29 -0600714static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
715{
Gavin Howardfcf29012019-04-25 20:12:19 -0600716 // TODO: Check this function.
Gavin Howard971a2672019-04-26 14:32:07 -0600717 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600718 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard971a2672019-04-26 14:32:07 -0600719 unsigned long sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600720
Gavin Howard971a2672019-04-26 14:32:07 -0600721 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600722 assert(!a->rdx && !b->rdx);
723
Gavin Howard5083aec2019-04-23 18:03:46 -0600724 clen = bc_vm_growSize(alen, blen);
725 bc_num_expand(c, clen + 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600726
727 ptr_c = c->num;
Gavin Howard404ece72019-04-26 15:59:15 -0600728 memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600729
Gavin Howard5083aec2019-04-23 18:03:46 -0600730 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600731
Gavin Howard5083aec2019-04-23 18:03:46 -0600732 ssize_t sidx = (ssize_t) (i - blen + 1);
733 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600734
Stefan Essera299ffc2019-04-24 23:28:32 +0200735 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
Stefan Eßer4b111c22019-04-24 17:23:08 -0600736
Gavin Howard971a2672019-04-26 14:32:07 -0600737 sum += ((unsigned long) ptr_a[j]) * ((unsigned long) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600738
Gavin Howard0700efc2019-04-24 17:31:23 -0600739 if (sum >= BC_BASE_DIG) {
740 carry += sum / BC_BASE_DIG;
741 sum %= BC_BASE_DIG;
Stefan Essera299ffc2019-04-24 23:28:32 +0200742 }
743 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600744
Stefan Essera299ffc2019-04-24 23:28:32 +0200745 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600746 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Essera299ffc2019-04-24 23:28:32 +0200747 sum = carry;
748 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600749 }
750
Gavin Howard5083aec2019-04-23 18:03:46 -0600751 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600752 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600753 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600754 clen += 1;
755 }
756
757 c->len = clen;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600758 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
759}
760
Gavin Howard14c354a2019-04-24 14:28:24 -0600761static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
762 size_t shift, BcNumShiftAddOp op)
763{
Gavin Howardfcf29012019-04-25 20:12:19 -0600764 // TODO: Check this function.
Gavin Howard14c354a2019-04-24 14:28:24 -0600765 assert(n->len >= shift + a->len);
766 assert(!n->rdx && !a->rdx);
767 return op(n->num + shift, a->num, a->len);
768}
769
770static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600771
Gavin Howardfcf29012019-04-25 20:12:19 -0600772 // TODO: Check this function.
773
Gavin Howard9b801632019-02-16 23:34:06 -0700774 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200775 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600776 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700777 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600778 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700779 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600780
Gavin Howard14913fc2019-04-23 14:36:03 -0600781 assert(BC_NUM_ZERO(c));
782
Gavin Howardddce6d42019-01-03 14:07:06 -0700783 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600784 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700785 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600786 bc_num_zero(c);
787 return BC_STATUS_SUCCESS;
788 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700789 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600790 bc_num_copy(c, aone ? b : a);
791 return BC_STATUS_SUCCESS;
792 }
Gavin Howardb5093d72018-10-17 12:13:17 -0600793 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
794 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600795 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600796 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600797 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700798
Gavin Howarde0432502019-02-26 16:23:45 -0700799 max = BC_MAX(a->len, b->len);
Stefan Essera299ffc2019-04-24 23:28:32 +0200800 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700801 max2 = (max + 1) / 2;
802
Stefan Esser5fd43a32019-04-25 07:05:24 +0200803 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
Gavin Howard404ece72019-04-26 15:59:15 -0600804 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total));
Gavin Howard5bede412019-02-23 09:31:28 -0700805
806 bc_num_setup(&l1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200807 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700808 bc_num_setup(&h1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200809 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700810 bc_num_setup(&l2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200811 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700812 bc_num_setup(&h2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200813 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700814 bc_num_setup(&m1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200815 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700816 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600817 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600818 bc_num_init(&z0, max);
819 bc_num_init(&z1, max);
820 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600821 max = bc_vm_growSize(max, max) + 1;
822 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600823
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600824 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600825 bc_num_clean(&l1);
826 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600827 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600828 bc_num_clean(&l2);
829 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700830
Gavin Howard14c354a2019-04-24 14:28:24 -0600831 bc_num_expand(c, max);
832 c->len = max;
Gavin Howard404ece72019-04-26 15:59:15 -0600833 memset(c->num, 0, BC_NUM_SIZE(c->len));
Gavin Howard305249a2018-10-15 20:24:47 -0600834
Gavin Howard14c354a2019-04-24 14:28:24 -0600835 s = bc_num_sub(&h1, &l1, &m1, 0);
836 if (BC_ERR(s)) goto err;
837 s = bc_num_sub(&l2, &h2, &m2, 0);
838 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600839
Gavin Howard971a2672019-04-26 14:32:07 -0600840 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600841
Gavin Howard14c354a2019-04-24 14:28:24 -0600842 s = bc_num_m(&h1, &h2, &z2, 0);
843 if (BC_ERR(s)) goto err;
844 bc_num_clean(&z2);
845
846 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
847 if (BC_ERR(s)) goto err;
848 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
849 if (BC_ERR(s)) goto err;
850 }
851
Gavin Howard971a2672019-04-26 14:32:07 -0600852 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600853
854 s = bc_num_m(&l1, &l2, &z0, 0);
855 if (BC_ERR(s)) goto err;
856 bc_num_clean(&z0);
857
858 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
859 if (BC_ERR(s)) goto err;
860 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
861 if (BC_ERR(s)) goto err;
862 }
863
Gavin Howard971a2672019-04-26 14:32:07 -0600864 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600865
866 s = bc_num_m(&m1, &m2, &z1, 0);
867 if (BC_ERR(s)) goto err;
868 bc_num_clean(&z1);
869
870 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
871 s = bc_num_shiftAddSub(c, &z1, max2, op);
872 if (BC_ERR(s)) goto err;
873 }
Gavin Howard305249a2018-10-15 20:24:47 -0600874
875err:
Gavin Howard5bede412019-02-23 09:31:28 -0700876 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600877 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600878 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600879 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600880 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600881 return s;
882}
883
Gavin Howard1cbfe242019-01-09 17:13:11 -0700884static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600885
Gavin Howardfcf29012019-04-25 20:12:19 -0600886 // TODO: Check this function.
887
Gavin Howard305249a2018-10-15 20:24:47 -0600888 BcStatus s;
889 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600890 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600891
Gavin Howard14913fc2019-04-23 14:36:03 -0600892 bc_num_setToZero(c, 0);
Stefan Esserb5aeb442019-04-27 23:33:08 +0200893 ascale = a->scale;
894 bscale = b->scale;
Stefan Esserd793fa52019-04-28 12:30:47 +0200895 scale = BC_MAX(scale, ascale);
896 scale = BC_MAX(scale, bscale);
Stefan Esser45a56122019-04-27 23:45:48 +0200897
Gavin Howard48a0e472019-04-26 15:39:46 -0600898 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600899 scale = BC_MIN(rscale, scale);
Gavin Howardbaa4f582019-01-24 13:56:35 -0700900 bc_num_createCopy(&cpa, a);
901 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600902
Gavin Howard23d8d252018-10-17 12:03:41 -0600903 cpa.neg = cpb.neg = false;
Gavin Howard48a0e472019-04-26 15:39:46 -0600904 ardx = cpa.rdx * BC_BASE_POWER;
905 s = bc_num_shiftLeft(&cpa, ardx);
906 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600907 bc_num_clean(&cpa);
908 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600909
910 brdx = cpb.rdx * BC_BASE_POWER;
911 s = bc_num_shiftLeft(&cpb, brdx);
912 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600913 bzero = bc_num_shiftZero(&cpb);
914 bc_num_clean(&cpb);
915
Gavin Howard14c354a2019-04-24 14:28:24 -0600916 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700917 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600918
Gavin Howard14913fc2019-04-23 14:36:03 -0600919 zero = bc_vm_growSize(azero, bzero);
920 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600921
Gavin Howard14913fc2019-04-23 14:36:03 -0600922 bc_num_expand(c, len);
Gavin Howard48a0e472019-04-26 15:39:46 -0600923 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_POWER);
924 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
925 s = bc_num_shiftRight(c, ardx + brdx);
926 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600927 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600928
Gavin Howard305249a2018-10-15 20:24:47 -0600929err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600930 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600931 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600932 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600933 bc_num_free(&cpa);
934 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700935}
936
Stefan Esser154b4732019-04-28 17:21:34 +0200937static BcStatus bc_num_invert(BcNum *val, size_t scale) { // --> num.h <se>
938
939 BcNum one, x, temp;
940 bool done = false;
941 BcStatus s = BC_STATUS_SUCCESS;
942
943 bc_num_createFromUlong(&one, 1);
Stefan Esser40b53d22019-04-29 12:56:32 +0200944 bc_num_init(&temp, scale / BC_BASE_POWER + 1);
945 bc_num_init(&x, scale / BC_BASE_POWER + 1);
Stefan Esser154b4732019-04-28 17:21:34 +0200946 s = bc_num_sub(&one, val, &x, scale);
947 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
948 s = bc_num_add(&one, &x, val, scale);
949 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
950
951 for (;;) {
952 s = bc_num_mul(&x, &x, &x, scale);
953 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
954 if BC_NUM_ZERO(&x) break;
955
956 s = bc_num_mul(val, &x, &temp, scale);
957 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
958 s = bc_num_add(val, &temp, val, scale);
959 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
960 }
961err:
962 bc_num_free(&one);
963 bc_num_free(&x);
964 bc_num_free(&temp);
965
966 return s;
967}
968
Stefan Esser193f8522019-04-27 01:29:01 +0200969static int intlog2(size_t n) {
Stefan Esser40b53d22019-04-29 12:56:32 +0200970
Stefan Esser193f8522019-04-27 01:29:01 +0200971 int bits = 0;
972
973 n >>= 1;
974 while (n != 0) {
975 bits++;
976 n >>= 1;
977 }
978 return (bits);
979}
980
Stefan Esser40b53d22019-04-29 12:56:32 +0200981// normalize number to have rdx == len and return the number of BcDigs the value has been shifted to the right (negative for left)
982static int bc_num_normalize(BcNum *n) {
983
984 int i, shift = 0;
985 ssize_t len, rdx;
986
987 if (BC_NUM_ZERO(n)) return 0;
988
989 len = n->len;
990 rdx = n->rdx;
991
992 while (len > 0 && n->num[len - 1] == 0)
993 len--;
994
995 n->len = len;
996 n->rdx = len;
997 n->scale += (len - rdx) * BC_BASE_POWER;
998
999 return len - rdx;
1000}
1001
Stefan Esserb5aeb442019-04-27 23:33:08 +02001002static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001003
Gavin Howardfcf29012019-04-25 20:12:19 -06001004 // TODO: Check this function.
1005
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001006 BcStatus s = BC_STATUS_SUCCESS;
Stefan Esser6e129fc2019-04-27 23:41:21 +02001007 ssize_t cmp;
Stefan Esser193f8522019-04-27 01:29:01 +02001008 BcNum b1, f;
1009 size_t factor, dividend, divisor;
Stefan Esser40b53d22019-04-29 12:56:32 +02001010 size_t i, j, mindivisor, temp_scale;
1011 int shift;
Stefan Esser6e129fc2019-04-27 23:41:21 +02001012 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001013
Gavin Howardddce6d42019-01-03 14:07:06 -07001014 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1015 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001016 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001017 return BC_STATUS_SUCCESS;
1018 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001019 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001020 bc_num_copy(c, a);
1021 bc_num_retireMul(c, scale, a->neg, b->neg);
1022 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001023 }
Gavin Howardb651f1a2018-02-28 17:34:18 -07001024
Stefan Esser40b53d22019-04-29 12:56:32 +02001025 temp_scale = BC_MAX(scale, BC_BASE_POWER * (a->len + b->len + 1));
Gavin Howardb651f1a2018-02-28 17:34:18 -07001026
Stefan Esserb5aeb442019-04-27 23:33:08 +02001027 bc_num_createCopy(c, a);
Stefan Esser40b53d22019-04-29 12:56:32 +02001028 shift = bc_num_normalize(c);
Stefan Esserb5aeb442019-04-27 23:33:08 +02001029
1030 bc_num_createCopy(&b1, b);
Stefan Esser40b53d22019-04-29 12:56:32 +02001031 shift -= bc_num_normalize(&b1);
Stefan Esserb5aeb442019-04-27 23:33:08 +02001032
Stefan Esser6e129fc2019-04-27 23:41:21 +02001033 neg = c->neg != b1.neg;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001034 c->neg = false;
1035 b1.neg = false;
Stefan Esser70997152019-04-28 15:50:14 +02001036
Stefan Esser6e129fc2019-04-27 23:41:21 +02001037 cmp = bc_num_cmp(&b1, c);
Stefan Esserb5aeb442019-04-27 23:33:08 +02001038 if (cmp == 0) {
1039 bc_num_free(c);
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001040 bc_num_createFromUlong(c, 1); // identical BcNum arrays
Stefan Esserf76f0362019-04-29 21:28:52 +02001041 }
1042 else {
Stefan Esserb5aeb442019-04-27 23:33:08 +02001043 if (cmp > 0) //==> b > a, result will be one exp higher
1044 shift--;
1045
1046 dividend = 1;
1047 divisor = 0;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001048
Stefan Esser40b53d22019-04-29 12:56:32 +02001049 for (i = 0; i < 19 / BC_BASE_POWER; i++) {
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001050 dividend *= BC_BASE_DIG;
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001051 }
Stefan Esserb5aeb442019-04-27 23:33:08 +02001052
Stefan Esser40b53d22019-04-29 12:56:32 +02001053 mindivisor = bc_num_pow10((19 - BC_BASE_POWER)/2);
1054 if (BC_BASE_POWER % 2 != 0)
1055 mindivisor *= 3;
1056
1057 j = 0;
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001058 for (i = 0; i < b1.len; i++) {
Stefan Esser40b53d22019-04-29 12:56:32 +02001059 if (divisor < mindivisor) {
1060 divisor *= BC_BASE_DIG;
1061 divisor += b1.num[b1.len - 1 - i];
Stefan Esserf76f0362019-04-29 21:28:52 +02001062 }
1063 else {
Stefan Esser40b53d22019-04-29 12:56:32 +02001064 if (b1.num[b1.len - 1 - i] != 0)
1065 j = 1;
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001066 }
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001067 }
Stefan Esserb5aeb442019-04-27 23:33:08 +02001068 divisor += j;
Stefan Esser40b53d22019-04-29 12:56:32 +02001069
Stefan Esserb5aeb442019-04-27 23:33:08 +02001070 factor = dividend / divisor;
Stefan Esser40b53d22019-04-29 12:56:32 +02001071 if (j == 0 && factor * divisor != dividend)
1072 factor = dividend / (divisor + 1);
1073
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001074 bc_num_createFromUlong(&f, factor);
Stefan Esser40b53d22019-04-29 12:56:32 +02001075 bc_num_mul(&b1, &f, &b1, temp_scale);
Stefan Esser154b4732019-04-28 17:21:34 +02001076 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001077
Stefan Esser217379d2019-04-27 23:43:02 +02001078 if (b1.num[b1.len - 1] == 1) {
1079 b1.rdx = b1.len - 1; // only possible if b1 == 1.000000...
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001080 b1.scale = b1.rdx * BC_BASE_POWER;
Stefan Esserf76f0362019-04-29 21:28:52 +02001081 }
1082 else {
Stefan Esserb5aeb442019-04-27 23:33:08 +02001083 b1.rdx = b1.len;
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001084 b1.scale = b1.rdx * BC_BASE_POWER;
Stefan Esser40b53d22019-04-29 12:56:32 +02001085 s = bc_num_invert(&b1, temp_scale + BC_BASE_POWER);
Stefan Esser154b4732019-04-28 17:21:34 +02001086 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Esser40b53d22019-04-29 12:56:32 +02001087 bc_num_mul(&b1, c, c, temp_scale);
Stefan Esser154b4732019-04-28 17:21:34 +02001088 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001089 }
Stefan Esser40b53d22019-04-29 12:56:32 +02001090 bc_num_mul(&f, c, c, temp_scale);
Stefan Esser154b4732019-04-28 17:21:34 +02001091 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001092 c->rdx = c->len - 1;
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001093 c->scale = c->rdx * BC_BASE_POWER;
Gavin Howard63738202018-09-26 15:34:20 -06001094 }
Stefan Esserf76f0362019-04-29 21:28:52 +02001095
1096 if (shift > 0)
Stefan Esserb5aeb442019-04-27 23:33:08 +02001097 bc_num_shiftLeft(c, shift * BC_BASE_POWER);
Stefan Esserf76f0362019-04-29 21:28:52 +02001098 else
Stefan Esserb5aeb442019-04-27 23:33:08 +02001099 bc_num_shiftRight(c, -shift * BC_BASE_POWER);
Stefan Esserf76f0362019-04-29 21:28:52 +02001100
Stefan Esser40b53d22019-04-29 12:56:32 +02001101 bc_num_roundPlaces(c, scale +3); // <se> is this correct???
Stefan Esser193f8522019-04-27 01:29:01 +02001102err:
Gavin Howardf2ee6342019-04-09 17:05:20 -06001103 if (BC_SIG) s = BC_STATUS_SIGNAL;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001104 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, neg, false);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001105
Gavin Howard63738202018-09-26 15:34:20 -06001106 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001107}
1108
Gavin Howard1cbfe242019-01-09 17:13:11 -07001109static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
1110 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -06001111{
Gavin Howardfcf29012019-04-25 20:12:19 -06001112 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001113 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -06001114 BcNum temp;
1115 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001116
Gavin Howardddce6d42019-01-03 14:07:06 -07001117 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1118 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -07001119 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001120 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001121 return BC_STATUS_SUCCESS;
1122 }
Gavin Howard8b254872018-03-14 01:13:35 -06001123
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001124 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -07001125 s = bc_num_d(a, b, c, scale);
1126 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001127 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001128
Gavin Howard1ab22d22019-01-03 13:32:17 -07001129 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -06001130
Gavin Howard53eba8b2018-10-31 15:14:37 -06001131 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001132 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001133 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001134 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -06001135
Gavin Howard77445432019-04-26 15:59:33 -06001136 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
Gavin Howard18441082018-10-22 10:03:30 -06001137
1138 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001139 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -06001140 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001141
1142err:
Gavin Howard18441082018-10-22 10:03:30 -06001143 bc_num_free(&temp);
1144 return s;
1145}
1146
Gavin Howard2d188a52019-02-25 14:19:08 -07001147static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1148{
Gavin Howardfcf29012019-04-25 20:12:19 -06001149 // TODO: Check this function.
Gavin Howard18441082018-10-22 10:03:30 -06001150 BcStatus s;
1151 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -07001152 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -06001153
Gavin Howard77445432019-04-26 15:59:33 -06001154 ts = bc_vm_growSize(scale, b->scale);
1155 ts = BC_MAX(ts, a->scale);
Gavin Howard798508b2019-02-21 16:45:32 -07001156
1157 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -06001158 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001159 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -06001160
Gavin Howard63738202018-09-26 15:34:20 -06001161 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001162}
1163
Gavin Howard1cbfe242019-01-09 17:13:11 -07001164static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001165
Gavin Howardfcf29012019-04-25 20:12:19 -06001166 // TODO: Check this function.
1167
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001168 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001169 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -07001170 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001171 size_t i, powrdx, resrdx;
1172 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001173
Gavin Howardecafd4f2019-02-23 09:30:45 -07001174 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001175
Gavin Howardddce6d42019-01-03 14:07:06 -07001176 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -06001177 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -06001178 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001179 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001180 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001181 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001182 return BC_STATUS_SUCCESS;
1183 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001184 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001185 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -06001186 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001187 return s;
1188 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001189
Gavin Howard63738202018-09-26 15:34:20 -06001190 neg = b->neg;
1191 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001192 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -07001193 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001194 if (s) return s;
1195
Gavin Howardbaa4f582019-01-24 13:56:35 -07001196 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001197
Stefan Esserc66e4482019-04-29 14:24:31 +02001198 if (!neg) scale = BC_MIN(a->scale * pow, BC_MAX(scale, a->scale));
Gavin Howardb29674f2018-03-22 22:24:58 -06001199
Stefan Esserc66e4482019-04-29 14:24:31 +02001200 for (powrdx = a->scale; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -06001201 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001202 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001203 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001204 }
Gavin Howard6fb635f2018-03-03 12:45:42 -07001205
Gavin Howard2d188a52019-02-25 14:19:08 -07001206 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -07001207
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001208 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -07001209 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001210
Gavin Howard2d188a52019-02-25 14:19:08 -07001211 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001212
Gavin Howard4d5daba2019-01-07 14:42:50 -07001213 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001214 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001215 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001216
Gavin Howard63738202018-09-26 15:34:20 -06001217 if (pow & 1) {
1218 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001219 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001220 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001221 }
1222 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001223
Gavin Howard2d188a52019-02-25 14:19:08 -07001224 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -07001225 if (neg) {
1226 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001227 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -07001228 }
1229
Stefan Esserc66e4482019-04-29 14:24:31 +02001230 if (c->scale > scale) bc_num_truncate(c, c->scale - scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001231
Gavin Howard53eba8b2018-10-31 15:14:37 -06001232 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -06001233 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001234 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001235
Gavin Howard9b801632019-02-16 23:34:06 -07001236sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001237 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001238err:
Gavin Howard63738202018-09-26 15:34:20 -06001239 bc_num_free(&copy);
1240 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001241}
1242
Gavin Howard7bda4782018-12-28 09:53:22 -07001243#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -07001244static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
1245 size_t scale)
1246{
Gavin Howardac4d9122018-12-27 23:59:12 -07001247 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001248 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001249
1250 BC_UNUSED(scale);
1251
1252 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001253 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001254
Gavin Howardc9bef2d2019-04-26 14:46:52 -06001255 if (val < c->scale) bc_num_truncate(c, c->scale - val);
1256 else if (val > c->scale) bc_num_extend(c, val - c->scale);
Gavin Howardac4d9122018-12-27 23:59:12 -07001257
1258 return s;
1259}
1260
Gavin Howard2d188a52019-02-25 14:19:08 -07001261static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1262{
Gavin Howardac4d9122018-12-27 23:59:12 -07001263 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001264 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001265
1266 BC_UNUSED(scale);
1267
1268 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001269 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001270
Gavin Howardb1558662019-04-26 14:32:55 -06001271 return bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001272}
1273
Gavin Howard2d188a52019-02-25 14:19:08 -07001274static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1275 size_t scale)
1276{
Gavin Howardac4d9122018-12-27 23:59:12 -07001277 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001278 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001279
1280 BC_UNUSED(scale);
1281
1282 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001283 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001284
Gavin Howard2a366922019-01-16 10:50:20 -07001285 if (BC_NUM_ZERO(c)) return s;
1286
Gavin Howardb1558662019-04-26 14:32:55 -06001287 return bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001288}
Gavin Howard7bda4782018-12-28 09:53:22 -07001289#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001290
Gavin Howard1cbfe242019-01-09 17:13:11 -07001291static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1292 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001293{
Gavin Howarda1c44392018-09-27 12:41:15 -06001294 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001295 BcNum num2, *ptr_a, *ptr_b;
1296 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001297
Gavin Howard63738202018-09-26 15:34:20 -06001298 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001299
Gavin Howard890d0c02018-10-30 16:34:50 -06001300 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001301 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001302 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001303 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001304 }
1305 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001306
Gavin Howard63738202018-09-26 15:34:20 -06001307 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001308 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001309 if (c != a) {
1310 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001311 init = true;
1312 }
1313 }
1314 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001315
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001316 if (init) bc_num_init(c, req);
1317 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001318
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001319 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001320
Gavin Howardddce6d42019-01-03 14:07:06 -07001321 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001322 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001323
Gavin Howardba009802018-09-29 04:41:51 -06001324 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001325
Stefan Esser193f8522019-04-27 01:29:01 +02001326// bc_num_truncDecimals(c, scale);
1327
Gavin Howardee9d01e2019-02-16 22:48:11 -07001328 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001329}
1330
Gavin Howardc90ed902019-01-02 13:07:49 -07001331#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001332static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001333
Gavin Howardc7d655b2018-12-28 19:45:13 -07001334 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001335 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001336
Gavin Howard63738202018-09-26 15:34:20 -06001337 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001338
Gavin Howard63738202018-09-26 15:34:20 -06001339 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001340
Gavin Howard8a921bd2018-10-11 14:15:32 -06001341 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001342
Gavin Howard63738202018-09-26 15:34:20 -06001343 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001344
Gavin Howard63738202018-09-26 15:34:20 -06001345 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001346
Gavin Howard63738202018-09-26 15:34:20 -06001347 radix = true;
1348 continue;
1349 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001350
Gavin Howardc7d655b2018-12-28 19:45:13 -07001351 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001352 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001353
Gavin Howard63738202018-09-26 15:34:20 -06001354 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001355}
Gavin Howardc90ed902019-01-02 13:07:49 -07001356#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001357
Gavin Howard1cbfe242019-01-09 17:13:11 -07001358static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001359
Gavin Howardfcf29012019-04-25 20:12:19 -06001360 // TODO: Check this function.
1361
Gavin Howardc7d655b2018-12-28 19:45:13 -07001362 if (isupper(c)) {
1363 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001364 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001365 }
1366 else c -= '0';
1367
1368 return (unsigned long) (uchar) c;
1369}
1370
Gavin Howard1cbfe242019-01-09 17:13:11 -07001371static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001372
Gavin Howard3b60abb2019-04-26 08:49:17 -06001373 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001374 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001375 bool zero = true, rdx;
Stefan Essera299ffc2019-04-24 23:28:32 +02001376
Gavin Howard63738202018-09-26 15:34:20 -06001377 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001378
Gavin Howard63738202018-09-26 15:34:20 -06001379 val += i;
Gavin Howard3b60abb2019-04-26 08:49:17 -06001380 assert(isalnum(val[0]) || val[0] == '.');
Gavin Howard63738202018-09-26 15:34:20 -06001381 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001382
Gavin Howard63738202018-09-26 15:34:20 -06001383 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001384 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001385
Gavin Howardd43ec372019-04-25 21:13:54 -06001386 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001387
Gavin Howardd43ec372019-04-25 21:13:54 -06001388 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001389 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001390
Gavin Howard3b60abb2019-04-26 08:49:17 -06001391 i = len - (ptr == val ? 0 : i) - rdx;
Gavin Howard48a0e472019-04-26 15:39:46 -06001392 temp = BC_NUM_ROUND_POW(i);
Gavin Howard3b60abb2019-04-26 08:49:17 -06001393 mod = n->scale % BC_BASE_POWER;
1394 i = mod ? BC_BASE_POWER - mod : 0;
1395 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001396
1397 bc_num_expand(n, n->len);
Gavin Howard404ece72019-04-26 15:59:15 -06001398 memset(n->num, 0, BC_NUM_SIZE(n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -07001399
Gavin Howard6076dbb2019-04-26 09:47:50 -06001400 if (zero) n->len = n->rdx = 0;
1401 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001402
Gavin Howard971a2672019-04-26 14:32:07 -06001403 unsigned long exp, pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001404
Gavin Howard3b60abb2019-04-26 08:49:17 -06001405 exp = i;
Gavin Howard971a2672019-04-26 14:32:07 -06001406 pow = bc_num_pow10(exp);
Gavin Howardd43ec372019-04-25 21:13:54 -06001407
1408 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001409
Gavin Howardc7d655b2018-12-28 19:45:13 -07001410 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001411
Gavin Howardd43ec372019-04-25 21:13:54 -06001412 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001413 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001414
1415 size_t idx = exp / BC_BASE_POWER;
1416
Gavin Howard8dd307e2019-01-08 23:05:19 -07001417 if (isupper(c)) c = '9';
Gavin Howardf196fbe2019-04-26 08:49:05 -06001418 n->num[idx] += (((unsigned long) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001419
1420 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1421 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001422 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001423 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001424 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001425}
1426
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001427static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1428 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001429{
Gavin Howardfcf29012019-04-25 20:12:19 -06001430 // TODO: Check this function.
Gavin Howard1cbfe242019-01-09 17:13:11 -07001431 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001432 BcNum temp, mult, result;
Gavin Howard77445432019-04-26 15:59:33 -06001433 char c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001434 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001435 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001436 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001437
Gavin Howard11b9afd2018-10-18 14:23:28 -06001438 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001439 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001440
Gavin Howard26d078f2019-01-24 13:58:12 -07001441 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1442 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001443
Gavin Howardc7d655b2018-12-28 19:45:13 -07001444 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001445
Gavin Howardc7d655b2018-12-28 19:45:13 -07001446 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001447
Gavin Howard53eba8b2018-10-31 15:14:37 -06001448 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001449 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001450 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001451 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001452 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001453 }
Gavin Howardede51f02018-03-02 12:30:00 -07001454
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001455 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001456
Gavin Howard63738202018-09-26 15:34:20 -06001457 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001458 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001459 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001460
Gavin Howard2d188a52019-02-25 14:19:08 -07001461 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001462 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001463 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001464
Gavin Howard53eba8b2018-10-31 15:14:37 -06001465 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001466 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001467
Gavin Howard705c4bc2018-12-03 15:38:04 -07001468 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001469 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001470 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001471 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001472 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001473 }
Gavin Howardede51f02018-03-02 12:30:00 -07001474
Gavin Howardf2ee6342019-04-09 17:05:20 -06001475 if (BC_SIG) {
1476 s = BC_STATUS_SIGNAL;
1477 goto err;
1478 }
1479
Gavin Howard1769abc2019-02-21 09:36:14 -07001480 // This one cannot be a divide by 0 because mult starts out at 1, then is
1481 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1482 s = bc_num_div(&result, &mult, &result, digs);
1483 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001484 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001485 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001486 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001487
Gavin Howardddce6d42019-01-03 14:07:06 -07001488 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001489 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001490 }
1491 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001492
Gavin Howardd1417682019-02-15 17:08:42 -07001493err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001494 bc_num_free(&result);
1495int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001496 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001497 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001498 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001499}
1500
Gavin Howard1cbfe242019-01-09 17:13:11 -07001501static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001502 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001503 bc_vm_putchar('\\');
1504 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001505 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001506 }
Gavin Howard80977b22018-09-06 14:29:13 -06001507}
1508
Gavin Howard40a085f2018-12-03 12:08:59 -07001509#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001510static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001511 BC_UNUSED(rdx);
1512 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001513 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001514}
1515#endif // DC_ENABLED
1516
Gavin Howard1cbfe242019-01-09 17:13:11 -07001517static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1518
Gavin Howardfcf29012019-04-25 20:12:19 -06001519 // TODO: Check this function.
1520
Gavin Howarda84ad992018-12-03 19:11:06 -07001521 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001522
Gavin Howardab5e9632019-01-02 13:32:02 -07001523 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001524 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001525 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001526
Gavin Howardab5e9632019-01-02 13:32:02 -07001527 bc_num_printNewline();
Gavin Howard530e3072019-04-23 16:23:36 -06001528 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001529
Gavin Howard530e3072019-04-23 16:23:36 -06001530 for (exp = 0; exp < len; pow /= BC_BASE, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001531 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001532 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001533 dig = n / pow;
1534 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001535 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001536 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001537}
Gavin Howardfe679f02018-02-14 15:50:09 -07001538
Gavin Howard1cbfe242019-01-09 17:13:11 -07001539static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001540
Gavin Howard3c02ed32018-12-28 18:17:15 -07001541 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001542
Gavin Howard3c02ed32018-12-28 18:17:15 -07001543 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001544 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001545 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001546 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001547 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001548
Gavin Howardab5e9632019-01-02 13:32:02 -07001549 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001550 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001551 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001552}
1553
Gavin Howard1cbfe242019-01-09 17:13:11 -07001554static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001555
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001556 size_t i, j, rdx = n->rdx;
1557 bool zero = true;
1558 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001559
Gavin Howard48af52e2018-10-30 14:47:38 -06001560 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001561 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001562
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001563 for (i = n->len - 1; i < n->len; --i) {
1564
1565 BcDig n9 = n->num[i];
1566 size_t temp;
1567 bool irdx = (i == rdx - 1);
1568
1569 zero = (zero & !irdx);
1570 temp = n->scale % BC_BASE_POWER;
1571 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1572
1573 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1574
1575 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Essera299ffc2019-04-24 23:28:32 +02001576 buffer[j] = n9 % BC_BASE;
1577 n9 /= BC_BASE;
1578 }
Stefan Esser0da17752019-04-25 12:25:15 +02001579
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001580 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1581 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1582 zero = (zero && buffer[j] == 0);
1583 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Essera299ffc2019-04-24 23:28:32 +02001584 }
1585 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001586}
1587
Gavin Howard7ad5a662019-02-19 14:40:46 -07001588#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001589static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) {
Gavin Howard7ad5a662019-02-19 14:40:46 -07001590
Gavin Howardfcf29012019-04-25 20:12:19 -06001591 // TODO: Check this function.
1592
Gavin Howardb1558662019-04-26 14:32:55 -06001593 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001594 bool neg = (n->len <= n->rdx);
1595 BcNum temp, exp;
1596 size_t places, mod;
1597 BcDig digs[BC_NUM_LONG_LOG10];
1598
1599 bc_num_createCopy(&temp, n);
1600
1601 if (neg) {
1602 places = n->rdx - bc_num_len(n) + 1;
1603 mod = places % 3;
1604 if (eng && mod != 0) places += 3 - mod;
Gavin Howardb1558662019-04-26 14:32:55 -06001605 s = bc_num_shiftLeft(&temp, places);
1606 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001607 }
1608 else {
Stefan Essercc7cd932019-04-29 17:10:43 +02001609 places = bc_num_int_digits(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001610 mod = places % 3;
1611 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howardb1558662019-04-26 14:32:55 -06001612 s = bc_num_shiftRight(&temp, places);
1613 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001614 }
1615
1616 bc_num_printDecimal(&temp);
1617
1618 bc_num_printNewline();
1619 bc_vm_putchar('e');
1620
1621 if (!places) {
1622 bc_num_printHex(0, 1, false);
1623 goto exit;
1624 }
1625
1626 if (neg) {
1627 bc_num_printNewline();
1628 bc_vm_putchar('-');
1629 }
1630
1631 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1632 bc_num_ulong2num(&exp, (unsigned long) places);
1633
1634 bc_num_printDecimal(&exp);
1635
1636exit:
1637 bc_num_free(&temp);
Gavin Howardb1558662019-04-26 14:32:55 -06001638 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001639}
1640#endif // BC_ENABLE_EXTRA_MATH
1641
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001642static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1643 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001644{
Gavin Howardfcf29012019-04-25 20:12:19 -06001645 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001646 BcStatus s;
1647 BcVec stack;
1648 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001649 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001650 size_t i;
1651 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001652
Gavin Howard971a2672019-04-26 14:32:07 -06001653 assert(BC_NUM_NONZERO(base));
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001654
Gavin Howardddce6d42019-01-03 14:07:06 -07001655 if (BC_NUM_ZERO(n)) {
1656 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001657 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001658 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001659
Gavin Howardd3929832018-12-24 15:13:15 -07001660 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001661 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001662 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001663 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001664 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001665 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001666
Gavin Howard63738202018-09-26 15:34:20 -06001667 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001668 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001669 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001670
Gavin Howard2d188a52019-02-25 14:19:08 -07001671 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001672
1673 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001674 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001675 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001676
1677 // Will never fail (except for signals) because digit is
1678 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001679 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001680 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001681
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001682 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001683 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001684
Gavin Howard2d188a52019-02-25 14:19:08 -07001685 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001686
Gavin Howard2d188a52019-02-25 14:19:08 -07001687 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001688 ptr = bc_vec_item_rev(&stack, i);
1689 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001690 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001691 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001692
Gavin Howard2d188a52019-02-25 14:19:08 -07001693 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001694 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001695
Gavin Howard2d188a52019-02-25 14:19:08 -07001696 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001697
Gavin Howard53eba8b2018-10-31 15:14:37 -06001698 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001699 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001700
1701 // Will never fail (except for signals) because fracp is
1702 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001703 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001704 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001705
Gavin Howard705c4bc2018-12-03 15:38:04 -07001706 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001707 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001708 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001709
Gavin Howardddce6d42019-01-03 14:07:06 -07001710 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001711 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001712 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001713 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001714
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001715sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001716 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001717err:
Gavin Howard63738202018-09-26 15:34:20 -06001718 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001719 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001720 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001721 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001722 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001723 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001724}
1725
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001726static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1727 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001728{
Gavin Howardfcf29012019-04-25 20:12:19 -06001729 // TODO: Check this function.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001730 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001731 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001732 BcNumDigitOp print;
1733 bool neg = n->neg;
1734
Gavin Howard48af52e2018-10-30 14:47:38 -06001735 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001736 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001737
1738 n->neg = false;
1739
Gavin Howardbe4e8222019-01-03 13:34:16 -07001740 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001741 width = 1;
1742 print = bc_num_printHex;
1743 }
1744 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001745 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001746 print = bc_num_printDigits;
1747 }
1748
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001749 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001750 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001751
1752 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001753}
1754
Gavin Howard40a085f2018-12-03 12:08:59 -07001755#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001756BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1757 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001758}
1759#endif // DC_ENABLED
1760
Gavin Howard3c02ed32018-12-28 18:17:15 -07001761void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001762 assert(n);
1763 n->num = num;
1764 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001765 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001766 n->neg = false;
1767}
1768
Gavin Howard3c02ed32018-12-28 18:17:15 -07001769void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001770 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001771 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howard404ece72019-04-26 15:59:15 -06001772 bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001773}
1774
Gavin Howarded392aa2018-02-27 13:09:26 -07001775void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001776 assert(num);
1777 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001778}
1779
Gavin Howard3c02ed32018-12-28 18:17:15 -07001780void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001781 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001782 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001783 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001784 d->len = s->len;
1785 d->neg = s->neg;
1786 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001787 d->scale = s->scale;
Gavin Howard404ece72019-04-26 15:59:15 -06001788 memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -07001789}
1790
Gavin Howardbaa4f582019-01-24 13:56:35 -07001791void bc_num_createCopy(BcNum *d, const BcNum *s) {
1792 bc_num_init(d, s->len);
1793 bc_num_copy(d, s);
1794}
1795
1796void bc_num_createFromUlong(BcNum *n, unsigned long val) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001797 bc_num_init(n, (BC_NUM_LONG_LOG10 - 1) / BC_BASE_POWER + 1);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001798 bc_num_ulong2num(n, val);
1799}
1800
Gavin Howard7ad5a662019-02-19 14:40:46 -07001801size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001802 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001803}
1804
1805size_t bc_num_len(const BcNum *restrict n) {
1806
Gavin Howardb41483b2019-04-27 08:30:10 -06001807 size_t i, pow, scale, len = n->len;
Gavin Howardfcf29012019-04-25 20:12:19 -06001808 BcDig dig;
1809
Gavin Howardb41483b2019-04-27 08:30:10 -06001810 if (BC_NUM_ZERO(n)) return 0;
1811
1812 if (n->rdx == len) {
1813 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1814 }
Gavin Howard7ad5a662019-02-19 14:40:46 -07001815
Gavin Howardfcf29012019-04-25 20:12:19 -06001816 dig = n->num[len - 1];
Gavin Howardb41483b2019-04-27 08:30:10 -06001817 pow = BC_BASE_DIG;
1818 i = BC_BASE_POWER + 1;
Gavin Howardfcf29012019-04-25 20:12:19 -06001819
Gavin Howardb41483b2019-04-27 08:30:10 -06001820 while (pow && (dig % (BcDig) pow == dig)) {
1821 i -= 1;
1822 pow /= BC_BASE;
1823 }
Gavin Howardfcf29012019-04-25 20:12:19 -06001824
Gavin Howardb41483b2019-04-27 08:30:10 -06001825 scale = n->scale % BC_BASE_POWER;
1826 scale = scale ? scale : BC_BASE_POWER;
1827
1828 return (len - 1) * BC_BASE_POWER + i - (BC_BASE_POWER - scale);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001829}
1830
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001831BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1832 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001833{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001834 BcStatus s = BC_STATUS_SUCCESS;
1835
Gavin Howard63738202018-09-26 15:34:20 -06001836 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001837 assert(BC_ENABLE_EXTRA_MATH ||
1838 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001839 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001840
Gavin Howardd746e802018-12-28 22:06:00 -07001841 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06001842 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001843 else s = bc_num_parseBase(n, val, base, base_t);
1844
1845 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001846}
1847
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001848BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1849 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001850{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001851 BcStatus s = BC_STATUS_SUCCESS;
1852
Gavin Howardab5e9632019-01-02 13:32:02 -07001853 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001854 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001855
Gavin Howardab5e9632019-01-02 13:32:02 -07001856 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001857
Gavin Howardddce6d42019-01-03 14:07:06 -07001858 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06001859 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001860#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001861 else if (base_t == 0 || base_t == 1)
1862 s = bc_num_printExponent(n, base_t != 0);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001863#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001864 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001865
Gavin Howarde5f11c72019-02-23 09:42:51 -07001866 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001867 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001868 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001869 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001870
1871 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001872}
1873
Gavin Howard3c02ed32018-12-28 18:17:15 -07001874BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001875
Gavin Howard63738202018-09-26 15:34:20 -06001876 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001877 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001878
Gavin Howard63738202018-09-26 15:34:20 -06001879 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001880
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001881 *result = 0;
1882
Gavin Howardecafd4f2019-02-23 09:30:45 -07001883 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001884
Gavin Howard06b6e862019-02-15 11:15:31 -07001885 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001886
Gavin Howard0700efc2019-04-24 17:31:23 -06001887 unsigned long prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07001888
Gavin Howard0700efc2019-04-24 17:31:23 -06001889 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001890 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1891
Gavin Howard77445432019-04-26 15:59:33 -06001892 r = prev + (unsigned long) n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07001893
1894 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001895 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001896 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001897
Gavin Howard7b557da2018-12-21 10:25:32 -07001898 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001899
Gavin Howard63738202018-09-26 15:34:20 -06001900 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001901}
1902
Gavin Howard3c02ed32018-12-28 18:17:15 -07001903void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001904
Gavin Howardc25fd612019-04-26 19:31:31 -06001905 BcDig *ptr;
1906 unsigned long i;
1907
Gavin Howard63738202018-09-26 15:34:20 -06001908 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001909
Gavin Howard63738202018-09-26 15:34:20 -06001910 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001911
Gavin Howard1ab22d22019-01-03 13:32:17 -07001912 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001913
Gavin Howard798508b2019-02-21 16:45:32 -07001914 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howard05feab02019-04-23 16:24:07 -06001915
Gavin Howardc25fd612019-04-26 19:31:31 -06001916 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_DIG)
1917 ptr[i] = val % BC_BASE_DIG;
Gavin Howard025d04d2018-02-20 13:53:28 -07001918}
1919
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001920size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001921
1922 size_t aint, bint, ardx, brdx;
1923
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001924 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001925
1926 ardx = a->rdx;
1927 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001928 aint = bc_num_int(a);
1929 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001930 ardx = BC_MAX(ardx, brdx);
1931 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001932
Gavin Howard2d188a52019-02-25 14:19:08 -07001933 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001934}
1935
1936size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001937 size_t max, rdx;
1938 rdx = bc_vm_growSize(a->rdx, b->rdx);
Stefan Essercc7cd932019-04-29 17:10:43 +02001939 max = bc_vm_growSize(BC_MAX(BC_NUM_RDX(scale), rdx), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001940 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1941 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001942}
1943
1944size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1945 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001946 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001947}
1948
1949#if BC_ENABLE_EXTRA_MATH
1950size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1951 BC_UNUSED(b);
1952 BC_UNUSED(scale);
1953 return BC_NUM_SHREQ(a);
1954}
1955#endif // BC_ENABLE_EXTRA_MATH
1956
Gavin Howard12fe7812018-09-29 03:45:18 -06001957BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001958 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001959 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001960 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001961}
1962
Gavin Howard12fe7812018-09-29 03:45:18 -06001963BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001964 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001965 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001966 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001967}
1968
Gavin Howard12fe7812018-09-29 03:45:18 -06001969BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001970 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001971}
1972
Gavin Howard12fe7812018-09-29 03:45:18 -06001973BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001974 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001975}
1976
Gavin Howard54b946a2018-10-23 12:06:57 -06001977BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001978 size_t req = bc_num_mulReq(a, b, scale);
1979 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001980}
1981
Gavin Howard12fe7812018-09-29 03:45:18 -06001982BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001983 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001984}
1985
Gavin Howard7d8f0382018-12-27 16:15:13 -07001986#if BC_ENABLE_EXTRA_MATH
1987BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001988 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001989}
1990
1991BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001992 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001993}
1994
1995BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001996 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001997}
1998#endif // BC_ENABLE_EXTRA_MATH
1999
Gavin Howard3c02ed32018-12-28 18:17:15 -07002000BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06002001
Gavin Howardfcf29012019-04-25 20:12:19 -06002002 // TODO: Check this function.
2003
Gavin Howard1cbfe242019-01-09 17:13:11 -07002004 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002005 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Stefan Esser62769362019-04-29 15:06:00 +02002006 size_t pow, len, rdx, req, digs, digs1, digs2, resscale, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002007 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Stefan Essercc7cd932019-04-29 17:10:43 +02002008 BcDig half_digs[1];
Gavin Howard954276b2018-05-16 01:43:58 -06002009
Gavin Howard34edd0a2018-10-25 15:51:05 -06002010 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06002011
Gavin Howardecafd4f2019-02-23 09:30:45 -07002012 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07002013
Stefan Esser1f0f69f2019-04-29 20:49:15 +02002014 if (a->scale > scale) scale = a->scale;
2015 len = bc_vm_growSize(bc_num_int_digits(a), 1);
Stefan Esser62769362019-04-29 15:06:00 +02002016 req = bc_vm_growSize(BC_MAX(BC_NUM_RDX(scale), a->rdx), len >> 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07002017 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06002018
Gavin Howardddce6d42019-01-03 14:07:06 -07002019 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06002020 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002021 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06002022 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07002023 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06002024 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002025 bc_num_extend(b, scale);
2026 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06002027 }
Gavin Howard954276b2018-05-16 01:43:58 -06002028
Stefan Esser62769362019-04-29 15:06:00 +02002029 rdx = BC_MAX(BC_NUM_RDX(scale), a->rdx);
2030 len = bc_vm_growSize(a->len, rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06002031
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002032 bc_num_init(&num1, len);
2033 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07002034 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06002035
Gavin Howard63738202018-09-26 15:34:20 -06002036 bc_num_one(&half);
Stefan Esser62769362019-04-29 15:06:00 +02002037 half.num[0] = BC_BASE_DIG / 2;
Stefan Essercc7cd932019-04-29 17:10:43 +02002038 half.len = 1;
Gavin Howard63738202018-09-26 15:34:20 -06002039 half.rdx = 1;
Stefan Esser62769362019-04-29 15:06:00 +02002040 half.scale = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06002041
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002042 bc_num_init(&f, len);
2043 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06002044
Gavin Howard63738202018-09-26 15:34:20 -06002045 x0 = &num1;
2046 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06002047
Gavin Howard63738202018-09-26 15:34:20 -06002048 bc_num_one(x0);
Stefan Essercc7cd932019-04-29 17:10:43 +02002049 pow = bc_num_int_digits(a);
Gavin Howard954276b2018-05-16 01:43:58 -06002050
Gavin Howard53eba8b2018-10-31 15:14:37 -06002051 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06002052
Gavin Howardc39fd492018-10-04 10:07:03 -06002053 if (pow & 1) x0->num[0] = 2;
2054 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06002055
Gavin Howardc39fd492018-10-04 10:07:03 -06002056 pow -= 2 - (pow & 1);
Stefan Essercc7cd932019-04-29 17:10:43 +02002057 bc_num_shiftLeft(x0, pow / 2);
Gavin Howarded742ce2018-08-29 14:23:12 -06002058
Gavin Howard63738202018-09-26 15:34:20 -06002059 // Make sure to move the radix back.
Stefan Essercc7cd932019-04-29 17:10:43 +02002060 x0->scale -= pow;
2061 x0->rdx = BC_NUM_RDX(x0->scale);
Gavin Howard63738202018-09-26 15:34:20 -06002062 }
Gavin Howard954276b2018-05-16 01:43:58 -06002063
Gavin Howard14913fc2019-04-23 14:36:03 -06002064 x0->rdx = digs = digs1 = digs2 = 0;
Stefan Esser62769362019-04-29 15:06:00 +02002065 x0->scale = 0;
Stefan Esser8f7c5a52019-04-29 20:13:51 +02002066 resscale = (scale + BC_BASE_POWER) * 2;
Stefan Essercc7cd932019-04-29 17:10:43 +02002067
2068 len = BC_NUM_RDX(bc_num_int_digits(x0) + resscale - 1); // <se> apply -1 after BC_NUM_RDX???
Gavin Howard954276b2018-05-16 01:43:58 -06002069
Gavin Howard2d188a52019-02-25 14:19:08 -07002070 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06002071
Gavin Howard971a2672019-04-26 14:32:07 -06002072 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07002073
Stefan Esser62769362019-04-29 15:06:00 +02002074 s = bc_num_div(a, x0, &f, resscale);
Gavin Howard1769abc2019-02-21 09:36:14 -07002075 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002076 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Esser62769362019-04-29 15:06:00 +02002077 s = bc_num_add(x0, &f, &fprime, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07002078 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Esser62769362019-04-29 15:06:00 +02002079 s = bc_num_mul(&fprime, &half, x1, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07002080 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06002081
Gavin Howard53eba8b2018-10-31 15:14:37 -06002082 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06002083 if (cmp == BC_NUM_SSIZE_MIN) {
2084 s = BC_STATUS_SIGNAL;
2085 break;
2086 }
2087
Gavin Howard53eba8b2018-10-31 15:14:37 -06002088 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06002089
Gavin Howardb5ec7f32018-10-29 12:32:39 -06002090 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06002091 else times = 0;
2092
Stefan Esser8f7c5a52019-04-29 20:13:51 +02002093 resscale += times > 2;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002094
2095 cmp2 = cmp1;
2096 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06002097 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002098
Gavin Howard63738202018-09-26 15:34:20 -06002099 temp = x0;
2100 x0 = x1;
2101 x1 = temp;
2102 }
Gavin Howard954276b2018-05-16 01:43:58 -06002103
Gavin Howard2d188a52019-02-25 14:19:08 -07002104 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07002105 s = BC_STATUS_SIGNAL;
2106 goto err;
2107 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06002108
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002109 bc_num_copy(b, x0);
Stefan Esser62769362019-04-29 15:06:00 +02002110 if (b->scale > scale) bc_num_truncate(b, b->scale - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06002111
2112err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06002113 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06002114 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06002115 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06002116 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06002117 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07002118 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002119 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06002120 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07002121}
Gavin Howardba009802018-09-29 04:41:51 -06002122
Gavin Howard2cb39612018-10-22 08:46:48 -06002123BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
2124
Gavin Howardfcf29012019-04-25 20:12:19 -06002125 // TODO: Check this function.
2126
Gavin Howard2cb39612018-10-22 08:46:48 -06002127 BcStatus s;
2128 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06002129 bool init = false;
Stefan Esser1f0f69f2019-04-29 20:49:15 +02002130 size_t ts = BC_MAX(scale + b->scale, a->scale);
Stefan Essercc7cd932019-04-29 17:10:43 +02002131 size_t len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002132
Gavin Howardf2ef2f32019-01-16 11:25:16 -07002133 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06002134
Gavin Howard890d0c02018-10-30 16:34:50 -06002135 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06002136 memcpy(&num2, c, sizeof(BcNum));
2137 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002138 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06002139 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06002140 }
Gavin Howardf679ad82018-10-29 12:26:30 -06002141 else {
2142 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002143 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06002144 }
Gavin Howard2cb39612018-10-22 08:46:48 -06002145
Gavin Howard54b946a2018-10-23 12:06:57 -06002146 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002147
Gavin Howardddce6d42019-01-03 14:07:06 -07002148 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002149 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07002150 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002151 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06002152
2153 if (init) bc_num_free(&num2);
2154
2155 return s;
2156}
2157
Gavin Howard40a085f2018-12-03 12:08:59 -07002158#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06002159BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06002160
Gavin Howardfcf29012019-04-25 20:12:19 -06002161 // TODO: Check this function.
2162
Gavin Howardba009802018-09-29 04:41:51 -06002163 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002164 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07002165 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06002166
Gavin Howard34edd0a2018-10-25 15:51:05 -06002167 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06002168
Gavin Howardecafd4f2019-02-23 09:30:45 -07002169 if (BC_ERR(BC_NUM_ZERO(c)))
2170 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
2171 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
2172 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07002173 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06002174
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002175 bc_num_expand(d, c->len);
2176 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07002177 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002178 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06002179
2180 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06002181 two.num[0] = 2;
2182 bc_num_one(d);
2183
Gavin Howard1769abc2019-02-21 09:36:14 -07002184 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002185 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002186 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002187 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07002188 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06002189
Gavin Howard2d188a52019-02-25 14:19:08 -07002190 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06002191
Gavin Howard1769abc2019-02-21 09:36:14 -07002192 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002193 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002194 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002195
Gavin Howarda83a5e52019-02-21 16:51:06 -07002196 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06002197 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002198 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002199
2200 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002201 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002202 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002203 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002204 }
2205
Gavin Howard53eba8b2018-10-31 15:14:37 -06002206 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002207 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002208
2209 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002210 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002211 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002212 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002213 }
2214
Gavin Howard2d188a52019-02-25 14:19:08 -07002215 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002216
Gavin Howardba009802018-09-29 04:41:51 -06002217err:
Gavin Howardba009802018-09-29 04:41:51 -06002218 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002219rem_err:
2220 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002221 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002222 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002223 return s;
2224}
Gavin Howarde6e84762018-10-03 11:46:34 -06002225#endif // DC_ENABLED