O'Caml Patches

This is a web page for a patch for the O'Caml compiler which the O'Caml maintainers have chosen not to apply to their distribution.

The purpose of this patch is to add a new function to the O'Caml standard library:


        val round_to_int : float -> int
        (** Round the given floating-point number to an integer
            using the current rounding mode.
            The result is unspecified if it falls outside the
            range of representable integers. *)

Yes, a very similar result could be achieved using:


        let round_of_int f = int_of_float (f +. 0.5)

but unfortunately this suffers from a serious performance hit all Pentium class CPUs as explained in this post to the O'Caml mailing list.

Once you have patched the compiler you can test it using this test program. I have have found that the round_to_int provided by may patch runs 2.5 times faster than the niave implementation above on Pentium 3 and 2 times faster on P4.

The patch currently only works for x86 and PowerPC. If you need support for other architectures and you can provide me with access to a machine, please contact me at the email address below.


Download the patch

The current patch was against CVS on Feb 22nd, 2005. It also applys cleanly against O'Caml 3.08.2. If you have trouble applying this patch to a later version of O'Caml, contact me at the email address below.

Contact : Erik de Castro Lopo

This page has been accessed counter.gif times.