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