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 165 bcl_handleSignal(void); 166 167 bool 168 bcl_running(void); 169 170 BclError 171 bcl_init(void); 172 173 void 174 bcl_free(void); 175 176 bool 177 bcl_abortOnFatalError(void); 178 179 void 180 bcl_setAbortOnFatalError(bool abrt); 181 182 bool 183 bcl_leadingZeroes(void); 184 185 void 186 bcl_setLeadingZeroes(bool leadingZeroes); 187 188 void 189 bcl_gc(void); 190 191 BclError 192 bcl_pushContext(BclContext ctxt); 193 194 void 195 bcl_popContext(void); 196 197 BclContext 198 bcl_context(void); 199 200 BclContext 201 bcl_ctxt_create(void); 202 203 void 204 bcl_ctxt_free(BclContext ctxt); 205 206 void 207 bcl_ctxt_freeNums(BclContext ctxt); 208 209 size_t 210 bcl_ctxt_scale(BclContext ctxt); 211 212 void 213 bcl_ctxt_setScale(BclContext ctxt, size_t scale); 214 215 size_t 216 bcl_ctxt_ibase(BclContext ctxt); 217 218 void 219 bcl_ctxt_setIbase(BclContext ctxt, size_t ibase); 220 221 size_t 222 bcl_ctxt_obase(BclContext ctxt); 223 224 void 225 bcl_ctxt_setObase(BclContext ctxt, size_t obase); 226 227 BclError 228 bcl_err(BclNumber n); 229 230 BclNumber 231 bcl_num_create(void); 232 233 void 234 bcl_num_free(BclNumber n); 235 236 bool 237 bcl_num_neg(BclNumber n); 238 239 void 240 bcl_num_setNeg(BclNumber n, bool neg); 241 242 size_t 243 bcl_num_scale(BclNumber n); 244 245 BclError 246 bcl_num_setScale(BclNumber n, size_t scale); 247 248 size_t 249 bcl_num_len(BclNumber n); 250 251 BclError 252 bcl_copy(BclNumber d, BclNumber s); 253 254 BclNumber 255 bcl_dup(BclNumber s); 256 257 BclError 258 bcl_bigdig(BclNumber n, BclBigDig* result); 259 260 BclNumber 261 bcl_bigdig2num(BclBigDig val); 262 263 BclNumber 264 bcl_add(BclNumber a, BclNumber b); 265 266 BclNumber 267 bcl_sub(BclNumber a, BclNumber b); 268 269 BclNumber 270 bcl_mul(BclNumber a, BclNumber b); 271 272 BclNumber 273 bcl_div(BclNumber a, BclNumber b); 274 275 BclNumber 276 bcl_mod(BclNumber a, BclNumber b); 277 278 BclNumber 279 bcl_pow(BclNumber a, BclNumber b); 280 281 BclNumber 282 bcl_lshift(BclNumber a, BclNumber b); 283 284 BclNumber 285 bcl_rshift(BclNumber a, BclNumber b); 286 287 BclNumber 288 bcl_sqrt(BclNumber a); 289 290 BclError 291 bcl_divmod(BclNumber a, BclNumber b, BclNumber* c, BclNumber* d); 292 293 BclNumber 294 bcl_modexp(BclNumber a, BclNumber b, BclNumber c); 295 296 ssize_t 297 bcl_cmp(BclNumber a, BclNumber b); 298 299 void 300 bcl_zero(BclNumber n); 301 302 void 303 bcl_one(BclNumber n); 304 305 BclNumber 306 bcl_parse(const char* restrict val); 307 308 char* 309 bcl_string(BclNumber n); 310 311 BclNumber 312 bcl_irand(BclNumber a); 313 314 BclNumber 315 bcl_frand(size_t places); 316 317 BclNumber 318 bcl_ifrand(BclNumber a, size_t places); 319 320 BclError 321 bcl_rand_seedWithNum(BclNumber n); 322 323 BclError 324 bcl_rand_seed(unsigned char seed[BCL_SEED_SIZE]); 325 326 void 327 bcl_rand_reseed(void); 328 329 BclNumber 330 bcl_rand_seed2num(void); 331 332 BclRandInt 333 bcl_rand_int(void); 334 335 BclRandInt 336 bcl_rand_bounded(BclRandInt bound); 337 338 #endif // BC_ENABLE_LIBRARY 339 340 #endif // BC_BCL_H 341