@samitouri / QOSamiQemu / commits / 9e96b5953e

host-utils: fix ssub32/64_saturate return type and clamp direction

ssub32_saturate() and ssub64_saturate() were declared to return bool instead of int32_t/int64_t, and clamped to the wrong bound on overflow. Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com> Cc: qemu-stable@nongnu.org Fixes: 16495533131 ("host-utils: Introduce signed saturation primitives") Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com> Link: https://lore.kernel.org/qemu-devel/20260807152241.1576334-2-brian.cain@oss.qualcomm.com Signed-off-by: Brian Cain <brian.cain@oss.qualcomm.com>

Brian Cain committed Aug 7, 2026 at 08:22 UTC 9e96b5953e2fb619a5ad6274ebba3baee0b933c4
1 file changed +12 -12
include/qemu/host-utils.h
+12 -12
@@ -607,10 +607,10 @@ static inline bool umul64_overflow(uint64_t x, uint64_t y, uint64_t *ret)
607 }
608
609 /**
610 - * sadd32_saturate - addition with saturation
610 + * sadd32_saturate - 32-bit signed addition with saturation
611 * @x, @y: addends
612 *
613 - * Computes @x + @y, and saturates rathern than truncating the result.
613 + * Computes @x + @y, and saturates rather than truncating the result.
614 */
615 static inline int32_t sadd32_saturate(int32_t x, int32_t y)
616 {
@@ -622,10 +622,10 @@ static inline int32_t sadd32_saturate(int32_t x, int32_t y)
622 }
623
624 /**
625 - * sadd64_saturate - addition with saturation
625 + * sadd64_saturate - 64-bit signed addition with saturation
626 * @x, @y: addends
627 *
628 - * Computes @x + @y, and saturates rathern than truncating the result.
628 + * Computes @x + @y, and saturates rather than truncating the result.
629 */
630 static inline int64_t sadd64_saturate(int64_t x, int64_t y)
631 {
@@ -637,31 +637,31 @@ static inline int64_t sadd64_saturate(int64_t x, int64_t y)
637 }
638
639 /**
640 - * ssub32_saturate - subtraction with saturation
640 + * ssub32_saturate - 32-bit signed subtraction with saturation
641 * @x, @y: addends
642 *
643 - * Computes @x + @y, and saturates rathern than truncating the result.
643 + * Computes @x - @y, and saturates rather than truncating the result.
644 */
645 -static inline bool ssub32_saturate(int32_t x, int32_t y)
645 +static inline int32_t ssub32_saturate(int32_t x, int32_t y)
646 {
647 int32_t ret;
648 if (ssub32_overflow(x, y, &ret)) {
649 - ret = x < 0 ? INT32_MAX : INT32_MIN;
649 + ret = x < 0 ? INT32_MIN : INT32_MAX;
650 }
651 return ret;
652 }
653
654 /**
655 - * ssub64_saturate - subtraction with saturation
655 + * ssub64_saturate - 64-bit signed subtraction with saturation
656 * @x, @y: addends
657 *
658 - * Computes @x + @y, and saturates rathern than truncating the result.
658 + * Computes @x - @y, and saturates rather than truncating the result.
659 */
660 -static inline bool ssub64_saturate(int64_t x, int64_t y)
660 +static inline int64_t ssub64_saturate(int64_t x, int64_t y)
661 {
662 int64_t ret;
663 if (ssub64_overflow(x, y, &ret)) {
664 - ret = x < 0 ? INT64_MAX : INT64_MIN;
664 + ret = x < 0 ? INT64_MIN : INT64_MAX;
665 }
666 return ret;
667 }