File: math.c

package info (click to toggle)
fizmo 0.7.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,924 kB
  • sloc: ansic: 27,167; makefile: 1,083; cpp: 257
file content (444 lines) | stat: -rw-r--r-- 11,244 bytes parent folder | download
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444

/* math.c
 *
 * This file is part of fizmo.
 *
 * Copyright (c) 2009-2012 Christoph Ender.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#ifndef math_c_INCLUDED
#define math_c_INCLUDED

#include <time.h>
#include <sys/time.h>
#include <time.h>
#include <math.h> // for rint
#include <stdlib.h> // for [s]random on Mac OS X
#include <string.h>

#include "../tools/tracelog.h"
#include "../tools/i18n.h"
#include "math.h"
#include "config.h"
#include "mt19937ar.h"
#include "variable.h"
#include "zpu.h"
#include "../locales/libfizmo_locales.h"


static int16_t predictable_upper_border = 999;
static int16_t last_predictable_random = 0;
static unsigned uninitialized_unsigned;


void opcode_and(void)
{
  TRACE_LOG("Opcode: AND.\n");
  read_z_result_variable();
  TRACE_LOG("ANDing %x and %x to %x.\n", op[0], op[1], op[0] & op[1]);
  set_variable(z_res_var, op[0] & op[1], false);
}


void opcode_add(void)
{
  TRACE_LOG("Opcode: ADD.\n");
  read_z_result_variable();
  TRACE_LOG("Adding %d and %d.\n", (int16_t)op[0], (int16_t)op[1]);
  set_variable(z_res_var, (uint16_t)(((int16_t)op[0]) + ((int16_t) op[1])),
      false);
}


void opcode_sub(void)
{
  TRACE_LOG("Opcode: SUB.\n");
  read_z_result_variable();
  // (Updates / Clarifications): Opcode operands are always evaluated
  // from first to last -- this order is important when the stack
  // pointer appears as an argument. Thus "@sub sp sp" subtracts the
  // second-from-top stack item from the topmost stack item.
  TRACE_LOG("Subtracting %x from %x.\n", (int16_t)op[0], (int16_t)op[1]);
  set_variable(z_res_var, (uint16_t)(((int16_t)op[0]) - ((int16_t)op[1])),
      false);
}


void opcode_je(void)
{
  uint8_t result = 0;
  uint8_t i;

  TRACE_LOG("Opcode: JE.\n");
  // je can take between 2 and 4 operands. je with just 1 operand
  // is not permitted.

  if (number_of_operands == 1)
    i18n_translate_and_exit(
        libfizmo_module_name,
        i18n_libfizmo_OPCODE_JE_WITH_ONLY_1_OPERAND_IS_ILLEGAL,
        -1);

  TRACE_LOG("Number of operands for JE: %d.\n", number_of_operands);

  for (i=1; i<number_of_operands; i++)
  {
    TRACE_LOG("Comparing %x and %x.\n", op[0], op[i]);

    if (op[0] == op[i])
    {
      result = 1;
      break;
    }
  }

  evaluate_branch(result == 1 ? (uint8_t)1 : (uint8_t)0);
}


void opcode_jz(void)
{
  TRACE_LOG("Opcode: JZ.\n");
  TRACE_LOG("Checking whether %x is equal zero.\n", op[0]);
  evaluate_branch(op[0] == 0 ? (uint8_t)1 : (uint8_t)0);
}


void opcode_jg(void)
{
  TRACE_LOG("Opcode: JG.\n");
  TRACE_LOG("Jump if %d is greater than %d.\n", (int16_t)op[0], (int16_t)op[1]);
  evaluate_branch(
      ((int16_t)op[0]) > ((int16_t)op[1]) ? (uint8_t)1 : (uint8_t)0);
}


void opcode_inc_chk(void)
{
  int16_t value;

  // FIXME: Is all this signed? I hope so, because JG is singed.

  TRACE_LOG("Opcode: INC_CHK.\n");

  //Indirect variable references
  // In the seven opcodes that take indirect variable references
  // (inc, dec, inc_chk, dec_chk, load, store, pull), an indirect
  // reference to the stack pointer does not push or pull the top
  // item of the stack -- it is read or written in place.

  value = (int16_t)get_variable(op[0], false);

  TRACE_LOG("Incrementing variable with code %d from %d to %d.\n",
      op[0], value, value+1);

  value++;
  set_variable(op[0], (uint16_t)value, false);

  TRACE_LOG("Checking whether %d > %d.\n", value, (int16_t)op[1]);

  evaluate_branch(value > ((int16_t)op[1]) ? (uint8_t)1 : (uint8_t)0);
}


void opcode_test(void)
{
  TRACE_LOG("Opcode: TEST.\n");
  TRACE_LOG("Testing if %x in %x is set.\n", op[0], op[1]);
  evaluate_branch(((op[0] & op[1]) == op[1]) ? (uint8_t)1 : (uint8_t)0);
}


void opcode_jl(void)
{
  // Jump if a < b (using a signed 16-bit comparison).
  TRACE_LOG("Opcode: JL.\n");
  TRACE_LOG("Testing if %i is smaller than %i.\n",
      (int16_t)op[0], (int16_t)op[1]);
  evaluate_branch(
      (((int16_t)op[0]) < ((int16_t)op[1])) ? (uint8_t)1: (uint8_t)0);
}


void opcode_dec_chk(void)
{
  int16_t value;

  // FIXME: Is all this signed? I hope so, because JG is singed.

  TRACE_LOG("Opcode: DEC_CHK.\n");

  value = (int16_t)get_variable(op[0], false);

  TRACE_LOG("Decrementing variable with code %d from %d to %d.\n",
      op[0], value, value-1);

  value--;
  set_variable(op[0], (uint16_t)value, false);

  TRACE_LOG("Checking whether %d < %d.\n", value, (int16_t)op[1]);

  evaluate_branch(value < ((int16_t)op[1]) ? (uint8_t)1 : (uint8_t)0);
}


void opcode_mul(void)
{
  TRACE_LOG("Opcode: MUL.\n");

  read_z_result_variable();

  TRACE_LOG("Multiplying %d and %d.\n", (int16_t)op[0], (int16_t)op[1]);

  set_variable(z_res_var, (uint16_t)(((int16_t)op[0]) * ((int16_t) op[1])),
      false);
}


void seed_random_generator(void)
{
  unsigned long init[RANDOM_SEED_SIZE];
  time_t seconds;
  int i;

  if ((seconds = time(NULL)) == (time_t)-1)
    i18n_translate_and_exit(
        libfizmo_module_name,
        i18n_libfizmo_FUNCTION_CALL_TIME_RETURNED_NEG_1,
        -1);

  srand(((unsigned)seconds) ^ uninitialized_unsigned);

  // Init parameters are alternately seeded with a pseudo-random value and
  // the uninitialzed stack memory.
  for (i=0; i<RANDOM_SEED_SIZE; i+=2)
    init[i] = (unsigned long)rand();

  init_by_array(init, RANDOM_SEED_SIZE);
}


void opcode_random(void)
{
  unsigned long int random_number;
  double multiplier;
  char *ptr;

  TRACE_LOG("Opcode: RANDOM.\n");

  // If range is positive, returns a uniformly random number between
  // 1 and range. If range is negative, the random number generator
  // is seeded to that value and the return value is 0. Most interpreters
  // consider giving 0 as range illegal (because they attempt a division
  // with remainder by the range), but correct behaviour is to reseed the
  // generator in as random a way as the interpreter can (e.g. by using
  // the time in milliseconds).

  read_z_result_variable();

  if (op[0] == 0)
  {
    seed_random_generator();
  }
  else if ((int16_t)op[0] < 0)
  {
    // If range is negative, the random number generator is seeded to
    // that value and the return value is 0.

    ptr = get_configuration_value("random-mode");
    if (
        (ptr == NULL)
        ||
        (strcmp(ptr, "predictable") != 0)
        ||
        ((int16_t)op[0] <= -1000)
       )
    {
      init_genrand((unsigned long)(int16_t)op[0]);
    }
    else
    {
      predictable_upper_border = abs((int16_t)op[0]);

      TRACE_LOG("Setting predictable_upper_border to %d.\n",
          predictable_upper_border);
    }

    set_variable(z_res_var, 0, false);
  }
  else
  {
    // If range is positive, returns a uniformly random number between 1
    // and range.

    ptr = get_configuration_value("random-mode");
    if (
        (ptr == NULL)
        ||
        (strcmp(ptr, "predictable") != 0)
        ||
        (predictable_upper_border >= 1000)
       )
    {
      TRACE_LOG("Result should be >= 1 and <= %d.\n", op[0]);

      random_number = genrand_int32();

      TRACE_LOG("Random number drawn: %ld.\n", random_number);

      multiplier = (double)(op[0] - 1) / (double)GENRAND_INT32_MAX;
      // Subtract 1 from op[0] since we want to cover the range from 1 to op[0],
      // not 0 to op[0].

      TRACE_LOG("Result: %d.\n",
          ((uint16_t)rint((double)random_number * multiplier)) + 1);

      set_variable(
          z_res_var,
          ((uint16_t)rint((double)random_number * multiplier)) + 1,
          false);
    }
    else
    {
      TRACE_LOG("Random generator in predictable mode.\n");

      last_predictable_random++;

      if (last_predictable_random > predictable_upper_border)
        last_predictable_random = 1;

      TRACE_LOG("Returning random value %d\n", last_predictable_random);

      set_variable(
          z_res_var,
          (uint16_t)last_predictable_random,
          false);
    }
  }
}


void opcode_div(void)
{
  TRACE_LOG("Opcode: DIV.\n");

  read_z_result_variable();

  if (op[1] == 0)
    i18n_translate_and_exit(
        libfizmo_module_name,
        i18n_libfizmo_CANNOT_DIVIDE_BY_ZERO,
        -1);

  TRACE_LOG("Dividing %d by %d.\n", (int16_t)op[0], (int16_t)op[1]);

  set_variable(z_res_var, (uint16_t)(((int16_t)op[0]) / ((int16_t)op[1])),
      false);
}


void opcode_or(void)
{
  TRACE_LOG("Opcode: OR.\n");
  read_z_result_variable();
  TRACE_LOG("ORing %x and %x to %x.\n", op[0], op[1], op[0] | op[1]);
  set_variable(z_res_var, op[0] | op[1], false);
}


void opcode_mod(void)
{
  TRACE_LOG("Opcode: MOD.\n");

  if (op[1] == 0)
    i18n_translate_and_exit(
        libfizmo_module_name,
        i18n_libfizmo_CANNOT_DIVIDE_BY_ZERO,
        -1);

  read_z_result_variable();

  TRACE_LOG("MODing %d and %d to %d.\n",
      (int16_t)op[0],
      (int16_t)op[1],
      (uint16_t)((int16_t)op[0] % (int16_t)op[1]));

  set_variable(z_res_var, (uint16_t)((int16_t)op[0] % (int16_t)op[1]), false);
}


void opcode_not(void)
{
  TRACE_LOG("Opcode: NOT.\n");
  read_z_result_variable();
  TRACE_LOG("NOTing %x to %x.\n", op[0], ~op[0]);
  set_variable(z_res_var, ~op[0], false);
}


void opcode_art_shift(void)
{
  int16_t result = (int16_t)op[0];
  int16_t shift_places = (int16_t)op[1];

  TRACE_LOG("Opcode: ART_SHIFT.\n");

  read_z_result_variable();

  /*@-shiftnegative@*/
  /*@-shiftimplementation@*/

  if (shift_places > 0)
    result <<= shift_places;
  else if (shift_places < 0)
    result >>= (-shift_places);

  /*@+shiftimplementation@*/
  /*@+shiftnegative@*/

  set_variable(z_res_var, (uint16_t)result, false);
}


void opcode_log_shift(void)
{
  uint16_t result = (uint16_t)op[0];
  int16_t shift_places = (int16_t)op[1];

  read_z_result_variable();

  if (shift_places > 0)
    result <<= shift_places;
  else if (shift_places < 0)
    result >>= (-shift_places);

  set_variable(z_res_var, result, false);
}


#endif /* math_c_INCLUDED */