blob: 9773202245910af41b8201c01817cfca5930a26c [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 Howard798508b2019-02-21 16:45:32 -070062static void bc_num_expand(BcNum *restrict n, size_t req) {
63 assert(n);
64 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
65 if (req > n->cap) {
Gavin Howard404ece72019-04-26 15:59:15 -060066 n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req));
Gavin Howard798508b2019-02-21 16:45:32 -070067 n->cap = req;
68 }
69}
70
Gavin Howard1cbfe242019-01-09 17:13:11 -070071static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060072 assert(n);
Gavin Howardfcf29012019-04-25 20:12:19 -060073 n->scale = scale;
74 n->len = n->rdx = 0;
Gavin Howard97102b12018-11-01 23:37:31 -060075 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060076}
77
Gavin Howard1cbfe242019-01-09 17:13:11 -070078static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060079 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060080}
81
Gavin Howard3c02ed32018-12-28 18:17:15 -070082void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060083 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060084 n->len = 1;
85 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060086}
87
Gavin Howard3c02ed32018-12-28 18:17:15 -070088void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060089 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060090 bc_num_setToZero(n, 0);
Gavin Howardb53a9942019-04-25 11:33:54 -060091 n->len = 1;
Stefan Eßer4b111c22019-04-24 17:23:08 -060092 n->num[0] = BC_BASE;
Gavin Howard0be26ed2018-08-31 20:21:56 -060093}
94
Gavin Howardb3f1ee22019-05-07 21:49:15 -060095static void bc_num_clean(BcNum *restrict n) {
96 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
97 if (BC_NUM_ZERO(n)) n->neg = false;
98 else if (n->len < n->rdx) n->len = n->rdx;
99}
100
Gavin Howard1cbfe242019-01-09 17:13:11 -0700101static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -0700102 size_t len;
Gavin Howard530e3072019-04-23 16:23:36 -0600103 for (len = 1; i; i /= BC_BASE, ++len);
Gavin Howardc1349922019-04-30 20:48:09 -0600104 return len - 1;
Gavin Howard1ab22d22019-01-03 13:32:17 -0700105}
106
Gavin Howard4007e2f2019-05-08 09:08:00 -0600107static size_t bc_num_zeroDigits(const BcDig *n) {
Gavin Howard92ba4e52019-05-10 20:49:13 -0600108 return BC_BASE_DIGS - bc_num_log10((size_t) *n);
Gavin Howard4007e2f2019-05-08 09:08:00 -0600109}
110
Gavin Howard06fee0c2019-05-09 14:43:20 -0600111static size_t bc_num_intDigits(const BcNum *n) {
Gavin Howard92ba4e52019-05-10 20:49:13 -0600112 size_t digits = bc_num_int(n) * BC_BASE_DIGS;
Gavin Howard4007e2f2019-05-08 09:08:00 -0600113 if (digits > 0) digits -= bc_num_zeroDigits(n->num +n->len - 1);
Gavin Howard445c1892019-04-29 13:50:24 -0600114 return digits;
115}
116
Gavin Howardc1349922019-04-30 20:48:09 -0600117static size_t bc_num_nonzeroLen(const BcNum *restrict n) {
Gavin Howard08f78562019-04-29 07:56:04 -0600118 size_t i, len = n->len;
119 assert(len == n->rdx);
120 for (i = len - 1; i < n->len && !n->num[i]; --len, --i);
Gavin Howardc1349922019-04-30 20:48:09 -0600121 assert(len > 0);
Gavin Howard08f78562019-04-29 07:56:04 -0600122 return len;
123}
124
Gavin Howard2956a5e2019-05-08 08:04:06 -0600125static BcBigDig bc_num_addDigit(BcDig *restrict num, BcBigDig d, BcBigDig c) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600126 d += c;
Gavin Howard92ba4e52019-05-10 20:49:13 -0600127 *num = (BcDig) (d % BC_BASE_POW);
128 assert(*num >= 0 && *num < BC_BASE_POW);
129 return d / BC_BASE_POW;
Gavin Howard14c354a2019-04-24 14:28:24 -0600130}
131
132static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
133 size_t len)
134{
135 size_t i;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600136 BcBigDig carry = 0;
Gavin Howard14c354a2019-04-24 14:28:24 -0600137
138 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600139 BcBigDig in = ((BcBigDig) a[i]) + ((BcBigDig) b[i]);
Gavin Howard14c354a2019-04-24 14:28:24 -0600140 carry = bc_num_addDigit(a + i, in, carry);
141 }
142
143 for (; BC_NO_SIG && carry; ++i)
Gavin Howard2956a5e2019-05-08 08:04:06 -0600144 carry = bc_num_addDigit(a + i, (BcBigDig) a[i], carry);
Gavin Howard14c354a2019-04-24 14:28:24 -0600145
146 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
147}
148
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700149static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
150 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700151{
Gavin Howard63738202018-09-26 15:34:20 -0600152 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600153
Gavin Howard2d188a52019-02-25 14:19:08 -0700154 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600155
Gavin Howard2d188a52019-02-25 14:19:08 -0700156 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard92ba4e52019-05-10 20:49:13 -0600157 assert(a[i + j] >= -BC_BASE_POW);
158 a[i + j++] += BC_BASE_POW;
Gavin Howard54b946a2018-10-23 12:06:57 -0600159 a[i + j] -= 1;
Gavin Howard92ba4e52019-05-10 20:49:13 -0600160 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_POW);
Gavin Howard63738202018-09-26 15:34:20 -0600161 }
162 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600163
Gavin Howard2d188a52019-02-25 14:19:08 -0700164 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600165}
166
Gavin Howard2956a5e2019-05-08 08:04:06 -0600167static BcStatus bc_num_mulArray(const BcNum *restrict a, BcBigDig b,
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600168 BcNum *restrict c)
169{
170 size_t i;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600171 BcBigDig carry = 0;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600172
Gavin Howard92ba4e52019-05-10 20:49:13 -0600173 assert(b <= BC_BASE_POW);
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600174 assert(c->cap >= a->len + 1);
175
176 memset(c->num, 0, BC_NUM_SIZE(c->cap));
177
178 for (i = 0; BC_NO_SIG && i < a->len; ++i) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600179 BcBigDig in = ((BcBigDig) a->num[i]) * b + carry;
Gavin Howard92ba4e52019-05-10 20:49:13 -0600180 c->num[i] = in % BC_BASE_POW;
181 carry = in / BC_BASE_POW;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600182 }
183
184 if (BC_NO_SIG) {
Gavin Howard92ba4e52019-05-10 20:49:13 -0600185 assert(carry < BC_BASE_POW);
Gavin Howarde7ae4f42019-05-08 09:08:15 -0600186 c->num[i] = (BcDig) carry;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600187 c->len = a->len;
188 c->len += (carry != 0);
189 }
190
191 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
192}
193
Gavin Howard2956a5e2019-05-08 08:04:06 -0600194static BcStatus bc_num_divArray(const BcNum *restrict a, BcBigDig b,
195 BcNum *restrict c, BcBigDig *rem)
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600196{
197 size_t i;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600198 BcBigDig carry = 0;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600199
200 assert(c->cap >= a->len);
201
202 for (i = a->len - 1; BC_NO_SIG && i < a->len; --i) {
Gavin Howard92ba4e52019-05-10 20:49:13 -0600203 BcBigDig in = ((BcBigDig) a->num[i]) + carry * BC_BASE_POW;
Gavin Howarde7ae4f42019-05-08 09:08:15 -0600204 c->num[i] = (BcDig) (in / b);
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600205 carry = in % b;
206 }
207
208 c->len = a->len;
209 bc_num_clean(c);
210 *rem = carry;
211
212 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
213}
214
Gavin Howard2d188a52019-02-25 14:19:08 -0700215static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
216 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700217{
Gavin Howard63738202018-09-26 15:34:20 -0600218 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600219 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700220 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howarda36f0312019-05-09 10:23:07 -0600221 return BC_SIG ? BC_NUM_CMP_SIGNAL : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600222}
223
Gavin Howard3c02ed32018-12-28 18:17:15 -0700224ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700225
Gavin Howard63738202018-09-26 15:34:20 -0600226 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600227 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600228 bool a_max, neg = false;
229 ssize_t cmp;
230
231 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700232
Gavin Howard8a921bd2018-10-11 14:15:32 -0600233 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700234 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700235 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600236 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600237 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600238 else return -1;
239 }
240 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700241
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700242 a_int = bc_num_int(a);
243 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600244 a_int -= b_int;
245 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700246
Gavin Howard1ab22d22019-01-03 13:32:17 -0700247 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700248
Gavin Howard890d0c02018-10-30 16:34:50 -0600249 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600250 min = b->rdx;
251 diff = a->rdx - b->rdx;
252 max_num = a->num + diff;
253 min_num = b->num;
254 }
255 else {
256 min = a->rdx;
257 diff = b->rdx - a->rdx;
258 max_num = b->num + diff;
259 min_num = a->num;
260 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700261
Gavin Howard890d0c02018-10-30 16:34:50 -0600262 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howarda36f0312019-05-09 10:23:07 -0600263 if (cmp == BC_NUM_CMP_SIGNAL) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700264 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700265
Gavin Howard2d188a52019-02-25 14:19:08 -0700266 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700267 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600268 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700269
Gavin Howarda36f0312019-05-09 10:23:07 -0600270 return BC_SIG ? BC_NUM_CMP_SIGNAL : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700271}
272
Gavin Howard3c02ed32018-12-28 18:17:15 -0700273void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700274
Gavin Howardb1558662019-04-26 14:32:55 -0600275 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700276
Stefan Esser70f11852019-04-27 10:39:00 +0200277 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700278
Gavin Howardb1558662019-04-26 14:32:55 -0600279 places_rdx = n->rdx - BC_NUM_RDX(n->scale - places);
280 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
281
282 n->scale -= places;
283 n->rdx -= places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700284
Gavin Howardddce6d42019-01-03 14:07:06 -0700285 if (BC_NUM_NONZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600286
Gavin Howardc9bef2d2019-04-26 14:46:52 -0600287 size_t pow;
288
Gavin Howard92ba4e52019-05-10 20:49:13 -0600289 pow = n->scale % BC_BASE_DIGS;
290 pow = pow ? BC_BASE_DIGS - pow : 0;
Gavin Howardc1542802019-05-08 17:20:51 -0600291 pow = bc_num_pow10[pow];
Gavin Howardb1558662019-04-26 14:32:55 -0600292
293 n->len -= places_rdx;
Gavin Howard404ece72019-04-26 15:59:15 -0600294 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len));
Gavin Howardb1558662019-04-26 14:32:55 -0600295
296 // Clear the lower part of the last digit.
Gavin Howardda6b5432019-04-26 14:33:43 -0600297 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow;
Gavin Howardb1558662019-04-26 14:32:55 -0600298
Gavin Howard2a366922019-01-16 10:50:20 -0700299 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600300 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700301}
302
Stefan Esser70f11852019-04-27 10:39:00 +0200303static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700304
Gavin Howardb5e6da92019-04-30 19:58:04 -0600305 size_t places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700306
Gavin Howard1ab22d22019-01-03 13:32:17 -0700307 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700308
Gavin Howardb5e6da92019-04-30 19:58:04 -0600309 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
310
Gavin Howard8721e332019-05-06 08:02:49 -0600311 if (places_rdx) {
312 bc_num_expand(n, bc_vm_growSize(n->len, places_rdx));
313 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
314 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
315 }
Gavin Howardb5e6da92019-04-30 19:58:04 -0600316
Gavin Howarde6f326e2019-04-26 10:13:45 -0600317 n->rdx += places_rdx;
318 n->scale += places;
Gavin Howardb1558662019-04-26 14:32:55 -0600319 n->len += places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700320
Gavin Howarde6f326e2019-04-26 10:13:45 -0600321 assert(n->rdx == BC_NUM_RDX(n->scale));
Stefan Esser193f8522019-04-27 01:29:01 +0200322}
323
Gavin Howard2d188a52019-02-25 14:19:08 -0700324static void bc_num_retireMul(BcNum *restrict n, size_t scale,
325 bool neg1, bool neg2)
326{
Gavin Howard971a2672019-04-26 14:32:07 -0600327 if (n->scale < scale) bc_num_extend(n, scale - n->scale);
328 else bc_num_truncate(n, n->scale - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600329
Gavin Howard305249a2018-10-15 20:24:47 -0600330 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700331 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600332}
333
Gavin Howard2d188a52019-02-25 14:19:08 -0700334static void bc_num_split(const BcNum *restrict n, size_t idx,
335 BcNum *restrict a, BcNum *restrict b)
336{
Gavin Howard305249a2018-10-15 20:24:47 -0600337 if (idx < n->len) {
338
Gavin Howard305249a2018-10-15 20:24:47 -0600339 b->len = n->len - idx;
340 a->len = idx;
Gavin Howarda3f260c2019-04-26 15:47:25 -0600341 a->scale = a->rdx = b->scale = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600342
Gavin Howard404ece72019-04-26 15:59:15 -0600343 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len));
344 memcpy(a->num, n->num, BC_NUM_SIZE(idx));
Gavin Howard50c80022019-01-03 15:14:33 -0700345
346 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600347 }
Gavin Howard50c80022019-01-03 15:14:33 -0700348 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600349
350 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600351}
352
Gavin Howard14913fc2019-04-23 14:36:03 -0600353static size_t bc_num_shiftZero(BcNum *restrict n) {
354
355 size_t i;
356
357 assert(!n->rdx || BC_NUM_ZERO(n));
358
359 for (i = 0; i < n->len && !n->num[i]; ++i);
360
361 n->len -= i;
362 n->num += i;
363
364 return i;
365}
366
Gavin Howard2e736de2019-04-27 06:41:58 -0600367static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) {
368 n->len += places_rdx;
369 n->num -= places_rdx;
Gavin Howard14913fc2019-04-23 14:36:03 -0600370}
371
Gavin Howard2956a5e2019-05-08 08:04:06 -0600372static BcStatus bc_num_shift(BcNum *restrict n, BcBigDig dig) {
Gavin Howardbe884592019-04-29 15:07:49 -0600373
374 size_t i, len = n->len;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600375 BcBigDig carry = 0, pow;
Gavin Howardbe884592019-04-29 15:07:49 -0600376 BcDig *ptr = n->num;
Gavin Howard305249a2018-10-15 20:24:47 -0600377
Gavin Howard92ba4e52019-05-10 20:49:13 -0600378 assert(dig < BC_BASE_DIGS);
Gavin Howard305249a2018-10-15 20:24:47 -0600379
Gavin Howardc1542802019-05-08 17:20:51 -0600380 pow = bc_num_pow10[dig];
Gavin Howard92ba4e52019-05-10 20:49:13 -0600381 dig = bc_num_pow10[BC_BASE_DIGS - dig];
Gavin Howardb1558662019-04-26 14:32:55 -0600382
Gavin Howard834fde12019-04-30 08:00:05 -0600383 for (i = len - 1; BC_NO_SIG && i < len; --i) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600384 BcBigDig in, temp;
385 in = ((BcBigDig) ptr[i]);
Gavin Howard834fde12019-04-30 08:00:05 -0600386 temp = carry * dig;
387 carry = in % pow;
388 ptr[i] = ((BcDig) (in / pow)) + (BcDig) temp;
Gavin Howard305249a2018-10-15 20:24:47 -0600389 }
390
Gavin Howard834fde12019-04-30 08:00:05 -0600391 assert(!carry);
Gavin Howardb1558662019-04-26 14:32:55 -0600392
393 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600394}
395
Gavin Howardb1558662019-04-26 14:32:55 -0600396static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700397
Gavin Howardb1558662019-04-26 14:32:55 -0600398 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600399 BcBigDig dig;
Gavin Howard48a0e472019-04-26 15:39:46 -0600400 size_t places_rdx;
Gavin Howardbe884592019-04-29 15:07:49 -0600401 bool shift;
Gavin Howard14913fc2019-04-23 14:36:03 -0600402
Gavin Howardb1558662019-04-26 14:32:55 -0600403 if (!places) return s;
Gavin Howardabe4fdf2019-05-06 08:39:53 -0600404 if (places > n->scale) {
405 size_t size = bc_vm_growSize(BC_NUM_RDX(places - n->scale), n->len);
406 if (size > SIZE_MAX - 1) return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
407 }
Gavin Howard4631f3a2019-04-30 10:48:40 -0600408 if (BC_NUM_ZERO(n)) {
409 if (n->scale >= places) n->scale -= places;
410 else n->scale = 0;
411 return s;
412 }
Gavin Howard14913fc2019-04-23 14:36:03 -0600413
Gavin Howard92ba4e52019-05-10 20:49:13 -0600414 dig = (BcBigDig) (places % BC_BASE_DIGS);
Gavin Howard48a0e472019-04-26 15:39:46 -0600415 shift = (dig != 0);
Gavin Howard22253c72019-05-06 08:03:57 -0600416 places_rdx = BC_NUM_RDX(places);
Gavin Howard48a0e472019-04-26 15:39:46 -0600417
Gavin Howard22253c72019-05-06 08:03:57 -0600418 if (n->scale) {
Gavin Howarda1879192019-04-30 18:55:08 -0600419
420 if (n->rdx >= places_rdx) {
421
Gavin Howard92ba4e52019-05-10 20:49:13 -0600422 size_t mod = n->scale % BC_BASE_DIGS, revdig;
Gavin Howarda1879192019-04-30 18:55:08 -0600423
Gavin Howard92ba4e52019-05-10 20:49:13 -0600424 mod = mod ? mod : BC_BASE_DIGS;
425 revdig = dig ? BC_BASE_DIGS - dig : 0;
Gavin Howarda1879192019-04-30 18:55:08 -0600426
Gavin Howard92ba4e52019-05-10 20:49:13 -0600427 if (mod + revdig > BC_BASE_DIGS) places_rdx = 1;
Gavin Howarda1879192019-04-30 18:55:08 -0600428 else places_rdx = 0;
429 }
430 else places_rdx -= n->rdx;
431 }
Gavin Howard4631f3a2019-04-30 10:48:40 -0600432
433 if (places_rdx) {
Gavin Howarde396dff2019-05-01 07:28:36 -0600434 bc_num_expand(n, bc_vm_growSize(n->len, places_rdx));
Gavin Howard834fde12019-04-30 08:00:05 -0600435 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len));
436 memset(n->num, 0, BC_NUM_SIZE(places_rdx));
437 n->len += places_rdx;
Gavin Howardb1558662019-04-26 14:32:55 -0600438 }
Gavin Howard4631f3a2019-04-30 10:48:40 -0600439
440 if (places > n->scale) n->scale = n->rdx = 0;
441 else {
442 n->scale -= places;
443 n->rdx = BC_NUM_RDX(n->scale);
444 }
Gavin Howardb1558662019-04-26 14:32:55 -0600445
Gavin Howard92ba4e52019-05-10 20:49:13 -0600446 if (shift) s = bc_num_shift(n, BC_BASE_DIGS - dig);
Gavin Howardb1558662019-04-26 14:32:55 -0600447
448 bc_num_clean(n);
449
450 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
451}
452
453static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) {
454
455 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600456 BcBigDig dig;
Gavin Howardb5e6da92019-04-30 19:58:04 -0600457 size_t places_rdx, scale, scale_mod, int_len, expand;
458 bool shift;
Gavin Howardb1558662019-04-26 14:32:55 -0600459
460 if (!places) return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600461 if (BC_NUM_ZERO(n)) {
Gavin Howardb1558662019-04-26 14:32:55 -0600462 n->scale += places;
463 bc_num_expand(n, BC_NUM_RDX(n->scale));
464 return s;
Gavin Howard14913fc2019-04-23 14:36:03 -0600465 }
466
Gavin Howard92ba4e52019-05-10 20:49:13 -0600467 dig = (BcBigDig) (places % BC_BASE_DIGS);
Gavin Howardb5e6da92019-04-30 19:58:04 -0600468 shift = (dig != 0);
Gavin Howard834fde12019-04-30 08:00:05 -0600469 scale = n->scale;
Gavin Howard92ba4e52019-05-10 20:49:13 -0600470 scale_mod = scale % BC_BASE_DIGS;
471 scale_mod = scale_mod ? scale_mod : BC_BASE_DIGS;
Gavin Howard834fde12019-04-30 08:00:05 -0600472 int_len = bc_num_int(n);
Gavin Howardb1558662019-04-26 14:32:55 -0600473 places_rdx = BC_NUM_RDX(places);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700474
Gavin Howard92ba4e52019-05-10 20:49:13 -0600475 if (scale_mod + dig > BC_BASE_DIGS) {
Gavin Howardb5e6da92019-04-30 19:58:04 -0600476 expand = places_rdx - 1;
477 places_rdx = 1;
Gavin Howard834fde12019-04-30 08:00:05 -0600478 }
479 else {
Gavin Howardb5e6da92019-04-30 19:58:04 -0600480 expand = places_rdx;
481 places_rdx = 0;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700482 }
483
Gavin Howardb5e6da92019-04-30 19:58:04 -0600484 if (expand > int_len) expand -= int_len;
485 else expand = 0;
486
Gavin Howard92ba4e52019-05-10 20:49:13 -0600487 bc_num_extend(n, places_rdx * BC_BASE_DIGS);
Gavin Howarde396dff2019-05-01 07:28:36 -0600488 bc_num_expand(n, bc_vm_growSize(expand, n->len));
Gavin Howardb5e6da92019-04-30 19:58:04 -0600489 memset(n->num + n->len, 0, BC_NUM_SIZE(expand));
490 n->len += expand;
Gavin Howard834fde12019-04-30 08:00:05 -0600491 n->scale = n->rdx = 0;
Gavin Howardb1558662019-04-26 14:32:55 -0600492
Gavin Howard834fde12019-04-30 08:00:05 -0600493 if (shift) s = bc_num_shift(n, dig);
494
495 n->scale = scale + places;
496 n->rdx = BC_NUM_RDX(n->scale);
497
498 bc_num_clean(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700499
500 assert(n->rdx <= n->len && n->len <= n->cap);
Gavin Howardb1558662019-04-26 14:32:55 -0600501 assert(n->rdx == BC_NUM_RDX(n->scale));
502
503 return BC_SIG && !s ? BC_STATUS_SIGNAL : s;
Gavin Howard7ad5a662019-02-19 14:40:46 -0700504}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700505
Gavin Howard1cbfe242019-01-09 17:13:11 -0700506static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600507
Gavin Howard63738202018-09-26 15:34:20 -0600508 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600509 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600510
Gavin Howard971a2672019-04-26 14:32:07 -0600511 assert(BC_NUM_NONZERO(a));
Gavin Howard1769abc2019-02-21 09:36:14 -0700512
Gavin Howardd32d7df2018-10-15 08:42:25 -0600513 one.cap = 2;
514 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600515 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600516
Gavin Howardd32d7df2018-10-15 08:42:25 -0600517 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600518}
519
Gavin Howard7bda4782018-12-28 09:53:22 -0700520#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700521static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
Gavin Howard2956a5e2019-05-08 08:04:06 -0600522 BcBigDig *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700523{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700524 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700525 bc_num_copy(c, a);
Gavin Howard2956a5e2019-05-08 08:04:06 -0600526 return bc_num_bigdig(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700527}
Gavin Howard7bda4782018-12-28 09:53:22 -0700528#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700529
Gavin Howard1cbfe242019-01-09 17:13:11 -0700530static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700531
Gavin Howard8a921bd2018-10-11 14:15:32 -0600532 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600533 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600534 BcBigDig carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700535
Gavin Howard63738202018-09-26 15:34:20 -0600536 // Because this function doesn't need to use scale (per the bc spec),
537 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600538
Gavin Howardddce6d42019-01-03 14:07:06 -0700539 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600540 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700541 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600542 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600543 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700544 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600545 bc_num_copy(c, a);
546 return BC_STATUS_SUCCESS;
547 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600548
Gavin Howard63738202018-09-26 15:34:20 -0600549 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600550 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600551 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600552 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700553
Gavin Howard63738202018-09-26 15:34:20 -0600554 if (a->rdx > b->rdx) {
555 diff = a->rdx - b->rdx;
556 ptr = a->num;
557 ptr_a = a->num + diff;
558 ptr_b = b->num;
559 }
560 else {
561 diff = b->rdx - a->rdx;
562 ptr = b->num;
563 ptr_a = a->num;
564 ptr_b = b->num + diff;
565 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700566
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700567 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700568
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700569 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600570 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700571 a_int = bc_num_int(a);
572 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700573
Gavin Howard890d0c02018-10-30 16:34:50 -0600574 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600575 min_int = b_int;
576 max = a_int;
577 ptr = ptr_a;
578 }
579 else {
580 min_int = a_int;
581 max = b_int;
582 ptr = ptr_b;
583 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700584
Gavin Howard2d188a52019-02-25 14:19:08 -0700585 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600586 BcBigDig in = ((BcBigDig) ptr_a[i]) + ((BcBigDig) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700587 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600588 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700589
Gavin Howard2d188a52019-02-25 14:19:08 -0700590 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howard2956a5e2019-05-08 08:04:06 -0600591 carry = bc_num_addDigit(ptr_c + i, (BcBigDig) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700592
593 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700594
Gavin Howard1ab22d22019-01-03 13:32:17 -0700595 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600596
Gavin Howard2d188a52019-02-25 14:19:08 -0700597 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700598}
599
Gavin Howard1cbfe242019-01-09 17:13:11 -0700600static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700601
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700602 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600603 ssize_t cmp;
604 BcNum *minuend, *subtrahend;
605 size_t start;
606 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700607
Gavin Howard63738202018-09-26 15:34:20 -0600608 // Because this function doesn't need to use scale (per the bc spec),
609 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700610
Gavin Howardddce6d42019-01-03 14:07:06 -0700611 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600612 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700613 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600614 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600615 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700616 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600617 bc_num_copy(c, a);
618 return BC_STATUS_SUCCESS;
619 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700620
Gavin Howard63738202018-09-26 15:34:20 -0600621 aneg = a->neg;
622 bneg = b->neg;
623 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700624
Gavin Howard63738202018-09-26 15:34:20 -0600625 cmp = bc_num_cmp(a, b);
Gavin Howarda36f0312019-05-09 10:23:07 -0600626 if (cmp == BC_NUM_CMP_SIGNAL) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700627
Gavin Howard63738202018-09-26 15:34:20 -0600628 a->neg = aneg;
629 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700630
Gavin Howard1ab22d22019-01-03 13:32:17 -0700631 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600632 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600633 return BC_STATUS_SUCCESS;
634 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700635
636 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600637 neg = a->neg;
638 minuend = a;
639 subtrahend = b;
640 }
641 else {
642 neg = b->neg;
643 if (sub) neg = !neg;
644 minuend = b;
645 subtrahend = a;
646 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700647
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600648 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600649 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700650
Gavin Howarde6f326e2019-04-26 10:13:45 -0600651 if (c->scale < subtrahend->scale) {
652 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600653 start = 0;
654 }
655 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700656
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700657 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700658
Gavin Howard63738202018-09-26 15:34:20 -0600659 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700660
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700661 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700662}
663
Gavin Howarddc4e3712019-04-23 14:56:29 -0600664static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
665{
Gavin Howard971a2672019-04-26 14:32:07 -0600666 size_t i, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600667 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600668 BcBigDig sum = 0, carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600669
Gavin Howard971a2672019-04-26 14:32:07 -0600670 assert(sizeof(sum) >= sizeof(BcDig) * 2);
Gavin Howardb538d802019-04-23 16:50:55 -0600671 assert(!a->rdx && !b->rdx);
672
Gavin Howard5083aec2019-04-23 18:03:46 -0600673 clen = bc_vm_growSize(alen, blen);
Gavin Howarde396dff2019-05-01 07:28:36 -0600674 bc_num_expand(c, bc_vm_growSize(clen, 1));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600675
676 ptr_c = c->num;
Gavin Howard404ece72019-04-26 15:59:15 -0600677 memset(ptr_c, 0, BC_NUM_SIZE(c->cap));
Gavin Howarddc4e3712019-04-23 14:56:29 -0600678
Gavin Howard5083aec2019-04-23 18:03:46 -0600679 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600680
Gavin Howard5083aec2019-04-23 18:03:46 -0600681 ssize_t sidx = (ssize_t) (i - blen + 1);
682 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600683
Stefan Essera299ffc2019-04-24 23:28:32 +0200684 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
Stefan Eßer4b111c22019-04-24 17:23:08 -0600685
Gavin Howard2956a5e2019-05-08 08:04:06 -0600686 sum += ((BcBigDig) ptr_a[j]) * ((BcBigDig) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600687
Gavin Howard92ba4e52019-05-10 20:49:13 -0600688 if (sum >= BC_BASE_POW) {
689 carry += sum / BC_BASE_POW;
690 sum %= BC_BASE_POW;
Stefan Essera299ffc2019-04-24 23:28:32 +0200691 }
692 }
Stefan Eßer4b111c22019-04-24 17:23:08 -0600693
Stefan Essera299ffc2019-04-24 23:28:32 +0200694 ptr_c[i] = (BcDig) sum;
Gavin Howard92ba4e52019-05-10 20:49:13 -0600695 assert(ptr_c[i] < BC_BASE_POW);
Stefan Essera299ffc2019-04-24 23:28:32 +0200696 sum = carry;
697 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600698 }
699
Gavin Howard5083aec2019-04-23 18:03:46 -0600700 if (sum) {
Gavin Howard92ba4e52019-05-10 20:49:13 -0600701 assert(sum < BC_BASE_POW);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600702 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600703 clen += 1;
704 }
705
706 c->len = clen;
Gavin Howarda58840c2019-05-07 19:33:53 -0600707
Gavin Howarddc4e3712019-04-23 14:56:29 -0600708 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
709}
710
Gavin Howard14c354a2019-04-24 14:28:24 -0600711static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
712 size_t shift, BcNumShiftAddOp op)
713{
714 assert(n->len >= shift + a->len);
715 assert(!n->rdx && !a->rdx);
716 return op(n->num + shift, a->num, a->len);
717}
718
719static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600720
Gavin Howard9b801632019-02-16 23:34:06 -0700721 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200722 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600723 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700724 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600725 BcNumShiftAddOp op;
Gavin Howardb2f01a42019-05-10 20:00:32 -0600726 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600727
Gavin Howard14913fc2019-04-23 14:36:03 -0600728 assert(BC_NUM_ZERO(c));
729
Gavin Howardddce6d42019-01-03 14:07:06 -0700730 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600731 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardf1ae2be2019-05-09 11:58:22 -0600732 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) return BC_STATUS_SUCCESS;
Gavin Howardb2f01a42019-05-10 20:00:32 -0600733 if (aone || BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600734 bc_num_copy(c, aone ? b : a);
Gavin Howardb2f01a42019-05-10 20:00:32 -0600735 if ((aone && a->neg) || b->neg) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600736 return BC_STATUS_SUCCESS;
737 }
Gavin Howardb7417b12019-05-10 18:39:46 -0600738 if (a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howarddc4e3712019-04-23 14:56:29 -0600739 return bc_num_m_simp(a, b, c);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700740
Gavin Howarde0432502019-02-26 16:23:45 -0700741 max = BC_MAX(a->len, b->len);
Stefan Essera299ffc2019-04-24 23:28:32 +0200742 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700743 max2 = (max + 1) / 2;
744
Stefan Esser5fd43a32019-04-25 07:05:24 +0200745 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
Gavin Howard404ece72019-04-26 15:59:15 -0600746 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total));
Gavin Howard5bede412019-02-23 09:31:28 -0700747
748 bc_num_setup(&l1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200749 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700750 bc_num_setup(&h1, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200751 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700752 bc_num_setup(&l2, dig_ptr, max);
Stefan Essera299ffc2019-04-24 23:28:32 +0200753 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700754 bc_num_setup(&h2, 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(&m1, 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(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600759 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600760 bc_num_init(&z0, max);
761 bc_num_init(&z1, max);
762 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600763 max = bc_vm_growSize(max, max) + 1;
764 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600765
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600766 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600767 bc_num_clean(&l1);
768 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600769 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600770 bc_num_clean(&l2);
771 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700772
Gavin Howard14c354a2019-04-24 14:28:24 -0600773 bc_num_expand(c, max);
774 c->len = max;
Gavin Howard404ece72019-04-26 15:59:15 -0600775 memset(c->num, 0, BC_NUM_SIZE(c->len));
Gavin Howard305249a2018-10-15 20:24:47 -0600776
Gavin Howard14c354a2019-04-24 14:28:24 -0600777 s = bc_num_sub(&h1, &l1, &m1, 0);
778 if (BC_ERR(s)) goto err;
779 s = bc_num_sub(&l2, &h2, &m2, 0);
780 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600781
Gavin Howard971a2672019-04-26 14:32:07 -0600782 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600783
Gavin Howard14c354a2019-04-24 14:28:24 -0600784 s = bc_num_m(&h1, &h2, &z2, 0);
785 if (BC_ERR(s)) goto err;
786 bc_num_clean(&z2);
787
788 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
789 if (BC_ERR(s)) goto err;
790 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
791 if (BC_ERR(s)) goto err;
792 }
793
Gavin Howard971a2672019-04-26 14:32:07 -0600794 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600795
796 s = bc_num_m(&l1, &l2, &z0, 0);
797 if (BC_ERR(s)) goto err;
798 bc_num_clean(&z0);
799
800 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
801 if (BC_ERR(s)) goto err;
802 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
803 if (BC_ERR(s)) goto err;
804 }
805
Gavin Howard971a2672019-04-26 14:32:07 -0600806 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600807
808 s = bc_num_m(&m1, &m2, &z1, 0);
809 if (BC_ERR(s)) goto err;
810 bc_num_clean(&z1);
811
812 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
813 s = bc_num_shiftAddSub(c, &z1, max2, op);
814 if (BC_ERR(s)) goto err;
815 }
Gavin Howard305249a2018-10-15 20:24:47 -0600816
817err:
Gavin Howard5bede412019-02-23 09:31:28 -0700818 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600819 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600820 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600821 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600822 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600823 return s;
824}
825
Gavin Howard1cbfe242019-01-09 17:13:11 -0700826static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600827
828 BcStatus s;
829 BcNum cpa, cpb;
Gavin Howard48a0e472019-04-26 15:39:46 -0600830 size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600831
Gavin Howard14913fc2019-04-23 14:36:03 -0600832 bc_num_setToZero(c, 0);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600833 ascale = a->scale;
834 bscale = b->scale;
835 scale = BC_MAX(scale, ascale);
Gavin Howarda9a887a2019-04-29 14:00:56 -0600836 scale = BC_MAX(scale, bscale);
Gavin Howard7cb5f6d2019-04-29 14:01:44 -0600837
Gavin Howard48a0e472019-04-26 15:39:46 -0600838 rscale = ascale + bscale;
Gavin Howard14913fc2019-04-23 14:36:03 -0600839 scale = BC_MIN(rscale, scale);
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600840
Gavin Howardcc2974b2019-05-08 11:07:28 -0600841 if ((a->len == 1 || b->len == 1) && !a->rdx && !b->rdx) {
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600842
843 BcNum *operand;
Gavin Howard2956a5e2019-05-08 08:04:06 -0600844 BcBigDig dig;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600845
846 if (a->len == 1) {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600847 dig = (BcBigDig) a->num[0];
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600848 operand = b;
849 }
850 else {
Gavin Howard2956a5e2019-05-08 08:04:06 -0600851 dig = (BcBigDig) b->num[0];
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600852 operand = a;
853 }
854
855 s = bc_num_mulArray(operand, dig, c);
Gavin Howard66a93552019-05-09 17:13:59 -0600856 if (BC_ERROR_SIGNAL_ONLY(s)) return s;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600857
858 c->scale = operand->scale;
859 c->rdx = operand->rdx;
860 c->neg = (a->neg != b->neg);
Gavin Howard66a93552019-05-09 17:13:59 -0600861 return s;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600862 }
863
Gavin Howard66a93552019-05-09 17:13:59 -0600864 bc_num_init(&cpa, a->len + a->rdx);
865 bc_num_init(&cpb, b->len + b->rdx);
866 bc_num_copy(&cpa, a);
867 bc_num_copy(&cpb, b);
868
869 cpa.neg = cpb.neg = false;
870
Gavin Howard92ba4e52019-05-10 20:49:13 -0600871 ardx = cpa.rdx * BC_BASE_DIGS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600872 s = bc_num_shiftLeft(&cpa, ardx);
873 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600874 bc_num_clean(&cpa);
875 azero = bc_num_shiftZero(&cpa);
Gavin Howard48a0e472019-04-26 15:39:46 -0600876
Gavin Howard92ba4e52019-05-10 20:49:13 -0600877 brdx = cpb.rdx * BC_BASE_DIGS;
Gavin Howard48a0e472019-04-26 15:39:46 -0600878 s = bc_num_shiftLeft(&cpb, brdx);
879 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard14913fc2019-04-23 14:36:03 -0600880 bzero = bc_num_shiftZero(&cpb);
881 bc_num_clean(&cpb);
882
Gavin Howard14c354a2019-04-24 14:28:24 -0600883 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700884 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600885
Gavin Howard14913fc2019-04-23 14:36:03 -0600886 zero = bc_vm_growSize(azero, bzero);
887 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600888
Gavin Howard14913fc2019-04-23 14:36:03 -0600889 bc_num_expand(c, len);
Gavin Howard92ba4e52019-05-10 20:49:13 -0600890 s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_DIGS);
Gavin Howard48a0e472019-04-26 15:39:46 -0600891 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
892 s = bc_num_shiftRight(c, ardx + brdx);
893 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardb3f1ee22019-05-07 21:49:15 -0600894
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600895 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howardcf9f2462019-04-27 06:43:57 -0600896
Gavin Howard305249a2018-10-15 20:24:47 -0600897err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600898 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600899 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600900 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600901 bc_num_free(&cpa);
902 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700903}
904
Gavin Howard1980e032019-05-08 17:21:17 -0600905static ssize_t bc_num_divCmp(const BcDig *a, const BcNum *b, size_t len) {
906
907 ssize_t cmp;
908
909 if (b->len > len && a[len]) cmp = bc_num_compare(a, b->num, len + 1);
910 else if (b->len <= len) {
911 if (a[len]) cmp = 1;
912 else cmp = bc_num_compare(a, b->num, len);
913 }
914 else cmp = -1;
915
916 return cmp;
917}
918
Gavin Howardd5b27c82019-05-09 12:13:07 -0600919static BcStatus bc_num_d_long(BcNum *restrict a, const BcNum *restrict b,
Gavin Howardbd60ec72019-05-10 08:01:22 -0600920 BcNum *restrict c, size_t scale)
Gavin Howardd5b27c82019-05-09 12:13:07 -0600921{
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600922 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard92cf9052019-05-09 08:05:57 -0600923 BcBigDig divisor, q;
Gavin Howardbd60ec72019-05-10 08:01:22 -0600924 size_t len, end, i, rdx;
925 BcNum cpb, sub, temp;
Gavin Howardd5b27c82019-05-09 12:13:07 -0600926 BcDig *n;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700927
Gavin Howardd5b27c82019-05-09 12:13:07 -0600928 len = b->len;
929 end = a->len - len;
930 divisor = (BcBigDig) b->num[len - 1];
Gavin Howard070a6ce2019-05-08 11:50:25 -0600931
Gavin Howardd5b27c82019-05-09 12:13:07 -0600932 bc_num_expand(c, a->len);
933 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howardbd60ec72019-05-10 08:01:22 -0600934
Gavin Howardd5b27c82019-05-09 12:13:07 -0600935 c->rdx = a->rdx;
936 c->scale = a->scale;
937 c->len = a->len;
938
Gavin Howardbd60ec72019-05-10 08:01:22 -0600939 assert(c->scale >= scale);
940 rdx = c->rdx - BC_NUM_RDX(scale);
Gavin Howard61349662019-05-08 11:08:45 -0600941
Gavin Howardbd60ec72019-05-10 08:01:22 -0600942 bc_num_init(&cpb, len + 1);
943 bc_num_init(&sub, len + 1);
944 bc_num_init(&temp, len + 1);
945
946 for (i = end - 1; BC_NO_SIG && BC_NO_ERR(!s) && i < end && i >= rdx; --i) {
Gavin Howard61349662019-05-08 11:08:45 -0600947
Gavin Howard070a6ce2019-05-08 11:50:25 -0600948 ssize_t cmp;
949
Gavin Howardd5b27c82019-05-09 12:13:07 -0600950 n = a->num + i;
Gavin Howard070a6ce2019-05-08 11:50:25 -0600951 q = 0;
952
Gavin Howard1980e032019-05-08 17:21:17 -0600953 cmp = bc_num_divCmp(n, b, len);
Gavin Howarda36f0312019-05-09 10:23:07 -0600954 if (cmp == BC_NUM_CMP_SIGNAL) break;
Gavin Howard070a6ce2019-05-08 11:50:25 -0600955
Gavin Howardbd60ec72019-05-10 08:01:22 -0600956 if (!cmp) {
957
958 q = 1;
959
960 s = bc_num_mulArray(b, (BcBigDig) q, &cpb);
961 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
962 }
Gavin Howard070a6ce2019-05-08 11:50:25 -0600963 else if (cmp > 0) {
964
Gavin Howard92cf9052019-05-09 08:05:57 -0600965 BcBigDig n1, pow, dividend;
Gavin Howardbd60ec72019-05-10 08:01:22 -0600966 size_t cpblen;
Gavin Howard1980e032019-05-08 17:21:17 -0600967
968 n1 = (BcBigDig) n[len];
Gavin Howard92ba4e52019-05-10 20:49:13 -0600969 dividend = n1 * BC_BASE_POW + (BcBigDig) n[len - 1];
Gavin Howard92cf9052019-05-09 08:05:57 -0600970 q = (dividend / divisor + 1);
Gavin Howard92ba4e52019-05-10 20:49:13 -0600971 q = q > BC_BASE_POW ? BC_BASE_POW : q;
Gavin Howard1980e032019-05-08 17:21:17 -0600972 dividend = ((BcBigDig) bc_num_log10((size_t) q));
973
974 pow = bc_num_pow10[dividend - 1];
Gavin Howard1980e032019-05-08 17:21:17 -0600975
Gavin Howard92cf9052019-05-09 08:05:57 -0600976 s = bc_num_mulArray(b, (BcBigDig) q, &cpb);
Gavin Howard1980e032019-05-08 17:21:17 -0600977 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
978
Gavin Howard6945eaf2019-05-09 17:06:54 -0600979 s = bc_num_mulArray(b, pow, &sub);
Gavin Howard1980e032019-05-08 17:21:17 -0600980 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
981
Gavin Howardbd60ec72019-05-10 08:01:22 -0600982 cpblen = cpb.len;
983
Gavin Howard1980e032019-05-08 17:21:17 -0600984 while (BC_NO_SIG && BC_NO_ERR(!s) && pow > 0) {
985
Gavin Howardbd60ec72019-05-10 08:01:22 -0600986 s = bc_num_subArrays(cpb.num, sub.num, sub.len);
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 bc_num_clean(&cpb);
Gavin Howard1980e032019-05-08 17:21:17 -0600990
Gavin Howardbd60ec72019-05-10 08:01:22 -0600991 cmp = bc_num_divCmp(n, &cpb, len);
Gavin Howarda36f0312019-05-09 10:23:07 -0600992 if (cmp == BC_NUM_CMP_SIGNAL) goto err;
Gavin Howard1980e032019-05-08 17:21:17 -0600993
994 while (BC_NO_SIG && BC_NO_ERR(!s) && cmp < 0) {
995
996 q -= pow;
997
998 s = bc_num_subArrays(cpb.num, sub.num, sub.len);
999 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1000
Gavin Howardbd60ec72019-05-10 08:01:22 -06001001 bc_num_clean(&cpb);
Gavin Howard1980e032019-05-08 17:21:17 -06001002
Gavin Howardbd60ec72019-05-10 08:01:22 -06001003 cmp = bc_num_divCmp(n, &cpb, len);
Gavin Howarda36f0312019-05-09 10:23:07 -06001004 if (cmp == BC_NUM_CMP_SIGNAL) goto err;
Gavin Howard1980e032019-05-08 17:21:17 -06001005 }
1006
1007 pow /= BC_BASE;
Gavin Howard1980e032019-05-08 17:21:17 -06001008
Gavin Howardbd60ec72019-05-10 08:01:22 -06001009 if (pow) {
1010
Gavin Howard5a684032019-05-10 08:07:19 -06001011 BcBigDig rem;
1012
Gavin Howardbd60ec72019-05-10 08:01:22 -06001013 s = bc_num_addArrays(cpb.num, sub.num, sub.len);
1014 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1015
1016 cpb.len = cpblen;
1017 bc_num_clean(&cpb);
1018
1019 bc_num_copy(&temp, &sub);
1020 s = bc_num_divArray(&temp, 10, &sub, &rem);
1021 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
1022 assert(rem == 0);
1023 }
Gavin Howard070a6ce2019-05-08 11:50:25 -06001024 }
Gavin Howard1980e032019-05-08 17:21:17 -06001025
1026 q -= 1;
1027 }
1028
Gavin Howard92ba4e52019-05-10 20:49:13 -06001029 assert(q <= BC_BASE_POW);
Gavin Howard1980e032019-05-08 17:21:17 -06001030
1031 if (q) {
1032
Gavin Howard1980e032019-05-08 17:21:17 -06001033 s = bc_num_subArrays(n, cpb.num, len);
1034 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard070a6ce2019-05-08 11:50:25 -06001035 }
1036
Gavin Howard92cf9052019-05-09 08:05:57 -06001037 c->num[i] = (BcDig) q;
Gavin Howard61349662019-05-08 11:08:45 -06001038 }
1039
Gavin Howard1980e032019-05-08 17:21:17 -06001040err:
Gavin Howardd5b27c82019-05-09 12:13:07 -06001041 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardbd60ec72019-05-10 08:01:22 -06001042 bc_num_free(&temp);
Gavin Howard66a93552019-05-09 17:13:59 -06001043 bc_num_free(&cpb);
Gavin Howard66a93552019-05-09 17:13:59 -06001044 bc_num_free(&sub);
Gavin Howardd5b27c82019-05-09 12:13:07 -06001045 return s;
1046}
1047
Gavin Howardd5b27c82019-05-09 12:13:07 -06001048static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
1049
1050 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howardfcb74462019-05-09 14:42:35 -06001051 size_t len;
1052 BcNum cpa, cpb;
Gavin Howardd5b27c82019-05-09 12:13:07 -06001053 bool zero = true;
1054
1055 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1056 if (BC_NUM_ZERO(a)) {
1057 bc_num_setToZero(c, scale);
1058 return BC_STATUS_SUCCESS;
1059 }
Gavin Howardb2f01a42019-05-10 20:00:32 -06001060 if (BC_NUM_ONE(b)) {
Gavin Howardd5b27c82019-05-09 12:13:07 -06001061 bc_num_copy(c, a);
1062 bc_num_retireMul(c, scale, a->neg, b->neg);
1063 return BC_STATUS_SUCCESS;
1064 }
1065 if (!a->rdx && !b->rdx && b->len == 1 && !scale) {
1066 BcBigDig rem;
1067 s = bc_num_divArray(a, (BcBigDig) b->num[0], c, &rem);
1068 bc_num_retireMul(c, scale, a->neg, b->neg);
1069 return s;
1070 }
1071
1072 len = bc_num_mulReq(a, b, scale);
1073 bc_num_init(&cpa, len);
1074 bc_num_copy(&cpa, a);
Gavin Howardfcb74462019-05-09 14:42:35 -06001075 bc_num_createCopy(&cpb, b);
Gavin Howardd5b27c82019-05-09 12:13:07 -06001076
Gavin Howard03e35dd2019-05-11 06:56:30 -06001077 len = b->len;
1078
Gavin Howardd5b27c82019-05-09 12:13:07 -06001079 if (len > cpa.len) {
1080 bc_num_expand(&cpa, bc_vm_growSize(len, 2));
Gavin Howard92ba4e52019-05-10 20:49:13 -06001081 bc_num_extend(&cpa, (len - cpa.len) * BC_BASE_DIGS);
Gavin Howardd5b27c82019-05-09 12:13:07 -06001082 }
1083
Gavin Howard92ba4e52019-05-10 20:49:13 -06001084 cpa.scale = cpa.rdx * BC_BASE_DIGS;
Gavin Howardd5b27c82019-05-09 12:13:07 -06001085
1086 bc_num_extend(&cpa, b->scale);
1087 cpa.rdx -= BC_NUM_RDX(b->scale);
Gavin Howard92ba4e52019-05-10 20:49:13 -06001088 cpa.scale = cpa.rdx * BC_BASE_DIGS;
Gavin Howardbd60ec72019-05-10 08:01:22 -06001089 if (scale > cpa.scale) {
1090 bc_num_extend(&cpa, scale);
Gavin Howard92ba4e52019-05-10 20:49:13 -06001091 cpa.scale = cpa.rdx * BC_BASE_DIGS;
Gavin Howardbd60ec72019-05-10 08:01:22 -06001092 }
Gavin Howardd5b27c82019-05-09 12:13:07 -06001093
1094 if (b->rdx == b->len) {
Gavin Howardfcb74462019-05-09 14:42:35 -06001095 size_t i;
Gavin Howardd5b27c82019-05-09 12:13:07 -06001096 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
1097 assert(i != len || !zero);
1098 len -= i - 1;
1099 }
1100
1101 if (cpa.cap == cpa.len) bc_num_expand(&cpa, bc_vm_growSize(cpa.len, 1));
1102
1103 // We want an extra zero in front to make things simpler.
1104 cpa.num[cpa.len++] = 0;
1105
Gavin Howardfcb74462019-05-09 14:42:35 -06001106 if (cpa.rdx == cpa.len) cpa.len = bc_num_nonzeroLen(&cpa);
1107 if (cpb.rdx == cpb.len) cpb.len = bc_num_nonzeroLen(&cpb);
1108 cpb.scale = cpb.rdx = 0;
Gavin Howardd5b27c82019-05-09 12:13:07 -06001109
Gavin Howardbd60ec72019-05-10 08:01:22 -06001110 s = bc_num_d_long(&cpa, &cpb, c, scale);
Gavin Howardd5b27c82019-05-09 12:13:07 -06001111
1112 if (BC_NO_ERR(!s)) {
1113 bc_num_retireMul(c, scale, a->neg, b->neg);
1114 if (BC_SIG) s = BC_STATUS_SIGNAL;
1115 }
1116
Gavin Howardfcb74462019-05-09 14:42:35 -06001117 bc_num_free(&cpb);
Gavin Howard1980e032019-05-08 17:21:17 -06001118 bc_num_free(&cpa);
Gavin Howardd5b27c82019-05-09 12:13:07 -06001119
Gavin Howard63738202018-09-26 15:34:20 -06001120 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001121}
1122
Gavin Howard1cbfe242019-01-09 17:13:11 -07001123static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
1124 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -06001125{
Gavin Howard63738202018-09-26 15:34:20 -06001126 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -06001127 BcNum temp;
1128 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001129
Gavin Howardddce6d42019-01-03 14:07:06 -07001130 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1131 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -07001132 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001133 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001134 return BC_STATUS_SUCCESS;
1135 }
Gavin Howard8b254872018-03-14 01:13:35 -06001136
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001137 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -07001138 s = bc_num_d(a, b, c, scale);
1139 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001140 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001141
Gavin Howard996fc222019-04-29 13:22:38 -06001142 if (scale) scale = ts + 1;
Gavin Howard3115c012018-10-04 10:53:56 -06001143
Gavin Howard53eba8b2018-10-31 15:14:37 -06001144 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001145 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001146 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -07001147 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -06001148
Gavin Howard77445432019-04-26 15:59:33 -06001149 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
Gavin Howard18441082018-10-22 10:03:30 -06001150
1151 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001152 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard996fc222019-04-29 13:22:38 -06001153 d->neg = BC_NUM_NONZERO(d) ? neg : false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001154
1155err:
Gavin Howard18441082018-10-22 10:03:30 -06001156 bc_num_free(&temp);
1157 return s;
1158}
1159
Gavin Howard2d188a52019-02-25 14:19:08 -07001160static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1161{
Gavin Howard18441082018-10-22 10:03:30 -06001162 BcStatus s;
1163 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -07001164 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -06001165
Gavin Howard77445432019-04-26 15:59:33 -06001166 ts = bc_vm_growSize(scale, b->scale);
1167 ts = BC_MAX(ts, a->scale);
Gavin Howard798508b2019-02-21 16:45:32 -07001168
1169 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -06001170 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -06001171 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -06001172
Gavin Howard63738202018-09-26 15:34:20 -06001173 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001174}
1175
Gavin Howard1cbfe242019-01-09 17:13:11 -07001176static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001177
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001178 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001179 BcNum copy;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001180 BcBigDig pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001181 size_t i, powrdx, resrdx;
1182 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001183
Gavin Howardecafd4f2019-02-23 09:30:45 -07001184 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001185
Gavin Howardddce6d42019-01-03 14:07:06 -07001186 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -06001187 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -06001188 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001189 }
Gavin Howardddce6d42019-01-03 14:07:06 -07001190 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001191 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001192 return BC_STATUS_SUCCESS;
1193 }
Gavin Howardb2f01a42019-05-10 20:00:32 -06001194 if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001195 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -06001196 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -06001197 return s;
1198 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001199
Gavin Howard63738202018-09-26 15:34:20 -06001200 neg = b->neg;
1201 b->neg = false;
Gavin Howard2956a5e2019-05-08 08:04:06 -06001202 s = bc_num_bigdig(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -07001203 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001204 if (s) return s;
1205
Gavin Howardbaa4f582019-01-24 13:56:35 -07001206 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001207
Gavin Howardeec07282019-05-10 20:10:18 -06001208 if (!neg) {
1209 size_t max = BC_MAX(scale, a->scale), scalepow = a->scale * pow;
1210 scale = BC_MIN(scalepow, max);
1211 }
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 Howard92ba4e52019-05-10 20:49:13 -06001407 mod = n->scale % BC_BASE_DIGS;
1408 i = mod ? BC_BASE_DIGS - mod : 0;
1409 n->len = ((temp + i) / BC_BASE_DIGS);
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
Gavin Howard92ba4e52019-05-10 20:49:13 -06001429 size_t idx = exp / BC_BASE_DIGS;
Gavin Howardd43ec372019-04-25 21:13:54 -06001430
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
Gavin Howard92ba4e52019-05-10 20:49:13 -06001434 if ((exp + 1) % BC_BASE_DIGS == 0) pow = 1;
Gavin Howardd43ec372019-04-25 21:13:54 -06001435 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;
Gavin Howard92ba4e52019-05-10 20:49:13 -06001577 size_t buffer[BC_BASE_DIGS];
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);
Gavin Howard92ba4e52019-05-10 20:49:13 -06001588 temp = n->scale % BC_BASE_DIGS;
1589 temp = i || !temp ? 0 : BC_BASE_DIGS - temp;
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001590
Gavin Howard92ba4e52019-05-10 20:49:13 -06001591 memset(buffer, 0, BC_BASE_DIGS * sizeof(size_t));
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001592
Gavin Howard92ba4e52019-05-10 20:49:13 -06001593 for (j = 0; n9 && j < BC_BASE_DIGS; ++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 Howard92ba4e52019-05-10 20:49:13 -06001598 for (j = BC_BASE_DIGS - 1; j < BC_BASE_DIGS && j >= temp; --j) {
1599 bool print_rdx = (irdx & (j == BC_BASE_DIGS - 1));
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001600 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
Gavin Howard92ba4e52019-05-10 20:49:13 -06001623 for (i = BC_BASE_DIGS - 1; i < BC_BASE_DIGS; --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
Gavin Howard92ba4e52019-05-10 20:49:13 -06001628 places += (n->rdx - (idx + 1)) * BC_BASE_DIGS;
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) {
Gavin Howard92ba4e52019-05-10 20:49:13 -06001836 bc_num_init(n, (BC_NUM_BIGDIG_LOG10 - 1) / BC_BASE_DIGS + 1);
Gavin Howard2956a5e2019-05-08 08:04:06 -06001837 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 Howard92ba4e52019-05-10 20:49:13 -06001853 pow = BC_BASE_POW;
1854 i = BC_BASE_DIGS + 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 Howard92ba4e52019-05-10 20:49:13 -06001861 scale = n->scale % BC_BASE_DIGS;
1862 scale = scale ? scale : BC_BASE_DIGS;
Gavin Howardb41483b2019-04-27 08:30:10 -06001863
Gavin Howard92ba4e52019-05-10 20:49:13 -06001864 return (len - 1) * BC_BASE_DIGS + i - (BC_BASE_DIGS - 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 Howard92ba4e52019-05-10 20:49:13 -06001920 BcBigDig prev = r * BC_BASE_POW;
Gavin Howard5bede412019-02-23 09:31:28 -07001921
Gavin Howard92ba4e52019-05-10 20:49:13 -06001922 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_POW != 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 Howard92ba4e52019-05-10 20:49:13 -06001949 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_POW)
1950 ptr[i] = val % BC_BASE_POW;
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);
Gavin Howardd7883272019-05-10 20:04:23 -06001972 max = BC_NUM_RDX(scale);
1973 max = bc_vm_growSize(BC_MAX(max, rdx), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001974 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1975 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001976}
1977
1978size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1979 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001980 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001981}
1982
1983#if BC_ENABLE_EXTRA_MATH
Gavin Howard194380e2019-05-06 08:39:30 -06001984size_t bc_num_placesReq(BcNum *a, BcNum *b, size_t scale) {
1985
1986 BcStatus s;
Gavin Howardea1ae3c2019-05-10 18:44:52 -06001987 BcBigDig places = 0;
Gavin Howard194380e2019-05-06 08:39:30 -06001988 size_t rdx;
1989
1990 BC_UNUSED(s);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001991 BC_UNUSED(scale);
Gavin Howard194380e2019-05-06 08:39:30 -06001992
Gavin Howard194380e2019-05-06 08:39:30 -06001993 // This error will be taken care of later. Ignore.
Gavin Howard2956a5e2019-05-08 08:04:06 -06001994 s = bc_num_bigdig(b, &places);
Gavin Howard194380e2019-05-06 08:39:30 -06001995
1996 if (a->scale <= places) rdx = BC_NUM_RDX(places);
1997 else rdx = BC_NUM_RDX(a->scale - places);
1998
Gavin Howard06fee0c2019-05-09 14:43:20 -06001999 return BC_NUM_RDX(bc_num_intDigits(a)) + rdx;
Gavin Howard194380e2019-05-06 08:39:30 -06002000}
2001
2002size_t bc_num_shiftLeftReq(BcNum *a, BcNum *b, size_t scale) {
2003
2004 BcStatus s;
Gavin Howardea1ae3c2019-05-10 18:44:52 -06002005 BcBigDig places = 0, rdx;
Gavin Howard194380e2019-05-06 08:39:30 -06002006
2007 BC_UNUSED(s);
2008 BC_UNUSED(scale);
2009
Gavin Howard194380e2019-05-06 08:39:30 -06002010 // This error will be taken care of later. Ignore.
Gavin Howard2956a5e2019-05-08 08:04:06 -06002011 s = bc_num_bigdig(b, &places);
Gavin Howard194380e2019-05-06 08:39:30 -06002012
2013 if (a->scale <= places) rdx = BC_NUM_RDX(places) - a->rdx + 1;
2014 else rdx = 0;
2015
2016 return a->len + rdx;
2017}
2018
2019size_t bc_num_shiftRightReq(BcNum *a, BcNum *b, size_t scale) {
2020
2021 BcStatus s;
Gavin Howardea1ae3c2019-05-10 18:44:52 -06002022 BcBigDig places = 0, int_digs, rdx;
Gavin Howard194380e2019-05-06 08:39:30 -06002023
2024 BC_UNUSED(s);
2025 BC_UNUSED(scale);
2026
Gavin Howard194380e2019-05-06 08:39:30 -06002027 // This error will be taken care of later. Ignore.
Gavin Howard2956a5e2019-05-08 08:04:06 -06002028 s = bc_num_bigdig(b, &places);
Gavin Howard194380e2019-05-06 08:39:30 -06002029
Gavin Howard06fee0c2019-05-09 14:43:20 -06002030 int_digs = BC_NUM_RDX(bc_num_intDigits(a));
Gavin Howard194380e2019-05-06 08:39:30 -06002031 rdx = BC_NUM_RDX(places);
2032
2033 if (int_digs <= rdx) rdx -= int_digs;
2034 else rdx = 0;
2035
2036 return a->len + rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002037}
2038#endif // BC_ENABLE_EXTRA_MATH
2039
Gavin Howard12fe7812018-09-29 03:45:18 -06002040BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06002041 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07002042 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002043 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002044}
2045
Gavin Howard12fe7812018-09-29 03:45:18 -06002046BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06002047 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07002048 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002049 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002050}
2051
Gavin Howard12fe7812018-09-29 03:45:18 -06002052BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002053 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002054}
2055
Gavin Howard12fe7812018-09-29 03:45:18 -06002056BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07002057 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002058}
2059
Gavin Howard54b946a2018-10-23 12:06:57 -06002060BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07002061 size_t req = bc_num_mulReq(a, b, scale);
2062 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07002063}
2064
Gavin Howard12fe7812018-09-29 03:45:18 -06002065BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07002066 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07002067}
2068
Gavin Howard7d8f0382018-12-27 16:15:13 -07002069#if BC_ENABLE_EXTRA_MATH
2070BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard8291ecd2019-05-06 14:34:53 -06002071 size_t req = bc_num_placesReq(a, b, scale);
2072 return bc_num_binary(a, b, c, scale, bc_num_place, req);
Gavin Howard7d8f0382018-12-27 16:15:13 -07002073}
2074
2075BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard8291ecd2019-05-06 14:34:53 -06002076 size_t req = bc_num_shiftLeftReq(a, b, scale);
2077 return bc_num_binary(a, b, c, scale, bc_num_left, req);
Gavin Howard7d8f0382018-12-27 16:15:13 -07002078}
2079
2080BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard8291ecd2019-05-06 14:34:53 -06002081 size_t req = bc_num_shiftRightReq(a, b, scale);
2082 return bc_num_binary(a, b, c, scale, bc_num_right, req);
Gavin Howard7d8f0382018-12-27 16:15:13 -07002083}
2084#endif // BC_ENABLE_EXTRA_MATH
2085
Gavin Howard3c02ed32018-12-28 18:17:15 -07002086BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06002087
Gavin Howard1cbfe242019-01-09 17:13:11 -07002088 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002089 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Stefan Eßer4276af42019-04-29 14:04:57 -06002090 size_t pow, len, rdx, req, digs, digs1, digs2, resscale, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002091 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Stefan Eßer4276af42019-04-29 14:04:57 -06002092 BcDig half_digs[1];
Gavin Howard954276b2018-05-16 01:43:58 -06002093
Gavin Howard34edd0a2018-10-25 15:51:05 -06002094 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06002095
Gavin Howardecafd4f2019-02-23 09:30:45 -07002096 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07002097
Stefan Eßer4276af42019-04-29 14:04:57 -06002098 if (a->scale > scale) scale = a->scale;
Gavin Howard06fee0c2019-05-09 14:43:20 -06002099 len = bc_vm_growSize(bc_num_intDigits(a), 1);
Gavin Howardeec07282019-05-10 20:10:18 -06002100 rdx = BC_NUM_RDX(scale);
2101 req = bc_vm_growSize(BC_MAX(rdx, a->rdx), len >> 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07002102 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06002103
Gavin Howardddce6d42019-01-03 14:07:06 -07002104 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06002105 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002106 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06002107 }
Gavin Howardb2f01a42019-05-10 20:00:32 -06002108 if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06002109 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002110 bc_num_extend(b, scale);
2111 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06002112 }
Gavin Howard954276b2018-05-16 01:43:58 -06002113
Gavin Howardeec07282019-05-10 20:10:18 -06002114 rdx = BC_NUM_RDX(scale);
2115 rdx = BC_MAX(rdx, a->rdx);
Stefan Eßer4276af42019-04-29 14:04:57 -06002116 len = bc_vm_growSize(a->len, rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06002117
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002118 bc_num_init(&num1, len);
2119 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07002120 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06002121
Gavin Howard63738202018-09-26 15:34:20 -06002122 bc_num_one(&half);
Gavin Howard92ba4e52019-05-10 20:49:13 -06002123 half.num[0] = BC_BASE_POW / 2;
Stefan Eßer4276af42019-04-29 14:04:57 -06002124 half.len = 1;
Gavin Howard63738202018-09-26 15:34:20 -06002125 half.rdx = 1;
Stefan Eßer4276af42019-04-29 14:04:57 -06002126 half.scale = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06002127
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002128 bc_num_init(&f, len);
2129 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06002130
Gavin Howard63738202018-09-26 15:34:20 -06002131 x0 = &num1;
2132 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06002133
Gavin Howard63738202018-09-26 15:34:20 -06002134 bc_num_one(x0);
Gavin Howard06fee0c2019-05-09 14:43:20 -06002135 pow = bc_num_intDigits(a);
Gavin Howard954276b2018-05-16 01:43:58 -06002136
Gavin Howard53eba8b2018-10-31 15:14:37 -06002137 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06002138
Gavin Howardc39fd492018-10-04 10:07:03 -06002139 if (pow & 1) x0->num[0] = 2;
2140 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06002141
Gavin Howardc39fd492018-10-04 10:07:03 -06002142 pow -= 2 - (pow & 1);
Gavin Howard74704572019-05-07 13:48:48 -06002143 s = bc_num_shiftLeft(x0, pow / 2);
2144 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06002145 }
Gavin Howard954276b2018-05-16 01:43:58 -06002146
Gavin Howardeec07282019-05-10 20:10:18 -06002147 x0->scale = x0->rdx = digs = digs1 = digs2 = 0;
Gavin Howard92ba4e52019-05-10 20:49:13 -06002148 resscale = (scale + BC_BASE_DIGS) * 2;
Stefan Eßer4276af42019-04-29 14:04:57 -06002149
Gavin Howard06fee0c2019-05-09 14:43:20 -06002150 len = BC_NUM_RDX(bc_num_intDigits(x0) + resscale - 1);
Gavin Howard954276b2018-05-16 01:43:58 -06002151
Gavin Howard2d188a52019-02-25 14:19:08 -07002152 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06002153
Gavin Howard971a2672019-04-26 14:32:07 -06002154 assert(BC_NUM_NONZERO(x0));
Gavin Howard1769abc2019-02-21 09:36:14 -07002155
Stefan Eßer4276af42019-04-29 14:04:57 -06002156 s = bc_num_div(a, x0, &f, resscale);
Gavin Howard1769abc2019-02-21 09:36:14 -07002157 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002158 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Eßer4276af42019-04-29 14:04:57 -06002159 s = bc_num_add(x0, &f, &fprime, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07002160 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Stefan Eßer4276af42019-04-29 14:04:57 -06002161 s = bc_num_mul(&fprime, &half, x1, resscale);
Gavin Howardc78e7522019-02-22 13:24:25 -07002162 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06002163
Gavin Howard53eba8b2018-10-31 15:14:37 -06002164 cmp = bc_num_cmp(x1, x0);
Gavin Howarda36f0312019-05-09 10:23:07 -06002165 if (cmp == BC_NUM_CMP_SIGNAL) {
Gavin Howardf2ee6342019-04-09 17:05:20 -06002166 s = BC_STATUS_SIGNAL;
2167 break;
2168 }
2169
Gavin Howard53eba8b2018-10-31 15:14:37 -06002170 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06002171
Gavin Howardb5ec7f32018-10-29 12:32:39 -06002172 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06002173 else times = 0;
2174
Gavin Howardeec07282019-05-10 20:10:18 -06002175 resscale += (times > 2);
Gavin Howard9f2395b2018-10-06 05:28:58 -06002176
2177 cmp2 = cmp1;
2178 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06002179 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06002180
Gavin Howard63738202018-09-26 15:34:20 -06002181 temp = x0;
2182 x0 = x1;
2183 x1 = temp;
2184 }
Gavin Howard954276b2018-05-16 01:43:58 -06002185
Gavin Howard2d188a52019-02-25 14:19:08 -07002186 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07002187 s = BC_STATUS_SIGNAL;
2188 goto err;
2189 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06002190
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002191 bc_num_copy(b, x0);
Stefan Eßer4276af42019-04-29 14:04:57 -06002192 if (b->scale > scale) bc_num_truncate(b, b->scale - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06002193
2194err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06002195 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06002196 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06002197 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06002198 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06002199 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07002200 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002201 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06002202 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07002203}
Gavin Howardba009802018-09-29 04:41:51 -06002204
Gavin Howard2cb39612018-10-22 08:46:48 -06002205BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
2206
2207 BcStatus s;
2208 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06002209 bool init = false;
Stefan Eßera7fbbf62019-04-29 14:14:30 -06002210 size_t ts, len;
2211
2212 ts = BC_MAX(scale + b->scale, a->scale);
2213 len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002214
Gavin Howardf2ef2f32019-01-16 11:25:16 -07002215 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06002216
Gavin Howard890d0c02018-10-30 16:34:50 -06002217 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06002218 memcpy(&num2, c, sizeof(BcNum));
2219 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002220 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06002221 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06002222 }
Gavin Howardf679ad82018-10-29 12:26:30 -06002223 else {
2224 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002225 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06002226 }
Gavin Howard2cb39612018-10-22 08:46:48 -06002227
Gavin Howardf5961782019-05-09 07:50:46 -06002228 if (BC_NUM_NONZERO(a) && !a->rdx && !b->rdx && b->len == 1 && !scale) {
Gavin Howard2956a5e2019-05-08 08:04:06 -06002229 BcBigDig rem;
Gavin Howard9d771ba2019-05-08 09:09:25 -06002230 s = bc_num_divArray(ptr_a, (BcBigDig) b->num[0], c, &rem);
Gavin Howard92ba4e52019-05-10 20:49:13 -06002231 assert(rem < BC_BASE_POW);
Gavin Howardb3f1ee22019-05-07 21:49:15 -06002232 d->num[0] = (BcDig) rem;
Gavin Howardf5961782019-05-09 07:50:46 -06002233 d->len = (rem != 0);
Gavin Howardb3f1ee22019-05-07 21:49:15 -06002234 }
Gavin Howardf5961782019-05-09 07:50:46 -06002235 else s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06002236
Gavin Howardddce6d42019-01-03 14:07:06 -07002237 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002238 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07002239 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07002240 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06002241
2242 if (init) bc_num_free(&num2);
2243
2244 return s;
2245}
2246
Gavin Howard40a085f2018-12-03 12:08:59 -07002247#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06002248BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06002249
2250 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06002251 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07002252 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06002253
Gavin Howard34edd0a2018-10-25 15:51:05 -06002254 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06002255
Gavin Howardecafd4f2019-02-23 09:30:45 -07002256 if (BC_ERR(BC_NUM_ZERO(c)))
2257 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
2258 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
2259 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07002260 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06002261
Gavin Howardad0ecfe2018-10-30 01:16:01 -06002262 bc_num_expand(d, c->len);
2263 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07002264 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howard303572b2019-05-09 07:50:56 -06002265 bc_num_init(&temp, b->len + 1);
Gavin Howardba009802018-09-29 04:41:51 -06002266
2267 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06002268 two.num[0] = 2;
2269 bc_num_one(d);
2270
Gavin Howard1769abc2019-02-21 09:36:14 -07002271 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002272 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002273 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002274 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07002275 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06002276
Gavin Howard2d188a52019-02-25 14:19:08 -07002277 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06002278
Gavin Howard1769abc2019-02-21 09:36:14 -07002279 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002280 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002281 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002282
Gavin Howardb2f01a42019-05-10 20:00:32 -06002283 if (BC_NUM_ONE(&temp) && !temp.neg) {
2284
Gavin Howard53eba8b2018-10-31 15:14:37 -06002285 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002286 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002287
2288 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002289 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002290 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002291 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002292 }
2293
Gavin Howard53eba8b2018-10-31 15:14:37 -06002294 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07002295 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07002296
2297 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06002298 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07002299 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07002300 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06002301 }
2302
Gavin Howard2d188a52019-02-25 14:19:08 -07002303 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07002304
Gavin Howardba009802018-09-29 04:41:51 -06002305err:
Gavin Howardba009802018-09-29 04:41:51 -06002306 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07002307rem_err:
2308 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06002309 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06002310 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06002311 return s;
2312}
Gavin Howarde6e84762018-10-03 11:46:34 -06002313#endif // DC_ENABLED
Gavin Howardefc1e202019-05-06 08:01:07 -06002314
2315#if BC_DEBUG_CODE
2316void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline) {
2317 printf("%s: ", name);
2318 bc_num_printDecimal(n);
2319 printf("\n");
2320 if (emptyline) printf("\n");
Gavin Howarde34f7d82019-05-07 10:32:39 -06002321 vm->nchars = 0;
Gavin Howardefc1e202019-05-06 08:01:07 -06002322}
2323
Gavin Howard2a84f962019-05-08 17:20:09 -06002324void bc_num_printDigs(const BcDig *n, size_t len, bool emptyline) {
Gavin Howardefc1e202019-05-06 08:01:07 -06002325
2326 size_t i;
2327
Gavin Howard92ba4e52019-05-10 20:49:13 -06002328 for (i = len - 1; i < len; --i) printf(" %0*d", BC_BASE_DIGS, n[i]);
Gavin Howardefc1e202019-05-06 08:01:07 -06002329
2330 printf("\n");
2331 if (emptyline) printf("\n");
Gavin Howarde34f7d82019-05-07 10:32:39 -06002332 vm->nchars = 0;
Gavin Howardefc1e202019-05-06 08:01:07 -06002333}
2334
Gavin Howard2a84f962019-05-08 17:20:09 -06002335void bc_num_printWithDigs(const BcNum *n, const char *name, bool emptyline) {
2336 printf("%s len: %zu, rdx: %zu, scale: %zu\n",
2337 name, n->len, n->rdx, n->scale);
2338 bc_num_printDigs(n->num, n->len, emptyline);
2339}
2340
Gavin Howardefc1e202019-05-06 08:01:07 -06002341void bc_num_dump(const char *varname, const BcNum *n) {
2342
2343 unsigned long i, scale = n->scale;
2344
2345 fprintf(stderr, "\n%s = %s", varname, n->len ? (n->neg ? "-" : "+") : "0 ");
2346
2347 for (i = n->len - 1; i < n->len; --i) {
2348
2349 if (i + 1 == n->rdx) fprintf(stderr, ". ");
2350
Gavin Howard92ba4e52019-05-10 20:49:13 -06002351 if (scale / BC_BASE_DIGS != n->rdx - i - 1)
2352 fprintf(stderr, "%0*d ", BC_BASE_DIGS, n->num[i]);
Gavin Howardefc1e202019-05-06 08:01:07 -06002353 else {
2354
Gavin Howard92ba4e52019-05-10 20:49:13 -06002355 int mod = scale % BC_BASE_DIGS;
2356 int d = BC_BASE_DIGS - mod;
Gavin Howardefc1e202019-05-06 08:01:07 -06002357 BcDig div;
2358
2359 if (mod != 0) {
Gavin Howard2a84f962019-05-08 17:20:09 -06002360 div = n->num[i] / ((BcDig) bc_num_pow10[(unsigned long) d]);
Gavin Howardefc1e202019-05-06 08:01:07 -06002361 fprintf(stderr, "%0*d", (int) mod, div);
2362 }
2363
Gavin Howard2a84f962019-05-08 17:20:09 -06002364 div = n->num[i] % ((BcDig) bc_num_pow10[(unsigned long) d]);
Gavin Howardefc1e202019-05-06 08:01:07 -06002365 fprintf(stderr, " ' %0*d ", d, div);
2366 }
2367 }
2368
2369 fprintf(stderr, "(%zu | %zu.%zu / %zu) %p\n",
2370 n->scale, n->len, n->rdx, n->cap, (void*) n->num);
Gavin Howarde34f7d82019-05-07 10:32:39 -06002371 vm->nchars = 0;
Gavin Howardefc1e202019-05-06 08:01:07 -06002372}
2373#endif // BC_DEBUG_CODE