Debian Codegraph @

extensions/Scribunto/includes/Engines/LuaCommon/lualib/bit32.lua

raw symbols SCIP
1 ---
2 -- An implementation of the lua 5.2 bit32 library, in pure Lua
3
4 -- Note that in Lua, "x % n" is defined such that will always return a number
5 -- between 0 and n-1 for positive n. We take advantage of that a lot here.
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 -- In two's complement, -x = not(x) + 1
36 -- So not(x) = -x - 1
37 return ( -x - 1 ) % 0x100000000
38 end
39
40
41 ---
42 -- Logic tables for and/or/xor. We do pairs of bits here as a tradeoff between
43 -- table space and speed. If you change the number of bits, also change the
44 -- constants 2 and 4 in comb() below, and the initial value in bit32.band and
45 -- bit32.btest
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 -- @param name string Function name
67 -- @param args table Function args
68 -- @param nargs number Arg count
69 -- @param s number Start value, 0-3
70 -- @param t table Logic table
71 -- @return number result
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 -- For the shifting functions, anything over 32 is the same as 32
148 -- and limiting to 32 prevents overflow/underflow
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 -- Non-positive displacement == left shift
174 -- (since exponent is non-negative, the multipication can never result
175 -- in a fractional part)
176 return ( x * 2^-disp ) % 0x100000000
177 elseif x < 0x80000000 then
178 -- High bit is 0 == right shift
179 -- (since exponent is positive, the division will never increase x)
180 return math.floor( x / 2^disp )
181 elseif disp > 31 then
182 -- Shifting off all bits
183 return 0xffffffff
184 else
185 -- 0x100000000 - 2 ^ ( 32 - disp ) creates a number with the high disp
186 -- bits set. So shift right then add that number.
187 return math.floor( x / 2^disp ) + ( 0x100000000 - 2 ^ ( 32 - disp ) )
188 end
189 end
190
191 -- For the rotation functions, disp works mod 32.
192 -- Note that lrotate( x, disp ) == rrotate( x, -disp ).
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