2 * *****************************************************************************
4 * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * Definitions for the num type.
44 #include <sys/types.h>
49 #ifndef BC_ENABLE_EXTRA_MATH
50 #define BC_ENABLE_EXTRA_MATH (1)
51 #endif // BC_ENABLE_EXTRA_MATH
55 typedef unsigned long ulong;
57 // For some reason, LONG_BIT is not defined in some versions of gcc.
58 // I define it here to the minimum accepted value in the POSIX standard.
64 #define BC_LONG_BIT LONG_BIT
67 #if BC_LONG_BIT > LONG_BIT
68 #error BC_LONG_BIT cannot be greater than LONG_BIT
69 #endif // BC_LONG_BIT > LONG_BIT
73 typedef int_least32_t BcDig;
74 typedef uint64_t BcBigDig;
76 #define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT64_MAX)
78 #define BC_BASE_DIGS (9)
79 #define BC_BASE_POW (1000000000)
81 #define BC_NUM_BIGDIG_C UINT64_C
83 #elif BC_LONG_BIT >= 32
85 typedef int_least16_t BcDig;
86 typedef uint32_t BcBigDig;
88 #define BC_NUM_BIGDIG_MAX ((BcBigDig) UINT32_MAX)
90 #define BC_BASE_DIGS (4)
91 #define BC_BASE_POW (10000)
93 #define BC_NUM_BIGDIG_C UINT32_C
97 #error BC_LONG_BIT must be at least 32
99 #endif // BC_LONG_BIT >= 64
101 #define BC_NUM_DEF_SIZE (8)
103 typedef struct BcNum {
112 #if BC_ENABLE_EXTRA_MATH
113 // Forward declaration
115 #endif // BC_ENABLE_EXTRA_MATH
117 #define BC_NUM_MIN_BASE (BC_NUM_BIGDIG_C(2))
118 #define BC_NUM_MAX_POSIX_IBASE (BC_NUM_BIGDIG_C(16))
119 #define BC_NUM_MAX_IBASE (BC_NUM_BIGDIG_C(36))
120 // This is the max base allowed by bc_num_parseChar().
121 #define BC_NUM_MAX_LBASE (BC_NUM_BIGDIG_C('Z' + BC_BASE + 1))
122 #define BC_NUM_PRINT_WIDTH (BC_NUM_BIGDIG_C(69))
124 #ifndef BC_NUM_KARATSUBA_LEN
125 #define BC_NUM_KARATSUBA_LEN (BC_NUM_BIGDIG_C(32))
126 #elif BC_NUM_KARATSUBA_LEN < 16
127 #error BC_NUM_KARATSUBA_LEN must be at least 16.
128 #endif // BC_NUM_KARATSUBA_LEN
130 // A crude, but always big enough, calculation of
131 // the size required for ibase and obase BcNum's.
132 #define BC_NUM_BIGDIG_LOG10 (BC_NUM_DEF_SIZE)
134 #define BC_NUM_NONZERO(n) ((n)->len)
135 #define BC_NUM_ZERO(n) (!BC_NUM_NONZERO(n))
136 #define BC_NUM_ONE(n) ((n)->len == 1 && (n)->rdx == 0 && (n)->num[0] == 1)
138 #define BC_NUM_NUM_LETTER(c) ((c) - 'A' + BC_BASE)
140 #define BC_NUM_KARATSUBA_ALLOCS (6)
142 #define BC_NUM_ROUND_POW(s) (bc_vm_growSize((s), BC_BASE_DIGS - 1))
143 #define BC_NUM_RDX(s) (BC_NUM_ROUND_POW(s) / BC_BASE_DIGS)
145 #define BC_NUM_SIZE(n) ((n) * sizeof(BcDig))
148 #define BC_NUM_PRINT(x) fprintf(stderr, "%s = %lu\n", #x, (unsigned long)(x))
149 #define DUMP_NUM bc_num_dump
150 #else // BC_DEBUG_CODE
152 #define DUMP_NUM(x,y)
153 #define BC_NUM_PRINT(x)
154 #endif // BC_DEBUG_CODE
156 typedef void (*BcNumBinaryOp)(BcNum*, BcNum*, BcNum*, size_t);
157 typedef size_t (*BcNumBinaryOpReq)(const BcNum*, const BcNum*, size_t);
158 typedef void (*BcNumDigitOp)(size_t, size_t, bool);
159 typedef void (*BcNumShiftAddOp)(BcDig*, const BcDig*, size_t);
161 void bc_num_init(BcNum *restrict n, size_t req);
162 void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap);
163 void bc_num_copy(BcNum *d, const BcNum *s);
164 void bc_num_createCopy(BcNum *d, const BcNum *s);
165 void bc_num_createFromBigdig(BcNum *n, BcBigDig val);
166 void bc_num_clear(BcNum *restrict n);
167 void bc_num_free(void *num);
169 size_t bc_num_scale(const BcNum *restrict n);
170 size_t bc_num_len(const BcNum *restrict n);
172 void bc_num_bigdig(const BcNum *restrict n, BcBigDig *result);
173 void bc_num_bigdig2(const BcNum *restrict n, BcBigDig *result);
174 void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val);
176 #if BC_ENABLE_EXTRA_MATH
177 void bc_num_irand(const BcNum *restrict a, BcNum *restrict b,
178 struct BcRNG *restrict rng);
179 void bc_num_rng(const BcNum *restrict n, struct BcRNG *rng);
180 void bc_num_createFromRNG(BcNum *restrict n, struct BcRNG *rng);
181 #endif // BC_ENABLE_EXTRA_MATH
183 void bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale);
184 void bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale);
185 void bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale);
186 void bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale);
187 void bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale);
188 void bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale);
189 #if BC_ENABLE_EXTRA_MATH
190 void bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale);
191 void bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale);
192 void bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale);
193 #endif // BC_ENABLE_EXTRA_MATH
194 void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale);
195 void bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale);
197 size_t bc_num_addReq(const BcNum* a, const BcNum* b, size_t scale);
199 size_t bc_num_mulReq(const BcNum *a, const BcNum *b, size_t scale);
200 size_t bc_num_powReq(const BcNum *a, const BcNum *b, size_t scale);
201 #if BC_ENABLE_EXTRA_MATH
202 size_t bc_num_placesReq(const BcNum *a, const BcNum *b, size_t scale);
203 #endif // BC_ENABLE_EXTRA_MATH
205 void bc_num_truncate(BcNum *restrict n, size_t places);
206 ssize_t bc_num_cmp(const BcNum *a, const BcNum *b);
209 void bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d);
212 void bc_num_one(BcNum *restrict n);
213 ssize_t bc_num_cmpZero(const BcNum *n);
215 void bc_num_parse(BcNum *restrict n, const char *restrict val,
216 BcBigDig base, bool letter);
217 void bc_num_print(BcNum *restrict n, BcBigDig base, bool newline);
219 void bc_num_stream(BcNum *restrict n, BcBigDig base);
223 void bc_num_printDebug(const BcNum *n, const char *name, bool emptyline);
224 void bc_num_printDigs(const BcDig* n, size_t len, bool emptyline);
225 void bc_num_printWithDigs(const BcNum *n, const char *name, bool emptyline);
226 void bc_num_dump(const char *varname, const BcNum *n);
227 #endif // BC_DEBUG_CODE
229 extern const char bc_num_hex_digits[];
230 extern const BcBigDig bc_num_pow10[BC_BASE_DIGS + 1];
232 extern const BcDig bc_num_bigdigMax[];
233 extern const size_t bc_num_bigdigMax_size;