Spring numbers and bitops

Spring numbers and bitops

Discuss the source code and development of Spring Engine in general from a technical point of view. Patches go here too.

Moderator: Moderators

Post Reply
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Spring numbers and bitops

Post by SpliFF »

I've been trying to use the bitops in Spring lua and noticed something confusing. The provided bitop functions appear to mask out the first 2 bytes of any result and returns only 24bits of precision.

I remember reading something years ago about spring using a custom lua number type. I was wondering if anybody has more information on the number type and the reasons for using it.

I'd also like to know if 32bit bit operations are possible because I would like to perform a SHA-1 hash as part of a luasocket operation and the lua number type appears to prevent operations like math.bit_inv from returning the expected results with lua numbers.

Code: Select all

256 = 0000 0000 0000 0000 0000 0001 0000 0000
math.bit_inv(256) = 0000 0000 1111 1111 1111 1110 1111 1111
The first 8 zeros are the result of the underlying Spring LuaBitOps code applying a mask to the result. They should be ones.

At this point i'm going to assume true 32 bit bitwise operations aren't possible and consider other options but it would be nice to know more about spring's lua number type so I can decide how to proceed.
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: Spring numbers and bitops

Post by abma »

hmm, not sure what is used here:

https://github.com/spring/spring/blob/d ... onf.h#L397

but having only 24 bit could be a bug..

https://github.com/spring/spring/blob/d ... ps.cpp#L72

without knowing/understanding the code i would replace lua_pushnumber with lua_pushinteger...
abma
Spring Developer
Posts: 3798
Joined: 01 Jun 2009, 00:08

Re: Spring numbers and bitops

Post by abma »

aaah:

https://github.com/spring/spring/blob/d ... ps.cpp#L11


(don't ask my why it is there...)

edit: http://springrts.com/mantis/view.php?id=3823

can the patch been applied or will it break stuff?

@spliff:
atm easiest would be to use a pure lua implementation i guess:
http://lua-users.org/wiki/BitwiseOperators
User avatar
SpliFF
Posts: 1224
Joined: 28 Jul 2008, 06:51

Re: Spring numbers and bitops

Post by SpliFF »

You'd be surprised how difficult it was to find a 'pure' lua sha1 but I eventually did. It emulates bit operations using tables so it's slow but I only need it occassionally so the overhead isn't significant.

The main problem is most implementations I found rely on either the 'bit' library or something highlevel like crypto or ssl. I tried implementing the bit library functions using spring or pure lua equivalents but the results weren't consistent.

Code: Select all

--[[
BIT LIBRARY
--]]
local w32 = include('w32_ops.h.lua')

local band = math.bit_and
local bor = math.bit_or
local bnot = math.bit_inv
local bxor = math.bit_xor

--local lshift = bit.lshift
--local rshift = bit.rshift
--local tobit = bit.tobit

local function lshift(x, by)
  return x * 2 ^ by
end

local function rshift(x, by)
  return math.floor(x / 2 ^ by)
end

--local bror = bit.ror
--local brol = bit.rol
-- bror(b, n)  ((b << (32-n)) | (b >> n))
-- brol(b, n)  ((b << n) | (b >> (32-n)))


local function bror(x, by)
	return bor(lshift(x,(32-by)),rshift(x,by))
end
local function brol(x, by)
	return bor(lshift(x,by),rshift(x,(32-by)))
end

local function test(v1,v2)
	local Log = Spring.Echo

	Log('v1 (' .. v1 .. ') = ' .. w32.w32_to_bit_string(v1))
	Log('v2 (' .. v2 .. ') = ' .. w32.w32_to_bit_string(v2))
	Log(v1 .. ' >> ' .. v2 .. ' = ' .. rshift(v1,v2))
	Log(v1 .. ' >>> ' .. v2 .. ' = ' .. bror(v1,v2))
	Log(v1 .. ' << ' .. v2 .. ' = ' .. lshift(v1,v2))
	Log(v1 .. ' & ' .. v2 .. ' = ' .. band(v1,v2))
	Log(v1 .. ' | ' .. v2 .. ' = ' .. bor(v1,v2))
	Log(v1 .. ' ^ ' .. v2 .. ' = ' .. bxor(v1,v2))
	Log(v1 .. ' ~ = ' .. bnot(v1), w32.w32_to_bit_string(v1), w32.w32_to_bit_string(bnot(v1)))
end
test(0xffffff,2)
--test(128,1)

return {
	band = band,
	bor = bor,
	bxor = bxor,
	bnot = bnot,
	lshift = lshift,
	rshift = rshift,
	brol = brol,
	bror = bror
}
Post Reply

Return to “Engine”