[MLton] Int Overflow detection

Neophytos Michael nmichael@yahoo.com
Wed, 16 Mar 2005 15:14:33 -0800 (PST)


I wanted to compare mlton's performance with gcc on this platform (cygwin, x86)
and wanted a fair test.  The results are not bad but not great either.  For the
tight loop of the following program:
---------------------------------------
C version:
----------
double foo( double* a, int l )
{
  int i;
  double s = 0.0;

  for (i = 0; i < l; ++i)
    s += a[i];

  return s;
}
ML version:
-----------
fun sum (a, l) =
  let
    fun loop (i, res) =
        if i = l then res
        else loop (i + 1, Unsafe.Array.sub (a, i) + res)
  in
    loop (0, 0.0)
  end
-----------------------------------------------------
gcc generates:
-----------------------------------------------------
L6:
        faddl   (%ecx,%eax,8)
        incl    %eax
        cmpl    %edx, %eax
        jl      L6
-----------------------------------------------------
and mlton:
-----------------------------------------------------
_loop_40:
	cmpl (0+((16*1)+(0*1)))(%ebp),%esi
	je _L_1267
_L_374:
	movl %esi,%edi
	incl %edi
	movl (0+((12*1)+(0*1)))(%ebp),%edx
	fldL (0+(0*1))(%edx,%esi,8)
	faddp %st, %st(1)
	movl %edi,%esi
	jmp _loop_40
-----------------------------------------------------

I am only showing the main loop here (this is with int overflow turned off). 
So it's 4 instructions for gcc versus 9 for mlton.
Some comments:
1. There are two jumps in mlton's case and only one for gcc.  This is because
gcc checks for the exit condition outside the loop and only enters the loop if
it will do at least one iteration.  This saves one jump instruction within the
loop.
2. There are two extra movl instructions that Mlton could easily get rid of by
noticing that the incremented value is not used until the next iteration of the
loop and so it could be done at the end.  But it doesn't seem to do instruction
reordering.
3. There is one instruction movl (0+((12*1)+(0*1)))(%ebp),%edx that seems to be
loop invariant and yet was left inside the loop.  This could be moved out.  I
could be wrong here since I don't really know x86 assembly very well.

I have not timed the loops.  If there is interest I could do that.

Neophytos

--- Stephen Weeks <sweeks@sweeks.com> wrote:
> 
> > Is there a way to turn integer (int) overflow detection off?
> 
> BTW, if you're doing this for performance reasons, I'd be interested
> to hear if you ever see a significant speedup (say, >10%).  I don't
> recall ever seeing one with MLton native on x86.
> 
> _______________________________________________
> MLton mailing list
> MLton@mlton.org
> http://mlton.org/mailman/listinfo/mlton
> 


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/