# c++ - two - restoring division algorithm

## Integer division algorithm (2)

I feel the need to add to this based on my comment. This isn't an answer, but an explanation as to the background.

A bignum library uses what are called limbs - search for mp_limb_t in the gmp source, which are usually a fixed-size integer field.

When you do something like addition, one way (albeit inefficient) to approach it is to do this:

```
doublelimb r = limb_a + limb_b + carryfrompreviousiteration
```

This double-sized limb catches the overflow of limb_a + limb_b in the case that the sum is bigger than the limb size. So if the total is bigger than 2^32 if we're using uint32_t as our limb size, the overflow can be caught.

Why do we need this? Well, what you typically do is loop through all the limbs - you've done this yourself in dividing your integer up and going through each one - but we do it LSL first (so the smallest limb first) just as you'd do arithmetic by hand.

This might seem inefficient, but this is just the C way of doing things. To really break out the big guns, x86 has `adc`

as an instruction - add with carry. What this does is an arithmetic and on your fields and sets the carry bit if the arithmetic overflows the size of the register. The next time you do `add`

or `adc`

, the processor factors in the carry bit too. In subtraction it's called the borrow flag.

This also applies to shift operations. As such, this feature of the processor is crucial to what makes bignums fast. So the fact is, there's electronic circuitry in the chip for doing this stuff - doing it in software is *always* going to be slower.

Without going into too much detail, operations are built up from this ability to add, shift, subtract etc. They're crucial. Oh and you use the full width of your processor's register per limb if you're doing it right.

Second point - conversion between bases. You *cannot* take a value in the middle of a number and change it's base, because you can't account for the overflow from the digit beneath it in your original base, and that number can't account for the overflow from the digit beneath... and so on. In short, every time you want to change base, you need to convert *the entire bignum* from the original base to your new base back again. So you have to walk the bignum (all the limbs) three times at least. Or, alternatively, detect overflows expensively in all other operations... remember, now you need to do modulo operations to work out if you overflowed, whereas before the processor was doing it for us.

I should also like to add that whilst what you've got is probably quick for this case, bear in mind that as a bignum library gmp does a fair bit of work for you, like memory management. If you're using `mpz_`

you're using an abstraction above what I've described here, for starters. Finally, gmp uses hand optimised assembly with unrolled loops for just about every platform you've ever heard of, plus more. There's a very good reason it ships with Mathematica, Maple et al.

Now, just for reference, some reading material.

- Modern Computer Arithmetic is a Knuth-like work for arbitrary precision libraries.
- Donald Knuth, Seminumerical Algorithms (The Art of Computer Programming Volume II).
- William Hart's blog on implementing algorithm's for bsdnt in which he discusses various division algorithms. If you're interested in bignum libraries, this is an excellent resource. I considered myself a good programmer until I started following this sort of stuff...

To sum it up for you: division assembly instructions suck, so people generally compute inverses and multiply instead, as you do when defining division in modular arithmetic. The various techniques that exist (see MCA) are mostly O(n).

Edit: Ok, not all of the techniques are O(n). Most of the techniques called div1 (dividing by something not bigger than a limb are O(n). When you go bigger you end up with O(n^2) complexity; this is hard to avoid.

Now, could you implement bigints as an array of digits? Well yes, of course you could. However, consider the idea just under addition

```
/* you wouldn't do this just before add, it's just to
show you the declaration.
*/
uint32_t* x = malloc(num_limbs*sizeof(uint32_t));
uint32_t* y = malloc(num_limbs*sizeof(uint32_t));
uint32_t* a = malloc(num_limbs*sizeof(uint32_t));
uint32_t m;
for ( i = 0; i < num_limbs; i++ )
{
m = 0;
uint64_t t = x[i] + y[i] + m;
/* now we need to work out if that overflowed at all */
if ( (t/somebase) >= 1 ) /* expensive division */
{
m = t % somebase; /* get the overflow */
}
}
/* frees somewhere */
```

That's a rough sketch of what you're looking at for addition via your scheme. So you *have* to run the conversion between bases. So you're going to need a conversion *to your representation* for the base, then back when you're done, because this form is just *really slow everywhere else*. We're not talking about the difference between O(n) and O(n^2) here, but we are talking about an expensive division instruction *per limb* or *an expensive conversion every time you want to divide*. See this.

Next up, *how* do you expand your division for general case division? By that, I mean when you want to divide those two numbers *x* and *y* from the above code. You can't, is the answer, without resorting to bignum-based facilities, which are expensive. See Knuth. Taking modulo a number greater than your size doesn't work.

Let me explain. Try 21979182173 mod 1099. Let's assume here for simplicity's sake that **the biggest size field we can have is three digits**. This is a contrived example, but the biggest field size I know if uses 128 bits using gcc extensions. Anyway, the point is, you:

```
21 979 182 173
```

Split your number into limbs. Then you take modulo and sum:

```
21 1000 1182 1355
```

It doesn't work. This is where Avi is correct, because this is a form of casting out nines, or an adaption thereof, but it doesn't work here because our fields have overflowed for a start - you're using the modulo to ensure each field stays within its limb/field size.

So what's the solution? Split your number up into a series of appropriately sized bignums? And start using bignum functions to calculate everything you need to? This is going to be much slower than any existing way of manipulating the fields directly.

Now perhaps you're only proposing this case for dividing by a limb, not a bignum, in which case it can work, but hensel division and precomputed inverses etc do to *without the conversion requirement*. I have no idea if this algorithm would be faster than say hensel division; it would be an interesting comparison; the problem comes with *a common representation across the bignum library*. The representation chosen in existing bignum libraries is for the reasons I've expanded on - it makes sense at the assembly level, where it was first done.

As a side note; you don't have to use `uint32_t`

to represent your limbs. You use a size ideally the size of the registers of the system (say uint64_t) so that you can take advantage of assembly-optimised versions. So on a 64-bit system `adc rax, rbx`

only sets the overflow (CF) if the result overspills 2^64 bits.

**tl;dr** version: the problem isn't your algorithm or idea; it's the problem of converting between bases, since the representation you need for your algorithm isn't the most efficient way to do it in add/sub/mul etc. To paraphrase knuth: This shows you the difference between mathematical elegance and computational efficiency.

I was thinking about an algorithm in division of large numbers: dividing with remainder bigint C by bigint D, where we know the representation of C in base b, and D is of form b^k-1. It's probably the easiest to show it on an example. Let's try dividing C=21979182173 by D=999.

- We write the number as sets of three digits: 21 979 182 173
- We take sums (modulo 999) of consecutive sets, starting from the left: 21 001 183 356
- We add 1 to those sets preceding the ones where we "went over 999": 22 001 183 356

Indeed, 21979182173/999=22001183 and remainder 356.

I've calculated the complexity and, if I'm not mistaken, the algorithm should work in O(n), n being the number of digits of C in base b representation. I've also done a very crude and unoptimized version of the algorithm (only for b=10) in C++, tested it against GMP's general integer division algorithm and it really does seem to fare better than GMP. I couldn't find anything like this implemented anywhere I looked, so I had to resort to testing it against general division.

I found several articles which discuss what seem to be quite similar matters, but none of them concentrate on actual implementations, especially in bases different than 2. I suppose that's because of the way numbers are internally stored, although the mentioned algorithm seems useful for, say, b=10, even taking that into account. I also tried contacting some other people, but, again, to no avail.

Thus, my question would be: is there an article or a book or something where the aforementioned algorithm is described, possibly discussing the implementations? If not, would it make sense for me to try and implement and test such an algorithm in, say, C/C++ or is this algorithm somehow inherently bad?

Also, I'm not a programmer and while I'm reasonably OK at programming, I admittedly don't have much knowledge of computer "internals". Thus, pardon my ignorance - it's highly possible there are one or more very stupid things in this post. Sorry once again.

Thanks a lot!

*Further clarification of points raised in the comments/answers:*

Thanks, everyone - as I didn't want to comment on all the great answers and advice with the same thing, I'd just like to address one point a lot of you touched on.

I am fully aware that working in bases 2^n is, generally speaking, clearly the most efficient way of doing things. Pretty much all bigint libraries use 2^32 or whatever. However, what if (and, I emphasize, it would be useful only for this particular algorithm!) we implement bigints as an array of digits in base b? Of course, we require b here to be "reasonable": b=10, the most natural case, seems reasonable enough. I know it's more or less inefficient both considering memory and time, taking into account how numbers are internally stored, but I have been able to, if my (basic and possibly somehow flawed) tests are correct, produce results faster than GMP's general division, which would give sense to implementing such an algorithm.

Ninefingers notices I'd have to use in that case an expensive modulo operation. I hope not: I can see if old+new crossed, say, 999, just by looking at the number of digits of old+new+1. If it has 4 digits, we're done. Even more, since old<999 and new<=999, we know that if old+new+1 has 4 digits (it can't have more), then, (old+new)%999 equals deleting the leftmost digit of (old+new+1), which I presume we can do cheaply.

Of course, I'm not disputing obvious limitations of this algorithm nor I claim it can't be improved - it can only divide with a certain class of numbers and we have to a priori know the representation of dividend in base b. However, for b=10, for instance, the latter seems natural.

Now, say we have implemented bignums as I outlined above. Say C=(a_1a_2...a_n) in base b and D=b^k-1. The algorithm (which could be probably much more optimized) would go like this. I hope there aren't many typos.

- if k>n, we're obviously done
- add a zero (i.e. a_0=0) at the beginning of C
*(just in case we try to divide, say, 9999 with 99)* - l=n%k
*(mod for "regular" integers - shouldn't be too expensive)* - old=(a_0...a_l)
*(the first set of digits, possibly with less than k digits)* - for (i=l+1; i < n; i=i+k)
*(We will have floor(n/k) or so iterations)*- new=(a_i...a_(i+k-1))
- new=new+old
*(this is bigint addition, thus O(k))* - aux=new+1
*(again, bigint addition - O(k) - which I'm not happy about)* - if aux has more than k digits
- delete first digit of aux
- old=old+1
*(bigint addition once again)* - fill old with zeroes at the beginning so it has as much digits as it should
- (a_(i-k)...a_(i-1))=old
*(if i=l+1, (a*_*0...a*_*l)=old)* - new=aux

- fill new with zeroes at the beginning so it has as much digits as it should
- (a_i...a_(i+k-1)=new

- quot=(a_0...a_(n-k+1))
- rem=new

There, thanks for discussing this with me - as I said, this does seem to me to be an interesting "special case" algorithm to try to implement, test and discuss, if nobody sees any fatal flaws in it. If it's something not widely discussed so far, even better. Please, let me know what you think. Sorry about the long post.

Also, just a few more personal comments:

@Ninefingers: I actually have some (very basic!) knowledge of how GMP works, what it does and of general bigint division algorithms, so I was able to understand much of your argument. I'm also aware GMP is highly optimized and in a way customizes itself for different platforms, so I'm certainly not trying to "beat it" in general - that seems as much fruitful as attacking a tank with a pointed stick. However, that's not the idea of this algorithm - it works in very special cases (which GMP does not appear to cover). On an unrelated note, are you sure general divisions are done in O(n)? The most I've seen done is M(n). (And that can, if I understand correctly, in practice (Schönhage–Strassen etc.) not reach O(n). Fürer's algorithm, which still doesn't reach O(n), is, if I'm correct, almost purely theoretical.)

@Avi Berger: This doesn't actually seem to be exactly the same as "casting out nines", although the idea is similar. However, the aforementioned algorithm should work all the time, if I'm not mistaken.

If you need to frequently divide by the same divisor, using *it* (or a power of it) as your base makes division as cheap as bit-shifting is for base 2 binary integers.

You could use base 999 if you want; there's nothing special about using a power-of-10 base except that it makes conversion to decimal integer very cheap. (You can work one limb at a time instead of having to do a full division over the whole integer. It's like the difference between converting a binary integer to decimal vs. turning every 4 bits into a hex digit. Binary -> hex can start with the most significant bits, but converting to non-power-of-2 bases has to be LSB-first using division.)

For example, to compute the first 1000 decimal digits of Fibonacci(10^{9}) for a code-golf question with a performance requirement, my 105 bytes of x86 machine code answer used the same algorithm as this Python answer: the usual `a+=b; b+=a`

Fibonacci iteration, but divide by (a power of) 10 every time `a`

gets too large.

Fibonacci grows faster than carry propagates, so discarding the low decimal digits occasionally doesn't change the high digits long-term. (You keep a few extra beyond the precision you want).

Dividing by a power of 2 *doesn't* work, unless you keep track of how many powers of 2 you've discarded, because the eventual binary -> decimal conversion at the end would depend on that.

So for this algorithm, you have to do extended-precision addition, and division by 10 (or whatever power of 10 you want).

I stored base-10^{9} limbs in 32-bit integer elements. Dividing by 10^{9} is trivially cheap: just a pointer increment to skip the low limb. Instead of actually doing a `memmove`

, I just offset the pointer used by the next add iteration.

I think division by a power of 10 other than 10^9 would be somewhat cheap, but would require an actual division on each limb, and propagating the remainder to the next limb.

**Extended-precision addition is somewhat more expensive this way** than with binary limbs, because I have to generate the carry-out manually with a compare: `sum[i] = a[i] + b[i];`

`carry = sum < a;`

(unsigned comparison). And also manually wrap to 10^9 based on that compare, with a conditional-move instruction. But I was able to use that carry-out as an input to `adc`

(x86 add-with-carry instruction).

You don't need a full modulo to handle the wrapping on addition, because you know you've wrapped at most once.

This wastes a just over 2 bits of each 32-bit limb: 10^9 instead of `2^32 = 4.29... * 10^9`

. Storing base-10 digits one per byte would be significantly less space efficient, and *very* much worse for performance, because an 8-bit binary addition costs the same as a 64-bit binary addition on a modern 64-bit CPU.

I was aiming for code-size: for pure performance I would have used 64-bit limbs holding base-10^19 "digits". (`2^64 = 1.84... * 10^19`

, so this wastes less than 1 bit per 64.) This lets you get twice as much work done with each hardware `add`

instruction. Hmm, actually this might be a problem: the sum of two limbs might wrap the 64-bit integer, so just checking for `> 10^19`

isn't sufficient anymore. You could work in base `5*10^18`

, or in base `10^18`

, or do more complicated carry-out detection that checks for binary carry as well as manual carry.

Storing packed BCD with one digit per 4 bit nibble would be even worse for performance, because there isn't hardware support for blocking carry from one nibble to the next within a byte.

Overall, my version ran about 10x faster than the Python extended-precision version on the same hardware (but it had room for significant optimization for speed, by dividing less often). (70 seconds or 80 seconds vs. 12 minutes)

Still, I think for this particular implementation of *that* algorithm (where I only needed addition and division, and division happened after every few additions), the choice of base-10^9 limbs was very good. There are much more efficient algorithms for the Nth Fibonacci number that don't need to do 1 billion extended-precision additions.