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 | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 4 | * Copyright (c) 2018-2019 Gavin D. Howard and contributors. |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 5 | * |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 6 | * All rights reserved. |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 7 | * |
Gavin Howard | 7345cb9 | 2019-04-08 14:13:43 -0600 | [diff] [blame] | 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright notice, this |
| 12 | * list of conditions and the following disclaimer. |
| 13 | * |
| 14 | * * Redistributions in binary form must reproduce the above copyright notice, |
| 15 | * this list of conditions and the following disclaimer in the documentation |
| 16 | * and/or other materials provided with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | * POSSIBILITY OF SUCH DAMAGE. |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 29 | * |
Gavin Howard | b5904bf | 2018-02-20 13:28:18 -0700 | [diff] [blame] | 30 | * ***************************************************************************** |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 31 | * |
| 32 | * Code for the number type. |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | #include <assert.h> |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 37 | #include <ctype.h> |
Gavin Howard | 411f732 | 2018-09-26 17:21:19 -0600 | [diff] [blame] | 38 | #include <stdbool.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 41 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 42 | #include <limits.h> |
| 43 | |
Gavin Howard | 2949306 | 2018-03-20 19:57:37 -0600 | [diff] [blame] | 44 | #include <status.h> |
Gavin Howard | 3ba6c8d | 2018-02-15 12:23:35 -0700 | [diff] [blame] | 45 | #include <num.h> |
Gavin Howard | d555167 | 2018-09-22 19:52:42 -0600 | [diff] [blame] | 46 | #include <vm.h> |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 47 | |
Gavin Howard | 49ea7b6 | 2019-04-23 14:46:26 -0600 | [diff] [blame] | 48 | static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale); |
| 49 | |
Gavin Howard | 56ebcf7 | 2019-02-21 16:59:48 -0700 | [diff] [blame] | 50 | static ssize_t bc_num_neg(size_t n, bool neg) { |
| 51 | return (((ssize_t) n) ^ -((ssize_t) neg)) + neg; |
| 52 | } |
| 53 | |
Gavin Howard | d2cf06a | 2019-02-21 17:03:24 -0700 | [diff] [blame] | 54 | ssize_t bc_num_cmpZero(const BcNum *n) { |
Gavin Howard | 56ebcf7 | 2019-02-21 16:59:48 -0700 | [diff] [blame] | 55 | return bc_num_neg((n)->len != 0, (n)->neg); |
Gavin Howard | 9d91b2c | 2019-02-21 16:55:32 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 58 | static size_t bc_num_int(const BcNum *n) { |
| 59 | return n->len ? n->len - n->rdx : 0; |
| 60 | } |
| 61 | |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 62 | static void bc_num_expand(BcNum *restrict n, size_t req) { |
| 63 | assert(n); |
| 64 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
| 65 | if (req > n->cap) { |
Gavin Howard | 404ece7 | 2019-04-26 15:59:15 -0600 | [diff] [blame] | 66 | n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req)); |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 67 | n->cap = req; |
| 68 | } |
| 69 | } |
| 70 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 71 | static void bc_num_setToZero(BcNum *restrict n, size_t scale) { |
Gavin Howard | a2514a0 | 2018-10-18 14:20:09 -0600 | [diff] [blame] | 72 | assert(n); |
Gavin Howard | fcf2901 | 2019-04-25 20:12:19 -0600 | [diff] [blame] | 73 | n->scale = scale; |
| 74 | n->len = n->rdx = 0; |
Gavin Howard | 97102b1 | 2018-11-01 23:37:31 -0600 | [diff] [blame] | 75 | n->neg = false; |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 76 | } |
| 77 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 78 | static void bc_num_zero(BcNum *restrict n) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 79 | bc_num_setToZero(n, 0); |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 80 | } |
| 81 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 82 | void bc_num_one(BcNum *restrict n) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 83 | bc_num_setToZero(n, 0); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 84 | n->len = 1; |
| 85 | n->num[0] = 1; |
Gavin Howard | 0be26ed | 2018-08-31 20:21:56 -0600 | [diff] [blame] | 86 | } |
| 87 | |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 88 | static void bc_num_clean(BcNum *restrict n) { |
| 89 | while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) --n->len; |
| 90 | if (BC_NUM_ZERO(n)) n->neg = false; |
| 91 | else if (n->len < n->rdx) n->len = n->rdx; |
| 92 | } |
| 93 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 94 | static size_t bc_num_log10(size_t i) { |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 95 | size_t len; |
Gavin Howard | 530e307 | 2019-04-23 16:23:36 -0600 | [diff] [blame] | 96 | for (len = 1; i; i /= BC_BASE, ++len); |
Gavin Howard | c849ed2 | 2019-05-13 02:01:10 -0600 | [diff] [blame] | 97 | assert(len - 1 <= BC_BASE_DIGS + 1); |
Gavin Howard | c134992 | 2019-04-30 20:48:09 -0600 | [diff] [blame] | 98 | return len - 1; |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Gavin Howard | 4007e2f | 2019-05-08 09:08:00 -0600 | [diff] [blame] | 101 | static size_t bc_num_zeroDigits(const BcDig *n) { |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 102 | return BC_BASE_DIGS - bc_num_log10((size_t) *n); |
Gavin Howard | 4007e2f | 2019-05-08 09:08:00 -0600 | [diff] [blame] | 103 | } |
| 104 | |
Gavin Howard | 06fee0c | 2019-05-09 14:43:20 -0600 | [diff] [blame] | 105 | static size_t bc_num_intDigits(const BcNum *n) { |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 106 | size_t digits = bc_num_int(n) * BC_BASE_DIGS; |
Gavin Howard | 4007e2f | 2019-05-08 09:08:00 -0600 | [diff] [blame] | 107 | if (digits > 0) digits -= bc_num_zeroDigits(n->num +n->len - 1); |
Gavin Howard | 445c189 | 2019-04-29 13:50:24 -0600 | [diff] [blame] | 108 | return digits; |
| 109 | } |
| 110 | |
Gavin Howard | c134992 | 2019-04-30 20:48:09 -0600 | [diff] [blame] | 111 | static size_t bc_num_nonzeroLen(const BcNum *restrict n) { |
Gavin Howard | 08f7856 | 2019-04-29 07:56:04 -0600 | [diff] [blame] | 112 | size_t i, len = n->len; |
| 113 | assert(len == n->rdx); |
Gavin Howard | a882ac2 | 2019-05-14 08:44:29 -0600 | [diff] [blame] | 114 | for (i = len - 1; i < n->len && !n->num[i]; --i); |
| 115 | assert(i + 1 > 0); |
| 116 | return i + 1; |
Gavin Howard | 08f7856 | 2019-04-29 07:56:04 -0600 | [diff] [blame] | 117 | } |
| 118 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 119 | static BcBigDig bc_num_addDigit(BcDig *restrict num, BcBigDig d, BcBigDig c) { |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 120 | d += c; |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 121 | *num = (BcDig) (d % BC_BASE_POW); |
| 122 | assert(*num >= 0 && *num < BC_BASE_POW); |
| 123 | return d / BC_BASE_POW; |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static BcStatus bc_num_addArrays(BcDig *restrict a, const BcDig *restrict b, |
| 127 | size_t len) |
| 128 | { |
| 129 | size_t i; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 130 | BcBigDig carry = 0; |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 131 | |
| 132 | for (i = 0; BC_NO_SIG && i < len; ++i) { |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 133 | BcBigDig in = ((BcBigDig) a[i]) + ((BcBigDig) b[i]); |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 134 | carry = bc_num_addDigit(a + i, in, carry); |
| 135 | } |
| 136 | |
| 137 | for (; BC_NO_SIG && carry; ++i) |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 138 | carry = bc_num_addDigit(a + i, (BcBigDig) a[i], carry); |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 139 | |
| 140 | return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
| 141 | } |
| 142 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 143 | static BcStatus bc_num_subArrays(BcDig *restrict a, const BcDig *restrict b, |
| 144 | size_t len) |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 145 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 146 | size_t i, j; |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 147 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 148 | for (i = 0; BC_NO_SIG && i < len; ++i) { |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 149 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 150 | for (a[i] -= b[i], j = 0; BC_NO_SIG && a[i + j] < 0;) { |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 151 | assert(a[i + j] >= -BC_BASE_POW); |
| 152 | a[i + j++] += BC_BASE_POW; |
Gavin Howard | 54b946a | 2018-10-23 12:06:57 -0600 | [diff] [blame] | 153 | a[i + j] -= 1; |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 154 | assert(a[i + j - 1] >= 0 && a[i + j - 1] < BC_BASE_POW); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 155 | } |
| 156 | } |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 157 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 158 | return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | e1e7494 | 2018-03-20 15:51:52 -0600 | [diff] [blame] | 159 | } |
| 160 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 161 | static BcStatus bc_num_mulArray(const BcNum *restrict a, BcBigDig b, |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 162 | BcNum *restrict c) |
| 163 | { |
| 164 | size_t i; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 165 | BcBigDig carry = 0; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 166 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 167 | assert(b <= BC_BASE_POW); |
Gavin Howard | f114882 | 2019-05-11 15:29:24 -0600 | [diff] [blame] | 168 | |
| 169 | if (a->len + 1 > c->cap) bc_num_expand(c, a->len + 1); |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 170 | |
| 171 | memset(c->num, 0, BC_NUM_SIZE(c->cap)); |
| 172 | |
| 173 | for (i = 0; BC_NO_SIG && i < a->len; ++i) { |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 174 | BcBigDig in = ((BcBigDig) a->num[i]) * b + carry; |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 175 | c->num[i] = in % BC_BASE_POW; |
| 176 | carry = in / BC_BASE_POW; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | if (BC_NO_SIG) { |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 180 | assert(carry < BC_BASE_POW); |
Gavin Howard | e7ae4f4 | 2019-05-08 09:08:15 -0600 | [diff] [blame] | 181 | c->num[i] = (BcDig) carry; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 182 | c->len = a->len; |
| 183 | c->len += (carry != 0); |
| 184 | } |
| 185 | |
| 186 | return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
| 187 | } |
| 188 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 189 | static BcStatus bc_num_divArray(const BcNum *restrict a, BcBigDig b, |
| 190 | BcNum *restrict c, BcBigDig *rem) |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 191 | { |
| 192 | size_t i; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 193 | BcBigDig carry = 0; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 194 | |
| 195 | assert(c->cap >= a->len); |
| 196 | |
| 197 | for (i = a->len - 1; BC_NO_SIG && i < a->len; --i) { |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 198 | BcBigDig in = ((BcBigDig) a->num[i]) + carry * BC_BASE_POW; |
Gavin Howard | e7ae4f4 | 2019-05-08 09:08:15 -0600 | [diff] [blame] | 199 | c->num[i] = (BcDig) (in / b); |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 200 | carry = in % b; |
| 201 | } |
| 202 | |
| 203 | c->len = a->len; |
| 204 | bc_num_clean(c); |
| 205 | *rem = carry; |
| 206 | |
| 207 | return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
| 208 | } |
| 209 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 210 | static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b, |
| 211 | size_t len) |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 212 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 213 | size_t i; |
Gavin Howard | 9a66a83 | 2019-05-13 20:20:12 -0600 | [diff] [blame] | 214 | BcDig c = 0; |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 215 | for (i = len - 1; BC_NO_SIG && i < len && !(c = a[i] - b[i]); --i); |
Gavin Howard | a36f031 | 2019-05-09 10:23:07 -0600 | [diff] [blame] | 216 | return BC_SIG ? BC_NUM_CMP_SIGNAL : bc_num_neg(i + 1, c < 0); |
Gavin Howard | 08bf529 | 2018-03-20 14:59:33 -0600 | [diff] [blame] | 217 | } |
| 218 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 219 | ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) { |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 220 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 221 | size_t i, min, a_int, b_int, diff; |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 222 | BcDig *max_num, *min_num; |
Gavin Howard | 7fbb4e2 | 2018-10-18 11:43:17 -0600 | [diff] [blame] | 223 | bool a_max, neg = false; |
| 224 | ssize_t cmp; |
| 225 | |
| 226 | assert(a && b); |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 227 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 228 | if (a == b) return 0; |
Gavin Howard | 56ebcf7 | 2019-02-21 16:59:48 -0700 | [diff] [blame] | 229 | if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg); |
Gavin Howard | d2cf06a | 2019-02-21 17:03:24 -0700 | [diff] [blame] | 230 | if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a); |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 231 | if (a->neg) { |
Gavin Howard | 7fbb4e2 | 2018-10-18 11:43:17 -0600 | [diff] [blame] | 232 | if (b->neg) neg = true; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 233 | else return -1; |
| 234 | } |
| 235 | else if (b->neg) return 1; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 236 | |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 237 | a_int = bc_num_int(a); |
| 238 | b_int = bc_num_int(b); |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 239 | a_int -= b_int; |
| 240 | a_max = (a->rdx > b->rdx); |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 241 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 242 | if (a_int) return (ssize_t) a_int; |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 243 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 244 | if (a_max) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 245 | min = b->rdx; |
| 246 | diff = a->rdx - b->rdx; |
| 247 | max_num = a->num + diff; |
| 248 | min_num = b->num; |
| 249 | } |
| 250 | else { |
| 251 | min = a->rdx; |
| 252 | diff = b->rdx - a->rdx; |
| 253 | max_num = b->num + diff; |
| 254 | min_num = a->num; |
| 255 | } |
Gavin Howard | 4681d1b | 2018-03-05 19:49:33 -0700 | [diff] [blame] | 256 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 257 | cmp = bc_num_compare(max_num, min_num, b_int + min); |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 258 | |
| 259 | #if BC_ENABLE_SIGNALS |
Gavin Howard | a36f031 | 2019-05-09 10:23:07 -0600 | [diff] [blame] | 260 | if (cmp == BC_NUM_CMP_SIGNAL) return cmp; |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 261 | #endif // BC_ENABLE_SIGNALS |
| 262 | |
Gavin Howard | 5eba0e8 | 2019-02-21 18:08:33 -0700 | [diff] [blame] | 263 | if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg); |
Gavin Howard | 021150b | 2018-03-10 15:40:42 -0700 | [diff] [blame] | 264 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 265 | for (max_num -= diff, i = diff - 1; BC_NO_SIG && i < diff; --i) { |
Gavin Howard | 5eba0e8 | 2019-02-21 18:08:33 -0700 | [diff] [blame] | 266 | if (max_num[i]) return bc_num_neg(1, !a_max == !neg); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 267 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 268 | |
Gavin Howard | a36f031 | 2019-05-09 10:23:07 -0600 | [diff] [blame] | 269 | return BC_SIG ? BC_NUM_CMP_SIGNAL : 0; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 272 | void bc_num_truncate(BcNum *restrict n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 273 | |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 274 | size_t places_rdx; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 275 | |
Stefan Esser | 70f1185 | 2019-04-27 10:39:00 +0200 | [diff] [blame] | 276 | if (!places) return; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 277 | |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 278 | places_rdx = n->rdx - BC_NUM_RDX(n->scale - places); |
| 279 | assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len)); |
| 280 | |
| 281 | n->scale -= places; |
| 282 | n->rdx -= places_rdx; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 283 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 284 | if (BC_NUM_NONZERO(n)) { |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 285 | |
Gavin Howard | c9bef2d | 2019-04-26 14:46:52 -0600 | [diff] [blame] | 286 | size_t pow; |
| 287 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 288 | pow = n->scale % BC_BASE_DIGS; |
| 289 | pow = pow ? BC_BASE_DIGS - pow : 0; |
Gavin Howard | c154280 | 2019-05-08 17:20:51 -0600 | [diff] [blame] | 290 | pow = bc_num_pow10[pow]; |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 291 | |
| 292 | n->len -= places_rdx; |
Gavin Howard | 404ece7 | 2019-04-26 15:59:15 -0600 | [diff] [blame] | 293 | memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len)); |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 294 | |
| 295 | // Clear the lower part of the last digit. |
Gavin Howard | da6b543 | 2019-04-26 14:33:43 -0600 | [diff] [blame] | 296 | if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow; |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 297 | |
Gavin Howard | 2a36692 | 2019-01-16 10:50:20 -0700 | [diff] [blame] | 298 | bc_num_clean(n); |
Gavin Howard | 955da85 | 2018-10-23 11:08:20 -0600 | [diff] [blame] | 299 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Stefan Esser | 70f1185 | 2019-04-27 10:39:00 +0200 | [diff] [blame] | 302 | static void bc_num_extend(BcNum *restrict n, size_t places) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 303 | |
Gavin Howard | b5e6da9 | 2019-04-30 19:58:04 -0600 | [diff] [blame] | 304 | size_t places_rdx; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 305 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 306 | if (!places) return; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 307 | |
Gavin Howard | b5e6da9 | 2019-04-30 19:58:04 -0600 | [diff] [blame] | 308 | places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx; |
| 309 | |
Gavin Howard | 8721e33 | 2019-05-06 08:02:49 -0600 | [diff] [blame] | 310 | if (places_rdx) { |
| 311 | bc_num_expand(n, bc_vm_growSize(n->len, places_rdx)); |
| 312 | memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len)); |
| 313 | memset(n->num, 0, BC_NUM_SIZE(places_rdx)); |
| 314 | } |
Gavin Howard | b5e6da9 | 2019-04-30 19:58:04 -0600 | [diff] [blame] | 315 | |
Gavin Howard | e6f326e | 2019-04-26 10:13:45 -0600 | [diff] [blame] | 316 | n->rdx += places_rdx; |
| 317 | n->scale += places; |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 318 | n->len += places_rdx; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 319 | |
Gavin Howard | e6f326e | 2019-04-26 10:13:45 -0600 | [diff] [blame] | 320 | assert(n->rdx == BC_NUM_RDX(n->scale)); |
Stefan Esser | 193f852 | 2019-04-27 01:29:01 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 323 | static void bc_num_retireMul(BcNum *restrict n, size_t scale, |
| 324 | bool neg1, bool neg2) |
| 325 | { |
Gavin Howard | 971a267 | 2019-04-26 14:32:07 -0600 | [diff] [blame] | 326 | if (n->scale < scale) bc_num_extend(n, scale - n->scale); |
| 327 | else bc_num_truncate(n, n->scale - scale); |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 328 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 329 | bc_num_clean(n); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 330 | if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 331 | } |
| 332 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 333 | static void bc_num_split(const BcNum *restrict n, size_t idx, |
| 334 | BcNum *restrict a, BcNum *restrict b) |
| 335 | { |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 336 | if (idx < n->len) { |
| 337 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 338 | b->len = n->len - idx; |
| 339 | a->len = idx; |
Gavin Howard | a3f260c | 2019-04-26 15:47:25 -0600 | [diff] [blame] | 340 | a->scale = a->rdx = b->scale = b->rdx = 0; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 341 | |
Gavin Howard | 404ece7 | 2019-04-26 15:59:15 -0600 | [diff] [blame] | 342 | memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len)); |
| 343 | memcpy(a->num, n->num, BC_NUM_SIZE(idx)); |
Gavin Howard | 50c8002 | 2019-01-03 15:14:33 -0700 | [diff] [blame] | 344 | |
| 345 | bc_num_clean(b); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 346 | } |
Gavin Howard | 50c8002 | 2019-01-03 15:14:33 -0700 | [diff] [blame] | 347 | else bc_num_copy(a, n); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 348 | |
| 349 | bc_num_clean(a); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 350 | } |
| 351 | |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 352 | static size_t bc_num_shiftZero(BcNum *restrict n) { |
| 353 | |
| 354 | size_t i; |
| 355 | |
| 356 | assert(!n->rdx || BC_NUM_ZERO(n)); |
| 357 | |
| 358 | for (i = 0; i < n->len && !n->num[i]; ++i); |
| 359 | |
| 360 | n->len -= i; |
| 361 | n->num += i; |
| 362 | |
| 363 | return i; |
| 364 | } |
| 365 | |
Gavin Howard | 2e736de | 2019-04-27 06:41:58 -0600 | [diff] [blame] | 366 | static void bc_num_unshiftZero(BcNum *restrict n, size_t places_rdx) { |
| 367 | n->len += places_rdx; |
| 368 | n->num -= places_rdx; |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 369 | } |
| 370 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 371 | static BcStatus bc_num_shift(BcNum *restrict n, BcBigDig dig) { |
Gavin Howard | be88459 | 2019-04-29 15:07:49 -0600 | [diff] [blame] | 372 | |
| 373 | size_t i, len = n->len; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 374 | BcBigDig carry = 0, pow; |
Gavin Howard | be88459 | 2019-04-29 15:07:49 -0600 | [diff] [blame] | 375 | BcDig *ptr = n->num; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 376 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 377 | assert(dig < BC_BASE_DIGS); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 378 | |
Gavin Howard | c154280 | 2019-05-08 17:20:51 -0600 | [diff] [blame] | 379 | pow = bc_num_pow10[dig]; |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 380 | dig = bc_num_pow10[BC_BASE_DIGS - dig]; |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 381 | |
Gavin Howard | 834fde1 | 2019-04-30 08:00:05 -0600 | [diff] [blame] | 382 | for (i = len - 1; BC_NO_SIG && i < len; --i) { |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 383 | BcBigDig in, temp; |
| 384 | in = ((BcBigDig) ptr[i]); |
Gavin Howard | 834fde1 | 2019-04-30 08:00:05 -0600 | [diff] [blame] | 385 | temp = carry * dig; |
| 386 | carry = in % pow; |
| 387 | ptr[i] = ((BcDig) (in / pow)) + (BcDig) temp; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 388 | } |
| 389 | |
Gavin Howard | 834fde1 | 2019-04-30 08:00:05 -0600 | [diff] [blame] | 390 | assert(!carry); |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 391 | |
| 392 | return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 393 | } |
| 394 | |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 395 | static BcStatus bc_num_shiftLeft(BcNum *restrict n, size_t places) { |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 396 | |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 397 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 398 | BcBigDig dig; |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 399 | size_t places_rdx; |
Gavin Howard | be88459 | 2019-04-29 15:07:49 -0600 | [diff] [blame] | 400 | bool shift; |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 401 | |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 402 | if (!places) return s; |
Gavin Howard | abe4fdf | 2019-05-06 08:39:53 -0600 | [diff] [blame] | 403 | if (places > n->scale) { |
| 404 | size_t size = bc_vm_growSize(BC_NUM_RDX(places - n->scale), n->len); |
| 405 | if (size > SIZE_MAX - 1) return bc_vm_err(BC_ERROR_MATH_OVERFLOW); |
| 406 | } |
Gavin Howard | 4631f3a | 2019-04-30 10:48:40 -0600 | [diff] [blame] | 407 | if (BC_NUM_ZERO(n)) { |
| 408 | if (n->scale >= places) n->scale -= places; |
| 409 | else n->scale = 0; |
| 410 | return s; |
| 411 | } |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 412 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 413 | dig = (BcBigDig) (places % BC_BASE_DIGS); |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 414 | shift = (dig != 0); |
Gavin Howard | 22253c7 | 2019-05-06 08:03:57 -0600 | [diff] [blame] | 415 | places_rdx = BC_NUM_RDX(places); |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 416 | |
Gavin Howard | 22253c7 | 2019-05-06 08:03:57 -0600 | [diff] [blame] | 417 | if (n->scale) { |
Gavin Howard | a187919 | 2019-04-30 18:55:08 -0600 | [diff] [blame] | 418 | |
| 419 | if (n->rdx >= places_rdx) { |
| 420 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 421 | size_t mod = n->scale % BC_BASE_DIGS, revdig; |
Gavin Howard | a187919 | 2019-04-30 18:55:08 -0600 | [diff] [blame] | 422 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 423 | mod = mod ? mod : BC_BASE_DIGS; |
| 424 | revdig = dig ? BC_BASE_DIGS - dig : 0; |
Gavin Howard | a187919 | 2019-04-30 18:55:08 -0600 | [diff] [blame] | 425 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 426 | if (mod + revdig > BC_BASE_DIGS) places_rdx = 1; |
Gavin Howard | a187919 | 2019-04-30 18:55:08 -0600 | [diff] [blame] | 427 | else places_rdx = 0; |
| 428 | } |
| 429 | else places_rdx -= n->rdx; |
| 430 | } |
Gavin Howard | 4631f3a | 2019-04-30 10:48:40 -0600 | [diff] [blame] | 431 | |
| 432 | if (places_rdx) { |
Gavin Howard | e396dff | 2019-05-01 07:28:36 -0600 | [diff] [blame] | 433 | bc_num_expand(n, bc_vm_growSize(n->len, places_rdx)); |
Gavin Howard | 834fde1 | 2019-04-30 08:00:05 -0600 | [diff] [blame] | 434 | memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len)); |
| 435 | memset(n->num, 0, BC_NUM_SIZE(places_rdx)); |
| 436 | n->len += places_rdx; |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 437 | } |
Gavin Howard | 4631f3a | 2019-04-30 10:48:40 -0600 | [diff] [blame] | 438 | |
| 439 | if (places > n->scale) n->scale = n->rdx = 0; |
| 440 | else { |
| 441 | n->scale -= places; |
| 442 | n->rdx = BC_NUM_RDX(n->scale); |
| 443 | } |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 444 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 445 | if (shift) s = bc_num_shift(n, BC_BASE_DIGS - dig); |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 446 | |
| 447 | bc_num_clean(n); |
| 448 | |
| 449 | return BC_SIG && !s ? BC_STATUS_SIGNAL : s; |
| 450 | } |
| 451 | |
| 452 | static BcStatus bc_num_shiftRight(BcNum *restrict n, size_t places) { |
| 453 | |
| 454 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 455 | BcBigDig dig; |
Gavin Howard | b5e6da9 | 2019-04-30 19:58:04 -0600 | [diff] [blame] | 456 | size_t places_rdx, scale, scale_mod, int_len, expand; |
| 457 | bool shift; |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 458 | |
| 459 | if (!places) return s; |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 460 | if (BC_NUM_ZERO(n)) { |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 461 | n->scale += places; |
| 462 | bc_num_expand(n, BC_NUM_RDX(n->scale)); |
| 463 | return s; |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 464 | } |
| 465 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 466 | dig = (BcBigDig) (places % BC_BASE_DIGS); |
Gavin Howard | b5e6da9 | 2019-04-30 19:58:04 -0600 | [diff] [blame] | 467 | shift = (dig != 0); |
Gavin Howard | 834fde1 | 2019-04-30 08:00:05 -0600 | [diff] [blame] | 468 | scale = n->scale; |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 469 | scale_mod = scale % BC_BASE_DIGS; |
| 470 | scale_mod = scale_mod ? scale_mod : BC_BASE_DIGS; |
Gavin Howard | 834fde1 | 2019-04-30 08:00:05 -0600 | [diff] [blame] | 471 | int_len = bc_num_int(n); |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 472 | places_rdx = BC_NUM_RDX(places); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 473 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 474 | if (scale_mod + dig > BC_BASE_DIGS) { |
Gavin Howard | b5e6da9 | 2019-04-30 19:58:04 -0600 | [diff] [blame] | 475 | expand = places_rdx - 1; |
| 476 | places_rdx = 1; |
Gavin Howard | 834fde1 | 2019-04-30 08:00:05 -0600 | [diff] [blame] | 477 | } |
| 478 | else { |
Gavin Howard | b5e6da9 | 2019-04-30 19:58:04 -0600 | [diff] [blame] | 479 | expand = places_rdx; |
| 480 | places_rdx = 0; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 481 | } |
| 482 | |
Gavin Howard | b5e6da9 | 2019-04-30 19:58:04 -0600 | [diff] [blame] | 483 | if (expand > int_len) expand -= int_len; |
| 484 | else expand = 0; |
| 485 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 486 | bc_num_extend(n, places_rdx * BC_BASE_DIGS); |
Gavin Howard | e396dff | 2019-05-01 07:28:36 -0600 | [diff] [blame] | 487 | bc_num_expand(n, bc_vm_growSize(expand, n->len)); |
Gavin Howard | b5e6da9 | 2019-04-30 19:58:04 -0600 | [diff] [blame] | 488 | memset(n->num + n->len, 0, BC_NUM_SIZE(expand)); |
| 489 | n->len += expand; |
Gavin Howard | 834fde1 | 2019-04-30 08:00:05 -0600 | [diff] [blame] | 490 | n->scale = n->rdx = 0; |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 491 | |
Gavin Howard | 834fde1 | 2019-04-30 08:00:05 -0600 | [diff] [blame] | 492 | if (shift) s = bc_num_shift(n, dig); |
| 493 | |
| 494 | n->scale = scale + places; |
| 495 | n->rdx = BC_NUM_RDX(n->scale); |
| 496 | |
| 497 | bc_num_clean(n); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 498 | |
| 499 | assert(n->rdx <= n->len && n->len <= n->cap); |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 500 | assert(n->rdx == BC_NUM_RDX(n->scale)); |
| 501 | |
| 502 | return BC_SIG && !s ? BC_STATUS_SIGNAL : s; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 503 | } |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 504 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 505 | static BcStatus bc_num_inv(BcNum *a, BcNum *b, size_t scale) { |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 506 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 507 | BcNum one; |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 508 | BcDig num[2]; |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 509 | |
Gavin Howard | 971a267 | 2019-04-26 14:32:07 -0600 | [diff] [blame] | 510 | assert(BC_NUM_NONZERO(a)); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 511 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 512 | one.cap = 2; |
| 513 | one.num = num; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 514 | bc_num_one(&one); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 515 | |
Gavin Howard | d32d7df | 2018-10-15 08:42:25 -0600 | [diff] [blame] | 516 | return bc_num_div(&one, a, b, scale); |
Gavin Howard | 9c4358c | 2018-03-22 20:11:28 -0600 | [diff] [blame] | 517 | } |
| 518 | |
Gavin Howard | 7bda478 | 2018-12-28 09:53:22 -0700 | [diff] [blame] | 519 | #if BC_ENABLE_EXTRA_MATH |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 520 | static BcStatus bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c, |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 521 | BcBigDig *v) |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 522 | { |
Gavin Howard | ecafd4f | 2019-02-23 09:30:45 -0700 | [diff] [blame] | 523 | if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER); |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 524 | bc_num_copy(c, a); |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 525 | return bc_num_bigdig(b, v); |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 526 | } |
Gavin Howard | 7bda478 | 2018-12-28 09:53:22 -0700 | [diff] [blame] | 527 | #endif // BC_ENABLE_EXTRA_MATH |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 528 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 529 | static BcStatus bc_num_a(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 530 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 531 | BcDig *ptr, *ptr_a, *ptr_b, *ptr_c; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 532 | size_t i, max, min_rdx, min_int, diff, a_int, b_int; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 533 | BcBigDig carry; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 534 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 535 | // Because this function doesn't need to use scale (per the bc spec), |
| 536 | // 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] | 537 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 538 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 539 | bc_num_copy(c, b); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 540 | if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 541 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 542 | } |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 543 | if (BC_NUM_ZERO(b)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 544 | bc_num_copy(c, a); |
| 545 | return BC_STATUS_SUCCESS; |
| 546 | } |
Gavin Howard | f6964a1 | 2018-03-14 10:52:31 -0600 | [diff] [blame] | 547 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 548 | c->neg = a->neg; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 549 | c->rdx = BC_MAX(a->rdx, b->rdx); |
Gavin Howard | fba50a2 | 2019-04-26 10:07:21 -0600 | [diff] [blame] | 550 | c->scale = BC_MAX(a->scale, b->scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 551 | min_rdx = BC_MIN(a->rdx, b->rdx); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 552 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 553 | if (a->rdx > b->rdx) { |
| 554 | diff = a->rdx - b->rdx; |
| 555 | ptr = a->num; |
| 556 | ptr_a = a->num + diff; |
| 557 | ptr_b = b->num; |
| 558 | } |
| 559 | else { |
| 560 | diff = b->rdx - a->rdx; |
| 561 | ptr = b->num; |
| 562 | ptr_a = a->num; |
| 563 | ptr_b = b->num + diff; |
| 564 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 565 | |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 566 | for (ptr_c = c->num, i = 0; i < diff; ++i) ptr_c[i] = ptr[i]; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 567 | |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 568 | c->len = diff; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 569 | ptr_c += diff; |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 570 | a_int = bc_num_int(a); |
| 571 | b_int = bc_num_int(b); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 572 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 573 | if (a_int > b_int) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 574 | min_int = b_int; |
| 575 | max = a_int; |
| 576 | ptr = ptr_a; |
| 577 | } |
| 578 | else { |
| 579 | min_int = a_int; |
| 580 | max = b_int; |
| 581 | ptr = ptr_b; |
| 582 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 583 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 584 | for (carry = 0, i = 0; BC_NO_SIG && i < min_rdx + min_int; ++i) { |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 585 | BcBigDig in = ((BcBigDig) ptr_a[i]) + ((BcBigDig) ptr_b[i]); |
Gavin Howard | dd7a0fd | 2019-01-22 09:56:46 -0700 | [diff] [blame] | 586 | carry = bc_num_addDigit(ptr_c + i, in, carry); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 587 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 588 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 589 | for (; BC_NO_SIG && i < max + min_rdx; ++i) |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 590 | carry = bc_num_addDigit(ptr_c + i, (BcBigDig) ptr[i], carry); |
Gavin Howard | cafcd3e | 2019-01-22 09:54:24 -0700 | [diff] [blame] | 591 | |
| 592 | c->len += i; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 593 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 594 | if (carry) c->num[c->len++] = (BcDig) carry; |
Gavin Howard | 2ea7dc4 | 2018-05-22 14:02:02 -0600 | [diff] [blame] | 595 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 596 | return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 599 | static BcStatus bc_num_s(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 600 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 601 | BcStatus s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 602 | ssize_t cmp; |
| 603 | BcNum *minuend, *subtrahend; |
| 604 | size_t start; |
| 605 | bool aneg, bneg, neg; |
Gavin Howard | a1c090a | 2018-03-05 14:20:33 -0700 | [diff] [blame] | 606 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 607 | // Because this function doesn't need to use scale (per the bc spec), |
| 608 | // 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] | 609 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 610 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 611 | bc_num_copy(c, b); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 612 | if (sub && BC_NUM_NONZERO(c)) c->neg = !c->neg; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 613 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 614 | } |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 615 | if (BC_NUM_ZERO(b)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 616 | bc_num_copy(c, a); |
| 617 | return BC_STATUS_SUCCESS; |
| 618 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 619 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 620 | aneg = a->neg; |
| 621 | bneg = b->neg; |
| 622 | a->neg = b->neg = false; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 623 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 624 | cmp = bc_num_cmp(a, b); |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 625 | |
| 626 | #if BC_ENABLE_SIGNALS |
Gavin Howard | a36f031 | 2019-05-09 10:23:07 -0600 | [diff] [blame] | 627 | if (cmp == BC_NUM_CMP_SIGNAL) return BC_STATUS_SIGNAL; |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 628 | #endif // BC_ENABLE_SIGNALS |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 629 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 630 | a->neg = aneg; |
| 631 | b->neg = bneg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 632 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 633 | if (!cmp) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 634 | bc_num_setToZero(c, BC_MAX(a->rdx, b->rdx)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 635 | return BC_STATUS_SUCCESS; |
| 636 | } |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 637 | |
| 638 | if (cmp > 0) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 639 | neg = a->neg; |
| 640 | minuend = a; |
| 641 | subtrahend = b; |
| 642 | } |
| 643 | else { |
| 644 | neg = b->neg; |
| 645 | if (sub) neg = !neg; |
| 646 | minuend = b; |
| 647 | subtrahend = a; |
| 648 | } |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 649 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 650 | bc_num_copy(c, minuend); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 651 | c->neg = neg; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 652 | |
Gavin Howard | e6f326e | 2019-04-26 10:13:45 -0600 | [diff] [blame] | 653 | if (c->scale < subtrahend->scale) { |
| 654 | bc_num_extend(c, subtrahend->scale - c->scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 655 | start = 0; |
| 656 | } |
| 657 | else start = c->rdx - subtrahend->rdx; |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 658 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 659 | s = bc_num_subArrays(c->num + start, subtrahend->num, subtrahend->len); |
Gavin Howard | fa4983a | 2019-02-16 23:43:03 -0700 | [diff] [blame] | 660 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 661 | bc_num_clean(c); |
Gavin Howard | 9a6f7a4 | 2018-03-05 14:07:06 -0700 | [diff] [blame] | 662 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 663 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 666 | static BcStatus bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c) |
| 667 | { |
Gavin Howard | 971a267 | 2019-04-26 14:32:07 -0600 | [diff] [blame] | 668 | size_t i, alen = a->len, blen = b->len, clen; |
Gavin Howard | 5083aec | 2019-04-23 18:03:46 -0600 | [diff] [blame] | 669 | BcDig *ptr_a = a->num, *ptr_b = b->num, *ptr_c; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 670 | BcBigDig sum = 0, carry = 0; |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 671 | |
Gavin Howard | 971a267 | 2019-04-26 14:32:07 -0600 | [diff] [blame] | 672 | assert(sizeof(sum) >= sizeof(BcDig) * 2); |
Gavin Howard | b538d80 | 2019-04-23 16:50:55 -0600 | [diff] [blame] | 673 | assert(!a->rdx && !b->rdx); |
| 674 | |
Gavin Howard | 5083aec | 2019-04-23 18:03:46 -0600 | [diff] [blame] | 675 | clen = bc_vm_growSize(alen, blen); |
Gavin Howard | e396dff | 2019-05-01 07:28:36 -0600 | [diff] [blame] | 676 | bc_num_expand(c, bc_vm_growSize(clen, 1)); |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 677 | |
| 678 | ptr_c = c->num; |
Gavin Howard | 404ece7 | 2019-04-26 15:59:15 -0600 | [diff] [blame] | 679 | memset(ptr_c, 0, BC_NUM_SIZE(c->cap)); |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 680 | |
Gavin Howard | 5083aec | 2019-04-23 18:03:46 -0600 | [diff] [blame] | 681 | for (i = 0; BC_NO_SIG && i < clen; ++i) { |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 682 | |
Gavin Howard | 5083aec | 2019-04-23 18:03:46 -0600 | [diff] [blame] | 683 | ssize_t sidx = (ssize_t) (i - blen + 1); |
| 684 | size_t j = (size_t) BC_MAX(0, sidx), k = BC_MIN(i, blen - 1); |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 685 | |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 686 | for (; BC_NO_SIG && j < alen && k < blen; ++j, --k) { |
Stefan Eßer | 4b111c2 | 2019-04-24 17:23:08 -0600 | [diff] [blame] | 687 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 688 | sum += ((BcBigDig) ptr_a[j]) * ((BcBigDig) ptr_b[k]); |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 689 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 690 | if (sum >= BC_BASE_POW) { |
| 691 | carry += sum / BC_BASE_POW; |
| 692 | sum %= BC_BASE_POW; |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 693 | } |
| 694 | } |
Stefan Eßer | 4b111c2 | 2019-04-24 17:23:08 -0600 | [diff] [blame] | 695 | |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 696 | ptr_c[i] = (BcDig) sum; |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 697 | assert(ptr_c[i] < BC_BASE_POW); |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 698 | sum = carry; |
| 699 | carry = 0; |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 700 | } |
| 701 | |
Gavin Howard | 5083aec | 2019-04-23 18:03:46 -0600 | [diff] [blame] | 702 | if (sum) { |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 703 | assert(sum < BC_BASE_POW); |
Gavin Howard | 12a7cd2 | 2019-04-24 07:10:14 -0600 | [diff] [blame] | 704 | ptr_c[clen] = (BcDig) sum; |
Gavin Howard | 5083aec | 2019-04-23 18:03:46 -0600 | [diff] [blame] | 705 | clen += 1; |
| 706 | } |
| 707 | |
| 708 | c->len = clen; |
Gavin Howard | a58840c | 2019-05-07 19:33:53 -0600 | [diff] [blame] | 709 | |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 710 | return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
| 711 | } |
| 712 | |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 713 | static BcStatus bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a, |
| 714 | size_t shift, BcNumShiftAddOp op) |
| 715 | { |
| 716 | assert(n->len >= shift + a->len); |
| 717 | assert(!n->rdx && !a->rdx); |
| 718 | return op(n->num + shift, a->num, a->len); |
| 719 | } |
| 720 | |
| 721 | static BcStatus bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) { |
Gavin Howard | 773c86b | 2018-11-02 14:07:19 -0600 | [diff] [blame] | 722 | |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 723 | BcStatus s; |
Stefan Esser | 14eff46 | 2019-04-25 07:42:03 +0200 | [diff] [blame] | 724 | size_t max, max2, total; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 725 | BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 726 | BcDig *digs, *dig_ptr; |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 727 | BcNumShiftAddOp op; |
Gavin Howard | b2f01a4 | 2019-05-10 20:00:32 -0600 | [diff] [blame] | 728 | bool aone = BC_NUM_ONE(a); |
Gavin Howard | 337fe60 | 2018-09-01 15:10:59 -0600 | [diff] [blame] | 729 | |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 730 | assert(BC_NUM_ZERO(c)); |
| 731 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 732 | // This is here because the function is recursive. |
Gavin Howard | 5083aec | 2019-04-23 18:03:46 -0600 | [diff] [blame] | 733 | if (BC_SIG) return BC_STATUS_SIGNAL; |
Gavin Howard | f1ae2be | 2019-05-09 11:58:22 -0600 | [diff] [blame] | 734 | if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) return BC_STATUS_SUCCESS; |
Gavin Howard | b2f01a4 | 2019-05-10 20:00:32 -0600 | [diff] [blame] | 735 | if (aone || BC_NUM_ONE(b)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 736 | bc_num_copy(c, aone ? b : a); |
Gavin Howard | b2f01a4 | 2019-05-10 20:00:32 -0600 | [diff] [blame] | 737 | if ((aone && a->neg) || b->neg) c->neg = !c->neg; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 738 | return BC_STATUS_SUCCESS; |
| 739 | } |
Gavin Howard | b7417b1 | 2019-05-10 18:39:46 -0600 | [diff] [blame] | 740 | if (a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) |
Gavin Howard | dc4e371 | 2019-04-23 14:56:29 -0600 | [diff] [blame] | 741 | return bc_num_m_simp(a, b, c); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 742 | |
Gavin Howard | e043250 | 2019-02-26 16:23:45 -0700 | [diff] [blame] | 743 | max = BC_MAX(a->len, b->len); |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 744 | max = BC_MAX(max, BC_NUM_DEF_SIZE); |
Gavin Howard | e043250 | 2019-02-26 16:23:45 -0700 | [diff] [blame] | 745 | max2 = (max + 1) / 2; |
| 746 | |
Stefan Esser | 5fd43a3 | 2019-04-25 07:05:24 +0200 | [diff] [blame] | 747 | total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max); |
Gavin Howard | 404ece7 | 2019-04-26 15:59:15 -0600 | [diff] [blame] | 748 | digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total)); |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 749 | |
| 750 | bc_num_setup(&l1, dig_ptr, max); |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 751 | dig_ptr += max; |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 752 | bc_num_setup(&h1, dig_ptr, max); |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 753 | dig_ptr += max; |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 754 | bc_num_setup(&l2, dig_ptr, max); |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 755 | dig_ptr += max; |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 756 | bc_num_setup(&h2, dig_ptr, max); |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 757 | dig_ptr += max; |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 758 | bc_num_setup(&m1, dig_ptr, max); |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 759 | dig_ptr += max; |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 760 | bc_num_setup(&m2, dig_ptr, max); |
Gavin Howard | 5722302 | 2019-04-24 08:37:14 -0600 | [diff] [blame] | 761 | max = bc_vm_growSize(max, 1); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 762 | bc_num_init(&z0, max); |
| 763 | bc_num_init(&z1, max); |
| 764 | bc_num_init(&z2, max); |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 765 | max = bc_vm_growSize(max, max) + 1; |
| 766 | bc_num_init(&temp, max); |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 767 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 768 | bc_num_split(a, max2, &l1, &h1); |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 769 | bc_num_clean(&l1); |
| 770 | bc_num_clean(&h1); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 771 | bc_num_split(b, max2, &l2, &h2); |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 772 | bc_num_clean(&l2); |
| 773 | bc_num_clean(&h2); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 774 | |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 775 | bc_num_expand(c, max); |
| 776 | c->len = max; |
Gavin Howard | 404ece7 | 2019-04-26 15:59:15 -0600 | [diff] [blame] | 777 | memset(c->num, 0, BC_NUM_SIZE(c->len)); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 778 | |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 779 | s = bc_num_sub(&h1, &l1, &m1, 0); |
| 780 | if (BC_ERR(s)) goto err; |
| 781 | s = bc_num_sub(&l2, &h2, &m2, 0); |
| 782 | if (BC_ERR(s)) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 783 | |
Gavin Howard | 971a267 | 2019-04-26 14:32:07 -0600 | [diff] [blame] | 784 | if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) { |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 785 | |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 786 | s = bc_num_m(&h1, &h2, &z2, 0); |
| 787 | if (BC_ERR(s)) goto err; |
| 788 | bc_num_clean(&z2); |
| 789 | |
| 790 | s = bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays); |
| 791 | if (BC_ERR(s)) goto err; |
| 792 | s = bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays); |
| 793 | if (BC_ERR(s)) goto err; |
| 794 | } |
| 795 | |
Gavin Howard | 971a267 | 2019-04-26 14:32:07 -0600 | [diff] [blame] | 796 | if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) { |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 797 | |
| 798 | s = bc_num_m(&l1, &l2, &z0, 0); |
| 799 | if (BC_ERR(s)) goto err; |
| 800 | bc_num_clean(&z0); |
| 801 | |
| 802 | s = bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays); |
| 803 | if (BC_ERR(s)) goto err; |
| 804 | s = bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays); |
| 805 | if (BC_ERR(s)) goto err; |
| 806 | } |
| 807 | |
Gavin Howard | 971a267 | 2019-04-26 14:32:07 -0600 | [diff] [blame] | 808 | if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) { |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 809 | |
| 810 | s = bc_num_m(&m1, &m2, &z1, 0); |
| 811 | if (BC_ERR(s)) goto err; |
| 812 | bc_num_clean(&z1); |
| 813 | |
| 814 | op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays; |
| 815 | s = bc_num_shiftAddSub(c, &z1, max2, op); |
| 816 | if (BC_ERR(s)) goto err; |
| 817 | } |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 818 | |
| 819 | err: |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 820 | free(digs); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 821 | bc_num_free(&temp); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 822 | bc_num_free(&z2); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 823 | bc_num_free(&z1); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 824 | bc_num_free(&z0); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 825 | return s; |
| 826 | } |
| 827 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 828 | static BcStatus bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 829 | |
| 830 | BcStatus s; |
| 831 | BcNum cpa, cpb; |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 832 | size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 833 | |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 834 | bc_num_setToZero(c, 0); |
Gavin Howard | cf9f246 | 2019-04-27 06:43:57 -0600 | [diff] [blame] | 835 | ascale = a->scale; |
| 836 | bscale = b->scale; |
| 837 | scale = BC_MAX(scale, ascale); |
Gavin Howard | a9a887a | 2019-04-29 14:00:56 -0600 | [diff] [blame] | 838 | scale = BC_MAX(scale, bscale); |
Gavin Howard | 7cb5f6d | 2019-04-29 14:01:44 -0600 | [diff] [blame] | 839 | |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 840 | rscale = ascale + bscale; |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 841 | scale = BC_MIN(rscale, scale); |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 842 | |
Gavin Howard | cc2974b | 2019-05-08 11:07:28 -0600 | [diff] [blame] | 843 | if ((a->len == 1 || b->len == 1) && !a->rdx && !b->rdx) { |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 844 | |
| 845 | BcNum *operand; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 846 | BcBigDig dig; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 847 | |
| 848 | if (a->len == 1) { |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 849 | dig = (BcBigDig) a->num[0]; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 850 | operand = b; |
| 851 | } |
| 852 | else { |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 853 | dig = (BcBigDig) b->num[0]; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 854 | operand = a; |
| 855 | } |
| 856 | |
| 857 | s = bc_num_mulArray(operand, dig, c); |
Gavin Howard | 66a9355 | 2019-05-09 17:13:59 -0600 | [diff] [blame] | 858 | if (BC_ERROR_SIGNAL_ONLY(s)) return s; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 859 | |
| 860 | c->scale = operand->scale; |
| 861 | c->rdx = operand->rdx; |
| 862 | c->neg = (a->neg != b->neg); |
Gavin Howard | 66a9355 | 2019-05-09 17:13:59 -0600 | [diff] [blame] | 863 | return s; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 864 | } |
| 865 | |
Gavin Howard | 66a9355 | 2019-05-09 17:13:59 -0600 | [diff] [blame] | 866 | bc_num_init(&cpa, a->len + a->rdx); |
| 867 | bc_num_init(&cpb, b->len + b->rdx); |
| 868 | bc_num_copy(&cpa, a); |
| 869 | bc_num_copy(&cpb, b); |
| 870 | |
| 871 | cpa.neg = cpb.neg = false; |
| 872 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 873 | ardx = cpa.rdx * BC_BASE_DIGS; |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 874 | s = bc_num_shiftLeft(&cpa, ardx); |
| 875 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 876 | bc_num_clean(&cpa); |
| 877 | azero = bc_num_shiftZero(&cpa); |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 878 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 879 | brdx = cpb.rdx * BC_BASE_DIGS; |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 880 | s = bc_num_shiftLeft(&cpb, brdx); |
| 881 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 882 | bzero = bc_num_shiftZero(&cpb); |
| 883 | bc_num_clean(&cpb); |
| 884 | |
Gavin Howard | 14c354a | 2019-04-24 14:28:24 -0600 | [diff] [blame] | 885 | s = bc_num_k(&cpa, &cpb, c); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 886 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 887 | |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 888 | zero = bc_vm_growSize(azero, bzero); |
| 889 | len = bc_vm_growSize(c->len, zero); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 890 | |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 891 | bc_num_expand(c, len); |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 892 | s = bc_num_shiftLeft(c, (len - c->len) * BC_BASE_DIGS); |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 893 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
| 894 | s = bc_num_shiftRight(c, ardx + brdx); |
| 895 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 896 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 897 | bc_num_retireMul(c, scale, a->neg, b->neg); |
Gavin Howard | cf9f246 | 2019-04-27 06:43:57 -0600 | [diff] [blame] | 898 | |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 899 | err: |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 900 | bc_num_unshiftZero(&cpb, bzero); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 901 | bc_num_free(&cpb); |
Gavin Howard | 14913fc | 2019-04-23 14:36:03 -0600 | [diff] [blame] | 902 | bc_num_unshiftZero(&cpa, azero); |
Gavin Howard | 305249a | 2018-10-15 20:24:47 -0600 | [diff] [blame] | 903 | bc_num_free(&cpa); |
| 904 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 907 | static ssize_t bc_num_divCmp(const BcDig *a, const BcNum *b, size_t len) { |
| 908 | |
| 909 | ssize_t cmp; |
| 910 | |
| 911 | if (b->len > len && a[len]) cmp = bc_num_compare(a, b->num, len + 1); |
| 912 | else if (b->len <= len) { |
| 913 | if (a[len]) cmp = 1; |
| 914 | else cmp = bc_num_compare(a, b->num, len); |
| 915 | } |
| 916 | else cmp = -1; |
| 917 | |
| 918 | return cmp; |
| 919 | } |
| 920 | |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 921 | static BcStatus bc_num_d_long(BcNum *restrict a, const BcNum *restrict b, |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 922 | BcNum *restrict c, size_t scale) |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 923 | { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 924 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 92cf905 | 2019-05-09 08:05:57 -0600 | [diff] [blame] | 925 | BcBigDig divisor, q; |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 926 | size_t len, end, i, rdx; |
| 927 | BcNum cpb, sub, temp; |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 928 | BcDig *n; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 929 | |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 930 | len = b->len; |
| 931 | end = a->len - len; |
| 932 | divisor = (BcBigDig) b->num[len - 1]; |
Gavin Howard | 070a6ce | 2019-05-08 11:50:25 -0600 | [diff] [blame] | 933 | |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 934 | bc_num_expand(c, a->len); |
| 935 | memset(c->num + end, 0, (c->cap - end) * sizeof(BcDig)); |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 936 | |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 937 | c->rdx = a->rdx; |
| 938 | c->scale = a->scale; |
| 939 | c->len = a->len; |
| 940 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 941 | assert(c->scale >= scale); |
| 942 | rdx = c->rdx - BC_NUM_RDX(scale); |
Gavin Howard | 6134966 | 2019-05-08 11:08:45 -0600 | [diff] [blame] | 943 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 944 | bc_num_init(&cpb, len + 1); |
| 945 | bc_num_init(&sub, len + 1); |
| 946 | bc_num_init(&temp, len + 1); |
| 947 | |
| 948 | for (i = end - 1; BC_NO_SIG && BC_NO_ERR(!s) && i < end && i >= rdx; --i) { |
Gavin Howard | 6134966 | 2019-05-08 11:08:45 -0600 | [diff] [blame] | 949 | |
Gavin Howard | 070a6ce | 2019-05-08 11:50:25 -0600 | [diff] [blame] | 950 | ssize_t cmp; |
| 951 | |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 952 | n = a->num + i; |
Gavin Howard | 070a6ce | 2019-05-08 11:50:25 -0600 | [diff] [blame] | 953 | q = 0; |
| 954 | |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 955 | cmp = bc_num_divCmp(n, b, len); |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 956 | |
| 957 | #if BC_ENABLE_SIGNALS |
Gavin Howard | a36f031 | 2019-05-09 10:23:07 -0600 | [diff] [blame] | 958 | if (cmp == BC_NUM_CMP_SIGNAL) break; |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 959 | #endif // BC_ENABLE_SIGNALS |
Gavin Howard | 070a6ce | 2019-05-08 11:50:25 -0600 | [diff] [blame] | 960 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 961 | if (!cmp) { |
| 962 | |
| 963 | q = 1; |
| 964 | |
| 965 | s = bc_num_mulArray(b, (BcBigDig) q, &cpb); |
| 966 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
| 967 | } |
Gavin Howard | 070a6ce | 2019-05-08 11:50:25 -0600 | [diff] [blame] | 968 | else if (cmp > 0) { |
| 969 | |
Gavin Howard | 92cf905 | 2019-05-09 08:05:57 -0600 | [diff] [blame] | 970 | BcBigDig n1, pow, dividend; |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 971 | size_t cpblen; |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 972 | |
| 973 | n1 = (BcBigDig) n[len]; |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 974 | dividend = n1 * BC_BASE_POW + (BcBigDig) n[len - 1]; |
Gavin Howard | 92cf905 | 2019-05-09 08:05:57 -0600 | [diff] [blame] | 975 | q = (dividend / divisor + 1); |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 976 | q = q > BC_BASE_POW ? BC_BASE_POW : q; |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 977 | dividend = ((BcBigDig) bc_num_log10((size_t) q)); |
| 978 | |
Gavin Howard | c0b8a7c | 2019-05-13 01:56:30 -0600 | [diff] [blame] | 979 | assert(dividend > 0); |
| 980 | |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 981 | pow = bc_num_pow10[dividend - 1]; |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 982 | |
Gavin Howard | 92cf905 | 2019-05-09 08:05:57 -0600 | [diff] [blame] | 983 | s = bc_num_mulArray(b, (BcBigDig) q, &cpb); |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 984 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
| 985 | |
Gavin Howard | 6945eaf | 2019-05-09 17:06:54 -0600 | [diff] [blame] | 986 | s = bc_num_mulArray(b, pow, &sub); |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 987 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
| 988 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 989 | cpblen = cpb.len; |
| 990 | |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 991 | while (BC_NO_SIG && BC_NO_ERR(!s) && pow > 0) { |
| 992 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 993 | s = bc_num_subArrays(cpb.num, sub.num, sub.len); |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 994 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
| 995 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 996 | bc_num_clean(&cpb); |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 997 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 998 | cmp = bc_num_divCmp(n, &cpb, len); |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 999 | |
| 1000 | #if BC_ENABLE_SIGNALS |
Gavin Howard | a36f031 | 2019-05-09 10:23:07 -0600 | [diff] [blame] | 1001 | if (cmp == BC_NUM_CMP_SIGNAL) goto err; |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 1002 | #endif // BC_ENABLE_SIGNALS |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 1003 | |
| 1004 | while (BC_NO_SIG && BC_NO_ERR(!s) && cmp < 0) { |
| 1005 | |
| 1006 | q -= pow; |
| 1007 | |
| 1008 | s = bc_num_subArrays(cpb.num, sub.num, sub.len); |
| 1009 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
| 1010 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 1011 | bc_num_clean(&cpb); |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 1012 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 1013 | cmp = bc_num_divCmp(n, &cpb, len); |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 1014 | |
| 1015 | #if BC_ENABLE_SIGNALS |
Gavin Howard | a36f031 | 2019-05-09 10:23:07 -0600 | [diff] [blame] | 1016 | if (cmp == BC_NUM_CMP_SIGNAL) goto err; |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 1017 | #endif // BC_ENABLE_SIGNALS |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | pow /= BC_BASE; |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 1021 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 1022 | if (pow) { |
| 1023 | |
Gavin Howard | 5a68403 | 2019-05-10 08:07:19 -0600 | [diff] [blame] | 1024 | BcBigDig rem; |
| 1025 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 1026 | s = bc_num_addArrays(cpb.num, sub.num, sub.len); |
| 1027 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
| 1028 | |
| 1029 | cpb.len = cpblen; |
| 1030 | bc_num_clean(&cpb); |
| 1031 | |
| 1032 | bc_num_copy(&temp, &sub); |
| 1033 | s = bc_num_divArray(&temp, 10, &sub, &rem); |
| 1034 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
| 1035 | assert(rem == 0); |
| 1036 | } |
Gavin Howard | 070a6ce | 2019-05-08 11:50:25 -0600 | [diff] [blame] | 1037 | } |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 1038 | |
| 1039 | q -= 1; |
| 1040 | } |
| 1041 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1042 | assert(q <= BC_BASE_POW); |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 1043 | |
| 1044 | if (q) { |
| 1045 | |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 1046 | s = bc_num_subArrays(n, cpb.num, len); |
| 1047 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 070a6ce | 2019-05-08 11:50:25 -0600 | [diff] [blame] | 1048 | } |
| 1049 | |
Gavin Howard | 92cf905 | 2019-05-09 08:05:57 -0600 | [diff] [blame] | 1050 | c->num[i] = (BcDig) q; |
Gavin Howard | 6134966 | 2019-05-08 11:08:45 -0600 | [diff] [blame] | 1051 | } |
| 1052 | |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 1053 | err: |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1054 | if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL; |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 1055 | bc_num_free(&temp); |
Gavin Howard | 66a9355 | 2019-05-09 17:13:59 -0600 | [diff] [blame] | 1056 | bc_num_free(&cpb); |
Gavin Howard | 66a9355 | 2019-05-09 17:13:59 -0600 | [diff] [blame] | 1057 | bc_num_free(&sub); |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1058 | return s; |
| 1059 | } |
| 1060 | |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1061 | static BcStatus bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
| 1062 | |
| 1063 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | fcb7446 | 2019-05-09 14:42:35 -0600 | [diff] [blame] | 1064 | size_t len; |
| 1065 | BcNum cpa, cpb; |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1066 | bool zero = true; |
| 1067 | |
| 1068 | if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); |
| 1069 | if (BC_NUM_ZERO(a)) { |
| 1070 | bc_num_setToZero(c, scale); |
| 1071 | return BC_STATUS_SUCCESS; |
| 1072 | } |
Gavin Howard | b2f01a4 | 2019-05-10 20:00:32 -0600 | [diff] [blame] | 1073 | if (BC_NUM_ONE(b)) { |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1074 | bc_num_copy(c, a); |
| 1075 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1076 | return BC_STATUS_SUCCESS; |
| 1077 | } |
| 1078 | if (!a->rdx && !b->rdx && b->len == 1 && !scale) { |
| 1079 | BcBigDig rem; |
| 1080 | s = bc_num_divArray(a, (BcBigDig) b->num[0], c, &rem); |
| 1081 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1082 | return s; |
| 1083 | } |
| 1084 | |
| 1085 | len = bc_num_mulReq(a, b, scale); |
| 1086 | bc_num_init(&cpa, len); |
| 1087 | bc_num_copy(&cpa, a); |
Gavin Howard | fcb7446 | 2019-05-09 14:42:35 -0600 | [diff] [blame] | 1088 | bc_num_createCopy(&cpb, b); |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1089 | |
Gavin Howard | 03e35dd | 2019-05-11 06:56:30 -0600 | [diff] [blame] | 1090 | len = b->len; |
| 1091 | |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1092 | if (len > cpa.len) { |
| 1093 | bc_num_expand(&cpa, bc_vm_growSize(len, 2)); |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1094 | bc_num_extend(&cpa, (len - cpa.len) * BC_BASE_DIGS); |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1095 | } |
| 1096 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1097 | cpa.scale = cpa.rdx * BC_BASE_DIGS; |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1098 | |
| 1099 | bc_num_extend(&cpa, b->scale); |
| 1100 | cpa.rdx -= BC_NUM_RDX(b->scale); |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1101 | cpa.scale = cpa.rdx * BC_BASE_DIGS; |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 1102 | if (scale > cpa.scale) { |
| 1103 | bc_num_extend(&cpa, scale); |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1104 | cpa.scale = cpa.rdx * BC_BASE_DIGS; |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 1105 | } |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1106 | |
| 1107 | if (b->rdx == b->len) { |
Gavin Howard | fcb7446 | 2019-05-09 14:42:35 -0600 | [diff] [blame] | 1108 | size_t i; |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1109 | for (i = 0; zero && i < len; ++i) zero = !b->num[len - i - 1]; |
| 1110 | assert(i != len || !zero); |
| 1111 | len -= i - 1; |
| 1112 | } |
| 1113 | |
| 1114 | if (cpa.cap == cpa.len) bc_num_expand(&cpa, bc_vm_growSize(cpa.len, 1)); |
| 1115 | |
| 1116 | // We want an extra zero in front to make things simpler. |
| 1117 | cpa.num[cpa.len++] = 0; |
| 1118 | |
Gavin Howard | fcb7446 | 2019-05-09 14:42:35 -0600 | [diff] [blame] | 1119 | if (cpa.rdx == cpa.len) cpa.len = bc_num_nonzeroLen(&cpa); |
| 1120 | if (cpb.rdx == cpb.len) cpb.len = bc_num_nonzeroLen(&cpb); |
| 1121 | cpb.scale = cpb.rdx = 0; |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1122 | |
Gavin Howard | bd60ec7 | 2019-05-10 08:01:22 -0600 | [diff] [blame] | 1123 | s = bc_num_d_long(&cpa, &cpb, c, scale); |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1124 | |
| 1125 | if (BC_NO_ERR(!s)) { |
| 1126 | bc_num_retireMul(c, scale, a->neg, b->neg); |
| 1127 | if (BC_SIG) s = BC_STATUS_SIGNAL; |
| 1128 | } |
| 1129 | |
Gavin Howard | fcb7446 | 2019-05-09 14:42:35 -0600 | [diff] [blame] | 1130 | bc_num_free(&cpb); |
Gavin Howard | 1980e03 | 2019-05-08 17:21:17 -0600 | [diff] [blame] | 1131 | bc_num_free(&cpa); |
Gavin Howard | d5b27c8 | 2019-05-09 12:13:07 -0600 | [diff] [blame] | 1132 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1133 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1136 | static BcStatus bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c, |
| 1137 | BcNum *restrict d, size_t scale, size_t ts) |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 1138 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1139 | BcStatus s; |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 1140 | BcNum temp; |
| 1141 | bool neg; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1142 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1143 | if (BC_NUM_ZERO(b)) return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); |
| 1144 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | 99ca499 | 2019-01-02 13:48:49 -0700 | [diff] [blame] | 1145 | bc_num_setToZero(c, ts); |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 1146 | bc_num_setToZero(d, ts); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1147 | return BC_STATUS_SUCCESS; |
| 1148 | } |
Gavin Howard | 8b25487 | 2018-03-14 01:13:35 -0600 | [diff] [blame] | 1149 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1150 | bc_num_init(&temp, d->cap); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1151 | s = bc_num_d(a, b, c, scale); |
| 1152 | assert(!s || s == BC_STATUS_SIGNAL); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1153 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1154 | |
Gavin Howard | 996fc22 | 2019-04-29 13:22:38 -0600 | [diff] [blame] | 1155 | if (scale) scale = ts + 1; |
Gavin Howard | 3115c01 | 2018-10-04 10:53:56 -0600 | [diff] [blame] | 1156 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1157 | s = bc_num_m(c, b, &temp, scale); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1158 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1159 | s = bc_num_sub(a, &temp, d, scale); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1160 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 5d149cf | 2018-09-06 13:46:23 -0600 | [diff] [blame] | 1161 | |
Gavin Howard | 7744543 | 2019-04-26 15:59:33 -0600 | [diff] [blame] | 1162 | if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale); |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 1163 | |
| 1164 | neg = d->neg; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1165 | bc_num_retireMul(d, ts, a->neg, b->neg); |
Gavin Howard | 996fc22 | 2019-04-29 13:22:38 -0600 | [diff] [blame] | 1166 | d->neg = BC_NUM_NONZERO(d) ? neg : false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1167 | |
| 1168 | err: |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 1169 | bc_num_free(&temp); |
| 1170 | return s; |
| 1171 | } |
| 1172 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1173 | static BcStatus bc_num_rem(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 1174 | { |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 1175 | BcStatus s; |
| 1176 | BcNum c1; |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 1177 | size_t ts; |
Gavin Howard | 1844108 | 2018-10-22 10:03:30 -0600 | [diff] [blame] | 1178 | |
Gavin Howard | 7744543 | 2019-04-26 15:59:33 -0600 | [diff] [blame] | 1179 | ts = bc_vm_growSize(scale, b->scale); |
| 1180 | ts = BC_MAX(ts, a->scale); |
Gavin Howard | 798508b | 2019-02-21 16:45:32 -0700 | [diff] [blame] | 1181 | |
| 1182 | bc_num_init(&c1, bc_num_mulReq(a, b, ts)); |
Gavin Howard | 54b946a | 2018-10-23 12:06:57 -0600 | [diff] [blame] | 1183 | s = bc_num_r(a, b, &c1, c, scale, ts); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1184 | bc_num_free(&c1); |
Gavin Howard | d64ce7b | 2018-10-24 16:20:20 -0600 | [diff] [blame] | 1185 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1186 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1189 | static BcStatus bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1190 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1191 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1192 | BcNum copy; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1193 | BcBigDig pow = 0; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1194 | size_t i, powrdx, resrdx; |
| 1195 | bool neg, zero; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1196 | |
Gavin Howard | ecafd4f | 2019-02-23 09:30:45 -0700 | [diff] [blame] | 1197 | if (BC_ERR(b->rdx)) return bc_vm_err(BC_ERROR_MATH_NON_INTEGER); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1198 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1199 | if (BC_NUM_ZERO(b)) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1200 | bc_num_one(c); |
Gavin Howard | 61abdbe | 2018-10-22 13:05:38 -0600 | [diff] [blame] | 1201 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1202 | } |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1203 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 1204 | bc_num_setToZero(c, scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1205 | return BC_STATUS_SUCCESS; |
| 1206 | } |
Gavin Howard | b2f01a4 | 2019-05-10 20:00:32 -0600 | [diff] [blame] | 1207 | if (BC_NUM_ONE(b)) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1208 | if (!b->neg) bc_num_copy(c, a); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1209 | else s = bc_num_inv(a, c, scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1210 | return s; |
| 1211 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1212 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1213 | neg = b->neg; |
| 1214 | b->neg = false; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1215 | s = bc_num_bigdig(b, &pow); |
Gavin Howard | fb14efc | 2018-12-22 14:01:58 -0700 | [diff] [blame] | 1216 | b->neg = neg; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1217 | if (s) return s; |
| 1218 | |
Gavin Howard | baa4f58 | 2019-01-24 13:56:35 -0700 | [diff] [blame] | 1219 | bc_num_createCopy(©, a); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1220 | |
Gavin Howard | eec0728 | 2019-05-10 20:10:18 -0600 | [diff] [blame] | 1221 | if (!neg) { |
| 1222 | size_t max = BC_MAX(scale, a->scale), scalepow = a->scale * pow; |
| 1223 | scale = BC_MIN(scalepow, max); |
| 1224 | } |
Gavin Howard | b29674f | 2018-03-22 22:24:58 -0600 | [diff] [blame] | 1225 | |
Gavin Howard | d497c23 | 2019-04-29 10:32:38 -0600 | [diff] [blame] | 1226 | for (powrdx = a->scale; BC_NO_SIG && !(pow & 1); pow >>= 1) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1227 | powrdx <<= 1; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1228 | s = bc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1229 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1230 | } |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 1231 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1232 | if (BC_SIG) goto sig_err; |
Gavin Howard | 6fb635f | 2018-03-03 12:45:42 -0700 | [diff] [blame] | 1233 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1234 | bc_num_copy(c, ©); |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 1235 | resrdx = powrdx; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1236 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1237 | while (BC_NO_SIG && (pow >>= 1)) { |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1238 | |
Gavin Howard | 4d5daba | 2019-01-07 14:42:50 -0700 | [diff] [blame] | 1239 | powrdx <<= 1; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1240 | s = bc_num_mul(©, ©, ©, powrdx); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1241 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1242 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1243 | if (pow & 1) { |
| 1244 | resrdx += powrdx; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1245 | s = bc_num_mul(c, ©, c, resrdx); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1246 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1247 | } |
| 1248 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1249 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1250 | if (BC_SIG) goto sig_err; |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 1251 | if (neg) { |
| 1252 | s = bc_num_inv(c, c, scale); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1253 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 1254 | } |
| 1255 | |
Stefan Eßer | b531834 | 2019-04-29 10:31:26 -0600 | [diff] [blame] | 1256 | if (c->scale > scale) bc_num_truncate(c, c->scale - scale); |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 1257 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1258 | // We can't use bc_num_clean() here. |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1259 | 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] | 1260 | if (zero) bc_num_setToZero(c, scale); |
Gavin Howard | 1d95915 | 2018-03-03 23:33:13 -0700 | [diff] [blame] | 1261 | |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 1262 | sig_err: |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1263 | if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1264 | err: |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1265 | bc_num_free(©); |
| 1266 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
Gavin Howard | 7bda478 | 2018-12-28 09:53:22 -0700 | [diff] [blame] | 1269 | #if BC_ENABLE_EXTRA_MATH |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1270 | static BcStatus bc_num_place(BcNum *a, BcNum *b, BcNum *restrict c, |
| 1271 | size_t scale) |
| 1272 | { |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1273 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1274 | BcBigDig val = 0; |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1275 | |
| 1276 | BC_UNUSED(scale); |
| 1277 | |
| 1278 | s = bc_num_intop(a, b, c, &val); |
Gavin Howard | ecafd4f | 2019-02-23 09:30:45 -0700 | [diff] [blame] | 1279 | if (BC_ERR(s)) return s; |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1280 | |
Gavin Howard | c9bef2d | 2019-04-26 14:46:52 -0600 | [diff] [blame] | 1281 | if (val < c->scale) bc_num_truncate(c, c->scale - val); |
| 1282 | else if (val > c->scale) bc_num_extend(c, val - c->scale); |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1283 | |
| 1284 | return s; |
| 1285 | } |
| 1286 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1287 | static BcStatus bc_num_left(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) |
| 1288 | { |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1289 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1290 | BcBigDig val = 0; |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1291 | |
| 1292 | BC_UNUSED(scale); |
| 1293 | |
| 1294 | s = bc_num_intop(a, b, c, &val); |
Gavin Howard | ecafd4f | 2019-02-23 09:30:45 -0700 | [diff] [blame] | 1295 | if (BC_ERR(s)) return s; |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1296 | |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 1297 | return bc_num_shiftLeft(c, (size_t) val); |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1300 | static BcStatus bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, |
| 1301 | size_t scale) |
| 1302 | { |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1303 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1304 | BcBigDig val = 0; |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1305 | |
| 1306 | BC_UNUSED(scale); |
| 1307 | |
| 1308 | s = bc_num_intop(a, b, c, &val); |
Gavin Howard | ecafd4f | 2019-02-23 09:30:45 -0700 | [diff] [blame] | 1309 | if (BC_ERR(s)) return s; |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1310 | |
Gavin Howard | 2a36692 | 2019-01-16 10:50:20 -0700 | [diff] [blame] | 1311 | if (BC_NUM_ZERO(c)) return s; |
| 1312 | |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 1313 | return bc_num_shiftRight(c, (size_t) val); |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1314 | } |
Gavin Howard | 7bda478 | 2018-12-28 09:53:22 -0700 | [diff] [blame] | 1315 | #endif // BC_ENABLE_EXTRA_MATH |
Gavin Howard | ac4d912 | 2018-12-27 23:59:12 -0700 | [diff] [blame] | 1316 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1317 | static BcStatus bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale, |
| 1318 | BcNumBinaryOp op, size_t req) |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1319 | { |
Gavin Howard | a1c4439 | 2018-09-27 12:41:15 -0600 | [diff] [blame] | 1320 | BcStatus s; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1321 | BcNum num2, *ptr_a, *ptr_b; |
| 1322 | bool init = false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1323 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1324 | assert(a && b && c && op); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1325 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 1326 | if (c == a) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1327 | ptr_a = &num2; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1328 | memcpy(ptr_a, c, sizeof(BcNum)); |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 1329 | init = true; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1330 | } |
| 1331 | else ptr_a = a; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1332 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1333 | if (c == b) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1334 | ptr_b = &num2; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1335 | if (c != a) { |
| 1336 | memcpy(ptr_b, c, sizeof(BcNum)); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1337 | init = true; |
| 1338 | } |
| 1339 | } |
| 1340 | else ptr_b = b; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1341 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1342 | if (init) bc_num_init(c, req); |
| 1343 | else bc_num_expand(c, req); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1344 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1345 | s = op(ptr_a, ptr_b, c, scale); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1346 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1347 | assert(!c->neg || BC_NUM_NONZERO(c)); |
Gavin Howard | 87c29c7 | 2019-03-28 11:22:51 -0600 | [diff] [blame] | 1348 | assert(c->rdx <= c->len || !c->len || s); |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 1349 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 1350 | if (init) bc_num_free(&num2); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1351 | |
Gavin Howard | ee9d01e | 2019-02-16 22:48:11 -0700 | [diff] [blame] | 1352 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
Gavin Howard | c90ed90 | 2019-01-02 13:07:49 -0700 | [diff] [blame] | 1355 | #ifndef NDEBUG |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1356 | static bool bc_num_strValid(const char *val) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1357 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 1358 | bool radix = false; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1359 | size_t i, len = strlen(val); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1360 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1361 | if (!len) return true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1362 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1363 | for (i = 0; i < len; ++i) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1364 | |
Gavin Howard | 8a921bd | 2018-10-11 14:15:32 -0600 | [diff] [blame] | 1365 | BcDig c = val[i]; |
Gavin Howard | f6e3fb3 | 2018-08-09 13:48:59 -0600 | [diff] [blame] | 1366 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1367 | if (c == '.') { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1368 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1369 | if (radix) return false; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1370 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1371 | radix = true; |
| 1372 | continue; |
| 1373 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1374 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 1375 | if (!(isdigit(c) || isupper(c))) return false; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1376 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1377 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1378 | return true; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1379 | } |
Gavin Howard | c90ed90 | 2019-01-02 13:07:49 -0700 | [diff] [blame] | 1380 | #endif // NDEBUG |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1381 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1382 | static BcBigDig bc_num_parseChar(char c, size_t base_t) { |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 1383 | |
| 1384 | if (isupper(c)) { |
| 1385 | c = BC_NUM_NUM_LETTER(c); |
Gavin Howard | 6193aaf | 2019-01-03 16:44:04 -0700 | [diff] [blame] | 1386 | c = ((size_t) c) >= base_t ? (char) base_t - 1 : c; |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 1387 | } |
| 1388 | else c -= '0'; |
| 1389 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1390 | return (BcBigDig) (uchar) c; |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1393 | static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) { |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1394 | |
Gavin Howard | 3b60abb | 2019-04-26 08:49:17 -0600 | [diff] [blame] | 1395 | size_t len, i, temp, mod; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1396 | const char *ptr; |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1397 | bool zero = true, rdx; |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 1398 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1399 | for (i = 0; val[i] == '0'; ++i); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1400 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1401 | val += i; |
Gavin Howard | 46a7d8d | 2019-05-06 15:16:12 -0600 | [diff] [blame] | 1402 | assert(!val[0] || isalnum(val[0]) || val[0] == '.'); |
| 1403 | |
| 1404 | // All 0's. We can just return, since this |
| 1405 | // procedure expects a virgin (already 0) BcNum. |
| 1406 | if (!val[0]) return; |
| 1407 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1408 | len = strlen(val); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1409 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1410 | ptr = strchr(val, '.'); |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1411 | rdx = (ptr != NULL); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1412 | |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1413 | for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1414 | |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1415 | n->scale = (size_t) (rdx * ((val + len) - (ptr + 1))); |
Gavin Howard | 1c1697c | 2019-04-26 10:07:45 -0600 | [diff] [blame] | 1416 | n->rdx = BC_NUM_RDX(n->scale); |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1417 | |
Gavin Howard | 3b60abb | 2019-04-26 08:49:17 -0600 | [diff] [blame] | 1418 | i = len - (ptr == val ? 0 : i) - rdx; |
Gavin Howard | 48a0e47 | 2019-04-26 15:39:46 -0600 | [diff] [blame] | 1419 | temp = BC_NUM_ROUND_POW(i); |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1420 | mod = n->scale % BC_BASE_DIGS; |
| 1421 | i = mod ? BC_BASE_DIGS - mod : 0; |
| 1422 | n->len = ((temp + i) / BC_BASE_DIGS); |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1423 | |
| 1424 | bc_num_expand(n, n->len); |
Gavin Howard | 404ece7 | 2019-04-26 15:59:15 -0600 | [diff] [blame] | 1425 | memset(n->num, 0, BC_NUM_SIZE(n->len)); |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1426 | |
Gavin Howard | 6076dbb | 2019-04-26 09:47:50 -0600 | [diff] [blame] | 1427 | if (zero) n->len = n->rdx = 0; |
| 1428 | else { |
Gavin Howard | fcf2901 | 2019-04-25 20:12:19 -0600 | [diff] [blame] | 1429 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1430 | BcBigDig exp, pow; |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1431 | |
Gavin Howard | 687cf4d | 2019-05-11 08:55:36 -0600 | [diff] [blame] | 1432 | assert(i <= BC_NUM_BIGDIG_MAX); |
| 1433 | |
| 1434 | exp = (BcBigDig) i; |
Gavin Howard | c154280 | 2019-05-08 17:20:51 -0600 | [diff] [blame] | 1435 | pow = bc_num_pow10[exp]; |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1436 | |
| 1437 | for (i = len - 1; i < len; --i, ++exp) { |
Gavin Howard | 8dd307e | 2019-01-08 23:05:19 -0700 | [diff] [blame] | 1438 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 1439 | char c = val[i]; |
Gavin Howard | 8dd307e | 2019-01-08 23:05:19 -0700 | [diff] [blame] | 1440 | |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1441 | if (c == '.') exp -= 1; |
Gavin Howard | 8dd307e | 2019-01-08 23:05:19 -0700 | [diff] [blame] | 1442 | else { |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1443 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1444 | size_t idx = exp / BC_BASE_DIGS; |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1445 | |
Gavin Howard | 8dd307e | 2019-01-08 23:05:19 -0700 | [diff] [blame] | 1446 | if (isupper(c)) c = '9'; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1447 | n->num[idx] += (((BcBigDig) c) - '0') * pow; |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1448 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1449 | if ((exp + 1) % BC_BASE_DIGS == 0) pow = 1; |
Gavin Howard | d43ec37 | 2019-04-25 21:13:54 -0600 | [diff] [blame] | 1450 | else pow *= BC_BASE; |
Gavin Howard | 8dd307e | 2019-01-08 23:05:19 -0700 | [diff] [blame] | 1451 | } |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 1452 | } |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1453 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1456 | static BcStatus bc_num_parseBase(BcNum *restrict n, const char *restrict val, |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1457 | BcBigDig base) |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1458 | { |
| 1459 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1460 | BcNum temp, mult1, mult2, result1, result2, *m1, *m2, *ptr; |
Gavin Howard | 7744543 | 2019-04-26 15:59:33 -0600 | [diff] [blame] | 1461 | char c = 0; |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 1462 | bool zero = true; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1463 | BcBigDig v; |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1464 | size_t i, digs, len = strlen(val); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 1465 | |
Gavin Howard | 11b9afd | 2018-10-18 14:23:28 -0600 | [diff] [blame] | 1466 | for (i = 0; zero && i < len; ++i) zero = (val[i] == '.' || val[i] == '0'); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1467 | if (zero) return BC_STATUS_SUCCESS; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 1468 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1469 | bc_num_init(&temp, BC_NUM_BIGDIG_LOG10); |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1470 | bc_num_init(&mult1, BC_NUM_BIGDIG_LOG10); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 1471 | |
Gavin Howard | c7d655b | 2018-12-28 19:45:13 -0700 | [diff] [blame] | 1472 | for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) { |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 1473 | |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1474 | v = bc_num_parseChar(c, base); |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 1475 | |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1476 | s = bc_num_mulArray(n, base, &mult1); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1477 | if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1478 | bc_num_bigdig2num(&temp, v); |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1479 | s = bc_num_add(&mult1, &temp, n, 0); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1480 | if (BC_ERROR_SIGNAL_ONLY(s)) goto int_err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1481 | } |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 1482 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1483 | if (i == len && !(c = val[i])) goto int_err; |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1484 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1485 | assert(c == '.'); |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1486 | bc_num_init(&mult2, BC_NUM_BIGDIG_LOG10); |
| 1487 | bc_num_init(&result1, BC_NUM_DEF_SIZE); |
| 1488 | bc_num_init(&result2, BC_NUM_DEF_SIZE); |
| 1489 | bc_num_one(&mult1); |
| 1490 | |
| 1491 | m1 = &mult1; |
| 1492 | m2 = &mult2; |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 1493 | |
Gavin Howard | 8722e83 | 2019-05-08 11:12:16 -0600 | [diff] [blame] | 1494 | for (i += 1, digs = 0; BC_NO_SIG && i < len && (c = val[i]); ++i, ++digs) { |
| 1495 | |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1496 | v = bc_num_parseChar(c, base); |
Gavin Howard | 9b80163 | 2019-02-16 23:34:06 -0700 | [diff] [blame] | 1497 | |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1498 | s = bc_num_mulArray(&result1, base, &result2); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1499 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1500 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1501 | bc_num_bigdig2num(&temp, v); |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1502 | s = bc_num_add(&result2, &temp, &result1, 0); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1503 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1504 | s = bc_num_mulArray(m1, base, m2); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1505 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1506 | |
| 1507 | ptr = m1; |
| 1508 | m1 = m2; |
| 1509 | m2 = ptr; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1510 | } |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 1511 | |
Gavin Howard | f2ee634 | 2019-04-09 17:05:20 -0600 | [diff] [blame] | 1512 | if (BC_SIG) { |
| 1513 | s = BC_STATUS_SIGNAL; |
| 1514 | goto err; |
| 1515 | } |
| 1516 | |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1517 | // This one cannot be a divide by 0 because mult starts out at 1, then is |
| 1518 | // multiplied by base, and base cannot be 0, so mult cannot be 0. |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1519 | s = bc_num_div(&result1, m1, &result2, digs * 2); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1520 | assert(!s || s == BC_STATUS_SIGNAL); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1521 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1522 | bc_num_truncate(&result2, digs); |
| 1523 | s = bc_num_add(n, &result2, n, digs); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1524 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | d141768 | 2019-02-15 17:08:42 -0700 | [diff] [blame] | 1525 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1526 | if (BC_NUM_NONZERO(n)) { |
Gavin Howard | b0642be | 2019-04-30 21:12:22 -0600 | [diff] [blame] | 1527 | if (n->scale < digs) bc_num_extend(n, digs - n->scale); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1528 | } |
| 1529 | else bc_num_zero(n); |
Gavin Howard | ede51f0 | 2018-03-02 12:30:00 -0700 | [diff] [blame] | 1530 | |
Gavin Howard | d141768 | 2019-02-15 17:08:42 -0700 | [diff] [blame] | 1531 | err: |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1532 | bc_num_free(&result2); |
| 1533 | bc_num_free(&result1); |
| 1534 | bc_num_free(&mult2); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1535 | int_err: |
Gavin Howard | 4c60569 | 2019-05-08 09:09:17 -0600 | [diff] [blame] | 1536 | bc_num_free(&mult1); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1537 | bc_num_free(&temp); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1538 | return s; |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1541 | static void bc_num_printNewline() { |
Gavin Howard | 6193aaf | 2019-01-03 16:44:04 -0700 | [diff] [blame] | 1542 | if (vm->nchars >= (size_t) (vm->line_len - 1)) { |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 1543 | bc_vm_putchar('\\'); |
| 1544 | bc_vm_putchar('\n'); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1545 | } |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 1546 | } |
| 1547 | |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1548 | static void bc_num_putchar(int c) { |
| 1549 | if (c != '\n') bc_num_printNewline(); |
| 1550 | bc_vm_putchar(c); |
| 1551 | } |
| 1552 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 1553 | #if DC_ENABLED |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1554 | static void bc_num_printChar(size_t n, size_t len, bool rdx) { |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1555 | BC_UNUSED(rdx); |
Gavin Howard | eedfb2c | 2019-05-07 08:00:13 -0600 | [diff] [blame] | 1556 | BC_UNUSED(len); |
| 1557 | assert(len == 1); |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1558 | bc_vm_putchar((uchar) n); |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1559 | } |
| 1560 | #endif // DC_ENABLED |
| 1561 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1562 | static void bc_num_printDigits(size_t n, size_t len, bool rdx) { |
| 1563 | |
Gavin Howard | a84ad99 | 2018-12-03 19:11:06 -0700 | [diff] [blame] | 1564 | size_t exp, pow; |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 1565 | |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1566 | bc_num_putchar(rdx ? '.' : ' '); |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 1567 | |
Gavin Howard | 530e307 | 2019-04-23 16:23:36 -0600 | [diff] [blame] | 1568 | for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE); |
Gavin Howard | 2ed2bfb | 2018-03-14 02:42:32 -0600 | [diff] [blame] | 1569 | |
Gavin Howard | eedfb2c | 2019-05-07 08:00:13 -0600 | [diff] [blame] | 1570 | for (exp = 0; exp < len; pow /= BC_BASE, ++exp) { |
Gavin Howard | 4b075f2 | 2019-05-07 15:27:53 -0600 | [diff] [blame] | 1571 | size_t dig = n / pow; |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1572 | n -= dig * pow; |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1573 | bc_num_putchar(((uchar) dig) + '0'); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1574 | } |
Gavin Howard | 6fbdb29 | 2018-02-27 15:44:48 -0700 | [diff] [blame] | 1575 | } |
Gavin Howard | fe679f0 | 2018-02-14 15:50:09 -0700 | [diff] [blame] | 1576 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1577 | static void bc_num_printHex(size_t n, size_t len, bool rdx) { |
Gavin Howard | 70e3d60 | 2018-12-11 11:00:49 -0700 | [diff] [blame] | 1578 | |
Gavin Howard | eedfb2c | 2019-05-07 08:00:13 -0600 | [diff] [blame] | 1579 | BC_UNUSED(len); |
| 1580 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1581 | assert(len == 1); |
Gavin Howard | 80977b2 | 2018-09-06 14:29:13 -0600 | [diff] [blame] | 1582 | |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1583 | if (rdx) bc_num_putchar('.'); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1584 | |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1585 | bc_num_putchar(bc_num_hex_digits[n]); |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1586 | } |
| 1587 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 1588 | static void bc_num_printDecimal(const BcNum *restrict n) { |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 1589 | |
Gavin Howard | 51cd7ea | 2019-04-26 09:48:12 -0600 | [diff] [blame] | 1590 | size_t i, j, rdx = n->rdx; |
| 1591 | bool zero = true; |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1592 | size_t buffer[BC_BASE_DIGS]; |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 1593 | |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1594 | if (n->neg) bc_num_putchar('-'); |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 1595 | |
Gavin Howard | 51cd7ea | 2019-04-26 09:48:12 -0600 | [diff] [blame] | 1596 | for (i = n->len - 1; i < n->len; --i) { |
| 1597 | |
| 1598 | BcDig n9 = n->num[i]; |
| 1599 | size_t temp; |
| 1600 | bool irdx = (i == rdx - 1); |
| 1601 | |
| 1602 | zero = (zero & !irdx); |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1603 | temp = n->scale % BC_BASE_DIGS; |
| 1604 | temp = i || !temp ? 0 : BC_BASE_DIGS - temp; |
Gavin Howard | 51cd7ea | 2019-04-26 09:48:12 -0600 | [diff] [blame] | 1605 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1606 | memset(buffer, 0, BC_BASE_DIGS * sizeof(size_t)); |
Gavin Howard | 51cd7ea | 2019-04-26 09:48:12 -0600 | [diff] [blame] | 1607 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1608 | for (j = 0; n9 && j < BC_BASE_DIGS; ++j) { |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 1609 | buffer[j] = n9 % BC_BASE; |
| 1610 | n9 /= BC_BASE; |
| 1611 | } |
Stefan Esser | 0da1775 | 2019-04-25 12:25:15 +0200 | [diff] [blame] | 1612 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1613 | for (j = BC_BASE_DIGS - 1; j < BC_BASE_DIGS && j >= temp; --j) { |
| 1614 | bool print_rdx = (irdx & (j == BC_BASE_DIGS - 1)); |
Gavin Howard | 51cd7ea | 2019-04-26 09:48:12 -0600 | [diff] [blame] | 1615 | zero = (zero && buffer[j] == 0); |
| 1616 | if (!zero) bc_num_printHex(buffer[j], 1, print_rdx); |
Stefan Esser | a299ffc | 2019-04-24 23:28:32 +0200 | [diff] [blame] | 1617 | } |
| 1618 | } |
Gavin Howard | 32f2beb | 2018-03-09 11:43:20 -0700 | [diff] [blame] | 1619 | } |
| 1620 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1621 | #if BC_ENABLE_EXTRA_MATH |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 1622 | static BcStatus bc_num_printExponent(const BcNum *restrict n, bool eng) { |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1623 | |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 1624 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1625 | bool neg = (n->len <= n->rdx); |
| 1626 | BcNum temp, exp; |
| 1627 | size_t places, mod; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1628 | BcDig digs[BC_NUM_BIGDIG_LOG10]; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1629 | |
| 1630 | bc_num_createCopy(&temp, n); |
| 1631 | |
| 1632 | if (neg) { |
Gavin Howard | c134992 | 2019-04-30 20:48:09 -0600 | [diff] [blame] | 1633 | |
| 1634 | size_t i, idx = bc_num_nonzeroLen(n) - 1; |
| 1635 | |
| 1636 | places = 1; |
| 1637 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1638 | for (i = BC_BASE_DIGS - 1; i < BC_BASE_DIGS; --i) { |
Gavin Howard | c154280 | 2019-05-08 17:20:51 -0600 | [diff] [blame] | 1639 | if (bc_num_pow10[i] > (BcBigDig) n->num[idx]) places += 1; |
Gavin Howard | c134992 | 2019-04-30 20:48:09 -0600 | [diff] [blame] | 1640 | else break; |
| 1641 | } |
| 1642 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1643 | places += (n->rdx - (idx + 1)) * BC_BASE_DIGS; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1644 | mod = places % 3; |
Gavin Howard | c134992 | 2019-04-30 20:48:09 -0600 | [diff] [blame] | 1645 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1646 | if (eng && mod != 0) places += 3 - mod; |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 1647 | s = bc_num_shiftLeft(&temp, places); |
| 1648 | if (BC_ERROR_SIGNAL_ONLY(s)) goto exit; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1649 | } |
| 1650 | else { |
Gavin Howard | 06fee0c | 2019-05-09 14:43:20 -0600 | [diff] [blame] | 1651 | places = bc_num_intDigits(n) - 1; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1652 | mod = places % 3; |
| 1653 | if (eng && mod != 0) places -= 3 - (3 - mod); |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 1654 | s = bc_num_shiftRight(&temp, places); |
| 1655 | if (BC_ERROR_SIGNAL_ONLY(s)) goto exit; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | bc_num_printDecimal(&temp); |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1659 | bc_num_putchar('e'); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1660 | |
| 1661 | if (!places) { |
| 1662 | bc_num_printHex(0, 1, false); |
| 1663 | goto exit; |
| 1664 | } |
| 1665 | |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1666 | if (neg) bc_num_putchar('-'); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1667 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1668 | bc_num_setup(&exp, digs, BC_NUM_BIGDIG_LOG10); |
| 1669 | bc_num_bigdig2num(&exp, (BcBigDig) places); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1670 | |
| 1671 | bc_num_printDecimal(&exp); |
| 1672 | |
| 1673 | exit: |
| 1674 | bc_num_free(&temp); |
Gavin Howard | b155866 | 2019-04-26 14:32:55 -0600 | [diff] [blame] | 1675 | return BC_SIG ? BC_STATUS_SIGNAL : BC_STATUS_SUCCESS; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1676 | } |
| 1677 | #endif // BC_ENABLE_EXTRA_MATH |
| 1678 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1679 | static BcStatus bc_num_printNum(BcNum *restrict n, BcBigDig base, |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1680 | size_t len, BcNumDigitOp print) |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 1681 | { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1682 | BcStatus s; |
| 1683 | BcVec stack; |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1684 | BcNum intp1, intp2, fracp1, fracp2, digit, flen1, flen2, *n1, *n2, *temp; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1685 | BcBigDig dig, *ptr; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1686 | size_t i; |
| 1687 | bool radix; |
Gavin Howard | 1705b98 | 2019-05-11 15:18:03 -0600 | [diff] [blame] | 1688 | BcDig digit_digs[BC_NUM_BIGDIG_LOG10 + 1]; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1689 | |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1690 | assert(base > 1); |
Gavin Howard | 3fb24fa | 2019-02-15 17:18:57 -0700 | [diff] [blame] | 1691 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1692 | if (BC_NUM_ZERO(n)) { |
| 1693 | print(0, len, false); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1694 | return BC_STATUS_SUCCESS; |
Gavin Howard | 48af52e | 2018-10-30 14:47:38 -0600 | [diff] [blame] | 1695 | } |
Gavin Howard | 9a4b6cd | 2018-10-23 15:13:30 -0600 | [diff] [blame] | 1696 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1697 | bc_vec_init(&stack, sizeof(BcBigDig), NULL); |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1698 | bc_num_init(&fracp1, n->rdx); |
| 1699 | bc_num_init(&fracp2, n->rdx); |
Gavin Howard | c4e9a08 | 2019-05-07 11:12:50 -0600 | [diff] [blame] | 1700 | bc_num_setup(&digit, digit_digs, sizeof(digit_digs) / sizeof(BcDig)); |
Gavin Howard | f114882 | 2019-05-11 15:29:24 -0600 | [diff] [blame] | 1701 | bc_num_init(&flen1, BC_NUM_BIGDIG_LOG10 + 1); |
| 1702 | bc_num_init(&flen2, BC_NUM_BIGDIG_LOG10 + 1); |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1703 | bc_num_one(&flen1); |
Gavin Howard | c4e9a08 | 2019-05-07 11:12:50 -0600 | [diff] [blame] | 1704 | bc_num_createCopy(&intp1, n); |
Gavin Howard | a50fc54 | 2018-03-29 17:25:38 -0600 | [diff] [blame] | 1705 | |
Gavin Howard | c4e9a08 | 2019-05-07 11:12:50 -0600 | [diff] [blame] | 1706 | bc_num_truncate(&intp1, intp1.scale); |
| 1707 | bc_num_init(&intp2, intp1.len); |
| 1708 | |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1709 | s = bc_num_sub(n, &intp1, &fracp1, 0); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1710 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1711 | |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1712 | n1 = &intp1; |
| 1713 | n2 = &intp2; |
Gavin Howard | c4e9a08 | 2019-05-07 11:12:50 -0600 | [diff] [blame] | 1714 | |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1715 | while (BC_NO_SIG && BC_NUM_NONZERO(n1)) { |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1716 | |
| 1717 | // Dividing by base cannot be divide by 0 because base cannot be 0. |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1718 | s = bc_num_divArray(n1, base, n2, &dig); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1719 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1720 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1721 | bc_vec_push(&stack, &dig); |
Gavin Howard | c4e9a08 | 2019-05-07 11:12:50 -0600 | [diff] [blame] | 1722 | |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1723 | temp = n1; |
| 1724 | n1 = n2; |
| 1725 | n2 = temp; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1726 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1727 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1728 | if (BC_SIG) goto sig_err; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1729 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1730 | for (i = 0; BC_NO_SIG && i < stack.len; ++i) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1731 | ptr = bc_vec_item_rev(&stack, i); |
| 1732 | assert(ptr); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1733 | print(*ptr, len, false); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1734 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1735 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1736 | if (BC_SIG) goto sig_err; |
Stefan Esser | 276de8c | 2019-05-04 20:12:43 +0200 | [diff] [blame] | 1737 | if (!n->scale) goto err; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1738 | |
Gavin Howard | fbae8a5 | 2019-05-07 11:15:38 -0600 | [diff] [blame] | 1739 | radix = true; |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1740 | n1 = &flen1; |
| 1741 | n2 = &flen2; |
Gavin Howard | fbae8a5 | 2019-05-07 11:15:38 -0600 | [diff] [blame] | 1742 | |
Gavin Howard | 06fee0c | 2019-05-09 14:43:20 -0600 | [diff] [blame] | 1743 | while (BC_NO_SIG && bc_num_intDigits(n1) < n->scale + 1) { |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1744 | |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1745 | bc_num_expand(&fracp2, fracp1.len + 1); |
| 1746 | s = bc_num_mulArray(&fracp1, base, &fracp2); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1747 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1748 | fracp2.scale = n->scale; |
| 1749 | fracp2.rdx = BC_NUM_RDX(fracp2.scale); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 1750 | |
| 1751 | // Will never fail (except for signals) because fracp is |
| 1752 | // guaranteed to be non-negative and small enough. |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1753 | s = bc_num_bigdig(&fracp2, &dig); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1754 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1755 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1756 | bc_num_bigdig2num(&digit, dig); |
Gavin Howard | 43b6fd7 | 2019-05-07 11:39:19 -0600 | [diff] [blame] | 1757 | s = bc_num_sub(&fracp2, &digit, &fracp1, 0); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1758 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1759 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1760 | print(dig, len, radix); |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1761 | s = bc_num_mulArray(n1, base, n2); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 1762 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | fbae8a5 | 2019-05-07 11:15:38 -0600 | [diff] [blame] | 1763 | |
| 1764 | radix = false; |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1765 | temp = n1; |
| 1766 | n1 = n2; |
| 1767 | n2 = temp; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1768 | } |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1769 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1770 | sig_err: |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1771 | if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1772 | err: |
Gavin Howard | c4e9a08 | 2019-05-07 11:12:50 -0600 | [diff] [blame] | 1773 | bc_num_free(&intp2); |
Gavin Howard | ea8d513 | 2019-05-07 11:52:04 -0600 | [diff] [blame] | 1774 | bc_num_free(&intp1); |
Gavin Howard | f114882 | 2019-05-11 15:29:24 -0600 | [diff] [blame] | 1775 | bc_num_free(&flen2); |
| 1776 | bc_num_free(&flen1); |
Gavin Howard | a8c2e58 | 2019-05-07 11:33:10 -0600 | [diff] [blame] | 1777 | bc_num_free(&fracp2); |
| 1778 | bc_num_free(&fracp1); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1779 | bc_vec_free(&stack); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1780 | return s; |
Gavin Howard | 2682a1f | 2018-03-03 09:09:13 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1783 | static BcStatus bc_num_printBase(BcNum *restrict n, BcBigDig base) { |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1784 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1785 | BcStatus s; |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 1786 | size_t width; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1787 | BcNumDigitOp print; |
| 1788 | bool neg = n->neg; |
| 1789 | |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1790 | if (neg) bc_num_putchar('-'); |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1791 | |
| 1792 | n->neg = false; |
| 1793 | |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1794 | if (base <= BC_NUM_MAX_POSIX_IBASE) { |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1795 | width = 1; |
| 1796 | print = bc_num_printHex; |
| 1797 | } |
| 1798 | else { |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1799 | width = bc_num_log10(base - 1); |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1800 | print = bc_num_printDigits; |
| 1801 | } |
| 1802 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1803 | s = bc_num_printNum(n, base, width, print); |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1804 | n->neg = neg; |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1805 | |
| 1806 | return s; |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1807 | } |
| 1808 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 1809 | #if DC_ENABLED |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1810 | BcStatus bc_num_stream(BcNum *restrict n, BcBigDig base) { |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1811 | return bc_num_printNum(n, base, 1, bc_num_printChar); |
Gavin Howard | 83eb839 | 2018-10-09 01:21:19 -0600 | [diff] [blame] | 1812 | } |
| 1813 | #endif // DC_ENABLED |
| 1814 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1815 | 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] | 1816 | assert(n); |
| 1817 | n->num = num; |
| 1818 | n->cap = cap; |
Gavin Howard | fcf2901 | 2019-04-25 20:12:19 -0600 | [diff] [blame] | 1819 | n->rdx = n->scale = n->len = 0; |
Gavin Howard | b8a1292 | 2018-12-21 21:40:45 -0700 | [diff] [blame] | 1820 | n->neg = false; |
| 1821 | } |
| 1822 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1823 | void bc_num_init(BcNum *restrict n, size_t req) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1824 | assert(n); |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 1825 | req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; |
Gavin Howard | 404ece7 | 2019-04-26 15:59:15 -0600 | [diff] [blame] | 1826 | bc_num_setup(n, bc_vm_malloc(BC_NUM_SIZE(req)), req); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1827 | } |
| 1828 | |
Gavin Howard | ed392aa | 2018-02-27 13:09:26 -0700 | [diff] [blame] | 1829 | void bc_num_free(void *num) { |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 1830 | assert(num); |
| 1831 | free(((BcNum*) num)->num); |
Gavin Howard | b5c7721 | 2018-02-14 17:12:34 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1834 | void bc_num_copy(BcNum *d, const BcNum *s) { |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1835 | assert(d && s); |
Gavin Howard | 7344ba7 | 2019-01-03 13:37:27 -0700 | [diff] [blame] | 1836 | if (d == s) return; |
Gavin Howard | dcff3c9 | 2019-01-09 10:04:56 -0700 | [diff] [blame] | 1837 | bc_num_expand(d, s->len); |
Gavin Howard | 7344ba7 | 2019-01-03 13:37:27 -0700 | [diff] [blame] | 1838 | d->len = s->len; |
| 1839 | d->neg = s->neg; |
| 1840 | d->rdx = s->rdx; |
Gavin Howard | fcf2901 | 2019-04-25 20:12:19 -0600 | [diff] [blame] | 1841 | d->scale = s->scale; |
Gavin Howard | 404ece7 | 2019-04-26 15:59:15 -0600 | [diff] [blame] | 1842 | memcpy(d->num, s->num, BC_NUM_SIZE(d->len)); |
Gavin Howard | 5a049c4 | 2018-02-15 11:24:11 -0700 | [diff] [blame] | 1843 | } |
| 1844 | |
Gavin Howard | baa4f58 | 2019-01-24 13:56:35 -0700 | [diff] [blame] | 1845 | void bc_num_createCopy(BcNum *d, const BcNum *s) { |
| 1846 | bc_num_init(d, s->len); |
| 1847 | bc_num_copy(d, s); |
| 1848 | } |
| 1849 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1850 | void bc_num_createFromBigdig(BcNum *n, BcBigDig val) { |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1851 | bc_num_init(n, (BC_NUM_BIGDIG_LOG10 - 1) / BC_BASE_DIGS + 1); |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1852 | bc_num_bigdig2num(n, val); |
Gavin Howard | baa4f58 | 2019-01-24 13:56:35 -0700 | [diff] [blame] | 1853 | } |
| 1854 | |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1855 | size_t bc_num_scale(const BcNum *restrict n) { |
Gavin Howard | fcf2901 | 2019-04-25 20:12:19 -0600 | [diff] [blame] | 1856 | return n->scale; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1857 | } |
| 1858 | |
| 1859 | size_t bc_num_len(const BcNum *restrict n) { |
| 1860 | |
Gavin Howard | 30ed640 | 2019-05-14 09:00:55 -0600 | [diff] [blame^] | 1861 | size_t len = n->len; |
Gavin Howard | fcf2901 | 2019-04-25 20:12:19 -0600 | [diff] [blame] | 1862 | |
Gavin Howard | b41483b | 2019-04-27 08:30:10 -0600 | [diff] [blame] | 1863 | if (BC_NUM_ZERO(n)) return 0; |
Gavin Howard | 7106c25 | 2019-05-14 08:54:07 -0600 | [diff] [blame] | 1864 | if (n->rdx == len) { |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1865 | |
Gavin Howard | 30ed640 | 2019-05-14 09:00:55 -0600 | [diff] [blame^] | 1866 | size_t zero, scale; |
| 1867 | |
Gavin Howard | 7106c25 | 2019-05-14 08:54:07 -0600 | [diff] [blame] | 1868 | len = bc_num_nonzeroLen(n); |
Gavin Howard | b41483b | 2019-04-27 08:30:10 -0600 | [diff] [blame] | 1869 | |
Gavin Howard | 7106c25 | 2019-05-14 08:54:07 -0600 | [diff] [blame] | 1870 | scale = n->scale % BC_BASE_DIGS; |
| 1871 | scale = scale ? scale : BC_BASE_DIGS; |
Gavin Howard | 0c593dd | 2019-05-14 08:50:09 -0600 | [diff] [blame] | 1872 | |
Gavin Howard | 7106c25 | 2019-05-14 08:54:07 -0600 | [diff] [blame] | 1873 | zero = bc_num_zeroDigits(n->num + len - 1); |
| 1874 | |
| 1875 | len = len * BC_BASE_DIGS - zero - (BC_BASE_DIGS - scale); |
| 1876 | } |
| 1877 | else len = bc_num_intDigits(n) + n->scale; |
| 1878 | |
| 1879 | return len; |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1882 | BcStatus bc_num_parse(BcNum *restrict n, const char *restrict val, |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1883 | BcBigDig base, bool letter) |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 1884 | { |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1885 | BcStatus s = BC_STATUS_SUCCESS; |
| 1886 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1887 | assert(n && val && base); |
Gavin Howard | e97ae92 | 2019-02-25 21:47:17 -0700 | [diff] [blame] | 1888 | assert(BC_ENABLE_EXTRA_MATH || |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1889 | (base >= BC_NUM_MIN_BASE && base <= vm->max_ibase)); |
Gavin Howard | c90ed90 | 2019-01-02 13:07:49 -0700 | [diff] [blame] | 1890 | assert(bc_num_strValid(val)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1891 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1892 | if (letter) { |
| 1893 | BcBigDig dig = bc_num_parseChar(val[0], BC_NUM_MAX_LBASE); |
| 1894 | bc_num_bigdig2num(n, dig); |
| 1895 | } |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1896 | else if (base == BC_BASE) bc_num_parseDecimal(n, val); |
| 1897 | else s = bc_num_parseBase(n, val, base); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1898 | |
| 1899 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1902 | BcStatus bc_num_print(BcNum *restrict n, BcBigDig base, bool newline) { |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1903 | |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1904 | BcStatus s = BC_STATUS_SUCCESS; |
| 1905 | |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1906 | assert(n); |
| 1907 | assert(BC_ENABLE_EXTRA_MATH || base >= BC_NUM_MIN_BASE); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1908 | |
Gavin Howard | ab5e963 | 2019-01-02 13:32:02 -0700 | [diff] [blame] | 1909 | bc_num_printNewline(); |
Gavin Howard | bc7cae8 | 2018-03-14 13:43:04 -0600 | [diff] [blame] | 1910 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 1911 | if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false); |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1912 | else if (base == BC_BASE) bc_num_printDecimal(n); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1913 | #if BC_ENABLE_EXTRA_MATH |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1914 | else if (base == 0 || base == 1) |
| 1915 | s = bc_num_printExponent(n, base != 0); |
Gavin Howard | 7ad5a66 | 2019-02-19 14:40:46 -0700 | [diff] [blame] | 1916 | #endif // BC_ENABLE_EXTRA_MATH |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 1917 | else s = bc_num_printBase(n, base); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1918 | |
Gavin Howard | b4a9cdb | 2019-05-07 15:25:51 -0600 | [diff] [blame] | 1919 | if (BC_NO_ERR(!s) && newline) bc_num_putchar('\n'); |
Gavin Howard | dc0ee9e | 2019-02-16 23:27:08 -0700 | [diff] [blame] | 1920 | |
| 1921 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1924 | BcStatus bc_num_bigdig(const BcNum *restrict n, BcBigDig *result) { |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1925 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1926 | size_t i; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1927 | BcBigDig r; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1928 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1929 | assert(n && result); |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1930 | |
Gavin Howard | ecafd4f | 2019-02-23 09:30:45 -0700 | [diff] [blame] | 1931 | if (BC_ERR(n->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE); |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1932 | |
Gavin Howard | 06b6e86 | 2019-02-15 11:15:31 -0700 | [diff] [blame] | 1933 | for (r = 0, i = n->len; i > n->rdx;) { |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 1934 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1935 | BcBigDig prev = r * BC_BASE_POW; |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 1936 | |
Gavin Howard | 4fb3720 | 2019-05-11 14:49:13 -0600 | [diff] [blame] | 1937 | if (BC_ERR(prev / BC_BASE_POW != r)) |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 1938 | return bc_vm_err(BC_ERROR_MATH_OVERFLOW); |
| 1939 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1940 | r = prev + (BcBigDig) n->num[--i]; |
Gavin Howard | 5bede41 | 2019-02-23 09:31:28 -0700 | [diff] [blame] | 1941 | |
Gavin Howard | 4fb3720 | 2019-05-11 14:49:13 -0600 | [diff] [blame] | 1942 | if (BC_ERR(r < prev)) return bc_vm_err(BC_ERROR_MATH_OVERFLOW); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1943 | } |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1944 | |
Gavin Howard | 7b557da | 2018-12-21 10:25:32 -0700 | [diff] [blame] | 1945 | *result = r; |
Gavin Howard | d3a1c39 | 2018-12-11 12:00:57 -0700 | [diff] [blame] | 1946 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1947 | return BC_STATUS_SUCCESS; |
Gavin Howard | 68b8a5c | 2018-02-15 11:41:24 -0700 | [diff] [blame] | 1948 | } |
| 1949 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1950 | void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val) { |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1951 | |
Gavin Howard | c25fd61 | 2019-04-26 19:31:31 -0600 | [diff] [blame] | 1952 | BcDig *ptr; |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1953 | size_t i; |
Gavin Howard | c25fd61 | 2019-04-26 19:31:31 -0600 | [diff] [blame] | 1954 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1955 | assert(n); |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1956 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 1957 | bc_num_zero(n); |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1958 | |
Gavin Howard | 1ab22d2 | 2019-01-03 13:32:17 -0700 | [diff] [blame] | 1959 | if (!val) return; |
Gavin Howard | 8e2cc69 | 2018-02-15 17:39:14 -0700 | [diff] [blame] | 1960 | |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 1961 | bc_num_expand(n, BC_NUM_BIGDIG_LOG10); |
Gavin Howard | 05feab0 | 2019-04-23 16:24:07 -0600 | [diff] [blame] | 1962 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 1963 | for (ptr = n->num, i = 0; val; ++i, ++n->len, val /= BC_BASE_POW) |
| 1964 | ptr[i] = val % BC_BASE_POW; |
Gavin Howard | 025d04d | 2018-02-20 13:53:28 -0700 | [diff] [blame] | 1965 | } |
| 1966 | |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1967 | size_t bc_num_addReq(BcNum *a, BcNum *b, size_t scale) { |
Gavin Howard | 1973d61 | 2019-02-21 15:37:32 -0700 | [diff] [blame] | 1968 | |
| 1969 | size_t aint, bint, ardx, brdx; |
| 1970 | |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1971 | BC_UNUSED(scale); |
Gavin Howard | 1973d61 | 2019-02-21 15:37:32 -0700 | [diff] [blame] | 1972 | |
| 1973 | ardx = a->rdx; |
| 1974 | brdx = b->rdx; |
Gavin Howard | 7b7d2f3 | 2019-02-21 16:46:47 -0700 | [diff] [blame] | 1975 | aint = bc_num_int(a); |
| 1976 | bint = bc_num_int(b); |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1977 | ardx = BC_MAX(ardx, brdx); |
| 1978 | aint = BC_MAX(aint, bint); |
Gavin Howard | 1973d61 | 2019-02-21 15:37:32 -0700 | [diff] [blame] | 1979 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1980 | return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1); |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | size_t bc_num_mulReq(BcNum *a, BcNum *b, size_t scale) { |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1984 | size_t max, rdx; |
| 1985 | rdx = bc_vm_growSize(a->rdx, b->rdx); |
Gavin Howard | d788327 | 2019-05-10 20:04:23 -0600 | [diff] [blame] | 1986 | max = BC_NUM_RDX(scale); |
| 1987 | max = bc_vm_growSize(BC_MAX(max, rdx), 1); |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 1988 | rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max); |
| 1989 | return rdx; |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
| 1992 | size_t bc_num_powReq(BcNum *a, BcNum *b, size_t scale) { |
| 1993 | BC_UNUSED(scale); |
Gavin Howard | 007afdf | 2019-02-23 09:45:42 -0700 | [diff] [blame] | 1994 | return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1); |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | #if BC_ENABLE_EXTRA_MATH |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 1998 | size_t bc_num_placesReq(BcNum *a, BcNum *b, size_t scale) { |
| 1999 | |
| 2000 | BcStatus s; |
Gavin Howard | ea1ae3c | 2019-05-10 18:44:52 -0600 | [diff] [blame] | 2001 | BcBigDig places = 0; |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2002 | size_t rdx; |
| 2003 | |
| 2004 | BC_UNUSED(s); |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 2005 | BC_UNUSED(scale); |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2006 | |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2007 | // This error will be taken care of later. Ignore. |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 2008 | s = bc_num_bigdig(b, &places); |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2009 | |
| 2010 | if (a->scale <= places) rdx = BC_NUM_RDX(places); |
| 2011 | else rdx = BC_NUM_RDX(a->scale - places); |
| 2012 | |
Gavin Howard | 06fee0c | 2019-05-09 14:43:20 -0600 | [diff] [blame] | 2013 | return BC_NUM_RDX(bc_num_intDigits(a)) + rdx; |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | size_t bc_num_shiftLeftReq(BcNum *a, BcNum *b, size_t scale) { |
| 2017 | |
| 2018 | BcStatus s; |
Gavin Howard | 687cf4d | 2019-05-11 08:55:36 -0600 | [diff] [blame] | 2019 | BcBigDig places = 0; |
| 2020 | size_t rdx; |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2021 | |
| 2022 | BC_UNUSED(s); |
| 2023 | BC_UNUSED(scale); |
| 2024 | |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2025 | // This error will be taken care of later. Ignore. |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 2026 | s = bc_num_bigdig(b, &places); |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2027 | |
| 2028 | if (a->scale <= places) rdx = BC_NUM_RDX(places) - a->rdx + 1; |
| 2029 | else rdx = 0; |
| 2030 | |
| 2031 | return a->len + rdx; |
| 2032 | } |
| 2033 | |
| 2034 | size_t bc_num_shiftRightReq(BcNum *a, BcNum *b, size_t scale) { |
| 2035 | |
| 2036 | BcStatus s; |
Gavin Howard | 687cf4d | 2019-05-11 08:55:36 -0600 | [diff] [blame] | 2037 | BcBigDig places = 0; |
| 2038 | size_t int_digs, rdx; |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2039 | |
| 2040 | BC_UNUSED(s); |
| 2041 | BC_UNUSED(scale); |
| 2042 | |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2043 | // This error will be taken care of later. Ignore. |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 2044 | s = bc_num_bigdig(b, &places); |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2045 | |
Gavin Howard | 06fee0c | 2019-05-09 14:43:20 -0600 | [diff] [blame] | 2046 | int_digs = BC_NUM_RDX(bc_num_intDigits(a)); |
Gavin Howard | 194380e | 2019-05-06 08:39:30 -0600 | [diff] [blame] | 2047 | rdx = BC_NUM_RDX(places); |
| 2048 | |
| 2049 | if (int_digs <= rdx) rdx -= int_digs; |
| 2050 | else rdx = 0; |
| 2051 | |
| 2052 | return a->len + rdx; |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 2053 | } |
| 2054 | #endif // BC_ENABLE_EXTRA_MATH |
| 2055 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 2056 | 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] | 2057 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_a : bc_num_s; |
Gavin Howard | 4185296 | 2018-12-27 17:15:00 -0700 | [diff] [blame] | 2058 | BC_UNUSED(scale); |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 2059 | return bc_num_binary(a, b, c, false, op, bc_num_addReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 2060 | } |
| 2061 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 2062 | 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] | 2063 | BcNumBinaryOp op = (!a->neg == !b->neg) ? bc_num_s : bc_num_a; |
Gavin Howard | 4185296 | 2018-12-27 17:15:00 -0700 | [diff] [blame] | 2064 | BC_UNUSED(scale); |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 2065 | return bc_num_binary(a, b, c, true, op, bc_num_addReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 2066 | } |
| 2067 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 2068 | BcStatus bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 2069 | return bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 2072 | BcStatus bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | d18fa6d | 2019-01-24 13:49:35 -0700 | [diff] [blame] | 2073 | return bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 2074 | } |
| 2075 | |
Gavin Howard | 54b946a | 2018-10-23 12:06:57 -0600 | [diff] [blame] | 2076 | BcStatus bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 2077 | size_t req = bc_num_mulReq(a, b, scale); |
| 2078 | return bc_num_binary(a, b, c, scale, bc_num_rem, req); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 2079 | } |
| 2080 | |
Gavin Howard | 12fe781 | 2018-09-29 03:45:18 -0600 | [diff] [blame] | 2081 | BcStatus bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 6734e1a | 2019-02-21 17:11:52 -0700 | [diff] [blame] | 2082 | return bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale)); |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 2083 | } |
| 2084 | |
Gavin Howard | 7d8f038 | 2018-12-27 16:15:13 -0700 | [diff] [blame] | 2085 | #if BC_ENABLE_EXTRA_MATH |
| 2086 | BcStatus bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 8291ecd | 2019-05-06 14:34:53 -0600 | [diff] [blame] | 2087 | size_t req = bc_num_placesReq(a, b, scale); |
| 2088 | return bc_num_binary(a, b, c, scale, bc_num_place, req); |
Gavin Howard | 7d8f038 | 2018-12-27 16:15:13 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
| 2091 | BcStatus bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 8291ecd | 2019-05-06 14:34:53 -0600 | [diff] [blame] | 2092 | size_t req = bc_num_shiftLeftReq(a, b, scale); |
| 2093 | return bc_num_binary(a, b, c, scale, bc_num_left, req); |
Gavin Howard | 7d8f038 | 2018-12-27 16:15:13 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | BcStatus bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) { |
Gavin Howard | 8291ecd | 2019-05-06 14:34:53 -0600 | [diff] [blame] | 2097 | size_t req = bc_num_shiftRightReq(a, b, scale); |
| 2098 | return bc_num_binary(a, b, c, scale, bc_num_right, req); |
Gavin Howard | 7d8f038 | 2018-12-27 16:15:13 -0700 | [diff] [blame] | 2099 | } |
| 2100 | #endif // BC_ENABLE_EXTRA_MATH |
| 2101 | |
Gavin Howard | 3c02ed3 | 2018-12-28 18:17:15 -0700 | [diff] [blame] | 2102 | 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] | 2103 | |
Gavin Howard | 1cbfe24 | 2019-01-09 17:13:11 -0700 | [diff] [blame] | 2104 | BcStatus s = BC_STATUS_SUCCESS; |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 2105 | BcNum num1, num2, half, f, fprime, *x0, *x1, *temp; |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2106 | size_t pow, len, rdx, req, digs, digs1, digs2, resscale, times = 0; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 2107 | ssize_t cmp = 1, cmp1 = SSIZE_MAX, cmp2 = SSIZE_MAX; |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2108 | BcDig half_digs[1]; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2109 | |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 2110 | assert(a && b && a != b); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2111 | |
Gavin Howard | ecafd4f | 2019-02-23 09:30:45 -0700 | [diff] [blame] | 2112 | if (BC_ERR(a->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE); |
Gavin Howard | 07fbf01 | 2019-02-19 09:25:11 -0700 | [diff] [blame] | 2113 | |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2114 | if (a->scale > scale) scale = a->scale; |
Gavin Howard | 06fee0c | 2019-05-09 14:43:20 -0600 | [diff] [blame] | 2115 | len = bc_vm_growSize(bc_num_intDigits(a), 1); |
Gavin Howard | eec0728 | 2019-05-10 20:10:18 -0600 | [diff] [blame] | 2116 | rdx = BC_NUM_RDX(scale); |
| 2117 | req = bc_vm_growSize(BC_MAX(rdx, a->rdx), len >> 1); |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 2118 | bc_num_init(b, bc_vm_growSize(req, 1)); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2119 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 2120 | if (BC_NUM_ZERO(a)) { |
Gavin Howard | f8ddb6d | 2018-10-22 12:58:53 -0600 | [diff] [blame] | 2121 | bc_num_setToZero(b, scale); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 2122 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2123 | } |
Gavin Howard | b2f01a4 | 2019-05-10 20:00:32 -0600 | [diff] [blame] | 2124 | if (BC_NUM_ONE(a)) { |
Gavin Howard | 757b66a | 2018-10-06 04:13:46 -0600 | [diff] [blame] | 2125 | bc_num_one(b); |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 2126 | bc_num_extend(b, scale); |
| 2127 | return BC_STATUS_SUCCESS; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2128 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2129 | |
Gavin Howard | eec0728 | 2019-05-10 20:10:18 -0600 | [diff] [blame] | 2130 | rdx = BC_NUM_RDX(scale); |
| 2131 | rdx = BC_MAX(rdx, a->rdx); |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2132 | len = bc_vm_growSize(a->len, rdx); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2133 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 2134 | bc_num_init(&num1, len); |
| 2135 | bc_num_init(&num2, len); |
Gavin Howard | 18f4020 | 2018-12-29 21:30:37 -0700 | [diff] [blame] | 2136 | bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig)); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2137 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2138 | bc_num_one(&half); |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 2139 | half.num[0] = BC_BASE_POW / 2; |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2140 | half.len = 1; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2141 | half.rdx = 1; |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2142 | half.scale = 1; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2143 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 2144 | bc_num_init(&f, len); |
| 2145 | bc_num_init(&fprime, len); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2146 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2147 | x0 = &num1; |
| 2148 | x1 = &num2; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2149 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2150 | bc_num_one(x0); |
Gavin Howard | 06fee0c | 2019-05-09 14:43:20 -0600 | [diff] [blame] | 2151 | pow = bc_num_intDigits(a); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2152 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 2153 | if (pow) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2154 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 2155 | if (pow & 1) x0->num[0] = 2; |
| 2156 | else x0->num[0] = 6; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2157 | |
Gavin Howard | c39fd49 | 2018-10-04 10:07:03 -0600 | [diff] [blame] | 2158 | pow -= 2 - (pow & 1); |
Gavin Howard | 7470457 | 2019-05-07 13:48:48 -0600 | [diff] [blame] | 2159 | s = bc_num_shiftLeft(x0, pow / 2); |
| 2160 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2161 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2162 | |
Gavin Howard | eec0728 | 2019-05-10 20:10:18 -0600 | [diff] [blame] | 2163 | x0->scale = x0->rdx = digs = digs1 = digs2 = 0; |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 2164 | resscale = (scale + BC_BASE_DIGS) * 2; |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2165 | |
Gavin Howard | 06fee0c | 2019-05-09 14:43:20 -0600 | [diff] [blame] | 2166 | len = BC_NUM_RDX(bc_num_intDigits(x0) + resscale - 1); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2167 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 2168 | while (BC_NO_SIG && (cmp || digs < len)) { |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2169 | |
Gavin Howard | 971a267 | 2019-04-26 14:32:07 -0600 | [diff] [blame] | 2170 | assert(BC_NUM_NONZERO(x0)); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 2171 | |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2172 | s = bc_num_div(a, x0, &f, resscale); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 2173 | assert(!s || s == BC_STATUS_SIGNAL); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 2174 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2175 | s = bc_num_add(x0, &f, &fprime, resscale); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 2176 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2177 | s = bc_num_mul(&fprime, &half, x1, resscale); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 2178 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2179 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 2180 | cmp = bc_num_cmp(x1, x0); |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 2181 | |
| 2182 | #if BC_ENABLE_SIGNALS |
Gavin Howard | a36f031 | 2019-05-09 10:23:07 -0600 | [diff] [blame] | 2183 | if (cmp == BC_NUM_CMP_SIGNAL) { |
Gavin Howard | f2ee634 | 2019-04-09 17:05:20 -0600 | [diff] [blame] | 2184 | s = BC_STATUS_SIGNAL; |
| 2185 | break; |
| 2186 | } |
Gavin Howard | 3378af8 | 2019-05-11 08:29:39 -0600 | [diff] [blame] | 2187 | #endif // BC_ENABLE_SIGNALS |
Gavin Howard | f2ee634 | 2019-04-09 17:05:20 -0600 | [diff] [blame] | 2188 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 2189 | digs = x1->len - (unsigned long long) llabs(cmp); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2190 | |
Gavin Howard | b5ec7f3 | 2018-10-29 12:32:39 -0600 | [diff] [blame] | 2191 | if (cmp == cmp2 && digs == digs1) times += 1; |
Gavin Howard | b11e9e2 | 2018-10-06 17:26:02 -0600 | [diff] [blame] | 2192 | else times = 0; |
| 2193 | |
Gavin Howard | eec0728 | 2019-05-10 20:10:18 -0600 | [diff] [blame] | 2194 | resscale += (times > 2); |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 2195 | |
| 2196 | cmp2 = cmp1; |
| 2197 | cmp1 = cmp; |
Gavin Howard | b5ec7f3 | 2018-10-29 12:32:39 -0600 | [diff] [blame] | 2198 | digs1 = digs; |
Gavin Howard | 9f2395b | 2018-10-06 05:28:58 -0600 | [diff] [blame] | 2199 | |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2200 | temp = x0; |
| 2201 | x0 = x1; |
| 2202 | x1 = temp; |
| 2203 | } |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2204 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 2205 | if (BC_SIG) { |
Gavin Howard | 176cfe6 | 2019-02-16 23:40:13 -0700 | [diff] [blame] | 2206 | s = BC_STATUS_SIGNAL; |
| 2207 | goto err; |
| 2208 | } |
Gavin Howard | 0dfe292 | 2018-05-22 13:57:02 -0600 | [diff] [blame] | 2209 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 2210 | bc_num_copy(b, x0); |
Stefan Eßer | 4276af4 | 2019-04-29 14:04:57 -0600 | [diff] [blame] | 2211 | if (b->scale > scale) bc_num_truncate(b, b->scale - scale); |
Gavin Howard | 954276b | 2018-05-16 01:43:58 -0600 | [diff] [blame] | 2212 | |
| 2213 | err: |
Gavin Howard | cf0748d | 2019-04-09 14:51:47 -0600 | [diff] [blame] | 2214 | if (BC_ERR(s)) bc_num_free(b); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2215 | bc_num_free(&fprime); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2216 | bc_num_free(&f); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2217 | bc_num_free(&num2); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2218 | bc_num_free(&num1); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 2219 | assert(!b->neg || BC_NUM_NONZERO(b)); |
Gavin Howard | c4bf4c4 | 2019-01-09 10:30:15 -0700 | [diff] [blame] | 2220 | assert(b->rdx <= b->len || !b->len); |
Gavin Howard | 6373820 | 2018-09-26 15:34:20 -0600 | [diff] [blame] | 2221 | return s; |
Gavin Howard | 3eb626f | 2018-02-14 13:54:35 -0700 | [diff] [blame] | 2222 | } |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2223 | |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 2224 | BcStatus bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) { |
| 2225 | |
| 2226 | BcStatus s; |
| 2227 | BcNum num2, *ptr_a; |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 2228 | bool init = false; |
Stefan Eßer | a7fbbf6 | 2019-04-29 14:14:30 -0600 | [diff] [blame] | 2229 | size_t ts, len; |
| 2230 | |
| 2231 | ts = BC_MAX(scale + b->scale, a->scale); |
| 2232 | len = bc_num_mulReq(a, b, ts); |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 2233 | |
Gavin Howard | f2ef2f3 | 2019-01-16 11:25:16 -0700 | [diff] [blame] | 2234 | assert(c != d && a != d && b != d && b != c); |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 2235 | |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 2236 | if (c == a) { |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 2237 | memcpy(&num2, c, sizeof(BcNum)); |
| 2238 | ptr_a = &num2; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 2239 | bc_num_init(c, len); |
Gavin Howard | 890d0c0 | 2018-10-30 16:34:50 -0600 | [diff] [blame] | 2240 | init = true; |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 2241 | } |
Gavin Howard | f679ad8 | 2018-10-29 12:26:30 -0600 | [diff] [blame] | 2242 | else { |
| 2243 | ptr_a = a; |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 2244 | bc_num_expand(c, len); |
Gavin Howard | f679ad8 | 2018-10-29 12:26:30 -0600 | [diff] [blame] | 2245 | } |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 2246 | |
Gavin Howard | f596178 | 2019-05-09 07:50:46 -0600 | [diff] [blame] | 2247 | if (BC_NUM_NONZERO(a) && !a->rdx && !b->rdx && b->len == 1 && !scale) { |
Gavin Howard | 2956a5e | 2019-05-08 08:04:06 -0600 | [diff] [blame] | 2248 | BcBigDig rem; |
Gavin Howard | 9d771ba | 2019-05-08 09:09:25 -0600 | [diff] [blame] | 2249 | s = bc_num_divArray(ptr_a, (BcBigDig) b->num[0], c, &rem); |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 2250 | assert(rem < BC_BASE_POW); |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 2251 | d->num[0] = (BcDig) rem; |
Gavin Howard | f596178 | 2019-05-09 07:50:46 -0600 | [diff] [blame] | 2252 | d->len = (rem != 0); |
Gavin Howard | b3f1ee2 | 2019-05-07 21:49:15 -0600 | [diff] [blame] | 2253 | } |
Gavin Howard | f596178 | 2019-05-09 07:50:46 -0600 | [diff] [blame] | 2254 | else s = bc_num_r(ptr_a, b, c, d, scale, ts); |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 2255 | |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 2256 | assert(!c->neg || BC_NUM_NONZERO(c)); |
Gavin Howard | c4bf4c4 | 2019-01-09 10:30:15 -0700 | [diff] [blame] | 2257 | assert(c->rdx <= c->len || !c->len); |
Gavin Howard | ddce6d4 | 2019-01-03 14:07:06 -0700 | [diff] [blame] | 2258 | assert(!d->neg || BC_NUM_NONZERO(d)); |
Gavin Howard | c4bf4c4 | 2019-01-09 10:30:15 -0700 | [diff] [blame] | 2259 | assert(d->rdx <= d->len || !d->len); |
Gavin Howard | 2cb3961 | 2018-10-22 08:46:48 -0600 | [diff] [blame] | 2260 | |
| 2261 | if (init) bc_num_free(&num2); |
| 2262 | |
| 2263 | return s; |
| 2264 | } |
| 2265 | |
Gavin Howard | 40a085f | 2018-12-03 12:08:59 -0700 | [diff] [blame] | 2266 | #if DC_ENABLED |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 2267 | 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] | 2268 | |
| 2269 | BcStatus s; |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 2270 | BcNum base, exp, two, temp; |
Gavin Howard | d392983 | 2018-12-24 15:13:15 -0700 | [diff] [blame] | 2271 | BcDig two_digs[2]; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2272 | |
Gavin Howard | 34edd0a | 2018-10-25 15:51:05 -0600 | [diff] [blame] | 2273 | assert(a && b && c && d && a != d && b != d && c != d); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2274 | |
Gavin Howard | ecafd4f | 2019-02-23 09:30:45 -0700 | [diff] [blame] | 2275 | if (BC_ERR(BC_NUM_ZERO(c))) |
| 2276 | return bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO); |
| 2277 | if (BC_ERR(b->neg)) return bc_vm_err(BC_ERROR_MATH_NEGATIVE); |
| 2278 | if (BC_ERR(a->rdx || b->rdx || c->rdx)) |
Gavin Howard | 7536dcf | 2018-12-15 19:27:09 -0700 | [diff] [blame] | 2279 | return bc_vm_err(BC_ERROR_MATH_NON_INTEGER); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2280 | |
Gavin Howard | ad0ecfe | 2018-10-30 01:16:01 -0600 | [diff] [blame] | 2281 | bc_num_expand(d, c->len); |
| 2282 | bc_num_init(&base, c->len); |
Gavin Howard | 7fdc30a | 2018-12-29 21:31:21 -0700 | [diff] [blame] | 2283 | bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig)); |
Gavin Howard | 303572b | 2019-05-09 07:50:56 -0600 | [diff] [blame] | 2284 | bc_num_init(&temp, b->len + 1); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2285 | |
| 2286 | bc_num_one(&two); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2287 | two.num[0] = 2; |
| 2288 | bc_num_one(d); |
| 2289 | |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 2290 | // We already checked for 0. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 2291 | s = bc_num_rem(a, c, &base, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 2292 | assert(!s || s == BC_STATUS_SIGNAL); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 2293 | if (BC_ERROR_SIGNAL_ONLY(s)) goto rem_err; |
Gavin Howard | baa4f58 | 2019-01-24 13:56:35 -0700 | [diff] [blame] | 2294 | bc_num_createCopy(&exp, b); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2295 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 2296 | while (BC_NO_SIG && BC_NUM_NONZERO(&exp)) { |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2297 | |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 2298 | // Num two cannot be 0, so no errors. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 2299 | s = bc_num_divmod(&exp, &two, &exp, &temp, 0); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 2300 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2301 | |
Gavin Howard | b2f01a4 | 2019-05-10 20:00:32 -0600 | [diff] [blame] | 2302 | if (BC_NUM_ONE(&temp) && !temp.neg) { |
| 2303 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 2304 | s = bc_num_mul(d, &base, &temp, 0); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 2305 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 2306 | |
| 2307 | // We already checked for 0. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 2308 | s = bc_num_rem(&temp, c, d, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 2309 | assert(!s || s == BC_STATUS_SIGNAL); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 2310 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2311 | } |
| 2312 | |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 2313 | s = bc_num_mul(&base, &base, &temp, 0); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 2314 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 2315 | |
| 2316 | // We already checked for 0. |
Gavin Howard | 53eba8b | 2018-10-31 15:14:37 -0600 | [diff] [blame] | 2317 | s = bc_num_rem(&temp, c, &base, 0); |
Gavin Howard | 1769abc | 2019-02-21 09:36:14 -0700 | [diff] [blame] | 2318 | assert(!s || s == BC_STATUS_SIGNAL); |
Gavin Howard | c78e752 | 2019-02-22 13:24:25 -0700 | [diff] [blame] | 2319 | if (BC_ERROR_SIGNAL_ONLY(s)) goto err; |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2320 | } |
| 2321 | |
Gavin Howard | 2d188a5 | 2019-02-25 14:19:08 -0700 | [diff] [blame] | 2322 | if (BC_NO_ERR(!s) && BC_SIG) s = BC_STATUS_SIGNAL; |
Gavin Howard | fa4983a | 2019-02-16 23:43:03 -0700 | [diff] [blame] | 2323 | |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2324 | err: |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2325 | bc_num_free(&exp); |
Gavin Howard | d88164d | 2019-02-21 09:57:31 -0700 | [diff] [blame] | 2326 | rem_err: |
| 2327 | bc_num_free(&temp); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2328 | bc_num_free(&base); |
Gavin Howard | 3cf769e | 2018-10-11 13:55:11 -0600 | [diff] [blame] | 2329 | assert(!d->neg || d->len); |
Gavin Howard | ba00980 | 2018-09-29 04:41:51 -0600 | [diff] [blame] | 2330 | return s; |
| 2331 | } |
Gavin Howard | e6e8476 | 2018-10-03 11:46:34 -0600 | [diff] [blame] | 2332 | #endif // DC_ENABLED |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2333 | |
| 2334 | #if BC_DEBUG_CODE |
| 2335 | void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline) { |
| 2336 | printf("%s: ", name); |
| 2337 | bc_num_printDecimal(n); |
| 2338 | printf("\n"); |
| 2339 | if (emptyline) printf("\n"); |
Gavin Howard | e34f7d8 | 2019-05-07 10:32:39 -0600 | [diff] [blame] | 2340 | vm->nchars = 0; |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2341 | } |
| 2342 | |
Gavin Howard | 2a84f96 | 2019-05-08 17:20:09 -0600 | [diff] [blame] | 2343 | void bc_num_printDigs(const BcDig *n, size_t len, bool emptyline) { |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2344 | |
| 2345 | size_t i; |
| 2346 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 2347 | for (i = len - 1; i < len; --i) printf(" %0*d", BC_BASE_DIGS, n[i]); |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2348 | |
| 2349 | printf("\n"); |
| 2350 | if (emptyline) printf("\n"); |
Gavin Howard | e34f7d8 | 2019-05-07 10:32:39 -0600 | [diff] [blame] | 2351 | vm->nchars = 0; |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2352 | } |
| 2353 | |
Gavin Howard | 2a84f96 | 2019-05-08 17:20:09 -0600 | [diff] [blame] | 2354 | void bc_num_printWithDigs(const BcNum *n, const char *name, bool emptyline) { |
| 2355 | printf("%s len: %zu, rdx: %zu, scale: %zu\n", |
| 2356 | name, n->len, n->rdx, n->scale); |
| 2357 | bc_num_printDigs(n->num, n->len, emptyline); |
| 2358 | } |
| 2359 | |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2360 | void bc_num_dump(const char *varname, const BcNum *n) { |
| 2361 | |
| 2362 | unsigned long i, scale = n->scale; |
| 2363 | |
| 2364 | fprintf(stderr, "\n%s = %s", varname, n->len ? (n->neg ? "-" : "+") : "0 "); |
| 2365 | |
| 2366 | for (i = n->len - 1; i < n->len; --i) { |
| 2367 | |
| 2368 | if (i + 1 == n->rdx) fprintf(stderr, ". "); |
| 2369 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 2370 | if (scale / BC_BASE_DIGS != n->rdx - i - 1) |
| 2371 | fprintf(stderr, "%0*d ", BC_BASE_DIGS, n->num[i]); |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2372 | else { |
| 2373 | |
Gavin Howard | 92ba4e5 | 2019-05-10 20:49:13 -0600 | [diff] [blame] | 2374 | int mod = scale % BC_BASE_DIGS; |
| 2375 | int d = BC_BASE_DIGS - mod; |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2376 | BcDig div; |
| 2377 | |
| 2378 | if (mod != 0) { |
Gavin Howard | 2a84f96 | 2019-05-08 17:20:09 -0600 | [diff] [blame] | 2379 | div = n->num[i] / ((BcDig) bc_num_pow10[(unsigned long) d]); |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2380 | fprintf(stderr, "%0*d", (int) mod, div); |
| 2381 | } |
| 2382 | |
Gavin Howard | 2a84f96 | 2019-05-08 17:20:09 -0600 | [diff] [blame] | 2383 | div = n->num[i] % ((BcDig) bc_num_pow10[(unsigned long) d]); |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2384 | fprintf(stderr, " ' %0*d ", d, div); |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | fprintf(stderr, "(%zu | %zu.%zu / %zu) %p\n", |
| 2389 | n->scale, n->len, n->rdx, n->cap, (void*) n->num); |
Gavin Howard | e34f7d8 | 2019-05-07 10:32:39 -0600 | [diff] [blame] | 2390 | vm->nchars = 0; |
Gavin Howard | efc1e20 | 2019-05-06 08:01:07 -0600 | [diff] [blame] | 2391 | } |
| 2392 | #endif // BC_DEBUG_CODE |