Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lbitlib.c File Reference
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

Go to the source code of this file.

Macros

#define lbitlib_c
 
#define LUA_LIB
 
#define LUA_NBITS   32
 
#define ALLONES   (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
 
#define trim(x)   ((x) & ALLONES)
 
#define mask(n)   (~((ALLONES << 1) << ((n) - 1)))
 

Typedefs

typedef lua_Unsigned b_uint
 

Functions

static b_uint andaux (lua_State *L)
 
static int b_and (lua_State *L)
 
static int b_test (lua_State *L)
 
static int b_or (lua_State *L)
 
static int b_xor (lua_State *L)
 
static int b_not (lua_State *L)
 
static int b_shift (lua_State *L, b_uint r, int i)
 
static int b_lshift (lua_State *L)
 
static int b_rshift (lua_State *L)
 
static int b_arshift (lua_State *L)
 
static int b_rot (lua_State *L, int i)
 
static int b_lrot (lua_State *L)
 
static int b_rrot (lua_State *L)
 
static int fieldargs (lua_State *L, int farg, int *width)
 
static int b_extract (lua_State *L)
 
static int b_replace (lua_State *L)
 
LUAMOD_API int luaopen_bit32 (lua_State *L)
 

Variables

static const luaL_Reg bitlib []
 

Macro Definition Documentation

◆ ALLONES

#define ALLONES   (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))

Definition at line 22 of file lua-5.2.4/src/lbitlib.c.

Referenced by b_arshift().

◆ lbitlib_c

#define lbitlib_c

Definition at line 7 of file lua-5.2.4/src/lbitlib.c.

◆ LUA_LIB

#define LUA_LIB

Definition at line 8 of file lua-5.2.4/src/lbitlib.c.

◆ LUA_NBITS

#define LUA_NBITS   32

Definition at line 18 of file lua-5.2.4/src/lbitlib.c.

Referenced by b_arshift(), b_rot(), b_shift(), and fieldargs().

◆ mask

◆ trim

#define trim ( x)    ((x) & ALLONES)

Typedef Documentation

◆ b_uint

Definition at line 32 of file lua-5.2.4/src/lbitlib.c.

Function Documentation

◆ andaux()

static b_uint andaux ( lua_State * L)
static

Definition at line 36 of file lua-5.2.4/src/lbitlib.c.

36 {
37 int i, n = lua_gettop(L);
38 b_uint r = ~(b_uint)0;
39 for (i = 1; i <= n; i++)
40 r &= luaL_checkunsigned(L, i);
41 return trim(r);
42}
LUA_API int lua_gettop(lua_State *L)
LUALIB_API lua_Unsigned luaL_checkunsigned(lua_State *L, int narg)
#define trim(x)
lua_Unsigned b_uint

References lua_gettop(), luaL_checkunsigned(), and trim.

Referenced by b_and(), and b_test().

◆ b_and()

static int b_and ( lua_State * L)
static

Definition at line 45 of file lua-5.2.4/src/lbitlib.c.

45 {
46 b_uint r = andaux(L);
47 lua_pushunsigned(L, r);
48 return 1;
49}
LUA_API void lua_pushunsigned(lua_State *L, lua_Unsigned u)
static b_uint andaux(lua_State *L)

References andaux(), and lua_pushunsigned().

◆ b_arshift()

static int b_arshift ( lua_State * L)
static

Definition at line 113 of file lua-5.2.4/src/lbitlib.c.

113 {
114 b_uint r = luaL_checkunsigned(L, 1);
115 int i = luaL_checkint(L, 2);
116 if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
117 return b_shift(L, r, -i);
118 else { /* arithmetic shift for 'negative' number */
119 if (i >= LUA_NBITS) r = ALLONES;
120 else
121 r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
122 lua_pushunsigned(L, r);
123 return 1;
124 }
125}
#define luaL_checkint(L, n)
#define ALLONES
static int b_shift(lua_State *L, b_uint r, int i)
#define LUA_NBITS

References ALLONES, b_shift(), LUA_NBITS, lua_pushunsigned(), luaL_checkint, luaL_checkunsigned(), and trim.

◆ b_extract()

static int b_extract ( lua_State * L)
static

Definition at line 167 of file lua-5.2.4/src/lbitlib.c.

167 {
168 int w;
169 b_uint r = luaL_checkunsigned(L, 1);
170 int f = fieldargs(L, 2, &w);
171 r = (r >> f) & mask(w);
172 lua_pushunsigned(L, r);
173 return 1;
174}
static int fieldargs(lua_State *L, int farg, int *width)
#define mask(n)

References fieldargs(), lua_pushunsigned(), luaL_checkunsigned(), and mask.

◆ b_lrot()

static int b_lrot ( lua_State * L)
static

Definition at line 139 of file lua-5.2.4/src/lbitlib.c.

139 {
140 return b_rot(L, luaL_checkint(L, 2));
141}
static int b_rot(lua_State *L, int i)

References b_rot(), and luaL_checkint.

◆ b_lshift()

static int b_lshift ( lua_State * L)
static

Definition at line 103 of file lua-5.2.4/src/lbitlib.c.

103 {
104 return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
105}

References b_shift(), luaL_checkint, and luaL_checkunsigned().

◆ b_not()

static int b_not ( lua_State * L)
static

Definition at line 79 of file lua-5.2.4/src/lbitlib.c.

79 {
80 b_uint r = ~luaL_checkunsigned(L, 1);
82 return 1;
83}

References lua_pushunsigned(), and trim.

◆ b_or()

static int b_or ( lua_State * L)
static

Definition at line 59 of file lua-5.2.4/src/lbitlib.c.

59 {
60 int i, n = lua_gettop(L);
61 b_uint r = 0;
62 for (i = 1; i <= n; i++)
63 r |= luaL_checkunsigned(L, i);
65 return 1;
66}

References lua_gettop(), lua_pushunsigned(), luaL_checkunsigned(), and trim.

◆ b_replace()

static int b_replace ( lua_State * L)
static

Definition at line 177 of file lua-5.2.4/src/lbitlib.c.

177 {
178 int w;
179 b_uint r = luaL_checkunsigned(L, 1);
180 b_uint v = luaL_checkunsigned(L, 2);
181 int f = fieldargs(L, 3, &w);
182 int m = mask(w);
183 v &= m; /* erase bits outside given width */
184 r = (r & ~(m << f)) | (v << f);
185 lua_pushunsigned(L, r);
186 return 1;
187}

References fieldargs(), lua_pushunsigned(), luaL_checkunsigned(), and mask.

◆ b_rot()

static int b_rot ( lua_State * L,
int i )
static

Definition at line 128 of file lua-5.2.4/src/lbitlib.c.

128 {
129 b_uint r = luaL_checkunsigned(L, 1);
130 i &= (LUA_NBITS - 1); /* i = i % NBITS */
131 r = trim(r);
132 if (i != 0) /* avoid undefined shift of LUA_NBITS when i == 0 */
133 r = (r << i) | (r >> (LUA_NBITS - i));
134 lua_pushunsigned(L, trim(r));
135 return 1;
136}

References LUA_NBITS, lua_pushunsigned(), luaL_checkunsigned(), and trim.

Referenced by b_lrot(), and b_rrot().

◆ b_rrot()

static int b_rrot ( lua_State * L)
static

Definition at line 144 of file lua-5.2.4/src/lbitlib.c.

144 {
145 return b_rot(L, -luaL_checkint(L, 2));
146}

References b_rot(), and luaL_checkint.

◆ b_rshift()

static int b_rshift ( lua_State * L)
static

Definition at line 108 of file lua-5.2.4/src/lbitlib.c.

108 {
109 return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
110}

References b_shift(), luaL_checkint, and luaL_checkunsigned().

◆ b_shift()

static int b_shift ( lua_State * L,
b_uint r,
int i )
static

Definition at line 86 of file lua-5.2.4/src/lbitlib.c.

86 {
87 if (i < 0) { /* shift right? */
88 i = -i;
89 r = trim(r);
90 if (i >= LUA_NBITS) r = 0;
91 else r >>= i;
92 }
93 else { /* shift left */
94 if (i >= LUA_NBITS) r = 0;
95 else r <<= i;
96 r = trim(r);
97 }
98 lua_pushunsigned(L, r);
99 return 1;
100}

References LUA_NBITS, lua_pushunsigned(), and trim.

Referenced by b_arshift(), b_lshift(), and b_rshift().

◆ b_test()

static int b_test ( lua_State * L)
static

Definition at line 52 of file lua-5.2.4/src/lbitlib.c.

52 {
53 b_uint r = andaux(L);
54 lua_pushboolean(L, r != 0);
55 return 1;
56}
LUA_API void lua_pushboolean(lua_State *L, int b)

References andaux(), and lua_pushboolean().

◆ b_xor()

static int b_xor ( lua_State * L)
static

Definition at line 69 of file lua-5.2.4/src/lbitlib.c.

69 {
70 int i, n = lua_gettop(L);
71 b_uint r = 0;
72 for (i = 1; i <= n; i++)
73 r ^= luaL_checkunsigned(L, i);
75 return 1;
76}

References lua_gettop(), lua_pushunsigned(), luaL_checkunsigned(), and trim.

◆ fieldargs()

static int fieldargs ( lua_State * L,
int farg,
int * width )
static

Definition at line 155 of file lua-5.2.4/src/lbitlib.c.

155 {
156 int f = luaL_checkint(L, farg);
157 int w = luaL_optint(L, farg + 1, 1);
158 luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
159 luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
160 if (f + w > LUA_NBITS)
161 luaL_error(L, "trying to access non-existent bits");
162 *width = w;
163 return f;
164}
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define luaL_optint(L, n, d)
#define luaL_argcheck(L, cond, numarg, extramsg)

References LUA_NBITS, luaL_argcheck, luaL_checkint, luaL_error(), and luaL_optint.

Referenced by b_extract(), and b_replace().

◆ luaopen_bit32()

LUAMOD_API int luaopen_bit32 ( lua_State * L)

Definition at line 208 of file lua-5.2.4/src/lbitlib.c.

208 {
210 return 1;
211}
#define luaL_newlib(L, l)
static const luaL_Reg bitlib[]

References bitlib, and luaL_newlib.

Variable Documentation

◆ bitlib

const luaL_Reg bitlib[]
static
Initial value:
= {
{"arshift", b_arshift},
{"band", b_and},
{"bnot", b_not},
{"bor", b_or},
{"bxor", b_xor},
{"btest", b_test},
{"extract", b_extract},
{"lrotate", b_lrot},
{"lshift", b_lshift},
{"replace", b_replace},
{"rrotate", b_rrot},
{"rshift", b_rshift},
}
#define NULL
Definition gmacros.h:924
static int b_rrot(lua_State *L)
static int b_xor(lua_State *L)
static int b_replace(lua_State *L)
static int b_or(lua_State *L)
static int b_and(lua_State *L)
static int b_extract(lua_State *L)
static int b_rshift(lua_State *L)
static int b_not(lua_State *L)
static int b_lshift(lua_State *L)
static int b_lrot(lua_State *L)
static int b_test(lua_State *L)
static int b_arshift(lua_State *L)

Definition at line 190 of file lua-5.2.4/src/lbitlib.c.

190 {
191 {"arshift", b_arshift},
192 {"band", b_and},
193 {"bnot", b_not},
194 {"bor", b_or},
195 {"bxor", b_xor},
196 {"btest", b_test},
197 {"extract", b_extract},
198 {"lrotate", b_lrot},
199 {"lshift", b_lshift},
200 {"replace", b_replace},
201 {"rrotate", b_rrot},
202 {"rshift", b_rshift},
203 {NULL, NULL}
204};

Referenced by luaopen_bit32().