mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 17:23:45 +01:00
Localize global fields
This commit is contained in:
parent
4026f6dd0d
commit
35081f3e4b
15
binary.lua
15
binary.lua
@ -1,5 +1,6 @@
|
|||||||
-- Localize globals
|
-- Localize globals
|
||||||
local assert, math = assert, math
|
local assert, math_huge, math_frexp, math_floor
|
||||||
|
= assert, math.huge, math.frexp, math.floor
|
||||||
|
|
||||||
-- Set environment
|
-- Set environment
|
||||||
local _ENV = {}
|
local _ENV = {}
|
||||||
@ -30,7 +31,7 @@ function read_float(read_byte, double)
|
|||||||
mantissa = (mantissa + byte_2) / 0x80
|
mantissa = (mantissa + byte_2) / 0x80
|
||||||
if exponent == 0xFF then
|
if exponent == 0xFF then
|
||||||
if mantissa == 0 then
|
if mantissa == 0 then
|
||||||
return sign * math.huge
|
return sign * math_huge
|
||||||
end
|
end
|
||||||
-- Differentiating quiet and signalling nan is not possible in Lua, hence we don't have to do it
|
-- Differentiating quiet and signalling nan is not possible in Lua, hence we don't have to do it
|
||||||
-- HACK ((0/0)^1) yields nan, 0/0 yields -nan
|
-- HACK ((0/0)^1) yields nan, 0/0 yields -nan
|
||||||
@ -67,7 +68,7 @@ end
|
|||||||
function write_uint(write_byte, uint, bytes)
|
function write_uint(write_byte, uint, bytes)
|
||||||
for _ = 1, bytes do
|
for _ = 1, bytes do
|
||||||
write_byte(uint % 0x100)
|
write_byte(uint % 0x100)
|
||||||
uint = math.floor(uint / 0x100)
|
uint = math_floor(uint / 0x100)
|
||||||
end
|
end
|
||||||
assert(uint == 0)
|
assert(uint == 0)
|
||||||
end
|
end
|
||||||
@ -80,16 +81,16 @@ function write_float(write_byte, number, on_write, double)
|
|||||||
number = -number
|
number = -number
|
||||||
sign = 0x80
|
sign = 0x80
|
||||||
end
|
end
|
||||||
local mantissa, exponent = math.frexp(number)
|
local mantissa, exponent = math_frexp(number)
|
||||||
exponent = exponent + 127
|
exponent = exponent + 127
|
||||||
if exponent > 1 then
|
if exponent > 1 then
|
||||||
-- TODO ensure this deals properly with subnormal numbers
|
-- TODO ensure this deals properly with subnormal numbers
|
||||||
mantissa = mantissa * 2 - 1
|
mantissa = mantissa * 2 - 1
|
||||||
exponent = exponent - 1
|
exponent = exponent - 1
|
||||||
end
|
end
|
||||||
local sign_byte = sign + math.floor(exponent / 2)
|
local sign_byte = sign + math_floor(exponent / 2)
|
||||||
mantissa = mantissa * 0x80
|
mantissa = mantissa * 0x80
|
||||||
local exponent_byte = (exponent % 2) * 0x80 + math.floor(mantissa)
|
local exponent_byte = (exponent % 2) * 0x80 + math_floor(mantissa)
|
||||||
mantissa = mantissa % 1
|
mantissa = mantissa % 1
|
||||||
local mantissa_bytes = {}
|
local mantissa_bytes = {}
|
||||||
-- TODO ensure this check is proper
|
-- TODO ensure this check is proper
|
||||||
@ -102,7 +103,7 @@ function write_float(write_byte, number, on_write, double)
|
|||||||
local len = double and 6 or 2
|
local len = double and 6 or 2
|
||||||
for index = len, 1, -1 do
|
for index = len, 1, -1 do
|
||||||
mantissa = mantissa * 0x100
|
mantissa = mantissa * 0x100
|
||||||
mantissa_bytes[index] = math.floor(mantissa)
|
mantissa_bytes[index] = math_floor(mantissa)
|
||||||
mantissa = mantissa % 1
|
mantissa = mantissa % 1
|
||||||
end
|
end
|
||||||
assert(mantissa == 0)
|
assert(mantissa == 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user