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