[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 03/46] qemu/host-utils: Add wrappers for carry builtins
From: |
Richard Henderson |
Subject: |
[PULL 03/46] qemu/host-utils: Add wrappers for carry builtins |
Date: |
Sun, 16 May 2021 07:33:48 -0500 |
These builtins came in clang 3.8, but are not present in gcc through
version 11. Even in clang the optimization is only ideal on x86_64,
but never worse than the hand-coding that we currently do.
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
include/qemu/host-utils.h | 50 +++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index cb95626c7d..711b221704 100644
--- a/include/qemu/host-utils.h
+++ b/include/qemu/host-utils.h
@@ -26,6 +26,7 @@
#ifndef HOST_UTILS_H
#define HOST_UTILS_H
+#include "qemu/compiler.h"
#include "qemu/bswap.h"
#ifdef CONFIG_INT128
@@ -581,6 +582,55 @@ static inline bool umul64_overflow(uint64_t x, uint64_t y,
uint64_t *ret)
#endif
}
+/**
+ * uadd64_carry - addition with carry-in and carry-out
+ * @x, @y: addends
+ * @pcarry: in-out carry value
+ *
+ * Computes @x + @y + *@pcarry, placing the carry-out back
+ * into *@pcarry and returning the 64-bit sum.
+ */
+static inline uint64_t uadd64_carry(uint64_t x, uint64_t y, bool *pcarry)
+{
+#if __has_builtin(__builtin_addcll)
+ unsigned long long c = *pcarry;
+ x = __builtin_addcll(x, y, c, &c);
+ *pcarry = c & 1;
+ return x;
+#else
+ bool c = *pcarry;
+ /* This is clang's internal expansion of __builtin_addc. */
+ c = uadd64_overflow(x, c, &x);
+ c |= uadd64_overflow(x, y, &x);
+ *pcarry = c;
+ return x;
+#endif
+}
+
+/**
+ * usub64_borrow - subtraction with borrow-in and borrow-out
+ * @x, @y: addends
+ * @pborrow: in-out borrow value
+ *
+ * Computes @x - @y - *@pborrow, placing the borrow-out back
+ * into *@pborrow and returning the 64-bit sum.
+ */
+static inline uint64_t usub64_borrow(uint64_t x, uint64_t y, bool *pborrow)
+{
+#if __has_builtin(__builtin_subcll)
+ unsigned long long b = *pborrow;
+ x = __builtin_subcll(x, y, b, &b);
+ *pborrow = b & 1;
+ return x;
+#else
+ bool b = *pborrow;
+ b = usub64_overflow(x, b, &x);
+ b |= usub64_overflow(x, y, &x);
+ *pborrow = b;
+ return x;
+#endif
+}
+
/* Host type specific sizes of these routines. */
#if ULONG_MAX == UINT32_MAX
--
2.25.1
- [PULL 00/46] softfloat: Convert float128 to FloatParts (part 1), Richard Henderson, 2021/05/16
- [PULL 01/46] qemu/host-utils: Use __builtin_bitreverseN, Richard Henderson, 2021/05/16
- [PULL 03/46] qemu/host-utils: Add wrappers for carry builtins,
Richard Henderson <=
- [PULL 05/46] tests/fp: add quad support to the benchmark utility, Richard Henderson, 2021/05/16
- [PULL 02/46] qemu/host-utils: Add wrappers for overflow builtins, Richard Henderson, 2021/05/16
- [PULL 04/46] accel/tcg: Use add/sub overflow routines in tcg-runtime-gvec.c, Richard Henderson, 2021/05/16
- [PULL 06/46] softfloat: Move the binary point to the msb, Richard Henderson, 2021/05/16
- [PULL 09/46] softfloat: Tidy a * b + inf return, Richard Henderson, 2021/05/16
- [PULL 08/46] softfloat: Use float_raise in more places, Richard Henderson, 2021/05/16
- [PULL 10/46] softfloat: Add float_cmask and constants, Richard Henderson, 2021/05/16
- [PULL 11/46] softfloat: Use return_nan in float_to_float, Richard Henderson, 2021/05/16
- [PULL 13/46] target/mips: Set set_default_nan_mode with set_snan_bit_is_one, Richard Henderson, 2021/05/16
- [PULL 16/46] softfloat: Move type-specific pack/unpack routines, Richard Henderson, 2021/05/16