mirror of
https://github.com/lua/lua.git
synced 2025-01-28 06:03:00 +08:00
fancier way to do sign extension
This commit is contained in:
parent
ea69f17d98
commit
8ef9117924
@ -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
|
** Standard library for string operations and pattern-matching
|
||||||
** See Copyright Notice in lua.h
|
** 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];
|
n |= (lua_Integer)(unsigned char)buff[littleendian ? size - 1 - i : i];
|
||||||
}
|
}
|
||||||
if (size < SZINT) { /* need sign extension? */
|
if (size < SZINT) { /* need sign extension? */
|
||||||
lua_Integer mask = (~(lua_Integer)0) << (size*NB - 1);
|
lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
|
||||||
if (n & mask) /* negative value? */
|
n = (lua_Integer)((n ^ mask) - mask); /* do sign extension */
|
||||||
n |= mask; /* signal extension */
|
|
||||||
}
|
}
|
||||||
*res = n;
|
*res = n;
|
||||||
return 1;
|
return 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user