Add saturating addition/subtraction functions for unsigned 32 and 64 bit
values.

Signed-off-by: Hanna Czenczek <[email protected]>
---
 include/qemu/host-utils.h | 62 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)

diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index 1db9dbb138e..848e04c8fb1 100644
--- a/include/qemu/host-utils.h
+++ b/include/qemu/host-utils.h
@@ -636,6 +636,36 @@ static inline int64_t sadd64_saturate(int64_t x, int64_t y)
     return ret;
 }
 
+/**
+ * uadd32_saturate - 32-bit unsigned addition with saturation
+ * @x, @y: addends
+ *
+ * Computes @x + @y, and saturates rather than truncating the result.
+ */
+static inline uint32_t uadd32_saturate(uint32_t x, uint32_t y)
+{
+    uint32_t ret;
+    if (uadd32_overflow(x, y, &ret)) {
+        ret = UINT32_MAX;
+    }
+    return ret;
+}
+
+/**
+ * uadd64_saturate - 64-bit unsigned addition with saturation
+ * @x, @y: addends
+ *
+ * Computes @x + @y, and saturates rather than truncating the result.
+ */
+static inline uint64_t uadd64_saturate(uint64_t x, uint64_t y)
+{
+    uint64_t ret;
+    if (uadd64_overflow(x, y, &ret)) {
+        ret = UINT64_MAX;
+    }
+    return ret;
+}
+
 /**
  * ssub32_saturate - 32-bit signed subtraction with saturation
  * @x, @y: addends
@@ -666,6 +696,38 @@ static inline int64_t ssub64_saturate(int64_t x, int64_t y)
     return ret;
 }
 
+/**
+ * usub32_saturate - 32-bit unsigned subtraction with saturation
+ * @x: Minuend
+ * @y: Subtrahend
+ *
+ * Computes @x - @y, and saturates rather than truncating the result.
+ */
+static inline uint32_t usub32_saturate(uint32_t x, uint32_t y)
+{
+    uint32_t ret;
+    if (usub32_overflow(x, y, &ret)) {
+        ret = 0;
+    }
+    return ret;
+}
+
+/**
+ * usub64_saturate - 64-bit unsigned subtraction with saturation
+ * @x: Minuend
+ * @y: Subtrahend
+ *
+ * Computes @x - @y, and saturates rather than truncating the result.
+ */
+static inline uint64_t usub64_saturate(uint64_t x, uint64_t y)
+{
+    uint64_t ret;
+    if (usub64_overflow(x, y, &ret)) {
+        ret = 0;
+    }
+    return ret;
+}
+
 /*
  * Unsigned 128x64 multiplication.
  * Returns true if the result got truncated to 128 bits.
-- 
2.55.0


Reply via email to