w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

lbitlib.c
Go to the documentation of this file.
1 /*
2 ** $Id: lbitlib.c,v 1.16 2011/06/20 16:35:23 roberto Exp $
3 ** Standard library for bitwise operations
4 ** See Copyright Notice in lua.h
5 */
6 
7 #define lbitlib_c
8 #define LUA_LIB
9 
10 #include "lua.h"
11 
12 #include "lauxlib.h"
13 #include "lualib.h"
14 
15 #include "lj_obj.h"
16 #include "lj_state.h"
17 
18 /* http://lua-users.org/lists/lua-l/2011-01/msg01039.html */
19 typedef unsigned int lua_Unsigned;
20 #define LUA_UNSIGNED unsigned LUA_INT32
21 
22 
23 #if !defined(lua_unsigned2number)
24 /* on several machines, coercion from unsigned to double is slow,
25  so it may be worth to avoid */
26 #define lua_unsigned2number(u) \
27  (((u) <= (lua_Unsigned)INT_MAX) ? (lua_Number)(int)(u) : (lua_Number)( unsigned int )(u))
28 #endif
29 
30 #if !defined(lua_lock)
31 #define lua_lock(L) ((void) 0)
32 #define lua_unlock(L) ((void) 0)
33 #endif
34 
35 
37  lua_Number n=0;
38  lua_lock(L);
39 /* printf("u=%d,%u,%x\n",u,u,u);
40  printf("INT_MAX=%d %u %x, (lua_Unsigned)INT_MAX=%d %u %x (unsigned int)INT_MAX=%d %u %x\n",
41  INT_MAX,INT_MAX,INT_MAX,
42  (lua_Unsigned)INT_MAX,(lua_Unsigned)INT_MAX,(lua_Unsigned)INT_MAX,
43  (unsigned int)INT_MAX,(unsigned int)INT_MAX,(unsigned int)INT_MAX);
44  printf("((u) <= (lua_Unsigned)INT_MAX) :%d\n",(((unsigned int)u) <= (lua_Unsigned)INT_MAX));
45  printf("(lua_Number)(int)(u) =%d,%u,%x\n",(lua_Number)(int)(u),(lua_Number)(int)(u),(lua_Number)(int)(u));
46  printf("(lua_Number)(u) =%f,%u,%x\n",(lua_Number)(u),(lua_Number)(u),(lua_Number)(u));
47  printf("u=%d,%u,%x\n",u,u,u);
48 */
49  /*printf("n=%d,%u,%x u=%d,%u,%x\n",n,n,n,u,u,u); */
51 /* printf("n=%f \n",n);
52  printf("u=%d %u %x n=%d %u %x\n",u,u,u,n,n,n);
53  printf("n=%d %u %x\n",n,n,n);
54  printf("u=%d,%u,%x\n",u,u,u);
55 */
56 /* setintptrV(L->top, n);
57  incr_top(L);
58  setnumV(L->top, n);
59 */
60  setnumV(L->top, n);
61  if (LJ_UNLIKELY(tvisnan(L->top)))
62  setnanV(L->top); /* Canonicalize injected NaNs. */
63  incr_top(L);
64  /*setnvalue(L->top, n);
65  api_incr_top(L);
66  */
67  lua_unlock(L);
68 }
69 
70 
71 
72 
73 
74 
75 #define luaL_checkunsigned (lua_Unsigned) luaL_checkinteger
76 /*#define lua_pushunsigned lua_pushinteger*/
77 
78 
79 
80 /* number of bits to consider in a number */
81 #if !defined(LUA_NBITS)
82 #define LUA_NBITS 32
83 #endif
84 
85 
86 #define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
87 
88 /* macro to trim extra bits */
89 #define trim(x) ((x) & ALLONES)
90 
91 
92 /* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
93 #define mask(n) (~((ALLONES << 1) << ((n) - 1)))
94 
95 
97 
98 
99 
101  int i, n = lua_gettop(L);
102  b_uint r = ~(b_uint)0;
103 /* printf("1 r=%d,%u,%x\n",r,r,r);*/
104  for (i = 1; i <= n; i++)
105  r &= luaL_checkunsigned(L, i);
106 /* printf("2 r=%d,%u,%x\n",r,r,r);
107  printf("3 r=%d,%u,%x\n",trim(r),trim(r),trim(r));
108 */
109  return trim(r);
110 }
111 
112 
113 static int b_and (lua_State *L) {
114  b_uint r = andaux(L);
115 /* printf("4 r=%d,%u,%x\n",r,r,r);*/
116  lua_pushunsigned(L, r);
117  return 1;
118 }
119 
120 
121 static int b_test (lua_State *L) {
122  b_uint r = andaux(L);
123  lua_pushboolean(L, r != 0);
124  return 1;
125 }
126 
127 
128 static int b_or (lua_State *L) {
129  int i, n = lua_gettop(L);
130  b_uint r = 0;
131  for (i = 1; i <= n; i++)
132  r |= luaL_checkunsigned(L, i);
134  return 1;
135 }
136 
137 
138 static int b_xor (lua_State *L) {
139  int i, n = lua_gettop(L);
140  b_uint r = 0;
141  for (i = 1; i <= n; i++)
142  r ^= luaL_checkunsigned(L, i);
144  return 1;
145 }
146 
147 
148 static int b_not (lua_State *L) {
149  b_uint r = ~~luaL_checkunsigned(L, 1);
151  return 1;
152 }
153 
154 
155 static int b_shift (lua_State *L, b_uint r, int i) {
156  if (i < 0) { /* shift right? */
157  i = -i;
158  r = trim(r);
159  if (i >= LUA_NBITS) r = 0;
160  else r >>= i;
161  }
162  else { /* shift left */
163  if (i >= LUA_NBITS) r = 0;
164  else r <<= i;
165  r = trim(r);
166  }
167  lua_pushunsigned(L, r);
168  return 1;
169 }
170 
171 
172 static int b_lshift (lua_State *L) {
173  return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
174 }
175 
176 
177 static int b_rshift (lua_State *L) {
178  return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
179 }
180 
181 
182 static int b_arshift (lua_State *L) {
184  int i = luaL_checkint(L, 2);
185  if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
186  return b_shift(L, r, -i);
187  else { /* arithmetic shift for 'negative' number */
188  if (i >= LUA_NBITS) r = ALLONES;
189  else
190  r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
191  lua_pushunsigned(L, r);
192  return 1;
193  }
194 }
195 
196 
197 static int b_rot (lua_State *L, int i) {
199  i &= (LUA_NBITS - 1); /* i = i % NBITS */
200  r = trim(r);
201  r = (r << i) | (r >> (LUA_NBITS - i));
203  return 1;
204 }
205 
206 
207 static int b_lrot (lua_State *L) {
208  return b_rot(L, luaL_checkint(L, 2));
209 }
210 
211 
212 static int b_rrot (lua_State *L) {
213  return b_rot(L, -luaL_checkint(L, 2));
214 }
215 
216 
217 /*
218 ** get field and width arguments for field-manipulation functions,
219 ** checking whether they are valid
220 */
221 static int fieldargs (lua_State *L, int farg, int *width) {
222  int f = luaL_checkint(L, farg);
223  int w = luaL_optint(L, farg + 1, 1);
224  luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
225  luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
226  if (f + w > LUA_NBITS)
227  luaL_error(L, "trying to access non-existent bits");
228  *width = w;
229  return f;
230 }
231 
232 
233 static int b_extract (lua_State *L) {
234  int w;
236  int f = fieldargs(L, 2, &w);
237  r = (r >> f) & mask(w);
238  lua_pushunsigned(L, r);
239  return 1;
240 }
241 
242 
243 static int b_replace (lua_State *L) {
244  int w;
247  int f = fieldargs(L, 3, &w);
248  int m = mask(w);
249  v &= m; /* erase bits outside given width */
250  r = (r & ~(m << f)) | (v << f);
251  lua_pushunsigned(L, r);
252  return 1;
253 }
254 
255 
256 static const luaL_Reg bitlib[] = {
257  {"arshift", b_arshift},
258  {"band", b_and},
259  {"bnot", b_not},
260  {"bor", b_or},
261  {"bxor", b_xor},
262  {"btest", b_test},
263  {"extract", b_extract},
264  {"lrotate", b_lrot},
265  {"lshift", b_lshift},
266  {"replace", b_replace},
267  {"rrotate", b_rrot},
268  {"rshift", b_rshift},
269  {NULL, NULL}
270 };
271 
272 
273 
274 /* http://lua-users.org/lists/lua-l/2011-01/msg01039.html */
275 
276 /*LUAMOD_API int luaopen_bit32 (lua_State *L) {*/
277 /* luaL_newlib(L, bitlib);*/
278 /* return 1;*/
279 /*} */
280 
282  luaL_register(L, "bit32", bitlib);
283  return 1;
284 }
285 
286 
287 
#define width(a)
Definition: aptex-macros.h:198
#define n
Definition: t4ht.c:1290
int w
Definition: dviconv.c:26
int v
Definition: dviconv.c:10
mpz_t * f
Definition: gen-fib.c:34
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific f u
Definition: afcover.h:88
small capitals from c petite p scientific i
Definition: afcover.h:80
int r
Definition: ppmqvga.c:68
#define incr_top(L)
Definition: minilua.c:519
#define luaL_optint(L, n, d)
Definition: minilua.c:5858
static void luaL_register(lua_State *L, const char *libname, const luaL_Reg *l)
Definition: minilua.c:5995
#define luaL_checkint(L, n)
Definition: minilua.c:5857
static int b_rrot(lua_State *L)
Definition: lbitlib.c:212
#define lua_unlock(L)
Definition: lbitlib.c:32
static int b_xor(lua_State *L)
Definition: lbitlib.c:138
#define lua_unsigned2number(u)
Definition: lbitlib.c:26
static int b_replace(lua_State *L)
Definition: lbitlib.c:243
static int b_or(lua_State *L)
Definition: lbitlib.c:128
unsigned int lua_Unsigned
Definition: lbitlib.c:19
#define ALLONES
Definition: lbitlib.c:86
static int b_and(lua_State *L)
Definition: lbitlib.c:113
#define trim(x)
Definition: lbitlib.c:89
static int b_extract(lua_State *L)
Definition: lbitlib.c:233
static int b_shift(lua_State *L, b_uint r, int i)
Definition: lbitlib.c:155
static int fieldargs(lua_State *L, int farg, int *width)
Definition: lbitlib.c:221
static int b_rshift(lua_State *L)
Definition: lbitlib.c:177
#define LUA_NBITS
Definition: lbitlib.c:82
static int b_not(lua_State *L)
Definition: lbitlib.c:148
#define lua_lock(L)
Definition: lbitlib.c:31
static int b_lshift(lua_State *L)
Definition: lbitlib.c:172
static const luaL_Reg bitlib[]
Definition: lbitlib.c:256
#define mask(n)
Definition: lbitlib.c:93
static int b_lrot(lua_State *L)
Definition: lbitlib.c:207
static int b_test(lua_State *L)
Definition: lbitlib.c:121
static int b_arshift(lua_State *L)
Definition: lbitlib.c:182
static int b_rot(lua_State *L, int i)
Definition: lbitlib.c:197
#define luaL_checkunsigned
Definition: lbitlib.c:75
lua_Unsigned b_uint
Definition: lbitlib.c:96
static b_uint andaux(lua_State *L)
Definition: lbitlib.c:100
void lua_pushunsigned(lua_State *L, lua_Unsigned u)
Definition: lbitlib.c:36
#define LJ_UNLIKELY(x)
Definition: lj_def.h:324
#define setnanV(o)
Definition: lj_obj.h:889
#define setnumV(o, x)
Definition: lj_obj.h:888
#define tvisnan(o)
Definition: lj_obj.h:770
int lua_gettop(lua_State *L)
Definition: lapi.c:167
void lua_pushboolean(lua_State *L, int b)
Definition: lapi.c:557
int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.c:223
#define luaL_argcheck(L, cond, arg, extramsg)
Definition: lauxlib.h:122
int luaopen_bit32(lua_State *L)
Definition: lbitlib.c:229
unsigned LUA_INTEGER lua_Unsigned
Definition: lua.h:95
double lua_Number
Definition: lua.h:88
#define LUA_API
Definition: luaconf.h:259
m
Definition: tex4ht.c:3990
@ L
Definition: ubidiimp.h:45