Since this is an old topic, I'm not quite sure somebody is still interested in this. But I noticed that we are lacking a good log2 in Lua that can work with plain integers.
I wrote (a fast) one that also takes an (optional) multiplicator factor as second parameter. Using this mult parameter, there is less loss of parts behind the comma in the result.
Anyway, here it is ...
function min(a, b)
return a < b and a or b;
end
function max(a, b)
return a > b and a or b;
end
-- returns : mult * log2(x)
-- mult is optional, it defaults to 1
-- safe for x between 0 and 2097152
function log2(x, mult)
local mult = mult or 1;
local x = min(max(x, 0), 2097152);
local result = 0;
x = bitshl(x, 10);
while x >= 2048 do
result = result + 1048576;
x = bitshru(x, 1);
end
local fraction = 1048576;
while fraction >= 1 do
fraction = bitshru(fraction, 1);
x = bitshru(x * x, 10);
if x >= 2048 then
result = result + fraction;
x = bitshru(x, 1);
end
end
return bitshru(mult * result, 20);
end
Using this log2, the nice functions fbonomi provided us with can now be written fairly trivial as ...
function msec_2_tv96(milliseconds)
return log2(milliseconds, -96) + 957;
end
function sec_2_tv96(seconds)
return log2(seconds, -96);
end
function iso_2_sv96(iso)
return log2(iso, 96) - 157;
end
A little warning ...
I'm still anxiously waiting for my new little A480 to arrive, so I had no chance to actually test it in real live yet.
I hope I can do that tomorrow.