xref: /freebsd/contrib/bc/include/bcl.h (revision 7937bfbc0ca53fe7cdd0d54414f9296e273a518e)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2024 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 #include <stdbool.h>
40 #include <stdlib.h>
41 #include <limits.h>
42 #include <stdint.h>
43 
44 #ifndef NDEBUG
45 #define BC_DEBUG (1)
46 #else // NDEBUG
47 #define BC_DEBUG (0)
48 #endif // NDEBUG
49 
50 #ifdef _WIN32
51 #include <Windows.h>
52 #include <BaseTsd.h>
53 #include <stdio.h>
54 #include <io.h>
55 #endif // _WIN32
56 
57 #ifdef _WIN32
58 #define ssize_t SSIZE_T
59 #endif // _WIN32
60 
61 #define BCL_SEED_ULONGS (4)
62 #define BCL_SEED_SIZE (sizeof(long) * BCL_SEED_ULONGS)
63 
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.
66 #ifndef LONG_BIT
67 #define LONG_BIT (32)
68 #endif // LONG_BIT
69 
70 #ifndef BC_LONG_BIT
71 #define BC_LONG_BIT LONG_BIT
72 #endif // BC_LONG_BIT
73 
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
77 
78 // For more information about the items here, see the either the
79 // manuals/bcl.3.md or manuals/bcl.3 manuals.
80 
81 // BclBigDig is a fixed-size integer type that bcl can convert numbers to.
82 //
83 // BclRandInt is the type of fixed-size integer natively returned by the
84 // pseudo-random number generator.
85 #if BC_LONG_BIT >= 64
86 
87 typedef uint64_t BclBigDig;
88 typedef uint64_t BclRandInt;
89 
90 #elif BC_LONG_BIT >= 32
91 
92 typedef uint32_t BclBigDig;
93 typedef uint32_t BclRandInt;
94 
95 #else
96 
97 #error BC_LONG_BIT must be at least 32
98 
99 #endif // BC_LONG_BIT >= 64
100 
101 #ifndef BC_ENABLE_LIBRARY
102 #define BC_ENABLE_LIBRARY (1)
103 #endif // BC_ENABLE_LIBRARY
104 
105 #if BC_ENABLE_LIBRARY
106 
107 typedef enum BclError
108 {
109 	BCL_ERROR_NONE,
110 
111 	BCL_ERROR_INVALID_NUM,
112 	BCL_ERROR_INVALID_CONTEXT,
113 	BCL_ERROR_SIGNAL,
114 
115 	BCL_ERROR_MATH_NEGATIVE,
116 	BCL_ERROR_MATH_NON_INTEGER,
117 	BCL_ERROR_MATH_OVERFLOW,
118 	BCL_ERROR_MATH_DIVIDE_BY_ZERO,
119 
120 	BCL_ERROR_PARSE_INVALID_STR,
121 
122 	BCL_ERROR_FATAL_ALLOC_ERR,
123 	BCL_ERROR_FATAL_UNKNOWN_ERR,
124 
125 	BCL_ERROR_NELEMS,
126 
127 } BclError;
128 
129 typedef struct BclNumber
130 {
131 	size_t i;
132 
133 } BclNumber;
134 
135 struct BclCtxt;
136 
137 typedef struct BclCtxt* BclContext;
138 
139 BclError
140 bcl_start(void);
141 
142 void
143 bcl_end(void);
144 
145 BclError
146 bcl_init(void);
147 
148 void
149 bcl_free(void);
150 
151 bool
152 bcl_abortOnFatalError(void);
153 
154 void
155 bcl_setAbortOnFatalError(bool abrt);
156 
157 bool
158 bcl_leadingZeroes(void);
159 
160 void
161 bcl_setLeadingZeroes(bool leadingZeroes);
162 
163 bool
164 bcl_digitClamp(void);
165 
166 void
167 bcl_setDigitClamp(bool digitClamp);
168 
169 void
170 bcl_gc(void);
171 
172 BclError
173 bcl_pushContext(BclContext ctxt);
174 
175 void
176 bcl_popContext(void);
177 
178 BclContext
179 bcl_context(void);
180 
181 BclContext
182 bcl_ctxt_create(void);
183 
184 void
185 bcl_ctxt_free(BclContext ctxt);
186 
187 void
188 bcl_ctxt_freeNums(BclContext ctxt);
189 
190 size_t
191 bcl_ctxt_scale(BclContext ctxt);
192 
193 void
194 bcl_ctxt_setScale(BclContext ctxt, size_t scale);
195 
196 size_t
197 bcl_ctxt_ibase(BclContext ctxt);
198 
199 void
200 bcl_ctxt_setIbase(BclContext ctxt, size_t ibase);
201 
202 size_t
203 bcl_ctxt_obase(BclContext ctxt);
204 
205 void
206 bcl_ctxt_setObase(BclContext ctxt, size_t obase);
207 
208 BclError
209 bcl_err(BclNumber n);
210 
211 BclNumber
212 bcl_num_create(void);
213 
214 void
215 bcl_num_free(BclNumber n);
216 
217 bool
218 bcl_num_neg(BclNumber n);
219 
220 void
221 bcl_num_setNeg(BclNumber n, bool neg);
222 
223 size_t
224 bcl_num_scale(BclNumber n);
225 
226 BclError
227 bcl_num_setScale(BclNumber n, size_t scale);
228 
229 size_t
230 bcl_num_len(BclNumber n);
231 
232 BclError
233 bcl_copy(BclNumber d, BclNumber s);
234 
235 BclNumber
236 bcl_dup(BclNumber s);
237 
238 BclError
239 bcl_bigdig(BclNumber n, BclBigDig* result);
240 
241 BclError
242 bcl_bigdig_keep(BclNumber n, BclBigDig* result);
243 
244 BclNumber
245 bcl_bigdig2num(BclBigDig val);
246 
247 BclNumber
248 bcl_add(BclNumber a, BclNumber b);
249 
250 BclNumber
251 bcl_add_keep(BclNumber a, BclNumber b);
252 
253 BclNumber
254 bcl_sub(BclNumber a, BclNumber b);
255 
256 BclNumber
257 bcl_sub_keep(BclNumber a, BclNumber b);
258 
259 BclNumber
260 bcl_mul(BclNumber a, BclNumber b);
261 
262 BclNumber
263 bcl_mul_keep(BclNumber a, BclNumber b);
264 
265 BclNumber
266 bcl_div(BclNumber a, BclNumber b);
267 
268 BclNumber
269 bcl_div_keep(BclNumber a, BclNumber b);
270 
271 BclNumber
272 bcl_mod(BclNumber a, BclNumber b);
273 
274 BclNumber
275 bcl_mod_keep(BclNumber a, BclNumber b);
276 
277 BclNumber
278 bcl_pow(BclNumber a, BclNumber b);
279 
280 BclNumber
281 bcl_pow_keep(BclNumber a, BclNumber b);
282 
283 BclNumber
284 bcl_lshift(BclNumber a, BclNumber b);
285 
286 BclNumber
287 bcl_lshift_keep(BclNumber a, BclNumber b);
288 
289 BclNumber
290 bcl_rshift(BclNumber a, BclNumber b);
291 
292 BclNumber
293 bcl_rshift_keep(BclNumber a, BclNumber b);
294 
295 BclNumber
296 bcl_sqrt(BclNumber a);
297 
298 BclNumber
299 bcl_sqrt_keep(BclNumber a);
300 
301 BclError
302 bcl_divmod(BclNumber a, BclNumber b, BclNumber* c, BclNumber* d);
303 
304 BclError
305 bcl_divmod_keep(BclNumber a, BclNumber b, BclNumber* c, BclNumber* d);
306 
307 BclNumber
308 bcl_modexp(BclNumber a, BclNumber b, BclNumber c);
309 
310 BclNumber
311 bcl_modexp_keep(BclNumber a, BclNumber b, BclNumber c);
312 
313 ssize_t
314 bcl_cmp(BclNumber a, BclNumber b);
315 
316 void
317 bcl_zero(BclNumber n);
318 
319 void
320 bcl_one(BclNumber n);
321 
322 BclNumber
323 bcl_parse(const char* restrict val);
324 
325 char*
326 bcl_string(BclNumber n);
327 
328 char*
329 bcl_string_keep(BclNumber n);
330 
331 BclNumber
332 bcl_irand(BclNumber a);
333 
334 BclNumber
335 bcl_irand_keep(BclNumber a);
336 
337 BclNumber
338 bcl_frand(size_t places);
339 
340 BclNumber
341 bcl_ifrand(BclNumber a, size_t places);
342 
343 BclNumber
344 bcl_ifrand_keep(BclNumber a, size_t places);
345 
346 BclError
347 bcl_rand_seedWithNum(BclNumber n);
348 
349 BclError
350 bcl_rand_seedWithNum_keep(BclNumber n);
351 
352 BclError
353 bcl_rand_seed(unsigned char seed[BCL_SEED_SIZE]);
354 
355 void
356 bcl_rand_reseed(void);
357 
358 BclNumber
359 bcl_rand_seed2num(void);
360 
361 BclRandInt
362 bcl_rand_int(void);
363 
364 BclRandInt
365 bcl_rand_bounded(BclRandInt bound);
366 
367 #endif // BC_ENABLE_LIBRARY
368 
369 #endif // BC_BCL_H
370