lua-users home
lua-l archive

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


Am 15.05.19 um 04:02 schröbte Coda Highland:
On Tue, May 14, 2019 at 8:47 PM Jim <djvaios@gmail.com> wrote:

it seems that a MAX macro for LUA_UNSIGNED is not #defined in luaconf.h
so that i had to use ULLONG_MAX from <limits.h> directly instead.

you could also use

lua_Unsigned umax = ~ 0 ;

to determine this upper maximum.
this has the advantage that you do not need to know what C integer type
is actually used as Lua integer type.


Doesn't that need to be:

lua_Unsigned umax = ~(lua_Unsigned)0;

to cover all of the cases that the C standard supports? It's been a while
since I've had my head that deep in this, but I know that 0 is of type
signed int. What I don't remember is if sign extension applies BEFORE or
AFTER converting to unsigned; that expression might end up being
0x00000000FFFFFFFF if it converts to unsigned before doing sign extension.


    lua_Unsigned umax = ~(lua_Unsigned)0;
    lua_Unsigned umax = -1;

are both fine.

    lua_Unsigned umax = ~0;

works for two's complement machines because it is equivalent to the `-1` case. In the worst case it might be undefined behavior (on one's complement machines which cannot represent negative zeros).

The issue you mentioned above applies to

    lua_Unsigned umax = ~0u;



/s/ Adam


Philipp