1 /* 2 * ***************************************************************************** 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 * 6 * Copyright (c) 2018-2024 Gavin D. Howard and contributors. 7 * 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. 29 * 30 * ***************************************************************************** 31 * 32 * Code for the number type. 33 * 34 */ 35 36 #include <assert.h> 37 #include <ctype.h> 38 #include <stdbool.h> 39 #include <stdlib.h> 40 #include <string.h> 41 #include <setjmp.h> 42 #include <limits.h> 43 44 #include <num.h> 45 #include <rand.h> 46 #include <vm.h> 47 #if BC_ENABLE_LIBRARY 48 #include <library.h> 49 #endif // BC_ENABLE_LIBRARY 50 51 // Before you try to understand this code, see the development manual 52 // (manuals/development.md#numbers). 53 54 static void 55 bc_num_m(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale); 56 57 /** 58 * Multiply two numbers and throw a math error if they overflow. 59 * @param a The first operand. 60 * @param b The second operand. 61 * @return The product of the two operands. 62 */ 63 static inline size_t 64 bc_num_mulOverflow(size_t a, size_t b) 65 { 66 size_t res = a * b; 67 if (BC_ERR(BC_VM_MUL_OVERFLOW(a, b, res))) bc_err(BC_ERR_MATH_OVERFLOW); 68 return res; 69 } 70 71 /** 72 * Conditionally negate @a n based on @a neg. Algorithm taken from 73 * https://graphics.stanford.edu/~seander/bithacks.html#ConditionalNegate . 74 * @param n The value to turn into a signed value and negate. 75 * @param neg The condition to negate or not. 76 */ 77 static inline ssize_t 78 bc_num_neg(size_t n, bool neg) 79 { 80 return (((ssize_t) n) ^ -((ssize_t) neg)) + neg; 81 } 82 83 /** 84 * Compare a BcNum against zero. 85 * @param n The number to compare. 86 * @return -1 if the number is less than 0, 1 if greater, and 0 if equal. 87 */ 88 ssize_t 89 bc_num_cmpZero(const BcNum* n) 90 { 91 return bc_num_neg((n)->len != 0, BC_NUM_NEG(n)); 92 } 93 94 /** 95 * Return the number of integer limbs in a BcNum. This is the opposite of rdx. 96 * @param n The number to return the amount of integer limbs for. 97 * @return The amount of integer limbs in @a n. 98 */ 99 static inline size_t 100 bc_num_int(const BcNum* n) 101 { 102 return n->len ? n->len - BC_NUM_RDX_VAL(n) : 0; 103 } 104 105 /** 106 * Expand a number's allocation capacity to at least req limbs. 107 * @param n The number to expand. 108 * @param req The number limbs to expand the allocation capacity to. 109 */ 110 static void 111 bc_num_expand(BcNum* restrict n, size_t req) 112 { 113 assert(n != NULL); 114 115 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; 116 117 if (req > n->cap) 118 { 119 BC_SIG_LOCK; 120 121 n->num = bc_vm_realloc(n->num, BC_NUM_SIZE(req)); 122 n->cap = req; 123 124 BC_SIG_UNLOCK; 125 } 126 } 127 128 /** 129 * Set a number to 0 with the specified scale. 130 * @param n The number to set to zero. 131 * @param scale The scale to set the number to. 132 */ 133 static inline void 134 bc_num_setToZero(BcNum* restrict n, size_t scale) 135 { 136 assert(n != NULL); 137 n->scale = scale; 138 n->len = n->rdx = 0; 139 } 140 141 void 142 bc_num_zero(BcNum* restrict n) 143 { 144 bc_num_setToZero(n, 0); 145 } 146 147 void 148 bc_num_one(BcNum* restrict n) 149 { 150 bc_num_zero(n); 151 n->len = 1; 152 n->num[0] = 1; 153 } 154 155 /** 156 * "Cleans" a number, which means reducing the length if the most significant 157 * limbs are zero. 158 * @param n The number to clean. 159 */ 160 static void 161 bc_num_clean(BcNum* restrict n) 162 { 163 // Reduce the length. 164 while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) 165 { 166 n->len -= 1; 167 } 168 169 // Special cases. 170 if (BC_NUM_ZERO(n)) n->rdx = 0; 171 else 172 { 173 // len must be at least as much as rdx. 174 size_t rdx = BC_NUM_RDX_VAL(n); 175 if (n->len < rdx) n->len = rdx; 176 } 177 } 178 179 /** 180 * Returns the log base 10 of @a i. I could have done this with floating-point 181 * math, and in fact, I originally did. However, that was the only 182 * floating-point code in the entire codebase, and I decided I didn't want any. 183 * This is fast enough. Also, it might handle larger numbers better. 184 * @param i The number to return the log base 10 of. 185 * @return The log base 10 of @a i. 186 */ 187 static size_t 188 bc_num_log10(size_t i) 189 { 190 size_t len; 191 192 for (len = 1; i; i /= BC_BASE, ++len) 193 { 194 continue; 195 } 196 197 assert(len - 1 <= BC_BASE_DIGS + 1); 198 199 return len - 1; 200 } 201 202 /** 203 * Returns the number of decimal digits in a limb that are zero starting at the 204 * most significant digits. This basically returns how much of the limb is used. 205 * @param n The number. 206 * @return The number of decimal digits that are 0 starting at the most 207 * significant digits. 208 */ 209 static inline size_t 210 bc_num_zeroDigits(const BcDig* n) 211 { 212 assert(*n >= 0); 213 assert(((size_t) *n) < BC_BASE_POW); 214 return BC_BASE_DIGS - bc_num_log10((size_t) *n); 215 } 216 217 /** 218 * Returns the power of 10 that the least significant limb should be multiplied 219 * by to put its digits in the right place. For example, if the scale only 220 * reaches 8 places into the limb, this will return 1 (because it should be 221 * multiplied by 10^1) to put the number in the correct place. 222 * @param scale The scale. 223 * @return The power of 10 that the least significant limb should be 224 * multiplied by 225 */ 226 static inline size_t 227 bc_num_leastSigPow(size_t scale) 228 { 229 size_t digs; 230 231 digs = scale % BC_BASE_DIGS; 232 digs = digs != 0 ? BC_BASE_DIGS - digs : 0; 233 234 return bc_num_pow10[digs]; 235 } 236 237 /** 238 * Return the total number of integer digits in a number. This is the opposite 239 * of scale, like bc_num_int() is the opposite of rdx. 240 * @param n The number. 241 * @return The number of integer digits in @a n. 242 */ 243 static size_t 244 bc_num_intDigits(const BcNum* n) 245 { 246 size_t digits = bc_num_int(n) * BC_BASE_DIGS; 247 if (digits > 0) digits -= bc_num_zeroDigits(n->num + n->len - 1); 248 return digits; 249 } 250 251 /** 252 * Returns the number of limbs of a number that are non-zero starting at the 253 * most significant limbs. This expects that there are *no* integer limbs in the 254 * number because it is specifically to figure out how many zero limbs after the 255 * decimal place to ignore. If there are zero limbs after non-zero limbs, they 256 * are counted as non-zero limbs. 257 * @param n The number. 258 * @return The number of non-zero limbs after the decimal point. 259 */ 260 static size_t 261 bc_num_nonZeroLen(const BcNum* restrict n) 262 { 263 size_t i, len = n->len; 264 265 assert(len == BC_NUM_RDX_VAL(n)); 266 267 for (i = len - 1; i < len && !n->num[i]; --i) 268 { 269 continue; 270 } 271 272 assert(i + 1 > 0); 273 274 return i + 1; 275 } 276 277 /** 278 * Returns the power of 10 that a number with an absolute value less than 1 279 * needs to be multiplied by in order to be greater than 1 or less than -1. 280 * @param n The number. 281 * @return The power of 10 that a number greater than 1 and less than -1 must 282 * be multiplied by to be greater than 1 or less than -1. 283 */ 284 static size_t 285 bc_num_negPow10(const BcNum* restrict n) 286 { 287 // Figure out how many limbs after the decimal point is zero. 288 size_t i, places, idx = bc_num_nonZeroLen(n) - 1; 289 290 places = 1; 291 292 // Figure out how much in the last limb is zero. 293 for (i = BC_BASE_DIGS - 1; i < BC_BASE_DIGS; --i) 294 { 295 if (bc_num_pow10[i] > (BcBigDig) n->num[idx]) places += 1; 296 else break; 297 } 298 299 // Calculate the combination of zero limbs and zero digits in the last 300 // limb. 301 return places + (BC_NUM_RDX_VAL(n) - (idx + 1)) * BC_BASE_DIGS; 302 } 303 304 /** 305 * Performs a one-limb add with a carry. 306 * @param a The first limb. 307 * @param b The second limb. 308 * @param carry An in/out parameter; the carry in from the previous add and the 309 * carry out from this add. 310 * @return The resulting limb sum. 311 */ 312 static BcDig 313 bc_num_addDigits(BcDig a, BcDig b, bool* carry) 314 { 315 assert(((BcBigDig) BC_BASE_POW) * 2 == ((BcDig) BC_BASE_POW) * 2); 316 assert(a < BC_BASE_POW && a >= 0); 317 assert(b < BC_BASE_POW && b >= 0); 318 319 a += b + *carry; 320 *carry = (a >= BC_BASE_POW); 321 if (*carry) a -= BC_BASE_POW; 322 323 assert(a >= 0); 324 assert(a < BC_BASE_POW); 325 326 return a; 327 } 328 329 /** 330 * Performs a one-limb subtract with a carry. 331 * @param a The first limb. 332 * @param b The second limb. 333 * @param carry An in/out parameter; the carry in from the previous subtract 334 * and the carry out from this subtract. 335 * @return The resulting limb difference. 336 */ 337 static BcDig 338 bc_num_subDigits(BcDig a, BcDig b, bool* carry) 339 { 340 assert(a < BC_BASE_POW && a >= 0); 341 assert(b < BC_BASE_POW && b >= 0); 342 343 b += *carry; 344 *carry = (a < b); 345 if (*carry) a += BC_BASE_POW; 346 347 assert(a - b >= 0); 348 assert(a - b < BC_BASE_POW); 349 350 return a - b; 351 } 352 353 /** 354 * Add two BcDig arrays and store the result in the first array. 355 * @param a The first operand and out array. 356 * @param b The second operand. 357 * @param len The length of @a b. 358 */ 359 static void 360 bc_num_addArrays(BcDig* restrict a, const BcDig* restrict b, size_t len) 361 { 362 size_t i; 363 bool carry = false; 364 365 for (i = 0; i < len; ++i) 366 { 367 a[i] = bc_num_addDigits(a[i], b[i], &carry); 368 } 369 370 // Take care of the extra limbs in the bigger array. 371 for (; carry; ++i) 372 { 373 a[i] = bc_num_addDigits(a[i], 0, &carry); 374 } 375 } 376 377 /** 378 * Subtract two BcDig arrays and store the result in the first array. 379 * @param a The first operand and out array. 380 * @param b The second operand. 381 * @param len The length of @a b. 382 */ 383 static void 384 bc_num_subArrays(BcDig* restrict a, const BcDig* restrict b, size_t len) 385 { 386 size_t i; 387 bool carry = false; 388 389 for (i = 0; i < len; ++i) 390 { 391 a[i] = bc_num_subDigits(a[i], b[i], &carry); 392 } 393 394 // Take care of the extra limbs in the bigger array. 395 for (; carry; ++i) 396 { 397 a[i] = bc_num_subDigits(a[i], 0, &carry); 398 } 399 } 400 401 /** 402 * Multiply a BcNum array by a one-limb number. This is a faster version of 403 * multiplication for when we can use it. 404 * @param a The BcNum to multiply by the one-limb number. 405 * @param b The one limb of the one-limb number. 406 * @param c The return parameter. 407 */ 408 static void 409 bc_num_mulArray(const BcNum* restrict a, BcBigDig b, BcNum* restrict c) 410 { 411 size_t i; 412 BcBigDig carry = 0; 413 414 assert(b <= BC_BASE_POW); 415 416 // Make sure the return parameter is big enough. 417 if (a->len + 1 > c->cap) bc_num_expand(c, a->len + 1); 418 419 // We want the entire return parameter to be zero for cleaning later. 420 // NOLINTNEXTLINE 421 memset(c->num, 0, BC_NUM_SIZE(c->cap)); 422 423 // Actual multiplication loop. 424 for (i = 0; i < a->len; ++i) 425 { 426 BcBigDig in = ((BcBigDig) a->num[i]) * b + carry; 427 c->num[i] = in % BC_BASE_POW; 428 carry = in / BC_BASE_POW; 429 } 430 431 assert(carry < BC_BASE_POW); 432 433 // Finishing touches. 434 c->num[i] = (BcDig) carry; 435 assert(c->num[i] >= 0 && c->num[i] < BC_BASE_POW); 436 c->len = a->len; 437 c->len += (carry != 0); 438 439 bc_num_clean(c); 440 441 // Postconditions. 442 assert(!BC_NUM_NEG(c) || BC_NUM_NONZERO(c)); 443 assert(BC_NUM_RDX_VAL(c) <= c->len || !c->len); 444 assert(!c->len || c->num[c->len - 1] || BC_NUM_RDX_VAL(c) == c->len); 445 } 446 447 /** 448 * Divide a BcNum array by a one-limb number. This is a faster version of divide 449 * for when we can use it. 450 * @param a The BcNum to multiply by the one-limb number. 451 * @param b The one limb of the one-limb number. 452 * @param c The return parameter for the quotient. 453 * @param rem The return parameter for the remainder. 454 */ 455 static void 456 bc_num_divArray(const BcNum* restrict a, BcBigDig b, BcNum* restrict c, 457 BcBigDig* rem) 458 { 459 size_t i; 460 BcBigDig carry = 0; 461 462 assert(c->cap >= a->len); 463 464 // Actual division loop. 465 for (i = a->len - 1; i < a->len; --i) 466 { 467 BcBigDig in = ((BcBigDig) a->num[i]) + carry * BC_BASE_POW; 468 assert(in / b < BC_BASE_POW); 469 c->num[i] = (BcDig) (in / b); 470 assert(c->num[i] >= 0 && c->num[i] < BC_BASE_POW); 471 carry = in % b; 472 } 473 474 // Finishing touches. 475 c->len = a->len; 476 bc_num_clean(c); 477 *rem = carry; 478 479 // Postconditions. 480 assert(!BC_NUM_NEG(c) || BC_NUM_NONZERO(c)); 481 assert(BC_NUM_RDX_VAL(c) <= c->len || !c->len); 482 assert(!c->len || c->num[c->len - 1] || BC_NUM_RDX_VAL(c) == c->len); 483 } 484 485 /** 486 * Compare two BcDig arrays and return >0 if @a b is greater, <0 if @a b is 487 * less, and 0 if equal. Both @a a and @a b must have the same length. 488 * @param a The first array. 489 * @param b The second array. 490 * @param len The minimum length of the arrays. 491 */ 492 static ssize_t 493 bc_num_compare(const BcDig* restrict a, const BcDig* restrict b, size_t len) 494 { 495 size_t i; 496 BcDig c = 0; 497 for (i = len - 1; i < len && !(c = a[i] - b[i]); --i) 498 { 499 continue; 500 } 501 return bc_num_neg(i + 1, c < 0); 502 } 503 504 ssize_t 505 bc_num_cmp(const BcNum* a, const BcNum* b) 506 { 507 size_t i, min, a_int, b_int, diff, ardx, brdx; 508 BcDig* max_num; 509 BcDig* min_num; 510 bool a_max, neg = false; 511 ssize_t cmp; 512 513 assert(a != NULL && b != NULL); 514 515 // Same num? Equal. 516 if (a == b) return 0; 517 518 // Easy cases. 519 if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !BC_NUM_NEG(b)); 520 if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a); 521 if (BC_NUM_NEG(a)) 522 { 523 if (BC_NUM_NEG(b)) neg = true; 524 else return -1; 525 } 526 else if (BC_NUM_NEG(b)) return 1; 527 528 // Get the number of int limbs in each number and get the difference. 529 a_int = bc_num_int(a); 530 b_int = bc_num_int(b); 531 a_int -= b_int; 532 533 // If there's a difference, then just return the comparison. 534 if (a_int) return neg ? -((ssize_t) a_int) : (ssize_t) a_int; 535 536 // Get the rdx's and figure out the max. 537 ardx = BC_NUM_RDX_VAL(a); 538 brdx = BC_NUM_RDX_VAL(b); 539 a_max = (ardx > brdx); 540 541 // Set variables based on the above. 542 if (a_max) 543 { 544 min = brdx; 545 diff = ardx - brdx; 546 max_num = a->num + diff; 547 min_num = b->num; 548 } 549 else 550 { 551 min = ardx; 552 diff = brdx - ardx; 553 max_num = b->num + diff; 554 min_num = a->num; 555 } 556 557 // Do a full limb-by-limb comparison. 558 cmp = bc_num_compare(max_num, min_num, b_int + min); 559 560 // If we found a difference, return it based on state. 561 if (cmp) return bc_num_neg((size_t) cmp, !a_max == !neg); 562 563 // If there was no difference, then the final step is to check which number 564 // has greater or lesser limbs beyond the other's. 565 for (max_num -= diff, i = diff - 1; i < diff; --i) 566 { 567 if (max_num[i]) return bc_num_neg(1, !a_max == !neg); 568 } 569 570 return 0; 571 } 572 573 void 574 bc_num_truncate(BcNum* restrict n, size_t places) 575 { 576 size_t nrdx, places_rdx; 577 578 if (!places) return; 579 580 // Grab these needed values; places_rdx is the rdx equivalent to places like 581 // rdx is to scale. 582 nrdx = BC_NUM_RDX_VAL(n); 583 places_rdx = nrdx ? nrdx - BC_NUM_RDX(n->scale - places) : 0; 584 585 // We cannot truncate more places than we have. 586 assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len)); 587 588 n->scale -= places; 589 BC_NUM_RDX_SET(n, nrdx - places_rdx); 590 591 // Only when the number is nonzero do we need to do the hard stuff. 592 if (BC_NUM_NONZERO(n)) 593 { 594 size_t pow; 595 596 // This calculates how many decimal digits are in the least significant 597 // limb, then gets the power for that. 598 pow = bc_num_leastSigPow(n->scale); 599 600 n->len -= places_rdx; 601 602 // We have to move limbs to maintain invariants. The limbs must begin at 603 // the beginning of the BcNum array. 604 // NOLINTNEXTLINE 605 memmove(n->num, n->num + places_rdx, BC_NUM_SIZE(n->len)); 606 607 // Clear the lower part of the last digit. 608 if (BC_NUM_NONZERO(n)) n->num[0] -= n->num[0] % (BcDig) pow; 609 610 bc_num_clean(n); 611 } 612 } 613 614 void 615 bc_num_extend(BcNum* restrict n, size_t places) 616 { 617 size_t nrdx, places_rdx; 618 619 if (!places) return; 620 621 // Easy case with zero; set the scale. 622 if (BC_NUM_ZERO(n)) 623 { 624 n->scale += places; 625 return; 626 } 627 628 // Grab these needed values; places_rdx is the rdx equivalent to places like 629 // rdx is to scale. 630 nrdx = BC_NUM_RDX_VAL(n); 631 places_rdx = BC_NUM_RDX(places + n->scale) - nrdx; 632 633 // This is the hard case. We need to expand the number, move the limbs, and 634 // set the limbs that were just cleared. 635 if (places_rdx) 636 { 637 bc_num_expand(n, bc_vm_growSize(n->len, places_rdx)); 638 // NOLINTNEXTLINE 639 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len)); 640 // NOLINTNEXTLINE 641 memset(n->num, 0, BC_NUM_SIZE(places_rdx)); 642 } 643 644 // Finally, set scale and rdx. 645 BC_NUM_RDX_SET(n, nrdx + places_rdx); 646 n->scale += places; 647 n->len += places_rdx; 648 649 assert(BC_NUM_RDX_VAL(n) == BC_NUM_RDX(n->scale)); 650 } 651 652 /** 653 * Retires (finishes) a multiplication or division operation. 654 */ 655 static void 656 bc_num_retireMul(BcNum* restrict n, size_t scale, bool neg1, bool neg2) 657 { 658 // Make sure scale is correct. 659 if (n->scale < scale) bc_num_extend(n, scale - n->scale); 660 else bc_num_truncate(n, n->scale - scale); 661 662 bc_num_clean(n); 663 664 // We need to ensure rdx is correct. 665 if (BC_NUM_NONZERO(n)) n->rdx = BC_NUM_NEG_VAL(n, !neg1 != !neg2); 666 } 667 668 /** 669 * Splits a number into two BcNum's. This is used in Karatsuba. 670 * @param n The number to split. 671 * @param idx The index to split at. 672 * @param a An out parameter; the low part of @a n. 673 * @param b An out parameter; the high part of @a n. 674 */ 675 static void 676 bc_num_split(const BcNum* restrict n, size_t idx, BcNum* restrict a, 677 BcNum* restrict b) 678 { 679 // We want a and b to be clear. 680 assert(BC_NUM_ZERO(a)); 681 assert(BC_NUM_ZERO(b)); 682 683 // The usual case. 684 if (idx < n->len) 685 { 686 // Set the fields first. 687 b->len = n->len - idx; 688 a->len = idx; 689 a->scale = b->scale = 0; 690 BC_NUM_RDX_SET(a, 0); 691 BC_NUM_RDX_SET(b, 0); 692 693 assert(a->cap >= a->len); 694 assert(b->cap >= b->len); 695 696 // Copy the arrays. This is not necessary for safety, but it is faster, 697 // for some reason. 698 // NOLINTNEXTLINE 699 memcpy(b->num, n->num + idx, BC_NUM_SIZE(b->len)); 700 // NOLINTNEXTLINE 701 memcpy(a->num, n->num, BC_NUM_SIZE(idx)); 702 703 bc_num_clean(b); 704 } 705 // If the index is weird, just skip the split. 706 else bc_num_copy(a, n); 707 708 bc_num_clean(a); 709 } 710 711 /** 712 * Copies a number into another, but shifts the rdx so that the result number 713 * only sees the integer part of the source number. 714 * @param n The number to copy. 715 * @param r The result number with a shifted rdx, len, and num. 716 */ 717 static void 718 bc_num_shiftRdx(const BcNum* restrict n, BcNum* restrict r) 719 { 720 size_t rdx = BC_NUM_RDX_VAL(n); 721 722 r->len = n->len - rdx; 723 r->cap = n->cap - rdx; 724 r->num = n->num + rdx; 725 726 BC_NUM_RDX_SET_NEG(r, 0, BC_NUM_NEG(n)); 727 r->scale = 0; 728 } 729 730 /** 731 * Shifts a number so that all of the least significant limbs of the number are 732 * skipped. This must be undone by bc_num_unshiftZero(). 733 * @param n The number to shift. 734 */ 735 static size_t 736 bc_num_shiftZero(BcNum* restrict n) 737 { 738 // This is volatile to quiet a GCC warning about longjmp() clobbering. 739 volatile size_t i; 740 741 // If we don't have an integer, that is a problem, but it's also a bug 742 // because the caller should have set everything up right. 743 assert(!BC_NUM_RDX_VAL(n) || BC_NUM_ZERO(n)); 744 745 for (i = 0; i < n->len && !n->num[i]; ++i) 746 { 747 continue; 748 } 749 750 n->len -= i; 751 n->num += i; 752 753 return i; 754 } 755 756 /** 757 * Undo the damage done by bc_num_unshiftZero(). This must be called like all 758 * cleanup functions: after a label used by setjmp() and longjmp(). 759 * @param n The number to unshift. 760 * @param places_rdx The amount the number was originally shift. 761 */ 762 static void 763 bc_num_unshiftZero(BcNum* restrict n, size_t places_rdx) 764 { 765 n->len += places_rdx; 766 n->num -= places_rdx; 767 } 768 769 /** 770 * Shifts the digits right within a number by no more than BC_BASE_DIGS - 1. 771 * This is the final step on shifting numbers with the shift operators. It 772 * depends on the caller to shift the limbs properly because all it does is 773 * ensure that the rdx point is realigned to be between limbs. 774 * @param n The number to shift digits in. 775 * @param dig The number of places to shift right. 776 */ 777 static void 778 bc_num_shift(BcNum* restrict n, BcBigDig dig) 779 { 780 size_t i, len = n->len; 781 BcBigDig carry = 0, pow; 782 BcDig* ptr = n->num; 783 784 assert(dig < BC_BASE_DIGS); 785 786 // Figure out the parameters for division. 787 pow = bc_num_pow10[dig]; 788 dig = bc_num_pow10[BC_BASE_DIGS - dig]; 789 790 // Run a series of divisions and mods with carries across the entire number 791 // array. This effectively shifts everything over. 792 for (i = len - 1; i < len; --i) 793 { 794 BcBigDig in, temp; 795 in = ((BcBigDig) ptr[i]); 796 temp = carry * dig; 797 carry = in % pow; 798 ptr[i] = ((BcDig) (in / pow)) + (BcDig) temp; 799 assert(ptr[i] >= 0 && ptr[i] < BC_BASE_POW); 800 } 801 802 assert(!carry); 803 } 804 805 /** 806 * Shift a number left by a certain number of places. This is the workhorse of 807 * the left shift operator. 808 * @param n The number to shift left. 809 * @param places The amount of places to shift @a n left by. 810 */ 811 static void 812 bc_num_shiftLeft(BcNum* restrict n, size_t places) 813 { 814 BcBigDig dig; 815 size_t places_rdx; 816 bool shift; 817 818 if (!places) return; 819 820 // Make sure to grow the number if necessary. 821 if (places > n->scale) 822 { 823 size_t size = bc_vm_growSize(BC_NUM_RDX(places - n->scale), n->len); 824 if (size > SIZE_MAX - 1) bc_err(BC_ERR_MATH_OVERFLOW); 825 } 826 827 // If zero, we can just set the scale and bail. 828 if (BC_NUM_ZERO(n)) 829 { 830 if (n->scale >= places) n->scale -= places; 831 else n->scale = 0; 832 return; 833 } 834 835 // When I changed bc to have multiple digits per limb, this was the hardest 836 // code to change. This and shift right. Make sure you understand this 837 // before attempting anything. 838 839 // This is how many limbs we will shift. 840 dig = (BcBigDig) (places % BC_BASE_DIGS); 841 shift = (dig != 0); 842 843 // Convert places to a rdx value. 844 places_rdx = BC_NUM_RDX(places); 845 846 // If the number is not an integer, we need special care. The reason an 847 // integer doesn't is because left shift would only extend the integer, 848 // whereas a non-integer might have its fractional part eliminated or only 849 // partially eliminated. 850 if (n->scale) 851 { 852 size_t nrdx = BC_NUM_RDX_VAL(n); 853 854 // If the number's rdx is bigger, that's the hard case. 855 if (nrdx >= places_rdx) 856 { 857 size_t mod = n->scale % BC_BASE_DIGS, revdig; 858 859 // We want mod to be in the range [1, BC_BASE_DIGS], not 860 // [0, BC_BASE_DIGS). 861 mod = mod ? mod : BC_BASE_DIGS; 862 863 // We need to reverse dig to get the actual number of digits. 864 revdig = dig ? BC_BASE_DIGS - dig : 0; 865 866 // If the two overflow BC_BASE_DIGS, we need to move an extra place. 867 if (mod + revdig > BC_BASE_DIGS) places_rdx = 1; 868 else places_rdx = 0; 869 } 870 else places_rdx -= nrdx; 871 } 872 873 // If this is non-zero, we need an extra place, so expand, move, and set. 874 if (places_rdx) 875 { 876 bc_num_expand(n, bc_vm_growSize(n->len, places_rdx)); 877 // NOLINTNEXTLINE 878 memmove(n->num + places_rdx, n->num, BC_NUM_SIZE(n->len)); 879 // NOLINTNEXTLINE 880 memset(n->num, 0, BC_NUM_SIZE(places_rdx)); 881 n->len += places_rdx; 882 } 883 884 // Set the scale appropriately. 885 if (places > n->scale) 886 { 887 n->scale = 0; 888 BC_NUM_RDX_SET(n, 0); 889 } 890 else 891 { 892 n->scale -= places; 893 BC_NUM_RDX_SET(n, BC_NUM_RDX(n->scale)); 894 } 895 896 // Finally, shift within limbs. 897 if (shift) bc_num_shift(n, BC_BASE_DIGS - dig); 898 899 bc_num_clean(n); 900 } 901 902 void 903 bc_num_shiftRight(BcNum* restrict n, size_t places) 904 { 905 BcBigDig dig; 906 size_t places_rdx, scale, scale_mod, int_len, expand; 907 bool shift; 908 909 if (!places) return; 910 911 // If zero, we can just set the scale and bail. 912 if (BC_NUM_ZERO(n)) 913 { 914 n->scale += places; 915 bc_num_expand(n, BC_NUM_RDX(n->scale)); 916 return; 917 } 918 919 // Amount within a limb we have to shift by. 920 dig = (BcBigDig) (places % BC_BASE_DIGS); 921 shift = (dig != 0); 922 923 scale = n->scale; 924 925 // Figure out how the scale is affected. 926 scale_mod = scale % BC_BASE_DIGS; 927 scale_mod = scale_mod ? scale_mod : BC_BASE_DIGS; 928 929 // We need to know the int length and rdx for places. 930 int_len = bc_num_int(n); 931 places_rdx = BC_NUM_RDX(places); 932 933 // If we are going to shift past a limb boundary or not, set accordingly. 934 if (scale_mod + dig > BC_BASE_DIGS) 935 { 936 expand = places_rdx - 1; 937 places_rdx = 1; 938 } 939 else 940 { 941 expand = places_rdx; 942 places_rdx = 0; 943 } 944 945 // Clamp expanding. 946 if (expand > int_len) expand -= int_len; 947 else expand = 0; 948 949 // Extend, expand, and zero. 950 bc_num_extend(n, places_rdx * BC_BASE_DIGS); 951 bc_num_expand(n, bc_vm_growSize(expand, n->len)); 952 // NOLINTNEXTLINE 953 memset(n->num + n->len, 0, BC_NUM_SIZE(expand)); 954 955 // Set the fields. 956 n->len += expand; 957 n->scale = 0; 958 BC_NUM_RDX_SET(n, 0); 959 960 // Finally, shift within limbs. 961 if (shift) bc_num_shift(n, dig); 962 963 n->scale = scale + places; 964 BC_NUM_RDX_SET(n, BC_NUM_RDX(n->scale)); 965 966 bc_num_clean(n); 967 968 assert(BC_NUM_RDX_VAL(n) <= n->len && n->len <= n->cap); 969 assert(BC_NUM_RDX_VAL(n) == BC_NUM_RDX(n->scale)); 970 } 971 972 /** 973 * Tests if a number is a integer with scale or not. Returns true if the number 974 * is not an integer. If it is, its integer shifted form is copied into the 975 * result parameter for use where only integers are allowed. 976 * @param n The integer to test and shift. 977 * @param r The number to store the shifted result into. This number should 978 * *not* be allocated. 979 * @return True if the number is a non-integer, false otherwise. 980 */ 981 static bool 982 bc_num_nonInt(const BcNum* restrict n, BcNum* restrict r) 983 { 984 bool zero; 985 size_t i, rdx = BC_NUM_RDX_VAL(n); 986 987 if (!rdx) 988 { 989 // NOLINTNEXTLINE 990 memcpy(r, n, sizeof(BcNum)); 991 return false; 992 } 993 994 zero = true; 995 996 for (i = 0; zero && i < rdx; ++i) 997 { 998 zero = (n->num[i] == 0); 999 } 1000 1001 if (BC_ERR(!zero)) return true; 1002 1003 bc_num_shiftRdx(n, r); 1004 1005 return false; 1006 } 1007 1008 #if BC_ENABLE_EXTRA_MATH 1009 1010 /** 1011 * Execute common code for an operater that needs an integer for the second 1012 * operand and return the integer operand as a BcBigDig. 1013 * @param a The first operand. 1014 * @param b The second operand. 1015 * @param c The result operand. 1016 * @return The second operand as a hardware integer. 1017 */ 1018 static BcBigDig 1019 bc_num_intop(const BcNum* a, const BcNum* b, BcNum* restrict c) 1020 { 1021 BcNum temp; 1022 1023 #if BC_GCC 1024 temp.len = 0; 1025 temp.rdx = 0; 1026 temp.num = NULL; 1027 #endif // BC_GCC 1028 1029 if (BC_ERR(bc_num_nonInt(b, &temp))) bc_err(BC_ERR_MATH_NON_INTEGER); 1030 1031 bc_num_copy(c, a); 1032 1033 return bc_num_bigdig(&temp); 1034 } 1035 #endif // BC_ENABLE_EXTRA_MATH 1036 1037 /** 1038 * This is the actual implementation of add *and* subtract. Since this function 1039 * doesn't need to use scale (per the bc spec), I am hijacking it to say whether 1040 * it's doing an add or a subtract. And then I convert substraction to addition 1041 * of negative second operand. This is a BcNumBinOp function. 1042 * @param a The first operand. 1043 * @param b The second operand. 1044 * @param c The return parameter. 1045 * @param sub Non-zero for a subtract, zero for an add. 1046 */ 1047 static void 1048 bc_num_as(BcNum* a, BcNum* b, BcNum* restrict c, size_t sub) 1049 { 1050 BcDig* ptr_c; 1051 BcDig* ptr_l; 1052 BcDig* ptr_r; 1053 size_t i, min_rdx, max_rdx, diff, a_int, b_int, min_len, max_len, max_int; 1054 size_t len_l, len_r, ardx, brdx; 1055 bool b_neg, do_sub, do_rev_sub, carry, c_neg; 1056 1057 if (BC_NUM_ZERO(b)) 1058 { 1059 bc_num_copy(c, a); 1060 return; 1061 } 1062 1063 if (BC_NUM_ZERO(a)) 1064 { 1065 bc_num_copy(c, b); 1066 c->rdx = BC_NUM_NEG_VAL(c, BC_NUM_NEG(b) != sub); 1067 return; 1068 } 1069 1070 // Invert sign of b if it is to be subtracted. This operation must 1071 // precede the tests for any of the operands being zero. 1072 b_neg = (BC_NUM_NEG(b) != sub); 1073 1074 // Figure out if we will actually add the numbers if their signs are equal 1075 // or subtract. 1076 do_sub = (BC_NUM_NEG(a) != b_neg); 1077 1078 a_int = bc_num_int(a); 1079 b_int = bc_num_int(b); 1080 max_int = BC_MAX(a_int, b_int); 1081 1082 // Figure out which number will have its last limbs copied (for addition) or 1083 // subtracted (for subtraction). 1084 ardx = BC_NUM_RDX_VAL(a); 1085 brdx = BC_NUM_RDX_VAL(b); 1086 min_rdx = BC_MIN(ardx, brdx); 1087 max_rdx = BC_MAX(ardx, brdx); 1088 diff = max_rdx - min_rdx; 1089 1090 max_len = max_int + max_rdx; 1091 1092 if (do_sub) 1093 { 1094 // Check whether b has to be subtracted from a or a from b. 1095 if (a_int != b_int) do_rev_sub = (a_int < b_int); 1096 else if (ardx > brdx) 1097 { 1098 do_rev_sub = (bc_num_compare(a->num + diff, b->num, b->len) < 0); 1099 } 1100 else do_rev_sub = (bc_num_compare(a->num, b->num + diff, a->len) <= 0); 1101 } 1102 else 1103 { 1104 // The result array of the addition might come out one element 1105 // longer than the bigger of the operand arrays. 1106 max_len += 1; 1107 do_rev_sub = (a_int < b_int); 1108 } 1109 1110 assert(max_len <= c->cap); 1111 1112 // Cache values for simple code later. 1113 if (do_rev_sub) 1114 { 1115 ptr_l = b->num; 1116 ptr_r = a->num; 1117 len_l = b->len; 1118 len_r = a->len; 1119 } 1120 else 1121 { 1122 ptr_l = a->num; 1123 ptr_r = b->num; 1124 len_l = a->len; 1125 len_r = b->len; 1126 } 1127 1128 ptr_c = c->num; 1129 carry = false; 1130 1131 // This is true if the numbers have a different number of limbs after the 1132 // decimal point. 1133 if (diff) 1134 { 1135 // If the rdx values of the operands do not match, the result will 1136 // have low end elements that are the positive or negative trailing 1137 // elements of the operand with higher rdx value. 1138 if ((ardx > brdx) != do_rev_sub) 1139 { 1140 // !do_rev_sub && ardx > brdx || do_rev_sub && brdx > ardx 1141 // The left operand has BcDig values that need to be copied, 1142 // either from a or from b (in case of a reversed subtraction). 1143 // NOLINTNEXTLINE 1144 memcpy(ptr_c, ptr_l, BC_NUM_SIZE(diff)); 1145 ptr_l += diff; 1146 len_l -= diff; 1147 } 1148 else 1149 { 1150 // The right operand has BcDig values that need to be copied 1151 // or subtracted from zero (in case of a subtraction). 1152 if (do_sub) 1153 { 1154 // do_sub (do_rev_sub && ardx > brdx || 1155 // !do_rev_sub && brdx > ardx) 1156 for (i = 0; i < diff; i++) 1157 { 1158 ptr_c[i] = bc_num_subDigits(0, ptr_r[i], &carry); 1159 } 1160 } 1161 else 1162 { 1163 // !do_sub && brdx > ardx 1164 // NOLINTNEXTLINE 1165 memcpy(ptr_c, ptr_r, BC_NUM_SIZE(diff)); 1166 } 1167 1168 // Future code needs to ignore the limbs we just did. 1169 ptr_r += diff; 1170 len_r -= diff; 1171 } 1172 1173 // The return value pointer needs to ignore what we just did. 1174 ptr_c += diff; 1175 } 1176 1177 // This is the length that can be directly added/subtracted. 1178 min_len = BC_MIN(len_l, len_r); 1179 1180 // After dealing with possible low array elements that depend on only one 1181 // operand above, the actual add or subtract can be performed as if the rdx 1182 // of both operands was the same. 1183 // 1184 // Inlining takes care of eliminating constant zero arguments to 1185 // addDigit/subDigit (checked in disassembly of resulting bc binary 1186 // compiled with gcc and clang). 1187 if (do_sub) 1188 { 1189 // Actual subtraction. 1190 for (i = 0; i < min_len; ++i) 1191 { 1192 ptr_c[i] = bc_num_subDigits(ptr_l[i], ptr_r[i], &carry); 1193 } 1194 1195 // Finishing the limbs beyond the direct subtraction. 1196 for (; i < len_l; ++i) 1197 { 1198 ptr_c[i] = bc_num_subDigits(ptr_l[i], 0, &carry); 1199 } 1200 } 1201 else 1202 { 1203 // Actual addition. 1204 for (i = 0; i < min_len; ++i) 1205 { 1206 ptr_c[i] = bc_num_addDigits(ptr_l[i], ptr_r[i], &carry); 1207 } 1208 1209 // Finishing the limbs beyond the direct addition. 1210 for (; i < len_l; ++i) 1211 { 1212 ptr_c[i] = bc_num_addDigits(ptr_l[i], 0, &carry); 1213 } 1214 1215 // Addition can create an extra limb. We take care of that here. 1216 ptr_c[i] = bc_num_addDigits(0, 0, &carry); 1217 } 1218 1219 assert(carry == false); 1220 1221 // The result has the same sign as a, unless the operation was a 1222 // reverse subtraction (b - a). 1223 c_neg = BC_NUM_NEG(a) != (do_sub && do_rev_sub); 1224 BC_NUM_RDX_SET_NEG(c, max_rdx, c_neg); 1225 c->len = max_len; 1226 c->scale = BC_MAX(a->scale, b->scale); 1227 1228 bc_num_clean(c); 1229 } 1230 1231 /** 1232 * The simple multiplication that karatsuba dishes out to when the length of the 1233 * numbers gets low enough. This doesn't use scale because it treats the 1234 * operands as though they are integers. 1235 * @param a The first operand. 1236 * @param b The second operand. 1237 * @param c The return parameter. 1238 */ 1239 static void 1240 bc_num_m_simp(const BcNum* a, const BcNum* b, BcNum* restrict c) 1241 { 1242 size_t i, alen = a->len, blen = b->len, clen; 1243 BcDig* ptr_a = a->num; 1244 BcDig* ptr_b = b->num; 1245 BcDig* ptr_c; 1246 BcBigDig sum = 0, carry = 0; 1247 1248 assert(sizeof(sum) >= sizeof(BcDig) * 2); 1249 assert(!BC_NUM_RDX_VAL(a) && !BC_NUM_RDX_VAL(b)); 1250 1251 // Make sure c is big enough. 1252 clen = bc_vm_growSize(alen, blen); 1253 bc_num_expand(c, bc_vm_growSize(clen, 1)); 1254 1255 // If we don't memset, then we might have uninitialized data use later. 1256 ptr_c = c->num; 1257 // NOLINTNEXTLINE 1258 memset(ptr_c, 0, BC_NUM_SIZE(c->cap)); 1259 1260 // This is the actual multiplication loop. It uses the lattice form of long 1261 // multiplication (see the explanation on the web page at 1262 // https://knilt.arcc.albany.edu/What_is_Lattice_Multiplication or the 1263 // explanation at Wikipedia). 1264 for (i = 0; i < clen; ++i) 1265 { 1266 ssize_t sidx = (ssize_t) (i - blen + 1); 1267 size_t j, k; 1268 1269 // These are the start indices. 1270 j = (size_t) BC_MAX(0, sidx); 1271 k = BC_MIN(i, blen - 1); 1272 1273 // On every iteration of this loop, a multiplication happens, then the 1274 // sum is automatically calculated. 1275 for (; j < alen && k < blen; ++j, --k) 1276 { 1277 sum += ((BcBigDig) ptr_a[j]) * ((BcBigDig) ptr_b[k]); 1278 1279 if (sum >= ((BcBigDig) BC_BASE_POW) * BC_BASE_POW) 1280 { 1281 carry += sum / BC_BASE_POW; 1282 sum %= BC_BASE_POW; 1283 } 1284 } 1285 1286 // Calculate the carry. 1287 if (sum >= BC_BASE_POW) 1288 { 1289 carry += sum / BC_BASE_POW; 1290 sum %= BC_BASE_POW; 1291 } 1292 1293 // Store and set up for next iteration. 1294 ptr_c[i] = (BcDig) sum; 1295 assert(ptr_c[i] < BC_BASE_POW); 1296 sum = carry; 1297 carry = 0; 1298 } 1299 1300 // This should always be true because there should be no carry on the last 1301 // digit; multiplication never goes above the sum of both lengths. 1302 assert(!sum); 1303 1304 c->len = clen; 1305 } 1306 1307 /** 1308 * Does a shifted add or subtract for Karatsuba below. This calls either 1309 * bc_num_addArrays() or bc_num_subArrays(). 1310 * @param n An in/out parameter; the first operand and return parameter. 1311 * @param a The second operand. 1312 * @param shift The amount to shift @a n by when adding/subtracting. 1313 * @param op The function to call, either bc_num_addArrays() or 1314 * bc_num_subArrays(). 1315 */ 1316 static void 1317 bc_num_shiftAddSub(BcNum* restrict n, const BcNum* restrict a, size_t shift, 1318 BcNumShiftAddOp op) 1319 { 1320 assert(n->len >= shift + a->len); 1321 assert(!BC_NUM_RDX_VAL(n) && !BC_NUM_RDX_VAL(a)); 1322 op(n->num + shift, a->num, a->len); 1323 } 1324 1325 /** 1326 * Implements the Karatsuba algorithm. 1327 */ 1328 static void 1329 bc_num_k(const BcNum* a, const BcNum* b, BcNum* restrict c) 1330 { 1331 size_t max, max2, total; 1332 BcNum l1, h1, l2, h2, m2, m1, z0, z1, z2, temp; 1333 BcDig* digs; 1334 BcDig* dig_ptr; 1335 BcNumShiftAddOp op; 1336 bool aone = BC_NUM_ONE(a); 1337 #if BC_ENABLE_LIBRARY 1338 BcVm* vm = bcl_getspecific(); 1339 #endif // BC_ENABLE_LIBRARY 1340 1341 assert(BC_NUM_ZERO(c)); 1342 1343 if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) return; 1344 1345 if (aone || BC_NUM_ONE(b)) 1346 { 1347 bc_num_copy(c, aone ? b : a); 1348 if ((aone && BC_NUM_NEG(a)) || BC_NUM_NEG(b)) BC_NUM_NEG_TGL(c); 1349 return; 1350 } 1351 1352 // Shell out to the simple algorithm with certain conditions. 1353 if (a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) 1354 { 1355 bc_num_m_simp(a, b, c); 1356 return; 1357 } 1358 1359 // We need to calculate the max size of the numbers that can result from the 1360 // operations. 1361 max = BC_MAX(a->len, b->len); 1362 max = BC_MAX(max, BC_NUM_DEF_SIZE); 1363 max2 = (max + 1) / 2; 1364 1365 // Calculate the space needed for all of the temporary allocations. We do 1366 // this to just allocate once. 1367 total = bc_vm_arraySize(BC_NUM_KARATSUBA_ALLOCS, max); 1368 1369 BC_SIG_LOCK; 1370 1371 // Allocate space for all of the temporaries. 1372 digs = dig_ptr = bc_vm_malloc(BC_NUM_SIZE(total)); 1373 1374 // Set up the temporaries. 1375 bc_num_setup(&l1, dig_ptr, max); 1376 dig_ptr += max; 1377 bc_num_setup(&h1, dig_ptr, max); 1378 dig_ptr += max; 1379 bc_num_setup(&l2, dig_ptr, max); 1380 dig_ptr += max; 1381 bc_num_setup(&h2, dig_ptr, max); 1382 dig_ptr += max; 1383 bc_num_setup(&m1, dig_ptr, max); 1384 dig_ptr += max; 1385 bc_num_setup(&m2, dig_ptr, max); 1386 1387 // Some temporaries need the ability to grow, so we allocate them 1388 // separately. 1389 max = bc_vm_growSize(max, 1); 1390 bc_num_init(&z0, max); 1391 bc_num_init(&z1, max); 1392 bc_num_init(&z2, max); 1393 max = bc_vm_growSize(max, max) + 1; 1394 bc_num_init(&temp, max); 1395 1396 BC_SETJMP_LOCKED(vm, err); 1397 1398 BC_SIG_UNLOCK; 1399 1400 // First, set up c. 1401 bc_num_expand(c, max); 1402 c->len = max; 1403 // NOLINTNEXTLINE 1404 memset(c->num, 0, BC_NUM_SIZE(c->len)); 1405 1406 // Split the parameters. 1407 bc_num_split(a, max2, &l1, &h1); 1408 bc_num_split(b, max2, &l2, &h2); 1409 1410 // Do the subtraction. 1411 bc_num_sub(&h1, &l1, &m1, 0); 1412 bc_num_sub(&l2, &h2, &m2, 0); 1413 1414 // The if statements below are there for efficiency reasons. The best way to 1415 // understand them is to understand the Karatsuba algorithm because now that 1416 // the ollocations and splits are done, the algorithm is pretty 1417 // straightforward. 1418 1419 if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) 1420 { 1421 assert(BC_NUM_RDX_VALID_NP(h1)); 1422 assert(BC_NUM_RDX_VALID_NP(h2)); 1423 1424 bc_num_m(&h1, &h2, &z2, 0); 1425 bc_num_clean(&z2); 1426 1427 bc_num_shiftAddSub(c, &z2, max2 * 2, bc_num_addArrays); 1428 bc_num_shiftAddSub(c, &z2, max2, bc_num_addArrays); 1429 } 1430 1431 if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) 1432 { 1433 assert(BC_NUM_RDX_VALID_NP(l1)); 1434 assert(BC_NUM_RDX_VALID_NP(l2)); 1435 1436 bc_num_m(&l1, &l2, &z0, 0); 1437 bc_num_clean(&z0); 1438 1439 bc_num_shiftAddSub(c, &z0, max2, bc_num_addArrays); 1440 bc_num_shiftAddSub(c, &z0, 0, bc_num_addArrays); 1441 } 1442 1443 if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) 1444 { 1445 assert(BC_NUM_RDX_VALID_NP(m1)); 1446 assert(BC_NUM_RDX_VALID_NP(m1)); 1447 1448 bc_num_m(&m1, &m2, &z1, 0); 1449 bc_num_clean(&z1); 1450 1451 op = (BC_NUM_NEG_NP(m1) != BC_NUM_NEG_NP(m2)) ? 1452 bc_num_subArrays : 1453 bc_num_addArrays; 1454 bc_num_shiftAddSub(c, &z1, max2, op); 1455 } 1456 1457 err: 1458 BC_SIG_MAYLOCK; 1459 free(digs); 1460 bc_num_free(&temp); 1461 bc_num_free(&z2); 1462 bc_num_free(&z1); 1463 bc_num_free(&z0); 1464 BC_LONGJMP_CONT(vm); 1465 } 1466 1467 /** 1468 * Does checks for Karatsuba. It also changes things to ensure that the 1469 * Karatsuba and simple multiplication can treat the numbers as integers. This 1470 * is a BcNumBinOp function. 1471 * @param a The first operand. 1472 * @param b The second operand. 1473 * @param c The return parameter. 1474 * @param scale The current scale. 1475 */ 1476 static void 1477 bc_num_m(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale) 1478 { 1479 BcNum cpa, cpb; 1480 size_t ascale, bscale, ardx, brdx, zero, len, rscale; 1481 // These are meant to quiet warnings on GCC about longjmp() clobbering. 1482 // The problem is real here. 1483 size_t scale1, scale2, realscale; 1484 // These are meant to quiet the GCC longjmp() clobbering, even though it 1485 // does not apply here. 1486 volatile size_t azero; 1487 volatile size_t bzero; 1488 #if BC_ENABLE_LIBRARY 1489 BcVm* vm = bcl_getspecific(); 1490 #endif // BC_ENABLE_LIBRARY 1491 1492 assert(BC_NUM_RDX_VALID(a)); 1493 assert(BC_NUM_RDX_VALID(b)); 1494 1495 bc_num_zero(c); 1496 1497 ascale = a->scale; 1498 bscale = b->scale; 1499 1500 // This sets the final scale according to the bc spec. 1501 scale1 = BC_MAX(scale, ascale); 1502 scale2 = BC_MAX(scale1, bscale); 1503 rscale = ascale + bscale; 1504 realscale = BC_MIN(rscale, scale2); 1505 1506 // If this condition is true, we can use bc_num_mulArray(), which would be 1507 // much faster. 1508 if ((a->len == 1 || b->len == 1) && !a->rdx && !b->rdx) 1509 { 1510 BcNum* operand; 1511 BcBigDig dig; 1512 1513 // Set the correct operands. 1514 if (a->len == 1) 1515 { 1516 dig = (BcBigDig) a->num[0]; 1517 operand = b; 1518 } 1519 else 1520 { 1521 dig = (BcBigDig) b->num[0]; 1522 operand = a; 1523 } 1524 1525 bc_num_mulArray(operand, dig, c); 1526 1527 // Need to make sure the sign is correct. 1528 if (BC_NUM_NONZERO(c)) 1529 { 1530 c->rdx = BC_NUM_NEG_VAL(c, BC_NUM_NEG(a) != BC_NUM_NEG(b)); 1531 } 1532 1533 return; 1534 } 1535 1536 assert(BC_NUM_RDX_VALID(a)); 1537 assert(BC_NUM_RDX_VALID(b)); 1538 1539 BC_SIG_LOCK; 1540 1541 // We need copies because of all of the mutation needed to make Karatsuba 1542 // think the numbers are integers. 1543 bc_num_init(&cpa, a->len + BC_NUM_RDX_VAL(a)); 1544 bc_num_init(&cpb, b->len + BC_NUM_RDX_VAL(b)); 1545 1546 BC_SETJMP_LOCKED(vm, init_err); 1547 1548 BC_SIG_UNLOCK; 1549 1550 bc_num_copy(&cpa, a); 1551 bc_num_copy(&cpb, b); 1552 1553 assert(BC_NUM_RDX_VALID_NP(cpa)); 1554 assert(BC_NUM_RDX_VALID_NP(cpb)); 1555 1556 BC_NUM_NEG_CLR_NP(cpa); 1557 BC_NUM_NEG_CLR_NP(cpb); 1558 1559 assert(BC_NUM_RDX_VALID_NP(cpa)); 1560 assert(BC_NUM_RDX_VALID_NP(cpb)); 1561 1562 // These are what makes them appear like integers. 1563 ardx = BC_NUM_RDX_VAL_NP(cpa) * BC_BASE_DIGS; 1564 bc_num_shiftLeft(&cpa, ardx); 1565 1566 brdx = BC_NUM_RDX_VAL_NP(cpb) * BC_BASE_DIGS; 1567 bc_num_shiftLeft(&cpb, brdx); 1568 1569 // We need to reset the jump here because azero and bzero are used in the 1570 // cleanup, and local variables are not guaranteed to be the same after a 1571 // jump. 1572 BC_SIG_LOCK; 1573 1574 BC_UNSETJMP(vm); 1575 1576 // We want to ignore zero limbs. 1577 azero = bc_num_shiftZero(&cpa); 1578 bzero = bc_num_shiftZero(&cpb); 1579 1580 BC_SETJMP_LOCKED(vm, err); 1581 1582 BC_SIG_UNLOCK; 1583 1584 bc_num_clean(&cpa); 1585 bc_num_clean(&cpb); 1586 1587 bc_num_k(&cpa, &cpb, c); 1588 1589 // The return parameter needs to have its scale set. This is the start. It 1590 // also needs to be shifted by the same amount as a and b have limbs after 1591 // the decimal point. 1592 zero = bc_vm_growSize(azero, bzero); 1593 len = bc_vm_growSize(c->len, zero); 1594 1595 bc_num_expand(c, len); 1596 1597 // Shift c based on the limbs after the decimal point in a and b. 1598 bc_num_shiftLeft(c, (len - c->len) * BC_BASE_DIGS); 1599 bc_num_shiftRight(c, ardx + brdx); 1600 1601 bc_num_retireMul(c, realscale, BC_NUM_NEG(a), BC_NUM_NEG(b)); 1602 1603 err: 1604 BC_SIG_MAYLOCK; 1605 bc_num_unshiftZero(&cpb, bzero); 1606 bc_num_unshiftZero(&cpa, azero); 1607 init_err: 1608 BC_SIG_MAYLOCK; 1609 bc_num_free(&cpb); 1610 bc_num_free(&cpa); 1611 BC_LONGJMP_CONT(vm); 1612 } 1613 1614 /** 1615 * Returns true if the BcDig array has non-zero limbs, false otherwise. 1616 * @param a The array to test. 1617 * @param len The length of the array. 1618 * @return True if @a has any non-zero limbs, false otherwise. 1619 */ 1620 static bool 1621 bc_num_nonZeroDig(BcDig* restrict a, size_t len) 1622 { 1623 size_t i; 1624 1625 for (i = len - 1; i < len; --i) 1626 { 1627 if (a[i] != 0) return true; 1628 } 1629 1630 return false; 1631 } 1632 1633 /** 1634 * Compares a BcDig array against a BcNum. This is especially suited for 1635 * division. Returns >0 if @a a is greater than @a b, <0 if it is less, and =0 1636 * if they are equal. 1637 * @param a The array. 1638 * @param b The number. 1639 * @param len The length to assume the arrays are. This is always less than the 1640 * actual length because of how this is implemented. 1641 */ 1642 static ssize_t 1643 bc_num_divCmp(const BcDig* a, const BcNum* b, size_t len) 1644 { 1645 ssize_t cmp; 1646 1647 if (b->len > len && a[len]) cmp = bc_num_compare(a, b->num, len + 1); 1648 else if (b->len <= len) 1649 { 1650 if (a[len]) cmp = 1; 1651 else cmp = bc_num_compare(a, b->num, len); 1652 } 1653 else cmp = -1; 1654 1655 return cmp; 1656 } 1657 1658 /** 1659 * Extends the two operands of a division by BC_BASE_DIGS minus the number of 1660 * digits in the divisor estimate. In other words, it is shifting the numbers in 1661 * order to force the divisor estimate to fill the limb. 1662 * @param a The first operand. 1663 * @param b The second operand. 1664 * @param divisor The divisor estimate. 1665 */ 1666 static void 1667 bc_num_divExtend(BcNum* restrict a, BcNum* restrict b, BcBigDig divisor) 1668 { 1669 size_t pow; 1670 1671 assert(divisor < BC_BASE_POW); 1672 1673 pow = BC_BASE_DIGS - bc_num_log10((size_t) divisor); 1674 1675 bc_num_shiftLeft(a, pow); 1676 bc_num_shiftLeft(b, pow); 1677 } 1678 1679 /** 1680 * Actually does division. This is a rewrite of my original code by Stefan Esser 1681 * from FreeBSD. 1682 * @param a The first operand. 1683 * @param b The second operand. 1684 * @param c The return parameter. 1685 * @param scale The current scale. 1686 */ 1687 static void 1688 bc_num_d_long(BcNum* restrict a, BcNum* restrict b, BcNum* restrict c, 1689 size_t scale) 1690 { 1691 BcBigDig divisor; 1692 size_t i, rdx; 1693 // This is volatile and len 2 and reallen exist to quiet the GCC warning 1694 // about clobbering on longjmp(). This one is possible, I think. 1695 volatile size_t len; 1696 size_t len2, reallen; 1697 // This is volatile and realend exists to quiet the GCC warning about 1698 // clobbering on longjmp(). This one is possible, I think. 1699 volatile size_t end; 1700 size_t realend; 1701 BcNum cpb; 1702 // This is volatile and realnonzero exists to quiet the GCC warning about 1703 // clobbering on longjmp(). This one is possible, I think. 1704 volatile bool nonzero; 1705 bool realnonzero; 1706 #if BC_ENABLE_LIBRARY 1707 BcVm* vm = bcl_getspecific(); 1708 #endif // BC_ENABLE_LIBRARY 1709 1710 assert(b->len < a->len); 1711 1712 len = b->len; 1713 end = a->len - len; 1714 1715 assert(len >= 1); 1716 1717 // This is a final time to make sure c is big enough and that its array is 1718 // properly zeroed. 1719 bc_num_expand(c, a->len); 1720 // NOLINTNEXTLINE 1721 memset(c->num, 0, c->cap * sizeof(BcDig)); 1722 1723 // Setup. 1724 BC_NUM_RDX_SET(c, BC_NUM_RDX_VAL(a)); 1725 c->scale = a->scale; 1726 c->len = a->len; 1727 1728 // This is pulling the most significant limb of b in order to establish a 1729 // good "estimate" for the actual divisor. 1730 divisor = (BcBigDig) b->num[len - 1]; 1731 1732 // The entire bit of code in this if statement is to tighten the estimate of 1733 // the divisor. The condition asks if b has any other non-zero limbs. 1734 if (len > 1 && bc_num_nonZeroDig(b->num, len - 1)) 1735 { 1736 // This takes a little bit of understanding. The "10*BC_BASE_DIGS/6+1" 1737 // results in either 16 for 64-bit 9-digit limbs or 7 for 32-bit 4-digit 1738 // limbs. Then it shifts a 1 by that many, which in both cases, puts the 1739 // result above *half* of the max value a limb can store. Basically, 1740 // this quickly calculates if the divisor is greater than half the max 1741 // of a limb. 1742 nonzero = (divisor > 1 << ((10 * BC_BASE_DIGS) / 6 + 1)); 1743 1744 // If the divisor is *not* greater than half the limb... 1745 if (!nonzero) 1746 { 1747 // Extend the parameters by the number of missing digits in the 1748 // divisor. 1749 bc_num_divExtend(a, b, divisor); 1750 1751 // Check bc_num_d(). In there, we grow a again and again. We do it 1752 // again here; we *always* want to be sure it is big enough. 1753 len2 = BC_MAX(a->len, b->len); 1754 bc_num_expand(a, len2 + 1); 1755 1756 // Make a have a zero most significant limb to match the len. 1757 if (len2 + 1 > a->len) a->len = len2 + 1; 1758 1759 // Grab the new divisor estimate, new because the shift has made it 1760 // different. 1761 reallen = b->len; 1762 realend = a->len - reallen; 1763 divisor = (BcBigDig) b->num[reallen - 1]; 1764 1765 realnonzero = bc_num_nonZeroDig(b->num, reallen - 1); 1766 } 1767 else 1768 { 1769 realend = end; 1770 realnonzero = nonzero; 1771 } 1772 } 1773 else 1774 { 1775 realend = end; 1776 realnonzero = false; 1777 } 1778 1779 // If b has other nonzero limbs, we want the divisor to be one higher, so 1780 // that it is an upper bound. 1781 divisor += realnonzero; 1782 1783 // Make sure c can fit the new length. 1784 bc_num_expand(c, a->len); 1785 // NOLINTNEXTLINE 1786 memset(c->num, 0, BC_NUM_SIZE(c->cap)); 1787 1788 assert(c->scale >= scale); 1789 rdx = BC_NUM_RDX_VAL(c) - BC_NUM_RDX(scale); 1790 1791 BC_SIG_LOCK; 1792 1793 bc_num_init(&cpb, len + 1); 1794 1795 BC_SETJMP_LOCKED(vm, err); 1796 1797 BC_SIG_UNLOCK; 1798 1799 // This is the actual division loop. 1800 for (i = realend - 1; i < realend && i >= rdx && BC_NUM_NONZERO(a); --i) 1801 { 1802 ssize_t cmp; 1803 BcDig* n; 1804 BcBigDig result; 1805 1806 n = a->num + i; 1807 assert(n >= a->num); 1808 result = 0; 1809 1810 cmp = bc_num_divCmp(n, b, len); 1811 1812 // This is true if n is greater than b, which means that division can 1813 // proceed, so this inner loop is the part that implements one instance 1814 // of the division. 1815 while (cmp >= 0) 1816 { 1817 BcBigDig n1, dividend, quotient; 1818 1819 // These should be named obviously enough. Just imagine that it's a 1820 // division of one limb. Because that's what it is. 1821 n1 = (BcBigDig) n[len]; 1822 dividend = n1 * BC_BASE_POW + (BcBigDig) n[len - 1]; 1823 quotient = (dividend / divisor); 1824 1825 // If this is true, then we can just subtract. Remember: setting 1826 // quotient to 1 is not bad because we already know that n is 1827 // greater than b. 1828 if (quotient <= 1) 1829 { 1830 quotient = 1; 1831 bc_num_subArrays(n, b->num, len); 1832 } 1833 else 1834 { 1835 assert(quotient <= BC_BASE_POW); 1836 1837 // We need to multiply and subtract for a quotient above 1. 1838 bc_num_mulArray(b, (BcBigDig) quotient, &cpb); 1839 bc_num_subArrays(n, cpb.num, cpb.len); 1840 } 1841 1842 // The result is the *real* quotient, by the way, but it might take 1843 // multiple trips around this loop to get it. 1844 result += quotient; 1845 assert(result <= BC_BASE_POW); 1846 1847 // And here's why it might take multiple trips: n might *still* be 1848 // greater than b. So we have to loop again. That's what this is 1849 // setting up for: the condition of the while loop. 1850 if (realnonzero) cmp = bc_num_divCmp(n, b, len); 1851 else cmp = -1; 1852 } 1853 1854 assert(result < BC_BASE_POW); 1855 1856 // Store the actual limb quotient. 1857 c->num[i] = (BcDig) result; 1858 } 1859 1860 err: 1861 BC_SIG_MAYLOCK; 1862 bc_num_free(&cpb); 1863 BC_LONGJMP_CONT(vm); 1864 } 1865 1866 /** 1867 * Implements division. This is a BcNumBinOp function. 1868 * @param a The first operand. 1869 * @param b The second operand. 1870 * @param c The return parameter. 1871 * @param scale The current scale. 1872 */ 1873 static void 1874 bc_num_d(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale) 1875 { 1876 size_t len, cpardx; 1877 BcNum cpa, cpb; 1878 #if BC_ENABLE_LIBRARY 1879 BcVm* vm = bcl_getspecific(); 1880 #endif // BC_ENABLE_LIBRARY 1881 1882 if (BC_NUM_ZERO(b)) bc_err(BC_ERR_MATH_DIVIDE_BY_ZERO); 1883 1884 if (BC_NUM_ZERO(a)) 1885 { 1886 bc_num_setToZero(c, scale); 1887 return; 1888 } 1889 1890 if (BC_NUM_ONE(b)) 1891 { 1892 bc_num_copy(c, a); 1893 bc_num_retireMul(c, scale, BC_NUM_NEG(a), BC_NUM_NEG(b)); 1894 return; 1895 } 1896 1897 // If this is true, we can use bc_num_divArray(), which would be faster. 1898 if (!BC_NUM_RDX_VAL(a) && !BC_NUM_RDX_VAL(b) && b->len == 1 && !scale) 1899 { 1900 BcBigDig rem; 1901 bc_num_divArray(a, (BcBigDig) b->num[0], c, &rem); 1902 bc_num_retireMul(c, scale, BC_NUM_NEG(a), BC_NUM_NEG(b)); 1903 return; 1904 } 1905 1906 len = bc_num_divReq(a, b, scale); 1907 1908 BC_SIG_LOCK; 1909 1910 // Initialize copies of the parameters. We want the length of the first 1911 // operand copy to be as big as the result because of the way the division 1912 // is implemented. 1913 bc_num_init(&cpa, len); 1914 bc_num_copy(&cpa, a); 1915 bc_num_createCopy(&cpb, b); 1916 1917 BC_SETJMP_LOCKED(vm, err); 1918 1919 BC_SIG_UNLOCK; 1920 1921 len = b->len; 1922 1923 // Like the above comment, we want the copy of the first parameter to be 1924 // larger than the second parameter. 1925 if (len > cpa.len) 1926 { 1927 bc_num_expand(&cpa, bc_vm_growSize(len, 2)); 1928 bc_num_extend(&cpa, (len - cpa.len) * BC_BASE_DIGS); 1929 } 1930 1931 cpardx = BC_NUM_RDX_VAL_NP(cpa); 1932 cpa.scale = cpardx * BC_BASE_DIGS; 1933 1934 // This is just setting up the scale in preparation for the division. 1935 bc_num_extend(&cpa, b->scale); 1936 cpardx = BC_NUM_RDX_VAL_NP(cpa) - BC_NUM_RDX(b->scale); 1937 BC_NUM_RDX_SET_NP(cpa, cpardx); 1938 cpa.scale = cpardx * BC_BASE_DIGS; 1939 1940 // Once again, just setting things up, this time to match scale. 1941 if (scale > cpa.scale) 1942 { 1943 bc_num_extend(&cpa, scale); 1944 cpardx = BC_NUM_RDX_VAL_NP(cpa); 1945 cpa.scale = cpardx * BC_BASE_DIGS; 1946 } 1947 1948 // Grow if necessary. 1949 if (cpa.cap == cpa.len) bc_num_expand(&cpa, bc_vm_growSize(cpa.len, 1)); 1950 1951 // We want an extra zero in front to make things simpler. 1952 cpa.num[cpa.len++] = 0; 1953 1954 // Still setting things up. Why all of these things are needed is not 1955 // something that can be easily explained, but it has to do with making the 1956 // actual algorithm easier to understand because it can assume a lot of 1957 // things. Thus, you should view all of this setup code as establishing 1958 // assumptions for bc_num_d_long(), where the actual division happens. 1959 // 1960 // But in short, this setup makes it so bc_num_d_long() can pretend the 1961 // numbers are integers. 1962 if (cpardx == cpa.len) cpa.len = bc_num_nonZeroLen(&cpa); 1963 if (BC_NUM_RDX_VAL_NP(cpb) == cpb.len) cpb.len = bc_num_nonZeroLen(&cpb); 1964 cpb.scale = 0; 1965 BC_NUM_RDX_SET_NP(cpb, 0); 1966 1967 bc_num_d_long(&cpa, &cpb, c, scale); 1968 1969 bc_num_retireMul(c, scale, BC_NUM_NEG(a), BC_NUM_NEG(b)); 1970 1971 err: 1972 BC_SIG_MAYLOCK; 1973 bc_num_free(&cpb); 1974 bc_num_free(&cpa); 1975 BC_LONGJMP_CONT(vm); 1976 } 1977 1978 /** 1979 * Implements divmod. This is the actual modulus function; since modulus 1980 * requires a division anyway, this returns the quotient and modulus. Either can 1981 * be thrown out as desired. 1982 * @param a The first operand. 1983 * @param b The second operand. 1984 * @param c The return parameter for the quotient. 1985 * @param d The return parameter for the modulus. 1986 * @param scale The current scale. 1987 * @param ts The scale that the operation should be done to. Yes, it's not 1988 * necessarily the same as scale, per the bc spec. 1989 */ 1990 static void 1991 bc_num_r(BcNum* a, BcNum* b, BcNum* restrict c, BcNum* restrict d, size_t scale, 1992 size_t ts) 1993 { 1994 BcNum temp; 1995 // realscale is meant to quiet a warning on GCC about longjmp() clobbering. 1996 // This one is real. 1997 size_t realscale; 1998 bool neg; 1999 #if BC_ENABLE_LIBRARY 2000 BcVm* vm = bcl_getspecific(); 2001 #endif // BC_ENABLE_LIBRARY 2002 2003 if (BC_NUM_ZERO(b)) bc_err(BC_ERR_MATH_DIVIDE_BY_ZERO); 2004 2005 if (BC_NUM_ZERO(a)) 2006 { 2007 bc_num_setToZero(c, ts); 2008 bc_num_setToZero(d, ts); 2009 return; 2010 } 2011 2012 BC_SIG_LOCK; 2013 2014 bc_num_init(&temp, d->cap); 2015 2016 BC_SETJMP_LOCKED(vm, err); 2017 2018 BC_SIG_UNLOCK; 2019 2020 // Division. 2021 bc_num_d(a, b, c, scale); 2022 2023 // We want an extra digit so we can safely truncate. 2024 if (scale) realscale = ts + 1; 2025 else realscale = scale; 2026 2027 assert(BC_NUM_RDX_VALID(c)); 2028 assert(BC_NUM_RDX_VALID(b)); 2029 2030 // Implement the rest of the (a - (a / b) * b) formula. 2031 bc_num_m(c, b, &temp, realscale); 2032 bc_num_sub(a, &temp, d, realscale); 2033 2034 // Extend if necessary. 2035 if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale); 2036 2037 neg = BC_NUM_NEG(d); 2038 bc_num_retireMul(d, ts, BC_NUM_NEG(a), BC_NUM_NEG(b)); 2039 d->rdx = BC_NUM_NEG_VAL(d, BC_NUM_NONZERO(d) ? neg : false); 2040 2041 err: 2042 BC_SIG_MAYLOCK; 2043 bc_num_free(&temp); 2044 BC_LONGJMP_CONT(vm); 2045 } 2046 2047 /** 2048 * Implements modulus/remainder. (Yes, I know they are different, but not in the 2049 * context of bc.) This is a BcNumBinOp function. 2050 * @param a The first operand. 2051 * @param b The second operand. 2052 * @param c The return parameter. 2053 * @param scale The current scale. 2054 */ 2055 static void 2056 bc_num_rem(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale) 2057 { 2058 BcNum c1; 2059 size_t ts; 2060 #if BC_ENABLE_LIBRARY 2061 BcVm* vm = bcl_getspecific(); 2062 #endif // BC_ENABLE_LIBRARY 2063 2064 ts = bc_vm_growSize(scale, b->scale); 2065 ts = BC_MAX(ts, a->scale); 2066 2067 BC_SIG_LOCK; 2068 2069 // Need a temp for the quotient. 2070 bc_num_init(&c1, bc_num_mulReq(a, b, ts)); 2071 2072 BC_SETJMP_LOCKED(vm, err); 2073 2074 BC_SIG_UNLOCK; 2075 2076 bc_num_r(a, b, &c1, c, scale, ts); 2077 2078 err: 2079 BC_SIG_MAYLOCK; 2080 bc_num_free(&c1); 2081 BC_LONGJMP_CONT(vm); 2082 } 2083 2084 /** 2085 * Implements power (exponentiation). This is a BcNumBinOp function. 2086 * @param a The first operand. 2087 * @param b The second operand. 2088 * @param c The return parameter. 2089 * @param scale The current scale. 2090 */ 2091 static void 2092 bc_num_p(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale) 2093 { 2094 BcNum copy, btemp; 2095 BcBigDig exp; 2096 // realscale is meant to quiet a warning on GCC about longjmp() clobbering. 2097 // This one is real. 2098 size_t powrdx, resrdx, realscale; 2099 bool neg; 2100 #if BC_ENABLE_LIBRARY 2101 BcVm* vm = bcl_getspecific(); 2102 #endif // BC_ENABLE_LIBRARY 2103 2104 // This is here to silence a warning from GCC. 2105 #if BC_GCC 2106 btemp.len = 0; 2107 btemp.rdx = 0; 2108 btemp.num = NULL; 2109 #endif // BC_GCC 2110 2111 if (BC_ERR(bc_num_nonInt(b, &btemp))) bc_err(BC_ERR_MATH_NON_INTEGER); 2112 2113 assert(btemp.len == 0 || btemp.num != NULL); 2114 2115 if (BC_NUM_ZERO(&btemp)) 2116 { 2117 bc_num_one(c); 2118 return; 2119 } 2120 2121 if (BC_NUM_ZERO(a)) 2122 { 2123 if (BC_NUM_NEG_NP(btemp)) bc_err(BC_ERR_MATH_DIVIDE_BY_ZERO); 2124 bc_num_setToZero(c, scale); 2125 return; 2126 } 2127 2128 if (BC_NUM_ONE(&btemp)) 2129 { 2130 if (!BC_NUM_NEG_NP(btemp)) bc_num_copy(c, a); 2131 else bc_num_inv(a, c, scale); 2132 return; 2133 } 2134 2135 neg = BC_NUM_NEG_NP(btemp); 2136 BC_NUM_NEG_CLR_NP(btemp); 2137 2138 exp = bc_num_bigdig(&btemp); 2139 2140 BC_SIG_LOCK; 2141 2142 bc_num_createCopy(©, a); 2143 2144 BC_SETJMP_LOCKED(vm, err); 2145 2146 BC_SIG_UNLOCK; 2147 2148 // If this is true, then we do not have to do a division, and we need to 2149 // set scale accordingly. 2150 if (!neg) 2151 { 2152 size_t max = BC_MAX(scale, a->scale), scalepow; 2153 scalepow = bc_num_mulOverflow(a->scale, exp); 2154 realscale = BC_MIN(scalepow, max); 2155 } 2156 else realscale = scale; 2157 2158 // This is only implementing the first exponentiation by squaring, until it 2159 // reaches the first time where the square is actually used. 2160 for (powrdx = a->scale; !(exp & 1); exp >>= 1) 2161 { 2162 powrdx <<= 1; 2163 assert(BC_NUM_RDX_VALID_NP(copy)); 2164 bc_num_mul(©, ©, ©, powrdx); 2165 } 2166 2167 // Make c a copy of copy for the purpose of saving the squares that should 2168 // be saved. 2169 bc_num_copy(c, ©); 2170 resrdx = powrdx; 2171 2172 // Now finish the exponentiation by squaring, this time saving the squares 2173 // as necessary. 2174 while (exp >>= 1) 2175 { 2176 powrdx <<= 1; 2177 assert(BC_NUM_RDX_VALID_NP(copy)); 2178 bc_num_mul(©, ©, ©, powrdx); 2179 2180 // If this is true, we want to save that particular square. This does 2181 // that by multiplying c with copy. 2182 if (exp & 1) 2183 { 2184 resrdx += powrdx; 2185 assert(BC_NUM_RDX_VALID(c)); 2186 assert(BC_NUM_RDX_VALID_NP(copy)); 2187 bc_num_mul(c, ©, c, resrdx); 2188 } 2189 } 2190 2191 // Invert if necessary. 2192 if (neg) bc_num_inv(c, c, realscale); 2193 2194 // Truncate if necessary. 2195 if (c->scale > realscale) bc_num_truncate(c, c->scale - realscale); 2196 2197 bc_num_clean(c); 2198 2199 err: 2200 BC_SIG_MAYLOCK; 2201 bc_num_free(©); 2202 BC_LONGJMP_CONT(vm); 2203 } 2204 2205 #if BC_ENABLE_EXTRA_MATH 2206 /** 2207 * Implements the places operator. This is a BcNumBinOp function. 2208 * @param a The first operand. 2209 * @param b The second operand. 2210 * @param c The return parameter. 2211 * @param scale The current scale. 2212 */ 2213 static void 2214 bc_num_place(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale) 2215 { 2216 BcBigDig val; 2217 2218 BC_UNUSED(scale); 2219 2220 val = bc_num_intop(a, b, c); 2221 2222 // Just truncate or extend as appropriate. 2223 if (val < c->scale) bc_num_truncate(c, c->scale - val); 2224 else if (val > c->scale) bc_num_extend(c, val - c->scale); 2225 } 2226 2227 /** 2228 * Implements the left shift operator. This is a BcNumBinOp function. 2229 */ 2230 static void 2231 bc_num_left(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale) 2232 { 2233 BcBigDig val; 2234 2235 BC_UNUSED(scale); 2236 2237 val = bc_num_intop(a, b, c); 2238 2239 bc_num_shiftLeft(c, (size_t) val); 2240 } 2241 2242 /** 2243 * Implements the right shift operator. This is a BcNumBinOp function. 2244 */ 2245 static void 2246 bc_num_right(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale) 2247 { 2248 BcBigDig val; 2249 2250 BC_UNUSED(scale); 2251 2252 val = bc_num_intop(a, b, c); 2253 2254 if (BC_NUM_ZERO(c)) return; 2255 2256 bc_num_shiftRight(c, (size_t) val); 2257 } 2258 #endif // BC_ENABLE_EXTRA_MATH 2259 2260 /** 2261 * Prepares for, and calls, a binary operator function. This is probably the 2262 * most important function in the entire file because it establishes assumptions 2263 * that make the rest of the code so easy. Those assumptions include: 2264 * 2265 * - a is not the same pointer as c. 2266 * - b is not the same pointer as c. 2267 * - there is enough room in c for the result. 2268 * 2269 * Without these, this whole function would basically have to be duplicated for 2270 * *all* binary operators. 2271 * 2272 * @param a The first operand. 2273 * @param b The second operand. 2274 * @param c The return parameter. 2275 * @param scale The current scale. 2276 * @param req The number of limbs needed to fit the result. 2277 */ 2278 static void 2279 bc_num_binary(BcNum* a, BcNum* b, BcNum* c, size_t scale, BcNumBinOp op, 2280 size_t req) 2281 { 2282 BcNum* ptr_a; 2283 BcNum* ptr_b; 2284 BcNum num2; 2285 #if BC_ENABLE_LIBRARY 2286 BcVm* vm = NULL; 2287 #endif // BC_ENABLE_LIBRARY 2288 2289 assert(a != NULL && b != NULL && c != NULL && op != NULL); 2290 2291 assert(BC_NUM_RDX_VALID(a)); 2292 assert(BC_NUM_RDX_VALID(b)); 2293 2294 BC_SIG_LOCK; 2295 2296 ptr_a = c == a ? &num2 : a; 2297 ptr_b = c == b ? &num2 : b; 2298 2299 // Actually reallocate. If we don't reallocate, we want to expand at the 2300 // very least. 2301 if (c == a || c == b) 2302 { 2303 #if BC_ENABLE_LIBRARY 2304 vm = bcl_getspecific(); 2305 #endif // BC_ENABLE_LIBRARY 2306 2307 // NOLINTNEXTLINE 2308 memcpy(&num2, c, sizeof(BcNum)); 2309 2310 bc_num_init(c, req); 2311 2312 // Must prepare for cleanup. We want this here so that locals that got 2313 // set stay set since a longjmp() is not guaranteed to preserve locals. 2314 BC_SETJMP_LOCKED(vm, err); 2315 BC_SIG_UNLOCK; 2316 } 2317 else 2318 { 2319 BC_SIG_UNLOCK; 2320 bc_num_expand(c, req); 2321 } 2322 2323 // It is okay for a and b to be the same. If a binary operator function does 2324 // need them to be different, the binary operator function is responsible 2325 // for that. 2326 2327 // Call the actual binary operator function. 2328 op(ptr_a, ptr_b, c, scale); 2329 2330 assert(!BC_NUM_NEG(c) || BC_NUM_NONZERO(c)); 2331 assert(BC_NUM_RDX_VAL(c) <= c->len || !c->len); 2332 assert(BC_NUM_RDX_VALID(c)); 2333 assert(!c->len || c->num[c->len - 1] || BC_NUM_RDX_VAL(c) == c->len); 2334 2335 err: 2336 // Cleanup only needed if we initialized c to a new number. 2337 if (c == a || c == b) 2338 { 2339 BC_SIG_MAYLOCK; 2340 bc_num_free(&num2); 2341 BC_LONGJMP_CONT(vm); 2342 } 2343 } 2344 2345 /** 2346 * Tests a number string for validity. This function has a history; I originally 2347 * wrote it because I did not trust my parser. Over time, however, I came to 2348 * trust it, so I was able to relegate this function to debug builds only, and I 2349 * used it in assert()'s. But then I created the library, and well, I can't 2350 * trust users, so I reused this for yelling at users. 2351 * @param val The string to check to see if it's a valid number string. 2352 * @return True if the string is a valid number string, false otherwise. 2353 */ 2354 bool 2355 bc_num_strValid(const char* restrict val) 2356 { 2357 bool radix = false; 2358 size_t i, len = strlen(val); 2359 2360 // Notice that I don't check if there is a negative sign. That is not part 2361 // of a valid number, except in the library. The library-specific code takes 2362 // care of that part. 2363 2364 // Nothing in the string is okay. 2365 if (!len) return true; 2366 2367 // Loop through the characters. 2368 for (i = 0; i < len; ++i) 2369 { 2370 BcDig c = val[i]; 2371 2372 // If we have found a radix point... 2373 if (c == '.') 2374 { 2375 // We don't allow two radices. 2376 if (radix) return false; 2377 2378 radix = true; 2379 continue; 2380 } 2381 2382 // We only allow digits and uppercase letters. 2383 if (!(isdigit(c) || isupper(c))) return false; 2384 } 2385 2386 return true; 2387 } 2388 2389 /** 2390 * Parses one character and returns the digit that corresponds to that 2391 * character according to the base. 2392 * @param c The character to parse. 2393 * @param base The base. 2394 * @return The character as a digit. 2395 */ 2396 static BcBigDig 2397 bc_num_parseChar(char c, size_t base) 2398 { 2399 assert(isupper(c) || isdigit(c)); 2400 2401 // If a letter... 2402 if (isupper(c)) 2403 { 2404 #if BC_ENABLE_LIBRARY 2405 BcVm* vm = bcl_getspecific(); 2406 #endif // BC_ENABLE_LIBRARY 2407 2408 // This returns the digit that directly corresponds with the letter. 2409 c = BC_NUM_NUM_LETTER(c); 2410 2411 // If the digit is greater than the base, we clamp. 2412 if (BC_DIGIT_CLAMP) 2413 { 2414 c = ((size_t) c) >= base ? (char) base - 1 : c; 2415 } 2416 } 2417 // Straight convert the digit to a number. 2418 else c -= '0'; 2419 2420 return (BcBigDig) (uchar) c; 2421 } 2422 2423 /** 2424 * Parses a string as a decimal number. This is separate because it's going to 2425 * be the most used, and it can be heavily optimized for decimal only. 2426 * @param n The number to parse into and return. Must be preallocated. 2427 * @param val The string to parse. 2428 */ 2429 static void 2430 bc_num_parseDecimal(BcNum* restrict n, const char* restrict val) 2431 { 2432 size_t len, i, temp, mod; 2433 const char* ptr; 2434 bool zero = true, rdx; 2435 #if BC_ENABLE_LIBRARY 2436 BcVm* vm = bcl_getspecific(); 2437 #endif // BC_ENABLE_LIBRARY 2438 2439 // Eat leading zeroes. 2440 for (i = 0; val[i] == '0'; ++i) 2441 { 2442 continue; 2443 } 2444 2445 val += i; 2446 assert(!val[0] || isalnum(val[0]) || val[0] == '.'); 2447 2448 // All 0's. We can just return, since this procedure expects a virgin 2449 // (already 0) BcNum. 2450 if (!val[0]) return; 2451 2452 // The length of the string is the length of the number, except it might be 2453 // one bigger because of a decimal point. 2454 len = strlen(val); 2455 2456 // Find the location of the decimal point. 2457 ptr = strchr(val, '.'); 2458 rdx = (ptr != NULL); 2459 2460 // We eat leading zeroes again. These leading zeroes are different because 2461 // they will come after the decimal point if they exist, and since that's 2462 // the case, they must be preserved. 2463 for (i = 0; i < len && (zero = (val[i] == '0' || val[i] == '.')); ++i) 2464 { 2465 continue; 2466 } 2467 2468 // Set the scale of the number based on the location of the decimal point. 2469 // The casts to uintptr_t is to ensure that bc does not hit undefined 2470 // behavior when doing math on the values. 2471 n->scale = (size_t) (rdx * 2472 (((uintptr_t) (val + len)) - (((uintptr_t) ptr) + 1))); 2473 2474 // Set rdx. 2475 BC_NUM_RDX_SET(n, BC_NUM_RDX(n->scale)); 2476 2477 // Calculate length. First, the length of the integer, then the number of 2478 // digits in the last limb, then the length. 2479 i = len - (ptr == val ? 0 : i) - rdx; 2480 temp = BC_NUM_ROUND_POW(i); 2481 mod = n->scale % BC_BASE_DIGS; 2482 i = mod ? BC_BASE_DIGS - mod : 0; 2483 n->len = ((temp + i) / BC_BASE_DIGS); 2484 2485 // Expand and zero. The plus extra is in case the lack of clamping causes 2486 // the number to overflow the original bounds. 2487 bc_num_expand(n, n->len + !BC_DIGIT_CLAMP); 2488 // NOLINTNEXTLINE 2489 memset(n->num, 0, BC_NUM_SIZE(n->len + !BC_DIGIT_CLAMP)); 2490 2491 if (zero) 2492 { 2493 // I think I can set rdx directly to zero here because n should be a 2494 // new number with sign set to false. 2495 n->len = n->rdx = 0; 2496 } 2497 else 2498 { 2499 // There is actually stuff to parse if we make it here. Yay... 2500 BcBigDig exp, pow; 2501 2502 assert(i <= BC_NUM_BIGDIG_MAX); 2503 2504 // The exponent and power. 2505 exp = (BcBigDig) i; 2506 pow = bc_num_pow10[exp]; 2507 2508 // Parse loop. We parse backwards because numbers are stored little 2509 // endian. 2510 for (i = len - 1; i < len; --i, ++exp) 2511 { 2512 char c = val[i]; 2513 2514 // Skip the decimal point. 2515 if (c == '.') exp -= 1; 2516 else 2517 { 2518 // The index of the limb. 2519 size_t idx = exp / BC_BASE_DIGS; 2520 BcBigDig dig; 2521 2522 if (isupper(c)) 2523 { 2524 // Clamp for the base. 2525 if (!BC_DIGIT_CLAMP) c = BC_NUM_NUM_LETTER(c); 2526 else c = 9; 2527 } 2528 else c -= '0'; 2529 2530 // Add the digit to the limb. This takes care of overflow from 2531 // lack of clamping. 2532 dig = ((BcBigDig) n->num[idx]) + ((BcBigDig) c) * pow; 2533 if (dig >= BC_BASE_POW) 2534 { 2535 // We cannot go over BC_BASE_POW with clamping. 2536 assert(!BC_DIGIT_CLAMP); 2537 2538 n->num[idx + 1] = (BcDig) (dig / BC_BASE_POW); 2539 n->num[idx] = (BcDig) (dig % BC_BASE_POW); 2540 assert(n->num[idx] >= 0 && n->num[idx] < BC_BASE_POW); 2541 assert(n->num[idx + 1] >= 0 && 2542 n->num[idx + 1] < BC_BASE_POW); 2543 } 2544 else 2545 { 2546 n->num[idx] = (BcDig) dig; 2547 assert(n->num[idx] >= 0 && n->num[idx] < BC_BASE_POW); 2548 } 2549 2550 // Adjust the power and exponent. 2551 if ((exp + 1) % BC_BASE_DIGS == 0) pow = 1; 2552 else pow *= BC_BASE; 2553 } 2554 } 2555 } 2556 2557 // Make sure to add one to the length if needed from lack of clamping. 2558 n->len += (!BC_DIGIT_CLAMP && n->num[n->len] != 0); 2559 } 2560 2561 /** 2562 * Parse a number in any base (besides decimal). 2563 * @param n The number to parse into and return. Must be preallocated. 2564 * @param val The string to parse. 2565 * @param base The base to parse as. 2566 */ 2567 static void 2568 bc_num_parseBase(BcNum* restrict n, const char* restrict val, BcBigDig base) 2569 { 2570 BcNum temp, mult1, mult2, result1, result2; 2571 BcNum* m1; 2572 BcNum* m2; 2573 BcNum* ptr; 2574 char c = 0; 2575 bool zero = true; 2576 BcBigDig v; 2577 size_t digs, len = strlen(val); 2578 // This is volatile to quiet a warning on GCC about longjmp() clobbering. 2579 volatile size_t i; 2580 #if BC_ENABLE_LIBRARY 2581 BcVm* vm = bcl_getspecific(); 2582 #endif // BC_ENABLE_LIBRARY 2583 2584 // If zero, just return because the number should be virgin (already 0). 2585 for (i = 0; zero && i < len; ++i) 2586 { 2587 zero = (val[i] == '.' || val[i] == '0'); 2588 } 2589 if (zero) return; 2590 2591 BC_SIG_LOCK; 2592 2593 bc_num_init(&temp, BC_NUM_BIGDIG_LOG10); 2594 bc_num_init(&mult1, BC_NUM_BIGDIG_LOG10); 2595 2596 BC_SETJMP_LOCKED(vm, int_err); 2597 2598 BC_SIG_UNLOCK; 2599 2600 // We split parsing into parsing the integer and parsing the fractional 2601 // part. 2602 2603 // Parse the integer part. This is the easy part because we just multiply 2604 // the number by the base, then add the digit. 2605 for (i = 0; i < len && (c = val[i]) && c != '.'; ++i) 2606 { 2607 // Convert the character to a digit. 2608 v = bc_num_parseChar(c, base); 2609 2610 // Multiply the number. 2611 bc_num_mulArray(n, base, &mult1); 2612 2613 // Convert the digit to a number and add. 2614 bc_num_bigdig2num(&temp, v); 2615 bc_num_add(&mult1, &temp, n, 0); 2616 } 2617 2618 // If this condition is true, then we are done. We still need to do cleanup 2619 // though. 2620 if (i == len && !val[i]) goto int_err; 2621 2622 // If we get here, we *must* be at the radix point. 2623 assert(val[i] == '.'); 2624 2625 BC_SIG_LOCK; 2626 2627 // Unset the jump to reset in for these new initializations. 2628 BC_UNSETJMP(vm); 2629 2630 bc_num_init(&mult2, BC_NUM_BIGDIG_LOG10); 2631 bc_num_init(&result1, BC_NUM_DEF_SIZE); 2632 bc_num_init(&result2, BC_NUM_DEF_SIZE); 2633 bc_num_one(&mult1); 2634 2635 BC_SETJMP_LOCKED(vm, err); 2636 2637 BC_SIG_UNLOCK; 2638 2639 // Pointers for easy switching. 2640 m1 = &mult1; 2641 m2 = &mult2; 2642 2643 // Parse the fractional part. This is the hard part. 2644 for (i += 1, digs = 0; i < len && (c = val[i]); ++i, ++digs) 2645 { 2646 size_t rdx; 2647 2648 // Convert the character to a digit. 2649 v = bc_num_parseChar(c, base); 2650 2651 // We keep growing result2 according to the base because the more digits 2652 // after the radix, the more significant the digits close to the radix 2653 // should be. 2654 bc_num_mulArray(&result1, base, &result2); 2655 2656 // Convert the digit to a number. 2657 bc_num_bigdig2num(&temp, v); 2658 2659 // Add the digit into the fraction part. 2660 bc_num_add(&result2, &temp, &result1, 0); 2661 2662 // Keep growing m1 and m2 for use after the loop. 2663 bc_num_mulArray(m1, base, m2); 2664 2665 rdx = BC_NUM_RDX_VAL(m2); 2666 2667 if (m2->len < rdx) m2->len = rdx; 2668 2669 // Switch. 2670 ptr = m1; 2671 m1 = m2; 2672 m2 = ptr; 2673 } 2674 2675 // This one cannot be a divide by 0 because mult starts out at 1, then is 2676 // multiplied by base, and base cannot be 0, so mult cannot be 0. And this 2677 // is the reason we keep growing m1 and m2; this division is what converts 2678 // the parsed fractional part from an integer to a fractional part. 2679 bc_num_div(&result1, m1, &result2, digs * 2); 2680 2681 // Pretruncate. 2682 bc_num_truncate(&result2, digs); 2683 2684 // The final add of the integer part to the fractional part. 2685 bc_num_add(n, &result2, n, digs); 2686 2687 // Basic cleanup. 2688 if (BC_NUM_NONZERO(n)) 2689 { 2690 if (n->scale < digs) bc_num_extend(n, digs - n->scale); 2691 } 2692 else bc_num_zero(n); 2693 2694 err: 2695 BC_SIG_MAYLOCK; 2696 bc_num_free(&result2); 2697 bc_num_free(&result1); 2698 bc_num_free(&mult2); 2699 int_err: 2700 BC_SIG_MAYLOCK; 2701 bc_num_free(&mult1); 2702 bc_num_free(&temp); 2703 BC_LONGJMP_CONT(vm); 2704 } 2705 2706 /** 2707 * Prints a backslash+newline combo if the number of characters needs it. This 2708 * is really a convenience function. 2709 */ 2710 static inline void 2711 bc_num_printNewline(void) 2712 { 2713 #if !BC_ENABLE_LIBRARY 2714 if (vm->nchars >= vm->line_len - 1 && vm->line_len) 2715 { 2716 bc_vm_putchar('\\', bc_flush_none); 2717 bc_vm_putchar('\n', bc_flush_err); 2718 } 2719 #endif // !BC_ENABLE_LIBRARY 2720 } 2721 2722 /** 2723 * Prints a character after a backslash+newline, if needed. 2724 * @param c The character to print. 2725 * @param bslash Whether to print a backslash+newline. 2726 */ 2727 static void 2728 bc_num_putchar(int c, bool bslash) 2729 { 2730 if (c != '\n' && bslash) bc_num_printNewline(); 2731 bc_vm_putchar(c, bc_flush_save); 2732 } 2733 2734 #if !BC_ENABLE_LIBRARY 2735 2736 /** 2737 * Prints a character for a number's digit. This is for printing for dc's P 2738 * command. This function does not need to worry about radix points. This is a 2739 * BcNumDigitOp. 2740 * @param n The "digit" to print. 2741 * @param len The "length" of the digit, or number of characters that will 2742 * need to be printed for the digit. 2743 * @param rdx True if a decimal (radix) point should be printed. 2744 * @param bslash True if a backslash+newline should be printed if the character 2745 * limit for the line is reached, false otherwise. 2746 */ 2747 static void 2748 bc_num_printChar(size_t n, size_t len, bool rdx, bool bslash) 2749 { 2750 BC_UNUSED(rdx); 2751 BC_UNUSED(len); 2752 BC_UNUSED(bslash); 2753 assert(len == 1); 2754 bc_vm_putchar((uchar) n, bc_flush_save); 2755 } 2756 2757 #endif // !BC_ENABLE_LIBRARY 2758 2759 /** 2760 * Prints a series of characters for large bases. This is for printing in bases 2761 * above hexadecimal. This is a BcNumDigitOp. 2762 * @param n The "digit" to print. 2763 * @param len The "length" of the digit, or number of characters that will 2764 * need to be printed for the digit. 2765 * @param rdx True if a decimal (radix) point should be printed. 2766 * @param bslash True if a backslash+newline should be printed if the character 2767 * limit for the line is reached, false otherwise. 2768 */ 2769 static void 2770 bc_num_printDigits(size_t n, size_t len, bool rdx, bool bslash) 2771 { 2772 size_t exp, pow; 2773 2774 // If needed, print the radix; otherwise, print a space to separate digits. 2775 bc_num_putchar(rdx ? '.' : ' ', true); 2776 2777 // Calculate the exponent and power. 2778 for (exp = 0, pow = 1; exp < len - 1; ++exp, pow *= BC_BASE) 2779 { 2780 continue; 2781 } 2782 2783 // Print each character individually. 2784 for (exp = 0; exp < len; pow /= BC_BASE, ++exp) 2785 { 2786 // The individual subdigit. 2787 size_t dig = n / pow; 2788 2789 // Take the subdigit away. 2790 n -= dig * pow; 2791 2792 // Print the subdigit. 2793 bc_num_putchar(((uchar) dig) + '0', bslash || exp != len - 1); 2794 } 2795 } 2796 2797 /** 2798 * Prints a character for a number's digit. This is for printing in bases for 2799 * hexadecimal and below because they always print only one character at a time. 2800 * This is a BcNumDigitOp. 2801 * @param n The "digit" to print. 2802 * @param len The "length" of the digit, or number of characters that will 2803 * need to be printed for the digit. 2804 * @param rdx True if a decimal (radix) point should be printed. 2805 * @param bslash True if a backslash+newline should be printed if the character 2806 * limit for the line is reached, false otherwise. 2807 */ 2808 static void 2809 bc_num_printHex(size_t n, size_t len, bool rdx, bool bslash) 2810 { 2811 BC_UNUSED(len); 2812 BC_UNUSED(bslash); 2813 2814 assert(len == 1); 2815 2816 if (rdx) bc_num_putchar('.', true); 2817 2818 bc_num_putchar(bc_num_hex_digits[n], bslash); 2819 } 2820 2821 /** 2822 * Prints a decimal number. This is specially written for optimization since 2823 * this will be used the most and because bc's numbers are already in decimal. 2824 * @param n The number to print. 2825 * @param newline Whether to print backslash+newlines on long enough lines. 2826 */ 2827 static void 2828 bc_num_printDecimal(const BcNum* restrict n, bool newline) 2829 { 2830 size_t i, j, rdx = BC_NUM_RDX_VAL(n); 2831 bool zero = true; 2832 size_t buffer[BC_BASE_DIGS]; 2833 2834 // Print loop. 2835 for (i = n->len - 1; i < n->len; --i) 2836 { 2837 BcDig n9 = n->num[i]; 2838 size_t temp; 2839 bool irdx = (i == rdx - 1); 2840 2841 // Calculate the number of digits in the limb. 2842 zero = (zero & !irdx); 2843 temp = n->scale % BC_BASE_DIGS; 2844 temp = i || !temp ? 0 : BC_BASE_DIGS - temp; 2845 2846 // NOLINTNEXTLINE 2847 memset(buffer, 0, BC_BASE_DIGS * sizeof(size_t)); 2848 2849 // Fill the buffer with individual digits. 2850 for (j = 0; n9 && j < BC_BASE_DIGS; ++j) 2851 { 2852 buffer[j] = ((size_t) n9) % BC_BASE; 2853 n9 /= BC_BASE; 2854 } 2855 2856 // Print the digits in the buffer. 2857 for (j = BC_BASE_DIGS - 1; j < BC_BASE_DIGS && j >= temp; --j) 2858 { 2859 // Figure out whether to print the decimal point. 2860 bool print_rdx = (irdx & (j == BC_BASE_DIGS - 1)); 2861 2862 // The zero variable helps us skip leading zero digits in the limb. 2863 zero = (zero && buffer[j] == 0); 2864 2865 if (!zero) 2866 { 2867 // While the first three arguments should be self-explanatory, 2868 // the last needs explaining. I don't want to print a newline 2869 // when the last digit to be printed could take the place of the 2870 // backslash rather than being pushed, as a single character, to 2871 // the next line. That's what that last argument does for bc. 2872 bc_num_printHex(buffer[j], 1, print_rdx, 2873 !newline || (j > temp || i != 0)); 2874 } 2875 } 2876 } 2877 } 2878 2879 #if BC_ENABLE_EXTRA_MATH 2880 2881 /** 2882 * Prints a number in scientific or engineering format. When doing this, we are 2883 * always printing in decimal. 2884 * @param n The number to print. 2885 * @param eng True if we are in engineering mode. 2886 * @param newline Whether to print backslash+newlines on long enough lines. 2887 */ 2888 static void 2889 bc_num_printExponent(const BcNum* restrict n, bool eng, bool newline) 2890 { 2891 size_t places, mod, nrdx = BC_NUM_RDX_VAL(n); 2892 bool neg = (n->len <= nrdx); 2893 BcNum temp, exp; 2894 BcDig digs[BC_NUM_BIGDIG_LOG10]; 2895 #if BC_ENABLE_LIBRARY 2896 BcVm* vm = bcl_getspecific(); 2897 #endif // BC_ENABLE_LIBRARY 2898 2899 BC_SIG_LOCK; 2900 2901 bc_num_createCopy(&temp, n); 2902 2903 BC_SETJMP_LOCKED(vm, exit); 2904 2905 BC_SIG_UNLOCK; 2906 2907 // We need to calculate the exponents, and they change based on whether the 2908 // number is all fractional or not, obviously. 2909 if (neg) 2910 { 2911 // Figure out the negative power of 10. 2912 places = bc_num_negPow10(n); 2913 2914 // Figure out how many digits mod 3 there are (important for 2915 // engineering mode). 2916 mod = places % 3; 2917 2918 // Calculate places if we are in engineering mode. 2919 if (eng && mod != 0) places += 3 - mod; 2920 2921 // Shift the temp to the right place. 2922 bc_num_shiftLeft(&temp, places); 2923 } 2924 else 2925 { 2926 // This is the number of digits that we are supposed to put behind the 2927 // decimal point. 2928 places = bc_num_intDigits(n) - 1; 2929 2930 // Calculate the true number based on whether engineering mode is 2931 // activated. 2932 mod = places % 3; 2933 if (eng && mod != 0) places -= 3 - (3 - mod); 2934 2935 // Shift the temp to the right place. 2936 bc_num_shiftRight(&temp, places); 2937 } 2938 2939 // Print the shifted number. 2940 bc_num_printDecimal(&temp, newline); 2941 2942 // Print the e. 2943 bc_num_putchar('e', !newline); 2944 2945 // Need to explicitly print a zero exponent. 2946 if (!places) 2947 { 2948 bc_num_printHex(0, 1, false, !newline); 2949 goto exit; 2950 } 2951 2952 // Need to print sign for the exponent. 2953 if (neg) bc_num_putchar('-', true); 2954 2955 // Create a temporary for the exponent... 2956 bc_num_setup(&exp, digs, BC_NUM_BIGDIG_LOG10); 2957 bc_num_bigdig2num(&exp, (BcBigDig) places); 2958 2959 /// ..and print it. 2960 bc_num_printDecimal(&exp, newline); 2961 2962 exit: 2963 BC_SIG_MAYLOCK; 2964 bc_num_free(&temp); 2965 BC_LONGJMP_CONT(vm); 2966 } 2967 #endif // BC_ENABLE_EXTRA_MATH 2968 2969 /** 2970 * Takes a number with limbs with base BC_BASE_POW and converts the limb at the 2971 * given index to base @a pow, where @a pow is obase^N. 2972 * @param n The number to convert. 2973 * @param rem BC_BASE_POW - @a pow. 2974 * @param pow The power of obase we will convert the number to. 2975 * @param idx The index of the number to start converting at. Doing the 2976 * conversion is O(n^2); we have to sweep through starting at the 2977 * least significant limb. 2978 */ 2979 static void 2980 bc_num_printFixup(BcNum* restrict n, BcBigDig rem, BcBigDig pow, size_t idx) 2981 { 2982 size_t i, len = n->len - idx; 2983 BcBigDig acc; 2984 BcDig* a = n->num + idx; 2985 2986 // Ignore if there's just one limb left. This is the part that requires the 2987 // extra loop after the one calling this function in bc_num_printPrepare(). 2988 if (len < 2) return; 2989 2990 // Loop through the remaining limbs and convert. We start at the second limb 2991 // because we pull the value from the previous one as well. 2992 for (i = len - 1; i > 0; --i) 2993 { 2994 // Get the limb and add it to the previous, along with multiplying by 2995 // the remainder because that's the proper overflow. "acc" means 2996 // "accumulator," by the way. 2997 acc = ((BcBigDig) a[i]) * rem + ((BcBigDig) a[i - 1]); 2998 2999 // Store a value in base pow in the previous limb. 3000 a[i - 1] = (BcDig) (acc % pow); 3001 3002 // Divide by the base and accumulate the remaining value in the limb. 3003 acc /= pow; 3004 acc += (BcBigDig) a[i]; 3005 3006 // If the accumulator is greater than the base... 3007 if (acc >= BC_BASE_POW) 3008 { 3009 // Do we need to grow? 3010 if (i == len - 1) 3011 { 3012 // Grow. 3013 len = bc_vm_growSize(len, 1); 3014 bc_num_expand(n, bc_vm_growSize(len, idx)); 3015 3016 // Update the pointer because it may have moved. 3017 a = n->num + idx; 3018 3019 // Zero out the last limb. 3020 a[len - 1] = 0; 3021 } 3022 3023 // Overflow into the next limb since we are over the base. 3024 a[i + 1] += acc / BC_BASE_POW; 3025 acc %= BC_BASE_POW; 3026 } 3027 3028 assert(acc < BC_BASE_POW); 3029 3030 // Set the limb. 3031 a[i] = (BcDig) acc; 3032 } 3033 3034 // We may have grown the number, so adjust the length. 3035 n->len = len + idx; 3036 } 3037 3038 /** 3039 * Prepares a number for printing in a base that does not have BC_BASE_POW as a 3040 * power. This basically converts the number from having limbs of base 3041 * BC_BASE_POW to limbs of pow, where pow is obase^N. 3042 * @param n The number to prepare for printing. 3043 * @param rem The remainder of BC_BASE_POW when divided by a power of the base. 3044 * @param pow The power of the base. 3045 */ 3046 static void 3047 bc_num_printPrepare(BcNum* restrict n, BcBigDig rem, BcBigDig pow) 3048 { 3049 size_t i; 3050 3051 // Loop from the least significant limb to the most significant limb and 3052 // convert limbs in each pass. 3053 for (i = 0; i < n->len; ++i) 3054 { 3055 bc_num_printFixup(n, rem, pow, i); 3056 } 3057 3058 // bc_num_printFixup() does not do everything it is supposed to, so we do 3059 // the last bit of cleanup here. That cleanup is to ensure that each limb 3060 // is less than pow and to expand the number to fit new limbs as necessary. 3061 for (i = 0; i < n->len; ++i) 3062 { 3063 assert(pow == ((BcBigDig) ((BcDig) pow))); 3064 3065 // If the limb needs fixing... 3066 if (n->num[i] >= (BcDig) pow) 3067 { 3068 // Do we need to grow? 3069 if (i + 1 == n->len) 3070 { 3071 // Grow the number. 3072 n->len = bc_vm_growSize(n->len, 1); 3073 bc_num_expand(n, n->len); 3074 3075 // Without this, we might use uninitialized data. 3076 n->num[i + 1] = 0; 3077 } 3078 3079 assert(pow < BC_BASE_POW); 3080 3081 // Overflow into the next limb. 3082 n->num[i + 1] += n->num[i] / ((BcDig) pow); 3083 n->num[i] %= (BcDig) pow; 3084 } 3085 } 3086 } 3087 3088 static void 3089 bc_num_printNum(BcNum* restrict n, BcBigDig base, size_t len, 3090 BcNumDigitOp print, bool newline) 3091 { 3092 BcVec stack; 3093 BcNum intp, fracp1, fracp2, digit, flen1, flen2; 3094 BcNum* n1; 3095 BcNum* n2; 3096 BcNum* temp; 3097 BcBigDig dig = 0, acc, exp; 3098 BcBigDig* ptr; 3099 size_t i, j, nrdx, idigits; 3100 bool radix; 3101 BcDig digit_digs[BC_NUM_BIGDIG_LOG10 + 1]; 3102 #if BC_ENABLE_LIBRARY 3103 BcVm* vm = bcl_getspecific(); 3104 #endif // BC_ENABLE_LIBRARY 3105 3106 assert(base > 1); 3107 3108 // Easy case. Even with scale, we just print this. 3109 if (BC_NUM_ZERO(n)) 3110 { 3111 print(0, len, false, !newline); 3112 return; 3113 } 3114 3115 // This function uses an algorithm that Stefan Esser <se@freebsd.org> came 3116 // up with to print the integer part of a number. What it does is convert 3117 // intp into a number of the specified base, but it does it directly, 3118 // instead of just doing a series of divisions and printing the remainders 3119 // in reverse order. 3120 // 3121 // Let me explain in a bit more detail: 3122 // 3123 // The algorithm takes the current least significant limb (after intp has 3124 // been converted to an integer) and the next to least significant limb, and 3125 // it converts the least significant limb into one of the specified base, 3126 // putting any overflow into the next to least significant limb. It iterates 3127 // through the whole number, from least significant to most significant, 3128 // doing this conversion. At the end of that iteration, the least 3129 // significant limb is converted, but the others are not, so it iterates 3130 // again, starting at the next to least significant limb. It keeps doing 3131 // that conversion, skipping one more limb than the last time, until all 3132 // limbs have been converted. Then it prints them in reverse order. 3133 // 3134 // That is the gist of the algorithm. It leaves out several things, such as 3135 // the fact that limbs are not always converted into the specified base, but 3136 // into something close, basically a power of the specified base. In 3137 // Stefan's words, "You could consider BcDigs to be of base 10^BC_BASE_DIGS 3138 // in the normal case and obase^N for the largest value of N that satisfies 3139 // obase^N <= 10^BC_BASE_DIGS. [This means that] the result is not in base 3140 // "obase", but in base "obase^N", which happens to be printable as a number 3141 // of base "obase" without consideration for neighbouring BcDigs." This fact 3142 // is what necessitates the existence of the loop later in this function. 3143 // 3144 // The conversion happens in bc_num_printPrepare() where the outer loop 3145 // happens and bc_num_printFixup() where the inner loop, or actual 3146 // conversion, happens. In other words, bc_num_printPrepare() is where the 3147 // loop that starts at the least significant limb and goes to the most 3148 // significant limb. Then, on every iteration of its loop, it calls 3149 // bc_num_printFixup(), which has the inner loop of actually converting 3150 // the limbs it passes into limbs of base obase^N rather than base 3151 // BC_BASE_POW. 3152 3153 nrdx = BC_NUM_RDX_VAL(n); 3154 3155 BC_SIG_LOCK; 3156 3157 // The stack is what allows us to reverse the digits for printing. 3158 bc_vec_init(&stack, sizeof(BcBigDig), BC_DTOR_NONE); 3159 bc_num_init(&fracp1, nrdx); 3160 3161 // intp will be the "integer part" of the number, so copy it. 3162 bc_num_createCopy(&intp, n); 3163 3164 BC_SETJMP_LOCKED(vm, err); 3165 3166 BC_SIG_UNLOCK; 3167 3168 // Make intp an integer. 3169 bc_num_truncate(&intp, intp.scale); 3170 3171 // Get the fractional part out. 3172 bc_num_sub(n, &intp, &fracp1, 0); 3173 3174 // If the base is not the same as the last base used for printing, we need 3175 // to update the cached exponent and power. Yes, we cache the values of the 3176 // exponent and power. That is to prevent us from calculating them every 3177 // time because printing will probably happen multiple times on the same 3178 // base. 3179 if (base != vm->last_base) 3180 { 3181 vm->last_pow = 1; 3182 vm->last_exp = 0; 3183 3184 // Calculate the exponent and power. 3185 while (vm->last_pow * base <= BC_BASE_POW) 3186 { 3187 vm->last_pow *= base; 3188 vm->last_exp += 1; 3189 } 3190 3191 // Also, the remainder and base itself. 3192 vm->last_rem = BC_BASE_POW - vm->last_pow; 3193 vm->last_base = base; 3194 } 3195 3196 exp = vm->last_exp; 3197 3198 // If vm->last_rem is 0, then the base we are printing in is a divisor of 3199 // BC_BASE_POW, which is the easy case because it means that BC_BASE_POW is 3200 // a power of obase, and no conversion is needed. If it *is* 0, then we have 3201 // the hard case, and we have to prepare the number for the base. 3202 if (vm->last_rem != 0) 3203 { 3204 bc_num_printPrepare(&intp, vm->last_rem, vm->last_pow); 3205 } 3206 3207 // After the conversion comes the surprisingly easy part. From here on out, 3208 // this is basically naive code that I wrote, adjusted for the larger bases. 3209 3210 // Fill the stack of digits for the integer part. 3211 for (i = 0; i < intp.len; ++i) 3212 { 3213 // Get the limb. 3214 acc = (BcBigDig) intp.num[i]; 3215 3216 // Turn the limb into digits of base obase. 3217 for (j = 0; j < exp && (i < intp.len - 1 || acc != 0); ++j) 3218 { 3219 // This condition is true if we are not at the last digit. 3220 if (j != exp - 1) 3221 { 3222 dig = acc % base; 3223 acc /= base; 3224 } 3225 else 3226 { 3227 dig = acc; 3228 acc = 0; 3229 } 3230 3231 assert(dig < base); 3232 3233 // Push the digit onto the stack. 3234 bc_vec_push(&stack, &dig); 3235 } 3236 3237 assert(acc == 0); 3238 } 3239 3240 // Go through the stack backwards and print each digit. 3241 for (i = 0; i < stack.len; ++i) 3242 { 3243 ptr = bc_vec_item_rev(&stack, i); 3244 3245 assert(ptr != NULL); 3246 3247 // While the first three arguments should be self-explanatory, the last 3248 // needs explaining. I don't want to print a backslash+newline when the 3249 // last digit to be printed could take the place of the backslash rather 3250 // than being pushed, as a single character, to the next line. That's 3251 // what that last argument does for bc. 3252 // 3253 // First, it needs to check if newlines are completely disabled. If they 3254 // are not disabled, it needs to check the next part. 3255 // 3256 // If the number has a scale, then because we are printing just the 3257 // integer part, there will be at least two more characters (a radix 3258 // point plus at least one digit). So if there is a scale, a backslash 3259 // is necessary. 3260 // 3261 // Finally, the last condition checks to see if we are at the end of the 3262 // stack. If we are *not* (i.e., the index is not one less than the 3263 // stack length), then a backslash is necessary because there is at 3264 // least one more character for at least one more digit). Otherwise, if 3265 // the index is equal to one less than the stack length, we want to 3266 // disable backslash printing. 3267 // 3268 // The function that prints bases 17 and above will take care of not 3269 // printing a backslash in the right case. 3270 print(*ptr, len, false, 3271 !newline || (n->scale != 0 || i < stack.len - 1)); 3272 } 3273 3274 // We are done if there is no fractional part. 3275 if (!n->scale) goto err; 3276 3277 BC_SIG_LOCK; 3278 3279 // Reset the jump because some locals are changing. 3280 BC_UNSETJMP(vm); 3281 3282 bc_num_init(&fracp2, nrdx); 3283 bc_num_setup(&digit, digit_digs, sizeof(digit_digs) / sizeof(BcDig)); 3284 bc_num_init(&flen1, BC_NUM_BIGDIG_LOG10); 3285 bc_num_init(&flen2, BC_NUM_BIGDIG_LOG10); 3286 3287 BC_SETJMP_LOCKED(vm, frac_err); 3288 3289 BC_SIG_UNLOCK; 3290 3291 bc_num_one(&flen1); 3292 3293 radix = true; 3294 3295 // Pointers for easy switching. 3296 n1 = &flen1; 3297 n2 = &flen2; 3298 3299 fracp2.scale = n->scale; 3300 BC_NUM_RDX_SET_NP(fracp2, BC_NUM_RDX(fracp2.scale)); 3301 3302 // As long as we have not reached the scale of the number, keep printing. 3303 while ((idigits = bc_num_intDigits(n1)) <= n->scale) 3304 { 3305 // These numbers will keep growing. 3306 bc_num_expand(&fracp2, fracp1.len + 1); 3307 bc_num_mulArray(&fracp1, base, &fracp2); 3308 3309 nrdx = BC_NUM_RDX_VAL_NP(fracp2); 3310 3311 // Ensure an invariant. 3312 if (fracp2.len < nrdx) fracp2.len = nrdx; 3313 3314 // fracp is guaranteed to be non-negative and small enough. 3315 dig = bc_num_bigdig2(&fracp2); 3316 3317 // Convert the digit to a number and subtract it from the number. 3318 bc_num_bigdig2num(&digit, dig); 3319 bc_num_sub(&fracp2, &digit, &fracp1, 0); 3320 3321 // While the first three arguments should be self-explanatory, the last 3322 // needs explaining. I don't want to print a newline when the last digit 3323 // to be printed could take the place of the backslash rather than being 3324 // pushed, as a single character, to the next line. That's what that 3325 // last argument does for bc. 3326 print(dig, len, radix, !newline || idigits != n->scale); 3327 3328 // Update the multipliers. 3329 bc_num_mulArray(n1, base, n2); 3330 3331 radix = false; 3332 3333 // Switch. 3334 temp = n1; 3335 n1 = n2; 3336 n2 = temp; 3337 } 3338 3339 frac_err: 3340 BC_SIG_MAYLOCK; 3341 bc_num_free(&flen2); 3342 bc_num_free(&flen1); 3343 bc_num_free(&fracp2); 3344 err: 3345 BC_SIG_MAYLOCK; 3346 bc_num_free(&fracp1); 3347 bc_num_free(&intp); 3348 bc_vec_free(&stack); 3349 BC_LONGJMP_CONT(vm); 3350 } 3351 3352 /** 3353 * Prints a number in the specified base, or rather, figures out which function 3354 * to call to print the number in the specified base and calls it. 3355 * @param n The number to print. 3356 * @param base The base to print in. 3357 * @param newline Whether to print backslash+newlines on long enough lines. 3358 */ 3359 static void 3360 bc_num_printBase(BcNum* restrict n, BcBigDig base, bool newline) 3361 { 3362 size_t width; 3363 BcNumDigitOp print; 3364 bool neg = BC_NUM_NEG(n); 3365 3366 // Clear the sign because it makes the actual printing easier when we have 3367 // to do math. 3368 BC_NUM_NEG_CLR(n); 3369 3370 // Bases at hexadecimal and below are printed as one character, larger bases 3371 // are printed as a series of digits separated by spaces. 3372 if (base <= BC_NUM_MAX_POSIX_IBASE) 3373 { 3374 width = 1; 3375 print = bc_num_printHex; 3376 } 3377 else 3378 { 3379 assert(base <= BC_BASE_POW); 3380 width = bc_num_log10(base - 1); 3381 print = bc_num_printDigits; 3382 } 3383 3384 // Print. 3385 bc_num_printNum(n, base, width, print, newline); 3386 3387 // Reset the sign. 3388 n->rdx = BC_NUM_NEG_VAL(n, neg); 3389 } 3390 3391 #if !BC_ENABLE_LIBRARY 3392 3393 void 3394 bc_num_stream(BcNum* restrict n) 3395 { 3396 bc_num_printNum(n, BC_NUM_STREAM_BASE, 1, bc_num_printChar, false); 3397 } 3398 3399 #endif // !BC_ENABLE_LIBRARY 3400 3401 void 3402 bc_num_setup(BcNum* restrict n, BcDig* restrict num, size_t cap) 3403 { 3404 assert(n != NULL); 3405 n->num = num; 3406 n->cap = cap; 3407 bc_num_zero(n); 3408 } 3409 3410 void 3411 bc_num_init(BcNum* restrict n, size_t req) 3412 { 3413 BcDig* num; 3414 3415 BC_SIG_ASSERT_LOCKED; 3416 3417 assert(n != NULL); 3418 3419 // BC_NUM_DEF_SIZE is set to be about the smallest allocation size that 3420 // malloc() returns in practice, so just use it. 3421 req = req >= BC_NUM_DEF_SIZE ? req : BC_NUM_DEF_SIZE; 3422 3423 // If we can't use a temp, allocate. 3424 if (req != BC_NUM_DEF_SIZE) num = bc_vm_malloc(BC_NUM_SIZE(req)); 3425 else 3426 { 3427 num = bc_vm_getTemp() == NULL ? bc_vm_malloc(BC_NUM_SIZE(req)) : 3428 bc_vm_takeTemp(); 3429 } 3430 3431 bc_num_setup(n, num, req); 3432 } 3433 3434 void 3435 bc_num_clear(BcNum* restrict n) 3436 { 3437 n->num = NULL; 3438 n->cap = 0; 3439 } 3440 3441 void 3442 bc_num_free(void* num) 3443 { 3444 BcNum* n = (BcNum*) num; 3445 3446 BC_SIG_ASSERT_LOCKED; 3447 3448 assert(n != NULL); 3449 3450 if (n->cap == BC_NUM_DEF_SIZE) bc_vm_addTemp(n->num); 3451 else free(n->num); 3452 } 3453 3454 void 3455 bc_num_copy(BcNum* d, const BcNum* s) 3456 { 3457 assert(d != NULL && s != NULL); 3458 3459 if (d == s) return; 3460 3461 bc_num_expand(d, s->len); 3462 d->len = s->len; 3463 3464 // I can just copy directly here because the sign *and* rdx will be 3465 // properly preserved. 3466 d->rdx = s->rdx; 3467 d->scale = s->scale; 3468 // NOLINTNEXTLINE 3469 memcpy(d->num, s->num, BC_NUM_SIZE(d->len)); 3470 } 3471 3472 void 3473 bc_num_createCopy(BcNum* d, const BcNum* s) 3474 { 3475 BC_SIG_ASSERT_LOCKED; 3476 bc_num_init(d, s->len); 3477 bc_num_copy(d, s); 3478 } 3479 3480 void 3481 bc_num_createFromBigdig(BcNum* restrict n, BcBigDig val) 3482 { 3483 BC_SIG_ASSERT_LOCKED; 3484 bc_num_init(n, BC_NUM_BIGDIG_LOG10); 3485 bc_num_bigdig2num(n, val); 3486 } 3487 3488 size_t 3489 bc_num_scale(const BcNum* restrict n) 3490 { 3491 return n->scale; 3492 } 3493 3494 size_t 3495 bc_num_len(const BcNum* restrict n) 3496 { 3497 size_t len = n->len; 3498 3499 // Always return at least 1. 3500 if (BC_NUM_ZERO(n)) return n->scale ? n->scale : 1; 3501 3502 // If this is true, there is no integer portion of the number. 3503 if (BC_NUM_RDX_VAL(n) == len) 3504 { 3505 // We have to take into account the fact that some of the digits right 3506 // after the decimal could be zero. If that is the case, we need to 3507 // ignore them until we hit the first non-zero digit. 3508 3509 size_t zero, scale; 3510 3511 // The number of limbs with non-zero digits. 3512 len = bc_num_nonZeroLen(n); 3513 3514 // Get the number of digits in the last limb. 3515 scale = n->scale % BC_BASE_DIGS; 3516 scale = scale ? scale : BC_BASE_DIGS; 3517 3518 // Get the number of zero digits. 3519 zero = bc_num_zeroDigits(n->num + len - 1); 3520 3521 // Calculate the true length. 3522 len = len * BC_BASE_DIGS - zero - (BC_BASE_DIGS - scale); 3523 } 3524 // Otherwise, count the number of int digits and return that plus the scale. 3525 else len = bc_num_intDigits(n) + n->scale; 3526 3527 return len; 3528 } 3529 3530 void 3531 bc_num_parse(BcNum* restrict n, const char* restrict val, BcBigDig base) 3532 { 3533 #if BC_DEBUG 3534 #if BC_ENABLE_LIBRARY 3535 BcVm* vm = bcl_getspecific(); 3536 #endif // BC_ENABLE_LIBRARY 3537 #endif // BC_DEBUG 3538 3539 assert(n != NULL && val != NULL && base); 3540 assert(base >= BC_NUM_MIN_BASE && base <= vm->maxes[BC_PROG_GLOBALS_IBASE]); 3541 assert(bc_num_strValid(val)); 3542 3543 // A one character number is *always* parsed as though the base was the 3544 // maximum allowed ibase, per the bc spec. 3545 if (!val[1]) 3546 { 3547 BcBigDig dig = bc_num_parseChar(val[0], BC_NUM_MAX_LBASE); 3548 bc_num_bigdig2num(n, dig); 3549 } 3550 else if (base == BC_BASE) bc_num_parseDecimal(n, val); 3551 else bc_num_parseBase(n, val, base); 3552 3553 assert(BC_NUM_RDX_VALID(n)); 3554 } 3555 3556 void 3557 bc_num_print(BcNum* restrict n, BcBigDig base, bool newline) 3558 { 3559 assert(n != NULL); 3560 assert(BC_ENABLE_EXTRA_MATH || base >= BC_NUM_MIN_BASE); 3561 3562 // We may need a newline, just to start. 3563 bc_num_printNewline(); 3564 3565 if (BC_NUM_NONZERO(n)) 3566 { 3567 #if BC_ENABLE_LIBRARY 3568 BcVm* vm = bcl_getspecific(); 3569 #endif // BC_ENABLE_LIBRARY 3570 3571 // Print the sign. 3572 if (BC_NUM_NEG(n)) bc_num_putchar('-', true); 3573 3574 // Print the leading zero if necessary. We don't print when using 3575 // scientific or engineering modes. 3576 if (BC_Z && BC_NUM_RDX_VAL(n) == n->len && base != 0 && base != 1) 3577 { 3578 bc_num_printHex(0, 1, false, !newline); 3579 } 3580 } 3581 3582 // Short-circuit 0. 3583 if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false, !newline); 3584 else if (base == BC_BASE) bc_num_printDecimal(n, newline); 3585 #if BC_ENABLE_EXTRA_MATH 3586 else if (base == 0 || base == 1) 3587 { 3588 bc_num_printExponent(n, base != 0, newline); 3589 } 3590 #endif // BC_ENABLE_EXTRA_MATH 3591 else bc_num_printBase(n, base, newline); 3592 3593 if (newline) bc_num_putchar('\n', false); 3594 } 3595 3596 BcBigDig 3597 bc_num_bigdig2(const BcNum* restrict n) 3598 { 3599 #if BC_DEBUG 3600 #if BC_ENABLE_LIBRARY 3601 BcVm* vm = bcl_getspecific(); 3602 #endif // BC_ENABLE_LIBRARY 3603 #endif // BC_DEBUG 3604 3605 // This function returns no errors because it's guaranteed to succeed if 3606 // its preconditions are met. Those preconditions include both n needs to 3607 // be non-NULL, n being non-negative, and n being less than vm->max. If all 3608 // of that is true, then we can just convert without worrying about negative 3609 // errors or overflow. 3610 3611 BcBigDig r = 0; 3612 size_t nrdx = BC_NUM_RDX_VAL(n); 3613 3614 assert(n != NULL); 3615 assert(!BC_NUM_NEG(n)); 3616 assert(bc_num_cmp(n, &vm->max) < 0); 3617 assert(n->len - nrdx <= 3); 3618 3619 // There is a small speed win from unrolling the loop here, and since it 3620 // only adds 53 bytes, I decided that it was worth it. 3621 switch (n->len - nrdx) 3622 { 3623 case 3: 3624 { 3625 r = (BcBigDig) n->num[nrdx + 2]; 3626 3627 // Fallthrough. 3628 BC_FALLTHROUGH 3629 } 3630 3631 case 2: 3632 { 3633 r = r * BC_BASE_POW + (BcBigDig) n->num[nrdx + 1]; 3634 3635 // Fallthrough. 3636 BC_FALLTHROUGH 3637 } 3638 3639 case 1: 3640 { 3641 r = r * BC_BASE_POW + (BcBigDig) n->num[nrdx]; 3642 } 3643 } 3644 3645 return r; 3646 } 3647 3648 BcBigDig 3649 bc_num_bigdig(const BcNum* restrict n) 3650 { 3651 #if BC_ENABLE_LIBRARY 3652 BcVm* vm = bcl_getspecific(); 3653 #endif // BC_ENABLE_LIBRARY 3654 3655 assert(n != NULL); 3656 3657 // This error checking is extremely important, and if you do not have a 3658 // guarantee that converting a number will always succeed in a particular 3659 // case, you *must* call this function to get these error checks. This 3660 // includes all instances of numbers inputted by the user or calculated by 3661 // the user. Otherwise, you can call the faster bc_num_bigdig2(). 3662 if (BC_ERR(BC_NUM_NEG(n))) bc_err(BC_ERR_MATH_NEGATIVE); 3663 if (BC_ERR(bc_num_cmp(n, &vm->max) >= 0)) bc_err(BC_ERR_MATH_OVERFLOW); 3664 3665 return bc_num_bigdig2(n); 3666 } 3667 3668 void 3669 bc_num_bigdig2num(BcNum* restrict n, BcBigDig val) 3670 { 3671 BcDig* ptr; 3672 size_t i; 3673 3674 assert(n != NULL); 3675 3676 bc_num_zero(n); 3677 3678 // Already 0. 3679 if (!val) return; 3680 3681 // Expand first. This is the only way this function can fail, and it's a 3682 // fatal error. 3683 bc_num_expand(n, BC_NUM_BIGDIG_LOG10); 3684 3685 // The conversion is easy because numbers are laid out in little-endian 3686 // order. 3687 for (ptr = n->num, i = 0; val; ++i, val /= BC_BASE_POW) 3688 { 3689 ptr[i] = val % BC_BASE_POW; 3690 } 3691 3692 n->len = i; 3693 } 3694 3695 #if BC_ENABLE_EXTRA_MATH 3696 3697 void 3698 bc_num_rng(const BcNum* restrict n, BcRNG* rng) 3699 { 3700 BcNum temp, temp2, intn, frac; 3701 BcRand state1, state2, inc1, inc2; 3702 size_t nrdx = BC_NUM_RDX_VAL(n); 3703 #if BC_ENABLE_LIBRARY 3704 BcVm* vm = bcl_getspecific(); 3705 #endif // BC_ENABLE_LIBRARY 3706 3707 // This function holds the secret of how I interpret a seed number for the 3708 // PRNG. Well, it's actually in the development manual 3709 // (manuals/development.md#pseudo-random-number-generator), so look there 3710 // before you try to understand this. 3711 3712 BC_SIG_LOCK; 3713 3714 bc_num_init(&temp, n->len); 3715 bc_num_init(&temp2, n->len); 3716 bc_num_init(&frac, nrdx); 3717 bc_num_init(&intn, bc_num_int(n)); 3718 3719 BC_SETJMP_LOCKED(vm, err); 3720 3721 BC_SIG_UNLOCK; 3722 3723 assert(BC_NUM_RDX_VALID_NP(vm->max)); 3724 3725 // NOLINTNEXTLINE 3726 memcpy(frac.num, n->num, BC_NUM_SIZE(nrdx)); 3727 frac.len = nrdx; 3728 BC_NUM_RDX_SET_NP(frac, nrdx); 3729 frac.scale = n->scale; 3730 3731 assert(BC_NUM_RDX_VALID_NP(frac)); 3732 assert(BC_NUM_RDX_VALID_NP(vm->max2)); 3733 3734 // Multiply the fraction and truncate so that it's an integer. The 3735 // truncation is what clamps it, by the way. 3736 bc_num_mul(&frac, &vm->max2, &temp, 0); 3737 bc_num_truncate(&temp, temp.scale); 3738 bc_num_copy(&frac, &temp); 3739 3740 // Get the integer. 3741 // NOLINTNEXTLINE 3742 memcpy(intn.num, n->num + nrdx, BC_NUM_SIZE(bc_num_int(n))); 3743 intn.len = bc_num_int(n); 3744 3745 // This assert is here because it has to be true. It is also here to justify 3746 // some optimizations. 3747 assert(BC_NUM_NONZERO(&vm->max)); 3748 3749 // If there *was* a fractional part... 3750 if (BC_NUM_NONZERO(&frac)) 3751 { 3752 // This divmod splits frac into the two state parts. 3753 bc_num_divmod(&frac, &vm->max, &temp, &temp2, 0); 3754 3755 // frac is guaranteed to be smaller than vm->max * vm->max (pow). 3756 // This means that when dividing frac by vm->max, as above, the 3757 // quotient and remainder are both guaranteed to be less than vm->max, 3758 // which means we can use bc_num_bigdig2() here and not worry about 3759 // overflow. 3760 state1 = (BcRand) bc_num_bigdig2(&temp2); 3761 state2 = (BcRand) bc_num_bigdig2(&temp); 3762 } 3763 else state1 = state2 = 0; 3764 3765 // If there *was* an integer part... 3766 if (BC_NUM_NONZERO(&intn)) 3767 { 3768 // This divmod splits intn into the two inc parts. 3769 bc_num_divmod(&intn, &vm->max, &temp, &temp2, 0); 3770 3771 // Because temp2 is the mod of vm->max, from above, it is guaranteed 3772 // to be small enough to use bc_num_bigdig2(). 3773 inc1 = (BcRand) bc_num_bigdig2(&temp2); 3774 3775 // Clamp the second inc part. 3776 if (bc_num_cmp(&temp, &vm->max) >= 0) 3777 { 3778 bc_num_copy(&temp2, &temp); 3779 bc_num_mod(&temp2, &vm->max, &temp, 0); 3780 } 3781 3782 // The if statement above ensures that temp is less than vm->max, which 3783 // means that we can use bc_num_bigdig2() here. 3784 inc2 = (BcRand) bc_num_bigdig2(&temp); 3785 } 3786 else inc1 = inc2 = 0; 3787 3788 bc_rand_seed(rng, state1, state2, inc1, inc2); 3789 3790 err: 3791 BC_SIG_MAYLOCK; 3792 bc_num_free(&intn); 3793 bc_num_free(&frac); 3794 bc_num_free(&temp2); 3795 bc_num_free(&temp); 3796 BC_LONGJMP_CONT(vm); 3797 } 3798 3799 void 3800 bc_num_createFromRNG(BcNum* restrict n, BcRNG* rng) 3801 { 3802 BcRand s1, s2, i1, i2; 3803 BcNum conv, temp1, temp2, temp3; 3804 BcDig temp1_num[BC_RAND_NUM_SIZE], temp2_num[BC_RAND_NUM_SIZE]; 3805 BcDig conv_num[BC_NUM_BIGDIG_LOG10]; 3806 #if BC_ENABLE_LIBRARY 3807 BcVm* vm = bcl_getspecific(); 3808 #endif // BC_ENABLE_LIBRARY 3809 3810 BC_SIG_LOCK; 3811 3812 bc_num_init(&temp3, 2 * BC_RAND_NUM_SIZE); 3813 3814 BC_SETJMP_LOCKED(vm, err); 3815 3816 BC_SIG_UNLOCK; 3817 3818 bc_num_setup(&temp1, temp1_num, sizeof(temp1_num) / sizeof(BcDig)); 3819 bc_num_setup(&temp2, temp2_num, sizeof(temp2_num) / sizeof(BcDig)); 3820 bc_num_setup(&conv, conv_num, sizeof(conv_num) / sizeof(BcDig)); 3821 3822 // This assert is here because it has to be true. It is also here to justify 3823 // the assumption that vm->max is not zero. 3824 assert(BC_NUM_NONZERO(&vm->max)); 3825 3826 // Because this is true, we can just ignore math errors that would happen 3827 // otherwise. 3828 assert(BC_NUM_NONZERO(&vm->max2)); 3829 3830 bc_rand_getRands(rng, &s1, &s2, &i1, &i2); 3831 3832 // Put the second piece of state into a number. 3833 bc_num_bigdig2num(&conv, (BcBigDig) s2); 3834 3835 assert(BC_NUM_RDX_VALID_NP(conv)); 3836 3837 // Multiply by max to make room for the first piece of state. 3838 bc_num_mul(&conv, &vm->max, &temp1, 0); 3839 3840 // Add in the first piece of state. 3841 bc_num_bigdig2num(&conv, (BcBigDig) s1); 3842 bc_num_add(&conv, &temp1, &temp2, 0); 3843 3844 // Divide to make it an entirely fractional part. 3845 bc_num_div(&temp2, &vm->max2, &temp3, BC_RAND_STATE_BITS); 3846 3847 // Now start on the increment parts. It's the same process without the 3848 // divide, so put the second piece of increment into a number. 3849 bc_num_bigdig2num(&conv, (BcBigDig) i2); 3850 3851 assert(BC_NUM_RDX_VALID_NP(conv)); 3852 3853 // Multiply by max to make room for the first piece of increment. 3854 bc_num_mul(&conv, &vm->max, &temp1, 0); 3855 3856 // Add in the first piece of increment. 3857 bc_num_bigdig2num(&conv, (BcBigDig) i1); 3858 bc_num_add(&conv, &temp1, &temp2, 0); 3859 3860 // Now add the two together. 3861 bc_num_add(&temp2, &temp3, n, 0); 3862 3863 assert(BC_NUM_RDX_VALID(n)); 3864 3865 err: 3866 BC_SIG_MAYLOCK; 3867 bc_num_free(&temp3); 3868 BC_LONGJMP_CONT(vm); 3869 } 3870 3871 void 3872 bc_num_irand(BcNum* restrict a, BcNum* restrict b, BcRNG* restrict rng) 3873 { 3874 BcNum atemp; 3875 size_t i; 3876 3877 assert(a != b); 3878 3879 if (BC_ERR(BC_NUM_NEG(a))) bc_err(BC_ERR_MATH_NEGATIVE); 3880 3881 // If either of these are true, then the numbers are integers. 3882 if (BC_NUM_ZERO(a) || BC_NUM_ONE(a)) return; 3883 3884 #if BC_GCC 3885 // This is here in GCC to quiet the "maybe-uninitialized" warning. 3886 atemp.num = NULL; 3887 atemp.len = 0; 3888 #endif // BC_GCC 3889 3890 if (BC_ERR(bc_num_nonInt(a, &atemp))) bc_err(BC_ERR_MATH_NON_INTEGER); 3891 3892 assert(atemp.num != NULL); 3893 assert(atemp.len); 3894 3895 if (atemp.len > 2) 3896 { 3897 size_t len; 3898 3899 len = atemp.len - 2; 3900 3901 // Just generate a random number for each limb. 3902 for (i = 0; i < len; i += 2) 3903 { 3904 BcRand dig; 3905 3906 dig = bc_rand_bounded(rng, BC_BASE_RAND_POW); 3907 3908 b->num[i] = (BcDig) (dig % BC_BASE_POW); 3909 b->num[i + 1] = (BcDig) (dig / BC_BASE_POW); 3910 } 3911 } 3912 else 3913 { 3914 // We need this set. 3915 i = 0; 3916 } 3917 3918 // This will be true if there's one full limb after the two limb groups. 3919 if (i == atemp.len - 2) 3920 { 3921 // Increment this for easy use. 3922 i += 1; 3923 3924 // If the last digit is not one, we need to set a bound for it 3925 // explicitly. Since there's still an empty limb, we need to fill that. 3926 if (atemp.num[i] != 1) 3927 { 3928 BcRand dig; 3929 BcRand bound; 3930 3931 // Set the bound to the bound of the last limb times the amount 3932 // needed to fill the second-to-last limb as well. 3933 bound = ((BcRand) atemp.num[i]) * BC_BASE_POW; 3934 3935 dig = bc_rand_bounded(rng, bound); 3936 3937 // Fill the last two. 3938 b->num[i - 1] = (BcDig) (dig % BC_BASE_POW); 3939 b->num[i] = (BcDig) (dig / BC_BASE_POW); 3940 3941 // Ensure that the length will be correct. If the last limb is zero, 3942 // then the length needs to be one less than the bound. 3943 b->len = atemp.len - (b->num[i] == 0); 3944 } 3945 // Here the last limb *is* one, which means the last limb does *not* 3946 // need to be filled. Also, the length needs to be one less because the 3947 // last limb is 0. 3948 else 3949 { 3950 b->num[i - 1] = (BcDig) bc_rand_bounded(rng, BC_BASE_POW); 3951 b->len = atemp.len - 1; 3952 } 3953 } 3954 // Here, there is only one limb to fill. 3955 else 3956 { 3957 // See above for how this works. 3958 if (atemp.num[i] != 1) 3959 { 3960 b->num[i] = (BcDig) bc_rand_bounded(rng, (BcRand) atemp.num[i]); 3961 b->len = atemp.len - (b->num[i] == 0); 3962 } 3963 else b->len = atemp.len - 1; 3964 } 3965 3966 bc_num_clean(b); 3967 3968 assert(BC_NUM_RDX_VALID(b)); 3969 } 3970 #endif // BC_ENABLE_EXTRA_MATH 3971 3972 size_t 3973 bc_num_addReq(const BcNum* a, const BcNum* b, size_t scale) 3974 { 3975 size_t aint, bint, ardx, brdx; 3976 3977 // Addition and subtraction require the max of the length of the two numbers 3978 // plus 1. 3979 3980 BC_UNUSED(scale); 3981 3982 ardx = BC_NUM_RDX_VAL(a); 3983 aint = bc_num_int(a); 3984 assert(aint <= a->len && ardx <= a->len); 3985 3986 brdx = BC_NUM_RDX_VAL(b); 3987 bint = bc_num_int(b); 3988 assert(bint <= b->len && brdx <= b->len); 3989 3990 ardx = BC_MAX(ardx, brdx); 3991 aint = BC_MAX(aint, bint); 3992 3993 return bc_vm_growSize(bc_vm_growSize(ardx, aint), 1); 3994 } 3995 3996 size_t 3997 bc_num_mulReq(const BcNum* a, const BcNum* b, size_t scale) 3998 { 3999 size_t max, rdx; 4000 4001 // Multiplication requires the sum of the lengths of the numbers. 4002 4003 rdx = bc_vm_growSize(BC_NUM_RDX_VAL(a), BC_NUM_RDX_VAL(b)); 4004 4005 max = BC_NUM_RDX(scale); 4006 4007 max = bc_vm_growSize(BC_MAX(max, rdx), 1); 4008 rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max); 4009 4010 return rdx; 4011 } 4012 4013 size_t 4014 bc_num_divReq(const BcNum* a, const BcNum* b, size_t scale) 4015 { 4016 size_t max, rdx; 4017 4018 // Division requires the length of the dividend plus the scale. 4019 4020 rdx = bc_vm_growSize(BC_NUM_RDX_VAL(a), BC_NUM_RDX_VAL(b)); 4021 4022 max = BC_NUM_RDX(scale); 4023 4024 max = bc_vm_growSize(BC_MAX(max, rdx), 1); 4025 rdx = bc_vm_growSize(bc_num_int(a), max); 4026 4027 return rdx; 4028 } 4029 4030 size_t 4031 bc_num_powReq(const BcNum* a, const BcNum* b, size_t scale) 4032 { 4033 BC_UNUSED(scale); 4034 return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1); 4035 } 4036 4037 #if BC_ENABLE_EXTRA_MATH 4038 size_t 4039 bc_num_placesReq(const BcNum* a, const BcNum* b, size_t scale) 4040 { 4041 BC_UNUSED(scale); 4042 return a->len + b->len - BC_NUM_RDX_VAL(a) - BC_NUM_RDX_VAL(b); 4043 } 4044 #endif // BC_ENABLE_EXTRA_MATH 4045 4046 void 4047 bc_num_add(BcNum* a, BcNum* b, BcNum* c, size_t scale) 4048 { 4049 assert(BC_NUM_RDX_VALID(a)); 4050 assert(BC_NUM_RDX_VALID(b)); 4051 bc_num_binary(a, b, c, false, bc_num_as, bc_num_addReq(a, b, scale)); 4052 } 4053 4054 void 4055 bc_num_sub(BcNum* a, BcNum* b, BcNum* c, size_t scale) 4056 { 4057 assert(BC_NUM_RDX_VALID(a)); 4058 assert(BC_NUM_RDX_VALID(b)); 4059 bc_num_binary(a, b, c, true, bc_num_as, bc_num_addReq(a, b, scale)); 4060 } 4061 4062 void 4063 bc_num_mul(BcNum* a, BcNum* b, BcNum* c, size_t scale) 4064 { 4065 assert(BC_NUM_RDX_VALID(a)); 4066 assert(BC_NUM_RDX_VALID(b)); 4067 bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale)); 4068 } 4069 4070 void 4071 bc_num_div(BcNum* a, BcNum* b, BcNum* c, size_t scale) 4072 { 4073 assert(BC_NUM_RDX_VALID(a)); 4074 assert(BC_NUM_RDX_VALID(b)); 4075 bc_num_binary(a, b, c, scale, bc_num_d, bc_num_divReq(a, b, scale)); 4076 } 4077 4078 void 4079 bc_num_mod(BcNum* a, BcNum* b, BcNum* c, size_t scale) 4080 { 4081 assert(BC_NUM_RDX_VALID(a)); 4082 assert(BC_NUM_RDX_VALID(b)); 4083 bc_num_binary(a, b, c, scale, bc_num_rem, bc_num_divReq(a, b, scale)); 4084 } 4085 4086 void 4087 bc_num_pow(BcNum* a, BcNum* b, BcNum* c, size_t scale) 4088 { 4089 assert(BC_NUM_RDX_VALID(a)); 4090 assert(BC_NUM_RDX_VALID(b)); 4091 bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale)); 4092 } 4093 4094 #if BC_ENABLE_EXTRA_MATH 4095 void 4096 bc_num_places(BcNum* a, BcNum* b, BcNum* c, size_t scale) 4097 { 4098 assert(BC_NUM_RDX_VALID(a)); 4099 assert(BC_NUM_RDX_VALID(b)); 4100 bc_num_binary(a, b, c, scale, bc_num_place, bc_num_placesReq(a, b, scale)); 4101 } 4102 4103 void 4104 bc_num_lshift(BcNum* a, BcNum* b, BcNum* c, size_t scale) 4105 { 4106 assert(BC_NUM_RDX_VALID(a)); 4107 assert(BC_NUM_RDX_VALID(b)); 4108 bc_num_binary(a, b, c, scale, bc_num_left, bc_num_placesReq(a, b, scale)); 4109 } 4110 4111 void 4112 bc_num_rshift(BcNum* a, BcNum* b, BcNum* c, size_t scale) 4113 { 4114 assert(BC_NUM_RDX_VALID(a)); 4115 assert(BC_NUM_RDX_VALID(b)); 4116 bc_num_binary(a, b, c, scale, bc_num_right, bc_num_placesReq(a, b, scale)); 4117 } 4118 #endif // BC_ENABLE_EXTRA_MATH 4119 4120 void 4121 bc_num_sqrt(BcNum* restrict a, BcNum* restrict b, size_t scale) 4122 { 4123 BcNum num1, num2, half, f, fprime; 4124 BcNum* x0; 4125 BcNum* x1; 4126 BcNum* temp; 4127 // realscale is meant to quiet a warning on GCC about longjmp() clobbering. 4128 // This one is real. 4129 size_t pow, len, rdx, req, resscale, realscale; 4130 BcDig half_digs[1]; 4131 #if BC_ENABLE_LIBRARY 4132 BcVm* vm = bcl_getspecific(); 4133 #endif // BC_ENABLE_LIBRARY 4134 4135 assert(a != NULL && b != NULL && a != b); 4136 4137 if (BC_ERR(BC_NUM_NEG(a))) bc_err(BC_ERR_MATH_NEGATIVE); 4138 4139 // We want to calculate to a's scale if it is bigger so that the result will 4140 // truncate properly. 4141 if (a->scale > scale) realscale = a->scale; 4142 else realscale = scale; 4143 4144 // Set parameters for the result. 4145 len = bc_vm_growSize(bc_num_intDigits(a), 1); 4146 rdx = BC_NUM_RDX(realscale); 4147 4148 // Square root needs half of the length of the parameter. 4149 req = bc_vm_growSize(BC_MAX(rdx, BC_NUM_RDX_VAL(a)), len >> 1); 4150 req = bc_vm_growSize(req, 1); 4151 4152 BC_SIG_LOCK; 4153 4154 // Unlike the binary operators, this function is the only single parameter 4155 // function and is expected to initialize the result. This means that it 4156 // expects that b is *NOT* preallocated. We allocate it here. 4157 bc_num_init(b, req); 4158 4159 BC_SIG_UNLOCK; 4160 4161 assert(a != NULL && b != NULL && a != b); 4162 assert(a->num != NULL && b->num != NULL); 4163 4164 // Easy case. 4165 if (BC_NUM_ZERO(a)) 4166 { 4167 bc_num_setToZero(b, realscale); 4168 return; 4169 } 4170 4171 // Another easy case. 4172 if (BC_NUM_ONE(a)) 4173 { 4174 bc_num_one(b); 4175 bc_num_extend(b, realscale); 4176 return; 4177 } 4178 4179 // Set the parameters again. 4180 rdx = BC_NUM_RDX(realscale); 4181 rdx = BC_MAX(rdx, BC_NUM_RDX_VAL(a)); 4182 len = bc_vm_growSize(a->len, rdx); 4183 4184 BC_SIG_LOCK; 4185 4186 bc_num_init(&num1, len); 4187 bc_num_init(&num2, len); 4188 bc_num_setup(&half, half_digs, sizeof(half_digs) / sizeof(BcDig)); 4189 4190 // There is a division by two in the formula. We set up a number that's 1/2 4191 // so that we can use multiplication instead of heavy division. 4192 bc_num_setToZero(&half, 1); 4193 half.num[0] = BC_BASE_POW / 2; 4194 half.len = 1; 4195 BC_NUM_RDX_SET_NP(half, 1); 4196 4197 bc_num_init(&f, len); 4198 bc_num_init(&fprime, len); 4199 4200 BC_SETJMP_LOCKED(vm, err); 4201 4202 BC_SIG_UNLOCK; 4203 4204 // Pointers for easy switching. 4205 x0 = &num1; 4206 x1 = &num2; 4207 4208 // Start with 1. 4209 bc_num_one(x0); 4210 4211 // The power of the operand is needed for the estimate. 4212 pow = bc_num_intDigits(a); 4213 4214 // The code in this if statement calculates the initial estimate. First, if 4215 // a is less than 1, then 0 is a good estimate. Otherwise, we want something 4216 // in the same ballpark. That ballpark is half of pow because the result 4217 // will have half the digits. 4218 if (pow) 4219 { 4220 // An odd number is served by starting with 2^((pow-1)/2), and an even 4221 // number is served by starting with 6^((pow-2)/2). Why? Because math. 4222 if (pow & 1) x0->num[0] = 2; 4223 else x0->num[0] = 6; 4224 4225 pow -= 2 - (pow & 1); 4226 bc_num_shiftLeft(x0, pow / 2); 4227 } 4228 4229 // I can set the rdx here directly because neg should be false. 4230 x0->scale = x0->rdx = 0; 4231 resscale = (realscale + BC_BASE_DIGS) + 2; 4232 4233 // This is the calculation loop. This compare goes to 0 eventually as the 4234 // difference between the two numbers gets smaller than resscale. 4235 while (bc_num_cmp(x1, x0)) 4236 { 4237 assert(BC_NUM_NONZERO(x0)); 4238 4239 // This loop directly corresponds to the iteration in Newton's method. 4240 // If you know the formula, this loop makes sense. Go study the formula. 4241 4242 bc_num_div(a, x0, &f, resscale); 4243 bc_num_add(x0, &f, &fprime, resscale); 4244 4245 assert(BC_NUM_RDX_VALID_NP(fprime)); 4246 assert(BC_NUM_RDX_VALID_NP(half)); 4247 4248 bc_num_mul(&fprime, &half, x1, resscale); 4249 4250 // Switch. 4251 temp = x0; 4252 x0 = x1; 4253 x1 = temp; 4254 } 4255 4256 // Copy to the result and truncate. 4257 bc_num_copy(b, x0); 4258 if (b->scale > realscale) bc_num_truncate(b, b->scale - realscale); 4259 4260 assert(!BC_NUM_NEG(b) || BC_NUM_NONZERO(b)); 4261 assert(BC_NUM_RDX_VALID(b)); 4262 assert(BC_NUM_RDX_VAL(b) <= b->len || !b->len); 4263 assert(!b->len || b->num[b->len - 1] || BC_NUM_RDX_VAL(b) == b->len); 4264 4265 err: 4266 BC_SIG_MAYLOCK; 4267 bc_num_free(&fprime); 4268 bc_num_free(&f); 4269 bc_num_free(&num2); 4270 bc_num_free(&num1); 4271 BC_LONGJMP_CONT(vm); 4272 } 4273 4274 void 4275 bc_num_divmod(BcNum* a, BcNum* b, BcNum* c, BcNum* d, size_t scale) 4276 { 4277 size_t ts, len; 4278 BcNum *ptr_a, num2; 4279 // This is volatile to quiet a warning on GCC about clobbering with 4280 // longjmp(). 4281 volatile bool init = false; 4282 #if BC_ENABLE_LIBRARY 4283 BcVm* vm = bcl_getspecific(); 4284 #endif // BC_ENABLE_LIBRARY 4285 4286 // The bulk of this function is just doing what bc_num_binary() does for the 4287 // binary operators. However, it assumes that only c and a can be equal. 4288 4289 // Set up the parameters. 4290 ts = BC_MAX(scale + b->scale, a->scale); 4291 len = bc_num_mulReq(a, b, ts); 4292 4293 assert(a != NULL && b != NULL && c != NULL && d != NULL); 4294 assert(c != d && a != d && b != d && b != c); 4295 4296 // Initialize or expand as necessary. 4297 if (c == a) 4298 { 4299 // NOLINTNEXTLINE 4300 memcpy(&num2, c, sizeof(BcNum)); 4301 ptr_a = &num2; 4302 4303 BC_SIG_LOCK; 4304 4305 bc_num_init(c, len); 4306 4307 init = true; 4308 4309 BC_SETJMP_LOCKED(vm, err); 4310 4311 BC_SIG_UNLOCK; 4312 } 4313 else 4314 { 4315 ptr_a = a; 4316 bc_num_expand(c, len); 4317 } 4318 4319 // Do the quick version if possible. 4320 if (BC_NUM_NONZERO(a) && !BC_NUM_RDX_VAL(a) && !BC_NUM_RDX_VAL(b) && 4321 b->len == 1 && !scale) 4322 { 4323 BcBigDig rem; 4324 4325 bc_num_divArray(ptr_a, (BcBigDig) b->num[0], c, &rem); 4326 4327 assert(rem < BC_BASE_POW); 4328 4329 d->num[0] = (BcDig) rem; 4330 d->len = (rem != 0); 4331 } 4332 // Do the slow method. 4333 else bc_num_r(ptr_a, b, c, d, scale, ts); 4334 4335 assert(!BC_NUM_NEG(c) || BC_NUM_NONZERO(c)); 4336 assert(BC_NUM_RDX_VALID(c)); 4337 assert(BC_NUM_RDX_VAL(c) <= c->len || !c->len); 4338 assert(!c->len || c->num[c->len - 1] || BC_NUM_RDX_VAL(c) == c->len); 4339 assert(!BC_NUM_NEG(d) || BC_NUM_NONZERO(d)); 4340 assert(BC_NUM_RDX_VALID(d)); 4341 assert(BC_NUM_RDX_VAL(d) <= d->len || !d->len); 4342 assert(!d->len || d->num[d->len - 1] || BC_NUM_RDX_VAL(d) == d->len); 4343 4344 err: 4345 // Only cleanup if we initialized. 4346 if (init) 4347 { 4348 BC_SIG_MAYLOCK; 4349 bc_num_free(&num2); 4350 BC_LONGJMP_CONT(vm); 4351 } 4352 } 4353 4354 void 4355 bc_num_modexp(BcNum* a, BcNum* b, BcNum* c, BcNum* restrict d) 4356 { 4357 BcNum base, exp, two, temp, atemp, btemp, ctemp; 4358 BcDig two_digs[2]; 4359 #if BC_ENABLE_LIBRARY 4360 BcVm* vm = bcl_getspecific(); 4361 #endif // BC_ENABLE_LIBRARY 4362 4363 assert(a != NULL && b != NULL && c != NULL && d != NULL); 4364 assert(a != d && b != d && c != d); 4365 4366 if (BC_ERR(BC_NUM_ZERO(c))) bc_err(BC_ERR_MATH_DIVIDE_BY_ZERO); 4367 if (BC_ERR(BC_NUM_NEG(b))) bc_err(BC_ERR_MATH_NEGATIVE); 4368 4369 #if BC_DEBUG || BC_GCC 4370 // This is entirely for quieting a useless scan-build error. 4371 btemp.len = 0; 4372 ctemp.len = 0; 4373 #endif // BC_DEBUG || BC_GCC 4374 4375 // Eliminate fractional parts that are zero or error if they are not zero. 4376 if (BC_ERR(bc_num_nonInt(a, &atemp) || bc_num_nonInt(b, &btemp) || 4377 bc_num_nonInt(c, &ctemp))) 4378 { 4379 bc_err(BC_ERR_MATH_NON_INTEGER); 4380 } 4381 4382 bc_num_expand(d, ctemp.len); 4383 4384 BC_SIG_LOCK; 4385 4386 bc_num_init(&base, ctemp.len); 4387 bc_num_setup(&two, two_digs, sizeof(two_digs) / sizeof(BcDig)); 4388 bc_num_init(&temp, btemp.len + 1); 4389 bc_num_createCopy(&exp, &btemp); 4390 4391 BC_SETJMP_LOCKED(vm, err); 4392 4393 BC_SIG_UNLOCK; 4394 4395 bc_num_one(&two); 4396 two.num[0] = 2; 4397 bc_num_one(d); 4398 4399 // We already checked for 0. 4400 bc_num_rem(&atemp, &ctemp, &base, 0); 4401 4402 // If you know the algorithm I used, the memory-efficient method, then this 4403 // loop should be self-explanatory because it is the calculation loop. 4404 while (BC_NUM_NONZERO(&exp)) 4405 { 4406 // Num two cannot be 0, so no errors. 4407 bc_num_divmod(&exp, &two, &exp, &temp, 0); 4408 4409 if (BC_NUM_ONE(&temp) && !BC_NUM_NEG_NP(temp)) 4410 { 4411 assert(BC_NUM_RDX_VALID(d)); 4412 assert(BC_NUM_RDX_VALID_NP(base)); 4413 4414 bc_num_mul(d, &base, &temp, 0); 4415 4416 // We already checked for 0. 4417 bc_num_rem(&temp, &ctemp, d, 0); 4418 } 4419 4420 assert(BC_NUM_RDX_VALID_NP(base)); 4421 4422 bc_num_mul(&base, &base, &temp, 0); 4423 4424 // We already checked for 0. 4425 bc_num_rem(&temp, &ctemp, &base, 0); 4426 } 4427 4428 err: 4429 BC_SIG_MAYLOCK; 4430 bc_num_free(&exp); 4431 bc_num_free(&temp); 4432 bc_num_free(&base); 4433 BC_LONGJMP_CONT(vm); 4434 assert(!BC_NUM_NEG(d) || d->len); 4435 assert(BC_NUM_RDX_VALID(d)); 4436 assert(!d->len || d->num[d->len - 1] || BC_NUM_RDX_VAL(d) == d->len); 4437 } 4438 4439 #if BC_DEBUG_CODE 4440 void 4441 bc_num_printDebug(const BcNum* n, const char* name, bool emptyline) 4442 { 4443 bc_file_puts(&vm->fout, bc_flush_none, name); 4444 bc_file_puts(&vm->fout, bc_flush_none, ": "); 4445 bc_num_printDecimal(n, true); 4446 bc_file_putchar(&vm->fout, bc_flush_err, '\n'); 4447 if (emptyline) bc_file_putchar(&vm->fout, bc_flush_err, '\n'); 4448 vm->nchars = 0; 4449 } 4450 4451 void 4452 bc_num_printDigs(const BcDig* n, size_t len, bool emptyline) 4453 { 4454 size_t i; 4455 4456 for (i = len - 1; i < len; --i) 4457 { 4458 bc_file_printf(&vm->fout, " %lu", (unsigned long) n[i]); 4459 } 4460 4461 bc_file_putchar(&vm->fout, bc_flush_err, '\n'); 4462 if (emptyline) bc_file_putchar(&vm->fout, bc_flush_err, '\n'); 4463 vm->nchars = 0; 4464 } 4465 4466 void 4467 bc_num_printWithDigs(const BcNum* n, const char* name, bool emptyline) 4468 { 4469 bc_file_puts(&vm->fout, bc_flush_none, name); 4470 bc_file_printf(&vm->fout, " len: %zu, rdx: %zu, scale: %zu\n", name, n->len, 4471 BC_NUM_RDX_VAL(n), n->scale); 4472 bc_num_printDigs(n->num, n->len, emptyline); 4473 } 4474 4475 void 4476 bc_num_dump(const char* varname, const BcNum* n) 4477 { 4478 ulong i, scale = n->scale; 4479 4480 bc_file_printf(&vm->ferr, "\n%s = %s", varname, 4481 n->len ? (BC_NUM_NEG(n) ? "-" : "+") : "0 "); 4482 4483 for (i = n->len - 1; i < n->len; --i) 4484 { 4485 if (i + 1 == BC_NUM_RDX_VAL(n)) 4486 { 4487 bc_file_puts(&vm->ferr, bc_flush_none, ". "); 4488 } 4489 4490 if (scale / BC_BASE_DIGS != BC_NUM_RDX_VAL(n) - i - 1) 4491 { 4492 bc_file_printf(&vm->ferr, "%lu ", (unsigned long) n->num[i]); 4493 } 4494 else 4495 { 4496 int mod = scale % BC_BASE_DIGS; 4497 int d = BC_BASE_DIGS - mod; 4498 BcDig div; 4499 4500 if (mod != 0) 4501 { 4502 div = n->num[i] / ((BcDig) bc_num_pow10[(ulong) d]); 4503 bc_file_printf(&vm->ferr, "%lu", (unsigned long) div); 4504 } 4505 4506 div = n->num[i] % ((BcDig) bc_num_pow10[(ulong) d]); 4507 bc_file_printf(&vm->ferr, " ' %lu ", (unsigned long) div); 4508 } 4509 } 4510 4511 bc_file_printf(&vm->ferr, "(%zu | %zu.%zu / %zu) %lu\n", n->scale, n->len, 4512 BC_NUM_RDX_VAL(n), n->cap, (unsigned long) (void*) n->num); 4513 4514 bc_file_flush(&vm->ferr, bc_flush_err); 4515 } 4516 #endif // BC_DEBUG_CODE 4517