Skip to content

Commit 7f47dde

Browse files
authored
corrected to use Lua double convetion (53-bit) (#174)
* corrected to use Lua double convetion (53-bit) * fixed comment
1 parent 95727ca commit 7f47dde

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/Data/Global.lua

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ function hexToRGB(hex)
9494
end
9595

9696
-- NOTE: the LuaJIT bitwise operations we have are not 64-bit
97-
-- so we need to implement them ourselves
97+
-- so we need to implement them ourselves. Lua uses 53-bit doubles.
9898
function OR64(a, b)
9999
-- Split into high and low 32-bit parts
100100
local ah = math.floor(a / 0x100000000)
@@ -107,7 +107,7 @@ function OR64(a, b)
107107
local low = bit.bor(al, bl)
108108

109109
-- Combine the results
110-
return high * 0x100000000 + low
110+
return bit.band(high, 0x7FF) * 0x100000000 + low
111111
end
112112

113113
function AND64(a, b)
@@ -122,7 +122,7 @@ function AND64(a, b)
122122
local low = bit.band(al, bl)
123123

124124
-- Combine the results
125-
return high * 0x100000000 + low
125+
return bit.band(high, 0x7FF) * 0x100000000 + low
126126
end
127127

128128
function XOR64(a, b)
@@ -137,20 +137,25 @@ function XOR64(a, b)
137137
local low = bit.bxor(al, bl)
138138

139139
-- Combine the results
140-
return high * 0x100000000 + low
140+
return bit.band(high, 0x7FF) * 0x100000000 + low
141141
end
142142

143143
function NOT64(a)
144-
-- Split into high and low 32-bit parts
144+
-- Split into high and low 32-bit parts
145145
local ah = math.floor(a / 0x100000000)
146146
local al = a % 0x100000000
147147

148148
-- Perform NOT operation on both parts
149149
local high = bit.bnot(ah)
150150
local low = bit.bnot(al)
151151

152-
-- Combine the results
153-
return high * 0x100000000 + low
152+
-- Convert negative numbers to their unsigned equivalents
153+
if high < 0 then high = high + 0x100000000 end
154+
if low < 0 then low = low + 0x100000000 end
155+
156+
-- Use bit operations to combine the results
157+
-- This avoids potential floating-point precision issues
158+
return bit.band(high, 0x7FF) * 0x100000000 + low
154159
end
155160

156161
ModFlag = { }

0 commit comments

Comments
 (0)