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