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