-
Notifications
You must be signed in to change notification settings - Fork 4
/
fix32.lua
346 lines (287 loc) · 7.08 KB
/
fix32.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
local bit = require("bit")
local consts=require("fix32_constants")
local api=require("api")
local exponent=2^16
local max32=2^32
local mask=exponent-1
local inf=0x7fffffff/exponent
local neginf=bit.tobit(0x80000001)/exponent
local function fix32_add(a,b)
return bit.tobit(a*exponent + b*exponent)/exponent
end
local function fix32_sub(a,b)
return bit.tobit(a*exponent - b*exponent)/exponent
end
local function fix32_mul(a,b)
--use 64 bit integers
return bit.tobit(bit.rshift((a*exponent* 1LL) * (b * exponent * 1LL),16))/exponent
end
local function trunc(x)
return x>=0 and math.floor(x) or math.ceil(x)
end
local function fix32_div(a,b)
a=a*exponent
b=b*exponent
if b==0 then
return a>=0 and inf or neginf
elseif b>=0 and bit.band(b,0xffff)==0 then
-- b is an integer. no overflow can occur.
return bit.tobit(trunc(a/(bit.rshift(b,16))))/exponent
end
local val = trunc(a*exponent / b)
if val<=-0x80000000 then
return neginf
elseif val>0x7fffffff then
return inf
end
return val/exponent
end
local function fix32_unm(a)
return bit.tobit(-a*exponent)/exponent
end
local function fix32_bnot(a)
return bit.bnot(a*exponent)/exponent
end
local function fix32_bor(a,b)
return bit.bor(a*exponent, b*exponent)/exponent
end
local function fix32_band(a,b)
return bit.band(a*exponent, b*exponent)/exponent
end
local function fix32_bxor(a,b)
return bit.bxor(a*exponent, b*exponent)/exponent
end
local function fix32_tostr(a)
a=a*exponent
return string.format("0x%04x.%04x",bit.rshift(bit.band(a,0xffff0000),16), bit.band(a,0xffff))
end
local function fix32_abs(a)
return a==-0x8000.0000 and 0x7fff.ffff or
a>=0 and a or fix32_unm(a)
end
local function fix32_mod(a,b)
a=a*exponent
b=fix32_abs(b)*exponent
if b==0 then
return 0
else
return (a%b)/exponent
end
end
local function fix32_sqrt(a)
-- uses this algorithm https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Binary_numeral_system_(base_2)
-- (same as pico8)
--
--the result is sqrt(a)=sqrt(a*exponent^2)/exponent
--
--a*exponent^2 is 2^48, which still fits within lossless double precision
local a=a*exponent^2
if(a<0) then
return 0
end
local pow2=2^24
local res=0
while pow2>=1 do
if (res+pow2)^2<=a then
res=res+pow2
end
pow2=pow2/2
end
return res/exponent
end
local function fix32_pow(a,b)
if b==0 then
return 1
end
-- a^(b) = (1/a)^(-b)
if b<0 then
b=-b
a=fix32_div(1,a)
end
--divide b into integer and fractional parts
local int_b=math.floor(b)
local frac_b=b-int_b
-- fractional powers of negatives are (usually) not defined
if a<0 and frac_b>0 then
return 0
end
local res=1
--standard integer exponentitation
local an=a
while(int_b>=1) do
if int_b%2~=0 then
res=fix32_mul(res,an)
end
int_b = bit.rshift(int_b,1)
an=fix32_mul(an,an)
end
while frac_b~=0 do
while frac_b<1 do
a=fix32_sqrt(a)
frac_b=frac_b*2
end
frac_b=frac_b-1
res=fix32_mul(res,a)
end
end
local function fix32_from_int(a)
--convert a 32 bit int to a 16.16 value
return bit.tobit(a)/exponent
end
local function str_to_fix32_str(s)
-- receives a string of a number
-- returns a string that will be parsed as the exact 16.16 number by the lua parser
--
local dotidx=string.find(s,"%.") or #s+1
--string representation of the exact value
local numstr
if s:match("[xX]") then
--only take 4 digits before and after the point
local radixidx=string.find(s, "[xX]")
numstr= s:sub(1,radixidx)..s:sub(math.max(dotidx-4,radixidx+1),dotidx+4)
elseif s:match("[bB]") then
--similarly to hex, only take 16 digits before and after the point
local radixidx=string.find(s, "[bB]")
numstr= s:sub(1,radixidx)..s:sub(math.max(dotidx-16,radixidx+1),dotidx+16)
else
numstr=s
end
--invalid number
if not tonumber(numstr) then
return nil
end
--return a string that will be parsed correctly as this number
return string.format("%a",bit.tobit(trunc(tonumber(numstr)*exponent))/exponent)
end
local function fix32_tonumber(x)
local orig_tonumber = tonumber(x)
-- if this is not a valid number string - just return nil
if orig_tonumber and type(x)=="string" then
return tonumber(str_to_fix32_str(x))
end
return orig_tonumber
end
local function fix32_rnd(x)
if type(x)=="table" then
return x[math.floor(fix32_rnd(#x)+1)]
else
x = (fix32_tonumber(x) or 1) * exponent
if(x==0) then
return 0
end
x = x % max32
pico8.rng_high=bit.bor(bit.lshift(pico8.rng_high,16), bit.rshift(pico8.rng_high, 16))
pico8.rng_high=bit.tobit(pico8.rng_low + pico8.rng_high)
pico8.rng_low=bit.tobit(pico8.rng_low + pico8.rng_high)
return bit.tobit(pico8.rng_high % max32 % x)/exponent
end
end
local function fix32_srand(x)
x = (fix32_tonumber(x) or 0) * exponent
if(x==0) then
pico8.rng_high=0x60009755
x=0xdeadbeef
else
pico8.rng_high=bit.bxor(x, 0xbead29ba)
end
for _=1,0x20 do
pico8.rng_high=bit.bor(bit.lshift(pico8.rng_high,16), bit.rshift(pico8.rng_high, 16))
pico8.rng_high=bit.tobit(pico8.rng_high + x)
x = bit.tobit(x + pico8.rng_high)
end
pico8.rng_low = x
end
local function fix32_run_ext()
fix32_srand(love.math.random(max32-1)/exponent)
end
local function fix32_sin(x)
x=fix32_tonumber(x)*exponent
local index=bit.band(bit.rshift(x+0x4002,2), 0x3fff)
if index > 0x1fff then
index = 0x4000 - index
end
if index < 0x1000 then
return bit.tobit(consts.cos_val[index])/exponent
end
return bit.tobit(-consts.cos_val[0x2000-index])/exponent
end
local function fix32_cos(x)
x=fix32_tonumber(x)*exponent
local index=bit.band(bit.rshift(x+2,2), 0x3fff)
if index > 0x1fff then
index = 0x4000 - index
end
if index < 0x1000 then
return bit.tobit(consts.cos_val[index])/exponent
end
return bit.tobit(-consts.cos_val[0x2000-index])/exponent
end
local function fix32_atan2(dx,dy)
local quot=fix32_div(-dy,dx)
local sign=api.sgn(quot)
local abs=fix32_abs(quot)
quot = quot*exponent
abs = abs*exponent
local ret
if abs<0x10001 then
ret=sign * consts.atan_val[bit.rshift(abs,5)]
else
abs=fix32_abs(fix32_div(dx,-dy))*exponent
ret = sign * (0x4000 - consts.atan_val[bit.rshift(abs,5)])
end
if dx<0 then
ret = ret + 0x8000
end
return bit.band(ret%max32,0xffff)/exponent
end
local function fix32_init()
fixed_point_enabled = true
print("fixed point enabled!")
api.__fix_add=fix32_add
api.__fix_sub=fix32_sub
api.__fix_mul=fix32_mul
api.__fix_div=fix32_div
api.__fix_mod=fix32_mod
api.__fix_pow=fix32_pow
api.__fix_unm=fix32_unm
api.sqrt=fix32_sqrt
api._tonumber = fix32_tonumber
function api.time()
return fix32_div(pico8.frames,30)
end
api.t=api.time
api.rnd=fix32_rnd
api.srand=fix32_srand
api.sin=fix32_sin
api.cos=fix32_cos
api.atan2=fix32_atan2
local api_run=api.run
function api.run()
fix32_run_ext()
api_run()
end
end
return {
add=fix32_add,
sub=fix32_sub,
mul=fix32_mul,
div=fix32_div,
unm=fix32_unm,
bnot=fix32_bnot,
bor=fix32_bor,
band=fix32_band,
tostr=fix32_tostr,
abs=fix32_abs,
mod=fix32_mod,
sqrt=fix32_sqrt,
pow=fix32_pow,
from_int=fix32_from_int,
str_to_fix32_str= str_to_fix32_str,
rnd=fix32_rnd,
srand=fix32_srand,
run_ext=fix32_run_ext,
init=fix32_init,
sin=fix32_sin,
cos=fix32_cos,
atan2=fix32_atan2
}