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