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

Philippe Mathieu-Daude <[email protected]> Wed, 5 Aug 2026 19:12:35 +0200
Newsgroups gmane.comp.emulators.qemu
Message-ID <CANWQopVRj=Uogwhf=MTGrs3-Hk3HrE+X6aU5Zmtq8zyCiNciWg@mail.gmail.com>
On 5/8/26 18:52, 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.
>

Cc: [email protected]
Fixes: 16495533131 ("host-utils: Introduce signed saturation primitives")

> Signed-off-by: Brian Cain <[email protected]>
> ---
>   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

While here, "32-bit signed 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

Also  "64-bit signed addition with saturation", etc.

>    * @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

Eh? "addends" is a good description. If you rather:

  * @x: first addend
  * @y: second addend

>    *
> - * 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.
>    */

Reviewed-by: Philippe Mathieu-Daudé <[email protected]>