2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
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.
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.
30 * *****************************************************************************
32 * Definitions for the num type.
44 #include <sys/types.h>
50 #ifndef BC_ENABLE_EXTRA_MATH
51 #define BC_ENABLE_EXTRA_MATH (1)
52 #endif // BC_ENABLE_EXTRA_MATH
54 /// Everything in bc is base 10..
58 typedef unsigned long ulong;
60 /// This is here because BcBigDig came first, but when I created bcl, it's
61 /// definition has to be defined first.
62 typedef BclBigDig BcBigDig;
66 /// The biggest number held by a BcBigDig.
67 #define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT64_MAX)
69 /// The number of decimal digits in one limb.
70 #define BC_BASE_DIGS (9)
72 /// The max number + 1 that one limb can hold.
73 #define BC_BASE_POW (1000000000)
75 /// An alias for portability.
76 #define BC_NUM_BIGDIG_C UINT64_C
78 /// The actual limb type.
79 typedef int_least32_t BcDig;
81 #elif BC_LONG_BIT >= 32
83 /// The biggest number held by a BcBigDig.
84 #define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT32_MAX)
86 /// The number of decimal digits in one limb.
87 #define BC_BASE_DIGS (4)
89 /// The max number + 1 that one limb can hold.
90 #define BC_BASE_POW (10000)
92 /// An alias for portability.
93 #define BC_NUM_BIGDIG_C UINT32_C
95 /// The actual limb type.
96 typedef int_least16_t BcDig;
100 /// LONG_BIT must be at least 32 on POSIX. We depend on that.
101 #error BC_LONG_BIT must be at least 32
103 #endif // BC_LONG_BIT >= 64
105 /// The default (and minimum) number of limbs when allocating a number.
106 #define BC_NUM_DEF_SIZE (8)
108 /// The actual number struct. This is where the magic happens.
109 typedef struct BcNum {
111 /// The limb array. It is restrict because *no* other item should own the
112 /// array. For more information, see the development manual
113 /// (manuals/development.md#numbers).
116 /// The number of limbs before the decimal (radix) point. This also stores
117 /// the negative bit in the least significant bit since it uses at least two
118 /// bits less than scale. It is also used less than scale. See the
119 /// development manual (manuals/development.md#numbers) for more info.
122 /// The actual scale of the number. This is different from rdx because there
123 /// are multiple digits in one limb, and in the last limb, only some of the
124 /// digits may be part of the scale. However, scale must always match rdx
125 /// (except when the number is 0), or there is a bug. For more information,
126 /// see the development manual (manuals/development.md#numbers).
129 /// The number of valid limbs in the array. If this is 0, then the number is
133 /// The capacity of the limbs array. This is how many limbs the number could
134 /// expand to without reallocation.
139 #if BC_ENABLE_EXTRA_MATH
141 // Forward declaration
144 #endif // BC_ENABLE_EXTRA_MATH
146 /// The minimum obase.
147 #define BC_NUM_MIN_BASE (BC_NUM_BIGDIG_C(2))
149 /// The maximum ibase allowed by POSIX.
150 #define BC_NUM_MAX_POSIX_IBASE (BC_NUM_BIGDIG_C(16))
152 /// The actual ibase supported by this implementation.
153 #define BC_NUM_MAX_IBASE (BC_NUM_BIGDIG_C(36))
155 /// The max base allowed by bc_num_parseChar().
156 #define BC_NUM_MAX_LBASE (BC_NUM_BIGDIG_C('Z' + BC_BASE + 1))
158 /// The default number of characters to print before a backslash newline.
159 #define BC_NUM_PRINT_WIDTH (BC_NUM_BIGDIG_C(69))
161 /// The base for printing streams from numbers.
162 #define BC_NUM_STREAM_BASE (256)
164 // This sets a default for the Karatsuba length.
165 #ifndef BC_NUM_KARATSUBA_LEN
166 #define BC_NUM_KARATSUBA_LEN (BC_NUM_BIGDIG_C(32))
167 #elif BC_NUM_KARATSUBA_LEN < 16
168 #error BC_NUM_KARATSUBA_LEN must be at least 16.
169 #endif // BC_NUM_KARATSUBA_LEN
171 // A crude, but always big enough, calculation of
172 // the size required for ibase and obase BcNum's.
173 #define BC_NUM_BIGDIG_LOG10 (BC_NUM_DEF_SIZE)
176 * Returns non-zero if the BcNum @a n is non-zero.
177 * @param n The number to test.
178 * @return Non-zero if @a n is non-zero, zero otherwise.
180 #define BC_NUM_NONZERO(n) ((n)->len)
183 * Returns true if the BcNum @a n is zero.
184 * @param n The number to test.
185 * @return True if @a n is zero, false otherwise.
187 #define BC_NUM_ZERO(n) (!BC_NUM_NONZERO(n))
190 * Returns true if the BcNum @a n is one with no scale.
191 * @param n The number to test.
192 * @return True if @a n equals 1 with no scale, false otherwise.
194 #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
197 * Converts the letter @a c into a number.
198 * @param c The letter to convert.
199 * @return The number corresponding to the letter.
201 #define BC_NUM_NUM_LETTER(c) ((c) - 'A' + BC_BASE)
203 /// The number of allocations done by bc_num_k(). If you change the number of
204 /// allocations, you must change this. This is done in order to allocate them
205 /// all as one allocation and just give them all pointers to different parts.
206 /// Works pretty well, but you have to be careful.
207 #define BC_NUM_KARATSUBA_ALLOCS (6)
210 * Rounds @a s (scale) up to the next power of BC_BASE_DIGS. This also check for
211 * overflow and gives a fatal error if that happens because we just can't go
212 * over the limits we have imposed.
213 * @param s The scale to round up.
214 * @return @a s rounded up to the next power of BC_BASE_DIGS.
216 #define BC_NUM_ROUND_POW(s) (bc_vm_growSize((s), BC_BASE_DIGS - 1))
219 * Returns the equivalent rdx for the scale @a s.
220 * @param s The scale to convert.
221 * @return The rdx for @a s.
223 #define BC_NUM_RDX(s) (BC_NUM_ROUND_POW(s) / BC_BASE_DIGS)
226 * Returns the actual rdx of @a n. (It removes the negative bit.)
227 * @param n The number.
228 * @return The real rdx of @a n.
230 #define BC_NUM_RDX_VAL(n) ((n)->rdx >> 1)
233 * Returns the actual rdx of @a n, where @a n is not a pointer. (It removes the
235 * @param n The number.
236 * @return The real rdx of @a n.
238 #define BC_NUM_RDX_VAL_NP(n) ((n).rdx >> 1)
241 * Sets the rdx of @a n to @a v.
242 * @param n The number.
243 * @param v The value to set the rdx to.
245 #define BC_NUM_RDX_SET(n, v) \
246 ((n)->rdx = (((v) << 1) | ((n)->rdx & (BcBigDig) 1)))
249 * Sets the rdx of @a n to @a v, where @a n is not a pointer.
250 * @param n The number.
251 * @param v The value to set the rdx to.
253 #define BC_NUM_RDX_SET_NP(n, v) \
254 ((n).rdx = (((v) << 1) | ((n).rdx & (BcBigDig) 1)))
257 * Sets the rdx of @a n to @a v and the negative bit to @a neg.
258 * @param n The number.
259 * @param v The value to set the rdx to.
260 * @param neg The value to set the negative bit to.
262 #define BC_NUM_RDX_SET_NEG(n, v, neg) \
263 ((n)->rdx = (((v) << 1) | (neg)))
266 * Returns true if the rdx and scale for @a n match.
267 * @param n The number to test.
268 * @return True if the rdx and scale of @a n match, false otherwise.
270 #define BC_NUM_RDX_VALID(n) \
271 (BC_NUM_ZERO(n) || BC_NUM_RDX_VAL(n) * BC_BASE_DIGS >= (n)->scale)
274 * Returns true if the rdx and scale for @a n match, where @a n is not a
276 * @param n The number to test.
277 * @return True if the rdx and scale of @a n match, false otherwise.
279 #define BC_NUM_RDX_VALID_NP(n) \
280 ((!(n).len) || BC_NUM_RDX_VAL_NP(n) * BC_BASE_DIGS >= (n).scale)
283 * Returns true if @a n is negative, false otherwise.
284 * @param n The number to test.
285 * @return True if @a n is negative, false otherwise.
287 #define BC_NUM_NEG(n) ((n)->rdx & ((BcBigDig) 1))
290 * Returns true if @a n is negative, false otherwise, where @a n is not a
292 * @param n The number to test.
293 * @return True if @a n is negative, false otherwise.
295 #define BC_NUM_NEG_NP(n) ((n).rdx & ((BcBigDig) 1))
298 * Clears the negative bit on @a n.
299 * @param n The number.
301 #define BC_NUM_NEG_CLR(n) ((n)->rdx &= ~((BcBigDig) 1))
304 * Clears the negative bit on @a n, where @a n is not a pointer.
305 * @param n The number.
307 #define BC_NUM_NEG_CLR_NP(n) ((n).rdx &= ~((BcBigDig) 1))
310 * Sets the negative bit on @a n.
311 * @param n The number.
313 #define BC_NUM_NEG_SET(n) ((n)->rdx |= ((BcBigDig) 1))
316 * Toggles the negative bit on @a n.
317 * @param n The number.
319 #define BC_NUM_NEG_TGL(n) ((n)->rdx ^= ((BcBigDig) 1))
322 * Toggles the negative bit on @a n, where @a n is not a pointer.
323 * @param n The number.
325 #define BC_NUM_NEG_TGL_NP(n) ((n).rdx ^= ((BcBigDig) 1))
328 * Returns the rdx val for @a n if the negative bit is set to @a v.
329 * @param n The number.
330 * @param v The value for the negative bit.
331 * @return The value of the rdx of @a n if the negative bit were set to @a v.
333 #define BC_NUM_NEG_VAL(n, v) (((n)->rdx & ~((BcBigDig) 1)) | (v))
336 * Returns the rdx val for @a n if the negative bit is set to @a v, where @a n
338 * @param n The number.
339 * @param v The value for the negative bit.
340 * @return The value of the rdx of @a n if the negative bit were set to @a v.
342 #define BC_NUM_NEG_VAL_NP(n, v) (((n).rdx & ~((BcBigDig) 1)) | (v))
345 * Returns the size, in bytes, of limb array with @a n limbs.
346 * @param n The number.
347 * @return The size, in bytes, of a limb array with @a n limbs.
349 #define BC_NUM_SIZE(n) ((n) * sizeof(BcDig))
351 // These are for debugging only.
353 #define BC_NUM_PRINT(x) fprintf(stderr, "%s = %lu\n", #x, (unsigned long)(x))
354 #define DUMP_NUM bc_num_dump
355 #else // BC_DEBUG_CODE
357 #define DUMP_NUM(x,y)
358 #define BC_NUM_PRINT(x)
359 #endif // BC_DEBUG_CODE
362 * A function type for binary operators.
363 * @param a The first parameter.
364 * @param b The second parameter.
365 * @param c The return value.
366 * @param scale The current scale.
368 typedef void (*BcNumBinaryOp)(BcNum* a, BcNum* b, BcNum* c, size_t scale);
371 * A function type for binary operators *after* @a c has been properly
372 * allocated. At this point, *nothing* should be pointing to @a c (in any way
373 * that matters, anyway).
374 * @param a The first operand.
375 * @param b The second operand.
376 * @param c The return parameter.
377 * @param scale The current scale.
379 typedef void (*BcNumBinOp)(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale);
382 * A function type for getting the allocation size needed for a binary operator.
383 * Any function used for this *must* return enough space for *all* possible
384 * invocations of the operator.
385 * @param a The first parameter.
386 * @param b The second parameter.
387 * @param scale The current scale.
388 * @return The size of allocation needed for the result of the operator
389 * with @a a, @a b, and @a scale.
391 typedef size_t (*BcNumBinaryOpReq)(const BcNum* a, const BcNum* b,
395 * A function type for printing a "digit." Functions of this type will print one
396 * digit in a number. Digits are printed differently based on the base, which is
397 * why there is more than one implementation of this function type.
398 * @param n The "digit" to print.
399 * @param len The "length" of the digit, or number of characters that will
400 * need to be printed for the digit.
401 * @param rdx True if a decimal (radix) point should be printed.
402 * @param bslash True if a backslash+newline should be printed if the character
403 * limit for the line is reached, false otherwise.
405 typedef void (*BcNumDigitOp)(size_t n, size_t len, bool rdx, bool bslash);
408 * A function type to run an operator on @a a and @a b and store the result in
409 * @a a. This is used in karatsuba for faster adds and subtracts at the end.
410 * @param a The first parameter and return value.
411 * @param b The second parameter.
412 * @param len The minimum length of both arrays.
414 typedef void (*BcNumShiftAddOp)(BcDig* restrict a, const BcDig* restrict b,
418 * Initializes @a n with @a req limbs in its array.
419 * @param n The number to initialize.
420 * @param req The number of limbs @a n must have in its limb array.
422 void bc_num_init(BcNum *restrict n, size_t req);
425 * Initializes (sets up) @a n with the preallocated limb array @a num that has
426 * size @a cap. This is called by @a bc_num_init(), but it is also used by parts
427 * of bc that use statically allocated limb arrays.
428 * @param n The number to initialize.
429 * @param num The preallocated limb array.
430 * @param cap The capacity of @a num.
432 void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap);
435 * Copies @a s into @a d. This does a deep copy and requires that @a d is
436 * already a valid and allocated BcNum.
437 * @param d The destination BcNum.
438 * @param s The source BcNum.
440 void bc_num_copy(BcNum *d, const BcNum *s);
443 * Creates @a d and copies @a s into @a d. This does a deep copy and requires
444 * that @a d is *not* a valid or allocated BcNum.
445 * @param d The destination BcNum.
446 * @param s The source BcNum.
448 void bc_num_createCopy(BcNum *d, const BcNum *s);
451 * Creates (initializes) @a n and sets its value to the equivalent of @a val.
452 * @a n must *not* be a valid or preallocated BcNum.
453 * @param n The number to initialize and set.
454 * @param val The value to set @a n's value to.
456 void bc_num_createFromBigdig(BcNum *restrict n, BcBigDig val);
459 * Makes @a n valid for holding strings. @a n must *not* be allocated; this
460 * simply clears some fields, including setting the num field to NULL.
461 * @param n The number to clear.
463 void bc_num_clear(BcNum *restrict n);
466 * Frees @a num, which is a BcNum as a void pointer. This is a destructor.
467 * @param num The BcNum to free as a void pointer.
469 void bc_num_free(void *num);
472 * Returns the scale of @a n.
473 * @param n The number.
474 * @return The scale of @a n.
476 size_t bc_num_scale(const BcNum *restrict n);
479 * Returns the length (in decimal digits) of @a n. This is complicated. First,
480 * if the number is zero, we always return at least one, but we also return the
481 * scale if it exists. Then, If it is not zero, it opens a whole other can of
482 * worms. Read the comments in the definition.
483 * @param n The number.
484 * @return The length of @a n.
486 size_t bc_num_len(const BcNum *restrict n);
489 * Convert a number to a BcBigDig (hardware integer). This version does error
490 * checking, and if it finds an error, throws it. Otherwise, it calls
492 * @param n The number to convert.
493 * @return The number as a hardware integer.
495 BcBigDig bc_num_bigdig(const BcNum *restrict n);
498 * Convert a number to a BcBigDig (hardware integer). This version does no error
500 * @param n The number to convert.
501 * @return The number as a hardware integer.
503 BcBigDig bc_num_bigdig2(const BcNum *restrict n);
506 * Sets @a n to the value of @a val. @a n is expected to be a valid and
508 * @param n The number to set.
509 * @param val The value to set the number to.
511 void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val);
513 #if BC_ENABLE_EXTRA_MATH
516 * Generates a random arbitrary-size integer less than or equal to @a a and
517 * returns it in @a b. This implements irand().
518 * @param a The limit for the integer to generate.
519 * @param b The return value.
520 * @param rng The pseudo-random number generator.
522 void bc_num_irand(BcNum *restrict a, BcNum *restrict b,
523 struct BcRNG *restrict rng);
526 * Sets the seed for the PRNG @a rng from @a n.
527 * @param n The new seed for the PRNG.
528 * @param rng The PRNG to set the seed for.
530 void bc_num_rng(const BcNum *restrict n, struct BcRNG *rng);
533 * Sets @a n to the value produced by the PRNG. This implements rand().
534 * @param n The number to set.
535 * @param rng The pseudo-random number generator.
537 void bc_num_createFromRNG(BcNum *restrict n, struct BcRNG *rng);
539 #endif // BC_ENABLE_EXTRA_MATH
542 * The add function. This is a BcNumBinaryOp function.
543 * @param a The first parameter.
544 * @param b The second parameter.
545 * @param c The return value.
546 * @param scale The current scale.
548 void bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale);
551 * The subtract function. This is a BcNumBinaryOp function.
552 * @param a The first parameter.
553 * @param b The second parameter.
554 * @param c The return value.
555 * @param scale The current scale.
557 void bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale);
560 * The multiply function.
561 * @param a The first parameter. This is a BcNumBinaryOp function.
562 * @param b The second parameter.
563 * @param c The return value.
564 * @param scale The current scale.
566 void bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale);
569 * The division function.
570 * @param a The first parameter. This is a BcNumBinaryOp function.
571 * @param b The second parameter.
572 * @param c The return value.
573 * @param scale The current scale.
575 void bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale);
578 * The modulus function.
579 * @param a The first parameter. This is a BcNumBinaryOp function.
580 * @param b The second parameter.
581 * @param c The return value.
582 * @param scale The current scale.
584 void bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale);
587 * The power function.
588 * @param a The first parameter. This is a BcNumBinaryOp function.
589 * @param b The second parameter.
590 * @param c The return value.
591 * @param scale The current scale.
593 void bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale);
594 #if BC_ENABLE_EXTRA_MATH
597 * The places function (@ operator). This is a BcNumBinaryOp function.
598 * @param a The first parameter.
599 * @param b The second parameter.
600 * @param c The return value.
601 * @param scale The current scale.
603 void bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale);
606 * The left shift function (<< operator). This is a BcNumBinaryOp function.
607 * @param a The first parameter.
608 * @param b The second parameter.
609 * @param c The return value.
610 * @param scale The current scale.
612 void bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale);
615 * The right shift function (>> operator). This is a BcNumBinaryOp function.
616 * @param a The first parameter.
617 * @param b The second parameter.
618 * @param c The return value.
619 * @param scale The current scale.
621 void bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale);
623 #endif // BC_ENABLE_EXTRA_MATH
627 * @param a The first parameter.
628 * @param b The return value.
629 * @param scale The current scale.
631 void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale);
634 * Divsion and modulus together. This is a dc extension.
635 * @param a The first parameter.
636 * @param b The second parameter.
637 * @param c The first return value (quotient).
638 * @param d The second return value (modulus).
639 * @param scale The current scale.
641 void bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale);
644 * A function returning the required allocation size for an addition or a
645 * subtraction. This is a BcNumBinaryOpReq function.
646 * @param a The first parameter.
647 * @param b The second parameter.
648 * @param scale The current scale.
649 * @return The size of allocation needed for the result of add or subtract
650 * with @a a, @a b, and @a scale.
652 size_t bc_num_addReq(const BcNum* a, const BcNum* b, size_t scale);
655 * A function returning the required allocation size for a multiplication. This
656 * is a BcNumBinaryOpReq function.
657 * @param a The first parameter.
658 * @param b The second parameter.
659 * @param scale The current scale.
660 * @return The size of allocation needed for the result of multiplication
661 * with @a a, @a b, and @a scale.
663 size_t bc_num_mulReq(const BcNum *a, const BcNum *b, size_t scale);
666 * A function returning the required allocation size for a division or modulus.
667 * This is a BcNumBinaryOpReq function.
668 * @param a The first parameter.
669 * @param b The second parameter.
670 * @param scale The current scale.
671 * @return The size of allocation needed for the result of division or
672 * modulus with @a a, @a b, and @a scale.
674 size_t bc_num_divReq(const BcNum *a, const BcNum *b, size_t scale);
677 * A function returning the required allocation size for an exponentiation. This
678 * is a BcNumBinaryOpReq function.
679 * @param a The first parameter.
680 * @param b The second parameter.
681 * @param scale The current scale.
682 * @return The size of allocation needed for the result of exponentiation
683 * with @a a, @a b, and @a scale.
685 size_t bc_num_powReq(const BcNum *a, const BcNum *b, size_t scale);
687 #if BC_ENABLE_EXTRA_MATH
690 * A function returning the required allocation size for a places, left shift,
691 * or right shift. This is a BcNumBinaryOpReq function.
692 * @param a The first parameter.
693 * @param b The second parameter.
694 * @param scale The current scale.
695 * @return The size of allocation needed for the result of places, left
696 * shift, or right shift with @a a, @a b, and @a scale.
698 size_t bc_num_placesReq(const BcNum *a, const BcNum *b, size_t scale);
700 #endif // BC_ENABLE_EXTRA_MATH
703 * Truncate @a n *by* @a places decimal places. This only extends places *after*
705 * @param n The number to truncate.
706 * @param places The number of places to truncate @a n by.
708 void bc_num_truncate(BcNum *restrict n, size_t places);
711 * Extend @a n *by* @a places decimal places. This only extends places *after*
713 * @param n The number to truncate.
714 * @param places The number of places to extend @a n by.
716 void bc_num_extend(BcNum *restrict n, size_t places);
719 * Shifts @a n right by @a places decimal places. This is the workhorse of the
720 * right shift operator, and would be static to src/num.c, except that
721 * src/library.c uses it for efficiency when executing its frand.
722 * @param n The number to shift right.
723 * @param places The number of decimal places to shift @a n right by.
725 void bc_num_shiftRight(BcNum *restrict n, size_t places);
728 * Compare a and b and return the result of their comparison as an ssize_t.
729 * Returns >0 if @a a is greater than @a b, <0 if @a a is less than @a b, and =0
731 * @param a The first number.
732 * @param b The second number.
733 * @return The result of the comparison.
735 ssize_t bc_num_cmp(const BcNum *a, const BcNum *b);
738 * Modular exponentiation.
739 * @param a The first parameter.
740 * @param b The second parameter.
741 * @param c The third parameter.
742 * @param d The return value.
744 void bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d);
747 * Sets @a n to zero with a scale of zero.
748 * @param n The number to zero.
750 void bc_num_zero(BcNum *restrict n);
753 * Sets @a n to one with a scale of zero.
754 * @param n The number to set to one.
756 void bc_num_one(BcNum *restrict n);
759 * An efficient function to compare @a n to zero.
760 * @param n The number to compare to zero.
761 * @return The result of the comparison.
763 ssize_t bc_num_cmpZero(const BcNum *n);
765 #if !defined(NDEBUG) || BC_ENABLE_LIBRARY
768 * Check a number string for validity and return true if it is, false otherwise.
769 * The library needs this to check user-supplied strings, but in bc and dc, this
770 * is only used for debug asserts because the parsers should get the numbers
771 * parsed right, which should ensure they are always valid.
772 * @param val The string to check.
773 * @return True if the string is a valid number, false otherwise.
775 bool bc_num_strValid(const char *restrict val);
777 #endif // !defined(NDEBUG) || BC_ENABLE_LIBRARY
780 * Parses a number string into the number @a n according to @a base.
781 * @param n The number to set to the parsed value.
782 * @param val The number string to parse.
783 * @param base The base to parse the number string by.
785 void bc_num_parse(BcNum *restrict n, const char *restrict val, BcBigDig base);
788 * Prints the number @a n according to @a base.
789 * @param n The number to print.
790 * @param base The base to print the number by.
791 * @param newline True if a newline should be inserted at the end, false
794 void bc_num_print(BcNum *restrict n, BcBigDig base, bool newline);
796 #if !BC_ENABLE_LIBRARY
799 * Prints a number as a character stream.
800 * @param n The number to print as a character stream.
802 void bc_num_stream(BcNum *restrict n);
804 #endif // !BC_ENABLE_LIBRARY
809 * Print a number with a label. This is a debug-only function.
810 * @param n The number to print.
811 * @param name The label to print the number with.
812 * @param emptyline True if there should be an empty line after the number.
814 void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline);
817 * Print the limbs of @a n. This is a debug-only function.
818 * @param n The number to print.
819 * @param len The length of the number.
820 * @param emptyline True if there should be an empty line after the number.
822 void bc_num_printDigs(const BcDig* n, size_t len, bool emptyline);
825 * Print debug info about @a n along with its limbs.
826 * @param n The number to print.
827 * @param name The label to print the number with.
828 * @param emptyline True if there should be an empty line after the number.
830 void bc_num_printWithDigs(const BcNum *n, const char *name, bool emptyline);
833 * Dump debug info about a BcNum variable.
834 * @param varname The variable name.
835 * @param n The number.
837 void bc_num_dump(const char *varname, const BcNum *n);
839 #endif // BC_DEBUG_CODE
841 /// A reference to an array of hex digits for easy conversion for printing.
842 extern const char bc_num_hex_digits[];
844 /// An array of powers of 10 for easy conversion from number of digits to
846 extern const BcBigDig bc_num_pow10[BC_BASE_DIGS + 1];
848 /// A reference to a constant array that is the max of a BigDig.
849 extern const BcDig bc_num_bigdigMax[];
851 /// A reference to a constant size of the above array.
852 extern const size_t bc_num_bigdigMax_size;
854 /// A reference to a constant array that is 2 times the max of a BigDig.
855 extern const BcDig bc_num_bigdigMax2[];
857 /// A reference to a constant size of the above array.
858 extern const size_t bc_num_bigdigMax2_size;