summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStefan Eßer <se@FreeBSD.org>2020-11-26 17:18:18 +0000
committerStefan Eßer <se@FreeBSD.org>2020-11-26 17:18:18 +0000
commit907a6834f73ccf2839853eb67a29726275ed04b4 (patch)
treebe26a5882ff196a219183e684e983bfbd23f9055 /src
parent04f2650428200cc540b6f8ce803224911d47d797 (diff)
Notes
Diffstat (limited to 'src')
-rw-r--r--src/args.c6
-rw-r--r--src/data.c45
-rw-r--r--src/file.c6
-rw-r--r--src/lang.c2
-rw-r--r--src/lex.c11
-rw-r--r--src/main.c3
-rw-r--r--src/num.c562
-rw-r--r--src/opt.c18
-rw-r--r--src/parse.c3
-rw-r--r--src/program.c83
-rw-r--r--src/read.c12
-rw-r--r--src/vector.c3
-rw-r--r--src/vm.c126
13 files changed, 331 insertions, 549 deletions
diff --git a/src/args.c b/src/args.c
index 4c9ad3b95549..029237627786 100644
--- a/src/args.c
+++ b/src/args.c
@@ -41,8 +41,10 @@
#include <unistd.h>
+#include <status.h>
#include <vector.h>
#include <read.h>
+#include <vm.h>
#include <args.h>
#include <opt.h>
@@ -107,7 +109,7 @@ void bc_args(int argc, char *argv[]) {
case 'e':
{
if (vm.no_exit_exprs)
- bc_vm_verr(BC_ERR_FATAL_OPTION, "-e (--expression)");
+ bc_vm_verr(BC_ERROR_FATAL_OPTION, "-e (--expression)");
bc_args_exprs(opts.optarg);
break;
}
@@ -117,7 +119,7 @@ void bc_args(int argc, char *argv[]) {
if (!strcmp(opts.optarg, "-")) vm.no_exit_exprs = true;
else {
if (vm.no_exit_exprs)
- bc_vm_verr(BC_ERR_FATAL_OPTION, "-f (--file)");
+ bc_vm_verr(BC_ERROR_FATAL_OPTION, "-f (--file)");
bc_args_file(opts.optarg);
}
break;
diff --git a/src/data.c b/src/data.c
index a3cf4dbda293..039c83e1cac1 100644
--- a/src/data.c
+++ b/src/data.c
@@ -43,8 +43,6 @@
#include <program.h>
#include <vm.h>
-#if !BC_ENABLE_LIBRARY
-
#if BC_ENABLED
const char bc_sig_msg[] = "\ninterrupt (type \"quit\" to exit)\n";
const uchar bc_sig_msg_len = (uchar) (sizeof(bc_sig_msg) - 1);
@@ -666,8 +664,11 @@ const char* bc_inst_names[] = {
};
#endif // BC_DEBUG_CODE
-const char bc_parse_zero[2] = "0";
-const char bc_parse_one[2] = "1";
+#if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
+
+const BcRandState bc_rand_multiplier = BC_RAND_MULTIPLIER;
+
+#endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
#if BC_ENABLED
const BcLexKeyword bc_lex_kws[] = {
@@ -711,6 +712,8 @@ const BcLexKeyword bc_lex_kws[] = {
const size_t bc_lex_kws_len = sizeof(bc_lex_kws) / sizeof(BcLexKeyword);
+const char* const bc_parse_const1 = "1";
+
// This is an array that corresponds to token types. An entry is
// true if the token is valid in an expression, false otherwise.
const uint8_t bc_parse_exprs[] = {
@@ -934,26 +937,11 @@ const uchar dc_parse_insts[] = {
};
#endif // DC_ENABLED
-#endif // !BC_ENABLE_LIBRARY
-
-#if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
-
-const BcRandState bc_rand_multiplier = BC_RAND_MULTIPLIER;
-
-#endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
-
#if BC_LONG_BIT >= 64
const BcDig bc_num_bigdigMax[] = {
709551616U,
446744073U,
- 18U,
-};
-const BcDig bc_num_bigdigMax2[] = {
- 768211456U,
- 374607431U,
- 938463463U,
- 282366920U,
- 340U,
+ 18U
};
#else // BC_LONG_BIT >= 64
const BcDig bc_num_bigdigMax[] = {
@@ -961,17 +949,12 @@ const BcDig bc_num_bigdigMax[] = {
9496U,
42U,
};
-const BcDig bc_num_bigdigMax2[] = {
- 1616U,
- 955U,
- 737U,
- 6744U,
- 1844U,
-};
#endif // BC_LONG_BIT >= 64
const size_t bc_num_bigdigMax_size = sizeof(bc_num_bigdigMax) / sizeof(BcDig);
-const size_t bc_num_bigdigMax2_size = sizeof(bc_num_bigdigMax2) / sizeof(BcDig);
+
+const char bc_parse_zero[] = "0";
+const char bc_parse_one[] = "1";
const char bc_num_hex_digits[] = "0123456789ABCDEF";
@@ -990,8 +973,6 @@ const BcBigDig bc_num_pow10[BC_BASE_DIGS + 1] = {
#endif // BC_BASE_DIGS > 4
};
-#if !BC_ENABLE_LIBRARY
-
const BcNumBinaryOp bc_program_ops[] = {
bc_num_pow, bc_num_mul, bc_num_div, bc_num_mod, bc_num_add, bc_num_sub,
#if BC_ENABLE_EXTRA_MATH
@@ -1000,7 +981,7 @@ const BcNumBinaryOp bc_program_ops[] = {
};
const BcNumBinaryOpReq bc_program_opReqs[] = {
- bc_num_powReq, bc_num_mulReq, bc_num_divReq, bc_num_divReq,
+ bc_num_powReq, bc_num_mulReq, bc_num_mulReq, bc_num_mulReq,
bc_num_addReq, bc_num_addReq,
#if BC_ENABLE_EXTRA_MATH
bc_num_placesReq, bc_num_placesReq, bc_num_placesReq,
@@ -1021,5 +1002,3 @@ const char bc_program_ready_msg[] = "ready for more input\n";
const size_t bc_program_ready_msg_len = sizeof(bc_program_ready_msg) - 1;
const char bc_program_esc_chars[] = "ab\\efnqrt";
const char bc_program_esc_seqs[] = "\a\b\\\\\f\n\"\r\t";
-
-#endif // !BC_ENABLE_LIBRARY
diff --git a/src/file.c b/src/file.c
index 1d4d390f89a4..01997399f452 100644
--- a/src/file.c
+++ b/src/file.c
@@ -41,8 +41,8 @@
#include <file.h>
#include <vm.h>
-static void bc_file_ultoa(unsigned long long val, char buf[BC_FILE_ULL_LENGTH])
-{
+void bc_file_ultoa(unsigned long long val, char buf[BC_FILE_ULL_LENGTH]) {
+
char buf2[BC_FILE_ULL_LENGTH];
size_t i, len;
@@ -105,7 +105,7 @@ void bc_file_flush(BcFile *restrict f) {
vm.status = (sig_atomic_t) s;
BC_VM_JMP;
}
- else bc_vm_err(BC_ERR_FATAL_IO_ERR);
+ else bc_vm_err(BC_ERROR_FATAL_IO_ERR);
}
}
diff --git a/src/lang.c b/src/lang.c
index bc34e7c269f8..bd287c75ee78 100644
--- a/src/lang.c
+++ b/src/lang.c
@@ -77,7 +77,7 @@ void bc_func_insert(BcFunc *f, BcProgram *p, char *name,
BcLoc *id = bc_vec_item(&f->autos, i);
if (BC_ERR(idx == id->loc && type == (BcType) id->idx)) {
const char *array = type == BC_TYPE_ARRAY ? "[]" : "";
- bc_vm_error(BC_ERR_PARSE_DUP_LOCAL, line, name, array);
+ bc_vm_error(BC_ERROR_PARSE_DUP_LOCAL, line, name, array);
}
}
diff --git a/src/lex.c b/src/lex.c
index d6f09f995a6a..2b705c8bc71b 100644
--- a/src/lex.c
+++ b/src/lex.c
@@ -38,13 +38,14 @@
#include <stdbool.h>
#include <string.h>
+#include <status.h>
#include <lex.h>
#include <vm.h>
#include <bc.h>
void bc_lex_invalidChar(BcLex *l, char c) {
l->t = BC_LEX_INVALID;
- bc_lex_verr(l, BC_ERR_PARSE_CHAR, c);
+ bc_lex_verr(l, BC_ERROR_PARSE_CHAR, c);
}
void bc_lex_lineComment(BcLex *l) {
@@ -68,7 +69,7 @@ void bc_lex_comment(BcLex *l) {
if (BC_ERR(!c || buf[i + 1] == '\0')) {
l->i = i;
- bc_lex_err(l, BC_ERR_PARSE_COMMENT);
+ bc_lex_err(l, BC_ERROR_PARSE_COMMENT);
}
end = buf[i + 1] == '/';
@@ -142,7 +143,7 @@ void bc_lex_number(BcLex *l, char start) {
if (c == 'e') {
#if BC_ENABLED
- if (BC_IS_POSIX) bc_lex_err(l, BC_ERR_POSIX_EXP_NUM);
+ if (BC_IS_POSIX) bc_lex_err(l, BC_ERROR_POSIX_EXP_NUM);
#endif // BC_ENABLED
bc_vec_push(&l->str, &c);
@@ -156,7 +157,7 @@ void bc_lex_number(BcLex *l, char start) {
}
if (BC_ERR(!BC_LEX_NUM_CHAR(c, false, true)))
- bc_lex_verr(l, BC_ERR_PARSE_CHAR, c);
+ bc_lex_verr(l, BC_ERROR_PARSE_CHAR, c);
l->i += bc_lex_num(l, 0, true);
}
@@ -207,7 +208,7 @@ void bc_lex_next(BcLex *l) {
l->last = l->t;
l->line += (l->i != 0 && l->buf[l->i - 1] == '\n');
- if (BC_ERR(l->last == BC_LEX_EOF)) bc_lex_err(l, BC_ERR_PARSE_EOF);
+ if (BC_ERR(l->last == BC_LEX_EOF)) bc_lex_err(l, BC_ERROR_PARSE_EOF);
l->t = BC_LEX_EOF;
diff --git a/src/main.c b/src/main.c
index 9c16e766e798..7e5e2905cf75 100644
--- a/src/main.c
+++ b/src/main.c
@@ -47,6 +47,9 @@
#include <bc.h>
#include <dc.h>
+char output_bufs[BC_VM_BUF_SIZE];
+BcVm vm;
+
int main(int argc, char *argv[]) {
int s;
diff --git a/src/num.c b/src/num.c
index 0b8823a3fec2..de5fa5c566fb 100644
--- a/src/num.c
+++ b/src/num.c
@@ -41,6 +41,7 @@
#include <setjmp.h>
#include <limits.h>
+#include <status.h>
#include <num.h>
#include <rand.h>
#include <vm.h>
@@ -52,11 +53,11 @@ static inline ssize_t bc_num_neg(size_t n, bool neg) {
}
ssize_t bc_num_cmpZero(const BcNum *n) {
- return bc_num_neg((n)->len != 0, BC_NUM_NEG(n));
+ return bc_num_neg((n)->len != 0, (n)->neg);
}
static inline size_t bc_num_int(const BcNum *n) {
- return n->len ? n->len - BC_NUM_RDX_VAL(n) : 0;
+ return n->len ? n->len - n->rdx : 0;
}
static void bc_num_expand(BcNum *restrict n, size_t req) {
@@ -80,9 +81,10 @@ static void bc_num_setToZero(BcNum *restrict n, size_t scale) {
assert(n != NULL);
n->scale = scale;
n->len = n->rdx = 0;
+ n->neg = false;
}
-void bc_num_zero(BcNum *restrict n) {
+static inline void bc_num_zero(BcNum *restrict n) {
bc_num_setToZero(n, 0);
}
@@ -96,11 +98,11 @@ static void bc_num_clean(BcNum *restrict n) {
while (BC_NUM_NONZERO(n) && !n->num[n->len - 1]) n->len -= 1;
- if (BC_NUM_ZERO(n)) n->rdx = 0;
- else {
- size_t rdx = BC_NUM_RDX_VAL(n);
- if (n->len < rdx) n->len = rdx;
+ if (BC_NUM_ZERO(n)) {
+ n->neg = false;
+ n->rdx = 0;
}
+ else if (n->len < n->rdx) n->len = n->rdx;
}
static size_t bc_num_log10(size_t i) {
@@ -124,7 +126,7 @@ static size_t bc_num_intDigits(const BcNum *n) {
static size_t bc_num_nonzeroLen(const BcNum *restrict n) {
size_t i, len = n->len;
- assert(len == BC_NUM_RDX_VAL(n));
+ assert(len == n->rdx);
for (i = len - 1; i < len && !n->num[i]; --i);
assert(i + 1 > 0);
return i + 1;
@@ -208,9 +210,9 @@ static void bc_num_mulArray(const BcNum *restrict a, BcBigDig b,
bc_num_clean(c);
- assert(!BC_NUM_NEG(c) || BC_NUM_NONZERO(c));
- assert(BC_NUM_RDX_VAL(c) <= c->len || !c->len);
- assert(!c->len || c->num[c->len - 1] || BC_NUM_RDX_VAL(c) == c->len);
+ assert(!c->neg || BC_NUM_NONZERO(c));
+ assert(c->rdx <= c->len || !c->len);
+ assert(!c->len || c->num[c->len - 1] || c->rdx == c->len);
}
static void bc_num_divArray(const BcNum *restrict a, BcBigDig b,
@@ -232,9 +234,9 @@ static void bc_num_divArray(const BcNum *restrict a, BcBigDig b,
bc_num_clean(c);
*rem = carry;
- assert(!BC_NUM_NEG(c) || BC_NUM_NONZERO(c));
- assert(BC_NUM_RDX_VAL(c) <= c->len || !c->len);
- assert(!c->len || c->num[c->len - 1] || BC_NUM_RDX_VAL(c) == c->len);
+ assert(!c->neg || BC_NUM_NONZERO(c));
+ assert(c->rdx <= c->len || !c->len);
+ assert(!c->len || c->num[c->len - 1] || c->rdx == c->len);
}
static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
@@ -248,7 +250,7 @@ static ssize_t bc_num_compare(const BcDig *restrict a, const BcDig *restrict b,
ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
- size_t i, min, a_int, b_int, diff, ardx, brdx;
+ size_t i, min, a_int, b_int, diff;
BcDig *max_num, *min_num;
bool a_max, neg = false;
ssize_t cmp;
@@ -256,13 +258,13 @@ ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
assert(a != NULL && b != NULL);
if (a == b) return 0;
- if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !BC_NUM_NEG(b));
+ if (BC_NUM_ZERO(a)) return bc_num_neg(b->len != 0, !b->neg);
if (BC_NUM_ZERO(b)) return bc_num_cmpZero(a);
- if (BC_NUM_NEG(a)) {
- if (BC_NUM_NEG(b)) neg = true;
+ if (a->neg) {
+ if (b->neg) neg = true;
else return -1;
}
- else if (BC_NUM_NEG(b)) return 1;
+ else if (b->neg) return 1;
a_int = bc_num_int(a);
b_int = bc_num_int(b);
@@ -270,19 +272,17 @@ ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
if (a_int) return neg ? -((ssize_t) a_int) : (ssize_t) a_int;
- ardx = BC_NUM_RDX_VAL(a);
- brdx = BC_NUM_RDX_VAL(b);
- a_max = (ardx > brdx);
+ a_max = (a->rdx > b->rdx);
if (a_max) {
- min = brdx;
- diff = ardx - brdx;
+ min = b->rdx;
+ diff = a->rdx - b->rdx;
max_num = a->num + diff;
min_num = b->num;
}
else {
- min = ardx;
- diff = brdx - ardx;
+ min = a->rdx;
+ diff = b->rdx - a->rdx;
max_num = b->num + diff;
min_num = a->num;
}
@@ -300,16 +300,15 @@ ssize_t bc_num_cmp(const BcNum *a, const BcNum *b) {
void bc_num_truncate(BcNum *restrict n, size_t places) {
- size_t nrdx, places_rdx;
+ size_t places_rdx;
if (!places) return;
- nrdx = BC_NUM_RDX_VAL(n);
- places_rdx = nrdx ? nrdx - BC_NUM_RDX(n->scale - places) : 0;
+ places_rdx = n->rdx ? n->rdx - BC_NUM_RDX(n->scale - places) : 0;
assert(places <= n->scale && (BC_NUM_ZERO(n) || places_rdx <= n->len));
n->scale -= places;
- BC_NUM_RDX_SET(n, nrdx - places_rdx);
+ n->rdx -= places_rdx;
if (BC_NUM_NONZERO(n)) {
@@ -329,9 +328,9 @@ void bc_num_truncate(BcNum *restrict n, size_t places) {
}
}
-void bc_num_extend(BcNum *restrict n, size_t places) {
+static void bc_num_extend(BcNum *restrict n, size_t places) {
- size_t nrdx, places_rdx;
+ size_t places_rdx;
if (!places) return;
if (BC_NUM_ZERO(n)) {
@@ -339,8 +338,7 @@ void bc_num_extend(BcNum *restrict n, size_t places) {
return;
}
- nrdx = BC_NUM_RDX_VAL(n);
- places_rdx = BC_NUM_RDX(places + n->scale) - nrdx;
+ places_rdx = BC_NUM_RDX(places + n->scale) - n->rdx;
if (places_rdx) {
bc_num_expand(n, bc_vm_growSize(n->len, places_rdx));
@@ -348,11 +346,11 @@ void bc_num_extend(BcNum *restrict n, size_t places) {
memset(n->num, 0, BC_NUM_SIZE(places_rdx));
}
- BC_NUM_RDX_SET(n, nrdx + places_rdx);
+ n->rdx += places_rdx;
n->scale += places;
n->len += places_rdx;
- assert(BC_NUM_RDX_VAL(n) == BC_NUM_RDX(n->scale));
+ assert(n->rdx == BC_NUM_RDX(n->scale));
}
static void bc_num_retireMul(BcNum *restrict n, size_t scale,
@@ -362,7 +360,7 @@ static void bc_num_retireMul(BcNum *restrict n, size_t scale,
else bc_num_truncate(n, n->scale - scale);
bc_num_clean(n);
- if (BC_NUM_NONZERO(n)) n->rdx = BC_NUM_NEG_VAL(n, !neg1 != !neg2);
+ if (BC_NUM_NONZERO(n)) n->neg = (!neg1 != !neg2);
}
static void bc_num_split(const BcNum *restrict n, size_t idx,
@@ -375,9 +373,7 @@ static void bc_num_split(const BcNum *restrict n, size_t idx,
b->len = n->len - idx;
a->len = idx;
- a->scale = b->scale = 0;
- BC_NUM_RDX_SET(a, 0);
- BC_NUM_RDX_SET(b, 0);
+ a->scale = a->rdx = b->scale = b->rdx = 0;
assert(a->cap >= a->len);
assert(b->cap >= b->len);
@@ -396,7 +392,7 @@ static size_t bc_num_shiftZero(BcNum *restrict n) {
size_t i;
- assert(!BC_NUM_RDX_VAL(n) || BC_NUM_ZERO(n));
+ assert(!n->rdx || BC_NUM_ZERO(n));
for (i = 0; i < n->len && !n->num[i]; ++i);
@@ -442,7 +438,7 @@ static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
if (!places) return;
if (places > n->scale) {
size_t size = bc_vm_growSize(BC_NUM_RDX(places - n->scale), n->len);
- if (size > SIZE_MAX - 1) bc_vm_err(BC_ERR_MATH_OVERFLOW);
+ if (size > SIZE_MAX - 1) bc_vm_err(BC_ERROR_MATH_OVERFLOW);
}
if (BC_NUM_ZERO(n)) {
if (n->scale >= places) n->scale -= places;
@@ -456,9 +452,7 @@ static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
if (n->scale) {
- size_t nrdx = BC_NUM_RDX_VAL(n);
-
- if (nrdx >= places_rdx) {
+ if (n->rdx >= places_rdx) {
size_t mod = n->scale % BC_BASE_DIGS, revdig;
@@ -468,7 +462,7 @@ static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
if (mod + revdig > BC_BASE_DIGS) places_rdx = 1;
else places_rdx = 0;
}
- else places_rdx -= nrdx;
+ else places_rdx -= n->rdx;
}
if (places_rdx) {
@@ -478,13 +472,10 @@ static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
n->len += places_rdx;
}
- if (places > n->scale) {
- n->scale = 0;
- BC_NUM_RDX_SET(n, 0);
- }
+ if (places > n->scale) n->scale = n->rdx = 0;
else {
n->scale -= places;
- BC_NUM_RDX_SET(n, BC_NUM_RDX(n->scale));
+ n->rdx = BC_NUM_RDX(n->scale);
}
if (shift) bc_num_shift(n, BC_BASE_DIGS - dig);
@@ -492,7 +483,7 @@ static void bc_num_shiftLeft(BcNum *restrict n, size_t places) {
bc_num_clean(n);
}
-void bc_num_shiftRight(BcNum *restrict n, size_t places) {
+static void bc_num_shiftRight(BcNum *restrict n, size_t places) {
BcBigDig dig;
size_t places_rdx, scale, scale_mod, int_len, expand;
@@ -529,18 +520,17 @@ void bc_num_shiftRight(BcNum *restrict n, size_t places) {
bc_num_expand(n, bc_vm_growSize(expand, n->len));
memset(n->num + n->len, 0, BC_NUM_SIZE(expand));
n->len += expand;
- n->scale = 0;
- BC_NUM_RDX_SET(n, 0);
+ n->scale = n->rdx = 0;
if (shift) bc_num_shift(n, dig);
n->scale = scale + places;
- BC_NUM_RDX_SET(n, BC_NUM_RDX(n->scale));
+ n->rdx = BC_NUM_RDX(n->scale);
bc_num_clean(n);
- assert(BC_NUM_RDX_VAL(n) <= n->len && n->len <= n->cap);
- assert(BC_NUM_RDX_VAL(n) == BC_NUM_RDX(n->scale));
+ assert(n->rdx <= n->len && n->len <= n->cap);
+ assert(n->rdx == BC_NUM_RDX(n->scale));
}
static void bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
@@ -560,7 +550,7 @@ static void bc_num_inv(BcNum *a, BcNum *b, size_t scale) {
static void bc_num_intop(const BcNum *a, const BcNum *b, BcNum *restrict c,
BcBigDig *v)
{
- if (BC_ERR(BC_NUM_RDX_VAL(b))) bc_vm_err(BC_ERR_MATH_NON_INTEGER);
+ if (BC_ERR(b->rdx)) bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
bc_num_copy(c, a);
bc_num_bigdig(b, v);
}
@@ -570,8 +560,8 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
BcDig *ptr_c, *ptr_l, *ptr_r;
size_t i, min_rdx, max_rdx, diff, a_int, b_int, min_len, max_len, max_int;
- size_t len_l, len_r, ardx, brdx;
- bool b_neg, do_sub, do_rev_sub, carry, c_neg;
+ size_t len_l, len_r;
+ bool b_neg, do_sub, do_rev_sub, carry;
// Because this function doesn't need to use scale (per the bc spec),
// I am hijacking it to say whether it's doing an add or a subtract.
@@ -583,25 +573,23 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
}
if (BC_NUM_ZERO(a)) {
bc_num_copy(c, b);
- c->rdx = BC_NUM_NEG_VAL(c, BC_NUM_NEG(b) != sub);
+ c->neg = (b->neg != sub);
return;
}
// Invert sign of b if it is to be subtracted. This operation must
// preced the tests for any of the operands being zero.
- b_neg = (BC_NUM_NEG(b) != sub);
+ b_neg = (b->neg != sub);
// Actually add the numbers if their signs are equal, else subtract.
- do_sub = (BC_NUM_NEG(a) != b_neg);
+ do_sub = (a->neg != b_neg);
a_int = bc_num_int(a);
b_int = bc_num_int(b);
max_int = BC_MAX(a_int, b_int);
- ardx = BC_NUM_RDX_VAL(a);
- brdx = BC_NUM_RDX_VAL(b);
- min_rdx = BC_MIN(ardx, brdx);
- max_rdx = BC_MAX(ardx, brdx);
+ min_rdx = BC_MIN(a->rdx, b->rdx);
+ max_rdx = BC_MAX(a->rdx, b->rdx);
diff = max_rdx - min_rdx;
max_len = max_int + max_rdx;
@@ -610,7 +598,7 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
// Check whether b has to be subtracted from a or a from b.
if (a_int != b_int) do_rev_sub = (a_int < b_int);
- else if (ardx > brdx)
+ else if (a->rdx > b->rdx)
do_rev_sub = (bc_num_compare(a->num + diff, b->num, b->len) < 0);
else
do_rev_sub = (bc_num_compare(a->num, b->num + diff, a->len) <= 0);
@@ -646,9 +634,9 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
// If the rdx values of the operands do not match, the result will
// have low end elements that are the positive or negative trailing
// elements of the operand with higher rdx value.
- if ((ardx > brdx) != do_rev_sub) {
+ if ((a->rdx > b->rdx) != do_rev_sub) {
- // !do_rev_sub && ardx > brdx || do_rev_sub && brdx > ardx
+ // !do_rev_sub && a->rdx > b->rdx || do_rev_sub && b->rdx > a->rdx
// The left operand has BcDig values that need to be copied,
// either from a or from b (in case of a reversed subtraction).
memcpy(ptr_c, ptr_l, BC_NUM_SIZE(diff));
@@ -661,14 +649,14 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
// or subtracted from zero (in case of a subtraction).
if (do_sub) {
- // do_sub (do_rev_sub && ardx > brdx ||
- // !do_rev_sub && brdx > ardx)
+ // do_sub (do_rev_sub && a->rdx > b->rdx ||
+ // !do_rev_sub && b->rdx > a->rdx)
for (i = 0; i < diff; i++)
ptr_c[i] = bc_num_subDigits(0, ptr_r[i], &carry);
}
else {
- // !do_sub && brdx > ardx
+ // !do_sub && b->rdx > a->rdx
memcpy(ptr_c, ptr_r, BC_NUM_SIZE(diff));
}
@@ -703,9 +691,9 @@ static void bc_num_as(BcNum *a, BcNum *b, BcNum *restrict c, size_t sub) {
// The result has the same sign as a, unless the operation was a
// reverse subtraction (b - a).
- c_neg = BC_NUM_NEG(a) != (do_sub && do_rev_sub);
- BC_NUM_RDX_SET_NEG(c, max_rdx, c_neg);
+ c->neg = (a->neg != (do_sub && do_rev_sub));
c->len = max_len;
+ c->rdx = max_rdx;
c->scale = BC_MAX(a->scale, b->scale);
bc_num_clean(c);
@@ -718,7 +706,7 @@ static void bc_num_m_simp(const BcNum *a, const BcNum *b, BcNum *restrict c)
BcBigDig sum = 0, carry = 0;
assert(sizeof(sum) >= sizeof(BcDig) * 2);
- assert(!BC_NUM_RDX_VAL(a) && !BC_NUM_RDX_VAL(b));
+ assert(!a->rdx && !b->rdx);
clen = bc_vm_growSize(alen, blen);
bc_num_expand(c, bc_vm_growSize(clen, 1));
@@ -763,7 +751,7 @@ static void bc_num_shiftAddSub(BcNum *restrict n, const BcNum *restrict a,
size_t shift, BcNumShiftAddOp op)
{
assert(n->len >= shift + a->len);
- assert(!BC_NUM_RDX_VAL(n) && !BC_NUM_RDX_VAL(a));
+ assert(!n->rdx && !a->rdx);
op(n->num + shift, a->num, a->len);
}
@@ -780,7 +768,7 @@ static void bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
if (BC_NUM_ZERO(a) || BC_NUM_ZERO(b)) return;
if (aone || BC_NUM_ONE(b)) {
bc_num_copy(c, aone ? b : a);
- if ((aone && BC_NUM_NEG(a)) || BC_NUM_NEG(b)) BC_NUM_NEG_TGL(c);
+ if ((aone && a->neg) || b->neg) c->neg = !c->neg;
return;
}
if (a->len < BC_NUM_KARATSUBA_LEN || b->len < BC_NUM_KARATSUBA_LEN) {
@@ -832,9 +820,6 @@ static void bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
if (BC_NUM_NONZERO(&h1) && BC_NUM_NONZERO(&h2)) {
- assert(BC_NUM_RDX_VALID_NP(h1));
- assert(BC_NUM_RDX_VALID_NP(h2));
-
bc_num_m(&h1, &h2, &z2, 0);
bc_num_clean(&z2);
@@ -844,9 +829,6 @@ static void bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
if (BC_NUM_NONZERO(&l1) && BC_NUM_NONZERO(&l2)) {
- assert(BC_NUM_RDX_VALID_NP(l1));
- assert(BC_NUM_RDX_VALID_NP(l2));
-
bc_num_m(&l1, &l2, &z0, 0);
bc_num_clean(&z0);
@@ -856,14 +838,10 @@ static void bc_num_k(BcNum *a, BcNum *b, BcNum *restrict c) {
if (BC_NUM_NONZERO(&m1) && BC_NUM_NONZERO(&m2)) {
- assert(BC_NUM_RDX_VALID_NP(m1));
- assert(BC_NUM_RDX_VALID_NP(m1));
-
bc_num_m(&m1, &m2, &z1, 0);
bc_num_clean(&z1);
- op = (BC_NUM_NEG_NP(m1) != BC_NUM_NEG_NP(m2)) ?
- bc_num_subArrays : bc_num_addArrays;
+ op = (m1.neg != m2.neg) ? bc_num_subArrays : bc_num_addArrays;
bc_num_shiftAddSub(c, &z1, max2, op);
}
@@ -882,9 +860,6 @@ static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
BcNum cpa, cpb;
size_t ascale, bscale, ardx, brdx, azero = 0, bzero = 0, zero, len, rscale;
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
-
bc_num_zero(c);
ascale = a->scale;
bscale = b->scale;
@@ -910,19 +885,15 @@ static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
bc_num_mulArray(operand, dig, c);
- if (BC_NUM_NONZERO(c))
- c->rdx = BC_NUM_NEG_VAL(c, BC_NUM_NEG(a) != BC_NUM_NEG(b));
+ if (BC_NUM_NONZERO(c)) c->neg = (a->neg != b->neg);
return;
}
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
-
BC_SIG_LOCK;
- bc_num_init(&cpa, a->len + BC_NUM_RDX_VAL(a));
- bc_num_init(&cpb, b->len + BC_NUM_RDX_VAL(b));
+ bc_num_init(&cpa, a->len + a->rdx);
+ bc_num_init(&cpb, b->len + b->rdx);
BC_SETJMP_LOCKED(err);
@@ -931,19 +902,12 @@ static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
bc_num_copy(&cpa, a);
bc_num_copy(&cpb, b);
- assert(BC_NUM_RDX_VALID_NP(cpa));
- assert(BC_NUM_RDX_VALID_NP(cpb));
-
- BC_NUM_NEG_CLR_NP(cpa);
- BC_NUM_NEG_CLR_NP(cpb);
+ cpa.neg = cpb.neg = false;
- assert(BC_NUM_RDX_VALID_NP(cpa));
- assert(BC_NUM_RDX_VALID_NP(cpb));
-
- ardx = BC_NUM_RDX_VAL_NP(cpa) * BC_BASE_DIGS;
+ ardx = cpa.rdx * BC_BASE_DIGS;
bc_num_shiftLeft(&cpa, ardx);
- brdx = BC_NUM_RDX_VAL_NP(cpb) * BC_BASE_DIGS;
+ brdx = cpb.rdx * BC_BASE_DIGS;
bc_num_shiftLeft(&cpb, brdx);
// We need to reset the jump here because azero and bzero are used in the
@@ -972,7 +936,7 @@ static void bc_num_m(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
bc_num_shiftLeft(c, (len - c->len) * BC_BASE_DIGS);
bc_num_shiftRight(c, ardx + brdx);
- bc_num_retireMul(c, scale, BC_NUM_NEG(a), BC_NUM_NEG(b));
+ bc_num_retireMul(c, scale, a->neg, b->neg);
err:
BC_SIG_MAYLOCK;
@@ -1033,7 +997,7 @@ static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
bc_num_expand(c, a->len);
memset(c->num, 0, c->cap * sizeof(BcDig));
- BC_NUM_RDX_SET(c, BC_NUM_RDX_VAL(a));
+ c->rdx = a->rdx;
c->scale = a->scale;
c->len = a->len;
@@ -1066,7 +1030,7 @@ static void bc_num_d_long(BcNum *restrict a, BcNum *restrict b,
memset(c->num, 0, BC_NUM_SIZE(c->cap));
assert(c->scale >= scale);
- rdx = BC_NUM_RDX_VAL(c) - BC_NUM_RDX(scale);
+ rdx = c->rdx - BC_NUM_RDX(scale);
BC_SIG_LOCK;
@@ -1130,27 +1094,27 @@ err:
static void bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
- size_t len, cpardx;
+ size_t len;
BcNum cpa, cpb;
- if (BC_NUM_ZERO(b)) bc_vm_err(BC_ERR_MATH_DIVIDE_BY_ZERO);
+ if (BC_NUM_ZERO(b)) bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
if (BC_NUM_ZERO(a)) {
bc_num_setToZero(c, scale);
return;
}
if (BC_NUM_ONE(b)) {
bc_num_copy(c, a);
- bc_num_retireMul(c, scale, BC_NUM_NEG(a), BC_NUM_NEG(b));
+ bc_num_retireMul(c, scale, a->neg, b->neg);
return;
}
- if (!BC_NUM_RDX_VAL(a) && !BC_NUM_RDX_VAL(b) && b->len == 1 && !scale) {
+ if (!a->rdx && !b->rdx && b->len == 1 && !scale) {
BcBigDig rem;
bc_num_divArray(a, (BcBigDig) b->num[0], c, &rem);
- bc_num_retireMul(c, scale, BC_NUM_NEG(a), BC_NUM_NEG(b));
+ bc_num_retireMul(c, scale, a->neg, b->neg);
return;
}
- len = bc_num_divReq(a, b, scale);
+ len = bc_num_mulReq(a, b, scale);
BC_SIG_LOCK;
@@ -1169,18 +1133,15 @@ static void bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
bc_num_extend(&cpa, (len - cpa.len) * BC_BASE_DIGS);
}
- cpardx = BC_NUM_RDX_VAL_NP(cpa);
- cpa.scale = cpardx * BC_BASE_DIGS;
+ cpa.scale = cpa.rdx * BC_BASE_DIGS;
bc_num_extend(&cpa, b->scale);
- cpardx = BC_NUM_RDX_VAL_NP(cpa) - BC_NUM_RDX(b->scale);
- BC_NUM_RDX_SET_NP(cpa, cpardx);
- cpa.scale = cpardx * BC_BASE_DIGS;
+ cpa.rdx -= BC_NUM_RDX(b->scale);
+ cpa.scale = cpa.rdx * BC_BASE_DIGS;
if (scale > cpa.scale) {
bc_num_extend(&cpa, scale);
- cpardx = BC_NUM_RDX_VAL_NP(cpa);
- cpa.scale = cpardx * BC_BASE_DIGS;
+ cpa.scale = cpa.rdx * BC_BASE_DIGS;
}
if (cpa.cap == cpa.len) bc_num_expand(&cpa, bc_vm_growSize(cpa.len, 1));
@@ -1188,14 +1149,13 @@ static void bc_num_d(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
// We want an extra zero in front to make things simpler.
cpa.num[cpa.len++] = 0;
- if (cpardx == cpa.len) cpa.len = bc_num_nonzeroLen(&cpa);
- if (BC_NUM_RDX_VAL_NP(cpb) == cpb.len) cpb.len = bc_num_nonzeroLen(&cpb);
- cpb.scale = 0;
- BC_NUM_RDX_SET_NP(cpb, 0);
+ if (cpa.rdx == cpa.len) cpa.len = bc_num_nonzeroLen(&cpa);
+ if (cpb.rdx == cpb.len) cpb.len = bc_num_nonzeroLen(&cpb);
+ cpb.scale = cpb.rdx = 0;
bc_num_d_long(&cpa, &cpb, c, scale);
- bc_num_retireMul(c, scale, BC_NUM_NEG(a), BC_NUM_NEG(b));
+ bc_num_retireMul(c, scale, a->neg, b->neg);
err:
BC_SIG_MAYLOCK;
@@ -1210,7 +1170,7 @@ static void bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
BcNum temp;
bool neg;
- if (BC_NUM_ZERO(b)) bc_vm_err(BC_ERR_MATH_DIVIDE_BY_ZERO);
+ if (BC_NUM_ZERO(b)) bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
if (BC_NUM_ZERO(a)) {
bc_num_setToZero(c, ts);
bc_num_setToZero(d, ts);
@@ -1229,17 +1189,14 @@ static void bc_num_r(BcNum *a, BcNum *b, BcNum *restrict c,
if (scale) scale = ts + 1;
- assert(BC_NUM_RDX_VALID(c));
- assert(BC_NUM_RDX_VALID(b));
-
bc_num_m(c, b, &temp, scale);
bc_num_sub(a, &temp, d, scale);
if (ts > d->scale && BC_NUM_NONZERO(d)) bc_num_extend(d, ts - d->scale);
- neg = BC_NUM_NEG(d);
- bc_num_retireMul(d, ts, BC_NUM_NEG(a), BC_NUM_NEG(b));
- d->rdx = BC_NUM_NEG_VAL(d, BC_NUM_NONZERO(d) ? neg : false);
+ neg = d->neg;
+ bc_num_retireMul(d, ts, a->neg, b->neg);
+ d->neg = BC_NUM_NONZERO(d) ? neg : false;
err:
BC_SIG_MAYLOCK;
@@ -1278,29 +1235,29 @@ static void bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
size_t i, powrdx, resrdx;
bool neg, zero;
- if (BC_ERR(BC_NUM_RDX_VAL(b))) bc_vm_err(BC_ERR_MATH_NON_INTEGER);
+ if (BC_ERR(b->rdx)) bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
if (BC_NUM_ZERO(b)) {
bc_num_one(c);
return;
}
if (BC_NUM_ZERO(a)) {
- if (BC_NUM_NEG(b)) bc_vm_err(BC_ERR_MATH_DIVIDE_BY_ZERO);
+ if (b->neg) bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
bc_num_setToZero(c, scale);
return;
}
if (BC_NUM_ONE(b)) {
- if (!BC_NUM_NEG(b)) bc_num_copy(c, a);
+ if (!b->neg) bc_num_copy(c, a);
else bc_num_inv(a, c, scale);
return;
}
BC_SIG_LOCK;
- neg = BC_NUM_NEG(b);
- BC_NUM_NEG_CLR(b);
+ neg = b->neg;
+ b->neg = false;
bc_num_bigdig(b, &pow);
- b->rdx = BC_NUM_NEG_VAL(b, neg);
+ b->neg = neg;
bc_num_createCopy(&copy, a);
@@ -1315,7 +1272,6 @@ static void bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
for (powrdx = a->scale; !(pow & 1); pow >>= 1) {
powrdx <<= 1;
- assert(BC_NUM_RDX_VALID_NP(copy));
bc_num_mul(&copy, &copy, &copy, powrdx);
}
@@ -1325,13 +1281,10 @@ static void bc_num_p(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
while (pow >>= 1) {
powrdx <<= 1;
- assert(BC_NUM_RDX_VALID_NP(copy));
bc_num_mul(&copy, &copy, &copy, powrdx);
if (pow & 1) {
resrdx += powrdx;
- assert(BC_NUM_RDX_VALID(c));
- assert(BC_NUM_RDX_VALID_NP(copy));
bc_num_mul(c, &copy, c, resrdx);
}
}
@@ -1391,14 +1344,11 @@ static void bc_num_right(BcNum *a, BcNum *b, BcNum *restrict c, size_t scale) {
static void bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
BcNumBinaryOp op, size_t req)
{
- BcNum *ptr_a, *ptr_b, num2;
+ BcNum num2, *ptr_a, *ptr_b;
bool init = false;
assert(a != NULL && b != NULL && c != NULL && op != NULL);
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
-
BC_SIG_LOCK;
if (c == a) {
@@ -1408,9 +1358,7 @@ static void bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
memcpy(ptr_a, c, sizeof(BcNum));
init = true;
}
- else {
- ptr_a = a;
- }
+ else ptr_a = a;
if (c == b) {
@@ -1421,9 +1369,7 @@ static void bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
init = true;
}
}
- else {
- ptr_b = b;
- }
+ else ptr_b = b;
if (init) {
@@ -1439,10 +1385,9 @@ static void bc_num_binary(BcNum *a, BcNum *b, BcNum *c, size_t scale,
op(ptr_a, ptr_b, c, scale);
- assert(!BC_NUM_NEG(c) || BC_NUM_NONZERO(c));
- assert(BC_NUM_RDX_VAL(c) <= c->len || !c->len);
- assert(BC_NUM_RDX_VALID(c));
- assert(!c->len || c->num[c->len - 1] || BC_NUM_RDX_VAL(c) == c->len);
+ assert(!c->neg || BC_NUM_NONZERO(c));
+ assert(c->rdx <= c->len || !c->len);
+ assert(!c->len || c->num[c->len - 1] || c->rdx == c->len);
err:
if (init) {
@@ -1452,8 +1397,8 @@ err:
}
}
-#if !defined(NDEBUG) || BC_ENABLE_LIBRARY
-bool bc_num_strValid(const char *restrict val) {
+#ifndef NDEBUG
+static bool bc_num_strValid(const char *val) {
bool radix = false;
size_t i, len = strlen(val);
@@ -1477,7 +1422,7 @@ bool bc_num_strValid(const char *restrict val) {
return true;
}
-#endif // !defined(NDEBUG) || BC_ENABLE_LIBRARY
+#endif // NDEBUG
static BcBigDig bc_num_parseChar(char c, size_t base_t) {
@@ -1514,8 +1459,8 @@ static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
n->scale = (size_t) (rdx * (((uintptr_t) (val + len)) -
(((uintptr_t) ptr) + 1)));
+ n->rdx = BC_NUM_RDX(n->scale);
- BC_NUM_RDX_SET(n, BC_NUM_RDX(n->scale));
i = len - (ptr == val ? 0 : i) - rdx;
temp = BC_NUM_ROUND_POW(i);
mod = n->scale % BC_BASE_DIGS;
@@ -1525,11 +1470,7 @@ static void bc_num_parseDecimal(BcNum *restrict n, const char *restrict val) {
bc_num_expand(n, n->len);
memset(n->num, 0, BC_NUM_SIZE(n->len));
- if (zero) {
- // I think I can set rdx directly to zero here because n should be a
- // new number with sign set to false.
- n->len = n->rdx = 0;
- }
+ if (zero) n->len = n->rdx = 0;
else {
BcBigDig exp, pow;
@@ -1610,8 +1551,6 @@ static void bc_num_parseBase(BcNum *restrict n, const char *restrict val,
for (i += 1, digs = 0; i < len && (c = val[i]); ++i, ++digs) {
- size_t rdx;
-
v = bc_num_parseChar(c, base);
bc_num_mulArray(&result1, base, &result2);
@@ -1620,9 +1559,7 @@ static void bc_num_parseBase(BcNum *restrict n, const char *restrict val,
bc_num_add(&result2, &temp, &result1, 0);
bc_num_mulArray(m1, base, m2);
- rdx = BC_NUM_RDX_VAL(m2);
-
- if (m2->len < rdx) m2->len = rdx;
+ if (m2->len < m2->rdx) m2->len = m2->rdx;
ptr = m1;
m1 = m2;
@@ -1652,13 +1589,11 @@ int_err:
BC_LONGJMP_CONT;
}
-static inline void bc_num_printNewline(void) {
-#if !BC_ENABLE_LIBRARY
+static void bc_num_printNewline(void) {
if (vm.nchars >= vm.line_len - 1) {
bc_vm_putchar('\\');
bc_vm_putchar('\n');
}
-#endif // !BC_ENABLE_LIBRARY
}
static void bc_num_putchar(int c) {
@@ -1666,14 +1601,14 @@ static void bc_num_putchar(int c) {
bc_vm_putchar(c);
}
-#if DC_ENABLED && !BC_ENABLE_LIBRARY
+#if DC_ENABLED
static void bc_num_printChar(size_t n, size_t len, bool rdx) {
BC_UNUSED(rdx);
BC_UNUSED(len);
assert(len == 1);
bc_vm_putchar((uchar) n);
}
-#endif // DC_ENABLED && !BC_ENABLE_LIBRARY
+#endif // DC_ENABLED
static void bc_num_printDigits(size_t n, size_t len, bool rdx) {
@@ -1703,11 +1638,11 @@ static void bc_num_printHex(size_t n, size_t len, bool rdx) {
static void bc_num_printDecimal(const BcNum *restrict n) {
- size_t i, j, rdx = BC_NUM_RDX_VAL(n);
+ size_t i, j, rdx = n->rdx;
bool zero = true;
size_t buffer[BC_BASE_DIGS];
- if (BC_NUM_NEG(n)) bc_num_putchar('-');
+ if (n->neg) bc_num_putchar('-');
for (i = n->len - 1; i < n->len; --i) {
@@ -1737,9 +1672,9 @@ static void bc_num_printDecimal(const BcNum *restrict n) {
#if BC_ENABLE_EXTRA_MATH
static void bc_num_printExponent(const BcNum *restrict n, bool eng) {
- size_t places, mod, nrdx = BC_NUM_RDX_VAL(n);
- bool neg = (n->len <= nrdx);
+ bool neg = (n->len <= n->rdx);
BcNum temp, exp;
+ size_t places, mod;
BcDig digs[BC_NUM_BIGDIG_LOG10];
BC_SIG_LOCK;
@@ -1761,7 +1696,7 @@ static void bc_num_printExponent(const BcNum *restrict n, bool eng) {
else break;
}
- places += (nrdx - (idx + 1)) * BC_BASE_DIGS;
+ places += (n->rdx - (idx + 1)) * BC_BASE_DIGS;
mod = places % 3;
if (eng && mod != 0) places += 3 - mod;
@@ -1864,7 +1799,7 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base,
BcVec stack;
BcNum intp, fracp1, fracp2, digit, flen1, flen2, *n1, *n2, *temp;
BcBigDig dig = 0, *ptr, acc, exp;
- size_t i, j, nrdx;
+ size_t i, j;
bool radix;
BcDig digit_digs[BC_NUM_BIGDIG_LOG10 + 1];
@@ -1908,12 +1843,10 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base,
// happens and bc_num_printFixup() where the inner loop, or actual
// conversion, happens.
- nrdx = BC_NUM_RDX_VAL(n);
-
BC_SIG_LOCK;
bc_vec_init(&stack, sizeof(BcBigDig), NULL);
- bc_num_init(&fracp1, nrdx);
+ bc_num_init(&fracp1, n->rdx);
bc_num_createCopy(&intp, n);
@@ -1978,7 +1911,7 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base,
BC_UNSETJMP;
- bc_num_init(&fracp2, nrdx);
+ bc_num_init(&fracp2, n->rdx);
bc_num_setup(&digit, digit_digs, sizeof(digit_digs) / sizeof(BcDig));
bc_num_init(&flen1, BC_NUM_BIGDIG_LOG10);
bc_num_init(&flen2, BC_NUM_BIGDIG_LOG10);
@@ -1994,16 +1927,13 @@ static void bc_num_printNum(BcNum *restrict n, BcBigDig base,
n2 = &flen2;
fracp2.scale = n->scale;
- BC_NUM_RDX_SET_NP(fracp2, BC_NUM_RDX(fracp2.scale));
+ fracp2.rdx = BC_NUM_RDX(fracp2.scale);
while (bc_num_intDigits(n1) < n->scale + 1) {
bc_num_expand(&fracp2, fracp1.len + 1);
bc_num_mulArray(&fracp1, base, &fracp2);
-
- nrdx = BC_NUM_RDX_VAL_NP(fracp2);
-
- if (fracp2.len < nrdx) fracp2.len = nrdx;
+ if (fracp2.len < fracp2.rdx) fracp2.len = fracp2.rdx;
// fracp is guaranteed to be non-negative and small enough.
bc_num_bigdig2(&fracp2, &dig);
@@ -2037,11 +1967,11 @@ static void bc_num_printBase(BcNum *restrict n, BcBigDig base) {
size_t width;
BcNumDigitOp print;
- bool neg = BC_NUM_NEG(n);
+ bool neg = n->neg;
if (neg) bc_num_putchar('-');
- BC_NUM_NEG_CLR(n);
+ n->neg = false;
if (base <= BC_NUM_MAX_POSIX_IBASE) {
width = 1;
@@ -2054,14 +1984,14 @@ static void bc_num_printBase(BcNum *restrict n, BcBigDig base) {
}
bc_num_printNum(n, base, width, print);
- n->rdx = BC_NUM_NEG_VAL(n, neg);
+ n->neg = neg;
}
-#if DC_ENABLED && !BC_ENABLE_LIBRARY
+#if DC_ENABLED
void bc_num_stream(BcNum *restrict n, BcBigDig base) {
bc_num_printNum(n, base, 1, bc_num_printChar);
}
-#endif // DC_ENABLED && !BC_ENABLE_LIBRARY
+#endif // DC_ENABLED
void bc_num_setup(BcNum *restrict n, BcDig *restrict num, size_t cap) {
assert(n != NULL);
@@ -2112,7 +2042,7 @@ void bc_num_copy(BcNum *d, const BcNum *s) {
if (d == s) return;
bc_num_expand(d, s->len);
d->len = s->len;
- // I can just copy directly here.
+ d->neg = s->neg;
d->rdx = s->rdx;
d->scale = s->scale;
memcpy(d->num, s->num, BC_NUM_SIZE(d->len));
@@ -2126,7 +2056,7 @@ void bc_num_createCopy(BcNum *d, const BcNum *s) {
void bc_num_createFromBigdig(BcNum *n, BcBigDig val) {
BC_SIG_ASSERT_LOCKED;
- bc_num_init(n, BC_NUM_BIGDIG_LOG10);
+ bc_num_init(n, (BC_NUM_BIGDIG_LOG10 - 1) / BC_BASE_DIGS + 1);
bc_num_bigdig2num(n, val);
}
@@ -2140,7 +2070,7 @@ size_t bc_num_len(const BcNum *restrict n) {
if (BC_NUM_ZERO(n)) return 0;
- if (BC_NUM_RDX_VAL(n) == len) {
+ if (n->rdx == len) {
size_t zero, scale;
@@ -2158,20 +2088,19 @@ size_t bc_num_len(const BcNum *restrict n) {
return len;
}
-void bc_num_parse(BcNum *restrict n, const char *restrict val, BcBigDig base) {
-
+void bc_num_parse(BcNum *restrict n, const char *restrict val,
+ BcBigDig base, bool letter)
+{
assert(n != NULL && val != NULL && base);
assert(base >= BC_NUM_MIN_BASE && base <= vm.maxes[BC_PROG_GLOBALS_IBASE]);
assert(bc_num_strValid(val));
- if (!val[1]) {
+ if (letter) {
BcBigDig dig = bc_num_parseChar(val[0], BC_NUM_MAX_LBASE);
bc_num_bigdig2num(n, dig);
}
else if (base == BC_BASE) bc_num_parseDecimal(n, val);
else bc_num_parseBase(n, val, base);
-
- assert(BC_NUM_RDX_VALID(n));
}
void bc_num_print(BcNum *restrict n, BcBigDig base, bool newline) {
@@ -2184,7 +2113,8 @@ void bc_num_print(BcNum *restrict n, BcBigDig base, bool newline) {
if (BC_NUM_ZERO(n)) bc_num_printHex(0, 1, false);
else if (base == BC_BASE) bc_num_printDecimal(n);
#if BC_ENABLE_EXTRA_MATH
- else if (base == 0 || base == 1) bc_num_printExponent(n, base != 0);
+ else if (base == 0 || base == 1)
+ bc_num_printExponent(n, base != 0);
#endif // BC_ENABLE_EXTRA_MATH
else bc_num_printBase(n, base);
@@ -2197,38 +2127,28 @@ void bc_num_bigdig2(const BcNum *restrict n, BcBigDig *result) {
// its preconditions are met. Those preconditions include both parameters
// being non-NULL, n being non-negative, and n being less than vm.max. If
// all of that is true, then we can just convert without worrying about
- // negative errors or overflow.
+ // negative errors or overflow. We also don't care about signals because
+ // this function should execute in only a few iterations, meaning that
+ // ignoring signals here should be fine.
BcBigDig r = 0;
- size_t nrdx = BC_NUM_RDX_VAL(n);
assert(n != NULL && result != NULL);
- assert(!BC_NUM_NEG(n));
+ assert(!n->neg);
assert(bc_num_cmp(n, &vm.max) < 0);
- assert(n->len - nrdx <= 3);
+ assert(n->len - n->rdx <= 3);
// There is a small speed win from unrolling the loop here, and since it
// only adds 53 bytes, I decided that it was worth it.
- switch (n->len - nrdx) {
-
+ switch (n->len - n->rdx) {
case 3:
- {
- r = (BcBigDig) n->num[nrdx + 2];
- }
- // Fallthrough.
- BC_FALLTHROUGH
-
+ r = (BcBigDig) n->num[n->rdx + 2];
+ // Fallthrough.
case 2:
- {
- r = r * BC_BASE_POW + (BcBigDig) n->num[nrdx + 1];
- }
- // Fallthrough.
- BC_FALLTHROUGH
-
+ r = r * BC_BASE_POW + (BcBigDig) n->num[n->rdx + 1];
+ // Fallthrough.
case 1:
- {
- r = r * BC_BASE_POW + (BcBigDig) n->num[nrdx];
- }
+ r = r * BC_BASE_POW + (BcBigDig) n->num[n->rdx];
}
*result = r;
@@ -2238,9 +2158,9 @@ void bc_num_bigdig(const BcNum *restrict n, BcBigDig *result) {
assert(n != NULL && result != NULL);
- if (BC_ERR(BC_NUM_NEG(n))) bc_vm_err(BC_ERR_MATH_NEGATIVE);
+ if (BC_ERR(n->neg)) bc_vm_err(BC_ERROR_MATH_NEGATIVE);
if (BC_ERR(bc_num_cmp(n, &vm.max) >= 0))
- bc_vm_err(BC_ERR_MATH_OVERFLOW);
+ bc_vm_err(BC_ERROR_MATH_OVERFLOW);
bc_num_bigdig2(n, result);
}
@@ -2267,41 +2187,41 @@ void bc_num_bigdig2num(BcNum *restrict n, BcBigDig val) {
#if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
void bc_num_rng(const BcNum *restrict n, BcRNG *rng) {
- BcNum temp, temp2, intn, frac;
+ BcNum pow, temp, temp2, intn, frac;
BcRand state1, state2, inc1, inc2;
- size_t nrdx = BC_NUM_RDX_VAL(n);
+ BcDig pow_num[BC_RAND_NUM_SIZE];
+
+ bc_num_setup(&pow, pow_num, sizeof(pow_num) / sizeof(BcDig));
BC_SIG_LOCK;
bc_num_init(&temp, n->len);
bc_num_init(&temp2, n->len);
- bc_num_init(&frac, nrdx);
+ bc_num_init(&frac, n->rdx);
bc_num_init(&intn, bc_num_int(n));
BC_SETJMP_LOCKED(err);
BC_SIG_UNLOCK;
- assert(BC_NUM_RDX_VALID_NP(vm.max));
+ bc_num_mul(&vm.max, &vm.max, &pow, 0);
- memcpy(frac.num, n->num, BC_NUM_SIZE(nrdx));
- frac.len = nrdx;
- BC_NUM_RDX_SET_NP(frac, nrdx);
+ memcpy(frac.num, n->num, BC_NUM_SIZE(n->rdx));
+ frac.len = n->rdx;
+ frac.rdx = n->rdx;
frac.scale = n->scale;
- assert(BC_NUM_RDX_VALID_NP(frac));
- assert(BC_NUM_RDX_VALID_NP(vm.max2));
-
- bc_num_mul(&frac, &vm.max2, &temp, 0);
+ bc_num_mul(&frac, &pow, &temp, 0);
bc_num_truncate(&temp, temp.scale);
bc_num_copy(&frac, &temp);
- memcpy(intn.num, n->num + nrdx, BC_NUM_SIZE(bc_num_int(n)));
+ memcpy(intn.num, n->num + n->rdx, BC_NUM_SIZE(bc_num_int(n)));
intn.len = bc_num_int(n);
// This assert is here because it has to be true. It is also here to justify
- // the use of BC_ERR_SIGNAL_ONLY() on each of the divmod's and mod's below.
+ // the use of BC_ERROR_SIGNAL_ONLY() on each of the divmod's and mod's
+ // below.
assert(BC_NUM_NONZERO(&vm.max));
if (BC_NUM_NONZERO(&frac)) {
@@ -2351,7 +2271,8 @@ err:
void bc_num_createFromRNG(BcNum *restrict n, BcRNG *rng) {
BcRand s1, s2, i1, i2;
- BcNum conv, temp1, temp2, temp3;
+ BcNum pow, conv, temp1, temp2, temp3;
+ BcDig pow_num[BC_RAND_NUM_SIZE];
BcDig temp1_num[BC_RAND_NUM_SIZE], temp2_num[BC_RAND_NUM_SIZE];
BcDig conv_num[BC_NUM_BIGDIG_LOG10];
@@ -2363,36 +2284,35 @@ void bc_num_createFromRNG(BcNum *restrict n, BcRNG *rng) {
BC_SIG_UNLOCK;
+ bc_num_setup(&pow, pow_num, sizeof(pow_num) / sizeof(BcDig));
bc_num_setup(&temp1, temp1_num, sizeof(temp1_num) / sizeof(BcDig));
bc_num_setup(&temp2, temp2_num, sizeof(temp2_num) / sizeof(BcDig));
bc_num_setup(&conv, conv_num, sizeof(conv_num) / sizeof(BcDig));
// This assert is here because it has to be true. It is also here to justify
- // the assumption that vm.max2 is not zero.
+ // the assumption that pow is not zero.
assert(BC_NUM_NONZERO(&vm.max));
- // Because this is true, we can just use BC_ERR_SIGNAL_ONLY() below when
- // dividing by vm.max2.
- assert(BC_NUM_NONZERO(&vm.max2));
+ bc_num_mul(&vm.max, &vm.max, &pow, 0);
+
+ // Because this is true, we can just use BC_ERROR_SIGNAL_ONLY() below when
+ // dividing by pow.
+ assert(BC_NUM_NONZERO(&pow));
bc_rand_getRands(rng, &s1, &s2, &i1, &i2);
bc_num_bigdig2num(&conv, (BcBigDig) s2);
- assert(BC_NUM_RDX_VALID_NP(conv));
-
bc_num_mul(&conv, &vm.max, &temp1, 0);
bc_num_bigdig2num(&conv, (BcBigDig) s1);
bc_num_add(&conv, &temp1, &temp2, 0);
- bc_num_div(&temp2, &vm.max2, &temp3, BC_RAND_STATE_BITS);
+ bc_num_div(&temp2, &pow, &temp3, BC_RAND_STATE_BITS);
bc_num_bigdig2num(&conv, (BcBigDig) i2);
- assert(BC_NUM_RDX_VALID_NP(conv));
-
bc_num_mul(&conv, &vm.max, &temp1, 0);
bc_num_bigdig2num(&conv, (BcBigDig) i1);
@@ -2401,8 +2321,6 @@ void bc_num_createFromRNG(BcNum *restrict n, BcRNG *rng) {
bc_num_add(&temp2, &temp3, n, 0);
- assert(BC_NUM_RDX_VALID(n));
-
err:
BC_SIG_MAYLOCK;
bc_num_free(&temp3);
@@ -2422,8 +2340,8 @@ void bc_num_irand(const BcNum *restrict a, BcNum *restrict b,
assert(a != b);
- if (BC_ERR(BC_NUM_NEG(a))) bc_vm_err(BC_ERR_MATH_NEGATIVE);
- if (BC_ERR(BC_NUM_RDX_VAL(a))) bc_vm_err(BC_ERR_MATH_NON_INTEGER);
+ if (BC_ERR(a->neg)) bc_vm_err(BC_ERROR_MATH_NEGATIVE);
+ if (BC_ERR(a->rdx)) bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
if (BC_NUM_ZERO(a) || BC_NUM_ONE(a)) return;
cmp = bc_num_cmp(a, &vm.max);
@@ -2478,7 +2396,8 @@ void bc_num_irand(const BcNum *restrict a, BcNum *restrict b,
c2 = &cp2;
// This assert is here because it has to be true. It is also here to justify
- // the use of BC_ERR_SIGNAL_ONLY() on each of the divmod's and mod's below.
+ // the use of BC_ERROR_SIGNAL_ONLY() on each of the divmod's and mod's
+ // below.
assert(BC_NUM_NONZERO(&vm.max));
while (BC_NUM_NONZERO(c1)) {
@@ -2510,17 +2429,11 @@ void bc_num_irand(const BcNum *restrict a, BcNum *restrict b,
bc_num_bigdig2num(&rand, r);
- assert(BC_NUM_RDX_VALID_NP(rand));
- assert(BC_NUM_RDX_VALID(p1));
-
bc_num_mul(&rand, p1, p2, 0);
bc_num_add(p2, t1, t2, 0);
if (BC_NUM_NONZERO(c2)) {
- assert(BC_NUM_RDX_VALID_NP(vm.max));
- assert(BC_NUM_RDX_VALID(p1));
-
bc_num_mul(&vm.max, p1, p2, 0);
tmp = p1;
@@ -2541,8 +2454,6 @@ void bc_num_irand(const BcNum *restrict a, BcNum *restrict b,
bc_num_copy(b, t1);
bc_num_clean(b);
- assert(BC_NUM_RDX_VALID(b));
-
err:
BC_SIG_MAYLOCK;
bc_num_free(&pow);
@@ -2562,11 +2473,11 @@ size_t bc_num_addReq(const BcNum *a, const BcNum *b, size_t scale) {
BC_UNUSED(scale);
- ardx = BC_NUM_RDX_VAL(a);
+ ardx = a->rdx;
aint = bc_num_int(a);
assert(aint <= a->len && ardx <= a->len);
- brdx = BC_NUM_RDX_VAL(b);
+ brdx = b->rdx;
bint = bc_num_int(b);
assert(bint <= b->len && brdx <= b->len);
@@ -2578,22 +2489,13 @@ size_t bc_num_addReq(const BcNum *a, const BcNum *b, size_t scale) {
size_t bc_num_mulReq(const BcNum *a, const BcNum *b, size_t scale) {
size_t max, rdx;
- rdx = bc_vm_growSize(BC_NUM_RDX_VAL(a), BC_NUM_RDX_VAL(b));
+ rdx = bc_vm_growSize(a->rdx, b->rdx);
max = BC_NUM_RDX(scale);
max = bc_vm_growSize(BC_MAX(max, rdx), 1);
rdx = bc_vm_growSize(bc_vm_growSize(bc_num_int(a), bc_num_int(b)), max);
return rdx;
}
-size_t bc_num_divReq(const BcNum *a, const BcNum *b, size_t scale) {
- size_t max, rdx;
- rdx = bc_vm_growSize(BC_NUM_RDX_VAL(a), BC_NUM_RDX_VAL(b));
- max = BC_NUM_RDX(scale);
- max = bc_vm_growSize(BC_MAX(max, rdx), 1);
- rdx = bc_vm_growSize(bc_num_int(a), max);
- return rdx;
-}
-
size_t bc_num_powReq(const BcNum *a, const BcNum *b, size_t scale) {
BC_UNUSED(scale);
return bc_vm_growSize(bc_vm_growSize(a->len, b->len), 1);
@@ -2602,62 +2504,44 @@ size_t bc_num_powReq(const BcNum *a, const BcNum *b, size_t scale) {
#if BC_ENABLE_EXTRA_MATH
size_t bc_num_placesReq(const BcNum *a, const BcNum *b, size_t scale) {
BC_UNUSED(scale);
- return a->len + b->len - BC_NUM_RDX_VAL(a) - BC_NUM_RDX_VAL(b);
+ return a->len + b->len - a->rdx - b->rdx;
}
#endif // BC_ENABLE_EXTRA_MATH
void bc_num_add(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, false, bc_num_as, bc_num_addReq(a, b, scale));
}
void bc_num_sub(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, true, bc_num_as, bc_num_addReq(a, b, scale));
}
void bc_num_mul(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_m, bc_num_mulReq(a, b, scale));
}
void bc_num_div(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
- bc_num_binary(a, b, c, scale, bc_num_d, bc_num_divReq(a, b, scale));
+ bc_num_binary(a, b, c, scale, bc_num_d, bc_num_mulReq(a, b, scale));
}
void bc_num_mod(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
- bc_num_binary(a, b, c, scale, bc_num_rem, bc_num_divReq(a, b, scale));
+ bc_num_binary(a, b, c, scale, bc_num_rem, bc_num_mulReq(a, b, scale));
}
void bc_num_pow(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_p, bc_num_powReq(a, b, scale));
}
#if BC_ENABLE_EXTRA_MATH
void bc_num_places(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_place, bc_num_placesReq(a, b, scale));
}
void bc_num_lshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_left, bc_num_placesReq(a, b, scale));
}
void bc_num_rshift(BcNum *a, BcNum *b, BcNum *c, size_t scale) {
- assert(BC_NUM_RDX_VALID(a));
- assert(BC_NUM_RDX_VALID(b));
bc_num_binary(a, b, c, scale, bc_num_right, bc_num_placesReq(a, b, scale));
}
#endif // BC_ENABLE_EXTRA_MATH
@@ -2670,13 +2554,13 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
assert(a != NULL && b != NULL && a != b);
- if (BC_ERR(BC_NUM_NEG(a))) bc_vm_err(BC_ERR_MATH_NEGATIVE);
+ if (BC_ERR(a->neg)) bc_vm_err(BC_ERROR_MATH_NEGATIVE);
if (a->scale > scale) scale = a->scale;
len = bc_vm_growSize(bc_num_intDigits(a), 1);
rdx = BC_NUM_RDX(scale);
- req = bc_vm_growSize(BC_MAX(rdx, BC_NUM_RDX_VAL(a)), len >> 1);
+ req = bc_vm_growSize(BC_MAX(rdx, a->rdx), len >> 1);
BC_SIG_LOCK;
@@ -2684,9 +2568,6 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
BC_SIG_UNLOCK;
- assert(a != NULL && b != NULL && a != b);
- assert(a->num != NULL && b->num != NULL);
-
if (BC_NUM_ZERO(a)) {
bc_num_setToZero(b, scale);
return;
@@ -2698,7 +2579,7 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
}
rdx = BC_NUM_RDX(scale);
- rdx = BC_MAX(rdx, BC_NUM_RDX_VAL(a));
+ rdx = BC_MAX(rdx, a->rdx);
len = bc_vm_growSize(a->len, rdx);
BC_SIG_LOCK;
@@ -2710,7 +2591,7 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
bc_num_one(&half);
half.num[0] = BC_BASE_POW / 2;
half.len = 1;
- BC_NUM_RDX_SET_NP(half, 1);
+ half.rdx = 1;
half.scale = 1;
bc_num_init(&f, len);
@@ -2735,7 +2616,6 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
bc_num_shiftLeft(x0, pow / 2);
}
- // I can set the rdx here directly because neg should be false.
x0->scale = x0->rdx = digs = digs1 = digs2 = 0;
resscale = (scale + BC_BASE_DIGS) + 2;
@@ -2745,10 +2625,6 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
bc_num_div(a, x0, &f, resscale);
bc_num_add(x0, &f, &fprime, resscale);
-
- assert(BC_NUM_RDX_VALID_NP(fprime));
- assert(BC_NUM_RDX_VALID_NP(half));
-
bc_num_mul(&fprime, &half, x1, resscale);
temp = x0;
@@ -2759,10 +2635,9 @@ void bc_num_sqrt(BcNum *restrict a, BcNum *restrict b, size_t scale) {
bc_num_copy(b, x0);
if (b->scale > scale) bc_num_truncate(b, b->scale - scale);
- assert(!BC_NUM_NEG(b) || BC_NUM_NONZERO(b));
- assert(BC_NUM_RDX_VALID(b));
- assert(BC_NUM_RDX_VAL(b) <= b->len || !b->len);
- assert(!b->len || b->num[b->len - 1] || BC_NUM_RDX_VAL(b) == b->len);
+ assert(!b->neg || BC_NUM_NONZERO(b));
+ assert(b->rdx <= b->len || !b->len);
+ assert(!b->len || b->num[b->len - 1] || b->rdx == b->len);
err:
BC_SIG_MAYLOCK;
@@ -2775,9 +2650,9 @@ err:
void bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
- size_t ts, len;
- BcNum *ptr_a, num2;
+ BcNum num2, *ptr_a;
bool init = false;
+ size_t ts, len;
ts = BC_MAX(scale + b->scale, a->scale);
len = bc_num_mulReq(a, b, ts);
@@ -2805,9 +2680,8 @@ void bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
bc_num_expand(c, len);
}
- if (BC_NUM_NONZERO(a) && !BC_NUM_RDX_VAL(a) &&
- !BC_NUM_RDX_VAL(b) && b->len == 1 && !scale)
- {
+ if (BC_NUM_NONZERO(a) && !a->rdx && !b->rdx && b->len == 1 && !scale) {
+
BcBigDig rem;
bc_num_divArray(ptr_a, (BcBigDig) b->num[0], c, &rem);
@@ -2819,14 +2693,12 @@ void bc_num_divmod(BcNum *a, BcNum *b, BcNum *c, BcNum *d, size_t scale) {
}
else bc_num_r(ptr_a, b, c, d, scale, ts);
- assert(!BC_NUM_NEG(c) || BC_NUM_NONZERO(c));
- assert(BC_NUM_RDX_VALID(c));
- assert(BC_NUM_RDX_VAL(c) <= c->len || !c->len);
- assert(!c->len || c->num[c->len - 1] || BC_NUM_RDX_VAL(c) == c->len);
- assert(!BC_NUM_NEG(d) || BC_NUM_NONZERO(d));
- assert(BC_NUM_RDX_VALID(d));
- assert(BC_NUM_RDX_VAL(d) <= d->len || !d->len);
- assert(!d->len || d->num[d->len - 1] || BC_NUM_RDX_VAL(d) == d->len);
+ assert(!c->neg || BC_NUM_NONZERO(c));
+ assert(c->rdx <= c->len || !c->len);
+ assert(!c->len || c->num[c->len - 1] || c->rdx == c->len);
+ assert(!d->neg || BC_NUM_NONZERO(d));
+ assert(d->rdx <= d->len || !d->len);
+ assert(!d->len || d->num[d->len - 1] || d->rdx == d->len);
err:
if (init) {
@@ -2845,10 +2717,10 @@ void bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
assert(a != NULL && b != NULL && c != NULL && d != NULL);
assert(a != d && b != d && c != d);
- if (BC_ERR(BC_NUM_ZERO(c))) bc_vm_err(BC_ERR_MATH_DIVIDE_BY_ZERO);
- if (BC_ERR(BC_NUM_NEG(b))) bc_vm_err(BC_ERR_MATH_NEGATIVE);
- if (BC_ERR(BC_NUM_RDX_VAL(a) || BC_NUM_RDX_VAL(b) || BC_NUM_RDX_VAL(c)))
- bc_vm_err(BC_ERR_MATH_NON_INTEGER);
+ if (BC_ERR(BC_NUM_ZERO(c))) bc_vm_err(BC_ERROR_MATH_DIVIDE_BY_ZERO);
+ if (BC_ERR(b->neg)) bc_vm_err(BC_ERROR_MATH_NEGATIVE);
+ if (BC_ERR(a->rdx || b->rdx || c->rdx))
+ bc_vm_err(BC_ERROR_MATH_NON_INTEGER);
bc_num_expand(d, c->len);
@@ -2875,10 +2747,7 @@ void bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
// Num two cannot be 0, so no errors.
bc_num_divmod(&exp, &two, &exp, &temp, 0);
- if (BC_NUM_ONE(&temp) && !BC_NUM_NEG_NP(temp)) {
-
- assert(BC_NUM_RDX_VALID(d));
- assert(BC_NUM_RDX_VALID_NP(base));
+ if (BC_NUM_ONE(&temp) && !temp.neg) {
bc_num_mul(d, &base, &temp, 0);
@@ -2886,8 +2755,6 @@ void bc_num_modexp(BcNum *a, BcNum *b, BcNum *c, BcNum *restrict d) {
bc_num_rem(&temp, c, d, 0);
}
- assert(BC_NUM_RDX_VALID_NP(base));
-
bc_num_mul(&base, &base, &temp, 0);
// We already checked for 0.
@@ -2900,9 +2767,8 @@ err:
bc_num_free(&temp);
bc_num_free(&base);
BC_LONGJMP_CONT;
- assert(!BC_NUM_NEG(d) || d->len);
- assert(BC_NUM_RDX_VALID(d));
- assert(!d->len || d->num[d->len - 1] || BC_NUM_RDX_VAL(d) == d->len);
+ assert(!d->neg || d->len);
+ assert(!d->len || d->num[d->len - 1] || d->rdx == d->len);
}
#endif // DC_ENABLED
@@ -2931,7 +2797,7 @@ void bc_num_printDigs(const BcDig *n, size_t len, bool emptyline) {
void bc_num_printWithDigs(const BcNum *n, const char *name, bool emptyline) {
bc_file_puts(&vm.fout, name);
bc_file_printf(&vm.fout, " len: %zu, rdx: %zu, scale: %zu\n",
- name, n->len, BC_NUM_RDX_VAL(n), n->scale);
+ name, n->len, n->rdx, n->scale);
bc_num_printDigs(n->num, n->len, emptyline);
}
@@ -2940,13 +2806,13 @@ void bc_num_dump(const char *varname, const BcNum *n) {
ulong i, scale = n->scale;
bc_file_printf(&vm.ferr, "\n%s = %s", varname,
- n->len ? (BC_NUM_NEG(n) ? "-" : "+") : "0 ");
+ n->len ? (n->neg ? "-" : "+") : "0 ");
for (i = n->len - 1; i < n->len; --i) {
- if (i + 1 == BC_NUM_RDX_VAL(n)) bc_file_puts(&vm.ferr, ". ");
+ if (i + 1 == n->rdx) bc_file_puts(&vm.ferr, ". ");
- if (scale / BC_BASE_DIGS != BC_NUM_RDX_VAL(n) - i - 1)
+ if (scale / BC_BASE_DIGS != n->rdx - i - 1)
bc_file_printf(&vm.ferr, "%lu ", (unsigned long) n->num[i]);
else {
@@ -2965,7 +2831,7 @@ void bc_num_dump(const char *varname, const BcNum *n) {
}
bc_file_printf(&vm.ferr, "(%zu | %zu.%zu / %zu) %lu\n",
- n->scale, n->len, BC_NUM_RDX_VAL(n), n->cap,
+ n->scale, n->len, n->rdx, n->cap,
(unsigned long) (void*) n->num);
}
#endif // BC_DEBUG_CODE
diff --git a/src/opt.c b/src/opt.c
index 57cee759af5e..3a01a2657f15 100644
--- a/src/opt.c
+++ b/src/opt.c
@@ -62,8 +62,8 @@ static const char* bc_opt_longopt(const BcOptLong *longopts, int c) {
return "NULL";
}
-static void bc_opt_error(BcErr err, int c, const char *str) {
- if (err == BC_ERR_FATAL_OPTION) bc_vm_error(err, 0, str);
+static void bc_opt_error(BcError err, int c, const char *str) {
+ if (err == BC_ERROR_FATAL_OPTION) bc_vm_error(err, 0, str);
else bc_vm_error(err, 0, (int) c, str);
}
@@ -110,12 +110,10 @@ static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
str[0] = option[0];
o->optind += 1;
- bc_opt_error(BC_ERR_FATAL_OPTION, option[0], str);
+ bc_opt_error(BC_ERROR_FATAL_OPTION, option[0], str);
}
}
// Fallthrough.
- BC_FALLTHROUGH
-
case BC_OPT_NONE:
{
if (option[1]) o->subopt += 1;
@@ -138,7 +136,7 @@ static int bc_opt_parseShort(BcOpt *o, const BcOptLong *longopts) {
o->optarg = next;
o->optind += 1;
}
- else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG, option[0],
+ else bc_opt_error(BC_ERROR_FATAL_OPTION_NO_ARG, option[0],
bc_opt_longopt(longopts, option[0]));
@@ -217,12 +215,12 @@ int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
if ((longopts[i].type == BC_OPT_BC_ONLY && BC_IS_DC) ||
(longopts[i].type == BC_OPT_DC_ONLY && BC_IS_BC))
{
- bc_opt_error(BC_ERR_FATAL_OPTION, o->optopt, name);
+ bc_opt_error(BC_ERROR_FATAL_OPTION, o->optopt, name);
}
if (longopts[i].type == BC_OPT_NONE && arg != NULL)
{
- bc_opt_error(BC_ERR_FATAL_OPTION_ARG, o->optopt, name);
+ bc_opt_error(BC_ERROR_FATAL_OPTION_ARG, o->optopt, name);
}
if (arg != NULL) o->optarg = arg;
@@ -231,7 +229,7 @@ int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
o->optarg = o->argv[o->optind];
if (o->optarg != NULL) o->optind += 1;
- else bc_opt_error(BC_ERR_FATAL_OPTION_NO_ARG,
+ else bc_opt_error(BC_ERROR_FATAL_OPTION_NO_ARG,
o->optopt, name);
}
@@ -239,7 +237,7 @@ int bc_opt_parse(BcOpt *o, const BcOptLong *longopts) {
}
}
- bc_opt_error(BC_ERR_FATAL_OPTION, 0, option);
+ bc_opt_error(BC_ERROR_FATAL_OPTION, 0, option);
return -1;
}
diff --git a/src/parse.c b/src/parse.c
index 39b79efdd02f..a48f5807e9ce 100644
--- a/src/parse.c
+++ b/src/parse.c
@@ -40,6 +40,9 @@
#include <limits.h>
+#include <status.h>
+#include <vector.h>
+#include <lex.h>
#include <parse.h>
#include <program.h>
#include <vm.h>
diff --git a/src/program.c b/src/program.c
index f0a67ee194c1..3c2544f8a61f 100644
--- a/src/program.c
+++ b/src/program.c
@@ -61,7 +61,7 @@ static inline void bc_program_type_num(BcResult *r, BcNum *n) {
assert(r->t != BC_RESULT_VOID);
#endif // BC_ENABLED
- if (BC_ERR(!BC_PROG_NUM(r, n))) bc_vm_err(BC_ERR_EXEC_TYPE);
+ if (BC_ERR(!BC_PROG_NUM(r, n))) bc_vm_err(BC_ERROR_EXEC_TYPE);
}
#if BC_ENABLED
@@ -72,7 +72,7 @@ static void bc_program_type_match(BcResult *r, BcType t) {
#endif // DC_ENABLED
if (BC_ERR((r->t != BC_RESULT_ARRAY) != (!t)))
- bc_vm_err(BC_ERR_EXEC_TYPE);
+ bc_vm_err(BC_ERROR_EXEC_TYPE);
}
#endif // BC_ENABLED
@@ -270,7 +270,7 @@ static void bc_program_operand(BcProgram *p, BcResult **r,
*r = bc_vec_item_rev(&p->results, idx);
#if BC_ENABLED
- if (BC_ERR((*r)->t == BC_RESULT_VOID)) bc_vm_err(BC_ERR_EXEC_VOID_VAL);
+ if (BC_ERR((*r)->t == BC_RESULT_VOID)) bc_vm_err(BC_ERROR_EXEC_VOID_VAL);
#endif // BC_ENABLED
*n = bc_program_num(p, *r);
@@ -286,7 +286,7 @@ static void bc_program_binPrep(BcProgram *p, BcResult **l, BcNum **ln,
#ifndef BC_PROG_NO_STACK_CHECK
if (BC_IS_DC) {
if (BC_ERR(!BC_PROG_STACK(&p->results, idx + 2)))
- bc_vm_err(BC_ERR_EXEC_STACK);
+ bc_vm_err(BC_ERROR_EXEC_STACK);
}
#endif // BC_PROG_NO_STACK_CHECK
@@ -306,7 +306,7 @@ static void bc_program_binPrep(BcProgram *p, BcResult **l, BcNum **ln,
if (lt == (*r)->t && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM))
*ln = bc_program_num(p, *l);
- if (BC_ERR(lt == BC_RESULT_STR)) bc_vm_err(BC_ERR_EXEC_TYPE);
+ if (BC_ERR(lt == BC_RESULT_STR)) bc_vm_err(BC_ERROR_EXEC_TYPE);
}
static void bc_program_binOpPrep(BcProgram *p, BcResult **l, BcNum **ln,
@@ -329,7 +329,7 @@ static void bc_program_assignPrep(BcProgram *p, BcResult **l, BcNum **ln,
lt = (*l)->t;
if (BC_ERR(lt >= min && lt <= BC_RESULT_ONE))
- bc_vm_err(BC_ERR_EXEC_TYPE);
+ bc_vm_err(BC_ERROR_EXEC_TYPE);
#if DC_ENABLED
if(BC_IS_DC) {
@@ -351,7 +351,7 @@ static void bc_program_prep(BcProgram *p, BcResult **r, BcNum **n, size_t idx) {
#ifndef BC_PROG_NO_STACK_CHECK
if (BC_IS_DC) {
if (BC_ERR(!BC_PROG_STACK(&p->results, idx + 1)))
- bc_vm_err(BC_ERR_EXEC_STACK);
+ bc_vm_err(BC_ERROR_EXEC_STACK);
}
#endif // BC_PROG_NO_STACK_CHECK
@@ -391,7 +391,7 @@ static void bc_program_const(BcProgram *p, const char *code, size_t *bgn) {
}
// bc_num_parse() should only do operations that cannot fail.
- bc_num_parse(&c->num, c->val, base);
+ bc_num_parse(&c->num, c->val, base, !c->val[1]);
c->base = base;
}
@@ -419,9 +419,6 @@ static void bc_program_op(BcProgram *p, uchar inst) {
BC_SIG_UNLOCK;
- assert(BC_NUM_RDX_VALID(n1));
- assert(BC_NUM_RDX_VALID(n2));
-
bc_program_ops[idx](n1, n2, &res->d.n, BC_PROG_SCALE(p));
bc_program_retire(p, 1, 2);
@@ -440,7 +437,7 @@ static void bc_program_read(BcProgram *p) {
for (i = 0; i < p->stack.len; ++i) {
BcInstPtr *ip_ptr = bc_vec_item(&p->stack, i);
if (ip_ptr->func == BC_PROG_READ)
- bc_vm_err(BC_ERR_EXEC_REC_READ);
+ bc_vm_err(BC_ERROR_EXEC_REC_READ);
}
BC_SIG_LOCK;
@@ -457,13 +454,13 @@ static void bc_program_read(BcProgram *p) {
bc_vec_npop(&f->code, f->code.len);
s = bc_read_line(&buf, BC_IS_BC ? "read> " : "?> ");
- if (s == BC_STATUS_EOF) bc_vm_err(BC_ERR_EXEC_READ_EXPR);
+ if (s == BC_STATUS_EOF) bc_vm_err(BC_ERROR_EXEC_READ_EXPR);
bc_parse_text(&parse, buf.v);
vm.expr(&parse, BC_PARSE_NOREAD | BC_PARSE_NEEDVAL);
if (BC_ERR(parse.l.t != BC_LEX_NLINE && parse.l.t != BC_LEX_EOF))
- bc_vm_err(BC_ERR_EXEC_READ_EXPR);
+ bc_vm_err(BC_ERROR_EXEC_READ_EXPR);
#if BC_ENABLED
if (BC_G) bc_program_prepGlobals(p);
@@ -498,12 +495,6 @@ exec_err:
static void bc_program_rand(BcProgram *p) {
BcRand rand = bc_rand_int(&p->rng);
bc_program_pushBigdig(p, (BcBigDig) rand, BC_RESULT_TEMP);
-#ifndef NDEBUG
- {
- BcResult *r = bc_vec_top(&p->results);
- assert(BC_NUM_RDX_VALID_NP(r->d.n));
- }
-#endif // NDEBUG
}
#endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
@@ -569,7 +560,7 @@ static void bc_program_print(BcProgram *p, uchar inst, size_t idx) {
#ifndef BC_PROG_NO_STACK_CHECK
if (BC_IS_DC) {
if (BC_ERR(!BC_PROG_STACK(&p->results, idx + 1)))
- bc_vm_err(BC_ERR_EXEC_STACK);
+ bc_vm_err(BC_ERROR_EXEC_STACK);
}
#endif // BC_PROG_NO_STACK_CHECK
@@ -579,7 +570,7 @@ static void bc_program_print(BcProgram *p, uchar inst, size_t idx) {
#if BC_ENABLED
if (r->t == BC_RESULT_VOID) {
- if (BC_ERR(pop)) bc_vm_err(BC_ERR_EXEC_VOID_VAL);
+ if (BC_ERR(pop)) bc_vm_err(BC_ERROR_EXEC_VOID_VAL);
bc_vec_pop(&p->results);
return;
}
@@ -613,7 +604,7 @@ static void bc_program_print(BcProgram *p, uchar inst, size_t idx) {
void bc_program_negate(BcResult *r, BcNum *n) {
bc_num_copy(&r->d.n, n);
- if (BC_NUM_NONZERO(&r->d.n)) BC_NUM_NEG_TGL_NP(r->d.n);
+ if (BC_NUM_NONZERO(&r->d.n)) r->d.n.neg = !r->d.n.neg;
}
void bc_program_not(BcResult *r, BcNum *n) {
@@ -752,7 +743,7 @@ static void bc_program_copyToVar(BcProgram *p, size_t idx,
if (BC_IS_DC) {
if (BC_ERR(!BC_PROG_STACK(&p->results, 1)))
- bc_vm_err(BC_ERR_EXEC_STACK);
+ bc_vm_err(BC_ERROR_EXEC_STACK);
assert(BC_PROG_STACK(&p->results, 1));
@@ -777,7 +768,7 @@ static void bc_program_copyToVar(BcProgram *p, size_t idx,
#if DC_ENABLED
if (BC_IS_DC && (ptr->t == BC_RESULT_STR || BC_PROG_STR(n))) {
- if (BC_ERR(!var)) bc_vm_err(BC_ERR_EXEC_TYPE);
+ if (BC_ERR(!var)) bc_vm_err(BC_ERROR_EXEC_TYPE);
bc_program_assignStr(p, ptr, vec, true);
return;
}
@@ -880,9 +871,6 @@ static void bc_program_assign(BcProgram *p, uchar inst) {
if (!use_val)
inst -= (BC_INST_ASSIGN_POWER_NO_VAL - BC_INST_ASSIGN_POWER);
- assert(BC_NUM_RDX_VALID(l));
- assert(BC_NUM_RDX_VALID(r));
-
bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, scale);
}
#endif // BC_ENABLED
@@ -894,10 +882,10 @@ static void bc_program_assign(BcProgram *p, uchar inst) {
BcVec *v;
BcBigDig *ptr, *ptr_t, val, max, min;
- BcErr e;
+ BcError e;
bc_num_bigdig(l, &val);
- e = left->t - BC_RESULT_IBASE + BC_ERR_EXEC_IBASE;
+ e = left->t - BC_RESULT_IBASE + BC_ERROR_EXEC_IBASE;
if (sc) {
min = 0;
@@ -952,7 +940,7 @@ static void bc_program_pushVar(BcProgram *p, const char *restrict code,
BcVec *v = bc_program_vec(p, idx, BC_TYPE_VAR);
BcNum *num = bc_vec_top(v);
- if (BC_ERR(!BC_PROG_STACK(v, 2 - copy))) bc_vm_err(BC_ERR_EXEC_STACK);
+ if (BC_ERR(!BC_PROG_STACK(v, 2 - copy))) bc_vm_err(BC_ERROR_EXEC_STACK);
assert(BC_PROG_STACK(v, 2 - copy));
@@ -1070,9 +1058,9 @@ static void bc_program_call(BcProgram *p, const char *restrict code,
ip.func = bc_program_index(code, idx);
f = bc_vec_item(&p->fns, ip.func);
- if (BC_ERR(!f->code.len)) bc_vm_verr(BC_ERR_EXEC_UNDEF_FUNC, f->name);
+ if (BC_ERR(!f->code.len)) bc_vm_verr(BC_ERROR_EXEC_UNDEF_FUNC, f->name);
if (BC_ERR(nparams != f->nparams))
- bc_vm_verr(BC_ERR_EXEC_PARAMS, f->nparams, nparams);
+ bc_vm_verr(BC_ERROR_EXEC_PARAMS, f->nparams, nparams);
ip.len = p->results.len - nparams;
assert(BC_PROG_STACK(&p->results, nparams));
@@ -1085,7 +1073,8 @@ static void bc_program_call(BcProgram *p, const char *restrict code,
bool last = true;
arg = bc_vec_top(&p->results);
- if (BC_ERR(arg->t == BC_RESULT_VOID)) bc_vm_err(BC_ERR_EXEC_VOID_VAL);
+ if (BC_ERR(arg->t == BC_RESULT_VOID))
+ bc_vm_err(BC_ERROR_EXEC_VOID_VAL);
a = bc_vec_item(&f->autos, nparams - 1 - i);
@@ -1189,7 +1178,7 @@ static void bc_program_builtin(BcProgram *p, uchar inst) {
#ifndef BC_PROG_NO_STACK_CHECK
if (BC_IS_DC) {
if (BC_ERR(!BC_PROG_STACK(&p->results, 1)))
- bc_vm_err(BC_ERR_EXEC_STACK);
+ bc_vm_err(BC_ERROR_EXEC_STACK);
}
#endif // BC_PROG_NO_STACK_CHECK
@@ -1214,7 +1203,7 @@ static void bc_program_builtin(BcProgram *p, uchar inst) {
BC_SIG_UNLOCK;
- BC_NUM_NEG_CLR_NP(res->d.n);
+ res->d.n.neg = false;
}
#if BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
else if (inst == BC_INST_IRAND) {
@@ -1282,7 +1271,7 @@ static void bc_program_divmod(BcProgram *p) {
BcNum *n1, *n2;
size_t req;
- bc_vec_grow(&p->results, 2);
+ bc_vec_expand(&p->results, p->results.len + 2);
// We don't need to update the pointer because
// the capacity is enough due to the line above.
@@ -1310,7 +1299,7 @@ static void bc_program_modexp(BcProgram *p) {
BcResult *r1, *r2, *r3, *res;
BcNum *n1, *n2, *n3;
- if (BC_ERR(!BC_PROG_STACK(&p->results, 3))) bc_vm_err(BC_ERR_EXEC_STACK);
+ if (BC_ERR(!BC_PROG_STACK(&p->results, 3))) bc_vm_err(BC_ERROR_EXEC_STACK);
assert(BC_PROG_STACK(&p->results, 3));
@@ -1357,7 +1346,7 @@ static uchar bc_program_asciifyNum(BcProgram *p, BcNum *n) {
BC_SIG_UNLOCK;
bc_num_truncate(&num, num.scale);
- BC_NUM_NEG_CLR_NP(num);
+ num.neg = false;
// This is guaranteed to not have a divide by 0
// because strmb is equal to UCHAR_MAX + 1.
@@ -1383,7 +1372,7 @@ static void bc_program_asciify(BcProgram *p) {
uchar c;
size_t idx;
- if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_vm_err(BC_ERR_EXEC_STACK);
+ if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_vm_err(BC_ERROR_EXEC_STACK);
assert(BC_PROG_STACK(&p->results, 1));
@@ -1420,7 +1409,7 @@ static void bc_program_printStream(BcProgram *p) {
BcResult *r;
BcNum *n;
- if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_vm_err(BC_ERR_EXEC_STACK);
+ if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_vm_err(BC_ERROR_EXEC_STACK);
assert(BC_PROG_STACK(&p->results, 1));
@@ -1482,7 +1471,7 @@ static void bc_program_execStr(BcProgram *p, const char *restrict code,
assert(p->stack.len == p->tail_calls.len);
- if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_vm_err(BC_ERR_EXEC_STACK);
+ if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_vm_err(BC_ERROR_EXEC_STACK);
assert(BC_PROG_STACK(&p->results, 1));
@@ -1507,7 +1496,7 @@ static void bc_program_execStr(BcProgram *p, const char *restrict code,
n = bc_vec_top(bc_program_vec(p, idx, BC_TYPE_VAR));
else goto exit;
- if (BC_ERR(!BC_PROG_STR(n))) bc_vm_err(BC_ERR_EXEC_TYPE);
+ if (BC_ERR(!BC_PROG_STR(n))) bc_vm_err(BC_ERROR_EXEC_TYPE);
BC_UNSETJMP;
BC_SIG_UNLOCK;
@@ -1843,8 +1832,6 @@ void bc_program_exec(BcProgram *p) {
bc_vec_pop(&p->results);
}
// Fallthrough.
- BC_FALLTHROUGH
-
case BC_INST_JUMP:
{
idx = bc_program_index(code, &ip->idx);
@@ -2090,7 +2077,7 @@ void bc_program_exec(BcProgram *p) {
#ifndef BC_PROG_NO_STACK_CHECK
if (!BC_IS_BC) {
if (BC_ERR(!BC_PROG_STACK(&p->results, 1)))
- bc_vm_err(BC_ERR_EXEC_STACK);
+ bc_vm_err(BC_ERROR_EXEC_STACK);
}
#endif // BC_PROG_NO_STACK_CHECK
@@ -2158,7 +2145,7 @@ void bc_program_exec(BcProgram *p) {
case BC_INST_DUPLICATE:
{
if (BC_ERR(!BC_PROG_STACK(&p->results, 1)))
- bc_vm_err(BC_ERR_EXEC_STACK);
+ bc_vm_err(BC_ERROR_EXEC_STACK);
assert(BC_PROG_STACK(&p->results, 1));
@@ -2179,7 +2166,7 @@ void bc_program_exec(BcProgram *p) {
BcResult *ptr2;
if (BC_ERR(!BC_PROG_STACK(&p->results, 2)))
- bc_vm_err(BC_ERR_EXEC_STACK);
+ bc_vm_err(BC_ERROR_EXEC_STACK);
assert(BC_PROG_STACK(&p->results, 2));
diff --git a/src/read.c b/src/read.c
index 45e868c927da..6886a7e13602 100644
--- a/src/read.c
+++ b/src/read.c
@@ -143,7 +143,7 @@ BcStatus bc_read_chars(BcVec *vec, const char *prompt) {
BC_SIG_UNLOCK;
- bc_vm_err(BC_ERR_FATAL_IO_ERR);
+ bc_vm_err(BC_ERROR_FATAL_IO_ERR);
}
BC_SIG_UNLOCK;
@@ -177,14 +177,14 @@ BcStatus bc_read_line(BcVec *vec, const char *prompt) {
#endif // BC_ENABLE_HISTORY
if (BC_ERR(bc_read_binary(vec->v, vec->len - 1)))
- bc_vm_verr(BC_ERR_FATAL_BIN_FILE, bc_program_stdin_name);
+ bc_vm_verr(BC_ERROR_FATAL_BIN_FILE, bc_program_stdin_name);
return s;
}
void bc_read_file(const char *path, char **buf) {
- BcErr e = BC_ERR_FATAL_IO_ERR;
+ BcError e = BC_ERROR_FATAL_IO_ERR;
size_t size, r;
struct stat pstat;
int fd;
@@ -194,11 +194,11 @@ void bc_read_file(const char *path, char **buf) {
assert(path != NULL);
fd = open(path, O_RDONLY);
- if (BC_ERR(fd < 0)) bc_vm_verr(BC_ERR_FATAL_FILE_ERR, path);
+ if (BC_ERR(fd < 0)) bc_vm_verr(BC_ERROR_FATAL_FILE_ERR, path);
if (BC_ERR(fstat(fd, &pstat) == -1)) goto malloc_err;
if (BC_ERR(S_ISDIR(pstat.st_mode))) {
- e = BC_ERR_FATAL_PATH_DIR;
+ e = BC_ERROR_FATAL_PATH_DIR;
goto malloc_err;
}
@@ -211,7 +211,7 @@ void bc_read_file(const char *path, char **buf) {
(*buf)[size] = '\0';
if (BC_ERR(bc_read_binary(*buf, size))) {
- e = BC_ERR_FATAL_BIN_FILE;
+ e = BC_ERROR_FATAL_BIN_FILE;
goto read_err;
}
diff --git a/src/vector.c b/src/vector.c
index df6936aaeb76..f45bcb198a25 100644
--- a/src/vector.c
+++ b/src/vector.c
@@ -37,11 +37,12 @@
#include <stdlib.h>
#include <string.h>
+#include <status.h>
#include <vector.h>
#include <lang.h>
#include <vm.h>
-void bc_vec_grow(BcVec *restrict v, size_t n) {
+static void bc_vec_grow(BcVec *restrict v, size_t n) {
size_t len, cap = v->cap;
sig_atomic_t lock;
diff --git a/src/vm.c b/src/vm.c
index 3922b088414f..e15b1398734e 100644
--- a/src/vm.c
+++ b/src/vm.c
@@ -56,15 +56,13 @@
#endif // _WIN32
+#include <status.h>
#include <vector.h>
#include <args.h>
#include <vm.h>
#include <read.h>
#include <bc.h>
-char output_bufs[BC_VM_BUF_SIZE];
-BcVm vm;
-
#if BC_DEBUG_CODE
BC_NORETURN void bc_vm_jmp(const char* f) {
#else // BC_DEBUG_CODE
@@ -86,14 +84,12 @@ BC_NORETURN void bc_vm_jmp(void) {
assert(vm.jmp_bufs.len - (size_t) vm.sig_pop);
#endif // NDEBUG
- if (vm.jmp_bufs.len == 0) abort();
if (vm.sig_pop) bc_vec_pop(&vm.jmp_bufs);
else vm.sig_pop = 1;
siglongjmp(*((sigjmp_buf*) bc_vec_top(&vm.jmp_bufs)), 1);
}
-#if !BC_ENABLE_LIBRARY
static void bc_vm_sig(int sig) {
// There is already a signal in flight.
@@ -136,28 +132,8 @@ void bc_vm_info(const char* const help) {
bc_file_flush(&vm.fout);
}
-#endif // !BC_ENABLE_LIBRARY
-
-#if BC_ENABLE_LIBRARY
-void bc_vm_handleError(BcErr e) {
-
- assert(e < BC_ERR_NELEMS);
- assert(!vm.sig_pop);
-
- BC_SIG_LOCK;
- if (e <= BC_ERR_MATH_DIVIDE_BY_ZERO) {
- vm.err = (BclError) (e - BC_ERR_MATH_NEGATIVE +
- BCL_ERROR_MATH_NEGATIVE);
- }
- else if (vm.abrt) abort();
- else if (e == BC_ERR_FATAL_ALLOC_ERR) vm.err = BCL_ERROR_FATAL_ALLOC_ERR;
- else vm.err = BCL_ERROR_FATAL_UNKNOWN_ERR;
-
- BC_VM_JMP;
-}
-#else // BC_ENABLE_LIBRARY
-void bc_vm_handleError(BcErr e, size_t line, ...) {
+void bc_vm_error(BcError e, size_t line, ...) {
BcStatus s;
va_list args;
@@ -165,11 +141,11 @@ void bc_vm_handleError(BcErr e, size_t line, ...) {
const char* err_type = vm.err_ids[id];
sig_atomic_t lock;
- assert(e < BC_ERR_NELEMS);
+ assert(e < BC_ERROR_NELEMS);
assert(!vm.sig_pop);
#if BC_ENABLED
- if (!BC_S && e >= BC_ERR_POSIX_START) {
+ if (!BC_S && e >= BC_ERROR_POSIX_START) {
if (BC_W) {
// Make sure to not return an error.
id = UCHAR_MAX;
@@ -285,7 +261,7 @@ static void bc_vm_envArgs(const char* const env_args_name) {
buf += 1;
start = buf;
}
- else if (instr) bc_vm_error(BC_ERR_FATAL_OPTION, 0, start);
+ else if (instr) bc_vm_error(BC_ERROR_FATAL_OPTION, 0, start);
}
else buf += 1;
}
@@ -317,7 +293,6 @@ static size_t bc_vm_envLen(const char *var) {
return len;
}
-#endif // BC_ENABLE_LIBRARY
void bc_vm_shutdown(void) {
@@ -333,7 +308,6 @@ void bc_vm_shutdown(void) {
#endif // BC_ENABLE_HISTORY
#ifndef NDEBUG
-#if !BC_ENABLE_LIBRARY
bc_vec_free(&vm.env_args);
free(vm.env_args_buffer);
bc_vec_free(&vm.files);
@@ -341,40 +315,31 @@ void bc_vm_shutdown(void) {
bc_program_free(&vm.prog);
bc_parse_free(&vm.prs);
-#endif // !BC_ENABLE_LIBRARY
- bc_vm_freeTemps();
- bc_vec_free(&vm.temps);
+ {
+ size_t i;
+ for (i = 0; i < vm.temps.len; ++i)
+ free(((BcNum*) bc_vec_item(&vm.temps, i))->num);
+
+ bc_vec_free(&vm.temps);
+ }
#endif // NDEBUG
-#if !BC_ENABLE_LIBRARY
bc_file_free(&vm.fout);
bc_file_free(&vm.ferr);
-#endif // !BC_ENABLE_LIBRARY
}
-#if !defined(NDEBUG) || BC_ENABLE_LIBRARY
-void bc_vm_freeTemps(void) {
-
- size_t i;
-
- for (i = 0; i < vm.temps.len; ++i) {
- free(((BcNum*) bc_vec_item(&vm.temps, i))->num);
- }
-}
-#endif // !defined(NDEBUG) || BC_ENABLE_LIBRARY
-
inline size_t bc_vm_arraySize(size_t n, size_t size) {
size_t res = n * size;
if (BC_ERR(res >= SIZE_MAX || (n != 0 && res / n != size)))
- bc_vm_err(BC_ERR_FATAL_ALLOC_ERR);
+ bc_vm_err(BC_ERROR_FATAL_ALLOC_ERR);
return res;
}
inline size_t bc_vm_growSize(size_t a, size_t b) {
size_t res = a + b;
if (BC_ERR(res >= SIZE_MAX || res < a || res < b))
- bc_vm_err(BC_ERR_FATAL_ALLOC_ERR);
+ bc_vm_err(BC_ERROR_FATAL_ALLOC_ERR);
return res;
}
@@ -386,7 +351,7 @@ void* bc_vm_malloc(size_t n) {
ptr = malloc(n);
- if (BC_ERR(ptr == NULL)) bc_vm_err(BC_ERR_FATAL_ALLOC_ERR);
+ if (BC_ERR(ptr == NULL)) bc_vm_err(BC_ERROR_FATAL_ALLOC_ERR);
return ptr;
}
@@ -399,7 +364,7 @@ void* bc_vm_realloc(void *ptr, size_t n) {
temp = realloc(ptr, n);
- if (BC_ERR(temp == NULL)) bc_vm_err(BC_ERR_FATAL_ALLOC_ERR);
+ if (BC_ERR(temp == NULL)) bc_vm_err(BC_ERROR_FATAL_ALLOC_ERR);
return temp;
}
@@ -412,12 +377,11 @@ char* bc_vm_strdup(const char *str) {
s = strdup(str);
- if (BC_ERR(!s)) bc_vm_err(BC_ERR_FATAL_ALLOC_ERR);
+ if (BC_ERR(!s)) bc_vm_err(BC_ERROR_FATAL_ALLOC_ERR);
return s;
}
-#if !BC_ENABLE_LIBRARY
void bc_vm_printf(const char *fmt, ...) {
va_list args;
@@ -432,18 +396,12 @@ void bc_vm_printf(const char *fmt, ...) {
BC_SIG_UNLOCK;
}
-#endif // !BC_ENABLE_LIBRARY
void bc_vm_putchar(int c) {
-#if BC_ENABLE_LIBRARY
- bc_vec_pushByte(&vm.out, (uchar) c);
-#else // BC_ENABLE_LIBRARY
bc_file_putchar(&vm.fout, (uchar) c);
vm.nchars = (c == '\n' ? 0 : vm.nchars + 1);
-#endif // BC_ENABLE_LIBRARY
}
-#if !BC_ENABLE_LIBRARY
static void bc_vm_clean(void) {
BcVec *fns = &vm.prog.fns;
@@ -533,7 +491,7 @@ static void bc_vm_endif(void) {
if (good) {
while (BC_PARSE_IF_END(&vm.prs)) bc_vm_process("else {}");
}
- else bc_parse_err(&vm.prs, BC_ERR_PARSE_BLOCK);
+ else bc_parse_err(&vm.prs, BC_ERROR_PARSE_BLOCK);
}
#endif // BC_ENABLED
@@ -646,9 +604,9 @@ restart:
if (!BC_STATUS_IS_ERROR(s)) {
if (BC_ERR(comment))
- bc_parse_err(&vm.prs, BC_ERR_PARSE_COMMENT);
+ bc_parse_err(&vm.prs, BC_ERROR_PARSE_COMMENT);
else if (BC_ERR(string))
- bc_parse_err(&vm.prs, BC_ERR_PARSE_STRING);
+ bc_parse_err(&vm.prs, BC_ERROR_PARSE_STRING);
#if BC_ENABLED
else if (BC_IS_BC) bc_vm_endif();
#endif // BC_ENABLED
@@ -694,7 +652,7 @@ static void bc_vm_defaultMsgs(void) {
for (i = 0; i < BC_ERR_IDX_NELEMS + BC_ENABLED; ++i)
vm.err_ids[i] = bc_errs[i];
- for (i = 0; i < BC_ERR_NELEMS; ++i) vm.err_msgs[i] = bc_err_msgs[i];
+ for (i = 0; i < BC_ERROR_NELEMS; ++i) vm.err_msgs[i] = bc_err_msgs[i];
}
static void bc_vm_gettext(void) {
@@ -725,7 +683,7 @@ static void bc_vm_gettext(void) {
i = 0;
id = bc_err_ids[i];
- for (set = id + 3, msg = 1; i < BC_ERR_NELEMS; ++i, ++msg) {
+ for (set = id + 3, msg = 1; i < BC_ERROR_NELEMS; ++i, ++msg) {
if (id != bc_err_ids[i]) {
msg = 1;
@@ -817,8 +775,8 @@ err:
#endif // NDEBUG
}
-void bc_vm_boot(int argc, char *argv[], const char *env_len,
- const char* const env_args)
+void bc_vm_boot(int argc, char *argv[], const char *env_len,
+ const char* const env_args)
{
int ttyin, ttyout, ttyerr;
struct sigaction sa;
@@ -845,7 +803,10 @@ void bc_vm_boot(int argc, char *argv[], const char *env_len,
if (BC_TTY) sigaction(SIGHUP, &sa, NULL);
#endif // BC_ENABLE_HISTORY
- bc_vm_init();
+ memcpy(vm.max_num, bc_num_bigdigMax,
+ bc_num_bigdigMax_size * sizeof(BcDig));
+ bc_num_setup(&vm.max, vm.max_num, BC_NUM_BIGDIG_LOG10);
+ vm.max.len = bc_num_bigdigMax_size;
vm.file = NULL;
@@ -861,6 +822,8 @@ void bc_vm_boot(int argc, char *argv[], const char *env_len,
bc_vec_clear(&vm.files);
bc_vec_clear(&vm.exprs);
+ bc_vec_init(&vm.temps, sizeof(BcNum), NULL);
+
bc_program_init(&vm.prog);
bc_parse_init(&vm.prs, &vm.prog, BC_PROG_MAIN);
@@ -879,27 +842,6 @@ void bc_vm_boot(int argc, char *argv[], const char *env_len,
if (BC_IS_POSIX) vm.flags &= ~(BC_FLAG_G);
#endif // BC_ENABLED
- BC_SIG_UNLOCK;
-
- bc_vm_exec();
-}
-#endif // !BC_ENABLE_LIBRARY
-
-void bc_vm_init(void) {
-
- BC_SIG_ASSERT_LOCKED;
-
- memcpy(vm.max_num, bc_num_bigdigMax,
- bc_num_bigdigMax_size * sizeof(BcDig));
- memcpy(vm.max2_num, bc_num_bigdigMax2,
- bc_num_bigdigMax2_size * sizeof(BcDig));
- bc_num_setup(&vm.max, vm.max_num, BC_NUM_BIGDIG_LOG10);
- bc_num_setup(&vm.max2, vm.max2_num, BC_NUM_BIGDIG_LOG10);
- vm.max.len = bc_num_bigdigMax_size;
- vm.max2.len = bc_num_bigdigMax2_size;
-
- bc_vec_init(&vm.temps, sizeof(BcNum), NULL);
-
vm.maxes[BC_PROG_GLOBALS_IBASE] = BC_NUM_MAX_POSIX_IBASE;
vm.maxes[BC_PROG_GLOBALS_OBASE] = BC_MAX_OBASE;
vm.maxes[BC_PROG_GLOBALS_SCALE] = BC_MAX_SCALE;
@@ -909,11 +851,11 @@ void bc_vm_init(void) {
#endif // BC_ENABLE_EXTRA_MATH && BC_ENABLE_RAND
#if BC_ENABLED
-#if !BC_ENABLE_LIBRARY
if (BC_IS_BC && !BC_IS_POSIX)
-#endif // !BC_ENABLE_LIBRARY
- {
vm.maxes[BC_PROG_GLOBALS_IBASE] = BC_NUM_MAX_IBASE;
- }
#endif // BC_ENABLED
+
+ BC_SIG_UNLOCK;
+
+ bc_vm_exec();
}