From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 0AEF3BBC4 for ; Mon, 16 Mar 2009 22:58:55 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AggEABVovkmBrw8EgWdsb2JhbACVVQEBFiK+e4N/Bg X-IronPort-AV: E=Sophos;i="4.38,375,1233529200"; d="scan'208";a="36643237" Received: from ext.lri.fr ([129.175.15.4]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 16 Mar 2009 22:58:54 +0100 Received: from localhost (localhost [127.0.0.1]) by ext.lri.fr (Postfix) with ESMTP id A1406A4363; Mon, 16 Mar 2009 22:58:54 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at lri.fr Received: from ext.lri.fr ([127.0.0.1]) by localhost (ext.lri.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id YOcPMp422E2K; Mon, 16 Mar 2009 22:58:54 +0100 (CET) Received: from [192.168.0.10] (mry91-1-82-229-156-20.fbx.proxad.net [82.229.156.20]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ext.lri.fr (Postfix) with ESMTP id 527FFA42A3; Mon, 16 Mar 2009 22:58:54 +0100 (CET) Message-ID: <49BECB9D.5010805@lri.fr> Date: Mon, 16 Mar 2009 22:58:53 +0100 From: =?ISO-8859-1?Q?Jean-Christophe_Filli=E2tre?= User-Agent: Thunderbird 1.5.0.14ubu (X11/20080925) MIME-Version: 1.0 To: Jon Harrop Cc: O'Caml Mailing List Subject: Re: [Caml-list] Parsing 64-bit ints in 32-bit OCaml References: <200903071728.56418.jon@ffconsultancy.com> In-Reply-To: <200903071728.56418.jon@ffconsultancy.com> X-Enigmail-Version: 0.94.2.0 OpenPGP: url=http://www.lri.fr/~filliatr/mykey.asc Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Spam: no; 0.00; filliatre:01 filliatre:01 lri:01 ocaml:01 failwith:01 ocaml's:01 parser:01 integers:01 parsing:01 integer:01 ints:01 caml-list:01 functions:01 computation:01 int:01 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