You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Fake a C-union using a typed arrayconstunionU32=newUint32Array(1),unionF32=newFloat32Array(unionU32.buffer);functionfastlog2(x){unionF32[0]=x;letvx_u=unionU32[0];unionU32[0]=(vx_u&0x007FFFFF)|0x3F000000;letmx_f=unionF32[0];return(vx_u*1.1920928955078125e-7)-124.22551499-1.498030302*mx_f-1.72587999/(0.3520887068+mx_f);}
It actually seems to be slightly faster, despite all the overhead involved. Then I figured "I wonder if WASM works... but I don't want to involve a big toolchain just to test that". A bit of searching later, I found Walt, and using your on-line explorer I created this:
constmemory: Memory={'initial': 1};exportfunctionfastlog2(x: f32): f32{// To use arrays simple define a type with array subscript// Assign arrays to the same address to mimic a C-unionconstuf: f32[]=1;constui: i32[]=1;uf[0]=x;
let vx_u : i32=ui[0];ui[0]=(vx_u&8388607)|1056964608;
let mx_f : f32=uf[0];return(vx_u*0.00000011920928955078125)-124.22551499-1.498030302*mx_f-1.72587999/(0.3520887068+mx_f);}
... which I extracted (quite crudely), by typing this in the JS tab of the explorer:
So this week, as an experiment, I ported Paul Mineiro's fast log2 approximation (original C repo here) to plain JavaScript:
It actually seems to be slightly faster, despite all the overhead involved. Then I figured "I wonder if WASM works... but I don't want to involve a big toolchain just to test that". A bit of searching later, I found Walt, and using your on-line explorer I created this:
... which I extracted (quite crudely), by typing this in the JS tab of the explorer:
... the output of which was then copied turned into a quick-and-dirty function like this:
It seems to work! Now I just have to figure out how to benchmark it.
Anyway, I haven't seen any examples discussion unions so far. Perhaps this could work for that purpose?
The text was updated successfully, but these errors were encountered: