blob: 9f80acec2e9813486e632f2eeb474fef802f036e [file] [log] [blame]
Gavin Howard3eb626f2018-02-14 13:54:35 -07001/*
Gavin Howardb5904bf2018-02-20 13:28:18 -07002 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -07003 *
Gavin Howard7345cb92019-04-08 14:13:43 -06004 * Copyright (c) 2018-2019 Gavin D. Howard and contributors.
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
Gavin Howard7345cb92019-04-08 14:13:43 -06006 * All rights reserved.
Gavin Howard3eb626f2018-02-14 13:54:35 -07007 *
Gavin Howard7345cb92019-04-08 14:13:43 -06008 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
Gavin Howard3eb626f2018-02-14 13:54:35 -070029 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070030 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070031 *
32 * Code for the number type.
33 *
34 */
35
36#include <assert.h>
Gavin Howardc7d655b2018-12-28 19:45:13 -070037#include <ctype.h>
Gavin Howard411f7322018-09-26 17:21:19 -060038#include <stdbool.h>
39#include <stdlib.h>
40#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070041
Gavin Howard305249a2018-10-15 20:24:47 -060042#include <limits.h>
43
Gavin Howard29493062018-03-20 19:57:37 -060044#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070045#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060046#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070047
Gavin Howard55c8f732019-04-24 14:05:27 -060048#if BC_DEBUG_CODE
49static void bc_num_printDecimal(const BcNum *restrict a);
50
51static void bc_num_printDebug(const BcNum *n, const char* name, bool emptynl) {
52 printf("%s: ", name);
53 bc_num_printDecimal(n);
54 printf("\n");
55 if (emptynl) printf("\n");
56 vm->nchars = 0;
57}
Stefan Eßer4b111c22019-04-24 17:23:08 -060058
Gavin Howardaf107d92019-04-25 19:35:41 -060059static void bc_num_dump(const BcNum *n, const char *c) {
Stefan Eßer4b111c22019-04-24 17:23:08 -060060
61 int i;
62
63 fprintf(stderr, "\n%s=", c);
64
65 for (i = n->len -1; i >= 0; i--) {
66 if (i+1 == n->rdx)
67 fprintf(stderr, ".");
68 fprintf(stderr, "%09d ", n->num[i]);
69 }
70
71 fprintf(stderr, "(%p|%zu/%zu)\n", n->num, n->len, n->cap);
72}
Gavin Howard55c8f732019-04-24 14:05:27 -060073#endif // BC_DEBUG_CODE
74
Gavin Howard49ea7b62019-04-23 14:46:26 -060075static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale);
76
Gavin Howard56ebcf72019-02-21 16:59:48 -070077static ssize_t bc_num_neg(size_t n, bool neg) {
78 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg;
79}
80
Gavin Howardd2cf06a2019-02-21 17:03:24 -070081ssize_t bc_num_cmpZero(const BcNum *n) {
Gavin Howard56ebcf72019-02-21 16:59:48 -070082 return bc_num_neg((n)->len != 0, (n)->neg);
Gavin Howard9d91b2c2019-02-21 16:55:32 -070083}
84
Gavin Howard7b7d2f32019-02-21 16:46:47 -070085static size_t bc_num_int(const BcNum *n) {
86 return n->len ? n->len - n->rdx : 0;
87}
88
Gavin Howarda83a5e52019-02-21 16:51:06 -070089static bool bc_num_isOne(const BcNum *n) {
90 return n->len == 1 && n->rdx == 0 && n->num[0] == 1;
91}
92
Gavin Howard798508b2019-02-21 16:45:32 -070093static void bc_num_expand(BcNum *restrict n, size_t req) {
94 assert(n);
95 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
96 if (req > n->cap) {
Stefan Esser5fd43a32019-04-25 07:05:24 +020097 n->num = bc_vm_realloc_digs(n->num, req);
Gavin Howard798508b2019-02-21 16:45:32 -070098 n->cap = req;
99 }
100}
101
Gavin Howard1cbfe242019-01-09 17:13:11 -0700102static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600103 assert(n);
Gavin Howardfcf29012019-04-25 20:12:19 -0600104 n->scale = scale;
105 n->len = n->rdx = 0;
Gavin Howard97102b12018-11-01 23:37:31 -0600106 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600107}
108
Gavin Howard1cbfe242019-01-09 17:13:11 -0700109static void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600110 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -0600111}
112
Gavin Howard3c02ed32018-12-28 18:17:15 -0700113void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600114 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -0600115 n->len = 1;
116 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -0600117}
118
Gavin Howard3c02ed32018-12-28 18:17:15 -0700119void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -0600120 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600121 bc_num_setToZero(n, 0);
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600122#if BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600123 n->len = 2;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600124 n->num[0] = 0;
125 n->num[1] = 1;
126#else // BC_BASE_DIG == 10
Gavin Howardb53a9942019-04-25 11:33:54 -0600127 n->len = 1;
Stefan Eßer4b111c22019-04-24 17:23:08 -0600128 n->num[0] = BC_BASE;
Gavin Howardd7e4d6e2019-04-24 17:33:48 -0600129#endif // BC_BASE_DIG == 10
Gavin Howard0be26ed2018-08-31 20:21:56 -0600130}
131
Stefan Esser394a5162019-04-25 06:33:10 +0200132static void bc_num_set(BcDig *restrict dst, const size_t val,
133 const size_t n)
134{
Gavin Howardf49fdaa2019-04-25 19:43:49 -0600135 memset(dst, val, n * sizeof(BcDig));
Stefan Esser394a5162019-04-25 06:33:10 +0200136}
137
138static void bc_num_cpy(BcDig *restrict dst, const BcDig *restrict src,
139 const size_t n)
140{
Gavin Howardf49fdaa2019-04-25 19:43:49 -0600141 memcpy(dst, src, n * sizeof(BcDig));
Stefan Esser394a5162019-04-25 06:33:10 +0200142}
143
144static void bc_num_move(BcDig *restrict dst, const BcDig *restrict src,
145 const size_t n)
146{
Gavin Howardf49fdaa2019-04-25 19:43:49 -0600147 memmove(dst, src, n * sizeof(BcDig));
Stefan Esser394a5162019-04-25 06:33:10 +0200148}
149
Gavin Howard1cbfe242019-01-09 17:13:11 -0700150static size_t bc_num_log10(size_t i) {
Gavin Howard1ab22d22019-01-03 13:32:17 -0700151 size_t len;
Gavin Howard530e3072019-04-23 16:23:36 -0600152 for (len = 1; i; i /= BC_BASE, ++len);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700153 return len;
154}
155
Gavin Howardfcf29012019-04-25 20:12:19 -0600156static unsigned long bc_num_addDigit(BcDig *restrict num, unsigned long d,
157 unsigned long c)
Gavin Howard14c354a2019-04-24 14:28:24 -0600158{
159 d += c;
Gavin Howard0700efc2019-04-24 17:31:23 -0600160 *num = (BcDig) (d % BC_BASE_DIG);
161 assert(*num >= 0 && *num < BC_BASE_DIG);
162 return d / BC_BASE_DIG;
Gavin Howard14c354a2019-04-24 14:28:24 -0600163}
164
165static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b,
166 size_t len)
167{
Gavin Howardfcf29012019-04-25 20:12:19 -0600168 // TODO: Check this function.
Gavin Howard14c354a2019-04-24 14:28:24 -0600169 size_t i;
170 unsigned int carry = 0;
171
172 for (i = 0; BC_NO_SIG && i < len; ++i) {
173 unsigned int in = ((unsigned int) a[i]) + ((unsigned int) b[i]);
174 carry = bc_num_addDigit(a + i, in, carry);
175 }
176
177 for (; BC_NO_SIG && carry; ++i)
178 carry = bc_num_addDigit(a + i, (unsigned int) a[i], carry);
179
180 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
181}
182
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700183static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b,
184 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700185{
Gavin Howardfcf29012019-04-25 20:12:19 -0600186 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -0600187 size_t i, j;
Gavin Howard14c354a2019-04-24 14:28:24 -0600188
Gavin Howard2d188a52019-02-25 14:19:08 -0700189 for (i = 0; BC_NO_SIG && i < len; ++i) {
Gavin Howard14c354a2019-04-24 14:28:24 -0600190
Gavin Howard2d188a52019-02-25 14:19:08 -0700191 for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600192 assert(a[i + j] >= -BC_BASE_DIG);
193 a[i + j++] += BC_BASE_DIG;
Gavin Howard54b946a2018-10-23 12:06:57 -0600194 a[i + j] -= 1;
Gavin Howard0700efc2019-04-24 17:31:23 -0600195 assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_DIG);
Gavin Howard63738202018-09-26 15:34:20 -0600196 }
197 }
Gavin Howard14c354a2019-04-24 14:28:24 -0600198
Gavin Howard2d188a52019-02-25 14:19:08 -0700199 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -0600200}
201
Gavin Howard2d188a52019-02-25 14:19:08 -0700202static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
203 size_t len)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700204{
Gavin Howard63738202018-09-26 15:34:20 -0600205 size_t i;
Gavin Howardfcf29012019-04-25 20:12:19 -0600206 long c = 0;
Gavin Howard2d188a52019-02-25 14:19:08 -0700207 for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600208 return BC_SIG ? BC_NUM_SSIZE_MIN : bc_num_neg(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -0600209}
210
Gavin Howard3c02ed32018-12-28 18:17:15 -0700211ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -0700212
Gavin Howard63738202018-09-26 15:34:20 -0600213 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -0600214 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600215 bool a_max, neg = false;
216 ssize_t cmp;
217
218 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700219
Gavin Howard8a921bd2018-10-11 14:15:32 -0600220 if (a == b) return 0;
Gavin Howard56ebcf72019-02-21 16:59:48 -0700221 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
Gavin Howardd2cf06a2019-02-21 17:03:24 -0700222 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600223 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -0600224 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -0600225 else return -1;
226 }
227 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700228
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700229 a_int = bc_num_int(a);
230 b_int = bc_num_int(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600231 a_int -= b_int;
232 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700233
Gavin Howard1ab22d22019-01-03 13:32:17 -0700234 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700235
Gavin Howard890d0c02018-10-30 16:34:50 -0600236 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600237 min = b->rdx;
238 diff = a->rdx - b->rdx;
239 max_num = a->num + diff;
240 min_num = b->num;
241 }
242 else {
243 min = a->rdx;
244 diff = b->rdx - a->rdx;
245 max_num = b->num + diff;
246 min_num = a->num;
247 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700248
Gavin Howard890d0c02018-10-30 16:34:50 -0600249 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600250 if (cmp == BC_NUM_SSIZE_MIN) return cmp;
Gavin Howard5eba0e82019-02-21 18:08:33 -0700251 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700252
Gavin Howard2d188a52019-02-25 14:19:08 -0700253 for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) {
Gavin Howard5eba0e82019-02-21 18:08:33 -0700254 if (max_num[i]) return bc_num_neg(1, !a_max == !neg);
Gavin Howard63738202018-09-26 15:34:20 -0600255 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700256
Gavin Howardf2ee6342019-04-09 17:05:20 -0600257 return BC_SIG ? BC_NUM_SSIZE_MIN : 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700258}
259
Gavin Howard2a366922019-01-16 10:50:20 -0700260static void bc_num_clean(BcNum *restrict n) {
261 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len;
262 if (BC_NUM_ZERO(n)) n->neg = false;
263 else if (n->len < n->rdx) n->len = n->rdx;
264}
265
Gavin Howard3c02ed32018-12-28 18:17:15 -0700266void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700267
Gavin Howardfcf29012019-04-25 20:12:19 -0600268 // TODO: Check this function.
269
Gavin Howardddce6d42019-01-03 14:07:06 -0700270 assert(places <= n->rdx && (BC_NUM_ZERO(n) || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700271
Gavin Howard1ab22d22019-01-03 13:32:17 -0700272 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700273
Gavin Howard63738202018-09-26 15:34:20 -0600274 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700275
Gavin Howardddce6d42019-01-03 14:07:06 -0700276 if (BC_NUM_NONZERO(n)) {
Gavin Howard955da852018-10-23 11:08:20 -0600277 n->len -= places;
Stefan Esser394a5162019-04-25 06:33:10 +0200278 bc_num_move(n->num, n->num + places, n->len);
Gavin Howard2a366922019-01-16 10:50:20 -0700279 bc_num_clean(n);
Gavin Howard955da852018-10-23 11:08:20 -0600280 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700281}
282
Gavin Howard1cbfe242019-01-09 17:13:11 -0700283static void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700284
Gavin Howardfcf29012019-04-25 20:12:19 -0600285 // TODO: Check this function.
286
Gavin Howarde6f326e2019-04-26 10:13:45 -0600287 size_t len, places_rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700288
Gavin Howard1ab22d22019-01-03 13:32:17 -0700289 if (!places) return;
Gavin Howarde6f326e2019-04-26 10:13:45 -0600290
291 places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
292 len = bc_vm_growSize(n->len, places_rdx);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700293 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700294
Gavin Howarde6f326e2019-04-26 10:13:45 -0600295 bc_num_move(n->num + places_rdx, n->num, n->len);
296 bc_num_set(n->num, 0, places_rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700297
Gavin Howarde6f326e2019-04-26 10:13:45 -0600298 if (n->len) n->len += places_rdx;
Gavin Howardc4bf4c42019-01-09 10:30:15 -0700299
Gavin Howarde6f326e2019-04-26 10:13:45 -0600300 n->rdx += places_rdx;
301 n->scale += places;
302
303 assert(n->rdx == BC_NUM_RDX(n->scale));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700304}
305
Gavin Howard2d188a52019-02-25 14:19:08 -0700306static void bc_num_retireMul(BcNum *restrict n, size_t scale,
307 bool neg1, bool neg2)
308{
Gavin Howardfcf29012019-04-25 20:12:19 -0600309 // TODO: Check this function.
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600310 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600311 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600312
Gavin Howard305249a2018-10-15 20:24:47 -0600313 bc_num_clean(n);
Gavin Howardddce6d42019-01-03 14:07:06 -0700314 if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
Gavin Howard305249a2018-10-15 20:24:47 -0600315}
316
Gavin Howard2d188a52019-02-25 14:19:08 -0700317static void bc_num_split(const BcNum *restrict n, size_t idx,
318 BcNum *restrict a, BcNum *restrict b)
319{
Gavin Howardfcf29012019-04-25 20:12:19 -0600320 // TODO: Check this function.
Gavin Howard305249a2018-10-15 20:24:47 -0600321 if (idx < n->len) {
322
Gavin Howard305249a2018-10-15 20:24:47 -0600323 b->len = n->len - idx;
324 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600325 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600326
Stefan Esser394a5162019-04-25 06:33:10 +0200327 bc_num_cpy(b->num, n->num + idx, b->len);
328 bc_num_cpy(a->num, n->num, idx);
Gavin Howard50c80022019-01-03 15:14:33 -0700329
330 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600331 }
Gavin Howard50c80022019-01-03 15:14:33 -0700332 else bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600333
334 bc_num_clean(a);
Gavin Howard305249a2018-10-15 20:24:47 -0600335}
336
Gavin Howard14913fc2019-04-23 14:36:03 -0600337static size_t bc_num_shiftZero(BcNum *restrict n) {
338
Gavin Howardfcf29012019-04-25 20:12:19 -0600339 // TODO: Check this function.
340
Gavin Howard14913fc2019-04-23 14:36:03 -0600341 size_t i;
342
343 assert(!n->rdx || BC_NUM_ZERO(n));
344
345 for (i = 0; i < n->len && !n->num[i]; ++i);
346
347 n->len -= i;
348 n->num += i;
349
350 return i;
351}
352
353static void bc_num_unshiftZero(BcNum *restrict n, size_t places) {
Gavin Howardfcf29012019-04-25 20:12:19 -0600354 // TODO: Check this function.
Gavin Howard14913fc2019-04-23 14:36:03 -0600355 n->len += places;
356 n->num -= places;
357}
358
Gavin Howarddbbada82019-02-22 08:51:19 -0700359static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600360
Gavin Howardfcf29012019-04-25 20:12:19 -0600361 // TODO: Check this function.
362
Gavin Howardd8b004b2019-02-16 22:35:08 -0700363 if (!places || BC_NUM_ZERO(n)) return;
Gavin Howard305249a2018-10-15 20:24:47 -0600364
365 if (n->rdx >= places) n->rdx -= places;
366 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600367 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600368 n->rdx = 0;
369 }
370
371 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600372}
373
Gavin Howarddbbada82019-02-22 08:51:19 -0700374static void bc_num_shiftRight(BcNum *restrict n, size_t places) {
Gavin Howard7ad5a662019-02-19 14:40:46 -0700375
Gavin Howardfcf29012019-04-25 20:12:19 -0600376 // TODO: Check this function.
377
Gavin Howard14913fc2019-04-23 14:36:03 -0600378 size_t len;
379
380 if (!places) return;
381 if (BC_NUM_ZERO(n)) {
382 n->rdx += places;
383 bc_num_expand(n, n->rdx);
384 return;
385 }
386
387 len = bc_vm_growSize(n->rdx, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700388
389 if (len > n->len) {
390
391 if (len > n->cap) bc_num_expand(n, len);
392
Stefan Esser394a5162019-04-25 06:33:10 +0200393 bc_num_set(n->num + n->len, 0, len - n->len);
Gavin Howard7ad5a662019-02-19 14:40:46 -0700394 n->len = len;
395 }
396
397 n->rdx += places;
398
399 assert(n->rdx <= n->len && n->len <= n->cap);
400}
Gavin Howard7ad5a662019-02-19 14:40:46 -0700401
Gavin Howard1cbfe242019-01-09 17:13:11 -0700402static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600403
Gavin Howard63738202018-09-26 15:34:20 -0600404 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600405 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600406
Gavin Howard1769abc2019-02-21 09:36:14 -0700407 assert(!BC_NUM_ZERO(a));
408
Gavin Howardd32d7df2018-10-15 08:42:25 -0600409 one.cap = 2;
410 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600411 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600412
Gavin Howardd32d7df2018-10-15 08:42:25 -0600413 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600414}
415
Gavin Howard7bda4782018-12-28 09:53:22 -0700416#if BC_ENABLE_EXTRA_MATH
Gavin Howard1cbfe242019-01-09 17:13:11 -0700417static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
418 unsigned long *v)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700419{
Gavin Howardecafd4f2019-02-23 09:30:45 -0700420 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardac4d9122018-12-27 23:59:12 -0700421 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700422 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700423}
Gavin Howard7bda4782018-12-28 09:53:22 -0700424#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700425
Gavin Howard1cbfe242019-01-09 17:13:11 -0700426static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700427
Gavin Howardfcf29012019-04-25 20:12:19 -0600428 // TODO: Check this function.
429
Gavin Howard8a921bd2018-10-11 14:15:32 -0600430 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600431 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700432 unsigned int carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700433
Gavin Howard63738202018-09-26 15:34:20 -0600434 // Because this function doesn't need to use scale (per the bc spec),
435 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600436
Gavin Howardddce6d42019-01-03 14:07:06 -0700437 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600438 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700439 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600440 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600441 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700442 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600443 bc_num_copy(c, a);
444 return BC_STATUS_SUCCESS;
445 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600446
Gavin Howard63738202018-09-26 15:34:20 -0600447 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600448 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howardfba50a22019-04-26 10:07:21 -0600449 c->scale = BC_MAX(a->scale, b->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600450 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700451
Gavin Howard63738202018-09-26 15:34:20 -0600452 if (a->rdx > b->rdx) {
453 diff = a->rdx - b->rdx;
454 ptr = a->num;
455 ptr_a = a->num + diff;
456 ptr_b = b->num;
457 }
458 else {
459 diff = b->rdx - a->rdx;
460 ptr = b->num;
461 ptr_a = a->num;
462 ptr_b = b->num + diff;
463 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700464
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700465 for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700466
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700467 c->len = diff;
Gavin Howard63738202018-09-26 15:34:20 -0600468 ptr_c += diff;
Gavin Howard7b7d2f32019-02-21 16:46:47 -0700469 a_int = bc_num_int(a);
470 b_int = bc_num_int(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700471
Gavin Howard890d0c02018-10-30 16:34:50 -0600472 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600473 min_int = b_int;
474 max = a_int;
475 ptr = ptr_a;
476 }
477 else {
478 min_int = a_int;
479 max = b_int;
480 ptr = ptr_b;
481 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700482
Gavin Howard2d188a52019-02-25 14:19:08 -0700483 for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) {
Gavin Howard977767f2019-04-24 10:46:23 -0600484 unsigned int in = ((unsigned int) ptr_a[i]) + ((unsigned int) ptr_b[i]);
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700485 carry = bc_num_addDigit(ptr_c + i, in, carry);
Gavin Howard63738202018-09-26 15:34:20 -0600486 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700487
Gavin Howard2d188a52019-02-25 14:19:08 -0700488 for (; BC_NO_SIG && i < max + min_rdx; ++i)
Gavin Howarddd7a0fd2019-01-22 09:56:46 -0700489 carry = bc_num_addDigit(ptr_c + i, (unsigned int) ptr[i], carry);
Gavin Howardcafcd3e2019-01-22 09:54:24 -0700490
491 c->len += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700492
Gavin Howard1ab22d22019-01-03 13:32:17 -0700493 if (carry) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600494
Gavin Howard2d188a52019-02-25 14:19:08 -0700495 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700496}
497
Gavin Howard1cbfe242019-01-09 17:13:11 -0700498static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700499
Gavin Howardfcf29012019-04-25 20:12:19 -0600500 // TODO: Check this function.
501
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700502 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600503 ssize_t cmp;
504 BcNum *minuend, *subtrahend;
505 size_t start;
506 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700507
Gavin Howard63738202018-09-26 15:34:20 -0600508 // Because this function doesn't need to use scale (per the bc spec),
509 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700510
Gavin Howardddce6d42019-01-03 14:07:06 -0700511 if (BC_NUM_ZERO(a)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600512 bc_num_copy(c, b);
Gavin Howardddce6d42019-01-03 14:07:06 -0700513 if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600514 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600515 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700516 if (BC_NUM_ZERO(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600517 bc_num_copy(c, a);
518 return BC_STATUS_SUCCESS;
519 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700520
Gavin Howard63738202018-09-26 15:34:20 -0600521 aneg = a->neg;
522 bneg = b->neg;
523 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700524
Gavin Howard63738202018-09-26 15:34:20 -0600525 cmp = bc_num_cmp(a, b);
Gavin Howardf2ee6342019-04-09 17:05:20 -0600526 if (cmp == BC_NUM_SSIZE_MIN) return BC_STATUS_SIGNAL;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700527
Gavin Howard63738202018-09-26 15:34:20 -0600528 a->neg = aneg;
529 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700530
Gavin Howard1ab22d22019-01-03 13:32:17 -0700531 if (!cmp) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600532 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600533 return BC_STATUS_SUCCESS;
534 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700535
536 if (cmp > 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600537 neg = a->neg;
538 minuend = a;
539 subtrahend = b;
540 }
541 else {
542 neg = b->neg;
543 if (sub) neg = !neg;
544 minuend = b;
545 subtrahend = a;
546 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700547
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600548 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600549 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700550
Gavin Howarde6f326e2019-04-26 10:13:45 -0600551 if (c->scale < subtrahend->scale) {
552 bc_num_extend(c, subtrahend->scale - c->scale);
Gavin Howard63738202018-09-26 15:34:20 -0600553 start = 0;
554 }
555 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700556
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700557 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howardfa4983a2019-02-16 23:43:03 -0700558
Gavin Howard63738202018-09-26 15:34:20 -0600559 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700560
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700561 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700562}
563
Gavin Howarddc4e3712019-04-23 14:56:29 -0600564static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
565{
Gavin Howardfcf29012019-04-25 20:12:19 -0600566 // TODO: Check this function.
Stefan Eßer4b111c22019-04-24 17:23:08 -0600567 size_t i, sum = 0, carry = 0, alen = a->len, blen = b->len, clen;
Gavin Howard5083aec2019-04-23 18:03:46 -0600568 BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600569
Gavin Howardfcf29012019-04-25 20:12:19 -0600570 // TODO: This needs to be portable to non-64-bit platforms.
Stefan Eßer4b111c22019-04-24 17:23:08 -0600571 assert(sizeof(sum) >= 8);
Gavin Howardb538d802019-04-23 16:50:55 -0600572 assert(!a->rdx && !b->rdx);
573
Gavin Howard5083aec2019-04-23 18:03:46 -0600574 clen = bc_vm_growSize(alen, blen);
575 bc_num_expand(c, clen + 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600576
577 ptr_c = c->num;
Stefan Esser394a5162019-04-25 06:33:10 +0200578 bc_num_set(ptr_c, 0, c->cap);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600579
Gavin Howard5083aec2019-04-23 18:03:46 -0600580 for (i = 0; BC_NO_SIG && i < clen; ++i) {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600581
Gavin Howard5083aec2019-04-23 18:03:46 -0600582 ssize_t sidx = (ssize_t) (i - blen + 1);
583 size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600584
Stefan Eßer4b111c22019-04-24 17:23:08 -0600585 for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) {
586
Gavin Howard5083aec2019-04-23 18:03:46 -0600587 sum += ((size_t) ptr_a[j]) * ((size_t) ptr_b[k]);
Gavin Howarddc4e3712019-04-23 14:56:29 -0600588
Gavin Howard0700efc2019-04-24 17:31:23 -0600589 if (sum >= BC_BASE_DIG) {
590 carry += sum / BC_BASE_DIG;
591 sum %= BC_BASE_DIG;
Stefan Eßer4b111c22019-04-24 17:23:08 -0600592 }
593 }
594
595 ptr_c[i] = (BcDig) sum;
Gavin Howard0700efc2019-04-24 17:31:23 -0600596 assert(ptr_c[i] < BC_BASE_DIG);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600597 sum = carry;
598 carry = 0;
Gavin Howarddc4e3712019-04-23 14:56:29 -0600599 }
600
Gavin Howard5083aec2019-04-23 18:03:46 -0600601 if (sum) {
Gavin Howard0700efc2019-04-24 17:31:23 -0600602 assert(sum < BC_BASE_DIG);
Gavin Howard12a7cd22019-04-24 07:10:14 -0600603 ptr_c[clen] = (BcDig) sum;
Gavin Howard5083aec2019-04-23 18:03:46 -0600604 clen += 1;
605 }
606
607 c->len = clen;
608
Gavin Howarddc4e3712019-04-23 14:56:29 -0600609 return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
610}
611
Gavin Howard14c354a2019-04-24 14:28:24 -0600612static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
613 size_t shift, BcNumShiftAddOp op)
614{
Gavin Howardfcf29012019-04-25 20:12:19 -0600615 // TODO: Check this function.
Gavin Howard14c354a2019-04-24 14:28:24 -0600616 assert(n->len >= shift + a->len);
617 assert(!n->rdx && !a->rdx);
618 return op(n->num + shift, a->num, a->len);
619}
620
621static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600622
Gavin Howardfcf29012019-04-25 20:12:19 -0600623 // TODO: Check this function.
624
Gavin Howard9b801632019-02-16 23:34:06 -0700625 BcStatus s;
Stefan Esser14eff462019-04-25 07:42:03 +0200626 size_t max, max2, total;
Gavin Howard305249a2018-10-15 20:24:47 -0600627 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard5bede412019-02-23 09:31:28 -0700628 BcDig *digs, *dig_ptr;
Gavin Howard14c354a2019-04-24 14:28:24 -0600629 BcNumShiftAddOp op;
Gavin Howarda83a5e52019-02-21 16:51:06 -0700630 bool aone = bc_num_isOne(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600631
Gavin Howard14913fc2019-04-23 14:36:03 -0600632 assert(BC_NUM_ZERO(c));
633
Gavin Howardddce6d42019-01-03 14:07:06 -0700634 // This is here because the function is recursive.
Gavin Howard5083aec2019-04-23 18:03:46 -0600635 if (BC_SIG) return BC_STATUS_SIGNAL;
Gavin Howardddce6d42019-01-03 14:07:06 -0700636 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600637 bc_num_zero(c);
638 return BC_STATUS_SUCCESS;
639 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700640 if (aone || bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600641 bc_num_copy(c, aone ? b : a);
642 return BC_STATUS_SUCCESS;
643 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700644
Gavin Howardb5093d72018-10-17 12:13:17 -0600645 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
646 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600647 {
Gavin Howarddc4e3712019-04-23 14:56:29 -0600648 return bc_num_m_simp(a, b, c);
Gavin Howard63738202018-09-26 15:34:20 -0600649 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700650
Gavin Howarde0432502019-02-26 16:23:45 -0700651 max = BC_MAX(a->len, b->len);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600652 max = BC_MAX(max, BC_NUM_DEF_SIZE);
Gavin Howarde0432502019-02-26 16:23:45 -0700653 max2 = (max + 1) / 2;
654
Stefan Esser5fd43a32019-04-25 07:05:24 +0200655 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max);
656 digs = dig_ptr = bc_vm_malloc_digs(total);
Gavin Howard5bede412019-02-23 09:31:28 -0700657
658 bc_num_setup(&l1, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600659 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700660 bc_num_setup(&h1, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600661 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700662 bc_num_setup(&l2, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600663 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700664 bc_num_setup(&h2, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600665 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700666 bc_num_setup(&m1, dig_ptr, max);
Stefan Eßer4b111c22019-04-24 17:23:08 -0600667 dig_ptr += max;
Gavin Howard5bede412019-02-23 09:31:28 -0700668 bc_num_setup(&m2, dig_ptr, max);
Gavin Howard57223022019-04-24 08:37:14 -0600669 max = bc_vm_growSize(max, 1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600670 bc_num_init(&z0, max);
671 bc_num_init(&z1, max);
672 bc_num_init(&z2, max);
Gavin Howard14c354a2019-04-24 14:28:24 -0600673 max = bc_vm_growSize(max, max) + 1;
674 bc_num_init(&temp, max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600675
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600676 bc_num_split(a, max2, &l1, &h1);
Gavin Howard14c354a2019-04-24 14:28:24 -0600677 bc_num_clean(&l1);
678 bc_num_clean(&h1);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600679 bc_num_split(b, max2, &l2, &h2);
Gavin Howard14c354a2019-04-24 14:28:24 -0600680 bc_num_clean(&l2);
681 bc_num_clean(&h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700682
Gavin Howard14c354a2019-04-24 14:28:24 -0600683 bc_num_expand(c, max);
684 c->len = max;
Stefan Esser887d6b92019-04-25 07:56:10 +0200685 bc_num_set(c->num, 0, c->len);
Gavin Howard305249a2018-10-15 20:24:47 -0600686
Gavin Howard14c354a2019-04-24 14:28:24 -0600687 s = bc_num_sub(&h1, &l1, &m1, 0);
688 if (BC_ERR(s)) goto err;
689 s = bc_num_sub(&l2, &h2, &m2, 0);
690 if (BC_ERR(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600691
Gavin Howard14c354a2019-04-24 14:28:24 -0600692 if (!BC_NUM_ZERO(&h1) && !BC_NUM_ZERO(&h2)) {
Gavin Howard305249a2018-10-15 20:24:47 -0600693
Gavin Howard14c354a2019-04-24 14:28:24 -0600694 s = bc_num_m(&h1, &h2, &z2, 0);
695 if (BC_ERR(s)) goto err;
696 bc_num_clean(&z2);
697
698 s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays);
699 if (BC_ERR(s)) goto err;
700 s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays);
701 if (BC_ERR(s)) goto err;
702 }
703
704 if (!BC_NUM_ZERO(&l1) && !BC_NUM_ZERO(&l2)) {
705
706 s = bc_num_m(&l1, &l2, &z0, 0);
707 if (BC_ERR(s)) goto err;
708 bc_num_clean(&z0);
709
710 s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays);
711 if (BC_ERR(s)) goto err;
712 s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays);
713 if (BC_ERR(s)) goto err;
714 }
715
716 if (!BC_NUM_ZERO(&m1) && !BC_NUM_ZERO(&m2)) {
717
718 s = bc_num_m(&m1, &m2, &z1, 0);
719 if (BC_ERR(s)) goto err;
720 bc_num_clean(&z1);
721
722 op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
723 s = bc_num_shiftAddSub(c, &z1, max2, op);
724 if (BC_ERR(s)) goto err;
725 }
Gavin Howard305249a2018-10-15 20:24:47 -0600726
727err:
Gavin Howard5bede412019-02-23 09:31:28 -0700728 free(digs);
Gavin Howard305249a2018-10-15 20:24:47 -0600729 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600730 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600731 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600732 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600733 return s;
734}
735
Gavin Howard1cbfe242019-01-09 17:13:11 -0700736static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600737
Gavin Howardfcf29012019-04-25 20:12:19 -0600738 // TODO: Check this function.
739
Gavin Howard305249a2018-10-15 20:24:47 -0600740 BcStatus s;
741 BcNum cpa, cpb;
Gavin Howard14913fc2019-04-23 14:36:03 -0600742 size_t ardx = a->rdx, brdx = b->rdx, azero, bzero, zero, len, rscale;
Gavin Howard305249a2018-10-15 20:24:47 -0600743
Gavin Howard14913fc2019-04-23 14:36:03 -0600744 bc_num_setToZero(c, 0);
745
746 scale = BC_MAX(scale, ardx);
747 scale = BC_MAX(scale, brdx);
748 rscale = ardx + brdx;
749 scale = BC_MIN(rscale, scale);
Gavin Howard305249a2018-10-15 20:24:47 -0600750
Gavin Howardbaa4f582019-01-24 13:56:35 -0700751 bc_num_createCopy(&cpa, a);
752 bc_num_createCopy(&cpb, b);
Gavin Howard305249a2018-10-15 20:24:47 -0600753
Gavin Howard23d8d252018-10-17 12:03:41 -0600754 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600755
Gavin Howard14913fc2019-04-23 14:36:03 -0600756 bc_num_shiftLeft(&cpa, ardx);
757 bc_num_clean(&cpa);
758 azero = bc_num_shiftZero(&cpa);
759 bc_num_shiftLeft(&cpb, brdx);
760 bzero = bc_num_shiftZero(&cpb);
761 bc_num_clean(&cpb);
762
Gavin Howard14c354a2019-04-24 14:28:24 -0600763 s = bc_num_k(&cpa, &cpb, c);
Gavin Howardc78e7522019-02-22 13:24:25 -0700764 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600765
Gavin Howard14913fc2019-04-23 14:36:03 -0600766 zero = bc_vm_growSize(azero, bzero);
767 len = bc_vm_growSize(c->len, zero);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600768
Gavin Howard14913fc2019-04-23 14:36:03 -0600769 bc_num_expand(c, len);
770 bc_num_shiftLeft(c, len - c->len);
771 bc_num_shiftRight(c, rscale);
Gavin Howard305249a2018-10-15 20:24:47 -0600772
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600773 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600774
775err:
Gavin Howard14913fc2019-04-23 14:36:03 -0600776 bc_num_unshiftZero(&cpb, bzero);
Gavin Howard305249a2018-10-15 20:24:47 -0600777 bc_num_free(&cpb);
Gavin Howard14913fc2019-04-23 14:36:03 -0600778 bc_num_unshiftZero(&cpa, azero);
Gavin Howard305249a2018-10-15 20:24:47 -0600779 bc_num_free(&cpa);
780 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700781}
782
Gavin Howard1cbfe242019-01-09 17:13:11 -0700783static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700784
Gavin Howardfcf29012019-04-25 20:12:19 -0600785 // TODO: Check this function.
786
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600787 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600788 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600789 size_t len, end, i;
790 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600791 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700792
Gavin Howardddce6d42019-01-03 14:07:06 -0700793 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
794 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600795 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600796 return BC_STATUS_SUCCESS;
797 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700798 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600799 bc_num_copy(c, a);
800 bc_num_retireMul(c, scale, a->neg, b->neg);
801 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600802 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700803
Gavin Howard620a8af2019-01-24 13:54:54 -0700804 bc_num_init(&cp, bc_num_mulReq(a, b, scale));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600805 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600806 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700807
Gavin Howard890d0c02018-10-30 16:34:50 -0600808 if (len > cp.len) {
Gavin Howard007afdf2019-02-23 09:45:42 -0700809 bc_num_expand(&cp, bc_vm_growSize(len, 2));
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600810 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600811 }
Gavin Howard021150b2018-03-10 15:40:42 -0700812
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600813 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600814 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600815 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700816
Gavin Howard63738202018-09-26 15:34:20 -0600817 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600818 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600819 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600820 len -= i - 1;
821 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700822
Gavin Howard007afdf2019-02-23 09:45:42 -0700823 if (cp.cap == cp.len) bc_num_expand(&cp, bc_vm_growSize(cp.len, 1));
Gavin Howardb651f1a2018-02-28 17:34:18 -0700824
Gavin Howard63738202018-09-26 15:34:20 -0600825 // We want an extra zero in front to make things simpler.
826 cp.num[cp.len++] = 0;
827 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700828
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600829 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700830
Stefan Esser394a5162019-04-25 06:33:10 +0200831 bc_num_set(c->num + end, 0, c->cap - end);
Gavin Howard63738202018-09-26 15:34:20 -0600832 c->rdx = cp.rdx;
833 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600834 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700835
Gavin Howard2d188a52019-02-25 14:19:08 -0700836 for (i = end - 1; BC_NO_SIG && BC_NO_ERR(!s) && i < end; --i) {
Gavin Howard5a788392019-03-28 11:01:13 -0600837
Gavin Howard63738202018-09-26 15:34:20 -0600838 n = cp.num + i;
Gavin Howardecafd4f2019-02-23 09:30:45 -0700839 q = 0;
Gavin Howard5a788392019-03-28 11:01:13 -0600840
841 while (BC_NO_SIG && BC_NO_ERR(!s) &&
842 (n[len] || bc_num_compare(n, p, len) >= 0))
843 {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -0700844 s = bc_num_subArrays(n, p, len);
Gavin Howard5a788392019-03-28 11:01:13 -0600845 q += 1;
846 }
847
Gavin Howard63738202018-09-26 15:34:20 -0600848 c->num[i] = q;
849 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700850
Gavin Howardf2ee6342019-04-09 17:05:20 -0600851 if (BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howarde5f11c72019-02-23 09:42:51 -0700852 if (BC_NO_ERR(!s)) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600853 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600854
Gavin Howard63738202018-09-26 15:34:20 -0600855 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700856}
857
Gavin Howard1cbfe242019-01-09 17:13:11 -0700858static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
859 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600860{
Gavin Howardfcf29012019-04-25 20:12:19 -0600861 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -0600862 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600863 BcNum temp;
864 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700865
Gavin Howardddce6d42019-01-03 14:07:06 -0700866 if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
867 if (BC_NUM_ZERO(a)) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700868 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600869 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600870 return BC_STATUS_SUCCESS;
871 }
Gavin Howard8b254872018-03-14 01:13:35 -0600872
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600873 bc_num_init(&temp, d->cap);
Gavin Howard1769abc2019-02-21 09:36:14 -0700874 s = bc_num_d(a, b, c, scale);
875 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -0700876 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700877
Gavin Howard1ab22d22019-01-03 13:32:17 -0700878 if (scale) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600879
Gavin Howard53eba8b2018-10-31 15:14:37 -0600880 s = bc_num_m(c, b, &temp, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700881 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600882 s = bc_num_sub(a, &temp, d, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700883 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600884
Gavin Howardddce6d42019-01-03 14:07:06 -0700885 if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600886
887 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600888 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600889 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700890
891err:
Gavin Howard18441082018-10-22 10:03:30 -0600892 bc_num_free(&temp);
893 return s;
894}
895
Gavin Howard2d188a52019-02-25 14:19:08 -0700896static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
897{
Gavin Howardfcf29012019-04-25 20:12:19 -0600898 // TODO: Check this function.
Gavin Howard18441082018-10-22 10:03:30 -0600899 BcStatus s;
900 BcNum c1;
Gavin Howard798508b2019-02-21 16:45:32 -0700901 size_t ts;
Gavin Howard18441082018-10-22 10:03:30 -0600902
Gavin Howard007afdf2019-02-23 09:45:42 -0700903 ts = bc_vm_growSize(scale, b->rdx);
Gavin Howard798508b2019-02-21 16:45:32 -0700904 ts = BC_MAX(ts, a->rdx);
905
906 bc_num_init(&c1, bc_num_mulReq(a, b, ts));
Gavin Howard54b946a2018-10-23 12:06:57 -0600907 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600908 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600909
Gavin Howard63738202018-09-26 15:34:20 -0600910 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700911}
912
Gavin Howard1cbfe242019-01-09 17:13:11 -0700913static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700914
Gavin Howardfcf29012019-04-25 20:12:19 -0600915 // TODO: Check this function.
916
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600917 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600918 BcNum copy;
Gavin Howard510859c2019-01-07 09:14:01 -0700919 unsigned long pow = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600920 size_t i, powrdx, resrdx;
921 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700922
Gavin Howardecafd4f2019-02-23 09:30:45 -0700923 if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700924
Gavin Howardddce6d42019-01-03 14:07:06 -0700925 if (BC_NUM_ZERO(b)) {
Gavin Howard63738202018-09-26 15:34:20 -0600926 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600927 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600928 }
Gavin Howardddce6d42019-01-03 14:07:06 -0700929 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600930 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600931 return BC_STATUS_SUCCESS;
932 }
Gavin Howarda83a5e52019-02-21 16:51:06 -0700933 if (bc_num_isOne(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600934 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600935 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600936 return s;
937 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700938
Gavin Howard63738202018-09-26 15:34:20 -0600939 neg = b->neg;
940 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600941 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700942 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600943 if (s) return s;
944
Gavin Howardbaa4f582019-01-24 13:56:35 -0700945 bc_num_createCopy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700946
Gavin Howard63738202018-09-26 15:34:20 -0600947 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600948
Gavin Howard2d188a52019-02-25 14:19:08 -0700949 for (powrdx = a->rdx; BC_NO_SIG && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600950 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600951 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700952 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600953 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700954
Gavin Howard2d188a52019-02-25 14:19:08 -0700955 if (BC_SIG) goto sig_err;
Gavin Howard6fb635f2018-03-03 12:45:42 -0700956
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600957 bc_num_copy(c, &copy);
Gavin Howard1ab22d22019-01-03 13:32:17 -0700958 resrdx = powrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600959
Gavin Howard2d188a52019-02-25 14:19:08 -0700960 while (BC_NO_SIG && (pow >>= 1)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600961
Gavin Howard4d5daba2019-01-07 14:42:50 -0700962 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600963 s = bc_num_mul(&copy, &copy, &copy, powrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700964 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600965
Gavin Howard63738202018-09-26 15:34:20 -0600966 if (pow & 1) {
967 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600968 s = bc_num_mul(c, &copy, c, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -0700969 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600970 }
971 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700972
Gavin Howard2d188a52019-02-25 14:19:08 -0700973 if (BC_SIG) goto sig_err;
Gavin Howard9b801632019-02-16 23:34:06 -0700974 if (neg) {
975 s = bc_num_inv(c, c, scale);
Gavin Howardc78e7522019-02-22 13:24:25 -0700976 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard9b801632019-02-16 23:34:06 -0700977 }
978
Gavin Howard63738202018-09-26 15:34:20 -0600979 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700980
Gavin Howard53eba8b2018-10-31 15:14:37 -0600981 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600982 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600983 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700984
Gavin Howard9b801632019-02-16 23:34:06 -0700985sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -0700986 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700987err:
Gavin Howard63738202018-09-26 15:34:20 -0600988 bc_num_free(&copy);
989 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700990}
991
Gavin Howard7bda4782018-12-28 09:53:22 -0700992#if BC_ENABLE_EXTRA_MATH
Gavin Howard2d188a52019-02-25 14:19:08 -0700993static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c,
994 size_t scale)
995{
Gavin Howardfcf29012019-04-25 20:12:19 -0600996 // TODO: Check this function.
Gavin Howardac4d9122018-12-27 23:59:12 -0700997 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -0700998 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -0700999
1000 BC_UNUSED(scale);
1001
1002 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001003 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001004
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001005 if (val < c->rdx) bc_num_truncate(c, c->rdx - val);
1006 else if (val > c->rdx) bc_num_extend(c, val - c->rdx);
Gavin Howardac4d9122018-12-27 23:59:12 -07001007
1008 return s;
1009}
1010
Gavin Howard2d188a52019-02-25 14:19:08 -07001011static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale)
1012{
Gavin Howardfcf29012019-04-25 20:12:19 -06001013 // TODO: Check this function.
Gavin Howardac4d9122018-12-27 23:59:12 -07001014 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001015 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001016
1017 BC_UNUSED(scale);
1018
1019 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001020 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001021
Gavin Howarddbbada82019-02-22 08:51:19 -07001022 bc_num_shiftLeft(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001023
1024 return s;
1025}
1026
Gavin Howard2d188a52019-02-25 14:19:08 -07001027static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c,
1028 size_t scale)
1029{
Gavin Howardfcf29012019-04-25 20:12:19 -06001030 // TODO: Check this function.
Gavin Howardac4d9122018-12-27 23:59:12 -07001031 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard510859c2019-01-07 09:14:01 -07001032 unsigned long val = 0;
Gavin Howardac4d9122018-12-27 23:59:12 -07001033
1034 BC_UNUSED(scale);
1035
1036 s = bc_num_intop(a, b, c, &val);
Gavin Howardecafd4f2019-02-23 09:30:45 -07001037 if (BC_ERR(s)) return s;
Gavin Howardac4d9122018-12-27 23:59:12 -07001038
Gavin Howard2a366922019-01-16 10:50:20 -07001039 if (BC_NUM_ZERO(c)) return s;
1040
Gavin Howarddbbada82019-02-22 08:51:19 -07001041 bc_num_shiftRight(c, (size_t) val);
Gavin Howardac4d9122018-12-27 23:59:12 -07001042
1043 return s;
1044}
Gavin Howard7bda4782018-12-28 09:53:22 -07001045#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -07001046
Gavin Howard1cbfe242019-01-09 17:13:11 -07001047static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
1048 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -07001049{
Gavin Howardfcf29012019-04-25 20:12:19 -06001050 // TODO: Check this function.
Gavin Howarda1c44392018-09-27 12:41:15 -06001051 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -06001052 BcNum num2, *ptr_a, *ptr_b;
1053 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001054
Gavin Howard63738202018-09-26 15:34:20 -06001055 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001056
Gavin Howard890d0c02018-10-30 16:34:50 -06001057 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -06001058 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001059 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -06001060 init = true;
Gavin Howard63738202018-09-26 15:34:20 -06001061 }
1062 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001063
Gavin Howard63738202018-09-26 15:34:20 -06001064 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -06001065 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -06001066 if (c != a) {
1067 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -06001068 init = true;
1069 }
1070 }
1071 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001072
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001073 if (init) bc_num_init(c, req);
1074 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001075
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001076 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001077
Gavin Howardddce6d42019-01-03 14:07:06 -07001078 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howard87c29c72019-03-28 11:22:51 -06001079 assert(c->rdx <= c->len || !c->len || s);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001080
Gavin Howardba009802018-09-29 04:41:51 -06001081 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001082
Gavin Howardee9d01e2019-02-16 22:48:11 -07001083 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001084}
1085
Gavin Howardc90ed902019-01-02 13:07:49 -07001086#ifndef NDEBUG
Gavin Howard1cbfe242019-01-09 17:13:11 -07001087static bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001088
Gavin Howardc7d655b2018-12-28 19:45:13 -07001089 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -06001090 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001091
Gavin Howard63738202018-09-26 15:34:20 -06001092 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001093
Gavin Howard63738202018-09-26 15:34:20 -06001094 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001095
Gavin Howard8a921bd2018-10-11 14:15:32 -06001096 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001097
Gavin Howard63738202018-09-26 15:34:20 -06001098 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001099
Gavin Howard63738202018-09-26 15:34:20 -06001100 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001101
Gavin Howard63738202018-09-26 15:34:20 -06001102 radix = true;
1103 continue;
1104 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001105
Gavin Howardc7d655b2018-12-28 19:45:13 -07001106 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -06001107 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001108
Gavin Howard63738202018-09-26 15:34:20 -06001109 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001110}
Gavin Howardc90ed902019-01-02 13:07:49 -07001111#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -07001112
Gavin Howard1cbfe242019-01-09 17:13:11 -07001113static unsigned long bc_num_parseChar(char c, size_t base_t) {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001114
Gavin Howardfcf29012019-04-25 20:12:19 -06001115 // TODO: Check this function.
1116
Gavin Howardc7d655b2018-12-28 19:45:13 -07001117 if (isupper(c)) {
1118 c = BC_NUM_NUM_LETTER(c);
Gavin Howard6193aaf2019-01-03 16:44:04 -07001119 c = ((size_t) c) >= base_t ? (char) base_t - 1 : c;
Gavin Howardc7d655b2018-12-28 19:45:13 -07001120 }
1121 else c -= '0';
1122
1123 return (unsigned long) (uchar) c;
1124}
1125
Gavin Howard1cbfe242019-01-09 17:13:11 -07001126static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -07001127
Gavin Howard3b60abb2019-04-26 08:49:17 -06001128 size_t len, i, temp, mod;
Gavin Howard63738202018-09-26 15:34:20 -06001129 const char *ptr;
Gavin Howardd43ec372019-04-25 21:13:54 -06001130 bool zero = true, rdx;
Stefan Eßer4b111c22019-04-24 17:23:08 -06001131
Gavin Howard63738202018-09-26 15:34:20 -06001132 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001133
Gavin Howard63738202018-09-26 15:34:20 -06001134 val += i;
Gavin Howard3b60abb2019-04-26 08:49:17 -06001135 assert(isalnum(val[0]) || val[0] == '.');
Gavin Howard63738202018-09-26 15:34:20 -06001136 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001137
Gavin Howard63738202018-09-26 15:34:20 -06001138 ptr = strchr(val, '.');
Gavin Howardd43ec372019-04-25 21:13:54 -06001139 rdx = (ptr != NULL);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001140
Gavin Howardd43ec372019-04-25 21:13:54 -06001141 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i);
1142
1143 n->scale = (size_t) (rdx * ((val + len) - (ptr + 1)));
Gavin Howard1c1697c2019-04-26 10:07:45 -06001144 n->rdx = BC_NUM_RDX(n->scale);
Gavin Howardd43ec372019-04-25 21:13:54 -06001145
Gavin Howard3b60abb2019-04-26 08:49:17 -06001146 i = len - (ptr == val ? 0 : i) - rdx;
1147 temp = i + (BC_BASE_POWER - 1);
1148 mod = n->scale % BC_BASE_POWER;
1149 i = mod ? BC_BASE_POWER - mod : 0;
1150 n->len = ((temp + i) / BC_BASE_POWER);
Gavin Howardd43ec372019-04-25 21:13:54 -06001151
1152 bc_num_expand(n, n->len);
1153 bc_num_set(n->num, 0, n->len);
Gavin Howard6fbdb292018-02-27 15:44:48 -07001154
Gavin Howard6076dbb2019-04-26 09:47:50 -06001155 if (zero) n->len = n->rdx = 0;
1156 else {
Gavin Howardfcf29012019-04-25 20:12:19 -06001157
Gavin Howard3b60abb2019-04-26 08:49:17 -06001158 unsigned long exp, pow = 1;
Gavin Howardd43ec372019-04-25 21:13:54 -06001159
Gavin Howard3b60abb2019-04-26 08:49:17 -06001160 exp = i;
Gavin Howardd43ec372019-04-25 21:13:54 -06001161 for (i = 0; i < exp; pow *= BC_BASE, ++i);
1162
1163 for (i = len - 1; i < len; --i, ++exp) {
Gavin Howard8dd307e2019-01-08 23:05:19 -07001164
Gavin Howardc7d655b2018-12-28 19:45:13 -07001165 char c = val[i];
Gavin Howard8dd307e2019-01-08 23:05:19 -07001166
Gavin Howardd43ec372019-04-25 21:13:54 -06001167 if (c == '.') exp -= 1;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001168 else {
Gavin Howardd43ec372019-04-25 21:13:54 -06001169
1170 size_t idx = exp / BC_BASE_POWER;
1171
Gavin Howard8dd307e2019-01-08 23:05:19 -07001172 if (isupper(c)) c = '9';
Gavin Howardf196fbe2019-04-26 08:49:05 -06001173 n->num[idx] += (((unsigned long) c) - '0') * pow;
Gavin Howardd43ec372019-04-25 21:13:54 -06001174
1175 if ((exp + 1) % BC_BASE_POWER == 0) pow = 1;
1176 else pow *= BC_BASE;
Gavin Howard8dd307e2019-01-08 23:05:19 -07001177 }
Gavin Howardc7d655b2018-12-28 19:45:13 -07001178 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001179 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001180}
1181
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001182static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
1183 BcNum *restrict base, size_t base_t)
Gavin Howard1cbfe242019-01-09 17:13:11 -07001184{
Gavin Howardfcf29012019-04-25 20:12:19 -06001185 // TODO: Check this function.
Gavin Howard1cbfe242019-01-09 17:13:11 -07001186 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001187 BcNum temp, mult, result;
Gavin Howard1cbfe242019-01-09 17:13:11 -07001188 BcDig c = 0;
Gavin Howard11b9afd2018-10-18 14:23:28 -06001189 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -06001190 unsigned long v;
Gavin Howard1769abc2019-02-21 09:36:14 -07001191 size_t i, digs, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -07001192
Gavin Howard11b9afd2018-10-18 14:23:28 -06001193 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001194 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -07001195
Gavin Howard26d078f2019-01-24 13:58:12 -07001196 bc_num_init(&temp, BC_NUM_LONG_LOG10);
1197 bc_num_init(&mult, BC_NUM_LONG_LOG10);
Gavin Howardede51f02018-03-02 12:30:00 -07001198
Gavin Howardc7d655b2018-12-28 19:45:13 -07001199 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard9b801632019-02-16 23:34:06 -07001200
Gavin Howardc7d655b2018-12-28 19:45:13 -07001201 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001202
Gavin Howard53eba8b2018-10-31 15:14:37 -06001203 s = bc_num_mul(n, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001204 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001205 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001206 s = bc_num_add(&mult, &temp, n, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001207 if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -06001208 }
Gavin Howardede51f02018-03-02 12:30:00 -07001209
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001210 if (i == len && !(c = val[i])) goto int_err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001211
Gavin Howard63738202018-09-26 15:34:20 -06001212 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001213 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -06001214 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -07001215
Gavin Howard2d188a52019-02-25 14:19:08 -07001216 for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs)
Gavin Howardecafd4f2019-02-23 09:30:45 -07001217 {
Gavin Howardc7d655b2018-12-28 19:45:13 -07001218 v = bc_num_parseChar(c, base_t);
Gavin Howard9b801632019-02-16 23:34:06 -07001219
Gavin Howard53eba8b2018-10-31 15:14:37 -06001220 s = bc_num_mul(&result, base, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001221 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001222
Gavin Howard705c4bc2018-12-03 15:38:04 -07001223 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001224 s = bc_num_add(&result, &temp, &result, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001225 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001226 s = bc_num_mul(&mult, base, &mult, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001227 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001228 }
Gavin Howardede51f02018-03-02 12:30:00 -07001229
Gavin Howardf2ee6342019-04-09 17:05:20 -06001230 if (BC_SIG) {
1231 s = BC_STATUS_SIGNAL;
1232 goto err;
1233 }
1234
Gavin Howard1769abc2019-02-21 09:36:14 -07001235 // This one cannot be a divide by 0 because mult starts out at 1, then is
1236 // multiplied by base, and base cannot be 0, so mult cannot be 0.
1237 s = bc_num_div(&result, &mult, &result, digs);
1238 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001239 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001240 s = bc_num_add(n, &result, n, digs);
Gavin Howardc78e7522019-02-22 13:24:25 -07001241 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardd1417682019-02-15 17:08:42 -07001242
Gavin Howardddce6d42019-01-03 14:07:06 -07001243 if (BC_NUM_NONZERO(n)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001244 if (n->rdx < digs) bc_num_extend(n, digs - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -06001245 }
1246 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -07001247
Gavin Howardd1417682019-02-15 17:08:42 -07001248err:
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001249 bc_num_free(&result);
1250int_err:
Gavin Howard63738202018-09-26 15:34:20 -06001251 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -06001252 bc_num_free(&temp);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001253 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -07001254}
1255
Gavin Howard1cbfe242019-01-09 17:13:11 -07001256static void bc_num_printNewline() {
Gavin Howard6193aaf2019-01-03 16:44:04 -07001257 if (vm->nchars >= (size_t) (vm->line_len - 1)) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001258 bc_vm_putchar('\\');
1259 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001260 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001261 }
Gavin Howard80977b22018-09-06 14:29:13 -06001262}
1263
Gavin Howard40a085f2018-12-03 12:08:59 -07001264#if DC_ENABLED
Gavin Howard1cbfe242019-01-09 17:13:11 -07001265static void bc_num_printChar(size_t n, size_t len, bool rdx) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001266 // TODO: Check this function.
Gavin Howard3c02ed32018-12-28 18:17:15 -07001267 BC_UNUSED(rdx);
1268 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -07001269 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -06001270}
1271#endif // DC_ENABLED
1272
Gavin Howard1cbfe242019-01-09 17:13:11 -07001273static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
1274
Gavin Howardfcf29012019-04-25 20:12:19 -06001275 // TODO: Check this function.
1276
Gavin Howarda84ad992018-12-03 19:11:06 -07001277 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -06001278
Gavin Howardab5e9632019-01-02 13:32:02 -07001279 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001280 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -07001281 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001282
Gavin Howardab5e9632019-01-02 13:32:02 -07001283 bc_num_printNewline();
Gavin Howard530e3072019-04-23 16:23:36 -06001284 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -06001285
Gavin Howard530e3072019-04-23 16:23:36 -06001286 for (exp = 0; exp < len; pow /= BC_BASE, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -07001287 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -07001288 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001289 dig = n / pow;
1290 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -07001291 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -06001292 }
Gavin Howard6fbdb292018-02-27 15:44:48 -07001293}
Gavin Howardfe679f02018-02-14 15:50:09 -07001294
Gavin Howard1cbfe242019-01-09 17:13:11 -07001295static void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -07001296
Gavin Howard3c02ed32018-12-28 18:17:15 -07001297 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -06001298
Gavin Howard3c02ed32018-12-28 18:17:15 -07001299 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001300 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -06001301 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -07001302 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -06001303 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001304
Gavin Howardab5e9632019-01-02 13:32:02 -07001305 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -07001306 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -07001307 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001308}
1309
Gavin Howard1cbfe242019-01-09 17:13:11 -07001310static void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -07001311
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001312 size_t i, j, rdx = n->rdx;
1313 bool zero = true;
1314 size_t buffer[BC_BASE_POWER];
Gavin Howard32f2beb2018-03-09 11:43:20 -07001315
Gavin Howard48af52e2018-10-30 14:47:38 -06001316 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001317 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001318
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001319 for (i = n->len - 1; i < n->len; --i) {
1320
1321 BcDig n9 = n->num[i];
1322 size_t temp;
1323 bool irdx = (i == rdx - 1);
1324
1325 zero = (zero & !irdx);
1326 temp = n->scale % BC_BASE_POWER;
1327 temp = i || !temp ? 0 : BC_BASE_POWER - temp;
1328
1329 memset(buffer, 0, BC_BASE_POWER * sizeof(size_t));
1330
1331 for (j = 0; n9 && j < BC_BASE_POWER; ++j) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001332 buffer[j] = n9 % BC_BASE;
1333 n9 /= BC_BASE;
1334 }
Stefan Esser0da17752019-04-25 12:25:15 +02001335
Gavin Howard51cd7ea2019-04-26 09:48:12 -06001336 for (j = BC_BASE_POWER - 1; j < BC_BASE_POWER && j >= temp; --j) {
1337 bool print_rdx = (irdx & (j == BC_BASE_POWER - 1));
1338 zero = (zero && buffer[j] == 0);
1339 if (!zero) bc_num_printHex(buffer[j], 1, print_rdx);
Stefan Eßer4b111c22019-04-24 17:23:08 -06001340 }
1341 }
Gavin Howard32f2beb2018-03-09 11:43:20 -07001342}
1343
Gavin Howard7ad5a662019-02-19 14:40:46 -07001344#if BC_ENABLE_EXTRA_MATH
1345static void bc_num_printExponent(const BcNum *restrict n, bool eng) {
1346
Gavin Howardfcf29012019-04-25 20:12:19 -06001347 // TODO: Check this function.
1348
Gavin Howard7ad5a662019-02-19 14:40:46 -07001349 bool neg = (n->len <= n->rdx);
1350 BcNum temp, exp;
1351 size_t places, mod;
1352 BcDig digs[BC_NUM_LONG_LOG10];
1353
1354 bc_num_createCopy(&temp, n);
1355
1356 if (neg) {
1357 places = n->rdx - bc_num_len(n) + 1;
1358 mod = places % 3;
1359 if (eng && mod != 0) places += 3 - mod;
Gavin Howarddbbada82019-02-22 08:51:19 -07001360 bc_num_shiftLeft(&temp, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001361 }
1362 else {
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001363 places = bc_num_int(n) - 1;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001364 mod = places % 3;
1365 if (eng && mod != 0) places -= 3 - (3 - mod);
Gavin Howarddbbada82019-02-22 08:51:19 -07001366 bc_num_shiftRight(&temp, places);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001367 }
1368
1369 bc_num_printDecimal(&temp);
1370
1371 bc_num_printNewline();
1372 bc_vm_putchar('e');
1373
1374 if (!places) {
1375 bc_num_printHex(0, 1, false);
1376 goto exit;
1377 }
1378
1379 if (neg) {
1380 bc_num_printNewline();
1381 bc_vm_putchar('-');
1382 }
1383
1384 bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10);
1385 bc_num_ulong2num(&exp, (unsigned long) places);
1386
1387 bc_num_printDecimal(&exp);
1388
1389exit:
1390 bc_num_free(&temp);
1391}
1392#endif // BC_ENABLE_EXTRA_MATH
1393
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001394static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1395 size_t len, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001396{
Gavin Howardfcf29012019-04-25 20:12:19 -06001397 // TODO: Check this function.
Gavin Howard63738202018-09-26 15:34:20 -06001398 BcStatus s;
1399 BcVec stack;
1400 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001401 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001402 size_t i;
1403 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001404
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001405 assert(!BC_NUM_ZERO(base));
1406
Gavin Howardddce6d42019-01-03 14:07:06 -07001407 if (BC_NUM_ZERO(n)) {
1408 print(0, len, false);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001409 return BC_STATUS_SUCCESS;
Gavin Howard48af52e2018-10-30 14:47:38 -06001410 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001411
Gavin Howardd3929832018-12-24 15:13:15 -07001412 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001413 bc_num_init(&fracp, n->rdx);
Gavin Howardddce6d42019-01-03 14:07:06 -07001414 bc_num_init(&digit, len);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001415 bc_num_init(&frac_len, bc_num_int(n));
Gavin Howard63738202018-09-26 15:34:20 -06001416 bc_num_one(&frac_len);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001417 bc_num_createCopy(&intp, n);
Gavin Howarda50fc542018-03-29 17:25:38 -06001418
Gavin Howard63738202018-09-26 15:34:20 -06001419 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001420 s = bc_num_sub(n, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001421 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001422
Gavin Howard2d188a52019-02-25 14:19:08 -07001423 while (BC_NO_SIG && BC_NUM_NONZERO(&intp)) {
Gavin Howard1769abc2019-02-21 09:36:14 -07001424
1425 // Dividing by base cannot be divide by 0 because base cannot be 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001426 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001427 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001428
1429 // Will never fail (except for signals) because digit is
1430 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001431 s = bc_num_ulong(&digit, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001432 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001433
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001434 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001435 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001436
Gavin Howard2d188a52019-02-25 14:19:08 -07001437 if (BC_SIG) goto sig_err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001438
Gavin Howard2d188a52019-02-25 14:19:08 -07001439 for (i = 0; BC_NO_SIG && i < stack.len; ++i) {
Gavin Howard63738202018-09-26 15:34:20 -06001440 ptr = bc_vec_item_rev(&stack, i);
1441 assert(ptr);
Gavin Howardddce6d42019-01-03 14:07:06 -07001442 print(*ptr, len, false);
Gavin Howard63738202018-09-26 15:34:20 -06001443 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001444
Gavin Howard2d188a52019-02-25 14:19:08 -07001445 if (BC_SIG) goto sig_err;
Gavin Howard63738202018-09-26 15:34:20 -06001446 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001447
Gavin Howard2d188a52019-02-25 14:19:08 -07001448 for (radix = true; BC_NO_SIG && frac_len.len <= n->rdx; radix = false) {
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001449
Gavin Howard53eba8b2018-10-31 15:14:37 -06001450 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001451 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001452
1453 // Will never fail (except for signals) because fracp is
1454 // guaranteed to be non-negative and small enough.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001455 s = bc_num_ulong(&fracp, &dig);
Gavin Howardc78e7522019-02-22 13:24:25 -07001456 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001457
Gavin Howard705c4bc2018-12-03 15:38:04 -07001458 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001459 s = bc_num_sub(&fracp, &intp, &fracp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001460 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001461
Gavin Howardddce6d42019-01-03 14:07:06 -07001462 print(dig, len, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001463 s = bc_num_mul(&frac_len, base, &frac_len, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001464 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001465 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001466
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001467sig_err:
Gavin Howard2d188a52019-02-25 14:19:08 -07001468 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001469err:
Gavin Howard63738202018-09-26 15:34:20 -06001470 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001471 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001472 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001473 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001474 bc_vec_free(&stack);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001475 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001476}
1477
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001478static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base,
1479 size_t base_t)
Gavin Howard3fb24fa2019-02-15 17:18:57 -07001480{
Gavin Howardfcf29012019-04-25 20:12:19 -06001481 // TODO: Check this function.
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001482 BcStatus s;
Gavin Howard1ab22d22019-01-03 13:32:17 -07001483 size_t width;
Gavin Howard83eb8392018-10-09 01:21:19 -06001484 BcNumDigitOp print;
1485 bool neg = n->neg;
1486
Gavin Howard48af52e2018-10-30 14:47:38 -06001487 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001488 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001489
1490 n->neg = false;
1491
Gavin Howardbe4e8222019-01-03 13:34:16 -07001492 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001493 width = 1;
1494 print = bc_num_printHex;
1495 }
1496 else {
Gavin Howardbe4e8222019-01-03 13:34:16 -07001497 width = bc_num_log10(base_t - 1) - 1;
Gavin Howard83eb8392018-10-09 01:21:19 -06001498 print = bc_num_printDigits;
1499 }
1500
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001501 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001502 n->neg = neg;
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001503
1504 return s;
Gavin Howard83eb8392018-10-09 01:21:19 -06001505}
1506
Gavin Howard40a085f2018-12-03 12:08:59 -07001507#if DC_ENABLED
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001508BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1509 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001510}
1511#endif // DC_ENABLED
1512
Gavin Howard3c02ed32018-12-28 18:17:15 -07001513void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001514 assert(n);
1515 n->num = num;
1516 n->cap = cap;
Gavin Howardfcf29012019-04-25 20:12:19 -06001517 n->rdx = n->scale = n->len = 0;
Gavin Howardb8a12922018-12-21 21:40:45 -07001518 n->neg = false;
1519}
1520
Gavin Howard3c02ed32018-12-28 18:17:15 -07001521void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard88e2e632019-04-24 14:01:20 -06001522 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001523 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Stefan Esser5fd43a32019-04-25 07:05:24 +02001524 bc_num_setup(n, bc_vm_malloc_digs(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001525}
1526
Gavin Howarded392aa2018-02-27 13:09:26 -07001527void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001528 assert(num);
1529 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001530}
1531
Gavin Howard3c02ed32018-12-28 18:17:15 -07001532void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard63738202018-09-26 15:34:20 -06001533 assert(d && s);
Gavin Howard7344ba72019-01-03 13:37:27 -07001534 if (d == s) return;
Gavin Howarddcff3c92019-01-09 10:04:56 -07001535 bc_num_expand(d, s->len);
Gavin Howard7344ba72019-01-03 13:37:27 -07001536 d->len = s->len;
1537 d->neg = s->neg;
1538 d->rdx = s->rdx;
Gavin Howardfcf29012019-04-25 20:12:19 -06001539 d->scale = s->scale;
Stefan Esser394a5162019-04-25 06:33:10 +02001540 bc_num_cpy(d->num, s->num, d->len);
Gavin Howard5a049c42018-02-15 11:24:11 -07001541}
1542
Gavin Howardbaa4f582019-01-24 13:56:35 -07001543void bc_num_createCopy(BcNum *d, const BcNum *s) {
1544 bc_num_init(d, s->len);
1545 bc_num_copy(d, s);
1546}
1547
1548void bc_num_createFromUlong(BcNum *n, unsigned long val) {
Stefan Eßer4b111c22019-04-24 17:23:08 -06001549 bc_num_init(n, (BC_NUM_LONG_LOG10 - 1) / BC_BASE_POWER + 1);
Gavin Howardbaa4f582019-01-24 13:56:35 -07001550 bc_num_ulong2num(n, val);
1551}
1552
Gavin Howard7ad5a662019-02-19 14:40:46 -07001553size_t bc_num_scale(const BcNum *restrict n) {
Gavin Howardfcf29012019-04-25 20:12:19 -06001554 return n->scale;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001555}
1556
1557size_t bc_num_len(const BcNum *restrict n) {
1558
Gavin Howardfcf29012019-04-25 20:12:19 -06001559 // TODO: Check this function.
Gavin Howard7ad5a662019-02-19 14:40:46 -07001560
Gavin Howardfcf29012019-04-25 20:12:19 -06001561 size_t i, len = n->len;
1562 BcDig dig;
1563
1564 if (n->rdx != len) return len;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001565 for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i);
1566
Gavin Howardfcf29012019-04-25 20:12:19 -06001567 dig = n->num[len - 1];
1568
1569 for (i = BC_BASE_DIG; i && (dig % (BcDig) i == dig); i /= BC_BASE);
1570
1571 return (len - 1) * BC_BASE_POWER + i;
Gavin Howard7ad5a662019-02-19 14:40:46 -07001572}
1573
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001574BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
1575 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001576{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001577 BcStatus s = BC_STATUS_SUCCESS;
1578
Gavin Howard63738202018-09-26 15:34:20 -06001579 assert(n && val && base);
Gavin Howarde97ae922019-02-25 21:47:17 -07001580 assert(BC_ENABLE_EXTRA_MATH ||
1581 (base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase));
Gavin Howardc90ed902019-01-02 13:07:49 -07001582 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001583
Gavin Howardd746e802018-12-28 22:06:00 -07001584 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howard530e3072019-04-23 16:23:36 -06001585 else if (base_t == BC_BASE) bc_num_parseDecimal(n, val);
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001586 else s = bc_num_parseBase(n, val, base, base_t);
1587
1588 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001589}
1590
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001591BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1592 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001593{
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001594 BcStatus s = BC_STATUS_SUCCESS;
1595
Gavin Howardab5e9632019-01-02 13:32:02 -07001596 assert(n && base);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001597 assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001598
Gavin Howardab5e9632019-01-02 13:32:02 -07001599 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001600
Gavin Howardddce6d42019-01-03 14:07:06 -07001601 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
Gavin Howard530e3072019-04-23 16:23:36 -06001602 else if (base_t == BC_BASE) bc_num_printDecimal(n);
Gavin Howard7ad5a662019-02-19 14:40:46 -07001603#if BC_ENABLE_EXTRA_MATH
1604 else if (base_t == 0 || base_t == 1) bc_num_printExponent(n, base_t != 0);
1605#endif // BC_ENABLE_EXTRA_MATH
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001606 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001607
Gavin Howarde5f11c72019-02-23 09:42:51 -07001608 if (BC_NO_ERR(!s) && newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001609 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001610 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001611 }
Gavin Howarddc0ee9e2019-02-16 23:27:08 -07001612
1613 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001614}
1615
Gavin Howard3c02ed32018-12-28 18:17:15 -07001616BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001617
Gavin Howardfcf29012019-04-25 20:12:19 -06001618 // TODO: Check this function.
1619
Gavin Howard63738202018-09-26 15:34:20 -06001620 size_t i;
Gavin Howard06b6e862019-02-15 11:15:31 -07001621 unsigned long r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001622
Gavin Howard63738202018-09-26 15:34:20 -06001623 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001624
Gavin Howardfdf64bc2019-01-07 16:52:54 -07001625 *result = 0;
1626
Gavin Howardecafd4f2019-02-23 09:30:45 -07001627 if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001628
Gavin Howard06b6e862019-02-15 11:15:31 -07001629 for (r = 0, i = n->len; i > n->rdx;) {
Gavin Howard5bede412019-02-23 09:31:28 -07001630
Gavin Howard0700efc2019-04-24 17:31:23 -06001631 unsigned long prev = r * BC_BASE_DIG;
Gavin Howard5bede412019-02-23 09:31:28 -07001632
Gavin Howard0700efc2019-04-24 17:31:23 -06001633 if (BC_ERR(prev == SIZE_MAX || prev / BC_BASE_DIG != r))
Gavin Howard5bede412019-02-23 09:31:28 -07001634 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
1635
Stefan Eßer4b111c22019-04-24 17:23:08 -06001636 r = prev + n->num[--i];
Gavin Howard5bede412019-02-23 09:31:28 -07001637
1638 if (BC_ERR(r == SIZE_MAX || r < prev))
Gavin Howard5edf8492019-02-21 14:06:58 -07001639 return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001640 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001641
Gavin Howard7b557da2018-12-21 10:25:32 -07001642 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001643
Gavin Howard63738202018-09-26 15:34:20 -06001644 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001645}
1646
Gavin Howard3c02ed32018-12-28 18:17:15 -07001647void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001648
Gavin Howardfcf29012019-04-25 20:12:19 -06001649 // TODO: Check this function.
1650
Gavin Howard63738202018-09-26 15:34:20 -06001651 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001652
Gavin Howard63738202018-09-26 15:34:20 -06001653 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001654
Gavin Howard1ab22d22019-01-03 13:32:17 -07001655 if (!val) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001656
Gavin Howard798508b2019-02-21 16:45:32 -07001657 bc_num_expand(n, bc_num_log10(ULONG_MAX));
Gavin Howard05feab02019-04-23 16:24:07 -06001658
Stefan Eßer4b111c22019-04-24 17:23:08 -06001659 while (val) {
Gavin Howard0700efc2019-04-24 17:31:23 -06001660 n->num[n->len++] = val % BC_BASE_DIG;
1661 val /= BC_BASE_DIG;
Stefan Eßer4b111c22019-04-24 17:23:08 -06001662 }
Gavin Howard025d04d2018-02-20 13:53:28 -07001663}
1664
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001665size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard1973d612019-02-21 15:37:32 -07001666
1667 size_t aint, bint, ardx, brdx;
1668
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001669 BC_UNUSED(scale);
Gavin Howard1973d612019-02-21 15:37:32 -07001670
1671 ardx = a->rdx;
1672 brdx = b->rdx;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001673 aint = bc_num_int(a);
1674 bint = bc_num_int(b);
Gavin Howard2d188a52019-02-25 14:19:08 -07001675 ardx = BC_MAX(ardx, brdx);
1676 aint = BC_MAX(aint, bint);
Gavin Howard1973d612019-02-21 15:37:32 -07001677
Gavin Howard2d188a52019-02-25 14:19:08 -07001678 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001679}
1680
1681size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001682 size_t max, rdx;
1683 rdx = bc_vm_growSize(a->rdx, b->rdx);
1684 max = bc_vm_growSize(BC_MAX(scale, rdx), 1);
1685 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
1686 return rdx;
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001687}
1688
1689size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) {
1690 BC_UNUSED(scale);
Gavin Howard007afdf2019-02-23 09:45:42 -07001691 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001692}
1693
1694#if BC_ENABLE_EXTRA_MATH
1695size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) {
1696 BC_UNUSED(b);
1697 BC_UNUSED(scale);
1698 return BC_NUM_SHREQ(a);
1699}
1700#endif // BC_ENABLE_EXTRA_MATH
1701
Gavin Howard12fe7812018-09-29 03:45:18 -06001702BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001703 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001704 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001705 return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001706}
1707
Gavin Howard12fe7812018-09-29 03:45:18 -06001708BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001709 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001710 BC_UNUSED(scale);
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001711 return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001712}
1713
Gavin Howard12fe7812018-09-29 03:45:18 -06001714BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001715 return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001716}
1717
Gavin Howard12fe7812018-09-29 03:45:18 -06001718BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001719 return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001720}
1721
Gavin Howard54b946a2018-10-23 12:06:57 -06001722BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard2d188a52019-02-25 14:19:08 -07001723 size_t req = bc_num_mulReq(a, b, scale);
1724 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001725}
1726
Gavin Howard12fe7812018-09-29 03:45:18 -06001727BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6734e1a2019-02-21 17:11:52 -07001728 return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001729}
1730
Gavin Howard7d8f0382018-12-27 16:15:13 -07001731#if BC_ENABLE_EXTRA_MATH
1732BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001733 return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001734}
1735
1736BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001737 return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001738}
1739
1740BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardd18fa6d2019-01-24 13:49:35 -07001741 return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a));
Gavin Howard7d8f0382018-12-27 16:15:13 -07001742}
1743#endif // BC_ENABLE_EXTRA_MATH
1744
Gavin Howard3c02ed32018-12-28 18:17:15 -07001745BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001746
Gavin Howardfcf29012019-04-25 20:12:19 -06001747 // TODO: Check this function.
1748
Gavin Howard1cbfe242019-01-09 17:13:11 -07001749 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001750 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard14913fc2019-04-23 14:36:03 -06001751 size_t pow, len, req, digs, digs1, digs2, resrdx, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001752 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001753 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001754
Gavin Howard34edd0a2018-10-25 15:51:05 -06001755 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001756
Gavin Howardecafd4f2019-02-23 09:30:45 -07001757 if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard07fbf012019-02-19 09:25:11 -07001758
Gavin Howard007afdf2019-02-23 09:45:42 -07001759 len = bc_vm_growSize(bc_num_int(a), 1);
Gavin Howard2d188a52019-02-25 14:19:08 -07001760 req = bc_vm_growSize(BC_MAX(scale, a->rdx), len >> 1);
1761 bc_num_init(b, bc_vm_growSize(req, 1));
Gavin Howard954276b2018-05-16 01:43:58 -06001762
Gavin Howardddce6d42019-01-03 14:07:06 -07001763 if (BC_NUM_ZERO(a)) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001764 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001765 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001766 }
Gavin Howarda83a5e52019-02-21 16:51:06 -07001767 if (bc_num_isOne(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001768 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001769 bc_num_extend(b, scale);
1770 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001771 }
Gavin Howard954276b2018-05-16 01:43:58 -06001772
Gavin Howard890d0c02018-10-30 16:34:50 -06001773 scale = BC_MAX(scale, a->rdx) + 1;
Gavin Howard007afdf2019-02-23 09:45:42 -07001774 len = bc_vm_growSize(a->len, scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001775
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001776 bc_num_init(&num1, len);
1777 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001778 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001779
Gavin Howard63738202018-09-26 15:34:20 -06001780 bc_num_one(&half);
1781 half.num[0] = 5;
1782 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001783
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001784 bc_num_init(&f, len);
1785 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001786
Gavin Howard63738202018-09-26 15:34:20 -06001787 x0 = &num1;
1788 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001789
Gavin Howard63738202018-09-26 15:34:20 -06001790 bc_num_one(x0);
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001791 pow = bc_num_int(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001792
Gavin Howard53eba8b2018-10-31 15:14:37 -06001793 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001794
Gavin Howardc39fd492018-10-04 10:07:03 -06001795 if (pow & 1) x0->num[0] = 2;
1796 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001797
Gavin Howardc39fd492018-10-04 10:07:03 -06001798 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001799
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001800 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001801
Gavin Howard63738202018-09-26 15:34:20 -06001802 // Make sure to move the radix back.
1803 x0->rdx -= pow;
1804 }
Gavin Howard954276b2018-05-16 01:43:58 -06001805
Gavin Howard14913fc2019-04-23 14:36:03 -06001806 x0->rdx = digs = digs1 = digs2 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001807 resrdx = scale + 2;
Gavin Howard7b7d2f32019-02-21 16:46:47 -07001808 len = bc_num_int(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001809
Gavin Howard2d188a52019-02-25 14:19:08 -07001810 while (BC_NO_SIG && (cmp || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001811
Gavin Howard1769abc2019-02-21 09:36:14 -07001812 assert(!BC_NUM_ZERO(x0));
1813
Gavin Howard53eba8b2018-10-31 15:14:37 -06001814 s = bc_num_div(a, x0, &f, resrdx);
Gavin Howard1769abc2019-02-21 09:36:14 -07001815 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001816 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001817 s = bc_num_add(x0, &f, &fprime, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001818 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard53eba8b2018-10-31 15:14:37 -06001819 s = bc_num_mul(&fprime, &half, x1, resrdx);
Gavin Howardc78e7522019-02-22 13:24:25 -07001820 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001821
Gavin Howard53eba8b2018-10-31 15:14:37 -06001822 cmp = bc_num_cmp(x1, x0);
Gavin Howardf2ee6342019-04-09 17:05:20 -06001823 if (cmp == BC_NUM_SSIZE_MIN) {
1824 s = BC_STATUS_SIGNAL;
1825 break;
1826 }
1827
Gavin Howard53eba8b2018-10-31 15:14:37 -06001828 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001829
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001830 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001831 else times = 0;
1832
1833 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001834
1835 cmp2 = cmp1;
1836 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001837 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001838
Gavin Howard63738202018-09-26 15:34:20 -06001839 temp = x0;
1840 x0 = x1;
1841 x1 = temp;
1842 }
Gavin Howard954276b2018-05-16 01:43:58 -06001843
Gavin Howard2d188a52019-02-25 14:19:08 -07001844 if (BC_SIG) {
Gavin Howard176cfe62019-02-16 23:40:13 -07001845 s = BC_STATUS_SIGNAL;
1846 goto err;
1847 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001848
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001849 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001850 scale -= 1;
1851 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001852
1853err:
Gavin Howardcf0748d2019-04-09 14:51:47 -06001854 if (BC_ERR(s)) bc_num_free(b);
Gavin Howard63738202018-09-26 15:34:20 -06001855 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001856 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001857 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001858 bc_num_free(&num1);
Gavin Howardddce6d42019-01-03 14:07:06 -07001859 assert(!b->neg || BC_NUM_NONZERO(b));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001860 assert(b->rdx <= b->len || !b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001861 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001862}
Gavin Howardba009802018-09-29 04:41:51 -06001863
Gavin Howard2cb39612018-10-22 08:46:48 -06001864BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1865
Gavin Howardfcf29012019-04-25 20:12:19 -06001866 // TODO: Check this function.
1867
Gavin Howard2cb39612018-10-22 08:46:48 -06001868 BcStatus s;
1869 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001870 bool init = false;
Gavin Howard620a8af2019-01-24 13:54:54 -07001871 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001872
Gavin Howardf2ef2f32019-01-16 11:25:16 -07001873 assert(c != d && a != d && b != d && b != c);
Gavin Howard2cb39612018-10-22 08:46:48 -06001874
Gavin Howard890d0c02018-10-30 16:34:50 -06001875 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001876 memcpy(&num2, c, sizeof(BcNum));
1877 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001878 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001879 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001880 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001881 else {
1882 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001883 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001884 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001885
Gavin Howard54b946a2018-10-23 12:06:57 -06001886 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001887
Gavin Howardddce6d42019-01-03 14:07:06 -07001888 assert(!c->neg || BC_NUM_NONZERO(c));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001889 assert(c->rdx <= c->len || !c->len);
Gavin Howardddce6d42019-01-03 14:07:06 -07001890 assert(!d->neg || BC_NUM_NONZERO(d));
Gavin Howardc4bf4c42019-01-09 10:30:15 -07001891 assert(d->rdx <= d->len || !d->len);
Gavin Howard2cb39612018-10-22 08:46:48 -06001892
1893 if (init) bc_num_free(&num2);
1894
1895 return s;
1896}
1897
Gavin Howard40a085f2018-12-03 12:08:59 -07001898#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001899BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001900
Gavin Howardfcf29012019-04-25 20:12:19 -06001901 // TODO: Check this function.
1902
Gavin Howardba009802018-09-29 04:41:51 -06001903 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001904 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001905 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001906
Gavin Howard34edd0a2018-10-25 15:51:05 -06001907 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001908
Gavin Howardecafd4f2019-02-23 09:30:45 -07001909 if (BC_ERR(BC_NUM_ZERO(c)))
1910 return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1911 if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1912 if (BC_ERR(a->rdx || b->rdx || c->rdx))
Gavin Howard7536dcf2018-12-15 19:27:09 -07001913 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001914
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001915 bc_num_expand(d, c->len);
1916 bc_num_init(&base, c->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001917 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001918 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001919
1920 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001921 two.num[0] = 2;
1922 bc_num_one(d);
1923
Gavin Howard1769abc2019-02-21 09:36:14 -07001924 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001925 s = bc_num_rem(a, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001926 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001927 if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err;
Gavin Howardbaa4f582019-01-24 13:56:35 -07001928 bc_num_createCopy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001929
Gavin Howard2d188a52019-02-25 14:19:08 -07001930 while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) {
Gavin Howardba009802018-09-29 04:41:51 -06001931
Gavin Howard1769abc2019-02-21 09:36:14 -07001932 // Num two cannot be 0, so no errors.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001933 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001934 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001935
Gavin Howarda83a5e52019-02-21 16:51:06 -07001936 if (bc_num_isOne(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001937 s = bc_num_mul(d, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001938 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001939
1940 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001941 s = bc_num_rem(&temp, c, d, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001942 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001943 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001944 }
1945
Gavin Howard53eba8b2018-10-31 15:14:37 -06001946 s = bc_num_mul(&base, &base, &temp, 0);
Gavin Howardc78e7522019-02-22 13:24:25 -07001947 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howard1769abc2019-02-21 09:36:14 -07001948
1949 // We already checked for 0.
Gavin Howard53eba8b2018-10-31 15:14:37 -06001950 s = bc_num_rem(&temp, c, &base, 0);
Gavin Howard1769abc2019-02-21 09:36:14 -07001951 assert(!s || s == BC_STATUS_SIGNAL);
Gavin Howardc78e7522019-02-22 13:24:25 -07001952 if (BC_ERROR_SIGNAL_ONLY(s)) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001953 }
1954
Gavin Howard2d188a52019-02-25 14:19:08 -07001955 if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL;
Gavin Howardfa4983a2019-02-16 23:43:03 -07001956
Gavin Howardba009802018-09-29 04:41:51 -06001957err:
Gavin Howardba009802018-09-29 04:41:51 -06001958 bc_num_free(&exp);
Gavin Howardd88164d2019-02-21 09:57:31 -07001959rem_err:
1960 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001961 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001962 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001963 return s;
1964}
Gavin Howarde6e84762018-10-03 11:46:34 -06001965#endif // DC_ENABLED