Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/freedreno/decode/util.h
4565 views
1
/*
2
* Copyright (c) 2012-2018 Rob Clark <[email protected]>
3
*
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
10
*
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
13
* Software.
14
*
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
* SOFTWARE.
22
*/
23
24
#ifndef __UTIL_H__
25
#define __UTIL_H__
26
27
#include <ctype.h>
28
#include <stdint.h>
29
#include <stdio.h>
30
31
/* old-style program binary XOR'd ascii w/ 0xff */
32
#ifndef ASCII_XOR
33
#define ASCII_XOR 0
34
#endif
35
36
static inline const char *
37
tab(int lvl)
38
{
39
const char *TAB = "\t\t\t\t\t\t\t\t\0";
40
return &TAB[strlen(TAB) - lvl];
41
}
42
43
/* convert float to dword */
44
static inline float
45
d2f(uint32_t d)
46
{
47
union {
48
float f;
49
uint32_t d;
50
} u = {
51
.d = d,
52
};
53
return u.f;
54
}
55
56
static inline void
57
dump_hex(const void *buf, int sz)
58
{
59
uint8_t *ptr = (uint8_t *)buf;
60
uint8_t *end = ptr + sz;
61
int i = 0;
62
63
while (ptr < end) {
64
uint32_t d = 0;
65
66
printf((i % 8) ? " " : "\t");
67
68
d |= *(ptr++) << 0;
69
d |= *(ptr++) << 8;
70
d |= *(ptr++) << 16;
71
d |= *(ptr++) << 24;
72
73
printf("%08x", d);
74
75
if ((i % 8) == 7) {
76
printf("\n");
77
}
78
79
i++;
80
}
81
82
if (i % 8) {
83
printf("\n");
84
}
85
}
86
87
static inline void
88
dump_float(const void *buf, int sz)
89
{
90
uint8_t *ptr = (uint8_t *)buf;
91
uint8_t *end = ptr + sz - 3;
92
int i = 0;
93
94
while (ptr < end) {
95
uint32_t d = 0;
96
97
printf((i % 8) ? " " : "\t");
98
99
d |= *(ptr++) << 0;
100
d |= *(ptr++) << 8;
101
d |= *(ptr++) << 16;
102
d |= *(ptr++) << 24;
103
104
printf("%8f", d2f(d));
105
106
if ((i % 8) == 7) {
107
printf("\n");
108
}
109
110
i++;
111
}
112
113
if (i % 8) {
114
printf("\n");
115
}
116
}
117
118
#define is_ok_ascii(c) (isascii(c) && ((c == '\t') || !iscntrl(c)))
119
120
static inline void
121
clean_ascii(char *buf, int sz)
122
{
123
uint8_t *ptr = (uint8_t *)buf;
124
uint8_t *end = ptr + sz;
125
while (ptr < end) {
126
*(ptr++) ^= ASCII_XOR;
127
}
128
}
129
130
static inline void
131
dump_ascii(const void *buf, int sz)
132
{
133
uint8_t *ptr = (uint8_t *)buf;
134
uint8_t *end = ptr + sz;
135
printf("\t");
136
while (ptr < end) {
137
uint8_t c = *(ptr++) ^ ASCII_XOR;
138
if (c == '\n') {
139
printf("\n\t");
140
} else if (c == '\0') {
141
printf("\n\t-----------------------------------\n\t");
142
} else if (is_ok_ascii(c)) {
143
printf("%c", c);
144
} else {
145
printf("?");
146
}
147
}
148
printf("\n");
149
}
150
151
static inline void
152
dump_hex_ascii(const void *buf, int sz, int level)
153
{
154
uint8_t *ptr = (uint8_t *)buf;
155
uint8_t *end = ptr + sz;
156
uint8_t *ascii = ptr;
157
int i = 0;
158
159
printf("%s-----------------------------------------------\n", tab(level));
160
printf("%s%d (0x%x) bytes\n", tab(level), sz, sz);
161
162
while (ptr < end) {
163
uint32_t d = 0;
164
165
if (i % 4) {
166
printf(" ");
167
} else {
168
printf("%s%06x: ", tab(level), (uint32_t)(ptr - (uint8_t *)buf));
169
}
170
171
d |= *(ptr++) << 0;
172
d |= *(ptr++) << 8;
173
d |= *(ptr++) << 16;
174
d |= *(ptr++) << 24;
175
176
printf("%08x", d);
177
178
if ((i % 4) == 3) {
179
int j;
180
printf("\t|");
181
for (j = 0; j < 16; j++) {
182
uint8_t c = *(ascii++);
183
c ^= ASCII_XOR;
184
printf("%c", (isascii(c) && !iscntrl(c)) ? c : '.');
185
}
186
printf("|\n");
187
}
188
189
i++;
190
}
191
192
if (i % 4) {
193
for (int j = 4 - (i % 4); j > 0; j--) {
194
printf(" ");
195
}
196
printf("\t|");
197
while (ascii < end) {
198
uint8_t c = *(ascii++);
199
c ^= ASCII_XOR;
200
printf("%c", (isascii(c) && !iscntrl(c)) ? c : '.');
201
}
202
printf("|\n");
203
}
204
}
205
206
#endif /* __UTIL_H__ */
207
208