Can we compile using only Integers Of Unusual Size?

6

1

I coined Integers Of Unusual Size to mean "bignum" using GMP, the underpinning of Mathematica's ability to craft very big numbers.

I have these functions from this question:

ascendingQ[x_] := 3 == Mod[x, 4]
uniqueQ[x_] := 0 != Mod[2 x - 1, 3]
uniqueRank[n_, m_] := Block[{a = If[1 != n && OddQ[n], n - 1, n]},
   (n + IntegerExponent[a, 2]) 2^m - 1]  

The first two need "bignum" inputs and the third needs "bignum" output

And I have this variation of countOrbit which we will use for our research:

totalOrbit[x_] := 
 Block[{h = x, t, c = 0, m, n, mt = 0, nt = 0},
  While[1 != h,
   h = (t = -1 + (3/2)^(m = IntegerExponent[h + 1, 2]) (h + 1))/
     2^(n = IntegerExponent[t, 2]);
   mt += m;
   nt += n;
   ];
  c = 2 mt + nt
  ]

This needs "bignum" everywhere. When we get a sufficiently frisky totalOrbit I will recast countOrbit.

After I execute the compiled totalOrbit, I get error messages about exceeding machine precision,

cto = Compile[{x}, totalOrbit[x]]

so, I need a way to indicate that all numbers are "bignums."

Fred Kline

Posted 2013-08-05T10:38:50.260

Reputation: 2 226

3Unfortunately Compile supports only machine-size integers, ruling out bignums. – kirma – 2013-08-05T10:54:55.187

Answers

3

Unfortunately Compile supports only machine-size integers, ruling out bignums.

kirma

Posted 2013-08-05T10:38:50.260

Reputation: 13 550

Where is "machine-size integer" defined and documented? Does it vary between machines, and how can I determine what it is on my machine? – rogerl – 2015-12-25T02:26:21.600

1@rogerl Please see $MaxMachineInteger. You can get the maximum machine-size integer with Needs["Developer\"]; Developer`$MaxMachineInteger.$MinMachineIntegerinterestingly enough doesn't exist, but on all even remotely modern architectures (that is, two's complement arithmetic) it would be equal to-Developer`$MaxMachineInteger-1`. – kirma – 2015-12-25T07:21:42.513

Actually my statement on minimum machine-size integer seems to be false. Developer\MachineIntegerQ[-Developer`$MaxMachineInteger - 1]returns false, butDeveloper`MachineIntegerQ[-Developer`$MaxMachineInteger]` returns true. This also corresponds to the documentation... but it's not consistent with how "native" signed types work on real hardware. – kirma – 2015-12-25T07:30:44.690

Thanks. I didn't know about that package. And so given this (on my machine, this is $2^{63}-1$, as I expected), can I expect compiled programs with integer arguments to function properly up to this bound? (I realize that this is exactly what the documentation for Compile says.) – rogerl – 2015-12-25T13:14:28.120

1Oh, and with respect to the minimum machine-size integer, I think you would expect it to be equal to -$MaxMachineInteger + 1, not -$MaxMachineInteger-1$. But in any case, -$MaxMachineInteger returning True is indeed unexpected. – rogerl – 2015-12-25T13:19:10.040

3

As pointed out in the comments, Compile uses machine numbers only. Also note that IntegerExponent is not a compilable function as can be seen from this example:

f = Compile[{{in, _Integer, 0}}, IntegerExponent[in]];
Needs["CompiledFunctionTools`"]
CompilePrint[f]

Note the call to MainEvaluate in the output. This indicates that the function can not be compiled.

Concerning your top level functions, the switch between bignum and machine integer is opaque in Mathematica. If a calculation needs big integers it will internally switch to such a representation. No action on your part is required for that.

user21

Posted 2013-08-05T10:38:50.260

Reputation: