xref: /aosp_15_r20/external/mesa3d/src/freedreno/decode/util.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2012-2018 Rob Clark <[email protected]>
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #ifndef __UTIL_H__
7 #define __UTIL_H__
8 
9 #include <ctype.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 
13 #include "util/u_math.h"
14 
15 /* old-style program binary XOR'd ascii w/ 0xff */
16 #ifndef ASCII_XOR
17 #define ASCII_XOR 0
18 #endif
19 
20 static inline const char *
tab(int lvl)21 tab(int lvl)
22 {
23    const char *TAB = "\t\t\t\t\t\t\t\t\0";
24    return &TAB[strlen(TAB) - lvl];
25 }
26 
27 static inline void
dump_hex(const void * buf,int sz)28 dump_hex(const void *buf, int sz)
29 {
30    uint8_t *ptr = (uint8_t *)buf;
31    uint8_t *end = ptr + sz;
32    int i = 0;
33 
34    while (ptr < end) {
35       uint32_t d = 0;
36 
37       printf((i % 8) ? " " : "\t");
38 
39       d |= *(ptr++) << 0;
40       d |= *(ptr++) << 8;
41       d |= *(ptr++) << 16;
42       d |= (uint32_t)*(ptr++) << 24;
43 
44       printf("%08x", d);
45 
46       if ((i % 8) == 7) {
47          printf("\n");
48       }
49 
50       i++;
51    }
52 
53    if (i % 8) {
54       printf("\n");
55    }
56 }
57 
58 static inline void
dump_float(const void * buf,int sz)59 dump_float(const void *buf, int sz)
60 {
61    uint8_t *ptr = (uint8_t *)buf;
62    uint8_t *end = ptr + sz - 3;
63    int i = 0;
64 
65    while (ptr < end) {
66       uint32_t d = 0;
67 
68       printf((i % 8) ? " " : "\t");
69 
70       d |= *(ptr++) << 0;
71       d |= *(ptr++) << 8;
72       d |= *(ptr++) << 16;
73       d |= (uint32_t)*(ptr++) << 24;
74 
75       printf("%8f", uif(d));
76 
77       if ((i % 8) == 7) {
78          printf("\n");
79       }
80 
81       i++;
82    }
83 
84    if (i % 8) {
85       printf("\n");
86    }
87 }
88 
89 #define is_ok_ascii(c) (isascii(c) && ((c == '\t') || !iscntrl(c)))
90 
91 static inline void
clean_ascii(char * buf,int sz)92 clean_ascii(char *buf, int sz)
93 {
94    uint8_t *ptr = (uint8_t *)buf;
95    uint8_t *end = ptr + sz;
96    while (ptr < end) {
97       *(ptr++) ^= ASCII_XOR;
98    }
99 }
100 
101 static inline void
dump_ascii(const void * buf,int sz)102 dump_ascii(const void *buf, int sz)
103 {
104    uint8_t *ptr = (uint8_t *)buf;
105    uint8_t *end = ptr + sz;
106    printf("\t");
107    while (ptr < end) {
108       uint8_t c = *(ptr++) ^ ASCII_XOR;
109       if (c == '\n') {
110          printf("\n\t");
111       } else if (c == '\0') {
112          printf("\n\t-----------------------------------\n\t");
113       } else if (is_ok_ascii(c)) {
114          printf("%c", c);
115       } else {
116          printf("?");
117       }
118    }
119    printf("\n");
120 }
121 
122 static inline void
dump_hex_ascii(const void * buf,int sz,int level)123 dump_hex_ascii(const void *buf, int sz, int level)
124 {
125    uint8_t *ptr = (uint8_t *)buf;
126    uint8_t *end = ptr + sz;
127    uint8_t *ascii = ptr;
128    int i = 0;
129 
130    printf("%s-----------------------------------------------\n", tab(level));
131    printf("%s%d (0x%x) bytes\n", tab(level), sz, sz);
132 
133    while (ptr < end) {
134       uint32_t d = 0;
135 
136       if (i % 4) {
137          printf(" ");
138       } else {
139          printf("%s%06x: ", tab(level), (uint32_t)(ptr - (uint8_t *)buf));
140       }
141 
142       d |= *(ptr++) << 0;
143       d |= *(ptr++) << 8;
144       d |= *(ptr++) << 16;
145       d |= (uint32_t)*(ptr++) << 24;
146 
147       printf("%08x", d);
148 
149       if ((i % 4) == 3) {
150          int j;
151          printf("\t|");
152          for (j = 0; j < 16; j++) {
153             uint8_t c = *(ascii++);
154             c ^= ASCII_XOR;
155             printf("%c", (isascii(c) && !iscntrl(c)) ? c : '.');
156          }
157          printf("|\n");
158       }
159 
160       i++;
161    }
162 
163    if (i % 4) {
164       for (int j = 4 - (i % 4); j > 0; j--) {
165          printf("         ");
166       }
167       printf("\t|");
168       while (ascii < end) {
169          uint8_t c = *(ascii++);
170          c ^= ASCII_XOR;
171          printf("%c", (isascii(c) && !iscntrl(c)) ? c : '.');
172       }
173       printf("|\n");
174    }
175 }
176 
177 #endif /* __UTIL_H__ */
178