From f5b89e8060ac718d971b5de97a78c60930b07b95 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 26 Oct 2020 16:47:31 +0100 Subject: shared: add nm_mult_clamped_u() helper --- shared/nm-std-aux/nm-std-aux.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/shared/nm-std-aux/nm-std-aux.h b/shared/nm-std-aux/nm-std-aux.h index a10abfb6a6..7b06472631 100644 --- a/shared/nm-std-aux/nm-std-aux.h +++ b/shared/nm-std-aux/nm-std-aux.h @@ -202,6 +202,24 @@ nm_add_clamped_u32(uint32_t a, uint32_t b) return c; } +static inline unsigned +nm_mult_clamped_u(unsigned a, unsigned b) +{ + unsigned c; + + /* returns a*b, or UINT_MAX if the result would overflow. */ + + if (b == 0) + return 0; + + c = a * b; + + if (c / b != a) + return (unsigned) -1; + + return c; +} + /* glib's MIN()/MAX() macros don't have function-like behavior, in that they evaluate * the argument possibly twice. * -- cgit v1.2.1