|
| 1 | + |
| 2 | +<<ls9 macros>>= |
| 3 | +#define mkifix(v) mkfix((full)(v)) |
| 4 | +@ |
| 5 | + |
| 6 | +<<ls9 init>>= |
| 7 | +P_plus = symref("+"); |
| 8 | +P_minus = symref("-"); |
| 9 | +P_times = symref("*"); |
| 10 | +P_div = symref("div"); |
| 11 | +P_rem = symref("rem"); |
| 12 | +P_grtr = symref(">"); |
| 13 | +P_gteq = symref(">="); |
| 14 | +P_less = symref("<"); |
| 15 | +P_lteq = symref("<="); |
| 16 | +P_equal = symref("="); |
| 17 | +P_bitop = symref("bitop"); |
| 18 | +@ |
| 19 | + |
| 20 | +<<ls9 impl>>= |
| 21 | +void fixover(char *who, full x, full y) { |
| 22 | + char b[100]; |
| 23 | + |
| 24 | + sprintf(b, "%s: fixnum overflow", who); |
| 25 | + error(b, cons(x, cons(y, NIL))); |
| 26 | +} |
| 27 | + |
| 28 | +full add(full x, full y) { |
| 29 | + if (!fixp(x)) expect("+", "fixnum", x); |
| 30 | + if (!fixp(y)) expect("+", "fixnum", y); |
| 31 | + if (add_ovfl(fixval(x), fixval(y))) fixover("+", x, y); |
| 32 | + return mkifix((ifull)fixval(x) + (ifull)fixval(y)); |
| 33 | +} |
| 34 | + |
| 35 | +full xsub(full x, full y) { |
| 36 | + if (!fixp(x)) expect("-", "fixnum", x); |
| 37 | + if (!fixp(y)) expect("-", "fixnum", y); |
| 38 | + if (sub_ovfl(fixval(y), fixval(x))) fixover("+", y, x); |
| 39 | + return mkifix((ifull)fixval(y) - (ifull)fixval(x)); |
| 40 | +} |
| 41 | + |
| 42 | +full mul(full x, full y) { |
| 43 | + ifull a, b; |
| 44 | + |
| 45 | + if (!fixp(x)) expect("*", "fixnum", x); |
| 46 | + if (!fixp(y)) expect("*", "fixnum", y); |
| 47 | + a = (ifull)fixval(x); |
| 48 | + b = (ifull)fixval(y); |
| 49 | + /* |
| 50 | + * Overflow of a*b is undefined, sooo |
| 51 | + */ |
| 52 | + /* Shortcuts, also protect later division */ |
| 53 | + if (0 == a || 0 == b) return Zero; |
| 54 | + if (1 == a) return y; |
| 55 | + if (1 == b) return x; |
| 56 | + /* abs(IFULL_MIN) is undefined using two's complement, so */ |
| 57 | + if (IFULL_MIN == a || IFULL_MIN == b) fixover("*", x, y); |
| 58 | + /* Catch the rest */ |
| 59 | + /* Bug: result may not be IFULL_MIN |
| 60 | + */ |
| 61 | + if (abs_wrap(a) > IFULL_MAX / abs_wrap(b)) fixover("*", x, y); |
| 62 | + return mkifix(a * b); |
| 63 | +} |
| 64 | + |
| 65 | +full intdiv(full x, full y) { |
| 66 | + if (!fixp(x)) expect("div", "fixnum", x); |
| 67 | + if (!fixp(y)) expect("div", "fixnum", y); |
| 68 | + if (0 == fixval(y)) error("div: divide by zero", UNDEF); |
| 69 | + return mkifix((ifull)fixval(x) / (ifull)fixval(y)); |
| 70 | +} |
| 71 | + |
| 72 | +full intrem(full x, full y) { |
| 73 | + if (!fixp(x)) expect("rem", "fixnum", x); |
| 74 | + if (!fixp(y)) expect("rem", "fixnum", y); |
| 75 | + if (0 == fixval(y)) error("rem: divide by zero", UNDEF); |
| 76 | + return mkifix((ifull)fixval(x) % (ifull)fixval(y)); |
| 77 | +} |
| 78 | + |
| 79 | +void grtr(full x, full y) { |
| 80 | + if (!fixp(x)) expect(">", "fixnum", x); |
| 81 | + if (!fixp(y)) expect(">", "fixnum", y); |
| 82 | + if ((ifull)fixval(y) <= (ifull)fixval(x)) stackset(Sp-1, NIL); |
| 83 | +} |
| 84 | + |
| 85 | +void gteq(full x, full y) { |
| 86 | + if (!fixp(x)) expect(">=", "fixnum", x); |
| 87 | + if (!fixp(y)) expect(">=", "fixnum", y); |
| 88 | + if ((ifull)fixval(y) < (ifull)fixval(x)) stackset(Sp-1, NIL); |
| 89 | +} |
| 90 | + |
| 91 | +void less(full x, full y) { |
| 92 | + if (!fixp(x)) expect("<", "fixnum", x); |
| 93 | + if (!fixp(y)) expect("<", "fixnum", y); |
| 94 | + if ((ifull)fixval(y) >= (ifull)fixval(x)) stackset(Sp-1, NIL); |
| 95 | +} |
| 96 | + |
| 97 | +void lteq(full x, full y) { |
| 98 | + if (!fixp(x)) expect("<=", "fixnum", x); |
| 99 | + if (!fixp(y)) expect("<=", "fixnum", y); |
| 100 | + if ((ifull)fixval(y) > (ifull)fixval(x)) stackset(Sp-1, NIL); |
| 101 | +} |
| 102 | + |
| 103 | +void equal(full x, full y) { |
| 104 | + /* fprintf(stderr, "**************** equal: %"PRIxFULL" %"PRIxFULL"\n", x, y); */ |
| 105 | + if (!fixp(x)) expect("=", "fixnum", x); |
| 106 | + if (!fixp(y)) expect("=", "fixnum", y); |
| 107 | + if (fixval(y) != fixval(x)) stackset(Sp-1, NIL); |
| 108 | +} |
| 109 | + |
| 110 | +full bitop(full x, full y, full o) { |
| 111 | + full i, op, a, b; |
| 112 | + |
| 113 | + if (!fixp(o)) expect("bitop", "fixnum", o); |
| 114 | + if (!fixp(x)) expect("bitop", "fixnum", x); |
| 115 | + if (!fixp(y)) expect("bitop", "fixnum", y); |
| 116 | + op = fixval(o); |
| 117 | + b = fixval(x); |
| 118 | + a = i = fixval(y); |
| 119 | + switch (op) { |
| 120 | + case 0: a = 0; break; |
| 121 | + case 1: a = a & b; break; |
| 122 | + case 2: a = a & ~b; break; |
| 123 | + case 3: /* a = a; */ break; |
| 124 | + case 4: a = ~a & b; break; |
| 125 | + case 5: a = b; break; |
| 126 | + case 6: a = a ^ b; break; |
| 127 | + case 7: a = a | b; break; |
| 128 | + case 8: a = ~(a | b); break; |
| 129 | + case 9: a = ~(a ^ b); break; |
| 130 | + case 10: a = ~b; break; |
| 131 | + case 11: a = a | ~b; break; |
| 132 | + case 12: a = ~a; break; |
| 133 | + case 13: a = ~a | b; break; |
| 134 | + case 14: a = ~(a & b); break; |
| 135 | + case 15: a = (full)~0; break; |
| 136 | + case 16: a = a @<< b; break; |
| 137 | + case 17: a = i @>> b; break; |
| 138 | + case 18: a = a @>> b; break; |
| 139 | + default: error("bitop: invalid opcode", o); |
| 140 | + break; |
| 141 | + } |
| 142 | + return mkfix(a); |
| 143 | +} |
| 144 | +@ |
0 commit comments