aboutsummaryrefslogtreecommitdiff
path: root/src/ops.c
diff options
context:
space:
mode:
authorLibravatar Comrade Martin Miroslavovich Michalec <martin@michalec.dev>2023-11-26 21:34:11 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-07 02:13:38 +0000
commit252df6c74c7be9b82aa89cdb4972f91c87b06184 (patch)
tree988497c67eb3d8d8789aa9bdea47bafd300e25b2 /src/ops.c
downloadcoursework-master.tar.gz
publishHEADmaster
Diffstat (limited to 'src/ops.c')
-rw-r--r--src/ops.c459
1 files changed, 459 insertions, 0 deletions
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
6void print_number (u32 num);
7
8static bool
9binary_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
17static bool
18unary_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
26static bool
27op_ovf_p (u32 a)
28{
29 if (a & MASK_OVF) {
30 ERROR ("operation has overflown");
31 return true;
32 } return false;
33}
34
35typedef u32 (*binary_op_fn) (u32, u32);
36static void
37binary_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
69static void
70op_nop (void) {}
71
72static void
73op_dup (void)
74{
75 if (!unary_op_p ("duplication")) return;
76 stack_push_u32 (stack_top_u32 ());
77}
78
79static bool
80stack_already_empty ()
81{
82 if (stack_count_u32 () == 0) {
83 INFO ("stack is already empty");
84 return true;
85 } return false;
86}
87
88static void
89op_del (void)
90{
91 if (stack_already_empty ()) return;
92 stack_discard_u32 ();
93}
94
95static void
96op_clr (void)
97{
98 if (stack_already_empty ()) return;
99 stack_clear ();
100}
101
102static void
103op_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
114static void
115op_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
122static u32 sub (u32 lhs, u32 rhs);
123
124static u32
125add (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
154static u32
155sub (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
199static void
200op_add (void) { binary_op ("addition", add); }
201
202static u32
203mul (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
235static void
236op_mul (void) { binary_op ("multiplication", mul); }
237
238static void
239op_sub (void) { binary_op ("substraction", sub); }
240
241static u32
242div (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
293static void
294op_div (void) { binary_op ("division", div); }
295
296static u32
297mod (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
309static void
310op_mod (void) { binary_op ("modulo", mod); }
311
312static void
313op_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
325static void
326op_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
338static u32
339pow (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
353static void
354op_pow (void) { binary_op ("power", pow); }
355
356static u32
357gcd (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
373static void
374op_gcd (void) { binary_op ("GCD", gcd); }
375
376static u32
377lcm (u32 a, u32 b)
378{
379 return div (mul (a, b), gcd (a, b));
380}
381
382static void
383op_lcm (void) { binary_op ("LCM", lcm); }
384
385op_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
404const 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
423enum op
424str2op (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
432void
433ops_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}