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 | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 24 | #include <ctype.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 | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 29 | #include <limits.h> |
| 30 | |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 31 | #include <status.h> |
Gavin Howard | 3ba6c8d | 2018-02-15 12:23:35 -0700 | [diff] [blame] | 32 | #include <num.h> |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 33 | #include <vm.h> |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 34 | |
Gavin Howard | 56ebcf7 | 2019-02-21 16:59:48 -0700 | [diff] [blame] | 35 | static ssize_t bc_num_neg(size_t n, bool neg) { |
| 36 | return (((ssize_t) n) ^ -((ssize_t) neg)) + neg; |
| 37 | } |
| 38 | |
Gavin Howard | d2cf06a | 2019-02-21 17:03:24 -0700 | [diff] [blame^] | 39 | ssize_t bc_num_cmpZero(const BcNum *n) { |
Gavin Howard | 56ebcf7 | 2019-02-21 16:59:48 -0700 | [diff] [blame] | 40 | return bc_num_neg((n)->len != 0, (n)->neg); |
Gavin Howard | 9d91b2c | 2019-02-21 16:55:32 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 43 | static size_t bc_num_int(const BcNum *n) { |
| 44 | return n->len ? n->len - n->rdx : 0; |
| 45 | } |
| 46 | |
Gavin Howard | a83a5e5 | 2019-02-21 16:51:06 -0700 | [diff] [blame] | 47 | static bool bc_num_isOne(const BcNum *n) { |
| 48 | return n->len == 1 && n->rdx == 0 && n->num[0] == 1; |
| 49 | } |
| 50 | |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 51 | static void bc_num_expand(BcNum *restrict n, size_t req) { |
| 52 | assert(n); |
| 53 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 54 | if (req > n->cap) { |
| 55 | n->num = bc_vm_realloc(n->num, req); |
| 56 | n->cap = req; |
| 57 | } |
| 58 | } |
| 59 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 60 | static void bc_num_setToZero(BcNum *restrict n, size_t scale) { |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 61 | assert(n); |
Gavin Howard | 97102b1 | 2018-11-01 23:37:31 -0600 | [diff] [blame] | 62 | n->len = 0; |
| 63 | n->neg = false; |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 64 | n->rdx = scale; |
| 65 | } |
| 66 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 67 | static void bc_num_zero(BcNum *restrict n) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 68 | bc_num_setToZero(n, 0); |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 69 | } |
| 70 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 71 | void bc_num_one(BcNum *restrict n) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 72 | bc_num_setToZero(n, 0); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 73 | n->len = 1; |
| 74 | n->num[0] = 1; |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 75 | } |
| 76 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 77 | void bc_num_ten(BcNum *restrict n) { |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 78 | assert(n); |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 79 | bc_num_setToZero(n, 0); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 80 | n->len = 2; |
| 81 | n->num[0] = 0; |
| 82 | n->num[1] = 1; |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 83 | } |
| 84 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 85 | static size_t bc_num_log10(size_t i) { |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 86 | size_t len; |
| 87 | for (len = 1; i; i /= 10, ++len); |
| 88 | return len; |
| 89 | } |
| 90 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 91 | static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b, |
| 92 | size_t len) |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 93 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 94 | size_t i, j; |
Gavin Howard | 5dbb48b | 2019-01-04 11:29:29 -0700 | [diff] [blame] | 95 | for (i = 0; !BC_SIGNAL && i < len; ++i) { |
| 96 | for (a[i] -= b[i], j = 0; !BC_SIGNAL && a[i + j] < 0;) { |
Gavin Howard | 54b946a | 2018-10-23 12:06:57 -0600 | [diff] [blame] | 97 | a[i + j++] += 10; |
| 98 | a[i + j] -= 1; |
Gavin Howard | 231e66c | 2019-01-08 23:02:19 -0700 | [diff] [blame] | 99 | assert(a[i + j - 1] >= 0 && a[i + j - 1] < 10); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 100 | } |
| 101 | } |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 102 | return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 103 | } |
| 104 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 105 | static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b, size_t len) |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 106 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 107 | size_t i; |
Gavin Howard | d1a7836 | 2018-10-18 09:52:06 -0600 | [diff] [blame] | 108 | int c = 0; |
Gavin Howard | 5dbb48b | 2019-01-04 11:29:29 -0700 | [diff] [blame] | 109 | for (i = len - 1; !BC_SIGNAL && i < len && !(c = a[i] - b[i]); --i); |
Gavin Howard | 56ebcf7 | 2019-02-21 16:59:48 -0700 | [diff] [blame] | 110 | return bc_num_neg(i + 1, c < 0); |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 111 | } |
| 112 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 113 | ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) { |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 114 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 115 | size_t i, min, a_int, b_int, diff; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 116 | BcDig *max_num, *min_num; |
Gavin Howard | 7fbb4e2 | 2018-10-18 11:43:17 -0600 | [diff] [blame] | 117 | bool a_max, neg = false; |
| 118 | ssize_t cmp; |
| 119 | |
| 120 | assert(a && b); |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 121 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 122 | if (a == b) return 0; |
Gavin Howard | 56ebcf7 | 2019-02-21 16:59:48 -0700 | [diff] [blame] | 123 | if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg); |
Gavin Howard | d2cf06a | 2019-02-21 17:03:24 -0700 | [diff] [blame^] | 124 | if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a); |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 125 | if (a->neg) { |
Gavin Howard | 7fbb4e2 | 2018-10-18 11:43:17 -0600 | [diff] [blame] | 126 | if (b->neg) neg = true; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 127 | else return -1; |
| 128 | } |
| 129 | else if (b->neg) return 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 130 | |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 131 | a_int = bc_num_int(a); |
| 132 | b_int = bc_num_int(b); |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 133 | a_int -= b_int; |
| 134 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 135 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 136 | if (a_int) return (ssize_t) a_int; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 137 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 138 | if (a_max) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 139 | min = b->rdx; |
| 140 | diff = a->rdx - b->rdx; |
| 141 | max_num = a->num + diff; |
| 142 | min_num = b->num; |
| 143 | } |
| 144 | else { |
| 145 | min = a->rdx; |
| 146 | diff = b->rdx - a->rdx; |
| 147 | max_num = b->num + diff; |
| 148 | min_num = a->num; |
| 149 | } |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 150 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 151 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Gavin Howard | 56ebcf7 | 2019-02-21 16:59:48 -0700 | [diff] [blame] | 152 | if (cmp) return bc_num_neg((size_t) cmp, (!a_max) != neg); |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 153 | |
Gavin Howard | 5dbb48b | 2019-01-04 11:29:29 -0700 | [diff] [blame] | 154 | for (max_num -= diff, i = diff - 1; !BC_SIGNAL && i < diff; --i) { |
Gavin Howard | 56ebcf7 | 2019-02-21 16:59:48 -0700 | [diff] [blame] | 155 | if (max_num[i]) return bc_num_neg(1, (!a_max) != neg); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 156 | } |
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 0; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Gavin Howard | 2a36692 | 2019-01-16 10:50:20 -0700 | [diff] [blame] | 161 | static void bc_num_clean(BcNum *restrict n) { |
| 162 | while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len; |
| 163 | if (BC_NUM_ZERO(n)) n->neg = false; |
| 164 | else if (n->len < n->rdx) n->len = n->rdx; |
| 165 | } |
| 166 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 167 | void bc_num_truncate(BcNum *restrict n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 168 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 169 | assert(places <= n->rdx && (BC_NUM_ZERO(n) || places <= n->len)); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 170 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 171 | if (!places) return; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 172 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 173 | n->rdx -= places; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 174 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 175 | if (BC_NUM_NONZERO(n)) { |
Gavin Howard | 955da85 | 2018-10-23 11:08:20 -0600 | [diff] [blame] | 176 | n->len -= places; |
| 177 | memmove(n->num, n->num + places, n->len * sizeof(BcDig)); |
Gavin Howard | 2a36692 | 2019-01-16 10:50:20 -0700 | [diff] [blame] | 178 | bc_num_clean(n); |
Gavin Howard | 955da85 | 2018-10-23 11:08:20 -0600 | [diff] [blame] | 179 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 182 | static void bc_num_extend(BcNum *restrict n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 183 | |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 184 | size_t len = bc_vm_checkSize(n->len, places); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 185 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 186 | if (!places) return; |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 187 | if (n->cap < len) bc_num_expand(n, len); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 188 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 189 | memmove(n->num + places, n->num, sizeof(BcDig) * n->len); |
| 190 | memset(n->num, 0, sizeof(BcDig) * places); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 191 | |
Gavin Howard | c4bf4c4 | 2019-01-09 10:30:15 -0700 | [diff] [blame] | 192 | if (n->len) n->len += places; |
| 193 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 194 | n->rdx += places; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 197 | static void bc_num_retireMul(BcNum *restrict n, size_t scale, bool neg1, bool neg2) { |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 198 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 199 | if (n->rdx < scale) bc_num_extend(n, scale - n->rdx); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 200 | else bc_num_truncate(n, n->rdx - scale); |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 201 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 202 | bc_num_clean(n); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 203 | if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 204 | } |
| 205 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 206 | static void bc_num_split(const BcNum *restrict n, size_t idx, BcNum *restrict a, BcNum *restrict b) { |
Gavin Howard | 50c8002 | 2019-01-03 15:14:33 -0700 | [diff] [blame] | 207 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 208 | if (idx < n->len) { |
| 209 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 210 | b->len = n->len - idx; |
| 211 | a->len = idx; |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 212 | a->rdx = b->rdx = 0; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 213 | |
| 214 | memcpy(b->num, n->num + idx, b->len * sizeof(BcDig)); |
| 215 | memcpy(a->num, n->num, idx * sizeof(BcDig)); |
Gavin Howard | 50c8002 | 2019-01-03 15:14:33 -0700 | [diff] [blame] | 216 | |
| 217 | bc_num_clean(b); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 218 | } |
Gavin Howard | 50c8002 | 2019-01-03 15:14:33 -0700 | [diff] [blame] | 219 | else bc_num_copy(a, n); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 220 | |
| 221 | bc_num_clean(a); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 222 | } |
| 223 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 224 | static void bc_num_shift_l(BcNum *restrict n, size_t places) { |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 225 | |
Gavin Howard | d8b004b | 2019-02-16 22:35:08 -0700 | [diff] [blame] | 226 | if (!places || BC_NUM_ZERO(n)) return; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 227 | |
| 228 | if (n->rdx >= places) n->rdx -= places; |
| 229 | else { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 230 | bc_num_extend(n, places - n->rdx); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 231 | n->rdx = 0; |
| 232 | } |
| 233 | |
| 234 | bc_num_clean(n); |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 235 | } |
| 236 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 237 | #if BC_ENABLE_EXTRA_MATH |
| 238 | static void bc_num_shift_r(BcNum *restrict n, size_t places) { |
| 239 | |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 240 | size_t len = bc_vm_checkSize(n->rdx, places); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 241 | |
| 242 | if (len > n->len) { |
| 243 | |
| 244 | if (len > n->cap) bc_num_expand(n, len); |
| 245 | |
| 246 | memset(n->num + n->len, 0, len - n->len); |
| 247 | n->len = len; |
| 248 | } |
| 249 | |
| 250 | n->rdx += places; |
| 251 | |
| 252 | assert(n->rdx <= n->len && n->len <= n->cap); |
| 253 | } |
| 254 | #endif // BC_ENABLE_EXTRA_MATH |
| 255 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 256 | static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 257 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 258 | BcNum one; |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 259 | BcDig num[2]; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 260 | |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 261 | assert(!BC_NUM_ZERO(a)); |
| 262 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 263 | one.cap = 2; |
| 264 | one.num = num; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 265 | bc_num_one(&one); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 266 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 267 | return bc_num_div(&one, a, b, scale); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 268 | } |
| 269 | |
Gavin Howard | 7bda478 | 2018-12-28 09:53:22 -0700 | [diff] [blame] | 270 | #if BC_ENABLE_EXTRA_MATH |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 271 | static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c, |
| 272 | unsigned long *v) |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 273 | { |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 274 | if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER); |
| 275 | bc_num_copy(c, a); |
Gavin Howard | 7bda478 | 2018-12-28 09:53:22 -0700 | [diff] [blame] | 276 | return bc_num_ulong(b, v); |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 277 | } |
Gavin Howard | 7bda478 | 2018-12-28 09:53:22 -0700 | [diff] [blame] | 278 | #endif // BC_ENABLE_EXTRA_MATH |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 279 | |
Gavin Howard | dd7a0fd | 2019-01-22 09:56:46 -0700 | [diff] [blame] | 280 | static unsigned int bc_num_addDigit(BcDig *restrict num, unsigned int d, unsigned int c) |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 281 | { |
Gavin Howard | dd7a0fd | 2019-01-22 09:56:46 -0700 | [diff] [blame] | 282 | d += c; |
Gavin Howard | dd7a0fd | 2019-01-22 09:56:46 -0700 | [diff] [blame] | 283 | *num = (BcDig) (d % 10); |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 284 | assert(*num >= 0 && *num < 10); |
Gavin Howard | 201cb6f | 2019-01-22 10:01:29 -0700 | [diff] [blame] | 285 | assert((d / 10) < 10); |
| 286 | return d / 10; |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 289 | static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 290 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 291 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 292 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 293 | unsigned int carry; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 294 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 295 | // Because this function doesn't need to use scale (per the bc spec), |
| 296 | // 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] | 297 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 298 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 299 | bc_num_copy(c, b); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 300 | if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 301 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 302 | } |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 303 | if (BC_NUM_ZERO(b)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 304 | bc_num_copy(c, a); |
| 305 | return BC_STATUS_SUCCESS; |
| 306 | } |
Gavin Howard | f6964a1 | 2018-03-14 10:52:31 -0600 | [diff] [blame] | 307 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 308 | c->neg = a->neg; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 309 | c->rdx = BC_MAX(a->rdx, b->rdx); |
| 310 | min_rdx = BC_MIN(a->rdx, b->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 311 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 312 | if (a->rdx > b->rdx) { |
| 313 | diff = a->rdx - b->rdx; |
| 314 | ptr = a->num; |
| 315 | ptr_a = a->num + diff; |
| 316 | ptr_b = b->num; |
| 317 | } |
| 318 | else { |
| 319 | diff = b->rdx - a->rdx; |
| 320 | ptr = b->num; |
| 321 | ptr_a = a->num; |
| 322 | ptr_b = b->num + diff; |
| 323 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 324 | |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 325 | for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i]; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 326 | |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 327 | c->len = diff; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 328 | ptr_c += diff; |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 329 | a_int = bc_num_int(a); |
| 330 | b_int = bc_num_int(b); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 331 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 332 | if (a_int > b_int) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 333 | min_int = b_int; |
| 334 | max = a_int; |
| 335 | ptr = ptr_a; |
| 336 | } |
| 337 | else { |
| 338 | min_int = a_int; |
| 339 | max = b_int; |
| 340 | ptr = ptr_b; |
| 341 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 342 | |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 343 | for (carry = 0, i = 0; !BC_SIGNAL && i < min_rdx + min_int; ++i) { |
| 344 | unsigned int in = (unsigned int) (ptr_a[i] + ptr_b[i]); |
Gavin Howard | dd7a0fd | 2019-01-22 09:56:46 -0700 | [diff] [blame] | 345 | carry = bc_num_addDigit(ptr_c + i, in, carry); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 346 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 347 | |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 348 | for (; !BC_SIGNAL && i < max + min_rdx; ++i) |
Gavin Howard | dd7a0fd | 2019-01-22 09:56:46 -0700 | [diff] [blame] | 349 | carry = bc_num_addDigit(ptr_c + i, (unsigned int) ptr[i], carry); |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 350 | |
| 351 | c->len += i; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 352 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 353 | if (carry) c->num[c->len++] = (BcDig) carry; |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 354 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 355 | return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 356 | } |
| 357 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 358 | static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 359 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 360 | BcStatus s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 361 | ssize_t cmp; |
| 362 | BcNum *minuend, *subtrahend; |
| 363 | size_t start; |
| 364 | bool aneg, bneg, neg; |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 365 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 366 | // Because this function doesn't need to use scale (per the bc spec), |
| 367 | // 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] | 368 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 369 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 370 | bc_num_copy(c, b); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 371 | if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 372 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 373 | } |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 374 | if (BC_NUM_ZERO(b)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 375 | bc_num_copy(c, a); |
| 376 | return BC_STATUS_SUCCESS; |
| 377 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 378 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 379 | aneg = a->neg; |
| 380 | bneg = b->neg; |
| 381 | a->neg = b->neg = false; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 382 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 383 | cmp = bc_num_cmp(a, b); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 384 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 385 | a->neg = aneg; |
| 386 | b->neg = bneg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 387 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 388 | if (!cmp) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 389 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 390 | return BC_STATUS_SUCCESS; |
| 391 | } |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 392 | |
| 393 | if (cmp > 0) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 394 | neg = a->neg; |
| 395 | minuend = a; |
| 396 | subtrahend = b; |
| 397 | } |
| 398 | else { |
| 399 | neg = b->neg; |
| 400 | if (sub) neg = !neg; |
| 401 | minuend = b; |
| 402 | subtrahend = a; |
| 403 | } |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 404 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 405 | bc_num_copy(c, minuend); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 406 | c->neg = neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 407 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 408 | if (c->rdx < subtrahend->rdx) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 409 | bc_num_extend(c, subtrahend->rdx - c->rdx); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 410 | start = 0; |
| 411 | } |
| 412 | else start = c->rdx - subtrahend->rdx; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 413 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 414 | s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | fa4983a | 2019-02-16 23:43:03 -0700 | [diff] [blame] | 415 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 416 | bc_num_clean(c); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 417 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 418 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 421 | static BcStatus bc_num_k(const BcNum *a, const BcNum *b, BcNum *restrict c) { |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 422 | |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 423 | BcStatus s; |
Gavin Howard | 34c4ba0 | 2018-12-11 10:25:01 -0700 | [diff] [blame] | 424 | size_t max = BC_MAX(a->len, b->len), max2 = (max + 1) / 2; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 425 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Gavin Howard | a83a5e5 | 2019-02-21 16:51:06 -0700 | [diff] [blame] | 426 | bool aone = bc_num_isOne(a); |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 427 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 428 | // This is here because the function is recursive. |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 429 | if (BC_SIGNAL) return BC_STATUS_SIGNAL; |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 430 | if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 431 | bc_num_zero(c); |
| 432 | return BC_STATUS_SUCCESS; |
| 433 | } |
Gavin Howard | a83a5e5 | 2019-02-21 16:51:06 -0700 | [diff] [blame] | 434 | if (aone || bc_num_isOne(b)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 435 | bc_num_copy(c, aone ? b : a); |
| 436 | return BC_STATUS_SUCCESS; |
| 437 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 438 | |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 439 | if (a->len + b->len < BC_NUM_KARATSUBA_LEN || |
| 440 | a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 441 | { |
Gavin Howard | 34c4ba0 | 2018-12-11 10:25:01 -0700 | [diff] [blame] | 442 | size_t i, j, len; |
Gavin Howard | 2aef61b | 2018-12-18 10:56:20 -0700 | [diff] [blame] | 443 | unsigned int carry; |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 444 | BcDig *ptr_c; |
Gavin Howard | 34c4ba0 | 2018-12-11 10:25:01 -0700 | [diff] [blame] | 445 | |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 446 | bc_num_expand(c, bc_vm_checkSize3(a->len, b->len, 1)); |
Gavin Howard | 23d8d25 | 2018-10-17 12:03:41 -0600 | [diff] [blame] | 447 | |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 448 | ptr_c = c->num; |
| 449 | memset(ptr_c, 0, sizeof(BcDig) * c->cap); |
Gavin Howard | 34c4ba0 | 2018-12-11 10:25:01 -0700 | [diff] [blame] | 450 | c->len = len = 0; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 451 | |
Gavin Howard | 5dbb48b | 2019-01-04 11:29:29 -0700 | [diff] [blame] | 452 | for (i = 0; !BC_SIGNAL && i < b->len; ++i) { |
Gavin Howard | d1a7836 | 2018-10-18 09:52:06 -0600 | [diff] [blame] | 453 | |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 454 | BcDig *ptr = ptr_c + i; |
| 455 | |
Gavin Howard | 34c4ba0 | 2018-12-11 10:25:01 -0700 | [diff] [blame] | 456 | carry = 0; |
| 457 | |
Gavin Howard | 5dbb48b | 2019-01-04 11:29:29 -0700 | [diff] [blame] | 458 | for (j = 0; !BC_SIGNAL && j < a->len; ++j) { |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 459 | unsigned int in = (uchar) ptr[j]; |
| 460 | assert(in < 10); |
Gavin Howard | 2aef61b | 2018-12-18 10:56:20 -0700 | [diff] [blame] | 461 | in += ((unsigned int) a->num[j]) * ((unsigned int) b->num[i]); |
Gavin Howard | dd7a0fd | 2019-01-22 09:56:46 -0700 | [diff] [blame] | 462 | carry = bc_num_addDigit(ptr + j, in, carry); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 463 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 464 | |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 465 | ptr[j] += (BcDig) carry; |
| 466 | assert(ptr[j] >= 0 && ptr[j] < 10); |
| 467 | len = BC_MAX(len, i + j + (carry != 0)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 468 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 469 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 470 | c->len = len; |
Gavin Howard | 23d8d25 | 2018-10-17 12:03:41 -0600 | [diff] [blame] | 471 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 472 | return BC_SIGNAL ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 473 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 474 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 475 | bc_num_init(&l1, max); |
| 476 | bc_num_init(&h1, max); |
| 477 | bc_num_init(&l2, max); |
| 478 | bc_num_init(&h2, max); |
| 479 | bc_num_init(&m1, max); |
| 480 | bc_num_init(&m2, max); |
| 481 | bc_num_init(&z0, max); |
| 482 | bc_num_init(&z1, max); |
| 483 | bc_num_init(&z2, max); |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 484 | bc_num_init(&temp, bc_vm_checkSize(max, max)); |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 485 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 486 | bc_num_split(a, max2, &l1, &h1); |
| 487 | bc_num_split(b, max2, &l2, &h2); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 488 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 489 | s = bc_num_add(&h1, &l1, &m1, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 490 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 491 | s = bc_num_add(&h2, &l2, &m2, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 492 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 493 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 494 | s = bc_num_k(&h1, &h2, &z0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 495 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 496 | s = bc_num_k(&m1, &m2, &z1); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 497 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 498 | s = bc_num_k(&l1, &l2, &z2); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 499 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 500 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 501 | s = bc_num_sub(&z1, &z0, &temp, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 502 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 503 | s = bc_num_sub(&temp, &z2, &z1, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 504 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 505 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 506 | bc_num_shift_l(&z0, max2 * 2); |
| 507 | bc_num_shift_l(&z1, max2); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 508 | s = bc_num_add(&z0, &z1, &temp, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 509 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | b5093d7 | 2018-10-17 12:13:17 -0600 | [diff] [blame] | 510 | s = bc_num_add(&temp, &z2, c, 0); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 511 | |
| 512 | err: |
| 513 | bc_num_free(&temp); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 514 | bc_num_free(&z2); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 515 | bc_num_free(&z1); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 516 | bc_num_free(&z0); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 517 | bc_num_free(&m2); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 518 | bc_num_free(&m1); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 519 | bc_num_free(&h2); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 520 | bc_num_free(&l2); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 521 | bc_num_free(&h1); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 522 | bc_num_free(&l1); |
| 523 | return s; |
| 524 | } |
| 525 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 526 | static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 527 | |
| 528 | BcStatus s; |
| 529 | BcNum cpa, cpb; |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 530 | size_t maxrdx = BC_MAX(a->rdx, b->rdx); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 531 | |
| 532 | scale = BC_MAX(scale, a->rdx); |
| 533 | scale = BC_MAX(scale, b->rdx); |
| 534 | scale = BC_MIN(a->rdx + b->rdx, scale); |
| 535 | maxrdx = BC_MAX(maxrdx, scale); |
| 536 | |
Gavin Howard | baa4f58 | 2019-01-24 13:56:35 -0700 | [diff] [blame] | 537 | bc_num_createCopy(&cpa, a); |
| 538 | bc_num_createCopy(&cpb, b); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 539 | |
Gavin Howard | 23d8d25 | 2018-10-17 12:03:41 -0600 | [diff] [blame] | 540 | cpa.neg = cpb.neg = false; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 541 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 542 | bc_num_shift_l(&cpa, maxrdx); |
| 543 | bc_num_shift_l(&cpb, maxrdx); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 544 | s = bc_num_k(&cpa, &cpb, c); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 545 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 546 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 547 | maxrdx += scale; |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 548 | bc_num_expand(c, bc_vm_checkSize(c->len, maxrdx)); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 549 | |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 550 | if (c->len < maxrdx) { |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 551 | memset(c->num + c->len, 0, (c->cap - c->len) * sizeof(BcDig)); |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 552 | c->len += maxrdx; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 553 | } |
| 554 | |
Gavin Howard | 88c2530 | 2018-10-17 13:32:23 -0600 | [diff] [blame] | 555 | c->rdx = maxrdx; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 556 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 557 | |
| 558 | err: |
| 559 | bc_num_free(&cpb); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 560 | bc_num_free(&cpa); |
| 561 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 564 | static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 565 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 566 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 567 | BcDig *n, *p, q; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 568 | size_t len, end, i; |
| 569 | BcNum cp; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 570 | bool zero = true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 571 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 572 | if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); |
| 573 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 574 | bc_num_setToZero(c, scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 575 | return BC_STATUS_SUCCESS; |
| 576 | } |
Gavin Howard | a83a5e5 | 2019-02-21 16:51:06 -0700 | [diff] [blame] | 577 | if (bc_num_isOne(b)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 578 | bc_num_copy(c, a); |
| 579 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 580 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 581 | } |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 582 | |
Gavin Howard | 620a8af | 2019-01-24 13:54:54 -0700 | [diff] [blame] | 583 | bc_num_init(&cp, bc_num_mulReq(a, b, scale)); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 584 | bc_num_copy(&cp, a); |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 585 | len = b->len; |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 586 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 587 | if (len > cp.len) { |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 588 | bc_num_expand(&cp, bc_vm_checkSize(len, 2)); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 589 | bc_num_extend(&cp, len - cp.len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 590 | } |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 591 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 592 | if (b->rdx > cp.rdx) bc_num_extend(&cp, b->rdx - cp.rdx); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 593 | cp.rdx -= b->rdx; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 594 | if (scale > cp.rdx) bc_num_extend(&cp, scale - cp.rdx); |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 595 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 596 | if (b->rdx == b->len) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 597 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 598 | assert(i != len || !zero); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 599 | len -= i - 1; |
| 600 | } |
Gavin Howard | c1a6a34 | 2018-03-05 12:10:14 -0700 | [diff] [blame] | 601 | |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 602 | if (cp.cap == cp.len) bc_num_expand(&cp, bc_vm_checkSize(cp.len, 1)); |
Gavin Howard | b651f1a | 2018-02-28 17:34:18 -0700 | [diff] [blame] | 603 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 604 | // We want an extra zero in front to make things simpler. |
| 605 | cp.num[cp.len++] = 0; |
| 606 | end = cp.len - len; |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 607 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 608 | bc_num_expand(c, cp.len); |
Gavin Howard | ac7656d | 2018-03-01 17:18:40 -0700 | [diff] [blame] | 609 | |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 610 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 611 | c->rdx = cp.rdx; |
| 612 | c->len = cp.len; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 613 | p = b->num; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 614 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 615 | for (i = end - 1; !BC_SIGNAL && !s && i < end; --i) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 616 | n = cp.num + i; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 617 | for (q = 0; !s && (n[len] || bc_num_compare(n, p, len) >= 0); ++q) |
| 618 | s = bc_num_subArrays(n, p, len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 619 | c->num[i] = q; |
| 620 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 621 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 622 | if (!s) bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 623 | bc_num_free(&cp); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 624 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 625 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 626 | } |
| 627 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 628 | static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, |
| 629 | BcNum *restrict d, size_t scale, size_t ts) |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 630 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 631 | BcStatus s; |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 632 | BcNum temp; |
| 633 | bool neg; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 634 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 635 | if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); |
| 636 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | 99ca499 | 2019-01-02 13:48:49 -0700 | [diff] [blame] | 637 | bc_num_setToZero(c, ts); |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 638 | bc_num_setToZero(d, ts); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 639 | return BC_STATUS_SUCCESS; |
| 640 | } |
Gavin Howard | 8b25487 | 2018-03-14 01:13:35 -0600 | [diff] [blame] | 641 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 642 | bc_num_init(&temp, d->cap); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 643 | s = bc_num_d(a, b, c, scale); |
| 644 | assert(!s || s == BC_STATUS_SIGNAL); |
| 645 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 646 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 647 | if (scale) scale = ts; |
Gavin Howard | 3115c01 | 2018-10-04 10:53:56 -0600 | [diff] [blame] | 648 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 649 | s = bc_num_m(c, b, &temp, scale); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 650 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 651 | s = bc_num_sub(a, &temp, d, scale); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 652 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 5d149cf | 2018-09-06 13:46:23 -0600 | [diff] [blame] | 653 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 654 | if (ts > d->rdx && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->rdx); |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 655 | |
| 656 | neg = d->neg; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 657 | bc_num_retireMul(d, ts, a->neg, b->neg); |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 658 | d->neg = neg; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 659 | |
| 660 | err: |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 661 | bc_num_free(&temp); |
| 662 | return s; |
| 663 | } |
| 664 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 665 | static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 666 | |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 667 | BcStatus s; |
| 668 | BcNum c1; |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 669 | size_t ts; |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 670 | |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 671 | ts = bc_vm_checkSize(scale, b->rdx); |
| 672 | ts = BC_MAX(ts, a->rdx); |
| 673 | |
| 674 | bc_num_init(&c1, bc_num_mulReq(a, b, ts)); |
Gavin Howard | 54b946a | 2018-10-23 12:06:57 -0600 | [diff] [blame] | 675 | s = bc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 676 | bc_num_free(&c1); |
Gavin Howard | d64ce7b | 2018-10-24 16:20:20 -0600 | [diff] [blame] | 677 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 678 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 681 | static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 682 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 683 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 684 | BcNum copy; |
Gavin Howard | 510859c | 2019-01-07 09:14:01 -0700 | [diff] [blame] | 685 | unsigned long pow = 0; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 686 | size_t i, powrdx, resrdx; |
| 687 | bool neg, zero; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 688 | |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 689 | if (b->rdx) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 690 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 691 | if (BC_NUM_ZERO(b)) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 692 | bc_num_one(c); |
Gavin Howard | 61abdbe | 2018-10-22 13:05:38 -0600 | [diff] [blame] | 693 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 694 | } |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 695 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 696 | bc_num_setToZero(c, scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 697 | return BC_STATUS_SUCCESS; |
| 698 | } |
Gavin Howard | a83a5e5 | 2019-02-21 16:51:06 -0700 | [diff] [blame] | 699 | if (bc_num_isOne(b)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 700 | if (!b->neg) bc_num_copy(c, a); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 701 | else s = bc_num_inv(a, c, scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 702 | return s; |
| 703 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 704 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 705 | neg = b->neg; |
| 706 | b->neg = false; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 707 | s = bc_num_ulong(b, &pow); |
Gavin Howard | fb14efc | 2018-12-22 14:01:58 -0700 | [diff] [blame] | 708 | b->neg = neg; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 709 | if (s) return s; |
| 710 | |
Gavin Howard | baa4f58 | 2019-01-24 13:56:35 -0700 | [diff] [blame] | 711 | bc_num_createCopy(©, a); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 712 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 713 | 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] | 714 | |
Gavin Howard | 5dbb48b | 2019-01-04 11:29:29 -0700 | [diff] [blame] | 715 | for (powrdx = a->rdx; !BC_SIGNAL && !(pow & 1); pow >>= 1) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 716 | powrdx <<= 1; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 717 | s = bc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 718 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 719 | } |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 720 | |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 721 | if (BC_SIGNAL) goto sig_err; |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 722 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 723 | bc_num_copy(c, ©); |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 724 | resrdx = powrdx; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 725 | |
Gavin Howard | 4d5daba | 2019-01-07 14:42:50 -0700 | [diff] [blame] | 726 | while (!BC_SIGNAL && (pow >>= 1)) { |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 727 | |
Gavin Howard | 4d5daba | 2019-01-07 14:42:50 -0700 | [diff] [blame] | 728 | powrdx <<= 1; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 729 | s = bc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 730 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 731 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 732 | if (pow & 1) { |
| 733 | resrdx += powrdx; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 734 | s = bc_num_mul(c, ©, c, resrdx); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 735 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 736 | } |
| 737 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 738 | |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 739 | if (BC_SIGNAL) goto sig_err; |
| 740 | if (neg) { |
| 741 | s = bc_num_inv(c, c, scale); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 742 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 745 | if (c->rdx > scale) bc_num_truncate(c, c->rdx - scale); |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 746 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 747 | // We can't use bc_num_clean() here. |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 748 | for (zero = true, i = 0; zero && i < c->len; ++i) zero = !c->num[i]; |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 749 | if (zero) bc_num_setToZero(c, scale); |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 750 | |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 751 | sig_err: |
| 752 | if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 753 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 754 | bc_num_free(©); |
| 755 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Gavin Howard | 7bda478 | 2018-12-28 09:53:22 -0700 | [diff] [blame] | 758 | #if BC_ENABLE_EXTRA_MATH |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 759 | static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 760 | |
| 761 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 510859c | 2019-01-07 09:14:01 -0700 | [diff] [blame] | 762 | unsigned long val = 0; |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 763 | |
| 764 | BC_UNUSED(scale); |
| 765 | |
| 766 | s = bc_num_intop(a, b, c, &val); |
| 767 | if (s) return s; |
| 768 | |
Gavin Howard | c4bf4c4 | 2019-01-09 10:30:15 -0700 | [diff] [blame] | 769 | if (val < c->rdx) bc_num_truncate(c, c->rdx - val); |
| 770 | else if (val > c->rdx) bc_num_extend(c, val - c->rdx); |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 771 | |
| 772 | return s; |
| 773 | } |
| 774 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 775 | static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 776 | |
| 777 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 510859c | 2019-01-07 09:14:01 -0700 | [diff] [blame] | 778 | unsigned long val = 0; |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 779 | |
| 780 | BC_UNUSED(scale); |
| 781 | |
| 782 | s = bc_num_intop(a, b, c, &val); |
| 783 | if (s) return s; |
| 784 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 785 | bc_num_shift_l(c, (size_t) val); |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 786 | |
| 787 | return s; |
| 788 | } |
| 789 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 790 | static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 791 | |
| 792 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 510859c | 2019-01-07 09:14:01 -0700 | [diff] [blame] | 793 | unsigned long val = 0; |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 794 | |
| 795 | BC_UNUSED(scale); |
| 796 | |
| 797 | s = bc_num_intop(a, b, c, &val); |
| 798 | if (s) return s; |
| 799 | |
Gavin Howard | 2a36692 | 2019-01-16 10:50:20 -0700 | [diff] [blame] | 800 | if (BC_NUM_ZERO(c)) return s; |
| 801 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 802 | bc_num_shift_r(c, (size_t) val); |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 803 | |
| 804 | return s; |
| 805 | } |
Gavin Howard | 7bda478 | 2018-12-28 09:53:22 -0700 | [diff] [blame] | 806 | #endif // BC_ENABLE_EXTRA_MATH |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 807 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 808 | static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 809 | BcNumBinaryOp op, size_t req) |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 810 | { |
Gavin Howard | a1c4439 | 2018-09-27 12:41:15 -0600 | [diff] [blame] | 811 | BcStatus s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 812 | BcNum num2, *ptr_a, *ptr_b; |
| 813 | bool init = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 814 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 815 | assert(a && b && c && op); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 816 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 817 | if (c == a) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 818 | ptr_a = &num2; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 819 | memcpy(ptr_a, c, sizeof(BcNum)); |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 820 | init = true; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 821 | } |
| 822 | else ptr_a = a; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 823 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 824 | if (c == b) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 825 | ptr_b = &num2; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 826 | if (c != a) { |
| 827 | memcpy(ptr_b, c, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 828 | init = true; |
| 829 | } |
| 830 | } |
| 831 | else ptr_b = b; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 832 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 833 | if (init) bc_num_init(c, req); |
| 834 | else bc_num_expand(c, req); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 835 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 836 | s = op(ptr_a, ptr_b, c, scale); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 837 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 838 | assert(!c->neg || BC_NUM_NONZERO(c)); |
Gavin Howard | c4bf4c4 | 2019-01-09 10:30:15 -0700 | [diff] [blame] | 839 | assert(c->rdx <= c->len || !c->len); |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 840 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 841 | if (init) bc_num_free(&num2); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 842 | |
Gavin Howard | ee9d01e | 2019-02-16 22:48:11 -0700 | [diff] [blame] | 843 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Gavin Howard | c90ed90 | 2019-01-02 13:07:49 -0700 | [diff] [blame] | 846 | #ifndef NDEBUG |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 847 | static bool bc_num_strValid(const char *val) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 848 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 849 | bool radix = false; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 850 | size_t i, len = strlen(val); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 851 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 852 | if (!len) return true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 853 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 854 | for (i = 0; i < len; ++i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 855 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 856 | BcDig c = val[i]; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 857 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 858 | if (c == '.') { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 859 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 860 | if (radix) return false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 861 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 862 | radix = true; |
| 863 | continue; |
| 864 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 865 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 866 | if (!(isdigit(c) || isupper(c))) return false; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 867 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 868 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 869 | return true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 870 | } |
Gavin Howard | c90ed90 | 2019-01-02 13:07:49 -0700 | [diff] [blame] | 871 | #endif // NDEBUG |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 872 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 873 | static unsigned long bc_num_parseChar(char c, size_t base_t) { |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 874 | |
| 875 | if (isupper(c)) { |
| 876 | c = BC_NUM_NUM_LETTER(c); |
Gavin Howard | 6193aaf | 2019-01-03 16:44:04 -0700 | [diff] [blame] | 877 | c = ((size_t) c) >= base_t ? (char) base_t - 1 : c; |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 878 | } |
| 879 | else c -= '0'; |
| 880 | |
| 881 | return (unsigned long) (uchar) c; |
| 882 | } |
| 883 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 884 | static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 885 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 886 | size_t len, i; |
| 887 | const char *ptr; |
| 888 | bool zero = true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 889 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 890 | for (i = 0; val[i] == '0'; ++i); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 891 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 892 | val += i; |
| 893 | len = strlen(val); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 894 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 895 | if (len) { |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 896 | for (i = 0; zero && i < len; ++i) |
| 897 | zero = (val[i] == '0') || val[i] == '.'; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 898 | bc_num_expand(n, len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 899 | } |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 900 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 901 | ptr = strchr(val, '.'); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 902 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 903 | // Explicitly test for NULL here to produce either a 0 or 1. |
| 904 | n->rdx = (size_t) ((ptr != NULL) * ((val + len) - (ptr + 1))); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 905 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 906 | if (!zero) { |
Gavin Howard | 8dd307e | 2019-01-08 23:05:19 -0700 | [diff] [blame] | 907 | for (i = len - 1; i < len; ++n->len, --i) { |
| 908 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 909 | char c = val[i]; |
Gavin Howard | 8dd307e | 2019-01-08 23:05:19 -0700 | [diff] [blame] | 910 | |
| 911 | if (c == '.') n->len -= 1; |
| 912 | else { |
| 913 | if (isupper(c)) c = '9'; |
| 914 | n->num[n->len] = c - '0'; |
| 915 | } |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 916 | } |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 917 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 918 | } |
| 919 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 920 | static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val, |
| 921 | BcNum *restrict base, size_t base_t) |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 922 | { |
| 923 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 924 | BcNum temp, mult, result; |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 925 | BcDig c = 0; |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 926 | bool zero = true; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 927 | unsigned long v; |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 928 | size_t i, digs, len = strlen(val); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 929 | |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 930 | for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0'); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 931 | if (zero) return BC_STATUS_SUCCESS; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 932 | |
Gavin Howard | 26d078f | 2019-01-24 13:58:12 -0700 | [diff] [blame] | 933 | bc_num_init(&temp, BC_NUM_LONG_LOG10); |
| 934 | bc_num_init(&mult, BC_NUM_LONG_LOG10); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 935 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 936 | for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) { |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 937 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 938 | v = bc_num_parseChar(c, base_t); |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 939 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 940 | s = bc_num_mul(n, base, &mult, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 941 | if (BC_STATUS_SIGNAL_ONLY(s)) goto int_err; |
Gavin Howard | 705c4bc | 2018-12-03 15:38:04 -0700 | [diff] [blame] | 942 | bc_num_ulong2num(&temp, v); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 943 | s = bc_num_add(&mult, &temp, n, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 944 | if (BC_STATUS_SIGNAL_ONLY(s)) goto int_err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 945 | } |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 946 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 947 | if (i == len && !(c = val[i])) goto int_err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 948 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 949 | assert(c == '.'); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 950 | bc_num_init(&result, base->len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 951 | bc_num_one(&mult); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 952 | |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 953 | for (i += 1, digs = 0; !BC_SIGNAL && i < len && (c = val[i]); ++i, ++digs) { |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 954 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 955 | v = bc_num_parseChar(c, base_t); |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 956 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 957 | s = bc_num_mul(&result, base, &result, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 958 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 959 | |
Gavin Howard | 705c4bc | 2018-12-03 15:38:04 -0700 | [diff] [blame] | 960 | bc_num_ulong2num(&temp, v); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 961 | s = bc_num_add(&result, &temp, &result, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 962 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 963 | s = bc_num_mul(&mult, base, &mult, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 964 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 965 | } |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 966 | |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 967 | // This one cannot be a divide by 0 because mult starts out at 1, then is |
| 968 | // multiplied by base, and base cannot be 0, so mult cannot be 0. |
| 969 | s = bc_num_div(&result, &mult, &result, digs); |
| 970 | assert(!s || s == BC_STATUS_SIGNAL); |
| 971 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
| 972 | s = bc_num_add(n, &result, n, digs); |
| 973 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | d141768 | 2019-02-15 17:08:42 -0700 | [diff] [blame] | 974 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 975 | if (BC_NUM_NONZERO(n)) { |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 976 | if (n->rdx < digs) bc_num_extend(n, digs - n->rdx); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 977 | } |
| 978 | else bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 979 | |
Gavin Howard | d141768 | 2019-02-15 17:08:42 -0700 | [diff] [blame] | 980 | err: |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 981 | bc_num_free(&result); |
| 982 | int_err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 983 | bc_num_free(&mult); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 984 | bc_num_free(&temp); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 985 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 986 | } |
| 987 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 988 | static void bc_num_printNewline() { |
Gavin Howard | 6193aaf | 2019-01-03 16:44:04 -0700 | [diff] [blame] | 989 | if (vm->nchars >= (size_t) (vm->line_len - 1)) { |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 990 | bc_vm_putchar('\\'); |
| 991 | bc_vm_putchar('\n'); |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 992 | vm->nchars = 0; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 993 | } |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 994 | } |
| 995 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 996 | #if DC_ENABLED |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 997 | static void bc_num_printChar(size_t n, size_t len, bool rdx) { |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 998 | BC_UNUSED(rdx); |
| 999 | bc_vm_putchar((uchar) n); |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1000 | vm->nchars += len; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1001 | } |
| 1002 | #endif // DC_ENABLED |
| 1003 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1004 | static void bc_num_printDigits(size_t n, size_t len, bool rdx) { |
| 1005 | |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 1006 | size_t exp, pow; |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 1007 | |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1008 | bc_num_printNewline(); |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1009 | bc_vm_putchar(rdx ? '.' : ' '); |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1010 | ++vm->nchars; |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 1011 | |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1012 | bc_num_printNewline(); |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1013 | for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= 10); |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 1014 | |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1015 | for (exp = 0; exp < len; pow /= 10, ++vm->nchars, ++exp) { |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 1016 | size_t dig; |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1017 | bc_num_printNewline(); |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1018 | dig = n / pow; |
| 1019 | n -= dig * pow; |
Gavin Howard | 954f9b6 | 2018-12-19 15:22:20 -0700 | [diff] [blame] | 1020 | bc_vm_putchar(((uchar) dig) + '0'); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1021 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1022 | } |
Gavin Howard | fe679f0 | 2018-02-14 15:50:09 -0700 | [diff] [blame] | 1023 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1024 | static void bc_num_printHex(size_t n, size_t len, bool rdx) { |
Gavin Howard | 70e3d60 | 2018-12-11 11:00:49 -0700 | [diff] [blame] | 1025 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1026 | assert(len == 1); |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 1027 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1028 | if (rdx) { |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1029 | bc_num_printNewline(); |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 1030 | bc_vm_putchar('.'); |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1031 | vm->nchars += 1; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1032 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1033 | |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1034 | bc_num_printNewline(); |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1035 | bc_vm_putchar(bc_num_hex_digits[n]); |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1036 | vm->nchars += len; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1039 | static void bc_num_printDecimal(const BcNum *restrict n) { |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 1040 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1041 | size_t i, rdx = n->rdx - 1; |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 1042 | |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 1043 | if (n->neg) bc_vm_putchar('-'); |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1044 | vm->nchars += n->neg; |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 1045 | |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 1046 | for (i = n->len - 1; i < n->len; --i) |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1047 | bc_num_printHex((size_t) n->num[i], 1, i == rdx); |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1050 | #if BC_ENABLE_EXTRA_MATH |
| 1051 | static void bc_num_printExponent(const BcNum *restrict n, bool eng) { |
| 1052 | |
| 1053 | bool neg = (n->len <= n->rdx); |
| 1054 | BcNum temp, exp; |
| 1055 | size_t places, mod; |
| 1056 | BcDig digs[BC_NUM_LONG_LOG10]; |
| 1057 | |
| 1058 | bc_num_createCopy(&temp, n); |
| 1059 | |
| 1060 | if (neg) { |
| 1061 | places = n->rdx - bc_num_len(n) + 1; |
| 1062 | mod = places % 3; |
| 1063 | if (eng && mod != 0) places += 3 - mod; |
| 1064 | bc_num_shift_l(&temp, places); |
| 1065 | } |
| 1066 | else { |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 1067 | places = bc_num_int(n) - 1; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1068 | mod = places % 3; |
| 1069 | if (eng && mod != 0) places -= 3 - (3 - mod); |
| 1070 | bc_num_shift_r(&temp, places); |
| 1071 | } |
| 1072 | |
| 1073 | bc_num_printDecimal(&temp); |
| 1074 | |
| 1075 | bc_num_printNewline(); |
| 1076 | bc_vm_putchar('e'); |
| 1077 | |
| 1078 | if (!places) { |
| 1079 | bc_num_printHex(0, 1, false); |
| 1080 | goto exit; |
| 1081 | } |
| 1082 | |
| 1083 | if (neg) { |
| 1084 | bc_num_printNewline(); |
| 1085 | bc_vm_putchar('-'); |
| 1086 | } |
| 1087 | |
| 1088 | bc_num_setup(&exp, digs, BC_NUM_LONG_LOG10); |
| 1089 | bc_num_ulong2num(&exp, (unsigned long) places); |
| 1090 | |
| 1091 | bc_num_printDecimal(&exp); |
| 1092 | |
| 1093 | exit: |
| 1094 | bc_num_free(&temp); |
| 1095 | } |
| 1096 | #endif // BC_ENABLE_EXTRA_MATH |
| 1097 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1098 | static BcStatus bc_num_printNum(BcNum *restrict n, BcNum *restrict base, |
| 1099 | size_t len, BcNumDigitOp print) |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 1100 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1101 | BcStatus s; |
| 1102 | BcVec stack; |
| 1103 | BcNum intp, fracp, digit, frac_len; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1104 | unsigned long dig, *ptr; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1105 | size_t i; |
| 1106 | bool radix; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1107 | |
Gavin Howard | 3fb24fa | 2019-02-15 17:18:57 -0700 | [diff] [blame] | 1108 | assert(!BC_NUM_ZERO(base)); |
| 1109 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1110 | if (BC_NUM_ZERO(n)) { |
| 1111 | print(0, len, false); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1112 | return BC_STATUS_SUCCESS; |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 1113 | } |
Gavin Howard | 9a4b6cd | 2018-10-23 15:13:30 -0600 | [diff] [blame] | 1114 | |
Gavin Howard | d392983 | 2018-12-24 15:13:15 -0700 | [diff] [blame] | 1115 | bc_vec_init(&stack, sizeof(unsigned long), NULL); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1116 | bc_num_init(&fracp, n->rdx); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1117 | bc_num_init(&digit, len); |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 1118 | bc_num_init(&frac_len, bc_num_int(n)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1119 | bc_num_one(&frac_len); |
Gavin Howard | baa4f58 | 2019-01-24 13:56:35 -0700 | [diff] [blame] | 1120 | bc_num_createCopy(&intp, n); |
Gavin Howard | a50fc54 | 2018-03-29 17:25:38 -0600 | [diff] [blame] | 1121 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1122 | bc_num_truncate(&intp, intp.rdx); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1123 | s = bc_num_sub(n, &intp, &fracp, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1124 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1125 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1126 | while (!BC_SIGNAL && BC_NUM_NONZERO(&intp)) { |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1127 | |
| 1128 | // Dividing by base cannot be divide by 0 because base cannot be 0. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1129 | s = bc_num_divmod(&intp, base, &intp, &digit, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1130 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
| 1131 | |
| 1132 | // Will never fail (except for signals) because digit is |
| 1133 | // guaranteed to be non-negative and small enough. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1134 | s = bc_num_ulong(&digit, &dig); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1135 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
| 1136 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1137 | bc_vec_push(&stack, &dig); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1138 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1139 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1140 | if (BC_SIGNAL) goto sig_err; |
| 1141 | |
| 1142 | for (i = 0; !BC_SIGNAL && i < stack.len; ++i) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1143 | ptr = bc_vec_item_rev(&stack, i); |
| 1144 | assert(ptr); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1145 | print(*ptr, len, false); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1146 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1147 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1148 | if (BC_SIGNAL) goto sig_err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1149 | if (!n->rdx) goto err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1150 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1151 | for (radix = true; !BC_SIGNAL && frac_len.len <= n->rdx; radix = false) { |
| 1152 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1153 | s = bc_num_mul(&fracp, base, &fracp, n->rdx); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1154 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
| 1155 | |
| 1156 | // Will never fail (except for signals) because fracp is |
| 1157 | // guaranteed to be non-negative and small enough. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1158 | s = bc_num_ulong(&fracp, &dig); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1159 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1160 | |
Gavin Howard | 705c4bc | 2018-12-03 15:38:04 -0700 | [diff] [blame] | 1161 | bc_num_ulong2num(&intp, dig); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1162 | s = bc_num_sub(&fracp, &intp, &fracp, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1163 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1164 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1165 | print(dig, len, radix); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1166 | s = bc_num_mul(&frac_len, base, &frac_len, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1167 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1168 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1169 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1170 | sig_err: |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 1171 | if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1172 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1173 | bc_num_free(&frac_len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1174 | bc_num_free(&digit); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1175 | bc_num_free(&fracp); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1176 | bc_num_free(&intp); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1177 | bc_vec_free(&stack); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1178 | return s; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1181 | static BcStatus bc_num_printBase(BcNum *restrict n, BcNum *restrict base, |
| 1182 | size_t base_t) |
Gavin Howard | 3fb24fa | 2019-02-15 17:18:57 -0700 | [diff] [blame] | 1183 | { |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1184 | BcStatus s; |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 1185 | size_t width; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1186 | BcNumDigitOp print; |
| 1187 | bool neg = n->neg; |
| 1188 | |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 1189 | if (neg) bc_vm_putchar('-'); |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1190 | vm->nchars += neg; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1191 | |
| 1192 | n->neg = false; |
| 1193 | |
Gavin Howard | be4e822 | 2019-01-03 13:34:16 -0700 | [diff] [blame] | 1194 | if (base_t <= BC_NUM_MAX_POSIX_IBASE) { |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1195 | width = 1; |
| 1196 | print = bc_num_printHex; |
| 1197 | } |
| 1198 | else { |
Gavin Howard | be4e822 | 2019-01-03 13:34:16 -0700 | [diff] [blame] | 1199 | width = bc_num_log10(base_t - 1) - 1; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1200 | print = bc_num_printDigits; |
| 1201 | } |
| 1202 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1203 | s = bc_num_printNum(n, base, width, print); |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1204 | n->neg = neg; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1205 | |
| 1206 | return s; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1207 | } |
| 1208 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 1209 | #if DC_ENABLED |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1210 | BcStatus bc_num_stream(BcNum *restrict n, BcNum *restrict base) { |
| 1211 | return bc_num_printNum(n, base, 1, bc_num_printChar); |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1212 | } |
| 1213 | #endif // DC_ENABLED |
| 1214 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1215 | void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) { |
Gavin Howard | b8a1292 | 2018-12-21 21:40:45 -0700 | [diff] [blame] | 1216 | assert(n); |
| 1217 | n->num = num; |
| 1218 | n->cap = cap; |
| 1219 | n->rdx = n->len = 0; |
| 1220 | n->neg = false; |
| 1221 | } |
| 1222 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1223 | void bc_num_init(BcNum *restrict n, size_t req) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1224 | assert(n); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1225 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Gavin Howard | b8a1292 | 2018-12-21 21:40:45 -0700 | [diff] [blame] | 1226 | bc_num_setup(n, bc_vm_malloc(req), req); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1227 | } |
| 1228 | |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 1229 | void bc_num_free(void *num) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1230 | assert(num); |
| 1231 | free(((BcNum*) num)->num); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1234 | void bc_num_copy(BcNum *d, const BcNum *s) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1235 | assert(d && s); |
Gavin Howard | 7344ba7 | 2019-01-03 13:37:27 -0700 | [diff] [blame] | 1236 | if (d == s) return; |
Gavin Howard | dcff3c9 | 2019-01-09 10:04:56 -0700 | [diff] [blame] | 1237 | bc_num_expand(d, s->len); |
Gavin Howard | 7344ba7 | 2019-01-03 13:37:27 -0700 | [diff] [blame] | 1238 | d->len = s->len; |
| 1239 | d->neg = s->neg; |
| 1240 | d->rdx = s->rdx; |
| 1241 | memcpy(d->num, s->num, sizeof(BcDig) * d->len); |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1242 | } |
| 1243 | |
Gavin Howard | baa4f58 | 2019-01-24 13:56:35 -0700 | [diff] [blame] | 1244 | void bc_num_createCopy(BcNum *d, const BcNum *s) { |
| 1245 | bc_num_init(d, s->len); |
| 1246 | bc_num_copy(d, s); |
| 1247 | } |
| 1248 | |
| 1249 | void bc_num_createFromUlong(BcNum *n, unsigned long val) { |
| 1250 | bc_num_init(n, BC_NUM_LONG_LOG10); |
| 1251 | bc_num_ulong2num(n, val); |
| 1252 | } |
| 1253 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1254 | size_t bc_num_scale(const BcNum *restrict n) { |
| 1255 | return n->rdx; |
| 1256 | } |
| 1257 | |
| 1258 | size_t bc_num_len(const BcNum *restrict n) { |
| 1259 | |
| 1260 | size_t i, len = n->len; |
| 1261 | |
| 1262 | if (n->rdx != n->len) return len; |
| 1263 | for (i = n->len - 1; i < n->len && !n->num[i]; --len, --i); |
| 1264 | |
| 1265 | return len; |
| 1266 | } |
| 1267 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1268 | BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val, |
| 1269 | BcNum *restrict base, size_t base_t, bool letter) |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1270 | { |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1271 | BcStatus s = BC_STATUS_SUCCESS; |
| 1272 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1273 | assert(n && val && base); |
Gavin Howard | 96edeb3 | 2018-12-31 09:33:22 -0700 | [diff] [blame] | 1274 | assert(base_t >= BC_NUM_MIN_BASE && base_t <= vm->max_ibase); |
Gavin Howard | c90ed90 | 2019-01-02 13:07:49 -0700 | [diff] [blame] | 1275 | assert(bc_num_strValid(val)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1276 | |
Gavin Howard | d746e80 | 2018-12-28 22:06:00 -0700 | [diff] [blame] | 1277 | if (letter) bc_num_ulong2num(n, bc_num_parseChar(val[0], BC_NUM_MAX_LBASE)); |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 1278 | else if (base_t == 10) bc_num_parseDecimal(n, val); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1279 | else s = bc_num_parseBase(n, val, base, base_t); |
| 1280 | |
| 1281 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1284 | BcStatus bc_num_print(BcNum *restrict n, BcNum *restrict base, |
| 1285 | size_t base_t, bool newline) |
Gavin Howard | 152f3e8 | 2018-03-07 12:33:15 -0700 | [diff] [blame] | 1286 | { |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1287 | BcStatus s = BC_STATUS_SUCCESS; |
| 1288 | |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1289 | assert(n && base); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1290 | assert(BC_ENABLE_EXTRA_MATH || base_t >= BC_NUM_MIN_BASE); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1291 | |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1292 | bc_num_printNewline(); |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 1293 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1294 | if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false); |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1295 | else if (base_t == 10) bc_num_printDecimal(n); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1296 | #if BC_ENABLE_EXTRA_MATH |
| 1297 | else if (base_t == 0 || base_t == 1) bc_num_printExponent(n, base_t != 0); |
| 1298 | #endif // BC_ENABLE_EXTRA_MATH |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1299 | else s = bc_num_printBase(n, base, base_t); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1300 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1301 | if (!s && newline) { |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 1302 | bc_vm_putchar('\n'); |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1303 | vm->nchars = 0; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1304 | } |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1305 | |
| 1306 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1309 | BcStatus bc_num_ulong(const BcNum *restrict n, unsigned long *result) { |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1310 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1311 | size_t i; |
Gavin Howard | 06b6e86 | 2019-02-15 11:15:31 -0700 | [diff] [blame] | 1312 | unsigned long r; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1313 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1314 | assert(n && result); |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1315 | |
Gavin Howard | fdf64bc | 2019-01-07 16:52:54 -0700 | [diff] [blame] | 1316 | *result = 0; |
| 1317 | |
Gavin Howard | 7536dcf | 2018-12-15 19:27:09 -0700 | [diff] [blame] | 1318 | if (n->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE); |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1319 | |
Gavin Howard | 06b6e86 | 2019-02-15 11:15:31 -0700 | [diff] [blame] | 1320 | for (r = 0, i = n->len; i > n->rdx;) { |
| 1321 | unsigned long prev = r; |
| 1322 | r = r * 10 + ((uchar) n->num[--i]); |
Gavin Howard | 5edf849 | 2019-02-21 14:06:58 -0700 | [diff] [blame] | 1323 | if (r == SIZE_MAX || (r / 8) < prev) |
| 1324 | return bc_vm_err(BC_ERROR_MATH_OVERFLOW); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1325 | } |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1326 | |
Gavin Howard | 7b557da | 2018-12-21 10:25:32 -0700 | [diff] [blame] | 1327 | *result = r; |
Gavin Howard | d3a1c39 | 2018-12-11 12:00:57 -0700 | [diff] [blame] | 1328 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1329 | return BC_STATUS_SUCCESS; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1332 | void bc_num_ulong2num(BcNum *restrict n, unsigned long val) { |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1333 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 1334 | BcDig *ptr; |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1335 | unsigned long i; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1336 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1337 | assert(n); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1338 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1339 | bc_num_zero(n); |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1340 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 1341 | if (!val) return; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1342 | |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 1343 | bc_num_expand(n, bc_num_log10(ULONG_MAX)); |
Gavin Howard | ec82bdc | 2018-10-08 23:50:57 -0600 | [diff] [blame] | 1344 | for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= 10) ptr[i] = val % 10; |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1345 | } |
| 1346 | |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1347 | size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) { |
Gavin Howard | 1973d61 | 2019-02-21 15:37:32 -0700 | [diff] [blame] | 1348 | |
| 1349 | size_t aint, bint, ardx, brdx; |
| 1350 | |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1351 | BC_UNUSED(scale); |
Gavin Howard | 1973d61 | 2019-02-21 15:37:32 -0700 | [diff] [blame] | 1352 | |
| 1353 | ardx = a->rdx; |
| 1354 | brdx = b->rdx; |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 1355 | aint = bc_num_int(a); |
| 1356 | bint = bc_num_int(b); |
Gavin Howard | 1973d61 | 2019-02-21 15:37:32 -0700 | [diff] [blame] | 1357 | |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 1358 | return bc_vm_checkSize3(BC_MAX(ardx, brdx), BC_MAX(aint, bint), 1); |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) { |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 1362 | size_t rdx = bc_vm_checkSize(a->rdx, b->rdx); |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 1363 | rdx = bc_vm_checkSize3(bc_num_int(a), bc_num_int(b), BC_MAX(scale, rdx)); |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 1364 | return rdx + 1; |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) { |
| 1368 | BC_UNUSED(scale); |
| 1369 | return BC_NUM_PREQ(a, b); |
| 1370 | } |
| 1371 | |
| 1372 | #if BC_ENABLE_EXTRA_MATH |
| 1373 | size_t bc_num_shiftReq(BcNum *a, BcNum *b, size_t scale) { |
| 1374 | BC_UNUSED(b); |
| 1375 | BC_UNUSED(scale); |
| 1376 | return BC_NUM_SHREQ(a); |
| 1377 | } |
| 1378 | #endif // BC_ENABLE_EXTRA_MATH |
| 1379 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1380 | BcStatus bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 54b946a | 2018-10-23 12:06:57 -0600 | [diff] [blame] | 1381 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s; |
Gavin Howard | 4185296 | 2018-12-27 17:15:00 -0700 | [diff] [blame] | 1382 | BC_UNUSED(scale); |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1383 | return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1384 | } |
| 1385 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1386 | BcStatus bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 54b946a | 2018-10-23 12:06:57 -0600 | [diff] [blame] | 1387 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a; |
Gavin Howard | 4185296 | 2018-12-27 17:15:00 -0700 | [diff] [blame] | 1388 | BC_UNUSED(scale); |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1389 | return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1392 | BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1393 | return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1394 | } |
| 1395 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1396 | BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1397 | return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Gavin Howard | 54b946a | 2018-10-23 12:06:57 -0600 | [diff] [blame] | 1400 | BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1401 | return bc_num_binary(a, b, c, scale, bc_num_rem, bc_num_mulReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1402 | } |
| 1403 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 1404 | BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1405 | return bc_num_binary(a, b, c, scale, bc_num_p, BC_NUM_PREQ(a, b)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
Gavin Howard | 7d8f038 | 2018-12-27 16:15:13 -0700 | [diff] [blame] | 1408 | #if BC_ENABLE_EXTRA_MATH |
| 1409 | BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1410 | return bc_num_binary(a, b, c, scale, bc_num_place, BC_NUM_SHREQ(a)); |
Gavin Howard | 7d8f038 | 2018-12-27 16:15:13 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1414 | return bc_num_binary(a, b, c, scale, bc_num_left, BC_NUM_SHREQ(a)); |
Gavin Howard | 7d8f038 | 2018-12-27 16:15:13 -0700 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1418 | return bc_num_binary(a, b, c, scale, bc_num_right, BC_NUM_SHREQ(a)); |
Gavin Howard | 7d8f038 | 2018-12-27 16:15:13 -0700 | [diff] [blame] | 1419 | } |
| 1420 | #endif // BC_ENABLE_EXTRA_MATH |
| 1421 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1422 | BcStatus bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1423 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1424 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 1425 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
Gavin Howard | 283c4eb | 2019-01-24 16:36:46 -0700 | [diff] [blame] | 1426 | size_t pow, len, digs, digs1, resrdx, times = 0; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1427 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
Gavin Howard | 18f4020 | 2018-12-29 21:30:37 -0700 | [diff] [blame] | 1428 | BcDig half_digs[2]; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1429 | |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 1430 | assert(a && b && a != b); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1431 | |
Gavin Howard | 07fbf01 | 2019-02-19 09:25:11 -0700 | [diff] [blame] | 1432 | if (a->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE); |
| 1433 | |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 1434 | len = bc_vm_checkSize(bc_num_int(a), 1); |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 1435 | bc_num_init(b, bc_vm_checkSize3(BC_MAX(scale, a->rdx), len >> 1, 1)); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1436 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1437 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 1438 | bc_num_setToZero(b, scale); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1439 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1440 | } |
Gavin Howard | a83a5e5 | 2019-02-21 16:51:06 -0700 | [diff] [blame] | 1441 | if (bc_num_isOne(a)) { |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 1442 | bc_num_one(b); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1443 | bc_num_extend(b, scale); |
| 1444 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1445 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1446 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 1447 | scale = BC_MAX(scale, a->rdx) + 1; |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 1448 | len = bc_vm_checkSize(a->len, scale); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1449 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1450 | bc_num_init(&num1, len); |
| 1451 | bc_num_init(&num2, len); |
Gavin Howard | 18f4020 | 2018-12-29 21:30:37 -0700 | [diff] [blame] | 1452 | bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig)); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1453 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1454 | bc_num_one(&half); |
| 1455 | half.num[0] = 5; |
| 1456 | half.rdx = 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1457 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1458 | bc_num_init(&f, len); |
| 1459 | bc_num_init(&fprime, len); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1460 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1461 | x0 = &num1; |
| 1462 | x1 = &num2; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1463 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1464 | bc_num_one(x0); |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 1465 | pow = bc_num_int(a); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1466 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1467 | if (pow) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1468 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1469 | if (pow & 1) x0->num[0] = 2; |
| 1470 | else x0->num[0] = 6; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1471 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 1472 | pow -= 2 - (pow & 1); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1473 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1474 | bc_num_extend(x0, pow); |
Gavin Howard | ed742ce | 2018-08-29 14:23:12 -0600 | [diff] [blame] | 1475 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1476 | // Make sure to move the radix back. |
| 1477 | x0->rdx -= pow; |
| 1478 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1479 | |
Gavin Howard | b5ec7f3 | 2018-10-29 12:32:39 -0600 | [diff] [blame] | 1480 | x0->rdx = digs = digs1 = 0; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1481 | resrdx = scale + 2; |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 1482 | len = bc_num_int(x0) + resrdx - 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1483 | |
Gavin Howard | 5dbb48b | 2019-01-04 11:29:29 -0700 | [diff] [blame] | 1484 | while (!BC_SIGNAL && (cmp || digs < len)) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1485 | |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1486 | assert(!BC_NUM_ZERO(x0)); |
| 1487 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1488 | s = bc_num_div(a, x0, &f, resrdx); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1489 | assert(!s || s == BC_STATUS_SIGNAL); |
| 1490 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1491 | s = bc_num_add(x0, &f, &fprime, resrdx); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1492 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1493 | s = bc_num_mul(&fprime, &half, x1, resrdx); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1494 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1495 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1496 | cmp = bc_num_cmp(x1, x0); |
| 1497 | digs = x1->len - (unsigned long long) llabs(cmp); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1498 | |
Gavin Howard | b5ec7f3 | 2018-10-29 12:32:39 -0600 | [diff] [blame] | 1499 | if (cmp == cmp2 && digs == digs1) times += 1; |
Gavin Howard | b11e9e2 | 2018-10-06 17:26:02 -0600 | [diff] [blame] | 1500 | else times = 0; |
| 1501 | |
| 1502 | resrdx += times > 4; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1503 | |
| 1504 | cmp2 = cmp1; |
| 1505 | cmp1 = cmp; |
Gavin Howard | b5ec7f3 | 2018-10-29 12:32:39 -0600 | [diff] [blame] | 1506 | digs1 = digs; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 1507 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1508 | temp = x0; |
| 1509 | x0 = x1; |
| 1510 | x1 = temp; |
| 1511 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1512 | |
Gavin Howard | 176cfe6 | 2019-02-16 23:40:13 -0700 | [diff] [blame] | 1513 | if (BC_SIGNAL) { |
| 1514 | s = BC_STATUS_SIGNAL; |
| 1515 | goto err; |
| 1516 | } |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 1517 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1518 | bc_num_copy(b, x0); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1519 | scale -= 1; |
| 1520 | if (b->rdx > scale) bc_num_truncate(b, b->rdx - scale); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 1521 | |
| 1522 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1523 | bc_num_free(&fprime); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1524 | bc_num_free(&f); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1525 | bc_num_free(&num2); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1526 | bc_num_free(&num1); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1527 | assert(!b->neg || BC_NUM_NONZERO(b)); |
Gavin Howard | c4bf4c4 | 2019-01-09 10:30:15 -0700 | [diff] [blame] | 1528 | assert(b->rdx <= b->len || !b->len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1529 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1530 | } |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1531 | |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1532 | BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) { |
| 1533 | |
| 1534 | BcStatus s; |
| 1535 | BcNum num2, *ptr_a; |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 1536 | bool init = false; |
Gavin Howard | 620a8af | 2019-01-24 13:54:54 -0700 | [diff] [blame] | 1537 | size_t ts = BC_MAX(scale + b->rdx, a->rdx), len = bc_num_mulReq(a, b, ts); |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1538 | |
Gavin Howard | f2ef2f3 | 2019-01-16 11:25:16 -0700 | [diff] [blame] | 1539 | assert(c != d && a != d && b != d && b != c); |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1540 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 1541 | if (c == a) { |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1542 | memcpy(&num2, c, sizeof(BcNum)); |
| 1543 | ptr_a = &num2; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1544 | bc_num_init(c, len); |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 1545 | init = true; |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1546 | } |
Gavin Howard | f679ad8 | 2018-10-29 12:26:30 -0600 | [diff] [blame] | 1547 | else { |
| 1548 | ptr_a = a; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1549 | bc_num_expand(c, len); |
Gavin Howard | f679ad8 | 2018-10-29 12:26:30 -0600 | [diff] [blame] | 1550 | } |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1551 | |
Gavin Howard | 54b946a | 2018-10-23 12:06:57 -0600 | [diff] [blame] | 1552 | s = bc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1553 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1554 | assert(!c->neg || BC_NUM_NONZERO(c)); |
Gavin Howard | c4bf4c4 | 2019-01-09 10:30:15 -0700 | [diff] [blame] | 1555 | assert(c->rdx <= c->len || !c->len); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1556 | assert(!d->neg || BC_NUM_NONZERO(d)); |
Gavin Howard | c4bf4c4 | 2019-01-09 10:30:15 -0700 | [diff] [blame] | 1557 | assert(d->rdx <= d->len || !d->len); |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 1558 | |
| 1559 | if (init) bc_num_free(&num2); |
| 1560 | |
| 1561 | return s; |
| 1562 | } |
| 1563 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 1564 | #if DC_ENABLED |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 1565 | BcStatus bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1566 | |
| 1567 | BcStatus s; |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 1568 | BcNum base, exp, two, temp; |
Gavin Howard | d392983 | 2018-12-24 15:13:15 -0700 | [diff] [blame] | 1569 | BcDig two_digs[2]; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1570 | |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 1571 | assert(a && b && c && d && a != d && b != d && c != d); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1572 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1573 | if (BC_NUM_ZERO(c)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); |
Gavin Howard | 7536dcf | 2018-12-15 19:27:09 -0700 | [diff] [blame] | 1574 | if (b->neg) return bc_vm_err(BC_ERROR_MATH_NEGATIVE); |
| 1575 | if (a->rdx || b->rdx || c->rdx) |
| 1576 | return bc_vm_err(BC_ERROR_MATH_NON_INTEGER); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1577 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1578 | bc_num_expand(d, c->len); |
| 1579 | bc_num_init(&base, c->len); |
Gavin Howard | 7fdc30a | 2018-12-29 21:31:21 -0700 | [diff] [blame] | 1580 | bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig)); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1581 | bc_num_init(&temp, b->len); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1582 | |
| 1583 | bc_num_one(&two); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1584 | two.num[0] = 2; |
| 1585 | bc_num_one(d); |
| 1586 | |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1587 | // We already checked for 0. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1588 | s = bc_num_rem(a, c, &base, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1589 | assert(!s || s == BC_STATUS_SIGNAL); |
Gavin Howard | d88164d | 2019-02-21 09:57:31 -0700 | [diff] [blame] | 1590 | if (BC_STATUS_SIGNAL_ONLY(s)) goto rem_err; |
Gavin Howard | baa4f58 | 2019-01-24 13:56:35 -0700 | [diff] [blame] | 1591 | bc_num_createCopy(&exp, b); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1592 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1593 | while (!BC_SIGNAL && BC_NUM_NONZERO(&exp)) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1594 | |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1595 | // Num two cannot be 0, so no errors. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1596 | s = bc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1597 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1598 | |
Gavin Howard | a83a5e5 | 2019-02-21 16:51:06 -0700 | [diff] [blame] | 1599 | if (bc_num_isOne(&temp)) { |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1600 | s = bc_num_mul(d, &base, &temp, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1601 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
| 1602 | |
| 1603 | // We already checked for 0. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1604 | s = bc_num_rem(&temp, c, d, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1605 | assert(!s || s == BC_STATUS_SIGNAL); |
| 1606 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1607 | } |
| 1608 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1609 | s = bc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1610 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
| 1611 | |
| 1612 | // We already checked for 0. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1613 | s = bc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1614 | assert(!s || s == BC_STATUS_SIGNAL); |
| 1615 | if (BC_STATUS_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1616 | } |
| 1617 | |
Gavin Howard | fa4983a | 2019-02-16 23:43:03 -0700 | [diff] [blame] | 1618 | if (!s && BC_SIGNAL) s = BC_STATUS_SIGNAL; |
| 1619 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1620 | err: |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1621 | bc_num_free(&exp); |
Gavin Howard | d88164d | 2019-02-21 09:57:31 -0700 | [diff] [blame] | 1622 | rem_err: |
| 1623 | bc_num_free(&temp); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1624 | bc_num_free(&base); |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 1625 | assert(!d->neg || d->len); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1626 | return s; |
| 1627 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 1628 | #endif // DC_ENABLED |