aboutsummaryrefslogtreecommitdiff
path: root/include/bcl.h
blob: 0908e215182ccb4404aa8794292e4dbd2f63c6fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * *****************************************************************************
 *
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * *****************************************************************************
 *
 * The public header for the bc library.
 *
 */

#ifndef BC_BCL_H
#define BC_BCL_H

// TODO: Add a generation index when building with Valgrind to check for
// use-after-free's or double frees.

#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
#include <stdint.h>

#ifndef NDEBUG
#define BC_DEBUG (1)
#else // NDEBUG
#define BC_DEBUG (0)
#endif // NDEBUG

#ifdef _WIN32
#include <Windows.h>
#include <BaseTsd.h>
#include <stdio.h>
#include <io.h>
#endif // _WIN32

#ifdef _WIN32
#define ssize_t SSIZE_T
#endif // _WIN32

#define BCL_SEED_ULONGS (4)
#define BCL_SEED_SIZE (sizeof(long) * BCL_SEED_ULONGS)

// For some reason, LONG_BIT is not defined in some versions of gcc.
// I define it here to the minimum accepted value in the POSIX standard.
#ifndef LONG_BIT
#define LONG_BIT (32)
#endif // LONG_BIT

#ifndef BC_LONG_BIT
#define BC_LONG_BIT LONG_BIT
#endif // BC_LONG_BIT

#if BC_LONG_BIT > LONG_BIT
#error BC_LONG_BIT cannot be greater than LONG_BIT
#endif // BC_LONG_BIT > LONG_BIT

// For more information about the items here, see the either the
// manuals/bcl.3.md or manuals/bcl.3 manuals.

// BclBigDig is a fixed-size integer type that bcl can convert numbers to.
//
// BclRandInt is the type of fixed-size integer natively returned by the
// pseudo-random number generator.
#if BC_LONG_BIT >= 64

typedef uint64_t BclBigDig;
typedef uint64_t BclRandInt;

#elif BC_LONG_BIT >= 32

typedef uint32_t BclBigDig;
typedef uint32_t BclRandInt;

#else

#error BC_LONG_BIT must be at least 32

#endif // BC_LONG_BIT >= 64

#ifndef BC_ENABLE_LIBRARY
#define BC_ENABLE_LIBRARY (1)
#endif // BC_ENABLE_LIBRARY

#if BC_ENABLE_LIBRARY

typedef enum BclError
{
	BCL_ERROR_NONE,

	BCL_ERROR_INVALID_NUM,
	BCL_ERROR_INVALID_CONTEXT,
	BCL_ERROR_SIGNAL,

	BCL_ERROR_MATH_NEGATIVE,
	BCL_ERROR_MATH_NON_INTEGER,
	BCL_ERROR_MATH_OVERFLOW,
	BCL_ERROR_MATH_DIVIDE_BY_ZERO,

	BCL_ERROR_PARSE_INVALID_STR,

	BCL_ERROR_FATAL_ALLOC_ERR,
	BCL_ERROR_FATAL_UNKNOWN_ERR,

	BCL_ERROR_NELEMS,

} BclError;

typedef struct BclNumber
{
	size_t i;

} BclNumber;

struct BclCtxt;

typedef struct BclCtxt* BclContext;

BclError
bcl_start(void);

void
bcl_end(void);

BclError
bcl_init(void);

void
bcl_free(void);

bool
bcl_abortOnFatalError(void);

void
bcl_setAbortOnFatalError(bool abrt);

bool
bcl_leadingZeroes(void);

void
bcl_setLeadingZeroes(bool leadingZeroes);

bool
bcl_digitClamp(void);

void
bcl_setDigitClamp(bool digitClamp);

void
bcl_gc(void);

BclError
bcl_pushContext(BclContext ctxt);

void
bcl_popContext(void);

BclContext
bcl_context(void);

BclContext
bcl_ctxt_create(void);

void
bcl_ctxt_free(BclContext ctxt);

void
bcl_ctxt_freeNums(BclContext ctxt);

size_t
bcl_ctxt_scale(BclContext ctxt);

void
bcl_ctxt_setScale(BclContext ctxt, size_t scale);

size_t
bcl_ctxt_ibase(BclContext ctxt);

void
bcl_ctxt_setIbase(BclContext ctxt, size_t ibase);

size_t
bcl_ctxt_obase(BclContext ctxt);

void
bcl_ctxt_setObase(BclContext ctxt, size_t obase);

BclError
bcl_err(BclNumber n);

BclNumber
bcl_num_create(void);

void
bcl_num_free(BclNumber n);

bool
bcl_num_neg(BclNumber n);

void
bcl_num_setNeg(BclNumber n, bool neg);

size_t
bcl_num_scale(BclNumber n);

BclError
bcl_num_setScale(BclNumber n, size_t scale);

size_t
bcl_num_len(BclNumber n);

BclError
bcl_copy(BclNumber d, BclNumber s);

BclNumber
bcl_dup(BclNumber s);

BclError
bcl_bigdig(BclNumber n, BclBigDig* result);

BclError
bcl_bigdig_keep(BclNumber n, BclBigDig* result);

BclNumber
bcl_bigdig2num(BclBigDig val);

BclNumber
bcl_add(BclNumber a, BclNumber b);

BclNumber
bcl_add_keep(BclNumber a, BclNumber b);

BclNumber
bcl_sub(BclNumber a, BclNumber b);

BclNumber
bcl_sub_keep(BclNumber a, BclNumber b);

BclNumber
bcl_mul(BclNumber a, BclNumber b);

BclNumber
bcl_mul_keep(BclNumber a, BclNumber b);

BclNumber
bcl_div(BclNumber a, BclNumber b);

BclNumber
bcl_div_keep(BclNumber a, BclNumber b);

BclNumber
bcl_mod(BclNumber a, BclNumber b);

BclNumber
bcl_mod_keep(BclNumber a, BclNumber b);

BclNumber
bcl_pow(BclNumber a, BclNumber b);

BclNumber
bcl_pow_keep(BclNumber a, BclNumber b);

BclNumber
bcl_lshift(BclNumber a, BclNumber b);

BclNumber
bcl_lshift_keep(BclNumber a, BclNumber b);

BclNumber
bcl_rshift(BclNumber a, BclNumber b);

BclNumber
bcl_rshift_keep(BclNumber a, BclNumber b);

BclNumber
bcl_sqrt(BclNumber a);

BclNumber
bcl_sqrt_keep(BclNumber a);

BclError
bcl_divmod(BclNumber a, BclNumber b, BclNumber* c, BclNumber* d);

BclError
bcl_divmod_keep(BclNumber a, BclNumber b, BclNumber* c, BclNumber* d);

BclNumber
bcl_modexp(BclNumber a, BclNumber b, BclNumber c);

BclNumber
bcl_modexp_keep(BclNumber a, BclNumber b, BclNumber c);

ssize_t
bcl_cmp(BclNumber a, BclNumber b);

void
bcl_zero(BclNumber n);

void
bcl_one(BclNumber n);

BclNumber
bcl_parse(const char* restrict val);

char*
bcl_string(BclNumber n);

char*
bcl_string_keep(BclNumber n);

BclNumber
bcl_irand(BclNumber a);

BclNumber
bcl_irand_keep(BclNumber a);

BclNumber
bcl_frand(size_t places);

BclNumber
bcl_ifrand(BclNumber a, size_t places);

BclNumber
bcl_ifrand_keep(BclNumber a, size_t places);

BclError
bcl_rand_seedWithNum(BclNumber n);

BclError
bcl_rand_seedWithNum_keep(BclNumber n);

BclError
bcl_rand_seed(unsigned char seed[BCL_SEED_SIZE]);

void
bcl_rand_reseed(void);

BclNumber
bcl_rand_seed2num(void);

BclRandInt
bcl_rand_int(void);

BclRandInt
bcl_rand_bounded(BclRandInt bound);

#endif // BC_ENABLE_LIBRARY

#endif // BC_BCL_H