blob: 292726799867dca4f5bb954f1a430672e536abdb [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 Howardb53a9942019-04-25 11:33:54 -060095 n->len = 1;
Stefan Eßer4b111c22019-04-24 17:23:08 -060096 n->num[0] = BC_BASE;
Gavin Howard0be26ed2018-08-31 20:21:56 -060097}
98
Gavin Howardb3f1ee22019-05-07 21:49:15 -060099static void bc_num_clean(BcNum *restrict n) {
100 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
101 if (BC_NUM_ZERO(n)) n->neg = false;
102 else if (n->len < n->rdx) n->len = n->rdx;
103}
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 Howard4007e2f2019-05-08 09:08:00 -0600111static size_t bc_num_zeroDigits(const BcDig *n) {
112 return BC_BASE_POWER - bc_num_log10((size_t) *n);
113}
114
Gavin Howard06fee0c2019-05-09 14:43:20 -0600115static size_t bc_num_intDigits(const BcNum *n) {
Gavin Howard4007e2f2019-05-08 09:08:00 -0600116 size_t digits = bc_num_int(n) * BC_BASE_POWER;
117 if (digits > 0) digits -= bc_num_zeroDigits(n->num +n->len - 1);
Gavin Howard445c1892019-04-29 13:50:24 -0600118 return digits;
119}
120
Gavin Howardc1349922019-04-30 20:48:09 -0600121static size_t bc_num_nonzeroLen(const BcNum *restrict n) {
Gavin Howard08f78562019-04-29 07:56:04 -0600122 size_t i, len = n->len;
123 assert(len == n->rdx);
124 for (i = len - 1; i < n->len && !n->num[i]; --len, --i);
Gavin Howardc1349922019-04-30 20:48:09 -0600125 assert(len > 0);
Gavin Howard08f78562019-04-29 07:56:04 -0600126 return len;
127}
128
Gavin Howard2956a5e2019-05-08 08:04:06 -0600129static BcBigDig bc_num_addDigit(BcDig *restrict num, BcBigDig d, BcBigDig c) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600130 d += c;
Gavin Howard0700efc2019-04-24 17:31:23 -0600131 *num = (BcDig) (d % BC_BASE_DIG);
132 assert(*num >= 0 && *num < BC_BASE_DIG);
133 return d / BC_BASE_DIG;
Gavin Howard14c354a2019-04-24 14:28:24 -0600134}
135
136static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
137 size_t len)
138{
139 size_t i;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600140 BcBigDig carry = 0;
Gavin Howard14c354a2019-04-24 14:28:24 -0600141
142 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600143 BcBigDig in = ((BcBigDig) a[i]) + ((BcBigDig) b[i]);
Gavin Howard14c354a2019-04-24 14:28:24 -0600144 carry = bc_num_addDigit(a + i, in, carry);
145 }
146
147 for (; BC_NO_SIG && carry; ++i)
Gavin Howard2956a5e2019-05-08 08:04:06 -0600148 carry = bc_num_addDigit(a + i, (BcBigDig) a[i], carry);
Gavin Howard14c354a2019-04-24 14:28:24 -0600149
150 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
151}
152
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700153static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
154 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700155{
Gavin Howard63738202018-09-26 15:34:20 -0600156 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600157
Gavin Howard2d188a52019-02-25 14:19:08 -0700158 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600159
Gavin Howard2d188a52019-02-25 14:19:08 -0700160 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600161 assert(a[i + j] >= -BC_BASE_DIG);
162 a[i + j++] += BC_BASE_DIG;
Gavin Howard54b946a2018-10-23 12:06:57 -0600163 a[i + j] -= 1;
Gavin Howard0700efc2019-04-24 17:31:23 -0600164 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_DIG);
Gavin Howard63738202018-09-26 15:34:20 -0600165 }
166 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600167
Gavin Howard2d188a52019-02-25 14:19:08 -0700168 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600169}
170
Gavin Howard2956a5e2019-05-08 08:04:06 -0600171static BcStatus bc_num_mulArray(const BcNum *restrict a, BcBigDig b,
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600172 BcNum *restrict c)
173{
174 size_t i;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600175 BcBigDig carry = 0;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600176
Gavin Howard402d6172019-05-09 08:48:29 -0600177 assert(b <= BC_BASE_DIG);
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600178 assert(c->cap >= a->len + 1);
179
180 memset(c->num, 0, BC_NUM_SIZE(c->cap));
181
182 for (i = 0; BC_NO_SIG && i < a->len; ++i) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600183 BcBigDig in = ((BcBigDig) a->num[i]) * b + carry;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600184 c->num[i] = in % BC_BASE_DIG;
185 carry = in / BC_BASE_DIG;
186 }
187
188 if (BC_NO_SIG) {
Gavin Howarde7ae4f42019-05-08 09:08:15 -0600189 assert(carry < BC_BASE_DIG);
190 c->num[i] = (BcDig) carry;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600191 c->len = a->len;
192 c->len += (carry != 0);
193 }
194
195 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
196}
197
Gavin Howard2956a5e2019-05-08 08:04:06 -0600198static BcStatus bc_num_divArray(const BcNum *restrict a, BcBigDig b,
199 BcNum *restrict c, BcBigDig *rem)
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600200{
201 size_t i;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600202 BcBigDig carry = 0;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600203
204 assert(c->cap >= a->len);
205
206 for (i = a->len - 1; BC_NO_SIG && i < a->len; --i) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600207 BcBigDig in = ((BcBigDig) a->num[i]) + carry * BC_BASE_DIG;
Gavin Howarde7ae4f42019-05-08 09:08:15 -0600208 c->num[i] = (BcDig) (in / b);
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600209 carry = in % b;
210 }
211
212 c->len = a->len;
213 bc_num_clean(c);
214 *rem = carry;
215
216 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
217}
218
Gavin Howard2d188a52019-02-25 14:19:08 -0700219static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
220 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700221{
Gavin Howard63738202018-09-26 15:34:20 -0600222 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600223 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700224 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howarda36f0312019-05-09 10:23:07 -0600225 return BC_SIG ? BC_NUM_CMP_SIGNAL : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600226}
227
Gavin Howard3c02ed32018-12-28 18:17:15 -0700228ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700229
Gavin Howard63738202018-09-26 15:34:20 -0600230 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600231 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600232 bool a_max, neg = false;
233 ssize_t cmp;
234
235 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700236
Gavin Howard8a921bd2018-10-11 14:15:32 -0600237 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700238 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700239 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600240 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600241 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600242 else return -1;
243 }
244 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700245
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700246 a_int = bc_num_int(a);
247 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600248 a_int -= b_int;
249 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700250
Gavin Howard1ab22d22019-01-03 13:32:17 -0700251 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700252
Gavin Howard890d0c02018-10-30 16:34:50 -0600253 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600254 min = b->rdx;
255 diff = a->rdx - b->rdx;
256 max_num = a->num + diff;
257 min_num = b->num;
258 }
259 else {
260 min = a->rdx;
261 diff = b->rdx - a->rdx;
262 max_num = b->num + diff;
263 min_num = a->num;
264 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700265
Gavin Howard890d0c02018-10-30 16:34:50 -0600266 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howarda36f0312019-05-09 10:23:07 -0600267 if (cmp == BC_NUM_CMP_SIGNAL) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700268 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700269
Gavin Howard2d188a52019-02-25 14:19:08 -0700270 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700271 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600272 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700273
Gavin Howarda36f0312019-05-09 10:23:07 -0600274 return BC_SIG ? BC_NUM_CMP_SIGNAL : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700275}
276
Gavin Howard3c02ed32018-12-28 18:17:15 -0700277void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700278
Gavin Howardb1558662019-04-26 14:32:55 -0600279 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700280
Stefan Esser70f11852019-04-27 10:39:00 +0200281 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700282
Gavin Howardb1558662019-04-26 14:32:55 -0600283 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
284 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
285
286 n->scale -= places;
287 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700288
Gavin Howardddce6d42019-01-03 14:07:06 -0700289 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600290
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600291 size_t pow;
292
293 pow = n->scale % BC_BASE_POWER;
294 pow = pow ? BC_BASE_POWER - pow : 0;
Gavin Howardc1542802019-05-08 17:20:51 -0600295 pow = bc_num_pow10[pow];
Gavin Howardb1558662019-04-26 14:32:55 -0600296
297 n->len -= places_rdx;
Gavin Howard404ece72019-04-26 15:59:15 -0600298 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
Gavin Howardb1558662019-04-26 14:32:55 -0600299
300 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600301 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600302
Gavin Howard2a366922019-01-16 10:50:20 -0700303 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600304 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700305}
306
Stefan Esser70f11852019-04-27 10:39:00 +0200307static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700308
Gavin Howardb5e6da92019-04-30 19:58:04 -0600309 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700310
Gavin Howard1ab22d22019-01-03 13:32:17 -0700311 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700312
Gavin Howardb5e6da92019-04-30 19:58:04 -0600313 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
314
Gavin Howard8721e332019-05-06 08:02:49 -0600315 if (places_rdx) {
316 bc_num_expand(n, bc_vm_growSize(n->len, places_rdx));
317 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
318 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
319 }
Gavin Howardb5e6da92019-04-30 19:58:04 -0600320
Gavin Howarde6f326e2019-04-26 10:13:45 -0600321 n->rdx += places_rdx;
322 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600323 n->len += places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700324
Gavin Howarde6f326e2019-04-26 10:13:45 -0600325 assert(n->rdx == BC_NUM_RDX(n->scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200326}
327
Gavin Howard2d188a52019-02-25 14:19:08 -0700328static void bc_num_retireMul(BcNum *restrict n, size_t scale,
329 bool neg1, bool neg2)
330{
Gavin Howard971a2672019-04-26 14:32:07 -0600331 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
332 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600333
Gavin Howard305249a2018-10-15 20:24:47 -0600334 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700335 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600336}
337
Gavin Howard2d188a52019-02-25 14:19:08 -0700338static void bc_num_split(const BcNum *restrict n, size_t idx,
339 BcNum *restrict a, BcNum *restrict b)
340{
Gavin Howard305249a2018-10-15 20:24:47 -0600341 if (idx < n->len) {
342
Gavin Howard305249a2018-10-15 20:24:47 -0600343 b->len = n->len - idx;
344 a->len = idx;
Gavin Howarda3f260c2019-04-26 15:47:25 -0600345 a->scale = a->rdx = b->scale = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600346
Gavin Howard404ece72019-04-26 15:59:15 -0600347 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
348 memcpy(a->num, n->num, BC_NUM_SIZE(idx));
Gavin Howard50c80022019-01-03 15:14:33 -0700349
350 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600351 }
Gavin Howard50c80022019-01-03 15:14:33 -0700352 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600353
354 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600355}
356
Gavin Howard14913fc2019-04-23 14:36:03 -0600357static size_t bc_num_shiftZero(BcNum *restrict n) {
358
359 size_t i;
360
361 assert(!n->rdx || BC_NUM_ZERO(n));
362
363 for (i = 0; i < n->len && !n->num[i]; ++i);
364
365 n->len -= i;
366 n->num += i;
367
368 return i;
369}
370
Gavin Howard2e736de2019-04-27 06:41:58 -0600371static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
372 n->len += places_rdx;
373 n->num -= places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600374}
375
Gavin Howard2956a5e2019-05-08 08:04:06 -0600376static BcStatus bc_num_shift(BcNum *restrict n, BcBigDig dig) {
Gavin Howardbe884592019-04-29 15:07:49 -0600377
378 size_t i, len = n->len;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600379 BcBigDig carry = 0, pow;
Gavin Howardbe884592019-04-29 15:07:49 -0600380 BcDig *ptr = n->num;
Gavin Howard305249a2018-10-15 20:24:47 -0600381
Gavin Howardb1558662019-04-26 14:32:55 -0600382 assert(dig < BC_BASE_POWER);
Gavin Howard305249a2018-10-15 20:24:47 -0600383
Gavin Howardc1542802019-05-08 17:20:51 -0600384 pow = bc_num_pow10[dig];
385 dig = bc_num_pow10[BC_BASE_POWER - dig];
Gavin Howardb1558662019-04-26 14:32:55 -0600386
Gavin Howard834fde12019-04-30 08:00:05 -0600387 for (i = len - 1; BC_NO_SIG && i < len; --i) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600388 BcBigDig in, temp;
389 in = ((BcBigDig) ptr[i]);
Gavin Howard834fde12019-04-30 08:00:05 -0600390 temp = carry * dig;
391 carry = in % pow;
392 ptr[i] = ((BcDig) (in / pow)) + (BcDig) temp;
Gavin Howard305249a2018-10-15 20:24:47 -0600393 }
394
Gavin Howard834fde12019-04-30 08:00:05 -0600395 assert(!carry);
Gavin Howardb1558662019-04-26 14:32:55 -0600396
397 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600398}
399
Gavin Howardb1558662019-04-26 14:32:55 -0600400static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700401
Gavin Howardb1558662019-04-26 14:32:55 -0600402 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600403 BcBigDig dig;
Gavin Howard48a0e472019-04-26 15:39:46 -0600404 size_t places_rdx;
Gavin Howardbe884592019-04-29 15:07:49 -0600405 bool shift;
Gavin Howard14913fc2019-04-23 14:36:03 -0600406
Gavin Howardb1558662019-04-26 14:32:55 -0600407 if (!places) return s;
Gavin Howardabe4fdf2019-05-06 08:39:53 -0600408 if (places > n->scale) {
409 size_t size = bc_vm_growSize(BC_NUM_RDX(places - n->scale), n->len);
410 if (size > SIZE_MAX - 1) return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
411 }
Gavin Howard4631f3a2019-04-30 10:48:40 -0600412 if (BC_NUM_ZERO(n)) {
413 if (n->scale >= places) n->scale -= places;
414 else n->scale = 0;
415 return s;
416 }
Gavin Howard14913fc2019-04-23 14:36:03 -0600417
Gavin Howard2956a5e2019-05-08 08:04:06 -0600418 dig = (BcBigDig) (places % BC_BASE_POWER);
Gavin Howard48a0e472019-04-26 15:39:46 -0600419 shift = (dig != 0);
Gavin Howard22253c72019-05-06 08:03:57 -0600420 places_rdx = BC_NUM_RDX(places);
Gavin Howard48a0e472019-04-26 15:39:46 -0600421
Gavin Howard22253c72019-05-06 08:03:57 -0600422 if (n->scale) {
Gavin Howarda1879192019-04-30 18:55:08 -0600423
424 if (n->rdx >= places_rdx) {
425
426 size_t mod = n->scale % BC_BASE_POWER, revdig;
427
428 mod = mod ? mod : BC_BASE_POWER;
429 revdig = dig ? BC_BASE_POWER - dig : 0;
430
431 if (mod + revdig > BC_BASE_POWER) places_rdx = 1;
432 else places_rdx = 0;
433 }
434 else places_rdx -= n->rdx;
435 }
Gavin Howard4631f3a2019-04-30 10:48:40 -0600436
437 if (places_rdx) {
Gavin Howarde396dff2019-05-01 07:28:36 -0600438 bc_num_expand(n, bc_vm_growSize(n->len, places_rdx));
Gavin Howard834fde12019-04-30 08:00:05 -0600439 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
440 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
441 n->len += places_rdx;
Gavin Howardb1558662019-04-26 14:32:55 -0600442 }
Gavin Howard4631f3a2019-04-30 10:48:40 -0600443
444 if (places > n->scale) n->scale = n->rdx = 0;
445 else {
446 n->scale -= places;
447 n->rdx = BC_NUM_RDX(n->scale);
448 }
Gavin Howardb1558662019-04-26 14:32:55 -0600449
Gavin Howard834fde12019-04-30 08:00:05 -0600450 if (shift) s = bc_num_shift(n, BC_BASE_POWER - dig);
Gavin Howardb1558662019-04-26 14:32:55 -0600451
452 bc_num_clean(n);
453
454 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
455}
456
457static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
458
459 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600460 BcBigDig dig;
Gavin Howardb5e6da92019-04-30 19:58:04 -0600461 size_t places_rdx, scale, scale_mod, int_len, expand;
462 bool shift;
Gavin Howardb1558662019-04-26 14:32:55 -0600463
464 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600465 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600466 n->scale += places;
467 bc_num_expand(n, BC_NUM_RDX(n->scale));
468 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600469 }
470
Gavin Howard2956a5e2019-05-08 08:04:06 -0600471 dig = (BcBigDig) (places % BC_BASE_POWER);
Gavin Howardb5e6da92019-04-30 19:58:04 -0600472 shift = (dig != 0);
Gavin Howard834fde12019-04-30 08:00:05 -0600473 scale = n->scale;
Gavin Howardb5e6da92019-04-30 19:58:04 -0600474 scale_mod = scale % BC_BASE_POWER;
475 scale_mod = scale_mod ? scale_mod : BC_BASE_POWER;
Gavin Howard834fde12019-04-30 08:00:05 -0600476 int_len = bc_num_int(n);
Gavin Howardb1558662019-04-26 14:32:55 -0600477 places_rdx = BC_NUM_RDX(places);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700478
Gavin Howardb5e6da92019-04-30 19:58:04 -0600479 if (scale_mod + dig > BC_BASE_POWER) {
480 expand = places_rdx - 1;
481 places_rdx = 1;
Gavin Howard834fde12019-04-30 08:00:05 -0600482 }
483 else {
Gavin Howardb5e6da92019-04-30 19:58:04 -0600484 expand = places_rdx;
485 places_rdx = 0;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700486 }
487
Gavin Howardb5e6da92019-04-30 19:58:04 -0600488 if (expand > int_len) expand -= int_len;
489 else expand = 0;
490
Gavin Howard834fde12019-04-30 08:00:05 -0600491 bc_num_extend(n, places_rdx * BC_BASE_POWER);
Gavin Howarde396dff2019-05-01 07:28:36 -0600492 bc_num_expand(n, bc_vm_growSize(expand, n->len));
Gavin Howardb5e6da92019-04-30 19:58:04 -0600493 memset(n->num + n->len, 0, BC_NUM_SIZE(expand));
494 n->len += expand;
Gavin Howard834fde12019-04-30 08:00:05 -0600495 n->scale = n->rdx = 0;
Gavin Howardb1558662019-04-26 14:32:55 -0600496
Gavin Howard834fde12019-04-30 08:00:05 -0600497 if (shift) s = bc_num_shift(n, dig);
498
499 n->scale = scale + places;
500 n->rdx = BC_NUM_RDX(n->scale);
501
502 bc_num_clean(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700503
504 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600505 assert(n->rdx == BC_NUM_RDX(n->scale));
506
507 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700508}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700509
Gavin Howard1cbfe242019-01-09 17:13:11 -0700510static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600511
Gavin Howard63738202018-09-26 15:34:20 -0600512 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600513 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600514
Gavin Howard971a2672019-04-26 14:32:07 -0600515 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700516
Gavin Howardd32d7df2018-10-15 08:42:25 -0600517 one.cap = 2;
518 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600519 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600520
Gavin Howardd32d7df2018-10-15 08:42:25 -0600521 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600522}
523
Gavin Howard7bda4782018-12-28 09:53:22 -0700524#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700525static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
Gavin Howard2956a5e2019-05-08 08:04:06 -0600526 BcBigDig *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700527{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700528 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700529 bc_num_copy(c, a);
Gavin Howard2956a5e2019-05-08 08:04:06 -0600530 return bc_num_bigdig(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700531}
Gavin Howard7bda4782018-12-28 09:53:22 -0700532#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700533
Gavin Howard1cbfe242019-01-09 17:13:11 -0700534static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700535
Gavin Howard8a921bd2018-10-11 14:15:32 -0600536 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600537 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600538 BcBigDig carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700539
Gavin Howard63738202018-09-26 15:34:20 -0600540 // Because this function doesn't need to use scale (per the bc spec),
541 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600542
Gavin Howardddce6d42019-01-03 14:07:06 -0700543 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600544 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700545 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600546 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600547 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700548 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600549 bc_num_copy(c, a);
550 return BC_STATUS_SUCCESS;
551 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600552
Gavin Howard63738202018-09-26 15:34:20 -0600553 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600554 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600555 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600556 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700557
Gavin Howard63738202018-09-26 15:34:20 -0600558 if (a->rdx > b->rdx) {
559 diff = a->rdx - b->rdx;
560 ptr = a->num;
561 ptr_a = a->num + diff;
562 ptr_b = b->num;
563 }
564 else {
565 diff = b->rdx - a->rdx;
566 ptr = b->num;
567 ptr_a = a->num;
568 ptr_b = b->num + diff;
569 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700570
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700571 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700572
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700573 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600574 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700575 a_int = bc_num_int(a);
576 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700577
Gavin Howard890d0c02018-10-30 16:34:50 -0600578 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600579 min_int = b_int;
580 max = a_int;
581 ptr = ptr_a;
582 }
583 else {
584 min_int = a_int;
585 max = b_int;
586 ptr = ptr_b;
587 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700588
Gavin Howard2d188a52019-02-25 14:19:08 -0700589 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600590 BcBigDig in = ((BcBigDig) ptr_a[i]) + ((BcBigDig) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700591 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600592 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700593
Gavin Howard2d188a52019-02-25 14:19:08 -0700594 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard2956a5e2019-05-08 08:04:06 -0600595 carry = bc_num_addDigit(ptr_c + i, (BcBigDig) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700596
597 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700598
Gavin Howard1ab22d22019-01-03 13:32:17 -0700599 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600600
Gavin Howard2d188a52019-02-25 14:19:08 -0700601 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700602}
603
Gavin Howard1cbfe242019-01-09 17:13:11 -0700604static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700605
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700606 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600607 ssize_t cmp;
608 BcNum *minuend, *subtrahend;
609 size_t start;
610 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700611
Gavin Howard63738202018-09-26 15:34:20 -0600612 // Because this function doesn't need to use scale (per the bc spec),
613 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700614
Gavin Howardddce6d42019-01-03 14:07:06 -0700615 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600616 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700617 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600618 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600619 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700620 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600621 bc_num_copy(c, a);
622 return BC_STATUS_SUCCESS;
623 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700624
Gavin Howard63738202018-09-26 15:34:20 -0600625 aneg = a->neg;
626 bneg = b->neg;
627 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700628
Gavin Howard63738202018-09-26 15:34:20 -0600629 cmp = bc_num_cmp(a, b);
Gavin Howarda36f0312019-05-09 10:23:07 -0600630 if (cmp == BC_NUM_CMP_SIGNAL) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700631
Gavin Howard63738202018-09-26 15:34:20 -0600632 a->neg = aneg;
633 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700634
Gavin Howard1ab22d22019-01-03 13:32:17 -0700635 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600636 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600637 return BC_STATUS_SUCCESS;
638 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700639
640 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600641 neg = a->neg;
642 minuend = a;
643 subtrahend = b;
644 }
645 else {
646 neg = b->neg;
647 if (sub) neg = !neg;
648 minuend = b;
649 subtrahend = a;
650 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700651
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600652 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600653 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700654
Gavin Howarde6f326e2019-04-26 10:13:45 -0600655 if (c->scale < subtrahend->scale) {
656 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600657 start = 0;
658 }
659 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700660
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700661 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700662
Gavin Howard63738202018-09-26 15:34:20 -0600663 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700664
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700665 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700666}
667
Gavin Howarddc4e3712019-04-23 14:56:29 -0600668static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
669{
Gavin Howard971a2672019-04-26 14:32:07 -0600670 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600671 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600672 BcBigDig sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600673
Gavin Howard971a2672019-04-26 14:32:07 -0600674 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600675 assert(!a->rdx && !b->rdx);
676
Gavin Howard5083aec2019-04-23 18:03:46 -0600677 clen = bc_vm_growSize(alen, blen);
Gavin Howarde396dff2019-05-01 07:28:36 -0600678 bc_num_expand(c, bc_vm_growSize(clen, 1));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600679
680 ptr_c = c->num;
Gavin Howard404ece72019-04-26 15:59:15 -0600681 memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600682
Gavin Howard5083aec2019-04-23 18:03:46 -0600683 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600684
Gavin Howard5083aec2019-04-23 18:03:46 -0600685 ssize_t sidx = (ssize_t) (i - blen + 1);
686 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600687
Stefan Essera299ffc2019-04-24 23:28:32 +0200688 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
Stefan Eßer4b111c22019-04-24 17:23:08 -0600689
Gavin Howard2956a5e2019-05-08 08:04:06 -0600690 sum += ((BcBigDig) ptr_a[j]) * ((BcBigDig) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600691
Gavin Howard0700efc2019-04-24 17:31:23 -0600692 if (sum >= BC_BASE_DIG) {
693 carry += sum / BC_BASE_DIG;
694 sum %= BC_BASE_DIG;
Stefan Essera299ffc2019-04-24 23:28:32 +0200695 }
696 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600697
Stefan Essera299ffc2019-04-24 23:28:32 +0200698 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600699 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Essera299ffc2019-04-24 23:28:32 +0200700 sum = carry;
701 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600702 }
703
Gavin Howard5083aec2019-04-23 18:03:46 -0600704 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600705 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600706 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600707 clen += 1;
708 }
709
710 c->len = clen;
Gavin Howarda58840c2019-05-07 19:33:53 -0600711
Gavin Howarddc4e3712019-04-23 14:56:29 -0600712 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
713}
714
Gavin Howard14c354a2019-04-24 14:28:24 -0600715static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
716 size_t shift, BcNumShiftAddOp op)
717{
718 assert(n->len >= shift + a->len);
719 assert(!n->rdx && !a->rdx);
720 return op(n->num + shift, a->num, a->len);
721}
722
723static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600724
Gavin Howard9b801632019-02-16 23:34:06 -0700725 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200726 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600727 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700728 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600729 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700730 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600731
Gavin Howard14913fc2019-04-23 14:36:03 -0600732 assert(BC_NUM_ZERO(c));
733
Gavin Howardddce6d42019-01-03 14:07:06 -0700734 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600735 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardf1ae2be2019-05-09 11:58:22 -0600736 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) return BC_STATUS_SUCCESS;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700737 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600738 bc_num_copy(c, aone ? b : a);
739 return BC_STATUS_SUCCESS;
740 }
Gavin Howardb5093d72018-10-17 12:13:17 -0600741 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
742 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600743 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600744 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600745 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700746
Gavin Howarde0432502019-02-26 16:23:45 -0700747 max = BC_MAX(a->len, b->len);
Stefan Essera299ffc2019-04-24 23:28:32 +0200748 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700749 max2 = (max + 1) / 2;
750
Stefan Esser5fd43a32019-04-25 07:05:24 +0200751 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
Gavin Howard404ece72019-04-26 15:59:15 -0600752 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total));
Gavin Howard5bede412019-02-23 09:31:28 -0700753
754 bc_num_setup(&l1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200755 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700756 bc_num_setup(&h1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200757 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700758 bc_num_setup(&l2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200759 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700760 bc_num_setup(&h2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200761 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700762 bc_num_setup(&m1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200763 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700764 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600765 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600766 bc_num_init(&z0, max);
767 bc_num_init(&z1, max);
768 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600769 max = bc_vm_growSize(max, max) + 1;
770 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600771
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600772 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600773 bc_num_clean(&l1);
774 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600775 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600776 bc_num_clean(&l2);
777 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700778
Gavin Howard14c354a2019-04-24 14:28:24 -0600779 bc_num_expand(c, max);
780 c->len = max;
Gavin Howard404ece72019-04-26 15:59:15 -0600781 memset(c->num, 0, BC_NUM_SIZE(c->len));
Gavin Howard305249a2018-10-15 20:24:47 -0600782
Gavin Howard14c354a2019-04-24 14:28:24 -0600783 s = bc_num_sub(&h1, &l1, &m1, 0);
784 if (BC_ERR(s)) goto err;
785 s = bc_num_sub(&l2, &h2, &m2, 0);
786 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600787
Gavin Howard971a2672019-04-26 14:32:07 -0600788 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600789
Gavin Howard14c354a2019-04-24 14:28:24 -0600790 s = bc_num_m(&h1, &h2, &z2, 0);
791 if (BC_ERR(s)) goto err;
792 bc_num_clean(&z2);
793
794 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
795 if (BC_ERR(s)) goto err;
796 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
797 if (BC_ERR(s)) goto err;
798 }
799
Gavin Howard971a2672019-04-26 14:32:07 -0600800 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600801
802 s = bc_num_m(&l1, &l2, &z0, 0);
803 if (BC_ERR(s)) goto err;
804 bc_num_clean(&z0);
805
806 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
807 if (BC_ERR(s)) goto err;
808 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
809 if (BC_ERR(s)) goto err;
810 }
811
Gavin Howard971a2672019-04-26 14:32:07 -0600812 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600813
814 s = bc_num_m(&m1, &m2, &z1, 0);
815 if (BC_ERR(s)) goto err;
816 bc_num_clean(&z1);
817
818 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
819 s = bc_num_shiftAddSub(c, &z1, max2, op);
820 if (BC_ERR(s)) goto err;
821 }
Gavin Howard305249a2018-10-15 20:24:47 -0600822
823err:
Gavin Howard5bede412019-02-23 09:31:28 -0700824 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600825 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600826 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600827 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600828 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600829 return s;
830}
831
Gavin Howard1cbfe242019-01-09 17:13:11 -0700832static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600833
834 BcStatus s;
835 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600836 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600837
Gavin Howard14913fc2019-04-23 14:36:03 -0600838 bc_num_setToZero(c, 0);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600839 ascale = a->scale;
840 bscale = b->scale;
841 scale = BC_MAX(scale, ascale);
Gavin Howarda9a887a2019-04-29 14:00:56 -0600842 scale = BC_MAX(scale, bscale);
Gavin Howard7cb5f6d2019-04-29 14:01:44 -0600843
Gavin Howard48a0e472019-04-26 15:39:46 -0600844 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600845 scale = BC_MIN(rscale, scale);
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600846
Gavin Howardcc2974b2019-05-08 11:07:28 -0600847 if ((a->len == 1 || b->len == 1) && !a->rdx && !b->rdx) {
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600848
849 BcNum *operand;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600850 BcBigDig dig;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600851
852 if (a->len == 1) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600853 dig = (BcBigDig) a->num[0];
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600854 operand = b;
855 }
856 else {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600857 dig = (BcBigDig) b->num[0];
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600858 operand = a;
859 }
860
861 s = bc_num_mulArray(operand, dig, c);
Gavin Howard66a93552019-05-09 17:13:59 -0600862 if (BC_ERROR_SIGNAL_ONLY(s)) return s;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600863
864 c->scale = operand->scale;
865 c->rdx = operand->rdx;
866 c->neg = (a->neg != b->neg);
Gavin Howard66a93552019-05-09 17:13:59 -0600867 return s;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600868 }
869
Gavin Howard66a93552019-05-09 17:13:59 -0600870 bc_num_init(&cpa, a->len + a->rdx);
871 bc_num_init(&cpb, b->len + b->rdx);
872 bc_num_copy(&cpa, a);
873 bc_num_copy(&cpb, b);
874
875 cpa.neg = cpb.neg = false;
876
Gavin Howard48a0e472019-04-26 15:39:46 -0600877 ardx = cpa.rdx * BC_BASE_POWER;
878 s = bc_num_shiftLeft(&cpa, ardx);
879 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600880 bc_num_clean(&cpa);
881 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600882
883 brdx = cpb.rdx * BC_BASE_POWER;
884 s = bc_num_shiftLeft(&cpb, brdx);
885 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600886 bzero = bc_num_shiftZero(&cpb);
887 bc_num_clean(&cpb);
888
Gavin Howard14c354a2019-04-24 14:28:24 -0600889 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700890 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600891
Gavin Howard14913fc2019-04-23 14:36:03 -0600892 zero = bc_vm_growSize(azero, bzero);
893 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600894
Gavin Howard14913fc2019-04-23 14:36:03 -0600895 bc_num_expand(c, len);
Gavin Howard48a0e472019-04-26 15:39:46 -0600896 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_POWER);
897 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
898 s = bc_num_shiftRight(c, ardx + brdx);
899 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600900
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600901 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600902
Gavin Howard305249a2018-10-15 20:24:47 -0600903err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600904 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600905 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600906 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600907 bc_num_free(&cpa);
908 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700909}
910
Gavin Howard1980e032019-05-08 17:21:17 -0600911static ssize_t bc_num_divCmp(const BcDig *a, const BcNum *b, size_t len) {
912
913 ssize_t cmp;
914
915 if (b->len > len && a[len]) cmp = bc_num_compare(a, b->num, len + 1);
916 else if (b->len <= len) {
917 if (a[len]) cmp = 1;
918 else cmp = bc_num_compare(a, b->num, len);
919 }
920 else cmp = -1;
921
922 return cmp;
923}
924
Gavin Howardd5b27c82019-05-09 12:13:07 -0600925static BcStatus bc_num_d_long(BcNum *restrict a, const BcNum *restrict b,
Gavin Howardbd60ec72019-05-10 08:01:22 -0600926 BcNum *restrict c, size_t scale)
Gavin Howardd5b27c82019-05-09 12:13:07 -0600927{
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600928 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard92cf9052019-05-09 08:05:57 -0600929 BcBigDig divisor, q;
Gavin Howardbd60ec72019-05-10 08:01:22 -0600930 size_t len, end, i, rdx;
931 BcNum cpb, sub, temp;
Gavin Howardd5b27c82019-05-09 12:13:07 -0600932 BcDig *n;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700933
Gavin Howardd5b27c82019-05-09 12:13:07 -0600934 len = b->len;
935 end = a->len - len;
936 divisor = (BcBigDig) b->num[len - 1];
Gavin Howard070a6ce2019-05-08 11:50:25 -0600937
Gavin Howardd5b27c82019-05-09 12:13:07 -0600938 bc_num_expand(c, a->len);
939 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howardbd60ec72019-05-10 08:01:22 -0600940
Gavin Howardd5b27c82019-05-09 12:13:07 -0600941 c->rdx = a->rdx;
942 c->scale = a->scale;
943 c->len = a->len;
944
Gavin Howardbd60ec72019-05-10 08:01:22 -0600945 assert(c->scale >= scale);
946 rdx = c->rdx - BC_NUM_RDX(scale);
Gavin Howard61349662019-05-08 11:08:45 -0600947
Gavin Howardbd60ec72019-05-10 08:01:22 -0600948 bc_num_init(&cpb, len + 1);
949 bc_num_init(&sub, len + 1);
950 bc_num_init(&temp, len + 1);
951
952 for (i = end - 1; BC_NO_SIG && BC_NO_ERR(!s) && i < end && i >= rdx; --i) {
Gavin Howard61349662019-05-08 11:08:45 -0600953
Gavin Howard070a6ce2019-05-08 11:50:25 -0600954 ssize_t cmp;
Gavin Howard48fb8df2019-05-09 17:02:55 -0600955 BcBigDig rem;
Gavin Howard070a6ce2019-05-08 11:50:25 -0600956
Gavin Howardd5b27c82019-05-09 12:13:07 -0600957 n = a->num + i;
Gavin Howard070a6ce2019-05-08 11:50:25 -0600958 q = 0;
959
Gavin Howard1980e032019-05-08 17:21:17 -0600960 cmp = bc_num_divCmp(n, b, len);
Gavin Howarda36f0312019-05-09 10:23:07 -0600961 if (cmp == BC_NUM_CMP_SIGNAL) break;
Gavin Howard070a6ce2019-05-08 11:50:25 -0600962
Gavin Howardbd60ec72019-05-10 08:01:22 -0600963 if (!cmp) {
964
965 q = 1;
966
967 s = bc_num_mulArray(b, (BcBigDig) q, &cpb);
968 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
969 }
Gavin Howard070a6ce2019-05-08 11:50:25 -0600970 else if (cmp > 0) {
971
Gavin Howard92cf9052019-05-09 08:05:57 -0600972 BcBigDig n1, pow, dividend;
Gavin Howardbd60ec72019-05-10 08:01:22 -0600973 size_t cpblen;
Gavin Howard1980e032019-05-08 17:21:17 -0600974
975 n1 = (BcBigDig) n[len];
976 dividend = n1 * BC_BASE_DIG + (BcBigDig) n[len - 1];
Gavin Howard92cf9052019-05-09 08:05:57 -0600977 q = (dividend / divisor + 1);
Gavin Howard402d6172019-05-09 08:48:29 -0600978 q = q > BC_BASE_DIG ? BC_BASE_DIG : q;
Gavin Howard1980e032019-05-08 17:21:17 -0600979 dividend = ((BcBigDig) bc_num_log10((size_t) q));
980
981 pow = bc_num_pow10[dividend - 1];
Gavin Howard1980e032019-05-08 17:21:17 -0600982
Gavin Howard92cf9052019-05-09 08:05:57 -0600983 s = bc_num_mulArray(b, (BcBigDig) q, &cpb);
Gavin Howard1980e032019-05-08 17:21:17 -0600984 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
985
Gavin Howard6945eaf2019-05-09 17:06:54 -0600986 s = bc_num_mulArray(b, pow, &sub);
Gavin Howard1980e032019-05-08 17:21:17 -0600987 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
988
Gavin Howardbd60ec72019-05-10 08:01:22 -0600989 cpblen = cpb.len;
990
Gavin Howard1980e032019-05-08 17:21:17 -0600991 while (BC_NO_SIG && BC_NO_ERR(!s) && pow > 0) {
992
Gavin Howardbd60ec72019-05-10 08:01:22 -0600993 s = bc_num_subArrays(cpb.num, sub.num, sub.len);
Gavin Howard1980e032019-05-08 17:21:17 -0600994 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
995
Gavin Howardbd60ec72019-05-10 08:01:22 -0600996 bc_num_clean(&cpb);
Gavin Howard1980e032019-05-08 17:21:17 -0600997
Gavin Howardbd60ec72019-05-10 08:01:22 -0600998 cmp = bc_num_divCmp(n, &cpb, len);
Gavin Howarda36f0312019-05-09 10:23:07 -0600999 if (cmp == BC_NUM_CMP_SIGNAL) goto err;
Gavin Howard1980e032019-05-08 17:21:17 -06001000
1001 while (BC_NO_SIG && BC_NO_ERR(!s) && cmp < 0) {
1002
1003 q -= pow;
1004
1005 s = bc_num_subArrays(cpb.num, sub.num, sub.len);
1006 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1007
Gavin Howardbd60ec72019-05-10 08:01:22 -06001008 bc_num_clean(&cpb);
Gavin Howard1980e032019-05-08 17:21:17 -06001009
Gavin Howardbd60ec72019-05-10 08:01:22 -06001010 cmp = bc_num_divCmp(n, &cpb, len);
Gavin Howarda36f0312019-05-09 10:23:07 -06001011 if (cmp == BC_NUM_CMP_SIGNAL) goto err;
Gavin Howard1980e032019-05-08 17:21:17 -06001012 }
1013
1014 pow /= BC_BASE;
Gavin Howard1980e032019-05-08 17:21:17 -06001015
Gavin Howardbd60ec72019-05-10 08:01:22 -06001016 if (pow) {
1017
1018 s = bc_num_addArrays(cpb.num, sub.num, sub.len);
1019 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1020
1021 cpb.len = cpblen;
1022 bc_num_clean(&cpb);
1023
1024 bc_num_copy(&temp, &sub);
1025 s = bc_num_divArray(&temp, 10, &sub, &rem);
1026 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1027 assert(rem == 0);
1028 }
Gavin Howard070a6ce2019-05-08 11:50:25 -06001029 }
Gavin Howard1980e032019-05-08 17:21:17 -06001030
1031 q -= 1;
1032 }
1033
1034 assert(q <= BC_BASE_DIG);
1035
1036 if (q) {
1037
Gavin Howard1980e032019-05-08 17:21:17 -06001038 s = bc_num_subArrays(n, cpb.num, len);
1039 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard070a6ce2019-05-08 11:50:25 -06001040 }
1041
Gavin Howard92cf9052019-05-09 08:05:57 -06001042 c->num[i] = (BcDig) q;
Gavin Howard61349662019-05-08 11:08:45 -06001043 }
1044
Gavin Howard1980e032019-05-08 17:21:17 -06001045err:
Gavin Howardd5b27c82019-05-09 12:13:07 -06001046 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardbd60ec72019-05-10 08:01:22 -06001047 bc_num_free(&temp);
Gavin Howard66a93552019-05-09 17:13:59 -06001048 bc_num_free(&cpb);
Gavin Howard66a93552019-05-09 17:13:59 -06001049 bc_num_free(&sub);
Gavin Howardd5b27c82019-05-09 12:13:07 -06001050 return s;
1051}
1052
Gavin Howardd5b27c82019-05-09 12:13:07 -06001053static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
1054
1055 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howardfcb74462019-05-09 14:42:35 -06001056 size_t len;
1057 BcNum cpa, cpb;
Gavin Howardd5b27c82019-05-09 12:13:07 -06001058 bool zero = true;
1059
1060 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1061 if (BC_NUM_ZERO(a)) {
1062 bc_num_setToZero(c, scale);
1063 return BC_STATUS_SUCCESS;
1064 }
1065 if (bc_num_isOne(b)) {
1066 bc_num_copy(c, a);
1067 bc_num_retireMul(c, scale, a->neg, b->neg);
1068 return BC_STATUS_SUCCESS;
1069 }
1070 if (!a->rdx && !b->rdx && b->len == 1 && !scale) {
1071 BcBigDig rem;
1072 s = bc_num_divArray(a, (BcBigDig) b->num[0], c, &rem);
1073 bc_num_retireMul(c, scale, a->neg, b->neg);
1074 return s;
1075 }
1076
1077 len = bc_num_mulReq(a, b, scale);
1078 bc_num_init(&cpa, len);
1079 bc_num_copy(&cpa, a);
Gavin Howardfcb74462019-05-09 14:42:35 -06001080 bc_num_createCopy(&cpb, b);
Gavin Howardd5b27c82019-05-09 12:13:07 -06001081
1082 if (len > cpa.len) {
1083 bc_num_expand(&cpa, bc_vm_growSize(len, 2));
1084 bc_num_extend(&cpa, (len - cpa.len) * BC_BASE_POWER);
1085 }
1086
1087 cpa.scale = cpa.rdx * BC_BASE_POWER;
1088
1089 bc_num_extend(&cpa, b->scale);
1090 cpa.rdx -= BC_NUM_RDX(b->scale);
1091 cpa.scale = cpa.rdx * BC_BASE_POWER;
Gavin Howardbd60ec72019-05-10 08:01:22 -06001092 if (scale > cpa.scale) {
1093 bc_num_extend(&cpa, scale);
1094 cpa.scale = cpa.rdx * BC_BASE_POWER;
1095 }
Gavin Howardd5b27c82019-05-09 12:13:07 -06001096
1097 if (b->rdx == b->len) {
Gavin Howardfcb74462019-05-09 14:42:35 -06001098 size_t i;
Gavin Howardd5b27c82019-05-09 12:13:07 -06001099 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
1100 assert(i != len || !zero);
1101 len -= i - 1;
1102 }
1103
1104 if (cpa.cap == cpa.len) bc_num_expand(&cpa, bc_vm_growSize(cpa.len, 1));
1105
1106 // We want an extra zero in front to make things simpler.
1107 cpa.num[cpa.len++] = 0;
1108
Gavin Howardfcb74462019-05-09 14:42:35 -06001109 if (cpa.rdx == cpa.len) cpa.len = bc_num_nonzeroLen(&cpa);
1110 if (cpb.rdx == cpb.len) cpb.len = bc_num_nonzeroLen(&cpb);
1111 cpb.scale = cpb.rdx = 0;
Gavin Howardd5b27c82019-05-09 12:13:07 -06001112
Gavin Howardbd60ec72019-05-10 08:01:22 -06001113 s = bc_num_d_long(&cpa, &cpb, c, scale);
Gavin Howardd5b27c82019-05-09 12:13:07 -06001114
1115 if (BC_NO_ERR(!s)) {
1116 bc_num_retireMul(c, scale, a->neg, b->neg);
1117 if (BC_SIG) s = BC_STATUS_SIGNAL;
1118 }
1119
Gavin Howardfcb74462019-05-09 14:42:35 -06001120 bc_num_free(&cpb);
Gavin Howard1980e032019-05-08 17:21:17 -06001121 bc_num_free(&cpa);
Gavin Howardd5b27c82019-05-09 12:13:07 -06001122
Gavin Howard63738202018-09-26 15:34:20 -06001123 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001124}
1125
Gavin Howard1cbfe242019-01-09 17:13:11 -07001126static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
1127 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -06001128{
Gavin Howard63738202018-09-26 15:34:20 -06001129 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -06001130 BcNum temp;
1131 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001132
Gavin Howardddce6d42019-01-03 14:07:06 -07001133 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1134 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -07001135 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001136 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001137 return BC_STATUS_SUCCESS;
1138 }
Gavin Howard8b254872018-03-14 01:13:35 -06001139
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001140 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -07001141 s = bc_num_d(a, b, c, scale);
1142 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001143 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001144
Gavin Howard996fc222019-04-29 13:22:38 -06001145 if (scale) scale = ts + 1;
Gavin Howard3115c012018-10-04 10:53:56 -06001146
Gavin Howard53eba8b2018-10-31 15:14:37 -06001147 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001148 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001149 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001150 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -06001151
Gavin Howard77445432019-04-26 15:59:33 -06001152 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
Gavin Howard18441082018-10-22 10:03:30 -06001153
1154 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001155 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard996fc222019-04-29 13:22:38 -06001156 d->neg = BC_NUM_NONZERO(d) ? neg : false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001157
1158err:
Gavin Howard18441082018-10-22 10:03:30 -06001159 bc_num_free(&temp);
1160 return s;
1161}
1162
Gavin Howard2d188a52019-02-25 14:19:08 -07001163static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1164{
Gavin Howard18441082018-10-22 10:03:30 -06001165 BcStatus s;
1166 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -07001167 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -06001168
Gavin Howard77445432019-04-26 15:59:33 -06001169 ts = bc_vm_growSize(scale, b->scale);
1170 ts = BC_MAX(ts, a->scale);
Gavin Howard798508b2019-02-21 16:45:32 -07001171
1172 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -06001173 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001174 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -06001175
Gavin Howard63738202018-09-26 15:34:20 -06001176 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001177}
1178
Gavin Howard1cbfe242019-01-09 17:13:11 -07001179static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001180
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001181 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001182 BcNum copy;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001183 BcBigDig pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001184 size_t i, powrdx, resrdx;
1185 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001186
Gavin Howardecafd4f2019-02-23 09:30:45 -07001187 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001188
Gavin Howardddce6d42019-01-03 14:07:06 -07001189 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -06001190 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -06001191 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001192 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001193 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001194 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001195 return BC_STATUS_SUCCESS;
1196 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001197 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001198 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -06001199 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001200 return s;
1201 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001202
Gavin Howard63738202018-09-26 15:34:20 -06001203 neg = b->neg;
1204 b->neg = false;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001205 s = bc_num_bigdig(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -07001206 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001207 if (s) return s;
1208
Gavin Howardbaa4f582019-01-24 13:56:35 -07001209 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001210
Stefan Eßerb5318342019-04-29 10:31:26 -06001211 if (!neg) scale = BC_MIN(a->scale * pow, BC_MAX(scale, a->scale));
Gavin Howardb29674f2018-03-22 22:24:58 -06001212
Gavin Howardd497c232019-04-29 10:32:38 -06001213 for (powrdx = a->scale; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -06001214 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001215 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001216 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001217 }
Gavin Howard6fb635f2018-03-03 12:45:42 -07001218
Gavin Howard2d188a52019-02-25 14:19:08 -07001219 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -07001220
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001221 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -07001222 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001223
Gavin Howard2d188a52019-02-25 14:19:08 -07001224 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001225
Gavin Howard4d5daba2019-01-07 14:42:50 -07001226 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001227 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001228 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001229
Gavin Howard63738202018-09-26 15:34:20 -06001230 if (pow & 1) {
1231 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001232 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001233 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001234 }
1235 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001236
Gavin Howard2d188a52019-02-25 14:19:08 -07001237 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -07001238 if (neg) {
1239 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001240 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -07001241 }
1242
Stefan Eßerb5318342019-04-29 10:31:26 -06001243 if (c->scale > scale) bc_num_truncate(c, c->scale - scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001244
Gavin Howard53eba8b2018-10-31 15:14:37 -06001245 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -06001246 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001247 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -07001248
Gavin Howard9b801632019-02-16 23:34:06 -07001249sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001250 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001251err:
Gavin Howard63738202018-09-26 15:34:20 -06001252 bc_num_free(&copy);
1253 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001254}
1255
Gavin Howard7bda4782018-12-28 09:53:22 -07001256#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -07001257static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
1258 size_t scale)
1259{
Gavin Howardac4d9122018-12-27 23:59:12 -07001260 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001261 BcBigDig val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001262
1263 BC_UNUSED(scale);
1264
1265 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001266 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001267
Gavin Howardc9bef2d2019-04-26 14:46:52 -06001268 if (val < c->scale) bc_num_truncate(c, c->scale - val);
1269 else if (val > c->scale) bc_num_extend(c, val - c->scale);
Gavin Howardac4d9122018-12-27 23:59:12 -07001270
1271 return s;
1272}
1273
Gavin Howard2d188a52019-02-25 14:19:08 -07001274static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1275{
Gavin Howardac4d9122018-12-27 23:59:12 -07001276 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001277 BcBigDig val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001278
1279 BC_UNUSED(scale);
1280
1281 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001282 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001283
Gavin Howardb1558662019-04-26 14:32:55 -06001284 return bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001285}
1286
Gavin Howard2d188a52019-02-25 14:19:08 -07001287static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1288 size_t scale)
1289{
Gavin Howardac4d9122018-12-27 23:59:12 -07001290 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001291 BcBigDig val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001292
1293 BC_UNUSED(scale);
1294
1295 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001296 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001297
Gavin Howard2a366922019-01-16 10:50:20 -07001298 if (BC_NUM_ZERO(c)) return s;
1299
Gavin Howardb1558662019-04-26 14:32:55 -06001300 return bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001301}
Gavin Howard7bda4782018-12-28 09:53:22 -07001302#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001303
Gavin Howard1cbfe242019-01-09 17:13:11 -07001304static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1305 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001306{
Gavin Howarda1c44392018-09-27 12:41:15 -06001307 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001308 BcNum num2, *ptr_a, *ptr_b;
1309 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001310
Gavin Howard63738202018-09-26 15:34:20 -06001311 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001312
Gavin Howard890d0c02018-10-30 16:34:50 -06001313 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001314 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001315 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001316 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001317 }
1318 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001319
Gavin Howard63738202018-09-26 15:34:20 -06001320 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001321 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001322 if (c != a) {
1323 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001324 init = true;
1325 }
1326 }
1327 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001328
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001329 if (init) bc_num_init(c, req);
1330 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001331
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001332 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001333
Gavin Howardddce6d42019-01-03 14:07:06 -07001334 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001335 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001336
Gavin Howardba009802018-09-29 04:41:51 -06001337 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001338
Gavin Howardee9d01e2019-02-16 22:48:11 -07001339 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001340}
1341
Gavin Howardc90ed902019-01-02 13:07:49 -07001342#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001343static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001344
Gavin Howardc7d655b2018-12-28 19:45:13 -07001345 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001346 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001347
Gavin Howard63738202018-09-26 15:34:20 -06001348 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001349
Gavin Howard63738202018-09-26 15:34:20 -06001350 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001351
Gavin Howard8a921bd2018-10-11 14:15:32 -06001352 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001353
Gavin Howard63738202018-09-26 15:34:20 -06001354 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001355
Gavin Howard63738202018-09-26 15:34:20 -06001356 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001357
Gavin Howard63738202018-09-26 15:34:20 -06001358 radix = true;
1359 continue;
1360 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001361
Gavin Howardc7d655b2018-12-28 19:45:13 -07001362 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001363 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001364
Gavin Howard63738202018-09-26 15:34:20 -06001365 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001366}
Gavin Howardc90ed902019-01-02 13:07:49 -07001367#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001368
Gavin Howard2956a5e2019-05-08 08:04:06 -06001369static BcBigDig bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001370
1371 if (isupper(c)) {
1372 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001373 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001374 }
1375 else c -= '0';
1376
Gavin Howard2956a5e2019-05-08 08:04:06 -06001377 return (BcBigDig) (uchar) c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001378}
1379
Gavin Howard1cbfe242019-01-09 17:13:11 -07001380static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001381
Gavin Howard3b60abb2019-04-26 08:49:17 -06001382 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001383 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001384 bool zero = true, rdx;
Stefan Essera299ffc2019-04-24 23:28:32 +02001385
Gavin Howard63738202018-09-26 15:34:20 -06001386 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001387
Gavin Howard63738202018-09-26 15:34:20 -06001388 val += i;
Gavin Howard46a7d8d2019-05-06 15:16:12 -06001389 assert(!val[0] || isalnum(val[0]) || val[0] == '.');
1390
1391 // All 0's. We can just return, since this
1392 // procedure expects a virgin (already 0) BcNum.
1393 if (!val[0]) return;
1394
Gavin Howard63738202018-09-26 15:34:20 -06001395 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001396
Gavin Howard63738202018-09-26 15:34:20 -06001397 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001398 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001399
Gavin Howardd43ec372019-04-25 21:13:54 -06001400 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001401
Gavin Howardd43ec372019-04-25 21:13:54 -06001402 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001403 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001404
Gavin Howard3b60abb2019-04-26 08:49:17 -06001405 i = len - (ptr == val ? 0 : i) - rdx;
Gavin Howard48a0e472019-04-26 15:39:46 -06001406 temp = BC_NUM_ROUND_POW(i);
Gavin Howard3b60abb2019-04-26 08:49:17 -06001407 mod = n->scale % BC_BASE_POWER;
1408 i = mod ? BC_BASE_POWER - mod : 0;
1409 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001410
1411 bc_num_expand(n, n->len);
Gavin Howard404ece72019-04-26 15:59:15 -06001412 memset(n->num, 0, BC_NUM_SIZE(n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -07001413
Gavin Howard6076dbb2019-04-26 09:47:50 -06001414 if (zero) n->len = n->rdx = 0;
1415 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001416
Gavin Howard2956a5e2019-05-08 08:04:06 -06001417 BcBigDig exp, pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001418
Gavin Howard3b60abb2019-04-26 08:49:17 -06001419 exp = i;
Gavin Howardc1542802019-05-08 17:20:51 -06001420 pow = bc_num_pow10[exp];
Gavin Howardd43ec372019-04-25 21:13:54 -06001421
1422 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001423
Gavin Howardc7d655b2018-12-28 19:45:13 -07001424 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001425
Gavin Howardd43ec372019-04-25 21:13:54 -06001426 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001427 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001428
1429 size_t idx = exp / BC_BASE_POWER;
1430
Gavin Howard8dd307e2019-01-08 23:05:19 -07001431 if (isupper(c)) c = '9';
Gavin Howard2956a5e2019-05-08 08:04:06 -06001432 n->num[idx] += (((BcBigDig) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001433
1434 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1435 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001436 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001437 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001438 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001439}
1440
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001441static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
Gavin Howard2956a5e2019-05-08 08:04:06 -06001442 BcBigDig base)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001443{
1444 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard4c605692019-05-08 09:09:17 -06001445 BcNum temp, mult1, mult2, result1, result2, *m1, *m2, *ptr;
Gavin Howard77445432019-04-26 15:59:33 -06001446 char c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001447 bool zero = true;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001448 BcBigDig v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001449 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001450
Gavin Howard11b9afd2018-10-18 14:23:28 -06001451 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001452 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001453
Gavin Howard2956a5e2019-05-08 08:04:06 -06001454 bc_num_init(&temp, BC_NUM_BIGDIG_LOG10);
Gavin Howard4c605692019-05-08 09:09:17 -06001455 bc_num_init(&mult1, BC_NUM_BIGDIG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001456
Gavin Howardc7d655b2018-12-28 19:45:13 -07001457 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001458
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001459 v = bc_num_parseChar(c, base);
Gavin Howard9b801632019-02-16 23:34:06 -07001460
Gavin Howard4c605692019-05-08 09:09:17 -06001461 s = bc_num_mulArray(n, base, &mult1);
Gavin Howardc78e7522019-02-22 13:24:25 -07001462 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001463 bc_num_bigdig2num(&temp, v);
Gavin Howard4c605692019-05-08 09:09:17 -06001464 s = bc_num_add(&mult1, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001465 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001466 }
Gavin Howardede51f02018-03-02 12:30:00 -07001467
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001468 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001469
Gavin Howard63738202018-09-26 15:34:20 -06001470 assert(c == '.');
Gavin Howard4c605692019-05-08 09:09:17 -06001471 bc_num_init(&mult2, BC_NUM_BIGDIG_LOG10);
1472 bc_num_init(&result1, BC_NUM_DEF_SIZE);
1473 bc_num_init(&result2, BC_NUM_DEF_SIZE);
1474 bc_num_one(&mult1);
1475
1476 m1 = &mult1;
1477 m2 = &mult2;
Gavin Howardede51f02018-03-02 12:30:00 -07001478
Gavin Howard8722e832019-05-08 11:12:16 -06001479 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs) {
1480
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001481 v = bc_num_parseChar(c, base);
Gavin Howard9b801632019-02-16 23:34:06 -07001482
Gavin Howard4c605692019-05-08 09:09:17 -06001483 s = bc_num_mulArray(&result1, base, &result2);
Gavin Howardc78e7522019-02-22 13:24:25 -07001484 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001485
Gavin Howard2956a5e2019-05-08 08:04:06 -06001486 bc_num_bigdig2num(&temp, v);
Gavin Howard4c605692019-05-08 09:09:17 -06001487 s = bc_num_add(&result2, &temp, &result1, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001488 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard4c605692019-05-08 09:09:17 -06001489 s = bc_num_mulArray(m1, base, m2);
Gavin Howardc78e7522019-02-22 13:24:25 -07001490 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard4c605692019-05-08 09:09:17 -06001491
1492 ptr = m1;
1493 m1 = m2;
1494 m2 = ptr;
Gavin Howard63738202018-09-26 15:34:20 -06001495 }
Gavin Howardede51f02018-03-02 12:30:00 -07001496
Gavin Howardf2ee6342019-04-09 17:05:20 -06001497 if (BC_SIG) {
1498 s = BC_STATUS_SIGNAL;
1499 goto err;
1500 }
1501
Gavin Howard1769abc2019-02-21 09:36:14 -07001502 // This one cannot be a divide by 0 because mult starts out at 1, then is
1503 // multiplied by base, and base cannot be 0, so mult cannot be 0.
Gavin Howard4c605692019-05-08 09:09:17 -06001504 s = bc_num_div(&result1, m1, &result2, digs * 2);
Gavin Howard1769abc2019-02-21 09:36:14 -07001505 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001506 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard4c605692019-05-08 09:09:17 -06001507 bc_num_truncate(&result2, digs);
1508 s = bc_num_add(n, &result2, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001509 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001510
Gavin Howardddce6d42019-01-03 14:07:06 -07001511 if (BC_NUM_NONZERO(n)) {
Gavin Howardb0642be2019-04-30 21:12:22 -06001512 if (n->scale < digs) bc_num_extend(n, digs - n->scale);
Gavin Howard63738202018-09-26 15:34:20 -06001513 }
1514 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001515
Gavin Howardd1417682019-02-15 17:08:42 -07001516err:
Gavin Howard4c605692019-05-08 09:09:17 -06001517 bc_num_free(&result2);
1518 bc_num_free(&result1);
1519 bc_num_free(&mult2);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001520int_err:
Gavin Howard4c605692019-05-08 09:09:17 -06001521 bc_num_free(&mult1);
Gavin Howard63738202018-09-26 15:34:20 -06001522 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001523 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001524}
1525
Gavin Howard1cbfe242019-01-09 17:13:11 -07001526static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001527 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001528 bc_vm_putchar('\\');
1529 bc_vm_putchar('\n');
Gavin Howard63738202018-09-26 15:34:20 -06001530 }
Gavin Howard80977b22018-09-06 14:29:13 -06001531}
1532
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001533static void bc_num_putchar(int c) {
1534 if (c != '\n') bc_num_printNewline();
1535 bc_vm_putchar(c);
1536}
1537
Gavin Howard40a085f2018-12-03 12:08:59 -07001538#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001539static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howard3c02ed32018-12-28 18:17:15 -07001540 BC_UNUSED(rdx);
Gavin Howardeedfb2c2019-05-07 08:00:13 -06001541 BC_UNUSED(len);
1542 assert(len == 1);
Gavin Howard3c02ed32018-12-28 18:17:15 -07001543 bc_vm_putchar((uchar) n);
Gavin Howard83eb8392018-10-09 01:21:19 -06001544}
1545#endif // DC_ENABLED
1546
Gavin Howard1cbfe242019-01-09 17:13:11 -07001547static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1548
Gavin Howarda84ad992018-12-03 19:11:06 -07001549 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001550
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001551 bc_num_putchar(rdx ? '.' : ' ');
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001552
Gavin Howard530e3072019-04-23 16:23:36 -06001553 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001554
Gavin Howardeedfb2c2019-05-07 08:00:13 -06001555 for (exp = 0; exp < len; pow /= BC_BASE, ++exp) {
Gavin Howard4b075f22019-05-07 15:27:53 -06001556 size_t dig = n / pow;
Gavin Howard3c02ed32018-12-28 18:17:15 -07001557 n -= dig * pow;
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001558 bc_num_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001559 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001560}
Gavin Howardfe679f02018-02-14 15:50:09 -07001561
Gavin Howard1cbfe242019-01-09 17:13:11 -07001562static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001563
Gavin Howardeedfb2c2019-05-07 08:00:13 -06001564 BC_UNUSED(len);
1565
Gavin Howard3c02ed32018-12-28 18:17:15 -07001566 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001567
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001568 if (rdx) bc_num_putchar('.');
Gavin Howard2682a1f2018-03-03 09:09:13 -07001569
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001570 bc_num_putchar(bc_num_hex_digits[n]);
Gavin Howard2682a1f2018-03-03 09:09:13 -07001571}
1572
Gavin Howard1cbfe242019-01-09 17:13:11 -07001573static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001574
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001575 size_t i, j, rdx = n->rdx;
1576 bool zero = true;
1577 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001578
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001579 if (n->neg) bc_num_putchar('-');
Gavin Howard32f2beb2018-03-09 11:43:20 -07001580
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001581 for (i = n->len - 1; i < n->len; --i) {
1582
1583 BcDig n9 = n->num[i];
1584 size_t temp;
1585 bool irdx = (i == rdx - 1);
1586
1587 zero = (zero & !irdx);
1588 temp = n->scale % BC_BASE_POWER;
1589 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1590
1591 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1592
1593 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Essera299ffc2019-04-24 23:28:32 +02001594 buffer[j] = n9 % BC_BASE;
1595 n9 /= BC_BASE;
1596 }
Stefan Esser0da17752019-04-25 12:25:15 +02001597
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001598 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1599 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1600 zero = (zero && buffer[j] == 0);
1601 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Essera299ffc2019-04-24 23:28:32 +02001602 }
1603 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001604}
1605
Gavin Howard7ad5a662019-02-19 14:40:46 -07001606#if BC_ENABLE_EXTRA_MATH
Gavin Howardb1558662019-04-26 14:32:55 -06001607static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) {
Gavin Howard7ad5a662019-02-19 14:40:46 -07001608
Gavin Howardb1558662019-04-26 14:32:55 -06001609 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001610 bool neg = (n->len <= n->rdx);
1611 BcNum temp, exp;
1612 size_t places, mod;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001613 BcDig digs[BC_NUM_BIGDIG_LOG10];
Gavin Howard7ad5a662019-02-19 14:40:46 -07001614
1615 bc_num_createCopy(&temp, n);
1616
1617 if (neg) {
Gavin Howardc1349922019-04-30 20:48:09 -06001618
1619 size_t i, idx = bc_num_nonzeroLen(n) - 1;
1620
1621 places = 1;
1622
1623 for (i = BC_BASE_POWER - 1; i < BC_BASE_POWER; --i) {
Gavin Howardc1542802019-05-08 17:20:51 -06001624 if (bc_num_pow10[i] > (BcBigDig) n->num[idx]) places += 1;
Gavin Howardc1349922019-04-30 20:48:09 -06001625 else break;
1626 }
1627
1628 places += (n->rdx - (idx + 1)) * BC_BASE_POWER;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001629 mod = places % 3;
Gavin Howardc1349922019-04-30 20:48:09 -06001630
Gavin Howard7ad5a662019-02-19 14:40:46 -07001631 if (eng && mod != 0) places += 3 - mod;
Gavin Howardb1558662019-04-26 14:32:55 -06001632 s = bc_num_shiftLeft(&temp, places);
1633 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001634 }
1635 else {
Gavin Howard06fee0c2019-05-09 14:43:20 -06001636 places = bc_num_intDigits(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001637 mod = places % 3;
1638 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howardb1558662019-04-26 14:32:55 -06001639 s = bc_num_shiftRight(&temp, places);
1640 if (BC_ERROR_SIGNAL_ONLY(s)) goto exit;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001641 }
1642
1643 bc_num_printDecimal(&temp);
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001644 bc_num_putchar('e');
Gavin Howard7ad5a662019-02-19 14:40:46 -07001645
1646 if (!places) {
1647 bc_num_printHex(0, 1, false);
1648 goto exit;
1649 }
1650
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001651 if (neg) bc_num_putchar('-');
Gavin Howard7ad5a662019-02-19 14:40:46 -07001652
Gavin Howard2956a5e2019-05-08 08:04:06 -06001653 bc_num_setup(&exp, digs, BC_NUM_BIGDIG_LOG10);
1654 bc_num_bigdig2num(&exp, (BcBigDig) places);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001655
1656 bc_num_printDecimal(&exp);
1657
1658exit:
1659 bc_num_free(&temp);
Gavin Howardb1558662019-04-26 14:32:55 -06001660 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001661}
1662#endif // BC_ENABLE_EXTRA_MATH
1663
Gavin Howard2956a5e2019-05-08 08:04:06 -06001664static BcStatus bc_num_printNum(BcNum *restrict n, BcBigDig base,
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001665 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001666{
Gavin Howard63738202018-09-26 15:34:20 -06001667 BcStatus s;
1668 BcVec stack;
Gavin Howarda8c2e582019-05-07 11:33:10 -06001669 BcNum intp1, intp2, fracp1, fracp2, digit, flen1, flen2, *n1, *n2, *temp;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001670 BcBigDig dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001671 size_t i;
1672 bool radix;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001673 BcDig digit_digs[BC_NUM_BIGDIG_LOG10];
1674 BcDig flen1_digs[BC_NUM_BIGDIG_LOG10];
1675 BcDig flen2_digs[BC_NUM_BIGDIG_LOG10];
Gavin Howard2682a1f2018-03-03 09:09:13 -07001676
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001677 assert(base > 1);
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001678
Gavin Howardddce6d42019-01-03 14:07:06 -07001679 if (BC_NUM_ZERO(n)) {
1680 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001681 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001682 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001683
Gavin Howard2956a5e2019-05-08 08:04:06 -06001684 bc_vec_init(&stack, sizeof(BcBigDig), NULL);
Gavin Howarda8c2e582019-05-07 11:33:10 -06001685 bc_num_init(&fracp1, n->rdx);
1686 bc_num_init(&fracp2, n->rdx);
Gavin Howardc4e9a082019-05-07 11:12:50 -06001687 bc_num_setup(&digit, digit_digs, sizeof(digit_digs) / sizeof(BcDig));
Gavin Howarda8c2e582019-05-07 11:33:10 -06001688 bc_num_setup(&flen1, flen1_digs, sizeof(flen1_digs) / sizeof(BcDig));
1689 bc_num_setup(&flen2, flen2_digs, sizeof(flen2_digs) / sizeof(BcDig));
1690 bc_num_one(&flen1);
Gavin Howardc4e9a082019-05-07 11:12:50 -06001691 bc_num_createCopy(&intp1, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001692
Gavin Howardc4e9a082019-05-07 11:12:50 -06001693 bc_num_truncate(&intp1, intp1.scale);
1694 bc_num_init(&intp2, intp1.len);
1695
Gavin Howarda8c2e582019-05-07 11:33:10 -06001696 s = bc_num_sub(n, &intp1, &fracp1, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001697 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001698
Gavin Howarda8c2e582019-05-07 11:33:10 -06001699 n1 = &intp1;
1700 n2 = &intp2;
Gavin Howardc4e9a082019-05-07 11:12:50 -06001701
Gavin Howarda8c2e582019-05-07 11:33:10 -06001702 while (BC_NO_SIG && BC_NUM_NONZERO(n1)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001703
1704 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001705 s = bc_num_divArray(n1, base, n2, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001706 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001707
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001708 bc_vec_push(&stack, &dig);
Gavin Howardc4e9a082019-05-07 11:12:50 -06001709
Gavin Howarda8c2e582019-05-07 11:33:10 -06001710 temp = n1;
1711 n1 = n2;
1712 n2 = temp;
Gavin Howard63738202018-09-26 15:34:20 -06001713 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001714
Gavin Howard2d188a52019-02-25 14:19:08 -07001715 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001716
Gavin Howard2d188a52019-02-25 14:19:08 -07001717 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001718 ptr = bc_vec_item_rev(&stack, i);
1719 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001720 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001721 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001722
Gavin Howard2d188a52019-02-25 14:19:08 -07001723 if (BC_SIG) goto sig_err;
Stefan Esser276de8c2019-05-04 20:12:43 +02001724 if (!n->scale) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001725
Gavin Howardfbae8a52019-05-07 11:15:38 -06001726 radix = true;
Gavin Howarda8c2e582019-05-07 11:33:10 -06001727 n1 = &flen1;
1728 n2 = &flen2;
Gavin Howardfbae8a52019-05-07 11:15:38 -06001729
Gavin Howard06fee0c2019-05-09 14:43:20 -06001730 while (BC_NO_SIG && bc_num_intDigits(n1) < n->scale + 1) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001731
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001732 bc_num_expand(&fracp2, fracp1.len + 1);
1733 s = bc_num_mulArray(&fracp1, base, &fracp2);
Gavin Howardc78e7522019-02-22 13:24:25 -07001734 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001735 fracp2.scale = n->scale;
1736 fracp2.rdx = BC_NUM_RDX(fracp2.scale);
Gavin Howard1769abc2019-02-21 09:36:14 -07001737
1738 // Will never fail (except for signals) because fracp is
1739 // guaranteed to be non-negative and small enough.
Gavin Howard2956a5e2019-05-08 08:04:06 -06001740 s = bc_num_bigdig(&fracp2, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001741 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001742
Gavin Howard2956a5e2019-05-08 08:04:06 -06001743 bc_num_bigdig2num(&digit, dig);
Gavin Howard43b6fd72019-05-07 11:39:19 -06001744 s = bc_num_sub(&fracp2, &digit, &fracp1, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001745 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001746
Gavin Howardddce6d42019-01-03 14:07:06 -07001747 print(dig, len, radix);
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001748 s = bc_num_mulArray(n1, base, n2);
Gavin Howardc78e7522019-02-22 13:24:25 -07001749 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardfbae8a52019-05-07 11:15:38 -06001750
1751 radix = false;
Gavin Howarda8c2e582019-05-07 11:33:10 -06001752 temp = n1;
1753 n1 = n2;
1754 n2 = temp;
Gavin Howard63738202018-09-26 15:34:20 -06001755 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001756
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001757sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001758 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001759err:
Gavin Howardc4e9a082019-05-07 11:12:50 -06001760 bc_num_free(&intp2);
Gavin Howardea8d5132019-05-07 11:52:04 -06001761 bc_num_free(&intp1);
Gavin Howarda8c2e582019-05-07 11:33:10 -06001762 bc_num_free(&fracp2);
1763 bc_num_free(&fracp1);
Gavin Howard63738202018-09-26 15:34:20 -06001764 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001765 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001766}
1767
Gavin Howard2956a5e2019-05-08 08:04:06 -06001768static BcStatus bc_num_printBase(BcNum *restrict n, BcBigDig base) {
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001769
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001770 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001771 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001772 BcNumDigitOp print;
1773 bool neg = n->neg;
1774
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001775 if (neg) bc_num_putchar('-');
Gavin Howard83eb8392018-10-09 01:21:19 -06001776
1777 n->neg = false;
1778
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001779 if (base <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001780 width = 1;
1781 print = bc_num_printHex;
1782 }
1783 else {
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001784 width = bc_num_log10(base - 1);
Gavin Howard83eb8392018-10-09 01:21:19 -06001785 print = bc_num_printDigits;
1786 }
1787
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001788 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001789 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001790
1791 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001792}
1793
Gavin Howard40a085f2018-12-03 12:08:59 -07001794#if DC_ENABLED
Gavin Howard2956a5e2019-05-08 08:04:06 -06001795BcStatus bc_num_stream(BcNum *restrict n, BcBigDig base) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001796 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001797}
1798#endif // DC_ENABLED
1799
Gavin Howard3c02ed32018-12-28 18:17:15 -07001800void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001801 assert(n);
1802 n->num = num;
1803 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001804 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001805 n->neg = false;
1806}
1807
Gavin Howard3c02ed32018-12-28 18:17:15 -07001808void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001809 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001810 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howard404ece72019-04-26 15:59:15 -06001811 bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001812}
1813
Gavin Howarded392aa2018-02-27 13:09:26 -07001814void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001815 assert(num);
1816 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001817}
1818
Gavin Howard3c02ed32018-12-28 18:17:15 -07001819void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001820 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001821 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001822 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001823 d->len = s->len;
1824 d->neg = s->neg;
1825 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001826 d->scale = s->scale;
Gavin Howard404ece72019-04-26 15:59:15 -06001827 memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -07001828}
1829
Gavin Howardbaa4f582019-01-24 13:56:35 -07001830void bc_num_createCopy(BcNum *d, const BcNum *s) {
1831 bc_num_init(d, s->len);
1832 bc_num_copy(d, s);
1833}
1834
Gavin Howard2956a5e2019-05-08 08:04:06 -06001835void bc_num_createFromBigdig(BcNum *n, BcBigDig val) {
1836 bc_num_init(n, (BC_NUM_BIGDIG_LOG10 - 1) / BC_BASE_POWER + 1);
1837 bc_num_bigdig2num(n, val);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001838}
1839
Gavin Howard7ad5a662019-02-19 14:40:46 -07001840size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001841 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001842}
1843
1844size_t bc_num_len(const BcNum *restrict n) {
1845
Gavin Howardb41483b2019-04-27 08:30:10 -06001846 size_t i, pow, scale, len = n->len;
Gavin Howardfcf29012019-04-25 20:12:19 -06001847 BcDig dig;
1848
Gavin Howardb41483b2019-04-27 08:30:10 -06001849 if (BC_NUM_ZERO(n)) return 0;
Gavin Howardc1349922019-04-30 20:48:09 -06001850 if (n->rdx == len) len = bc_num_nonzeroLen(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001851
Gavin Howardfcf29012019-04-25 20:12:19 -06001852 dig = n->num[len - 1];
Gavin Howardb41483b2019-04-27 08:30:10 -06001853 pow = BC_BASE_DIG;
1854 i = BC_BASE_POWER + 1;
Gavin Howardfcf29012019-04-25 20:12:19 -06001855
Gavin Howardb41483b2019-04-27 08:30:10 -06001856 while (pow && (dig % (BcDig) pow == dig)) {
1857 i -= 1;
1858 pow /= BC_BASE;
1859 }
Gavin Howardfcf29012019-04-25 20:12:19 -06001860
Gavin Howardb41483b2019-04-27 08:30:10 -06001861 scale = n->scale % BC_BASE_POWER;
1862 scale = scale ? scale : BC_BASE_POWER;
1863
1864 return (len - 1) * BC_BASE_POWER + i - (BC_BASE_POWER - scale);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001865}
1866
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001867BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
Gavin Howard2956a5e2019-05-08 08:04:06 -06001868 BcBigDig base, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001869{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001870 BcStatus s = BC_STATUS_SUCCESS;
1871
Gavin Howard63738202018-09-26 15:34:20 -06001872 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001873 assert(BC_ENABLE_EXTRA_MATH ||
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001874 (base >= BC_NUM_MIN_BASE && base <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001875 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001876
Gavin Howard2956a5e2019-05-08 08:04:06 -06001877 if (letter) {
1878 BcBigDig dig = bc_num_parseChar(val[0], BC_NUM_MAX_LBASE);
1879 bc_num_bigdig2num(n, dig);
1880 }
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001881 else if (base == BC_BASE) bc_num_parseDecimal(n, val);
1882 else s = bc_num_parseBase(n, val, base);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001883
1884 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001885}
1886
Gavin Howard2956a5e2019-05-08 08:04:06 -06001887BcStatus bc_num_print(BcNum *restrict n, BcBigDig base, bool newline) {
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001888
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001889 BcStatus s = BC_STATUS_SUCCESS;
1890
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001891 assert(n);
1892 assert(BC_ENABLE_EXTRA_MATH || base >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001893
Gavin Howardab5e9632019-01-02 13:32:02 -07001894 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001895
Gavin Howardddce6d42019-01-03 14:07:06 -07001896 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001897 else if (base == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001898#if BC_ENABLE_EXTRA_MATH
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001899 else if (base == 0 || base == 1)
1900 s = bc_num_printExponent(n, base != 0);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001901#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardb3f1ee22019-05-07 21:49:15 -06001902 else s = bc_num_printBase(n, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001903
Gavin Howardb4a9cdb2019-05-07 15:25:51 -06001904 if (BC_NO_ERR(!s) && newline) bc_num_putchar('\n');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001905
1906 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001907}
1908
Gavin Howard2956a5e2019-05-08 08:04:06 -06001909BcStatus bc_num_bigdig(const BcNum *restrict n, BcBigDig *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001910
Gavin Howard63738202018-09-26 15:34:20 -06001911 size_t i;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001912 BcBigDig r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001913
Gavin Howard63738202018-09-26 15:34:20 -06001914 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001915
Gavin Howardecafd4f2019-02-23 09:30:45 -07001916 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001917
Gavin Howard06b6e862019-02-15 11:15:31 -07001918 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001919
Gavin Howard2956a5e2019-05-08 08:04:06 -06001920 BcBigDig prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07001921
Gavin Howard0700efc2019-04-24 17:31:23 -06001922 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001923 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1924
Gavin Howard2956a5e2019-05-08 08:04:06 -06001925 r = prev + (BcBigDig) n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07001926
1927 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001928 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001929 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001930
Gavin Howard7b557da2018-12-21 10:25:32 -07001931 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001932
Gavin Howard63738202018-09-26 15:34:20 -06001933 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001934}
1935
Gavin Howard2956a5e2019-05-08 08:04:06 -06001936void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001937
Gavin Howardc25fd612019-04-26 19:31:31 -06001938 BcDig *ptr;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001939 size_t i;
Gavin Howardc25fd612019-04-26 19:31:31 -06001940
Gavin Howard63738202018-09-26 15:34:20 -06001941 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001942
Gavin Howard63738202018-09-26 15:34:20 -06001943 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001944
Gavin Howard1ab22d22019-01-03 13:32:17 -07001945 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001946
Gavin Howard2956a5e2019-05-08 08:04:06 -06001947 bc_num_expand(n, BC_NUM_BIGDIG_LOG10);
Gavin Howard05feab02019-04-23 16:24:07 -06001948
Gavin Howardc25fd612019-04-26 19:31:31 -06001949 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_DIG)
1950 ptr[i] = val % BC_BASE_DIG;
Gavin Howard025d04d2018-02-20 13:53:28 -07001951}
1952
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001953size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001954
1955 size_t aint, bint, ardx, brdx;
1956
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001957 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001958
1959 ardx = a->rdx;
1960 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001961 aint = bc_num_int(a);
1962 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001963 ardx = BC_MAX(ardx, brdx);
1964 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001965
Gavin Howard2d188a52019-02-25 14:19:08 -07001966 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001967}
1968
1969size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001970 size_t max, rdx;
1971 rdx = bc_vm_growSize(a->rdx, b->rdx);
Stefan Eßer0dc37b62019-04-29 14:13:11 -06001972 max = bc_vm_growSize(BC_MAX(BC_NUM_RDX(scale), rdx), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001973 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1974 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001975}
1976
1977size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1978 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001979 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001980}
1981
1982#if BC_ENABLE_EXTRA_MATH
Gavin Howard194380e2019-05-06 08:39:30 -06001983size_t bc_num_placesReq(BcNum *a, BcNum *b, size_t scale) {
1984
1985 BcStatus s;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001986 BcBigDig places;
Gavin Howard194380e2019-05-06 08:39:30 -06001987 size_t rdx;
1988
1989 BC_UNUSED(s);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001990 BC_UNUSED(scale);
Gavin Howard194380e2019-05-06 08:39:30 -06001991
Gavin Howard194380e2019-05-06 08:39:30 -06001992 // This error will be taken care of later. Ignore.
Gavin Howard2956a5e2019-05-08 08:04:06 -06001993 s = bc_num_bigdig(b, &places);
Gavin Howard194380e2019-05-06 08:39:30 -06001994
1995 if (a->scale <= places) rdx = BC_NUM_RDX(places);
1996 else rdx = BC_NUM_RDX(a->scale - places);
1997
Gavin Howard06fee0c2019-05-09 14:43:20 -06001998 return BC_NUM_RDX(bc_num_intDigits(a)) + rdx;
Gavin Howard194380e2019-05-06 08:39:30 -06001999}
2000
2001size_t bc_num_shiftLeftReq(BcNum *a, BcNum *b, size_t scale) {
2002
2003 BcStatus s;
Gavin Howard2956a5e2019-05-08 08:04:06 -06002004 BcBigDig places, rdx;
Gavin Howard194380e2019-05-06 08:39:30 -06002005
2006 BC_UNUSED(s);
2007 BC_UNUSED(scale);
2008
Gavin Howard194380e2019-05-06 08:39:30 -06002009 // This error will be taken care of later. Ignore.
Gavin Howard2956a5e2019-05-08 08:04:06 -06002010 s = bc_num_bigdig(b, &places);
Gavin Howard194380e2019-05-06 08:39:30 -06002011
2012 if (a->scale <= places) rdx = BC_NUM_RDX(places) - a->rdx + 1;
2013 else rdx = 0;
2014
2015 return a->len + rdx;
2016}
2017
2018size_t bc_num_shiftRightReq(BcNum *a, BcNum *b, size_t scale) {
2019
2020 BcStatus s;
Gavin Howard2956a5e2019-05-08 08:04:06 -06002021 BcBigDig places, int_digs, rdx;
Gavin Howard194380e2019-05-06 08:39:30 -06002022
2023 BC_UNUSED(s);
2024 BC_UNUSED(scale);
2025
Gavin Howard194380e2019-05-06 08:39:30 -06002026 // This error will be taken care of later. Ignore.
Gavin Howard2956a5e2019-05-08 08:04:06 -06002027 s = bc_num_bigdig(b, &places);
Gavin Howard194380e2019-05-06 08:39:30 -06002028
Gavin Howard06fee0c2019-05-09 14:43:20 -06002029 int_digs = BC_NUM_RDX(bc_num_intDigits(a));
Gavin Howard194380e2019-05-06 08:39:30 -06002030 rdx = BC_NUM_RDX(places);
2031
2032 if (int_digs <= rdx) rdx -= int_digs;
2033 else rdx = 0;
2034
2035 return a->len + rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002036}
2037#endif // BC_ENABLE_EXTRA_MATH
2038
Gavin Howard12fe7812018-09-29 03:45:18 -06002039BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06002040 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07002041 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002042 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002043}
2044
Gavin Howard12fe7812018-09-29 03:45:18 -06002045BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06002046 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07002047 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002048 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002049}
2050
Gavin Howard12fe7812018-09-29 03:45:18 -06002051BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002052 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002053}
2054
Gavin Howard12fe7812018-09-29 03:45:18 -06002055BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002056 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002057}
2058
Gavin Howard54b946a2018-10-23 12:06:57 -06002059BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07002060 size_t req = bc_num_mulReq(a, b, scale);
2061 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07002062}
2063
Gavin Howard12fe7812018-09-29 03:45:18 -06002064BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07002065 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002066}
2067
Gavin Howard7d8f0382018-12-27 16:15:13 -07002068#if BC_ENABLE_EXTRA_MATH
2069BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard8291ecd2019-05-06 14:34:53 -06002070 size_t req = bc_num_placesReq(a, b, scale);
2071 return bc_num_binary(a, b, c, scale, bc_num_place, req);
Gavin Howard7d8f0382018-12-27 16:15:13 -07002072}
2073
2074BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard8291ecd2019-05-06 14:34:53 -06002075 size_t req = bc_num_shiftLeftReq(a, b, scale);
2076 return bc_num_binary(a, b, c, scale, bc_num_left, req);
Gavin Howard7d8f0382018-12-27 16:15:13 -07002077}
2078
2079BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard8291ecd2019-05-06 14:34:53 -06002080 size_t req = bc_num_shiftRightReq(a, b, scale);
2081 return bc_num_binary(a, b, c, scale, bc_num_right, req);
Gavin Howard7d8f0382018-12-27 16:15:13 -07002082}
2083#endif // BC_ENABLE_EXTRA_MATH
2084
Gavin Howard3c02ed32018-12-28 18:17:15 -07002085BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06002086
Gavin Howard1cbfe242019-01-09 17:13:11 -07002087 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002088 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Stefan Eßer4276af42019-04-29 14:04:57 -06002089 size_t pow, len, rdx, req, digs, digs1, digs2, resscale, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002090 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Stefan Eßer4276af42019-04-29 14:04:57 -06002091 BcDig half_digs[1];
Gavin Howard954276b2018-05-16 01:43:58 -06002092
Gavin Howard34edd0a2018-10-25 15:51:05 -06002093 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06002094
Gavin Howardecafd4f2019-02-23 09:30:45 -07002095 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07002096
Stefan Eßer4276af42019-04-29 14:04:57 -06002097 if (a->scale > scale) scale = a->scale;
Gavin Howard06fee0c2019-05-09 14:43:20 -06002098 len = bc_vm_growSize(bc_num_intDigits(a), 1);
Stefan Eßer4276af42019-04-29 14:04:57 -06002099 req = bc_vm_growSize(BC_MAX(BC_NUM_RDX(scale), a->rdx), len >> 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07002100 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06002101
Gavin Howardddce6d42019-01-03 14:07:06 -07002102 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06002103 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002104 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06002105 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07002106 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06002107 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002108 bc_num_extend(b, scale);
2109 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06002110 }
Gavin Howard954276b2018-05-16 01:43:58 -06002111
Stefan Eßer4276af42019-04-29 14:04:57 -06002112 rdx = BC_MAX(BC_NUM_RDX(scale), a->rdx);
2113 len = bc_vm_growSize(a->len, rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06002114
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002115 bc_num_init(&num1, len);
2116 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07002117 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06002118
Gavin Howard63738202018-09-26 15:34:20 -06002119 bc_num_one(&half);
Stefan Eßer4276af42019-04-29 14:04:57 -06002120 half.num[0] = BC_BASE_DIG / 2;
2121 half.len = 1;
Gavin Howard63738202018-09-26 15:34:20 -06002122 half.rdx = 1;
Stefan Eßer4276af42019-04-29 14:04:57 -06002123 half.scale = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06002124
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002125 bc_num_init(&f, len);
2126 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06002127
Gavin Howard63738202018-09-26 15:34:20 -06002128 x0 = &num1;
2129 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06002130
Gavin Howard63738202018-09-26 15:34:20 -06002131 bc_num_one(x0);
Gavin Howard06fee0c2019-05-09 14:43:20 -06002132 pow = bc_num_intDigits(a);
Gavin Howard954276b2018-05-16 01:43:58 -06002133
Gavin Howard53eba8b2018-10-31 15:14:37 -06002134 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06002135
Gavin Howardc39fd492018-10-04 10:07:03 -06002136 if (pow & 1) x0->num[0] = 2;
2137 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06002138
Gavin Howardc39fd492018-10-04 10:07:03 -06002139 pow -= 2 - (pow & 1);
Gavin Howard74704572019-05-07 13:48:48 -06002140 s = bc_num_shiftLeft(x0, pow / 2);
2141 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06002142
Gavin Howard63738202018-09-26 15:34:20 -06002143 // Make sure to move the radix back.
Gavin Howard1d857192019-05-06 14:35:07 -06002144 if (x0->scale >= pow) x0->scale -= pow;
2145 else x0->scale = 0;
Stefan Eßer4276af42019-04-29 14:04:57 -06002146 x0->rdx = BC_NUM_RDX(x0->scale);
Gavin Howard63738202018-09-26 15:34:20 -06002147 }
Gavin Howard954276b2018-05-16 01:43:58 -06002148
Gavin Howard14913fc2019-04-23 14:36:03 -06002149 x0->rdx = digs = digs1 = digs2 = 0;
Stefan Eßer4276af42019-04-29 14:04:57 -06002150 x0->scale = 0;
2151 resscale = (scale + BC_BASE_POWER) * 2;
2152
Gavin Howard06fee0c2019-05-09 14:43:20 -06002153 len = BC_NUM_RDX(bc_num_intDigits(x0) + resscale - 1);
Gavin Howard954276b2018-05-16 01:43:58 -06002154
Gavin Howard2d188a52019-02-25 14:19:08 -07002155 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06002156
Gavin Howard971a2672019-04-26 14:32:07 -06002157 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07002158
Stefan Eßer4276af42019-04-29 14:04:57 -06002159 s = bc_num_div(a, x0, &f, resscale);
Gavin Howard1769abc2019-02-21 09:36:14 -07002160 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002161 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Eßer4276af42019-04-29 14:04:57 -06002162 s = bc_num_add(x0, &f, &fprime, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07002163 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Eßer4276af42019-04-29 14:04:57 -06002164 s = bc_num_mul(&fprime, &half, x1, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07002165 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06002166
Gavin Howard53eba8b2018-10-31 15:14:37 -06002167 cmp = bc_num_cmp(x1, x0);
Gavin Howarda36f0312019-05-09 10:23:07 -06002168 if (cmp == BC_NUM_CMP_SIGNAL) {
Gavin Howardf2ee6342019-04-09 17:05:20 -06002169 s = BC_STATUS_SIGNAL;
2170 break;
2171 }
2172
Gavin Howard53eba8b2018-10-31 15:14:37 -06002173 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06002174
Gavin Howardb5ec7f32018-10-29 12:32:39 -06002175 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06002176 else times = 0;
2177
Stefan Eßer4276af42019-04-29 14:04:57 -06002178 resscale += times > 2;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002179
2180 cmp2 = cmp1;
2181 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06002182 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002183
Gavin Howard63738202018-09-26 15:34:20 -06002184 temp = x0;
2185 x0 = x1;
2186 x1 = temp;
2187 }
Gavin Howard954276b2018-05-16 01:43:58 -06002188
Gavin Howard2d188a52019-02-25 14:19:08 -07002189 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07002190 s = BC_STATUS_SIGNAL;
2191 goto err;
2192 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06002193
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002194 bc_num_copy(b, x0);
Stefan Eßer4276af42019-04-29 14:04:57 -06002195 if (b->scale > scale) bc_num_truncate(b, b->scale - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06002196
2197err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06002198 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06002199 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06002200 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06002201 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06002202 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07002203 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002204 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06002205 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07002206}
Gavin Howardba009802018-09-29 04:41:51 -06002207
Gavin Howard2cb39612018-10-22 08:46:48 -06002208BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
2209
2210 BcStatus s;
2211 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06002212 bool init = false;
Stefan Eßera7fbbf62019-04-29 14:14:30 -06002213 size_t ts, len;
2214
2215 ts = BC_MAX(scale + b->scale, a->scale);
2216 len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002217
Gavin Howardf2ef2f32019-01-16 11:25:16 -07002218 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06002219
Gavin Howard890d0c02018-10-30 16:34:50 -06002220 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06002221 memcpy(&num2, c, sizeof(BcNum));
2222 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002223 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06002224 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06002225 }
Gavin Howardf679ad82018-10-29 12:26:30 -06002226 else {
2227 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002228 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06002229 }
Gavin Howard2cb39612018-10-22 08:46:48 -06002230
Gavin Howardf5961782019-05-09 07:50:46 -06002231 if (BC_NUM_NONZERO(a) && !a->rdx && !b->rdx && b->len == 1 && !scale) {
Gavin Howard2956a5e2019-05-08 08:04:06 -06002232 BcBigDig rem;
Gavin Howard9d771ba2019-05-08 09:09:25 -06002233 s = bc_num_divArray(ptr_a, (BcBigDig) b->num[0], c, &rem);
Gavin Howardb3f1ee22019-05-07 21:49:15 -06002234 assert(rem < BC_BASE_DIG);
2235 d->num[0] = (BcDig) rem;
Gavin Howardf5961782019-05-09 07:50:46 -06002236 d->len = (rem != 0);
Gavin Howardb3f1ee22019-05-07 21:49:15 -06002237 }
Gavin Howardf5961782019-05-09 07:50:46 -06002238 else s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002239
Gavin Howardddce6d42019-01-03 14:07:06 -07002240 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002241 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07002242 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002243 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06002244
2245 if (init) bc_num_free(&num2);
2246
2247 return s;
2248}
2249
Gavin Howard40a085f2018-12-03 12:08:59 -07002250#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06002251BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06002252
2253 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002254 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07002255 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06002256
Gavin Howard34edd0a2018-10-25 15:51:05 -06002257 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06002258
Gavin Howardecafd4f2019-02-23 09:30:45 -07002259 if (BC_ERR(BC_NUM_ZERO(c)))
2260 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
2261 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
2262 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07002263 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06002264
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002265 bc_num_expand(d, c->len);
2266 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07002267 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howard303572b2019-05-09 07:50:56 -06002268 bc_num_init(&temp, b->len + 1);
Gavin Howardba009802018-09-29 04:41:51 -06002269
2270 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06002271 two.num[0] = 2;
2272 bc_num_one(d);
2273
Gavin Howard1769abc2019-02-21 09:36:14 -07002274 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002275 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002276 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002277 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07002278 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06002279
Gavin Howard2d188a52019-02-25 14:19:08 -07002280 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06002281
Gavin Howard1769abc2019-02-21 09:36:14 -07002282 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002283 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002284 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002285
Gavin Howarda83a5e52019-02-21 16:51:06 -07002286 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06002287 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002288 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002289
2290 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002291 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002292 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002293 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002294 }
2295
Gavin Howard53eba8b2018-10-31 15:14:37 -06002296 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002297 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002298
2299 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002300 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002301 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002302 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002303 }
2304
Gavin Howard2d188a52019-02-25 14:19:08 -07002305 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002306
Gavin Howardba009802018-09-29 04:41:51 -06002307err:
Gavin Howardba009802018-09-29 04:41:51 -06002308 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002309rem_err:
2310 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002311 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002312 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002313 return s;
2314}
Gavin Howarde6e84762018-10-03 11:46:34 -06002315#endif // DC_ENABLED
Gavin Howardefc1e202019-05-06 08:01:07 -06002316
2317#if BC_DEBUG_CODE
2318void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline) {
2319 printf("%s: ", name);
2320 bc_num_printDecimal(n);
2321 printf("\n");
2322 if (emptyline) printf("\n");
Gavin Howarde34f7d82019-05-07 10:32:39 -06002323 vm->nchars = 0;
Gavin Howardefc1e202019-05-06 08:01:07 -06002324}
2325
Gavin Howard2a84f962019-05-08 17:20:09 -06002326void bc_num_printDigs(const BcDig *n, size_t len, bool emptyline) {
Gavin Howardefc1e202019-05-06 08:01:07 -06002327
2328 size_t i;
2329
Gavin Howard2a84f962019-05-08 17:20:09 -06002330 for (i = len - 1; i < len; --i) printf(" %0*d", BC_BASE_POWER, n[i]);
Gavin Howardefc1e202019-05-06 08:01:07 -06002331
2332 printf("\n");
2333 if (emptyline) printf("\n");
Gavin Howarde34f7d82019-05-07 10:32:39 -06002334 vm->nchars = 0;
Gavin Howardefc1e202019-05-06 08:01:07 -06002335}
2336
Gavin Howard2a84f962019-05-08 17:20:09 -06002337void bc_num_printWithDigs(const BcNum *n, const char *name, bool emptyline) {
2338 printf("%s len: %zu, rdx: %zu, scale: %zu\n",
2339 name, n->len, n->rdx, n->scale);
2340 bc_num_printDigs(n->num, n->len, emptyline);
2341}
2342
Gavin Howardefc1e202019-05-06 08:01:07 -06002343void bc_num_dump(const char *varname, const BcNum *n) {
2344
2345 unsigned long i, scale = n->scale;
2346
2347 fprintf(stderr, "\n%s = %s", varname, n->len ? (n->neg ? "-" : "+") : "0 ");
2348
2349 for (i = n->len - 1; i < n->len; --i) {
2350
2351 if (i + 1 == n->rdx) fprintf(stderr, ". ");
2352
2353 if (scale / BC_BASE_POWER != n->rdx - i - 1)
2354 fprintf(stderr, "%0*d ", BC_BASE_POWER, n->num[i]);
2355 else {
2356
2357 int mod = scale % BC_BASE_POWER;
2358 int d = BC_BASE_POWER - mod;
2359 BcDig div;
2360
2361 if (mod != 0) {
Gavin Howard2a84f962019-05-08 17:20:09 -06002362 div = n->num[i] / ((BcDig) bc_num_pow10[(unsigned long) d]);
Gavin Howardefc1e202019-05-06 08:01:07 -06002363 fprintf(stderr, "%0*d", (int) mod, div);
2364 }
2365
Gavin Howard2a84f962019-05-08 17:20:09 -06002366 div = n->num[i] % ((BcDig) bc_num_pow10[(unsigned long) d]);
Gavin Howardefc1e202019-05-06 08:01:07 -06002367 fprintf(stderr, " ' %0*d ", d, div);
2368 }
2369 }
2370
2371 fprintf(stderr, "(%zu | %zu.%zu / %zu) %p\n",
2372 n->scale, n->len, n->rdx, n->cap, (void*) n->num);
Gavin Howarde34f7d82019-05-07 10:32:39 -06002373 vm->nchars = 0;
Gavin Howardefc1e202019-05-06 08:01:07 -06002374}
2375#endif // BC_DEBUG_CODE