Boost logo

Boost :

Subject: Re: [boost] Algebraic abstractions related to (big) integers in C++
From: Phil Endecott (spam_from_boost_dev_at_[hidden])
Date: 2011-03-30 20:51:18


pavel wrote:
> i think that ideally a feature like 'add_with_carry()' should be
> available as a compiler intrinsic

I had a look at this for the XInt review, when I wrote some ARM
assembler here:

http://article.gmane.org/gmane.comp.lib.boost.devel/215565

The relevant code is this snippet to do a 96-bit add:

      "adds %0, %3, %6\n" // Add and set carry flag
      "adcs %1, %4, %7\n" // Add with carry-in and set carry flag
      "adc %2, %5, %8\n" // Add with carry-in.

The difficulty is that there is only one carry flag and that it is
implicit. So in a compiler intrinsic for add-with-carry you need to either:
- Keep the carry implicit, and somehow rely on the compiler not
inserting any instructions that change it in the generated instruction
stream (which seems impractical), or
- Make it explicit, but since there is only one place that it can be
stored, the compiler will have a challenge to generate code, or
- Transfer it to and from a general-purpose register, which will
greatly reduce the speed (back to what you can get without assembler or intrinsics).

I came to the conclusion that it is better to write multi-word addition
code (like the above) in assembler for each platform.

I believe that the issues are similar on other architectures that have
a flag register, but maybe others can confirm or deny that.

Any thoughts anyone?

Phil.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk