From 8074a040e7a99a66cf7149d470f3a0f8eb6f3fd4 Mon Sep 17 00:00:00 2001 From: Italo Nicola Date: Mon, 19 Apr 2021 09:43:30 +0000 Subject: [PATCH] util: add util_sign_extend This code is taken from src/freedreno/isa/decode.c. Since we need a similar function in panfrost, it's probably good to move it to utils. Signed-off-by: Italo Nicola Acked-by: Rob Clark Part-of: --- src/freedreno/isa/decode.c | 17 +++-------------- src/util/u_math.h | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/freedreno/isa/decode.c b/src/freedreno/isa/decode.c index 57fafe00680..c8c8aa9874e 100644 --- a/src/freedreno/isa/decode.c +++ b/src/freedreno/isa/decode.c @@ -479,17 +479,6 @@ isa_decode_field(struct decode_scope *scope, const char *field_name) return val; } -static int64_t -sign_extend(uint64_t val, unsigned width) -{ - assert(width > 0); - if (val & (UINT64_C(1) << (width - 1))) { - return -(int64_t)((UINT64_C(1) << width) - val); - } else { - return val; - } -} - static void display_field(struct decode_scope *scope, const char *field_name) { @@ -528,7 +517,7 @@ display_field(struct decode_scope *scope, const char *field_name) /* Basic types: */ case TYPE_BRANCH: if (scope->state->options->branch_labels) { - int offset = sign_extend(val, width) + scope->state->n; + int offset = util_sign_extend(val, width) + scope->state->n; if (offset < scope->state->num_instr) { fprintf(out, "l%d", offset); BITSET_SET(scope->state->branch_targets, offset); @@ -537,7 +526,7 @@ display_field(struct decode_scope *scope, const char *field_name) } FALLTHROUGH; case TYPE_INT: - fprintf(out, "%"PRId64, sign_extend(val, width)); + fprintf(out, "%"PRId64, util_sign_extend(val, width)); break; case TYPE_UINT: fprintf(out, "%"PRIu64, val); @@ -548,7 +537,7 @@ display_field(struct decode_scope *scope, const char *field_name) break; case TYPE_OFFSET: if (val != 0) { - fprintf(out, "%+"PRId64, sign_extend(val, width)); + fprintf(out, "%+"PRId64, util_sign_extend(val, width)); } break; case TYPE_FLOAT: diff --git a/src/util/u_math.h b/src/util/u_math.h index 2b12267b961..567c51d7aec 100644 --- a/src/util/u_math.h +++ b/src/util/u_math.h @@ -642,6 +642,20 @@ util_bswap16(uint16_t n) (n << 8); } +/** + * Extend sign. + */ +static inline int64_t +util_sign_extend(uint64_t val, unsigned width) +{ + assert(width > 0); + if (val & (UINT64_C(1) << (width - 1))) { + return -(int64_t)((UINT64_C(1) << width) - val); + } else { + return val; + } +} + static inline void* util_memcpy_cpu_to_le32(void * restrict dest, const void * restrict src, size_t n) {