1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
if not modules then modules = { } end modules ['l-bit32'] = {
version = 1.001,
license = "the same as regular Lua",
source = "bitwise.lua, v 1.24 2014/12/26 17:20:53 roberto",
comment = "drop-in for bit32, adapted a bit by Hans Hagen",
}
-- Lua 5.3 has bitwise operators built in but code meant for 5.2 can expect the
-- bit32 library to be present. For the moment (and maybe forever) we will ship
-- the bit32 library as part of LuaTeX bit just in case it is missing, here is a
-- drop-in. The code is an adapted version of code by Roberto. The Luajit variant
-- is a mixture of mapping and magic.
if bit32 then
-- lua 5.2: we're okay
elseif utf8 then
-- lua 5.3: bitwise.lua, v 1.24 2014/12/26 17:20:53 roberto
bit32 = load ( [[
local select = select -- instead of: arg = { ... }
bit32 = {
bnot = function (a)
return ~a & 0xFFFFFFFF
end,
band = function (x, y, z, ...)
if not z then
return ((x or -1) & (y or -1)) & 0xFFFFFFFF
else
local res = x & y & z
for i=1,select("#",...) do
res = res & select(i,...)
end
return res & 0xFFFFFFFF
end
end,
bor = function (x, y, z, ...)
if not z then
return ((x or 0) | (y or 0)) & 0xFFFFFFFF
else
local res = x | y | z
for i=1,select("#",...) do
res = res | select(i,...)
end
return res & 0xFFFFFFFF
end
end,
bxor = function (x, y, z, ...)
if not z then
return ((x or 0) ~ (y or 0)) & 0xFFFFFFFF
else
local res = x ~ y ~ z
for i=1,select("#",...) do
res = res ~ select(i,...)
end
return res & 0xFFFFFFFF
end
end,
btest = function (x, y, z, ...)
if not z then
return (((x or -1) & (y or -1)) & 0xFFFFFFFF) ~= 0
else
local res = x & y & z
for i=1,select("#",...) do
res = res & select(i,...)
end
return (res & 0xFFFFFFFF) ~= 0
end
end,
lshift = function (a, b)
return ((a & 0xFFFFFFFF) << b) & 0xFFFFFFFF
end,
rshift = function (a, b)
return ((a & 0xFFFFFFFF) >> b) & 0xFFFFFFFF
end,
arshift = function (a, b)
a = a & 0xFFFFFFFF
if b <= 0 or (a & 0x80000000) == 0 then
return (a >> b) & 0xFFFFFFFF
else
return ((a >> b) | ~(0xFFFFFFFF >> b)) & 0xFFFFFFFF
end
end,
lrotate = function (a ,b)
b = b & 31
a = a & 0xFFFFFFFF
a = (a << b) | (a >> (32 - b))
return a & 0xFFFFFFFF
end,
rrotate = function (a, b)
b = -b & 31
a = a & 0xFFFFFFFF
a = (a << b) | (a >> (32 - b))
return a & 0xFFFFFFFF
end,
extract = function (a, f, w)
return (a >> f) & ~(-1 << (w or 1))
end,
replace = function (a, v, f, w)
local mask = ~(-1 << (w or 1))
return ((a & ~(mask << f)) | ((v & mask) << f)) & 0xFFFFFFFF
end,
}
]] )
elseif bit then
-- luajit (for now)
bit32 = load ( [[
local band, bnot, rshift, lshift = bit.band, bit.bnot, bit.rshift, bit.lshift
bit32 = {
arshift = bit.arshift,
band = band,
bnot = bnot,
bor = bit.bor,
bxor = bit.bxor,
btest = function(...)
return band(...) ~= 0
end,
extract = function(a,f,w)
return band(rshift(a,f),2^(w or 1)-1)
end,
lrotate = bit.rol,
lshift = lshift,
replace = function(a,v,f,w)
local mask = 2^(w or 1)-1
return band(a,bnot(lshift(mask,f)))+lshift(band(v,mask),f)
end,
rrotate = bit.ror,
rshift = rshift,
}
]] )
else
-- hope for the best or fail
-- bit32 = require("bit32")
xpcall(function() local _, t = require("bit32") if t then bit32 = t end return end,function() end)
end
return bit32 or false
|