xref: /freebsd/contrib/bc/include/bcl.h (revision 3332f1b444d4a73238e9f59cca27bfc95fe936bd)
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 // Windows has deprecated isatty() and the rest of these. Or doesn't have them.
53 // So these are just fixes for Windows.
54 #ifdef _WIN32
55 
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.
59 #define inline
60 
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))
65 #define close _close
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
78 #define stat _stat
79 #define fstat _fstat
80 #define BC_FILE_SEP '\\'
81 
82 #else // _WIN32
83 #define BC_FILE_SEP '/'
84 #endif // _WIN32
85 
86 #define BCL_SEED_ULONGS (4)
87 #define BCL_SEED_SIZE (sizeof(long) * BCL_SEED_ULONGS)
88 
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.
91 #ifndef LONG_BIT
92 #define LONG_BIT (32)
93 #endif // LONG_BIT
94 
95 #ifndef BC_LONG_BIT
96 #define BC_LONG_BIT LONG_BIT
97 #endif // BC_LONG_BIT
98 
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
102 
103 // For more information about the items here, see the either the
104 // manuals/bcl.3.md or manuals/bcl.3 manuals.
105 
106 // BclBigDig is a fixed-size integer type that bcl can convert numbers to.
107 //
108 // BclRandInt is the type of fixed-size integer natively returned by the
109 // pseudo-random number generator.
110 #if BC_LONG_BIT >= 64
111 
112 typedef uint64_t BclBigDig;
113 typedef uint64_t BclRandInt;
114 
115 #elif BC_LONG_BIT >= 32
116 
117 typedef uint32_t BclBigDig;
118 typedef uint32_t BclRandInt;
119 
120 #else
121 
122 #error BC_LONG_BIT must be at least 32
123 
124 #endif // BC_LONG_BIT >= 64
125 
126 #ifndef BC_ENABLE_LIBRARY
127 #define BC_ENABLE_LIBRARY (1)
128 #endif // BC_ENABLE_LIBRARY
129 
130 #if BC_ENABLE_LIBRARY
131 
132 typedef enum BclError {
133 
134 	BCL_ERROR_NONE,
135 
136 	BCL_ERROR_INVALID_NUM,
137 	BCL_ERROR_INVALID_CONTEXT,
138 	BCL_ERROR_SIGNAL,
139 
140 	BCL_ERROR_MATH_NEGATIVE,
141 	BCL_ERROR_MATH_NON_INTEGER,
142 	BCL_ERROR_MATH_OVERFLOW,
143 	BCL_ERROR_MATH_DIVIDE_BY_ZERO,
144 
145 	BCL_ERROR_PARSE_INVALID_STR,
146 
147 	BCL_ERROR_FATAL_ALLOC_ERR,
148 	BCL_ERROR_FATAL_UNKNOWN_ERR,
149 
150 	BCL_ERROR_NELEMS,
151 
152 } BclError;
153 
154 typedef struct BclNumber {
155 
156 	size_t i;
157 
158 } BclNumber;
159 
160 struct BclCtxt;
161 
162 typedef struct BclCtxt* BclContext;
163 
164 void bcl_handleSignal(void);
165 bool bcl_running(void);
166 
167 BclError bcl_init(void);
168 void bcl_free(void);
169 
170 bool bcl_abortOnFatalError(void);
171 void bcl_setAbortOnFatalError(bool abrt);
172 bool bcl_leadingZeroes(void);
173 void bcl_setLeadingZeroes(bool leadingZeroes);
174 
175 void bcl_gc(void);
176 
177 BclError bcl_pushContext(BclContext ctxt);
178 void bcl_popContext(void);
179 BclContext bcl_context(void);
180 
181 BclContext bcl_ctxt_create(void);
182 void bcl_ctxt_free(BclContext ctxt);
183 void bcl_ctxt_freeNums(BclContext ctxt);
184 
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);
191 
192 BclError bcl_err(BclNumber n);
193 
194 BclNumber bcl_num_create(void);
195 void bcl_num_free(BclNumber n);
196 
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);
202 
203 BclError bcl_copy(BclNumber d, BclNumber s);
204 BclNumber bcl_dup(BclNumber s);
205 
206 BclError bcl_bigdig(BclNumber n, BclBigDig *result);
207 BclNumber bcl_bigdig2num(BclBigDig val);
208 
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);
220 
221 ssize_t bcl_cmp(BclNumber a, BclNumber b);
222 
223 void bcl_zero(BclNumber n);
224 void bcl_one(BclNumber n);
225 
226 BclNumber bcl_parse(const char *restrict val);
227 char* bcl_string(BclNumber n);
228 
229 BclNumber bcl_irand(BclNumber a);
230 BclNumber bcl_frand(size_t places);
231 BclNumber bcl_ifrand(BclNumber a, size_t places);
232 
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);
239 
240 #endif // BC_ENABLE_LIBRARY
241 
242 #endif // BC_BCL_H
243