caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Parsing 64-bit ints in 32-bit OCaml
@ 2009-03-07 17:28 Jon Harrop
  2009-03-07 18:12 ` [Caml-list] " Jérémie Dimino
  2009-03-16 21:58 ` Jean-Christophe Filliâtre
  0 siblings, 2 replies; 3+ messages in thread
From: Jon Harrop @ 2009-03-07 17:28 UTC (permalink / raw)
  To: O'Caml Mailing List


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?

-- 
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Parsing 64-bit ints in 32-bit OCaml
  2009-03-07 17:28 Parsing 64-bit ints in 32-bit OCaml Jon Harrop
@ 2009-03-07 18:12 ` Jérémie Dimino
  2009-03-16 21:58 ` Jean-Christophe Filliâtre
  1 sibling, 0 replies; 3+ messages in thread
From: Jérémie Dimino @ 2009-03-07 18:12 UTC (permalink / raw)
  To: Jon Harrop; +Cc: O'Caml Mailing List

Jon Harrop wrote:
> Is there a better workaround?

A (maybe overkill) solution is to use optcomp [1]. What you have to do
is to add word_size variable:

  Pa_optcomp.define "word_size" (Int Sys.word_size)

Then you can write:

  let gcd3 = 
  #if word_size = 32
     715827883
  #elif word_size = 64
     3074457345618258603
  #else
  #  error "unknown word size"
  #endif

Jérémie
     
 [1] http://forge.ocamlcore.org/projects/optcomp/


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Parsing 64-bit ints in 32-bit OCaml
  2009-03-07 17:28 Parsing 64-bit ints in 32-bit OCaml Jon Harrop
  2009-03-07 18:12 ` [Caml-list] " Jérémie Dimino
@ 2009-03-16 21:58 ` Jean-Christophe Filliâtre
  1 sibling, 0 replies; 3+ messages in thread
From: Jean-Christophe Filliâtre @ 2009-03-16 21:58 UTC (permalink / raw)
  To: Jon Harrop; +Cc: O'Caml Mailing List

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-03-16 21:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-03-07 17:28 Parsing 64-bit ints in 32-bit OCaml Jon Harrop
2009-03-07 18:12 ` [Caml-list] " Jérémie Dimino
2009-03-16 21:58 ` Jean-Christophe Filliâtre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).