1*5a6e8488SAndroid Build Coastguard Worker /* 2*5a6e8488SAndroid Build Coastguard Worker * ***************************************************************************** 3*5a6e8488SAndroid Build Coastguard Worker * 4*5a6e8488SAndroid Build Coastguard Worker * SPDX-License-Identifier: BSD-2-Clause 5*5a6e8488SAndroid Build Coastguard Worker * 6*5a6e8488SAndroid Build Coastguard Worker * Copyright (c) 2018-2024 Gavin D. Howard and contributors. 7*5a6e8488SAndroid Build Coastguard Worker * 8*5a6e8488SAndroid Build Coastguard Worker * Redistribution and use in source and binary forms, with or without 9*5a6e8488SAndroid Build Coastguard Worker * modification, are permitted provided that the following conditions are met: 10*5a6e8488SAndroid Build Coastguard Worker * 11*5a6e8488SAndroid Build Coastguard Worker * * Redistributions of source code must retain the above copyright notice, this 12*5a6e8488SAndroid Build Coastguard Worker * list of conditions and the following disclaimer. 13*5a6e8488SAndroid Build Coastguard Worker * 14*5a6e8488SAndroid Build Coastguard Worker * * Redistributions in binary form must reproduce the above copyright notice, 15*5a6e8488SAndroid Build Coastguard Worker * this list of conditions and the following disclaimer in the documentation 16*5a6e8488SAndroid Build Coastguard Worker * and/or other materials provided with the distribution. 17*5a6e8488SAndroid Build Coastguard Worker * 18*5a6e8488SAndroid Build Coastguard Worker * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*5a6e8488SAndroid Build Coastguard Worker * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*5a6e8488SAndroid Build Coastguard Worker * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*5a6e8488SAndroid Build Coastguard Worker * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*5a6e8488SAndroid Build Coastguard Worker * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*5a6e8488SAndroid Build Coastguard Worker * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*5a6e8488SAndroid Build Coastguard Worker * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*5a6e8488SAndroid Build Coastguard Worker * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*5a6e8488SAndroid Build Coastguard Worker * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*5a6e8488SAndroid Build Coastguard Worker * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*5a6e8488SAndroid Build Coastguard Worker * POSSIBILITY OF SUCH DAMAGE. 29*5a6e8488SAndroid Build Coastguard Worker * 30*5a6e8488SAndroid Build Coastguard Worker * ***************************************************************************** 31*5a6e8488SAndroid Build Coastguard Worker * 32*5a6e8488SAndroid Build Coastguard Worker * Definitions for the num type. 33*5a6e8488SAndroid Build Coastguard Worker * 34*5a6e8488SAndroid Build Coastguard Worker */ 35*5a6e8488SAndroid Build Coastguard Worker 36*5a6e8488SAndroid Build Coastguard Worker #ifndef BC_NUM_H 37*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_H 38*5a6e8488SAndroid Build Coastguard Worker 39*5a6e8488SAndroid Build Coastguard Worker #include <limits.h> 40*5a6e8488SAndroid Build Coastguard Worker #include <stdbool.h> 41*5a6e8488SAndroid Build Coastguard Worker #include <stddef.h> 42*5a6e8488SAndroid Build Coastguard Worker #include <stdint.h> 43*5a6e8488SAndroid Build Coastguard Worker 44*5a6e8488SAndroid Build Coastguard Worker #include <sys/types.h> 45*5a6e8488SAndroid Build Coastguard Worker 46*5a6e8488SAndroid Build Coastguard Worker #include <status.h> 47*5a6e8488SAndroid Build Coastguard Worker #include <vector.h> 48*5a6e8488SAndroid Build Coastguard Worker #include <bcl.h> 49*5a6e8488SAndroid Build Coastguard Worker 50*5a6e8488SAndroid Build Coastguard Worker /// Everything in bc is base 10.. 51*5a6e8488SAndroid Build Coastguard Worker #define BC_BASE (10) 52*5a6e8488SAndroid Build Coastguard Worker 53*5a6e8488SAndroid Build Coastguard Worker /// Alias. 54*5a6e8488SAndroid Build Coastguard Worker typedef unsigned long ulong; 55*5a6e8488SAndroid Build Coastguard Worker 56*5a6e8488SAndroid Build Coastguard Worker /// This is here because BcBigDig came first, but when I created bcl, it's 57*5a6e8488SAndroid Build Coastguard Worker /// definition has to be defined first. 58*5a6e8488SAndroid Build Coastguard Worker typedef BclBigDig BcBigDig; 59*5a6e8488SAndroid Build Coastguard Worker 60*5a6e8488SAndroid Build Coastguard Worker #if BC_LONG_BIT >= 64 61*5a6e8488SAndroid Build Coastguard Worker 62*5a6e8488SAndroid Build Coastguard Worker /// The biggest number held by a BcBigDig. 63*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT64_MAX) 64*5a6e8488SAndroid Build Coastguard Worker 65*5a6e8488SAndroid Build Coastguard Worker /// The number of decimal digits in one limb. 66*5a6e8488SAndroid Build Coastguard Worker #define BC_BASE_DIGS (9) 67*5a6e8488SAndroid Build Coastguard Worker 68*5a6e8488SAndroid Build Coastguard Worker /// The max number + 1 that one limb can hold. 69*5a6e8488SAndroid Build Coastguard Worker #define BC_BASE_POW (1000000000) 70*5a6e8488SAndroid Build Coastguard Worker 71*5a6e8488SAndroid Build Coastguard Worker /// An alias for portability. 72*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_BIGDIG_C UINT64_C 73*5a6e8488SAndroid Build Coastguard Worker 74*5a6e8488SAndroid Build Coastguard Worker /// The max number + 1 that two limbs can hold. This is used for generating 75*5a6e8488SAndroid Build Coastguard Worker /// numbers because the PRNG can generate a number that will fill two limbs. 76*5a6e8488SAndroid Build Coastguard Worker #define BC_BASE_RAND_POW (BC_NUM_BIGDIG_C(1000000000000000000)) 77*5a6e8488SAndroid Build Coastguard Worker 78*5a6e8488SAndroid Build Coastguard Worker /// The actual limb type. 79*5a6e8488SAndroid Build Coastguard Worker typedef int_least32_t BcDig; 80*5a6e8488SAndroid Build Coastguard Worker 81*5a6e8488SAndroid Build Coastguard Worker #elif BC_LONG_BIT >= 32 82*5a6e8488SAndroid Build Coastguard Worker 83*5a6e8488SAndroid Build Coastguard Worker /// The biggest number held by a BcBigDig. 84*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT32_MAX) 85*5a6e8488SAndroid Build Coastguard Worker 86*5a6e8488SAndroid Build Coastguard Worker /// The number of decimal digits in one limb. 87*5a6e8488SAndroid Build Coastguard Worker #define BC_BASE_DIGS (4) 88*5a6e8488SAndroid Build Coastguard Worker 89*5a6e8488SAndroid Build Coastguard Worker /// The max number + 1 that one limb can hold. 90*5a6e8488SAndroid Build Coastguard Worker #define BC_BASE_POW (10000) 91*5a6e8488SAndroid Build Coastguard Worker 92*5a6e8488SAndroid Build Coastguard Worker /// An alias for portability. 93*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_BIGDIG_C UINT32_C 94*5a6e8488SAndroid Build Coastguard Worker 95*5a6e8488SAndroid Build Coastguard Worker /// The max number + 1 that two limbs can hold. This is used for generating 96*5a6e8488SAndroid Build Coastguard Worker /// numbers because the PRNG can generate a number that will fill two limbs. 97*5a6e8488SAndroid Build Coastguard Worker #define BC_BASE_RAND_POW (UINT64_C(100000000)) 98*5a6e8488SAndroid Build Coastguard Worker 99*5a6e8488SAndroid Build Coastguard Worker /// The actual limb type. 100*5a6e8488SAndroid Build Coastguard Worker typedef int_least16_t BcDig; 101*5a6e8488SAndroid Build Coastguard Worker 102*5a6e8488SAndroid Build Coastguard Worker #else 103*5a6e8488SAndroid Build Coastguard Worker 104*5a6e8488SAndroid Build Coastguard Worker /// LONG_BIT must be at least 32 on POSIX. We depend on that. 105*5a6e8488SAndroid Build Coastguard Worker #error BC_LONG_BIT must be at least 32 106*5a6e8488SAndroid Build Coastguard Worker 107*5a6e8488SAndroid Build Coastguard Worker #endif // BC_LONG_BIT >= 64 108*5a6e8488SAndroid Build Coastguard Worker 109*5a6e8488SAndroid Build Coastguard Worker /// The default (and minimum) number of limbs when allocating a number. 110*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_DEF_SIZE (8) 111*5a6e8488SAndroid Build Coastguard Worker 112*5a6e8488SAndroid Build Coastguard Worker /// The actual number struct. This is where the magic happens. 113*5a6e8488SAndroid Build Coastguard Worker typedef struct BcNum 114*5a6e8488SAndroid Build Coastguard Worker { 115*5a6e8488SAndroid Build Coastguard Worker /// The limb array. It is restrict because *no* other item should own the 116*5a6e8488SAndroid Build Coastguard Worker /// array. For more information, see the development manual 117*5a6e8488SAndroid Build Coastguard Worker /// (manuals/development.md#numbers). 118*5a6e8488SAndroid Build Coastguard Worker BcDig* restrict num; 119*5a6e8488SAndroid Build Coastguard Worker 120*5a6e8488SAndroid Build Coastguard Worker /// The number of limbs before the decimal (radix) point. This also stores 121*5a6e8488SAndroid Build Coastguard Worker /// the negative bit in the least significant bit since it uses at least two 122*5a6e8488SAndroid Build Coastguard Worker /// bits less than scale. It is also used less than scale. See the 123*5a6e8488SAndroid Build Coastguard Worker /// development manual (manuals/development.md#numbers) for more info. 124*5a6e8488SAndroid Build Coastguard Worker size_t rdx; 125*5a6e8488SAndroid Build Coastguard Worker 126*5a6e8488SAndroid Build Coastguard Worker /// The actual scale of the number. This is different from rdx because there 127*5a6e8488SAndroid Build Coastguard Worker /// are multiple digits in one limb, and in the last limb, only some of the 128*5a6e8488SAndroid Build Coastguard Worker /// digits may be part of the scale. However, scale must always match rdx 129*5a6e8488SAndroid Build Coastguard Worker /// (except when the number is 0), or there is a bug. For more information, 130*5a6e8488SAndroid Build Coastguard Worker /// see the development manual (manuals/development.md#numbers). 131*5a6e8488SAndroid Build Coastguard Worker size_t scale; 132*5a6e8488SAndroid Build Coastguard Worker 133*5a6e8488SAndroid Build Coastguard Worker /// The number of valid limbs in the array. If this is 0, then the number is 134*5a6e8488SAndroid Build Coastguard Worker /// 0 as well. 135*5a6e8488SAndroid Build Coastguard Worker size_t len; 136*5a6e8488SAndroid Build Coastguard Worker 137*5a6e8488SAndroid Build Coastguard Worker /// The capacity of the limbs array. This is how many limbs the number could 138*5a6e8488SAndroid Build Coastguard Worker /// expand to without reallocation. 139*5a6e8488SAndroid Build Coastguard Worker size_t cap; 140*5a6e8488SAndroid Build Coastguard Worker 141*5a6e8488SAndroid Build Coastguard Worker } BcNum; 142*5a6e8488SAndroid Build Coastguard Worker 143*5a6e8488SAndroid Build Coastguard Worker #if BC_ENABLE_EXTRA_MATH 144*5a6e8488SAndroid Build Coastguard Worker 145*5a6e8488SAndroid Build Coastguard Worker // Forward declaration 146*5a6e8488SAndroid Build Coastguard Worker struct BcRNG; 147*5a6e8488SAndroid Build Coastguard Worker 148*5a6e8488SAndroid Build Coastguard Worker #endif // BC_ENABLE_EXTRA_MATH 149*5a6e8488SAndroid Build Coastguard Worker 150*5a6e8488SAndroid Build Coastguard Worker /// The minimum obase. 151*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_MIN_BASE (BC_NUM_BIGDIG_C(2)) 152*5a6e8488SAndroid Build Coastguard Worker 153*5a6e8488SAndroid Build Coastguard Worker /// The maximum ibase allowed by POSIX. 154*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_MAX_POSIX_IBASE (BC_NUM_BIGDIG_C(16)) 155*5a6e8488SAndroid Build Coastguard Worker 156*5a6e8488SAndroid Build Coastguard Worker /// The actual ibase supported by this implementation. 157*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_MAX_IBASE (BC_NUM_BIGDIG_C(36)) 158*5a6e8488SAndroid Build Coastguard Worker 159*5a6e8488SAndroid Build Coastguard Worker /// The max base allowed by bc_num_parseChar(). 160*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_MAX_LBASE (BC_NUM_BIGDIG_C('Z' + BC_BASE + 1)) 161*5a6e8488SAndroid Build Coastguard Worker 162*5a6e8488SAndroid Build Coastguard Worker /// The default number of characters to print before a backslash newline. 163*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_PRINT_WIDTH (BC_NUM_BIGDIG_C(69)) 164*5a6e8488SAndroid Build Coastguard Worker 165*5a6e8488SAndroid Build Coastguard Worker /// The base for printing streams from numbers. 166*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_STREAM_BASE (256) 167*5a6e8488SAndroid Build Coastguard Worker 168*5a6e8488SAndroid Build Coastguard Worker // This sets a default for the Karatsuba length. 169*5a6e8488SAndroid Build Coastguard Worker #ifndef BC_NUM_KARATSUBA_LEN 170*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_KARATSUBA_LEN (BC_NUM_BIGDIG_C(32)) 171*5a6e8488SAndroid Build Coastguard Worker #elif BC_NUM_KARATSUBA_LEN < 16 172*5a6e8488SAndroid Build Coastguard Worker #error BC_NUM_KARATSUBA_LEN must be at least 16. 173*5a6e8488SAndroid Build Coastguard Worker #endif // BC_NUM_KARATSUBA_LEN 174*5a6e8488SAndroid Build Coastguard Worker 175*5a6e8488SAndroid Build Coastguard Worker // A crude, but always big enough, calculation of 176*5a6e8488SAndroid Build Coastguard Worker // the size required for ibase and obase BcNum's. 177*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_BIGDIG_LOG10 (BC_NUM_DEF_SIZE) 178*5a6e8488SAndroid Build Coastguard Worker 179*5a6e8488SAndroid Build Coastguard Worker /** 180*5a6e8488SAndroid Build Coastguard Worker * Returns non-zero if the BcNum @a n is non-zero. 181*5a6e8488SAndroid Build Coastguard Worker * @param n The number to test. 182*5a6e8488SAndroid Build Coastguard Worker * @return Non-zero if @a n is non-zero, zero otherwise. 183*5a6e8488SAndroid Build Coastguard Worker */ 184*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NONZERO(n) ((n)->len) 185*5a6e8488SAndroid Build Coastguard Worker 186*5a6e8488SAndroid Build Coastguard Worker /** 187*5a6e8488SAndroid Build Coastguard Worker * Returns true if the BcNum @a n is zero. 188*5a6e8488SAndroid Build Coastguard Worker * @param n The number to test. 189*5a6e8488SAndroid Build Coastguard Worker * @return True if @a n is zero, false otherwise. 190*5a6e8488SAndroid Build Coastguard Worker */ 191*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_ZERO(n) (!BC_NUM_NONZERO(n)) 192*5a6e8488SAndroid Build Coastguard Worker 193*5a6e8488SAndroid Build Coastguard Worker /** 194*5a6e8488SAndroid Build Coastguard Worker * Returns true if the BcNum @a n is one with no scale. 195*5a6e8488SAndroid Build Coastguard Worker * @param n The number to test. 196*5a6e8488SAndroid Build Coastguard Worker * @return True if @a n equals 1 with no scale, false otherwise. 197*5a6e8488SAndroid Build Coastguard Worker */ 198*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1) 199*5a6e8488SAndroid Build Coastguard Worker 200*5a6e8488SAndroid Build Coastguard Worker /** 201*5a6e8488SAndroid Build Coastguard Worker * Converts the letter @a c into a number. 202*5a6e8488SAndroid Build Coastguard Worker * @param c The letter to convert. 203*5a6e8488SAndroid Build Coastguard Worker * @return The number corresponding to the letter. 204*5a6e8488SAndroid Build Coastguard Worker */ 205*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NUM_LETTER(c) ((c) - 'A' + BC_BASE) 206*5a6e8488SAndroid Build Coastguard Worker 207*5a6e8488SAndroid Build Coastguard Worker /// The number of allocations done by bc_num_k(). If you change the number of 208*5a6e8488SAndroid Build Coastguard Worker /// allocations, you must change this. This is done in order to allocate them 209*5a6e8488SAndroid Build Coastguard Worker /// all as one allocation and just give them all pointers to different parts. 210*5a6e8488SAndroid Build Coastguard Worker /// Works pretty well, but you have to be careful. 211*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_KARATSUBA_ALLOCS (6) 212*5a6e8488SAndroid Build Coastguard Worker 213*5a6e8488SAndroid Build Coastguard Worker /** 214*5a6e8488SAndroid Build Coastguard Worker * Rounds @a s (scale) up to the next power of BC_BASE_DIGS. This will also 215*5a6e8488SAndroid Build Coastguard Worker * check for overflow and gives a fatal error if that happens because we just 216*5a6e8488SAndroid Build Coastguard Worker * can't go over the limits we have imposed. 217*5a6e8488SAndroid Build Coastguard Worker * @param s The scale to round up. 218*5a6e8488SAndroid Build Coastguard Worker * @return @a s rounded up to the next power of BC_BASE_DIGS. 219*5a6e8488SAndroid Build Coastguard Worker */ 220*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_ROUND_POW(s) (bc_vm_growSize((s), BC_BASE_DIGS - 1)) 221*5a6e8488SAndroid Build Coastguard Worker 222*5a6e8488SAndroid Build Coastguard Worker /** 223*5a6e8488SAndroid Build Coastguard Worker * Returns the equivalent rdx for the scale @a s. 224*5a6e8488SAndroid Build Coastguard Worker * @param s The scale to convert. 225*5a6e8488SAndroid Build Coastguard Worker * @return The rdx for @a s. 226*5a6e8488SAndroid Build Coastguard Worker */ 227*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_RDX(s) (BC_NUM_ROUND_POW(s) / BC_BASE_DIGS) 228*5a6e8488SAndroid Build Coastguard Worker 229*5a6e8488SAndroid Build Coastguard Worker /** 230*5a6e8488SAndroid Build Coastguard Worker * Returns the actual rdx of @a n. (It removes the negative bit.) 231*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 232*5a6e8488SAndroid Build Coastguard Worker * @return The real rdx of @a n. 233*5a6e8488SAndroid Build Coastguard Worker */ 234*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_RDX_VAL(n) ((n)->rdx >> 1) 235*5a6e8488SAndroid Build Coastguard Worker 236*5a6e8488SAndroid Build Coastguard Worker /** 237*5a6e8488SAndroid Build Coastguard Worker * Returns the actual rdx of @a n, where @a n is not a pointer. (It removes the 238*5a6e8488SAndroid Build Coastguard Worker * negative bit.) 239*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 240*5a6e8488SAndroid Build Coastguard Worker * @return The real rdx of @a n. 241*5a6e8488SAndroid Build Coastguard Worker */ 242*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_RDX_VAL_NP(n) ((n).rdx >> 1) 243*5a6e8488SAndroid Build Coastguard Worker 244*5a6e8488SAndroid Build Coastguard Worker /** 245*5a6e8488SAndroid Build Coastguard Worker * Sets the rdx of @a n to @a v. 246*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 247*5a6e8488SAndroid Build Coastguard Worker * @param v The value to set the rdx to. 248*5a6e8488SAndroid Build Coastguard Worker */ 249*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_RDX_SET(n, v) \ 250*5a6e8488SAndroid Build Coastguard Worker ((n)->rdx = (((v) << 1) | ((n)->rdx & (BcBigDig) 1))) 251*5a6e8488SAndroid Build Coastguard Worker 252*5a6e8488SAndroid Build Coastguard Worker /** 253*5a6e8488SAndroid Build Coastguard Worker * Sets the rdx of @a n to @a v, where @a n is not a pointer. 254*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 255*5a6e8488SAndroid Build Coastguard Worker * @param v The value to set the rdx to. 256*5a6e8488SAndroid Build Coastguard Worker */ 257*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_RDX_SET_NP(n, v) \ 258*5a6e8488SAndroid Build Coastguard Worker ((n).rdx = (((v) << 1) | ((n).rdx & (BcBigDig) 1))) 259*5a6e8488SAndroid Build Coastguard Worker 260*5a6e8488SAndroid Build Coastguard Worker /** 261*5a6e8488SAndroid Build Coastguard Worker * Sets the rdx of @a n to @a v and the negative bit to @a neg. 262*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 263*5a6e8488SAndroid Build Coastguard Worker * @param v The value to set the rdx to. 264*5a6e8488SAndroid Build Coastguard Worker * @param neg The value to set the negative bit to. 265*5a6e8488SAndroid Build Coastguard Worker */ 266*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_RDX_SET_NEG(n, v, neg) ((n)->rdx = (((v) << 1) | (neg))) 267*5a6e8488SAndroid Build Coastguard Worker 268*5a6e8488SAndroid Build Coastguard Worker /** 269*5a6e8488SAndroid Build Coastguard Worker * Returns true if the rdx and scale for @a n match. 270*5a6e8488SAndroid Build Coastguard Worker * @param n The number to test. 271*5a6e8488SAndroid Build Coastguard Worker * @return True if the rdx and scale of @a n match, false otherwise. 272*5a6e8488SAndroid Build Coastguard Worker */ 273*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_RDX_VALID(n) \ 274*5a6e8488SAndroid Build Coastguard Worker (BC_NUM_ZERO(n) || BC_NUM_RDX_VAL(n) * BC_BASE_DIGS >= (n)->scale) 275*5a6e8488SAndroid Build Coastguard Worker 276*5a6e8488SAndroid Build Coastguard Worker /** 277*5a6e8488SAndroid Build Coastguard Worker * Returns true if the rdx and scale for @a n match, where @a n is not a 278*5a6e8488SAndroid Build Coastguard Worker * pointer. 279*5a6e8488SAndroid Build Coastguard Worker * @param n The number to test. 280*5a6e8488SAndroid Build Coastguard Worker * @return True if the rdx and scale of @a n match, false otherwise. 281*5a6e8488SAndroid Build Coastguard Worker */ 282*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_RDX_VALID_NP(n) \ 283*5a6e8488SAndroid Build Coastguard Worker ((!(n).len) || BC_NUM_RDX_VAL_NP(n) * BC_BASE_DIGS >= (n).scale) 284*5a6e8488SAndroid Build Coastguard Worker 285*5a6e8488SAndroid Build Coastguard Worker /** 286*5a6e8488SAndroid Build Coastguard Worker * Returns true if @a n is negative, false otherwise. 287*5a6e8488SAndroid Build Coastguard Worker * @param n The number to test. 288*5a6e8488SAndroid Build Coastguard Worker * @return True if @a n is negative, false otherwise. 289*5a6e8488SAndroid Build Coastguard Worker */ 290*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NEG(n) ((n)->rdx & ((BcBigDig) 1)) 291*5a6e8488SAndroid Build Coastguard Worker 292*5a6e8488SAndroid Build Coastguard Worker /** 293*5a6e8488SAndroid Build Coastguard Worker * Returns true if @a n is negative, false otherwise, where @a n is not a 294*5a6e8488SAndroid Build Coastguard Worker * pointer. 295*5a6e8488SAndroid Build Coastguard Worker * @param n The number to test. 296*5a6e8488SAndroid Build Coastguard Worker * @return True if @a n is negative, false otherwise. 297*5a6e8488SAndroid Build Coastguard Worker */ 298*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NEG_NP(n) ((n).rdx & ((BcBigDig) 1)) 299*5a6e8488SAndroid Build Coastguard Worker 300*5a6e8488SAndroid Build Coastguard Worker /** 301*5a6e8488SAndroid Build Coastguard Worker * Clears the negative bit on @a n. 302*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 303*5a6e8488SAndroid Build Coastguard Worker */ 304*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NEG_CLR(n) ((n)->rdx &= ~((BcBigDig) 1)) 305*5a6e8488SAndroid Build Coastguard Worker 306*5a6e8488SAndroid Build Coastguard Worker /** 307*5a6e8488SAndroid Build Coastguard Worker * Clears the negative bit on @a n, where @a n is not a pointer. 308*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 309*5a6e8488SAndroid Build Coastguard Worker */ 310*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NEG_CLR_NP(n) ((n).rdx &= ~((BcBigDig) 1)) 311*5a6e8488SAndroid Build Coastguard Worker 312*5a6e8488SAndroid Build Coastguard Worker /** 313*5a6e8488SAndroid Build Coastguard Worker * Sets the negative bit on @a n. 314*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 315*5a6e8488SAndroid Build Coastguard Worker */ 316*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NEG_SET(n) ((n)->rdx |= ((BcBigDig) 1)) 317*5a6e8488SAndroid Build Coastguard Worker 318*5a6e8488SAndroid Build Coastguard Worker /** 319*5a6e8488SAndroid Build Coastguard Worker * Toggles the negative bit on @a n. 320*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 321*5a6e8488SAndroid Build Coastguard Worker */ 322*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NEG_TGL(n) ((n)->rdx ^= ((BcBigDig) 1)) 323*5a6e8488SAndroid Build Coastguard Worker 324*5a6e8488SAndroid Build Coastguard Worker /** 325*5a6e8488SAndroid Build Coastguard Worker * Toggles the negative bit on @a n, where @a n is not a pointer. 326*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 327*5a6e8488SAndroid Build Coastguard Worker */ 328*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NEG_TGL_NP(n) ((n).rdx ^= ((BcBigDig) 1)) 329*5a6e8488SAndroid Build Coastguard Worker 330*5a6e8488SAndroid Build Coastguard Worker /** 331*5a6e8488SAndroid Build Coastguard Worker * Returns the rdx val for @a n if the negative bit is set to @a v. 332*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 333*5a6e8488SAndroid Build Coastguard Worker * @param v The value for the negative bit. 334*5a6e8488SAndroid Build Coastguard Worker * @return The value of the rdx of @a n if the negative bit were set to @a v. 335*5a6e8488SAndroid Build Coastguard Worker */ 336*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NEG_VAL(n, v) (((n)->rdx & ~((BcBigDig) 1)) | (v)) 337*5a6e8488SAndroid Build Coastguard Worker 338*5a6e8488SAndroid Build Coastguard Worker /** 339*5a6e8488SAndroid Build Coastguard Worker * Returns the rdx val for @a n if the negative bit is set to @a v, where @a n 340*5a6e8488SAndroid Build Coastguard Worker * is not a pointer. 341*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 342*5a6e8488SAndroid Build Coastguard Worker * @param v The value for the negative bit. 343*5a6e8488SAndroid Build Coastguard Worker * @return The value of the rdx of @a n if the negative bit were set to @a v. 344*5a6e8488SAndroid Build Coastguard Worker */ 345*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_NEG_VAL_NP(n, v) (((n).rdx & ~((BcBigDig) 1)) | (v)) 346*5a6e8488SAndroid Build Coastguard Worker 347*5a6e8488SAndroid Build Coastguard Worker /** 348*5a6e8488SAndroid Build Coastguard Worker * Returns the size, in bytes, of limb array with @a n limbs. 349*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 350*5a6e8488SAndroid Build Coastguard Worker * @return The size, in bytes, of a limb array with @a n limbs. 351*5a6e8488SAndroid Build Coastguard Worker */ 352*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_SIZE(n) ((n) * sizeof(BcDig)) 353*5a6e8488SAndroid Build Coastguard Worker 354*5a6e8488SAndroid Build Coastguard Worker // These are for debugging only. 355*5a6e8488SAndroid Build Coastguard Worker #if BC_DEBUG_CODE 356*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_PRINT(x) fprintf(stderr, "%s = %lu\n", #x, (unsigned long) (x)) 357*5a6e8488SAndroid Build Coastguard Worker #define DUMP_NUM bc_num_dump 358*5a6e8488SAndroid Build Coastguard Worker #else // BC_DEBUG_CODE 359*5a6e8488SAndroid Build Coastguard Worker #undef DUMP_NUM 360*5a6e8488SAndroid Build Coastguard Worker #define DUMP_NUM(x, y) 361*5a6e8488SAndroid Build Coastguard Worker #define BC_NUM_PRINT(x) 362*5a6e8488SAndroid Build Coastguard Worker #endif // BC_DEBUG_CODE 363*5a6e8488SAndroid Build Coastguard Worker 364*5a6e8488SAndroid Build Coastguard Worker /** 365*5a6e8488SAndroid Build Coastguard Worker * A function type for binary operators. 366*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 367*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 368*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 369*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 370*5a6e8488SAndroid Build Coastguard Worker */ 371*5a6e8488SAndroid Build Coastguard Worker typedef void (*BcNumBinaryOp)(BcNum* a, BcNum* b, BcNum* c, size_t scale); 372*5a6e8488SAndroid Build Coastguard Worker 373*5a6e8488SAndroid Build Coastguard Worker /** 374*5a6e8488SAndroid Build Coastguard Worker * A function type for binary operators *after* @a c has been properly 375*5a6e8488SAndroid Build Coastguard Worker * allocated. At this point, *nothing* should be pointing to @a c (in any way 376*5a6e8488SAndroid Build Coastguard Worker * that matters, anyway). 377*5a6e8488SAndroid Build Coastguard Worker * @param a The first operand. 378*5a6e8488SAndroid Build Coastguard Worker * @param b The second operand. 379*5a6e8488SAndroid Build Coastguard Worker * @param c The return parameter. 380*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 381*5a6e8488SAndroid Build Coastguard Worker */ 382*5a6e8488SAndroid Build Coastguard Worker typedef void (*BcNumBinOp)(BcNum* a, BcNum* b, BcNum* restrict c, size_t scale); 383*5a6e8488SAndroid Build Coastguard Worker 384*5a6e8488SAndroid Build Coastguard Worker /** 385*5a6e8488SAndroid Build Coastguard Worker * A function type for getting the allocation size needed for a binary operator. 386*5a6e8488SAndroid Build Coastguard Worker * Any function used for this *must* return enough space for *all* possible 387*5a6e8488SAndroid Build Coastguard Worker * invocations of the operator. 388*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 389*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 390*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 391*5a6e8488SAndroid Build Coastguard Worker * @return The size of allocation needed for the result of the operator 392*5a6e8488SAndroid Build Coastguard Worker * with @a a, @a b, and @a scale. 393*5a6e8488SAndroid Build Coastguard Worker */ 394*5a6e8488SAndroid Build Coastguard Worker typedef size_t (*BcNumBinaryOpReq)(const BcNum* a, const BcNum* b, 395*5a6e8488SAndroid Build Coastguard Worker size_t scale); 396*5a6e8488SAndroid Build Coastguard Worker 397*5a6e8488SAndroid Build Coastguard Worker /** 398*5a6e8488SAndroid Build Coastguard Worker * A function type for printing a "digit." Functions of this type will print one 399*5a6e8488SAndroid Build Coastguard Worker * digit in a number. Digits are printed differently based on the base, which is 400*5a6e8488SAndroid Build Coastguard Worker * why there is more than one implementation of this function type. 401*5a6e8488SAndroid Build Coastguard Worker * @param n The "digit" to print. 402*5a6e8488SAndroid Build Coastguard Worker * @param len The "length" of the digit, or number of characters that will 403*5a6e8488SAndroid Build Coastguard Worker * need to be printed for the digit. 404*5a6e8488SAndroid Build Coastguard Worker * @param rdx True if a decimal (radix) point should be printed. 405*5a6e8488SAndroid Build Coastguard Worker * @param bslash True if a backslash+newline should be printed if the character 406*5a6e8488SAndroid Build Coastguard Worker * limit for the line is reached, false otherwise. 407*5a6e8488SAndroid Build Coastguard Worker */ 408*5a6e8488SAndroid Build Coastguard Worker typedef void (*BcNumDigitOp)(size_t n, size_t len, bool rdx, bool bslash); 409*5a6e8488SAndroid Build Coastguard Worker 410*5a6e8488SAndroid Build Coastguard Worker /** 411*5a6e8488SAndroid Build Coastguard Worker * A function type to run an operator on @a a and @a b and store the result in 412*5a6e8488SAndroid Build Coastguard Worker * @a a. This is used in karatsuba for faster adds and subtracts at the end. 413*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter and return value. 414*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 415*5a6e8488SAndroid Build Coastguard Worker * @param len The minimum length of both arrays. 416*5a6e8488SAndroid Build Coastguard Worker */ 417*5a6e8488SAndroid Build Coastguard Worker typedef void (*BcNumShiftAddOp)(BcDig* restrict a, const BcDig* restrict b, 418*5a6e8488SAndroid Build Coastguard Worker size_t len); 419*5a6e8488SAndroid Build Coastguard Worker 420*5a6e8488SAndroid Build Coastguard Worker /** 421*5a6e8488SAndroid Build Coastguard Worker * Initializes @a n with @a req limbs in its array. 422*5a6e8488SAndroid Build Coastguard Worker * @param n The number to initialize. 423*5a6e8488SAndroid Build Coastguard Worker * @param req The number of limbs @a n must have in its limb array. 424*5a6e8488SAndroid Build Coastguard Worker */ 425*5a6e8488SAndroid Build Coastguard Worker void 426*5a6e8488SAndroid Build Coastguard Worker bc_num_init(BcNum* restrict n, size_t req); 427*5a6e8488SAndroid Build Coastguard Worker 428*5a6e8488SAndroid Build Coastguard Worker /** 429*5a6e8488SAndroid Build Coastguard Worker * Initializes (sets up) @a n with the preallocated limb array @a num that has 430*5a6e8488SAndroid Build Coastguard Worker * size @a cap. This is called by @a bc_num_init(), but it is also used by parts 431*5a6e8488SAndroid Build Coastguard Worker * of bc that use statically allocated limb arrays. 432*5a6e8488SAndroid Build Coastguard Worker * @param n The number to initialize. 433*5a6e8488SAndroid Build Coastguard Worker * @param num The preallocated limb array. 434*5a6e8488SAndroid Build Coastguard Worker * @param cap The capacity of @a num. 435*5a6e8488SAndroid Build Coastguard Worker */ 436*5a6e8488SAndroid Build Coastguard Worker void 437*5a6e8488SAndroid Build Coastguard Worker bc_num_setup(BcNum* restrict n, BcDig* restrict num, size_t cap); 438*5a6e8488SAndroid Build Coastguard Worker 439*5a6e8488SAndroid Build Coastguard Worker /** 440*5a6e8488SAndroid Build Coastguard Worker * Copies @a s into @a d. This does a deep copy and requires that @a d is 441*5a6e8488SAndroid Build Coastguard Worker * already a valid and allocated BcNum. 442*5a6e8488SAndroid Build Coastguard Worker * @param d The destination BcNum. 443*5a6e8488SAndroid Build Coastguard Worker * @param s The source BcNum. 444*5a6e8488SAndroid Build Coastguard Worker */ 445*5a6e8488SAndroid Build Coastguard Worker void 446*5a6e8488SAndroid Build Coastguard Worker bc_num_copy(BcNum* d, const BcNum* s); 447*5a6e8488SAndroid Build Coastguard Worker 448*5a6e8488SAndroid Build Coastguard Worker /** 449*5a6e8488SAndroid Build Coastguard Worker * Creates @a d and copies @a s into @a d. This does a deep copy and requires 450*5a6e8488SAndroid Build Coastguard Worker * that @a d is *not* a valid or allocated BcNum. 451*5a6e8488SAndroid Build Coastguard Worker * @param d The destination BcNum. 452*5a6e8488SAndroid Build Coastguard Worker * @param s The source BcNum. 453*5a6e8488SAndroid Build Coastguard Worker */ 454*5a6e8488SAndroid Build Coastguard Worker void 455*5a6e8488SAndroid Build Coastguard Worker bc_num_createCopy(BcNum* d, const BcNum* s); 456*5a6e8488SAndroid Build Coastguard Worker 457*5a6e8488SAndroid Build Coastguard Worker /** 458*5a6e8488SAndroid Build Coastguard Worker * Creates (initializes) @a n and sets its value to the equivalent of @a val. 459*5a6e8488SAndroid Build Coastguard Worker * @a n must *not* be a valid or preallocated BcNum. 460*5a6e8488SAndroid Build Coastguard Worker * @param n The number to initialize and set. 461*5a6e8488SAndroid Build Coastguard Worker * @param val The value to set @a n's value to. 462*5a6e8488SAndroid Build Coastguard Worker */ 463*5a6e8488SAndroid Build Coastguard Worker void 464*5a6e8488SAndroid Build Coastguard Worker bc_num_createFromBigdig(BcNum* restrict n, BcBigDig val); 465*5a6e8488SAndroid Build Coastguard Worker 466*5a6e8488SAndroid Build Coastguard Worker /** 467*5a6e8488SAndroid Build Coastguard Worker * Makes @a n valid for holding strings. @a n must *not* be allocated; this 468*5a6e8488SAndroid Build Coastguard Worker * simply clears some fields, including setting the num field to NULL. 469*5a6e8488SAndroid Build Coastguard Worker * @param n The number to clear. 470*5a6e8488SAndroid Build Coastguard Worker */ 471*5a6e8488SAndroid Build Coastguard Worker void 472*5a6e8488SAndroid Build Coastguard Worker bc_num_clear(BcNum* restrict n); 473*5a6e8488SAndroid Build Coastguard Worker 474*5a6e8488SAndroid Build Coastguard Worker /** 475*5a6e8488SAndroid Build Coastguard Worker * Frees @a num, which is a BcNum as a void pointer. This is a destructor. 476*5a6e8488SAndroid Build Coastguard Worker * @param num The BcNum to free as a void pointer. 477*5a6e8488SAndroid Build Coastguard Worker */ 478*5a6e8488SAndroid Build Coastguard Worker void 479*5a6e8488SAndroid Build Coastguard Worker bc_num_free(void* num); 480*5a6e8488SAndroid Build Coastguard Worker 481*5a6e8488SAndroid Build Coastguard Worker /** 482*5a6e8488SAndroid Build Coastguard Worker * Returns the scale of @a n. 483*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 484*5a6e8488SAndroid Build Coastguard Worker * @return The scale of @a n. 485*5a6e8488SAndroid Build Coastguard Worker */ 486*5a6e8488SAndroid Build Coastguard Worker size_t 487*5a6e8488SAndroid Build Coastguard Worker bc_num_scale(const BcNum* restrict n); 488*5a6e8488SAndroid Build Coastguard Worker 489*5a6e8488SAndroid Build Coastguard Worker /** 490*5a6e8488SAndroid Build Coastguard Worker * Returns the length (in decimal digits) of @a n. This is complicated. First, 491*5a6e8488SAndroid Build Coastguard Worker * if the number is zero, we always return at least one, but we also return the 492*5a6e8488SAndroid Build Coastguard Worker * scale if it exists. Then, If it is not zero, it opens a whole other can of 493*5a6e8488SAndroid Build Coastguard Worker * worms. Read the comments in the definition. 494*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 495*5a6e8488SAndroid Build Coastguard Worker * @return The length of @a n. 496*5a6e8488SAndroid Build Coastguard Worker */ 497*5a6e8488SAndroid Build Coastguard Worker size_t 498*5a6e8488SAndroid Build Coastguard Worker bc_num_len(const BcNum* restrict n); 499*5a6e8488SAndroid Build Coastguard Worker 500*5a6e8488SAndroid Build Coastguard Worker /** 501*5a6e8488SAndroid Build Coastguard Worker * Convert a number to a BcBigDig (hardware integer). This version does error 502*5a6e8488SAndroid Build Coastguard Worker * checking, and if it finds an error, throws it. Otherwise, it calls 503*5a6e8488SAndroid Build Coastguard Worker * bc_num_bigdig2(). 504*5a6e8488SAndroid Build Coastguard Worker * @param n The number to convert. 505*5a6e8488SAndroid Build Coastguard Worker * @return The number as a hardware integer. 506*5a6e8488SAndroid Build Coastguard Worker */ 507*5a6e8488SAndroid Build Coastguard Worker BcBigDig 508*5a6e8488SAndroid Build Coastguard Worker bc_num_bigdig(const BcNum* restrict n); 509*5a6e8488SAndroid Build Coastguard Worker 510*5a6e8488SAndroid Build Coastguard Worker /** 511*5a6e8488SAndroid Build Coastguard Worker * Convert a number to a BcBigDig (hardware integer). This version does no error 512*5a6e8488SAndroid Build Coastguard Worker * checking. 513*5a6e8488SAndroid Build Coastguard Worker * @param n The number to convert. 514*5a6e8488SAndroid Build Coastguard Worker * @return The number as a hardware integer. 515*5a6e8488SAndroid Build Coastguard Worker */ 516*5a6e8488SAndroid Build Coastguard Worker BcBigDig 517*5a6e8488SAndroid Build Coastguard Worker bc_num_bigdig2(const BcNum* restrict n); 518*5a6e8488SAndroid Build Coastguard Worker 519*5a6e8488SAndroid Build Coastguard Worker /** 520*5a6e8488SAndroid Build Coastguard Worker * Sets @a n to the value of @a val. @a n is expected to be a valid and 521*5a6e8488SAndroid Build Coastguard Worker * allocated BcNum. 522*5a6e8488SAndroid Build Coastguard Worker * @param n The number to set. 523*5a6e8488SAndroid Build Coastguard Worker * @param val The value to set the number to. 524*5a6e8488SAndroid Build Coastguard Worker */ 525*5a6e8488SAndroid Build Coastguard Worker void 526*5a6e8488SAndroid Build Coastguard Worker bc_num_bigdig2num(BcNum* restrict n, BcBigDig val); 527*5a6e8488SAndroid Build Coastguard Worker 528*5a6e8488SAndroid Build Coastguard Worker #if BC_ENABLE_EXTRA_MATH 529*5a6e8488SAndroid Build Coastguard Worker 530*5a6e8488SAndroid Build Coastguard Worker /** 531*5a6e8488SAndroid Build Coastguard Worker * Generates a random arbitrary-size integer less than or equal to @a a and 532*5a6e8488SAndroid Build Coastguard Worker * returns it in @a b. This implements irand(). 533*5a6e8488SAndroid Build Coastguard Worker * @param a The limit for the integer to generate. 534*5a6e8488SAndroid Build Coastguard Worker * @param b The return value. 535*5a6e8488SAndroid Build Coastguard Worker * @param rng The pseudo-random number generator. 536*5a6e8488SAndroid Build Coastguard Worker */ 537*5a6e8488SAndroid Build Coastguard Worker void 538*5a6e8488SAndroid Build Coastguard Worker bc_num_irand(BcNum* restrict a, BcNum* restrict b, struct BcRNG* restrict rng); 539*5a6e8488SAndroid Build Coastguard Worker 540*5a6e8488SAndroid Build Coastguard Worker /** 541*5a6e8488SAndroid Build Coastguard Worker * Sets the seed for the PRNG @a rng from @a n. 542*5a6e8488SAndroid Build Coastguard Worker * @param n The new seed for the PRNG. 543*5a6e8488SAndroid Build Coastguard Worker * @param rng The PRNG to set the seed for. 544*5a6e8488SAndroid Build Coastguard Worker */ 545*5a6e8488SAndroid Build Coastguard Worker void 546*5a6e8488SAndroid Build Coastguard Worker bc_num_rng(const BcNum* restrict n, struct BcRNG* rng); 547*5a6e8488SAndroid Build Coastguard Worker 548*5a6e8488SAndroid Build Coastguard Worker /** 549*5a6e8488SAndroid Build Coastguard Worker * Sets @a n to the value produced by the PRNG. This implements rand(). 550*5a6e8488SAndroid Build Coastguard Worker * @param n The number to set. 551*5a6e8488SAndroid Build Coastguard Worker * @param rng The pseudo-random number generator. 552*5a6e8488SAndroid Build Coastguard Worker */ 553*5a6e8488SAndroid Build Coastguard Worker void 554*5a6e8488SAndroid Build Coastguard Worker bc_num_createFromRNG(BcNum* restrict n, struct BcRNG* rng); 555*5a6e8488SAndroid Build Coastguard Worker 556*5a6e8488SAndroid Build Coastguard Worker #endif // BC_ENABLE_EXTRA_MATH 557*5a6e8488SAndroid Build Coastguard Worker 558*5a6e8488SAndroid Build Coastguard Worker /** 559*5a6e8488SAndroid Build Coastguard Worker * The add function. This is a BcNumBinaryOp function. 560*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 561*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 562*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 563*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 564*5a6e8488SAndroid Build Coastguard Worker */ 565*5a6e8488SAndroid Build Coastguard Worker void 566*5a6e8488SAndroid Build Coastguard Worker bc_num_add(BcNum* a, BcNum* b, BcNum* c, size_t scale); 567*5a6e8488SAndroid Build Coastguard Worker 568*5a6e8488SAndroid Build Coastguard Worker /** 569*5a6e8488SAndroid Build Coastguard Worker * The subtract function. This is a BcNumBinaryOp function. 570*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 571*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 572*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 573*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 574*5a6e8488SAndroid Build Coastguard Worker */ 575*5a6e8488SAndroid Build Coastguard Worker void 576*5a6e8488SAndroid Build Coastguard Worker bc_num_sub(BcNum* a, BcNum* b, BcNum* c, size_t scale); 577*5a6e8488SAndroid Build Coastguard Worker 578*5a6e8488SAndroid Build Coastguard Worker /** 579*5a6e8488SAndroid Build Coastguard Worker * The multiply function. 580*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. This is a BcNumBinaryOp function. 581*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 582*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 583*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 584*5a6e8488SAndroid Build Coastguard Worker */ 585*5a6e8488SAndroid Build Coastguard Worker void 586*5a6e8488SAndroid Build Coastguard Worker bc_num_mul(BcNum* a, BcNum* b, BcNum* c, size_t scale); 587*5a6e8488SAndroid Build Coastguard Worker 588*5a6e8488SAndroid Build Coastguard Worker /** 589*5a6e8488SAndroid Build Coastguard Worker * The division function. 590*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. This is a BcNumBinaryOp function. 591*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 592*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 593*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 594*5a6e8488SAndroid Build Coastguard Worker */ 595*5a6e8488SAndroid Build Coastguard Worker void 596*5a6e8488SAndroid Build Coastguard Worker bc_num_div(BcNum* a, BcNum* b, BcNum* c, size_t scale); 597*5a6e8488SAndroid Build Coastguard Worker 598*5a6e8488SAndroid Build Coastguard Worker /** 599*5a6e8488SAndroid Build Coastguard Worker * The modulus function. 600*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. This is a BcNumBinaryOp function. 601*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 602*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 603*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 604*5a6e8488SAndroid Build Coastguard Worker */ 605*5a6e8488SAndroid Build Coastguard Worker void 606*5a6e8488SAndroid Build Coastguard Worker bc_num_mod(BcNum* a, BcNum* b, BcNum* c, size_t scale); 607*5a6e8488SAndroid Build Coastguard Worker 608*5a6e8488SAndroid Build Coastguard Worker /** 609*5a6e8488SAndroid Build Coastguard Worker * The power function. 610*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. This is a BcNumBinaryOp function. 611*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 612*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 613*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 614*5a6e8488SAndroid Build Coastguard Worker */ 615*5a6e8488SAndroid Build Coastguard Worker void 616*5a6e8488SAndroid Build Coastguard Worker bc_num_pow(BcNum* a, BcNum* b, BcNum* c, size_t scale); 617*5a6e8488SAndroid Build Coastguard Worker #if BC_ENABLE_EXTRA_MATH 618*5a6e8488SAndroid Build Coastguard Worker 619*5a6e8488SAndroid Build Coastguard Worker /** 620*5a6e8488SAndroid Build Coastguard Worker * The places function (@ operator). This is a BcNumBinaryOp function. 621*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 622*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 623*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 624*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 625*5a6e8488SAndroid Build Coastguard Worker */ 626*5a6e8488SAndroid Build Coastguard Worker void 627*5a6e8488SAndroid Build Coastguard Worker bc_num_places(BcNum* a, BcNum* b, BcNum* c, size_t scale); 628*5a6e8488SAndroid Build Coastguard Worker 629*5a6e8488SAndroid Build Coastguard Worker /** 630*5a6e8488SAndroid Build Coastguard Worker * The left shift function (<< operator). This is a BcNumBinaryOp function. 631*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 632*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 633*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 634*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 635*5a6e8488SAndroid Build Coastguard Worker */ 636*5a6e8488SAndroid Build Coastguard Worker void 637*5a6e8488SAndroid Build Coastguard Worker bc_num_lshift(BcNum* a, BcNum* b, BcNum* c, size_t scale); 638*5a6e8488SAndroid Build Coastguard Worker 639*5a6e8488SAndroid Build Coastguard Worker /** 640*5a6e8488SAndroid Build Coastguard Worker * The right shift function (>> operator). This is a BcNumBinaryOp function. 641*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 642*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 643*5a6e8488SAndroid Build Coastguard Worker * @param c The return value. 644*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 645*5a6e8488SAndroid Build Coastguard Worker */ 646*5a6e8488SAndroid Build Coastguard Worker void 647*5a6e8488SAndroid Build Coastguard Worker bc_num_rshift(BcNum* a, BcNum* b, BcNum* c, size_t scale); 648*5a6e8488SAndroid Build Coastguard Worker 649*5a6e8488SAndroid Build Coastguard Worker #endif // BC_ENABLE_EXTRA_MATH 650*5a6e8488SAndroid Build Coastguard Worker 651*5a6e8488SAndroid Build Coastguard Worker /** 652*5a6e8488SAndroid Build Coastguard Worker * Square root. 653*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 654*5a6e8488SAndroid Build Coastguard Worker * @param b The return value. 655*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 656*5a6e8488SAndroid Build Coastguard Worker */ 657*5a6e8488SAndroid Build Coastguard Worker void 658*5a6e8488SAndroid Build Coastguard Worker bc_num_sqrt(BcNum* restrict a, BcNum* restrict b, size_t scale); 659*5a6e8488SAndroid Build Coastguard Worker 660*5a6e8488SAndroid Build Coastguard Worker /** 661*5a6e8488SAndroid Build Coastguard Worker * Divsion and modulus together. This is a dc extension. 662*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 663*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 664*5a6e8488SAndroid Build Coastguard Worker * @param c The first return value (quotient). 665*5a6e8488SAndroid Build Coastguard Worker * @param d The second return value (modulus). 666*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 667*5a6e8488SAndroid Build Coastguard Worker */ 668*5a6e8488SAndroid Build Coastguard Worker void 669*5a6e8488SAndroid Build Coastguard Worker bc_num_divmod(BcNum* a, BcNum* b, BcNum* c, BcNum* d, size_t scale); 670*5a6e8488SAndroid Build Coastguard Worker 671*5a6e8488SAndroid Build Coastguard Worker /** 672*5a6e8488SAndroid Build Coastguard Worker * A function returning the required allocation size for an addition or a 673*5a6e8488SAndroid Build Coastguard Worker * subtraction. This is a BcNumBinaryOpReq function. 674*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 675*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 676*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 677*5a6e8488SAndroid Build Coastguard Worker * @return The size of allocation needed for the result of add or subtract 678*5a6e8488SAndroid Build Coastguard Worker * with @a a, @a b, and @a scale. 679*5a6e8488SAndroid Build Coastguard Worker */ 680*5a6e8488SAndroid Build Coastguard Worker size_t 681*5a6e8488SAndroid Build Coastguard Worker bc_num_addReq(const BcNum* a, const BcNum* b, size_t scale); 682*5a6e8488SAndroid Build Coastguard Worker 683*5a6e8488SAndroid Build Coastguard Worker /** 684*5a6e8488SAndroid Build Coastguard Worker * A function returning the required allocation size for a multiplication. This 685*5a6e8488SAndroid Build Coastguard Worker * is a BcNumBinaryOpReq function. 686*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 687*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 688*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 689*5a6e8488SAndroid Build Coastguard Worker * @return The size of allocation needed for the result of multiplication 690*5a6e8488SAndroid Build Coastguard Worker * with @a a, @a b, and @a scale. 691*5a6e8488SAndroid Build Coastguard Worker */ 692*5a6e8488SAndroid Build Coastguard Worker size_t 693*5a6e8488SAndroid Build Coastguard Worker bc_num_mulReq(const BcNum* a, const BcNum* b, size_t scale); 694*5a6e8488SAndroid Build Coastguard Worker 695*5a6e8488SAndroid Build Coastguard Worker /** 696*5a6e8488SAndroid Build Coastguard Worker * A function returning the required allocation size for a division or modulus. 697*5a6e8488SAndroid Build Coastguard Worker * This is a BcNumBinaryOpReq function. 698*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 699*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 700*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 701*5a6e8488SAndroid Build Coastguard Worker * @return The size of allocation needed for the result of division or 702*5a6e8488SAndroid Build Coastguard Worker * modulus with @a a, @a b, and @a scale. 703*5a6e8488SAndroid Build Coastguard Worker */ 704*5a6e8488SAndroid Build Coastguard Worker size_t 705*5a6e8488SAndroid Build Coastguard Worker bc_num_divReq(const BcNum* a, const BcNum* b, size_t scale); 706*5a6e8488SAndroid Build Coastguard Worker 707*5a6e8488SAndroid Build Coastguard Worker /** 708*5a6e8488SAndroid Build Coastguard Worker * A function returning the required allocation size for an exponentiation. This 709*5a6e8488SAndroid Build Coastguard Worker * is a BcNumBinaryOpReq function. 710*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 711*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 712*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 713*5a6e8488SAndroid Build Coastguard Worker * @return The size of allocation needed for the result of exponentiation 714*5a6e8488SAndroid Build Coastguard Worker * with @a a, @a b, and @a scale. 715*5a6e8488SAndroid Build Coastguard Worker */ 716*5a6e8488SAndroid Build Coastguard Worker size_t 717*5a6e8488SAndroid Build Coastguard Worker bc_num_powReq(const BcNum* a, const BcNum* b, size_t scale); 718*5a6e8488SAndroid Build Coastguard Worker 719*5a6e8488SAndroid Build Coastguard Worker #if BC_ENABLE_EXTRA_MATH 720*5a6e8488SAndroid Build Coastguard Worker 721*5a6e8488SAndroid Build Coastguard Worker /** 722*5a6e8488SAndroid Build Coastguard Worker * A function returning the required allocation size for a places, left shift, 723*5a6e8488SAndroid Build Coastguard Worker * or right shift. This is a BcNumBinaryOpReq function. 724*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 725*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 726*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 727*5a6e8488SAndroid Build Coastguard Worker * @return The size of allocation needed for the result of places, left 728*5a6e8488SAndroid Build Coastguard Worker * shift, or right shift with @a a, @a b, and @a scale. 729*5a6e8488SAndroid Build Coastguard Worker */ 730*5a6e8488SAndroid Build Coastguard Worker size_t 731*5a6e8488SAndroid Build Coastguard Worker bc_num_placesReq(const BcNum* a, const BcNum* b, size_t scale); 732*5a6e8488SAndroid Build Coastguard Worker 733*5a6e8488SAndroid Build Coastguard Worker #endif // BC_ENABLE_EXTRA_MATH 734*5a6e8488SAndroid Build Coastguard Worker 735*5a6e8488SAndroid Build Coastguard Worker /** 736*5a6e8488SAndroid Build Coastguard Worker * Truncate @a n *by* @a places decimal places. This only extends places *after* 737*5a6e8488SAndroid Build Coastguard Worker * the decimal point. 738*5a6e8488SAndroid Build Coastguard Worker * @param n The number to truncate. 739*5a6e8488SAndroid Build Coastguard Worker * @param places The number of places to truncate @a n by. 740*5a6e8488SAndroid Build Coastguard Worker */ 741*5a6e8488SAndroid Build Coastguard Worker void 742*5a6e8488SAndroid Build Coastguard Worker bc_num_truncate(BcNum* restrict n, size_t places); 743*5a6e8488SAndroid Build Coastguard Worker 744*5a6e8488SAndroid Build Coastguard Worker /** 745*5a6e8488SAndroid Build Coastguard Worker * Extend @a n *by* @a places decimal places. This only extends places *after* 746*5a6e8488SAndroid Build Coastguard Worker * the decimal point. 747*5a6e8488SAndroid Build Coastguard Worker * @param n The number to truncate. 748*5a6e8488SAndroid Build Coastguard Worker * @param places The number of places to extend @a n by. 749*5a6e8488SAndroid Build Coastguard Worker */ 750*5a6e8488SAndroid Build Coastguard Worker void 751*5a6e8488SAndroid Build Coastguard Worker bc_num_extend(BcNum* restrict n, size_t places); 752*5a6e8488SAndroid Build Coastguard Worker 753*5a6e8488SAndroid Build Coastguard Worker /** 754*5a6e8488SAndroid Build Coastguard Worker * Shifts @a n right by @a places decimal places. This is the workhorse of the 755*5a6e8488SAndroid Build Coastguard Worker * right shift operator, and would be static to src/num.c, except that 756*5a6e8488SAndroid Build Coastguard Worker * src/library.c uses it for efficiency when executing its frand. 757*5a6e8488SAndroid Build Coastguard Worker * @param n The number to shift right. 758*5a6e8488SAndroid Build Coastguard Worker * @param places The number of decimal places to shift @a n right by. 759*5a6e8488SAndroid Build Coastguard Worker */ 760*5a6e8488SAndroid Build Coastguard Worker void 761*5a6e8488SAndroid Build Coastguard Worker bc_num_shiftRight(BcNum* restrict n, size_t places); 762*5a6e8488SAndroid Build Coastguard Worker 763*5a6e8488SAndroid Build Coastguard Worker /** 764*5a6e8488SAndroid Build Coastguard Worker * Compare a and b and return the result of their comparison as an ssize_t. 765*5a6e8488SAndroid Build Coastguard Worker * Returns >0 if @a a is greater than @a b, <0 if @a a is less than @a b, and =0 766*5a6e8488SAndroid Build Coastguard Worker * if a == b. 767*5a6e8488SAndroid Build Coastguard Worker * @param a The first number. 768*5a6e8488SAndroid Build Coastguard Worker * @param b The second number. 769*5a6e8488SAndroid Build Coastguard Worker * @return The result of the comparison. 770*5a6e8488SAndroid Build Coastguard Worker */ 771*5a6e8488SAndroid Build Coastguard Worker ssize_t 772*5a6e8488SAndroid Build Coastguard Worker bc_num_cmp(const BcNum* a, const BcNum* b); 773*5a6e8488SAndroid Build Coastguard Worker 774*5a6e8488SAndroid Build Coastguard Worker /** 775*5a6e8488SAndroid Build Coastguard Worker * Modular exponentiation. 776*5a6e8488SAndroid Build Coastguard Worker * @param a The first parameter. 777*5a6e8488SAndroid Build Coastguard Worker * @param b The second parameter. 778*5a6e8488SAndroid Build Coastguard Worker * @param c The third parameter. 779*5a6e8488SAndroid Build Coastguard Worker * @param d The return value. 780*5a6e8488SAndroid Build Coastguard Worker */ 781*5a6e8488SAndroid Build Coastguard Worker void 782*5a6e8488SAndroid Build Coastguard Worker bc_num_modexp(BcNum* a, BcNum* b, BcNum* c, BcNum* restrict d); 783*5a6e8488SAndroid Build Coastguard Worker 784*5a6e8488SAndroid Build Coastguard Worker /** 785*5a6e8488SAndroid Build Coastguard Worker * Sets @a n to zero with a scale of zero. 786*5a6e8488SAndroid Build Coastguard Worker * @param n The number to zero. 787*5a6e8488SAndroid Build Coastguard Worker */ 788*5a6e8488SAndroid Build Coastguard Worker void 789*5a6e8488SAndroid Build Coastguard Worker bc_num_zero(BcNum* restrict n); 790*5a6e8488SAndroid Build Coastguard Worker 791*5a6e8488SAndroid Build Coastguard Worker /** 792*5a6e8488SAndroid Build Coastguard Worker * Sets @a n to one with a scale of zero. 793*5a6e8488SAndroid Build Coastguard Worker * @param n The number to set to one. 794*5a6e8488SAndroid Build Coastguard Worker */ 795*5a6e8488SAndroid Build Coastguard Worker void 796*5a6e8488SAndroid Build Coastguard Worker bc_num_one(BcNum* restrict n); 797*5a6e8488SAndroid Build Coastguard Worker 798*5a6e8488SAndroid Build Coastguard Worker /** 799*5a6e8488SAndroid Build Coastguard Worker * An efficient function to compare @a n to zero. 800*5a6e8488SAndroid Build Coastguard Worker * @param n The number to compare to zero. 801*5a6e8488SAndroid Build Coastguard Worker * @return The result of the comparison. 802*5a6e8488SAndroid Build Coastguard Worker */ 803*5a6e8488SAndroid Build Coastguard Worker ssize_t 804*5a6e8488SAndroid Build Coastguard Worker bc_num_cmpZero(const BcNum* n); 805*5a6e8488SAndroid Build Coastguard Worker 806*5a6e8488SAndroid Build Coastguard Worker /** 807*5a6e8488SAndroid Build Coastguard Worker * Check a number string for validity and return true if it is, false otherwise. 808*5a6e8488SAndroid Build Coastguard Worker * The library needs this to check user-supplied strings, but in bc and dc, this 809*5a6e8488SAndroid Build Coastguard Worker * is only used for debug asserts because the parsers should get the numbers 810*5a6e8488SAndroid Build Coastguard Worker * parsed right, which should ensure they are always valid. 811*5a6e8488SAndroid Build Coastguard Worker * @param val The string to check. 812*5a6e8488SAndroid Build Coastguard Worker * @return True if the string is a valid number, false otherwise. 813*5a6e8488SAndroid Build Coastguard Worker */ 814*5a6e8488SAndroid Build Coastguard Worker bool 815*5a6e8488SAndroid Build Coastguard Worker bc_num_strValid(const char* restrict val); 816*5a6e8488SAndroid Build Coastguard Worker 817*5a6e8488SAndroid Build Coastguard Worker /** 818*5a6e8488SAndroid Build Coastguard Worker * Parses a number string into the number @a n according to @a base. 819*5a6e8488SAndroid Build Coastguard Worker * @param n The number to set to the parsed value. 820*5a6e8488SAndroid Build Coastguard Worker * @param val The number string to parse. 821*5a6e8488SAndroid Build Coastguard Worker * @param base The base to parse the number string by. 822*5a6e8488SAndroid Build Coastguard Worker */ 823*5a6e8488SAndroid Build Coastguard Worker void 824*5a6e8488SAndroid Build Coastguard Worker bc_num_parse(BcNum* restrict n, const char* restrict val, BcBigDig base); 825*5a6e8488SAndroid Build Coastguard Worker 826*5a6e8488SAndroid Build Coastguard Worker /** 827*5a6e8488SAndroid Build Coastguard Worker * Prints the number @a n according to @a base. 828*5a6e8488SAndroid Build Coastguard Worker * @param n The number to print. 829*5a6e8488SAndroid Build Coastguard Worker * @param base The base to print the number by. 830*5a6e8488SAndroid Build Coastguard Worker * @param newline True if a newline should be inserted at the end, false 831*5a6e8488SAndroid Build Coastguard Worker * otherwise. 832*5a6e8488SAndroid Build Coastguard Worker */ 833*5a6e8488SAndroid Build Coastguard Worker void 834*5a6e8488SAndroid Build Coastguard Worker bc_num_print(BcNum* restrict n, BcBigDig base, bool newline); 835*5a6e8488SAndroid Build Coastguard Worker 836*5a6e8488SAndroid Build Coastguard Worker /** 837*5a6e8488SAndroid Build Coastguard Worker * Invert @a into @a b at the current scale. 838*5a6e8488SAndroid Build Coastguard Worker * @param a The number to invert. 839*5a6e8488SAndroid Build Coastguard Worker * @param b The return parameter. This must be preallocated. 840*5a6e8488SAndroid Build Coastguard Worker * @param scale The current scale. 841*5a6e8488SAndroid Build Coastguard Worker */ 842*5a6e8488SAndroid Build Coastguard Worker #define bc_num_inv(a, b, scale) bc_num_div(&vm->one, (a), (b), (scale)) 843*5a6e8488SAndroid Build Coastguard Worker 844*5a6e8488SAndroid Build Coastguard Worker #if !BC_ENABLE_LIBRARY 845*5a6e8488SAndroid Build Coastguard Worker 846*5a6e8488SAndroid Build Coastguard Worker /** 847*5a6e8488SAndroid Build Coastguard Worker * Prints a number as a character stream. 848*5a6e8488SAndroid Build Coastguard Worker * @param n The number to print as a character stream. 849*5a6e8488SAndroid Build Coastguard Worker */ 850*5a6e8488SAndroid Build Coastguard Worker void 851*5a6e8488SAndroid Build Coastguard Worker bc_num_stream(BcNum* restrict n); 852*5a6e8488SAndroid Build Coastguard Worker 853*5a6e8488SAndroid Build Coastguard Worker #endif // !BC_ENABLE_LIBRARY 854*5a6e8488SAndroid Build Coastguard Worker 855*5a6e8488SAndroid Build Coastguard Worker #if BC_DEBUG_CODE 856*5a6e8488SAndroid Build Coastguard Worker 857*5a6e8488SAndroid Build Coastguard Worker /** 858*5a6e8488SAndroid Build Coastguard Worker * Print a number with a label. This is a debug-only function. 859*5a6e8488SAndroid Build Coastguard Worker * @param n The number to print. 860*5a6e8488SAndroid Build Coastguard Worker * @param name The label to print the number with. 861*5a6e8488SAndroid Build Coastguard Worker * @param emptyline True if there should be an empty line after the number. 862*5a6e8488SAndroid Build Coastguard Worker */ 863*5a6e8488SAndroid Build Coastguard Worker void 864*5a6e8488SAndroid Build Coastguard Worker bc_num_printDebug(const BcNum* n, const char* name, bool emptyline); 865*5a6e8488SAndroid Build Coastguard Worker 866*5a6e8488SAndroid Build Coastguard Worker /** 867*5a6e8488SAndroid Build Coastguard Worker * Print the limbs of @a n. This is a debug-only function. 868*5a6e8488SAndroid Build Coastguard Worker * @param n The number to print. 869*5a6e8488SAndroid Build Coastguard Worker * @param len The length of the number. 870*5a6e8488SAndroid Build Coastguard Worker * @param emptyline True if there should be an empty line after the number. 871*5a6e8488SAndroid Build Coastguard Worker */ 872*5a6e8488SAndroid Build Coastguard Worker void 873*5a6e8488SAndroid Build Coastguard Worker bc_num_printDigs(const BcDig* n, size_t len, bool emptyline); 874*5a6e8488SAndroid Build Coastguard Worker 875*5a6e8488SAndroid Build Coastguard Worker /** 876*5a6e8488SAndroid Build Coastguard Worker * Print debug info about @a n along with its limbs. 877*5a6e8488SAndroid Build Coastguard Worker * @param n The number to print. 878*5a6e8488SAndroid Build Coastguard Worker * @param name The label to print the number with. 879*5a6e8488SAndroid Build Coastguard Worker * @param emptyline True if there should be an empty line after the number. 880*5a6e8488SAndroid Build Coastguard Worker */ 881*5a6e8488SAndroid Build Coastguard Worker void 882*5a6e8488SAndroid Build Coastguard Worker bc_num_printWithDigs(const BcNum* n, const char* name, bool emptyline); 883*5a6e8488SAndroid Build Coastguard Worker 884*5a6e8488SAndroid Build Coastguard Worker /** 885*5a6e8488SAndroid Build Coastguard Worker * Dump debug info about a BcNum variable. 886*5a6e8488SAndroid Build Coastguard Worker * @param varname The variable name. 887*5a6e8488SAndroid Build Coastguard Worker * @param n The number. 888*5a6e8488SAndroid Build Coastguard Worker */ 889*5a6e8488SAndroid Build Coastguard Worker void 890*5a6e8488SAndroid Build Coastguard Worker bc_num_dump(const char* varname, const BcNum* n); 891*5a6e8488SAndroid Build Coastguard Worker 892*5a6e8488SAndroid Build Coastguard Worker #endif // BC_DEBUG_CODE 893*5a6e8488SAndroid Build Coastguard Worker 894*5a6e8488SAndroid Build Coastguard Worker /// A reference to an array of hex digits for easy conversion for printing. 895*5a6e8488SAndroid Build Coastguard Worker extern const char bc_num_hex_digits[]; 896*5a6e8488SAndroid Build Coastguard Worker 897*5a6e8488SAndroid Build Coastguard Worker /// An array of powers of 10 for easy conversion from number of digits to 898*5a6e8488SAndroid Build Coastguard Worker /// powers. 899*5a6e8488SAndroid Build Coastguard Worker extern const BcBigDig bc_num_pow10[BC_BASE_DIGS + 1]; 900*5a6e8488SAndroid Build Coastguard Worker 901*5a6e8488SAndroid Build Coastguard Worker /// A reference to a constant array that is the max of a BigDig. 902*5a6e8488SAndroid Build Coastguard Worker extern const BcDig bc_num_bigdigMax[]; 903*5a6e8488SAndroid Build Coastguard Worker 904*5a6e8488SAndroid Build Coastguard Worker /// A reference to a constant size of the above array. 905*5a6e8488SAndroid Build Coastguard Worker extern const size_t bc_num_bigdigMax_size; 906*5a6e8488SAndroid Build Coastguard Worker 907*5a6e8488SAndroid Build Coastguard Worker /// A reference to a constant array that is 2 times the max of a BigDig. 908*5a6e8488SAndroid Build Coastguard Worker extern const BcDig bc_num_bigdigMax2[]; 909*5a6e8488SAndroid Build Coastguard Worker 910*5a6e8488SAndroid Build Coastguard Worker /// A reference to a constant size of the above array. 911*5a6e8488SAndroid Build Coastguard Worker extern const size_t bc_num_bigdigMax2_size; 912*5a6e8488SAndroid Build Coastguard Worker 913*5a6e8488SAndroid Build Coastguard Worker #endif // BC_NUM_H 914