1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  *
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.
17  *
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.
29  *
30  * *****************************************************************************
31  *
32  * The public header for the bc library.
33  *
34  */
35 
36 #ifndef BC_BCL_H
37 #define BC_BCL_H
38 
39 #ifdef _WIN32
40 #include <Windows.h>
41 #include <BaseTsd.h>
42 #include <stdio.h>
43 #include <io.h>
44 #endif // _WIN32
45 
46 #include <stdbool.h>
47 #include <stdlib.h>
48 #include <limits.h>
49 #include <stdint.h>
50 #include <sys/types.h>
51 
52 #define BC_SEED_ULONGS (4)
53 #define BC_SEED_SIZE (sizeof(long) * BC_SEED_ULONGS)
54 
55 // For some reason, LONG_BIT is not defined in some versions of gcc.
56 // I define it here to the minimum accepted value in the POSIX standard.
57 #ifndef LONG_BIT
58 #define LONG_BIT (32)
59 #endif // LONG_BIT
60 
61 #ifndef BC_LONG_BIT
62 #define BC_LONG_BIT LONG_BIT
63 #endif // BC_LONG_BIT
64 
65 #if BC_LONG_BIT > LONG_BIT
66 #error BC_LONG_BIT cannot be greater than LONG_BIT
67 #endif // BC_LONG_BIT > LONG_BIT
68 
69 #if BC_LONG_BIT >= 64
70 
71 typedef uint64_t BclBigDig;
72 typedef uint64_t BclRandInt;
73 
74 #elif BC_LONG_BIT >= 32
75 
76 typedef uint32_t BclBigDig;
77 typedef uint32_t BclRandInt;
78 
79 #else
80 
81 #error BC_LONG_BIT must be at least 32
82 
83 #endif // BC_LONG_BIT >= 64
84 #define BC_UNUSED(e) ((void) (e))
85 
86 #ifndef BC_LIKELY
87 #define BC_LIKELY(e) (e)
88 #endif // BC_LIKELY
89 
90 #ifndef BC_UNLIKELY
91 #define BC_UNLIKELY(e) (e)
92 #endif // BC_UNLIKELY
93 
94 #define BC_ERR(e) BC_UNLIKELY(e)
95 #define BC_NO_ERR(s) BC_LIKELY(s)
96 
97 #ifndef BC_DEBUG_CODE
98 #define BC_DEBUG_CODE (0)
99 #endif // BC_DEBUG_CODE
100 
101 #if __STDC_VERSION__ >= 201100L
102 #include <stdnoreturn.h>
103 #define BC_NORETURN _Noreturn
104 #else // __STDC_VERSION__
105 #define BC_NORETURN
106 #define BC_MUST_RETURN
107 #endif // __STDC_VERSION__
108 
109 #if defined(__clang__) || defined(__GNUC__)
110 #if defined(__has_attribute)
111 #if __has_attribute(fallthrough)
112 #define BC_FALLTHROUGH __attribute__((fallthrough));
113 #else // __has_attribute(fallthrough)
114 #define BC_FALLTHROUGH
115 #endif // __has_attribute(fallthrough)
116 #else // defined(__has_attribute)
117 #define BC_FALLTHROUGH
118 #endif // defined(__has_attribute)
119 #else // defined(__clang__) || defined(__GNUC__)
120 #define BC_FALLTHROUGH
121 #endif // defined(__clang__) || defined(__GNUC__)
122 
123 // Workarounds for AIX's POSIX incompatibility.
124 #ifndef SIZE_MAX
125 #define SIZE_MAX __SIZE_MAX__
126 #endif // SIZE_MAX
127 #ifndef UINTMAX_C
128 #define UINTMAX_C __UINTMAX_C
129 #endif // UINTMAX_C
130 #ifndef UINT32_C
131 #define UINT32_C __UINT32_C
132 #endif // UINT32_C
133 #ifndef UINT_FAST32_MAX
134 #define UINT_FAST32_MAX __UINT_FAST32_MAX__
135 #endif // UINT_FAST32_MAX
136 #ifndef UINT16_MAX
137 #define UINT16_MAX __UINT16_MAX__
138 #endif // UINT16_MAX
139 #ifndef SIG_ATOMIC_MAX
140 #define SIG_ATOMIC_MAX __SIG_ATOMIC_MAX__
141 #endif // SIG_ATOMIC_MAX
142 
143 // Windows has deprecated isatty() and the rest of these.
144 // Or doesn't have them.
145 #ifdef _WIN32
146 
147 // This one is special. Windows did not like me defining an
148 // inline function that was not given a definition in a header
149 // file. This suppresses that by making inline functions non-inline.
150 #define inline
151 
152 #define restrict __restrict
153 #define strdup _strdup
154 #define write(f, b, s) _write((f), (b), (unsigned int) (s))
155 #define read(f, b, s) _read((f), (b), (unsigned int) (s))
156 #define close _close
157 #define open(f, n, m) _sopen_s(f, n, m, _SH_DENYNO, _S_IREAD | _S_IWRITE)
158 #define sigjmp_buf jmp_buf
159 #define sigsetjmp(j, s) setjmp(j)
160 #define siglongjmp longjmp
161 #define isatty _isatty
162 #define STDIN_FILENO (0)
163 #define STDOUT_FILENO (1)
164 #define STDERR_FILENO (2)
165 #define ssize_t SSIZE_T
166 #define S_ISDIR(m) ((m) & _S_IFDIR)
167 #define O_RDONLY _O_RDONLY
168 #define stat _stat
169 #define fstat _fstat
170 #define BC_FILE_SEP '\\'
171 
172 #else // _WIN32
173 #define BC_FILE_SEP '/'
174 #endif // _WIN32
175 
176 #if BC_ENABLE_LIBRARY
177 
178 typedef enum BclError {
179 
180 	BCL_ERROR_NONE,
181 
182 	BCL_ERROR_INVALID_NUM,
183 	BCL_ERROR_INVALID_CONTEXT,
184 	BCL_ERROR_SIGNAL,
185 
186 	BCL_ERROR_MATH_NEGATIVE,
187 	BCL_ERROR_MATH_NON_INTEGER,
188 	BCL_ERROR_MATH_OVERFLOW,
189 	BCL_ERROR_MATH_DIVIDE_BY_ZERO,
190 
191 	BCL_ERROR_PARSE_INVALID_STR,
192 
193 	BCL_ERROR_FATAL_ALLOC_ERR,
194 	BCL_ERROR_FATAL_UNKNOWN_ERR,
195 
196 	BCL_ERROR_NELEMS,
197 
198 } BclError;
199 
200 typedef struct BclNumber {
201 
202 	size_t i;
203 
204 } BclNumber;
205 
206 struct BclCtxt;
207 
208 typedef struct BclCtxt* BclContext;
209 
210 void bcl_handleSignal(void);
211 bool bcl_running(void);
212 
213 BclError bcl_init(void);
214 void bcl_free(void);
215 
216 bool bcl_abortOnFatalError(void);
217 void bcl_setAbortOnFatalError(bool abrt);
218 
219 void bcl_gc(void);
220 
221 BclError bcl_pushContext(BclContext ctxt);
222 void bcl_popContext(void);
223 BclContext bcl_context(void);
224 
225 BclContext bcl_ctxt_create(void);
226 void bcl_ctxt_free(BclContext ctxt);
227 void bcl_ctxt_freeNums(BclContext ctxt);
228 
229 size_t bcl_ctxt_scale(BclContext ctxt);
230 void bcl_ctxt_setScale(BclContext ctxt, size_t scale);
231 size_t bcl_ctxt_ibase(BclContext ctxt);
232 void bcl_ctxt_setIbase(BclContext ctxt, size_t ibase);
233 size_t bcl_ctxt_obase(BclContext ctxt);
234 void bcl_ctxt_setObase(BclContext ctxt, size_t obase);
235 
236 BclError bcl_err(BclNumber n);
237 
238 BclNumber bcl_num_create(void);
239 void bcl_num_free(BclNumber n);
240 
241 bool bcl_num_neg(BclNumber n);
242 void bcl_num_setNeg(BclNumber n, bool neg);
243 size_t bcl_num_scale(BclNumber n);
244 BclError bcl_num_setScale(BclNumber n, size_t scale);
245 size_t bcl_num_len(BclNumber n);
246 
247 BclError bcl_copy(BclNumber d, BclNumber s);
248 BclNumber bcl_dup(BclNumber s);
249 
250 BclError bcl_bigdig(BclNumber n, BclBigDig *result);
251 BclNumber bcl_bigdig2num(BclBigDig val);
252 
253 BclNumber bcl_add(BclNumber a, BclNumber b);
254 BclNumber bcl_sub(BclNumber a, BclNumber b);
255 BclNumber bcl_mul(BclNumber a, BclNumber b);
256 BclNumber bcl_div(BclNumber a, BclNumber b);
257 BclNumber bcl_mod(BclNumber a, BclNumber b);
258 BclNumber bcl_pow(BclNumber a, BclNumber b);
259 BclNumber bcl_lshift(BclNumber a, BclNumber b);
260 BclNumber bcl_rshift(BclNumber a, BclNumber b);
261 BclNumber bcl_sqrt(BclNumber a);
262 BclError bcl_divmod(BclNumber a, BclNumber b, BclNumber *c, BclNumber *d);
263 BclNumber bcl_modexp(BclNumber a, BclNumber b, BclNumber c);
264 
265 ssize_t bcl_cmp(BclNumber a, BclNumber b);
266 
267 void bcl_zero(BclNumber n);
268 void bcl_one(BclNumber n);
269 
270 BclNumber bcl_parse(const char *restrict val);
271 char* bcl_string(BclNumber n);
272 
273 BclNumber bcl_irand(BclNumber a);
274 BclNumber bcl_frand(size_t places);
275 BclNumber bcl_ifrand(BclNumber a, size_t places);
276 
277 BclError bcl_rand_seedWithNum(BclNumber n);
278 BclError bcl_rand_seed(unsigned char seed[BC_SEED_SIZE]);
279 void bcl_rand_reseed(void);
280 BclNumber bcl_rand_seed2num(void);
281 BclRandInt bcl_rand_int(void);
282 BclRandInt bcl_rand_bounded(BclRandInt bound);
283 
284 #endif // BC_ENABLE_LIBRARY
285 
286 #endif // BC_BCL_H
287