| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
local bit32 = {} |
| 8 |
|
| 9 |
local function checkint( name, argidx, x, level ) |
| 10 |
local n = tonumber( x ) |
| 11 |
if not n then |
| 12 |
error( string.format( |
| 13 |
"bad argument #%d to '%s' (number expected, got %s)", |
| 14 |
argidx, name, type( x ) |
| 15 |
), level + 1 ) |
| 16 |
end |
| 17 |
return math.floor( n ) |
| 18 |
end |
| 19 |
|
| 20 |
local function checkint32( name, argidx, x, level ) |
| 21 |
local n = tonumber( x ) |
| 22 |
if not n then |
| 23 |
error( string.format( |
| 24 |
"bad argument #%d to '%s' (number expected, got %s)", |
| 25 |
argidx, name, type( x ) |
| 26 |
), level + 1 ) |
| 27 |
end |
| 28 |
return math.floor( n ) % 0x100000000 |
| 29 |
end |
| 30 |
|
| 31 |
|
| 32 |
function bit32.bnot( x ) |
| 33 |
x = checkint32( 'bnot', 1, x, 2 ) |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
return ( -x - 1 ) % 0x100000000 |
| 38 |
end |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
local logic_and = { |
| 47 |
[0] = { [0] = 0, 0, 0, 0}, |
| 48 |
[1] = { [0] = 0, 1, 0, 1}, |
| 49 |
[2] = { [0] = 0, 0, 2, 2}, |
| 50 |
[3] = { [0] = 0, 1, 2, 3}, |
| 51 |
} |
| 52 |
local logic_or = { |
| 53 |
[0] = { [0] = 0, 1, 2, 3}, |
| 54 |
[1] = { [0] = 1, 1, 3, 3}, |
| 55 |
[2] = { [0] = 2, 3, 2, 3}, |
| 56 |
[3] = { [0] = 3, 3, 3, 3}, |
| 57 |
} |
| 58 |
local logic_xor = { |
| 59 |
[0] = { [0] = 0, 1, 2, 3}, |
| 60 |
[1] = { [0] = 1, 0, 3, 2}, |
| 61 |
[2] = { [0] = 2, 3, 0, 1}, |
| 62 |
[3] = { [0] = 3, 2, 1, 0}, |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
local function comb( name, args, nargs, s, t ) |
| 73 |
for i = 1, nargs do |
| 74 |
args[i] = checkint32( name, i, args[i], 3 ) |
| 75 |
end |
| 76 |
|
| 77 |
local pow = 1 |
| 78 |
local ret = 0 |
| 79 |
for b = 0, 31, 2 do |
| 80 |
local c = s |
| 81 |
for i = 1, nargs do |
| 82 |
c = t[c][args[i] % 4] |
| 83 |
args[i] = math.floor( args[i] / 4 ) |
| 84 |
end |
| 85 |
ret = ret + c * pow |
| 86 |
pow = pow * 4 |
| 87 |
end |
| 88 |
return ret |
| 89 |
end |
| 90 |
|
| 91 |
function bit32.band( ... ) |
| 92 |
return comb( 'band', { ... }, select( '#', ... ), 3, logic_and ) |
| 93 |
end |
| 94 |
|
| 95 |
function bit32.bor( ... ) |
| 96 |
return comb( 'bor', { ... }, select( '#', ... ), 0, logic_or ) |
| 97 |
end |
| 98 |
|
| 99 |
function bit32.bxor( ... ) |
| 100 |
return comb( 'bxor', { ... }, select( '#', ... ), 0, logic_xor ) |
| 101 |
end |
| 102 |
|
| 103 |
function bit32.btest( ... ) |
| 104 |
return comb( 'btest', { ... }, select( '#', ... ), 3, logic_and ) ~= 0 |
| 105 |
end |
| 106 |
|
| 107 |
|
| 108 |
function bit32.extract( n, field, width ) |
| 109 |
n = checkint32( 'extract', 1, n, 2 ) |
| 110 |
field = checkint( 'extract', 2, field, 2 ) |
| 111 |
width = checkint( 'extract', 3, width or 1, 2 ) |
| 112 |
if field < 0 then |
| 113 |
error( "bad argument #2 to 'extract' (field cannot be negative)", 2 ) |
| 114 |
end |
| 115 |
if width <= 0 then |
| 116 |
error( "bad argument #3 to 'extract' (width must be positive)", 2 ) |
| 117 |
end |
| 118 |
if field + width > 32 then |
| 119 |
error( 'trying to access non-existent bits', 2 ) |
| 120 |
end |
| 121 |
|
| 122 |
return math.floor( n / 2^field ) % 2^width |
| 123 |
end |
| 124 |
|
| 125 |
function bit32.replace( n, v, field, width ) |
| 126 |
n = checkint32( 'replace', 1, n, 2 ) |
| 127 |
v = checkint32( 'replace', 2, v, 2 ) |
| 128 |
field = checkint( 'replace', 3, field, 2 ) |
| 129 |
width = checkint( 'replace', 4, width or 1, 2 ) |
| 130 |
if field < 0 then |
| 131 |
error( "bad argument #3 to 'replace' (field cannot be negative)", 2 ) |
| 132 |
end |
| 133 |
if width <= 0 then |
| 134 |
error( "bad argument #4 to 'replace' (width must be positive)", 2 ) |
| 135 |
end |
| 136 |
if field + width > 32 then |
| 137 |
error( 'trying to access non-existent bits', 2 ) |
| 138 |
end |
| 139 |
|
| 140 |
local f = 2^field |
| 141 |
local w = 2^width |
| 142 |
local fw = f * w |
| 143 |
return ( n % f ) + ( v % w ) * f + math.floor( n / fw ) * fw |
| 144 |
end |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
local function checkdisp( name, x ) |
| 150 |
x = checkint( name, 2, x, 3 ) |
| 151 |
return math.min( math.max( -32, x ), 32 ) |
| 152 |
end |
| 153 |
|
| 154 |
function bit32.lshift( x, disp ) |
| 155 |
x = checkint32( 'lshift', 1, x, 2 ) |
| 156 |
disp = checkdisp( 'lshift', disp ) |
| 157 |
|
| 158 |
return math.floor( x * 2^disp ) % 0x100000000 |
| 159 |
end |
| 160 |
|
| 161 |
function bit32.rshift( x, disp ) |
| 162 |
x = checkint32( 'rshift', 1, x, 2 ) |
| 163 |
disp = checkdisp( 'rshift', disp ) |
| 164 |
|
| 165 |
return math.floor( x / 2^disp ) % 0x100000000 |
| 166 |
end |
| 167 |
|
| 168 |
function bit32.arshift( x, disp ) |
| 169 |
x = checkint32( 'arshift', 1, x, 2 ) |
| 170 |
disp = checkdisp( 'arshift', disp ) |
| 171 |
|
| 172 |
if disp <= 0 then |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
return ( x * 2^-disp ) % 0x100000000 |
| 177 |
elseif x < 0x80000000 then |
| 178 |
|
| 179 |
|
| 180 |
return math.floor( x / 2^disp ) |
| 181 |
elseif disp > 31 then |
| 182 |
|
| 183 |
return 0xffffffff |
| 184 |
else |
| 185 |
|
| 186 |
|
| 187 |
return math.floor( x / 2^disp ) + ( 0x100000000 - 2 ^ ( 32 - disp ) ) |
| 188 |
end |
| 189 |
end |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
function bit32.lrotate( x, disp ) |
| 194 |
x = checkint32( 'lrotate', 1, x, 2 ) |
| 195 |
disp = checkint( 'lrotate', 2, disp, 2 ) % 32 |
| 196 |
|
| 197 |
local x = x * 2^disp |
| 198 |
return ( x % 0x100000000 ) + math.floor( x / 0x100000000 ) |
| 199 |
end |
| 200 |
|
| 201 |
function bit32.rrotate( x, disp ) |
| 202 |
x = checkint32( 'rrotate', 1, x, 2 ) |
| 203 |
disp = -checkint( 'rrotate', 2, disp, 2 ) % 32 |
| 204 |
|
| 205 |
local x = x * 2^disp |
| 206 |
return ( x % 0x100000000 ) + math.floor( x / 0x100000000 ) |
| 207 |
end |
| 208 |
|
| 209 |
return bit32 |