Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1 | /* |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 2 | * ***************************************************************************** |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 3 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 4 | * Copyright 2018 Gavin D. Howard |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 5 | * |
| 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 Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 17 | * ***************************************************************************** |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 18 | * |
| 19 | * Code for the number type. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <assert.h> |
| 24 | #include <stdbool.h> |
| 25 | #include <string.h> |
| 26 | |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 27 | #include <math.h> |
| 28 | |
| 29 | #include <limits.h> |
| 30 | |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 31 | #include <status.h> |
Gavin Howard | 3ba6c8d | 2018-02-15 12:23:35 -0700 | [diff] [blame] | 32 | #include <num.h> |
Gavin Howard | f507f23 | 2018-02-23 21:10:24 -0700 | [diff] [blame] | 33 | #include <vector.h> |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 34 | #include <vm.h> |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 35 | |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 36 | void 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 | |
| 44 | void 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 | |
| 51 | void 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 Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 59 | BcStatus bc_num_subArrays(BcDigit *n1, BcDigit *n2, size_t len) { |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 60 | size_t i, j; |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 61 | for (i = 0; !bcg.signe && i < len; ++i) { |
| 62 | for (n1[i] -= n2[i], j = 0; !bcg.signe && n1[i + j] < 0;) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 63 | n1[i + j++] += 10; |
Gavin Howard | cfc5faa | 2018-03-20 18:22:42 -0600 | [diff] [blame] | 64 | n1[i + j] -= 1; |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 65 | } |
| 66 | } |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 67 | return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 68 | } |
| 69 | |
Gavin Howard | 6a804cf | 2018-05-17 16:54:12 -0600 | [diff] [blame] | 70 | ssize_t bc_num_compare(BcDigit *n1, BcDigit *n2, size_t len) { |
| 71 | size_t i; |
Gavin Howard | 8317a79 | 2018-09-04 22:34:53 -0600 | [diff] [blame] | 72 | BcDigit c = 0; |
| 73 | for (i = len - 1; !bcg.signe && i < len && !(c = n1[i] - n2[i]); --i); |
Gavin Howard | 6a804cf | 2018-05-17 16:54:12 -0600 | [diff] [blame] | 74 | return (c < 0 ? -1 : 1) * (ssize_t) (i + 1); |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 75 | } |
| 76 | |
Gavin Howard | 6a804cf | 2018-05-17 16:54:12 -0600 | [diff] [blame] | 77 | ssize_t bc_num_cmp(BcNum *a, BcNum *b) { |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 78 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 79 | size_t i, min, a_int, b_int, diff; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 80 | BcDigit *max_num, *min_num; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 81 | bool a_max; |
Gavin Howard | 8317a79 | 2018-09-04 22:34:53 -0600 | [diff] [blame] | 82 | ssize_t cmp, neg = 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 83 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 84 | if (!a) return !b ? 0 : !b->neg * -2 + 1; |
| 85 | else if (!b) return a->neg * -2 + 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 86 | |
Gavin Howard | 2444f27 | 2018-09-11 12:25:53 -0600 | [diff] [blame] | 87 | if (!a->len) return (!b->neg * -2 + 1) * !!b->len; |
| 88 | else if (!b->len) return a->neg * -2 + 1; |
| 89 | |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 90 | if (a->neg) { |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 91 | if (b->neg) neg = -1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 92 | else return -1; |
| 93 | } |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 94 | else if (b->neg) return 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 95 | |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 96 | a_int = BC_NUM_INT(a); |
| 97 | b_int = BC_NUM_INT(b); |
Gavin Howard | 43ab933 | 2018-03-20 15:02:48 -0600 | [diff] [blame] | 98 | a_int -= b_int; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 99 | |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 100 | if (a_int) return (ssize_t) a_int; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 101 | |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 102 | a_max = a->rdx > b->rdx; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 103 | |
| 104 | if (a_max) { |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 105 | min = b->rdx; |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 106 | diff = a->rdx - b->rdx; |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 107 | max_num = a->num + diff; |
| 108 | min_num = b->num; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 109 | } |
| 110 | else { |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 111 | min = a->rdx; |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 112 | diff = b->rdx - a->rdx; |
Gavin Howard | 4e5ee2b | 2018-03-10 15:10:17 -0700 | [diff] [blame] | 113 | max_num = b->num + diff; |
| 114 | min_num = a->num; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Gavin Howard | 6a804cf | 2018-05-17 16:54:12 -0600 | [diff] [blame] | 117 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Gavin Howard | 2ba0711 | 2018-03-20 15:40:58 -0600 | [diff] [blame] | 118 | if (cmp) return cmp * (!a_max * -2 + 1) * neg; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 119 | |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 120 | for (max_num -= diff, i = diff - 1; !bcg.signe && i < diff; --i) { |
Gavin Howard | 86f32e9 | 2018-03-20 19:21:12 -0600 | [diff] [blame] | 121 | if (max_num[i]) return neg * (!a_max * -2 + 1); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Gavin Howard | d9734e5 | 2018-03-29 15:54:41 -0600 | [diff] [blame] | 127 | void bc_num_truncate(BcNum *n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 128 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 129 | BcDigit *ptr; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 130 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 131 | assert(places <= n->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 132 | |
Gavin Howard | d9734e5 | 2018-03-29 15:54:41 -0600 | [diff] [blame] | 133 | if (!places) return; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 134 | |
| 135 | ptr = n->num + places; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 136 | n->len -= places; |
| 137 | n->rdx -= places; |
| 138 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 139 | memmove(n->num, ptr, n->len * sizeof(BcDigit)); |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 140 | memset(n->num + n->len, 0, sizeof(BcDigit) * (n->cap - n->len)); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 143 | BcStatus bc_num_extend(BcNum *n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 144 | |
| 145 | BcStatus status; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 146 | BcDigit *ptr; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 147 | size_t len; |
| 148 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 149 | if (!places) return BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 150 | |
| 151 | len = n->len + places; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 152 | if (n->cap < len && (status = bc_num_expand(n, len))) return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 153 | |
| 154 | ptr = n->num + places; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 155 | memmove(ptr, n->num, sizeof(BcDigit) * n->len); |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 156 | memset(n->num, 0, sizeof(BcDigit) * places); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 157 | |
| 158 | n->len += places; |
Gavin Howard | cde142c | 2018-02-28 17:33:44 -0700 | [diff] [blame] | 159 | n->rdx += places; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 160 | |
| 161 | return BC_STATUS_SUCCESS; |
| 162 | } |
| 163 | |
Gavin Howard | c0f65d7 | 2018-09-01 16:59:04 -0600 | [diff] [blame] | 164 | void bc_num_clean(BcNum *n) { |
Gavin Howard | e57525d | 2018-09-01 16:56:21 -0600 | [diff] [blame] | 165 | 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 Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 170 | BcStatus 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 Howard | c0f65d7 | 2018-09-01 16:59:04 -0600 | [diff] [blame] | 177 | bc_num_clean(n); |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 178 | |
| 179 | return status; |
| 180 | } |
| 181 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 182 | BcStatus 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 Howard | 24434e1 | 2018-09-06 15:10:32 -0600 | [diff] [blame] | 196 | BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 197 | |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 198 | BcDigit *ptr, *ptr_a, *ptr_b, *ptr_c; |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 199 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 200 | BcDigit carry; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 201 | |
Gavin Howard | 73cce1a | 2018-09-06 15:16:09 -0600 | [diff] [blame] | 202 | // 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 Howard | 695e5d9 | 2018-09-06 15:04:33 -0600 | [diff] [blame] | 205 | if (!a->len) { |
| 206 | BcStatus status = bc_num_copy(c, b); |
Gavin Howard | 24434e1 | 2018-09-06 15:10:32 -0600 | [diff] [blame] | 207 | if (sub) c->neg = !c->neg; |
Gavin Howard | 695e5d9 | 2018-09-06 15:04:33 -0600 | [diff] [blame] | 208 | return status; |
| 209 | } |
Gavin Howard | f6964a1 | 2018-03-14 10:52:31 -0600 | [diff] [blame] | 210 | else if (!b->len) return bc_num_copy(c, a); |
| 211 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 212 | c->neg = a->neg; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 213 | memset(c->num, 0, c->cap * sizeof(BcDigit)); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 214 | |
| 215 | c->rdx = BC_MAX(a->rdx, b->rdx); |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 216 | min_rdx = BC_MIN(a->rdx, b->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 217 | c->len = 0; |
| 218 | |
| 219 | if (a->rdx > b->rdx) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 220 | diff = a->rdx - b->rdx; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 221 | ptr = a->num; |
| 222 | ptr_a = a->num + diff; |
| 223 | ptr_b = b->num; |
| 224 | } |
| 225 | else { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 226 | diff = b->rdx - a->rdx; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 227 | ptr = b->num; |
| 228 | ptr_a = a->num; |
| 229 | ptr_b = b->num + diff; |
| 230 | } |
| 231 | |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 232 | for (ptr_c = c->num, i = 0; i < diff; ++i, ++c->len) ptr_c[i] = ptr[i]; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 233 | |
| 234 | ptr_c += diff; |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 235 | a_int = BC_NUM_INT(a); |
| 236 | b_int = BC_NUM_INT(b); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 237 | |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 238 | if (a_int > b_int) { |
| 239 | min_int = b_int; |
| 240 | max = a_int; |
| 241 | ptr = ptr_a; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 242 | } |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 243 | else { |
| 244 | min_int = a_int; |
| 245 | max = b_int; |
| 246 | ptr = ptr_b; |
| 247 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 248 | |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 249 | for (carry = 0, i = 0; !bcg.signe && i < min_rdx + min_int; ++i, ++c->len) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 250 | ptr_c[i] = ptr_a[i] + ptr_b[i] + carry; |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 251 | carry = ptr_c[i] / 10; |
| 252 | ptr_c[i] %= 10; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 255 | for (; !bcg.signe && i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 256 | ptr_c[i] += ptr[i] + carry; |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 257 | carry = ptr_c[i] / 10; |
| 258 | ptr_c[i] %= 10; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 261 | if (bcg.signe) return BC_STATUS_EXEC_SIGNAL; |
| 262 | |
Gavin Howard | 647a880 | 2018-03-22 09:42:46 -0600 | [diff] [blame] | 263 | if (carry) c->num[c->len++] = carry; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 264 | |
| 265 | return BC_STATUS_SUCCESS; |
| 266 | } |
| 267 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 268 | BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 269 | |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 270 | BcStatus status; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 271 | ssize_t cmp; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 272 | BcNum *minuend, *subtrahend; |
Gavin Howard | 2de9399 | 2018-03-20 15:58:13 -0600 | [diff] [blame] | 273 | size_t start; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 274 | bool aneg, bneg, neg; |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 275 | |
Gavin Howard | b56eb1a | 2018-09-06 15:15:22 -0600 | [diff] [blame] | 276 | // 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 Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 278 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 279 | if (!a->len) { |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 280 | status = bc_num_copy(c, b); |
Gavin Howard | 8e90a47 | 2018-09-06 15:16:38 -0600 | [diff] [blame] | 281 | if (sub) c->neg = !b->neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 282 | return status; |
| 283 | } |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 284 | else if (!b->len) return bc_num_copy(c, a); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 285 | |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 286 | aneg = a->neg; |
| 287 | bneg = b->neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 288 | a->neg = b->neg = false; |
| 289 | |
Gavin Howard | 6a804cf | 2018-05-17 16:54:12 -0600 | [diff] [blame] | 290 | cmp = bc_num_cmp(a, b); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 291 | |
| 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 Howard | c8c64a5 | 2018-09-01 16:57:15 -0600 | [diff] [blame] | 300 | neg = a->neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 301 | minuend = a; |
| 302 | subtrahend = b; |
| 303 | } |
| 304 | else { |
Gavin Howard | c8c64a5 | 2018-09-01 16:57:15 -0600 | [diff] [blame] | 305 | neg = b->neg; |
| 306 | if (sub) neg = !neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 307 | minuend = b; |
| 308 | subtrahend = a; |
| 309 | } |
| 310 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 311 | if ((status = bc_num_copy(c, minuend))) return status; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 312 | c->neg = neg; |
| 313 | |
| 314 | if (c->rdx < subtrahend->rdx) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 315 | if ((status = bc_num_extend(c, subtrahend->rdx - c->rdx))) return status; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 316 | start = 0; |
| 317 | } |
Gavin Howard | d9f5c8e | 2018-03-10 14:08:13 -0700 | [diff] [blame] | 318 | else start = c->rdx - subtrahend->rdx; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 319 | |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 320 | status = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 321 | |
Gavin Howard | c0f65d7 | 2018-09-01 16:59:04 -0600 | [diff] [blame] | 322 | bc_num_clean(c); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 323 | |
| 324 | return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 327 | BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 328 | |
Gavin Howard | be261e7 | 2018-09-05 10:11:41 -0600 | [diff] [blame] | 329 | BcStatus status; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 330 | BcDigit carry; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 331 | size_t i, j, len; |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 332 | 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 Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 338 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 339 | if (!a->len || !b->len) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 340 | bc_num_zero(c); |
| 341 | return BC_STATUS_SUCCESS; |
| 342 | } |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 343 | else if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 344 | |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 345 | 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 Howard | be261e7 | 2018-09-05 10:11:41 -0600 | [diff] [blame] | 354 | if (status) return status; |
| 355 | |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 356 | return bc_num_retireMul(c, scale); |
| 357 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 358 | |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 359 | memset(c->num, 0, sizeof(BcDigit) * c->cap); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 360 | c->len = carry = len = 0; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 361 | |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 362 | for (i = 0; !bcg.signe && i < b->len; ++i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 363 | |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 364 | for (j = 0; !bcg.signe && j < a->len; ++j) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 365 | c->num[i + j] += a->num[j] * b->num[i] + carry; |
Gavin Howard | 5f806ee | 2018-03-03 23:30:31 -0700 | [diff] [blame] | 366 | carry = c->num[i + j] / 10; |
| 367 | c->num[i + j] %= 10; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 370 | if (bcg.signe) return BC_STATUS_EXEC_SIGNAL; |
| 371 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 372 | 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 Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 380 | if (bcg.signe) return BC_STATUS_EXEC_SIGNAL; |
| 381 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 382 | c->len = BC_MAX(len, c->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 383 | c->neg = !a->neg != !b->neg; |
| 384 | |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 385 | return bc_num_retireMul(c, scale); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 388 | BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 389 | |
| 390 | BcStatus status; |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 391 | BcDigit *n, *bptr, q; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 392 | size_t len, end, i; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 393 | BcNum copy; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 394 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 395 | if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 396 | else if (!a->len) { |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 397 | bc_num_zero(c); |
| 398 | return BC_STATUS_SUCCESS; |
| 399 | } |
Gavin Howard | b11bc8a | 2018-03-01 17:23:00 -0700 | [diff] [blame] | 400 | else if (BC_NUM_ONE(b)) { |
Gavin Howard | ae6e7d5 | 2018-03-14 10:13:43 -0600 | [diff] [blame] | 401 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 402 | if ((status = bc_num_copy(c, a))) return status; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 403 | if (b->neg) c->neg = !c->neg; |
Gavin Howard | ae6e7d5 | 2018-03-14 10:13:43 -0600 | [diff] [blame] | 404 | |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 405 | return bc_num_retireMul(c, scale); |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 408 | if ((status = bc_num_init(©, BC_NUM_MREQ(a, b, scale)))) return status; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 409 | if ((status = bc_num_copy(©, a))) goto err; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 410 | |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 411 | if ((len = b->len) > copy.len) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 412 | if ((status = bc_num_expand(©, len + 2))) goto err; |
| 413 | if ((status = bc_num_extend(©, len - copy.len))) goto err; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 414 | } |
| 415 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 416 | if (b->rdx > copy.rdx && (status = bc_num_extend(©, b->rdx - copy.rdx))) |
| 417 | goto err; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 418 | |
| 419 | copy.rdx -= b->rdx; |
| 420 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 421 | if (scale > copy.rdx && (status = bc_num_extend(©, scale - copy.rdx))) |
| 422 | goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 423 | |
Gavin Howard | c1a6a34 | 2018-03-05 12:10:14 -0700 | [diff] [blame] | 424 | if (b->rdx == b->len) { |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 425 | |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 426 | bool zero; |
| 427 | for (zero = true, i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 428 | |
Gavin Howard | 4bb4855 | 2018-09-07 19:03:00 -0600 | [diff] [blame] | 429 | if (i == len && zero) { |
Gavin Howard | 8b22bb2 | 2018-08-09 10:53:37 -0600 | [diff] [blame] | 430 | status = BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 431 | goto err; |
| 432 | } |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 433 | |
Gavin Howard | c1a6a34 | 2018-03-05 12:10:14 -0700 | [diff] [blame] | 434 | len -= i - 1; |
| 435 | } |
| 436 | |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 437 | if (copy.cap == copy.len && (status = bc_num_expand(©, copy.len + 1))) |
| 438 | goto err; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 439 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 440 | // We want an extra zero in front to make things simpler. |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 441 | copy.num[copy.len++] = 0; |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 442 | end = copy.len - len; |
| 443 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 444 | if ((status = bc_num_expand(c, copy.len))) goto err; |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 445 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 446 | bc_num_zero(c); |
| 447 | c->rdx = copy.rdx; |
| 448 | c->len = copy.len; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 449 | bptr = b->num; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 450 | |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 451 | for (i = end - 1; !bcg.signe && i < end; --i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 452 | |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 453 | n = copy.num + i; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 454 | |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 455 | for (q = 0; (!status && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q) |
| 456 | status = bc_num_subArrays(n, bptr, len); |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 457 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 458 | c->num[i] = q; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 461 | if (status) goto err; |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 462 | |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 463 | c->neg = !a->neg != !b->neg; |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 464 | status = bc_num_retireMul(c, scale); |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 465 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 466 | err: |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 467 | bc_num_free(©); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 468 | return status; |
| 469 | } |
| 470 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 471 | BcStatus bc_num_alg_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 472 | |
| 473 | BcStatus status; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 474 | BcNum c1, c2; |
Gavin Howard | 5d149cf | 2018-09-06 13:46:23 -0600 | [diff] [blame] | 475 | size_t len, tscale = BC_MAX(scale + b->rdx, a->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 476 | |
Gavin Howard | 8b25487 | 2018-03-14 01:13:35 -0600 | [diff] [blame] | 477 | 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 Howard | 5d149cf | 2018-09-06 13:46:23 -0600 | [diff] [blame] | 484 | if (scale) scale = tscale; |
| 485 | len = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 486 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 487 | 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 Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 490 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 491 | if ((status = bc_num_mul(&c1, b, &c2, scale))) goto err; |
Gavin Howard | 5d149cf | 2018-09-06 13:46:23 -0600 | [diff] [blame] | 492 | if ((status = bc_num_sub(a, &c2, c, scale))) goto err; |
| 493 | |
Gavin Howard | 07c2335 | 2018-09-06 14:18:54 -0600 | [diff] [blame] | 494 | if (tscale > c->rdx && c->len) status = bc_num_extend(c, tscale - c->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 495 | |
| 496 | err: |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 497 | bc_num_free(&c2); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 498 | c2_err: |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 499 | bc_num_free(&c1); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 500 | return status; |
| 501 | } |
| 502 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 503 | BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 504 | |
| 505 | BcStatus status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 506 | BcNum copy; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 507 | unsigned long pow; |
| 508 | size_t i, powrdx, resrdx; |
| 509 | bool neg, zero; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 510 | |
Gavin Howard | 2395858 | 2018-03-03 09:30:20 -0700 | [diff] [blame] | 511 | if (b->rdx) return BC_STATUS_MATH_NON_INTEGER; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 512 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 513 | if (!b->len) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 514 | bc_num_one(c); |
| 515 | return BC_STATUS_SUCCESS; |
| 516 | } |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 517 | else if (!a->len) { |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 518 | bc_num_zero(c); |
| 519 | return BC_STATUS_SUCCESS; |
| 520 | } |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 521 | else if (BC_NUM_ONE(b)) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 522 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 523 | if (!b->neg) status = bc_num_copy(c, a); |
| 524 | else status = bc_num_inv(a, c, scale); |
Gavin Howard | 5fc40e7 | 2018-03-05 12:11:09 -0700 | [diff] [blame] | 525 | |
| 526 | return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 527 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 528 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 529 | neg = b->neg; |
| 530 | b->neg = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 531 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 532 | if ((status = bc_num_ulong(b, &pow))) return status; |
| 533 | if ((status = bc_num_init(©, a->len))) return status; |
| 534 | if ((status = bc_num_copy(©, a))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 535 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 536 | if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx)); |
| 537 | |
| 538 | b->neg = neg; |
| 539 | |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 540 | for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) { |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 541 | powrdx <<= 1; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 542 | if ((status = bc_num_mul(©, ©, ©, powrdx))) goto err; |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 543 | } |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 544 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 545 | if ((status = bc_num_copy(c, ©))) goto err; |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 546 | if (bcg.signe) { |
| 547 | status = BC_STATUS_EXEC_SIGNAL; |
| 548 | goto err; |
| 549 | } |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 550 | |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 551 | resrdx = powrdx; |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 552 | |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 553 | for (pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) { |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 554 | |
| 555 | powrdx <<= 1; |
| 556 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 557 | if ((status = bc_num_mul(©, ©, ©, powrdx))) goto err; |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 558 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 559 | if (pow & 1) { |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 560 | resrdx += powrdx; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 561 | if ((status = bc_num_mul(c, ©, c, resrdx))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 562 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 565 | if (neg && (status = bc_num_inv(c, c, scale))) goto err; |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 566 | if (bcg.signe) { |
| 567 | status = BC_STATUS_EXEC_SIGNAL; |
| 568 | goto err; |
| 569 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 570 | |
Gavin Howard | a50fc54 | 2018-03-29 17:25:38 -0600 | [diff] [blame] | 571 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 572 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 573 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 574 | if (zero) bc_num_zero(c); |
| 575 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 576 | err: |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 577 | bc_num_free(©); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 578 | return status; |
| 579 | } |
| 580 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 581 | BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
Gavin Howard | 6e0f3c5 | 2018-08-27 17:28:22 -0600 | [diff] [blame] | 582 | BcNumBinaryOp op, size_t req) |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 583 | { |
| 584 | BcStatus status; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 585 | BcNum num2, *ptr_a, *ptr_b; |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 586 | bool init = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 587 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 588 | assert(a && b && c && op); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 589 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 590 | if (c == a) { |
Gavin Howard | 1cc23f3 | 2018-02-28 16:09:33 -0700 | [diff] [blame] | 591 | memcpy(&num2, c, sizeof(BcNum)); |
| 592 | ptr_a = &num2; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 593 | 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 Howard | 1cc23f3 | 2018-02-28 16:09:33 -0700 | [diff] [blame] | 603 | memcpy(&num2, c, sizeof(BcNum)); |
| 604 | ptr_b = &num2; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 605 | 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 Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 613 | if (status) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 614 | status = op(ptr_a, ptr_b, c, scale); |
| 615 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 616 | err: |
Gavin Howard | 1cc23f3 | 2018-02-28 16:09:33 -0700 | [diff] [blame] | 617 | if (c == a || c == b) bc_num_free(&num2); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 618 | return status; |
| 619 | } |
| 620 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 621 | bool bc_num_strValid(const char *val, size_t base) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 622 | |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 623 | BcDigit b; |
Gavin Howard | 8317a79 | 2018-09-04 22:34:53 -0600 | [diff] [blame] | 624 | bool small, radix = false; |
| 625 | size_t i, len = strlen(val); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 626 | |
| 627 | if (!len) return true; |
| 628 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 629 | small = base <= 10; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 630 | b = (BcDigit) (small ? base + '0' : base - 9 + 'A'); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 631 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 632 | for (i = 0; i < len; ++i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 633 | |
Gavin Howard | 8317a79 | 2018-09-04 22:34:53 -0600 | [diff] [blame] | 634 | BcDigit c = val[i]; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 635 | |
Gavin Howard | 8317a79 | 2018-09-04 22:34:53 -0600 | [diff] [blame] | 636 | if (c == '.') { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 637 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 638 | if (radix) return false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 639 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 640 | radix = true; |
| 641 | continue; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 642 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 643 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 644 | if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b))) |
| 645 | return false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | return true; |
| 649 | } |
| 650 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 651 | BcStatus bc_num_parseDecimal(BcNum *n, const char *val) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 652 | |
| 653 | BcStatus status; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 654 | size_t len, i; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 655 | const char *ptr; |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 656 | bool zero = true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 657 | |
| 658 | for (i = 0; val[i] == '0'; ++i); |
| 659 | |
| 660 | val += i; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 661 | len = strlen(val); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 662 | bc_num_zero(n); |
| 663 | |
| 664 | if (len) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 665 | for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.'; |
| 666 | if ((status = bc_num_expand(n, len))) return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 667 | } |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 668 | |
| 669 | if (zero) { |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 670 | memset(n->num, 0, sizeof(BcDigit) * n->cap); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 671 | n->neg = false; |
| 672 | return BC_STATUS_SUCCESS; |
| 673 | } |
| 674 | |
| 675 | ptr = strchr(val, '.'); |
| 676 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 677 | // Explicitly test for NULL here to produce either a 0 or 1. |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 678 | n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1))); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 679 | |
Gavin Howard | 595886d | 2018-03-28 19:57:37 -0600 | [diff] [blame] | 680 | for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.')) |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 681 | n->num[n->len] = val[i] - '0'; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 682 | |
| 683 | return BC_STATUS_SUCCESS; |
| 684 | } |
| 685 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 686 | BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 687 | |
| 688 | BcStatus status; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 689 | BcNum temp, mult, result; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 690 | BcDigit c = '\0'; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 691 | bool zero; |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 692 | unsigned long v; |
Gavin Howard | 8317a79 | 2018-09-04 22:34:53 -0600 | [diff] [blame] | 693 | size_t i, digits, len = strlen(val); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 694 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 695 | bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 696 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 697 | for (zero = true, i = 0; zero && i < len; ++i) |
| 698 | zero = (val[i] == '.' || val[i] == '0'); |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 699 | if (zero) return BC_STATUS_SUCCESS; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 700 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 701 | 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 Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 703 | |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 704 | for (i = 0; i < len && (c = val[i]) != '.'; ++i) { |
| 705 | |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 706 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
Gavin Howard | fcb6ebb | 2018-03-09 10:41:06 -0700 | [diff] [blame] | 707 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 708 | 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 Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 711 | } |
| 712 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 713 | if (i == len && !(c = val[i])) goto int_err; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 714 | assert(c == '.'); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 715 | if ((status = bc_num_init(&result, base->len))) goto int_err; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 716 | |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 717 | bc_num_zero(&result); |
| 718 | bc_num_one(&mult); |
| 719 | |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 720 | for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 721 | |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 722 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 723 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 724 | 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 Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 730 | if ((status = bc_num_div(&result, &mult, &result, digits))) goto err; |
| 731 | if ((status = bc_num_add(n, &result, n, digits))) goto err; |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 732 | |
| 733 | if (n->len) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 734 | if (n->rdx < digits && n->len) status = bc_num_extend(n, digits - n->rdx); |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 735 | } |
| 736 | else bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 737 | |
| 738 | err: |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 739 | bc_num_free(&result); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 740 | int_err: |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 741 | bc_num_free(&mult); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 742 | mult_err: |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 743 | bc_num_free(&temp); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 744 | return status; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 747 | BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) { |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 748 | |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 749 | if (*nchars == line_len - 1) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 750 | if (putchar('\\') == EOF) return BC_STATUS_IO_ERR; |
| 751 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 752 | *nchars = 0; |
| 753 | } |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 754 | |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 755 | return BC_STATUS_SUCCESS; |
| 756 | } |
| 757 | |
| 758 | BcStatus 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 Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 764 | if (*nchars || radix) { |
| 765 | if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR; |
| 766 | ++(*nchars); |
| 767 | } |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 768 | |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 769 | if ((status = bc_num_printNewline(nchars, line_len))) return status; |
| 770 | |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 771 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10); |
| 772 | |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 773 | for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) { |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 774 | |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 775 | size_t div; |
| 776 | |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 777 | if ((status = bc_num_printNewline(nchars, line_len))) return status; |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 778 | |
| 779 | div = num / pow; |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 780 | num -= div * pow; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 781 | |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 782 | if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 783 | } |
| 784 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 785 | return BC_STATUS_SUCCESS; |
| 786 | } |
Gavin Howard | fe679f0 | 2018-02-14 15:50:09 -0700 | [diff] [blame] | 787 | |
Gavin Howard | 902a16c | 2018-05-16 01:22:53 -0600 | [diff] [blame] | 788 | BcStatus bc_num_printHex(size_t num, size_t width, bool radix, |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 789 | size_t *nchars, size_t line_len) |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 790 | { |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 791 | 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 Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 801 | if ((status = bc_num_printNewline(nchars, line_len))) return status; |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 802 | if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 803 | *nchars = *nchars + width; |
| 804 | |
| 805 | return BC_STATUS_SUCCESS; |
| 806 | } |
| 807 | |
Gavin Howard | 6e0f3c5 | 2018-08-27 17:28:22 -0600 | [diff] [blame] | 808 | BcStatus bc_num_printDecimal(BcNum *n, size_t *nchs, size_t len) { |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 809 | |
| 810 | BcStatus status; |
| 811 | size_t i; |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 812 | |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 813 | if (n->neg) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 814 | if (putchar('-') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 6e0f3c5 | 2018-08-27 17:28:22 -0600 | [diff] [blame] | 815 | ++(*nchs); |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | status = BC_STATUS_SUCCESS; |
| 819 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 820 | for (i = n->len - 1; !status && i < n->len; --i) |
Gavin Howard | 6e0f3c5 | 2018-08-27 17:28:22 -0600 | [diff] [blame] | 821 | status = bc_num_printHex((size_t) n->num[i], 1, i == n->rdx - 1, nchs, len); |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 822 | |
| 823 | return status; |
| 824 | } |
| 825 | |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 826 | BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t, |
| 827 | size_t *nchars, size_t line_len) |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 828 | { |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 829 | BcStatus status; |
| 830 | BcVec stack; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 831 | BcNum intp, fracp, digit, frac_len; |
| 832 | size_t width, i; |
Gavin Howard | 6e0f3c5 | 2018-08-27 17:28:22 -0600 | [diff] [blame] | 833 | BcNumDigitOp print; |
Gavin Howard | 6b5f5bb | 2018-03-22 23:16:57 -0600 | [diff] [blame] | 834 | unsigned long dig, *ptr; |
Gavin Howard | 8317a79 | 2018-09-04 22:34:53 -0600 | [diff] [blame] | 835 | bool radix, neg = n->neg; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 836 | |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 837 | if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR; |
| 838 | nchars += neg; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 839 | |
Gavin Howard | 7f45f1a | 2018-09-24 14:50:55 -0600 | [diff] [blame^] | 840 | n->neg = false; |
| 841 | |
Gavin Howard | 1dc4f09 | 2018-09-21 10:46:52 -0600 | [diff] [blame] | 842 | if (base_t <= BC_NUM_MAX_IBASE) { |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 843 | 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 Howard | 7f45f1a | 2018-09-24 14:50:55 -0600 | [diff] [blame^] | 851 | if ((status = bc_vec_init(&stack, sizeof(long), NULL))) goto stack_err; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 852 | 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 Howard | a50fc54 | 2018-03-29 17:25:38 -0600 | [diff] [blame] | 856 | |
| 857 | bc_num_truncate(&intp, intp.rdx); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 858 | if ((status = bc_num_sub(n, &intp, &fracp, 0))) goto frac_len_err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 859 | |
Gavin Howard | 53ed5bb | 2018-03-14 01:15:59 -0600 | [diff] [blame] | 860 | while (intp.len) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 861 | 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 Howard | a628aa2 | 2018-09-12 13:52:45 -0600 | [diff] [blame] | 863 | if ((status = bc_vec_push(&stack, 1, &dig))) goto frac_len_err; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 864 | if ((status = bc_num_div(&intp, base, &intp, 0))) goto frac_len_err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | for (i = 0; i < stack.len; ++i) { |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 868 | ptr = bc_vec_item_rev(&stack, i); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 869 | assert(ptr); |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 870 | status = print(*ptr, width, false, nchars, line_len); |
| 871 | if (status) goto frac_len_err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 874 | if (!n->rdx || (status = bc_num_init(&frac_len, BC_NUM_INT(n)))) |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 875 | goto frac_len_err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 876 | |
| 877 | bc_num_one(&frac_len); |
| 878 | |
Gavin Howard | 1819ecc | 2018-03-14 01:15:41 -0600 | [diff] [blame] | 879 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 880 | 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 Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 884 | if ((status = print(dig, width, radix, nchars, line_len))) goto err; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 885 | if ((status = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | err: |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 889 | bc_num_free(&frac_len); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 890 | frac_len_err: |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 891 | bc_num_free(&digit); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 892 | digit_err: |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 893 | bc_num_free(&fracp); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 894 | frac_err: |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 895 | bc_num_free(&intp); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 896 | int_err: |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 897 | bc_vec_free(&stack); |
Gavin Howard | 7f45f1a | 2018-09-24 14:50:55 -0600 | [diff] [blame^] | 898 | stack_err: |
| 899 | n->neg = neg; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 900 | return status; |
| 901 | } |
| 902 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 903 | BcStatus bc_num_init(BcNum *n, size_t request) { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 904 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 905 | assert(n); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 906 | |
Gavin Howard | 0b465d0 | 2018-02-15 11:41:16 -0700 | [diff] [blame] | 907 | memset(n, 0, sizeof(BcNum)); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 908 | |
Gavin Howard | 5d74e96 | 2018-02-26 13:44:13 -0700 | [diff] [blame] | 909 | request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 910 | if (!(n->num = malloc(request))) return BC_STATUS_MALLOC_FAIL; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 911 | |
Gavin Howard | 6f6dc94 | 2018-02-15 16:58:50 -0700 | [diff] [blame] | 912 | n->cap = request; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 913 | |
| 914 | return BC_STATUS_SUCCESS; |
| 915 | } |
| 916 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 917 | BcStatus bc_num_expand(BcNum *n, size_t request) { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 918 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 919 | BcDigit *temp; |
| 920 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 921 | assert(n && request); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 922 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 923 | if (request <= n->cap) return BC_STATUS_SUCCESS; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 924 | if (!(temp = realloc(n->num, request))) return BC_STATUS_MALLOC_FAIL; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 925 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 926 | memset(temp + n->cap, 0, sizeof(char) * (request - n->cap)); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 927 | n->num = temp; |
| 928 | n->cap = request; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 929 | |
| 930 | return BC_STATUS_SUCCESS; |
| 931 | } |
| 932 | |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 933 | void bc_num_free(void *num) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 934 | BcNum *n = (BcNum*) num; |
Gavin Howard | bc20f3d | 2018-09-11 10:26:04 -0600 | [diff] [blame] | 935 | assert(n && n->num); |
| 936 | free(n->num); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 939 | BcStatus bc_num_copy(BcNum *d, BcNum *s) { |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 940 | |
| 941 | BcStatus status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 942 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 943 | assert(d && s); |
Gavin Howard | f23448c | 2018-02-27 20:36:24 -0700 | [diff] [blame] | 944 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 945 | if (d == s) return BC_STATUS_SUCCESS; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 946 | if ((status = bc_num_expand(d, s->cap))) return status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 947 | |
| 948 | d->len = s->len; |
| 949 | d->neg = s->neg; |
Gavin Howard | 8389bb2 | 2018-02-15 17:40:34 -0700 | [diff] [blame] | 950 | d->rdx = s->rdx; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 951 | |
Gavin Howard | 188b4ac | 2018-03-24 14:49:19 -0600 | [diff] [blame] | 952 | memcpy(d->num, s->num, sizeof(BcDigit) * d->len); |
| 953 | memset(d->num + d->len, 0, sizeof(BcDigit) * (d->cap - d->len)); |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 954 | |
| 955 | return BC_STATUS_SUCCESS; |
| 956 | } |
| 957 | |
Gavin Howard | 5cea43b | 2018-03-02 11:32:11 -0700 | [diff] [blame] | 958 | BcStatus bc_num_parse(BcNum *n, const char *val, BcNum *base, size_t base_t) { |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 959 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 960 | BcStatus status; |
| 961 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 962 | assert(n && val && base && base_t >= BC_NUM_MIN_BASE && |
Gavin Howard | 1dc4f09 | 2018-09-21 10:46:52 -0600 | [diff] [blame] | 963 | base_t <= BC_NUM_MAX_IBASE); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 964 | |
Gavin Howard | bdb2f92 | 2018-03-15 09:25:18 -0600 | [diff] [blame] | 965 | if (!bc_num_strValid(val, base_t)) return BC_STATUS_MATH_BAD_STRING; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 966 | |
Gavin Howard | 5cea43b | 2018-03-02 11:32:11 -0700 | [diff] [blame] | 967 | if (base_t == 10) status = bc_num_parseDecimal(n, val); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 968 | else status = bc_num_parseBase(n, val, base); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 969 | |
| 970 | return status; |
| 971 | } |
| 972 | |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 973 | BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline, |
| 974 | size_t *nchars, size_t line_len) |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 975 | { |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 976 | BcStatus status; |
| 977 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 978 | assert(n && base && nchars && base_t >= BC_NUM_MIN_BASE && |
Gavin Howard | 1dc4f09 | 2018-09-21 10:46:52 -0600 | [diff] [blame] | 979 | base_t <= BC_MAX_OBASE); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 980 | |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 981 | if (*nchars >= line_len) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 982 | if (putchar('\\') == EOF) return BC_STATUS_IO_ERR; |
| 983 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 984 | *nchars = 0; |
| 985 | } |
| 986 | |
Gavin Howard | eb9a822 | 2018-03-13 09:25:56 -0600 | [diff] [blame] | 987 | if (!n->len) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 988 | if (putchar('0') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 989 | ++(*nchars); |
Gavin Howard | ec8f8f1 | 2018-03-03 09:47:19 -0700 | [diff] [blame] | 990 | status = BC_STATUS_SUCCESS; |
Gavin Howard | 9576c38 | 2018-03-03 08:11:53 -0700 | [diff] [blame] | 991 | } |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 992 | 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 Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 994 | |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 995 | if (status) return status; |
| 996 | |
| 997 | if (newline) { |
Gavin Howard | a141a0f | 2018-03-23 13:16:10 -0600 | [diff] [blame] | 998 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 999 | *nchars = 0; |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1002 | return status; |
| 1003 | } |
| 1004 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1005 | BcStatus bc_num_ulong(BcNum *n, unsigned long *result) { |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1006 | |
| 1007 | size_t i; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 1008 | unsigned long pow; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1009 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 1010 | assert(n && result); |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1011 | |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1012 | if (n->neg) return BC_STATUS_MATH_NEGATIVE; |
| 1013 | |
Gavin Howard | 45ff357 | 2018-05-15 16:33:58 -0600 | [diff] [blame] | 1014 | for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1015 | |
Gavin Howard | 96710ad | 2018-09-21 10:27:45 -0600 | [diff] [blame] | 1016 | unsigned long prev = *result, powprev = pow; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 1017 | *result += ((unsigned long) n->num[i]) * pow; |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1018 | pow *= 10; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1019 | |
Gavin Howard | 96710ad | 2018-09-21 10:27:45 -0600 | [diff] [blame] | 1020 | if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | return BC_STATUS_SUCCESS; |
| 1024 | } |
| 1025 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1026 | BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) { |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1027 | |
| 1028 | BcStatus status; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1029 | size_t len, i; |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 1030 | BcDigit *ptr; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1031 | |
Gavin Howard | 27fdfb9 | 2018-03-21 07:56:59 -0600 | [diff] [blame] | 1032 | assert(n); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1033 | |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1034 | bc_num_zero(n); |
| 1035 | |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1036 | if (!val) { |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1037 | memset(n->num, 0, sizeof(char) * n->cap); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1038 | return BC_STATUS_SUCCESS; |
| 1039 | } |
| 1040 | |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 1041 | len = (size_t) ceil(log10(((double) ULONG_MAX) + 1.0)); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1042 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1043 | if ((status = bc_num_expand(n, len))) return status; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1044 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1045 | for (ptr = n->num, i = 0; val; ++i, ++n->len) { |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1046 | ptr[i] = (char) (val % 10); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1047 | val /= 10; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1050 | return BC_STATUS_SUCCESS; |
| 1051 | } |
| 1052 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1053 | BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *res, size_t scale) { |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 1054 | (void) scale; |
Gavin Howard | 6e0f3c5 | 2018-08-27 17:28:22 -0600 | [diff] [blame] | 1055 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s; |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1056 | return bc_num_binary(a, b, res, false, op, BC_NUM_AREQ(a, b)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1059 | BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *res, size_t scale) { |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 1060 | (void) scale; |
Gavin Howard | 6e0f3c5 | 2018-08-27 17:28:22 -0600 | [diff] [blame] | 1061 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a; |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1062 | return bc_num_binary(a, b, res, true, op, BC_NUM_AREQ(a, b)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1065 | BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *res, size_t scale) { |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 1066 | size_t req = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1067 | return bc_num_binary(a, b, res, scale, bc_num_alg_m, req); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1068 | } |
| 1069 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1070 | BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *res, size_t scale) { |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 1071 | size_t req = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1072 | return bc_num_binary(a, b, res, scale, bc_num_alg_d, req); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1073 | } |
| 1074 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1075 | BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *res, size_t scale) { |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 1076 | size_t req = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1077 | return bc_num_binary(a, b, res, scale, bc_num_alg_mod, req); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1080 | BcStatus 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 Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1084 | BcStatus bc_num_sqrt(BcNum *a, BcNum *res, size_t scale) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1085 | |
| 1086 | BcStatus status; |
Gavin Howard | ed742ce | 2018-08-29 14:23:12 -0600 | [diff] [blame] | 1087 | BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1088 | size_t pow, len, digits, resrdx, req; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 1089 | ssize_t cmp; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1090 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1091 | assert(a && res); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1092 | |
Gavin Howard | 40e769c | 2018-08-29 12:05:01 -0600 | [diff] [blame] | 1093 | req = BC_MAX(scale, a->rdx) + ((BC_NUM_INT(a)+ 1) >> 1) + 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1094 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1095 | if (res == a) { |
| 1096 | memcpy(&a2, res, sizeof(BcNum)); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1097 | ptr_a = &a2; |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1098 | status = bc_num_init(res, req); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1099 | } |
| 1100 | else { |
| 1101 | ptr_a = a; |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1102 | status = bc_num_expand(res, req); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | if (status) goto init_err; |
| 1106 | |
| 1107 | if (!ptr_a->len) { |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1108 | bc_num_zero(res); |
Gavin Howard | ea13169 | 2018-08-29 13:00:14 -0600 | [diff] [blame] | 1109 | goto init_err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1110 | } |
Gavin Howard | ea13169 | 2018-08-29 13:00:14 -0600 | [diff] [blame] | 1111 | else if (ptr_a->neg) { |
| 1112 | status = BC_STATUS_MATH_NEG_SQRT; |
| 1113 | goto init_err; |
| 1114 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1115 | else if (BC_NUM_ONE(a)) { |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1116 | bc_num_one(res); |
| 1117 | status = bc_num_extend(res, scale); |
Gavin Howard | ea13169 | 2018-08-29 13:00:14 -0600 | [diff] [blame] | 1118 | goto init_err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1119 | } |
| 1120 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1121 | memset(res->num, 0, res->cap * sizeof(BcDigit)); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1122 | len = ptr_a->len; |
| 1123 | |
| 1124 | scale = BC_MAX(scale, ptr_a->rdx) + 1; |
| 1125 | |
Gavin Howard | 8e8e5bc | 2018-08-29 12:54:57 -0600 | [diff] [blame] | 1126 | if ((status = bc_num_init(&num1, len))) goto init_err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1127 | if ((status = bc_num_init(&num2, num1.len))) goto num2_err; |
Gavin Howard | ed742ce | 2018-08-29 14:23:12 -0600 | [diff] [blame] | 1128 | if ((status = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1129 | |
Gavin Howard | ed742ce | 2018-08-29 14:23:12 -0600 | [diff] [blame] | 1130 | bc_num_one(&half); |
| 1131 | half.num[0] = 5; |
| 1132 | half.rdx = 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1133 | |
| 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 Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 1144 | pow = BC_NUM_INT(ptr_a); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1145 | |
| 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 Howard | ed742ce | 2018-08-29 14:23:12 -0600 | [diff] [blame] | 1158 | |
| 1159 | // Make sure to move the radix back. |
| 1160 | x0->rdx -= pow; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | cmp = 1; |
| 1164 | x0->rdx = digits = 0; |
| 1165 | resrdx = scale + 1; |
Gavin Howard | 52e0126 | 2018-08-29 12:03:15 -0600 | [diff] [blame] | 1166 | len = BC_NUM_INT(x0) + resrdx; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1167 | |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 1168 | while (!bcg.signe && cmp && digits <= len) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1169 | |
Gavin Howard | ed742ce | 2018-08-29 14:23:12 -0600 | [diff] [blame] | 1170 | 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 Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1173 | |
Gavin Howard | 6a804cf | 2018-05-17 16:54:12 -0600 | [diff] [blame] | 1174 | cmp = bc_num_cmp(x1, x0); |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 1175 | digits = x1->len - (unsigned long long) llabs(cmp); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1176 | |
| 1177 | temp = x0; |
| 1178 | x0 = x1; |
| 1179 | x1 = temp; |
| 1180 | } |
| 1181 | |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 1182 | if (bcg.signe) { |
| 1183 | status = BC_STATUS_EXEC_SIGNAL; |
| 1184 | goto err; |
| 1185 | } |
| 1186 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1187 | if ((status = bc_num_copy(res, x0))) goto err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1188 | |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1189 | 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 Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1191 | |
| 1192 | err: |
| 1193 | bc_num_free(&fprime); |
| 1194 | fprime_err: |
| 1195 | bc_num_free(&f); |
| 1196 | f_err: |
Gavin Howard | ed742ce | 2018-08-29 14:23:12 -0600 | [diff] [blame] | 1197 | bc_num_free(&half); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1198 | two_err: |
| 1199 | bc_num_free(&num2); |
| 1200 | num2_err: |
| 1201 | bc_num_free(&num1); |
| 1202 | init_err: |
Gavin Howard | 4538c90 | 2018-08-29 14:31:40 -0600 | [diff] [blame] | 1203 | if (res == a) bc_num_free(&a2); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1204 | return status; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1205 | } |