diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-02-11 13:55:29 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2010-02-11 13:55:29 -0200 |
commit | af512ad6da83f75b9e764ceeba28198e6e842377 (patch) | |
tree | 9f217a4d9cb12adb9cebef71d541ed759d48c80a | |
parent | d27108ccd543049bd2006aa16ed74f8c4d899731 (diff) | |
download | lua-af512ad6da83f75b9e764ceeba28198e6e842377.tar.gz lua-af512ad6da83f75b9e764ceeba28198e6e842377.tar.bz2 lua-af512ad6da83f75b9e764ceeba28198e6e842377.zip |
use of 'conventional' names for shift and rotate operations +
right/left versions for them
-rw-r--r-- | lbitlib.c | 34 |
1 files changed, 27 insertions, 7 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbitlib.c,v 1.2 2009/11/24 12:05:44 roberto Exp roberto $ | 2 | ** $Id: lbitlib.c,v 1.3 2010/01/12 19:40:02 roberto Exp roberto $ |
3 | ** Standard library for bitwise operations | 3 | ** Standard library for bitwise operations |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -80,9 +80,8 @@ static int b_not (lua_State *L) { | |||
80 | } | 80 | } |
81 | 81 | ||
82 | 82 | ||
83 | static int b_shift (lua_State *L) { | 83 | static int b_shift (lua_State *L, int i) { |
84 | b_uint r = getuintarg(L, 1); | 84 | b_uint r = getuintarg(L, 1); |
85 | lua_Integer i = luaL_checkinteger(L, 2); | ||
86 | if (i < 0) { /* shift right? */ | 85 | if (i < 0) { /* shift right? */ |
87 | i = -i; | 86 | i = -i; |
88 | if (i >= NBITS) r = 0; | 87 | if (i >= NBITS) r = 0; |
@@ -97,9 +96,18 @@ static int b_shift (lua_State *L) { | |||
97 | } | 96 | } |
98 | 97 | ||
99 | 98 | ||
100 | static int b_rotate (lua_State *L) { | 99 | static int b_lshift (lua_State *L) { |
100 | return b_shift(L, luaL_checkint(L, 2)); | ||
101 | } | ||
102 | |||
103 | |||
104 | static int b_rshift (lua_State *L) { | ||
105 | return b_shift(L, -luaL_checkint(L, 2)); | ||
106 | } | ||
107 | |||
108 | |||
109 | static int b_rot (lua_State *L, int i) { | ||
101 | b_uint r = getuintarg(L, 1); | 110 | b_uint r = getuintarg(L, 1); |
102 | lua_Integer i = luaL_checkinteger(L, 2); | ||
103 | i &= (NBITS - 1); /* i = i % NBITS */ | 111 | i &= (NBITS - 1); /* i = i % NBITS */ |
104 | r = (r << i) | (r >> (NBITS - i)); | 112 | r = (r << i) | (r >> (NBITS - i)); |
105 | lua_pushnumber(L, lua_uint2number(r)); | 113 | lua_pushnumber(L, lua_uint2number(r)); |
@@ -107,14 +115,26 @@ static int b_rotate (lua_State *L) { | |||
107 | } | 115 | } |
108 | 116 | ||
109 | 117 | ||
118 | static int b_rol (lua_State *L) { | ||
119 | return b_rot(L, luaL_checkint(L, 2)); | ||
120 | } | ||
121 | |||
122 | |||
123 | static int b_ror (lua_State *L) { | ||
124 | return b_rot(L, -luaL_checkint(L, 2)); | ||
125 | } | ||
126 | |||
127 | |||
110 | static const luaL_Reg bitlib[] = { | 128 | static const luaL_Reg bitlib[] = { |
111 | {"band", b_and}, | 129 | {"band", b_and}, |
112 | {"btest", b_test}, | 130 | {"btest", b_test}, |
113 | {"bor", b_or}, | 131 | {"bor", b_or}, |
114 | {"bxor", b_xor}, | 132 | {"bxor", b_xor}, |
115 | {"bnot", b_not}, | 133 | {"bnot", b_not}, |
116 | {"bshift", b_shift}, | 134 | {"lshift", b_lshift}, |
117 | {"brotate", b_rotate}, | 135 | {"rshift", b_rshift}, |
136 | {"rol", b_rol}, | ||
137 | {"ror", b_ror}, | ||
118 | {NULL, NULL} | 138 | {NULL, NULL} |
119 | }; | 139 | }; |
120 | 140 | ||