blob: 93ed234d852cdc5dc83b9db5116c88f3f4cb87a0 [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>
24#include <stdbool.h>
25#include <string.h>
26
Gavin Howard8e2cc692018-02-15 17:39:14 -070027#include <math.h>
28
29#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 Howardf507f232018-02-23 21:10:24 -070033#include <vector.h>
Gavin Howardd5551672018-09-22 19:52:42 -060034#include <vm.h>
Gavin Howard3eb626f2018-02-14 13:54:35 -070035
Gavin Howard0be26ed2018-08-31 20:21:56 -060036void bc_num_zero(BcNum *n) {
37 if (!n) return;
38 memset(n->num, 0, n->cap * sizeof(char));
39 n->neg = false;
40 n->len = 0;
41 n->rdx = 0;
42}
43
44void bc_num_one(BcNum *n) {
45 if (!n) return;
46 bc_num_zero(n);
47 n->len = 1;
48 n->num[0] = 1;
49}
50
51void bc_num_ten(BcNum *n) {
52 if (!n) return;
53 bc_num_zero(n);
54 n->len = 2;
55 n->num[0] = 0;
56 n->num[1] = 1;
57}
58
Gavin Howard2ea7dc42018-05-22 14:02:02 -060059BcStatus bc_num_subArrays(BcDigit *n1, BcDigit *n2, size_t len) {
Gavin Howarde1e74942018-03-20 15:51:52 -060060 size_t i, j;
Gavin Howard2ea7dc42018-05-22 14:02:02 -060061 for (i = 0; !bcg.signe && i < len; ++i) {
62 for (n1[i] -= n2[i], j = 0; !bcg.signe && n1[i + j] < 0;) {
Gavin Howard9c4358c2018-03-22 20:11:28 -060063 n1[i + j++] += 10;
Gavin Howardcfc5faa2018-03-20 18:22:42 -060064 n1[i + j] -= 1;
Gavin Howarde1e74942018-03-20 15:51:52 -060065 }
66 }
Gavin Howard2ea7dc42018-05-22 14:02:02 -060067 return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS;
Gavin Howarde1e74942018-03-20 15:51:52 -060068}
69
Gavin Howard6a804cf2018-05-17 16:54:12 -060070ssize_t bc_num_compare(BcDigit *n1, BcDigit *n2, size_t len) {
71 size_t i;
Gavin Howard8317a792018-09-04 22:34:53 -060072 BcDigit c = 0;
73 for (i = len - 1; !bcg.signe && i < len && !(c = n1[i] - n2[i]); --i);
Gavin Howard6a804cf2018-05-17 16:54:12 -060074 return (c < 0 ? -1 : 1) * (ssize_t) (i + 1);
Gavin Howard08bf5292018-03-20 14:59:33 -060075}
76
Gavin Howard6a804cf2018-05-17 16:54:12 -060077ssize_t bc_num_cmp(BcNum *a, BcNum *b) {
Gavin Howard4681d1b2018-03-05 19:49:33 -070078
Gavin Howard9c4358c2018-03-22 20:11:28 -060079 size_t i, min, a_int, b_int, diff;
Gavin Howard6b5f5bb2018-03-22 23:16:57 -060080 BcDigit *max_num, *min_num;
Gavin Howard4681d1b2018-03-05 19:49:33 -070081 bool a_max;
Gavin Howard8317a792018-09-04 22:34:53 -060082 ssize_t cmp, neg = 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070083
Gavin Howard9c4358c2018-03-22 20:11:28 -060084 if (!a) return !b ? 0 : !b->neg * -2 + 1;
85 else if (!b) return a->neg * -2 + 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070086
Gavin Howard2444f272018-09-11 12:25:53 -060087 if (!a->len) return (!b->neg * -2 + 1) * !!b->len;
88 else if (!b->len) return a->neg * -2 + 1;
89
Gavin Howard4e5ee2b2018-03-10 15:10:17 -070090 if (a->neg) {
Gavin Howard08bf5292018-03-20 14:59:33 -060091 if (b->neg) neg = -1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070092 else return -1;
93 }
Gavin Howard4e5ee2b2018-03-10 15:10:17 -070094 else if (b->neg) return 1;
Gavin Howard4681d1b2018-03-05 19:49:33 -070095
Gavin Howard52e01262018-08-29 12:03:15 -060096 a_int = BC_NUM_INT(a);
97 b_int = BC_NUM_INT(b);
Gavin Howard43ab9332018-03-20 15:02:48 -060098 a_int -= b_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -070099
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600100 if (a_int) return (ssize_t) a_int;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700101
Gavin Howard4e5ee2b2018-03-10 15:10:17 -0700102 a_max = a->rdx > b->rdx;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700103
104 if (a_max) {
Gavin Howard4e5ee2b2018-03-10 15:10:17 -0700105 min = b->rdx;
Gavin Howard4e5ee2b2018-03-10 15:10:17 -0700106 diff = a->rdx - b->rdx;
Gavin Howard4e5ee2b2018-03-10 15:10:17 -0700107 max_num = a->num + diff;
108 min_num = b->num;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700109 }
110 else {
Gavin Howard4e5ee2b2018-03-10 15:10:17 -0700111 min = a->rdx;
Gavin Howard4e5ee2b2018-03-10 15:10:17 -0700112 diff = b->rdx - a->rdx;
Gavin Howard4e5ee2b2018-03-10 15:10:17 -0700113 max_num = b->num + diff;
114 min_num = a->num;
Gavin Howard4681d1b2018-03-05 19:49:33 -0700115 }
116
Gavin Howard6a804cf2018-05-17 16:54:12 -0600117 cmp = bc_num_compare(max_num, min_num, b_int + min);
Gavin Howard2ba07112018-03-20 15:40:58 -0600118 if (cmp) return cmp * (!a_max * -2 + 1) * neg;
Gavin Howard021150b2018-03-10 15:40:42 -0700119
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600120 for (max_num -= diff, i = diff - 1; !bcg.signe && i < diff; --i) {
Gavin Howard86f32e92018-03-20 19:21:12 -0600121 if (max_num[i]) return neg * (!a_max * -2 + 1);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700122 }
123
124 return 0;
125}
126
Gavin Howardd9734e52018-03-29 15:54:41 -0600127void bc_num_truncate(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700128
Gavin Howard021150b2018-03-10 15:40:42 -0700129 BcDigit *ptr;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700130
Gavin Howard27fdfb92018-03-21 07:56:59 -0600131 assert(places <= n->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700132
Gavin Howardd9734e52018-03-29 15:54:41 -0600133 if (!places) return;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700134
135 ptr = n->num + places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700136 n->len -= places;
137 n->rdx -= places;
138
Gavin Howard021150b2018-03-10 15:40:42 -0700139 memmove(n->num, ptr, n->len * sizeof(BcDigit));
Gavin Howard021150b2018-03-10 15:40:42 -0700140 memset(n->num + n->len, 0, sizeof(BcDigit) * (n->cap - n->len));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700141}
142
Gavin Howard3f68df72018-03-22 20:30:27 -0600143BcStatus bc_num_extend(BcNum *n, size_t places) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700144
145 BcStatus status;
Gavin Howard021150b2018-03-10 15:40:42 -0700146 BcDigit *ptr;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700147 size_t len;
148
Gavin Howard9c4358c2018-03-22 20:11:28 -0600149 if (!places) return BC_STATUS_SUCCESS;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700150
151 len = n->len + places;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600152 if (n->cap < len && (status = bc_num_expand(n, len))) return status;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700153
154 ptr = n->num + places;
Gavin Howard021150b2018-03-10 15:40:42 -0700155 memmove(ptr, n->num, sizeof(BcDigit) * n->len);
Gavin Howard021150b2018-03-10 15:40:42 -0700156 memset(n->num, 0, sizeof(BcDigit) * places);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700157
158 n->len += places;
Gavin Howardcde142c2018-02-28 17:33:44 -0700159 n->rdx += places;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700160
161 return BC_STATUS_SUCCESS;
162}
163
Gavin Howardc0f65d72018-09-01 16:59:04 -0600164void bc_num_clean(BcNum *n) {
Gavin Howarde57525d2018-09-01 16:56:21 -0600165 while (n->len > 0 && !n->num[n->len - 1]) --n->len;
166 if (n->len == 0) n->rdx = n->neg = 0;
167 else if (n->len < n->rdx) n->len = n->rdx;
168}
169
Gavin Howard337fe602018-09-01 15:10:59 -0600170BcStatus bc_num_retireMul(BcNum *n, size_t scale) {
171
172 BcStatus status = BC_STATUS_SUCCESS;
173
174 if (n->rdx < scale) status = bc_num_extend(n, scale - n->rdx);
175 else bc_num_truncate(n, n->rdx - scale);
176
Gavin Howardc0f65d72018-09-01 16:59:04 -0600177 bc_num_clean(n);
Gavin Howard337fe602018-09-01 15:10:59 -0600178
179 return status;
180}
181
Gavin Howard9c4358c2018-03-22 20:11:28 -0600182BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
183
184 BcStatus status;
185 BcNum one;
186
187 if ((status = bc_num_init(&one, BC_NUM_DEF_SIZE))) return status;
188
189 bc_num_one(&one);
190 status = bc_num_div(&one, a, b, scale);
191 bc_num_free(&one);
192
193 return status;
194}
195
Gavin Howard24434e12018-09-06 15:10:32 -0600196BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700197
Gavin Howard6b5f5bb2018-03-22 23:16:57 -0600198 BcDigit *ptr, *ptr_a, *ptr_b, *ptr_c;
Gavin Howard647a8802018-03-22 09:42:46 -0600199 size_t i, max, min_rdx, min_int, diff, a_int, b_int;
Gavin Howard021150b2018-03-10 15:40:42 -0700200 BcDigit carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700201
Gavin Howard73cce1a2018-09-06 15:16:09 -0600202 // Because this function doesn't need to use scale (per the bc spec), I am
203 // hijacking it to tell this function whether it's doing an add or a subtract.
204
Gavin Howard695e5d92018-09-06 15:04:33 -0600205 if (!a->len) {
206 BcStatus status = bc_num_copy(c, b);
Gavin Howard24434e12018-09-06 15:10:32 -0600207 if (sub) c->neg = !c->neg;
Gavin Howard695e5d92018-09-06 15:04:33 -0600208 return status;
209 }
Gavin Howardf6964a12018-03-14 10:52:31 -0600210 else if (!b->len) return bc_num_copy(c, a);
211
Gavin Howard6fbdb292018-02-27 15:44:48 -0700212 c->neg = a->neg;
Gavin Howard021150b2018-03-10 15:40:42 -0700213 memset(c->num, 0, c->cap * sizeof(BcDigit));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700214
215 c->rdx = BC_MAX(a->rdx, b->rdx);
Gavin Howard647a8802018-03-22 09:42:46 -0600216 min_rdx = BC_MIN(a->rdx, b->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700217 c->len = 0;
218
219 if (a->rdx > b->rdx) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700220 diff = a->rdx - b->rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700221 ptr = a->num;
222 ptr_a = a->num + diff;
223 ptr_b = b->num;
224 }
225 else {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700226 diff = b->rdx - a->rdx;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700227 ptr = b->num;
228 ptr_a = a->num;
229 ptr_b = b->num + diff;
230 }
231
Gavin Howard647a8802018-03-22 09:42:46 -0600232 for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i];
Gavin Howard6fbdb292018-02-27 15:44:48 -0700233
234 ptr_c += diff;
Gavin Howard52e01262018-08-29 12:03:15 -0600235 a_int = BC_NUM_INT(a);
236 b_int = BC_NUM_INT(b);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700237
Gavin Howard647a8802018-03-22 09:42:46 -0600238 if (a_int > b_int) {
239 min_int = b_int;
240 max = a_int;
241 ptr = ptr_a;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700242 }
Gavin Howard647a8802018-03-22 09:42:46 -0600243 else {
244 min_int = a_int;
245 max = b_int;
246 ptr = ptr_b;
247 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700248
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600249 for (carry = 0, i = 0; !bcg.signe && i < min_rdx + min_int; ++i, ++c->len) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700250 ptr_c[i] = ptr_a[i] + ptr_b[i] + carry;
Gavin Howard647a8802018-03-22 09:42:46 -0600251 carry = ptr_c[i] / 10;
252 ptr_c[i] %= 10;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700253 }
254
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600255 for (; !bcg.signe && i < max + min_rdx; ++i, ++c->len) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700256 ptr_c[i] += ptr[i] + carry;
Gavin Howard647a8802018-03-22 09:42:46 -0600257 carry = ptr_c[i] / 10;
258 ptr_c[i] %= 10;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700259 }
260
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600261 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
262
Gavin Howard647a8802018-03-22 09:42:46 -0600263 if (carry) c->num[c->len++] = carry;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700264
265 return BC_STATUS_SUCCESS;
266}
267
Gavin Howard3f68df72018-03-22 20:30:27 -0600268BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *c, size_t sub) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700269
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700270 BcStatus status;
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600271 ssize_t cmp;
Gavin Howard6b5f5bb2018-03-22 23:16:57 -0600272 BcNum *minuend, *subtrahend;
Gavin Howard2de93992018-03-20 15:58:13 -0600273 size_t start;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700274 bool aneg, bneg, neg;
Gavin Howarda1c090a2018-03-05 14:20:33 -0700275
Gavin Howardb56eb1a2018-09-06 15:15:22 -0600276 // Because this function doesn't need to use scale (per the bc spec), I am
277 // hijacking it to tell this function whether it's doing an add or a subtract.
Gavin Howard6fbdb292018-02-27 15:44:48 -0700278
Gavin Howardeb9a8222018-03-13 09:25:56 -0600279 if (!a->len) {
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700280 status = bc_num_copy(c, b);
Gavin Howard8e90a472018-09-06 15:16:38 -0600281 if (sub) c->neg = !b->neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700282 return status;
283 }
Gavin Howardeb9a8222018-03-13 09:25:56 -0600284 else if (!b->len) return bc_num_copy(c, a);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700285
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700286 aneg = a->neg;
287 bneg = b->neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700288 a->neg = b->neg = false;
289
Gavin Howard6a804cf2018-05-17 16:54:12 -0600290 cmp = bc_num_cmp(a, b);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700291
292 a->neg = aneg;
293 b->neg = bneg;
294
295 if (!cmp) {
296 bc_num_zero(c);
297 return BC_STATUS_SUCCESS;
298 }
299 else if (cmp > 0) {
Gavin Howardc8c64a52018-09-01 16:57:15 -0600300 neg = a->neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700301 minuend = a;
302 subtrahend = b;
303 }
304 else {
Gavin Howardc8c64a52018-09-01 16:57:15 -0600305 neg = b->neg;
306 if (sub) neg = !neg;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700307 minuend = b;
308 subtrahend = a;
309 }
310
Gavin Howard9c4358c2018-03-22 20:11:28 -0600311 if ((status = bc_num_copy(c, minuend))) return status;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700312 c->neg = neg;
313
314 if (c->rdx < subtrahend->rdx) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600315 if ((status = bc_num_extend(c, subtrahend->rdx - c->rdx))) return status;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700316 start = 0;
317 }
Gavin Howardd9f5c8e2018-03-10 14:08:13 -0700318 else start = c->rdx - subtrahend->rdx;
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700319
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600320 status = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700321
Gavin Howardc0f65d72018-09-01 16:59:04 -0600322 bc_num_clean(c);
Gavin Howard9a6f7a42018-03-05 14:07:06 -0700323
324 return status;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700325}
326
Gavin Howard3f68df72018-03-22 20:30:27 -0600327BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700328
Gavin Howardbe261e72018-09-05 10:11:41 -0600329 BcStatus status;
Gavin Howard021150b2018-03-10 15:40:42 -0700330 BcDigit carry;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600331 size_t i, j, len;
Gavin Howard337fe602018-09-01 15:10:59 -0600332 bool aone = BC_NUM_ONE(a);
333
334 c->rdx = a->rdx + b->rdx;
335 scale = BC_MAX(scale, a->rdx);
336 scale = BC_MAX(scale, b->rdx);
337 scale = BC_MIN(scale, c->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700338
Gavin Howardeb9a8222018-03-13 09:25:56 -0600339 if (!a->len || !b->len) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700340 bc_num_zero(c);
341 return BC_STATUS_SUCCESS;
342 }
Gavin Howard337fe602018-09-01 15:10:59 -0600343 else if (aone || BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700344
Gavin Howard337fe602018-09-01 15:10:59 -0600345 if (aone) {
346 status = bc_num_copy(c, b);
347 if (a->neg) c->neg = !c->neg;
348 }
349 else {
350 status = bc_num_copy(c, a);
351 if (b->neg) c->neg = !c->neg;
352 }
353
Gavin Howardbe261e72018-09-05 10:11:41 -0600354 if (status) return status;
355
Gavin Howard337fe602018-09-01 15:10:59 -0600356 return bc_num_retireMul(c, scale);
357 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700358
Gavin Howard021150b2018-03-10 15:40:42 -0700359 memset(c->num, 0, sizeof(BcDigit) * c->cap);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600360 c->len = carry = len = 0;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700361
Gavin Howard0dfe2922018-05-22 13:57:02 -0600362 for (i = 0; !bcg.signe && i < b->len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700363
Gavin Howard0dfe2922018-05-22 13:57:02 -0600364 for (j = 0; !bcg.signe && j < a->len; ++j) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700365 c->num[i + j] += a->num[j] * b->num[i] + carry;
Gavin Howard5f806ee2018-03-03 23:30:31 -0700366 carry = c->num[i + j] / 10;
367 c->num[i + j] %= 10;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700368 }
369
Gavin Howard0dfe2922018-05-22 13:57:02 -0600370 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
371
Gavin Howard6fbdb292018-02-27 15:44:48 -0700372 if (carry) {
373 c->num[i + j] += carry;
374 carry = 0;
375 len = BC_MAX(len, i + j + 1);
376 }
377 else len = BC_MAX(len, i + j);
378 }
379
Gavin Howard0dfe2922018-05-22 13:57:02 -0600380 if (bcg.signe) return BC_STATUS_EXEC_SIGNAL;
381
Gavin Howard6fbdb292018-02-27 15:44:48 -0700382 c->len = BC_MAX(len, c->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700383 c->neg = !a->neg != !b->neg;
384
Gavin Howard337fe602018-09-01 15:10:59 -0600385 return bc_num_retireMul(c, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700386}
387
Gavin Howard3f68df72018-03-22 20:30:27 -0600388BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700389
390 BcStatus status;
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600391 BcDigit *n, *bptr, q;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600392 size_t len, end, i;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700393 BcNum copy;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700394
Gavin Howardeb9a8222018-03-13 09:25:56 -0600395 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
396 else if (!a->len) {
Gavin Howardb651f1a2018-02-28 17:34:18 -0700397 bc_num_zero(c);
398 return BC_STATUS_SUCCESS;
399 }
Gavin Howardb11bc8a2018-03-01 17:23:00 -0700400 else if (BC_NUM_ONE(b)) {
Gavin Howardae6e7d52018-03-14 10:13:43 -0600401
Gavin Howard9c4358c2018-03-22 20:11:28 -0600402 if ((status = bc_num_copy(c, a))) return status;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700403 if (b->neg) c->neg = !c->neg;
Gavin Howardae6e7d52018-03-14 10:13:43 -0600404
Gavin Howard337fe602018-09-01 15:10:59 -0600405 return bc_num_retireMul(c, scale);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700406 }
407
Gavin Howard52e01262018-08-29 12:03:15 -0600408 if ((status = bc_num_init(&copy, BC_NUM_MREQ(a, b, scale)))) return status;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600409 if ((status = bc_num_copy(&copy, a))) goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700410
Gavin Howard45ff3572018-05-15 16:33:58 -0600411 if ((len = b->len) > copy.len) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600412 if ((status = bc_num_expand(&copy, len + 2))) goto err;
413 if ((status = bc_num_extend(&copy, len - copy.len))) goto err;
Gavin Howard021150b2018-03-10 15:40:42 -0700414 }
415
Gavin Howard9c4358c2018-03-22 20:11:28 -0600416 if (b->rdx > copy.rdx && (status = bc_num_extend(&copy, b->rdx - copy.rdx)))
417 goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700418
419 copy.rdx -= b->rdx;
420
Gavin Howard9c4358c2018-03-22 20:11:28 -0600421 if (scale > copy.rdx && (status = bc_num_extend(&copy, scale - copy.rdx)))
422 goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700423
Gavin Howardc1a6a342018-03-05 12:10:14 -0700424 if (b->rdx == b->len) {
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600425
Gavin Howard45ff3572018-05-15 16:33:58 -0600426 bool zero;
427 for (zero = true, i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1];
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600428
Gavin Howard4bb48552018-09-07 19:03:00 -0600429 if (i == len && zero) {
Gavin Howard8b22bb22018-08-09 10:53:37 -0600430 status = BC_STATUS_MATH_DIVIDE_BY_ZERO;
431 goto err;
432 }
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600433
Gavin Howardc1a6a342018-03-05 12:10:14 -0700434 len -= i - 1;
435 }
436
Gavin Howard45ff3572018-05-15 16:33:58 -0600437 if (copy.cap == copy.len && (status = bc_num_expand(&copy, copy.len + 1)))
438 goto err;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700439
Gavin Howardb651f1a2018-02-28 17:34:18 -0700440 // We want an extra zero in front to make things simpler.
Gavin Howard9c4358c2018-03-22 20:11:28 -0600441 copy.num[copy.len++] = 0;
Gavin Howardac7656d2018-03-01 17:18:40 -0700442 end = copy.len - len;
443
Gavin Howard9c4358c2018-03-22 20:11:28 -0600444 if ((status = bc_num_expand(c, copy.len))) goto err;
Gavin Howardac7656d2018-03-01 17:18:40 -0700445
Gavin Howardb651f1a2018-02-28 17:34:18 -0700446 bc_num_zero(c);
447 c->rdx = copy.rdx;
448 c->len = copy.len;
Gavin Howard021150b2018-03-10 15:40:42 -0700449 bptr = b->num;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700450
Gavin Howard0dfe2922018-05-22 13:57:02 -0600451 for (i = end - 1; !bcg.signe && i < end; --i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700452
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600453 n = copy.num + i;
Gavin Howardb651f1a2018-02-28 17:34:18 -0700454
Gavin Howard6dbf6bf2018-09-01 15:14:48 -0600455 for (q = 0; (!status && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q)
456 status = bc_num_subArrays(n, bptr, len);
Gavin Howardb651f1a2018-02-28 17:34:18 -0700457
Gavin Howard9c4358c2018-03-22 20:11:28 -0600458 c->num[i] = q;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700459 }
460
Gavin Howard2ea7dc42018-05-22 14:02:02 -0600461 if (status) goto err;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600462
Gavin Howardac7656d2018-03-01 17:18:40 -0700463 c->neg = !a->neg != !b->neg;
Gavin Howard337fe602018-09-01 15:10:59 -0600464 status = bc_num_retireMul(c, scale);
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600465
Gavin Howardb651f1a2018-02-28 17:34:18 -0700466err:
Gavin Howardb651f1a2018-02-28 17:34:18 -0700467 bc_num_free(&copy);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700468 return status;
469}
470
Gavin Howard3f68df72018-03-22 20:30:27 -0600471BcStatus bc_num_alg_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700472
473 BcStatus status;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600474 BcNum c1, c2;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600475 size_t len, tscale = BC_MAX(scale + b->rdx, a->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700476
Gavin Howard8b254872018-03-14 01:13:35 -0600477 if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO;
478
479 if (!a->len) {
480 bc_num_zero(c);
481 return BC_STATUS_SUCCESS;
482 }
483
Gavin Howard5d149cf2018-09-06 13:46:23 -0600484 if (scale) scale = tscale;
485 len = BC_NUM_MREQ(a, b, scale);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700486
Gavin Howard9c4358c2018-03-22 20:11:28 -0600487 if ((status = bc_num_init(&c1, len))) return status;
488 if ((status = bc_num_init(&c2, len))) goto c2_err;
489 if ((status = bc_num_div(a, b, &c1, scale))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700490
Gavin Howard9c4358c2018-03-22 20:11:28 -0600491 if ((status = bc_num_mul(&c1, b, &c2, scale))) goto err;
Gavin Howard5d149cf2018-09-06 13:46:23 -0600492 if ((status = bc_num_sub(a, &c2, c, scale))) goto err;
493
Gavin Howard07c23352018-09-06 14:18:54 -0600494 if (tscale > c->rdx && c->len) status = bc_num_extend(c, tscale - c->rdx);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700495
496err:
Gavin Howard6fbdb292018-02-27 15:44:48 -0700497 bc_num_free(&c2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700498c2_err:
Gavin Howard6fbdb292018-02-27 15:44:48 -0700499 bc_num_free(&c1);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700500 return status;
501}
502
Gavin Howard3f68df72018-03-22 20:30:27 -0600503BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700504
505 BcStatus status;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700506 BcNum copy;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600507 unsigned long pow;
508 size_t i, powrdx, resrdx;
509 bool neg, zero;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700510
Gavin Howard23958582018-03-03 09:30:20 -0700511 if (b->rdx) return BC_STATUS_MATH_NON_INTEGER;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700512
Gavin Howard9c4358c2018-03-22 20:11:28 -0600513 if (!b->len) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700514 bc_num_one(c);
515 return BC_STATUS_SUCCESS;
516 }
Gavin Howardeb9a8222018-03-13 09:25:56 -0600517 else if (!a->len) {
Gavin Howard1d959152018-03-03 23:33:13 -0700518 bc_num_zero(c);
519 return BC_STATUS_SUCCESS;
520 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600521 else if (BC_NUM_ONE(b)) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700522
Gavin Howard9c4358c2018-03-22 20:11:28 -0600523 if (!b->neg) status = bc_num_copy(c, a);
524 else status = bc_num_inv(a, c, scale);
Gavin Howard5fc40e72018-03-05 12:11:09 -0700525
526 return status;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700527 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700528
Gavin Howard9c4358c2018-03-22 20:11:28 -0600529 neg = b->neg;
530 b->neg = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700531
Gavin Howard9c4358c2018-03-22 20:11:28 -0600532 if ((status = bc_num_ulong(b, &pow))) return status;
533 if ((status = bc_num_init(&copy, a->len))) return status;
534 if ((status = bc_num_copy(&copy, a))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700535
Gavin Howardb29674f2018-03-22 22:24:58 -0600536 if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx));
537
538 b->neg = neg;
539
Gavin Howard0dfe2922018-05-22 13:57:02 -0600540 for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) {
Gavin Howard1d959152018-03-03 23:33:13 -0700541 powrdx <<= 1;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600542 if ((status = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
Gavin Howard1d959152018-03-03 23:33:13 -0700543 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700544
Gavin Howard9c4358c2018-03-22 20:11:28 -0600545 if ((status = bc_num_copy(c, &copy))) goto err;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600546 if (bcg.signe) {
547 status = BC_STATUS_EXEC_SIGNAL;
548 goto err;
549 }
Gavin Howard6fb635f2018-03-03 12:45:42 -0700550
Gavin Howard1d959152018-03-03 23:33:13 -0700551 resrdx = powrdx;
Gavin Howard6fb635f2018-03-03 12:45:42 -0700552
Gavin Howard0dfe2922018-05-22 13:57:02 -0600553 for (pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) {
Gavin Howard1d959152018-03-03 23:33:13 -0700554
555 powrdx <<= 1;
556
Gavin Howard9c4358c2018-03-22 20:11:28 -0600557 if ((status = bc_num_mul(&copy, &copy, &copy, powrdx))) goto err;
Gavin Howard1d959152018-03-03 23:33:13 -0700558
Gavin Howard9c4358c2018-03-22 20:11:28 -0600559 if (pow & 1) {
Gavin Howard1d959152018-03-03 23:33:13 -0700560 resrdx += powrdx;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600561 if ((status = bc_num_mul(c, &copy, c, resrdx))) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700562 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700563 }
564
Gavin Howardb29674f2018-03-22 22:24:58 -0600565 if (neg && (status = bc_num_inv(c, c, scale))) goto err;
Gavin Howard0dfe2922018-05-22 13:57:02 -0600566 if (bcg.signe) {
567 status = BC_STATUS_EXEC_SIGNAL;
568 goto err;
569 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700570
Gavin Howarda50fc542018-03-29 17:25:38 -0600571 if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale);
Gavin Howard1d959152018-03-03 23:33:13 -0700572
Gavin Howardb29674f2018-03-22 22:24:58 -0600573 for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i];
Gavin Howard1d959152018-03-03 23:33:13 -0700574 if (zero) bc_num_zero(c);
575
Gavin Howard6fbdb292018-02-27 15:44:48 -0700576err:
Gavin Howard1d959152018-03-03 23:33:13 -0700577 bc_num_free(&copy);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700578 return status;
579}
580
Gavin Howard3f68df72018-03-22 20:30:27 -0600581BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600582 BcNumBinaryOp op, size_t req)
Gavin Howard6fbdb292018-02-27 15:44:48 -0700583{
584 BcStatus status;
Gavin Howard6b5f5bb2018-03-22 23:16:57 -0600585 BcNum num2, *ptr_a, *ptr_b;
Gavin Howard45ff3572018-05-15 16:33:58 -0600586 bool init = false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700587
Gavin Howard27fdfb92018-03-21 07:56:59 -0600588 assert(a && b && c && op);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700589
Gavin Howard6fbdb292018-02-27 15:44:48 -0700590 if (c == a) {
Gavin Howard1cc23f32018-02-28 16:09:33 -0700591 memcpy(&num2, c, sizeof(BcNum));
592 ptr_a = &num2;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700593 init = true;
594 }
595 else ptr_a = a;
596
597 if (c == b) {
598
599 if (c == a) {
600 ptr_b = ptr_a;
601 }
602 else {
Gavin Howard1cc23f32018-02-28 16:09:33 -0700603 memcpy(&num2, c, sizeof(BcNum));
604 ptr_b = &num2;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700605 init = true;
606 }
607 }
608 else ptr_b = b;
609
610 if (init) status = bc_num_init(c, req);
611 else status = bc_num_expand(c, req);
612
Gavin Howard9c4358c2018-03-22 20:11:28 -0600613 if (status) goto err;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700614 status = op(ptr_a, ptr_b, c, scale);
615
Gavin Howard9c4358c2018-03-22 20:11:28 -0600616err:
Gavin Howard1cc23f32018-02-28 16:09:33 -0700617 if (c == a || c == b) bc_num_free(&num2);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700618 return status;
619}
620
Gavin Howard3f68df72018-03-22 20:30:27 -0600621bool bc_num_strValid(const char *val, size_t base) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700622
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600623 BcDigit b;
Gavin Howard8317a792018-09-04 22:34:53 -0600624 bool small, radix = false;
625 size_t i, len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700626
627 if (!len) return true;
628
Gavin Howard9c4358c2018-03-22 20:11:28 -0600629 small = base <= 10;
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600630 b = (BcDigit) (small ? base + '0' : base - 9 + 'A');
Gavin Howard6fbdb292018-02-27 15:44:48 -0700631
Gavin Howard9c4358c2018-03-22 20:11:28 -0600632 for (i = 0; i < len; ++i) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700633
Gavin Howard8317a792018-09-04 22:34:53 -0600634 BcDigit c = val[i];
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600635
Gavin Howard8317a792018-09-04 22:34:53 -0600636 if (c == '.') {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700637
Gavin Howard9c4358c2018-03-22 20:11:28 -0600638 if (radix) return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700639
Gavin Howard9c4358c2018-03-22 20:11:28 -0600640 radix = true;
641 continue;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700642 }
Gavin Howard6fbdb292018-02-27 15:44:48 -0700643
Gavin Howard9c4358c2018-03-22 20:11:28 -0600644 if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b)))
645 return false;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700646 }
647
648 return true;
649}
650
Gavin Howard3f68df72018-03-22 20:30:27 -0600651BcStatus bc_num_parseDecimal(BcNum *n, const char *val) {
Gavin Howard6fbdb292018-02-27 15:44:48 -0700652
653 BcStatus status;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600654 size_t len, i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700655 const char *ptr;
Gavin Howard45ff3572018-05-15 16:33:58 -0600656 bool zero = true;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700657
658 for (i = 0; val[i] == '0'; ++i);
659
660 val += i;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700661 len = strlen(val);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700662 bc_num_zero(n);
663
664 if (len) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600665 for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.';
666 if ((status = bc_num_expand(n, len))) return status;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700667 }
Gavin Howard9c4358c2018-03-22 20:11:28 -0600668
669 if (zero) {
Gavin Howard021150b2018-03-10 15:40:42 -0700670 memset(n->num, 0, sizeof(BcDigit) * n->cap);
Gavin Howard6fbdb292018-02-27 15:44:48 -0700671 n->neg = false;
672 return BC_STATUS_SUCCESS;
673 }
674
675 ptr = strchr(val, '.');
676
Gavin Howardb29674f2018-03-22 22:24:58 -0600677 // Explicitly test for NULL here to produce either a 0 or 1.
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600678 n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1)));
Gavin Howard6fbdb292018-02-27 15:44:48 -0700679
Gavin Howard595886d2018-03-28 19:57:37 -0600680 for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.'))
Gavin Howardb29674f2018-03-22 22:24:58 -0600681 n->num[n->len] = val[i] - '0';
Gavin Howard6fbdb292018-02-27 15:44:48 -0700682
683 return BC_STATUS_SUCCESS;
684}
685
Gavin Howard3f68df72018-03-22 20:30:27 -0600686BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) {
Gavin Howardede51f02018-03-02 12:30:00 -0700687
688 BcStatus status;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600689 BcNum temp, mult, result;
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600690 BcDigit c = '\0';
Gavin Howardede51f02018-03-02 12:30:00 -0700691 bool zero;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600692 unsigned long v;
Gavin Howard8317a792018-09-04 22:34:53 -0600693 size_t i, digits, len = strlen(val);
Gavin Howardede51f02018-03-02 12:30:00 -0700694
Gavin Howardb29674f2018-03-22 22:24:58 -0600695 bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700696
Gavin Howard9c4358c2018-03-22 20:11:28 -0600697 for (zero = true, i = 0; zero && i < len; ++i)
698 zero = (val[i] == '.' || val[i] == '0');
Gavin Howardb29674f2018-03-22 22:24:58 -0600699 if (zero) return BC_STATUS_SUCCESS;
Gavin Howardede51f02018-03-02 12:30:00 -0700700
Gavin Howard9c4358c2018-03-22 20:11:28 -0600701 if ((status = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return status;
702 if ((status = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700703
Gavin Howardede51f02018-03-02 12:30:00 -0700704 for (i = 0; i < len && (c = val[i]) != '.'; ++i) {
705
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600706 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardfcb6ebb2018-03-09 10:41:06 -0700707
Gavin Howard9c4358c2018-03-22 20:11:28 -0600708 if ((status = bc_num_mul(n, base, &mult, 0))) goto int_err;
709 if ((status = bc_num_ulong2num(&temp, v))) goto int_err;
710 if ((status = bc_num_add(&mult, &temp, n, 0))) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700711 }
712
Gavin Howardb29674f2018-03-22 22:24:58 -0600713 if (i == len && !(c = val[i])) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700714 assert(c == '.');
Gavin Howard9c4358c2018-03-22 20:11:28 -0600715 if ((status = bc_num_init(&result, base->len))) goto int_err;
Gavin Howardede51f02018-03-02 12:30:00 -0700716
Gavin Howardede51f02018-03-02 12:30:00 -0700717 bc_num_zero(&result);
718 bc_num_one(&mult);
719
Gavin Howardb29674f2018-03-22 22:24:58 -0600720 for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) {
Gavin Howardede51f02018-03-02 12:30:00 -0700721
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600722 v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10);
Gavin Howardede51f02018-03-02 12:30:00 -0700723
Gavin Howard9c4358c2018-03-22 20:11:28 -0600724 if ((status = bc_num_mul(&result, base, &result, 0))) goto err;
725 if ((status = bc_num_ulong2num(&temp, v))) goto err;
726 if ((status = bc_num_add(&result, &temp, &result, 0))) goto err;
727 if ((status = bc_num_mul(&mult, base, &mult, 0))) goto err;
Gavin Howardede51f02018-03-02 12:30:00 -0700728 }
729
Gavin Howard9c4358c2018-03-22 20:11:28 -0600730 if ((status = bc_num_div(&result, &mult, &result, digits))) goto err;
731 if ((status = bc_num_add(n, &result, n, digits))) goto err;
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600732
733 if (n->len) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600734 if (n->rdx < digits && n->len) status = bc_num_extend(n, digits - n->rdx);
Gavin Howard5b24c3f2018-03-13 23:57:15 -0600735 }
736 else bc_num_zero(n);
Gavin Howardede51f02018-03-02 12:30:00 -0700737
738err:
Gavin Howardede51f02018-03-02 12:30:00 -0700739 bc_num_free(&result);
Gavin Howardede51f02018-03-02 12:30:00 -0700740int_err:
Gavin Howardede51f02018-03-02 12:30:00 -0700741 bc_num_free(&mult);
Gavin Howardede51f02018-03-02 12:30:00 -0700742mult_err:
Gavin Howardede51f02018-03-02 12:30:00 -0700743 bc_num_free(&temp);
Gavin Howardede51f02018-03-02 12:30:00 -0700744 return status;
Gavin Howard6fbdb292018-02-27 15:44:48 -0700745}
746
Gavin Howard80977b22018-09-06 14:29:13 -0600747BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) {
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600748
Gavin Howard0011a3a2018-03-29 23:11:32 -0600749 if (*nchars == line_len - 1) {
Gavin Howarda141a0f2018-03-23 13:16:10 -0600750 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
751 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700752 *nchars = 0;
753 }
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600754
Gavin Howard80977b22018-09-06 14:29:13 -0600755 return BC_STATUS_SUCCESS;
756}
757
758BcStatus bc_num_printDigits(size_t num, size_t width, bool radix,
759 size_t *nchars, size_t line_len)
760{
761 BcStatus status;
762 size_t exp, pow;
763
Gavin Howarda141a0f2018-03-23 13:16:10 -0600764 if (*nchars || radix) {
765 if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR;
766 ++(*nchars);
767 }
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600768
Gavin Howard80977b22018-09-06 14:29:13 -0600769 if ((status = bc_num_printNewline(nchars, line_len))) return status;
770
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600771 for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10);
772
Gavin Howarda141a0f2018-03-23 13:16:10 -0600773 for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) {
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600774
Gavin Howardf6e3fb32018-08-09 13:48:59 -0600775 size_t div;
776
Gavin Howard80977b22018-09-06 14:29:13 -0600777 if ((status = bc_num_printNewline(nchars, line_len))) return status;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600778
779 div = num / pow;
Gavin Howard2ed2bfb2018-03-14 02:42:32 -0600780 num -= div * pow;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600781
Gavin Howarda141a0f2018-03-23 13:16:10 -0600782 if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700783 }
784
Gavin Howard6fbdb292018-02-27 15:44:48 -0700785 return BC_STATUS_SUCCESS;
786}
Gavin Howardfe679f02018-02-14 15:50:09 -0700787
Gavin Howard902a16c2018-05-16 01:22:53 -0600788BcStatus bc_num_printHex(size_t num, size_t width, bool radix,
Gavin Howard0011a3a2018-03-29 23:11:32 -0600789 size_t *nchars, size_t line_len)
Gavin Howard2682a1f2018-03-03 09:09:13 -0700790{
Gavin Howard80977b22018-09-06 14:29:13 -0600791 BcStatus status;
792
793 assert(width == 1);
794
795 if (radix) {
796 if ((status = bc_num_printNewline(nchars, line_len))) return status;
797 if (putchar('.') == EOF) return BC_STATUS_IO_ERR;
798 *nchars += 1;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700799 }
800
Gavin Howard80977b22018-09-06 14:29:13 -0600801 if ((status = bc_num_printNewline(nchars, line_len))) return status;
Gavin Howarda141a0f2018-03-23 13:16:10 -0600802 if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700803 *nchars = *nchars + width;
804
805 return BC_STATUS_SUCCESS;
806}
807
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600808BcStatus bc_num_printDecimal(BcNum *n, size_t *nchs, size_t len) {
Gavin Howard32f2beb2018-03-09 11:43:20 -0700809
810 BcStatus status;
811 size_t i;
Gavin Howard32f2beb2018-03-09 11:43:20 -0700812
Gavin Howard32f2beb2018-03-09 11:43:20 -0700813 if (n->neg) {
Gavin Howarda141a0f2018-03-23 13:16:10 -0600814 if (putchar('-') == EOF) return BC_STATUS_IO_ERR;
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600815 ++(*nchs);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700816 }
817
818 status = BC_STATUS_SUCCESS;
819
Gavin Howard9c4358c2018-03-22 20:11:28 -0600820 for (i = n->len - 1; !status && i < n->len; --i)
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600821 status = bc_num_printHex((size_t) n->num[i], 1, i == n->rdx - 1, nchs, len);
Gavin Howard32f2beb2018-03-09 11:43:20 -0700822
823 return status;
824}
825
Gavin Howard0011a3a2018-03-29 23:11:32 -0600826BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t,
827 size_t *nchars, size_t line_len)
Gavin Howardbc7cae82018-03-14 13:43:04 -0600828{
Gavin Howard2682a1f2018-03-03 09:09:13 -0700829 BcStatus status;
830 BcVec stack;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600831 BcNum intp, fracp, digit, frac_len;
832 size_t width, i;
Gavin Howard6e0f3c52018-08-27 17:28:22 -0600833 BcNumDigitOp print;
Gavin Howard6b5f5bb2018-03-22 23:16:57 -0600834 unsigned long dig, *ptr;
Gavin Howard8317a792018-09-04 22:34:53 -0600835 bool radix, neg = n->neg;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700836
Gavin Howard45ff3572018-05-15 16:33:58 -0600837 if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR;
838 nchars += neg;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700839
Gavin Howard7f45f1a2018-09-24 14:50:55 -0600840 n->neg = false;
841
Gavin Howard1dc4f092018-09-21 10:46:52 -0600842 if (base_t <= BC_NUM_MAX_IBASE) {
Gavin Howard2682a1f2018-03-03 09:09:13 -0700843 width = 1;
844 print = bc_num_printHex;
845 }
846 else {
847 width = (size_t) floor(log10((double) (base_t - 1)) + 1.0);
848 print = bc_num_printDigits;
849 }
850
Gavin Howard7f45f1a2018-09-24 14:50:55 -0600851 if ((status = bc_vec_init(&stack, sizeof(long), NULL))) goto stack_err;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600852 if ((status = bc_num_init(&intp, n->len))) goto int_err;
853 if ((status = bc_num_init(&fracp, n->rdx))) goto frac_err;
854 if ((status = bc_num_init(&digit, width))) goto digit_err;
855 if ((status = bc_num_copy(&intp, n))) goto frac_len_err;
Gavin Howarda50fc542018-03-29 17:25:38 -0600856
857 bc_num_truncate(&intp, intp.rdx);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600858 if ((status = bc_num_sub(n, &intp, &fracp, 0))) goto frac_len_err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700859
Gavin Howard53ed5bb2018-03-14 01:15:59 -0600860 while (intp.len) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600861 if ((status = bc_num_mod(&intp, base, &digit, 0))) goto frac_len_err;
862 if ((status = bc_num_ulong(&digit, &dig))) goto frac_len_err;
Gavin Howarda628aa22018-09-12 13:52:45 -0600863 if ((status = bc_vec_push(&stack, 1, &dig))) goto frac_len_err;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600864 if ((status = bc_num_div(&intp, base, &intp, 0))) goto frac_len_err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700865 }
866
867 for (i = 0; i < stack.len; ++i) {
Gavin Howard2682a1f2018-03-03 09:09:13 -0700868 ptr = bc_vec_item_rev(&stack, i);
Gavin Howard9c4358c2018-03-22 20:11:28 -0600869 assert(ptr);
Gavin Howard0011a3a2018-03-29 23:11:32 -0600870 status = print(*ptr, width, false, nchars, line_len);
871 if (status) goto frac_len_err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700872 }
873
Gavin Howard52e01262018-08-29 12:03:15 -0600874 if (!n->rdx || (status = bc_num_init(&frac_len, BC_NUM_INT(n))))
Gavin Howard45ff3572018-05-15 16:33:58 -0600875 goto frac_len_err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700876
877 bc_num_one(&frac_len);
878
Gavin Howard1819ecc2018-03-14 01:15:41 -0600879 for (radix = true; frac_len.len <= n->rdx; radix = false) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600880 if ((status = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err;
881 if ((status = bc_num_ulong(&fracp, &dig))) goto err;
882 if ((status = bc_num_ulong2num(&intp, dig))) goto err;
883 if ((status = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err;
Gavin Howard0011a3a2018-03-29 23:11:32 -0600884 if ((status = print(dig, width, radix, nchars, line_len))) goto err;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600885 if ((status = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700886 }
887
888err:
Gavin Howard2682a1f2018-03-03 09:09:13 -0700889 bc_num_free(&frac_len);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700890frac_len_err:
Gavin Howard2682a1f2018-03-03 09:09:13 -0700891 bc_num_free(&digit);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700892digit_err:
Gavin Howard2682a1f2018-03-03 09:09:13 -0700893 bc_num_free(&fracp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700894frac_err:
Gavin Howard2682a1f2018-03-03 09:09:13 -0700895 bc_num_free(&intp);
Gavin Howard2682a1f2018-03-03 09:09:13 -0700896int_err:
Gavin Howard2682a1f2018-03-03 09:09:13 -0700897 bc_vec_free(&stack);
Gavin Howard7f45f1a2018-09-24 14:50:55 -0600898stack_err:
899 n->neg = neg;
Gavin Howard2682a1f2018-03-03 09:09:13 -0700900 return status;
901}
902
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700903BcStatus bc_num_init(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700904
Gavin Howard27fdfb92018-03-21 07:56:59 -0600905 assert(n);
Gavin Howardb5c77212018-02-14 17:12:34 -0700906
Gavin Howard0b465d02018-02-15 11:41:16 -0700907 memset(n, 0, sizeof(BcNum));
Gavin Howardb5c77212018-02-14 17:12:34 -0700908
Gavin Howard5d74e962018-02-26 13:44:13 -0700909 request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600910 if (!(n->num = malloc(request))) return BC_STATUS_MALLOC_FAIL;
Gavin Howardb5c77212018-02-14 17:12:34 -0700911
Gavin Howard6f6dc942018-02-15 16:58:50 -0700912 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700913
914 return BC_STATUS_SUCCESS;
915}
916
Gavin Howard8d1f1db2018-02-23 11:29:41 -0700917BcStatus bc_num_expand(BcNum *n, size_t request) {
Gavin Howardb5c77212018-02-14 17:12:34 -0700918
Gavin Howard9c4358c2018-03-22 20:11:28 -0600919 BcDigit *temp;
920
Gavin Howard27fdfb92018-03-21 07:56:59 -0600921 assert(n && request);
Gavin Howardb5c77212018-02-14 17:12:34 -0700922
Gavin Howard9c4358c2018-03-22 20:11:28 -0600923 if (request <= n->cap) return BC_STATUS_SUCCESS;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600924 if (!(temp = realloc(n->num, request))) return BC_STATUS_MALLOC_FAIL;
Gavin Howardb5c77212018-02-14 17:12:34 -0700925
Gavin Howard9c4358c2018-03-22 20:11:28 -0600926 memset(temp + n->cap, 0, sizeof(char) * (request - n->cap));
Gavin Howard9c4358c2018-03-22 20:11:28 -0600927 n->num = temp;
928 n->cap = request;
Gavin Howardb5c77212018-02-14 17:12:34 -0700929
930 return BC_STATUS_SUCCESS;
931}
932
Gavin Howarded392aa2018-02-27 13:09:26 -0700933void bc_num_free(void *num) {
Gavin Howard9c4358c2018-03-22 20:11:28 -0600934 BcNum *n = (BcNum*) num;
Gavin Howardbc20f3d2018-09-11 10:26:04 -0600935 assert(n && n->num);
936 free(n->num);
Gavin Howardb5c77212018-02-14 17:12:34 -0700937}
938
Gavin Howard9c4358c2018-03-22 20:11:28 -0600939BcStatus bc_num_copy(BcNum *d, BcNum *s) {
Gavin Howard5a049c42018-02-15 11:24:11 -0700940
941 BcStatus status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700942
Gavin Howard9c4358c2018-03-22 20:11:28 -0600943 assert(d && s);
Gavin Howardf23448c2018-02-27 20:36:24 -0700944
Gavin Howard9c4358c2018-03-22 20:11:28 -0600945 if (d == s) return BC_STATUS_SUCCESS;
Gavin Howard9c4358c2018-03-22 20:11:28 -0600946 if ((status = bc_num_expand(d, s->cap))) return status;
Gavin Howard5a049c42018-02-15 11:24:11 -0700947
948 d->len = s->len;
949 d->neg = s->neg;
Gavin Howard8389bb22018-02-15 17:40:34 -0700950 d->rdx = s->rdx;
Gavin Howard5a049c42018-02-15 11:24:11 -0700951
Gavin Howard188b4ac2018-03-24 14:49:19 -0600952 memcpy(d->num, s->num, sizeof(BcDigit) * d->len);
953 memset(d->num + d->len, 0, sizeof(BcDigit) * (d->cap - d->len));
Gavin Howard5a049c42018-02-15 11:24:11 -0700954
955 return BC_STATUS_SUCCESS;
956}
957
Gavin Howard5cea43b2018-03-02 11:32:11 -0700958BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) {
Gavin Howard025d04d2018-02-20 13:53:28 -0700959
Gavin Howard3eb626f2018-02-14 13:54:35 -0700960 BcStatus status;
961
Gavin Howard27fdfb92018-03-21 07:56:59 -0600962 assert(n && val && base && base_t >= BC_NUM_MIN_BASE &&
Gavin Howard1dc4f092018-09-21 10:46:52 -0600963 base_t <= BC_NUM_MAX_IBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700964
Gavin Howardbdb2f922018-03-15 09:25:18 -0600965 if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING;
Gavin Howard3eb626f2018-02-14 13:54:35 -0700966
Gavin Howard5cea43b2018-03-02 11:32:11 -0700967 if (base_t == 10) status = bc_num_parseDecimal(n, val);
Gavin Howardede51f02018-03-02 12:30:00 -0700968 else status = bc_num_parseBase(n, val, base);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700969
970 return status;
971}
972
Gavin Howard0011a3a2018-03-29 23:11:32 -0600973BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline,
974 size_t *nchars, size_t line_len)
Gavin Howard152f3e82018-03-07 12:33:15 -0700975{
Gavin Howard3eb626f2018-02-14 13:54:35 -0700976 BcStatus status;
977
Gavin Howard9c4358c2018-03-22 20:11:28 -0600978 assert(n && base && nchars && base_t >= BC_NUM_MIN_BASE &&
Gavin Howard1dc4f092018-09-21 10:46:52 -0600979 base_t <= BC_MAX_OBASE);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700980
Gavin Howard0011a3a2018-03-29 23:11:32 -0600981 if (*nchars >= line_len) {
Gavin Howarda141a0f2018-03-23 13:16:10 -0600982 if (putchar('\\') == EOF) return BC_STATUS_IO_ERR;
983 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
Gavin Howardbc7cae82018-03-14 13:43:04 -0600984 *nchars = 0;
985 }
986
Gavin Howardeb9a8222018-03-13 09:25:56 -0600987 if (!n->len) {
Gavin Howarda141a0f2018-03-23 13:16:10 -0600988 if (putchar('0') == EOF) return BC_STATUS_IO_ERR;
Gavin Howardbc7cae82018-03-14 13:43:04 -0600989 ++(*nchars);
Gavin Howardec8f8f12018-03-03 09:47:19 -0700990 status = BC_STATUS_SUCCESS;
Gavin Howard9576c382018-03-03 08:11:53 -0700991 }
Gavin Howard0011a3a2018-03-29 23:11:32 -0600992 else if (base_t == 10) status = bc_num_printDecimal(n, nchars, line_len);
993 else status = bc_num_printBase(n, base, base_t, nchars, line_len);
Gavin Howard3eb626f2018-02-14 13:54:35 -0700994
Gavin Howard152f3e82018-03-07 12:33:15 -0700995 if (status) return status;
996
997 if (newline) {
Gavin Howarda141a0f2018-03-23 13:16:10 -0600998 if (putchar('\n') == EOF) return BC_STATUS_IO_ERR;
Gavin Howardbc7cae82018-03-14 13:43:04 -0600999 *nchars = 0;
Gavin Howard152f3e82018-03-07 12:33:15 -07001000 }
1001
Gavin Howard3eb626f2018-02-14 13:54:35 -07001002 return status;
1003}
1004
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001005BcStatus bc_num_ulong(BcNum *n, unsigned long *result) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001006
1007 size_t i;
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001008 unsigned long pow;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001009
Gavin Howard27fdfb92018-03-21 07:56:59 -06001010 assert(n && result);
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001011
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001012 if (n->neg) return BC_STATUS_MATH_NEGATIVE;
1013
Gavin Howard45ff3572018-05-15 16:33:58 -06001014 for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) {
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001015
Gavin Howard96710ad2018-09-21 10:27:45 -06001016 unsigned long prev = *result, powprev = pow;
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001017 *result += ((unsigned long) n->num[i]) * pow;
Gavin Howard025d04d2018-02-20 13:53:28 -07001018 pow *= 10;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001019
Gavin Howard96710ad2018-09-21 10:27:45 -06001020 if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW;
Gavin Howard68b8a5c2018-02-15 11:41:24 -07001021 }
1022
1023 return BC_STATUS_SUCCESS;
1024}
1025
Gavin Howard8d1f1db2018-02-23 11:29:41 -07001026BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001027
1028 BcStatus status;
Gavin Howard9c4358c2018-03-22 20:11:28 -06001029 size_t len, i;
Gavin Howard021150b2018-03-10 15:40:42 -07001030 BcDigit *ptr;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001031
Gavin Howard27fdfb92018-03-21 07:56:59 -06001032 assert(n);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001033
Gavin Howard025d04d2018-02-20 13:53:28 -07001034 bc_num_zero(n);
1035
Gavin Howard8e2cc692018-02-15 17:39:14 -07001036 if (!val) {
Gavin Howard8e2cc692018-02-15 17:39:14 -07001037 memset(n->num, 0, sizeof(char) * n->cap);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001038 return BC_STATUS_SUCCESS;
1039 }
1040
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001041 len = (size_t) ceil(log10(((double) ULONG_MAX) + 1.0));
Gavin Howard8e2cc692018-02-15 17:39:14 -07001042
Gavin Howard9c4358c2018-03-22 20:11:28 -06001043 if ((status = bc_num_expand(n, len))) return status;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001044
Gavin Howard9c4358c2018-03-22 20:11:28 -06001045 for (ptr = n->num, i = 0; val; ++i, ++n->len) {
Gavin Howard025d04d2018-02-20 13:53:28 -07001046 ptr[i] = (char) (val % 10);
Gavin Howard8e2cc692018-02-15 17:39:14 -07001047 val /= 10;
Gavin Howard8e2cc692018-02-15 17:39:14 -07001048 }
1049
Gavin Howard025d04d2018-02-20 13:53:28 -07001050 return BC_STATUS_SUCCESS;
1051}
1052
Gavin Howard4538c902018-08-29 14:31:40 -06001053BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howarda1c090a2018-03-05 14:20:33 -07001054 (void) scale;
Gavin Howard6e0f3c52018-08-27 17:28:22 -06001055 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s;
Gavin Howard4538c902018-08-29 14:31:40 -06001056 return bc_num_binary(a, b, res, false, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001057}
1058
Gavin Howard4538c902018-08-29 14:31:40 -06001059BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howarda1c090a2018-03-05 14:20:33 -07001060 (void) scale;
Gavin Howard6e0f3c52018-08-27 17:28:22 -06001061 BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a;
Gavin Howard4538c902018-08-29 14:31:40 -06001062 return bc_num_binary(a, b, res, true, op, BC_NUM_AREQ(a, b));
Gavin Howard3eb626f2018-02-14 13:54:35 -07001063}
1064
Gavin Howard4538c902018-08-29 14:31:40 -06001065BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howard52e01262018-08-29 12:03:15 -06001066 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard4538c902018-08-29 14:31:40 -06001067 return bc_num_binary(a, b, res, scale, bc_num_alg_m, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001068}
1069
Gavin Howard4538c902018-08-29 14:31:40 -06001070BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howard52e01262018-08-29 12:03:15 -06001071 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard4538c902018-08-29 14:31:40 -06001072 return bc_num_binary(a, b, res, scale, bc_num_alg_d, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001073}
1074
Gavin Howard4538c902018-08-29 14:31:40 -06001075BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
Gavin Howard52e01262018-08-29 12:03:15 -06001076 size_t req = BC_NUM_MREQ(a, b, scale);
Gavin Howard4538c902018-08-29 14:31:40 -06001077 return bc_num_binary(a, b, res, scale, bc_num_alg_mod, req);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001078}
1079
Gavin Howard4538c902018-08-29 14:31:40 -06001080BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *res, size_t scale) {
1081 return bc_num_binary(a, b, res, scale, bc_num_alg_p, a->len * b->len + 1);
Gavin Howard3eb626f2018-02-14 13:54:35 -07001082}
1083
Gavin Howard4538c902018-08-29 14:31:40 -06001084BcStatus bc_num_sqrt(BcNum *a, BcNum *res, size_t scale) {
Gavin Howard954276b2018-05-16 01:43:58 -06001085
1086 BcStatus status;
Gavin Howarded742ce2018-08-29 14:23:12 -06001087 BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp;
Gavin Howard954276b2018-05-16 01:43:58 -06001088 size_t pow, len, digits, resrdx, req;
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001089 ssize_t cmp;
Gavin Howard954276b2018-05-16 01:43:58 -06001090
Gavin Howard4538c902018-08-29 14:31:40 -06001091 assert(a && res);
Gavin Howard954276b2018-05-16 01:43:58 -06001092
Gavin Howard40e769c2018-08-29 12:05:01 -06001093 req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a)+ 1) >> 1) + 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001094
Gavin Howard4538c902018-08-29 14:31:40 -06001095 if (res == a) {
1096 memcpy(&a2, res, sizeof(BcNum));
Gavin Howard954276b2018-05-16 01:43:58 -06001097 ptr_a = &a2;
Gavin Howard4538c902018-08-29 14:31:40 -06001098 status = bc_num_init(res, req);
Gavin Howard954276b2018-05-16 01:43:58 -06001099 }
1100 else {
1101 ptr_a = a;
Gavin Howard4538c902018-08-29 14:31:40 -06001102 status = bc_num_expand(res, req);
Gavin Howard954276b2018-05-16 01:43:58 -06001103 }
1104
1105 if (status) goto init_err;
1106
1107 if (!ptr_a->len) {
Gavin Howard4538c902018-08-29 14:31:40 -06001108 bc_num_zero(res);
Gavin Howardea131692018-08-29 13:00:14 -06001109 goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001110 }
Gavin Howardea131692018-08-29 13:00:14 -06001111 else if (ptr_a->neg) {
1112 status = BC_STATUS_MATH_NEG_SQRT;
1113 goto init_err;
1114 }
Gavin Howard954276b2018-05-16 01:43:58 -06001115 else if (BC_NUM_ONE(a)) {
Gavin Howard4538c902018-08-29 14:31:40 -06001116 bc_num_one(res);
1117 status = bc_num_extend(res, scale);
Gavin Howardea131692018-08-29 13:00:14 -06001118 goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001119 }
1120
Gavin Howard4538c902018-08-29 14:31:40 -06001121 memset(res->num, 0, res->cap * sizeof(BcDigit));
Gavin Howard954276b2018-05-16 01:43:58 -06001122 len = ptr_a->len;
1123
1124 scale = BC_MAX(scale, ptr_a->rdx) + 1;
1125
Gavin Howard8e8e5bc2018-08-29 12:54:57 -06001126 if ((status = bc_num_init(&num1, len))) goto init_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001127 if ((status = bc_num_init(&num2, num1.len))) goto num2_err;
Gavin Howarded742ce2018-08-29 14:23:12 -06001128 if ((status = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err;
Gavin Howard954276b2018-05-16 01:43:58 -06001129
Gavin Howarded742ce2018-08-29 14:23:12 -06001130 bc_num_one(&half);
1131 half.num[0] = 5;
1132 half.rdx = 1;
Gavin Howard954276b2018-05-16 01:43:58 -06001133
1134 len += scale;
1135
1136 if ((status = bc_num_init(&f, len))) goto f_err;
1137 if ((status = bc_num_init(&fprime, len + scale))) goto fprime_err;
1138
1139 x0 = &num1;
1140 x1 = &num2;
1141
1142 bc_num_one(x0);
1143
Gavin Howard52e01262018-08-29 12:03:15 -06001144 pow = BC_NUM_INT(ptr_a);
Gavin Howard954276b2018-05-16 01:43:58 -06001145
1146 if (pow) {
1147
1148 if (pow & 1) {
1149 x0->num[0] = 2;
1150 pow -= 1;
1151 }
1152 else {
1153 x0->num[0] = 6;
1154 pow -= 2;
1155 }
1156
1157 if ((status = bc_num_extend(x0, pow))) goto err;
Gavin Howarded742ce2018-08-29 14:23:12 -06001158
1159 // Make sure to move the radix back.
1160 x0->rdx -= pow;
Gavin Howard954276b2018-05-16 01:43:58 -06001161 }
1162
1163 cmp = 1;
1164 x0->rdx = digits = 0;
1165 resrdx = scale + 1;
Gavin Howard52e01262018-08-29 12:03:15 -06001166 len = BC_NUM_INT(x0) + resrdx;
Gavin Howard954276b2018-05-16 01:43:58 -06001167
Gavin Howard0dfe2922018-05-22 13:57:02 -06001168 while (!bcg.signe && cmp && digits <= len) {
Gavin Howard954276b2018-05-16 01:43:58 -06001169
Gavin Howarded742ce2018-08-29 14:23:12 -06001170 if ((status = bc_num_div(a, x0, &f, resrdx))) goto err;
1171 if ((status = bc_num_add(x0, &f, &fprime, resrdx))) goto err;
1172 if ((status = bc_num_mul(&fprime, &half, x1, resrdx))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001173
Gavin Howard6a804cf2018-05-17 16:54:12 -06001174 cmp = bc_num_cmp(x1, x0);
Gavin Howardf6e3fb32018-08-09 13:48:59 -06001175 digits = x1->len - (unsigned long long) llabs(cmp);
Gavin Howard954276b2018-05-16 01:43:58 -06001176
1177 temp = x0;
1178 x0 = x1;
1179 x1 = temp;
1180 }
1181
Gavin Howard0dfe2922018-05-22 13:57:02 -06001182 if (bcg.signe) {
1183 status = BC_STATUS_EXEC_SIGNAL;
1184 goto err;
1185 }
1186
Gavin Howard4538c902018-08-29 14:31:40 -06001187 if ((status = bc_num_copy(res, x0))) goto err;
Gavin Howard954276b2018-05-16 01:43:58 -06001188
Gavin Howard4538c902018-08-29 14:31:40 -06001189 if (res->rdx > --scale) bc_num_truncate(res, res->rdx - scale);
1190 else if (res->rdx < scale) status = bc_num_extend(res, scale - res->rdx);
Gavin Howard954276b2018-05-16 01:43:58 -06001191
1192err:
1193 bc_num_free(&fprime);
1194fprime_err:
1195 bc_num_free(&f);
1196f_err:
Gavin Howarded742ce2018-08-29 14:23:12 -06001197 bc_num_free(&half);
Gavin Howard954276b2018-05-16 01:43:58 -06001198two_err:
1199 bc_num_free(&num2);
1200num2_err:
1201 bc_num_free(&num1);
1202init_err:
Gavin Howard4538c902018-08-29 14:31:40 -06001203 if (res == a) bc_num_free(&a2);
Gavin Howard954276b2018-05-16 01:43:58 -06001204 return status;
Gavin Howard3eb626f2018-02-14 13:54:35 -07001205}