Improve bit32 polyfill
This commit is contained in:
parent
31d448971a
commit
7eb9908f1a
|
@ -12,19 +12,20 @@ local function mask(n)
|
||||||
end
|
end
|
||||||
|
|
||||||
function bit.extract(n, field, width)
|
function bit.extract(n, field, width)
|
||||||
local r = trim(field)
|
local w = width or 1
|
||||||
local f = width
|
assert(w > 0, "width must be positive")
|
||||||
r = (r >> f) & mask(width)
|
assert(field + w < LUA_NBITS and field + w >= 0, "trying to access non-existent bits")
|
||||||
return r
|
local m = trim(n)
|
||||||
|
return m >> field & mask(w)
|
||||||
end
|
end
|
||||||
|
|
||||||
function bit.replace(n, v, field, width)
|
function bit.replace(n, v, field, width)
|
||||||
local r = trim(v);
|
local w = width or 1
|
||||||
local v = trim(field);
|
assert(w > 0, "width must be positive")
|
||||||
local f = width
|
assert(field + w < LUA_NBITS and field + w >= 0, "trying to access non-existent bits")
|
||||||
local m = mask(width);
|
local m = trim(n)
|
||||||
r = (r & ~(m << f)) | ((v & m) << f);
|
local x = v & mask(width);
|
||||||
return r
|
return m & ~(mask(w) << field) | (x << field)
|
||||||
end
|
end
|
||||||
|
|
||||||
return bit
|
return bit
|
Loading…
Reference in New Issue