[MLton-user] Raw conversion of 32 bits into Real32.t

Wesley W. Terpstra wesley at terpstra.ca
Mon Jan 22 15:22:07 PST 2007


On Jan 22, 2007, at 6:32 PM, Matthew Fluet wrote:
> You can do it with the Basis Library via the PackWord<N> and  
> PackReal<N> structures.  I'll assume you're on little endian intel  
> hardware:
>
> val convert =
>   let
>     val a = Word8Array.tabulate(4, fn _ => 0wx0)
>   in
>     fn (w: Word32.word) =>
>       let
>         val () = PackWord32Little.update (a, 0, w)
>         val r = PackReal32Little.subArr (a, 0)
>       in
>         r
>       end
>   end

Why do you put the array outside of the function scope? I've seen  
this done a lot in MLton code, and it strikes me as being not thread- 
safe. Even with the signal driven user-space threads MLton already  
has, wouldn't the above be a bug? If we ever want real multithreading  
in MLton, shouldn't we encourage the (perhaps slightly slower)  
alternative:

fun convert (w :Word32.word) =
   let
     val a = Word8Array.tabulate(4, fn _ => 0wx0)
     val () = PackWord32Little.update (a, 0, w)
   in
     PackReal32Little.subArr (a, 0)
   end

If this construct is significantly slower, then I think MLton should  
be what gets optimized, not the user code.

Or is there another reason for this and I'm completely confused? :-)




More information about the MLton-user mailing list