[PATCH 1/2] IEEE-754R 2008 nextUp/nextDown implementation: tcDecrement

Stephen Canon scanon at apple.com
Tue May 28 03:57:56 PDT 2013


Hi Michael —

+  integerPart carry = 0;
+  const integerPart allones = ~integerPart(0);
+  for (unsigned int i = 0; i < parts; i++) {
+    integerPart oldvalue = dst[i];
+    if (carry) {
+      dst[i] += allones + 1;
+      carry = (dst[i] <= oldvalue);
+    } else {
+      dst[i] += allones;
+      carry = (dst[i] < oldvalue);
+    }
+  }
+
+  // We only borrow when dst == 0. Conclude since:
+  //   1. dst != 0 => dst + ~0 will overflow.
+  //   2. dst == 0 => dst + ~0 = ~0 no overflow.
+  // that if the ``carry'' flag is not set, we need to borrow.
+  return !carry;
+}

While this is correct, it can be made much, much simpler by taking advantage of the fact that the addend is -1.  Once you get a carry-out, the result of the operation is a no-op:

	for (unsigned int i = 0; i < parts; i++) {
		// If the current word is non-zero, then the decrement has no effect on
		// higher-order words of the integer, and there is no borrow.
		if (dst[i]--) return 0; 
	}
	// If every word was zero, then there is a borrow.
	return 1;

– Steve

On May 27, 2013, at 9:50 PM, Michael Gottesman <mgottesman at apple.com> wrote:

> The attached patch is the first in a series of 2 patches which implement IEEE-754R 2008 nextUp/nextDown via the function APFloat::next.
> 
> This first patch implements the static function tcDecrement on APInt which decrements a bignum represented by a part count and an integerPart and returns the borrow flag. Unittests are included as well.
> 
> Please Review,
> Michael
> 
> <0001-APInt-Implement-tcDecrement-as-a-counterpart-to-tcIn.patch>_______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130528/211c2a2a/attachment.html>


More information about the llvm-commits mailing list