Skip to content

Commit e0e0fcf

Browse files
committed
Some more optimizations in the FixedPoint Lua code.
1 parent 77e5b28 commit e0e0fcf

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/mips/psyqo-lua/src/lua.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -262,31 +262,36 @@ void psyqo::Lua::setupFixedPointMetatable() {
262262
end
263263
264264
FixedPoint.newFromRaw = newFromRaw
265+
local lshift = bit32.lshift
266+
local rshift = bit32.rshift
267+
local err = function(op)
268+
error('Cannot ' .. op .. ' FixedPoint to this type')
269+
end
265270
266271
-- Simple operations can be done directly in Lua
267272
function FixedPoint.__add(a, b)
268273
local raw_a = a._raw
269-
if type(b) == "number" then
270-
-- FixedPoint + number (treated as integer)
271-
return newFromRaw(raw_a + bit32.lshift(b, 12))
272-
elseif type(b) == "table" and b._raw then
274+
if type(b) == 'number' then
275+
-- FixedPoint + number
276+
return newFromRaw(raw_a + lshift(b, 12))
277+
elseif type(b) == 'table' and b._raw then
273278
-- FixedPoint + FixedPoint
274279
return newFromRaw(raw_a + b._raw)
275280
else
276-
error("Cannot add FixedPoint to this type")
281+
err('add')
277282
end
278283
end
279284
280285
function FixedPoint.__sub(a, b)
281286
local raw_a = a._raw
282-
if type(b) == "number" then
283-
-- FixedPoint - number (treated as integer)
284-
return newFromRaw(raw_a - bit32.lshift(b, 12))
285-
elseif type(b) == "table" and b._raw then
287+
if type(b) == 'number' then
288+
-- FixedPoint - number
289+
return newFromRaw(raw_a - lshift(b, 12))
290+
elseif type(b) == 'table' and b._raw then
286291
-- FixedPoint - FixedPoint
287292
return newFromRaw(raw_a - b._raw)
288293
else
289-
error("Cannot subtract this type from FixedPoint")
294+
err('subtract')
290295
end
291296
end
292297
@@ -296,35 +301,35 @@ void psyqo::Lua::setupFixedPointMetatable() {
296301
end
297302
298303
function FixedPoint.__eq(a, b)
299-
if type(b) == "table" and b._raw then
304+
if type(b) == 'table' and b._raw then
300305
return a._raw == b._raw
301-
elseif type(b) == "number" then
306+
elseif type(b) == 'number' then
302307
-- Compare with an integer number (shifted)
303-
return a._raw == bit32.lshift(b, 12)
308+
return a._raw == lshift(b, 12)
304309
else
305310
return false
306311
end
307312
end
308313
309314
function FixedPoint.__lt(a, b)
310-
if type(b) == "table" and b._raw then
315+
if type(b) == 'table' and b._raw then
311316
return a._raw < b._raw
312-
elseif type(b) == "number" then
317+
elseif type(b) == 'number' then
313318
-- Compare with an integer number (shifted)
314-
return a._raw < bit32.lshift(b, 12)
319+
return a._raw < lshift(b, 12)
315320
else
316-
error("Cannot compare FixedPoint with this type")
321+
err('compare')
317322
end
318323
end
319324
320325
function FixedPoint.__le(a, b)
321-
if type(b) == "table" and b._raw then
326+
if type(b) == 'table' and b._raw then
322327
return a._raw <= b._raw
323-
elseif type(b) == "number" then
328+
elseif type(b) == 'number' then
324329
-- Compare with an integer number (shifted)
325-
return a._raw <= bit32.lshift(b, 12)
330+
return a._raw <= lshift(b, 12)
326331
else
327-
error("Cannot compare FixedPoint with this type")
332+
err('compare')
328333
end
329334
end
330335
@@ -335,13 +340,13 @@ void psyqo::Lua::setupFixedPointMetatable() {
335340
336341
-- Method to convert to a simple number (for simple calculations)
337342
function FixedPoint:toNumber()
338-
return bit32.rshift((self._raw + 2048), 12)
343+
return rshift((self._raw + 2048), 12)
339344
end
340345
341346
-- Create a new FixedPoint
342347
function FixedPoint.new(integer, fraction)
343348
if fraction == nil then fraction = 0 end
344-
return setmetatable({_raw = bit32.lshift(integer, 12) + fraction}, FixedPoint)
349+
return setmetatable({_raw = lshift(integer, 12) + fraction}, FixedPoint)
345350
end
346351
end
347352
)lua";

0 commit comments

Comments
 (0)