shared: add nm_add_u32_clamped() helper

This commit is contained in:
Thomas Haller 2020-10-22 10:22:48 +02:00
parent c3fe895ebc
commit b38075b751
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -189,6 +189,19 @@ typedef uint64_t _nm_bitwise nm_be64_t;
/*****************************************************************************/
static inline uint32_t
nm_add_u32_clamped(uint32_t a, uint32_t b)
{
uint32_t c;
/* returns the sum of a+b, or UINT32_MAX if the result would overflow. */
c = a + b;
if (c < a)
return UINT32_MAX;
return c;
}
/* glib's MIN()/MAX() macros don't have function-like behavior, in that they evaluate
* the argument possibly twice.
*