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> |
Gavin Howard | 411f732 | 2018-09-26 17:21:19 -0600 | [diff] [blame] | 24 | #include <stdbool.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 27 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 28 | #include <limits.h> |
| 29 | |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 30 | #include <status.h> |
Gavin Howard | 3ba6c8d | 2018-02-15 12:23:35 -0700 | [diff] [blame] | 31 | #include <num.h> |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 32 | #include <vm.h> |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 33 | |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 34 | void bc_num_zero(BcNum *n) { |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 35 | assert(n); |
| 36 | n->neg = (n->rdx = n->len = 0); |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void bc_num_one(BcNum *n) { |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 40 | assert(n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 41 | bc_num_zero(n); |
| 42 | n->len = 1; |
| 43 | n->num[0] = 1; |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void bc_num_ten(BcNum *n) { |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 47 | assert(n); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 48 | bc_num_zero(n); |
| 49 | n->len = 2; |
| 50 | n->num[0] = 0; |
| 51 | n->num[1] = 1; |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 52 | } |
| 53 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 54 | BcStatus bc_num_subArrays(BcDig *restrict n1, BcDig *restrict n2, size_t len) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 55 | size_t i, j; |
| 56 | for (i = 0; !bcg.signe && i < len; ++i) { |
| 57 | for (n1[i] -= n2[i], j = 0; !bcg.signe && n1[i + j] < 0;) { |
| 58 | n1[i + j++] += 10; |
| 59 | n1[i + j] -= 1; |
| 60 | } |
| 61 | } |
| 62 | return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 63 | } |
| 64 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 65 | ssize_t bc_num_compare(BcDig *restrict n1, BcDig *restrict n2, size_t len) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 66 | size_t i; |
Gavin Howard | d1a7836 | 2018-10-18 09:52:06 -0600 | [diff] [blame] | 67 | int c = 0; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 68 | for (i = len - 1; !bcg.signe && i < len && !(c = n1[i] - n2[i]); --i); |
Gavin Howard | 7fbb4e2 | 2018-10-18 11:43:17 -0600 | [diff] [blame] | 69 | return BC_NUM_NEG(i + 1, c < 0); |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 70 | } |
| 71 | |
Gavin Howard | 6a804cf | 2018-05-17 16:54:12 -0600 | [diff] [blame] | 72 | ssize_t bc_num_cmp(BcNum *a, BcNum *b) { |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 73 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 74 | size_t i, min, a_int, b_int, diff; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 75 | BcDig *max_num, *min_num; |
Gavin Howard | 7fbb4e2 | 2018-10-18 11:43:17 -0600 | [diff] [blame] | 76 | bool a_max, neg = false; |
| 77 | ssize_t cmp; |
| 78 | |
| 79 | assert(a && b); |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 80 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 81 | if (a == b) return 0; |
Gavin Howard | 7fbb4e2 | 2018-10-18 11:43:17 -0600 | [diff] [blame] | 82 | else if (!a->len) return BC_NUM_NEG(!!b->len, !b->neg); |
| 83 | else if (!b->len) return BC_NUM_NEG(1, a->neg); |
| 84 | else if (a->neg) { |
| 85 | if (b->neg) neg = true; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 86 | else return -1; |
| 87 | } |
| 88 | else if (b->neg) return 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 89 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 90 | a_int = BC_NUM_INT(a); |
| 91 | b_int = BC_NUM_INT(b); |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 92 | |
Gavin Howard | 7fbb4e2 | 2018-10-18 11:43:17 -0600 | [diff] [blame] | 93 | if ((a_int -= b_int)) return (ssize_t) a_int; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 94 | |
Gavin Howard | 7fbb4e2 | 2018-10-18 11:43:17 -0600 | [diff] [blame] | 95 | if ((a_max = a->rdx > b->rdx)) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 96 | min = b->rdx; |
| 97 | diff = a->rdx - b->rdx; |
| 98 | max_num = a->num + diff; |
| 99 | min_num = b->num; |
| 100 | } |
| 101 | else { |
| 102 | min = a->rdx; |
| 103 | diff = b->rdx - a->rdx; |
| 104 | max_num = b->num + diff; |
| 105 | min_num = a->num; |
| 106 | } |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 107 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 108 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Gavin Howard | 4097415 | 2018-10-18 14:23:01 -0600 | [diff] [blame] | 109 | if (cmp) return BC_NUM_NEG(cmp, (!a_max) != neg); |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 110 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 111 | for (max_num -= diff, i = diff - 1; !bcg.signe && i < diff; --i) { |
Gavin Howard | 4097415 | 2018-10-18 14:23:01 -0600 | [diff] [blame] | 112 | if (max_num[i]) return BC_NUM_NEG(1, (!a_max) != neg); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 113 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 114 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 115 | return 0; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Gavin Howard | d9734e5 | 2018-03-29 15:54:41 -0600 | [diff] [blame] | 118 | void bc_num_truncate(BcNum *n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 119 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 120 | assert(places <= n->rdx && places <= n->len); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 121 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 122 | if (!places) return; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 123 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 124 | n->len -= places; |
| 125 | n->rdx -= places; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 126 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 127 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 130 | BcStatus bc_num_extend(BcNum *n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 131 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 132 | BcStatus s; |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 133 | size_t len = n->len + places; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 134 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 135 | if (!places) return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 136 | if (n->cap < len && (s = bc_num_expand(n, len))) return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 137 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 138 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 139 | memset(n->num, 0, sizeof(BcDig) * places); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 140 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 141 | n->len += places; |
| 142 | n->rdx += places; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 143 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 144 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Gavin Howard | c0f65d7 | 2018-09-01 16:59:04 -0600 | [diff] [blame] | 147 | void bc_num_clean(BcNum *n) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 148 | while (n->len > 0 && !n->num[n->len - 1]) --n->len; |
Gavin Howard | 549d207 | 2018-10-22 08:38:09 -0600 | [diff] [blame] | 149 | if (!n->len) n->neg = false; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 150 | else if (n->len < n->rdx) n->len = n->rdx; |
Gavin Howard | e57525d | 2018-09-01 16:56:21 -0600 | [diff] [blame] | 151 | } |
| 152 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 153 | BcStatus bc_num_retireMul(BcNum *n, size_t scale, bool neg1, bool neg2) { |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 154 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 155 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 156 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 157 | if (n->rdx < scale) s = bc_num_extend(n, scale - n->rdx); |
| 158 | else bc_num_truncate(n, n->rdx - scale); |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 159 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 160 | bc_num_clean(n); |
Gavin Howard | 549d207 | 2018-10-22 08:38:09 -0600 | [diff] [blame] | 161 | if (n->len) n->neg = !neg1 != !neg2; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 162 | |
| 163 | return s; |
| 164 | } |
| 165 | |
| 166 | BcStatus bc_num_splitAt(BcNum *restrict n, size_t idx, BcNum *restrict a, |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 167 | BcNum *restrict b) |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 168 | { |
| 169 | BcStatus s = BC_STATUS_SUCCESS; |
| 170 | |
| 171 | if (idx < n->len) { |
| 172 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 173 | b->len = n->len - idx; |
| 174 | a->len = idx; |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 175 | a->rdx = b->rdx = 0; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 176 | |
| 177 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 178 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 179 | } |
| 180 | else { |
| 181 | bc_num_zero(b); |
| 182 | s = bc_num_copy(a, n); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | bc_num_clean(a); |
| 186 | bc_num_clean(b); |
| 187 | |
| 188 | return s; |
| 189 | } |
| 190 | |
| 191 | BcStatus bc_num_shift(BcNum *n, size_t places) { |
| 192 | |
| 193 | BcStatus s = BC_STATUS_SUCCESS; |
| 194 | |
| 195 | if (!places) return BC_STATUS_SUCCESS; |
| 196 | if (places + n->len > BC_MAX_NUM) return BC_STATUS_EXEC_NUM_LEN; |
| 197 | |
| 198 | if (n->rdx >= places) n->rdx -= places; |
| 199 | else { |
| 200 | s = bc_num_extend(n, places - n->rdx); |
| 201 | n->rdx = 0; |
| 202 | } |
| 203 | |
| 204 | bc_num_clean(n); |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 205 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 206 | return s; |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 207 | } |
| 208 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 209 | BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) { |
| 210 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 211 | BcNum one; |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 212 | BcDig num[2]; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 213 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 214 | one.cap = 2; |
| 215 | one.num = num; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 216 | bc_num_one(&one); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 217 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 218 | return bc_num_div(&one, a, b, scale); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 219 | } |
| 220 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 221 | BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 222 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 223 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 224 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
Gavin Howard | 60136d7 | 2018-10-15 14:28:19 -0600 | [diff] [blame] | 225 | int carry, in; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 226 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 227 | // Because this function doesn't need to use scale (per the bc spec), |
| 228 | // I am hijacking it to say whether it's doing an add or a subtract. |
Gavin Howard | 73cce1a | 2018-09-06 15:16:09 -0600 | [diff] [blame] | 229 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 230 | if (!a->len) { |
| 231 | BcStatus s = bc_num_copy(c, b); |
Gavin Howard | 38a10a1 | 2018-10-11 11:52:29 -0600 | [diff] [blame] | 232 | if (sub && c->len) c->neg = !c->neg; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 233 | return s; |
| 234 | } |
| 235 | else if (!b->len) return bc_num_copy(c, a); |
Gavin Howard | f6964a1 | 2018-03-14 10:52:31 -0600 | [diff] [blame] | 236 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 237 | c->neg = a->neg; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 238 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 239 | min_rdx = BC_MIN(a->rdx, b->rdx); |
| 240 | c->len = 0; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 241 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 242 | if (a->rdx > b->rdx) { |
| 243 | diff = a->rdx - b->rdx; |
| 244 | ptr = a->num; |
| 245 | ptr_a = a->num + diff; |
| 246 | ptr_b = b->num; |
| 247 | } |
| 248 | else { |
| 249 | diff = b->rdx - a->rdx; |
| 250 | ptr = b->num; |
| 251 | ptr_a = a->num; |
| 252 | ptr_b = b->num + diff; |
| 253 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 254 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 255 | 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] | 256 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 257 | ptr_c += diff; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 258 | |
Gavin Howard | 8ae3bf5 | 2018-10-19 13:30:17 -0600 | [diff] [blame] | 259 | if ((a_int = BC_NUM_INT(a)) > (b_int = BC_NUM_INT(b))) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 260 | min_int = b_int; |
| 261 | max = a_int; |
| 262 | ptr = ptr_a; |
| 263 | } |
| 264 | else { |
| 265 | min_int = a_int; |
| 266 | max = b_int; |
| 267 | ptr = ptr_b; |
| 268 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 269 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 270 | for (carry = 0, i = 0; !bcg.signe && i < min_rdx + min_int; ++i, ++c->len) { |
Gavin Howard | 60136d7 | 2018-10-15 14:28:19 -0600 | [diff] [blame] | 271 | in = ((int) ptr_a[i]) + ((int) ptr_b[i]) + carry; |
| 272 | carry = in / 10; |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 273 | ptr_c[i] = (BcDig) (in % 10); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 274 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 275 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 276 | for (; !bcg.signe && i < max + min_rdx; ++i, ++c->len) { |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 277 | in = ((int) ptr[i]) + carry; |
Gavin Howard | 60136d7 | 2018-10-15 14:28:19 -0600 | [diff] [blame] | 278 | carry = in / 10; |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 279 | ptr_c[i] = (BcDig) (in % 10); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 280 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 281 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 282 | if (bcg.signe) return BC_STATUS_EXEC_SIGNAL; |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 283 | |
Gavin Howard | 60136d7 | 2018-10-15 14:28:19 -0600 | [diff] [blame] | 284 | if (carry) c->num[c->len++] = (BcDig) carry; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 285 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 286 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 289 | BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 290 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 291 | BcStatus s; |
| 292 | ssize_t cmp; |
| 293 | BcNum *minuend, *subtrahend; |
| 294 | size_t start; |
| 295 | bool aneg, bneg, neg; |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 296 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 297 | // Because this function doesn't need to use scale (per the bc spec), |
| 298 | // I am hijacking it to say whether it's doing an add or a subtract. |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 299 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 300 | if (!a->len) { |
| 301 | s = bc_num_copy(c, b); |
Gavin Howard | 38a10a1 | 2018-10-11 11:52:29 -0600 | [diff] [blame] | 302 | if (sub && c->len) c->neg = !c->neg; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 303 | return s; |
| 304 | } |
| 305 | else if (!b->len) return bc_num_copy(c, a); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 306 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 307 | aneg = a->neg; |
| 308 | bneg = b->neg; |
| 309 | a->neg = b->neg = false; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 310 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 311 | cmp = bc_num_cmp(a, b); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 312 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 313 | a->neg = aneg; |
| 314 | b->neg = bneg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 315 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 316 | if (!cmp) { |
| 317 | bc_num_zero(c); |
| 318 | return BC_STATUS_SUCCESS; |
| 319 | } |
| 320 | else if (cmp > 0) { |
| 321 | neg = a->neg; |
| 322 | minuend = a; |
| 323 | subtrahend = b; |
| 324 | } |
| 325 | else { |
| 326 | neg = b->neg; |
| 327 | if (sub) neg = !neg; |
| 328 | minuend = b; |
| 329 | subtrahend = a; |
| 330 | } |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 331 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 332 | if ((s = bc_num_copy(c, minuend))) return s; |
| 333 | c->neg = neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 334 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 335 | if (c->rdx < subtrahend->rdx) { |
| 336 | if ((s = bc_num_extend(c, subtrahend->rdx - c->rdx))) return s; |
| 337 | start = 0; |
| 338 | } |
| 339 | else start = c->rdx - subtrahend->rdx; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 340 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 341 | s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 342 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 343 | bc_num_clean(c); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 344 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 345 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 348 | BcStatus bc_num_alg_k(BcNum *restrict a, BcNum *restrict b, BcNum *restrict c) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 349 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 350 | BcStatus s; |
Gavin Howard | 60136d7 | 2018-10-15 14:28:19 -0600 | [diff] [blame] | 351 | int carry; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 352 | size_t i, j, len, max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 353 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 354 | bool aone = BC_NUM_ONE(a); |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 355 | |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 356 | if (!a->len || !b->len) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 357 | bc_num_zero(c); |
| 358 | return BC_STATUS_SUCCESS; |
| 359 | } |
Gavin Howard | 23d8d25 | 2018-10-17 12:03:41 -0600 | [diff] [blame] | 360 | else if (aone || BC_NUM_ONE(b)) return bc_num_copy(c, aone ? b : a); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 361 | |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 362 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN || |
| 363 | a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 364 | { |
Gavin Howard | 23d8d25 | 2018-10-17 12:03:41 -0600 | [diff] [blame] | 365 | if ((s = bc_num_expand(c, a->len + b->len + 1))) return s; |
| 366 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 367 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
| 368 | c->len = carry = len = 0; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 369 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 370 | for (i = 0; !bcg.signe && i < b->len; ++i) { |
Gavin Howard | d1a7836 | 2018-10-18 09:52:06 -0600 | [diff] [blame] | 371 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 372 | for (j = 0; !bcg.signe && j < a->len; ++j) { |
Gavin Howard | 23d8d25 | 2018-10-17 12:03:41 -0600 | [diff] [blame] | 373 | int in = (int) c->num[i + j]; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 374 | in += ((int) a->num[j]) * ((int) b->num[i]) + carry; |
| 375 | carry = in / 10; |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 376 | c->num[i + j] = (BcDig) (in % 10); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 377 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 378 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 379 | if (bcg.signe) return BC_STATUS_EXEC_SIGNAL; |
| 380 | |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 381 | c->num[i + j] += (BcDig) carry; |
| 382 | len = BC_MAX(len, i + j + !!carry); |
| 383 | carry = 0; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 384 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 385 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 386 | c->len = len; |
Gavin Howard | 23d8d25 | 2018-10-17 12:03:41 -0600 | [diff] [blame] | 387 | |
Gavin Howard | d1a7836 | 2018-10-18 09:52:06 -0600 | [diff] [blame] | 388 | return bcg.signe ? BC_STATUS_EXEC_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 389 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 390 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 391 | if ((s = bc_num_init(&l1, max))) return s; |
| 392 | if ((s = bc_num_init(&h1, max))) goto high1_err; |
| 393 | if ((s = bc_num_init(&l2, max))) goto low2_err; |
| 394 | if ((s = bc_num_init(&h2, max))) goto high2_err; |
| 395 | if ((s = bc_num_init(&m1, max))) goto mix1_err; |
| 396 | if ((s = bc_num_init(&m2, max))) goto mix2_err; |
| 397 | if ((s = bc_num_init(&z0, max))) goto z0_err; |
| 398 | if ((s = bc_num_init(&z1, max))) goto z1_err; |
| 399 | if ((s = bc_num_init(&z2, max))) goto z2_err; |
| 400 | if ((s = bc_num_init(&temp, max + max))) goto temp_err; |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 401 | |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 402 | if ((s = bc_num_splitAt(a, max2, &l1, &h1))) goto err; |
| 403 | if ((s = bc_num_splitAt(b, max2, &l2, &h2))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 404 | |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 405 | if ((s = bc_num_add(&h1, &l1, &m1, 0))) goto err; |
| 406 | if ((s = bc_num_add(&h2, &l2, &m2, 0))) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 407 | |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 408 | if ((s = bc_num_alg_k(&h1, &h2, &z0))) goto err; |
| 409 | if ((s = bc_num_alg_k(&m1, &m2, &z1))) goto err; |
| 410 | if ((s = bc_num_alg_k(&l1, &l2, &z2))) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 411 | |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 412 | if ((s = bc_num_sub(&z1, &z0, &temp, 0))) goto err; |
| 413 | if ((s = bc_num_sub(&temp, &z2, &z1, 0))) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 414 | |
| 415 | if ((s = bc_num_shift(&z0, max2 * 2))) goto err; |
| 416 | if ((s = bc_num_shift(&z1, max2))) goto err; |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 417 | if ((s = bc_num_add(&z0, &z1, &temp, 0))) goto err; |
| 418 | s = bc_num_add(&temp, &z2, c, 0); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 419 | |
| 420 | err: |
| 421 | bc_num_free(&temp); |
| 422 | temp_err: |
| 423 | bc_num_free(&z2); |
| 424 | z2_err: |
| 425 | bc_num_free(&z1); |
| 426 | z1_err: |
| 427 | bc_num_free(&z0); |
| 428 | z0_err: |
| 429 | bc_num_free(&m2); |
| 430 | mix2_err: |
| 431 | bc_num_free(&m1); |
| 432 | mix1_err: |
| 433 | bc_num_free(&h2); |
| 434 | high2_err: |
| 435 | bc_num_free(&l2); |
| 436 | low2_err: |
| 437 | bc_num_free(&h1); |
| 438 | high1_err: |
| 439 | bc_num_free(&l1); |
| 440 | return s; |
| 441 | } |
| 442 | |
| 443 | BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
| 444 | |
| 445 | BcStatus s; |
| 446 | BcNum cpa, cpb; |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 447 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 448 | |
| 449 | scale = BC_MAX(scale, a->rdx); |
| 450 | scale = BC_MAX(scale, b->rdx); |
| 451 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 452 | maxrdx = BC_MAX(maxrdx, scale); |
| 453 | |
| 454 | if ((s = bc_num_init(&cpa, a->len))) return s; |
| 455 | if ((s = bc_num_init(&cpb, b->len))) goto b_err; |
| 456 | |
| 457 | if ((s = bc_num_copy(&cpa, a))) goto err; |
| 458 | if ((s = bc_num_copy(&cpb, b))) goto err; |
Gavin Howard | 23d8d25 | 2018-10-17 12:03:41 -0600 | [diff] [blame] | 459 | cpa.neg = cpb.neg = false; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 460 | if ((s = bc_num_shift(&cpa, maxrdx))) goto err; |
| 461 | if ((s = bc_num_shift(&cpb, maxrdx))) goto err; |
| 462 | |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 463 | if ((s = bc_num_alg_k(&cpa, &cpb, c))) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 464 | |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 465 | if ((s = bc_num_expand(c, c->len + (maxrdx += scale)))) goto err; |
| 466 | if (c->len < maxrdx) { |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 467 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 468 | c->len += maxrdx; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 469 | } |
| 470 | |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 471 | c->rdx = maxrdx; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 472 | s = bc_num_retireMul(c, scale, a->neg, b->neg); |
| 473 | |
| 474 | err: |
| 475 | bc_num_free(&cpb); |
| 476 | b_err: |
| 477 | bc_num_free(&cpa); |
| 478 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 481 | BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 482 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 483 | BcStatus s; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 484 | BcDig *n, *bptr, q; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 485 | size_t len, end, i; |
| 486 | BcNum cp; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 487 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 488 | if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 489 | else if (!a->len) { |
| 490 | bc_num_zero(c); |
| 491 | return BC_STATUS_SUCCESS; |
| 492 | } |
| 493 | else if (BC_NUM_ONE(b)) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 494 | if ((s = bc_num_copy(c, a))) return s; |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 495 | return bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 496 | } |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 497 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 498 | if ((s = bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)))) return s; |
| 499 | if ((s = bc_num_copy(&cp, a))) goto err; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 500 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 501 | if ((len = b->len) > cp.len) { |
| 502 | if ((s = bc_num_expand(&cp, len + 2))) goto err; |
| 503 | if ((s = bc_num_extend(&cp, len - cp.len))) goto err; |
| 504 | } |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 505 | |
Gavin Howard | 5065154 | 2018-09-29 02:53:11 -0600 | [diff] [blame] | 506 | if (b->rdx > cp.rdx && (s = bc_num_extend(&cp, b->rdx - cp.rdx))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 507 | cp.rdx -= b->rdx; |
Gavin Howard | 5065154 | 2018-09-29 02:53:11 -0600 | [diff] [blame] | 508 | if (scale > cp.rdx && (s = bc_num_extend(&cp, scale - cp.rdx))) goto err; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 509 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 510 | if (b->rdx == b->len) { |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 511 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 512 | bool zero = true; |
| 513 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 514 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 515 | if (i == len && zero) { |
| 516 | s = BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 517 | goto err; |
| 518 | } |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 519 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 520 | len -= i - 1; |
| 521 | } |
Gavin Howard | c1a6a34 | 2018-03-05 12:10:14 -0700 | [diff] [blame] | 522 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 523 | if (cp.cap == cp.len && (s = bc_num_expand(&cp, cp.len + 1))) goto err; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 524 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 525 | // We want an extra zero in front to make things simpler. |
| 526 | cp.num[cp.len++] = 0; |
| 527 | end = cp.len - len; |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 528 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 529 | if ((s = bc_num_expand(c, cp.len))) goto err; |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 530 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 531 | bc_num_zero(c); |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 532 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 533 | c->rdx = cp.rdx; |
| 534 | c->len = cp.len; |
| 535 | bptr = b->num; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 536 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 537 | for (i = end - 1; !bcg.signe && !s && i < end; --i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 538 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 539 | n = cp.num + i; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 540 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 541 | for (q = 0; (!s && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q) |
| 542 | s = bc_num_subArrays(n, bptr, len); |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 543 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 544 | c->num[i] = q; |
| 545 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 546 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 547 | if (!s) s = bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 548 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 549 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 550 | bc_num_free(&cp); |
| 551 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 554 | BcStatus bc_num_alg_r(BcNum *a, BcNum *b, BcNum *restrict c, BcNum *restrict d, |
| 555 | size_t scale, size_t ts) |
| 556 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 557 | BcStatus s; |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 558 | BcNum temp; |
| 559 | bool neg; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 560 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 561 | if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO; |
Gavin Howard | 8b25487 | 2018-03-14 01:13:35 -0600 | [diff] [blame] | 562 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 563 | if (!a->len) { |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 564 | bc_num_zero(d); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 565 | return BC_STATUS_SUCCESS; |
| 566 | } |
Gavin Howard | 8b25487 | 2018-03-14 01:13:35 -0600 | [diff] [blame] | 567 | |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 568 | if ((s = bc_num_init(&temp, d->cap))) return s; |
| 569 | |
| 570 | if ((s = bc_num_alg_d(a, b, c, scale))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 571 | |
Gavin Howard | 841a388 | 2018-10-04 11:28:14 -0600 | [diff] [blame] | 572 | if (scale) scale = ts; |
Gavin Howard | 3115c01 | 2018-10-04 10:53:56 -0600 | [diff] [blame] | 573 | |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 574 | if ((s = bc_num_alg_m(c, b, &temp, scale))) goto err; |
| 575 | if ((s = bc_num_sub(a, &temp, d, scale))) goto err; |
Gavin Howard | 5d149cf | 2018-09-06 13:46:23 -0600 | [diff] [blame] | 576 | |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 577 | if (ts > d->rdx && d->len && (s = bc_num_extend(d, ts - d->rdx))) goto err; |
| 578 | |
| 579 | neg = d->neg; |
| 580 | s = bc_num_retireMul(d, ts, a->neg, b->neg); |
| 581 | d->neg = neg; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 582 | |
| 583 | err: |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 584 | bc_num_free(&temp); |
| 585 | return s; |
| 586 | } |
| 587 | |
| 588 | BcStatus bc_num_alg_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
| 589 | |
| 590 | BcStatus s; |
| 591 | BcNum c1; |
| 592 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 593 | |
| 594 | if ((s = bc_num_init(&c1, len))) return s; |
| 595 | |
| 596 | s = bc_num_alg_r(a, b, &c1, c, scale, ts); |
| 597 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 598 | bc_num_free(&c1); |
| 599 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 600 | } |
| 601 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 602 | BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 603 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 604 | BcStatus s; |
| 605 | BcNum copy; |
| 606 | unsigned long pow; |
| 607 | size_t i, powrdx, resrdx; |
| 608 | bool neg, zero; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 609 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 610 | if (b->rdx) return BC_STATUS_MATH_NON_INTEGER; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 611 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 612 | if (!b->len) { |
| 613 | bc_num_one(c); |
| 614 | return BC_STATUS_SUCCESS; |
| 615 | } |
| 616 | else if (!a->len) { |
| 617 | bc_num_zero(c); |
| 618 | return BC_STATUS_SUCCESS; |
| 619 | } |
| 620 | else if (BC_NUM_ONE(b)) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 621 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 622 | if (!b->neg) s = bc_num_copy(c, a); |
| 623 | else s = bc_num_inv(a, c, scale); |
Gavin Howard | 5fc40e7 | 2018-03-05 12:11:09 -0700 | [diff] [blame] | 624 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 625 | return s; |
| 626 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 627 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 628 | neg = b->neg; |
| 629 | b->neg = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 630 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 631 | if ((s = bc_num_ulong(b, &pow))) return s; |
| 632 | if ((s = bc_num_init(©, a->len))) return s; |
| 633 | if ((s = bc_num_copy(©, a))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 634 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 635 | if (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx)); |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 636 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 637 | b->neg = neg; |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 638 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 639 | for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) { |
| 640 | powrdx <<= 1; |
| 641 | if ((s = bc_num_mul(©, ©, ©, powrdx))) goto err; |
| 642 | } |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 643 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 644 | if ((s = bc_num_copy(c, ©))) goto err; |
| 645 | if (bcg.signe) { |
| 646 | s = BC_STATUS_EXEC_SIGNAL; |
| 647 | goto err; |
| 648 | } |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 649 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 650 | for (resrdx = powrdx, pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) { |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 651 | if ((s = bc_num_mul(©, ©, ©, (powrdx <<= 1)))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 652 | if (pow & 1) { |
| 653 | resrdx += powrdx; |
| 654 | if ((s = bc_num_mul(c, ©, c, resrdx))) goto err; |
| 655 | } |
| 656 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 657 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 658 | if (neg && (s = bc_num_inv(c, c, scale))) goto err; |
| 659 | if (bcg.signe) { |
| 660 | s = BC_STATUS_EXEC_SIGNAL; |
| 661 | goto err; |
| 662 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 663 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 664 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 665 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 666 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
| 667 | if (zero) bc_num_zero(c); |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 668 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 669 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 670 | bc_num_free(©); |
| 671 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 672 | } |
| 673 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 674 | 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] | 675 | BcNumBinaryOp op, size_t req) |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 676 | { |
Gavin Howard | a1c4439 | 2018-09-27 12:41:15 -0600 | [diff] [blame] | 677 | BcStatus s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 678 | BcNum num2, *ptr_a, *ptr_b; |
| 679 | bool init = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 680 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 681 | assert(a && b && c && op); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 682 | |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 683 | if ((init = (c == a))) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 684 | ptr_a = &num2; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 685 | memcpy(ptr_a, c, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 686 | } |
| 687 | else ptr_a = a; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 688 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 689 | if (c == b) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 690 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 691 | ptr_b = &num2; |
| 692 | |
| 693 | if (c != a) { |
| 694 | memcpy(ptr_b, c, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 695 | init = true; |
| 696 | } |
| 697 | } |
| 698 | else ptr_b = b; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 699 | |
Gavin Howard | a1c4439 | 2018-09-27 12:41:15 -0600 | [diff] [blame] | 700 | if (init) s = bc_num_init(c, req); |
| 701 | else s = bc_num_expand(c, req); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 702 | |
Gavin Howard | 9ab772a | 2018-10-05 11:38:40 -0600 | [diff] [blame] | 703 | if (!s) s = op(ptr_a, ptr_b, c, scale); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 704 | |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 705 | assert(!c->neg || c->len); |
| 706 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 707 | if (init) bc_num_free(&num2); |
Gavin Howard | a1c4439 | 2018-09-27 12:41:15 -0600 | [diff] [blame] | 708 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 711 | bool bc_num_strValid(const char *val, size_t base) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 712 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 713 | BcDig b; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 714 | bool small, radix = false; |
| 715 | size_t i, len = strlen(val); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 716 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 717 | if (!len) return true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 718 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 719 | small = base <= 10; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 720 | b = (BcDig) (small ? base + '0' : base - 9 + 'A'); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 721 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 722 | for (i = 0; i < len; ++i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 723 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 724 | BcDig c = val[i]; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 725 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 726 | if (c == '.') { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 727 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 728 | if (radix) return false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 729 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 730 | radix = true; |
| 731 | continue; |
| 732 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 733 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 734 | if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b))) |
| 735 | return false; |
| 736 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 737 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 738 | return true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 741 | BcStatus bc_num_parseDecimal(BcNum *n, const char *val) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 742 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 743 | BcStatus s; |
| 744 | size_t len, i; |
| 745 | const char *ptr; |
| 746 | bool zero = true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 747 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 748 | for (i = 0; val[i] == '0'; ++i); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 749 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 750 | val += i; |
| 751 | len = strlen(val); |
| 752 | bc_num_zero(n); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 753 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 754 | if (len) { |
| 755 | for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.'; |
| 756 | if ((s = bc_num_expand(n, len))) return s; |
| 757 | } |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 758 | |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 759 | if (zero) return BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 760 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 761 | ptr = strchr(val, '.'); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 762 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 763 | // Explicitly test for NULL here to produce either a 0 or 1. |
| 764 | n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1))); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 765 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 766 | for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.')) |
| 767 | n->num[n->len] = val[i] - '0'; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 768 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 769 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 772 | BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 773 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 774 | BcStatus s; |
| 775 | BcNum temp, mult, result; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 776 | BcDig c = '\0'; |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 777 | bool zero = true; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 778 | unsigned long v; |
| 779 | size_t i, digits, len = strlen(val); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 780 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 781 | bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 782 | |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 783 | for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0'); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 784 | if (zero) return BC_STATUS_SUCCESS; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 785 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 786 | if ((s = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return s; |
| 787 | if ((s = bc_num_init(&mult, BC_NUM_DEF_SIZE))) goto mult_err; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 788 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 789 | for (i = 0; i < len && (c = val[i]) != '.'; ++i) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 790 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 791 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
Gavin Howard | fcb6ebb | 2018-03-09 10:41:06 -0700 | [diff] [blame] | 792 | |
Gavin Howard | 31060d9 | 2018-10-18 15:07:44 -0600 | [diff] [blame] | 793 | if ((s = bc_num_alg_m(n, base, &mult, 0))) goto int_err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 794 | if ((s = bc_num_ulong2num(&temp, v))) goto int_err; |
| 795 | if ((s = bc_num_add(&mult, &temp, n, 0))) goto int_err; |
| 796 | } |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 797 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 798 | if (i == len && !(c = val[i])) goto int_err; |
| 799 | assert(c == '.'); |
| 800 | if ((s = bc_num_init(&result, base->len))) goto int_err; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 801 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 802 | bc_num_zero(&result); |
| 803 | bc_num_one(&mult); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 804 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 805 | for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 806 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 807 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 808 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 809 | if ((s = bc_num_mul(&result, base, &result, 0))) goto err; |
| 810 | if ((s = bc_num_ulong2num(&temp, v))) goto err; |
| 811 | if ((s = bc_num_add(&result, &temp, &result, 0))) goto err; |
| 812 | if ((s = bc_num_mul(&mult, base, &mult, 0))) goto err; |
| 813 | } |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 814 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 815 | if ((s = bc_num_div(&result, &mult, &result, digits))) goto err; |
| 816 | if ((s = bc_num_add(n, &result, n, digits))) goto err; |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 817 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 818 | if (n->len) { |
| 819 | if (n->rdx < digits && n->len) s = bc_num_extend(n, digits - n->rdx); |
| 820 | } |
| 821 | else bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 822 | |
| 823 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 824 | bc_num_free(&result); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 825 | int_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 826 | bc_num_free(&mult); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 827 | mult_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 828 | bc_num_free(&temp); |
| 829 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 830 | } |
| 831 | |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 832 | BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 833 | if (*nchars == line_len - 1) { |
| 834 | if (putchar('\\') == EOF) return BC_STATUS_IO_ERR; |
| 835 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
| 836 | *nchars = 0; |
| 837 | } |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 838 | return BC_STATUS_SUCCESS; |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 839 | } |
| 840 | |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 841 | #ifdef DC_ENABLED |
| 842 | BcStatus bc_num_printChar(size_t num, size_t width, bool radix, |
| 843 | size_t *nchars, size_t line_len) |
| 844 | { |
Gavin Howard | 4c09154 | 2018-10-09 03:33:08 -0600 | [diff] [blame] | 845 | (void) radix, (void) line_len; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 846 | |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 847 | if (putchar((char) num) == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 848 | *nchars = *nchars + width; |
| 849 | |
| 850 | return BC_STATUS_SUCCESS; |
| 851 | } |
| 852 | #endif // DC_ENABLED |
| 853 | |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 854 | BcStatus bc_num_printDigits(size_t num, size_t width, bool radix, |
| 855 | size_t *nchars, size_t line_len) |
| 856 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 857 | BcStatus s; |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 858 | size_t exp, pow, div; |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 859 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 860 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 861 | if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR; |
| 862 | ++(*nchars); |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 863 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 864 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 865 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10); |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 866 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 867 | for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 868 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 869 | div = num / pow; |
| 870 | num -= div * pow; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 871 | if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR; |
| 872 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 873 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 874 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 875 | } |
Gavin Howard | fe679f0 | 2018-02-14 15:50:09 -0700 | [diff] [blame] | 876 | |
Gavin Howard | 902a16c | 2018-05-16 01:22:53 -0600 | [diff] [blame] | 877 | BcStatus bc_num_printHex(size_t num, size_t width, bool radix, |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 878 | size_t *nchars, size_t line_len) |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 879 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 880 | BcStatus s; |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 881 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 882 | assert(width == 1); |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 883 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 884 | if (radix) { |
| 885 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
| 886 | if (putchar('.') == EOF) return BC_STATUS_IO_ERR; |
| 887 | *nchars += 1; |
| 888 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 889 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 890 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
| 891 | if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR; |
| 892 | *nchars = *nchars + width; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 893 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 894 | return BC_STATUS_SUCCESS; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Gavin Howard | 5f46ee3 | 2018-09-25 13:02:43 -0600 | [diff] [blame] | 897 | BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) { |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 898 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 899 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 900 | size_t i, rdx = n->rdx - 1; |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 901 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 902 | if (n->neg && putchar('-') == EOF) return BC_STATUS_IO_ERR; |
| 903 | (*nchars) += n->neg; |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 904 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 905 | for (i = n->len - 1; !s && i < n->len; --i) |
| 906 | s = bc_num_printHex((size_t) n->num[i], 1, i == rdx, nchars, len); |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 907 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 908 | return s; |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 911 | BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, size_t *nchars, |
| 912 | size_t line_len, BcNumDigitOp print) |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 913 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 914 | BcStatus s; |
| 915 | BcVec stack; |
| 916 | BcNum intp, fracp, digit, frac_len; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 917 | unsigned long dig, *ptr; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 918 | size_t i; |
| 919 | bool radix; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 920 | |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 921 | if ((s = bc_vec_init(&stack, sizeof(long), NULL))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 922 | if ((s = bc_num_init(&intp, n->len))) goto int_err; |
| 923 | if ((s = bc_num_init(&fracp, n->rdx))) goto frac_err; |
| 924 | if ((s = bc_num_init(&digit, width))) goto digit_err; |
| 925 | if ((s = bc_num_init(&frac_len, BC_NUM_INT(n)))) goto frac_len_err; |
| 926 | if ((s = bc_num_copy(&intp, n))) goto err; |
| 927 | bc_num_one(&frac_len); |
Gavin Howard | a50fc54 | 2018-03-29 17:25:38 -0600 | [diff] [blame] | 928 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 929 | bc_num_truncate(&intp, intp.rdx); |
| 930 | if ((s = bc_num_sub(n, &intp, &fracp, 0))) goto err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 931 | |
Gavin Howard | 4c09154 | 2018-10-09 03:33:08 -0600 | [diff] [blame] | 932 | if (!n->len) { |
| 933 | s = print(0, width, false, nchars, line_len); |
| 934 | goto err; |
| 935 | } |
| 936 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 937 | while (intp.len) { |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 938 | if ((s = bc_num_divmod(&intp, base, &intp, &digit, 0))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 939 | if ((s = bc_num_ulong(&digit, &dig))) goto err; |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 940 | if ((s = bc_vec_push(&stack, &dig))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 941 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 942 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 943 | for (i = 0; i < stack.len; ++i) { |
| 944 | ptr = bc_vec_item_rev(&stack, i); |
| 945 | assert(ptr); |
| 946 | if ((s = print(*ptr, width, false, nchars, line_len))) goto err; |
| 947 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 948 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 949 | if (!n->rdx) goto err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 950 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 951 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
| 952 | if ((s = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err; |
| 953 | if ((s = bc_num_ulong(&fracp, &dig))) goto err; |
| 954 | if ((s = bc_num_ulong2num(&intp, dig))) goto err; |
| 955 | if ((s = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err; |
| 956 | if ((s = print(dig, width, radix, nchars, line_len))) goto err; |
| 957 | if ((s = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err; |
| 958 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 959 | |
| 960 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 961 | bc_num_free(&frac_len); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 962 | frac_len_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 963 | bc_num_free(&digit); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 964 | digit_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 965 | bc_num_free(&fracp); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 966 | frac_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 967 | bc_num_free(&intp); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 968 | int_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 969 | bc_vec_free(&stack); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 970 | return s; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 973 | BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t, |
| 974 | size_t *nchars, size_t line_len) |
| 975 | { |
| 976 | BcStatus s; |
| 977 | size_t width, i; |
| 978 | BcNumDigitOp print; |
| 979 | bool neg = n->neg; |
| 980 | |
| 981 | if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR; |
| 982 | (*nchars) += neg; |
| 983 | |
| 984 | n->neg = false; |
| 985 | |
| 986 | if (base_t <= BC_NUM_MAX_IBASE) { |
| 987 | width = 1; |
| 988 | print = bc_num_printHex; |
| 989 | } |
| 990 | else { |
| 991 | for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width); |
| 992 | print = bc_num_printDigits; |
| 993 | } |
| 994 | |
| 995 | s = bc_num_printNum(n, base, width, nchars, line_len, print); |
| 996 | n->neg = neg; |
| 997 | |
| 998 | return s; |
| 999 | } |
| 1000 | |
| 1001 | #ifdef DC_ENABLED |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 1002 | BcStatus bc_num_stream(BcNum *n, BcNum *base, size_t *nchars, size_t len) { |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1003 | return bc_num_printNum(n, base, 1, nchars, len, bc_num_printChar); |
| 1004 | } |
| 1005 | #endif // DC_ENABLED |
| 1006 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1007 | BcStatus bc_num_init(BcNum *n, size_t request) { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1008 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1009 | assert(n); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1010 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1011 | request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE; |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1012 | memset(n, 0, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1013 | if (!(n->num = malloc(request))) return BC_STATUS_ALLOC_ERR; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1014 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1015 | n->cap = request; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1016 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1017 | return BC_STATUS_SUCCESS; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 1020 | BcStatus bc_num_expand(BcNum *n, size_t req) { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1021 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 1022 | BcDig *temp; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1023 | |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 1024 | assert(n); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1025 | |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 1026 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1027 | |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 1028 | if (req <= n->cap) return BC_STATUS_SUCCESS; |
| 1029 | if (!(temp = realloc(n->num, req))) return BC_STATUS_ALLOC_ERR; |
| 1030 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1031 | n->num = temp; |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 1032 | n->cap = req; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1033 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1034 | return BC_STATUS_SUCCESS; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 1037 | void bc_num_free(void *num) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1038 | BcNum *n = (BcNum*) num; |
Gavin Howard | 8c01998 | 2018-10-05 12:35:09 -0600 | [diff] [blame] | 1039 | assert(n); |
Gavin Howard | 2bc5418 | 2018-10-06 17:03:46 -0600 | [diff] [blame] | 1040 | free(n->num); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 1043 | BcStatus bc_num_copy(BcNum *d, BcNum *s) { |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1044 | |
Gavin Howard | b20c2db | 2018-10-08 10:31:23 -0600 | [diff] [blame] | 1045 | BcStatus status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1046 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1047 | assert(d && s); |
Gavin Howard | f23448c | 2018-02-27 20:36:24 -0700 | [diff] [blame] | 1048 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1049 | if (d == s) return BC_STATUS_SUCCESS; |
Gavin Howard | b20c2db | 2018-10-08 10:31:23 -0600 | [diff] [blame] | 1050 | if ((status = bc_num_expand(d, s->cap))) return status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1051 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1052 | d->len = s->len; |
| 1053 | d->neg = s->neg; |
| 1054 | d->rdx = s->rdx; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1055 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 1056 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1057 | |
Gavin Howard | b20c2db | 2018-10-08 10:31:23 -0600 | [diff] [blame] | 1058 | return status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1059 | } |
| 1060 | |
Gavin Howard | 5cea43b | 2018-03-02 11:32:11 -0700 | [diff] [blame] | 1061 | 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] | 1062 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1063 | BcStatus s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1064 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1065 | assert(n && val && base); |
| 1066 | assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_NUM_MAX_IBASE); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1067 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1068 | 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] | 1069 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1070 | if (base_t == 10) s = bc_num_parseDecimal(n, val); |
| 1071 | else s = bc_num_parseBase(n, val, base); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1072 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1073 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1074 | } |
| 1075 | |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 1076 | BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline, |
| 1077 | size_t *nchars, size_t line_len) |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1078 | { |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1079 | BcStatus s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1080 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1081 | assert(n && base && nchars); |
| 1082 | assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1083 | |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1084 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 1085 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1086 | if (!n->len) { |
| 1087 | if (putchar('0') == EOF) return BC_STATUS_IO_ERR; |
| 1088 | ++(*nchars); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1089 | } |
| 1090 | else if (base_t == 10) s = bc_num_printDecimal(n, nchars, line_len); |
| 1091 | else s = bc_num_printBase(n, base, base_t, nchars, line_len); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1092 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1093 | if (s) return s; |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1094 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1095 | if (newline) { |
| 1096 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
| 1097 | *nchars = 0; |
| 1098 | } |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1099 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1100 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1103 | BcStatus bc_num_ulong(BcNum *n, unsigned long *result) { |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1104 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1105 | size_t i; |
| 1106 | unsigned long pow; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1107 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1108 | assert(n && result); |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1109 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1110 | if (n->neg) return BC_STATUS_MATH_NEGATIVE; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1111 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1112 | for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1113 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1114 | unsigned long prev = *result, powprev = pow; |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 1115 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1116 | *result += ((unsigned long) n->num[i]) * pow; |
| 1117 | pow *= 10; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1118 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1119 | if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW; |
| 1120 | } |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1121 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1122 | return BC_STATUS_SUCCESS; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1125 | BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) { |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1126 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1127 | BcStatus s; |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1128 | size_t len; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 1129 | BcDig *ptr; |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1130 | unsigned long i; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1131 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1132 | assert(n); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1133 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1134 | bc_num_zero(n); |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1135 | |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 1136 | if (!val) return BC_STATUS_SUCCESS; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1137 | |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1138 | for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len) |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1139 | if ((s = bc_num_expand(n, len))) return s; |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1140 | for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1141 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1142 | return BC_STATUS_SUCCESS; |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1143 | } |
| 1144 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1145 | BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1146 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_a : bc_num_alg_s; |
Gavin Howard | f8f232b | 2018-10-11 17:29:47 -0600 | [diff] [blame] | 1147 | (void) scale; |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1148 | return bc_num_binary(a, b, c, false, op, BC_NUM_AREQ(a, b)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1151 | BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1152 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_alg_s : bc_num_alg_a; |
Gavin Howard | f8f232b | 2018-10-11 17:29:47 -0600 | [diff] [blame] | 1153 | (void) scale; |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1154 | return bc_num_binary(a, b, c, true, op, BC_NUM_AREQ(a, b)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1157 | BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1158 | size_t req = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1159 | return bc_num_binary(a, b, c, scale, bc_num_alg_m, req); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1162 | BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1163 | size_t req = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1164 | return bc_num_binary(a, b, c, scale, bc_num_alg_d, req); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1165 | } |
| 1166 | |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 1167 | BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1168 | size_t req = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 1169 | return bc_num_binary(a, b, c, scale, bc_num_alg_rem, req); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1172 | BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
| 1173 | return bc_num_binary(a, b, c, scale, bc_num_alg_p, a->len * b->len + 1); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1174 | } |
| 1175 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1176 | BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1177 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1178 | BcStatus s; |
| 1179 | BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1180 | size_t pow, len, digits, digits1, resrdx, req, times = 0; |
| 1181 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1182 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1183 | assert(a && b); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1184 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1185 | 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] | 1186 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1187 | if (b == a) { |
| 1188 | memcpy(&a2, b, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1189 | ptr_a = &a2; |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1190 | s = bc_num_init(b, req); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1191 | } |
| 1192 | else { |
| 1193 | ptr_a = a; |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1194 | s = bc_num_expand(b, req); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1195 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1196 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1197 | if (s) goto init_err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1198 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1199 | if (!ptr_a->len) { |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1200 | bc_num_zero(b); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1201 | goto init_err; |
| 1202 | } |
| 1203 | else if (ptr_a->neg) { |
Gavin Howard | 4c4f5f0 | 2018-10-11 11:52:11 -0600 | [diff] [blame] | 1204 | s = BC_STATUS_MATH_NEGATIVE; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1205 | goto init_err; |
| 1206 | } |
| 1207 | else if (BC_NUM_ONE(a)) { |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1208 | bc_num_one(b); |
| 1209 | s = bc_num_extend(b, scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1210 | goto init_err; |
| 1211 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1212 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1213 | len = ptr_a->len; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1214 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1215 | scale = BC_MAX(scale, ptr_a->rdx) + 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1216 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1217 | if ((s = bc_num_init(&num1, len))) goto init_err; |
| 1218 | if ((s = bc_num_init(&num2, num1.len))) goto num2_err; |
| 1219 | if ((s = bc_num_init(&half, BC_NUM_DEF_SIZE))) goto two_err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1220 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1221 | bc_num_one(&half); |
| 1222 | half.num[0] = 5; |
| 1223 | half.rdx = 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1224 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1225 | len += scale; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1226 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1227 | if ((s = bc_num_init(&f, len))) goto f_err; |
| 1228 | if ((s = bc_num_init(&fprime, len + scale))) goto fprime_err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1229 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1230 | x0 = &num1; |
| 1231 | x1 = &num2; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1232 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1233 | bc_num_one(x0); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1234 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1235 | if ((pow = BC_NUM_INT(ptr_a))) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1236 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1237 | if (pow & 1) x0->num[0] = 2; |
| 1238 | else x0->num[0] = 6; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1239 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1240 | pow -= 2 - (pow & 1); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1241 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1242 | if ((s = bc_num_extend(x0, pow))) goto err; |
Gavin Howard | ed742ce | 2018-08-29 14:23:12 -0600 | [diff] [blame] | 1243 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1244 | // Make sure to move the radix back. |
| 1245 | x0->rdx -= pow; |
| 1246 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1247 | |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1248 | x0->rdx = digits = digits1 = 0; |
| 1249 | resrdx = scale + 2; |
| 1250 | len = BC_NUM_INT(x0) + resrdx - 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1251 | |
Gavin Howard | 0bfcf7a | 2018-10-11 09:37:09 -0600 | [diff] [blame] | 1252 | while (!bcg.signe && (cmp || digits < len)) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1253 | |
Gavin Howard | 31060d9 | 2018-10-18 15:07:44 -0600 | [diff] [blame] | 1254 | if ((s = bc_num_alg_d(a, x0, &f, resrdx))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1255 | if ((s = bc_num_add(x0, &f, &fprime, resrdx))) goto err; |
Gavin Howard | 31060d9 | 2018-10-18 15:07:44 -0600 | [diff] [blame] | 1256 | if ((s = bc_num_alg_m(&fprime, &half, x1, resrdx))) goto err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1257 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1258 | cmp = bc_num_cmp(x1, x0); |
| 1259 | digits = x1->len - (unsigned long long) llabs(cmp); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1260 | |
Gavin Howard | b11e9e2 | 2018-10-06 17:26:02 -0600 | [diff] [blame] | 1261 | if (cmp == cmp2 && digits == digits1) times += 1; |
| 1262 | else times = 0; |
| 1263 | |
| 1264 | resrdx += times > 4; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1265 | |
| 1266 | cmp2 = cmp1; |
| 1267 | cmp1 = cmp; |
| 1268 | digits1 = digits; |
| 1269 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1270 | temp = x0; |
| 1271 | x0 = x1; |
| 1272 | x1 = temp; |
| 1273 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1274 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1275 | if (bcg.signe) { |
| 1276 | s = BC_STATUS_EXEC_SIGNAL; |
| 1277 | goto err; |
| 1278 | } |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 1279 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1280 | if ((s = bc_num_copy(b, x0))) goto err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1281 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1282 | if (b->rdx > --scale) bc_num_truncate(b, b->rdx - scale); |
| 1283 | else if (b->rdx < scale) s = bc_num_extend(b, scale - b->rdx); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1284 | |
| 1285 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1286 | bc_num_free(&fprime); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1287 | fprime_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1288 | bc_num_free(&f); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1289 | f_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1290 | bc_num_free(&half); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1291 | two_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1292 | bc_num_free(&num2); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1293 | num2_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1294 | bc_num_free(&num1); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1295 | init_err: |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1296 | if (b == a) bc_num_free(&a2); |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 1297 | assert(!b->neg || b->len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1298 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1299 | } |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1300 | |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1301 | BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) { |
| 1302 | |
| 1303 | BcStatus s; |
| 1304 | BcNum num2, *ptr_a; |
| 1305 | bool init; |
| 1306 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = BC_NUM_MREQ(a, b, ts); |
| 1307 | |
| 1308 | assert(c != d && a != b && a != d && b != d && b != c); |
| 1309 | |
| 1310 | if ((init = (c == a))) { |
| 1311 | memcpy(&num2, c, sizeof(BcNum)); |
| 1312 | ptr_a = &num2; |
| 1313 | } |
| 1314 | else ptr_a = a; |
| 1315 | |
| 1316 | if (init) s = bc_num_init(c, len); |
| 1317 | else s = bc_num_expand(d, ptr_a->len); |
| 1318 | |
| 1319 | if (s) return s; |
| 1320 | |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 1321 | s = bc_num_alg_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1322 | |
| 1323 | assert(!c->neg || c->len); |
| 1324 | assert(!d->neg || d->len); |
| 1325 | |
| 1326 | if (init) bc_num_free(&num2); |
| 1327 | |
| 1328 | return s; |
| 1329 | } |
| 1330 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1331 | #ifdef DC_ENABLED |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1332 | BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) { |
| 1333 | |
| 1334 | BcStatus s; |
Gavin Howard | 2c8959e | 2018-10-04 18:32:58 -0600 | [diff] [blame] | 1335 | BcNum num2, *ptr_a, *ptr_b, *ptr_c, base, exp, two, temp; |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1336 | bool init; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1337 | |
| 1338 | assert(a && b && c && d); |
| 1339 | |
| 1340 | scale = 0; |
| 1341 | |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 1342 | if ((init = (d == a))) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1343 | memcpy(&num2, d, sizeof(BcNum)); |
| 1344 | ptr_a = &num2; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1345 | } |
| 1346 | else ptr_a = a; |
| 1347 | |
| 1348 | if (d == b) { |
| 1349 | |
| 1350 | ptr_b = &num2; |
| 1351 | |
| 1352 | if (d != a) { |
| 1353 | memcpy(ptr_b, d, sizeof(BcNum)); |
| 1354 | init = true; |
| 1355 | } |
| 1356 | } |
| 1357 | else ptr_b = b; |
| 1358 | |
| 1359 | if (d == c) { |
| 1360 | |
| 1361 | ptr_c = &num2; |
| 1362 | |
| 1363 | if (d != a && d != b) { |
| 1364 | memcpy(ptr_c, d, sizeof(BcNum)); |
| 1365 | init = true; |
| 1366 | } |
| 1367 | } |
| 1368 | else ptr_c = c; |
| 1369 | |
| 1370 | if (init) s = bc_num_init(d, ptr_c->len); |
| 1371 | else s = bc_num_expand(d, ptr_c->len); |
| 1372 | |
| 1373 | if (s) return s; |
| 1374 | |
| 1375 | if (!ptr_c->len) { |
| 1376 | s = BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 1377 | goto base_err; |
| 1378 | } |
| 1379 | |
Gavin Howard | 5ef610b | 2018-10-04 16:57:47 -0600 | [diff] [blame] | 1380 | if (ptr_a->rdx || ptr_b->rdx || ptr_c->rdx) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1381 | s = BC_STATUS_MATH_NON_INTEGER; |
| 1382 | goto base_err; |
| 1383 | } |
| 1384 | |
| 1385 | if (ptr_b->neg) { |
| 1386 | s = BC_STATUS_MATH_NEGATIVE; |
| 1387 | goto base_err; |
| 1388 | } |
| 1389 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1390 | if ((s = bc_num_init(&base, ptr_c->len))) goto base_err; |
| 1391 | if ((s = bc_num_init(&exp, ptr_b->len))) goto exp_err; |
| 1392 | if ((s = bc_num_init(&two, BC_NUM_DEF_SIZE))) goto two_err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1393 | if ((s = bc_num_init(&temp, ptr_b->len))) goto temp_err; |
| 1394 | |
| 1395 | bc_num_one(&two); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1396 | two.num[0] = 2; |
| 1397 | bc_num_one(d); |
| 1398 | |
Gavin Howard | 31060d9 | 2018-10-18 15:07:44 -0600 | [diff] [blame] | 1399 | if ((s = bc_num_alg_rem(ptr_a, ptr_c, &base, scale))) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1400 | if ((s = bc_num_copy(&exp, ptr_b))) goto err; |
| 1401 | |
Gavin Howard | af90560 | 2018-09-29 18:56:37 -0600 | [diff] [blame] | 1402 | while (exp.len) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1403 | |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame^] | 1404 | if ((s = bc_num_divmod(&exp, &two, &exp, &temp, scale))) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1405 | |
| 1406 | if (BC_NUM_ONE(&temp)) { |
Gavin Howard | 31060d9 | 2018-10-18 15:07:44 -0600 | [diff] [blame] | 1407 | if ((s = bc_num_alg_m(d, &base, &temp, scale))) goto err; |
| 1408 | if ((s = bc_num_alg_rem(&temp, ptr_c, d, scale))) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1409 | } |
| 1410 | |
Gavin Howard | 31060d9 | 2018-10-18 15:07:44 -0600 | [diff] [blame] | 1411 | if ((s = bc_num_alg_m(&base, &base, &temp, scale))) goto err; |
| 1412 | if ((s = bc_num_alg_rem(&temp, ptr_c, &base, scale))) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1413 | } |
| 1414 | |
| 1415 | err: |
| 1416 | bc_num_free(&temp); |
| 1417 | temp_err: |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1418 | bc_num_free(&two); |
| 1419 | two_err: |
| 1420 | bc_num_free(&exp); |
| 1421 | exp_err: |
| 1422 | bc_num_free(&base); |
| 1423 | base_err: |
| 1424 | if (init) bc_num_free(&num2); |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 1425 | assert(!d->neg || d->len); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1426 | return s; |
| 1427 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1428 | #endif // DC_ENABLED |