vu.c
4.84 KB
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
/*
* Vector Unit instruction Simulation
*/
#include <stdio.h>
#include "graphic.h"
/*
* These macros makes VU code looks more symbolic, it assumes
* i loop ctr for VU units
* d dst reg
* s src reg
* t 2nd src
* e scalar element
* sft post multiply/post accumulate shift
* sc where this is a scalar operation
*/
#define VD (vu.vregs[i][d])
#define VDs (vu.vregs[e][d]) /* scalar D */
#define VS (vu.vregs[i][s])
#define VT (sc ? vu.vregs[e][t] : vu.vregs[i][t])
#define ACC (vu.vacc[i])
#define VCC (vu.vcc)
#define SFT(x) (sft > 0 ? x << sft : x >> -sft)
static short space;
vinit()
{
int i, r;
for (i=0; i<VU_NMAC; i++) {
vu.vacc[i] = 0;
for (r=0; r<VU_NREG; r++)
vu.vregs[i][r] = 0;
}
/* XXX make sure SPECed as hardwired 1 */
for (i=0; i<VU_NMAC; i++)
vu.vregs[i][1] = 1;
vu.vcc = 0;
}
vload(int r, short *a) /* load */
{
int i;
/*
* register 0 = 0 read only
*/
if (r == 0) {
fprintf(stderr, "vld: attemp to load register 0\n");
exit(EXIT_FAILURE);
}
for (i=0; i<VU_NMAC; i++)
vu.vregs[i][r] = *a++;
}
vstore(int r, short *a) /* store */
{
int i;
for (i=0; i<VU_NMAC; i++)
*a++ = vu.vregs[i][r];
}
vu_mul(int s, int t, int e, int d, int double_prec, int sft, int sc)
{
int i;
for (i=0; i<VU_NMAC; i++) {
if (double_prec) {
ACC = SFT( mul16( VS, VT));
VD = ACC & 0x7fff;
}
else {
/* round if losing precision */
if (sft == -15) ACC = mul16( VS, VT) + (1<<(-sft-1));
else ACC = mul16( VS, VT);
VD = SFT(ACC);
}
}
}
vu_mac(int s, int t, int e, int d, int double_prec, int sft, int sc)
{
int i;
for (i=0; i<VU_NMAC; i++) {
if (double_prec) {
ACC += SFT( mul16( VS, VT));
VD = ACC & 0x7fff;
}
else {
ACC += mul16( VS, VT);
VD = SFT(ACC);
}
}
}
vu_mulu() {}
vu_macu() {}
vu_div(int s, int t, int e, int d, int sc) /* div, scalar T */
{
float r;
short i, f;
if (VT == 0) {
fprintf(stderr, "divide by 0\n");
/*
exit(EXIT_FAILURE);
*/ return;
}
/*
r = 1/(float)VT;
*/
{
int n, m;
n = VT;
divide_seed( n, &m);
r = (float)m * ((float)1/DIV_RECP_FRAC);
}
ftos(r, &i, &f);
f |= (r < 0) ? 0x8000 : 0; /* do a signed fraction */
VDs = f;
}
vu_divd(int s, int t, int e, int d, int high, int sc) /* double div */
{
static int i;
double r;
int tmp;
static short *h;
if (high) {
i = VT << 15;
h = &(VDs);
}
else {
i |= VT;
if (i == 0) {
fprintf(stderr, "divide by 0\n");
/*
exit(EXIT_FAILURE);
*/ return;
}
/*
tmp = (int)((double)0x40000000/(double)i);
*/
{
int n, m;
n = i;
divide_seed( n, &m);
/* used to be 30 bit fraction, divide seed is 31 bit fraction */
divide_newt( n, m, &m);
tmp = (int)((double)0x40000000 *
(float)m * ((float)1/DIV_RECP_FRAC));
}
*h = tmp >> 15;
VDs = tmp & 0x7fff;
}
}
vu_select(int s, int t, int e, int cc, int sc)
{
int i;
for (i=0; i<VU_NMAC; i++) {
switch (cc) {
case SEL_LT:
case SEL_LE:
case SEL_EQ:
case SEL_NE:
case SEL_GE:
case SEL_GT:
case SEL_MERGE:
break;
case SEL_CL:
if (VS <= -VT) VCC |= (1<<(2*i)); else VCC &= ~(1<<(2*i));
if (VS >= VT) VCC |= (1<<(2*i+1)); else VCC &= ~(1<<(2*i+1));
break;
}
}
/* mask out the clip code compares with element e againest itself */
/* basically, avoid w <= w and w >= w */
if (cc == SEL_CL) VCC &= ~(0x3 << (2*e));
}
/*-----------------------------------------------------------*/
/*
* Interface routines to map simulator data structures into VU
*/
vloadmtx(int r, struct Matrix *mtx)
{
short si[4], sf[4];
int col, row;
for (row=0; row<4; row++) {
for (col=0; col<4; col++) {
ftos(mtx->m[row][col], &(si[col]), &(sf[col]));
}
vload(r++, si);
vload(r++, sf);
}
}
vloaddc(int r, struct Image_space *is)
{
short dcs[4], dct[4], frac[4];
float f;
ftos(is->sx, &(dcs[0]), &(frac[0]));
ftos(is->sy, &(dcs[1]), &(frac[1]));
ftos(is->sz, &(dcs[2]), &(frac[2]));
ftos(1.0, &(dcs[3]), &(frac[3]));
ftos(is->tx, &(dct[0]), &(frac[0]));
ftos(is->ty, &(dct[1]), &(frac[1]));
ftos(is->tz, &(dct[2]), &(frac[2]));
ftos(1.0, &(dct[3]), &(frac[3]));
vload(r++, dcs);
vload(r++, dct);
}
/*
* convert double precision (float and int) to short routines
*/
void ftos(float f, short *si, short *sf)
{
int i;
i = (int)(f * 0x8000 + 0.5); /* scale up for 15 frac bits */
*sf = i & 0x7fff; /* get the fraction, positive number */
*si = i >> 15; /* get the int, signed number */
}
void itos(int i, short *si, short *sf)
{
*sf = i & 0x7fff; /* get the fraction, positive number */
*si = i >> 15; /* get the int, signed number */
}
/*
* convert short pair to float
*/
void stof(short si, short sf, float *f)
{
*f = (float)si + (float)(sf)/(float)(0x8000);
}
/*
* print VU register set as a familiar floating point matrix
*/
void vprintmtx(int r)
{
int row, col;
float f;
for (row = 0; row < 4; row++) {
for (col=0; col<4; col++) {
stof(vu.vregs[col][r], vu.vregs[col][r+1], &f);
printf("%8.3f ", f);
}
printf("\n");
r += 2;
}
}