@@ -262,31 +262,36 @@ void psyqo::Lua::setupFixedPointMetatable() {
262
262
end
263
263
264
264
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
265
270
266
271
-- Simple operations can be done directly in Lua
267
272
function FixedPoint.__add(a, b)
268
273
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
273
278
-- FixedPoint + FixedPoint
274
279
return newFromRaw(raw_a + b._raw)
275
280
else
276
- error("Cannot add FixedPoint to this type" )
281
+ err(' add' )
277
282
end
278
283
end
279
284
280
285
function FixedPoint.__sub(a, b)
281
286
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
286
291
-- FixedPoint - FixedPoint
287
292
return newFromRaw(raw_a - b._raw)
288
293
else
289
- error("Cannot subtract this type from FixedPoint" )
294
+ err(' subtract' )
290
295
end
291
296
end
292
297
@@ -296,35 +301,35 @@ void psyqo::Lua::setupFixedPointMetatable() {
296
301
end
297
302
298
303
function FixedPoint.__eq(a, b)
299
- if type(b) == " table" and b._raw then
304
+ if type(b) == ' table' and b._raw then
300
305
return a._raw == b._raw
301
- elseif type(b) == " number" then
306
+ elseif type(b) == ' number' then
302
307
-- Compare with an integer number (shifted)
303
- return a._raw == bit32. lshift(b, 12)
308
+ return a._raw == lshift(b, 12)
304
309
else
305
310
return false
306
311
end
307
312
end
308
313
309
314
function FixedPoint.__lt(a, b)
310
- if type(b) == " table" and b._raw then
315
+ if type(b) == ' table' and b._raw then
311
316
return a._raw < b._raw
312
- elseif type(b) == " number" then
317
+ elseif type(b) == ' number' then
313
318
-- Compare with an integer number (shifted)
314
- return a._raw < bit32. lshift(b, 12)
319
+ return a._raw < lshift(b, 12)
315
320
else
316
- error("Cannot compare FixedPoint with this type" )
321
+ err(' compare' )
317
322
end
318
323
end
319
324
320
325
function FixedPoint.__le(a, b)
321
- if type(b) == " table" and b._raw then
326
+ if type(b) == ' table' and b._raw then
322
327
return a._raw <= b._raw
323
- elseif type(b) == " number" then
328
+ elseif type(b) == ' number' then
324
329
-- Compare with an integer number (shifted)
325
- return a._raw <= bit32. lshift(b, 12)
330
+ return a._raw <= lshift(b, 12)
326
331
else
327
- error("Cannot compare FixedPoint with this type" )
332
+ err(' compare' )
328
333
end
329
334
end
330
335
@@ -335,13 +340,13 @@ void psyqo::Lua::setupFixedPointMetatable() {
335
340
336
341
-- Method to convert to a simple number (for simple calculations)
337
342
function FixedPoint:toNumber()
338
- return bit32. rshift((self._raw + 2048), 12)
343
+ return rshift((self._raw + 2048), 12)
339
344
end
340
345
341
346
-- Create a new FixedPoint
342
347
function FixedPoint.new(integer, fraction)
343
348
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)
345
350
end
346
351
end
347
352
)lua" ;
0 commit comments