From: VMware, Inc <> Date: Wed, 18 Sep 2013 03:19:34 +0000 (-0700) Subject: Add Clamped_UAdd64 X-Git-Tag: 2013.09.16-1328054~91 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7f89ef424c41687763e8a976337fea1a43ed299b;p=thirdparty%2Fopen-vm-tools.git Add Clamped_UAdd64 Signed-off-by: Dmitry Torokhov --- diff --git a/open-vm-tools/lib/include/clamped.h b/open-vm-tools/lib/include/clamped.h index 473398ecb..8d555928d 100644 --- a/open-vm-tools/lib/include/clamped.h +++ b/open-vm-tools/lib/include/clamped.h @@ -248,4 +248,40 @@ Clamped_UAdd32(uint32 *out, // OUT } +/* + *----------------------------------------------------------------------------- + * + * Clamped_UAdd64 -- + * + * Unsigned 64-bit addition. + * + * This is a utility function for 64-bit unsigned addition, + * in which the result is clamped to MAX_UINT64 on overflow. + * + * Results: + * On success, returns TRUE. If the result would have overflowed + * and we clamped it to MAX_UINT64, returns FALSE. + * + * Side effects: + * None. + * + *----------------------------------------------------------------------------- + */ + +static INLINE Bool +Clamped_UAdd64(uint64 *out, // OUT + uint64 a, // IN + uint64 b) // IN +{ + uint64 c = a + b; + + if(UNLIKELY(c < a || c < b)) { + *out = MAX_UINT64; + return FALSE; + } + + *out = c; + return TRUE; +} + #endif // ifndef _CLAMPED_H_