diff options
| author | 2023-11-26 21:34:11 +0000 | |
|---|---|---|
| committer | 2026-11-07 02:13:38 +0000 | |
| commit | 252df6c74c7be9b82aa89cdb4972f91c87b06184 (patch) | |
| tree | 988497c67eb3d8d8789aa9bdea47bafd300e25b2 /src | |
| download | coursework-master.tar.gz | |
Diffstat (limited to 'src')
| -rw-r--r-- | src/arith.c | 152 | ||||
| -rw-r--r-- | src/arith.h | 48 | ||||
| -rw-r--r-- | src/main.c | 105 | ||||
| -rw-r--r-- | src/ops.c | 459 | ||||
| -rw-r--r-- | src/ops.h | 33 | ||||
| -rw-r--r-- | src/stack.c | 74 | ||||
| -rw-r--r-- | src/stack.h | 17 | ||||
| -rw-r--r-- | src/test.c | 30 | ||||
| -rw-r--r-- | src/utils.c | 48 | ||||
| -rw-r--r-- | src/utils.h | 48 |
10 files changed, 1014 insertions, 0 deletions
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 @@ | |||
| 1 | #include "arith.h" | ||
| 2 | |||
| 3 | // A, B, C, D, E, F, G, H | ||
| 4 | u32 order = PACK8u32 (0, 1, 3, 2, 6, 7, 5, 4); | ||
| 5 | |||
| 6 | #define PACK_ROW(OA, CA, OB, CB, OC, CC, OD, CD, OE, CE, OF, CF, OG, CG, OH, CH) \ | ||
| 7 | (((u64) OA) << 0*BPD | \ | ||
| 8 | ((u64) CA) << 1*BPD | \ | ||
| 9 | ((u64) OB) << 2*BPD | \ | ||
| 10 | ((u64) CB) << 3*BPD | \ | ||
| 11 | ((u64) OC) << 4*BPD | \ | ||
| 12 | ((u64) CC) << 5*BPD | \ | ||
| 13 | ((u64) OD) << 6*BPD | \ | ||
| 14 | ((u64) CD) << 7*BPD | \ | ||
| 15 | ((u64) OE) << 8*BPD | \ | ||
| 16 | ((u64) CE) << 9*BPD | \ | ||
| 17 | ((u64) OF) << 10*BPD | \ | ||
| 18 | ((u64) CF) << 11*BPD | \ | ||
| 19 | ((u64) OG) << 12*BPD | \ | ||
| 20 | ((u64) CG) << 13*BPD | \ | ||
| 21 | ((u64) OH) << 14*BPD | \ | ||
| 22 | ((u64) CH) << 15*BPD) | ||
| 23 | #define PACK_TABLE(OAA, OAB, OAC, OAD, OAE, OAF, OAG, OAH, \ | ||
| 24 | OBA, OBB, OBC, OBD, OBE, OBF, OBG, OBH, \ | ||
| 25 | OCA, OCB, OCC, OCD, OCE, OCF, OCG, OCH, \ | ||
| 26 | ODA, ODB, ODC, ODD, ODE, ODF, ODG, ODH, \ | ||
| 27 | OEA, OEB, OEC, OED, OEE, OEF, OEG, OEH, \ | ||
| 28 | OFA, OFB, OFC, OFD, OFE, OFF, OFG, OFH, \ | ||
| 29 | OGA, OGB, OGC, OGD, OGE, OGF, OGG, OGH, \ | ||
| 30 | OHA, OHB, OHC, OHD, OHE, OHF, OHG, OHH, \ | ||
| 31 | CAA, CAB, CAC, CAD, CAE, CAF, CAG, CAH, \ | ||
| 32 | CBA, CBB, CBC, CBD, CBE, CBF, CBG, CBH, \ | ||
| 33 | CCA, CCB, CCC, CCD, CCE, CCF, CCG, CCH, \ | ||
| 34 | CDA, CDB, CDC, CDD, CDE, CDF, CDG, CDH, \ | ||
| 35 | CEA, CEB, CEC, CED, CEE, CEF, CEG, CEH, \ | ||
| 36 | CFA, CFB, CFC, CFD, CFE, CFF, CFG, CFH, \ | ||
| 37 | CGA, CGB, CGC, CGD, CGE, CGF, CGG, CGH, \ | ||
| 38 | CHA, CHB, CHC, CHD, CHE, CHF, CHG, CHH) \ | ||
| 39 | { PACK_ROW (OAA, CAA, OAB, CAB, OAC, CAC, OAD, CAD, OAE, CAE, OAF, CAF, OAG, CAG, OAH, CAH), \ | ||
| 40 | PACK_ROW (OBA, CBA, OBB, CBB, OBC, CBC, OBD, CBD, OBE, CBE, OBF, CBF, OBG, CBG, OBH, CBH), \ | ||
| 41 | PACK_ROW (OCA, CCA, OCB, CCB, OCC, CCC, OCD, CCD, OCE, CCE, OCF, CCF, OCG, CCG, OCH, CCH), \ | ||
| 42 | PACK_ROW (ODA, CDA, ODB, CDB, ODC, CDC, ODD, CDD, ODE, CDE, ODF, CDF, ODG, CDG, ODH, CDH), \ | ||
| 43 | PACK_ROW (OEA, CEA, OEB, CEB, OEC, CEC, OED, CED, OEE, CEE, OEF, CEF, OEG, CEG, OEH, CEH), \ | ||
| 44 | PACK_ROW (OFA, CFA, OFB, CFB, OFC, CFC, OFD, CFD, OFE, CFE, OFF, CFF, OFG, CFG, OFH, CFH), \ | ||
| 45 | PACK_ROW (OGA, CGA, OGB, CGB, OGC, CGC, OGD, CGD, OGE, CGE, OGF, CGF, OGG, CGG, OGH, CGH), \ | ||
| 46 | PACK_ROW (OHA, CHA, OHB, CHB, OHC, CHC, OHD, CHD, OHE, CHE, OHF, CHF, OHG, CHG, OHH, CHH), } | ||
| 47 | |||
| 48 | u64 addition_table[DIGIT_COUNT] = PACK_TABLE | ||
| 49 | (// A B C D E F G H | ||
| 50 | /* A */ A, B, C, D, E, F, G, H, // A | ||
| 51 | /* B */ B, D, H, C, F, A, E, G, // B | ||
| 52 | /* C */ C, H, E, G, B, D, A, F, // C | ||
| 53 | /* D */ D, C, G, H, A, B, F, E, // D | ||
| 54 | /* E */ E, F, B, A, H, G, C, D, // E | ||
| 55 | /* F */ F, A, D, B, G, E, H, C, // F | ||
| 56 | /* G */ G, E, A, F, C, H, D, B, // G | ||
| 57 | /* H */ H, G, F, E, D, C, B, A, // H | ||
| 58 | // A B C D E F G H | ||
| 59 | /* A */ A, A, A, A, A, A, A, A, // A | ||
| 60 | /* B */ A, A, A, A, A, B, A, A, // B | ||
| 61 | /* C */ A, A, A, A, B, B, B, A, // C | ||
| 62 | /* D */ A, A, A, A, B, B, A, A, // D | ||
| 63 | /* E */ A, A, B, B, B, B, B, B, // E | ||
| 64 | /* F */ A, B, B, B, B, B, B, B, // F | ||
| 65 | /* G */ A, A, B, A, B, B, B, B, // G | ||
| 66 | /* H */ A, A, A, A, B, B, B, B); // H | ||
| 67 | |||
| 68 | u64 multiplication_table[DIGIT_COUNT] = PACK_TABLE | ||
| 69 | (// A B C D E F G H | ||
| 70 | /* A */ A, A, A, A, A, A, A, A, // A | ||
| 71 | /* B */ A, B, C, D, E, F, G, H, // B | ||
| 72 | /* C */ A, C, B, E, D, C, F, H, // C | ||
| 73 | /* D */ A, D, E, H, H, E, D, A, // D | ||
| 74 | /* E */ A, E, D, H, H, D, E, A, // E | ||
| 75 | /* F */ A, F, C, E, D, B, C, H, // F | ||
| 76 | /* G */ A, G, F, D, E, C, B, H, // G | ||
| 77 | /* H */ A, H, H, A, A, H, H, A, // H | ||
| 78 | // A B C D E F G H | ||
| 79 | /* A */ A, A, A, A, A, A, A, A, // A | ||
| 80 | /* B */ A, A, A, A, A, A, A, A, // B | ||
| 81 | /* C */ A, A, B, A, D, D, B, B, // C | ||
| 82 | /* D */ A, A, A, A, B, B, B, B, // D | ||
| 83 | /* E */ A, A, D, B, H, G, C, C, // E | ||
| 84 | /* F */ A, A, D, B, G, E, H, C, // F | ||
| 85 | /* G */ A, A, B, B, C, H, C, D, // G | ||
| 86 | /* H */ A, A, B, B, C, C, D, D); // H | ||
| 87 | |||
| 88 | u32 | ||
| 89 | str2num (const char *str) | ||
| 90 | { | ||
| 91 | u32 num = 0; | ||
| 92 | |||
| 93 | char sign = *str++; | ||
| 94 | while (*str) { | ||
| 95 | num <<= BPD; | ||
| 96 | num |= C2I (*str++); | ||
| 97 | } | ||
| 98 | |||
| 99 | if (sign == '-') | ||
| 100 | num |= MASK_NEG; | ||
| 101 | |||
| 102 | return num; | ||
| 103 | } | ||
| 104 | |||
| 105 | int | ||
| 106 | arith_cmp (u32 a, u32 b) | ||
| 107 | { | ||
| 108 | if (a == b) return 0; | ||
| 109 | if ( (a & MASK_NEG) && !(b & MASK_NEG)) return -1; | ||
| 110 | if (!(a & MASK_NEG) && (b & MASK_NEG)) return 1; | ||
| 111 | if ( (a & MASK_NEG) && (b & MASK_NEG)) { | ||
| 112 | u32 tmp = a ^ MASK_NEG; | ||
| 113 | a = b ^ MASK_NEG; | ||
| 114 | b = tmp; | ||
| 115 | } | ||
| 116 | |||
| 117 | int diff = 0; | ||
| 118 | |||
| 119 | for (int i = DIGIT_COUNT; i; --i) { | ||
| 120 | diff += (UNPACK8u32 (order, a >> (i - 1)*BPD & DIGIT_MASK) - | ||
| 121 | UNPACK8u32 (order, b >> (i - 1)*BPD & DIGIT_MASK)); | ||
| 122 | diff *= DIGIT_COUNT; // << 3 | ||
| 123 | } | ||
| 124 | |||
| 125 | return diff; | ||
| 126 | } | ||
| 127 | |||
| 128 | u32 | ||
| 129 | arith_min (u32 a, u32 b) | ||
| 130 | { | ||
| 131 | return arith_cmp (a, b) <= 0 ? a : b; | ||
| 132 | } | ||
| 133 | |||
| 134 | u32 | ||
| 135 | arith_max (u32 a, u32 b) | ||
| 136 | { | ||
| 137 | return arith_cmp (a, b) >= 0 ? a : b; | ||
| 138 | } | ||
| 139 | |||
| 140 | s32 | ||
| 141 | arith2bin (u32 a) | ||
| 142 | { | ||
| 143 | s32 bin = 0; | ||
| 144 | |||
| 145 | for (int i = 0; i < 8; ++i) { | ||
| 146 | bin >>= 3; | ||
| 147 | bin |= (UNPACK8u32 (order, a & 0b111)) << 3*7; | ||
| 148 | a >>= BPD; | ||
| 149 | } | ||
| 150 | |||
| 151 | return bin; | ||
| 152 | } | ||
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 @@ | |||
| 1 | #ifndef ARITH_H | ||
| 2 | #define ARITH_H | ||
| 3 | |||
| 4 | #include "utils.h" | ||
| 5 | |||
| 6 | #define BPD 4 | ||
| 7 | // #define DIGIT_MASK ((1 << BPD) - 1) | ||
| 8 | #define DIGIT_MASK 0b111 | ||
| 9 | |||
| 10 | #define I2C(I) ((I) + 'a') | ||
| 11 | #define C2I(C) ((C) - 'a') | ||
| 12 | |||
| 13 | #define PACK8u32(A, B, C, D, E, F, G, H) \ | ||
| 14 | ((A) << BPD*0 | \ | ||
| 15 | (B) << BPD*1 | \ | ||
| 16 | (C) << BPD*2 | \ | ||
| 17 | (D) << BPD*3 | \ | ||
| 18 | (E) << BPD*4 | \ | ||
| 19 | (F) << BPD*5 | \ | ||
| 20 | (G) << BPD*6 | \ | ||
| 21 | (H) << BPD*7) | ||
| 22 | #define UNPACK8u32(V, I) ((V) >> BPD*(I) & DIGIT_MASK) | ||
| 23 | |||
| 24 | #define TBL_LOOKUP(TBL, ROW, COL) ((TBL)[(ROW)] >> (COL)*2*BPD) | ||
| 25 | #define TBL_VALUE(RET) ((RET) & DIGIT_MASK) | ||
| 26 | #define TBL_CARRY(RET) ((RET) >> BPD & DIGIT_MASK) | ||
| 27 | |||
| 28 | #define MASK_NEG 0x80000000 | ||
| 29 | #define MASK_OVF 0x08000000 | ||
| 30 | #define MASK_NAN 0x00800000 | ||
| 31 | #define MASK_INF 0x00080000 | ||
| 32 | #define MASK_UNI 0x00008000 | ||
| 33 | #define MASK_EMP 0x00000800 | ||
| 34 | #define MASK_FLAGS (MASK_NEG | MASK_OVF | MASK_NAN | MASK_INF | MASK_UNI | MASK_EMP) | ||
| 35 | |||
| 36 | enum digit { A, B, C, D, E, F, G, H, DIGIT_COUNT }; | ||
| 37 | |||
| 38 | extern u32 order; | ||
| 39 | extern u64 addition_table[DIGIT_COUNT]; | ||
| 40 | extern u64 multiplication_table[DIGIT_COUNT]; | ||
| 41 | |||
| 42 | u32 str2num (const char *str); | ||
| 43 | u32 arith_min (u32 a, u32 b); | ||
| 44 | u32 arith_max (u32 a, u32 b); | ||
| 45 | int arith_cmp (u32 a, u32 b); | ||
| 46 | s32 arith2bin (u32 a); | ||
| 47 | |||
| 48 | #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 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include <stdio.h> | ||
| 3 | #include <string.h> | ||
| 4 | #include <regex.h> | ||
| 5 | #include "utils.h" | ||
| 6 | #include "arith.h" | ||
| 7 | #include "stack.h" | ||
| 8 | #include "ops.h" | ||
| 9 | |||
| 10 | void | ||
| 11 | print_number (u32 num) | ||
| 12 | { | ||
| 13 | char sign = num & MASK_NEG && num & MASK_FLAGS ? '-' : ' '; | ||
| 14 | num &= ~MASK_FLAGS; | ||
| 15 | u8 offset; | ||
| 16 | printf (" "); | ||
| 17 | for (offset = 7*BPD; offset; offset -= BPD) { | ||
| 18 | if (num >> offset & DIGIT_MASK) | ||
| 19 | break; | ||
| 20 | putchar (' '); | ||
| 21 | } | ||
| 22 | printf ("%c", sign); | ||
| 23 | for (; offset; offset -= BPD) | ||
| 24 | printf ("%c", I2C (num >> offset & DIGIT_MASK)); | ||
| 25 | printf ("%c\n", I2C (num & DIGIT_MASK)); | ||
| 26 | } | ||
| 27 | |||
| 28 | static void | ||
| 29 | print_stack (void) | ||
| 30 | { | ||
| 31 | const u32 *ptr = (u32 *) stack_base (); | ||
| 32 | const u32 *end = (u32 *) stack_end (); | ||
| 33 | |||
| 34 | while (ptr != end) | ||
| 35 | print_number (*ptr++); | ||
| 36 | } | ||
| 37 | |||
| 38 | int | ||
| 39 | main (int argc, char *argv[]) | ||
| 40 | { | ||
| 41 | stack_init (sizeof (u32)); | ||
| 42 | |||
| 43 | 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*$"; | ||
| 44 | regex_t compiled_regexp; | ||
| 45 | int errcode = regcomp (&compiled_regexp, regexp, REG_EXTENDED | REG_NOSUB); | ||
| 46 | if (errcode != 0) { | ||
| 47 | size_t length = regerror (errcode, &compiled_regexp, NULL, 0); | ||
| 48 | char *buffer = malloc (length); | ||
| 49 | (void) regerror (errcode, &compiled_regexp, buffer, length); | ||
| 50 | DIE (EXIT_FAILURE, "regcomp: %s", buffer); | ||
| 51 | } | ||
| 52 | |||
| 53 | char *line = NULL; | ||
| 54 | size_t line_size = 0; | ||
| 55 | ssize_t line_len; | ||
| 56 | enum action { | ||
| 57 | ACTION_NONE, | ||
| 58 | ACTION_QUOTE, | ||
| 59 | ACTION_OP, | ||
| 60 | }; | ||
| 61 | enum action last_action = ACTION_NONE; | ||
| 62 | u32 last_num; | ||
| 63 | op_fn last_op = op_fns[OP_NOP]; | ||
| 64 | for (;;) { | ||
| 65 | printf ("> "); | ||
| 66 | line_len = getline (&line, &line_size, stdin); | ||
| 67 | line[--line_len] = '\0'; | ||
| 68 | |||
| 69 | if (line_len == 0) { | ||
| 70 | switch (last_action) { | ||
| 71 | case ACTION_NONE: | ||
| 72 | break; | ||
| 73 | case ACTION_QUOTE: | ||
| 74 | stack_push_u32 (last_num); | ||
| 75 | break; | ||
| 76 | case ACTION_OP: | ||
| 77 | last_op (); | ||
| 78 | break; | ||
| 79 | } | ||
| 80 | } else { | ||
| 81 | if (regexec (&compiled_regexp, line, 0, NULL, 0) != 0) { | ||
| 82 | ERROR ("input doesn't match regexp `%s'", regexp); | ||
| 83 | continue; | ||
| 84 | } | ||
| 85 | |||
| 86 | if (line[0] == '+' || | ||
| 87 | line[0] == '-') { | ||
| 88 | last_action = ACTION_QUOTE; | ||
| 89 | last_num = str2num (line); | ||
| 90 | stack_push_u32 (last_num); | ||
| 91 | } else { | ||
| 92 | last_action = ACTION_OP; | ||
| 93 | last_op = op_fns[str2op (line)]; | ||
| 94 | last_op (); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | print_stack (); | ||
| 99 | NEWLINE (); | ||
| 100 | } free (line); | ||
| 101 | |||
| 102 | regfree (&compiled_regexp); | ||
| 103 | stack_free (); | ||
| 104 | return EXIT_SUCCESS; | ||
| 105 | } | ||
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 @@ | |||
| 1 | #include "ops.h" | ||
| 2 | #include "stack.h" | ||
| 3 | #include "arith.h" | ||
| 4 | #include <stdbool.h> | ||
| 5 | |||
| 6 | void print_number (u32 num); | ||
| 7 | |||
| 8 | static bool | ||
| 9 | binary_op_p (const char *name) | ||
| 10 | { | ||
| 11 | if (stack_count_u32 () < 2) { | ||
| 12 | ERROR ("%s operation requires at least 2 arguments on the stack", name); | ||
| 13 | return false; | ||
| 14 | } return true; | ||
| 15 | } | ||
| 16 | |||
| 17 | static bool | ||
| 18 | unary_op_p (const char *name) | ||
| 19 | { | ||
| 20 | if (stack_count_u32 () < 1) { | ||
| 21 | ERROR ("%s operation requires at least 1 argument on the stack", name); | ||
| 22 | return false; | ||
| 23 | } return true; | ||
| 24 | } | ||
| 25 | |||
| 26 | static bool | ||
| 27 | op_ovf_p (u32 a) | ||
| 28 | { | ||
| 29 | if (a & MASK_OVF) { | ||
| 30 | ERROR ("operation has overflown"); | ||
| 31 | return true; | ||
| 32 | } return false; | ||
| 33 | } | ||
| 34 | |||
| 35 | typedef u32 (*binary_op_fn) (u32, u32); | ||
| 36 | static void | ||
| 37 | binary_op (const char *name, binary_op_fn op) | ||
| 38 | { | ||
| 39 | if (!binary_op_p (name)) return; | ||
| 40 | |||
| 41 | u32 rhs = stack_pop_u32 (); | ||
| 42 | u32 lhs = stack_pop_u32 (); | ||
| 43 | u32 res = op (lhs, rhs); | ||
| 44 | |||
| 45 | if (res & MASK_NAN) { | ||
| 46 | INFO ("operation result is not a number"); | ||
| 47 | return; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (res & MASK_EMP) { | ||
| 51 | INFO ("operation resulted in empty set"); | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | |||
| 55 | if (res & MASK_UNI) { | ||
| 56 | INFO ("operation result is a range: [-ffffffff; ffffffff]"); | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | |||
| 60 | if (res & MASK_INF) { | ||
| 61 | INFO ("operation resulted in infinity"); | ||
| 62 | res = 0x77777777; | ||
| 63 | } | ||
| 64 | |||
| 65 | if (!op_ovf_p (res)) | ||
| 66 | stack_push_u32 (res); | ||
| 67 | } | ||
| 68 | |||
| 69 | static void | ||
| 70 | op_nop (void) {} | ||
| 71 | |||
| 72 | static void | ||
| 73 | op_dup (void) | ||
| 74 | { | ||
| 75 | if (!unary_op_p ("duplication")) return; | ||
| 76 | stack_push_u32 (stack_top_u32 ()); | ||
| 77 | } | ||
| 78 | |||
| 79 | static bool | ||
| 80 | stack_already_empty () | ||
| 81 | { | ||
| 82 | if (stack_count_u32 () == 0) { | ||
| 83 | INFO ("stack is already empty"); | ||
| 84 | return true; | ||
| 85 | } return false; | ||
| 86 | } | ||
| 87 | |||
| 88 | static void | ||
| 89 | op_del (void) | ||
| 90 | { | ||
| 91 | if (stack_already_empty ()) return; | ||
| 92 | stack_discard_u32 (); | ||
| 93 | } | ||
| 94 | |||
| 95 | static void | ||
| 96 | op_clr (void) | ||
| 97 | { | ||
| 98 | if (stack_already_empty ()) return; | ||
| 99 | stack_clear (); | ||
| 100 | } | ||
| 101 | |||
| 102 | static void | ||
| 103 | op_swp (void) | ||
| 104 | { | ||
| 105 | if (!binary_op_p ("swap")) return; | ||
| 106 | |||
| 107 | u32 n1 = stack_pop_u32 (); | ||
| 108 | u32 n2 = stack_pop_u32 (); | ||
| 109 | |||
| 110 | stack_push_u32 (n1); | ||
| 111 | stack_push_u32 (n2); | ||
| 112 | } | ||
| 113 | |||
| 114 | static void | ||
| 115 | op_neg (void) | ||
| 116 | { | ||
| 117 | if (!unary_op_p ("negation")) return; | ||
| 118 | u32 num = stack_pop_u32 (); | ||
| 119 | stack_push_u32 (num ^ MASK_NEG); | ||
| 120 | } | ||
| 121 | |||
| 122 | static u32 sub (u32 lhs, u32 rhs); | ||
| 123 | |||
| 124 | static u32 | ||
| 125 | add (u32 lhs, u32 rhs) | ||
| 126 | { | ||
| 127 | if (lhs & MASK_NEG && rhs & MASK_NEG) | ||
| 128 | return add (lhs & ~MASK_NEG, rhs & ~MASK_NEG) | MASK_NEG; | ||
| 129 | if (lhs & MASK_NEG) return sub (rhs, lhs ^ MASK_NEG); | ||
| 130 | if (rhs & MASK_NEG) return sub (lhs, rhs ^ MASK_NEG); | ||
| 131 | |||
| 132 | u32 res = 0; | ||
| 133 | u8 carry = 0; | ||
| 134 | for (u8 offset = 0; lhs | rhs | carry && offset < BPD*8; offset += BPD) { | ||
| 135 | u8 lhsd = lhs & DIGIT_MASK; | ||
| 136 | u8 rhsd = rhs & DIGIT_MASK; | ||
| 137 | |||
| 138 | u8 step1 = TBL_LOOKUP (addition_table, TBL_VALUE (rhsd ), TBL_VALUE (carry)); | ||
| 139 | u8 step2 = TBL_LOOKUP (addition_table, TBL_VALUE (step1), TBL_VALUE (lhsd )); | ||
| 140 | u8 step3 = TBL_LOOKUP (addition_table, TBL_CARRY (step1), TBL_CARRY (step2)); | ||
| 141 | res |= TBL_VALUE (step2) << offset; | ||
| 142 | carry = TBL_VALUE (step3); | ||
| 143 | |||
| 144 | lhs >>= BPD; | ||
| 145 | rhs >>= BPD; | ||
| 146 | } | ||
| 147 | |||
| 148 | if (carry) | ||
| 149 | res |= MASK_OVF; | ||
| 150 | |||
| 151 | return res; | ||
| 152 | } | ||
| 153 | |||
| 154 | static u32 | ||
| 155 | sub (u32 lhs, u32 rhs) | ||
| 156 | { | ||
| 157 | if (lhs & MASK_NEG && rhs & MASK_NEG) { | ||
| 158 | lhs ^= MASK_NEG; | ||
| 159 | rhs ^= MASK_NEG; | ||
| 160 | SWAP (lhs, rhs); | ||
| 161 | } else { | ||
| 162 | if (lhs & MASK_NEG) return add (rhs, lhs ^ MASK_NEG) | MASK_NEG; | ||
| 163 | if (rhs & MASK_NEG) return add (lhs, rhs ^ MASK_NEG); | ||
| 164 | } | ||
| 165 | if (arith_cmp (lhs, rhs) < 0) | ||
| 166 | return sub (rhs, lhs) | MASK_NEG; | ||
| 167 | |||
| 168 | u32 res = 0; | ||
| 169 | u8 carry = 0; | ||
| 170 | for (u8 offset = 0; lhs | rhs | carry && offset < BPD*8; offset += BPD) { | ||
| 171 | u8 lhsd = lhs & DIGIT_MASK; | ||
| 172 | u8 rhsd = rhs & DIGIT_MASK; | ||
| 173 | u8 buf; | ||
| 174 | |||
| 175 | rhsd = TBL_LOOKUP (addition_table, | ||
| 176 | TBL_VALUE (rhsd), | ||
| 177 | TBL_VALUE (carry)); | ||
| 178 | u64 row = addition_table[TBL_VALUE (rhsd)]; | ||
| 179 | for (u8 i = 0; i < DIGIT_COUNT; ++i) { | ||
| 180 | if ((row >> 2*BPD*i & DIGIT_MASK) == lhsd) { | ||
| 181 | carry = TBL_VALUE (TBL_LOOKUP (addition_table, | ||
| 182 | TBL_CARRY (rhsd), | ||
| 183 | TBL_CARRY (row >> 2*BPD*i))); | ||
| 184 | res |= i << offset; | ||
| 185 | break; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | lhs >>= BPD; | ||
| 190 | rhs >>= BPD; | ||
| 191 | } | ||
| 192 | |||
| 193 | if (carry) | ||
| 194 | res |= MASK_OVF; | ||
| 195 | |||
| 196 | return res; | ||
| 197 | } | ||
| 198 | |||
| 199 | static void | ||
| 200 | op_add (void) { binary_op ("addition", add); } | ||
| 201 | |||
| 202 | static u32 | ||
| 203 | mul (u32 lhs, u32 rhs) | ||
| 204 | { | ||
| 205 | if (lhs & MASK_NEG && rhs & MASK_NEG) | ||
| 206 | return mul (lhs ^ MASK_NEG, rhs ^ MASK_NEG); | ||
| 207 | if (lhs & MASK_NEG) return mul (rhs, lhs ^ MASK_NEG) | MASK_NEG; | ||
| 208 | if (rhs & MASK_NEG) return mul (lhs, rhs ^ MASK_NEG) | MASK_NEG; | ||
| 209 | |||
| 210 | bool flag_ovf = false; | ||
| 211 | u32 res = A; | ||
| 212 | for (u8 i = 0; rhs; rhs >>= BPD, ++i) { | ||
| 213 | u8 rhsd = rhs & DIGIT_MASK; | ||
| 214 | u32 lhsc = lhs; | ||
| 215 | u32 inter = A; | ||
| 216 | u8 carry = A; | ||
| 217 | for (u8 offset = 0; lhsc || carry && offset < BPD*8; lhsc >>= BPD, offset += BPD) { | ||
| 218 | u8 lhsd = lhsc & DIGIT_MASK; | ||
| 219 | u8 step1 = TBL_LOOKUP (multiplication_table, TBL_VALUE (rhsd ), TBL_VALUE (lhsd )); | ||
| 220 | u8 step2 = TBL_LOOKUP ( addition_table, TBL_VALUE (step1), TBL_VALUE (carry)); | ||
| 221 | u8 step3 = TBL_LOOKUP ( addition_table, TBL_CARRY (step1), TBL_CARRY (step2)); | ||
| 222 | inter |= TBL_VALUE (step2) << offset; | ||
| 223 | carry = TBL_VALUE (step3); | ||
| 224 | } | ||
| 225 | res = add (res, inter << BPD*i); | ||
| 226 | if (carry || CLZ (inter) < BPD*i || res & MASK_OVF) | ||
| 227 | flag_ovf = true; | ||
| 228 | } | ||
| 229 | |||
| 230 | res |= flag_ovf * MASK_OVF; | ||
| 231 | |||
| 232 | return res; | ||
| 233 | } | ||
| 234 | |||
| 235 | static void | ||
| 236 | op_mul (void) { binary_op ("multiplication", mul); } | ||
| 237 | |||
| 238 | static void | ||
| 239 | op_sub (void) { binary_op ("substraction", sub); } | ||
| 240 | |||
| 241 | static u32 | ||
| 242 | div (u32 lhs, u32 rhs) | ||
| 243 | { | ||
| 244 | if (lhs == A && rhs == A) { | ||
| 245 | INFO ("dividing zero by zero"); | ||
| 246 | return MASK_UNI; | ||
| 247 | } | ||
| 248 | if (rhs == A) { | ||
| 249 | INFO ("division by zero"); | ||
| 250 | return MASK_EMP; | ||
| 251 | } | ||
| 252 | |||
| 253 | if (lhs & MASK_NEG && | ||
| 254 | rhs & MASK_NEG) { | ||
| 255 | return add (B, | ||
| 256 | div (sub (lhs ^ MASK_NEG, | ||
| 257 | B), | ||
| 258 | rhs ^ MASK_NEG)); | ||
| 259 | } | ||
| 260 | if (lhs & MASK_NEG) { | ||
| 261 | return add (B, | ||
| 262 | div (sub (lhs ^ MASK_NEG, | ||
| 263 | B), | ||
| 264 | rhs)) | MASK_NEG; | ||
| 265 | } | ||
| 266 | if (rhs & MASK_NEG) { | ||
| 267 | return div (lhs, | ||
| 268 | rhs ^ MASK_NEG) | MASK_NEG; | ||
| 269 | } | ||
| 270 | |||
| 271 | |||
| 272 | u32 res = A; | ||
| 273 | u32 buf = A; | ||
| 274 | for (u32 inc = B << 7*BPD; inc; inc >>= BPD) { | ||
| 275 | for (;;) { | ||
| 276 | u32 mul_res = mul (rhs, inc); | ||
| 277 | if (mul_res & MASK_OVF) | ||
| 278 | break; | ||
| 279 | |||
| 280 | u32 old = buf; | ||
| 281 | buf = add (buf, mul_res); | ||
| 282 | if (buf & MASK_OVF || arith_cmp (buf, lhs) > 0) { | ||
| 283 | buf = old; | ||
| 284 | break; | ||
| 285 | } | ||
| 286 | res = add (res, inc); | ||
| 287 | } | ||
| 288 | } | ||
| 289 | |||
| 290 | return res; | ||
| 291 | } | ||
| 292 | |||
| 293 | static void | ||
| 294 | op_div (void) { binary_op ("division", div); } | ||
| 295 | |||
| 296 | static u32 | ||
| 297 | mod (u32 lhs, u32 rhs) | ||
| 298 | { | ||
| 299 | if (rhs == A) { | ||
| 300 | ERROR ("division by zero"); | ||
| 301 | return MASK_NAN; | ||
| 302 | } | ||
| 303 | |||
| 304 | return sub (lhs, | ||
| 305 | mul (div (lhs, rhs), | ||
| 306 | rhs)); | ||
| 307 | } | ||
| 308 | |||
| 309 | static void | ||
| 310 | op_mod (void) { binary_op ("modulo", mod); } | ||
| 311 | |||
| 312 | static void | ||
| 313 | op_inc (void) | ||
| 314 | { | ||
| 315 | if (!unary_op_p ("incrementation")) return; | ||
| 316 | |||
| 317 | u32 lhs = stack_pop_u32 (); | ||
| 318 | u32 rhs = B; | ||
| 319 | u32 res = add (lhs, rhs); | ||
| 320 | |||
| 321 | if (!op_ovf_p (res)) | ||
| 322 | stack_push_u32 (res); | ||
| 323 | } | ||
| 324 | |||
| 325 | static void | ||
| 326 | op_dec (void) | ||
| 327 | { | ||
| 328 | if (!unary_op_p ("decrementation")) return; | ||
| 329 | |||
| 330 | u32 lhs = stack_pop_u32 (); | ||
| 331 | u32 rhs = B; | ||
| 332 | u32 res = sub (lhs, rhs); | ||
| 333 | |||
| 334 | if (!op_ovf_p (res)) | ||
| 335 | stack_push_u32 (res); | ||
| 336 | } | ||
| 337 | |||
| 338 | static u32 | ||
| 339 | pow (u32 lhs, u32 rhs) | ||
| 340 | { | ||
| 341 | u32 res = B; | ||
| 342 | u32 (*op) (u32, u32) = rhs & MASK_NEG ? div : mul; | ||
| 343 | rhs &= ~MASK_NEG; | ||
| 344 | |||
| 345 | while (rhs) { | ||
| 346 | rhs = sub (rhs, B); | ||
| 347 | res = op (res, lhs); | ||
| 348 | } | ||
| 349 | |||
| 350 | return res; | ||
| 351 | } | ||
| 352 | |||
| 353 | static void | ||
| 354 | op_pow (void) { binary_op ("power", pow); } | ||
| 355 | |||
| 356 | static u32 | ||
| 357 | gcd (u32 a, u32 b) | ||
| 358 | { | ||
| 359 | if (a == A || b == A) { | ||
| 360 | ERROR ("input out of range"); | ||
| 361 | return MASK_NAN; | ||
| 362 | } | ||
| 363 | |||
| 364 | while (b) { | ||
| 365 | u32 tmp = b; | ||
| 366 | b = mod (a, b); | ||
| 367 | a = tmp; | ||
| 368 | } | ||
| 369 | |||
| 370 | return a; | ||
| 371 | } | ||
| 372 | |||
| 373 | static void | ||
| 374 | op_gcd (void) { binary_op ("GCD", gcd); } | ||
| 375 | |||
| 376 | static u32 | ||
| 377 | lcm (u32 a, u32 b) | ||
| 378 | { | ||
| 379 | return div (mul (a, b), gcd (a, b)); | ||
| 380 | } | ||
| 381 | |||
| 382 | static void | ||
| 383 | op_lcm (void) { binary_op ("LCM", lcm); } | ||
| 384 | |||
| 385 | op_fn op_fns[] = { | ||
| 386 | [OP_NOP] = op_nop, | ||
| 387 | [OP_DUP] = op_dup, | ||
| 388 | [OP_DEL] = op_del, | ||
| 389 | [OP_CLR] = op_clr, | ||
| 390 | [OP_SWP] = op_swp, | ||
| 391 | [OP_NEG] = op_neg, | ||
| 392 | [OP_ADD] = op_add, | ||
| 393 | [OP_MUL] = op_mul, | ||
| 394 | [OP_SUB] = op_sub, | ||
| 395 | [OP_DIV] = op_div, | ||
| 396 | [OP_MOD] = op_mod, | ||
| 397 | [OP_INC] = op_inc, | ||
| 398 | [OP_DEC] = op_dec, | ||
| 399 | [OP_POW] = op_pow, | ||
| 400 | [OP_GCD] = op_gcd, | ||
| 401 | [OP_LCM] = op_lcm, | ||
| 402 | }; | ||
| 403 | |||
| 404 | const char *op_names[] = { | ||
| 405 | [OP_NOP] = "nop", | ||
| 406 | [OP_DUP] = "dup", | ||
| 407 | [OP_DEL] = "del", | ||
| 408 | [OP_CLR] = "clr", | ||
| 409 | [OP_SWP] = "swp", | ||
| 410 | [OP_NEG] = "neg", | ||
| 411 | [OP_ADD] = "add", | ||
| 412 | [OP_MUL] = "mul", | ||
| 413 | [OP_SUB] = "sub", | ||
| 414 | [OP_DIV] = "div", | ||
| 415 | [OP_MOD] = "mod", | ||
| 416 | [OP_INC] = "inc", | ||
| 417 | [OP_DEC] = "dec", | ||
| 418 | [OP_POW] = "pow", | ||
| 419 | [OP_GCD] = "gcd", | ||
| 420 | [OP_LCM] = "lcm", | ||
| 421 | }; | ||
| 422 | |||
| 423 | enum op | ||
| 424 | str2op (const char *str) | ||
| 425 | { | ||
| 426 | for (int i = 0; i < OP_COUNT; ++i) { | ||
| 427 | if (strcmp (str, op_names[i]) == 0) | ||
| 428 | return i; | ||
| 429 | } return OP_COUNT; | ||
| 430 | } | ||
| 431 | |||
| 432 | void | ||
| 433 | ops_test (void) | ||
| 434 | { | ||
| 435 | for (u32 lhs = PACK8u32 (F, F, F, F, F, F, F, F)|MASK_NEG; | ||
| 436 | arith_cmp (lhs, PACK8u32 (F, F, F, F, F, F, F, F)) < 0; | ||
| 437 | lhs = add (lhs, F)) { | ||
| 438 | for (u32 rhs = PACK8u32 (F, F, F, F, F, F, F, F)|MASK_NEG; | ||
| 439 | arith_cmp (rhs, PACK8u32 (F, F, F, F, F, F, F, F)) < 0; | ||
| 440 | rhs = add (rhs, F)) { | ||
| 441 | u32 a = mul (lhs, rhs); | ||
| 442 | if (a & MASK_OVF) continue; | ||
| 443 | u32 b = div (a, rhs); | ||
| 444 | if (b & MASK_OVF|MASK_EMP) continue; | ||
| 445 | u32 c = div (a, lhs); | ||
| 446 | if (c & MASK_OVF|MASK_EMP) continue; | ||
| 447 | if (b != lhs || | ||
| 448 | c != rhs) { | ||
| 449 | puts ("Test failed for:"); | ||
| 450 | printf ("lhs: "); print_number (lhs); | ||
| 451 | printf ("rhs: "); print_number (rhs); | ||
| 452 | printf ("a: "); print_number (a ); | ||
| 453 | printf ("b: "); print_number (b ); | ||
| 454 | printf ("c: "); print_number (c ); | ||
| 455 | putchar ('\n'); | ||
| 456 | } else puts ("OK"); | ||
| 457 | } | ||
| 458 | } | ||
| 459 | } | ||
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 @@ | |||
| 1 | #ifndef OPS_H | ||
| 2 | #define OPS_H | ||
| 3 | |||
| 4 | #include <obstack.h> | ||
| 5 | |||
| 6 | enum op { | ||
| 7 | OP_NOP, | ||
| 8 | OP_DUP, | ||
| 9 | OP_DEL, | ||
| 10 | OP_CLR, | ||
| 11 | OP_SWP, | ||
| 12 | OP_NEG, | ||
| 13 | OP_ADD, | ||
| 14 | OP_MUL, | ||
| 15 | OP_SUB, | ||
| 16 | OP_DIV, | ||
| 17 | OP_MOD, | ||
| 18 | OP_INC, | ||
| 19 | OP_DEC, | ||
| 20 | OP_POW, | ||
| 21 | OP_GCD, | ||
| 22 | OP_LCM, | ||
| 23 | OP_COUNT, | ||
| 24 | }; | ||
| 25 | |||
| 26 | typedef void (*op_fn) (void); | ||
| 27 | |||
| 28 | extern op_fn op_fns[OP_COUNT]; | ||
| 29 | extern const char *op_names[OP_COUNT]; | ||
| 30 | |||
| 31 | enum op str2op (const char *str); | ||
| 32 | |||
| 33 | #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 @@ | |||
| 1 | #include "stack.h" | ||
| 2 | #include <obstack.h> | ||
| 3 | #include <stdlib.h> | ||
| 4 | |||
| 5 | #define obstack_chunk_alloc malloc | ||
| 6 | #define obstack_chunk_free free | ||
| 7 | |||
| 8 | static struct obstack obstack; | ||
| 9 | |||
| 10 | void | ||
| 11 | stack_init (int alignment_mask) | ||
| 12 | { | ||
| 13 | obstack_init (&obstack); | ||
| 14 | obstack_alignment_mask (&obstack) = alignment_mask; | ||
| 15 | } | ||
| 16 | |||
| 17 | void | ||
| 18 | stack_free (void) | ||
| 19 | { | ||
| 20 | obstack_free (&obstack, NULL); | ||
| 21 | } | ||
| 22 | |||
| 23 | int | ||
| 24 | stack_count_u32 (void) | ||
| 25 | { | ||
| 26 | return obstack_object_size (&obstack)/sizeof (u32); | ||
| 27 | } | ||
| 28 | |||
| 29 | void | ||
| 30 | stack_push_u32 (u32 num) | ||
| 31 | { | ||
| 32 | obstack_grow (&obstack, &num, sizeof (num)); | ||
| 33 | } | ||
| 34 | |||
| 35 | u32 | ||
| 36 | stack_pop_u32 (void) | ||
| 37 | { | ||
| 38 | u32 val = *(((u32*) obstack_next_free (&obstack)) - 1); | ||
| 39 | obstack_blank (&obstack, -((int) sizeof (u32))); | ||
| 40 | return val; | ||
| 41 | } | ||
| 42 | |||
| 43 | void | ||
| 44 | stack_discard_u32 (void) | ||
| 45 | { | ||
| 46 | if (obstack_object_size (&obstack) == 0) | ||
| 47 | return; | ||
| 48 | |||
| 49 | obstack_blank (&obstack, -((int) sizeof (u32))); | ||
| 50 | } | ||
| 51 | |||
| 52 | void | ||
| 53 | stack_clear (void) | ||
| 54 | { | ||
| 55 | obstack_blank (&obstack, -obstack_object_size (&obstack)); | ||
| 56 | } | ||
| 57 | |||
| 58 | u32 | ||
| 59 | stack_top_u32 (void) | ||
| 60 | { | ||
| 61 | return *(((u32*) obstack_next_free (&obstack)) - 1); | ||
| 62 | } | ||
| 63 | |||
| 64 | void * | ||
| 65 | stack_base (void) | ||
| 66 | { | ||
| 67 | return obstack_base (&obstack); | ||
| 68 | } | ||
| 69 | |||
| 70 | void * | ||
| 71 | stack_end (void) | ||
| 72 | { | ||
| 73 | return obstack_next_free (&obstack); | ||
| 74 | } | ||
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 @@ | |||
| 1 | #ifndef STACK_H | ||
| 2 | #define STACK_H | ||
| 3 | |||
| 4 | #include "utils.h" | ||
| 5 | |||
| 6 | void stack_init (int alignment_mask); | ||
| 7 | void stack_free (void); | ||
| 8 | int stack_count_u32 (void); | ||
| 9 | void stack_push_u32 (u32 num); | ||
| 10 | u32 stack_pop_u32 (void); | ||
| 11 | void stack_discard_u32 (void); | ||
| 12 | void stack_clear (void); | ||
| 13 | u32 stack_top_u32 (void); | ||
| 14 | void *stack_base (void); | ||
| 15 | void *stack_end (void); | ||
| 16 | |||
| 17 | #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 @@ | |||
| 1 | #include <stdlib.h> | ||
| 2 | #include "utils.h" | ||
| 3 | #include "arith.h" | ||
| 4 | |||
| 5 | void ops_test (void); | ||
| 6 | |||
| 7 | void | ||
| 8 | print_number (u32 num) | ||
| 9 | { | ||
| 10 | char sign = num & MASK_NEG ? '-' : ' '; | ||
| 11 | num &= ~MASK_FLAGS; | ||
| 12 | u8 offset; | ||
| 13 | for (offset = 7*BPD; offset; offset -= BPD) { | ||
| 14 | if (num >> offset & DIGIT_MASK) | ||
| 15 | break; | ||
| 16 | putchar (' '); | ||
| 17 | } | ||
| 18 | printf ("%c", sign); | ||
| 19 | for (; offset; offset -= BPD) | ||
| 20 | printf ("%c", I2C (num >> offset & DIGIT_MASK)); | ||
| 21 | printf ("%c\n", I2C (num & DIGIT_MASK)); | ||
| 22 | } | ||
| 23 | |||
| 24 | int | ||
| 25 | main (void) | ||
| 26 | { | ||
| 27 | ops_test (); | ||
| 28 | |||
| 29 | return EXIT_SUCCESS; | ||
| 30 | } | ||
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 @@ | |||
| 1 | #include "utils.h" | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <stdio.h> | ||
| 4 | #include <stdarg.h> | ||
| 5 | #include <regex.h> | ||
| 6 | #include <string.h> | ||
| 7 | |||
| 8 | int | ||
| 9 | input (const char *message, const char *regex, const char *template, ...) | ||
| 10 | { | ||
| 11 | char *line = NULL; | ||
| 12 | size_t line_size = 0; | ||
| 13 | regex_t compiled; | ||
| 14 | int matches = 0; | ||
| 15 | int c; | ||
| 16 | va_list ap; | ||
| 17 | |||
| 18 | va_start (ap, template); | ||
| 19 | vprintf (message, ap); | ||
| 20 | printf (" (\"%s\"): ", regex); | ||
| 21 | getline (&line, &line_size, stdin); | ||
| 22 | line[strlen (line) - 1] = '\0'; | ||
| 23 | |||
| 24 | regcomp (&compiled, regex, REG_EXTENDED | REG_NOSUB); | ||
| 25 | if (regexec (&compiled, line, 0, NULL, 0)) | ||
| 26 | goto defer; | ||
| 27 | |||
| 28 | matches = vsscanf (line, template, ap); | ||
| 29 | va_end (ap); | ||
| 30 | |||
| 31 | defer: | ||
| 32 | free (line); | ||
| 33 | return matches; | ||
| 34 | } | ||
| 35 | |||
| 36 | uint | ||
| 37 | log10ui (uint ui) | ||
| 38 | { | ||
| 39 | return ((ui >= 1000000000u) ? 9 : | ||
| 40 | (ui >= 100000000u) ? 8 : | ||
| 41 | (ui >= 10000000u) ? 7 : | ||
| 42 | (ui >= 1000000u) ? 6 : | ||
| 43 | (ui >= 100000u) ? 5 : | ||
| 44 | (ui >= 10000u) ? 4 : | ||
| 45 | (ui >= 1000u) ? 3 : | ||
| 46 | (ui >= 100u) ? 2 : | ||
| 47 | (ui >= 10u) ? 1 : 0u); | ||
| 48 | } | ||
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 @@ | |||
| 1 | #ifndef UTILS_H | ||
| 2 | #define UTILS_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include <stdio.h> | ||
| 6 | |||
| 7 | #define MAX(a, b) ({ \ | ||
| 8 | typeof (a) _a = (a); \ | ||
| 9 | typeof (b) _b = (b); \ | ||
| 10 | _a > _b ? _a : _b; \ | ||
| 11 | }) | ||
| 12 | |||
| 13 | #define MIN(a, b) ({ \ | ||
| 14 | typeof (a) _a = (a); \ | ||
| 15 | typeof (b) _b = (b); \ | ||
| 16 | _a < _b ? _a : _b; \ | ||
| 17 | }) | ||
| 18 | |||
| 19 | #define SWAP(a, b) ({ \ | ||
| 20 | typeof (a) tmp = (a); \ | ||
| 21 | (a) = (b); \ | ||
| 22 | (b) = tmp; \ | ||
| 23 | }) | ||
| 24 | |||
| 25 | #define CLZ(X) __builtin_clz (X) | ||
| 26 | #define CTZ(X) __builtin_ctz (X) | ||
| 27 | #define POPCOUNT(X) __builtin_popcount (X) | ||
| 28 | |||
| 29 | #define NEWLINE() putchar ('\n') | ||
| 30 | #define PAGEBREAK() putchar ('\n'); | ||
| 31 | |||
| 32 | #define SUCCESS(MSG, ...) fprintf (stdout, "[+] SUCCESS: "MSG"\n", ##__VA_ARGS__) | ||
| 33 | #define INFO(MSG, ...) fprintf (stdout, "[*] INFO: " MSG"\n", ##__VA_ARGS__) | ||
| 34 | #define ERROR(MSG, ...) fprintf (stderr, "[-] ERROR: " MSG"\n", ##__VA_ARGS__) | ||
| 35 | |||
| 36 | #define DIE(EXIT_CODE, MSG, ...) ({ ERROR (MSG, ##__VA_ARGS__); exit (EXIT_CODE); }) | ||
| 37 | |||
| 38 | typedef unsigned int uint; | ||
| 39 | typedef uint8_t u8; | ||
| 40 | typedef int8_t s8; | ||
| 41 | typedef uint32_t u32; | ||
| 42 | typedef int32_t s32; | ||
| 43 | typedef uint64_t u64; | ||
| 44 | |||
| 45 | int input (const char *message, const char *regex, const char *template, ...); | ||
| 46 | uint log10ui (uint ui); | ||
| 47 | |||
| 48 | #endif | ||
