1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io> 3bf215546Sopenharmony_ci * Copyright (C) 2019-2020 Collabora, Ltd. 4bf215546Sopenharmony_ci * 5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 11bf215546Sopenharmony_ci * 12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 14bf215546Sopenharmony_ci * Software. 15bf215546Sopenharmony_ci * 16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22bf215546Sopenharmony_ci * SOFTWARE. 23bf215546Sopenharmony_ci */ 24bf215546Sopenharmony_ci 25bf215546Sopenharmony_ci#include <math.h> 26bf215546Sopenharmony_ci#include <inttypes.h> 27bf215546Sopenharmony_ci#include "util/half_float.h" 28bf215546Sopenharmony_ci#include "midgard.h" 29bf215546Sopenharmony_ci#include "helpers.h" 30bf215546Sopenharmony_ci#include "midgard_ops.h" 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_civoid 33bf215546Sopenharmony_cimir_print_constant_component(FILE *fp, const midgard_constants *consts, unsigned c, 34bf215546Sopenharmony_ci midgard_reg_mode reg_mode, bool half, 35bf215546Sopenharmony_ci unsigned mod, midgard_alu_op op) 36bf215546Sopenharmony_ci{ 37bf215546Sopenharmony_ci bool is_sint = false, is_uint = false, is_hex = false; 38bf215546Sopenharmony_ci const char *opname = alu_opcode_props[op].name; 39bf215546Sopenharmony_ci 40bf215546Sopenharmony_ci bool is_int = midgard_is_integer_op(op); 41bf215546Sopenharmony_ci 42bf215546Sopenharmony_ci /* Add a sentinel name to prevent crashing */ 43bf215546Sopenharmony_ci if (!opname) 44bf215546Sopenharmony_ci opname = "unknown"; 45bf215546Sopenharmony_ci 46bf215546Sopenharmony_ci if (is_int) { 47bf215546Sopenharmony_ci is_uint = midgard_is_unsigned_op(op); 48bf215546Sopenharmony_ci 49bf215546Sopenharmony_ci if (!is_uint) { 50bf215546Sopenharmony_ci /* Bit ops are easier to follow when the constant is printed in 51bf215546Sopenharmony_ci * hexadecimal. Other operations starting with a 'i' are 52bf215546Sopenharmony_ci * considered to operate on signed integers. That might not 53bf215546Sopenharmony_ci * be true for all of them, but it's good enough for traces. 54bf215546Sopenharmony_ci */ 55bf215546Sopenharmony_ci if (op >= midgard_alu_op_iand && 56bf215546Sopenharmony_ci op <= midgard_alu_op_ipopcnt) 57bf215546Sopenharmony_ci is_hex = true; 58bf215546Sopenharmony_ci else 59bf215546Sopenharmony_ci is_sint = true; 60bf215546Sopenharmony_ci } 61bf215546Sopenharmony_ci } 62bf215546Sopenharmony_ci 63bf215546Sopenharmony_ci if (half) 64bf215546Sopenharmony_ci reg_mode--; 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci switch (reg_mode) { 67bf215546Sopenharmony_ci case midgard_reg_mode_64: 68bf215546Sopenharmony_ci if (is_sint) { 69bf215546Sopenharmony_ci fprintf(fp, "%"PRIi64, consts->i64[c]); 70bf215546Sopenharmony_ci } else if (is_uint) { 71bf215546Sopenharmony_ci fprintf(fp, "%"PRIu64, consts->u64[c]); 72bf215546Sopenharmony_ci } else if (is_hex) { 73bf215546Sopenharmony_ci fprintf(fp, "0x%"PRIX64, consts->u64[c]); 74bf215546Sopenharmony_ci } else { 75bf215546Sopenharmony_ci double v = consts->f64[c]; 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_ci if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabs(v); 78bf215546Sopenharmony_ci if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v; 79bf215546Sopenharmony_ci 80bf215546Sopenharmony_ci printf("%g", v); 81bf215546Sopenharmony_ci } 82bf215546Sopenharmony_ci break; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci case midgard_reg_mode_32: 85bf215546Sopenharmony_ci if (is_sint) { 86bf215546Sopenharmony_ci int64_t v; 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci if (half && mod == midgard_int_zero_extend) 89bf215546Sopenharmony_ci v = consts->u32[c]; 90bf215546Sopenharmony_ci else if (half && mod == midgard_int_left_shift) 91bf215546Sopenharmony_ci v = (uint64_t)consts->u32[c] << 32; 92bf215546Sopenharmony_ci else 93bf215546Sopenharmony_ci v = consts->i32[c]; 94bf215546Sopenharmony_ci 95bf215546Sopenharmony_ci fprintf(fp, "%"PRIi64, v); 96bf215546Sopenharmony_ci } else if (is_uint || is_hex) { 97bf215546Sopenharmony_ci uint64_t v; 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci if (half && mod == midgard_int_left_shift) 100bf215546Sopenharmony_ci v = (uint64_t)consts->u32[c] << 32; 101bf215546Sopenharmony_ci else 102bf215546Sopenharmony_ci v = consts->u32[c]; 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci fprintf(fp, is_uint ? "%"PRIu64 : "0x%"PRIX64, v); 105bf215546Sopenharmony_ci } else { 106bf215546Sopenharmony_ci float v = consts->f32[c]; 107bf215546Sopenharmony_ci 108bf215546Sopenharmony_ci if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabsf(v); 109bf215546Sopenharmony_ci if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v; 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci fprintf(fp, "%g", v); 112bf215546Sopenharmony_ci } 113bf215546Sopenharmony_ci break; 114bf215546Sopenharmony_ci 115bf215546Sopenharmony_ci case midgard_reg_mode_16: 116bf215546Sopenharmony_ci if (is_sint) { 117bf215546Sopenharmony_ci int32_t v; 118bf215546Sopenharmony_ci 119bf215546Sopenharmony_ci if (half && mod == midgard_int_zero_extend) 120bf215546Sopenharmony_ci v = consts->u16[c]; 121bf215546Sopenharmony_ci else if (half && mod == midgard_int_left_shift) 122bf215546Sopenharmony_ci v = (uint32_t)consts->u16[c] << 16; 123bf215546Sopenharmony_ci else 124bf215546Sopenharmony_ci v = consts->i16[c]; 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci fprintf(fp, "%d", v); 127bf215546Sopenharmony_ci } else if (is_uint || is_hex) { 128bf215546Sopenharmony_ci uint32_t v; 129bf215546Sopenharmony_ci 130bf215546Sopenharmony_ci if (half && mod == midgard_int_left_shift) 131bf215546Sopenharmony_ci v = (uint32_t)consts->u16[c] << 16; 132bf215546Sopenharmony_ci else 133bf215546Sopenharmony_ci v = consts->u16[c]; 134bf215546Sopenharmony_ci 135bf215546Sopenharmony_ci fprintf(fp, is_uint ? "%u" : "0x%X", v); 136bf215546Sopenharmony_ci } else { 137bf215546Sopenharmony_ci float v = _mesa_half_to_float(consts->f16[c]); 138bf215546Sopenharmony_ci 139bf215546Sopenharmony_ci if (mod & MIDGARD_FLOAT_MOD_ABS) v = fabsf(v); 140bf215546Sopenharmony_ci if (mod & MIDGARD_FLOAT_MOD_NEG) v = -v; 141bf215546Sopenharmony_ci 142bf215546Sopenharmony_ci fprintf(fp, "%g", v); 143bf215546Sopenharmony_ci } 144bf215546Sopenharmony_ci break; 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci case midgard_reg_mode_8: 147bf215546Sopenharmony_ci fprintf(fp, "0x%X", consts->u8[c]); 148bf215546Sopenharmony_ci 149bf215546Sopenharmony_ci if (mod) 150bf215546Sopenharmony_ci fprintf(fp, " /* %u */", mod); 151bf215546Sopenharmony_ci 152bf215546Sopenharmony_ci assert(!half); /* No 4-bit */ 153bf215546Sopenharmony_ci 154bf215546Sopenharmony_ci break; 155bf215546Sopenharmony_ci } 156bf215546Sopenharmony_ci} 157bf215546Sopenharmony_ci 158bf215546Sopenharmony_cistatic char *outmod_names_float[4] = { 159bf215546Sopenharmony_ci "", 160bf215546Sopenharmony_ci ".clamp_0_inf", 161bf215546Sopenharmony_ci ".clamp_m1_1", 162bf215546Sopenharmony_ci ".clamp_0_1" 163bf215546Sopenharmony_ci}; 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_cistatic char *outmod_names_int[4] = { 166bf215546Sopenharmony_ci ".ssat", 167bf215546Sopenharmony_ci ".usat", 168bf215546Sopenharmony_ci ".keeplo", 169bf215546Sopenharmony_ci ".keephi" 170bf215546Sopenharmony_ci}; 171bf215546Sopenharmony_ci 172bf215546Sopenharmony_civoid 173bf215546Sopenharmony_cimir_print_outmod(FILE *fp, unsigned outmod, bool is_int) 174bf215546Sopenharmony_ci{ 175bf215546Sopenharmony_ci fprintf(fp, "%s", is_int ? outmod_names_int[outmod] : 176bf215546Sopenharmony_ci outmod_names_float[outmod]); 177bf215546Sopenharmony_ci} 178