|Anonymous | Login | Signup for a new account||2016-02-08 13:22 CET|
|Main | My View | View Issues | Change Log | Roadmap|
|View Issue Details|
|ID||Project||Category||View Status||Date Submitted||Last Update|
|0005355||OCaml||OCaml general||public||2011-09-06 00:03||2015-12-11 19:04|
|Status||closed||Resolution||no change required|
|Target Version||Fixed in Version|
|Summary||0005355: Sys.word_size and related fields should be constants|
|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
|Tags||No tags attached.|
|Attached Files||word_size.diff [^] (1,618 bytes) 2011-09-06 00:03 [Show Content]|
edited on: 2011-09-06 15:47
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);;
In my proposed approach, please replace "1 lsl 32" with "1 lsl 31" to avoid running into "unspecified behavior" territory.
In the absence of further discussion, I'm moving this PR to "no change required".
|2011-09-06 00:03||jfc||New Issue|
|2011-09-06 00:03||jfc||File Added: word_size.diff|
|2011-09-06 15:46||xleroy||Note Added: 0006114|
|2011-09-06 15:46||xleroy||Status||new => feedback|
|2011-09-06 15:47||xleroy||Note Edited: 0006114|
|2012-03-14 10:45||xleroy||Note Added: 0007073|
|2012-03-14 10:45||xleroy||Status||feedback => resolved|
|2012-03-14 10:45||xleroy||Resolution||open => no change required|
|2015-12-11 19:04||xleroy||Status||resolved => closed|
|Copyright © 2000 - 2011 MantisBT Group|