lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Jan 13, 2010, at 5:52 PM, Luiz Henrique de Figueiredo wrote:
Lua 5.2.0 (work2) is now available at
	http://www.lua.org/work/lua-5.2.0-work2.tar.gz

Yeah! New toy!

I don't believe the macro definition in src/luaconf.h for
lua_number2uint() added for the Microsoft compiler case is
going to work correctly:

 /* On a Microsoft compiler, use assembler */
 #if defined(_MSC_VER)

 #define lua_number2int(i,d)   { __asm fld d   __asm fistp i }
 #define lua_number2integer(i,n)         lua_number2int(i, n)
+#define lua_number2uint(i,n)            lua_number2int(i, n)

The problem is FISTP stores a signed integer, and if the double
to be converted is out of range of what the signed int can hold,
it stores 0x80000000.

I don't have a Microsoft compiler, but if I change my luaconf.h
and Makefile to cause gcc to use the assembler version, I get:

Lua 5.2.0 (work2)  Copyright (C) 1994-2010 Lua.org, PUC-Rio
> print(string.format("%08x", bit.bor(0x7fffffff, 0)))
7fffffff
> print(string.format("%08x", bit.bor(0x80000000, 0)))
80000000
> print(string.format("%08x", bit.bor(0x80000001, 0)))
80000000
> print(string.format("%08x", bit.bor(0xffffffff, 0)))
80000000

The following gives the correct conversion under gcc:

#define lua_number2uint(i,d) { long long q; { __asm fld d __asm fistp q } i = (unsigned LUA_INT32)q; }

but I don't know what a Microsoft compiler will think of that....

BTW, I'd love to know which architectures this comment further
down in luaconf.h is referring to:

/* on several machines, coercion from unsigned to double is too slow,
   so avoid that if possible */

Anyone?

-Sean