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) _sopen_s(f, n, m, _SH_DENYNO, _S_IREAD | _S_IWRITE) 67 #define sigjmp_buf jmp_buf 68 #define sigsetjmp(j, s) setjmp(j) 69 #define siglongjmp longjmp 70 #define isatty _isatty 71 #define STDIN_FILENO _fileno(stdin) 72 #define STDOUT_FILENO _fileno(stdout) 73 #define STDERR_FILENO _fileno(stderr) 74 #define ssize_t SSIZE_T 75 #define S_ISDIR(m) ((m) & _S_IFDIR) 76 #define O_RDONLY _O_RDONLY 77 #define stat _stat 78 #define fstat _fstat 79 #define BC_FILE_SEP '\\' 80 81 #else // _WIN32 82 #define BC_FILE_SEP '/' 83 #endif // _WIN32 84 85 #define BCL_SEED_ULONGS (4) 86 #define BCL_SEED_SIZE (sizeof(long) * BCL_SEED_ULONGS) 87 88 // For some reason, LONG_BIT is not defined in some versions of gcc. 89 // I define it here to the minimum accepted value in the POSIX standard. 90 #ifndef LONG_BIT 91 #define LONG_BIT (32) 92 #endif // LONG_BIT 93 94 #ifndef BC_LONG_BIT 95 #define BC_LONG_BIT LONG_BIT 96 #endif // BC_LONG_BIT 97 98 #if BC_LONG_BIT > LONG_BIT 99 #error BC_LONG_BIT cannot be greater than LONG_BIT 100 #endif // BC_LONG_BIT > LONG_BIT 101 102 // For more information about the items here, see the either the 103 // manuals/bcl.3.md or manuals/bcl.3 manuals. 104 105 // BclBigDig is a fixed-size integer type that bcl can convert numbers to. 106 // 107 // BclRandInt is the type of fixed-size integer natively returned by the 108 // pseudo-random number generator. 109 #if BC_LONG_BIT >= 64 110 111 typedef uint64_t BclBigDig; 112 typedef uint64_t BclRandInt; 113 114 #elif BC_LONG_BIT >= 32 115 116 typedef uint32_t BclBigDig; 117 typedef uint32_t BclRandInt; 118 119 #else 120 121 #error BC_LONG_BIT must be at least 32 122 123 #endif // BC_LONG_BIT >= 64 124 125 #ifndef BC_ENABLE_LIBRARY 126 #define BC_ENABLE_LIBRARY (1) 127 #endif // BC_ENABLE_LIBRARY 128 129 #if BC_ENABLE_LIBRARY 130 131 typedef enum BclError { 132 133 BCL_ERROR_NONE, 134 135 BCL_ERROR_INVALID_NUM, 136 BCL_ERROR_INVALID_CONTEXT, 137 BCL_ERROR_SIGNAL, 138 139 BCL_ERROR_MATH_NEGATIVE, 140 BCL_ERROR_MATH_NON_INTEGER, 141 BCL_ERROR_MATH_OVERFLOW, 142 BCL_ERROR_MATH_DIVIDE_BY_ZERO, 143 144 BCL_ERROR_PARSE_INVALID_STR, 145 146 BCL_ERROR_FATAL_ALLOC_ERR, 147 BCL_ERROR_FATAL_UNKNOWN_ERR, 148 149 BCL_ERROR_NELEMS, 150 151 } BclError; 152 153 typedef struct BclNumber { 154 155 size_t i; 156 157 } BclNumber; 158 159 struct BclCtxt; 160 161 typedef struct BclCtxt* BclContext; 162 163 void bcl_handleSignal(void); 164 bool bcl_running(void); 165 166 BclError bcl_init(void); 167 void bcl_free(void); 168 169 bool bcl_abortOnFatalError(void); 170 void bcl_setAbortOnFatalError(bool abrt); 171 172 void bcl_gc(void); 173 174 BclError bcl_pushContext(BclContext ctxt); 175 void bcl_popContext(void); 176 BclContext bcl_context(void); 177 178 BclContext bcl_ctxt_create(void); 179 void bcl_ctxt_free(BclContext ctxt); 180 void bcl_ctxt_freeNums(BclContext ctxt); 181 182 size_t bcl_ctxt_scale(BclContext ctxt); 183 void bcl_ctxt_setScale(BclContext ctxt, size_t scale); 184 size_t bcl_ctxt_ibase(BclContext ctxt); 185 void bcl_ctxt_setIbase(BclContext ctxt, size_t ibase); 186 size_t bcl_ctxt_obase(BclContext ctxt); 187 void bcl_ctxt_setObase(BclContext ctxt, size_t obase); 188 189 BclError bcl_err(BclNumber n); 190 191 BclNumber bcl_num_create(void); 192 void bcl_num_free(BclNumber n); 193 194 bool bcl_num_neg(BclNumber n); 195 void bcl_num_setNeg(BclNumber n, bool neg); 196 size_t bcl_num_scale(BclNumber n); 197 BclError bcl_num_setScale(BclNumber n, size_t scale); 198 size_t bcl_num_len(BclNumber n); 199 200 BclError bcl_copy(BclNumber d, BclNumber s); 201 BclNumber bcl_dup(BclNumber s); 202 203 BclError bcl_bigdig(BclNumber n, BclBigDig *result); 204 BclNumber bcl_bigdig2num(BclBigDig val); 205 206 BclNumber bcl_add(BclNumber a, BclNumber b); 207 BclNumber bcl_sub(BclNumber a, BclNumber b); 208 BclNumber bcl_mul(BclNumber a, BclNumber b); 209 BclNumber bcl_div(BclNumber a, BclNumber b); 210 BclNumber bcl_mod(BclNumber a, BclNumber b); 211 BclNumber bcl_pow(BclNumber a, BclNumber b); 212 BclNumber bcl_lshift(BclNumber a, BclNumber b); 213 BclNumber bcl_rshift(BclNumber a, BclNumber b); 214 BclNumber bcl_sqrt(BclNumber a); 215 BclError bcl_divmod(BclNumber a, BclNumber b, BclNumber *c, BclNumber *d); 216 BclNumber bcl_modexp(BclNumber a, BclNumber b, BclNumber c); 217 218 ssize_t bcl_cmp(BclNumber a, BclNumber b); 219 220 void bcl_zero(BclNumber n); 221 void bcl_one(BclNumber n); 222 223 BclNumber bcl_parse(const char *restrict val); 224 char* bcl_string(BclNumber n); 225 226 BclNumber bcl_irand(BclNumber a); 227 BclNumber bcl_frand(size_t places); 228 BclNumber bcl_ifrand(BclNumber a, size_t places); 229 230 BclError bcl_rand_seedWithNum(BclNumber n); 231 BclError bcl_rand_seed(unsigned char seed[BCL_SEED_SIZE]); 232 void bcl_rand_reseed(void); 233 BclNumber bcl_rand_seed2num(void); 234 BclRandInt bcl_rand_int(void); 235 BclRandInt bcl_rand_bounded(BclRandInt bound); 236 237 #endif // BC_ENABLE_LIBRARY 238 239 #endif // BC_BCL_H 240