<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div></div><div><br><div><div>On May 28, 2013, at 3:57 AM, Stephen Canon <<a href="mailto:scanon@apple.com">scanon@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>Hi Michael —</div><div><br></div><div><div>+  integerPart carry = 0;</div><div>+  const integerPart allones = ~integerPart(0);</div><div>+  for (unsigned int i = 0; i < parts; i++) {</div><div>+    integerPart oldvalue = dst[i];</div><div>+    if (carry) {</div><div>+      dst[i] += allones + 1;</div><div>+      carry = (dst[i] <= oldvalue);</div><div>+    } else {</div><div>+      dst[i] += allones;</div><div>+      carry = (dst[i] < oldvalue);</div><div>+    }</div><div>+  }</div><div>+</div><div>+  // We only borrow when dst == 0. Conclude since:</div><div>+  //   1. dst != 0 => dst + ~0 will overflow.</div><div>+  //   2. dst == 0 => dst + ~0 = ~0 no overflow.</div><div>+  // that if the ``carry'' flag is not set, we need to borrow.</div><div>+  return !carry;</div><div>+}</div></div><div><br></div><div>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:</div><div><br></div><div><span class="Apple-tab-span" style="white-space: pre;">   </span>for (unsigned int i = 0; i < parts; i++) {</div><div><span class="Apple-tab-span" style="white-space: pre;">              </span>// If the current word is non-zero, then the decrement has no effect on</div><div><span class="Apple-tab-span" style="white-space: pre;">            </span>// higher-order words of the integer, and there is no borrow.</div><div><span class="Apple-tab-span" style="white-space: pre;">              </span>if (dst[i]--) return 0; </div><div><span class="Apple-tab-span" style="white-space: pre;">      </span>}</div><div><span class="Apple-tab-span" style="white-space: pre;">  </span>// If every word was zero, then there is a borrow.</div><div><span class="Apple-tab-span" style="white-space: pre;"> </span>return 1;</div><div><br></div><div>– Steve</div><br><div><div>On May 27, 2013, at 9:50 PM, Michael Gottesman <<a href="mailto:mgottesman@apple.com">mgottesman@apple.com</a>> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div style="letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;">The attached patch is the first in a series of 2 patches which implement IEEE-754R 2008 nextUp/nextDown via the function APFloat::next.<br><br>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.<br><br>Please Review,<br>Michael<br><br><span><0001-APInt-Implement-tcDecrement-as-a-counterpart-to-tcIn.patch></span>_______________________________________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br><a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a></div></blockquote></div></div></blockquote></div><br></div></body></html>