fancier way to do sign extension

This commit is contained in:
Roberto Ierusalimschy 2014-03-31 15:38:26 -03:00
parent ea69f17d98
commit 8ef9117924
1 changed files with 3 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
** $Id: lstrlib.c,v 1.189 2014/03/21 14:26:44 roberto Exp roberto $
** $Id: lstrlib.c,v 1.190 2014/03/27 15:58:05 roberto Exp roberto $
** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h
*/
@ -1049,9 +1049,8 @@ static int unpackint (const char *buff, lua_Integer *res,
n |= (lua_Integer)(unsigned char)buff[littleendian ? size - 1 - i : i];
}
if (size < SZINT) { /* need sign extension? */
lua_Integer mask = (~(lua_Integer)0) << (size*NB - 1);
if (n & mask) /* negative value? */
n |= mask; /* signal extension */
lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
n = (lua_Integer)((n ^ mask) - mask); /* do sign extension */
}
*res = n;
return 1;