]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Add Clamped_UAdd64
authorVMware, Inc <>
Wed, 18 Sep 2013 03:19:34 +0000 (20:19 -0700)
committerDmitry Torokhov <dmitry.torokhov@gmail.com>
Mon, 23 Sep 2013 05:06:56 +0000 (22:06 -0700)
Signed-off-by: Dmitry Torokhov <dtor@vmware.com>
open-vm-tools/lib/include/clamped.h

index 473398ecb555b077d6d5624c0c7e20ec9f7dba98..8d555928daff08cb21df6242cfb9755d70a115c3 100644 (file)
@@ -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_