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