/*1* *****************************************************************************2*3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2018-2025 Gavin D. Howard and contributors.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions are met:9*10* * Redistributions of source code must retain the above copyright notice, this11* list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright notice,14* this list of conditions and the following disclaimer in the documentation15* and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"18* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE19* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE21* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS24* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN25* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)26* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGE.28*29* *****************************************************************************30*31* Definitions for the num type.32*33*/3435#ifndef BC_NUM_H36#define BC_NUM_H3738#include <limits.h>39#include <stdbool.h>40#include <stddef.h>41#include <stdint.h>4243#include <sys/types.h>4445#include <status.h>46#include <vector.h>47#include <bcl.h>4849/// Everything in bc is base 10..50#define BC_BASE (10)5152/// Alias.53typedef unsigned long ulong;5455/// This is here because BcBigDig came first, but when I created bcl, it's56/// definition has to be defined first.57typedef BclBigDig BcBigDig;5859#if BC_LONG_BIT >= 646061/// The biggest number held by a BcBigDig.62#define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT64_MAX)6364/// The number of decimal digits in one limb.65#define BC_BASE_DIGS (9)6667/// The max number + 1 that one limb can hold.68#define BC_BASE_POW (1000000000)6970/// An alias for portability.71#define BC_NUM_BIGDIG_C UINT64_C7273/// The max number + 1 that two limbs can hold. This is used for generating74/// numbers because the PRNG can generate a number that will fill two limbs.75#define BC_BASE_RAND_POW (BC_NUM_BIGDIG_C(1000000000000000000))7677/// The actual limb type.78typedef int_least32_t BcDig;7980#elif BC_LONG_BIT >= 328182/// The biggest number held by a BcBigDig.83#define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT32_MAX)8485/// The number of decimal digits in one limb.86#define BC_BASE_DIGS (4)8788/// The max number + 1 that one limb can hold.89#define BC_BASE_POW (10000)9091/// An alias for portability.92#define BC_NUM_BIGDIG_C UINT32_C9394/// The max number + 1 that two limbs can hold. This is used for generating95/// numbers because the PRNG can generate a number that will fill two limbs.96#define BC_BASE_RAND_POW (UINT64_C(100000000))9798/// The actual limb type.99typedef int_least16_t BcDig;100101#else102103/// LONG_BIT must be at least 32 on POSIX. We depend on that.104#error BC_LONG_BIT must be at least 32105106#endif // BC_LONG_BIT >= 64107108/// The default (and minimum) number of limbs when allocating a number.109#define BC_NUM_DEF_SIZE (8)110111/// The actual number struct. This is where the magic happens.112typedef struct BcNum113{114/// The limb array. It is restrict because *no* other item should own the115/// array. For more information, see the development manual116/// (manuals/development.md#numbers).117BcDig* restrict num;118119/// The number of limbs before the decimal (radix) point. This also stores120/// the negative bit in the least significant bit since it uses at least two121/// bits less than scale. It is also used less than scale. See the122/// development manual (manuals/development.md#numbers) for more info.123size_t rdx;124125/// The actual scale of the number. This is different from rdx because there126/// are multiple digits in one limb, and in the last limb, only some of the127/// digits may be part of the scale. However, scale must always match rdx128/// (except when the number is 0), or there is a bug. For more information,129/// see the development manual (manuals/development.md#numbers).130size_t scale;131132/// The number of valid limbs in the array. If this is 0, then the number is133/// 0 as well.134size_t len;135136/// The capacity of the limbs array. This is how many limbs the number could137/// expand to without reallocation.138size_t cap;139140} BcNum;141142#if BC_ENABLE_EXTRA_MATH143144// Forward declaration145struct BcRNG;146147#endif // BC_ENABLE_EXTRA_MATH148149/// The minimum obase.150#define BC_NUM_MIN_BASE (BC_NUM_BIGDIG_C(2))151152/// The maximum ibase allowed by POSIX.153#define BC_NUM_MAX_POSIX_IBASE (BC_NUM_BIGDIG_C(16))154155/// The actual ibase supported by this implementation.156#define BC_NUM_MAX_IBASE (BC_NUM_BIGDIG_C(36))157158/// The max base allowed by bc_num_parseChar().159#define BC_NUM_MAX_LBASE (BC_NUM_BIGDIG_C('Z' + BC_BASE + 1))160161/// The default number of characters to print before a backslash newline.162#define BC_NUM_PRINT_WIDTH (BC_NUM_BIGDIG_C(69))163164/// The base for printing streams from numbers.165#define BC_NUM_STREAM_BASE (256)166167// This sets a default for the Karatsuba length.168#ifndef BC_NUM_KARATSUBA_LEN169#define BC_NUM_KARATSUBA_LEN (BC_NUM_BIGDIG_C(32))170#elif BC_NUM_KARATSUBA_LEN < 16171#error BC_NUM_KARATSUBA_LEN must be at least 16.172#endif // BC_NUM_KARATSUBA_LEN173174// A crude, but always big enough, calculation of175// the size required for ibase and obase BcNum's.176#define BC_NUM_BIGDIG_LOG10 (BC_NUM_DEF_SIZE)177178/**179* Returns non-zero if the BcNum @a n is non-zero.180* @param n The number to test.181* @return Non-zero if @a n is non-zero, zero otherwise.182*/183#define BC_NUM_NONZERO(n) ((n)->len)184185/**186* Returns true if the BcNum @a n is zero.187* @param n The number to test.188* @return True if @a n is zero, false otherwise.189*/190#define BC_NUM_ZERO(n) (!BC_NUM_NONZERO(n))191192/**193* Returns true if the BcNum @a n is one with no scale.194* @param n The number to test.195* @return True if @a n equals 1 with no scale, false otherwise.196*/197#define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)198199/**200* Converts the letter @a c into a number.201* @param c The letter to convert.202* @return The number corresponding to the letter.203*/204#define BC_NUM_NUM_LETTER(c) ((c) - 'A' + BC_BASE)205206/// The number of allocations done by bc_num_k(). If you change the number of207/// allocations, you must change this. This is done in order to allocate them208/// all as one allocation and just give them all pointers to different parts.209/// Works pretty well, but you have to be careful.210#define BC_NUM_KARATSUBA_ALLOCS (6)211212/**213* Rounds @a s (scale) up to the next power of BC_BASE_DIGS. This will also214* check for overflow and gives a fatal error if that happens because we just215* can't go over the limits we have imposed.216* @param s The scale to round up.217* @return @a s rounded up to the next power of BC_BASE_DIGS.218*/219#define BC_NUM_ROUND_POW(s) (bc_vm_growSize((s), BC_BASE_DIGS - 1))220221/**222* Returns the equivalent rdx for the scale @a s.223* @param s The scale to convert.224* @return The rdx for @a s.225*/226#define BC_NUM_RDX(s) (BC_NUM_ROUND_POW(s) / BC_BASE_DIGS)227228/**229* Returns the actual rdx of @a n. (It removes the negative bit.)230* @param n The number.231* @return The real rdx of @a n.232*/233#define BC_NUM_RDX_VAL(n) ((n)->rdx >> 1)234235/**236* Returns the actual rdx of @a n, where @a n is not a pointer. (It removes the237* negative bit.)238* @param n The number.239* @return The real rdx of @a n.240*/241#define BC_NUM_RDX_VAL_NP(n) ((n).rdx >> 1)242243/**244* Sets the rdx of @a n to @a v.245* @param n The number.246* @param v The value to set the rdx to.247*/248#define BC_NUM_RDX_SET(n, v) \249((n)->rdx = (((v) << 1) | ((n)->rdx & (BcBigDig) 1)))250251/**252* Sets the rdx of @a n to @a v, where @a n is not a pointer.253* @param n The number.254* @param v The value to set the rdx to.255*/256#define BC_NUM_RDX_SET_NP(n, v) \257((n).rdx = (((v) << 1) | ((n).rdx & (BcBigDig) 1)))258259/**260* Sets the rdx of @a n to @a v and the negative bit to @a neg.261* @param n The number.262* @param v The value to set the rdx to.263* @param neg The value to set the negative bit to.264*/265#define BC_NUM_RDX_SET_NEG(n, v, neg) ((n)->rdx = (((v) << 1) | (neg)))266267/**268* Returns true if the rdx and scale for @a n match.269* @param n The number to test.270* @return True if the rdx and scale of @a n match, false otherwise.271*/272#define BC_NUM_RDX_VALID(n) \273(BC_NUM_ZERO(n) || BC_NUM_RDX_VAL(n) * BC_BASE_DIGS >= (n)->scale)274275/**276* Returns true if the rdx and scale for @a n match, where @a n is not a277* pointer.278* @param n The number to test.279* @return True if the rdx and scale of @a n match, false otherwise.280*/281#define BC_NUM_RDX_VALID_NP(n) \282((!(n).len) || BC_NUM_RDX_VAL_NP(n) * BC_BASE_DIGS >= (n).scale)283284/**285* Returns true if @a n is negative, false otherwise.286* @param n The number to test.287* @return True if @a n is negative, false otherwise.288*/289#define BC_NUM_NEG(n) ((n)->rdx & ((BcBigDig) 1))290291/**292* Returns true if @a n is negative, false otherwise, where @a n is not a293* pointer.294* @param n The number to test.295* @return True if @a n is negative, false otherwise.296*/297#define BC_NUM_NEG_NP(n) ((n).rdx & ((BcBigDig) 1))298299/**300* Clears the negative bit on @a n.301* @param n The number.302*/303#define BC_NUM_NEG_CLR(n) ((n)->rdx &= ~((BcBigDig) 1))304305/**306* Clears the negative bit on @a n, where @a n is not a pointer.307* @param n The number.308*/309#define BC_NUM_NEG_CLR_NP(n) ((n).rdx &= ~((BcBigDig) 1))310311/**312* Sets the negative bit on @a n.313* @param n The number.314*/315#define BC_NUM_NEG_SET(n) ((n)->rdx |= ((BcBigDig) 1))316317/**318* Toggles the negative bit on @a n.319* @param n The number.320*/321#define BC_NUM_NEG_TGL(n) ((n)->rdx ^= ((BcBigDig) 1))322323/**324* Toggles the negative bit on @a n, where @a n is not a pointer.325* @param n The number.326*/327#define BC_NUM_NEG_TGL_NP(n) ((n).rdx ^= ((BcBigDig) 1))328329/**330* Returns the rdx val for @a n if the negative bit is set to @a v.331* @param n The number.332* @param v The value for the negative bit.333* @return The value of the rdx of @a n if the negative bit were set to @a v.334*/335#define BC_NUM_NEG_VAL(n, v) (((n)->rdx & ~((BcBigDig) 1)) | (v))336337/**338* Returns the rdx val for @a n if the negative bit is set to @a v, where @a n339* is not a pointer.340* @param n The number.341* @param v The value for the negative bit.342* @return The value of the rdx of @a n if the negative bit were set to @a v.343*/344#define BC_NUM_NEG_VAL_NP(n, v) (((n).rdx & ~((BcBigDig) 1)) | (v))345346/**347* Returns the size, in bytes, of limb array with @a n limbs.348* @param n The number.349* @return The size, in bytes, of a limb array with @a n limbs.350*/351#define BC_NUM_SIZE(n) ((n) * sizeof(BcDig))352353// These are for debugging only.354#if BC_DEBUG_CODE355#define BC_NUM_PRINT(x) fprintf(stderr, "%s = %lu\n", #x, (unsigned long) (x))356#define DUMP_NUM bc_num_dump357#else // BC_DEBUG_CODE358#undef DUMP_NUM359#define DUMP_NUM(x, y)360#define BC_NUM_PRINT(x)361#endif // BC_DEBUG_CODE362363/**364* A function type for binary operators.365* @param a The first parameter.366* @param b The second parameter.367* @param c The return value.368* @param scale The current scale.369*/370typedef void (*BcNumBinaryOp)(BcNum* a, BcNum* b, BcNum* c, size_t scale);371372/**373* A function type for binary operators *after* @a c has been properly374* allocated. At this point, *nothing* should be pointing to @a c (in any way375* that matters, anyway).376* @param a The first operand.377* @param b The second operand.378* @param c The return parameter.379* @param scale The current scale.380*/381typedef void (*BcNumBinOp)(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale);382383/**384* A function type for getting the allocation size needed for a binary operator.385* Any function used for this *must* return enough space for *all* possible386* invocations of the operator.387* @param a The first parameter.388* @param b The second parameter.389* @param scale The current scale.390* @return The size of allocation needed for the result of the operator391* with @a a, @a b, and @a scale.392*/393typedef size_t (*BcNumBinaryOpReq)(const BcNum* a, const BcNum* b,394size_t scale);395396/**397* A function type for printing a "digit." Functions of this type will print one398* digit in a number. Digits are printed differently based on the base, which is399* why there is more than one implementation of this function type.400* @param n The "digit" to print.401* @param len The "length" of the digit, or number of characters that will402* need to be printed for the digit.403* @param rdx True if a decimal (radix) point should be printed.404* @param bslash True if a backslash+newline should be printed if the character405* limit for the line is reached, false otherwise.406*/407typedef void (*BcNumDigitOp)(size_t n, size_t len, bool rdx, bool bslash);408409/**410* A function type to run an operator on @a a and @a b and store the result in411* @a a. This is used in karatsuba for faster adds and subtracts at the end.412* @param a The first parameter and return value.413* @param b The second parameter.414* @param len The minimum length of both arrays.415*/416typedef void (*BcNumShiftAddOp)(BcDig* restrict a, const BcDig* restrict b,417size_t len);418419/**420* Initializes @a n with @a req limbs in its array.421* @param n The number to initialize.422* @param req The number of limbs @a n must have in its limb array.423*/424void425bc_num_init(BcNum* restrict n, size_t req);426427/**428* Initializes (sets up) @a n with the preallocated limb array @a num that has429* size @a cap. This is called by @a bc_num_init(), but it is also used by parts430* of bc that use statically allocated limb arrays.431* @param n The number to initialize.432* @param num The preallocated limb array.433* @param cap The capacity of @a num.434*/435void436bc_num_setup(BcNum* restrict n, BcDig* restrict num, size_t cap);437438/**439* Copies @a s into @a d. This does a deep copy and requires that @a d is440* already a valid and allocated BcNum.441* @param d The destination BcNum.442* @param s The source BcNum.443*/444void445bc_num_copy(BcNum* d, const BcNum* s);446447/**448* Creates @a d and copies @a s into @a d. This does a deep copy and requires449* that @a d is *not* a valid or allocated BcNum.450* @param d The destination BcNum.451* @param s The source BcNum.452*/453void454bc_num_createCopy(BcNum* d, const BcNum* s);455456/**457* Creates (initializes) @a n and sets its value to the equivalent of @a val.458* @a n must *not* be a valid or preallocated BcNum.459* @param n The number to initialize and set.460* @param val The value to set @a n's value to.461*/462void463bc_num_createFromBigdig(BcNum* restrict n, BcBigDig val);464465/**466* Makes @a n valid for holding strings. @a n must *not* be allocated; this467* simply clears some fields, including setting the num field to NULL.468* @param n The number to clear.469*/470void471bc_num_clear(BcNum* restrict n);472473/**474* Frees @a num, which is a BcNum as a void pointer. This is a destructor.475* @param num The BcNum to free as a void pointer.476*/477void478bc_num_free(void* num);479480/**481* Returns the scale of @a n.482* @param n The number.483* @return The scale of @a n.484*/485size_t486bc_num_scale(const BcNum* restrict n);487488/**489* Returns the length (in decimal digits) of @a n. This is complicated. First,490* if the number is zero, we always return at least one, but we also return the491* scale if it exists. Then, If it is not zero, it opens a whole other can of492* worms. Read the comments in the definition.493* @param n The number.494* @return The length of @a n.495*/496size_t497bc_num_len(const BcNum* restrict n);498499/**500* Convert a number to a BcBigDig (hardware integer). This version does error501* checking, and if it finds an error, throws it. Otherwise, it calls502* bc_num_bigdig2().503* @param n The number to convert.504* @return The number as a hardware integer.505*/506BcBigDig507bc_num_bigdig(const BcNum* restrict n);508509/**510* Convert a number to a BcBigDig (hardware integer). This version does no error511* checking.512* @param n The number to convert.513* @return The number as a hardware integer.514*/515BcBigDig516bc_num_bigdig2(const BcNum* restrict n);517518/**519* Sets @a n to the value of @a val. @a n is expected to be a valid and520* allocated BcNum.521* @param n The number to set.522* @param val The value to set the number to.523*/524void525bc_num_bigdig2num(BcNum* restrict n, BcBigDig val);526527#if BC_ENABLE_EXTRA_MATH528529/**530* Generates a random arbitrary-size integer less than or equal to @a a and531* returns it in @a b. This implements irand().532* @param a The limit for the integer to generate.533* @param b The return value.534* @param rng The pseudo-random number generator.535*/536void537bc_num_irand(BcNum* restrict a, BcNum* restrict b, struct BcRNG* restrict rng);538539/**540* Sets the seed for the PRNG @a rng from @a n.541* @param n The new seed for the PRNG.542* @param rng The PRNG to set the seed for.543*/544void545bc_num_rng(const BcNum* restrict n, struct BcRNG* rng);546547/**548* Sets @a n to the value produced by the PRNG. This implements rand().549* @param n The number to set.550* @param rng The pseudo-random number generator.551*/552void553bc_num_createFromRNG(BcNum* restrict n, struct BcRNG* rng);554555#endif // BC_ENABLE_EXTRA_MATH556557/**558* The add function. This is a BcNumBinaryOp function.559* @param a The first parameter.560* @param b The second parameter.561* @param c The return value.562* @param scale The current scale.563*/564void565bc_num_add(BcNum* a, BcNum* b, BcNum* c, size_t scale);566567/**568* The subtract function. This is a BcNumBinaryOp function.569* @param a The first parameter.570* @param b The second parameter.571* @param c The return value.572* @param scale The current scale.573*/574void575bc_num_sub(BcNum* a, BcNum* b, BcNum* c, size_t scale);576577/**578* The multiply 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.583*/584void585bc_num_mul(BcNum* a, BcNum* b, BcNum* c, size_t scale);586587/**588* The division function.589* @param a The first parameter. This is a BcNumBinaryOp function.590* @param b The second parameter.591* @param c The return value.592* @param scale The current scale.593*/594void595bc_num_div(BcNum* a, BcNum* b, BcNum* c, size_t scale);596597/**598* The modulus function.599* @param a The first parameter. This is a BcNumBinaryOp function.600* @param b The second parameter.601* @param c The return value.602* @param scale The current scale.603*/604void605bc_num_mod(BcNum* a, BcNum* b, BcNum* c, size_t scale);606607/**608* The power function.609* @param a The first parameter. This is a BcNumBinaryOp function.610* @param b The second parameter.611* @param c The return value.612* @param scale The current scale.613*/614void615bc_num_pow(BcNum* a, BcNum* b, BcNum* c, size_t scale);616#if BC_ENABLE_EXTRA_MATH617618/**619* The places function (@ operator). This is a BcNumBinaryOp function.620* @param a The first parameter.621* @param b The second parameter.622* @param c The return value.623* @param scale The current scale.624*/625void626bc_num_places(BcNum* a, BcNum* b, BcNum* c, size_t scale);627628/**629* The left shift function (<< operator). This is a BcNumBinaryOp function.630* @param a The first parameter.631* @param b The second parameter.632* @param c The return value.633* @param scale The current scale.634*/635void636bc_num_lshift(BcNum* a, BcNum* b, BcNum* c, size_t scale);637638/**639* The right shift function (>> operator). This is a BcNumBinaryOp function.640* @param a The first parameter.641* @param b The second parameter.642* @param c The return value.643* @param scale The current scale.644*/645void646bc_num_rshift(BcNum* a, BcNum* b, BcNum* c, size_t scale);647648#endif // BC_ENABLE_EXTRA_MATH649650/**651* Square root.652* @param a The first parameter.653* @param b The return value.654* @param scale The current scale.655*/656void657bc_num_sqrt(BcNum* restrict a, BcNum* restrict b, size_t scale);658659/**660* Divsion and modulus together. This is a dc extension.661* @param a The first parameter.662* @param b The second parameter.663* @param c The first return value (quotient).664* @param d The second return value (modulus).665* @param scale The current scale.666*/667void668bc_num_divmod(BcNum* a, BcNum* b, BcNum* c, BcNum* d, size_t scale);669670/**671* A function returning the required allocation size for an addition or a672* subtraction. This is a BcNumBinaryOpReq function.673* @param a The first parameter.674* @param b The second parameter.675* @param scale The current scale.676* @return The size of allocation needed for the result of add or subtract677* with @a a, @a b, and @a scale.678*/679size_t680bc_num_addReq(const BcNum* a, const BcNum* b, size_t scale);681682/**683* A function returning the required allocation size for a multiplication. This684* is a BcNumBinaryOpReq function.685* @param a The first parameter.686* @param b The second parameter.687* @param scale The current scale.688* @return The size of allocation needed for the result of multiplication689* with @a a, @a b, and @a scale.690*/691size_t692bc_num_mulReq(const BcNum* a, const BcNum* b, size_t scale);693694/**695* A function returning the required allocation size for a division or modulus.696* This is a BcNumBinaryOpReq function.697* @param a The first parameter.698* @param b The second parameter.699* @param scale The current scale.700* @return The size of allocation needed for the result of division or701* modulus with @a a, @a b, and @a scale.702*/703size_t704bc_num_divReq(const BcNum* a, const BcNum* b, size_t scale);705706/**707* A function returning the required allocation size for an exponentiation. This708* is a BcNumBinaryOpReq function.709* @param a The first parameter.710* @param b The second parameter.711* @param scale The current scale.712* @return The size of allocation needed for the result of exponentiation713* with @a a, @a b, and @a scale.714*/715size_t716bc_num_powReq(const BcNum* a, const BcNum* b, size_t scale);717718#if BC_ENABLE_EXTRA_MATH719720/**721* A function returning the required allocation size for a places, left shift,722* or right shift. This is a BcNumBinaryOpReq function.723* @param a The first parameter.724* @param b The second parameter.725* @param scale The current scale.726* @return The size of allocation needed for the result of places, left727* shift, or right shift with @a a, @a b, and @a scale.728*/729size_t730bc_num_placesReq(const BcNum* a, const BcNum* b, size_t scale);731732#endif // BC_ENABLE_EXTRA_MATH733734/**735* Truncate @a n *by* @a places decimal places. This only extends places *after*736* the decimal point.737* @param n The number to truncate.738* @param places The number of places to truncate @a n by.739*/740void741bc_num_truncate(BcNum* restrict n, size_t places);742743/**744* Extend @a n *by* @a places decimal places. This only extends places *after*745* the decimal point.746* @param n The number to truncate.747* @param places The number of places to extend @a n by.748*/749void750bc_num_extend(BcNum* restrict n, size_t places);751752/**753* Shifts @a n right by @a places decimal places. This is the workhorse of the754* right shift operator, and would be static to src/num.c, except that755* src/library.c uses it for efficiency when executing its frand.756* @param n The number to shift right.757* @param places The number of decimal places to shift @a n right by.758*/759void760bc_num_shiftRight(BcNum* restrict n, size_t places);761762/**763* Compare a and b and return the result of their comparison as an ssize_t.764* Returns >0 if @a a is greater than @a b, <0 if @a a is less than @a b, and =0765* if a == b.766* @param a The first number.767* @param b The second number.768* @return The result of the comparison.769*/770ssize_t771bc_num_cmp(const BcNum* a, const BcNum* b);772773/**774* Modular exponentiation.775* @param a The first parameter.776* @param b The second parameter.777* @param c The third parameter.778* @param d The return value.779*/780void781bc_num_modexp(BcNum* a, BcNum* b, BcNum* c, BcNum* restrict d);782783/**784* Sets @a n to zero with a scale of zero.785* @param n The number to zero.786*/787void788bc_num_zero(BcNum* restrict n);789790/**791* Sets @a n to one with a scale of zero.792* @param n The number to set to one.793*/794void795bc_num_one(BcNum* restrict n);796797/**798* An efficient function to compare @a n to zero.799* @param n The number to compare to zero.800* @return The result of the comparison.801*/802ssize_t803bc_num_cmpZero(const BcNum* n);804805/**806* Check a number string for validity and return true if it is, false otherwise.807* The library needs this to check user-supplied strings, but in bc and dc, this808* is only used for debug asserts because the parsers should get the numbers809* parsed right, which should ensure they are always valid.810* @param val The string to check.811* @return True if the string is a valid number, false otherwise.812*/813bool814bc_num_strValid(const char* restrict val);815816/**817* Parses a number string into the number @a n according to @a base.818* @param n The number to set to the parsed value.819* @param val The number string to parse.820* @param base The base to parse the number string by.821*/822void823bc_num_parse(BcNum* restrict n, const char* restrict val, BcBigDig base);824825/**826* Prints the number @a n according to @a base.827* @param n The number to print.828* @param base The base to print the number by.829* @param newline True if a newline should be inserted at the end, false830* otherwise.831*/832void833bc_num_print(BcNum* restrict n, BcBigDig base, bool newline);834835/**836* Invert @a into @a b at the current scale.837* @param a The number to invert.838* @param b The return parameter. This must be preallocated.839* @param scale The current scale.840*/841#define bc_num_inv(a, b, scale) bc_num_div(&vm->one, (a), (b), (scale))842843#if !BC_ENABLE_LIBRARY844845/**846* Prints a number as a character stream.847* @param n The number to print as a character stream.848*/849void850bc_num_stream(BcNum* restrict n);851852#endif // !BC_ENABLE_LIBRARY853854#if BC_DEBUG_CODE855856/**857* Print a number with a label. This is a debug-only function.858* @param n The number to print.859* @param name The label to print the number with.860* @param emptyline True if there should be an empty line after the number.861*/862void863bc_num_printDebug(const BcNum* n, const char* name, bool emptyline);864865/**866* Print the limbs of @a n. This is a debug-only function.867* @param n The number to print.868* @param len The length of the number.869* @param emptyline True if there should be an empty line after the number.870*/871void872bc_num_printDigs(const BcDig* n, size_t len, bool emptyline);873874/**875* Print debug info about @a n along with its limbs.876* @param n The number to print.877* @param name The label to print the number with.878* @param emptyline True if there should be an empty line after the number.879*/880void881bc_num_printWithDigs(const BcNum* n, const char* name, bool emptyline);882883/**884* Dump debug info about a BcNum variable.885* @param varname The variable name.886* @param n The number.887*/888void889bc_num_dump(const char* varname, const BcNum* n);890891#endif // BC_DEBUG_CODE892893/// A reference to an array of hex digits for easy conversion for printing.894extern const char bc_num_hex_digits[];895896/// An array of powers of 10 for easy conversion from number of digits to897/// powers.898extern const BcBigDig bc_num_pow10[BC_BASE_DIGS + 1];899900/// A reference to a constant array that is the max of a BigDig.901extern const BcDig bc_num_bigdigMax[];902903/// A reference to a constant size of the above array.904extern const size_t bc_num_bigdigMax_size;905906/// A reference to a constant array that is 2 times the max of a BigDig.907extern const BcDig bc_num_bigdigMax2[];908909/// A reference to a constant size of the above array.910extern const size_t bc_num_bigdigMax2_size;911912#endif // BC_NUM_H913914915