[MLton] Number theory and MLton C FFI

Stephen Weeks MLton@mlton.org
Thu, 21 Oct 2004 19:59:05 -0700


> > 	call WordU32_toWord64
> > 	call WordU32_toWord64
> > 	call WordU64_rshift
> > 	call WordU64_toWord32
> > 	call WordU64_toWord32
> > 	shrl $0x1,%eax
> > 	addl %ebx,%eax
> > 	movl %eax,%esi
> > 	andl $0x7FFFFFFF,%esi
> > 	shrl $0x1F,%eax
> > 	addl %esi,%eax
> 
> ... where's the multiply?

Whoops.  I missed that in editing the asm.  It is between the second
and third lines.  You can compile -stop g and see for yourself.

> Thank you; would you mind if I copied it?

Not at all.

> fun first_factor x =
>   let
>     open Word64
>     fun helper i =
>       if (x mod i) = 0w0 then i else
>       helper (i+0w1)
>   in
>     helper 0w2
>   end ;
> 
> val x = first_factor 0w4611686014132420609 ;
> print (Word64.toString x ^ "\n");;
> 
> Shouldn't x be computed at compile-time? (a bit extreme perhaps ;)

MLton won't unroll loops, so x will not be computed.  Of course, if
you're generating SML, you can always unroll the loop in your code
generator and then MLton will simplify away all word operations whose
arguments are constants.

> Another thing that matters for fast number theory functions is: SIMD.
> I imagine automatic use of SIMD in MLton is a long ways off, but a good
> programmer can make intrinsics go a long way.
> 
> Maybe I should try a source dive into MLton.
> If there was an SSE2 : SIMD, MMX : SIMD, AltiVec : SIMD and that 64bit 
> mul optimization, I could perhaps achieve everything I need in SML.

Hopefully our x86 codegen expert will comment on this :-).  Matthew? 

> Also, so far as I can tell, I can't write a library under MLton and
> expect people to be able to use it from C without forcing MLton on
> them. Are there any plans for MLton's whole-program optimization to
> use a different entry point than main?

You can export SML functions to C using _export, so the support for
different entry points is there.  The only thing we haven't done is
add the ability for the main function to be in C.  Nothing deep, it
just needs to be figured out where to initialize the runtime,
command-line args, etc.  An inspired user could probably do it in a
day or two.