1 /*
2 * math.c
3 *
4 * Arithmetic, compare and logical instructions
5 *
6 */
7
8 #include "ztypes.h"
9
10 /*
11 * add
12 *
13 * Add two operands
14 *
15 */
16
17 #ifdef __STDC__
add(zword_t a,zword_t b)18 void add (zword_t a, zword_t b)
19 #else
20 void add (a, b)
21 zword_t a, b;
22 #endif
23 {
24
25 store_operand (a + b);
26
27 }/* add */
28
29 /*
30 * subtract
31 *
32 * Subtract two operands
33 *
34 */
35
36 #ifdef __STDC__
subtract(zword_t a,zword_t b)37 void subtract (zword_t a, zword_t b)
38 #else
39 void subtract (a, b)
40 zword_t a, b;
41 #endif
42 {
43
44 store_operand (a - b);
45
46 }/* subtract */
47
48 /*
49 * multiply
50 *
51 * Multiply two operands
52 *
53 */
54
55 #ifdef __STDC__
multiply(zword_t a,zword_t b)56 void multiply (zword_t a, zword_t b)
57 #else
58 void multiply (a, b)
59 zword_t a, b;
60 #endif
61 {
62
63 store_operand ((short)a * (short)b);
64
65 }/* multiply */
66
67 /*
68 * divide
69 *
70 * Divide two operands
71 *
72 */
73
74 #ifdef __STDC__
divide(zword_t a,zword_t b)75 void divide (zword_t a, zword_t b)
76 #else
77 void divide (a, b)
78 zword_t a, b;
79 #endif
80 {
81 /* The magic rule is: round towards zero. */
82 short sa = (short)a;
83 short sb = (short)b;
84 short res;
85 if (sb < 0) {
86 sa = -sa;
87 sb = -sb;
88 }
89 if (sa >= 0) {
90 res = sa / sb;
91 }
92 else {
93 res = -((-sa) / (sb));
94 }
95 store_operand (res);
96
97 }/* divide */
98
99 /*
100 * remainder
101 *
102 * Modulus divide two operands
103 *
104 */
105
106 #ifdef __STDC__
remainder(zword_t a,zword_t b)107 void remainder (zword_t a, zword_t b)
108 #else
109 void remainder (a, b)
110 zword_t a, b;
111 #endif
112 {
113 /* The magic rule is: be consistent with divide, because
114 (a/b)*a + (a%b) == a. So (a%b) has the same sign as a. */
115 short sa = (short)a;
116 short sb = (short)b;
117 short res;
118 if (sb < 0) {
119 sb = -sb;
120 }
121 if (sa >= 0) {
122 res = sa % sb;
123 }
124 else {
125 res = -((-sa) % (sb));
126 }
127 store_operand (res);
128
129 }/* remainder */
130
131 /*
132 * shift
133 *
134 * Shift +/- n bits
135 *
136 */
137
138 #ifdef __STDC__
shift(zword_t a,zword_t b)139 void shift (zword_t a, zword_t b)
140 #else
141 void shift (a, b)
142 zword_t a;
143 zword_t b;
144 #endif
145 {
146
147 if ((short) b > 0)
148 store_operand (a << (short) b);
149 else
150 store_operand (a >> abs ((short) b));
151
152 }/* shift */
153
154
155 /*
156 * arith_shift
157 *
158 * Aritmetic shift +/- n bits
159 *
160 */
161
162 #ifdef __STDC__
arith_shift(zword_t a,zword_t b)163 void arith_shift (zword_t a, zword_t b)
164 #else
165 void arith_shift (a, b)
166 zword_t a;
167 zword_t b;
168 #endif
169 {
170
171 if ((short) b >= 0)
172 store_operand (a << (short) b);
173 else
174 if ((short) a >= 0)
175 store_operand (a >> abs ((short) b));
176 else
177 store_operand (~(((~a) & 0xFFFF) >> abs ((short) b)));
178
179 }/* arith_shift */
180
181 /*
182 * or
183 *
184 * Logical OR
185 *
186 */
187
188 #ifdef __STDC__
or(zword_t a,zword_t b)189 void or (zword_t a, zword_t b)
190 #else
191 void or (a, b)
192 zword_t a, b;
193 #endif
194 {
195
196 store_operand (a | b);
197
198 }/* or */
199
200 /*
201 * not
202 *
203 * Logical NOT
204 *
205 */
206
207 #ifdef __STDC__
not(zword_t a)208 void not (zword_t a)
209 #else
210 void not (a)
211 zword_t a;
212 #endif
213 {
214
215 store_operand (~a);
216
217 }/* not */
218
219 /*
220 * and
221 *
222 * Logical AND
223 *
224 */
225
226 #ifdef __STDC__
and(zword_t a,zword_t b)227 void and (zword_t a, zword_t b)
228 #else
229 void and (a, b)
230 zword_t a, b;
231 #endif
232 {
233
234 store_operand (a & b);
235
236 }/* and */
237
238 /*
239 * ziprandom
240 *
241 * Return random number between 1 and operand
242 *
243 */
244
245 #ifdef __STDC__
ziprandom(zword_t a)246 void ziprandom (zword_t a)
247 #else
248 void ziprandom (a)
249 zword_t a;
250 #endif
251 {
252
253 if (a == 0)
254 store_operand (0);
255 else if (a & 0x8000) { /* (a < 0) - used to set seed with #RANDOM */
256 SRANDOM_FUNC ((unsigned int) abs (a));
257 store_operand (0);
258 } else /* (a > 0) */
259 store_operand (((zword_t) RANDOM_FUNC () % a) + 1);
260
261 }/* ziprandom */
262
263 /*
264 * test
265 *
266 * Jump if operand 2 bit mask not set in operand 1
267 *
268 */
269
270 #ifdef __STDC__
test(zword_t a,zword_t b)271 void test (zword_t a, zword_t b)
272 #else
273 void test (a, b)
274 zword_t a, b;
275 #endif
276 {
277
278 conditional_jump (((~a) & b) == 0);
279
280 }/* test */
281
282 /*
283 * compare_zero
284 *
285 * Compare operand against zero
286 *
287 */
288
289 #ifdef __STDC__
compare_zero(zword_t a)290 void compare_zero (zword_t a)
291 #else
292 void compare_zero (a)
293 zword_t a;
294 #endif
295 {
296
297 conditional_jump (a == 0);
298
299 }/* compare_zero */
300
301 /*
302 * compare_je
303 *
304 * Jump if operand 1 is equal to any other operand
305 *
306 */
307
308 #ifdef __STDC__
compare_je(int count,zword_t * operand)309 void compare_je (int count, zword_t *operand)
310 #else
311 void compare_je (count, operand)
312 int count;
313 zword_t *operand;
314 #endif
315 {
316 int i;
317
318 for (i = 1; i < count; i++)
319 if (operand[0] == operand[i]) {
320 conditional_jump (TRUE);
321 return;
322 }
323 conditional_jump (FALSE);
324
325 }/* compare_je */
326
327 /*
328 * compare_jl
329 *
330 * Jump if operand 1 is less than operand 2
331 *
332 */
333
334 #ifdef __STDC__
compare_jl(zword_t a,zword_t b)335 void compare_jl (zword_t a, zword_t b)
336 #else
337 void compare_jl (a, b)
338 zword_t a, b;
339 #endif
340 {
341
342 conditional_jump ((short) a < (short) b);
343
344 }/* compare_jl */
345
346 /*
347 * compare_jg
348 *
349 * Jump if operand 1 is greater than operand 2
350 *
351 */
352
353 #ifdef __STDC__
compare_jg(zword_t a,zword_t b)354 void compare_jg (zword_t a, zword_t b)
355 #else
356 void compare_jg (a, b)
357 zword_t a, b;
358 #endif
359 {
360
361 conditional_jump ((short) a > (short) b);
362
363 }/* compare_jg */
364