Merge pull request #799 from Guldoman/bit32_polyfill

Improve bit32 polyfill
This commit is contained in:
Adam 2022-01-18 21:06:48 -05:00 committed by GitHub
commit 3b3e41c095
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 12 deletions

View File

@ -11,20 +11,26 @@ local function mask(n)
return (~((ALLONES << 1) << ((n) - 1)))
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)
local r = trim(field)
local f = width
r = (r >> f) & mask(width)
return r
local w = width or 1
check_args(field, w)
local m = trim(n)
return m >> field & mask(w)
end
function bit.replace(n, v, field, width)
local r = trim(v);
local v = trim(field);
local f = width
local m = mask(width);
r = (r & ~(m << f)) | ((v & m) << f);
return r
local w = width or 1
check_args(field, w)
local m = trim(n)
local x = v & mask(width);
return m & ~(mask(w) << field) | (x << field)
end
return bit
return bit

View File

@ -32,3 +32,5 @@ end }
table.pack = table.pack or pack or function(...) return {...} end
table.unpack = table.unpack or unpack
bit32 = bit32 or require "core.bit"

View File

@ -1,6 +1,5 @@
local syntax = require "core.syntax"
local common = require "core.common"
local bit32 = require "core.bit"
local tokenizer = {}