[MLton-user] power of 2 optimisation

Tom 7 twm@andrew.cmu.edu
Thu, 17 Jul 2003 16:04:37 -0400 (EDT)


It does, for div at least.

To comply with the semantics of div in SML (or C), one has to do a little
extra trickery, because shifting negative numbers rounds in the wrong
direction. So it's not just a shift--but mlton does a clever job of this,
even better than gcc.

If you don't care about negative numbers, you should probably use Word32
since then you won't have bounds checks, either!


By the way, here's a trick to see the assembly code that mlton generates
for something like this. (Usually one of two things happens: The code is
incredibly complicated and it's impossible to find the relevant parts, or,
mlton optimizes away the code you want to see!)

(* define ffi functions. mlton's optimizer won't touch
   these *)
val const = _ffi "x" : int ;
val out = _ffi "y" : int -> unit ;

(* make the computation depend on an ffi symbol
   and be needed to call another ffi symbol *)
val _ = out (const div 16)

Now compile like this:

mlton -keep g div.sml

Check out div.0.S and look for "call y".


 - Tom

[ NEW! : http://tom7.org/       ]
[ OLD! : http://fonts.tom7.com/ ]


> Hello,
>
> Does mlton optimise for div/mod by constant powers of 2?
> that is:
>
>  val const = 16
>  fun div16 x = x div const
>
> would compile to a bit shift by 4 bits rather than a division.
> (mlton's excellent profiler tells me I'm spending 1/4 of my
> time in div and mod, despite my having selected powers of 2s)
>
> If it does not, maybe an interesting question is whether it
> should or is it better that the programmer uses shifts
> explicitely?
>
> --
> franck@nenie.org


-------------------------------------------------------
This SF.net email is sponsored by: VM Ware
With VMware you can run multiple operating systems on a single machine.
WITHOUT REBOOTING! Mix Linux / Windows / Novell virtual machines at the
same time. Free trial click here: http://www.vmware.com/wl/offer/345/0
_______________________________________________
MLton-user mailing list
MLton-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mlton-user