-
Notifications
You must be signed in to change notification settings - Fork 9
/
binary.lua
236 lines (203 loc) · 6.2 KB
/
binary.lua
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
-- Localize globals
local assert, math_huge, math_frexp, math_floor
= assert, math.huge, math.frexp, math.floor
local positive_nan, negative_nan = modlib.math.positive_nan, modlib.math.negative_nan
-- Set environment
local _ENV = {}
setfenv(1, _ENV)
-- All little endian
--+ Reads an IEEE 754 single-precision floating point number (f32)
function read_single(read_byte)
-- First read the mantissa
local mantissa = read_byte() / 0x100
mantissa = (mantissa + read_byte()) / 0x100
-- Second and first byte in big endian: last bit of exponent + 7 bits of mantissa, sign bit + 7 bits of exponent
local exponent_byte = read_byte()
local sign_byte = read_byte()
local sign = 1
if sign_byte >= 0x80 then
sign = -1
sign_byte = sign_byte - 0x80
end
local exponent = sign_byte * 2
if exponent_byte >= 0x80 then
exponent = exponent + 1
exponent_byte = exponent_byte - 0x80
end
mantissa = (mantissa + exponent_byte) / 0x80
if exponent == 0xFF then
if mantissa == 0 then
return sign * math_huge
end
-- Differentiating quiet and signalling nan is not possible in Lua, hence we don't have to do it
return sign == 1 and positive_nan or negative_nan
end
assert(mantissa < 1)
if exponent == 0 then
-- subnormal value
return sign * 2^-126 * mantissa
end
return sign * 2 ^ (exponent - 127) * (1 + mantissa)
end
--+ Reads an IEEE 754 double-precision floating point number (f64)
function read_double(read_byte)
-- First read the mantissa
local mantissa = 0
for _ = 1, 6 do
mantissa = (mantissa + read_byte()) / 0x100
end
-- Second and first byte in big endian: last 4 bits of exponent + 4 bits of mantissa; sign bit + 7 bits of exponent
local exponent_byte = read_byte()
local sign_byte = read_byte()
local sign = 1
if sign_byte >= 0x80 then
sign = -1
sign_byte = sign_byte - 0x80
end
local exponent = sign_byte * 0x10
local mantissa_bits = exponent_byte % 0x10
exponent = exponent + (exponent_byte - mantissa_bits) / 0x10
mantissa = (mantissa + mantissa_bits) / 0x10
if exponent == 0x7FF then
if mantissa == 0 then
return sign * math_huge
end
-- Differentiating quiet and signalling nan is not possible in Lua, hence we don't have to do it
return sign == 1 and positive_nan or negative_nan
end
assert(mantissa < 1)
if exponent == 0 then
-- subnormal value
return sign * 2^-1022 * mantissa
end
return sign * 2 ^ (exponent - 1023) * (1 + mantissa)
end
--+ Reads doubles (f64) or floats (f32)
--: double reads an f64 if true, f32 otherwise
function read_float(read_byte, double)
return (double and read_double or read_single)(read_byte)
end
function read_uint(read_byte, bytes)
local factor = 1
local uint = 0
for _ = 1, bytes do
uint = uint + read_byte() * factor
factor = factor * 0x100
end
return uint
end
function read_int(read_byte, bytes)
local uint = read_uint(read_byte, bytes)
local max = 0x100 ^ bytes
if uint >= max / 2 then
return uint - max
end
return uint
end
function write_uint(write_byte, uint, bytes)
for _ = 1, bytes do
write_byte(uint % 0x100)
uint = math_floor(uint / 0x100)
end
assert(uint == 0)
end
function write_int(write_byte, int, bytes)
local max = 0x100 ^ bytes
if int < 0 then
assert(-int <= max / 2)
int = max + int
else
assert(int < max / 2)
end
return write_uint(write_byte, int, bytes)
end
function write_single(write_byte, number)
if number ~= number then -- nan: all ones
for _ = 1, 4 do write_byte(0xFF) end
return
end
local sign_byte, exponent_byte, mantissa_byte_1, mantissa_byte_2
local sign_bit = 0
if number < 0 then
number = -number
sign_bit = 0x80
end
if number == math_huge then -- inf: exponent = all 1, mantissa = all 0
sign_byte, exponent_byte, mantissa_byte_1, mantissa_byte_2 = sign_bit + 0x7F, 0x80, 0, 0
else -- real number
local mantissa, exponent = math_frexp(number)
if exponent <= -126 or number == 0 then -- must write a subnormal number
mantissa = mantissa * 2 ^ (exponent + 126)
exponent = 0
else -- normal numbers are stored as 1.<mantissa>
mantissa = mantissa * 2 - 1
exponent = exponent - 1 + 127 -- mantissa << 1 <=> exponent--
assert(exponent < 0xFF)
end
local exp_lowest_bit = exponent % 2
sign_byte = sign_bit + (exponent - exp_lowest_bit) / 2
mantissa = mantissa * 0x80
exponent_byte = exp_lowest_bit * 0x80 + math_floor(mantissa)
mantissa = mantissa % 1
mantissa = mantissa * 0x100
mantissa_byte_1 = math_floor(mantissa)
mantissa = mantissa % 1
mantissa = mantissa * 0x100
mantissa_byte_2 = math_floor(mantissa)
mantissa = mantissa % 1
assert(mantissa == 0) -- no truncation allowed: round numbers properly using modlib.math.fround
end
write_byte(mantissa_byte_2)
write_byte(mantissa_byte_1)
write_byte(exponent_byte)
write_byte(sign_byte)
end
function write_double(write_byte, number)
if number ~= number then -- nan: all ones
for _ = 1, 8 do write_byte(0xFF) end
return
end
local sign_byte, exponent_byte, mantissa_bytes
local sign_bit = 0
if number < 0 then
number = -number
sign_bit = 0x80
end
if number == math_huge then -- inf: exponent = all 1, mantissa = all 0
sign_byte, exponent_byte, mantissa_bytes = sign_bit + 0x7F, 0xF0, {0, 0, 0, 0, 0, 0}
else -- real number
local mantissa, exponent = math_frexp(number)
if exponent <= -1022 or number == 0 then -- must write a subnormal number
mantissa = mantissa * 2 ^ (exponent + 1022)
exponent = 0
else -- normal numbers are stored as 1.<mantissa>
mantissa = mantissa * 2 - 1
exponent = exponent - 1 + 1023 -- mantissa << 1 <=> exponent--
assert(exponent < 0x7FF)
end
local exp_low_nibble = exponent % 0x10
sign_byte = sign_bit + (exponent - exp_low_nibble) / 0x10
mantissa = mantissa * 0x10
exponent_byte = exp_low_nibble * 0x10 + math_floor(mantissa)
mantissa = mantissa % 1
mantissa_bytes = {}
for i = 1, 6 do
mantissa = mantissa * 0x100
mantissa_bytes[i] = math_floor(mantissa)
mantissa = mantissa % 1
end
assert(mantissa == 0)
end
for i = 6, 1, -1 do
write_byte(mantissa_bytes[i])
end
write_byte(exponent_byte)
write_byte(sign_byte)
end
--: on_write function(double)
--: double true - f64, false - f32
function write_float(write_byte, number, double)
(double and write_double or write_single)(write_byte, number)
end
-- Export environment
return _ENV