summaryrefslogtreecommitdiffstats
path: root/include/qemu/host-utils.h
diff options
context:
space:
mode:
authorLuis Pires2021-09-10 13:26:05 +0200
committerDavid Gibson2021-09-29 11:37:38 +0200
commitd03bba0bfbe6dcacc9d7be2b664d70fde081cc47 (patch)
tree341ef53a137c982ea064ed969b7287569190a7d8 /include/qemu/host-utils.h
parenthost-utils: fix missing zero-extension in divs128 (diff)
downloadqemu-d03bba0bfbe6dcacc9d7be2b664d70fde081cc47.tar.gz
qemu-d03bba0bfbe6dcacc9d7be2b664d70fde081cc47.tar.xz
qemu-d03bba0bfbe6dcacc9d7be2b664d70fde081cc47.zip
host-utils: introduce uabs64()
Introduce uabs64(), a function that returns the absolute value of a 64-bit int as an unsigned value. This avoids the undefined behavior for common abs implementations, where abs of the most negative value is undefined. Signed-off-by: Luis Pires <luis.pires@eldorado.org.br> Reviewed-by: Richard Henderson <richard.henderson@linaro.org> Reviewed-by: Eduardo Habkost <ehabkost@redhat.com> Message-Id: <20210910112624.72748-4-luis.pires@eldorado.org.br> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'include/qemu/host-utils.h')
-rw-r--r--include/qemu/host-utils.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index 753b9fb89f..ca9f3f021b 100644
--- a/include/qemu/host-utils.h
+++ b/include/qemu/host-utils.h
@@ -358,6 +358,14 @@ static inline uint64_t revbit64(uint64_t x)
}
/**
+ * Return the absolute value of a 64-bit integer as an unsigned 64-bit value
+ */
+static inline uint64_t uabs64(int64_t v)
+{
+ return v < 0 ? -v : v;
+}
+
+/**
* sadd32_overflow - addition with overflow indication
* @x, @y: addends
* @ret: Output for sum