[MLton-user] Raw conversion of 32 bits into Real32.t
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
>> val convert =
>> val a = Word8Array.tabulate(4, fn _ => 0wx0)
>> fn (w: Word32.word) =>
>> val () = PackWord32Little.update (a, 0, w)
>> val r = PackReal32Little.subArr (a, 0)
> 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
> If we ever want real multithreading in
> MLton, shouldn't we encourage the (perhaps slightly slower) alternative:
> fun convert (w :Word32.word) =
> val a = Word8Array.tabulate(4, fn _ => 0wx0)
> val () = PackWord32Little.update (a, 0, w)
> PackReal32Little.subArr (a, 0)
> 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