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