I'm becoming crazy with the following code and the GCC compiler (v11.4 to x86_64 on Linux):
static void disasm_TESTSX_X(char *__buffer, test_inst_t *inst) {
__buffer += sprintf(__buffer, "testsx %lx", ((uint32_t)(((int32_t)(TEST_TESTSX_X_x_i)))));
}
static void disasm_TESTW_X(char *__buffer, test_inst_t *inst) {
__buffer += sprintf(__buffer, "testw %lx", (((uint32_t)(TEST_TESTW_X_x_i)) + 1L));
}
First form gives me a warning at compilation time (I should use %x) while the second does not. TEST_TESTW_X_x_i is of type uint8_t. Yet, both expressions are explicitly converted to uint32_t (code is not very readable as it is generated code).
Thanks to anyone that could help me.
Neither is correct for converting a
uint32_tvalue. To convert auint32_tto hexadecimal, change"testsx %lx"to"testsx %" PRIx32and insert#include <inttypes.h>.uint32_tmay beunsignedin some C implementations,unsigned longin others, and some other type in yet others. So neither%xnor%lxwill be correct in every C implementation.PRIx32, defined in<inttypes.h>, will be replaced by a string literal with a correct conversion specifier foruint32_tin the C implementation being used.No, they are not. The argument in the second case is
(((uint32_t)(TEST_TESTW_X_x_i)) + 1L), which is the sum of auint32_texpression and1L. In a C implementation wherelongis wider thanuint32_t, the left operand of+will be converted tolong, and the type of the result will belong.