Skip to content
This repository was archived by the owner on Oct 21, 2025. It is now read-only.

Commit ef43a2a

Browse files
Merge pull request #10 from Nostrademous/BinaryLogic
make binary operations take arbitrary number of values
2 parents 51e13e1 + 8b24929 commit ef43a2a

File tree

1 file changed

+74
-35
lines changed

1 file changed

+74
-35
lines changed

src/Data/Global.lua

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -96,49 +96,88 @@ end
9696
-- NOTE: the LuaJIT bitwise operations we have are not 64-bit
9797
-- so we need to implement them ourselves. Lua uses 53-bit doubles.
9898
HIGH_MASK_53 = 0x1FFFFF
99-
function OR64(a, b)
100-
-- Split into high and low 32-bit parts
101-
local ah = math.floor(a / 0x100000000)
102-
local al = a % 0x100000000
103-
local bh = math.floor(b / 0x100000000)
104-
local bl = b % 0x100000000
99+
function OR64(...)
100+
local args = {...}
101+
if #args < 2 then
102+
return args[1] or 0
103+
end
105104

106-
-- Perform OR operation on both parts
107-
local high = bit.bor(ah, bh)
108-
local low = bit.bor(al, bl)
105+
-- Start with first value
106+
local result = args[1]
109107

110-
-- Combine the results
111-
return bit.band(high, HIGH_MASK_53) * 0x100000000 + low
108+
-- OR with each subsequent value
109+
for i = 2, #args do
110+
-- Split into high and low 32-bit parts
111+
local ah = math.floor(result / 0x100000000)
112+
local al = result % 0x100000000
113+
local bh = math.floor(args[i] / 0x100000000)
114+
local bl = args[i] % 0x100000000
115+
116+
-- Perform OR operation on both parts
117+
local high = bit.bor(ah, bh)
118+
local low = bit.bor(al, bl)
119+
120+
-- Combine the results
121+
result = bit.band(high, HIGH_MASK_53) * 0x100000000 + low
122+
end
123+
124+
return result
112125
end
113126

114-
function AND64(a, b)
115-
-- Split into high and low 32-bit parts
116-
local ah = math.floor(a / 0x100000000)
117-
local al = a % 0x100000000
118-
local bh = math.floor(b / 0x100000000)
119-
local bl = b % 0x100000000
120-
121-
-- Perform AND operation on both parts
122-
local high = bit.band(ah, bh)
123-
local low = bit.band(al, bl)
124-
125-
-- Combine the results
126-
return bit.band(high, HIGH_MASK_53) * 0x100000000 + low
127+
function AND64(...)
128+
local args = {...}
129+
if #args < 2 then
130+
return args[1] or 0
131+
end
132+
133+
-- Start with first value
134+
local result = args[1]
135+
136+
-- AND with each subsequent value
137+
for i = 2, #args do
138+
-- Split into high and low 32-bit parts
139+
local ah = math.floor(result / 0x100000000)
140+
local al = result % 0x100000000
141+
local bh = math.floor(args[i] / 0x100000000)
142+
local bl = args[i] % 0x100000000
143+
144+
-- Perform AND operation on both parts
145+
local high = bit.band(ah, bh)
146+
local low = bit.band(al, bl)
147+
148+
-- Combine the results
149+
result = bit.band(high, HIGH_MASK_53) * 0x100000000 + low
150+
end
151+
152+
return result
127153
end
128154

129-
function XOR64(a, b)
130-
-- Split into high and low 32-bit parts
131-
local ah = math.floor(a / 0x100000000)
132-
local al = a % 0x100000000
133-
local bh = math.floor(b / 0x100000000)
134-
local bl = b % 0x100000000
155+
function XOR64(...)
156+
local args = {...}
157+
if #args < 2 then
158+
return args[1] or 0
159+
end
135160

136-
-- Perform XOR operation on both parts
137-
local high = bit.bxor(ah, bh)
138-
local low = bit.bxor(al, bl)
161+
-- Start with first value
162+
local result = args[1]
139163

140-
-- Combine the results
141-
return bit.band(high, HIGH_MASK_53) * 0x100000000 + low
164+
-- XOR with each subsequent value
165+
for i = 2, #args do
166+
-- Split into high and low 32-bit parts
167+
local ah = math.floor(result / 0x100000000)
168+
local al = result % 0x100000000
169+
local bh = math.floor(args[i] / 0x100000000)
170+
local bl = args[i] % 0x100000000
171+
172+
-- Perform XOR operation on both parts
173+
local high = bit.bxor(ah, bh)
174+
local low = bit.bxor(al, bl)
175+
176+
-- Combine the results
177+
result = bit.band(high, HIGH_MASK_53) * 0x100000000 + low
178+
end
179+
180+
return result
142181
end
143182

144183
function NOT64(a)

0 commit comments

Comments
 (0)