Path: blob/21.2-virgl/src/panfrost/midgard/midgard_print_constant.c
4564 views
/*1* Copyright (C) 2018-2019 Alyssa Rosenzweig <[email protected]>2* Copyright (C) 2019-2020 Collabora, Ltd.3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* 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 THE21* SOFTWARE.22*/2324#include <math.h>25#include <inttypes.h>26#include "util/half_float.h"27#include "midgard.h"28#include "helpers.h"29#include "midgard_ops.h"3031void32mir_print_constant_component(FILE *fp, const midgard_constants *consts, unsigned c,33midgard_reg_mode reg_mode, bool half,34unsigned mod, midgard_alu_op op)35{36bool is_sint = false, is_uint = false, is_hex = false;37const char *opname = alu_opcode_props[op].name;3839bool is_int = midgard_is_integer_op(op);4041/* Add a sentinel name to prevent crashing */42if (!opname)43opname = "unknown";4445if (is_int) {46is_uint = midgard_is_unsigned_op(op);4748if (!is_uint) {49/* Bit ops are easier to follow when the constant is printed in50* hexadecimal. Other operations starting with a 'i' are51* considered to operate on signed integers. That might not52* be true for all of them, but it's good enough for traces.53*/54if (op >= midgard_alu_op_iand &&55op <= midgard_alu_op_ipopcnt)56is_hex = true;57else58is_sint = true;59}60}6162if (half)63reg_mode--;6465switch (reg_mode) {66case midgard_reg_mode_64:67if (is_sint) {68fprintf(fp, "%"PRIi64, consts->i64[c]);69} else if (is_uint) {70fprintf(fp, "%"PRIu64, consts->u64[c]);71} else if (is_hex) {72fprintf(fp, "0x%"PRIX64, consts->u64[c]);73} else {74double v = consts->f64[c];7576if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabs(v);77if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v;7879printf("%g", v);80}81break;8283case midgard_reg_mode_32:84if (is_sint) {85int64_t v;8687if (half && mod == midgard_int_zero_extend)88v = consts->u32[c];89else if (half && mod == midgard_int_left_shift)90v = (uint64_t)consts->u32[c] << 32;91else92v = consts->i32[c];9394fprintf(fp, "%"PRIi64, v);95} else if (is_uint || is_hex) {96uint64_t v;9798if (half && mod == midgard_int_left_shift)99v = (uint64_t)consts->u32[c] << 32;100else101v = consts->u32[c];102103fprintf(fp, is_uint ? "%"PRIu64 : "0x%"PRIX64, v);104} else {105float v = consts->f32[c];106107if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabsf(v);108if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v;109110fprintf(fp, "%g", v);111}112break;113114case midgard_reg_mode_16:115if (is_sint) {116int32_t v;117118if (half && mod == midgard_int_zero_extend)119v = consts->u16[c];120else if (half && mod == midgard_int_left_shift)121v = (uint32_t)consts->u16[c] << 16;122else123v = consts->i16[c];124125fprintf(fp, "%d", v);126} else if (is_uint || is_hex) {127uint32_t v;128129if (half && mod == midgard_int_left_shift)130v = (uint32_t)consts->u16[c] << 16;131else132v = consts->u16[c];133134fprintf(fp, is_uint ? "%u" : "0x%X", v);135} else {136float v = _mesa_half_to_float(consts->f16[c]);137138if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabsf(v);139if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v;140141fprintf(fp, "%g", v);142}143break;144145case midgard_reg_mode_8:146fprintf(fp, "0x%X", consts->u8[c]);147148if (mod)149fprintf(fp, " /* %u */", mod);150151assert(!half); /* No 4-bit */152153break;154}155}156157158159160