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 | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 55 | BcStatus bc_num_subArrays(BcDig *restrict n1, BcDig *restrict n2, size_t len) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 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 | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 66 | ssize_t bc_num_compare(BcDig *restrict n1, BcDig *restrict n2, size_t len) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 67 | size_t i; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 68 | BcDig c = 0; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 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; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 76 | BcDig *max_num, *min_num; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 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 | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 80 | if (a == b) return 0; |
| 81 | else if (!a) return !b ? 0 : !b->neg * -2 + 1; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 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 | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 126 | BcDig *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 | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 136 | memmove(n->num, ptr, n->len * sizeof(BcDig)); |
| 137 | memset(n->num + n->len, 0, sizeof(BcDig) * (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; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 143 | BcDig *ptr; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 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; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 152 | memmove(ptr, n->num, sizeof(BcDig) * n->len); |
| 153 | memset(n->num, 0, sizeof(BcDig) * 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 | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 193 | BcStatus bc_num_alg_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 194 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 195 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 196 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 197 | BcDig 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); |
Gavin Howard | 38a10a1 | 2018-10-11 11:52:29 -0600 | [diff] [blame] | 204 | if (sub && c->len) c->neg = !c->neg; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 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; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 210 | memset(c->num, 0, c->cap * sizeof(BcDig)); |
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 | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 265 | BcStatus bc_num_alg_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 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); |
Gavin Howard | 38a10a1 | 2018-10-11 11:52:29 -0600 | [diff] [blame] | 278 | if (sub && c->len) c->neg = !c->neg; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 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 | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 324 | BcStatus bc_num_alg_m(BcNum *a, BcNum *b, BcNum *restrict 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; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 327 | BcDig carry; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 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) { |
Gavin Howard | 9ab772a | 2018-10-05 11:38:40 -0600 | [diff] [blame] | 343 | if ((s = bc_num_copy(c, b))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 344 | if (a->neg) c->neg = !c->neg; |
| 345 | } |
| 346 | else { |
Gavin Howard | 9ab772a | 2018-10-05 11:38:40 -0600 | [diff] [blame] | 347 | if ((s = bc_num_copy(c, a))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 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 | return bc_num_retireMul(c, scale); |
| 352 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 353 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 354 | memset(c->num, 0, sizeof(BcDig) * c->cap); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 355 | c->len = carry = len = 0; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 356 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 357 | for (i = 0; !bcg.signe && i < b->len; ++i) { |
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 (j = 0; !bcg.signe && j < a->len; ++j) { |
| 360 | c->num[i + j] += a->num[j] * b->num[i] + carry; |
| 361 | carry = c->num[i + j] / 10; |
| 362 | c->num[i + j] %= 10; |
| 363 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 364 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 365 | if (bcg.signe) return BC_STATUS_EXEC_SIGNAL; |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 366 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 367 | if (carry) { |
| 368 | c->num[i + j] += carry; |
| 369 | carry = 0; |
| 370 | len = BC_MAX(len, i + j + 1); |
| 371 | } |
| 372 | else len = BC_MAX(len, i + j); |
| 373 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 374 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 375 | if (bcg.signe) return BC_STATUS_EXEC_SIGNAL; |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 376 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 377 | c->len = BC_MAX(len, c->rdx); |
| 378 | c->neg = !a->neg != !b->neg; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 379 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 380 | return bc_num_retireMul(c, scale); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 383 | BcStatus bc_num_alg_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 384 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 385 | BcStatus s; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 386 | BcDig *n, *bptr, q; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 387 | size_t len, end, i; |
| 388 | BcNum cp; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 389 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 390 | if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 391 | else if (!a->len) { |
| 392 | bc_num_zero(c); |
| 393 | return BC_STATUS_SUCCESS; |
| 394 | } |
| 395 | else if (BC_NUM_ONE(b)) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 396 | if ((s = bc_num_copy(c, a))) return s; |
| 397 | if (b->neg) c->neg = !c->neg; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 398 | return bc_num_retireMul(c, scale); |
| 399 | } |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 400 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 401 | if ((s = bc_num_init(&cp, BC_NUM_MREQ(a, b, scale)))) return s; |
| 402 | if ((s = bc_num_copy(&cp, a))) goto err; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 403 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 404 | if ((len = b->len) > cp.len) { |
| 405 | if ((s = bc_num_expand(&cp, len + 2))) goto err; |
| 406 | if ((s = bc_num_extend(&cp, len - cp.len))) goto err; |
| 407 | } |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 408 | |
Gavin Howard | 5065154 | 2018-09-29 02:53:11 -0600 | [diff] [blame] | 409 | 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] | 410 | cp.rdx -= b->rdx; |
Gavin Howard | 5065154 | 2018-09-29 02:53:11 -0600 | [diff] [blame] | 411 | 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] | 412 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 413 | if (b->rdx == b->len) { |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 414 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 415 | bool zero = true; |
| 416 | 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] | 417 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 418 | if (i == len && zero) { |
| 419 | s = BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 420 | goto err; |
| 421 | } |
Gavin Howard | 6dbf6bf | 2018-09-01 15:14:48 -0600 | [diff] [blame] | 422 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 423 | len -= i - 1; |
| 424 | } |
Gavin Howard | c1a6a34 | 2018-03-05 12:10:14 -0700 | [diff] [blame] | 425 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 426 | 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] | 427 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 428 | // We want an extra zero in front to make things simpler. |
| 429 | cp.num[cp.len++] = 0; |
| 430 | end = cp.len - len; |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 431 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 432 | if ((s = bc_num_expand(c, cp.len))) goto err; |
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 | bc_num_zero(c); |
| 435 | c->rdx = cp.rdx; |
| 436 | c->len = cp.len; |
| 437 | bptr = b->num; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 438 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 439 | for (i = end - 1; !bcg.signe && !s && i < end; --i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 440 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 441 | n = cp.num + i; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 442 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 443 | for (q = 0; (!s && n[len]) || bc_num_compare(n, bptr, len) >= 0; ++q) |
| 444 | s = bc_num_subArrays(n, bptr, len); |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 445 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 446 | c->num[i] = q; |
| 447 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 448 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 449 | if (s) goto err; |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 450 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 451 | c->neg = !a->neg != !b->neg; |
| 452 | s = bc_num_retireMul(c, scale); |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 453 | |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 454 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 455 | bc_num_free(&cp); |
| 456 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 457 | } |
| 458 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 459 | BcStatus bc_num_alg_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 460 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 461 | BcStatus s; |
| 462 | BcNum c1, c2; |
Gavin Howard | 841a388 | 2018-10-04 11:28:14 -0600 | [diff] [blame] | 463 | 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] | 464 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 465 | if (!b->len) return BC_STATUS_MATH_DIVIDE_BY_ZERO; |
Gavin Howard | 8b25487 | 2018-03-14 01:13:35 -0600 | [diff] [blame] | 466 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 467 | if (!a->len) { |
| 468 | bc_num_zero(c); |
| 469 | return BC_STATUS_SUCCESS; |
| 470 | } |
Gavin Howard | 8b25487 | 2018-03-14 01:13:35 -0600 | [diff] [blame] | 471 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 472 | if ((s = bc_num_init(&c1, len))) return s; |
| 473 | if ((s = bc_num_init(&c2, len))) goto c2_err; |
| 474 | if ((s = bc_num_div(a, b, &c1, scale))) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 475 | |
Gavin Howard | 841a388 | 2018-10-04 11:28:14 -0600 | [diff] [blame] | 476 | if (scale) scale = ts; |
Gavin Howard | 3115c01 | 2018-10-04 10:53:56 -0600 | [diff] [blame] | 477 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 478 | if ((s = bc_num_mul(&c1, b, &c2, scale))) goto err; |
| 479 | if ((s = bc_num_sub(a, &c2, c, scale))) goto err; |
Gavin Howard | 5d149cf | 2018-09-06 13:46:23 -0600 | [diff] [blame] | 480 | |
Gavin Howard | 841a388 | 2018-10-04 11:28:14 -0600 | [diff] [blame] | 481 | 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] | 482 | |
| 483 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 484 | bc_num_free(&c2); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 485 | c2_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 486 | bc_num_free(&c1); |
| 487 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 490 | BcStatus bc_num_alg_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 491 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 492 | BcStatus s; |
| 493 | BcNum copy; |
| 494 | unsigned long pow; |
| 495 | size_t i, powrdx, resrdx; |
| 496 | bool neg, zero; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 497 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 498 | if (b->rdx) return BC_STATUS_MATH_NON_INTEGER; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 499 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 500 | if (!b->len) { |
| 501 | bc_num_one(c); |
| 502 | return BC_STATUS_SUCCESS; |
| 503 | } |
| 504 | else if (!a->len) { |
| 505 | bc_num_zero(c); |
| 506 | return BC_STATUS_SUCCESS; |
| 507 | } |
| 508 | else if (BC_NUM_ONE(b)) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 509 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 510 | if (!b->neg) s = bc_num_copy(c, a); |
| 511 | else s = bc_num_inv(a, c, scale); |
Gavin Howard | 5fc40e7 | 2018-03-05 12:11:09 -0700 | [diff] [blame] | 512 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 513 | return s; |
| 514 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 515 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 516 | neg = b->neg; |
| 517 | b->neg = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 518 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 519 | if ((s = bc_num_ulong(b, &pow))) return s; |
| 520 | if ((s = bc_num_init(©, a->len))) return s; |
| 521 | if ((s = bc_num_copy(©, a))) goto err; |
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 (!neg) scale = BC_MIN(a->rdx * pow, BC_MAX(scale, a->rdx)); |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 524 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 525 | b->neg = neg; |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 526 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 527 | for (powrdx = a->rdx; !bcg.signe && !(pow & 1); pow >>= 1) { |
| 528 | powrdx <<= 1; |
| 529 | if ((s = bc_num_mul(©, ©, ©, powrdx))) goto err; |
| 530 | } |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 531 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 532 | if ((s = bc_num_copy(c, ©))) goto err; |
| 533 | if (bcg.signe) { |
| 534 | s = BC_STATUS_EXEC_SIGNAL; |
| 535 | goto err; |
| 536 | } |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 537 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 538 | for (resrdx = powrdx, pow >>= 1; !bcg.signe && pow != 0; pow >>= 1) { |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 539 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 540 | powrdx <<= 1; |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 541 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 542 | if ((s = bc_num_mul(©, ©, ©, powrdx))) goto err; |
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 | if (pow & 1) { |
| 545 | resrdx += powrdx; |
| 546 | if ((s = bc_num_mul(c, ©, c, resrdx))) goto err; |
| 547 | } |
| 548 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 549 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 550 | if (neg && (s = bc_num_inv(c, c, scale))) goto err; |
| 551 | if (bcg.signe) { |
| 552 | s = BC_STATUS_EXEC_SIGNAL; |
| 553 | goto err; |
| 554 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 555 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 556 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 557 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 558 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
| 559 | if (zero) bc_num_zero(c); |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 560 | |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 561 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 562 | bc_num_free(©); |
| 563 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 566 | 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] | 567 | BcNumBinaryOp op, size_t req) |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 568 | { |
Gavin Howard | a1c4439 | 2018-09-27 12:41:15 -0600 | [diff] [blame] | 569 | BcStatus s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 570 | BcNum num2, *ptr_a, *ptr_b; |
| 571 | bool init = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 572 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 573 | assert(a && b && c && op); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 574 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 575 | if (c == a) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 576 | ptr_a = &num2; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 577 | memcpy(ptr_a, c, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 578 | init = true; |
| 579 | } |
| 580 | else ptr_a = a; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 581 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 582 | if (c == b) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 583 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 584 | ptr_b = &num2; |
| 585 | |
| 586 | if (c != a) { |
| 587 | memcpy(ptr_b, c, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 588 | init = true; |
| 589 | } |
| 590 | } |
| 591 | else ptr_b = b; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 592 | |
Gavin Howard | a1c4439 | 2018-09-27 12:41:15 -0600 | [diff] [blame] | 593 | if (init) s = bc_num_init(c, req); |
| 594 | else s = bc_num_expand(c, req); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 595 | |
Gavin Howard | 9ab772a | 2018-10-05 11:38:40 -0600 | [diff] [blame] | 596 | if (!s) s = op(ptr_a, ptr_b, c, scale); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 597 | |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 598 | assert(!c->neg || c->len); |
| 599 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 600 | if (init) bc_num_free(&num2); |
Gavin Howard | a1c4439 | 2018-09-27 12:41:15 -0600 | [diff] [blame] | 601 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 604 | bool bc_num_strValid(const char *val, size_t base) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 605 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 606 | BcDig b; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 607 | bool small, radix = false; |
| 608 | size_t i, len = strlen(val); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 609 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 610 | if (!len) return true; |
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 | small = base <= 10; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 613 | b = (BcDig) (small ? base + '0' : base - 9 + 'A'); |
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 | for (i = 0; i < len; ++i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 616 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 617 | BcDig c = val[i]; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 618 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 619 | if (c == '.') { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 620 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 621 | if (radix) return false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 622 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 623 | radix = true; |
| 624 | continue; |
| 625 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 626 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 627 | if (c < '0' || (small && c >= b) || (c > '9' && (c < 'A' || c >= b))) |
| 628 | return false; |
| 629 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 630 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 631 | return true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 634 | BcStatus bc_num_parseDecimal(BcNum *n, const char *val) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 635 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 636 | BcStatus s; |
| 637 | size_t len, i; |
| 638 | const char *ptr; |
| 639 | bool zero = true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 640 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 641 | for (i = 0; val[i] == '0'; ++i); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 642 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 643 | val += i; |
| 644 | len = strlen(val); |
| 645 | bc_num_zero(n); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 646 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 647 | if (len) { |
| 648 | for (i = 0; zero && i < len; ++i) zero = val[i] == '0' || val[i] == '.'; |
| 649 | if ((s = bc_num_expand(n, len))) return s; |
| 650 | } |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 651 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 652 | if (zero) { |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 653 | memset(n->num, 0, sizeof(BcDig) * n->cap); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 654 | n->neg = false; |
| 655 | return BC_STATUS_SUCCESS; |
| 656 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 657 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 658 | ptr = strchr(val, '.'); |
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 | // Explicitly test for NULL here to produce either a 0 or 1. |
| 661 | n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1))); |
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 | for (i = len - 1; i < len; ++n->len, i -= 1 + (i && val[i - 1] == '.')) |
| 664 | n->num[n->len] = val[i] - '0'; |
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 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Gavin Howard | 3f68df7 | 2018-03-22 20:30:27 -0600 | [diff] [blame] | 669 | BcStatus bc_num_parseBase(BcNum *n, const char *val, BcNum *base) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 670 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 671 | BcStatus s; |
| 672 | BcNum temp, mult, result; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 673 | BcDig c = '\0'; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 674 | bool zero; |
| 675 | unsigned long v; |
| 676 | size_t i, digits, len = strlen(val); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 677 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 678 | bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 679 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 680 | for (zero = true, i = 0; zero && i < len; ++i) |
| 681 | zero = (val[i] == '.' || val[i] == '0'); |
| 682 | if (zero) return BC_STATUS_SUCCESS; |
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 | if ((s = bc_num_init(&temp, BC_NUM_DEF_SIZE))) return s; |
| 685 | 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] | 686 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 687 | for (i = 0; i < len && (c = val[i]) != '.'; ++i) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 688 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 689 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
Gavin Howard | fcb6ebb | 2018-03-09 10:41:06 -0700 | [diff] [blame] | 690 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 691 | if ((s = bc_num_mul(n, base, &mult, 0))) goto int_err; |
| 692 | if ((s = bc_num_ulong2num(&temp, v))) goto int_err; |
| 693 | if ((s = bc_num_add(&mult, &temp, n, 0))) goto int_err; |
| 694 | } |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 695 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 696 | if (i == len && !(c = val[i])) goto int_err; |
| 697 | assert(c == '.'); |
| 698 | if ((s = bc_num_init(&result, base->len))) goto int_err; |
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 | bc_num_zero(&result); |
| 701 | bc_num_one(&mult); |
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 | for (i += 1, digits = 0; i < len && (c = val[i]); ++i, ++digits) { |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 704 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 705 | v = (unsigned long) (c <= '9' ? c - '0' : c - 'A' + 10); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 706 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 707 | if ((s = bc_num_mul(&result, base, &result, 0))) goto err; |
| 708 | if ((s = bc_num_ulong2num(&temp, v))) goto err; |
| 709 | if ((s = bc_num_add(&result, &temp, &result, 0))) goto err; |
| 710 | if ((s = bc_num_mul(&mult, base, &mult, 0))) goto err; |
| 711 | } |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 712 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 713 | if ((s = bc_num_div(&result, &mult, &result, digits))) goto err; |
| 714 | if ((s = bc_num_add(n, &result, n, digits))) goto err; |
Gavin Howard | 5b24c3f | 2018-03-13 23:57:15 -0600 | [diff] [blame] | 715 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 716 | if (n->len) { |
| 717 | if (n->rdx < digits && n->len) s = bc_num_extend(n, digits - n->rdx); |
| 718 | } |
| 719 | else bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 720 | |
| 721 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 722 | bc_num_free(&result); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 723 | int_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 724 | bc_num_free(&mult); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 725 | mult_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 726 | bc_num_free(&temp); |
| 727 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 728 | } |
| 729 | |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 730 | BcStatus bc_num_printNewline(size_t *nchars, size_t line_len) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 731 | if (*nchars == line_len - 1) { |
| 732 | if (putchar('\\') == EOF) return BC_STATUS_IO_ERR; |
| 733 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
| 734 | *nchars = 0; |
| 735 | } |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 736 | return BC_STATUS_SUCCESS; |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 737 | } |
| 738 | |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 739 | #ifdef DC_ENABLED |
| 740 | BcStatus bc_num_printChar(size_t num, size_t width, bool radix, |
| 741 | size_t *nchars, size_t line_len) |
| 742 | { |
Gavin Howard | 4c09154 | 2018-10-09 03:33:08 -0600 | [diff] [blame] | 743 | (void) radix, (void) line_len; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 744 | |
Gavin Howard | 4c09154 | 2018-10-09 03:33:08 -0600 | [diff] [blame] | 745 | if (putc((char) num, stdout) == EOF) return BC_STATUS_IO_ERR; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 746 | *nchars = *nchars + width; |
| 747 | |
| 748 | return BC_STATUS_SUCCESS; |
| 749 | } |
| 750 | #endif // DC_ENABLED |
| 751 | |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 752 | BcStatus bc_num_printDigits(size_t num, size_t width, bool radix, |
| 753 | size_t *nchars, size_t line_len) |
| 754 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 755 | BcStatus s; |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 756 | size_t exp, pow, div; |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 757 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 758 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 759 | if (putchar(radix ? '.' : ' ') == EOF) return BC_STATUS_IO_ERR; |
| 760 | ++(*nchars); |
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 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 763 | for (exp = 0, pow = 1; exp < width - 1; ++exp, pow *= 10); |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 764 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 765 | for (exp = 0; exp < width; pow /= 10, ++(*nchars), ++exp) { |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 766 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 767 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 768 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 769 | div = num / pow; |
| 770 | num -= div * pow; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 771 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 772 | if (putchar(((char) div) + '0') == EOF) return BC_STATUS_IO_ERR; |
| 773 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 774 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 775 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 776 | } |
Gavin Howard | fe679f0 | 2018-02-14 15:50:09 -0700 | [diff] [blame] | 777 | |
Gavin Howard | 902a16c | 2018-05-16 01:22:53 -0600 | [diff] [blame] | 778 | BcStatus bc_num_printHex(size_t num, size_t width, bool radix, |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 779 | size_t *nchars, size_t line_len) |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 780 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 781 | BcStatus s; |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 782 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 783 | assert(width == 1); |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 784 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 785 | if (radix) { |
| 786 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
| 787 | if (putchar('.') == EOF) return BC_STATUS_IO_ERR; |
| 788 | *nchars += 1; |
| 789 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 790 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 791 | if ((s = bc_num_printNewline(nchars, line_len))) return s; |
| 792 | if (putchar(bc_num_hex_digits[num]) == EOF) return BC_STATUS_IO_ERR; |
| 793 | *nchars = *nchars + width; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 794 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 795 | return BC_STATUS_SUCCESS; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Gavin Howard | 5f46ee3 | 2018-09-25 13:02:43 -0600 | [diff] [blame] | 798 | BcStatus bc_num_printDecimal(BcNum *n, size_t *nchars, size_t len) { |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 799 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 800 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 801 | size_t i, rdx = n->rdx - 1; |
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 | if (n->neg && putchar('-') == EOF) return BC_STATUS_IO_ERR; |
| 804 | (*nchars) += n->neg; |
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 | for (i = n->len - 1; !s && i < n->len; --i) |
| 807 | 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] | 808 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 809 | return s; |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 810 | } |
| 811 | |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 812 | BcStatus bc_num_printNum(BcNum *n, BcNum *base, size_t width, size_t *nchars, |
| 813 | size_t line_len, BcNumDigitOp print) |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 814 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 815 | BcStatus s; |
| 816 | BcVec stack; |
| 817 | BcNum intp, fracp, digit, frac_len; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 818 | unsigned long dig, *ptr; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 819 | size_t i; |
| 820 | bool radix; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 821 | |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 822 | if ((s = bc_vec_init(&stack, sizeof(long), NULL))) return s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 823 | if ((s = bc_num_init(&intp, n->len))) goto int_err; |
| 824 | if ((s = bc_num_init(&fracp, n->rdx))) goto frac_err; |
| 825 | if ((s = bc_num_init(&digit, width))) goto digit_err; |
| 826 | if ((s = bc_num_init(&frac_len, BC_NUM_INT(n)))) goto frac_len_err; |
| 827 | if ((s = bc_num_copy(&intp, n))) goto err; |
| 828 | bc_num_one(&frac_len); |
Gavin Howard | a50fc54 | 2018-03-29 17:25:38 -0600 | [diff] [blame] | 829 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 830 | bc_num_truncate(&intp, intp.rdx); |
| 831 | if ((s = bc_num_sub(n, &intp, &fracp, 0))) goto err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 832 | |
Gavin Howard | 4c09154 | 2018-10-09 03:33:08 -0600 | [diff] [blame] | 833 | if (!n->len) { |
| 834 | s = print(0, width, false, nchars, line_len); |
| 835 | goto err; |
| 836 | } |
| 837 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 838 | while (intp.len) { |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 839 | if ((s = bc_num_rem(&intp, base, &digit, 0))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 840 | if ((s = bc_num_ulong(&digit, &dig))) goto err; |
Gavin Howard | 9019886 | 2018-09-29 03:37:47 -0600 | [diff] [blame] | 841 | if ((s = bc_vec_push(&stack, &dig))) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 842 | if ((s = bc_num_div(&intp, base, &intp, 0))) goto err; |
| 843 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 844 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 845 | for (i = 0; i < stack.len; ++i) { |
| 846 | ptr = bc_vec_item_rev(&stack, i); |
| 847 | assert(ptr); |
| 848 | if ((s = print(*ptr, width, false, nchars, line_len))) goto err; |
| 849 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 850 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 851 | if (!n->rdx) goto err; |
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 | for (radix = true; frac_len.len <= n->rdx; radix = false) { |
| 854 | if ((s = bc_num_mul(&fracp, base, &fracp, n->rdx))) goto err; |
| 855 | if ((s = bc_num_ulong(&fracp, &dig))) goto err; |
| 856 | if ((s = bc_num_ulong2num(&intp, dig))) goto err; |
| 857 | if ((s = bc_num_sub(&fracp, &intp, &fracp, 0))) goto err; |
| 858 | if ((s = print(dig, width, radix, nchars, line_len))) goto err; |
| 859 | if ((s = bc_num_mul(&frac_len, base, &frac_len, 0))) goto err; |
| 860 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 861 | |
| 862 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 863 | bc_num_free(&frac_len); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 864 | frac_len_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 865 | bc_num_free(&digit); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 866 | digit_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 867 | bc_num_free(&fracp); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 868 | frac_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 869 | bc_num_free(&intp); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 870 | int_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 871 | bc_vec_free(&stack); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 872 | return s; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 875 | BcStatus bc_num_printBase(BcNum *n, BcNum *base, size_t base_t, |
| 876 | size_t *nchars, size_t line_len) |
| 877 | { |
| 878 | BcStatus s; |
| 879 | size_t width, i; |
| 880 | BcNumDigitOp print; |
| 881 | bool neg = n->neg; |
| 882 | |
| 883 | if (neg && putchar('-') == EOF) return BC_STATUS_IO_ERR; |
| 884 | (*nchars) += neg; |
| 885 | |
| 886 | n->neg = false; |
| 887 | |
| 888 | if (base_t <= BC_NUM_MAX_IBASE) { |
| 889 | width = 1; |
| 890 | print = bc_num_printHex; |
| 891 | } |
| 892 | else { |
| 893 | for (i = base_t - 1, width = 0; i != 0; i /= 10, ++width); |
| 894 | print = bc_num_printDigits; |
| 895 | } |
| 896 | |
| 897 | s = bc_num_printNum(n, base, width, nchars, line_len, print); |
| 898 | n->neg = neg; |
| 899 | |
| 900 | return s; |
| 901 | } |
| 902 | |
| 903 | #ifdef DC_ENABLED |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 904 | 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] | 905 | return bc_num_printNum(n, base, 1, nchars, len, bc_num_printChar); |
| 906 | } |
| 907 | #endif // DC_ENABLED |
| 908 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 909 | BcStatus bc_num_init(BcNum *n, size_t request) { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 910 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 911 | assert(n); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 912 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 913 | request = request >= BC_NUM_DEF_SIZE ? request : BC_NUM_DEF_SIZE; |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 914 | memset(n, 0, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 915 | if (!(n->num = malloc(request))) return BC_STATUS_ALLOC_ERR; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 916 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 917 | n->cap = request; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 918 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 919 | return BC_STATUS_SUCCESS; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 922 | BcStatus bc_num_expand(BcNum *n, size_t req) { |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 923 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 924 | BcDig *temp; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 925 | |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 926 | assert(n); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 927 | |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 928 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 929 | |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 930 | if (req <= n->cap) return BC_STATUS_SUCCESS; |
| 931 | if (!(temp = realloc(n->num, req))) return BC_STATUS_ALLOC_ERR; |
| 932 | |
| 933 | memset(temp + n->cap, 0, sizeof(char) * (req - n->cap)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 934 | n->num = temp; |
Gavin Howard | 844e7a5 | 2018-10-09 11:46:39 -0600 | [diff] [blame] | 935 | n->cap = req; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 936 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 937 | return BC_STATUS_SUCCESS; |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 938 | } |
| 939 | |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 940 | void bc_num_free(void *num) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 941 | BcNum *n = (BcNum*) num; |
Gavin Howard | 8c01998 | 2018-10-05 12:35:09 -0600 | [diff] [blame] | 942 | assert(n); |
Gavin Howard | 2bc5418 | 2018-10-06 17:03:46 -0600 | [diff] [blame] | 943 | free(n->num); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 946 | BcStatus bc_num_copy(BcNum *d, BcNum *s) { |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 947 | |
Gavin Howard | b20c2db | 2018-10-08 10:31:23 -0600 | [diff] [blame] | 948 | BcStatus status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 949 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 950 | assert(d && s); |
Gavin Howard | f23448c | 2018-02-27 20:36:24 -0700 | [diff] [blame] | 951 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 952 | if (d == s) return BC_STATUS_SUCCESS; |
Gavin Howard | b20c2db | 2018-10-08 10:31:23 -0600 | [diff] [blame] | 953 | if ((status = bc_num_expand(d, s->cap))) return status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 954 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 955 | d->len = s->len; |
| 956 | d->neg = s->neg; |
| 957 | d->rdx = s->rdx; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 958 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 959 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
| 960 | memset(d->num + d->len, 0, sizeof(BcDig) * (d->cap - d->len)); |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 961 | |
Gavin Howard | b20c2db | 2018-10-08 10:31:23 -0600 | [diff] [blame] | 962 | return status; |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Gavin Howard | 5cea43b | 2018-03-02 11:32:11 -0700 | [diff] [blame] | 965 | 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] | 966 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 967 | BcStatus s; |
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 | assert(n && val && base); |
| 970 | 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] | 971 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 972 | 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] | 973 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 974 | if (base_t == 10) s = bc_num_parseDecimal(n, val); |
| 975 | else s = bc_num_parseBase(n, val, base); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 976 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 977 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Gavin Howard | 0011a3a | 2018-03-29 23:11:32 -0600 | [diff] [blame] | 980 | BcStatus bc_num_print(BcNum *n, BcNum *base, size_t base_t, bool newline, |
| 981 | size_t *nchars, size_t line_len) |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 982 | { |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 983 | BcStatus s = BC_STATUS_SUCCESS; |
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 | assert(n && base && nchars); |
| 986 | assert(base_t >= BC_NUM_MIN_BASE && base_t <= BC_MAX_OBASE); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 987 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 988 | if (*nchars >= line_len) { |
| 989 | if (putchar('\\') == EOF) return BC_STATUS_IO_ERR; |
| 990 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
| 991 | *nchars = 0; |
| 992 | } |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 993 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 994 | if (!n->len) { |
| 995 | if (putchar('0') == EOF) return BC_STATUS_IO_ERR; |
| 996 | ++(*nchars); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 997 | } |
| 998 | else if (base_t == 10) s = bc_num_printDecimal(n, nchars, line_len); |
| 999 | else s = bc_num_printBase(n, base, base_t, nchars, line_len); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1000 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1001 | if (s) return s; |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1002 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1003 | if (newline) { |
| 1004 | if (putchar('\n') == EOF) return BC_STATUS_IO_ERR; |
| 1005 | *nchars = 0; |
| 1006 | } |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1007 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1008 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1011 | BcStatus bc_num_ulong(BcNum *n, unsigned long *result) { |
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 | size_t i; |
| 1014 | unsigned long pow; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1015 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1016 | assert(n && result); |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1017 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1018 | if (n->neg) return BC_STATUS_MATH_NEGATIVE; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1019 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1020 | for (*result = 0, pow = 1, i = n->rdx; i < n->len; ++i) { |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1021 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1022 | unsigned long prev = *result, powprev = pow; |
| 1023 | *result += ((unsigned long) n->num[i]) * pow; |
| 1024 | pow *= 10; |
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 | if (*result < prev || pow < powprev) return BC_STATUS_MATH_OVERFLOW; |
| 1027 | } |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1028 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1029 | return BC_STATUS_SUCCESS; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Gavin Howard | 8d1f1db | 2018-02-23 11:29:41 -0700 | [diff] [blame] | 1032 | BcStatus bc_num_ulong2num(BcNum *n, unsigned long val) { |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1033 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1034 | BcStatus s; |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1035 | size_t len; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 1036 | BcDig *ptr; |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1037 | unsigned long i; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1038 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1039 | assert(n); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1040 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1041 | bc_num_zero(n); |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1042 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1043 | if (!val) { |
| 1044 | memset(n->num, 0, sizeof(char) * n->cap); |
| 1045 | return BC_STATUS_SUCCESS; |
| 1046 | } |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1047 | |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1048 | for (len = 1, i = ULONG_MAX; i != 0; i /= 10, ++len) |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1049 | if ((s = bc_num_expand(n, len))) return s; |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1050 | 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] | 1051 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1052 | return BC_STATUS_SUCCESS; |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1055 | 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] | 1056 | (void) scale; |
| 1057 | 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] | 1058 | 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] | 1059 | } |
| 1060 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1061 | 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] | 1062 | (void) scale; |
| 1063 | 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] | 1064 | 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] | 1065 | } |
| 1066 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1067 | 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] | 1068 | size_t req = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1069 | 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] | 1070 | } |
| 1071 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1072 | 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] | 1073 | size_t req = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1074 | 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] | 1075 | } |
| 1076 | |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 1077 | 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] | 1078 | size_t req = BC_NUM_MREQ(a, b, scale); |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 1079 | 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] | 1080 | } |
| 1081 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1082 | BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
| 1083 | 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] | 1084 | } |
| 1085 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1086 | BcStatus bc_num_sqrt(BcNum *a, BcNum *b, size_t scale) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1087 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1088 | BcStatus s; |
| 1089 | BcNum a2, *ptr_a, num1, num2, half, f, fprime, *x0, *x1, *temp; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1090 | size_t pow, len, digits, digits1, resrdx, req, times = 0; |
| 1091 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1092 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1093 | assert(a && b); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1094 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1095 | 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] | 1096 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1097 | if (b == a) { |
| 1098 | memcpy(&a2, b, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1099 | ptr_a = &a2; |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1100 | s = bc_num_init(b, req); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1101 | } |
| 1102 | else { |
| 1103 | ptr_a = a; |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1104 | s = bc_num_expand(b, req); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1105 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1106 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1107 | if (s) goto init_err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1108 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1109 | if (!ptr_a->len) { |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1110 | bc_num_zero(b); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1111 | goto init_err; |
| 1112 | } |
| 1113 | else if (ptr_a->neg) { |
Gavin Howard | 4c4f5f0 | 2018-10-11 11:52:11 -0600 | [diff] [blame] | 1114 | s = BC_STATUS_MATH_NEGATIVE; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1115 | goto init_err; |
| 1116 | } |
| 1117 | else if (BC_NUM_ONE(a)) { |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1118 | bc_num_one(b); |
| 1119 | s = bc_num_extend(b, scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1120 | goto init_err; |
| 1121 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1122 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame^] | 1123 | memset(b->num, 0, b->cap * sizeof(BcDig)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1124 | len = ptr_a->len; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1125 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1126 | scale = BC_MAX(scale, ptr_a->rdx) + 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1127 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1128 | if ((s = bc_num_init(&num1, len))) goto init_err; |
| 1129 | if ((s = bc_num_init(&num2, num1.len))) goto num2_err; |
| 1130 | 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] | 1131 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1132 | bc_num_one(&half); |
| 1133 | half.num[0] = 5; |
| 1134 | half.rdx = 1; |
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 | len += scale; |
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 | if ((s = bc_num_init(&f, len))) goto f_err; |
| 1139 | if ((s = bc_num_init(&fprime, len + scale))) goto fprime_err; |
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 | x0 = &num1; |
| 1142 | x1 = &num2; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1143 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1144 | bc_num_one(x0); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1145 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1146 | if ((pow = BC_NUM_INT(ptr_a))) { |
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 | if (pow & 1) x0->num[0] = 2; |
| 1149 | else x0->num[0] = 6; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1150 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1151 | pow -= 2 - (pow & 1); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1152 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1153 | if ((s = bc_num_extend(x0, pow))) goto err; |
Gavin Howard | ed742ce | 2018-08-29 14:23:12 -0600 | [diff] [blame] | 1154 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1155 | // Make sure to move the radix back. |
| 1156 | x0->rdx -= pow; |
| 1157 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1158 | |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1159 | x0->rdx = digits = digits1 = 0; |
| 1160 | resrdx = scale + 2; |
| 1161 | len = BC_NUM_INT(x0) + resrdx - 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1162 | |
Gavin Howard | 0bfcf7a | 2018-10-11 09:37:09 -0600 | [diff] [blame] | 1163 | while (!bcg.signe && (cmp || digits < len)) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1164 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1165 | if ((s = bc_num_div(a, x0, &f, resrdx))) goto err; |
| 1166 | if ((s = bc_num_add(x0, &f, &fprime, resrdx))) goto err; |
| 1167 | if ((s = bc_num_mul(&fprime, &half, x1, resrdx))) goto err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1168 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1169 | cmp = bc_num_cmp(x1, x0); |
| 1170 | digits = x1->len - (unsigned long long) llabs(cmp); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1171 | |
Gavin Howard | b11e9e2 | 2018-10-06 17:26:02 -0600 | [diff] [blame] | 1172 | if (cmp == cmp2 && digits == digits1) times += 1; |
| 1173 | else times = 0; |
| 1174 | |
| 1175 | resrdx += times > 4; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1176 | |
| 1177 | cmp2 = cmp1; |
| 1178 | cmp1 = cmp; |
| 1179 | digits1 = digits; |
| 1180 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1181 | temp = x0; |
| 1182 | x0 = x1; |
| 1183 | x1 = temp; |
| 1184 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1185 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1186 | if (bcg.signe) { |
| 1187 | s = BC_STATUS_EXEC_SIGNAL; |
| 1188 | goto err; |
| 1189 | } |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 1190 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1191 | if ((s = bc_num_copy(b, x0))) goto err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1192 | |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1193 | if (b->rdx > --scale) bc_num_truncate(b, b->rdx - scale); |
| 1194 | 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] | 1195 | |
| 1196 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1197 | bc_num_free(&fprime); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1198 | fprime_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1199 | bc_num_free(&f); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1200 | f_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1201 | bc_num_free(&half); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1202 | two_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1203 | bc_num_free(&num2); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1204 | num2_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1205 | bc_num_free(&num1); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1206 | init_err: |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1207 | if (b == a) bc_num_free(&a2); |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 1208 | assert(!b->neg || b->len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1209 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1210 | } |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1211 | |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1212 | #ifdef DC_ENABLED |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1213 | BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) { |
| 1214 | |
| 1215 | BcStatus s; |
Gavin Howard | 2c8959e | 2018-10-04 18:32:58 -0600 | [diff] [blame] | 1216 | BcNum num2, *ptr_a, *ptr_b, *ptr_c, base, exp, two, temp; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1217 | bool init = false; |
| 1218 | |
| 1219 | assert(a && b && c && d); |
| 1220 | |
| 1221 | scale = 0; |
| 1222 | |
| 1223 | if (d == a) { |
| 1224 | memcpy(&num2, d, sizeof(BcNum)); |
| 1225 | ptr_a = &num2; |
| 1226 | init = true; |
| 1227 | } |
| 1228 | else ptr_a = a; |
| 1229 | |
| 1230 | if (d == b) { |
| 1231 | |
| 1232 | ptr_b = &num2; |
| 1233 | |
| 1234 | if (d != a) { |
| 1235 | memcpy(ptr_b, d, sizeof(BcNum)); |
| 1236 | init = true; |
| 1237 | } |
| 1238 | } |
| 1239 | else ptr_b = b; |
| 1240 | |
| 1241 | if (d == c) { |
| 1242 | |
| 1243 | ptr_c = &num2; |
| 1244 | |
| 1245 | if (d != a && d != b) { |
| 1246 | memcpy(ptr_c, d, sizeof(BcNum)); |
| 1247 | init = true; |
| 1248 | } |
| 1249 | } |
| 1250 | else ptr_c = c; |
| 1251 | |
| 1252 | if (init) s = bc_num_init(d, ptr_c->len); |
| 1253 | else s = bc_num_expand(d, ptr_c->len); |
| 1254 | |
| 1255 | if (s) return s; |
| 1256 | |
| 1257 | if (!ptr_c->len) { |
| 1258 | s = BC_STATUS_MATH_DIVIDE_BY_ZERO; |
| 1259 | goto base_err; |
| 1260 | } |
| 1261 | |
Gavin Howard | 5ef610b | 2018-10-04 16:57:47 -0600 | [diff] [blame] | 1262 | if (ptr_a->rdx || ptr_b->rdx || ptr_c->rdx) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1263 | s = BC_STATUS_MATH_NON_INTEGER; |
| 1264 | goto base_err; |
| 1265 | } |
| 1266 | |
| 1267 | if (ptr_b->neg) { |
| 1268 | s = BC_STATUS_MATH_NEGATIVE; |
| 1269 | goto base_err; |
| 1270 | } |
| 1271 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1272 | if ((s = bc_num_init(&base, ptr_c->len))) goto base_err; |
| 1273 | if ((s = bc_num_init(&exp, ptr_b->len))) goto exp_err; |
| 1274 | 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] | 1275 | if ((s = bc_num_init(&temp, ptr_b->len))) goto temp_err; |
| 1276 | |
| 1277 | bc_num_one(&two); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1278 | two.num[0] = 2; |
| 1279 | bc_num_one(d); |
| 1280 | |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 1281 | 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] | 1282 | if ((s = bc_num_copy(&exp, ptr_b))) goto err; |
| 1283 | |
Gavin Howard | af90560 | 2018-09-29 18:56:37 -0600 | [diff] [blame] | 1284 | while (exp.len) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1285 | |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 1286 | if ((s = bc_num_rem(&exp, &two, &temp, scale))) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1287 | |
| 1288 | if (BC_NUM_ONE(&temp)) { |
| 1289 | if ((s = bc_num_mul(d, &base, &temp, scale))) goto err; |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 1290 | if ((s = bc_num_rem(&temp, ptr_c, d, scale))) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | if ((s = bc_num_div(&exp, &two, &exp, scale))) goto err; |
| 1294 | if ((s = bc_num_mul(&base, &base, &temp, scale))) goto err; |
Gavin Howard | be53edd | 2018-10-04 10:55:08 -0600 | [diff] [blame] | 1295 | if ((s = bc_num_rem(&temp, ptr_c, &base, scale))) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | err: |
| 1299 | bc_num_free(&temp); |
| 1300 | temp_err: |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1301 | bc_num_free(&two); |
| 1302 | two_err: |
| 1303 | bc_num_free(&exp); |
| 1304 | exp_err: |
| 1305 | bc_num_free(&base); |
| 1306 | base_err: |
| 1307 | if (init) bc_num_free(&num2); |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 1308 | assert(!d->neg || d->len); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1309 | return s; |
| 1310 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1311 | #endif // DC_ENABLED |