aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-31 15:38:26 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-03-31 15:38:26 -0300
commit8ef9117924709b0ce013b5f5d2db5bb7fe41b987 (patch)
treeede942b0ff60eb59a82a4e440d15209fc5ac1c55
parentea69f17d989784f75433eb69de0ad2dbe6c7195a (diff)
downloadlua-8ef9117924709b0ce013b5f5d2db5bb7fe41b987.tar.gz
lua-8ef9117924709b0ce013b5f5d2db5bb7fe41b987.tar.bz2
lua-8ef9117924709b0ce013b5f5d2db5bb7fe41b987.zip
fancier way to do sign extension
-rw-r--r--lstrlib.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 3da669d7..880c4519 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.189 2014/03/21 14:26:44 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.190 2014/03/27 15:58:05 roberto Exp roberto $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -1049,9 +1049,8 @@ static int unpackint (const char *buff, lua_Integer *res,
1049 n |= (lua_Integer)(unsigned char)buff[littleendian ? size - 1 - i : i]; 1049 n |= (lua_Integer)(unsigned char)buff[littleendian ? size - 1 - i : i];
1050 } 1050 }
1051 if (size < SZINT) { /* need sign extension? */ 1051 if (size < SZINT) { /* need sign extension? */
1052 lua_Integer mask = (~(lua_Integer)0) << (size*NB - 1); 1052 lua_Unsigned mask = (lua_Unsigned)1 << (size*NB - 1);
1053 if (n & mask) /* negative value? */ 1053 n = (lua_Integer)((n ^ mask) - mask); /* do sign extension */
1054 n |= mask; /* signal extension */
1055 } 1054 }
1056 *res = n; 1055 *res = n;
1057 return 1; 1056 return 1;