You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Original bug ID: 5355 Reporter: jfc Status: closed (set by @xavierleroy on 2015-12-11T18:04:22Z) Resolution: not a bug Priority: normal Severity: minor Version: 3.12.1 Category: ~DO NOT USE (was: OCaml general) Monitored by: "Pascal Cuoq" @ygrek
Bug description
I want to make some code conditional at compile time on the word size, i.e. I don't want the comparison word_size=64 to be done at runtime. In OCaml 3.12 Sys.word_size is initialized by a function call at program startup. The value is not available to the compiler. However, the value will not change from compile to runtime. It should be treated as a constant the same as sizeof() in C.
I have sample code showing where this matters. I attached a diff showing how to make Sys.word_size a constant. Pervasives.max_int and related values should be treated the same way. See caml-list traffic of September 5, 2011 for more background.
Additional information
module type M = sig type t val add : t -> t -> t val of_int : int -> t end;;
module Int : M = struct type t = int let add = (+) let of_int x = x end;;
module M = (val (if Sys.word_size = 64 then (module Int : M) else (module Int32 : M)) : M);;
(* The intent is for the module M to provide the usual int
operations on a 64 bit system and degenerate to boxed
integers and function calls on 32 bit systems. This is
appropriate when an integer of 32-63 bits is required.
In OCaml 3.12 the test (Sys.word_size = 64) is done at
runtime and the functions below make indirect calls
through the module.
If Sys.word_size is changed to a compile time constant
ocamlopt looks through the module pack and unpack and,
on a 64 bit system, compiles these functions to use
plain "int" addition. *)
This patch would break something much more important: bytecode executables generated by ocamlc are portable across 32 and 64-bit platforms. Therefore, Sys.word_size must be determined at program start-up time, and cannot be a link-time constant. (Otherwise, a bytecode executable generated on a 32-bit machine would run with the wrong word_size on a 64-bit machine.) So, thanks, but no, thanks.
A slightly more acceptable approach would be
let word_size = if 1 lsl 32 = 0 then 32 else 64
As long as the bytecode compiler doesn't do compile-time constant folding, the right value will be computed at program start-up time. On the other hand, ocamlopt folds the constant at compile-time. This won't help with native cross-compilation between 32 and 64-bit platforms, but such cross-compilation is already broken in many other ways.
At any rate, your particular problem should be solved by writing
module M = (val (if 1 lsl 32 <> 0 then (module Int : M) else (module Int32 : M)) : M);;
Original bug ID: 5355
Reporter: jfc
Status: closed (set by @xavierleroy on 2015-12-11T18:04:22Z)
Resolution: not a bug
Priority: normal
Severity: minor
Version: 3.12.1
Category: ~DO NOT USE (was: OCaml general)
Monitored by: "Pascal Cuoq" @ygrek
Bug description
I want to make some code conditional at compile time on the word size, i.e. I don't want the comparison word_size=64 to be done at runtime. In OCaml 3.12 Sys.word_size is initialized by a function call at program startup. The value is not available to the compiler. However, the value will not change from compile to runtime. It should be treated as a constant the same as sizeof() in C.
I have sample code showing where this matters. I attached a diff showing how to make Sys.word_size a constant. Pervasives.max_int and related values should be treated the same way. See caml-list traffic of September 5, 2011 for more background.
Additional information
module type M = sig type t val add : t -> t -> t val of_int : int -> t end;;
module Int : M = struct type t = int let add = (+) let of_int x = x end;;
module M = (val (if Sys.word_size = 64 then (module Int : M) else (module Int32 : M)) : M);;
(* The intent is for the module M to provide the usual int
operations on a 64 bit system and degenerate to boxed
integers and function calls on 32 bit systems. This is
appropriate when an integer of 32-63 bits is required.
In OCaml 3.12 the test (Sys.word_size = 64) is done at
runtime and the functions below make indirect calls
through the module.
If Sys.word_size is changed to a compile time constant
ocamlopt looks through the module pack and unpack and,
on a 64 bit system, compiles these functions to use
plain "int" addition. *)
let f x = M.add (M.of_int x) (M.of_int 1)
let g x = x + Sys.word_size
File attachments
The text was updated successfully, but these errors were encountered: