Merge pull request #799 from Guldoman/bit32_polyfill
Improve bit32 polyfill
This commit is contained in:
commit
3b3e41c095
|
@ -11,20 +11,26 @@ local function mask(n)
|
||||||
return (~((ALLONES << 1) << ((n) - 1)))
|
return (~((ALLONES << 1) << ((n) - 1)))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function check_args(field, width)
|
||||||
|
assert(field >= 0, "field cannot be negative")
|
||||||
|
assert(width > 0, "width must be positive")
|
||||||
|
assert(field + width < LUA_NBITS and field + width >= 0,
|
||||||
|
"trying to access non-existent bits")
|
||||||
|
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
|
check_args(field, w)
|
||||||
r = (r >> f) & mask(width)
|
local m = trim(n)
|
||||||
return r
|
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);
|
check_args(field, w)
|
||||||
local f = width
|
local m = trim(n)
|
||||||
local m = mask(width);
|
local x = v & mask(width);
|
||||||
r = (r & ~(m << f)) | ((v & m) << f);
|
return m & ~(mask(w) << field) | (x << field)
|
||||||
return r
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return bit
|
return bit
|
|
@ -32,3 +32,5 @@ end }
|
||||||
|
|
||||||
table.pack = table.pack or pack or function(...) return {...} end
|
table.pack = table.pack or pack or function(...) return {...} end
|
||||||
table.unpack = table.unpack or unpack
|
table.unpack = table.unpack or unpack
|
||||||
|
|
||||||
|
bit32 = bit32 or require "core.bit"
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
local syntax = require "core.syntax"
|
local syntax = require "core.syntax"
|
||||||
local common = require "core.common"
|
local common = require "core.common"
|
||||||
local bit32 = require "core.bit"
|
|
||||||
|
|
||||||
local tokenizer = {}
|
local tokenizer = {}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue