2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * *****************************************************************************
32 * The public header for the bc library.
50 #include <sys/types.h>
52 // Windows has deprecated isatty() and the rest of these. Or doesn't have them.
53 // So these are just fixes for Windows.
56 // This one is special. Windows did not like me defining an
57 // inline function that was not given a definition in a header
58 // file. This suppresses that by making inline functions non-inline.
61 #define restrict __restrict
62 #define strdup _strdup
63 #define write(f, b, s) _write((f), (b), (unsigned int) (s))
64 #define read(f, b, s) _read((f), (b), (unsigned int) (s))
66 #define open(f, n, m) \
67 _sopen_s((f), (n), (m) | _O_BINARY, _SH_DENYNO, _S_IREAD | _S_IWRITE)
68 #define sigjmp_buf jmp_buf
69 #define sigsetjmp(j, s) setjmp(j)
70 #define siglongjmp longjmp
71 #define isatty _isatty
72 #define STDIN_FILENO _fileno(stdin)
73 #define STDOUT_FILENO _fileno(stdout)
74 #define STDERR_FILENO _fileno(stderr)
75 #define ssize_t SSIZE_T
76 #define S_ISDIR(m) ((m) & _S_IFDIR)
77 #define O_RDONLY _O_RDONLY
80 #define BC_FILE_SEP '\\'
83 #define BC_FILE_SEP '/'
86 #define BCL_SEED_ULONGS (4)
87 #define BCL_SEED_SIZE (sizeof(long) * BCL_SEED_ULONGS)
89 // For some reason, LONG_BIT is not defined in some versions of gcc.
90 // I define it here to the minimum accepted value in the POSIX standard.
96 #define BC_LONG_BIT LONG_BIT
99 #if BC_LONG_BIT > LONG_BIT
100 #error BC_LONG_BIT cannot be greater than LONG_BIT
101 #endif // BC_LONG_BIT > LONG_BIT
103 // For more information about the items here, see the either the
104 // manuals/bcl.3.md or manuals/bcl.3 manuals.
106 // BclBigDig is a fixed-size integer type that bcl can convert numbers to.
108 // BclRandInt is the type of fixed-size integer natively returned by the
109 // pseudo-random number generator.
110 #if BC_LONG_BIT >= 64
112 typedef uint64_t BclBigDig;
113 typedef uint64_t BclRandInt;
115 #elif BC_LONG_BIT >= 32
117 typedef uint32_t BclBigDig;
118 typedef uint32_t BclRandInt;
122 #error BC_LONG_BIT must be at least 32
124 #endif // BC_LONG_BIT >= 64
126 #ifndef BC_ENABLE_LIBRARY
127 #define BC_ENABLE_LIBRARY (1)
128 #endif // BC_ENABLE_LIBRARY
130 #if BC_ENABLE_LIBRARY
132 typedef enum BclError {
136 BCL_ERROR_INVALID_NUM,
137 BCL_ERROR_INVALID_CONTEXT,
140 BCL_ERROR_MATH_NEGATIVE,
141 BCL_ERROR_MATH_NON_INTEGER,
142 BCL_ERROR_MATH_OVERFLOW,
143 BCL_ERROR_MATH_DIVIDE_BY_ZERO,
145 BCL_ERROR_PARSE_INVALID_STR,
147 BCL_ERROR_FATAL_ALLOC_ERR,
148 BCL_ERROR_FATAL_UNKNOWN_ERR,
154 typedef struct BclNumber {
162 typedef struct BclCtxt* BclContext;
164 void bcl_handleSignal(void);
165 bool bcl_running(void);
167 BclError bcl_init(void);
170 bool bcl_abortOnFatalError(void);
171 void bcl_setAbortOnFatalError(bool abrt);
172 bool bcl_leadingZeroes(void);
173 void bcl_setLeadingZeroes(bool leadingZeroes);
177 BclError bcl_pushContext(BclContext ctxt);
178 void bcl_popContext(void);
179 BclContext bcl_context(void);
181 BclContext bcl_ctxt_create(void);
182 void bcl_ctxt_free(BclContext ctxt);
183 void bcl_ctxt_freeNums(BclContext ctxt);
185 size_t bcl_ctxt_scale(BclContext ctxt);
186 void bcl_ctxt_setScale(BclContext ctxt, size_t scale);
187 size_t bcl_ctxt_ibase(BclContext ctxt);
188 void bcl_ctxt_setIbase(BclContext ctxt, size_t ibase);
189 size_t bcl_ctxt_obase(BclContext ctxt);
190 void bcl_ctxt_setObase(BclContext ctxt, size_t obase);
192 BclError bcl_err(BclNumber n);
194 BclNumber bcl_num_create(void);
195 void bcl_num_free(BclNumber n);
197 bool bcl_num_neg(BclNumber n);
198 void bcl_num_setNeg(BclNumber n, bool neg);
199 size_t bcl_num_scale(BclNumber n);
200 BclError bcl_num_setScale(BclNumber n, size_t scale);
201 size_t bcl_num_len(BclNumber n);
203 BclError bcl_copy(BclNumber d, BclNumber s);
204 BclNumber bcl_dup(BclNumber s);
206 BclError bcl_bigdig(BclNumber n, BclBigDig *result);
207 BclNumber bcl_bigdig2num(BclBigDig val);
209 BclNumber bcl_add(BclNumber a, BclNumber b);
210 BclNumber bcl_sub(BclNumber a, BclNumber b);
211 BclNumber bcl_mul(BclNumber a, BclNumber b);
212 BclNumber bcl_div(BclNumber a, BclNumber b);
213 BclNumber bcl_mod(BclNumber a, BclNumber b);
214 BclNumber bcl_pow(BclNumber a, BclNumber b);
215 BclNumber bcl_lshift(BclNumber a, BclNumber b);
216 BclNumber bcl_rshift(BclNumber a, BclNumber b);
217 BclNumber bcl_sqrt(BclNumber a);
218 BclError bcl_divmod(BclNumber a, BclNumber b, BclNumber *c, BclNumber *d);
219 BclNumber bcl_modexp(BclNumber a, BclNumber b, BclNumber c);
221 ssize_t bcl_cmp(BclNumber a, BclNumber b);
223 void bcl_zero(BclNumber n);
224 void bcl_one(BclNumber n);
226 BclNumber bcl_parse(const char *restrict val);
227 char* bcl_string(BclNumber n);
229 BclNumber bcl_irand(BclNumber a);
230 BclNumber bcl_frand(size_t places);
231 BclNumber bcl_ifrand(BclNumber a, size_t places);
233 BclError bcl_rand_seedWithNum(BclNumber n);
234 BclError bcl_rand_seed(unsigned char seed[BCL_SEED_SIZE]);
235 void bcl_rand_reseed(void);
236 BclNumber bcl_rand_seed2num(void);
237 BclRandInt bcl_rand_int(void);
238 BclRandInt bcl_rand_bounded(BclRandInt bound);
240 #endif // BC_ENABLE_LIBRARY