2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2023 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 * The public header for the bc library.
58 #define ssize_t SSIZE_T
61 #define BCL_SEED_ULONGS (4)
62 #define BCL_SEED_SIZE (sizeof(long) * BCL_SEED_ULONGS)
64 // For some reason, LONG_BIT is not defined in some versions of gcc.
65 // I define it here to the minimum accepted value in the POSIX standard.
71 #define BC_LONG_BIT LONG_BIT
74 #if BC_LONG_BIT > LONG_BIT
75 #error BC_LONG_BIT cannot be greater than LONG_BIT
76 #endif // BC_LONG_BIT > LONG_BIT
78 // For more information about the items here, see the either the
79 // manuals/bcl.3.md or manuals/bcl.3 manuals.
81 // BclBigDig is a fixed-size integer type that bcl can convert numbers to.
83 // BclRandInt is the type of fixed-size integer natively returned by the
84 // pseudo-random number generator.
87 typedef uint64_t BclBigDig;
88 typedef uint64_t BclRandInt;
90 #elif BC_LONG_BIT >= 32
92 typedef uint32_t BclBigDig;
93 typedef uint32_t BclRandInt;
97 #error BC_LONG_BIT must be at least 32
99 #endif // BC_LONG_BIT >= 64
101 #ifndef BC_ENABLE_LIBRARY
102 #define BC_ENABLE_LIBRARY (1)
103 #endif // BC_ENABLE_LIBRARY
105 #if BC_ENABLE_LIBRARY
107 typedef enum BclError
111 BCL_ERROR_INVALID_NUM,
112 BCL_ERROR_INVALID_CONTEXT,
115 BCL_ERROR_MATH_NEGATIVE,
116 BCL_ERROR_MATH_NON_INTEGER,
117 BCL_ERROR_MATH_OVERFLOW,
118 BCL_ERROR_MATH_DIVIDE_BY_ZERO,
120 BCL_ERROR_PARSE_INVALID_STR,
122 BCL_ERROR_FATAL_ALLOC_ERR,
123 BCL_ERROR_FATAL_UNKNOWN_ERR,
129 typedef struct BclNumber
137 typedef struct BclCtxt* BclContext;
152 bcl_abortOnFatalError(void);
155 bcl_setAbortOnFatalError(bool abrt);
158 bcl_leadingZeroes(void);
161 bcl_setLeadingZeroes(bool leadingZeroes);
164 bcl_digitClamp(void);
167 bcl_setDigitClamp(bool digitClamp);
173 bcl_pushContext(BclContext ctxt);
176 bcl_popContext(void);
182 bcl_ctxt_create(void);
185 bcl_ctxt_free(BclContext ctxt);
188 bcl_ctxt_freeNums(BclContext ctxt);
191 bcl_ctxt_scale(BclContext ctxt);
194 bcl_ctxt_setScale(BclContext ctxt, size_t scale);
197 bcl_ctxt_ibase(BclContext ctxt);
200 bcl_ctxt_setIbase(BclContext ctxt, size_t ibase);
203 bcl_ctxt_obase(BclContext ctxt);
206 bcl_ctxt_setObase(BclContext ctxt, size_t obase);
209 bcl_err(BclNumber n);
212 bcl_num_create(void);
215 bcl_num_free(BclNumber n);
218 bcl_num_neg(BclNumber n);
221 bcl_num_setNeg(BclNumber n, bool neg);
224 bcl_num_scale(BclNumber n);
227 bcl_num_setScale(BclNumber n, size_t scale);
230 bcl_num_len(BclNumber n);
233 bcl_copy(BclNumber d, BclNumber s);
236 bcl_dup(BclNumber s);
239 bcl_bigdig(BclNumber n, BclBigDig* result);
242 bcl_bigdig2num(BclBigDig val);
245 bcl_add(BclNumber a, BclNumber b);
248 bcl_sub(BclNumber a, BclNumber b);
251 bcl_mul(BclNumber a, BclNumber b);
254 bcl_div(BclNumber a, BclNumber b);
257 bcl_mod(BclNumber a, BclNumber b);
260 bcl_pow(BclNumber a, BclNumber b);
263 bcl_lshift(BclNumber a, BclNumber b);
266 bcl_rshift(BclNumber a, BclNumber b);
269 bcl_sqrt(BclNumber a);
272 bcl_divmod(BclNumber a, BclNumber b, BclNumber* c, BclNumber* d);
275 bcl_modexp(BclNumber a, BclNumber b, BclNumber c);
278 bcl_cmp(BclNumber a, BclNumber b);
281 bcl_zero(BclNumber n);
284 bcl_one(BclNumber n);
287 bcl_parse(const char* restrict val);
290 bcl_string(BclNumber n);
293 bcl_irand(BclNumber a);
296 bcl_frand(size_t places);
299 bcl_ifrand(BclNumber a, size_t places);
302 bcl_rand_seedWithNum(BclNumber n);
305 bcl_rand_seed(unsigned char seed[BCL_SEED_SIZE]);
308 bcl_rand_reseed(void);
311 bcl_rand_seed2num(void);
317 bcl_rand_bounded(BclRandInt bound);
319 #endif // BC_ENABLE_LIBRARY