GMP with small numbers (Paulo J. Matos)

Jim White mathimagics at yahoo.co.uk
Sun Jun 18 17:58:32 CEST 2006


> Date: Sat, 17 Jun 2006 22:44:43 +0100
> From: "Paulo J. Matos" <pocmatos at gmail.com>
> Subject: GMP with small numbers
> 
> 
> for efficiency I might think of implementing a new
> number structure in C++ which uses long long ints
> (or the biggest type provided by the compiler) and
> overload the arithmetic operators, then
> check internally for overflows and if one is about
> to happen I just switch the numbers internally for
> the GMP numbers and keep computing.
>
> I can even switch back if I notice the numbers
> already fit in a machine size number. Basically, 
> GMP does this automatically or if I want this kind 
> of stuff for efficiency, I have to implement it 
> myself?
 
If your platform has hardware support for 64-bit
arithmetic, then GMP properly configured should use
that.  

I find it difficult to imagine you could possibly need
to introduce such complexity, but the sensible thing
to do, it seems to me, is to run some simple timing
tests.

I did that myself some time ago, for vaguely similar
reasons, and was able to satisfy myself in a very
short time that, on my PC at least, using GMP for
predominantly single-limb operations was barely
distinguishable from using native integers.  

So, if any calculation at all involves multiple
precision, even if it's only a fraction of the total
cases, it's usually easier just to code it straight up
with mpz's


Cheers

Jim White
ANU, Canberra


More information about the gmp-discuss mailing list