blob: 67c7e2eacadce0e1ce7ef15d6c9a7ee08f89c238 [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 Howard49ea7b62019-04-23 14:46:26 -060048static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
49
Gavin Howard56ebcf72019-02-21 16:59:48 -070050static ssize_t bc_num_neg(size_t n, bool neg) {
51 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
52}
53
Gavin Howardd2cf06a2019-02-21 17:03:24 -070054ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -070055 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -070056}
57
Gavin Howard7b7d2f32019-02-21 16:46:47 -070058static size_t bc_num_int(const BcNum *n) {
59 return n->len ? n->len - n->rdx : 0;
60}
61
Gavin Howarda83a5e52019-02-21 16:51:06 -070062static bool bc_num_isOne(const BcNum *n) {
63 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
64}
65
Gavin Howard798508b2019-02-21 16:45:32 -070066static void bc_num_expand(BcNum *restrict n, size_t req) {
67 assert(n);
68 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
69 if (req > n->cap) {
Gavin Howard404ece72019-04-26 15:59:15 -060070 n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req));
Gavin Howard798508b2019-02-21 16:45:32 -070071 n->cap = req;
72 }
73}
74
Gavin Howard1cbfe242019-01-09 17:13:11 -070075static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060076 assert(n);
Gavin Howardfcf29012019-04-25 20:12:19 -060077 n->scale = scale;
78 n->len = n->rdx = 0;
Gavin Howard97102b12018-11-01 23:37:31 -060079 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060080}
81
Gavin Howard1cbfe242019-01-09 17:13:11 -070082static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060083 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060084}
85
Gavin Howard3c02ed32018-12-28 18:17:15 -070086void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060087 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060088 n->len = 1;
89 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060090}
91
Gavin Howard3c02ed32018-12-28 18:17:15 -070092void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060093 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060094 bc_num_setToZero(n, 0);
Gavin Howardd7e4d6e2019-04-24 17:33:48 -060095#if BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -060096 n->len = 2;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -060097 n->num[0] = 0;
98 n->num[1] = 1;
99#else // BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600100 n->len = 1;
Stefan Eßer4b111c22019-04-24 17:23:08 -0600101 n->num[0] = BC_BASE;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600102#endif // BC_BASE_DIG == 10
Gavin Howard0be26ed2018-08-31 20:21:56 -0600103}
104
Gavin Howard1cbfe242019-01-09 17:13:11 -0700105static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -0700106 size_t len;
Gavin Howard530e3072019-04-23 16:23:36 -0600107 for (len = 1; i; i /= BC_BASE, ++len);
Gavin Howardc1349922019-04-30 20:48:09 -0600108 return len - 1;
Gavin Howard1ab22d22019-01-03 13:32:17 -0700109}
110
Gavin Howard445c1892019-04-29 13:50:24 -0600111static size_t bc_num_int_digits(const BcNum *n) {
112
113 size_t digits;
114
115 digits = bc_num_int(n) * BC_BASE_POWER;
116
117 if (digits > 0)
Gavin Howardc1349922019-04-30 20:48:09 -0600118 digits -= BC_BASE_POWER - bc_num_log10((size_t) n->num[n->len - 1]);
Gavin Howard445c1892019-04-29 13:50:24 -0600119
120 return digits;
121}
122
Stefan Esser914a7f62019-05-05 22:00:21 +0200123#define POW10N 9
Stefan Esser72109f42019-04-28 21:32:13 +0200124
125static unsigned long pow10[POW10N] = {
Stefan Esser72109f42019-04-28 21:32:13 +0200126 10,
127 100,
128 1000,
129 10000,
130 100000,
131 1000000,
132 10000000,
133 100000000,
134 1000000000,
135};
136
Gavin Howard971a2672019-04-26 14:32:07 -0600137static unsigned long bc_num_pow10(unsigned long i) {
Stefan Esser33a1c682019-04-29 13:00:23 +0200138 if (i == 0) return 1;
Stefan Esser914a7f62019-05-05 22:00:21 +0200139 i--;
Stefan Esser72109f42019-04-28 21:32:13 +0200140 if (i < POW10N) return pow10[i];
Stefan Esser33a1c682019-04-29 13:00:23 +0200141 i -= POW10N;
Gavin Howard76eb0a62019-05-06 15:53:49 -0600142 assert(i < POW10N);
143 return pow10[POW10N - 1] * pow10[i];
Stefan Esser6167e222019-05-02 10:43:21 +0200144}
Stefan Esser45342832019-05-02 10:43:02 +0200145
Gavin Howardc1349922019-04-30 20:48:09 -0600146static size_t bc_num_nonzeroLen(const BcNum *restrict n) {
Gavin Howard08f78562019-04-29 07:56:04 -0600147 size_t i, len = n->len;
148 assert(len == n->rdx);
149 for (i = len - 1; i < n->len && !n->num[i]; --len, --i);
Gavin Howardc1349922019-04-30 20:48:09 -0600150 assert(len > 0);
Gavin Howard08f78562019-04-29 07:56:04 -0600151 return len;
152}
153
Gavin Howardfcf29012019-04-25 20:12:19 -0600154static unsigned long bc_num_addDigit(BcDig *restrict num, unsigned long d,
155 unsigned long c)
Gavin Howard14c354a2019-04-24 14:28:24 -0600156{
157 d += c;
Gavin Howard0700efc2019-04-24 17:31:23 -0600158 *num = (BcDig) (d % BC_BASE_DIG);
159 assert(*num >= 0 && *num < BC_BASE_DIG);
160 return d / BC_BASE_DIG;
Gavin Howard14c354a2019-04-24 14:28:24 -0600161}
162
163static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
164 size_t len)
165{
166 size_t i;
Gavin Howard971a2672019-04-26 14:32:07 -0600167 unsigned long carry = 0;
Gavin Howard14c354a2019-04-24 14:28:24 -0600168
169 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard971a2672019-04-26 14:32:07 -0600170 unsigned long in = ((unsigned long) a[i]) + ((unsigned long) b[i]);
Gavin Howard14c354a2019-04-24 14:28:24 -0600171 carry = bc_num_addDigit(a + i, in, carry);
172 }
173
174 for (; BC_NO_SIG && carry; ++i)
Gavin Howard971a2672019-04-26 14:32:07 -0600175 carry = bc_num_addDigit(a + i, (unsigned long) a[i], carry);
Gavin Howard14c354a2019-04-24 14:28:24 -0600176
177 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
178}
179
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700180static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
181 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700182{
Gavin Howard63738202018-09-26 15:34:20 -0600183 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600184
Gavin Howard2d188a52019-02-25 14:19:08 -0700185 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600186
Gavin Howard2d188a52019-02-25 14:19:08 -0700187 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600188 assert(a[i + j] >= -BC_BASE_DIG);
189 a[i + j++] += BC_BASE_DIG;
Gavin Howard54b946a2018-10-23 12:06:57 -0600190 a[i + j] -= 1;
Gavin Howard0700efc2019-04-24 17:31:23 -0600191 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_DIG);
Gavin Howard63738202018-09-26 15:34:20 -0600192 }
193 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600194
Gavin Howard2d188a52019-02-25 14:19:08 -0700195 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600196}
197
Gavin Howard2d188a52019-02-25 14:19:08 -0700198static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
199 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700200{
Gavin Howard63738202018-09-26 15:34:20 -0600201 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600202 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700203 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600204 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600205}
206
Gavin Howard3c02ed32018-12-28 18:17:15 -0700207ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700208
Gavin Howard63738202018-09-26 15:34:20 -0600209 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600210 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600211 bool a_max, neg = false;
212 ssize_t cmp;
213
214 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700215
Gavin Howard8a921bd2018-10-11 14:15:32 -0600216 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700217 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700218 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600219 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600220 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600221 else return -1;
222 }
223 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700224
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700225 a_int = bc_num_int(a);
226 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600227 a_int -= b_int;
228 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700229
Gavin Howard1ab22d22019-01-03 13:32:17 -0700230 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700231
Gavin Howard890d0c02018-10-30 16:34:50 -0600232 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600233 min = b->rdx;
234 diff = a->rdx - b->rdx;
235 max_num = a->num + diff;
236 min_num = b->num;
237 }
238 else {
239 min = a->rdx;
240 diff = b->rdx - a->rdx;
241 max_num = b->num + diff;
242 min_num = a->num;
243 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700244
Gavin Howard890d0c02018-10-30 16:34:50 -0600245 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600246 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700247 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700248
Gavin Howard2d188a52019-02-25 14:19:08 -0700249 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700250 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600251 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700252
Gavin Howardf2ee6342019-04-09 17:05:20 -0600253 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700254}
255
Gavin Howard2a366922019-01-16 10:50:20 -0700256static void bc_num_clean(BcNum *restrict n) {
257 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
258 if (BC_NUM_ZERO(n)) n->neg = false;
259 else if (n->len < n->rdx) n->len = n->rdx;
260}
261
Gavin Howard3c02ed32018-12-28 18:17:15 -0700262void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700263
Gavin Howardb1558662019-04-26 14:32:55 -0600264 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700265
Stefan Esser70f11852019-04-27 10:39:00 +0200266 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700267
Gavin Howardb1558662019-04-26 14:32:55 -0600268 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
269 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
270
271 n->scale -= places;
272 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700273
Gavin Howardddce6d42019-01-03 14:07:06 -0700274 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600275
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600276 size_t pow;
277
278 pow = n->scale % BC_BASE_POWER;
279 pow = pow ? BC_BASE_POWER - pow : 0;
Stefan Esser6167e222019-05-02 10:43:21 +0200280 pow = bc_num_pow10(pow);
Gavin Howardb1558662019-04-26 14:32:55 -0600281
282 n->len -= places_rdx;
Gavin Howard404ece72019-04-26 15:59:15 -0600283 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
Gavin Howardb1558662019-04-26 14:32:55 -0600284
285 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600286 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600287
Gavin Howard2a366922019-01-16 10:50:20 -0700288 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600289 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700290}
291
Stefan Esser70f11852019-04-27 10:39:00 +0200292static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700293
Gavin Howardb5e6da92019-04-30 19:58:04 -0600294 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700295
Gavin Howard1ab22d22019-01-03 13:32:17 -0700296 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700297
Gavin Howardb5e6da92019-04-30 19:58:04 -0600298 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
299
Gavin Howard8721e332019-05-06 08:02:49 -0600300 if (places_rdx) {
301 bc_num_expand(n, bc_vm_growSize(n->len, places_rdx));
302 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
303 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
304 }
Gavin Howardb5e6da92019-04-30 19:58:04 -0600305
Gavin Howarde6f326e2019-04-26 10:13:45 -0600306 n->rdx += places_rdx;
307 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600308 n->len += places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700309
Gavin Howarde6f326e2019-04-26 10:13:45 -0600310 assert(n->rdx == BC_NUM_RDX(n->scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200311}
312
Gavin Howardb2d733b2019-04-29 13:20:45 -0600313static void bc_num_roundPlaces(BcNum *restrict n, size_t places) {
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600314
315 size_t rdx, place, i;
316 BcDig p10, sum;
317
318 if (places >= n->scale || places >= n->rdx * BC_BASE_POWER) {
Gavin Howard8721e332019-05-06 08:02:49 -0600319 bc_num_extend(n, places - n->scale);
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600320 return;
321 }
Gavin Howardb2d733b2019-04-29 13:20:45 -0600322
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600323 rdx = n->rdx - BC_NUM_RDX(places + 1);
324 place = BC_BASE_POWER - (places % BC_BASE_POWER + 1);
325
Gavin Howardb2d733b2019-04-29 13:20:45 -0600326 for (i = 0; i < rdx; i++) n->num[i] = 0;
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600327
Stefan Esser6167e222019-05-02 10:43:21 +0200328 p10 = (BcDig) bc_num_pow10(place);
Gavin Howardb2d733b2019-04-29 13:20:45 -0600329 sum = n->num[rdx] + (BC_BASE / 2) * p10;
330 sum = sum - sum % (BC_BASE * p10);
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600331
Gavin Howardb2d733b2019-04-29 13:20:45 -0600332 if (sum < BC_BASE_DIG) n->num[rdx] = sum;
333 else {
334
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600335 sum -= BC_BASE_DIG;
336 n->num[rdx] = sum;
337
338 do {
Gavin Howardb2d733b2019-04-29 13:20:45 -0600339 rdx += 1;
340
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600341 if (n->len <= rdx) {
Gavin Howarde396dff2019-05-01 07:28:36 -0600342 bc_num_expand(n, bc_vm_growSize(rdx, 1));
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600343 n->num[rdx] = 0;
344 n->len = rdx +1;
345 }
Gavin Howardb2d733b2019-04-29 13:20:45 -0600346
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600347 sum = n->num[rdx] + 1;
348 n->num[rdx] = sum < BC_BASE_DIG ? sum : sum - BC_BASE_DIG;
349 } while (sum >= BC_BASE_DIG);
350 }
Gavin Howardb2d733b2019-04-29 13:20:45 -0600351
352 bc_num_truncate(n, n->scale - places);
Stefan Eßer9ef891f2019-04-29 13:18:09 -0600353}
354
Gavin Howard2d188a52019-02-25 14:19:08 -0700355static void bc_num_retireMul(BcNum *restrict n, size_t scale,
356 bool neg1, bool neg2)
357{
Gavin Howard971a2672019-04-26 14:32:07 -0600358 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
359 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600360
Gavin Howard305249a2018-10-15 20:24:47 -0600361 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700362 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600363}
364
Gavin Howard2d188a52019-02-25 14:19:08 -0700365static void bc_num_split(const BcNum *restrict n, size_t idx,
366 BcNum *restrict a, BcNum *restrict b)
367{
Gavin Howard305249a2018-10-15 20:24:47 -0600368 if (idx < n->len) {
369
Gavin Howard305249a2018-10-15 20:24:47 -0600370 b->len = n->len - idx;
371 a->len = idx;
Gavin Howarda3f260c2019-04-26 15:47:25 -0600372 a->scale = a->rdx = b->scale = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600373
Gavin Howard404ece72019-04-26 15:59:15 -0600374 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
375 memcpy(a->num, n->num, BC_NUM_SIZE(idx));
Gavin Howard50c80022019-01-03 15:14:33 -0700376
377 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600378 }
Gavin Howard50c80022019-01-03 15:14:33 -0700379 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600380
381 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600382}
383
Gavin Howard14913fc2019-04-23 14:36:03 -0600384static size_t bc_num_shiftZero(BcNum *restrict n) {
385
386 size_t i;
387
388 assert(!n->rdx || BC_NUM_ZERO(n));
389
390 for (i = 0; i < n->len && !n->num[i]; ++i);
391
392 n->len -= i;
393 n->num += i;
394
395 return i;
396}
397
Gavin Howard2e736de2019-04-27 06:41:58 -0600398static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
399 n->len += places_rdx;
400 n->num -= places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600401}
402
Gavin Howardbe884592019-04-29 15:07:49 -0600403static BcStatus bc_num_shift(BcNum *restrict n, unsigned long dig) {
404
405 size_t i, len = n->len;
Gavin Howard834fde12019-04-30 08:00:05 -0600406 unsigned long carry = 0, pow;
Gavin Howardbe884592019-04-29 15:07:49 -0600407 BcDig *ptr = n->num;
Gavin Howard305249a2018-10-15 20:24:47 -0600408
Gavin Howardb1558662019-04-26 14:32:55 -0600409 assert(dig < BC_BASE_POWER);
Gavin Howard305249a2018-10-15 20:24:47 -0600410
Stefan Esser6167e222019-05-02 10:43:21 +0200411 pow = bc_num_pow10(dig);
412 dig = bc_num_pow10(BC_BASE_POWER - dig);
Gavin Howardb1558662019-04-26 14:32:55 -0600413
Gavin Howard834fde12019-04-30 08:00:05 -0600414 for (i = len - 1; BC_NO_SIG && i < len; --i) {
415 unsigned long in, temp;
416 in = ((unsigned long) ptr[i]);
417 temp = carry * dig;
418 carry = in % pow;
419 ptr[i] = ((BcDig) (in / pow)) + (BcDig) temp;
Gavin Howard305249a2018-10-15 20:24:47 -0600420 }
421
Gavin Howard834fde12019-04-30 08:00:05 -0600422 assert(!carry);
Gavin Howardb1558662019-04-26 14:32:55 -0600423
424 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600425}
426
Gavin Howardb1558662019-04-26 14:32:55 -0600427static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700428
Gavin Howardb1558662019-04-26 14:32:55 -0600429 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600430 unsigned long dig;
Gavin Howard48a0e472019-04-26 15:39:46 -0600431 size_t places_rdx;
Gavin Howardbe884592019-04-29 15:07:49 -0600432 bool shift;
Gavin Howard14913fc2019-04-23 14:36:03 -0600433
Gavin Howardb1558662019-04-26 14:32:55 -0600434 if (!places) return s;
Gavin Howardabe4fdf2019-05-06 08:39:53 -0600435 if (places > n->scale) {
436 size_t size = bc_vm_growSize(BC_NUM_RDX(places - n->scale), n->len);
437 if (size > SIZE_MAX - 1) return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
438 }
Gavin Howard4631f3a2019-04-30 10:48:40 -0600439 if (BC_NUM_ZERO(n)) {
440 if (n->scale >= places) n->scale -= places;
441 else n->scale = 0;
442 return s;
443 }
Gavin Howard14913fc2019-04-23 14:36:03 -0600444
Gavin Howard48a0e472019-04-26 15:39:46 -0600445 dig = (unsigned long) (places % BC_BASE_POWER);
446 shift = (dig != 0);
Gavin Howard22253c72019-05-06 08:03:57 -0600447 places_rdx = BC_NUM_RDX(places);
Gavin Howard48a0e472019-04-26 15:39:46 -0600448
Gavin Howard22253c72019-05-06 08:03:57 -0600449 if (n->scale) {
Gavin Howarda1879192019-04-30 18:55:08 -0600450
451 if (n->rdx >= places_rdx) {
452
453 size_t mod = n->scale % BC_BASE_POWER, revdig;
454
455 mod = mod ? mod : BC_BASE_POWER;
456 revdig = dig ? BC_BASE_POWER - dig : 0;
457
458 if (mod + revdig > BC_BASE_POWER) places_rdx = 1;
459 else places_rdx = 0;
460 }
461 else places_rdx -= n->rdx;
462 }
Gavin Howard4631f3a2019-04-30 10:48:40 -0600463
464 if (places_rdx) {
Gavin Howarde396dff2019-05-01 07:28:36 -0600465 bc_num_expand(n, bc_vm_growSize(n->len, places_rdx));
Gavin Howard834fde12019-04-30 08:00:05 -0600466 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
467 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
468 n->len += places_rdx;
Gavin Howardb1558662019-04-26 14:32:55 -0600469 }
Gavin Howard4631f3a2019-04-30 10:48:40 -0600470
471 if (places > n->scale) n->scale = n->rdx = 0;
472 else {
473 n->scale -= places;
474 n->rdx = BC_NUM_RDX(n->scale);
475 }
Gavin Howardb1558662019-04-26 14:32:55 -0600476
Gavin Howard834fde12019-04-30 08:00:05 -0600477 if (shift) s = bc_num_shift(n, BC_BASE_POWER - dig);
Gavin Howardb1558662019-04-26 14:32:55 -0600478
479 bc_num_clean(n);
480
481 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
482}
483
484static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
485
486 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howardb5e6da92019-04-30 19:58:04 -0600487 unsigned long dig;
488 size_t places_rdx, scale, scale_mod, int_len, expand;
489 bool shift;
Gavin Howardb1558662019-04-26 14:32:55 -0600490
491 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600492 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600493 n->scale += places;
494 bc_num_expand(n, BC_NUM_RDX(n->scale));
495 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600496 }
497
Gavin Howardb5e6da92019-04-30 19:58:04 -0600498 dig = (unsigned long) (places % BC_BASE_POWER);
499 shift = (dig != 0);
Gavin Howard834fde12019-04-30 08:00:05 -0600500 scale = n->scale;
Gavin Howardb5e6da92019-04-30 19:58:04 -0600501 scale_mod = scale % BC_BASE_POWER;
502 scale_mod = scale_mod ? scale_mod : BC_BASE_POWER;
Gavin Howard834fde12019-04-30 08:00:05 -0600503 int_len = bc_num_int(n);
Gavin Howardb1558662019-04-26 14:32:55 -0600504 places_rdx = BC_NUM_RDX(places);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700505
Gavin Howardb5e6da92019-04-30 19:58:04 -0600506 if (scale_mod + dig > BC_BASE_POWER) {
507 expand = places_rdx - 1;
508 places_rdx = 1;
Gavin Howard834fde12019-04-30 08:00:05 -0600509 }
510 else {
Gavin Howardb5e6da92019-04-30 19:58:04 -0600511 expand = places_rdx;
512 places_rdx = 0;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700513 }
514
Gavin Howardb5e6da92019-04-30 19:58:04 -0600515 if (expand > int_len) expand -= int_len;
516 else expand = 0;
517
Gavin Howard834fde12019-04-30 08:00:05 -0600518 bc_num_extend(n, places_rdx * BC_BASE_POWER);
Gavin Howarde396dff2019-05-01 07:28:36 -0600519 bc_num_expand(n, bc_vm_growSize(expand, n->len));
Gavin Howardb5e6da92019-04-30 19:58:04 -0600520 memset(n->num + n->len, 0, BC_NUM_SIZE(expand));
521 n->len += expand;
Gavin Howard834fde12019-04-30 08:00:05 -0600522 n->scale = n->rdx = 0;
Gavin Howardb1558662019-04-26 14:32:55 -0600523
Gavin Howard834fde12019-04-30 08:00:05 -0600524 if (shift) s = bc_num_shift(n, dig);
525
526 n->scale = scale + places;
527 n->rdx = BC_NUM_RDX(n->scale);
528
529 bc_num_clean(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700530
531 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600532 assert(n->rdx == BC_NUM_RDX(n->scale));
533
534 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700535}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700536
Gavin Howard1cbfe242019-01-09 17:13:11 -0700537static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600538
Gavin Howard63738202018-09-26 15:34:20 -0600539 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600540 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600541
Gavin Howard971a2672019-04-26 14:32:07 -0600542 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700543
Gavin Howardd32d7df2018-10-15 08:42:25 -0600544 one.cap = 2;
545 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600546 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600547
Gavin Howardd32d7df2018-10-15 08:42:25 -0600548 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600549}
550
Gavin Howard7bda4782018-12-28 09:53:22 -0700551#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700552static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
553 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700554{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700555 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700556 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700557 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700558}
Gavin Howard7bda4782018-12-28 09:53:22 -0700559#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700560
Gavin Howard1cbfe242019-01-09 17:13:11 -0700561static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700562
Gavin Howard8a921bd2018-10-11 14:15:32 -0600563 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600564 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard6fccd222019-04-26 10:17:02 -0600565 unsigned long carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700566
Gavin Howard63738202018-09-26 15:34:20 -0600567 // Because this function doesn't need to use scale (per the bc spec),
568 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600569
Gavin Howardddce6d42019-01-03 14:07:06 -0700570 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600571 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700572 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600573 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600574 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700575 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600576 bc_num_copy(c, a);
577 return BC_STATUS_SUCCESS;
578 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600579
Gavin Howard63738202018-09-26 15:34:20 -0600580 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600581 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600582 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600583 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700584
Gavin Howard63738202018-09-26 15:34:20 -0600585 if (a->rdx > b->rdx) {
586 diff = a->rdx - b->rdx;
587 ptr = a->num;
588 ptr_a = a->num + diff;
589 ptr_b = b->num;
590 }
591 else {
592 diff = b->rdx - a->rdx;
593 ptr = b->num;
594 ptr_a = a->num;
595 ptr_b = b->num + diff;
596 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700597
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700598 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700599
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700600 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600601 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700602 a_int = bc_num_int(a);
603 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700604
Gavin Howard890d0c02018-10-30 16:34:50 -0600605 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600606 min_int = b_int;
607 max = a_int;
608 ptr = ptr_a;
609 }
610 else {
611 min_int = a_int;
612 max = b_int;
613 ptr = ptr_b;
614 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700615
Gavin Howard2d188a52019-02-25 14:19:08 -0700616 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard6fccd222019-04-26 10:17:02 -0600617 unsigned long in;
618 in = ((unsigned long) ptr_a[i]) + ((unsigned long) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700619 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600620 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700621
Gavin Howard2d188a52019-02-25 14:19:08 -0700622 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard6fccd222019-04-26 10:17:02 -0600623 carry = bc_num_addDigit(ptr_c + i, (unsigned long) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700624
625 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700626
Gavin Howard1ab22d22019-01-03 13:32:17 -0700627 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600628
Gavin Howard2d188a52019-02-25 14:19:08 -0700629 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700630}
631
Gavin Howard1cbfe242019-01-09 17:13:11 -0700632static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700633
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700634 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600635 ssize_t cmp;
636 BcNum *minuend, *subtrahend;
637 size_t start;
638 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700639
Gavin Howard63738202018-09-26 15:34:20 -0600640 // Because this function doesn't need to use scale (per the bc spec),
641 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700642
Gavin Howardddce6d42019-01-03 14:07:06 -0700643 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600644 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700645 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600646 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600647 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700648 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600649 bc_num_copy(c, a);
650 return BC_STATUS_SUCCESS;
651 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700652
Gavin Howard63738202018-09-26 15:34:20 -0600653 aneg = a->neg;
654 bneg = b->neg;
655 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700656
Gavin Howard63738202018-09-26 15:34:20 -0600657 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600658 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700659
Gavin Howard63738202018-09-26 15:34:20 -0600660 a->neg = aneg;
661 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700662
Gavin Howard1ab22d22019-01-03 13:32:17 -0700663 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600664 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600665 return BC_STATUS_SUCCESS;
666 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700667
668 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600669 neg = a->neg;
670 minuend = a;
671 subtrahend = b;
672 }
673 else {
674 neg = b->neg;
675 if (sub) neg = !neg;
676 minuend = b;
677 subtrahend = a;
678 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700679
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600680 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600681 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700682
Gavin Howarde6f326e2019-04-26 10:13:45 -0600683 if (c->scale < subtrahend->scale) {
684 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600685 start = 0;
686 }
687 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700688
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700689 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700690
Gavin Howard63738202018-09-26 15:34:20 -0600691 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700692
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700693 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700694}
695
Gavin Howarddc4e3712019-04-23 14:56:29 -0600696static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
697{
Gavin Howard971a2672019-04-26 14:32:07 -0600698 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600699 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard971a2672019-04-26 14:32:07 -0600700 unsigned long sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600701
Gavin Howard971a2672019-04-26 14:32:07 -0600702 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600703 assert(!a->rdx && !b->rdx);
704
Gavin Howard5083aec2019-04-23 18:03:46 -0600705 clen = bc_vm_growSize(alen, blen);
Gavin Howarde396dff2019-05-01 07:28:36 -0600706 bc_num_expand(c, bc_vm_growSize(clen, 1));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600707
708 ptr_c = c->num;
Gavin Howard404ece72019-04-26 15:59:15 -0600709 memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600710
Gavin Howard5083aec2019-04-23 18:03:46 -0600711 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600712
Gavin Howard5083aec2019-04-23 18:03:46 -0600713 ssize_t sidx = (ssize_t) (i - blen + 1);
714 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600715
Stefan Essera299ffc2019-04-24 23:28:32 +0200716 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
Stefan Eßer4b111c22019-04-24 17:23:08 -0600717
Gavin Howard971a2672019-04-26 14:32:07 -0600718 sum += ((unsigned long) ptr_a[j]) * ((unsigned long) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600719
Gavin Howard0700efc2019-04-24 17:31:23 -0600720 if (sum >= BC_BASE_DIG) {
721 carry += sum / BC_BASE_DIG;
722 sum %= BC_BASE_DIG;
Stefan Essera299ffc2019-04-24 23:28:32 +0200723 }
724 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600725
Stefan Essera299ffc2019-04-24 23:28:32 +0200726 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600727 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Essera299ffc2019-04-24 23:28:32 +0200728 sum = carry;
729 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600730 }
731
Gavin Howard5083aec2019-04-23 18:03:46 -0600732 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600733 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600734 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600735 clen += 1;
736 }
737
738 c->len = clen;
Gavin Howarda58840c2019-05-07 19:33:53 -0600739
Gavin Howarddc4e3712019-04-23 14:56:29 -0600740 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
741}
742
Gavin Howard14c354a2019-04-24 14:28:24 -0600743static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
744 size_t shift, BcNumShiftAddOp op)
745{
746 assert(n->len >= shift + a->len);
747 assert(!n->rdx && !a->rdx);
748 return op(n->num + shift, a->num, a->len);
749}
750
751static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600752
Gavin Howard9b801632019-02-16 23:34:06 -0700753 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200754 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600755 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700756 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600757 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700758 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600759
Gavin Howard14913fc2019-04-23 14:36:03 -0600760 assert(BC_NUM_ZERO(c));
761
Gavin Howardddce6d42019-01-03 14:07:06 -0700762 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600763 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700764 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600765 bc_num_zero(c);
766 return BC_STATUS_SUCCESS;
767 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700768 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600769 bc_num_copy(c, aone ? b : a);
770 return BC_STATUS_SUCCESS;
771 }
Gavin Howardb5093d72018-10-17 12:13:17 -0600772 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
773 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600774 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600775 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600776 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700777
Gavin Howarde0432502019-02-26 16:23:45 -0700778 max = BC_MAX(a->len, b->len);
Stefan Essera299ffc2019-04-24 23:28:32 +0200779 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700780 max2 = (max + 1) / 2;
781
Stefan Esser5fd43a32019-04-25 07:05:24 +0200782 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
Gavin Howard404ece72019-04-26 15:59:15 -0600783 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total));
Gavin Howard5bede412019-02-23 09:31:28 -0700784
785 bc_num_setup(&l1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200786 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700787 bc_num_setup(&h1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200788 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700789 bc_num_setup(&l2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200790 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700791 bc_num_setup(&h2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200792 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700793 bc_num_setup(&m1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200794 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700795 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600796 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600797 bc_num_init(&z0, max);
798 bc_num_init(&z1, max);
799 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600800 max = bc_vm_growSize(max, max) + 1;
801 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600802
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600803 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600804 bc_num_clean(&l1);
805 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600806 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600807 bc_num_clean(&l2);
808 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700809
Gavin Howard14c354a2019-04-24 14:28:24 -0600810 bc_num_expand(c, max);
811 c->len = max;
Gavin Howard404ece72019-04-26 15:59:15 -0600812 memset(c->num, 0, BC_NUM_SIZE(c->len));
Gavin Howard305249a2018-10-15 20:24:47 -0600813
Gavin Howard14c354a2019-04-24 14:28:24 -0600814 s = bc_num_sub(&h1, &l1, &m1, 0);
815 if (BC_ERR(s)) goto err;
816 s = bc_num_sub(&l2, &h2, &m2, 0);
817 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600818
Gavin Howard971a2672019-04-26 14:32:07 -0600819 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600820
Gavin Howard14c354a2019-04-24 14:28:24 -0600821 s = bc_num_m(&h1, &h2, &z2, 0);
822 if (BC_ERR(s)) goto err;
823 bc_num_clean(&z2);
824
825 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
826 if (BC_ERR(s)) goto err;
827 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
828 if (BC_ERR(s)) goto err;
829 }
830
Gavin Howard971a2672019-04-26 14:32:07 -0600831 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600832
833 s = bc_num_m(&l1, &l2, &z0, 0);
834 if (BC_ERR(s)) goto err;
835 bc_num_clean(&z0);
836
837 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
838 if (BC_ERR(s)) goto err;
839 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
840 if (BC_ERR(s)) goto err;
841 }
842
Gavin Howard971a2672019-04-26 14:32:07 -0600843 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600844
845 s = bc_num_m(&m1, &m2, &z1, 0);
846 if (BC_ERR(s)) goto err;
847 bc_num_clean(&z1);
848
849 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
850 s = bc_num_shiftAddSub(c, &z1, max2, op);
851 if (BC_ERR(s)) goto err;
852 }
Gavin Howard305249a2018-10-15 20:24:47 -0600853
854err:
Gavin Howard5bede412019-02-23 09:31:28 -0700855 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600856 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600857 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600858 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600859 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600860 return s;
861}
862
Gavin Howard1cbfe242019-01-09 17:13:11 -0700863static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600864
865 BcStatus s;
866 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600867 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600868
Gavin Howard14913fc2019-04-23 14:36:03 -0600869 bc_num_setToZero(c, 0);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600870 ascale = a->scale;
871 bscale = b->scale;
872 scale = BC_MAX(scale, ascale);
Gavin Howarda9a887a2019-04-29 14:00:56 -0600873 scale = BC_MAX(scale, bscale);
Gavin Howard7cb5f6d2019-04-29 14:01:44 -0600874
Gavin Howard48a0e472019-04-26 15:39:46 -0600875 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600876 scale = BC_MIN(rscale, scale);
Gavin Howardbaa4f582019-01-24 13:56:35 -0700877 bc_num_createCopy(&cpa, a);
878 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600879
Gavin Howard23d8d252018-10-17 12:03:41 -0600880 cpa.neg = cpb.neg = false;
Gavin Howard48a0e472019-04-26 15:39:46 -0600881 ardx = cpa.rdx * BC_BASE_POWER;
882 s = bc_num_shiftLeft(&cpa, ardx);
883 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600884 bc_num_clean(&cpa);
885 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600886
887 brdx = cpb.rdx * BC_BASE_POWER;
888 s = bc_num_shiftLeft(&cpb, brdx);
889 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600890 bzero = bc_num_shiftZero(&cpb);
891 bc_num_clean(&cpb);
892
Gavin Howard14c354a2019-04-24 14:28:24 -0600893 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700894 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600895
Gavin Howard14913fc2019-04-23 14:36:03 -0600896 zero = bc_vm_growSize(azero, bzero);
897 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600898
Gavin Howard14913fc2019-04-23 14:36:03 -0600899 bc_num_expand(c, len);
Gavin Howard48a0e472019-04-26 15:39:46 -0600900 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_POWER);
901 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
902 s = bc_num_shiftRight(c, ardx + brdx);
903 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600904 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600905
Gavin Howard305249a2018-10-15 20:24:47 -0600906err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600907 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600908 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600909 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600910 bc_num_free(&cpa);
911 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700912}
913
Stefan Esser45342832019-05-02 10:43:02 +0200914#ifdef USE_GOLDSCHMIDT
Gavin Howard24e3dea2019-05-07 08:34:40 -0600915/*
916 * Find reciprocal value for paramezter in range 0.5 < val <= 1.0.
917 */
918static BcStatus bc_num_invert(BcNum *val, size_t scale) {
Stefan Esser154b4732019-04-28 17:21:34 +0200919
Stefan Esser85f59a62019-05-04 16:19:12 +0200920 BcNum one, x, temp, sum;
Stefan Esser154b4732019-04-28 17:21:34 +0200921 bool done = false;
922 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard1bebc482019-05-07 12:15:31 -0600923 BcDig one_digs[BC_NUM_LONG_LOG10];
Stefan Esser154b4732019-04-28 17:21:34 +0200924
Gavin Howard24e3dea2019-05-07 08:34:40 -0600925 // We need one constant value 1 to start...
Gavin Howard959d54b2019-05-07 08:48:38 -0600926 bc_num_setup(&one, one_digs, sizeof(one_digs) / sizeof(BcDig));
927 bc_num_ulong2num(&one, 1);
Gavin Howard24e3dea2019-05-07 08:34:40 -0600928
929 // Create temporary variable used in each iteration step.
Stefan Esser40b53d22019-04-29 12:56:32 +0200930 bc_num_init(&temp, scale / BC_BASE_POWER + 1);
Gavin Howard24e3dea2019-05-07 08:34:40 -0600931
932 // Create variable to be squared per iteration.
Stefan Esser40b53d22019-04-29 12:56:32 +0200933 bc_num_init(&x, scale / BC_BASE_POWER + 1);
Gavin Howard24e3dea2019-05-07 08:34:40 -0600934
935 // Create variable for the sum the series elements.
Stefan Esser85f59a62019-05-04 16:19:12 +0200936 bc_num_init(&sum, scale / BC_BASE_POWER + 1);
Stefan Esser154b4732019-04-28 17:21:34 +0200937 s = bc_num_sub(&one, val, &x, scale);
938 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard24e3dea2019-05-07 08:34:40 -0600939
940 // Initialize series sum to 1.0 + error.
Stefan Esser85f59a62019-05-04 16:19:12 +0200941 s = bc_num_add(&one, &x, &sum, scale);
Stefan Esser154b4732019-04-28 17:21:34 +0200942 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
943
944 for (;;) {
Gavin Howardf1685152019-05-07 08:35:28 -0600945
Gavin Howard24e3dea2019-05-07 08:34:40 -0600946 // Calculate square of delta for next iteration.
Stefan Esser154b4732019-04-28 17:21:34 +0200947 s = bc_num_mul(&x, &x, &x, scale);
948 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard24e3dea2019-05-07 08:34:40 -0600949
950 // Nothing left to do, if the squared delta
951 // truncated to "scale" decimals is 0.0.
Stefan Esser154b4732019-04-28 17:21:34 +0200952 if BC_NUM_ZERO(&x) break;
953
Gavin Howard24e3dea2019-05-07 08:34:40 -0600954 // Multiply current series sum with the squared delta.
Stefan Esser85f59a62019-05-04 16:19:12 +0200955 s = bc_num_mul(&sum, &x, &temp, scale);
Stefan Esser154b4732019-04-28 17:21:34 +0200956 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard24e3dea2019-05-07 08:34:40 -0600957
958 // Add series element to sum.
Stefan Esser85f59a62019-05-04 16:19:12 +0200959 s = bc_num_add(&sum, &temp, &sum, scale);
Stefan Esser154b4732019-04-28 17:21:34 +0200960 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
961 }
Gavin Howard1d42c2f2019-05-07 08:35:39 -0600962
Gavin Howard24e3dea2019-05-07 08:34:40 -0600963 // Apply correction for finite number of series elements
964 // considered. Could be further optimized...
Gavin Howard40394172019-05-07 18:50:08 -0600965 bc_num_mul(val, &sum, &temp, scale + 1);
Gavin Howard24e3dea2019-05-07 08:34:40 -0600966
967 // The correction is derived from 1.0 - sum * (1/sum).
Stefan Esser85f59a62019-05-04 16:19:12 +0200968 bc_num_sub(&one, &temp, &temp, scale);
Gavin Howard24e3dea2019-05-07 08:34:40 -0600969
970 // Add delta twice, we could also use Newton-Raphson for the correction.
Gavin Howarda58840c2019-05-07 19:33:53 -0600971#if 1
Stefan Esserb9db23b2019-05-04 18:02:15 +0200972 bc_num_add(&sum, &temp, &sum, scale);
Stefan Esser85f59a62019-05-04 16:19:12 +0200973 bc_num_add(&sum, &temp, val, scale);
Gavin Howard40394172019-05-07 18:50:08 -0600974#else
975 bc_num_mul(&sum, &temp, &temp, scale * 2);
976 bc_num_add(&sum, &temp, val, scale);
977#endif
Gavin Howard24e3dea2019-05-07 08:34:40 -0600978
Stefan Esser154b4732019-04-28 17:21:34 +0200979err:
Stefan Esser85f59a62019-05-04 16:19:12 +0200980 bc_num_free(&sum);
Stefan Esser154b4732019-04-28 17:21:34 +0200981 bc_num_free(&x);
982 bc_num_free(&temp);
Stefan Esser154b4732019-04-28 17:21:34 +0200983 return s;
984}
985
Gavin Howard24e3dea2019-05-07 08:34:40 -0600986/*
987 * Normalize number to have rdx == len and return the number of
988 * BcDigs the value has been shifted to the right (negative for left).
989 */
Gavin Howardf0d38302019-05-07 08:14:50 -0600990static ssize_t bc_num_normalize(BcNum *n) {
Stefan Esser40b53d22019-04-29 12:56:32 +0200991
992 int i, shift = 0;
993 ssize_t len, rdx;
994
995 if (BC_NUM_ZERO(n)) return 0;
996
997 len = n->len;
998 rdx = n->rdx;
999
Gavin Howardaf3aca22019-05-07 08:36:28 -06001000 while (len > 0 && n->num[len - 1] == 0) len -= 1;
Stefan Esser40b53d22019-04-29 12:56:32 +02001001
1002 n->len = len;
1003 n->rdx = len;
1004 n->scale += (len - rdx) * BC_BASE_POWER;
1005
1006 return len - rdx;
1007}
1008
Stefan Eßerdaaaaa72019-04-27 18:04:02 -06001009static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001010
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001011 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001012 size_t rdx, rscale, req;
Gavin Howardf0d38302019-05-07 08:14:50 -06001013 ssize_t cmp, shift;
Stefan Esser193f8522019-04-27 01:29:01 +02001014 BcNum b1, f;
1015 size_t factor, dividend, divisor;
Stefan Esser40b53d22019-04-29 12:56:32 +02001016 size_t i, j, mindivisor, temp_scale;
Gavin Howardf0d38302019-05-07 08:14:50 -06001017 BcDig f_digs[BC_NUM_LONG_LOG10];
Gavin Howard6fbdb292018-02-27 15:44:48 -07001018
Gavin Howardddce6d42019-01-03 14:07:06 -07001019 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1020 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001021 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001022 return BC_STATUS_SUCCESS;
1023 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001024 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001025 bc_num_copy(c, a);
Stefan Esser45342832019-05-02 10:43:02 +02001026 goto exit;
Gavin Howard63738202018-09-26 15:34:20 -06001027 }
Gavin Howardb651f1a2018-02-28 17:34:18 -07001028
Gavin Howard24e3dea2019-05-07 08:34:40 -06001029 // Set up f.
Gavin Howardf0d38302019-05-07 08:14:50 -06001030 bc_num_setup(&f, f_digs, BC_NUM_LONG_LOG10);
1031
Gavin Howard24e3dea2019-05-07 08:34:40 -06001032 // Calculate scale that allows to represent
1033 // all possible multiplication results.
Stefan Esser40b53d22019-04-29 12:56:32 +02001034 temp_scale = BC_MAX(scale, BC_BASE_POWER * (a->len + b->len + 1));
Gavin Howardb651f1a2018-02-28 17:34:18 -07001035
Gavin Howard24e3dea2019-05-07 08:34:40 -06001036 // Create normalized copy of first argument in result variable "c".
Gavin Howardd1bea962019-05-06 12:55:21 -06001037 bc_num_copy(c, a);
Gavin Howard24e3dea2019-05-07 08:34:40 -06001038 // The shift value now records by how many
1039 // BcDigs the result will need to be shifted.
Stefan Esser40b53d22019-04-29 12:56:32 +02001040 shift = bc_num_normalize(c);
Stefan Esserb5aeb442019-04-27 23:33:08 +02001041
Gavin Howard24e3dea2019-05-07 08:34:40 -06001042 // Create normalized copy of second argument as temporary variable b1.
Stefan Esserb5aeb442019-04-27 23:33:08 +02001043 bc_num_createCopy(&b1, b);
Gavin Howard24e3dea2019-05-07 08:34:40 -06001044 // The shift value now records by how many
1045 // BcDigs the result will need to be shifted.
Stefan Esser40b53d22019-04-29 12:56:32 +02001046 shift -= bc_num_normalize(&b1);
Stefan Esserb5aeb442019-04-27 23:33:08 +02001047
Gavin Howard24e3dea2019-05-07 08:34:40 -06001048 // Set sign of (copies of the) operands to positive.
Stefan Esserb5aeb442019-04-27 23:33:08 +02001049 c->neg = false;
1050 b1.neg = false;
Stefan Esser70997152019-04-28 15:50:14 +02001051
Gavin Howard24e3dea2019-05-07 08:34:40 -06001052 // Compare normalized operands to determine whether
1053 // the result of dividing them will be < or > 1.
Stefan Esser6e129fc2019-04-27 23:41:21 +02001054 cmp = bc_num_cmp(&b1, c);
Gavin Howard92b81ce2019-05-07 08:37:07 -06001055
Stefan Esserb5aeb442019-04-27 23:33:08 +02001056 if (cmp == 0) {
Gavin Howard24e3dea2019-05-07 08:34:40 -06001057 // If the normalized values are identical the
1058 // result will be a power of (10^BC_BASE_POWER).
Gavin Howarda8ab3e62019-05-06 13:18:33 -06001059 bc_num_ulong2num(c, 1);
Stefan Esserf76f0362019-04-29 21:28:52 +02001060 }
1061 else {
Gavin Howard24e3dea2019-05-07 08:34:40 -06001062
1063 //==> b > a, result will be one exp higher
1064 shift -= (cmp > 0);
Stefan Esserb5aeb442019-04-27 23:33:08 +02001065
1066 dividend = 1;
1067 divisor = 0;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001068
Gavin Howard24e3dea2019-05-07 08:34:40 -06001069 // Calculate the maximum power of BC_BASE_DIG
1070 // that will fit into a size_t.
1071 for (i = 0; i < 19 / BC_BASE_POWER; ++i) dividend *= BC_BASE_DIG;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001072
Gavin Howard24e3dea2019-05-07 08:34:40 -06001073 // Determine the minimum number acceptable
1074 // for the initial divide operation.
1075 mindivisor = bc_num_pow10((19 - BC_BASE_POWER) / 2);
1076 if (BC_BASE_POWER % 2 != 0) mindivisor *= 3;
Stefan Esser40b53d22019-04-29 12:56:32 +02001077
Gavin Howard24e3dea2019-05-07 08:34:40 -06001078 for (i = 0, j = 0; i < b1.len; ++i) {
1079
Stefan Esser40b53d22019-04-29 12:56:32 +02001080 if (divisor < mindivisor) {
Gavin Howard24e3dea2019-05-07 08:34:40 -06001081
1082 // Accumulate BcDigs until the minimum
1083 // desired divisor has been formed
Stefan Esser40b53d22019-04-29 12:56:32 +02001084 divisor *= BC_BASE_DIG;
1085 divisor += b1.num[b1.len - 1 - i];
Stefan Esserf76f0362019-04-29 21:28:52 +02001086 }
1087 else {
Stefan Esser40b53d22019-04-29 12:56:32 +02001088 if (b1.num[b1.len - 1 - i] != 0)
Gavin Howard24e3dea2019-05-07 08:34:40 -06001089 // There were further non-zero digits not included
1090 // in the divisor. Account for them by incrementing
1091 // the divisor just to be sure
Stefan Esser40b53d22019-04-29 12:56:32 +02001092 j = 1;
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001093 }
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001094 }
Gavin Howard24e3dea2019-05-07 08:34:40 -06001095
Stefan Esserb5aeb442019-04-27 23:33:08 +02001096 divisor += j;
Stefan Esser40b53d22019-04-29 12:56:32 +02001097
Gavin Howard24e3dea2019-05-07 08:34:40 -06001098 // The quotient is used as the initial estimate of
1099 // the (scaled) reciprocal value of the divisor.
Stefan Esserb5aeb442019-04-27 23:33:08 +02001100 factor = dividend / divisor;
Stefan Esser40b53d22019-04-29 12:56:32 +02001101
Gavin Howard24e3dea2019-05-07 08:34:40 -06001102 // Multiply the estimate of 1/B ("factor") with
1103 // the actual value of B giving a result <= 1.0.
Gavin Howardf0d38302019-05-07 08:14:50 -06001104 bc_num_ulong2num(&f, factor);
Stefan Esser40b53d22019-04-29 12:56:32 +02001105 bc_num_mul(&b1, &f, &b1, temp_scale);
Stefan Esser154b4732019-04-28 17:21:34 +02001106 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001107
Stefan Esser7e21b3c2019-05-04 16:24:40 +02001108 if (b1.num[b1.len - 1] != 1) {
Gavin Howard24e3dea2019-05-07 08:34:40 -06001109
Gavin Howard40394172019-05-07 18:50:08 -06001110 // A correction is required, we multiply with the
1111 // inverse of the error since we cannot divide...
1112 b1.rdx = b1.len;
Gavin Howarda58840c2019-05-07 19:33:53 -06001113
1114 // Calculate the inverse of the error
1115 // to twice the number of decimals.
1116 b1.scale = b1.rdx * BC_BASE_POWER;
Stefan Esser7e21b3c2019-05-04 16:24:40 +02001117 s = bc_num_invert(&b1, temp_scale);
Stefan Esser154b4732019-04-28 17:21:34 +02001118 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard24e3dea2019-05-07 08:34:40 -06001119
1120 // Multiply with the correction factor (i.e.,
1121 // the reciprocal value of the error factor).
Stefan Esser7e21b3c2019-05-04 16:24:40 +02001122 bc_num_mul(&b1, c, c, temp_scale + BC_BASE_POWER);
Stefan Esser154b4732019-04-28 17:21:34 +02001123 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Esserb5aeb442019-04-27 23:33:08 +02001124 }
Gavin Howard24e3dea2019-05-07 08:34:40 -06001125
1126 // Multiply the corrected reciprocal value of B with A to get A/B.
Stefan Esser40b53d22019-04-29 12:56:32 +02001127 bc_num_mul(&f, c, c, temp_scale);
Stefan Esser154b4732019-04-28 17:21:34 +02001128 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard24e3dea2019-05-07 08:34:40 -06001129
1130 // Adjust the decimal point in such a way that
1131 // the result is 1 <= C <= BC_BASE_DIG - 1.
Stefan Esserb5aeb442019-04-27 23:33:08 +02001132 c->rdx = c->len - 1;
Stefan Esser4c1e43a2019-04-29 00:59:32 +02001133 c->scale = c->rdx * BC_BASE_POWER;
Gavin Howard63738202018-09-26 15:34:20 -06001134 }
Stefan Esserf76f0362019-04-29 21:28:52 +02001135
Gavin Howard24e3dea2019-05-07 08:34:40 -06001136 // Adjust the decimal point to account for
1137 // the normalization of the arguments A and B.
Gavin Howard32a7cd32019-05-07 13:49:40 -06001138 if (shift < 0) s = bc_num_shiftRight(c, -shift * BC_BASE_POWER);
1139 else s = bc_num_shiftLeft(c, shift * BC_BASE_POWER);
Gavin Howard24e3dea2019-05-07 08:34:40 -06001140
Stefan Esser193f8522019-04-27 01:29:01 +02001141err:
Stefan Esser10bca112019-05-04 09:14:02 +02001142 bc_num_free(&b1);
Stefan Esser45342832019-05-02 10:43:02 +02001143exit:
Gavin Howardf2ee6342019-04-09 17:05:20 -06001144 if (BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard24e3dea2019-05-07 08:34:40 -06001145 // Adjust sign of the result from the preserved input parameters.
Stefan Esser45342832019-05-02 10:43:02 +02001146 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -06001147 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001148}
1149
Stefan Esser45342832019-05-02 10:43:02 +02001150#else // USE_GOLDSCHMIDT
1151
Gavin Howard6fbdb292018-02-27 15:44:48 -07001152static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
1153
1154 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard24e3ee92019-05-06 07:41:45 -06001155 size_t rdx, rdx2, rscale, scale2, req;
Stefan Eßerdaaaaa72019-04-27 18:04:02 -06001156 ssize_t cmp;
Gavin Howard36ec9872019-04-29 10:14:21 -06001157 BcNum cpa, cpb, two, factor, factor2, *fi, *fnext, *temp;
1158 BcDig two_digs[2];
Gavin Howardc542d0a2019-04-29 10:28:51 -06001159 bool aneg, bneg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001160
Gavin Howard36ec9872019-04-29 10:14:21 -06001161 aneg = a->neg;
1162 bneg = b->neg;
1163
Gavin Howard6fbdb292018-02-27 15:44:48 -07001164 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1165 if (BC_NUM_ZERO(a)) {
1166 bc_num_setToZero(c, scale);
1167 return BC_STATUS_SUCCESS;
1168 }
1169 if (bc_num_isOne(b)) {
1170 bc_num_copy(c, a);
Gavin Howardfdbcd062019-04-29 07:36:46 -06001171 goto exit;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001172 }
1173
Gavin Howard36ec9872019-04-29 10:14:21 -06001174 a->neg = b->neg = false;
Gavin Howardfdbcd062019-04-29 07:36:46 -06001175 cmp = bc_num_cmp(a, b);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001176
Gavin Howardfdbcd062019-04-29 07:36:46 -06001177 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
1178 if (!cmp) {
1179 bc_num_one(c);
1180 goto exit;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001181 }
Gavin Howardfdbcd062019-04-29 07:36:46 -06001182
1183 bc_num_createCopy(&cpa, a);
1184 bc_num_createCopy(&cpb, b);
1185
Gavin Howard24e3ee92019-05-06 07:41:45 -06001186 // This is to calculate enough digits to make rounding only happen when
1187 // necessary. It rounds the scale up to the next BcDig boundary, then adds
1188 // on enough to create a whole extra BcDig which will then be tested to see
1189 // if it's equal to BC_BASE_DIG - 1. If it is, then, and only then, should
1190 // rounding be needed.
1191 scale2 = BC_NUM_RDX(scale + 1) * BC_BASE_POWER + BC_BASE_POWER + 1;
1192 rdx2 = BC_NUM_RDX(scale2);
1193 req = bc_num_int(a) + rdx2 + 1;
1194 scale2 = rdx2 * BC_BASE_POWER;
Gavin Howard36ec9872019-04-29 10:14:21 -06001195 bc_num_init(&factor, req);
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001196 bc_num_init(&factor2, req);
Gavin Howard36ec9872019-04-29 10:14:21 -06001197
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001198 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
1199 bc_num_one(&two);
1200 two.num[0] = 2;
Gavin Howard36ec9872019-04-29 10:14:21 -06001201
Gavin Howardc542d0a2019-04-29 10:28:51 -06001202 if (b->rdx == b->len) {
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001203 rdx = cpb.len - bc_num_nonzeroLen(&cpb);
1204 rscale = rdx * BC_BASE_POWER;
1205 s = bc_num_shiftLeft(&cpa, rscale);
1206 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1207 s = bc_num_shiftLeft(&cpb, rscale);
1208 }
1209 else {
1210 rdx = b->len - b->rdx;
1211 rscale = rdx * BC_BASE_POWER;
1212 s = bc_num_shiftRight(&cpa, rscale);
1213 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1214 s = bc_num_shiftRight(&cpb, rscale);
Gavin Howardfdbcd062019-04-29 07:36:46 -06001215 }
Gavin Howard034b5b92019-04-27 07:28:56 -06001216
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001217 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard36ec9872019-04-29 10:14:21 -06001218
1219 req = bc_num_int(&cpa);
Gavin Howardc1349922019-04-30 20:48:09 -06001220 if (!req) req = cpa.len - bc_num_nonzeroLen(&cpa);
Gavin Howardcef03972019-04-29 10:28:33 -06001221 req = BC_BASE_POWER * (req + 1);
Gavin Howard36ec9872019-04-29 10:14:21 -06001222 req += b->scale % BC_BASE_POWER == 0 ? BC_BASE_POWER : 0;
Gavin Howard24e3ee92019-05-06 07:41:45 -06001223 req += scale2;
Gavin Howard36ec9872019-04-29 10:14:21 -06001224
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001225 rscale += req;
1226 bc_num_extend(&cpa, req);
1227 bc_num_extend(&cpb, req);
Gavin Howard36ec9872019-04-29 10:14:21 -06001228
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001229 fi = &factor;
1230 fnext = &factor2;
1231 cmp = 1;
Gavin Howard36ec9872019-04-29 10:14:21 -06001232
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001233 while (!bc_num_isOne(fi) && cmp) {
Gavin Howard36ec9872019-04-29 10:14:21 -06001234
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001235 s = bc_num_sub(&two, &cpb, fi, rscale);
1236 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1237 s = bc_num_mul(&cpa, fi, &cpa, rscale);
1238 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1239 s = bc_num_mul(&cpb, fi, &cpb, rscale);
Gavin Howardea844d22019-05-02 09:10:39 -06001240 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1241
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001242 temp = fi;
1243 fi = fnext;
1244 fnext = temp;
Gavin Howardea844d22019-05-02 09:10:39 -06001245
Gavin Howard36ec9872019-04-29 10:14:21 -06001246 cmp = bc_num_cmp(fi, fnext);
Gavin Howardea844d22019-05-02 09:10:39 -06001247 if (cmp == BC_NUM_SSIZE_MIN) goto err;
Gavin Howardea844d22019-05-02 09:10:39 -06001248 }
Gavin Howard36ec9872019-04-29 10:14:21 -06001249
Gavin Howard24e3ee92019-05-06 07:41:45 -06001250 // We only round here if an entire BcDig is equal to BC_BASE_DIG - 1. We
1251 // have to round because without rounding, the Goldschmidt algorithm can
1252 // produce numbers that are 1 digit off in the last place, because of the
1253 // nature of the algorithm. That throws off things like negative powers
1254 // (like 10 ^ -1, which Goldschmidt calculates as 0.099999999...). The bc
1255 // spec requires truncation, but without this rounding, *more* calculations
1256 // will be off. To fix this, I have the algorithm calculate the result to
1257 // the scale plus 1 place plus a whole extra BcDig (at least). Then, when
1258 // rounding, I only round if the extra BcDig is only 1 less than the max,
1259 // and then I only add 1 to the right place (see bc_num_roundPlaces() for
1260 // more info). What this means is that if there is a chain of 9's from the
1261 // *actual* scale position to the rounded position, then rounding will
1262 // happen, but if not, the actual scale will not be affected (i.e., it will
1263 // appear truncated). By extending the calculation by 1 extra digit, then a
1264 // whole extra BcDig, I create a separation between the two that only is
1265 // closed when Goldschmidt has failed to calculate the exact truncated
1266 // number (or at least, I hope it does).
1267 assert(cpa.rdx >= rdx2);
1268 if (cpa.num[cpa.rdx - rdx2] == BC_BASE_DIG - 1)
1269 bc_num_roundPlaces(&cpa, scale2 - 1);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001270 bc_num_copy(c, &cpa);
1271
1272err:
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001273 bc_num_free(&factor2);
Gavin Howard36ec9872019-04-29 10:14:21 -06001274 bc_num_free(&factor);
Gavin Howard8e74e6b2019-05-07 07:28:06 -06001275 bc_num_free(&cpb);
1276 bc_num_free(&cpa);
Gavin Howardfdbcd062019-04-29 07:36:46 -06001277exit:
Gavin Howard36ec9872019-04-29 10:14:21 -06001278 a->neg = aneg;
1279 b->neg = bneg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001280 if (BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfdbcd062019-04-29 07:36:46 -06001281 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001282 return s;
1283}
Stefan Esser45342832019-05-02 10:43:02 +02001284#endif // USE_GOLDSCHMIDT
Gavin Howard6fbdb292018-02-27 15:44:48 -07001285
Gavin Howard1cbfe242019-01-09 17:13:11 -07001286static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
1287 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -06001288{
Gavin Howard63738202018-09-26 15:34:20 -06001289 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -06001290 BcNum temp;
1291 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001292
Gavin Howardddce6d42019-01-03 14:07:06 -07001293 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1294 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -07001295 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001296 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001297 return BC_STATUS_SUCCESS;
1298 }
Gavin Howard8b254872018-03-14 01:13:35 -06001299
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001300 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -07001301 s = bc_num_d(a, b, c, scale);
1302 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001303 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001304
Gavin Howard996fc222019-04-29 13:22:38 -06001305 if (scale) scale = ts + 1;
Gavin Howard3115c012018-10-04 10:53:56 -06001306
Gavin Howard53eba8b2018-10-31 15:14:37 -06001307 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001308 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001309 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001310 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -06001311
Gavin Howard77445432019-04-26 15:59:33 -06001312 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
Gavin Howard18441082018-10-22 10:03:30 -06001313
1314 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001315 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard996fc222019-04-29 13:22:38 -06001316 d->neg = BC_NUM_NONZERO(d) ? neg : false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001317
1318err:
Gavin Howard18441082018-10-22 10:03:30 -06001319 bc_num_free(&temp);
1320 return s;
1321}
1322
Gavin Howard2d188a52019-02-25 14:19:08 -07001323static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1324{
Gavin Howard18441082018-10-22 10:03:30 -06001325 BcStatus s;
1326 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -07001327 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -06001328
Gavin Howard77445432019-04-26 15:59:33 -06001329 ts = bc_vm_growSize(scale, b->scale);
1330 ts = BC_MAX(ts, a->scale);
Gavin Howard798508b2019-02-21 16:45:32 -07001331
1332 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -06001333 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001334 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -06001335
Gavin Howard63738202018-09-26 15:34:20 -06001336 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001337}
1338
Gavin Howard1cbfe242019-01-09 17:13:11 -07001339static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001340
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001341 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001342 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -07001343 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001344 size_t i, powrdx, resrdx;
1345 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001346
Gavin Howardecafd4f2019-02-23 09:30:45 -07001347 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001348
Gavin Howardddce6d42019-01-03 14:07:06 -07001349 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -06001350 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -06001351 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001352 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001353 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001354 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001355 return BC_STATUS_SUCCESS;
1356 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001357 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001358 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -06001359 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001360 return s;
1361 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001362
Gavin Howard63738202018-09-26 15:34:20 -06001363 neg = b->neg;
1364 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001365 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -07001366 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001367 if (s) return s;
1368
Gavin Howardbaa4f582019-01-24 13:56:35 -07001369 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001370
Stefan Eßerb5318342019-04-29 10:31:26 -06001371 if (!neg) scale = BC_MIN(a->scale * pow, BC_MAX(scale, a->scale));
Gavin Howardb29674f2018-03-22 22:24:58 -06001372
Gavin Howardd497c232019-04-29 10:32:38 -06001373 for (powrdx = a->scale; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -06001374 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001375 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001376 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001377 }
Gavin Howard6fb635f2018-03-03 12:45:42 -07001378
Gavin Howard2d188a52019-02-25 14:19:08 -07001379 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -07001380
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001381 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -07001382 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001383
Gavin Howard2d188a52019-02-25 14:19:08 -07001384 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001385
Gavin Howard4d5daba2019-01-07 14:42:50 -07001386 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001387 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001388 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001389
Gavin Howard63738202018-09-26 15:34:20 -06001390 if (pow & 1) {
1391 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001392 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001393 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001394 }
1395 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001396
Gavin Howard2d188a52019-02-25 14:19:08 -07001397 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -07001398 if (neg) {
1399 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001400 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -07001401 }
1402
Stefan Eßerb5318342019-04-29 10:31:26 -06001403 if (c->scale > scale) bc_num_truncate(c, c->scale - scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001404
Gavin Howard53eba8b2018-10-31 15:14:37 -06001405 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -06001406 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001407 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001408
Gavin Howard9b801632019-02-16 23:34:06 -07001409sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001410 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001411err:
Gavin Howard63738202018-09-26 15:34:20 -06001412 bc_num_free(&copy);
1413 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001414}
1415
Gavin Howard7bda4782018-12-28 09:53:22 -07001416#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -07001417static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
1418 size_t scale)
1419{
Gavin Howardac4d9122018-12-27 23:59:12 -07001420 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001421 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001422
1423 BC_UNUSED(scale);
1424
1425 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001426 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001427
Gavin Howardc9bef2d2019-04-26 14:46:52 -06001428 if (val < c->scale) bc_num_truncate(c, c->scale - val);
1429 else if (val > c->scale) bc_num_extend(c, val - c->scale);
Gavin Howardac4d9122018-12-27 23:59:12 -07001430
1431 return s;
1432}
1433
Gavin Howard2d188a52019-02-25 14:19:08 -07001434static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1435{
Gavin Howardac4d9122018-12-27 23:59:12 -07001436 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001437 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001438
1439 BC_UNUSED(scale);
1440
1441 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001442 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001443
Gavin Howardb1558662019-04-26 14:32:55 -06001444 return bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001445}
1446
Gavin Howard2d188a52019-02-25 14:19:08 -07001447static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1448 size_t scale)
1449{
Gavin Howardac4d9122018-12-27 23:59:12 -07001450 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001451 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001452
1453 BC_UNUSED(scale);
1454
1455 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001456 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001457
Gavin Howard2a366922019-01-16 10:50:20 -07001458 if (BC_NUM_ZERO(c)) return s;
1459
Gavin Howardb1558662019-04-26 14:32:55 -06001460 return bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001461}
Gavin Howard7bda4782018-12-28 09:53:22 -07001462#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001463
Gavin Howard1cbfe242019-01-09 17:13:11 -07001464static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1465 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001466{
Gavin Howarda1c44392018-09-27 12:41:15 -06001467 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001468 BcNum num2, *ptr_a, *ptr_b;
1469 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001470
Gavin Howard63738202018-09-26 15:34:20 -06001471 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001472
Gavin Howard890d0c02018-10-30 16:34:50 -06001473 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001474 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001475 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001476 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001477 }
1478 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001479
Gavin Howard63738202018-09-26 15:34:20 -06001480 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001481 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001482 if (c != a) {
1483 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001484 init = true;
1485 }
1486 }
1487 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001488
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001489 if (init) bc_num_init(c, req);
1490 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001491
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001492 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001493
Gavin Howardddce6d42019-01-03 14:07:06 -07001494 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001495 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001496
Gavin Howardba009802018-09-29 04:41:51 -06001497 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001498
Gavin Howardee9d01e2019-02-16 22:48:11 -07001499 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001500}
1501
Gavin Howardc90ed902019-01-02 13:07:49 -07001502#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001503static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001504
Gavin Howardc7d655b2018-12-28 19:45:13 -07001505 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001506 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001507
Gavin Howard63738202018-09-26 15:34:20 -06001508 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001509
Gavin Howard63738202018-09-26 15:34:20 -06001510 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001511
Gavin Howard8a921bd2018-10-11 14:15:32 -06001512 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001513
Gavin Howard63738202018-09-26 15:34:20 -06001514 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001515
Gavin Howard63738202018-09-26 15:34:20 -06001516 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001517
Gavin Howard63738202018-09-26 15:34:20 -06001518 radix = true;
1519 continue;
1520 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001521
Gavin Howardc7d655b2018-12-28 19:45:13 -07001522 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001523 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001524
Gavin Howard63738202018-09-26 15:34:20 -06001525 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001526}
Gavin Howardc90ed902019-01-02 13:07:49 -07001527#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001528
Gavin Howard1cbfe242019-01-09 17:13:11 -07001529static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001530
1531 if (isupper(c)) {
1532 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001533 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001534 }
1535 else c -= '0';
1536
1537 return (unsigned long) (uchar) c;
1538}
1539
Gavin Howard1cbfe242019-01-09 17:13:11 -07001540static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001541
Gavin Howard3b60abb2019-04-26 08:49:17 -06001542 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001543 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001544 bool zero = true, rdx;
Stefan Essera299ffc2019-04-24 23:28:32 +02001545
Gavin Howard63738202018-09-26 15:34:20 -06001546 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001547
Gavin Howard63738202018-09-26 15:34:20 -06001548 val += i;
Gavin Howard46a7d8d2019-05-06 15:16:12 -06001549 assert(!val[0] || isalnum(val[0]) || val[0] == '.');
1550
1551 // All 0's. We can just return, since this
1552 // procedure expects a virgin (already 0) BcNum.
1553 if (!val[0]) return;
1554
Gavin Howard63738202018-09-26 15:34:20 -06001555 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001556
Gavin Howard63738202018-09-26 15:34:20 -06001557 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001558 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001559
Gavin Howardd43ec372019-04-25 21:13:54 -06001560 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001561
Gavin Howardd43ec372019-04-25 21:13:54 -06001562 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001563 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001564
Gavin Howard3b60abb2019-04-26 08:49:17 -06001565 i = len - (ptr == val ? 0 : i) - rdx;
Gavin Howard48a0e472019-04-26 15:39:46 -06001566 temp = BC_NUM_ROUND_POW(i);
Gavin Howard3b60abb2019-04-26 08:49:17 -06001567 mod = n->scale % BC_BASE_POWER;
1568 i = mod ? BC_BASE_POWER - mod : 0;
1569 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001570
1571 bc_num_expand(n, n->len);
Gavin Howard404ece72019-04-26 15:59:15 -06001572 memset(n->num, 0, BC_NUM_SIZE(n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -07001573
Gavin Howard6076dbb2019-04-26 09:47:50 -06001574 if (zero) n->len = n->rdx = 0;
1575 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001576
Gavin Howard971a2672019-04-26 14:32:07 -06001577 unsigned long exp, pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001578
Gavin Howard3b60abb2019-04-26 08:49:17 -06001579 exp = i;
Stefan Esser6167e222019-05-02 10:43:21 +02001580 pow = bc_num_pow10(exp);
Gavin Howardd43ec372019-04-25 21:13:54 -06001581
1582 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001583
Gavin Howardc7d655b2018-12-28 19:45:13 -07001584 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001585
Gavin Howardd43ec372019-04-25 21:13:54 -06001586 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001587 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001588
1589 size_t idx = exp / BC_BASE_POWER;
1590
Gavin Howard8dd307e2019-01-08 23:05:19 -07001591 if (isupper(c)) c = '9';
Gavin Howardf196fbe2019-04-26 08:49:05 -06001592 n->num[idx] += (((unsigned long) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001593
1594 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1595 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001596 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001597 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001598 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001599}
1600
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001601static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1602 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001603{
1604 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001605 BcNum temp, mult, result;
Gavin Howard77445432019-04-26 15:59:33 -06001606 char c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001607 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001608 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001609 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001610
Gavin Howard11b9afd2018-10-18 14:23:28 -06001611 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001612 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001613
Gavin Howard26d078f2019-01-24 13:58:12 -07001614 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1615 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001616
Gavin Howardc7d655b2018-12-28 19:45:13 -07001617 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001618
Gavin Howardc7d655b2018-12-28 19:45:13 -07001619 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001620
Gavin Howard53eba8b2018-10-31 15:14:37 -06001621 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001622 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001623 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001624 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001625 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001626 }
Gavin Howardede51f02018-03-02 12:30:00 -07001627
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001628 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001629
Gavin Howard63738202018-09-26 15:34:20 -06001630 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001631 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001632 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001633
Gavin Howard2d188a52019-02-25 14:19:08 -07001634 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001635 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001636 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001637
Gavin Howard53eba8b2018-10-31 15:14:37 -06001638 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001639 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001640
Gavin Howard705c4bc2018-12-03 15:38:04 -07001641 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001642 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001643 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001644 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001645 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001646 }
Gavin Howardede51f02018-03-02 12:30:00 -07001647
Gavin Howardf2ee6342019-04-09 17:05:20 -06001648 if (BC_SIG) {
1649 s = BC_STATUS_SIGNAL;
1650 goto err;
1651 }
1652
Gavin Howard1769abc2019-02-21 09:36:14 -07001653 // This one cannot be a divide by 0 because mult starts out at 1, then is
1654 // multiplied by base, and base cannot be 0, so mult cannot be 0.
Gavin Howard8b4656f2019-04-30 21:18:00 -06001655 s = bc_num_div(&result, &mult, &result, digs * 2);
Gavin Howard1769abc2019-02-21 09:36:14 -07001656 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001657 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard8b4656f2019-04-30 21:18:00 -06001658 bc_num_truncate(&result, digs);
Gavin Howard1769abc2019-02-21 09:36:14 -07001659 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001660 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001661
Gavin Howardddce6d42019-01-03 14:07:06 -07001662 if (BC_NUM_NONZERO(n)) {
Gavin Howardb0642be2019-04-30 21:12:22 -06001663 if (n->scale < digs) bc_num_extend(n, digs - n->scale);
Gavin Howard63738202018-09-26 15:34:20 -06001664 }
1665 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001666
Gavin Howardd1417682019-02-15 17:08:42 -07001667err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001668 bc_num_free(&result);
1669int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001670 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001671 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001672 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001673}
1674
Gavin Howard1cbfe242019-01-09 17:13:11 -07001675static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001676 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001677 bc_vm_putchar('\\');
1678 bc_vm_putchar('\n');
Gavin Howard63738202018-09-26 15:34:20 -06001679 }
Gavin Howard80977b22018-09-06 14:29:13 -06001680}
1681
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001682static void bc_num_putchar(int c) {
1683 if (c != '\n') bc_num_printNewline();
1684 bc_vm_putchar(c);
1685}
1686
Gavin Howard40a085f2018-12-03 12:08:59 -07001687#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001688static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001689 BC_UNUSED(rdx);
Gavin Howardeedfb2c2019-05-07 08:00:13 -06001690 BC_UNUSED(len);
1691 assert(len == 1);
Gavin Howard3c02ed32018-12-28 18:17:15 -07001692 bc_vm_putchar((uchar) n);
Gavin Howard83eb8392018-10-09 01:21:19 -06001693}
1694#endif // DC_ENABLED
1695
Gavin Howard1cbfe242019-01-09 17:13:11 -07001696static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1697
Gavin Howarda84ad992018-12-03 19:11:06 -07001698 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001699
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001700 bc_num_putchar(rdx ? '.' : ' ');
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001701
Gavin Howard530e3072019-04-23 16:23:36 -06001702 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001703
Gavin Howardeedfb2c2019-05-07 08:00:13 -06001704 for (exp = 0; exp < len; pow /= BC_BASE, ++exp) {
Gavin Howard4b075f22019-05-07 15:27:53 -06001705 size_t dig = n / pow;
Gavin Howard3c02ed32018-12-28 18:17:15 -07001706 n -= dig * pow;
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001707 bc_num_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001708 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001709}
Gavin Howardfe679f02018-02-14 15:50:09 -07001710
Gavin Howard1cbfe242019-01-09 17:13:11 -07001711static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001712
Gavin Howardeedfb2c2019-05-07 08:00:13 -06001713 BC_UNUSED(len);
1714
Gavin Howard3c02ed32018-12-28 18:17:15 -07001715 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001716
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001717 if (rdx) bc_num_putchar('.');
Gavin Howard2682a1f2018-03-03 09:09:13 -07001718
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001719 bc_num_putchar(bc_num_hex_digits[n]);
Gavin Howard2682a1f2018-03-03 09:09:13 -07001720}
1721
Gavin Howard1cbfe242019-01-09 17:13:11 -07001722static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001723
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001724 size_t i, j, rdx = n->rdx;
1725 bool zero = true;
1726 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001727
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001728 if (n->neg) bc_num_putchar('-');
Gavin Howard32f2beb2018-03-09 11:43:20 -07001729
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001730 for (i = n->len - 1; i < n->len; --i) {
1731
1732 BcDig n9 = n->num[i];
1733 size_t temp;
1734 bool irdx = (i == rdx - 1);
1735
1736 zero = (zero & !irdx);
1737 temp = n->scale % BC_BASE_POWER;
1738 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1739
1740 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1741
1742 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Essera299ffc2019-04-24 23:28:32 +02001743 buffer[j] = n9 % BC_BASE;
1744 n9 /= BC_BASE;
1745 }
Stefan Esser0da17752019-04-25 12:25:15 +02001746
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001747 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1748 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1749 zero = (zero && buffer[j] == 0);
1750 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Essera299ffc2019-04-24 23:28:32 +02001751 }
1752 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001753}
1754
Gavin Howard7ad5a662019-02-19 14:40:46 -07001755#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001756static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) {
Gavin Howard7ad5a662019-02-19 14:40:46 -07001757
Gavin Howardb1558662019-04-26 14:32:55 -06001758 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001759 bool neg = (n->len <= n->rdx);
1760 BcNum temp, exp;
1761 size_t places, mod;
1762 BcDig digs[BC_NUM_LONG_LOG10];
1763
1764 bc_num_createCopy(&temp, n);
1765
1766 if (neg) {
Gavin Howardc1349922019-04-30 20:48:09 -06001767
1768 size_t i, idx = bc_num_nonzeroLen(n) - 1;
1769
1770 places = 1;
1771
1772 for (i = BC_BASE_POWER - 1; i < BC_BASE_POWER; --i) {
Gavin Howardeedfb2c2019-05-07 08:00:13 -06001773 if (bc_num_pow10(i) > (unsigned long) n->num[idx]) places += 1;
Gavin Howardc1349922019-04-30 20:48:09 -06001774 else break;
1775 }
1776
1777 places += (n->rdx - (idx + 1)) * BC_BASE_POWER;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001778 mod = places % 3;
Gavin Howardc1349922019-04-30 20:48:09 -06001779
Gavin Howard7ad5a662019-02-19 14:40:46 -07001780 if (eng && mod != 0) places += 3 - mod;
Gavin Howardb1558662019-04-26 14:32:55 -06001781 s = bc_num_shiftLeft(&temp, places);
1782 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001783 }
1784 else {
Stefan Eßerc2e08932019-04-29 14:11:31 -06001785 places = bc_num_int_digits(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001786 mod = places % 3;
1787 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howardb1558662019-04-26 14:32:55 -06001788 s = bc_num_shiftRight(&temp, places);
1789 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001790 }
1791
1792 bc_num_printDecimal(&temp);
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001793 bc_num_putchar('e');
Gavin Howard7ad5a662019-02-19 14:40:46 -07001794
1795 if (!places) {
1796 bc_num_printHex(0, 1, false);
1797 goto exit;
1798 }
1799
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001800 if (neg) bc_num_putchar('-');
Gavin Howard7ad5a662019-02-19 14:40:46 -07001801
1802 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1803 bc_num_ulong2num(&exp, (unsigned long) places);
1804
1805 bc_num_printDecimal(&exp);
1806
1807exit:
1808 bc_num_free(&temp);
Gavin Howardb1558662019-04-26 14:32:55 -06001809 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001810}
1811#endif // BC_ENABLE_EXTRA_MATH
1812
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001813static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1814 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001815{
Gavin Howard63738202018-09-26 15:34:20 -06001816 BcStatus s;
1817 BcVec stack;
Gavin Howarda8c2e582019-05-07 11:33:10 -06001818 BcNum intp1, intp2, fracp1, fracp2, digit, flen1, flen2, *n1, *n2, *temp;
Gavin Howard63738202018-09-26 15:34:20 -06001819 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001820 size_t i;
1821 bool radix;
Gavin Howardc4e9a082019-05-07 11:12:50 -06001822 BcDig digit_digs[BC_NUM_LONG_LOG10];
Gavin Howarda8c2e582019-05-07 11:33:10 -06001823 BcDig flen1_digs[BC_NUM_LONG_LOG10];
1824 BcDig flen2_digs[BC_NUM_LONG_LOG10];
Gavin Howard2682a1f2018-03-03 09:09:13 -07001825
Gavin Howarda86faf32019-05-07 11:34:00 -06001826 assert(BC_NUM_NONZERO(base) && !bc_num_isOne(base));
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001827
Gavin Howardddce6d42019-01-03 14:07:06 -07001828 if (BC_NUM_ZERO(n)) {
1829 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001830 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001831 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001832
Gavin Howardd3929832018-12-24 15:13:15 -07001833 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howarda8c2e582019-05-07 11:33:10 -06001834 bc_num_init(&fracp1, n->rdx);
1835 bc_num_init(&fracp2, n->rdx);
Gavin Howardc4e9a082019-05-07 11:12:50 -06001836 bc_num_setup(&digit, digit_digs, sizeof(digit_digs) / sizeof(BcDig));
Gavin Howarda8c2e582019-05-07 11:33:10 -06001837 bc_num_setup(&flen1, flen1_digs, sizeof(flen1_digs) / sizeof(BcDig));
1838 bc_num_setup(&flen2, flen2_digs, sizeof(flen2_digs) / sizeof(BcDig));
1839 bc_num_one(&flen1);
Gavin Howardc4e9a082019-05-07 11:12:50 -06001840 bc_num_createCopy(&intp1, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001841
Gavin Howardc4e9a082019-05-07 11:12:50 -06001842 bc_num_truncate(&intp1, intp1.scale);
1843 bc_num_init(&intp2, intp1.len);
1844
Gavin Howarda8c2e582019-05-07 11:33:10 -06001845 s = bc_num_sub(n, &intp1, &fracp1, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001846 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001847
Gavin Howarda8c2e582019-05-07 11:33:10 -06001848 n1 = &intp1;
1849 n2 = &intp2;
Gavin Howardc4e9a082019-05-07 11:12:50 -06001850
Gavin Howarda8c2e582019-05-07 11:33:10 -06001851 while (BC_NO_SIG && BC_NUM_NONZERO(n1)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001852
1853 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howarda8c2e582019-05-07 11:33:10 -06001854 s = bc_num_divmod(n1, base, n2, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001855 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001856
1857 // Will never fail (except for signals) because digit is
1858 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001859 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001860 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001861
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001862 bc_vec_push(&stack, &dig);
Gavin Howardc4e9a082019-05-07 11:12:50 -06001863
Gavin Howarda8c2e582019-05-07 11:33:10 -06001864 temp = n1;
1865 n1 = n2;
1866 n2 = temp;
Gavin Howard63738202018-09-26 15:34:20 -06001867 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001868
Gavin Howard2d188a52019-02-25 14:19:08 -07001869 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001870
Gavin Howard2d188a52019-02-25 14:19:08 -07001871 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001872 ptr = bc_vec_item_rev(&stack, i);
1873 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001874 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001875 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001876
Gavin Howard2d188a52019-02-25 14:19:08 -07001877 if (BC_SIG) goto sig_err;
Stefan Esser276de8c2019-05-04 20:12:43 +02001878 if (!n->scale) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001879
Gavin Howardfbae8a52019-05-07 11:15:38 -06001880 radix = true;
Gavin Howarda8c2e582019-05-07 11:33:10 -06001881 n1 = &flen1;
1882 n2 = &flen2;
Gavin Howardfbae8a52019-05-07 11:15:38 -06001883
Gavin Howarda8c2e582019-05-07 11:33:10 -06001884 while (BC_NO_SIG && bc_num_int_digits(n1) < n->scale + 1) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001885
Gavin Howarda8c2e582019-05-07 11:33:10 -06001886 s = bc_num_mul(&fracp1, base, &fracp2, n->scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001887 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001888
1889 // Will never fail (except for signals) because fracp is
1890 // guaranteed to be non-negative and small enough.
Gavin Howarda8c2e582019-05-07 11:33:10 -06001891 s = bc_num_ulong(&fracp2, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001892 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001893
Gavin Howard43b6fd72019-05-07 11:39:19 -06001894 bc_num_ulong2num(&digit, dig);
1895 s = bc_num_sub(&fracp2, &digit, &fracp1, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001896 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001897
Gavin Howardddce6d42019-01-03 14:07:06 -07001898 print(dig, len, radix);
Gavin Howarda8c2e582019-05-07 11:33:10 -06001899 s = bc_num_mul(n1, base, n2, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001900 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardfbae8a52019-05-07 11:15:38 -06001901
1902 radix = false;
Gavin Howarda8c2e582019-05-07 11:33:10 -06001903 temp = n1;
1904 n1 = n2;
1905 n2 = temp;
Gavin Howard63738202018-09-26 15:34:20 -06001906 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001907
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001908sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001909 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001910err:
Gavin Howardc4e9a082019-05-07 11:12:50 -06001911 bc_num_free(&intp2);
Gavin Howardea8d5132019-05-07 11:52:04 -06001912 bc_num_free(&intp1);
Gavin Howarda8c2e582019-05-07 11:33:10 -06001913 bc_num_free(&fracp2);
1914 bc_num_free(&fracp1);
Gavin Howard63738202018-09-26 15:34:20 -06001915 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001916 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001917}
1918
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001919static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1920 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001921{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001922 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001923 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001924 BcNumDigitOp print;
1925 bool neg = n->neg;
1926
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001927 if (neg) bc_num_putchar('-');
Gavin Howard83eb8392018-10-09 01:21:19 -06001928
1929 n->neg = false;
1930
Gavin Howardbe4e8222019-01-03 13:34:16 -07001931 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001932 width = 1;
1933 print = bc_num_printHex;
1934 }
1935 else {
Stefan Esser017f0c82019-05-04 17:01:21 +02001936 width = bc_num_log10(base_t - 1);
Gavin Howard83eb8392018-10-09 01:21:19 -06001937 print = bc_num_printDigits;
1938 }
1939
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001940 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001941 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001942
1943 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001944}
1945
Gavin Howard40a085f2018-12-03 12:08:59 -07001946#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001947BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1948 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001949}
1950#endif // DC_ENABLED
1951
Gavin Howard3c02ed32018-12-28 18:17:15 -07001952void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001953 assert(n);
1954 n->num = num;
1955 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001956 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001957 n->neg = false;
1958}
1959
Gavin Howard3c02ed32018-12-28 18:17:15 -07001960void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001961 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001962 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howard404ece72019-04-26 15:59:15 -06001963 bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001964}
1965
Gavin Howarded392aa2018-02-27 13:09:26 -07001966void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001967 assert(num);
1968 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001969}
1970
Gavin Howard3c02ed32018-12-28 18:17:15 -07001971void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001972 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001973 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001974 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001975 d->len = s->len;
1976 d->neg = s->neg;
1977 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001978 d->scale = s->scale;
Gavin Howard404ece72019-04-26 15:59:15 -06001979 memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -07001980}
1981
Gavin Howardbaa4f582019-01-24 13:56:35 -07001982void bc_num_createCopy(BcNum *d, const BcNum *s) {
1983 bc_num_init(d, s->len);
1984 bc_num_copy(d, s);
1985}
1986
1987void bc_num_createFromUlong(BcNum *n, unsigned long val) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001988 bc_num_init(n, (BC_NUM_LONG_LOG10 - 1) / BC_BASE_POWER + 1);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001989 bc_num_ulong2num(n, val);
1990}
1991
Gavin Howard7ad5a662019-02-19 14:40:46 -07001992size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001993 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001994}
1995
1996size_t bc_num_len(const BcNum *restrict n) {
1997
Gavin Howardb41483b2019-04-27 08:30:10 -06001998 size_t i, pow, scale, len = n->len;
Gavin Howardfcf29012019-04-25 20:12:19 -06001999 BcDig dig;
2000
Gavin Howardb41483b2019-04-27 08:30:10 -06002001 if (BC_NUM_ZERO(n)) return 0;
Gavin Howardc1349922019-04-30 20:48:09 -06002002 if (n->rdx == len) len = bc_num_nonzeroLen(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07002003
Gavin Howardfcf29012019-04-25 20:12:19 -06002004 dig = n->num[len - 1];
Gavin Howardb41483b2019-04-27 08:30:10 -06002005 pow = BC_BASE_DIG;
2006 i = BC_BASE_POWER + 1;
Gavin Howardfcf29012019-04-25 20:12:19 -06002007
Gavin Howardb41483b2019-04-27 08:30:10 -06002008 while (pow && (dig % (BcDig) pow == dig)) {
2009 i -= 1;
2010 pow /= BC_BASE;
2011 }
Gavin Howardfcf29012019-04-25 20:12:19 -06002012
Gavin Howardb41483b2019-04-27 08:30:10 -06002013 scale = n->scale % BC_BASE_POWER;
2014 scale = scale ? scale : BC_BASE_POWER;
2015
2016 return (len - 1) * BC_BASE_POWER + i - (BC_BASE_POWER - scale);
Gavin Howard7ad5a662019-02-19 14:40:46 -07002017}
2018
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07002019BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
2020 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07002021{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07002022 BcStatus s = BC_STATUS_SUCCESS;
2023
Gavin Howard63738202018-09-26 15:34:20 -06002024 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07002025 assert(BC_ENABLE_EXTRA_MATH ||
2026 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07002027 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002028
Gavin Howardd746e802018-12-28 22:06:00 -07002029 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06002030 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07002031 else s = bc_num_parseBase(n, val, base, base_t);
2032
2033 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07002034}
2035
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07002036BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
2037 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07002038{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07002039 BcStatus s = BC_STATUS_SUCCESS;
2040
Gavin Howardab5e9632019-01-02 13:32:02 -07002041 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07002042 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07002043
Gavin Howardab5e9632019-01-02 13:32:02 -07002044 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06002045
Gavin Howardddce6d42019-01-03 14:07:06 -07002046 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06002047 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07002048#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06002049 else if (base_t == 0 || base_t == 1)
2050 s = bc_num_printExponent(n, base_t != 0);
Gavin Howard7ad5a662019-02-19 14:40:46 -07002051#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07002052 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07002053
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06002054 if (BC_NO_ERR(!s) && newline) bc_num_putchar('\n');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07002055
2056 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07002057}
2058
Gavin Howard3c02ed32018-12-28 18:17:15 -07002059BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07002060
Gavin Howard63738202018-09-26 15:34:20 -06002061 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07002062 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07002063
Gavin Howard63738202018-09-26 15:34:20 -06002064 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07002065
Gavin Howardecafd4f2019-02-23 09:30:45 -07002066 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07002067
Gavin Howard06b6e862019-02-15 11:15:31 -07002068 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07002069
Gavin Howard0700efc2019-04-24 17:31:23 -06002070 unsigned long prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07002071
Gavin Howard0700efc2019-04-24 17:31:23 -06002072 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07002073 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
2074
Gavin Howard77445432019-04-26 15:59:33 -06002075 r = prev + (unsigned long) n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07002076
2077 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07002078 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06002079 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07002080
Gavin Howard7b557da2018-12-21 10:25:32 -07002081 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07002082
Gavin Howard63738202018-09-26 15:34:20 -06002083 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07002084}
2085
Gavin Howard3c02ed32018-12-28 18:17:15 -07002086void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07002087
Gavin Howardc25fd612019-04-26 19:31:31 -06002088 BcDig *ptr;
2089 unsigned long i;
2090
Gavin Howard63738202018-09-26 15:34:20 -06002091 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07002092
Gavin Howard63738202018-09-26 15:34:20 -06002093 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07002094
Gavin Howard1ab22d22019-01-03 13:32:17 -07002095 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07002096
Gavin Howard65f30192019-05-07 11:51:03 -06002097 bc_num_expand(n, BC_NUM_LONG_LOG10);
Gavin Howard05feab02019-04-23 16:24:07 -06002098
Gavin Howardc25fd612019-04-26 19:31:31 -06002099 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_DIG)
2100 ptr[i] = val % BC_BASE_DIG;
Gavin Howard025d04d2018-02-20 13:53:28 -07002101}
2102
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002103size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07002104
2105 size_t aint, bint, ardx, brdx;
2106
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002107 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07002108
2109 ardx = a->rdx;
2110 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07002111 aint = bc_num_int(a);
2112 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07002113 ardx = BC_MAX(ardx, brdx);
2114 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07002115
Gavin Howard2d188a52019-02-25 14:19:08 -07002116 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002117}
2118
2119size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07002120 size_t max, rdx;
2121 rdx = bc_vm_growSize(a->rdx, b->rdx);
Stefan Eßer0dc37b62019-04-29 14:13:11 -06002122 max = bc_vm_growSize(BC_MAX(BC_NUM_RDX(scale), rdx), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07002123 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
2124 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002125}
2126
2127size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
2128 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07002129 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002130}
2131
2132#if BC_ENABLE_EXTRA_MATH
Gavin Howard194380e2019-05-06 08:39:30 -06002133size_t bc_num_placesReq(BcNum *a, BcNum *b, size_t scale) {
2134
2135 BcStatus s;
2136 unsigned long places;
2137 size_t rdx;
2138
2139 BC_UNUSED(s);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002140 BC_UNUSED(scale);
Gavin Howard194380e2019-05-06 08:39:30 -06002141
Gavin Howard194380e2019-05-06 08:39:30 -06002142 // This error will be taken care of later. Ignore.
Gavin Howard8291ecd2019-05-06 14:34:53 -06002143 s = bc_num_ulong(b, &places);
Gavin Howard194380e2019-05-06 08:39:30 -06002144
2145 if (a->scale <= places) rdx = BC_NUM_RDX(places);
2146 else rdx = BC_NUM_RDX(a->scale - places);
2147
2148 return BC_NUM_RDX(bc_num_int_digits(a)) + rdx;
2149}
2150
2151size_t bc_num_shiftLeftReq(BcNum *a, BcNum *b, size_t scale) {
2152
2153 BcStatus s;
2154 unsigned long places, rdx;
2155
2156 BC_UNUSED(s);
2157 BC_UNUSED(scale);
2158
Gavin Howard194380e2019-05-06 08:39:30 -06002159 // This error will be taken care of later. Ignore.
Gavin Howard8291ecd2019-05-06 14:34:53 -06002160 s = bc_num_ulong(b, &places);
Gavin Howard194380e2019-05-06 08:39:30 -06002161
2162 if (a->scale <= places) rdx = BC_NUM_RDX(places) - a->rdx + 1;
2163 else rdx = 0;
2164
2165 return a->len + rdx;
2166}
2167
2168size_t bc_num_shiftRightReq(BcNum *a, BcNum *b, size_t scale) {
2169
2170 BcStatus s;
2171 unsigned long places, int_digs, rdx;
2172
2173 BC_UNUSED(s);
2174 BC_UNUSED(scale);
2175
Gavin Howard194380e2019-05-06 08:39:30 -06002176 // This error will be taken care of later. Ignore.
Gavin Howard8291ecd2019-05-06 14:34:53 -06002177 s = bc_num_ulong(b, &places);
Gavin Howard194380e2019-05-06 08:39:30 -06002178
2179 int_digs = BC_NUM_RDX(bc_num_int_digits(a));
2180 rdx = BC_NUM_RDX(places);
2181
2182 if (int_digs <= rdx) rdx -= int_digs;
2183 else rdx = 0;
2184
2185 return a->len + rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002186}
2187#endif // BC_ENABLE_EXTRA_MATH
2188
Gavin Howard12fe7812018-09-29 03:45:18 -06002189BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06002190 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07002191 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002192 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002193}
2194
Gavin Howard12fe7812018-09-29 03:45:18 -06002195BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06002196 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07002197 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002198 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002199}
2200
Gavin Howard12fe7812018-09-29 03:45:18 -06002201BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002202 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002203}
2204
Gavin Howard12fe7812018-09-29 03:45:18 -06002205BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002206 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002207}
2208
Gavin Howard54b946a2018-10-23 12:06:57 -06002209BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07002210 size_t req = bc_num_mulReq(a, b, scale);
2211 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07002212}
2213
Gavin Howard12fe7812018-09-29 03:45:18 -06002214BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07002215 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002216}
2217
Gavin Howard7d8f0382018-12-27 16:15:13 -07002218#if BC_ENABLE_EXTRA_MATH
2219BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard8291ecd2019-05-06 14:34:53 -06002220 size_t req = bc_num_placesReq(a, b, scale);
2221 return bc_num_binary(a, b, c, scale, bc_num_place, req);
Gavin Howard7d8f0382018-12-27 16:15:13 -07002222}
2223
2224BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard8291ecd2019-05-06 14:34:53 -06002225 size_t req = bc_num_shiftLeftReq(a, b, scale);
2226 return bc_num_binary(a, b, c, scale, bc_num_left, req);
Gavin Howard7d8f0382018-12-27 16:15:13 -07002227}
2228
2229BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard8291ecd2019-05-06 14:34:53 -06002230 size_t req = bc_num_shiftRightReq(a, b, scale);
2231 return bc_num_binary(a, b, c, scale, bc_num_right, req);
Gavin Howard7d8f0382018-12-27 16:15:13 -07002232}
2233#endif // BC_ENABLE_EXTRA_MATH
2234
Gavin Howard3c02ed32018-12-28 18:17:15 -07002235BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06002236
Gavin Howard1cbfe242019-01-09 17:13:11 -07002237 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002238 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Stefan Eßer4276af42019-04-29 14:04:57 -06002239 size_t pow, len, rdx, req, digs, digs1, digs2, resscale, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002240 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Stefan Eßer4276af42019-04-29 14:04:57 -06002241 BcDig half_digs[1];
Gavin Howard954276b2018-05-16 01:43:58 -06002242
Gavin Howard34edd0a2018-10-25 15:51:05 -06002243 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06002244
Gavin Howardecafd4f2019-02-23 09:30:45 -07002245 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07002246
Stefan Eßer4276af42019-04-29 14:04:57 -06002247 if (a->scale > scale) scale = a->scale;
2248 len = bc_vm_growSize(bc_num_int_digits(a), 1);
2249 req = bc_vm_growSize(BC_MAX(BC_NUM_RDX(scale), a->rdx), len >> 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07002250 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06002251
Gavin Howardddce6d42019-01-03 14:07:06 -07002252 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06002253 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002254 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06002255 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07002256 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06002257 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002258 bc_num_extend(b, scale);
2259 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06002260 }
Gavin Howard954276b2018-05-16 01:43:58 -06002261
Stefan Eßer4276af42019-04-29 14:04:57 -06002262 rdx = BC_MAX(BC_NUM_RDX(scale), a->rdx);
2263 len = bc_vm_growSize(a->len, rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06002264
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002265 bc_num_init(&num1, len);
2266 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07002267 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06002268
Gavin Howard63738202018-09-26 15:34:20 -06002269 bc_num_one(&half);
Stefan Eßer4276af42019-04-29 14:04:57 -06002270 half.num[0] = BC_BASE_DIG / 2;
2271 half.len = 1;
Gavin Howard63738202018-09-26 15:34:20 -06002272 half.rdx = 1;
Stefan Eßer4276af42019-04-29 14:04:57 -06002273 half.scale = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06002274
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002275 bc_num_init(&f, len);
2276 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06002277
Gavin Howard63738202018-09-26 15:34:20 -06002278 x0 = &num1;
2279 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06002280
Gavin Howard63738202018-09-26 15:34:20 -06002281 bc_num_one(x0);
Stefan Eßer4276af42019-04-29 14:04:57 -06002282 pow = bc_num_int_digits(a);
Gavin Howard954276b2018-05-16 01:43:58 -06002283
Gavin Howard53eba8b2018-10-31 15:14:37 -06002284 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06002285
Gavin Howardc39fd492018-10-04 10:07:03 -06002286 if (pow & 1) x0->num[0] = 2;
2287 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06002288
Gavin Howardc39fd492018-10-04 10:07:03 -06002289 pow -= 2 - (pow & 1);
Gavin Howard74704572019-05-07 13:48:48 -06002290 s = bc_num_shiftLeft(x0, pow / 2);
2291 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06002292
Gavin Howard63738202018-09-26 15:34:20 -06002293 // Make sure to move the radix back.
Gavin Howard1d857192019-05-06 14:35:07 -06002294 if (x0->scale >= pow) x0->scale -= pow;
2295 else x0->scale = 0;
Stefan Eßer4276af42019-04-29 14:04:57 -06002296 x0->rdx = BC_NUM_RDX(x0->scale);
Gavin Howard63738202018-09-26 15:34:20 -06002297 }
Gavin Howard954276b2018-05-16 01:43:58 -06002298
Gavin Howard14913fc2019-04-23 14:36:03 -06002299 x0->rdx = digs = digs1 = digs2 = 0;
Stefan Eßer4276af42019-04-29 14:04:57 -06002300 x0->scale = 0;
2301 resscale = (scale + BC_BASE_POWER) * 2;
2302
Gavin Howard28c087f2019-05-06 08:08:59 -06002303 len = BC_NUM_RDX(bc_num_int_digits(x0) + resscale - 1);
Gavin Howard954276b2018-05-16 01:43:58 -06002304
Gavin Howard2d188a52019-02-25 14:19:08 -07002305 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06002306
Gavin Howard971a2672019-04-26 14:32:07 -06002307 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07002308
Stefan Eßer4276af42019-04-29 14:04:57 -06002309 s = bc_num_div(a, x0, &f, resscale);
Gavin Howard1769abc2019-02-21 09:36:14 -07002310 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002311 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Eßer4276af42019-04-29 14:04:57 -06002312 s = bc_num_add(x0, &f, &fprime, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07002313 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Eßer4276af42019-04-29 14:04:57 -06002314 s = bc_num_mul(&fprime, &half, x1, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07002315 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06002316
Gavin Howard53eba8b2018-10-31 15:14:37 -06002317 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06002318 if (cmp == BC_NUM_SSIZE_MIN) {
2319 s = BC_STATUS_SIGNAL;
2320 break;
2321 }
2322
Gavin Howard53eba8b2018-10-31 15:14:37 -06002323 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06002324
Gavin Howardb5ec7f32018-10-29 12:32:39 -06002325 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06002326 else times = 0;
2327
Stefan Eßer4276af42019-04-29 14:04:57 -06002328 resscale += times > 2;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002329
2330 cmp2 = cmp1;
2331 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06002332 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002333
Gavin Howard63738202018-09-26 15:34:20 -06002334 temp = x0;
2335 x0 = x1;
2336 x1 = temp;
2337 }
Gavin Howard954276b2018-05-16 01:43:58 -06002338
Gavin Howard2d188a52019-02-25 14:19:08 -07002339 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07002340 s = BC_STATUS_SIGNAL;
2341 goto err;
2342 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06002343
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002344 bc_num_copy(b, x0);
Stefan Eßer4276af42019-04-29 14:04:57 -06002345 if (b->scale > scale) bc_num_truncate(b, b->scale - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06002346
2347err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06002348 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06002349 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06002350 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06002351 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06002352 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07002353 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002354 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06002355 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07002356}
Gavin Howardba009802018-09-29 04:41:51 -06002357
Gavin Howard2cb39612018-10-22 08:46:48 -06002358BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
2359
2360 BcStatus s;
2361 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06002362 bool init = false;
Stefan Eßera7fbbf62019-04-29 14:14:30 -06002363 size_t ts, len;
2364
2365 ts = BC_MAX(scale + b->scale, a->scale);
2366 len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002367
Gavin Howardf2ef2f32019-01-16 11:25:16 -07002368 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06002369
Gavin Howard890d0c02018-10-30 16:34:50 -06002370 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06002371 memcpy(&num2, c, sizeof(BcNum));
2372 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002373 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06002374 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06002375 }
Gavin Howardf679ad82018-10-29 12:26:30 -06002376 else {
2377 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002378 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06002379 }
Gavin Howard2cb39612018-10-22 08:46:48 -06002380
Gavin Howard54b946a2018-10-23 12:06:57 -06002381 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002382
Gavin Howardddce6d42019-01-03 14:07:06 -07002383 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002384 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07002385 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002386 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06002387
2388 if (init) bc_num_free(&num2);
2389
2390 return s;
2391}
2392
Gavin Howard40a085f2018-12-03 12:08:59 -07002393#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06002394BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06002395
2396 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002397 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07002398 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06002399
Gavin Howard34edd0a2018-10-25 15:51:05 -06002400 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06002401
Gavin Howardecafd4f2019-02-23 09:30:45 -07002402 if (BC_ERR(BC_NUM_ZERO(c)))
2403 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
2404 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
2405 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07002406 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06002407
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002408 bc_num_expand(d, c->len);
2409 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07002410 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002411 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06002412
2413 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06002414 two.num[0] = 2;
2415 bc_num_one(d);
2416
Gavin Howard1769abc2019-02-21 09:36:14 -07002417 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002418 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002419 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002420 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07002421 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06002422
Gavin Howard2d188a52019-02-25 14:19:08 -07002423 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06002424
Gavin Howard1769abc2019-02-21 09:36:14 -07002425 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002426 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002427 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002428
Gavin Howarda83a5e52019-02-21 16:51:06 -07002429 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06002430 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002431 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002432
2433 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002434 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002435 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002436 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002437 }
2438
Gavin Howard53eba8b2018-10-31 15:14:37 -06002439 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002440 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002441
2442 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002443 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002444 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002445 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002446 }
2447
Gavin Howard2d188a52019-02-25 14:19:08 -07002448 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002449
Gavin Howardba009802018-09-29 04:41:51 -06002450err:
Gavin Howardba009802018-09-29 04:41:51 -06002451 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002452rem_err:
2453 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002454 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002455 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002456 return s;
2457}
Gavin Howarde6e84762018-10-03 11:46:34 -06002458#endif // DC_ENABLED
Gavin Howardefc1e202019-05-06 08:01:07 -06002459
2460#if BC_DEBUG_CODE
2461void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline) {
2462 printf("%s: ", name);
2463 bc_num_printDecimal(n);
2464 printf("\n");
2465 if (emptyline) printf("\n");
Gavin Howarde34f7d82019-05-07 10:32:39 -06002466 vm->nchars = 0;
Gavin Howardefc1e202019-05-06 08:01:07 -06002467}
2468
2469void bc_num_printDigs(const BcNum *n, const char *name, bool emptyline) {
2470
2471 size_t i;
2472
2473 printf("%s len: %zu, rdx: %zu, scale: %zu\n",
2474 name, n->len, n->rdx, n->scale);
2475
2476 for (i = n->len - 1; i < n->len; --i)
2477 printf(" %0*d", BC_BASE_POWER, n->num[i]);
2478
2479 printf("\n");
2480 if (emptyline) printf("\n");
Gavin Howarde34f7d82019-05-07 10:32:39 -06002481 vm->nchars = 0;
Gavin Howardefc1e202019-05-06 08:01:07 -06002482}
2483
2484void bc_num_dump(const char *varname, const BcNum *n) {
2485
2486 unsigned long i, scale = n->scale;
2487
2488 fprintf(stderr, "\n%s = %s", varname, n->len ? (n->neg ? "-" : "+") : "0 ");
2489
2490 for (i = n->len - 1; i < n->len; --i) {
2491
2492 if (i + 1 == n->rdx) fprintf(stderr, ". ");
2493
2494 if (scale / BC_BASE_POWER != n->rdx - i - 1)
2495 fprintf(stderr, "%0*d ", BC_BASE_POWER, n->num[i]);
2496 else {
2497
2498 int mod = scale % BC_BASE_POWER;
2499 int d = BC_BASE_POWER - mod;
2500 BcDig div;
2501
2502 if (mod != 0) {
Gavin Howardaafebf52019-05-06 08:41:38 -06002503 div = n->num[i] / ((BcDig) bc_num_pow10((unsigned long) d));
Gavin Howardefc1e202019-05-06 08:01:07 -06002504 fprintf(stderr, "%0*d", (int) mod, div);
2505 }
2506
Gavin Howardaafebf52019-05-06 08:41:38 -06002507 div = n->num[i] % ((BcDig) bc_num_pow10((unsigned long) d));
Gavin Howardefc1e202019-05-06 08:01:07 -06002508 fprintf(stderr, " ' %0*d ", d, div);
2509 }
2510 }
2511
2512 fprintf(stderr, "(%zu | %zu.%zu / %zu) %p\n",
2513 n->scale, n->len, n->rdx, n->cap, (void*) n->num);
Gavin Howarde34f7d82019-05-07 10:32:39 -06002514 vm->nchars = 0;
Gavin Howardefc1e202019-05-06 08:01:07 -06002515}
2516#endif // BC_DEBUG_CODE