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