]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Common header file change not applicable to open-vm-tools.
authorOliver Kurth <okurth@vmware.com>
Fri, 2 Aug 2019 18:07:20 +0000 (11:07 -0700)
committerOliver Kurth <okurth@vmware.com>
Fri, 2 Aug 2019 18:07:20 +0000 (11:07 -0700)
open-vm-tools/lib/include/clamped.h

index 3e878857bdc966544003dd1af9a46d3a91e8f6a4..dacd057ebb2177e330c30ed3f7d45a42ac72028b 100644 (file)
@@ -377,7 +377,9 @@ Clamped_UMul64(uint64 *out,  // OUT
    uint32 bL = b & MASK64(32);
    uint32 bH = b >> 32;
 
-   if (aH > 0 && bH > 0) {
+   ASSERT(out != NULL);
+
+   if (UNLIKELY(aH > 0 && bH > 0)) {
       *out = MAX_UINT64;
       return FALSE;
    } else {
@@ -385,10 +387,11 @@ Clamped_UMul64(uint64 *out,  // OUT
       uint64 s2 = (aL * (uint64)bH) << 32;
       uint64 s3 = (aL * (uint64)bL);
       uint64 sum;
-      Bool clamped = FALSE;
+      Bool clamped;
 
-      clamped |= !Clamped_UAdd64(&sum, s1, s2);
-      clamped |= !Clamped_UAdd64(&sum, sum, s3);
+      ASSERT(s1 == 0 || s2 == 0);
+      sum = s1 + s2;
+      clamped = !Clamped_UAdd64(&sum, sum, s3);
 
       *out = sum;
       return !clamped;
@@ -396,6 +399,38 @@ Clamped_UMul64(uint64 *out,  // OUT
 }
 
 
+/*
+ *-----------------------------------------------------------------------------
+ *
+ * Clamped_USub64 --
+ *
+ *      Unsigned 64-bit subtraction.
+ *      Compute (a - b), and clamp to 0 if the result would have underflowed.
+ *
+ * Results:
+ *      On success, returns TRUE. If the result would have underflowed
+ *      and we clamped it to 0, returns FALSE.
+ *
+ *-----------------------------------------------------------------------------
+ */
+
+static INLINE Bool
+Clamped_USub64(uint64 *out,  // OUT
+               uint64 a,     // IN
+               uint64 b)     // IN
+{
+   ASSERT(out != NULL);
+
+   if (UNLIKELY(b > a)) {
+      *out = 0;
+      return FALSE;
+   } else {
+      *out = a - b;
+      return TRUE;
+   }
+}
+
+
 #if defined __cplusplus
 } // extern "C"
 #endif