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

Matthew Fluet fluet at tti-c.org
Mon Jan 22 15:32:40 PST 2007


Wesley W. Terpstra wrote:
> 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?

You are correct; there would be a race on the array if used in 
multi-threaded code.

 > 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.

It probably would be worth seeing the overhead of the array creation.




More information about the MLton-user mailing list