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
|
typedef enum
{
LODI,
STO,
ADDI,
ADD,
SUBI,
SUB,
MULI,
MUL,
DIVI,
DIV,
INC,
DEC
} INSN;
f (pc)
short *pc;
{
long long stack[16], *sp = &stack[16], acc = 0;
for (;;)
{
switch ((INSN)*pc++)
{
case LODI:
*--sp = acc;
acc = ((long long)*pc++) << 32;
break;
case STO:
return (acc >> 32) + (((((unsigned long long) acc) & 0xffffffff) & (1 << 31)) != 0);
break;
case ADDI:
acc += ((long long)*pc++) << 32;
break;
case ADD:
acc = *sp++ + acc;
break;
case SUBI:
acc -= ((long long)*pc++) << 32;
break;
case SUB:
acc = *sp++ - acc;
break;
case MULI:
acc *= *pc++;
break;
case MUL:
{
long long aux;
unsigned char minus;
minus = 0;
aux = *sp++;
if (aux < 0)
{
minus = ~minus;
aux = -aux;
}
if (acc < 0)
{
minus = ~minus;
acc = -acc;
}
acc = ((((((unsigned long long) acc) & 0xffffffff) * (((unsigned long long) aux) & 0xffffffff)) >> 32)
+ ((((unsigned long long) acc) >> 32) * (((unsigned long long) aux) & 0xffffffff) + (((unsigned long long) acc) & 0xffffffff) + (((unsigned long long) aux) >> 32))
+ (((((unsigned long long) acc) >> 32) * (((unsigned long long) aux) >> 32)) << 32));
if (minus)
acc = -acc;
}
break;
case DIVI:
{
short aux;
aux = *pc++;
acc = (acc + aux / 2) / aux;
}
break;
case DIV:
{
long long aux;
unsigned char minus;
minus = 0;
aux = *sp++;
if (aux < 0)
{
minus = ~minus;
aux = -aux;
}
if (acc < 0)
{
minus = ~minus;
acc = -acc;
}
if (((unsigned long long)acc) == 0)
acc = (unsigned long long)-1 / 2;
else if ((((unsigned long long) ((unsigned long long)acc)) & 0xffffffff) == 0)
acc = ((unsigned long long)aux) / (((unsigned long long) ((unsigned long long)acc)) >> 32);
else if ((((unsigned long long) ((unsigned long long)acc)) >> 32) == 0)
acc = ((((unsigned long long)aux) / ((unsigned long long)acc)) << 32)
+ ((((unsigned long long)aux) % ((unsigned long long)acc)) << 32) / ((unsigned long long)acc);
else
{
unsigned char shift;
unsigned long hi;
shift = 32;
hi = (((unsigned long long) ((unsigned long long)acc)) >> 32);
do {
if (hi & ((unsigned long)1 << (shift - 1)))
break;
} while (--shift != 0);
printf("shift = %d\n", shift);
acc = ((((unsigned long long)aux) / ((unsigned long long)acc)) << 32)
+ (((((unsigned long long)aux) % ((unsigned long long)acc)) << (32 - shift)) + ((((unsigned long long)acc) >> shift) / 2)) / (((unsigned long long)acc) >> shift);
}
if (minus)
acc = -acc;
}
break;
case INC:
acc += 1;
break;
case DEC:
acc -= 1;
break;
}
printf("%08lx.%08lx\n", (long)(((unsigned long long) acc) >> 32) , (long)(((unsigned long long) acc) & 0xffffffff));
}
}
|