blob: 126fe91f7ac8bd99f02ae7eb40485d93b3b13cce [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 Howardb5904bf2018-02-20 13:28:18 -07004 * Copyright 2018 Gavin D. Howard
Gavin Howard3eb626f2018-02-14 13:54:35 -07005 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 *
Gavin Howardb5904bf2018-02-20 13:28:18 -070017 * *****************************************************************************
Gavin Howard3eb626f2018-02-14 13:54:35 -070018 *
19 * Code for the number type.
20 *
21 */
22
23#include <assert.h>
Gavin Howardc7d655b2018-12-28 19:45:13 -070024#include <ctype.h>
Gavin Howard411f7322018-09-26 17:21:19 -060025#include <stdbool.h>
26#include <stdlib.h>
27#include <string.h>
Gavin Howard8e2cc692018-02-15 17:39:14 -070028
Gavin Howard305249a2018-10-15 20:24:47 -060029#include <limits.h>
30
Gavin Howard29493062018-03-20 19:57:37 -060031#include <status.h>
Gavin Howard3ba6c8d2018-02-15 12:23:35 -070032#include <num.h>
Gavin Howardd5551672018-09-22 19:52:42 -060033#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070034
Gavin Howard3c02ed32018-12-28 18:17:15 -070035void bc_num_setToZero(BcNum *restrict n, size_t scale) {
Gavin Howarda2514a02018-10-18 14:20:09 -060036 assert(n);
Gavin Howard97102b12018-11-01 23:37:31 -060037 n->len = 0;
38 n->neg = false;
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060039 n->rdx = scale;
40}
41
Gavin Howard3c02ed32018-12-28 18:17:15 -070042void bc_num_zero(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060043 bc_num_setToZero(n, 0);
Gavin Howard0be26ed2018-08-31 20:21:56 -060044}
45
Gavin Howard3c02ed32018-12-28 18:17:15 -070046void bc_num_one(BcNum *restrict n) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060047 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060048 n->len = 1;
49 n->num[0] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060050}
51
Gavin Howard3c02ed32018-12-28 18:17:15 -070052void bc_num_ten(BcNum *restrict n) {
Gavin Howarda2514a02018-10-18 14:20:09 -060053 assert(n);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -060054 bc_num_setToZero(n, 0);
Gavin Howard63738202018-09-26 15:34:20 -060055 n->len = 2;
56 n->num[0] = 0;
57 n->num[1] = 1;
Gavin Howard0be26ed2018-08-31 20:21:56 -060058}
59
Gavin Howard3c02ed32018-12-28 18:17:15 -070060BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b, size_t len)
61{
Gavin Howard63738202018-09-26 15:34:20 -060062 size_t i, j;
Gavin Howard112304b2018-12-04 14:30:01 -070063 for (i = 0; !BC_SIGINT && i < len; ++i) {
64 for (a[i] -= b[i], j = 0; !BC_SIGINT && a[i + j] < 0;) {
Gavin Howard54b946a2018-10-23 12:06:57 -060065 a[i + j++] += 10;
66 a[i + j] -= 1;
Gavin Howard63738202018-09-26 15:34:20 -060067 }
68 }
Gavin Howard43df94a2018-12-22 10:29:41 -070069 return BC_SIGINT ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060070}
71
Gavin Howard3c02ed32018-12-28 18:17:15 -070072ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
73 size_t len)
74{
Gavin Howard63738202018-09-26 15:34:20 -060075 size_t i;
Gavin Howardd1a78362018-10-18 09:52:06 -060076 int c = 0;
Gavin Howard112304b2018-12-04 14:30:01 -070077 for (i = len - 1; !BC_SIGINT && i < len && !(c = a[i] - b[i]); --i);
Gavin Howard7fbb4e22018-10-18 11:43:17 -060078 return BC_NUM_NEG(i + 1, c < 0);
Gavin Howard08bf5292018-03-20 14:59:33 -060079}
80
Gavin Howard3c02ed32018-12-28 18:17:15 -070081ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070082
Gavin Howard63738202018-09-26 15:34:20 -060083 size_t i, min, a_int, b_int, diff;
Gavin Howard8a921bd2018-10-11 14:15:32 -060084 BcDig *max_num, *min_num;
Gavin Howard7fbb4e22018-10-18 11:43:17 -060085 bool a_max, neg = false;
86 ssize_t cmp;
87
88 assert(a && b);
Gavin Howard4681d1b2018-03-05 19:49:33 -070089
Gavin Howard8a921bd2018-10-11 14:15:32 -060090 if (a == b) return 0;
Gavin Howard53eba8b2018-10-31 15:14:37 -060091 if (a->len == 0) return BC_NUM_NEG(!!b->len, !b->neg);
Gavin Howardbf282592019-01-02 11:06:14 -070092 if (b->len == 0) return BC_NUM_CMP_ZERO(a);
Gavin Howard890d0c02018-10-30 16:34:50 -060093 if (a->neg) {
Gavin Howard7fbb4e22018-10-18 11:43:17 -060094 if (b->neg) neg = true;
Gavin Howard63738202018-09-26 15:34:20 -060095 else return -1;
96 }
97 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070098
Gavin Howard63738202018-09-26 15:34:20 -060099 a_int = BC_NUM_INT(a);
100 b_int = BC_NUM_INT(b);
Gavin Howard890d0c02018-10-30 16:34:50 -0600101 a_int -= b_int;
102 a_max = (a->rdx > b->rdx);
Gavin Howard4681d1b2018-03-05 19:49:33 -0700103
Gavin Howard53eba8b2018-10-31 15:14:37 -0600104 if (a_int != 0) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700105
Gavin Howard890d0c02018-10-30 16:34:50 -0600106 if (a_max) {
Gavin Howard63738202018-09-26 15:34:20 -0600107 min = b->rdx;
108 diff = a->rdx - b->rdx;
109 max_num = a->num + diff;
110 min_num = b->num;
111 }
112 else {
113 min = a->rdx;
114 diff = b->rdx - a->rdx;
115 max_num = b->num + diff;
116 min_num = a->num;
117 }
Gavin Howard4681d1b2018-03-05 19:49:33 -0700118
Gavin Howard890d0c02018-10-30 16:34:50 -0600119 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600120 if (cmp != 0) return BC_NUM_NEG(cmp, (!a_max) != neg);
Gavin Howard021150b2018-03-10 15:40:42 -0700121
Gavin Howard112304b2018-12-04 14:30:01 -0700122 for (max_num -= diff, i = diff - 1; !BC_SIGINT && i < diff; --i) {
Gavin Howard40974152018-10-18 14:23:01 -0600123 if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg);
Gavin Howard63738202018-09-26 15:34:20 -0600124 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700125
Gavin Howard63738202018-09-26 15:34:20 -0600126 return 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700127}
128
Gavin Howard3c02ed32018-12-28 18:17:15 -0700129void bc_num_truncate(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700130
Gavin Howard53eba8b2018-10-31 15:14:37 -0600131 assert(places <= n->rdx && (n->len == 0 || places <= n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700132
Gavin Howard53eba8b2018-10-31 15:14:37 -0600133 if (places == 0) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700134
Gavin Howard63738202018-09-26 15:34:20 -0600135 n->rdx -= places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700136
Gavin Howard53eba8b2018-10-31 15:14:37 -0600137 if (n->len != 0) {
Gavin Howard955da852018-10-23 11:08:20 -0600138 n->len -= places;
139 memmove(n->num, n->num + places, n->len * sizeof(BcDig));
140 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700141}
142
Gavin Howard3c02ed32018-12-28 18:17:15 -0700143void bc_num_extend(BcNum *restrict n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700144
Gavin Howard88c25302018-10-17 13:32:23 -0600145 size_t len = n->len + places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700146
Gavin Howard53eba8b2018-10-31 15:14:37 -0600147 if (places != 0) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700148
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600149 if (n->cap < len) bc_num_expand(n, len);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700150
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600151 memmove(n->num + places, n->num, sizeof(BcDig) * n->len);
152 memset(n->num, 0, sizeof(BcDig) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700153
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600154 n->len += places;
155 n->rdx += places;
156 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700157}
158
Gavin Howard3c02ed32018-12-28 18:17:15 -0700159void bc_num_clean(BcNum *restrict n) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600160 while (n->len > 0 && n->num[n->len - 1] == 0) --n->len;
161 if (n->len == 0) n->neg = false;
Gavin Howard63738202018-09-26 15:34:20 -0600162 else if (n->len < n->rdx) n->len = n->rdx;
Gavin Howarde57525d2018-09-01 16:56:21 -0600163}
164
Gavin Howard3c02ed32018-12-28 18:17:15 -0700165void bc_num_retireMul(BcNum *restrict n, size_t scale, bool neg1, bool neg2) {
Gavin Howard337fe602018-09-01 15:10:59 -0600166
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600167 if (n->rdx < scale) bc_num_extend(n, scale - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600168 else bc_num_truncate(n, n->rdx - scale);
Gavin Howard337fe602018-09-01 15:10:59 -0600169
Gavin Howard305249a2018-10-15 20:24:47 -0600170 bc_num_clean(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600171 if (n->len != 0) n->neg = !neg1 != !neg2;
Gavin Howard305249a2018-10-15 20:24:47 -0600172}
173
Gavin Howard96664f22018-11-17 14:37:13 -0700174void bc_num_split(BcNum *restrict n, size_t idx, BcNum *restrict a,
175 BcNum *restrict b)
176{
Gavin Howard305249a2018-10-15 20:24:47 -0600177 if (idx < n->len) {
178
Gavin Howard305249a2018-10-15 20:24:47 -0600179 b->len = n->len - idx;
180 a->len = idx;
Gavin Howard88c25302018-10-17 13:32:23 -0600181 a->rdx = b->rdx = 0;
Gavin Howard305249a2018-10-15 20:24:47 -0600182
183 memcpy(b->num, n->num + idx, b->len * sizeof(BcDig));
184 memcpy(a->num, n->num, idx * sizeof(BcDig));
Gavin Howard305249a2018-10-15 20:24:47 -0600185 }
186 else {
187 bc_num_zero(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600188 bc_num_copy(a, n);
Gavin Howard305249a2018-10-15 20:24:47 -0600189 }
190
191 bc_num_clean(a);
192 bc_num_clean(b);
Gavin Howard305249a2018-10-15 20:24:47 -0600193}
194
Gavin Howard3c02ed32018-12-28 18:17:15 -0700195BcStatus bc_num_shift(BcNum *restrict n, size_t places) {
Gavin Howard305249a2018-10-15 20:24:47 -0600196
Gavin Howard53eba8b2018-10-31 15:14:37 -0600197 if (places == 0 || n->len == 0) return BC_STATUS_SUCCESS;
Gavin Howardecc7fc22018-12-31 13:21:47 -0700198 if (places + n->len > BC_MAX_NUM)
199 return bc_vm_verr(BC_ERROR_EXEC_NUM_LEN, BC_MAX_NUM);
Gavin Howard305249a2018-10-15 20:24:47 -0600200
201 if (n->rdx >= places) n->rdx -= places;
202 else {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600203 bc_num_extend(n, places - n->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600204 n->rdx = 0;
205 }
206
207 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600208
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600209 return BC_STATUS_SUCCESS;
Gavin Howard337fe602018-09-01 15:10:59 -0600210}
211
Gavin Howard773c86b2018-11-02 14:07:19 -0600212BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600213
Gavin Howard63738202018-09-26 15:34:20 -0600214 BcNum one;
Gavin Howardd32d7df2018-10-15 08:42:25 -0600215 BcDig num[2];
Gavin Howard9c4358c2018-03-22 20:11:28 -0600216
Gavin Howardd32d7df2018-10-15 08:42:25 -0600217 one.cap = 2;
218 one.num = num;
Gavin Howard63738202018-09-26 15:34:20 -0600219 bc_num_one(&one);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600220
Gavin Howardd32d7df2018-10-15 08:42:25 -0600221 return bc_num_div(&one, a, b, scale);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600222}
223
Gavin Howard7bda4782018-12-28 09:53:22 -0700224#if BC_ENABLE_EXTRA_MATH
Gavin Howard3c02ed32018-12-28 18:17:15 -0700225BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
226 unsigned long *v)
227{
Gavin Howardac4d9122018-12-27 23:59:12 -0700228 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
229 bc_num_copy(c, a);
Gavin Howard7bda4782018-12-28 09:53:22 -0700230 return bc_num_ulong(b, v);
Gavin Howardac4d9122018-12-27 23:59:12 -0700231}
Gavin Howard7bda4782018-12-28 09:53:22 -0700232#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700233
Gavin Howard773c86b2018-11-02 14:07:19 -0600234BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700235
Gavin Howard8a921bd2018-10-11 14:15:32 -0600236 BcDig *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard63738202018-09-26 15:34:20 -0600237 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700238 unsigned int carry, in;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700239
Gavin Howard63738202018-09-26 15:34:20 -0600240 // Because this function doesn't need to use scale (per the bc spec),
241 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard73cce1a2018-09-06 15:16:09 -0600242
Gavin Howard53eba8b2018-10-31 15:14:37 -0600243 if (a->len == 0) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600244 bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600245 if (sub && c->len) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600246 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600247 }
Gavin Howard53eba8b2018-10-31 15:14:37 -0600248 else if (b->len == 0) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600249 bc_num_copy(c, a);
250 return BC_STATUS_SUCCESS;
251 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600252
Gavin Howard63738202018-09-26 15:34:20 -0600253 c->neg = a->neg;
Gavin Howard63738202018-09-26 15:34:20 -0600254 c->rdx = BC_MAX(a->rdx, b->rdx);
255 min_rdx = BC_MIN(a->rdx, b->rdx);
256 c->len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700257
Gavin Howard63738202018-09-26 15:34:20 -0600258 if (a->rdx > b->rdx) {
259 diff = a->rdx - b->rdx;
260 ptr = a->num;
261 ptr_a = a->num + diff;
262 ptr_b = b->num;
263 }
264 else {
265 diff = b->rdx - a->rdx;
266 ptr = b->num;
267 ptr_a = a->num;
268 ptr_b = b->num + diff;
269 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700270
Gavin Howard63738202018-09-26 15:34:20 -0600271 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700272
Gavin Howard63738202018-09-26 15:34:20 -0600273 ptr_c += diff;
Gavin Howard890d0c02018-10-30 16:34:50 -0600274 a_int = BC_NUM_INT(a);
275 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700276
Gavin Howard890d0c02018-10-30 16:34:50 -0600277 if (a_int > b_int) {
Gavin Howard63738202018-09-26 15:34:20 -0600278 min_int = b_int;
279 max = a_int;
280 ptr = ptr_a;
281 }
282 else {
283 min_int = a_int;
284 max = b_int;
285 ptr = ptr_b;
286 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700287
Gavin Howard112304b2018-12-04 14:30:01 -0700288 for (carry = 0, i = 0; !BC_SIGINT && i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700289 in = ((unsigned int) ptr_a[i]) + ((unsigned int) ptr_b[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600290 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600291 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600292 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700293
Gavin Howard112304b2018-12-04 14:30:01 -0700294 for (; !BC_SIGINT && i < max + min_rdx; ++i, ++c->len) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700295 in = ((unsigned int) ptr[i]) + carry;
Gavin Howard60136d72018-10-15 14:28:19 -0600296 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600297 ptr_c[i] = (BcDig) (in % 10);
Gavin Howard63738202018-09-26 15:34:20 -0600298 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700299
Gavin Howard53eba8b2018-10-31 15:14:37 -0600300 if (carry != 0) c->num[c->len++] = (BcDig) carry;
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600301
Gavin Howard43df94a2018-12-22 10:29:41 -0700302 return BC_SIGINT ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700303}
304
Gavin Howard773c86b2018-11-02 14:07:19 -0600305BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700306
Gavin Howard63738202018-09-26 15:34:20 -0600307 BcStatus s;
308 ssize_t cmp;
309 BcNum *minuend, *subtrahend;
310 size_t start;
311 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700312
Gavin Howard63738202018-09-26 15:34:20 -0600313 // Because this function doesn't need to use scale (per the bc spec),
314 // I am hijacking it to say whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700315
Gavin Howard53eba8b2018-10-31 15:14:37 -0600316 if (a->len == 0) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600317 bc_num_copy(c, b);
Gavin Howard38a10a12018-10-11 11:52:29 -0600318 if (sub && c->len) c->neg = !c->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600319 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600320 }
Gavin Howard53eba8b2018-10-31 15:14:37 -0600321 else if (b->len == 0) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600322 bc_num_copy(c, a);
323 return BC_STATUS_SUCCESS;
324 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700325
Gavin Howard63738202018-09-26 15:34:20 -0600326 aneg = a->neg;
327 bneg = b->neg;
328 a->neg = b->neg = false;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700329
Gavin Howard63738202018-09-26 15:34:20 -0600330 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700331
Gavin Howard63738202018-09-26 15:34:20 -0600332 a->neg = aneg;
333 b->neg = bneg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700334
Gavin Howard53eba8b2018-10-31 15:14:37 -0600335 if (cmp == 0) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600336 bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx));
Gavin Howard63738202018-09-26 15:34:20 -0600337 return BC_STATUS_SUCCESS;
338 }
339 else if (cmp > 0) {
340 neg = a->neg;
341 minuend = a;
342 subtrahend = b;
343 }
344 else {
345 neg = b->neg;
346 if (sub) neg = !neg;
347 minuend = b;
348 subtrahend = a;
349 }
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700350
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600351 bc_num_copy(c, minuend);
Gavin Howard63738202018-09-26 15:34:20 -0600352 c->neg = neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700353
Gavin Howard63738202018-09-26 15:34:20 -0600354 if (c->rdx < subtrahend->rdx) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600355 bc_num_extend(c, subtrahend->rdx - c->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600356 start = 0;
357 }
358 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700359
Gavin Howard63738202018-09-26 15:34:20 -0600360 s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700361
Gavin Howard63738202018-09-26 15:34:20 -0600362 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700363
Gavin Howard63738202018-09-26 15:34:20 -0600364 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700365}
366
Gavin Howard3c02ed32018-12-28 18:17:15 -0700367BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
Gavin Howard773c86b2018-11-02 14:07:19 -0600368
Gavin Howard63738202018-09-26 15:34:20 -0600369 BcStatus s;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700370 size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2;
Gavin Howard305249a2018-10-15 20:24:47 -0600371 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp;
Gavin Howard63738202018-09-26 15:34:20 -0600372 bool aone = BC_NUM_ONE(a);
Gavin Howard337fe602018-09-01 15:10:59 -0600373
Gavin Howard43df94a2018-12-22 10:29:41 -0700374 if (BC_SIGINT) return BC_STATUS_SIGNAL;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600375 if (a->len == 0 || b->len == 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600376 bc_num_zero(c);
377 return BC_STATUS_SUCCESS;
378 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600379 else if (aone || BC_NUM_ONE(b)) {
380 bc_num_copy(c, aone ? b : a);
381 return BC_STATUS_SUCCESS;
382 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700383
Gavin Howardb5093d72018-10-17 12:13:17 -0600384 if (a->len + b->len < BC_NUM_KARATSUBA_LEN ||
385 a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN)
Gavin Howard305249a2018-10-15 20:24:47 -0600386 {
Gavin Howard34c4ba02018-12-11 10:25:01 -0700387 size_t i, j, len;
Gavin Howard2aef61b2018-12-18 10:56:20 -0700388 unsigned int carry;
Gavin Howard34c4ba02018-12-11 10:25:01 -0700389
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600390 bc_num_expand(c, a->len + b->len + 1);
Gavin Howard23d8d252018-10-17 12:03:41 -0600391
Gavin Howard305249a2018-10-15 20:24:47 -0600392 memset(c->num, 0, sizeof(BcDig) * c->cap);
Gavin Howard34c4ba02018-12-11 10:25:01 -0700393 c->len = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700394
Gavin Howard112304b2018-12-04 14:30:01 -0700395 for (i = 0; !BC_SIGINT && i < b->len; ++i) {
Gavin Howardd1a78362018-10-18 09:52:06 -0600396
Gavin Howard34c4ba02018-12-11 10:25:01 -0700397 carry = 0;
398
Gavin Howard112304b2018-12-04 14:30:01 -0700399 for (j = 0; !BC_SIGINT && j < a->len; ++j) {
Gavin Howard2aef61b2018-12-18 10:56:20 -0700400 unsigned int in = c->num[i + j];
401 in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]);
402 in += carry;
Gavin Howard305249a2018-10-15 20:24:47 -0600403 carry = in / 10;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600404 c->num[i + j] = (BcDig) (in % 10);
Gavin Howard305249a2018-10-15 20:24:47 -0600405 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700406
Gavin Howard88c25302018-10-17 13:32:23 -0600407 c->num[i + j] += (BcDig) carry;
408 len = BC_MAX(len, i + j + !!carry);
Gavin Howard63738202018-09-26 15:34:20 -0600409 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700410
Gavin Howard305249a2018-10-15 20:24:47 -0600411 c->len = len;
Gavin Howard23d8d252018-10-17 12:03:41 -0600412
Gavin Howard43df94a2018-12-22 10:29:41 -0700413 return BC_SIGINT ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600414 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700415
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600416 bc_num_init(&l1, max);
417 bc_num_init(&h1, max);
418 bc_num_init(&l2, max);
419 bc_num_init(&h2, max);
420 bc_num_init(&m1, max);
421 bc_num_init(&m2, max);
422 bc_num_init(&z0, max);
423 bc_num_init(&z1, max);
424 bc_num_init(&z2, max);
425 bc_num_init(&temp, max + max);
Gavin Howard0dfe2922018-05-22 13:57:02 -0600426
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600427 bc_num_split(a, max2, &l1, &h1);
428 bc_num_split(b, max2, &l2, &h2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700429
Gavin Howard53eba8b2018-10-31 15:14:37 -0600430 s = bc_num_add(&h1, &l1, &m1, 0);
431 if (s) goto err;
432 s = bc_num_add(&h2, &l2, &m2, 0);
433 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600434
Gavin Howard53eba8b2018-10-31 15:14:37 -0600435 s = bc_num_k(&h1, &h2, &z0);
436 if (s) goto err;
437 s = bc_num_k(&m1, &m2, &z1);
438 if (s) goto err;
439 s = bc_num_k(&l1, &l2, &z2);
440 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600441
Gavin Howard53eba8b2018-10-31 15:14:37 -0600442 s = bc_num_sub(&z1, &z0, &temp, 0);
443 if (s) goto err;
444 s = bc_num_sub(&temp, &z2, &z1, 0);
445 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600446
Gavin Howard53eba8b2018-10-31 15:14:37 -0600447 s = bc_num_shift(&z0, max2 * 2);
448 if (s) goto err;
449 s = bc_num_shift(&z1, max2);
450 if (s) goto err;
451 s = bc_num_add(&z0, &z1, &temp, 0);
452 if (s) goto err;
Gavin Howardb5093d72018-10-17 12:13:17 -0600453 s = bc_num_add(&temp, &z2, c, 0);
Gavin Howard305249a2018-10-15 20:24:47 -0600454
455err:
456 bc_num_free(&temp);
Gavin Howard305249a2018-10-15 20:24:47 -0600457 bc_num_free(&z2);
Gavin Howard305249a2018-10-15 20:24:47 -0600458 bc_num_free(&z1);
Gavin Howard305249a2018-10-15 20:24:47 -0600459 bc_num_free(&z0);
Gavin Howard305249a2018-10-15 20:24:47 -0600460 bc_num_free(&m2);
Gavin Howard305249a2018-10-15 20:24:47 -0600461 bc_num_free(&m1);
Gavin Howard305249a2018-10-15 20:24:47 -0600462 bc_num_free(&h2);
Gavin Howard305249a2018-10-15 20:24:47 -0600463 bc_num_free(&l2);
Gavin Howard305249a2018-10-15 20:24:47 -0600464 bc_num_free(&h1);
Gavin Howard305249a2018-10-15 20:24:47 -0600465 bc_num_free(&l1);
466 return s;
467}
468
Gavin Howard773c86b2018-11-02 14:07:19 -0600469BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard305249a2018-10-15 20:24:47 -0600470
471 BcStatus s;
472 BcNum cpa, cpb;
Gavin Howard88c25302018-10-17 13:32:23 -0600473 size_t maxrdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard305249a2018-10-15 20:24:47 -0600474
475 scale = BC_MAX(scale, a->rdx);
476 scale = BC_MAX(scale, b->rdx);
477 scale = BC_MIN(a->rdx + b->rdx, scale);
478 maxrdx = BC_MAX(maxrdx, scale);
479
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600480 bc_num_init(&cpa, a->len);
481 bc_num_init(&cpb, b->len);
Gavin Howard305249a2018-10-15 20:24:47 -0600482
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600483 bc_num_copy(&cpa, a);
484 bc_num_copy(&cpb, b);
Gavin Howard23d8d252018-10-17 12:03:41 -0600485 cpa.neg = cpb.neg = false;
Gavin Howard305249a2018-10-15 20:24:47 -0600486
Gavin Howard53eba8b2018-10-31 15:14:37 -0600487 s = bc_num_shift(&cpa, maxrdx);
488 if (s) goto err;
489 s = bc_num_shift(&cpb, maxrdx);
490 if (s) goto err;
491 s = bc_num_k(&cpa, &cpb, c);
492 if (s) goto err;
Gavin Howard305249a2018-10-15 20:24:47 -0600493
Gavin Howard53eba8b2018-10-31 15:14:37 -0600494 maxrdx += scale;
495 bc_num_expand(c, c->len + maxrdx);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600496
Gavin Howard88c25302018-10-17 13:32:23 -0600497 if (c->len < maxrdx) {
Gavin Howard305249a2018-10-15 20:24:47 -0600498 memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig));
Gavin Howard88c25302018-10-17 13:32:23 -0600499 c->len += maxrdx;
Gavin Howard305249a2018-10-15 20:24:47 -0600500 }
501
Gavin Howard88c25302018-10-17 13:32:23 -0600502 c->rdx = maxrdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600503 bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard305249a2018-10-15 20:24:47 -0600504
505err:
506 bc_num_free(&cpb);
Gavin Howard305249a2018-10-15 20:24:47 -0600507 bc_num_free(&cpa);
508 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700509}
510
Gavin Howard773c86b2018-11-02 14:07:19 -0600511BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700512
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600513 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600514 BcDig *n, *p, q;
Gavin Howard63738202018-09-26 15:34:20 -0600515 size_t len, end, i;
516 BcNum cp;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600517 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700518
Gavin Howard7536dcf2018-12-15 19:27:09 -0700519 if (b->len == 0) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600520 else if (a->len == 0) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600521 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600522 return BC_STATUS_SUCCESS;
523 }
524 else if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600525 bc_num_copy(c, a);
526 bc_num_retireMul(c, scale, a->neg, b->neg);
527 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600528 }
Gavin Howardb651f1a2018-02-28 17:34:18 -0700529
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600530 bc_num_init(&cp, BC_NUM_MREQ(a, b, scale));
531 bc_num_copy(&cp, a);
Gavin Howard890d0c02018-10-30 16:34:50 -0600532 len = b->len;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700533
Gavin Howard890d0c02018-10-30 16:34:50 -0600534 if (len > cp.len) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600535 bc_num_expand(&cp, len + 2);
536 bc_num_extend(&cp, len - cp.len);
Gavin Howard63738202018-09-26 15:34:20 -0600537 }
Gavin Howard021150b2018-03-10 15:40:42 -0700538
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600539 if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600540 cp.rdx -= b->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600541 if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700542
Gavin Howard63738202018-09-26 15:34:20 -0600543 if (b->rdx == b->len) {
Gavin Howard63738202018-09-26 15:34:20 -0600544 for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard34edd0a2018-10-25 15:51:05 -0600545 assert(i != len || !zero);
Gavin Howard63738202018-09-26 15:34:20 -0600546 len -= i - 1;
547 }
Gavin Howardc1a6a342018-03-05 12:10:14 -0700548
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600549 if (cp.cap == cp.len) bc_num_expand(&cp, cp.len + 1);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700550
Gavin Howard63738202018-09-26 15:34:20 -0600551 // We want an extra zero in front to make things simpler.
552 cp.num[cp.len++] = 0;
553 end = cp.len - len;
Gavin Howardac7656d2018-03-01 17:18:40 -0700554
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600555 bc_num_expand(c, cp.len);
Gavin Howardac7656d2018-03-01 17:18:40 -0700556
Gavin Howard63738202018-09-26 15:34:20 -0600557 bc_num_zero(c);
Gavin Howarda2514a02018-10-18 14:20:09 -0600558 memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig));
Gavin Howard63738202018-09-26 15:34:20 -0600559 c->rdx = cp.rdx;
560 c->len = cp.len;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600561 p = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700562
Gavin Howard112304b2018-12-04 14:30:01 -0700563 for (i = end - 1; !BC_SIGINT && !s && i < end; --i) {
Gavin Howard63738202018-09-26 15:34:20 -0600564 n = cp.num + i;
Gavin Howard8a2dedf2019-01-02 13:43:47 -0700565 for (q = 0; !s && (n[len] != 0 || bc_num_compare(n, p, len) >= 0); ++q)
Gavin Howard53eba8b2018-10-31 15:14:37 -0600566 s = bc_num_subArrays(n, p, len);
Gavin Howard63738202018-09-26 15:34:20 -0600567 c->num[i] = q;
568 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700569
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600570 if (!s) bc_num_retireMul(c, scale, a->neg, b->neg);
Gavin Howard63738202018-09-26 15:34:20 -0600571 bc_num_free(&cp);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600572
Gavin Howard63738202018-09-26 15:34:20 -0600573 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700574}
575
Gavin Howard773c86b2018-11-02 14:07:19 -0600576BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
577 BcNum *restrict d, size_t scale, size_t ts)
Gavin Howard18441082018-10-22 10:03:30 -0600578{
Gavin Howard63738202018-09-26 15:34:20 -0600579 BcStatus s;
Gavin Howard18441082018-10-22 10:03:30 -0600580 BcNum temp;
581 bool neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700582
Gavin Howard7536dcf2018-12-15 19:27:09 -0700583 if (b->len == 0) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
Gavin Howard8b254872018-03-14 01:13:35 -0600584
Gavin Howard53eba8b2018-10-31 15:14:37 -0600585 if (a->len == 0) {
Gavin Howard99ca4992019-01-02 13:48:49 -0700586 bc_num_setToZero(c, ts);
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600587 bc_num_setToZero(d, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600588 return BC_STATUS_SUCCESS;
589 }
Gavin Howard8b254872018-03-14 01:13:35 -0600590
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600591 bc_num_init(&temp, d->cap);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600592 bc_num_d(a, b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700593
Gavin Howard53eba8b2018-10-31 15:14:37 -0600594 if (scale != 0) scale = ts;
Gavin Howard3115c012018-10-04 10:53:56 -0600595
Gavin Howard53eba8b2018-10-31 15:14:37 -0600596 s = bc_num_m(c, b, &temp, scale);
597 if (s) goto err;
598 s = bc_num_sub(a, &temp, d, scale);
599 if (s) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600600
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600601 if (ts > d->rdx && d->len) bc_num_extend(d, ts - d->rdx);
Gavin Howard18441082018-10-22 10:03:30 -0600602
603 neg = d->neg;
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600604 bc_num_retireMul(d, ts, a->neg, b->neg);
Gavin Howard18441082018-10-22 10:03:30 -0600605 d->neg = neg;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700606
607err:
Gavin Howard18441082018-10-22 10:03:30 -0600608 bc_num_free(&temp);
609 return s;
610}
611
Gavin Howard773c86b2018-11-02 14:07:19 -0600612BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
613
Gavin Howard18441082018-10-22 10:03:30 -0600614 BcStatus s;
615 BcNum c1;
616 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
617
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600618 bc_num_init(&c1, len);
Gavin Howard54b946a2018-10-23 12:06:57 -0600619 s = bc_num_r(a, b, &c1, c, scale, ts);
Gavin Howard63738202018-09-26 15:34:20 -0600620 bc_num_free(&c1);
Gavin Howardd64ce7b2018-10-24 16:20:20 -0600621
Gavin Howard63738202018-09-26 15:34:20 -0600622 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700623}
624
Gavin Howard773c86b2018-11-02 14:07:19 -0600625BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700626
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600627 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600628 BcNum copy;
629 unsigned long pow;
630 size_t i, powrdx, resrdx;
631 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700632
Gavin Howardac4d9122018-12-27 23:59:12 -0700633 if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700634
Gavin Howard53eba8b2018-10-31 15:14:37 -0600635 if (b->len == 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600636 bc_num_one(c);
Gavin Howard61abdbe2018-10-22 13:05:38 -0600637 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -0600638 }
Gavin Howard53eba8b2018-10-31 15:14:37 -0600639 else if (a->len == 0) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600640 bc_num_setToZero(c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600641 return BC_STATUS_SUCCESS;
642 }
643 else if (BC_NUM_ONE(b)) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600644 if (!b->neg) bc_num_copy(c, a);
Gavin Howard63738202018-09-26 15:34:20 -0600645 else s = bc_num_inv(a, c, scale);
Gavin Howard63738202018-09-26 15:34:20 -0600646 return s;
647 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700648
Gavin Howard63738202018-09-26 15:34:20 -0600649 neg = b->neg;
650 b->neg = false;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600651 s = bc_num_ulong(b, &pow);
Gavin Howardfb14efc2018-12-22 14:01:58 -0700652 b->neg = neg;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600653 if (s) return s;
654
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600655 bc_num_init(&copy, a->len);
656 bc_num_copy(&copy, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700657
Gavin Howard63738202018-09-26 15:34:20 -0600658 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
Gavin Howardb29674f2018-03-22 22:24:58 -0600659
Gavin Howard112304b2018-12-04 14:30:01 -0700660 for (powrdx = a->rdx; !BC_SIGINT && !(pow & 1); pow >>= 1) {
Gavin Howard63738202018-09-26 15:34:20 -0600661 powrdx <<= 1;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600662 s = bc_num_mul(&copy, &copy, &copy, powrdx);
663 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600664 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700665
Gavin Howard112304b2018-12-04 14:30:01 -0700666 if (BC_SIGINT) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700667 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600668 goto err;
669 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700670
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600671 bc_num_copy(c, &copy);
672
Gavin Howard112304b2018-12-04 14:30:01 -0700673 for (resrdx = powrdx, pow >>= 1; !BC_SIGINT && pow != 0; pow >>= 1) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600674
675 powrdx <<= 1;
676 s = bc_num_mul(&copy, &copy, &copy, powrdx);
677 if (s) goto err;
678
Gavin Howard63738202018-09-26 15:34:20 -0600679 if (pow & 1) {
680 resrdx += powrdx;
Gavin Howard53eba8b2018-10-31 15:14:37 -0600681 s = bc_num_mul(c, &copy, c, resrdx);
682 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600683 }
684 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700685
Gavin Howard53eba8b2018-10-31 15:14:37 -0600686 if (neg) {
687 s = bc_num_inv(c, c, scale);
688 if (s) goto err;
689 }
690
Gavin Howard112304b2018-12-04 14:30:01 -0700691 if (BC_SIGINT) {
Gavin Howard43df94a2018-12-22 10:29:41 -0700692 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -0600693 goto err;
694 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700695
Gavin Howard63738202018-09-26 15:34:20 -0600696 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700697
Gavin Howard53eba8b2018-10-31 15:14:37 -0600698 // We can't use bc_num_clean() here.
Gavin Howard63738202018-09-26 15:34:20 -0600699 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howardf8ddb6d2018-10-22 12:58:53 -0600700 if (zero) bc_num_setToZero(c, scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700701
Gavin Howard6fbdb292018-02-27 15:44:48 -0700702err:
Gavin Howard63738202018-09-26 15:34:20 -0600703 bc_num_free(&copy);
704 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700705}
706
Gavin Howard7bda4782018-12-28 09:53:22 -0700707#if BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700708BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
709
710 BcStatus s = BC_STATUS_SUCCESS;
711 unsigned long val;
712
713 BC_UNUSED(scale);
714
715 s = bc_num_intop(a, b, c, &val);
716 if (s) return s;
717
718 if (val < a->rdx) bc_num_truncate(c, a->rdx - val);
719 else if (val > a->rdx) bc_num_extend(c, val - a->rdx);
720
721 return s;
722}
723
724BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
725
726 BcStatus s = BC_STATUS_SUCCESS;
727 unsigned long val;
728
729 BC_UNUSED(scale);
730
731 s = bc_num_intop(a, b, c, &val);
732 if (s) return s;
733
734 bc_num_shift(c, val);
735
736 return s;
737}
738
739BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
740
741 BcStatus s = BC_STATUS_SUCCESS;
742 unsigned long val;
743 size_t len;
744
745 BC_UNUSED(scale);
746
747 s = bc_num_intop(a, b, c, &val);
748 if (s) return s;
749
750 len = c->rdx + val;
751
752 if (len > c->len) {
753
Gavin Howardecc7fc22018-12-31 13:21:47 -0700754 if (len > BC_MAX_NUM) return bc_vm_verr(BC_ERROR_EXEC_NUM_LEN, BC_MAX_NUM);
Gavin Howardac4d9122018-12-27 23:59:12 -0700755 if (len > c->cap) bc_num_expand(c, len);
756
757 memset(c->num + c->len, 0, len - c->len);
758 c->len = len;
759 }
760
761 c->rdx += val;
762
763 assert(c->rdx <= c->len && c->len <= c->cap);
764
765 return s;
766}
Gavin Howard7bda4782018-12-28 09:53:22 -0700767#endif // BC_ENABLE_EXTRA_MATH
Gavin Howardac4d9122018-12-27 23:59:12 -0700768
Gavin Howard773c86b2018-11-02 14:07:19 -0600769BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
770 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700771{
Gavin Howarda1c44392018-09-27 12:41:15 -0600772 BcStatus s;
Gavin Howard63738202018-09-26 15:34:20 -0600773 BcNum num2, *ptr_a, *ptr_b;
774 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700775
Gavin Howard63738202018-09-26 15:34:20 -0600776 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700777
Gavin Howard890d0c02018-10-30 16:34:50 -0600778 if (c == a) {
Gavin Howard63738202018-09-26 15:34:20 -0600779 ptr_a = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600780 memcpy(ptr_a, c, sizeof(BcNum));
Gavin Howard890d0c02018-10-30 16:34:50 -0600781 init = true;
Gavin Howard63738202018-09-26 15:34:20 -0600782 }
783 else ptr_a = a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700784
Gavin Howard63738202018-09-26 15:34:20 -0600785 if (c == b) {
Gavin Howardba009802018-09-29 04:41:51 -0600786 ptr_b = &num2;
Gavin Howardba009802018-09-29 04:41:51 -0600787 if (c != a) {
788 memcpy(ptr_b, c, sizeof(BcNum));
Gavin Howard63738202018-09-26 15:34:20 -0600789 init = true;
790 }
791 }
792 else ptr_b = b;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700793
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600794 if (init) bc_num_init(c, req);
795 else bc_num_expand(c, req);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700796
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600797 s = op(ptr_a, ptr_b, c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700798
Gavin Howard3cf769e2018-10-11 13:55:11 -0600799 assert(!c->neg || c->len);
800
Gavin Howardba009802018-09-29 04:41:51 -0600801 if (init) bc_num_free(&num2);
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600802
Gavin Howarda1c44392018-09-27 12:41:15 -0600803 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700804}
805
Gavin Howardc90ed902019-01-02 13:07:49 -0700806#ifndef NDEBUG
Gavin Howardc7d655b2018-12-28 19:45:13 -0700807bool bc_num_strValid(const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700808
Gavin Howardc7d655b2018-12-28 19:45:13 -0700809 bool radix = false;
Gavin Howard63738202018-09-26 15:34:20 -0600810 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700811
Gavin Howard63738202018-09-26 15:34:20 -0600812 if (!len) return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700813
Gavin Howard63738202018-09-26 15:34:20 -0600814 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700815
Gavin Howard8a921bd2018-10-11 14:15:32 -0600816 BcDig c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600817
Gavin Howard63738202018-09-26 15:34:20 -0600818 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700819
Gavin Howard63738202018-09-26 15:34:20 -0600820 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700821
Gavin Howard63738202018-09-26 15:34:20 -0600822 radix = true;
823 continue;
824 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700825
Gavin Howardc7d655b2018-12-28 19:45:13 -0700826 if (!(isdigit(c) || isupper(c))) return false;
Gavin Howard63738202018-09-26 15:34:20 -0600827 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700828
Gavin Howard63738202018-09-26 15:34:20 -0600829 return true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700830}
Gavin Howardc90ed902019-01-02 13:07:49 -0700831#endif // NDEBUG
Gavin Howard6fbdb292018-02-27 15:44:48 -0700832
Gavin Howardc7d655b2018-12-28 19:45:13 -0700833unsigned long bc_num_parseChar(char c, size_t base_t) {
834
835 if (isupper(c)) {
836 c = BC_NUM_NUM_LETTER(c);
837 c = ((size_t) c) >= base_t ? base_t - 1 : c;
838 }
839 else c -= '0';
840
841 return (unsigned long) (uchar) c;
842}
843
Gavin Howard3c02ed32018-12-28 18:17:15 -0700844void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700845
Gavin Howard63738202018-09-26 15:34:20 -0600846 size_t len, i;
847 const char *ptr;
848 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700849
Gavin Howard63738202018-09-26 15:34:20 -0600850 for (i = 0; val[i] == '0'; ++i);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700851
Gavin Howard63738202018-09-26 15:34:20 -0600852 val += i;
853 len = strlen(val);
854 bc_num_zero(n);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700855
Gavin Howard53eba8b2018-10-31 15:14:37 -0600856 if (len != 0) {
Gavin Howard63738202018-09-26 15:34:20 -0600857 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600858 bc_num_expand(n, len);
Gavin Howard63738202018-09-26 15:34:20 -0600859 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600860
Gavin Howard63738202018-09-26 15:34:20 -0600861 ptr = strchr(val, '.');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700862
Gavin Howard63738202018-09-26 15:34:20 -0600863 // Explicitly test for NULL here to produce either a 0 or 1.
864 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700865
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600866 if (!zero) {
Gavin Howardc7d655b2018-12-28 19:45:13 -0700867 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.')) {
868 char c = val[i];
869 if (isupper(c)) c = '9';
870 n->num[n->len] = c - '0';
871 }
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600872 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700873}
874
Gavin Howard3c02ed32018-12-28 18:17:15 -0700875BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val,
Gavin Howardc7d655b2018-12-28 19:45:13 -0700876 BcNum *restrict base, size_t base_t)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700877{
Gavin Howard63738202018-09-26 15:34:20 -0600878 BcStatus s;
879 BcNum temp, mult, result;
Gavin Howard223fa6d2018-12-31 12:24:25 -0700880 BcDig c;
Gavin Howard11b9afd2018-10-18 14:23:28 -0600881 bool zero = true;
Gavin Howard63738202018-09-26 15:34:20 -0600882 unsigned long v;
883 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700884
Gavin Howard63738202018-09-26 15:34:20 -0600885 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700886
Gavin Howard11b9afd2018-10-18 14:23:28 -0600887 for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0');
Gavin Howard7536dcf2018-12-15 19:27:09 -0700888 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700889
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600890 bc_num_init(&temp, BC_NUM_DEF_SIZE);
891 bc_num_init(&mult, BC_NUM_DEF_SIZE);
Gavin Howardede51f02018-03-02 12:30:00 -0700892
Gavin Howardc7d655b2018-12-28 19:45:13 -0700893 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600894
Gavin Howardc7d655b2018-12-28 19:45:13 -0700895 v = bc_num_parseChar(c, base_t);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700896
Gavin Howard53eba8b2018-10-31 15:14:37 -0600897 s = bc_num_mul(n, base, &mult, 0);
898 if (s) goto int_err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700899 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600900 s = bc_num_add(&mult, &temp, n, 0);
901 if (s) goto int_err;
Gavin Howard63738202018-09-26 15:34:20 -0600902 }
Gavin Howardede51f02018-03-02 12:30:00 -0700903
Gavin Howard53eba8b2018-10-31 15:14:37 -0600904 if (i == len) {
905 c = val[i];
906 if (c == 0) goto int_err;
907 }
908
Gavin Howard63738202018-09-26 15:34:20 -0600909 assert(c == '.');
Gavin Howardad0ecfe2018-10-30 01:16:01 -0600910 bc_num_init(&result, base->len);
Gavin Howard63738202018-09-26 15:34:20 -0600911 bc_num_one(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700912
Gavin Howardc7d655b2018-12-28 19:45:13 -0700913 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howard53eba8b2018-10-31 15:14:37 -0600914
Gavin Howardc7d655b2018-12-28 19:45:13 -0700915 v = bc_num_parseChar(c, base_t);
Gavin Howardede51f02018-03-02 12:30:00 -0700916
Gavin Howard53eba8b2018-10-31 15:14:37 -0600917 s = bc_num_mul(&result, base, &result, 0);
918 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -0700919 bc_num_ulong2num(&temp, v);
Gavin Howard53eba8b2018-10-31 15:14:37 -0600920 s = bc_num_add(&result, &temp, &result, 0);
921 if (s) goto err;
922 s = bc_num_mul(&mult, base, &mult, 0);
923 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -0600924 }
Gavin Howardede51f02018-03-02 12:30:00 -0700925
Gavin Howard53eba8b2018-10-31 15:14:37 -0600926 s = bc_num_div(&result, &mult, &result, digits);
927 if (s) goto err;
928 s = bc_num_add(n, &result, n, digits);
929 if (s) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600930
Gavin Howard53eba8b2018-10-31 15:14:37 -0600931 if (n->len != 0) {
932 if (n->rdx < digits) bc_num_extend(n, digits - n->rdx);
Gavin Howard63738202018-09-26 15:34:20 -0600933 }
934 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700935
936err:
Gavin Howard63738202018-09-26 15:34:20 -0600937 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700938int_err:
Gavin Howard63738202018-09-26 15:34:20 -0600939 bc_num_free(&mult);
Gavin Howard63738202018-09-26 15:34:20 -0600940 bc_num_free(&temp);
Gavin Howard7536dcf2018-12-15 19:27:09 -0700941 return s;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700942}
943
Gavin Howardab5e9632019-01-02 13:32:02 -0700944void bc_num_printNewline() {
945 if (vm->nchars >= vm->line_len - 1) {
Gavin Howard48af52e2018-10-30 14:47:38 -0600946 bc_vm_putchar('\\');
947 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -0700948 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -0600949 }
Gavin Howard80977b22018-09-06 14:29:13 -0600950}
951
Gavin Howard40a085f2018-12-03 12:08:59 -0700952#if DC_ENABLED
Gavin Howardab5e9632019-01-02 13:32:02 -0700953void bc_num_printChar(size_t n, size_t len, bool rdx)
Gavin Howard83eb8392018-10-09 01:21:19 -0600954{
Gavin Howard3c02ed32018-12-28 18:17:15 -0700955 BC_UNUSED(rdx);
956 bc_vm_putchar((uchar) n);
Gavin Howardab5e9632019-01-02 13:32:02 -0700957 vm->nchars += len;
Gavin Howard83eb8392018-10-09 01:21:19 -0600958}
959#endif // DC_ENABLED
960
Gavin Howardab5e9632019-01-02 13:32:02 -0700961void bc_num_printDigits(size_t n, size_t len, bool rdx)
Gavin Howard3c02ed32018-12-28 18:17:15 -0700962{
Gavin Howarda84ad992018-12-03 19:11:06 -0700963 size_t exp, pow;
Gavin Howard80977b22018-09-06 14:29:13 -0600964
Gavin Howardab5e9632019-01-02 13:32:02 -0700965 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700966 bc_vm_putchar(rdx ? '.' : ' ');
Gavin Howardab5e9632019-01-02 13:32:02 -0700967 ++vm->nchars;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600968
Gavin Howardab5e9632019-01-02 13:32:02 -0700969 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700970 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10);
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600971
Gavin Howardab5e9632019-01-02 13:32:02 -0700972 for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) {
Gavin Howarda84ad992018-12-03 19:11:06 -0700973 size_t dig;
Gavin Howardab5e9632019-01-02 13:32:02 -0700974 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700975 dig = n / pow;
976 n -= dig * pow;
Gavin Howard954f9b62018-12-19 15:22:20 -0700977 bc_vm_putchar(((uchar) dig) + '0');
Gavin Howard63738202018-09-26 15:34:20 -0600978 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700979}
Gavin Howardfe679f02018-02-14 15:50:09 -0700980
Gavin Howardab5e9632019-01-02 13:32:02 -0700981void bc_num_printHex(size_t n, size_t len, bool rdx) {
Gavin Howard70e3d602018-12-11 11:00:49 -0700982
Gavin Howard3c02ed32018-12-28 18:17:15 -0700983 assert(len == 1);
Gavin Howard80977b22018-09-06 14:29:13 -0600984
Gavin Howard3c02ed32018-12-28 18:17:15 -0700985 if (rdx) {
Gavin Howardab5e9632019-01-02 13:32:02 -0700986 bc_num_printNewline();
Gavin Howard48af52e2018-10-30 14:47:38 -0600987 bc_vm_putchar('.');
Gavin Howardab5e9632019-01-02 13:32:02 -0700988 vm->nchars += 1;
Gavin Howard63738202018-09-26 15:34:20 -0600989 }
Gavin Howard2682a1f2018-03-03 09:09:13 -0700990
Gavin Howardab5e9632019-01-02 13:32:02 -0700991 bc_num_printNewline();
Gavin Howard3c02ed32018-12-28 18:17:15 -0700992 bc_vm_putchar(bc_num_hex_digits[n]);
Gavin Howardab5e9632019-01-02 13:32:02 -0700993 vm->nchars += len;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700994}
995
Gavin Howardab5e9632019-01-02 13:32:02 -0700996void bc_num_printDecimal(const BcNum *restrict n) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700997
Gavin Howard63738202018-09-26 15:34:20 -0600998 size_t i, rdx = n->rdx - 1;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700999
Gavin Howard48af52e2018-10-30 14:47:38 -06001000 if (n->neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001001 vm->nchars += n->neg;
Gavin Howard32f2beb2018-03-09 11:43:20 -07001002
Gavin Howard48af52e2018-10-30 14:47:38 -06001003 for (i = n->len - 1; i < n->len; --i)
Gavin Howardab5e9632019-01-02 13:32:02 -07001004 bc_num_printHex((size_t) n->num[i], 1, i == rdx);
Gavin Howard32f2beb2018-03-09 11:43:20 -07001005}
1006
Gavin Howardab5e9632019-01-02 13:32:02 -07001007BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base,
1008 size_t width, BcNumDigitOp print)
Gavin Howardbc7cae82018-03-14 13:43:04 -06001009{
Gavin Howard63738202018-09-26 15:34:20 -06001010 BcStatus s;
1011 BcVec stack;
1012 BcNum intp, fracp, digit, frac_len;
Gavin Howard63738202018-09-26 15:34:20 -06001013 unsigned long dig, *ptr;
Gavin Howard83eb8392018-10-09 01:21:19 -06001014 size_t i;
1015 bool radix;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001016
Gavin Howard53eba8b2018-10-31 15:14:37 -06001017 if (n->len == 0) {
Gavin Howardab5e9632019-01-02 13:32:02 -07001018 print(0, width, false);
Gavin Howard48af52e2018-10-30 14:47:38 -06001019 return BC_STATUS_SUCCESS;
1020 }
Gavin Howard9a4b6cd2018-10-23 15:13:30 -06001021
Gavin Howardd3929832018-12-24 15:13:15 -07001022 bc_vec_init(&stack, sizeof(unsigned long), NULL);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001023 bc_num_init(&intp, n->len);
1024 bc_num_init(&fracp, n->rdx);
1025 bc_num_init(&digit, width);
1026 bc_num_init(&frac_len, BC_NUM_INT(n));
1027 bc_num_copy(&intp, n);
Gavin Howard63738202018-09-26 15:34:20 -06001028 bc_num_one(&frac_len);
Gavin Howarda50fc542018-03-29 17:25:38 -06001029
Gavin Howard63738202018-09-26 15:34:20 -06001030 bc_num_truncate(&intp, intp.rdx);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001031 s = bc_num_sub(n, &intp, &fracp, 0);
1032 if (s) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001033
Gavin Howard53eba8b2018-10-31 15:14:37 -06001034 while (intp.len != 0) {
1035 s = bc_num_divmod(&intp, base, &intp, &digit, 0);
1036 if (s) goto err;
1037 s = bc_num_ulong(&digit, &dig);
1038 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001039 bc_vec_push(&stack, &dig);
Gavin Howard63738202018-09-26 15:34:20 -06001040 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001041
Gavin Howard63738202018-09-26 15:34:20 -06001042 for (i = 0; i < stack.len; ++i) {
1043 ptr = bc_vec_item_rev(&stack, i);
1044 assert(ptr);
Gavin Howardab5e9632019-01-02 13:32:02 -07001045 print(*ptr, width, false);
Gavin Howard63738202018-09-26 15:34:20 -06001046 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001047
Gavin Howard63738202018-09-26 15:34:20 -06001048 if (!n->rdx) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001049
Gavin Howard63738202018-09-26 15:34:20 -06001050 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001051 s = bc_num_mul(&fracp, base, &fracp, n->rdx);
1052 if (s) goto err;
1053 s = bc_num_ulong(&fracp, &dig);
1054 if (s) goto err;
Gavin Howard705c4bc2018-12-03 15:38:04 -07001055 bc_num_ulong2num(&intp, dig);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001056 s = bc_num_sub(&fracp, &intp, &fracp, 0);
1057 if (s) goto err;
Gavin Howardab5e9632019-01-02 13:32:02 -07001058 print(dig, width, radix);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001059 s = bc_num_mul(&frac_len, base, &frac_len, 0);
1060 if (s) goto err;
Gavin Howard63738202018-09-26 15:34:20 -06001061 }
Gavin Howard2682a1f2018-03-03 09:09:13 -07001062
1063err:
Gavin Howard63738202018-09-26 15:34:20 -06001064 bc_num_free(&frac_len);
Gavin Howard63738202018-09-26 15:34:20 -06001065 bc_num_free(&digit);
Gavin Howard63738202018-09-26 15:34:20 -06001066 bc_num_free(&fracp);
Gavin Howard63738202018-09-26 15:34:20 -06001067 bc_num_free(&intp);
Gavin Howard63738202018-09-26 15:34:20 -06001068 bc_vec_free(&stack);
Gavin Howard63738202018-09-26 15:34:20 -06001069 return s;
Gavin Howard2682a1f2018-03-03 09:09:13 -07001070}
1071
Gavin Howardab5e9632019-01-02 13:32:02 -07001072BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base, size_t base_t)
Gavin Howard83eb8392018-10-09 01:21:19 -06001073{
1074 BcStatus s;
1075 size_t width, i;
1076 BcNumDigitOp print;
1077 bool neg = n->neg;
1078
Gavin Howard48af52e2018-10-30 14:47:38 -06001079 if (neg) bc_vm_putchar('-');
Gavin Howardab5e9632019-01-02 13:32:02 -07001080 vm->nchars += neg;
Gavin Howard83eb8392018-10-09 01:21:19 -06001081
1082 n->neg = false;
1083
Gavin Howard431987e2018-12-31 19:16:36 -07001084 if (base_t <= BC_NUM_MAX_POSIX_IBASE) {
Gavin Howard83eb8392018-10-09 01:21:19 -06001085 width = 1;
1086 print = bc_num_printHex;
1087 }
1088 else {
1089 for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width);
1090 print = bc_num_printDigits;
1091 }
1092
Gavin Howardab5e9632019-01-02 13:32:02 -07001093 s = bc_num_printNum(n, base, width, print);
Gavin Howard83eb8392018-10-09 01:21:19 -06001094 n->neg = neg;
1095
1096 return s;
1097}
1098
Gavin Howard40a085f2018-12-03 12:08:59 -07001099#if DC_ENABLED
Gavin Howardab5e9632019-01-02 13:32:02 -07001100BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) {
1101 return bc_num_printNum(n, base, 1, bc_num_printChar);
Gavin Howard83eb8392018-10-09 01:21:19 -06001102}
1103#endif // DC_ENABLED
1104
Gavin Howard3c02ed32018-12-28 18:17:15 -07001105void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
Gavin Howardb8a12922018-12-21 21:40:45 -07001106 assert(n);
1107 n->num = num;
1108 n->cap = cap;
1109 n->rdx = n->len = 0;
1110 n->neg = false;
1111}
1112
Gavin Howard3c02ed32018-12-28 18:17:15 -07001113void bc_num_init(BcNum *restrict n, size_t req) {
Gavin Howard63738202018-09-26 15:34:20 -06001114 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001115 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
Gavin Howardb8a12922018-12-21 21:40:45 -07001116 bc_num_setup(n, bc_vm_malloc(req), req);
Gavin Howardb5c77212018-02-14 17:12:34 -07001117}
1118
Gavin Howard3c02ed32018-12-28 18:17:15 -07001119void bc_num_expand(BcNum *restrict n, size_t req) {
Gavin Howard844e7a52018-10-09 11:46:39 -06001120 assert(n);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001121 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE;
1122 if (req > n->cap) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001123 n->num = bc_vm_realloc(n->num, req);
1124 n->cap = req;
1125 }
Gavin Howardb5c77212018-02-14 17:12:34 -07001126}
1127
Gavin Howarded392aa2018-02-27 13:09:26 -07001128void bc_num_free(void *num) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001129 assert(num);
1130 free(((BcNum*) num)->num);
Gavin Howardb5c77212018-02-14 17:12:34 -07001131}
1132
Gavin Howard3c02ed32018-12-28 18:17:15 -07001133void bc_num_copy(BcNum *d, const BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -07001134
Gavin Howard63738202018-09-26 15:34:20 -06001135 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -07001136
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001137 if (d != s) {
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001138 bc_num_expand(d, s->cap);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001139 d->len = s->len;
1140 d->neg = s->neg;
1141 d->rdx = s->rdx;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001142 memcpy(d->num, s->num, sizeof(BcDig) * d->len);
1143 }
Gavin Howard5a049c42018-02-15 11:24:11 -07001144}
1145
Gavin Howard3c02ed32018-12-28 18:17:15 -07001146BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val,
Gavin Howardd746e802018-12-28 22:06:00 -07001147 BcNum *restrict base, size_t base_t, bool letter)
Gavin Howard3c02ed32018-12-28 18:17:15 -07001148{
Gavin Howard7536dcf2018-12-15 19:27:09 -07001149 BcStatus s = BC_STATUS_SUCCESS;
1150
Gavin Howard63738202018-09-26 15:34:20 -06001151 assert(n && val && base);
Gavin Howard96edeb32018-12-31 09:33:22 -07001152 assert(base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase);
Gavin Howardc90ed902019-01-02 13:07:49 -07001153 assert(bc_num_strValid(val));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001154
Gavin Howardd746e802018-12-28 22:06:00 -07001155 if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE));
Gavin Howardc7d655b2018-12-28 19:45:13 -07001156 else if (base_t == 10) bc_num_parseDecimal(n, val);
1157 else s = bc_num_parseBase(n, val, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001158
Gavin Howard7536dcf2018-12-15 19:27:09 -07001159 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001160}
1161
Gavin Howardab5e9632019-01-02 13:32:02 -07001162BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base,
1163 size_t base_t, bool newline)
Gavin Howard152f3e82018-03-07 12:33:15 -07001164{
Gavin Howard48af52e2018-10-30 14:47:38 -06001165 BcStatus s = BC_STATUS_SUCCESS;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001166
Gavin Howardab5e9632019-01-02 13:32:02 -07001167 assert(n && base);
Gavin Howard63738202018-09-26 15:34:20 -06001168 assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001169
Gavin Howardab5e9632019-01-02 13:32:02 -07001170 bc_num_printNewline();
Gavin Howardbc7cae82018-03-14 13:43:04 -06001171
Gavin Howardab5e9632019-01-02 13:32:02 -07001172 if (n->len == 0) bc_num_printHex(0, 1, false);
1173 else if (base_t == 10) bc_num_printDecimal(n);
1174 else s = bc_num_printBase(n, base, base_t);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001175
Gavin Howard63738202018-09-26 15:34:20 -06001176 if (newline) {
Gavin Howard48af52e2018-10-30 14:47:38 -06001177 bc_vm_putchar('\n');
Gavin Howardab5e9632019-01-02 13:32:02 -07001178 vm->nchars = 0;
Gavin Howard63738202018-09-26 15:34:20 -06001179 }
Gavin Howard152f3e82018-03-07 12:33:15 -07001180
Gavin Howard63738202018-09-26 15:34:20 -06001181 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001182}
1183
Gavin Howard3c02ed32018-12-28 18:17:15 -07001184BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001185
Gavin Howard63738202018-09-26 15:34:20 -06001186 size_t i;
Gavin Howard7b557da2018-12-21 10:25:32 -07001187 unsigned long pow, r;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001188
Gavin Howard63738202018-09-26 15:34:20 -06001189 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001190
Gavin Howard7536dcf2018-12-15 19:27:09 -07001191 if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001192
Gavin Howard7b557da2018-12-21 10:25:32 -07001193 for (r = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001194
Gavin Howard7b557da2018-12-21 10:25:32 -07001195 unsigned long prev = r, powprev = pow;
Gavin Howard88c25302018-10-17 13:32:23 -06001196
Gavin Howard7b557da2018-12-21 10:25:32 -07001197 r += ((unsigned long) n->num[i]) * pow;
Gavin Howard63738202018-09-26 15:34:20 -06001198 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001199
Gavin Howard7b557da2018-12-21 10:25:32 -07001200 if (r < prev || pow < powprev) return bc_vm_err(BC_ERROR_MATH_OVERFLOW);
Gavin Howard63738202018-09-26 15:34:20 -06001201 }
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001202
Gavin Howard7b557da2018-12-21 10:25:32 -07001203 *result = r;
Gavin Howardd3a1c392018-12-11 12:00:57 -07001204
Gavin Howard63738202018-09-26 15:34:20 -06001205 return BC_STATUS_SUCCESS;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001206}
1207
Gavin Howard3c02ed32018-12-28 18:17:15 -07001208void bc_num_ulong2num(BcNum *restrict n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001209
Gavin Howardec82bdc2018-10-08 23:50:57 -06001210 size_t len;
Gavin Howard8a921bd2018-10-11 14:15:32 -06001211 BcDig *ptr;
Gavin Howardec82bdc2018-10-08 23:50:57 -06001212 unsigned long i;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001213
Gavin Howard63738202018-09-26 15:34:20 -06001214 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001215
Gavin Howard63738202018-09-26 15:34:20 -06001216 bc_num_zero(n);
Gavin Howard025d04d2018-02-20 13:53:28 -07001217
Gavin Howard705c4bc2018-12-03 15:38:04 -07001218 if (val == 0) return;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001219
Gavin Howard7a3e7a72018-12-21 21:41:19 -07001220 for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001221 bc_num_expand(n, len);
Gavin Howardec82bdc2018-10-08 23:50:57 -06001222 for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10;
Gavin Howard025d04d2018-02-20 13:53:28 -07001223}
1224
Gavin Howard12fe7812018-09-29 03:45:18 -06001225BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001226 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s;
Gavin Howard41852962018-12-27 17:15:00 -07001227 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001228 return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001229}
1230
Gavin Howard12fe7812018-09-29 03:45:18 -06001231BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001232 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a;
Gavin Howard41852962018-12-27 17:15:00 -07001233 BC_UNUSED(scale);
Gavin Howard12fe7812018-09-29 03:45:18 -06001234 return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001235}
1236
Gavin Howard12fe7812018-09-29 03:45:18 -06001237BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001238 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001239 return bc_num_binary(a, b, c, scale, bc_num_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001240}
1241
Gavin Howard12fe7812018-09-29 03:45:18 -06001242BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001243 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001244 return bc_num_binary(a, b, c, scale, bc_num_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001245}
1246
Gavin Howard54b946a2018-10-23 12:06:57 -06001247BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard63738202018-09-26 15:34:20 -06001248 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard54b946a2018-10-23 12:06:57 -06001249 return bc_num_binary(a, b, c, scale, bc_num_rem, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001250}
1251
Gavin Howard12fe7812018-09-29 03:45:18 -06001252BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard54b946a2018-10-23 12:06:57 -06001253 return bc_num_binary(a, b, c, scale, bc_num_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001254}
1255
Gavin Howard7d8f0382018-12-27 16:15:13 -07001256#if BC_ENABLE_EXTRA_MATH
1257BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001258 return bc_num_binary(a, b, c, scale, bc_num_place, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001259}
1260
1261BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001262 return bc_num_binary(a, b, c, scale, bc_num_left, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001263}
1264
1265BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howardac4d9122018-12-27 23:59:12 -07001266 return bc_num_binary(a, b, c, scale, bc_num_right, a->len);
Gavin Howard7d8f0382018-12-27 16:15:13 -07001267}
1268#endif // BC_ENABLE_EXTRA_MATH
1269
Gavin Howard3c02ed32018-12-28 18:17:15 -07001270BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001271
Gavin Howard63738202018-09-26 15:34:20 -06001272 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001273 BcNum num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001274 size_t pow, len, digs, digs1, resrdx, req, times = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001275 ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX;
Gavin Howard18f40202018-12-29 21:30:37 -07001276 BcDig half_digs[2];
Gavin Howard954276b2018-05-16 01:43:58 -06001277
Gavin Howard34edd0a2018-10-25 15:51:05 -06001278 assert(a && b && a != b);
Gavin Howard954276b2018-05-16 01:43:58 -06001279
Gavin Howardd8d60c42018-10-29 12:13:09 -06001280 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a) + 1) >> 1) + 1;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001281 bc_num_expand(b, req);
Gavin Howard954276b2018-05-16 01:43:58 -06001282
Gavin Howard53eba8b2018-10-31 15:14:37 -06001283 if (a->len == 0) {
Gavin Howardf8ddb6d2018-10-22 12:58:53 -06001284 bc_num_setToZero(b, scale);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001285 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001286 }
Gavin Howard7536dcf2018-12-15 19:27:09 -07001287 else if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
Gavin Howard63738202018-09-26 15:34:20 -06001288 else if (BC_NUM_ONE(a)) {
Gavin Howard757b66a2018-10-06 04:13:46 -06001289 bc_num_one(b);
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001290 bc_num_extend(b, scale);
1291 return BC_STATUS_SUCCESS;
Gavin Howard63738202018-09-26 15:34:20 -06001292 }
Gavin Howard954276b2018-05-16 01:43:58 -06001293
Gavin Howard890d0c02018-10-30 16:34:50 -06001294 scale = BC_MAX(scale, a->rdx) + 1;
1295 len = a->len + scale;
Gavin Howard954276b2018-05-16 01:43:58 -06001296
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001297 bc_num_init(&num1, len);
1298 bc_num_init(&num2, len);
Gavin Howard18f40202018-12-29 21:30:37 -07001299 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig));
Gavin Howard954276b2018-05-16 01:43:58 -06001300
Gavin Howard63738202018-09-26 15:34:20 -06001301 bc_num_one(&half);
1302 half.num[0] = 5;
1303 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001304
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001305 bc_num_init(&f, len);
1306 bc_num_init(&fprime, len);
Gavin Howard954276b2018-05-16 01:43:58 -06001307
Gavin Howard63738202018-09-26 15:34:20 -06001308 x0 = &num1;
1309 x1 = &num2;
Gavin Howard954276b2018-05-16 01:43:58 -06001310
Gavin Howard63738202018-09-26 15:34:20 -06001311 bc_num_one(x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001312 pow = BC_NUM_INT(a);
Gavin Howard954276b2018-05-16 01:43:58 -06001313
Gavin Howard53eba8b2018-10-31 15:14:37 -06001314 if (pow) {
Gavin Howard954276b2018-05-16 01:43:58 -06001315
Gavin Howardc39fd492018-10-04 10:07:03 -06001316 if (pow & 1) x0->num[0] = 2;
1317 else x0->num[0] = 6;
Gavin Howard954276b2018-05-16 01:43:58 -06001318
Gavin Howardc39fd492018-10-04 10:07:03 -06001319 pow -= 2 - (pow & 1);
Gavin Howard954276b2018-05-16 01:43:58 -06001320
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001321 bc_num_extend(x0, pow);
Gavin Howarded742ce2018-08-29 14:23:12 -06001322
Gavin Howard63738202018-09-26 15:34:20 -06001323 // Make sure to move the radix back.
1324 x0->rdx -= pow;
1325 }
Gavin Howard954276b2018-05-16 01:43:58 -06001326
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001327 x0->rdx = digs = digs1 = 0;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001328 resrdx = scale + 2;
1329 len = BC_NUM_INT(x0) + resrdx - 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001330
Gavin Howard112304b2018-12-04 14:30:01 -07001331 while (!BC_SIGINT && (cmp != 0 || digs < len)) {
Gavin Howard954276b2018-05-16 01:43:58 -06001332
Gavin Howard53eba8b2018-10-31 15:14:37 -06001333 s = bc_num_div(a, x0, &f, resrdx);
1334 if (s) goto err;
1335 s = bc_num_add(x0, &f, &fprime, resrdx);
1336 if (s) goto err;
1337 s = bc_num_mul(&fprime, &half, x1, resrdx);
1338 if (s) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001339
Gavin Howard53eba8b2018-10-31 15:14:37 -06001340 cmp = bc_num_cmp(x1, x0);
1341 digs = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001342
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001343 if (cmp == cmp2 && digs == digs1) times += 1;
Gavin Howardb11e9e22018-10-06 17:26:02 -06001344 else times = 0;
1345
1346 resrdx += times > 4;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001347
1348 cmp2 = cmp1;
1349 cmp1 = cmp;
Gavin Howardb5ec7f32018-10-29 12:32:39 -06001350 digs1 = digs;
Gavin Howard9f2395b2018-10-06 05:28:58 -06001351
Gavin Howard63738202018-09-26 15:34:20 -06001352 temp = x0;
1353 x0 = x1;
1354 x1 = temp;
1355 }
Gavin Howard954276b2018-05-16 01:43:58 -06001356
Gavin Howard112304b2018-12-04 14:30:01 -07001357 if (BC_SIGINT) {
Gavin Howard43df94a2018-12-22 10:29:41 -07001358 s = BC_STATUS_SIGNAL;
Gavin Howard63738202018-09-26 15:34:20 -06001359 goto err;
1360 }
Gavin Howard0dfe2922018-05-22 13:57:02 -06001361
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001362 bc_num_copy(b, x0);
Gavin Howard53eba8b2018-10-31 15:14:37 -06001363 scale -= 1;
1364 if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale);
Gavin Howard954276b2018-05-16 01:43:58 -06001365
1366err:
Gavin Howard63738202018-09-26 15:34:20 -06001367 bc_num_free(&fprime);
Gavin Howard63738202018-09-26 15:34:20 -06001368 bc_num_free(&f);
Gavin Howard63738202018-09-26 15:34:20 -06001369 bc_num_free(&num2);
Gavin Howard63738202018-09-26 15:34:20 -06001370 bc_num_free(&num1);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001371 assert(!b->neg || b->len);
Gavin Howard63738202018-09-26 15:34:20 -06001372 return s;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001373}
Gavin Howardba009802018-09-29 04:41:51 -06001374
Gavin Howard2cb39612018-10-22 08:46:48 -06001375BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
1376
1377 BcStatus s;
1378 BcNum num2, *ptr_a;
Gavin Howard890d0c02018-10-30 16:34:50 -06001379 bool init = false;
Gavin Howard2cb39612018-10-22 08:46:48 -06001380 size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts);
1381
1382 assert(c != d && a != b && a != d && b != d && b != c);
1383
Gavin Howard890d0c02018-10-30 16:34:50 -06001384 if (c == a) {
Gavin Howard2cb39612018-10-22 08:46:48 -06001385 memcpy(&num2, c, sizeof(BcNum));
1386 ptr_a = &num2;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001387 bc_num_init(c, len);
Gavin Howard890d0c02018-10-30 16:34:50 -06001388 init = true;
Gavin Howard2cb39612018-10-22 08:46:48 -06001389 }
Gavin Howardf679ad82018-10-29 12:26:30 -06001390 else {
1391 ptr_a = a;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001392 bc_num_expand(c, len);
Gavin Howardf679ad82018-10-29 12:26:30 -06001393 }
Gavin Howard2cb39612018-10-22 08:46:48 -06001394
Gavin Howard54b946a2018-10-23 12:06:57 -06001395 s = bc_num_r(ptr_a, b, c, d, scale, ts);
Gavin Howard2cb39612018-10-22 08:46:48 -06001396
1397 assert(!c->neg || c->len);
1398 assert(!d->neg || d->len);
1399
1400 if (init) bc_num_free(&num2);
1401
1402 return s;
1403}
1404
Gavin Howard40a085f2018-12-03 12:08:59 -07001405#if DC_ENABLED
Gavin Howard34edd0a2018-10-25 15:51:05 -06001406BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
Gavin Howardba009802018-09-29 04:41:51 -06001407
1408 BcStatus s;
Gavin Howard34edd0a2018-10-25 15:51:05 -06001409 BcNum base, exp, two, temp;
Gavin Howardd3929832018-12-24 15:13:15 -07001410 BcDig two_digs[2];
Gavin Howardba009802018-09-29 04:41:51 -06001411
Gavin Howard34edd0a2018-10-25 15:51:05 -06001412 assert(a && b && c && d && a != d && b != d && c != d);
Gavin Howardba009802018-09-29 04:41:51 -06001413
Gavin Howard7536dcf2018-12-15 19:27:09 -07001414 if (c->len == 0) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
1415 if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE);
1416 if (a->rdx || b->rdx || c->rdx)
1417 return bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
Gavin Howardba009802018-09-29 04:41:51 -06001418
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001419 bc_num_expand(d, c->len);
1420 bc_num_init(&base, c->len);
1421 bc_num_init(&exp, b->len);
Gavin Howard7fdc30a2018-12-29 21:31:21 -07001422 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig));
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001423 bc_num_init(&temp, b->len);
Gavin Howardba009802018-09-29 04:41:51 -06001424
1425 bc_num_one(&two);
Gavin Howardba009802018-09-29 04:41:51 -06001426 two.num[0] = 2;
1427 bc_num_one(d);
1428
Gavin Howard53eba8b2018-10-31 15:14:37 -06001429 s = bc_num_rem(a, c, &base, 0);
1430 if (s) goto err;
Gavin Howardad0ecfe2018-10-30 01:16:01 -06001431 bc_num_copy(&exp, b);
Gavin Howardba009802018-09-29 04:41:51 -06001432
Gavin Howard53eba8b2018-10-31 15:14:37 -06001433 while (exp.len != 0) {
Gavin Howardba009802018-09-29 04:41:51 -06001434
Gavin Howard53eba8b2018-10-31 15:14:37 -06001435 s = bc_num_divmod(&exp, &two, &exp, &temp, 0);
1436 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001437
1438 if (BC_NUM_ONE(&temp)) {
Gavin Howard53eba8b2018-10-31 15:14:37 -06001439 s = bc_num_mul(d, &base, &temp, 0);
1440 if (s) goto err;
1441 s = bc_num_rem(&temp, c, d, 0);
1442 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001443 }
1444
Gavin Howard53eba8b2018-10-31 15:14:37 -06001445 s = bc_num_mul(&base, &base, &temp, 0);
1446 if (s) goto err;
1447 s = bc_num_rem(&temp, c, &base, 0);
1448 if (s) goto err;
Gavin Howardba009802018-09-29 04:41:51 -06001449 }
1450
1451err:
1452 bc_num_free(&temp);
Gavin Howardba009802018-09-29 04:41:51 -06001453 bc_num_free(&exp);
Gavin Howardba009802018-09-29 04:41:51 -06001454 bc_num_free(&base);
Gavin Howard3cf769e2018-10-11 13:55:11 -06001455 assert(!d->neg || d->len);
Gavin Howardba009802018-09-29 04:41:51 -06001456 return s;
1457}
Gavin Howarde6e84762018-10-03 11:46:34 -06001458#endif // DC_ENABLED