From 252df6c74c7be9b82aa89cdb4972f91c87b06184 Mon Sep 17 00:00:00 2001 From: Comrade Martin Miroslavovich Michalec Date: Mon, 27 Nov 2023 00:34:11 +0300 Subject: publish --- src/arith.c | 152 ++++++++++++++++++++ src/arith.h | 48 +++++++ src/main.c | 105 ++++++++++++++ src/ops.c | 459 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ops.h | 33 +++++ src/stack.c | 74 ++++++++++ src/stack.h | 17 +++ src/test.c | 30 ++++ src/utils.c | 48 +++++++ src/utils.h | 48 +++++++ 10 files changed, 1014 insertions(+) create mode 100644 src/arith.c create mode 100644 src/arith.h create mode 100644 src/main.c create mode 100644 src/ops.c create mode 100644 src/ops.h create mode 100644 src/stack.c create mode 100644 src/stack.h create mode 100644 src/test.c create mode 100644 src/utils.c create mode 100644 src/utils.h (limited to 'src') diff --git a/src/arith.c b/src/arith.c new file mode 100644 index 0000000..bf7dbd1 --- /dev/null +++ b/src/arith.c @@ -0,0 +1,152 @@ +#include "arith.h" + +// A, B, C, D, E, F, G, H +u32 order = PACK8u32 (0, 1, 3, 2, 6, 7, 5, 4); + +#define PACK_ROW(OA, CA, OB, CB, OC, CC, OD, CD, OE, CE, OF, CF, OG, CG, OH, CH) \ + (((u64) OA) << 0*BPD | \ + ((u64) CA) << 1*BPD | \ + ((u64) OB) << 2*BPD | \ + ((u64) CB) << 3*BPD | \ + ((u64) OC) << 4*BPD | \ + ((u64) CC) << 5*BPD | \ + ((u64) OD) << 6*BPD | \ + ((u64) CD) << 7*BPD | \ + ((u64) OE) << 8*BPD | \ + ((u64) CE) << 9*BPD | \ + ((u64) OF) << 10*BPD | \ + ((u64) CF) << 11*BPD | \ + ((u64) OG) << 12*BPD | \ + ((u64) CG) << 13*BPD | \ + ((u64) OH) << 14*BPD | \ + ((u64) CH) << 15*BPD) +#define PACK_TABLE(OAA, OAB, OAC, OAD, OAE, OAF, OAG, OAH, \ + OBA, OBB, OBC, OBD, OBE, OBF, OBG, OBH, \ + OCA, OCB, OCC, OCD, OCE, OCF, OCG, OCH, \ + ODA, ODB, ODC, ODD, ODE, ODF, ODG, ODH, \ + OEA, OEB, OEC, OED, OEE, OEF, OEG, OEH, \ + OFA, OFB, OFC, OFD, OFE, OFF, OFG, OFH, \ + OGA, OGB, OGC, OGD, OGE, OGF, OGG, OGH, \ + OHA, OHB, OHC, OHD, OHE, OHF, OHG, OHH, \ + CAA, CAB, CAC, CAD, CAE, CAF, CAG, CAH, \ + CBA, CBB, CBC, CBD, CBE, CBF, CBG, CBH, \ + CCA, CCB, CCC, CCD, CCE, CCF, CCG, CCH, \ + CDA, CDB, CDC, CDD, CDE, CDF, CDG, CDH, \ + CEA, CEB, CEC, CED, CEE, CEF, CEG, CEH, \ + CFA, CFB, CFC, CFD, CFE, CFF, CFG, CFH, \ + CGA, CGB, CGC, CGD, CGE, CGF, CGG, CGH, \ + CHA, CHB, CHC, CHD, CHE, CHF, CHG, CHH) \ + { PACK_ROW (OAA, CAA, OAB, CAB, OAC, CAC, OAD, CAD, OAE, CAE, OAF, CAF, OAG, CAG, OAH, CAH), \ + PACK_ROW (OBA, CBA, OBB, CBB, OBC, CBC, OBD, CBD, OBE, CBE, OBF, CBF, OBG, CBG, OBH, CBH), \ + PACK_ROW (OCA, CCA, OCB, CCB, OCC, CCC, OCD, CCD, OCE, CCE, OCF, CCF, OCG, CCG, OCH, CCH), \ + PACK_ROW (ODA, CDA, ODB, CDB, ODC, CDC, ODD, CDD, ODE, CDE, ODF, CDF, ODG, CDG, ODH, CDH), \ + PACK_ROW (OEA, CEA, OEB, CEB, OEC, CEC, OED, CED, OEE, CEE, OEF, CEF, OEG, CEG, OEH, CEH), \ + PACK_ROW (OFA, CFA, OFB, CFB, OFC, CFC, OFD, CFD, OFE, CFE, OFF, CFF, OFG, CFG, OFH, CFH), \ + PACK_ROW (OGA, CGA, OGB, CGB, OGC, CGC, OGD, CGD, OGE, CGE, OGF, CGF, OGG, CGG, OGH, CGH), \ + PACK_ROW (OHA, CHA, OHB, CHB, OHC, CHC, OHD, CHD, OHE, CHE, OHF, CHF, OHG, CHG, OHH, CHH), } + +u64 addition_table[DIGIT_COUNT] = PACK_TABLE + (// A B C D E F G H + /* A */ A, B, C, D, E, F, G, H, // A + /* B */ B, D, H, C, F, A, E, G, // B + /* C */ C, H, E, G, B, D, A, F, // C + /* D */ D, C, G, H, A, B, F, E, // D + /* E */ E, F, B, A, H, G, C, D, // E + /* F */ F, A, D, B, G, E, H, C, // F + /* G */ G, E, A, F, C, H, D, B, // G + /* H */ H, G, F, E, D, C, B, A, // H + // A B C D E F G H + /* A */ A, A, A, A, A, A, A, A, // A + /* B */ A, A, A, A, A, B, A, A, // B + /* C */ A, A, A, A, B, B, B, A, // C + /* D */ A, A, A, A, B, B, A, A, // D + /* E */ A, A, B, B, B, B, B, B, // E + /* F */ A, B, B, B, B, B, B, B, // F + /* G */ A, A, B, A, B, B, B, B, // G + /* H */ A, A, A, A, B, B, B, B); // H + +u64 multiplication_table[DIGIT_COUNT] = PACK_TABLE + (// A B C D E F G H + /* A */ A, A, A, A, A, A, A, A, // A + /* B */ A, B, C, D, E, F, G, H, // B + /* C */ A, C, B, E, D, C, F, H, // C + /* D */ A, D, E, H, H, E, D, A, // D + /* E */ A, E, D, H, H, D, E, A, // E + /* F */ A, F, C, E, D, B, C, H, // F + /* G */ A, G, F, D, E, C, B, H, // G + /* H */ A, H, H, A, A, H, H, A, // H + // A B C D E F G H + /* A */ A, A, A, A, A, A, A, A, // A + /* B */ A, A, A, A, A, A, A, A, // B + /* C */ A, A, B, A, D, D, B, B, // C + /* D */ A, A, A, A, B, B, B, B, // D + /* E */ A, A, D, B, H, G, C, C, // E + /* F */ A, A, D, B, G, E, H, C, // F + /* G */ A, A, B, B, C, H, C, D, // G + /* H */ A, A, B, B, C, C, D, D); // H + +u32 +str2num (const char *str) +{ + u32 num = 0; + + char sign = *str++; + while (*str) { + num <<= BPD; + num |= C2I (*str++); + } + + if (sign == '-') + num |= MASK_NEG; + + return num; +} + +int +arith_cmp (u32 a, u32 b) +{ + if (a == b) return 0; + if ( (a & MASK_NEG) && !(b & MASK_NEG)) return -1; + if (!(a & MASK_NEG) && (b & MASK_NEG)) return 1; + if ( (a & MASK_NEG) && (b & MASK_NEG)) { + u32 tmp = a ^ MASK_NEG; + a = b ^ MASK_NEG; + b = tmp; + } + + int diff = 0; + + for (int i = DIGIT_COUNT; i; --i) { + diff += (UNPACK8u32 (order, a >> (i - 1)*BPD & DIGIT_MASK) - + UNPACK8u32 (order, b >> (i - 1)*BPD & DIGIT_MASK)); + diff *= DIGIT_COUNT; // << 3 + } + + return diff; +} + +u32 +arith_min (u32 a, u32 b) +{ + return arith_cmp (a, b) <= 0 ? a : b; +} + +u32 +arith_max (u32 a, u32 b) +{ + return arith_cmp (a, b) >= 0 ? a : b; +} + +s32 +arith2bin (u32 a) +{ + s32 bin = 0; + + for (int i = 0; i < 8; ++i) { + bin >>= 3; + bin |= (UNPACK8u32 (order, a & 0b111)) << 3*7; + a >>= BPD; + } + + return bin; +} diff --git a/src/arith.h b/src/arith.h new file mode 100644 index 0000000..f43c830 --- /dev/null +++ b/src/arith.h @@ -0,0 +1,48 @@ +#ifndef ARITH_H +#define ARITH_H + +#include "utils.h" + +#define BPD 4 +// #define DIGIT_MASK ((1 << BPD) - 1) +#define DIGIT_MASK 0b111 + +#define I2C(I) ((I) + 'a') +#define C2I(C) ((C) - 'a') + +#define PACK8u32(A, B, C, D, E, F, G, H) \ + ((A) << BPD*0 | \ + (B) << BPD*1 | \ + (C) << BPD*2 | \ + (D) << BPD*3 | \ + (E) << BPD*4 | \ + (F) << BPD*5 | \ + (G) << BPD*6 | \ + (H) << BPD*7) +#define UNPACK8u32(V, I) ((V) >> BPD*(I) & DIGIT_MASK) + +#define TBL_LOOKUP(TBL, ROW, COL) ((TBL)[(ROW)] >> (COL)*2*BPD) +#define TBL_VALUE(RET) ((RET) & DIGIT_MASK) +#define TBL_CARRY(RET) ((RET) >> BPD & DIGIT_MASK) + +#define MASK_NEG 0x80000000 +#define MASK_OVF 0x08000000 +#define MASK_NAN 0x00800000 +#define MASK_INF 0x00080000 +#define MASK_UNI 0x00008000 +#define MASK_EMP 0x00000800 +#define MASK_FLAGS (MASK_NEG | MASK_OVF | MASK_NAN | MASK_INF | MASK_UNI | MASK_EMP) + +enum digit { A, B, C, D, E, F, G, H, DIGIT_COUNT }; + +extern u32 order; +extern u64 addition_table[DIGIT_COUNT]; +extern u64 multiplication_table[DIGIT_COUNT]; + +u32 str2num (const char *str); +u32 arith_min (u32 a, u32 b); +u32 arith_max (u32 a, u32 b); +int arith_cmp (u32 a, u32 b); +s32 arith2bin (u32 a); + +#endif diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..54b7ac3 --- /dev/null +++ b/src/main.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include +#include "utils.h" +#include "arith.h" +#include "stack.h" +#include "ops.h" + +void +print_number (u32 num) +{ + char sign = num & MASK_NEG && num & MASK_FLAGS ? '-' : ' '; + num &= ~MASK_FLAGS; + u8 offset; + printf (" "); + for (offset = 7*BPD; offset; offset -= BPD) { + if (num >> offset & DIGIT_MASK) + break; + putchar (' '); + } + printf ("%c", sign); + for (; offset; offset -= BPD) + printf ("%c", I2C (num >> offset & DIGIT_MASK)); + printf ("%c\n", I2C (num & DIGIT_MASK)); +} + +static void +print_stack (void) +{ + const u32 *ptr = (u32 *) stack_base (); + const u32 *end = (u32 *) stack_end (); + + while (ptr != end) + print_number (*ptr++); +} + +int +main (int argc, char *argv[]) +{ + stack_init (sizeof (u32)); + + const char *regexp = "^\\s*([+-][a-h]{1,8}|nop|dup|del|clr|swp|neg|add|mul|sub|div|mod|inc|dec|pow|gcd|lcm)\\s*$"; + regex_t compiled_regexp; + int errcode = regcomp (&compiled_regexp, regexp, REG_EXTENDED | REG_NOSUB); + if (errcode != 0) { + size_t length = regerror (errcode, &compiled_regexp, NULL, 0); + char *buffer = malloc (length); + (void) regerror (errcode, &compiled_regexp, buffer, length); + DIE (EXIT_FAILURE, "regcomp: %s", buffer); + } + + char *line = NULL; + size_t line_size = 0; + ssize_t line_len; + enum action { + ACTION_NONE, + ACTION_QUOTE, + ACTION_OP, + }; + enum action last_action = ACTION_NONE; + u32 last_num; + op_fn last_op = op_fns[OP_NOP]; + for (;;) { + printf ("> "); + line_len = getline (&line, &line_size, stdin); + line[--line_len] = '\0'; + + if (line_len == 0) { + switch (last_action) { + case ACTION_NONE: + break; + case ACTION_QUOTE: + stack_push_u32 (last_num); + break; + case ACTION_OP: + last_op (); + break; + } + } else { + if (regexec (&compiled_regexp, line, 0, NULL, 0) != 0) { + ERROR ("input doesn't match regexp `%s'", regexp); + continue; + } + + if (line[0] == '+' || + line[0] == '-') { + last_action = ACTION_QUOTE; + last_num = str2num (line); + stack_push_u32 (last_num); + } else { + last_action = ACTION_OP; + last_op = op_fns[str2op (line)]; + last_op (); + } + } + + print_stack (); + NEWLINE (); + } free (line); + + regfree (&compiled_regexp); + stack_free (); + return EXIT_SUCCESS; +} diff --git a/src/ops.c b/src/ops.c new file mode 100644 index 0000000..9e8f84d --- /dev/null +++ b/src/ops.c @@ -0,0 +1,459 @@ +#include "ops.h" +#include "stack.h" +#include "arith.h" +#include + +void print_number (u32 num); + +static bool +binary_op_p (const char *name) +{ + if (stack_count_u32 () < 2) { + ERROR ("%s operation requires at least 2 arguments on the stack", name); + return false; + } return true; +} + +static bool +unary_op_p (const char *name) +{ + if (stack_count_u32 () < 1) { + ERROR ("%s operation requires at least 1 argument on the stack", name); + return false; + } return true; +} + +static bool +op_ovf_p (u32 a) +{ + if (a & MASK_OVF) { + ERROR ("operation has overflown"); + return true; + } return false; +} + +typedef u32 (*binary_op_fn) (u32, u32); +static void +binary_op (const char *name, binary_op_fn op) +{ + if (!binary_op_p (name)) return; + + u32 rhs = stack_pop_u32 (); + u32 lhs = stack_pop_u32 (); + u32 res = op (lhs, rhs); + + if (res & MASK_NAN) { + INFO ("operation result is not a number"); + return; + } + + if (res & MASK_EMP) { + INFO ("operation resulted in empty set"); + return; + } + + if (res & MASK_UNI) { + INFO ("operation result is a range: [-ffffffff; ffffffff]"); + return; + } + + if (res & MASK_INF) { + INFO ("operation resulted in infinity"); + res = 0x77777777; + } + + if (!op_ovf_p (res)) + stack_push_u32 (res); +} + +static void +op_nop (void) {} + +static void +op_dup (void) +{ + if (!unary_op_p ("duplication")) return; + stack_push_u32 (stack_top_u32 ()); +} + +static bool +stack_already_empty () +{ + if (stack_count_u32 () == 0) { + INFO ("stack is already empty"); + return true; + } return false; +} + +static void +op_del (void) +{ + if (stack_already_empty ()) return; + stack_discard_u32 (); +} + +static void +op_clr (void) +{ + if (stack_already_empty ()) return; + stack_clear (); +} + +static void +op_swp (void) +{ + if (!binary_op_p ("swap")) return; + + u32 n1 = stack_pop_u32 (); + u32 n2 = stack_pop_u32 (); + + stack_push_u32 (n1); + stack_push_u32 (n2); +} + +static void +op_neg (void) +{ + if (!unary_op_p ("negation")) return; + u32 num = stack_pop_u32 (); + stack_push_u32 (num ^ MASK_NEG); +} + +static u32 sub (u32 lhs, u32 rhs); + +static u32 +add (u32 lhs, u32 rhs) +{ + if (lhs & MASK_NEG && rhs & MASK_NEG) + return add (lhs & ~MASK_NEG, rhs & ~MASK_NEG) | MASK_NEG; + if (lhs & MASK_NEG) return sub (rhs, lhs ^ MASK_NEG); + if (rhs & MASK_NEG) return sub (lhs, rhs ^ MASK_NEG); + + u32 res = 0; + u8 carry = 0; + for (u8 offset = 0; lhs | rhs | carry && offset < BPD*8; offset += BPD) { + u8 lhsd = lhs & DIGIT_MASK; + u8 rhsd = rhs & DIGIT_MASK; + + u8 step1 = TBL_LOOKUP (addition_table, TBL_VALUE (rhsd ), TBL_VALUE (carry)); + u8 step2 = TBL_LOOKUP (addition_table, TBL_VALUE (step1), TBL_VALUE (lhsd )); + u8 step3 = TBL_LOOKUP (addition_table, TBL_CARRY (step1), TBL_CARRY (step2)); + res |= TBL_VALUE (step2) << offset; + carry = TBL_VALUE (step3); + + lhs >>= BPD; + rhs >>= BPD; + } + + if (carry) + res |= MASK_OVF; + + return res; +} + +static u32 +sub (u32 lhs, u32 rhs) +{ + if (lhs & MASK_NEG && rhs & MASK_NEG) { + lhs ^= MASK_NEG; + rhs ^= MASK_NEG; + SWAP (lhs, rhs); + } else { + if (lhs & MASK_NEG) return add (rhs, lhs ^ MASK_NEG) | MASK_NEG; + if (rhs & MASK_NEG) return add (lhs, rhs ^ MASK_NEG); + } + if (arith_cmp (lhs, rhs) < 0) + return sub (rhs, lhs) | MASK_NEG; + + u32 res = 0; + u8 carry = 0; + for (u8 offset = 0; lhs | rhs | carry && offset < BPD*8; offset += BPD) { + u8 lhsd = lhs & DIGIT_MASK; + u8 rhsd = rhs & DIGIT_MASK; + u8 buf; + + rhsd = TBL_LOOKUP (addition_table, + TBL_VALUE (rhsd), + TBL_VALUE (carry)); + u64 row = addition_table[TBL_VALUE (rhsd)]; + for (u8 i = 0; i < DIGIT_COUNT; ++i) { + if ((row >> 2*BPD*i & DIGIT_MASK) == lhsd) { + carry = TBL_VALUE (TBL_LOOKUP (addition_table, + TBL_CARRY (rhsd), + TBL_CARRY (row >> 2*BPD*i))); + res |= i << offset; + break; + } + } + + lhs >>= BPD; + rhs >>= BPD; + } + + if (carry) + res |= MASK_OVF; + + return res; +} + +static void +op_add (void) { binary_op ("addition", add); } + +static u32 +mul (u32 lhs, u32 rhs) +{ + if (lhs & MASK_NEG && rhs & MASK_NEG) + return mul (lhs ^ MASK_NEG, rhs ^ MASK_NEG); + if (lhs & MASK_NEG) return mul (rhs, lhs ^ MASK_NEG) | MASK_NEG; + if (rhs & MASK_NEG) return mul (lhs, rhs ^ MASK_NEG) | MASK_NEG; + + bool flag_ovf = false; + u32 res = A; + for (u8 i = 0; rhs; rhs >>= BPD, ++i) { + u8 rhsd = rhs & DIGIT_MASK; + u32 lhsc = lhs; + u32 inter = A; + u8 carry = A; + for (u8 offset = 0; lhsc || carry && offset < BPD*8; lhsc >>= BPD, offset += BPD) { + u8 lhsd = lhsc & DIGIT_MASK; + u8 step1 = TBL_LOOKUP (multiplication_table, TBL_VALUE (rhsd ), TBL_VALUE (lhsd )); + u8 step2 = TBL_LOOKUP ( addition_table, TBL_VALUE (step1), TBL_VALUE (carry)); + u8 step3 = TBL_LOOKUP ( addition_table, TBL_CARRY (step1), TBL_CARRY (step2)); + inter |= TBL_VALUE (step2) << offset; + carry = TBL_VALUE (step3); + } + res = add (res, inter << BPD*i); + if (carry || CLZ (inter) < BPD*i || res & MASK_OVF) + flag_ovf = true; + } + + res |= flag_ovf * MASK_OVF; + + return res; +} + +static void +op_mul (void) { binary_op ("multiplication", mul); } + +static void +op_sub (void) { binary_op ("substraction", sub); } + +static u32 +div (u32 lhs, u32 rhs) +{ + if (lhs == A && rhs == A) { + INFO ("dividing zero by zero"); + return MASK_UNI; + } + if (rhs == A) { + INFO ("division by zero"); + return MASK_EMP; + } + + if (lhs & MASK_NEG && + rhs & MASK_NEG) { + return add (B, + div (sub (lhs ^ MASK_NEG, + B), + rhs ^ MASK_NEG)); + } + if (lhs & MASK_NEG) { + return add (B, + div (sub (lhs ^ MASK_NEG, + B), + rhs)) | MASK_NEG; + } + if (rhs & MASK_NEG) { + return div (lhs, + rhs ^ MASK_NEG) | MASK_NEG; + } + + + u32 res = A; + u32 buf = A; + for (u32 inc = B << 7*BPD; inc; inc >>= BPD) { + for (;;) { + u32 mul_res = mul (rhs, inc); + if (mul_res & MASK_OVF) + break; + + u32 old = buf; + buf = add (buf, mul_res); + if (buf & MASK_OVF || arith_cmp (buf, lhs) > 0) { + buf = old; + break; + } + res = add (res, inc); + } + } + + return res; +} + +static void +op_div (void) { binary_op ("division", div); } + +static u32 +mod (u32 lhs, u32 rhs) +{ + if (rhs == A) { + ERROR ("division by zero"); + return MASK_NAN; + } + + return sub (lhs, + mul (div (lhs, rhs), + rhs)); +} + +static void +op_mod (void) { binary_op ("modulo", mod); } + +static void +op_inc (void) +{ + if (!unary_op_p ("incrementation")) return; + + u32 lhs = stack_pop_u32 (); + u32 rhs = B; + u32 res = add (lhs, rhs); + + if (!op_ovf_p (res)) + stack_push_u32 (res); +} + +static void +op_dec (void) +{ + if (!unary_op_p ("decrementation")) return; + + u32 lhs = stack_pop_u32 (); + u32 rhs = B; + u32 res = sub (lhs, rhs); + + if (!op_ovf_p (res)) + stack_push_u32 (res); +} + +static u32 +pow (u32 lhs, u32 rhs) +{ + u32 res = B; + u32 (*op) (u32, u32) = rhs & MASK_NEG ? div : mul; + rhs &= ~MASK_NEG; + + while (rhs) { + rhs = sub (rhs, B); + res = op (res, lhs); + } + + return res; +} + +static void +op_pow (void) { binary_op ("power", pow); } + +static u32 +gcd (u32 a, u32 b) +{ + if (a == A || b == A) { + ERROR ("input out of range"); + return MASK_NAN; + } + + while (b) { + u32 tmp = b; + b = mod (a, b); + a = tmp; + } + + return a; +} + +static void +op_gcd (void) { binary_op ("GCD", gcd); } + +static u32 +lcm (u32 a, u32 b) +{ + return div (mul (a, b), gcd (a, b)); +} + +static void +op_lcm (void) { binary_op ("LCM", lcm); } + +op_fn op_fns[] = { + [OP_NOP] = op_nop, + [OP_DUP] = op_dup, + [OP_DEL] = op_del, + [OP_CLR] = op_clr, + [OP_SWP] = op_swp, + [OP_NEG] = op_neg, + [OP_ADD] = op_add, + [OP_MUL] = op_mul, + [OP_SUB] = op_sub, + [OP_DIV] = op_div, + [OP_MOD] = op_mod, + [OP_INC] = op_inc, + [OP_DEC] = op_dec, + [OP_POW] = op_pow, + [OP_GCD] = op_gcd, + [OP_LCM] = op_lcm, +}; + +const char *op_names[] = { + [OP_NOP] = "nop", + [OP_DUP] = "dup", + [OP_DEL] = "del", + [OP_CLR] = "clr", + [OP_SWP] = "swp", + [OP_NEG] = "neg", + [OP_ADD] = "add", + [OP_MUL] = "mul", + [OP_SUB] = "sub", + [OP_DIV] = "div", + [OP_MOD] = "mod", + [OP_INC] = "inc", + [OP_DEC] = "dec", + [OP_POW] = "pow", + [OP_GCD] = "gcd", + [OP_LCM] = "lcm", +}; + +enum op +str2op (const char *str) +{ + for (int i = 0; i < OP_COUNT; ++i) { + if (strcmp (str, op_names[i]) == 0) + return i; + } return OP_COUNT; +} + +void +ops_test (void) +{ + for (u32 lhs = PACK8u32 (F, F, F, F, F, F, F, F)|MASK_NEG; + arith_cmp (lhs, PACK8u32 (F, F, F, F, F, F, F, F)) < 0; + lhs = add (lhs, F)) { + for (u32 rhs = PACK8u32 (F, F, F, F, F, F, F, F)|MASK_NEG; + arith_cmp (rhs, PACK8u32 (F, F, F, F, F, F, F, F)) < 0; + rhs = add (rhs, F)) { + u32 a = mul (lhs, rhs); + if (a & MASK_OVF) continue; + u32 b = div (a, rhs); + if (b & MASK_OVF|MASK_EMP) continue; + u32 c = div (a, lhs); + if (c & MASK_OVF|MASK_EMP) continue; + if (b != lhs || + c != rhs) { + puts ("Test failed for:"); + printf ("lhs: "); print_number (lhs); + printf ("rhs: "); print_number (rhs); + printf ("a: "); print_number (a ); + printf ("b: "); print_number (b ); + printf ("c: "); print_number (c ); + putchar ('\n'); + } else puts ("OK"); + } + } +} diff --git a/src/ops.h b/src/ops.h new file mode 100644 index 0000000..e8eface --- /dev/null +++ b/src/ops.h @@ -0,0 +1,33 @@ +#ifndef OPS_H +#define OPS_H + +#include + +enum op { + OP_NOP, + OP_DUP, + OP_DEL, + OP_CLR, + OP_SWP, + OP_NEG, + OP_ADD, + OP_MUL, + OP_SUB, + OP_DIV, + OP_MOD, + OP_INC, + OP_DEC, + OP_POW, + OP_GCD, + OP_LCM, + OP_COUNT, +}; + +typedef void (*op_fn) (void); + +extern op_fn op_fns[OP_COUNT]; +extern const char *op_names[OP_COUNT]; + +enum op str2op (const char *str); + +#endif diff --git a/src/stack.c b/src/stack.c new file mode 100644 index 0000000..768b1f8 --- /dev/null +++ b/src/stack.c @@ -0,0 +1,74 @@ +#include "stack.h" +#include +#include + +#define obstack_chunk_alloc malloc +#define obstack_chunk_free free + +static struct obstack obstack; + +void +stack_init (int alignment_mask) +{ + obstack_init (&obstack); + obstack_alignment_mask (&obstack) = alignment_mask; +} + +void +stack_free (void) +{ + obstack_free (&obstack, NULL); +} + +int +stack_count_u32 (void) +{ + return obstack_object_size (&obstack)/sizeof (u32); +} + +void +stack_push_u32 (u32 num) +{ + obstack_grow (&obstack, &num, sizeof (num)); +} + +u32 +stack_pop_u32 (void) +{ + u32 val = *(((u32*) obstack_next_free (&obstack)) - 1); + obstack_blank (&obstack, -((int) sizeof (u32))); + return val; +} + +void +stack_discard_u32 (void) +{ + if (obstack_object_size (&obstack) == 0) + return; + + obstack_blank (&obstack, -((int) sizeof (u32))); +} + +void +stack_clear (void) +{ + obstack_blank (&obstack, -obstack_object_size (&obstack)); +} + +u32 +stack_top_u32 (void) +{ + return *(((u32*) obstack_next_free (&obstack)) - 1); +} + +void * +stack_base (void) +{ + return obstack_base (&obstack); +} + +void * +stack_end (void) +{ + return obstack_next_free (&obstack); +} diff --git a/src/stack.h b/src/stack.h new file mode 100644 index 0000000..d30f81b --- /dev/null +++ b/src/stack.h @@ -0,0 +1,17 @@ +#ifndef STACK_H +#define STACK_H + +#include "utils.h" + +void stack_init (int alignment_mask); +void stack_free (void); +int stack_count_u32 (void); +void stack_push_u32 (u32 num); +u32 stack_pop_u32 (void); +void stack_discard_u32 (void); +void stack_clear (void); +u32 stack_top_u32 (void); +void *stack_base (void); +void *stack_end (void); + +#endif diff --git a/src/test.c b/src/test.c new file mode 100644 index 0000000..77ae5fb --- /dev/null +++ b/src/test.c @@ -0,0 +1,30 @@ +#include +#include "utils.h" +#include "arith.h" + +void ops_test (void); + +void +print_number (u32 num) +{ + char sign = num & MASK_NEG ? '-' : ' '; + num &= ~MASK_FLAGS; + u8 offset; + for (offset = 7*BPD; offset; offset -= BPD) { + if (num >> offset & DIGIT_MASK) + break; + putchar (' '); + } + printf ("%c", sign); + for (; offset; offset -= BPD) + printf ("%c", I2C (num >> offset & DIGIT_MASK)); + printf ("%c\n", I2C (num & DIGIT_MASK)); +} + +int +main (void) +{ + ops_test (); + + return EXIT_SUCCESS; +} diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..49306bb --- /dev/null +++ b/src/utils.c @@ -0,0 +1,48 @@ +#include "utils.h" +#include +#include +#include +#include +#include + +int +input (const char *message, const char *regex, const char *template, ...) +{ + char *line = NULL; + size_t line_size = 0; + regex_t compiled; + int matches = 0; + int c; + va_list ap; + + va_start (ap, template); + vprintf (message, ap); + printf (" (\"%s\"): ", regex); + getline (&line, &line_size, stdin); + line[strlen (line) - 1] = '\0'; + + regcomp (&compiled, regex, REG_EXTENDED | REG_NOSUB); + if (regexec (&compiled, line, 0, NULL, 0)) + goto defer; + + matches = vsscanf (line, template, ap); + va_end (ap); + + defer: + free (line); + return matches; +} + +uint +log10ui (uint ui) +{ + return ((ui >= 1000000000u) ? 9 : + (ui >= 100000000u) ? 8 : + (ui >= 10000000u) ? 7 : + (ui >= 1000000u) ? 6 : + (ui >= 100000u) ? 5 : + (ui >= 10000u) ? 4 : + (ui >= 1000u) ? 3 : + (ui >= 100u) ? 2 : + (ui >= 10u) ? 1 : 0u); +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..c443b52 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,48 @@ +#ifndef UTILS_H +#define UTILS_H + +#include +#include + +#define MAX(a, b) ({ \ + typeof (a) _a = (a); \ + typeof (b) _b = (b); \ + _a > _b ? _a : _b; \ + }) + +#define MIN(a, b) ({ \ + typeof (a) _a = (a); \ + typeof (b) _b = (b); \ + _a < _b ? _a : _b; \ + }) + +#define SWAP(a, b) ({ \ + typeof (a) tmp = (a); \ + (a) = (b); \ + (b) = tmp; \ + }) + +#define CLZ(X) __builtin_clz (X) +#define CTZ(X) __builtin_ctz (X) +#define POPCOUNT(X) __builtin_popcount (X) + +#define NEWLINE() putchar ('\n') +#define PAGEBREAK() putchar ('\n'); + +#define SUCCESS(MSG, ...) fprintf (stdout, "[+] SUCCESS: "MSG"\n", ##__VA_ARGS__) +#define INFO(MSG, ...) fprintf (stdout, "[*] INFO: " MSG"\n", ##__VA_ARGS__) +#define ERROR(MSG, ...) fprintf (stderr, "[-] ERROR: " MSG"\n", ##__VA_ARGS__) + +#define DIE(EXIT_CODE, MSG, ...) ({ ERROR (MSG, ##__VA_ARGS__); exit (EXIT_CODE); }) + +typedef unsigned int uint; +typedef uint8_t u8; +typedef int8_t s8; +typedef uint32_t u32; +typedef int32_t s32; +typedef uint64_t u64; + +int input (const char *message, const char *regex, const char *template, ...); +uint log10ui (uint ui); + +#endif -- cgit v1.3.1