db449a2d284888fde5064d04458c7647eca3032b
[mesa.git] / src / freedreno / rnn / rnndec.c
1 /*
2 * Copyright (C) 2010-2011 Marcin Koƛcielnicki <koriakin@0x04.net>
3 * Copyright (C) 2010 Francisco Jerez <currojerez@riseup.net>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #define _GNU_SOURCE // for asprintf
27 #include "rnndec.h"
28 #include <assert.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <inttypes.h>
33 #include "util.h"
34
35 struct rnndeccontext *rnndec_newcontext(struct rnndb *db) {
36 struct rnndeccontext *res = calloc (sizeof *res, 1);
37 res->db = db;
38 res->colors = &envy_null_colors;
39 return res;
40 }
41
42 int rnndec_varadd(struct rnndeccontext *ctx, char *varset, char *variant) {
43 struct rnnenum *en = rnn_findenum(ctx->db, varset);
44 if (!en) {
45 fprintf (stderr, "Enum %s doesn't exist in database!\n", varset);
46 return 0;
47 }
48 int i, j;
49 for (i = 0; i < en->valsnum; i++)
50 if (!strcasecmp(en->vals[i]->name, variant)) {
51 struct rnndecvariant *ci = calloc (sizeof *ci, 1);
52 ci->en = en;
53 ci->variant = i;
54 ADDARRAY(ctx->vars, ci);
55 return 1;
56 }
57
58 if (i == en->valsnum) {
59 fprintf (stderr, "Variant %s doesn't exist in enum %s!\n", variant, varset);
60 return 0;
61 }
62
63 for (j = 0; j < ctx->varsnum; j++) {
64 if (ctx->vars[j]->en == en) {
65 ctx->vars[j]->variant = i;
66 break;
67 }
68 }
69
70 if (i == ctx->varsnum) {
71 struct rnndecvariant *ci = calloc (sizeof *ci, 1);
72 ci->en = en;
73 ci->variant = i;
74 ADDARRAY(ctx->vars, ci);
75 }
76
77 return 1;
78 }
79
80 int rnndec_varmatch(struct rnndeccontext *ctx, struct rnnvarinfo *vi) {
81 if (vi->dead)
82 return 0;
83 int i;
84 for (i = 0; i < vi->varsetsnum; i++) {
85 int j;
86 for (j = 0; j < ctx->varsnum; j++)
87 if (vi->varsets[i]->venum == ctx->vars[j]->en)
88 break;
89 if (j == ctx->varsnum) {
90 fprintf (stderr, "I don't know which %s variant to use!\n", vi->varsets[i]->venum->name);
91 } else {
92 if (!vi->varsets[i]->variants[ctx->vars[j]->variant])
93 return 0;
94 }
95 }
96 return 1;
97 }
98
99 /* see https://en.wikipedia.org/wiki/Half-precision_floating-point_format */
100 static uint32_t float16i(uint16_t val)
101 {
102 uint32_t sign = ((uint32_t)(val & 0x8000)) << 16;
103 uint32_t frac = val & 0x3ff;
104 int32_t expn = (val >> 10) & 0x1f;
105
106 if (expn == 0) {
107 if (frac) {
108 /* denormalized number: */
109 int shift = __builtin_clz(frac) - 21;
110 frac <<= shift;
111 expn = -shift;
112 } else {
113 /* +/- zero: */
114 return sign;
115 }
116 } else if (expn == 0x1f) {
117 /* Inf/NaN: */
118 return sign | 0x7f800000 | (frac << 13);
119 }
120
121 return sign | ((expn + 127 - 15) << 23) | (frac << 13);
122 }
123 static float float16(uint16_t val)
124 {
125 union { uint32_t i; float f; } u;
126 u.i = float16i(val);
127 return u.f;
128 }
129
130 static const char *rnndec_decode_enum_val(struct rnndeccontext *ctx,
131 struct rnnvalue **vals, int valsnum, uint64_t value)
132 {
133 int i;
134 for (i = 0; i < valsnum; i++)
135 if (rnndec_varmatch(ctx, &vals[i]->varinfo) &&
136 vals[i]->valvalid && vals[i]->value == value)
137 return vals[i]->name;
138 return NULL;
139 }
140
141 char *rnndec_decode_enum(struct rnndeccontext *ctx, const char *enumname, uint64_t enumval)
142 {
143 struct rnnenum *en = rnn_findenum (ctx->db, enumname);
144 if (en) {
145 int i;
146 for (i = 0; i < en->valsnum; i++)
147 if (en->vals[i]->valvalid && en->vals[i]->value == enumval)
148 return en->vals[i]->name;
149 }
150 return NULL;
151 }
152
153 /* The name UNK%u is used as a placeholder for bitfields that exist but
154 * have an unknown function.
155 */
156 static int is_unknown(const char *name)
157 {
158 unsigned u;
159 return sscanf(name, "UNK%u", &u) == 1;
160 }
161
162 char *rnndec_decodeval(struct rnndeccontext *ctx, struct rnntypeinfo *ti, uint64_t value) {
163 int width = ti->high - ti->low + 1;
164 char *res = 0;
165 int i;
166 struct rnnvalue **vals;
167 int valsnum;
168 struct rnnbitfield **bitfields;
169 int bitfieldsnum;
170 char *tmp;
171 uint64_t mask, value_orig;
172 if (!ti)
173 goto failhex;
174 value_orig = value;
175 value = (value & typeinfo_mask(ti)) >> ti->low;
176 value <<= ti->shr;
177
178 switch (ti->type) {
179 case RNN_TTYPE_ENUM:
180 vals = ti->eenum->vals;
181 valsnum = ti->eenum->valsnum;
182 goto doenum;
183 case RNN_TTYPE_INLINE_ENUM:
184 vals = ti->vals;
185 valsnum = ti->valsnum;
186 goto doenum;
187 doenum:
188 tmp = rnndec_decode_enum_val(ctx, vals, valsnum, value);
189 if (tmp) {
190 asprintf (&res, "%s%s%s", ctx->colors->eval, tmp, ctx->colors->reset);
191 if (ti->addvariant) {
192 rnndec_varadd(ctx, ti->eenum->name, tmp);
193 }
194 break;
195 }
196 goto failhex;
197 case RNN_TTYPE_BITSET:
198 bitfields = ti->ebitset->bitfields;
199 bitfieldsnum = ti->ebitset->bitfieldsnum;
200 goto dobitset;
201 case RNN_TTYPE_INLINE_BITSET:
202 bitfields = ti->bitfields;
203 bitfieldsnum = ti->bitfieldsnum;
204 goto dobitset;
205 dobitset:
206 mask = 0;
207 for (i = 0; i < bitfieldsnum; i++) {
208 if (!rnndec_varmatch(ctx, &bitfields[i]->varinfo))
209 continue;
210 uint64_t type_mask = typeinfo_mask(&bitfields[i]->typeinfo);
211 if (((value & type_mask) == 0) && is_unknown(bitfields[i]->name))
212 continue;
213 mask |= type_mask;
214 if (bitfields[i]->typeinfo.type == RNN_TTYPE_BOOLEAN) {
215 const char *color = is_unknown(bitfields[i]->name) ?
216 ctx->colors->err : ctx->colors->mod;
217 if (value & type_mask) {
218 if (!res)
219 asprintf (&res, "%s%s%s", color, bitfields[i]->name, ctx->colors->reset);
220 else {
221 asprintf (&tmp, "%s | %s%s%s", res, color, bitfields[i]->name, ctx->colors->reset);
222 free(res);
223 res = tmp;
224 }
225 }
226 continue;
227 }
228 char *subval;
229 if (is_unknown(bitfields[i]->name) && (bitfields[i]->typeinfo.type != RNN_TTYPE_A3XX_REGID)) {
230 uint64_t field_val = value & type_mask;
231 field_val = (field_val & typeinfo_mask(&bitfields[i]->typeinfo)) >> bitfields[i]->typeinfo.low;
232 field_val <<= bitfields[i]->typeinfo.shr;
233 asprintf (&subval, "%s%#"PRIx64"%s", ctx->colors->err, field_val, ctx->colors->reset);
234 } else {
235 subval = rnndec_decodeval(ctx, &bitfields[i]->typeinfo, value & type_mask);
236 }
237 if (!res)
238 asprintf (&res, "%s%s%s = %s", ctx->colors->rname, bitfields[i]->name, ctx->colors->reset, subval);
239 else {
240 asprintf (&tmp, "%s | %s%s%s = %s", res, ctx->colors->rname, bitfields[i]->name, ctx->colors->reset, subval);
241 free(res);
242 res = tmp;
243 }
244 free(subval);
245 }
246 if (value & ~mask) {
247 if (!res)
248 asprintf (&res, "%s%#"PRIx64"%s", ctx->colors->err, value & ~mask, ctx->colors->reset);
249 else {
250 asprintf (&tmp, "%s | %s%#"PRIx64"%s", res, ctx->colors->err, value & ~mask, ctx->colors->reset);
251 free(res);
252 res = tmp;
253 }
254 }
255 if (!res)
256 asprintf (&res, "%s0%s", ctx->colors->num, ctx->colors->reset);
257 asprintf (&tmp, "{ %s }", res);
258 free(res);
259 return tmp;
260 case RNN_TTYPE_SPECTYPE:
261 return rnndec_decodeval(ctx, &ti->spectype->typeinfo, value);
262 case RNN_TTYPE_HEX:
263 asprintf (&res, "%s%#"PRIx64"%s", ctx->colors->num, value, ctx->colors->reset);
264 break;
265 case RNN_TTYPE_FIXED:
266 if (value & UINT64_C(1) << (width-1)) {
267 asprintf (&res, "%s-%lf%s", ctx->colors->num,
268 ((double)((UINT64_C(1) << width) - value)) / ((double)(1 << ti->radix)),
269 ctx->colors->reset);
270 break;
271 }
272 /* fallthrough */
273 case RNN_TTYPE_UFIXED:
274 asprintf (&res, "%s%lf%s", ctx->colors->num,
275 ((double)value) / ((double)(1LL << ti->radix)),
276 ctx->colors->reset);
277 break;
278 case RNN_TTYPE_A3XX_REGID:
279 asprintf (&res, "%sr%"PRIu64".%c%s", ctx->colors->num, (value >> 2), "xyzw"[value & 0x3], ctx->colors->reset);
280 break;
281 case RNN_TTYPE_UINT:
282 asprintf (&res, "%s%"PRIu64"%s", ctx->colors->num, value, ctx->colors->reset);
283 break;
284 case RNN_TTYPE_INT:
285 if (value & UINT64_C(1) << (width-1))
286 asprintf (&res, "%s-%"PRIi64"%s", ctx->colors->num, (UINT64_C(1) << width) - value, ctx->colors->reset);
287 else
288 asprintf (&res, "%s%"PRIi64"%s", ctx->colors->num, value, ctx->colors->reset);
289 break;
290 case RNN_TTYPE_BOOLEAN:
291 if (value == 0) {
292 asprintf (&res, "%sFALSE%s", ctx->colors->eval, ctx->colors->reset);
293 } else if (value == 1) {
294 asprintf (&res, "%sTRUE%s", ctx->colors->eval, ctx->colors->reset);
295 }
296 break;
297 case RNN_TTYPE_FLOAT: {
298 union { uint64_t i; float f; double d; } val;
299 val.i = value;
300 if (width == 64)
301 asprintf(&res, "%s%f%s", ctx->colors->num,
302 val.d, ctx->colors->reset);
303 else if (width == 32)
304 asprintf(&res, "%s%f%s", ctx->colors->num,
305 val.f, ctx->colors->reset);
306 else if (width == 16)
307 asprintf(&res, "%s%f%s", ctx->colors->num,
308 float16(value), ctx->colors->reset);
309 else
310 goto failhex;
311
312 break;
313 }
314 failhex:
315 default:
316 asprintf (&res, "%s%#"PRIx64"%s", ctx->colors->num, value, ctx->colors->reset);
317 break;
318 }
319 if (value_orig & ~typeinfo_mask(ti)) {
320 asprintf (&tmp, "%s | %s%#"PRIx64"%s", res, ctx->colors->err, value_orig & ~typeinfo_mask(ti), ctx->colors->reset);
321 free(res);
322 res = tmp;
323 }
324 return res;
325 }
326
327 static char *appendidx (struct rnndeccontext *ctx, char *name, uint64_t idx, struct rnnenum *index) {
328 char *res, *index_name = NULL;
329
330 if (index)
331 index_name = rnndec_decode_enum_val(ctx, index->vals, index->valsnum, idx);
332
333 if (index_name)
334 asprintf (&res, "%s[%s%s%s]", name, ctx->colors->eval, index_name, ctx->colors->reset);
335 else
336 asprintf (&res, "%s[%s%#"PRIx64"%s]", name, ctx->colors->num, idx, ctx->colors->reset);
337
338 free (name);
339 return res;
340 }
341
342 /* This could probably be made to work for stripes too.. */
343 static int get_array_idx_offset(struct rnndelem *elem, uint64_t addr, uint64_t *idx, uint64_t *offset)
344 {
345 if (elem->offsets) {
346 int i;
347 for (i = 0; i < elem->offsetsnum; i++) {
348 uint64_t o = elem->offsets[i];
349 if ((o <= addr) && (addr < (o + elem->stride))) {
350 *idx = i;
351 *offset = addr - o;
352 return 0;
353 }
354 }
355 return -1;
356 } else {
357 if (addr < elem->offset)
358 return -1;
359
360 *idx = (addr - elem->offset) / elem->stride;
361 *offset = (addr - elem->offset) % elem->stride;
362
363 if (elem->length && (*idx >= elem->length))
364 return -1;
365
366 return 0;
367 }
368 }
369
370 static struct rnndecaddrinfo *trymatch (struct rnndeccontext *ctx, struct rnndelem **elems, int elemsnum, uint64_t addr, int write, int dwidth, uint64_t *indices, int indicesnum) {
371 struct rnndecaddrinfo *res;
372 int i, j;
373 for (i = 0; i < elemsnum; i++) {
374 if (!rnndec_varmatch(ctx, &elems[i]->varinfo))
375 continue;
376 uint64_t offset, idx;
377 char *tmp, *name;
378 switch (elems[i]->type) {
379 case RNN_ETYPE_REG:
380 if (addr < elems[i]->offset)
381 break;
382 if (elems[i]->stride) {
383 idx = (addr-elems[i]->offset)/elems[i]->stride;
384 offset = (addr-elems[i]->offset)%elems[i]->stride;
385 } else {
386 idx = 0;
387 offset = addr-elems[i]->offset;
388 }
389 if (offset >= elems[i]->width/dwidth)
390 break;
391 if (elems[i]->length && idx >= elems[i]->length)
392 break;
393 res = calloc (sizeof *res, 1);
394 res->typeinfo = &elems[i]->typeinfo;
395 res->width = elems[i]->width;
396 asprintf (&res->name, "%s%s%s", ctx->colors->rname, elems[i]->name, ctx->colors->reset);
397 for (j = 0; j < indicesnum; j++)
398 res->name = appendidx(ctx, res->name, indices[j], NULL);
399 if (elems[i]->length != 1)
400 res->name = appendidx(ctx, res->name, idx, elems[i]->index);
401 if (offset) {
402 asprintf (&tmp, "%s+%s%#"PRIx64"%s", res->name, ctx->colors->err, offset, ctx->colors->reset);
403 free(res->name);
404 res->name = tmp;
405 }
406 return res;
407 case RNN_ETYPE_STRIPE:
408 for (idx = 0; idx < elems[i]->length || !elems[i]->length; idx++) {
409 if (addr < elems[i]->offset + elems[i]->stride * idx)
410 break;
411 offset = addr - (elems[i]->offset + elems[i]->stride * idx);
412 int extraidx = (elems[i]->length != 1);
413 int nindnum = (elems[i]->name ? 0 : indicesnum + extraidx);
414 uint64_t nind[nindnum];
415 if (!elems[i]->name) {
416 for (j = 0; j < indicesnum; j++)
417 nind[j] = indices[j];
418 if (extraidx)
419 nind[indicesnum] = idx;
420 }
421 res = trymatch (ctx, elems[i]->subelems, elems[i]->subelemsnum, offset, write, dwidth, nind, nindnum);
422 if (!res)
423 continue;
424 if (!elems[i]->name)
425 return res;
426 asprintf (&name, "%s%s%s", ctx->colors->rname, elems[i]->name, ctx->colors->reset);
427 for (j = 0; j < indicesnum; j++)
428 name = appendidx(ctx, name, indices[j], NULL);
429 if (elems[i]->length != 1)
430 name = appendidx(ctx, name, idx, elems[i]->index);
431 asprintf (&tmp, "%s.%s", name, res->name);
432 free(name);
433 free(res->name);
434 res->name = tmp;
435 return res;
436 }
437 break;
438 case RNN_ETYPE_ARRAY:
439 if (get_array_idx_offset(elems[i], addr, &idx, &offset))
440 break;
441 asprintf (&name, "%s%s%s", ctx->colors->rname, elems[i]->name, ctx->colors->reset);
442 for (j = 0; j < indicesnum; j++)
443 name = appendidx(ctx, name, indices[j], NULL);
444 if (elems[i]->length != 1)
445 name = appendidx(ctx, name, idx, elems[i]->index);
446 if ((res = trymatch (ctx, elems[i]->subelems, elems[i]->subelemsnum, offset, write, dwidth, 0, 0))) {
447 asprintf (&tmp, "%s.%s", name, res->name);
448 free(name);
449 free(res->name);
450 res->name = tmp;
451 return res;
452 }
453 res = calloc (sizeof *res, 1);
454 asprintf (&tmp, "%s+%s%#"PRIx64"%s", name, ctx->colors->err, offset, ctx->colors->reset);
455 free(name);
456 res->name = tmp;
457 return res;
458 default:
459 break;
460 }
461 }
462 return 0;
463 }
464
465 int rnndec_checkaddr(struct rnndeccontext *ctx, struct rnndomain *domain, uint64_t addr, int write) {
466 struct rnndecaddrinfo *res = trymatch(ctx, domain->subelems, domain->subelemsnum, addr, write, domain->width, 0, 0);
467 if (res) {
468 free(res->name);
469 free(res);
470 }
471 return res != NULL;
472 }
473
474 struct rnndecaddrinfo *rnndec_decodeaddr(struct rnndeccontext *ctx, struct rnndomain *domain, uint64_t addr, int write) {
475 struct rnndecaddrinfo *res = trymatch(ctx, domain->subelems, domain->subelemsnum, addr, write, domain->width, 0, 0);
476 if (res)
477 return res;
478 res = calloc (sizeof *res, 1);
479 asprintf (&res->name, "%s%#"PRIx64"%s", ctx->colors->err, addr, ctx->colors->reset);
480 return res;
481 }
482
483 static uint64_t tryreg(struct rnndeccontext *ctx, struct rnndelem **elems, int elemsnum,
484 int dwidth, const char *name)
485 {
486 int i;
487 const char *suffix = strchr(name, '[');
488 unsigned n = suffix ? (suffix - name) : strlen(name);
489
490 const char *child = NULL;
491 unsigned idx = 0;
492
493 if (suffix) {
494 const char *tmp = strchr(suffix, ']');
495 idx = strtol(suffix+1, NULL, 0);
496 child = tmp+2;
497 }
498
499 for (i = 0; i < elemsnum; i++) {
500 struct rnndelem *elem = elems[i];
501 if (!rnndec_varmatch(ctx, &elem->varinfo))
502 continue;
503 int match = (strlen(elem->name) == n) && !strncmp(elem->name, name, n);
504 switch (elem->type) {
505 case RNN_ETYPE_REG:
506 if (match) {
507 assert(!suffix);
508 return elem->offset;
509 }
510 break;
511 case RNN_ETYPE_STRIPE:
512 assert(0); // TODO
513 break;
514 case RNN_ETYPE_ARRAY:
515 if (match) {
516 assert(suffix);
517 return elem->offset + (idx * elem->stride) +
518 tryreg(ctx, elem->subelems, elem->subelemsnum, dwidth, child);
519 }
520 break;
521 default:
522 break;
523 }
524 }
525 return 0;
526 }
527
528 uint64_t rnndec_decodereg(struct rnndeccontext *ctx, struct rnndomain *domain, const char *name)
529 {
530 return tryreg(ctx, domain->subelems, domain->subelemsnum, domain->width, name);
531 }