Sam Steingold on Thu, 27 May 2010 23:33:27 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

integer representation in pari


Hi,
It appears that integers are represented differently on different platforms.
Specifically, the "byte" order is different.
I have two machines, both little-endian:
32bit AMD with "GP/PARI CALCULATOR Version 2.3.4 (released)"
64bit Intel Pentium D with "GP/PARI CALCULATOR Version 2.3.5 (released)"

In gdb, I am trying to investigate the representation of the same
93-bit integer:

32-bit machine:

(gdb) p readseq("7906420317433764459701366445")
$1 = 11282736
(gdb) p ((ulong*)11282736)[0]
No symbol "ulong" in current context.
(gdb) p ((unsigned long*)11282736)[0]
$2 = 33554438
(gdb) p ((unsigned long*)11282736)[1]
$3 = 1073741829
(gdb) p ((unsigned long*)11282736)[2]
$4 = 290477
(gdb) p ((unsigned long*)11282736)[3]
$5 = 1019028024
(gdb) p ((unsigned long*)11282736)[4]
$6 = 428607904

(+ (ash 428607904 64) (ash 1019028024 32) 290477)
=> 7906420317433764459701366445

so we can see that the number is represented as 3 "32-bit bytes" in
positions 2,3,4,
***LOW*** bytes first

64-bit machine:
(gdb) p readseq("7906420317433764459701366445")
$1 = (long int *) 0x2aab17055be8
(gdb) p ((long int *) 0x2aab17055be8)[0]
$2 = 144115188075855876
(gdb) p ((long int *) 0x2aab17055be8)[1]
$3 = 4611686018427387908
(gdb) p ((long int *) 0x2aab17055be8)[2]
$4 = 428607904
(gdb) p ((long int *) 0x2aab17055be8)[3]
$5 = 4376692036787793581

(+ (ash 428607904 64) 4376692036787793581)
=> 7906420317433764459701366445

so we can see that the number is represented as 2 "64-bit bytes" in
positions 2,3,
***HIGH*** bytes first.

Where is this documented?
Where is this behavior defined?
How do I know what byte order is used on which platform?

Thanks!

-- 
Sam Steingold <http://sds.podval.org>