Version française
Home     About     Download     Resources     Contact us    
Browse thread
Parsing 64-bit ints in 32-bit OCaml
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jean-Christophe_Filliâtre <Jean-Christophe.Filliatre@l...>
Subject: Re: [Caml-list] Parsing 64-bit ints in 32-bit OCaml
Jon Harrop a écrit :
> I'm just trying to write efficient functions for div and mod by three. I'd 
> like to handle 32- and 64-bit machines with the same code so I tried:
> 
>   let gcd3 = match Sys.word_size with
>     | 32 -> 715827883
>     | 64 -> 3074457345618258603
>     | _ -> failwith "Unknown word size"
> 
> That works perfectly in 64-bit but breaks OCaml's parser in 32-bit, which dies 
> with:
> 
>   Integer literal exceeds the range of representable integers of type int
> 
> As a workaround, I replaced it with:
> 
>   | 64 -> Int64.to_int 3074457345618258603L
> 
> Is there a better workaround?

I also bump into the same problem from time to time, and I usually
replace the large constant by a (launch time) computation, such as

	(0xffff lsl 16) lor 0xffff

for 0xffffffff for instance. In your case, it could simply be

	(3074457 * 1000000 + 345618) * 1000000 + 258603

But your solution is equally good (the int64 is boxed but is simpler to
read).

-- 
Jean-Christophe