lua-users home
lua-l archive

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




On Wed, May 15, 2019 at 12:53 AM Philipp Janda <siffiejoe@gmx.net> wrote:
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;

Okay, so it does the widening / sign extension before it does the conversion to unsigned. This makes sense and it's probably the 

To be honest, I should have known this. I had to build a self-hosting C++ compiler from first principles as a class assignment in 2013-2014. But it's been YEARS and I don't remember everything still. (I also never finished it because the class instructors vanished around the time that we started working on the actual code generation part.)

/s/ Adam