[PATCH 1/6] host-utils: fix ssub32/64_saturate return type and clamp direction

Brian Cain posted 6 patches 1 month, 1 week ago
Maintainers: Brian Cain <brian.cain@oss.qualcomm.com>, Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
[PATCH 1/6] host-utils: fix ssub32/64_saturate return type and clamp direction
Posted by Brian Cain 1 month, 1 week ago
ssub32_saturate() and ssub64_saturate() were declared to return bool
instead of int32_t/int64_t, and clamped to the wrong bound on overflow.

Signed-off-by: Brian Cain <brian.cain@oss.qualcomm.com>
---
 include/qemu/host-utils.h | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/include/qemu/host-utils.h b/include/qemu/host-utils.h
index 2e8da7fb3d0..291bb198b65 100644
--- a/include/qemu/host-utils.h
+++ b/include/qemu/host-utils.h
@@ -610,7 +610,7 @@ static inline bool umul64_overflow(uint64_t x, uint64_t y, uint64_t *ret)
  * sadd32_saturate - addition with saturation
  * @x, @y: addends
  *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x + @y, and saturates rather than truncating the result.
  */
 static inline int32_t sadd32_saturate(int32_t x, int32_t y)
 {
@@ -625,7 +625,7 @@ static inline int32_t sadd32_saturate(int32_t x, int32_t y)
  * sadd64_saturate - addition with saturation
  * @x, @y: addends
  *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x + @y, and saturates rather than truncating the result.
  */
 static inline int64_t sadd64_saturate(int64_t x, int64_t y)
 {
@@ -638,30 +638,30 @@ static inline int64_t sadd64_saturate(int64_t x, int64_t y)
 
 /**
  * ssub32_saturate - subtraction with saturation
- * @x, @y: addends
+ * @x, @y: minuend and subtrahend
  *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x - @y, and saturates rather than truncating the result.
  */
-static inline bool ssub32_saturate(int32_t x, int32_t y)
+static inline int32_t ssub32_saturate(int32_t x, int32_t y)
 {
     int32_t ret;
     if (ssub32_overflow(x, y, &ret)) {
-        ret = x < 0 ? INT32_MAX : INT32_MIN;
+        ret = x < 0 ? INT32_MIN : INT32_MAX;
     }
     return ret;
 }
 
 /**
  * ssub64_saturate - subtraction with saturation
- * @x, @y: addends
+ * @x, @y: minuend and subtrahend
  *
- * Computes @x + @y, and saturates rathern than truncating the result.
+ * Computes @x - @y, and saturates rather than truncating the result.
  */
-static inline bool ssub64_saturate(int64_t x, int64_t y)
+static inline int64_t ssub64_saturate(int64_t x, int64_t y)
 {
     int64_t ret;
     if (ssub64_overflow(x, y, &ret)) {
-        ret = x < 0 ? INT64_MAX : INT64_MIN;
+        ret = x < 0 ? INT64_MIN : INT64_MAX;
     }
     return ret;
 }
-- 
2.34.1

Re: [PATCH 1/6] host-utils: fix ssub32/64_saturate return type and clamp direction
Posted by Pierrick Bouvier 1 month, 1 week ago
On 8/5/2026 9:52 AM, Brian Cain wrote:
> ssub32_saturate() and ssub64_saturate() were declared to return bool
> instead of int32_t/int64_t, and clamped to the wrong bound on overflow.
> 
> Signed-off-by: Brian Cain <brian.cain@oss.qualcomm.com>
> ---
>  include/qemu/host-utils.h | 20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)
> 

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>