923bcde0d887a8a092037cdb1a84c94bc9ed371a
[projects/chimara/chimara.git] / interpreters / nitfol / op_math.c
1 /*  Nitfol - z-machine interpreter using Glk for output.
2     Copyright (C) 1999  Evin Robertson
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
17
18     The author can be reached at nitfol@deja.com
19 */
20 #include "nitfol.h"
21
22
23 void op_load(void)
24 {
25   mop_store_result(get_var(operand[0]));
26 }
27
28 void op_store(void)
29 {
30   set_var(operand[0], operand[1]);
31 }
32
33 void op_add(void)
34 {
35   mop_store_result(ARITHMASK(operand[0] + operand[1]));
36 }
37
38 void op_sub(void)
39 {
40   mop_store_result(ARITHMASK(operand[0] + neg(operand[1])));
41 }
42
43
44 void op_and(void)
45 {
46   mop_store_result(operand[0] & operand[1]);
47 }
48
49 void op_or(void)
50 {
51   mop_store_result(operand[0] | operand[1]);
52 }
53
54 void op_not(void)
55 {
56   mop_store_result(ARITHMASK(~operand[0]));
57 }
58
59
60 void op_art_shift(void)
61 {
62   if(is_neg(operand[1])) {
63     if(is_neg(operand[0])) {
64       zword i;
65       zword foo = operand[0];
66       /* FIXME: is there a better way? */
67       for(i = 0; i < neg(operand[1]); i++) {
68         foo >>= 1;
69         foo |= ZWORD_TOPBITMASK;
70       }
71       mop_store_result(foo);
72     } else {
73       mop_store_result(operand[0] >> neg(operand[1]));
74     }
75   } else {
76       mop_store_result(ARITHMASK(operand[0] << operand[1]));
77   }
78 }
79
80 void op_log_shift(void)
81 {
82   if(is_neg(operand[1]))
83     mop_store_result(operand[0] >> neg(operand[1]) );
84   else
85     mop_store_result(ARITHMASK(operand[0] << operand[1]));
86 }
87
88
89 void op_dec(void)
90 {
91   zword val = ARITHMASK(get_var(operand[0]) - 1);
92   set_var(operand[0], val);
93 }
94
95 void op_dec_chk(void)
96 {
97   zword val = ARITHMASK(get_var(operand[0]) - 1);
98   set_var(operand[0], val);
99
100   if(is_lessthan(val, operand[1]))
101     mop_take_branch();
102   else
103     mop_skip_branch();
104 }
105
106 void op_inc(void)
107 {
108   zword val = ARITHMASK(get_var(operand[0]) + 1);
109   set_var(operand[0], val);
110 }
111
112 void op_inc_chk(void)
113 {
114   unsigned val = ARITHMASK(get_var(operand[0]) + 1);
115   set_var(operand[0], val);
116
117   if(is_greaterthan(val, operand[1]))
118     mop_take_branch();
119   else
120     mop_skip_branch();
121 }
122
123
124 zword z_mult(zword a, zword b)
125 {
126   int sign = 0;
127
128   if(is_neg(a)) {
129     a = neg(a);
130     sign = 1;
131   }
132   if(is_neg(b)) {
133     b = neg(b);
134     sign ^= 1;
135   }
136
137   if(sign)
138     return ARITHMASK(neg(a * b));
139   else
140     return ARITHMASK(a * b);
141
142 }
143
144
145 zword z_div(zword a, zword b)
146 {
147   int sign = 0;
148   
149   if(b == 0) {
150     n_show_error(E_MATH, "division by zero", a);
151     return ZWORD_MAX;
152   }
153   
154   if(is_neg(a)) {
155     a = neg(a);
156     sign = 1;
157   }
158   if(is_neg(b)) {
159     b = neg(b);
160     sign ^= 1;
161   }
162
163   if(sign)
164     return neg(a / b);
165   else
166     return a / b;
167 }
168
169
170 zword z_mod(zword a, zword b)
171 {
172   int sign = 0;
173
174   if(b == 0) {
175     n_show_error(E_MATH, "modulo by zero", a);
176     return 0;
177   }
178
179   if(is_neg(a)) {
180     a = neg(a);
181     sign = 1;
182   }
183   if(is_neg(b)) {
184     b = neg(b);
185   }
186
187   if(sign)
188     return neg(a % b);
189   else
190     return a % b;
191 }
192
193
194 void op_div(void)
195 {
196   mop_store_result(z_div(operand[0], operand[1]));
197 }
198
199
200 void op_mod(void)
201 {
202   mop_store_result(z_mod(operand[0], operand[1]));
203 }
204
205
206 void op_mul(void)
207 {
208   mop_store_result(z_mult(operand[0], operand[1]));
209 }
210
211
212 /* FIXME: use our own rng */
213 zword z_random(zword num)
214 {
215   static BOOL rising = FALSE;
216   static unsigned r = 0;
217   zword result = 0;
218   
219   if(num == 0) {
220     if(faked_random_seed)
221       srand(faked_random_seed);
222     else
223       srand(time(NULL));
224     rising = FALSE;
225   } else if(is_neg(num)) {
226     if(neg(num) < 1000) {
227       r = 0;
228       rising = TRUE;
229     } else {
230       srand(neg(num));
231       rising = FALSE;
232     }
233   } else {
234     if(rising) {
235       r++;
236       if(r > num)
237         r = 1;
238       result = r;
239     } else {
240       result = (1 + (rand() % num));
241     }
242   }
243   return result;
244 }
245
246 void op_random(void)
247 {
248   if(operand[0] == 0)
249     n_show_port(E_MATH, "some interpreters don't like @random 0", operand[0]);
250
251   mop_store_result(z_random(operand[0]));
252 }
253