added lg function (floor(log2(x))