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