[MLton] Performance of Real.toInt

Vesa Karvonen vesa.a.j.k at gmail.com
Sun Oct 26 01:17:26 PST 2008


On Fri, Oct 24, 2008 at 10:50 PM, Ryan Newton <rrnewton at gmail.com> wrote:
> [...] It's a
> computer vision app where the core computation involves updating
> histograms -- picking a location in some space (inexact) and then
> discretizing (truncating a floating point number).
>
> The truncation in the source language happens by converting the floats
> to ints.

Just to clarify, is the final result of "truncating a floating point
number" a floating point number or an integer?

> Under MLton I generate code like this:
>
>  (Real64.toInt IEEEReal.TO_ZERO (var_tmpsmp_77))
>
> But it performs very poorly.  I haven't researched this, but if I had
> to guess, I'd bet this is because mlton is implementing some more
> semantically meaningful notion than C casts.

An excellent guess!

> Nevertheless, is there
> any inexpensive way to ape the behavior one gets from (int)x in C?

Have you peeked into the real/real.sml source file in MLton's basis
library implementation?  The implementation of Real.toInt uses a
family of toInt<N>Unsafe functions, that do not set the rounding mode
or check that the floating point number is in the range of the integer
type.  One could perhaps extend the MLton.Real structure
(http://mlton.org/MLtonReal) to expose those functions.  You could
then implement the conversion in terms of the unsafe functions.

-Vesa Karvonen



More information about the MLton mailing list