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