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
|
package gfx;
typedef logic[31:0] word;
typedef word uword;
typedef logic signed[$bits(word) - 1:0] sword;
typedef logic[$bits(word) / 2 - 1:0] uhword;
typedef logic signed[$bits(word) / 2 - 1:0] shword;
typedef logic[2 * $bits(word) - 1:0] udword;
typedef logic signed[2 * $bits(word) - 1:0] sdword;
typedef logic signed[4 * $bits(word) - 1:0] qword;
typedef logic signed[8 * $bits(word) - 1:0] oword;
localparam int SUBWORD_BITS = $clog2($bits(word)) - $clog2($bits(byte));
localparam int BYTES_PER_WORD = 1 << SUBWORD_BITS;
typedef logic[$bits(word) - SUBWORD_BITS - 1:0] word_ptr;
typedef logic[$bits(word_ptr) - 1 - 1:0] dword_ptr;
typedef logic[$bits(word_ptr) - 2 - 1:0] qword_ptr;
typedef logic[$bits(word_ptr) - 3 - 1:0] oword_ptr;
typedef logic[7:0] float_exp;
typedef logic[$bits(word) - $bits(float_exp) - 2:0] float_mant;
typedef logic[$bits(float_mant):0] float_mant_full; // Incluye '1.' explícito
typedef logic[$bits(float_mant_full) + 1:0] float_mant_ext; // Considera overflow
localparam float_exp FLOAT_EXP_BIAS = (1 << ($bits(float_exp) - 1)) - 1;
localparam float_exp FLOAT_EXP_MAX = {($bits(float_exp)){1'b1}};
function float_mant_full full_mant(float_mant in);
full_mant = {1'b1, in};
endfunction
function float_mant implicit_mant(float_mant_full in);
assert (in[$bits(in) - 1]);
implicit_mant = in[$bits(in) - 2:0];
endfunction
typedef struct packed
{
logic sign;
float_exp exp;
float_mant mant;
} float;
/* Explicación de guard, round, sticky:
* https://drilian.com/2023/01/10/floating-point-numbers-and-rounding/
*/
typedef struct packed
{
float normal;
logic slow,
zero,
guard,
round,
sticky;
} float_round;
typedef struct packed
{
logic exp_max,
exp_min,
mant_zero;
} float_class;
function float_class classify_float(float in);
classify_float.exp_max = &in.exp;
classify_float.exp_min = ~|in.exp;
classify_float.mant_zero = ~|in.mant;
endfunction
function logic is_float_special(float_class in);
is_float_special = in.exp_max | (in.exp_min & ~in.mant_zero);
endfunction
function float_mant_ext float_prepare_round(float in, float_class in_class);
float_prepare_round = {~in_class.exp_min, in.mant, 2'b00};
endfunction
// -> 4,4,4,4,4,4,4,4 -> 8,8,8,8 -> 16,16 -> 32
localparam int FPINT_CLZ_STAGES = 4;
localparam int FPINT_STAGES = 7 + FPINT_CLZ_STAGES + 4;
localparam bit[$clog2($bits(float_mant_ext)):0] FPINT_MAX_SHIFT
= 1 << $clog2($bits(float_mant_ext));
typedef logic[$clog2(FPINT_MAX_SHIFT):0] fpint_shift;
typedef struct packed
{
logic setup_mul_float,
setup_unit_b,
mnorm_put_hi,
mnorm_put_lo,
mnorm_put_mul,
mnorm_zero_b,
mnorm_zero_flags,
minmax_abs,
minmax_swap,
minmax_zero_min,
minmax_copy_flags,
shiftr_int_signed,
addsub_copy_flags,
addsub_int_operand,
clz_force_nop,
shiftl_copy_flags,
round_copy_flags,
round_enable,
encode_enable,
writeback;
} fpint_op;
typedef struct packed
{
float a,
b,
a_mul,
b_mul;
} fpint_setup_mulclass;
typedef struct packed
{
float b;
float_exp exp;
float_class a_class,
b_class;
udword product;
logic sign,
overflow;
} fpint_mulclass_mnorm;
typedef struct packed
{
float a,
b;
float_class a_class,
b_class;
logic slow,
zero,
guard,
round,
sticky,
slow_in,
overflow;
} fpint_mnorm_minmax;
typedef struct packed
{
float max,
min;
float_class max_class,
min_class;
logic slow,
zero,
guard,
round,
sticky;
} fpint_minmax_expdiff;
typedef struct packed
{
float max,
min;
float_class max_class,
min_class;
fpint_shift exp_shift;
logic slow,
zero,
guard,
round,
sticky;
} fpint_expdiff_shiftr;
typedef struct packed
{
float max,
min;
float_class max_class,
min_class;
float_mant_ext max_mant,
min_mant,
sticky_mask;
logic slow,
zero,
guard,
round,
sticky,
int_sign;
} fpint_shiftr_addsub;
typedef struct packed
{
float max;
word add_sub;
logic slow,
zero,
guard,
round,
sticky;
} fpint_clz_hold;
typedef fpint_clz_hold fpint_addsub_clz;
typedef struct packed
{
fpint_clz_hold hold;
fpint_shift shift;
} fpint_clz_shiftl;
typedef struct packed
{
float val;
logic slow,
zero,
guard,
round,
sticky,
overflow,
sticky_last;
} fpint_shiftl_round;
typedef struct packed
{
float val;
logic slow,
zero,
exp_step,
overflow;
} fpint_round_rnorm;
typedef struct packed
{
float val;
logic slow,
zero,
overflow;
} fpint_rnorm_encode;
typedef struct packed
{
logic todo;
} mem_op;
typedef struct packed
{
logic todo;
} sfu_op;
typedef struct packed
{
logic todo;
} group_op;
// Q22.10
typedef logic[9:0] fixed_frac;
typedef logic[$bits(word) - $bits(fixed_frac) - 1:0] fixed_int;
typedef struct packed signed
{
fixed_int fint; // 'int' es una keyword
fixed_frac frac;
} fixed;
typedef struct packed
{
fixed x,
y;
} fixed_xy;
typedef struct packed
{
fixed a,
b,
c;
} vtx_fixed;
typedef struct packed
{
fixed_xy a,
b,
c;
} vtx_xy;
localparam int RASTER_BITS = 2;
localparam int RASTER_SUB_BITS = 4;
localparam int RASTER_SIZE = 1 << RASTER_BITS;
localparam int RASTER_COARSE_FRAGS = RASTER_SIZE * RASTER_SIZE;
typedef logic[RASTER_BITS - 1:0] raster_index;
// Caso RASTER_BITS = 2: -> 4,4,4,4 -> 8,8-> 16
localparam int RASTER_OUT_CLZ_DEPTH = 3;
// Asume RASTER_BITS == 2, hay que ajustarlo si cambia
typedef struct packed
{
// Esto ahorra muchos flops
//
// offsets[0] = inc * 0 = 0
// offsets[1] = inc * 1 = raster2_times1
// offsets[2] = inc * 2 = raster2_times1 << 1
// offsets[3] = inc * 3 = raster2_times3
fixed raster2_times1,
raster2_times3;
} raster_offsets;
function fixed raster_idx(raster_offsets offsets, raster_index idx);
unique case (idx)
RASTER_BITS'(0):
return '0;
RASTER_BITS'(1):
return offsets.raster2_times1;
RASTER_BITS'(2):
return offsets.raster2_times1 << 1;
RASTER_BITS'(3):
return offsets.raster2_times3;
endcase
endfunction
function raster_offsets make_raster_offsets(fixed inc);
make_raster_offsets.raster2_times1 = inc;
make_raster_offsets.raster2_times3 = inc + (inc << 1);
endfunction
typedef struct packed
{
raster_offsets x,
y;
} raster_offsets_xy;
typedef struct packed
{
logic[RASTER_SUB_BITS - 1:0] num;
logic[$bits(fixed_frac) - RASTER_SUB_BITS - 1:0] prec;
} raster_sub;
localparam int RASTER_COARSE_DIM_BITS = $bits(fixed) - $bits(raster_index) - $bits(raster_sub);
typedef logic signed[RASTER_COARSE_DIM_BITS - 1:0] raster_coarse_dim;
typedef struct packed
{
raster_coarse_dim x,
y;
} raster_coarse_xy;
typedef struct packed signed
{
raster_coarse_dim coarse;
raster_index fine;
raster_sub sub;
} raster_prec;
typedef struct packed
{
raster_prec x,
y;
} raster_prec_xy;
// Definir el número de lanes a partir de las dimensiones del
// rasterizer es una decisión crucial, el diseño entero depende de esto
localparam int SHADER_LANES = RASTER_COARSE_FRAGS;
typedef logic[RASTER_SIZE - 1:0] lane_no;
typedef logic[SHADER_LANES - 1:0] lane_mask;
typedef logic[5:0] group_id;
localparam int REGFILE_STAGES = 3;
localparam int REG_READ_STAGES = 2 + REGFILE_STAGES + 1;
typedef gfx_isa::sgpr_num sgpr_num;
typedef gfx_isa::vgpr_num vgpr_num;
typedef gfx_isa::xgpr_num xgpr_num;
typedef struct packed
{
// No incluye p0 porque p0 no tiene señal ready
logic p1,
p2,
p3,
valid;
} shader_dispatch;
localparam int FIXED_MULADD_DEPTH = 5;
localparam int FIXED_DOTADD_DEPTH = 2 * FIXED_MULADD_DEPTH;
localparam int SCHED_BRAM_WORDS = 2048; // 8KiB
typedef word irq_lines;
endpackage
|