-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitfield.lua
More file actions
267 lines (229 loc) · 6 KB
/
bitfield.lua
File metadata and controls
267 lines (229 loc) · 6 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
local _M = {}
_M._NAME = "bitfield"
_M._TYPE = 'module'
local _MT = {}
local nan = 1%0
local inf = 1/0
_M.width = 16
-- checks if a is a bitfield table, or not.
local function isbf(a)
return (type(a)=='table' and a._TYPE=='bitfield') and true or false
end
-- I really shouldn't need this
local function bin(n)
assert(type(n) == 'number', "dub requires a number")
return 2^(n-1)
end
-- Creates and returns a new bitfield. If opt_num is provided, the initial
-- value of the bitfield will be the binary representation of that number,
-- otherwise it will be zero. If opt_width is provided t will be used as
-- the effective bitwidth of the bitfield, otherwise it will default to
-- 16 bits.
--
-- Returns: bitfield
function _M:new(n, opt_width)
n = (type(n)=='number' and n) or 0
local b = {value = n, _TYPE = 'bitfield', width = opt_width or _M.width}
setmetatable(b, _MT)
return b
end
-- returns true if the nth bit is set, otherwise returns false. if a bit
-- outside the range of the bitfield's width is requested, it returns nil
-- instead (which is also logically false)
--
-- Returns: bit
function _M.GET(self, n)
if type(n) ~= 'number' then return _M[n] end
if n < 0 then return nil end
return self.value % (2*bin(n)) >= bin(n)
end
_MT.__index = _M.GET --(self, idx)
-- Sets the nth bit of bf to the truth value of v
--
-- Returns: (nothing)
function _M.SET(b, n, v)
if not isbf(b) then b = _M:new(b) end
assert(n > 0, "Cannot set imaginary bits.")
n = bin(n)
-- should I simply use the normal lua rules of truth?
if type(v) == 'number' then
v = ({[true]=1, [false]=0})[v~=0] -- false = 0, true = !false
end
if v then
if b.value % (2*n) < n then
b.value = b.value + n
end
else -- false/clear
if b.value % (2*n) >= n then
b.value = b.value - n
end
end
return b.value
end
_MT.__newindex = _M.SET --(self, idx, val)
-- Returns a string representing the value contained in the bitfield in
-- binary.
--
-- Returns: string
function _MT.__tostring(self)
local s ={}
for i=self.width,1,-1 do s[#s+1]=({[true]='1', [false]='0'})[self[i]] end
s[#s+1]="b"
return table.concat(s)
end
-- XORs the value of bf with b. Additionally returns the resulting value.
--
-- 0 XOR 0 -> 0
-- 0 XOR 1 -> 1
-- 1 XOR 0 -> 1
-- 1 XOR 1 -> 0
--
-- returns: num
function _M.XOR(a, b)
a = (isbf(a) and a) or _M:new(a)
b = (isbf(b) and b) or _M:new(b)
for i = 1, a.width do
a[i] = a[i] ~= b[i]
end
a.value = a.value % bin(a.width+1)
return a.value
end
-- Negates the value of a. Additionally returns the resulting value
--
-- NOT 1 -> 0
-- NOT 0 -> 1
--
-- returns: num
function _M.NOT(n)
n = (isbf(n) and n) or _M:new(n)
local r = (2^n.width - 1) - n.value
n.value = r % bin(n.width+1)
return n.value
end
-- ORs the value of a with b. Additionally returns the resulting value.
--
-- 0 OR 0 -> 0
-- 0 OR 1 -> 1
-- 1 OR 0 -> 1
-- 1 OR 1 -> 1
--
-- Returns: num
function _M.OR(a,b)
a = (isbf(a) and a) or _M:new(a)
b = (isbf(b) and b) or _M:new(b)
local max = (2^a.width - 1)
local r = max - _M.AND(max - a.value, max - b.value)
a.value = r % bin(a.width+1)
return a.value
end
-- ANDs the value of a with b. Additionally returns the resulting value.
--
-- 0 AND 0 -> 0
-- 0 AND 1 -> 0
-- 1 AND 0 -> 0
-- 1 AND 1 -> 1
--
-- Returns: num
function _M.AND(a,b)
a = (isbf(a) and a) or _M:new(a)
b = (isbf(b) and b) or _M:new(b)
local r = ((a.value+b.value) - _M.XOR(a, b))/2
a.value = r % bin(a.width+1)
return a.value
end
-- NANDs the value of a with b. Additionally returns the resulting value.
--
-- 0 NAND 0 -> 1
-- 0 NAND 1 -> 1
-- 1 NAND 0 -> 1
-- 1 NAND 1 -> 0
--
-- Returns: num
function _M.NAND(a,b)
a = (isbf(a) and a) or _M:new(a)
b = (isbf(b) and b) or _M:new(b)
local r = _M.NOT(_M.AND(a, b))
a.value = r % bin(a.width+1)
return a.value
end
-- NORs the value of a with b. Additionally returns the resulting value.
--
-- 0 NOR 0 -> 1
-- 0 NOR 1 -> 0
-- 1 NOR 0 -> 0
-- 1 NOR 1 -> 0
--
-- Returns: num
function _M.NOR(a,b)
a = (isbf(a) and a) or _M:new(a)
b = (isbf(b) and b) or _M:new(b)
local r = _M.NOT(_M.OR(a, b))
a.value = r % bin(a.width+1)
return a.value
end
-- XNORs the value of a with b. Additionally returns the resulting value.
--
-- 0 XNOR 0 -> 1
-- 0 XNOR 1 -> 0
-- 1 XNOR 0 -> 0
-- 1 XNOR 1 -> 1
--
-- Return: num
function _M.XNOR(a,b)
a = (isbf(a) and a) or _M:new(a)
b = (isbf(b) and b) or _M:new(b)
local r = _M.NOT(_M.XOR(a, b))
a.value = r % bin(a.width+1)
return a.value
end
-- Shifts the value of a by n. Additionally returns the resulting value.
-- Positive values shift left, negative values shift right. if sign-ext
-- is true, then the sign bit will be extended during a right-shift.
--
-- Returns: num
function _M.shift(a, n, sinex)
a = (isbf(a) and a) or _M:new(a)
if n == 0 then return a.value end
local r = a.value
if n > 0 then
for i=1,n do
r = r+r
end
else -- less than 0, shift right
local flr = math.floor
local s = a[a.width]
for i=1,math.abs(n) do
r = flr(r/2)
end
if sinex then
a.value = r
a[a.width] = s
r = a.value
end
end
a.value = math.floor(r % bin(a.width+1))
return a.value
end
-- Rolls the value of a by n. Additionally returns the resulting value.
-- Positive values roll left, negative values roll right.
-- A roll is similar to a shift, however values that fall of one end
-- simply return to the other end. thus rolling 100b left one would be
-- 001b. and again would be 010b
-- (thanks to GeDaMo)
-- Returns: num
function _M.roll(a, n)
a = (isbf(a) and a) or _M:new(a)
if n == 0 then return a.value end
if (a.value >= 2^a.width-1) then
--print(a.value, a.value)
else
a.value = (a.value * 2^(n % a.width)) % (2^a.width-1)
end
-- a.value = math.floor(r % bin(a.width)+1)
return a.value
end
-------------------------------------------------------------------------
-- MODULE TAIL
-- in 5.1.x the custom is to set a global equal to the module name, in all others this ill-behaved.
if _VERSION == "Lua 5.1" then _G[_M._NAME]=_M end
return _M