Path: blob/21.2-virgl/src/freedreno/decode/util.h
4565 views
/*1* Copyright (c) 2012-2018 Rob Clark <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,19* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE20* SOFTWARE.21*/2223#ifndef __UTIL_H__24#define __UTIL_H__2526#include <ctype.h>27#include <stdint.h>28#include <stdio.h>2930/* old-style program binary XOR'd ascii w/ 0xff */31#ifndef ASCII_XOR32#define ASCII_XOR 033#endif3435static inline const char *36tab(int lvl)37{38const char *TAB = "\t\t\t\t\t\t\t\t\0";39return &TAB[strlen(TAB) - lvl];40}4142/* convert float to dword */43static inline float44d2f(uint32_t d)45{46union {47float f;48uint32_t d;49} u = {50.d = d,51};52return u.f;53}5455static inline void56dump_hex(const void *buf, int sz)57{58uint8_t *ptr = (uint8_t *)buf;59uint8_t *end = ptr + sz;60int i = 0;6162while (ptr < end) {63uint32_t d = 0;6465printf((i % 8) ? " " : "\t");6667d |= *(ptr++) << 0;68d |= *(ptr++) << 8;69d |= *(ptr++) << 16;70d |= *(ptr++) << 24;7172printf("%08x", d);7374if ((i % 8) == 7) {75printf("\n");76}7778i++;79}8081if (i % 8) {82printf("\n");83}84}8586static inline void87dump_float(const void *buf, int sz)88{89uint8_t *ptr = (uint8_t *)buf;90uint8_t *end = ptr + sz - 3;91int i = 0;9293while (ptr < end) {94uint32_t d = 0;9596printf((i % 8) ? " " : "\t");9798d |= *(ptr++) << 0;99d |= *(ptr++) << 8;100d |= *(ptr++) << 16;101d |= *(ptr++) << 24;102103printf("%8f", d2f(d));104105if ((i % 8) == 7) {106printf("\n");107}108109i++;110}111112if (i % 8) {113printf("\n");114}115}116117#define is_ok_ascii(c) (isascii(c) && ((c == '\t') || !iscntrl(c)))118119static inline void120clean_ascii(char *buf, int sz)121{122uint8_t *ptr = (uint8_t *)buf;123uint8_t *end = ptr + sz;124while (ptr < end) {125*(ptr++) ^= ASCII_XOR;126}127}128129static inline void130dump_ascii(const void *buf, int sz)131{132uint8_t *ptr = (uint8_t *)buf;133uint8_t *end = ptr + sz;134printf("\t");135while (ptr < end) {136uint8_t c = *(ptr++) ^ ASCII_XOR;137if (c == '\n') {138printf("\n\t");139} else if (c == '\0') {140printf("\n\t-----------------------------------\n\t");141} else if (is_ok_ascii(c)) {142printf("%c", c);143} else {144printf("?");145}146}147printf("\n");148}149150static inline void151dump_hex_ascii(const void *buf, int sz, int level)152{153uint8_t *ptr = (uint8_t *)buf;154uint8_t *end = ptr + sz;155uint8_t *ascii = ptr;156int i = 0;157158printf("%s-----------------------------------------------\n", tab(level));159printf("%s%d (0x%x) bytes\n", tab(level), sz, sz);160161while (ptr < end) {162uint32_t d = 0;163164if (i % 4) {165printf(" ");166} else {167printf("%s%06x: ", tab(level), (uint32_t)(ptr - (uint8_t *)buf));168}169170d |= *(ptr++) << 0;171d |= *(ptr++) << 8;172d |= *(ptr++) << 16;173d |= *(ptr++) << 24;174175printf("%08x", d);176177if ((i % 4) == 3) {178int j;179printf("\t|");180for (j = 0; j < 16; j++) {181uint8_t c = *(ascii++);182c ^= ASCII_XOR;183printf("%c", (isascii(c) && !iscntrl(c)) ? c : '.');184}185printf("|\n");186}187188i++;189}190191if (i % 4) {192for (int j = 4 - (i % 4); j > 0; j--) {193printf(" ");194}195printf("\t|");196while (ascii < end) {197uint8_t c = *(ascii++);198c ^= ASCII_XOR;199printf("%c", (isascii(c) && !iscntrl(c)) ? c : '.');200}201printf("|\n");202}203}204205#endif /* __UTIL_H__ */206207208