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